Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop relying on UB in 'IContextCallback' dispatch logic #1865

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

Sergio0694
Copy link
Member

See https://github.com/dotnet/runtime/blob/main/docs/design/specs/Memory-model.md#cross-thread-access-to-local-variables. This PR fixes accessing the callback state (which is a managed object) from the native callback invoked on the target thread.

@Sergio0694 Sergio0694 added the gc Related to garbage collection label Nov 9, 2024
@Sergio0694 Sergio0694 requested a review from manodasanW November 9, 2024 23:03
@jkotas
Copy link
Member

jkotas commented Nov 10, 2024

Fix a GC hole

Nit: The potential undefined behavior that this is fixing does not fit the definition of GC hole (https://github.com/dotnet/runtime/blob/main/docs/coding-guidelines/clr-code-guide.md#211-how-gc-holes-are-created).

}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static CallbackData GetOrCreate()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure that there cannot ever be recursion that would cause multiple recursive uses of CallbackData on the same thread?

Calls between STA apartments always come with a lot of surprises and corner-case bugs...

Copy link
Member Author

@Sergio0694 Sergio0694 Nov 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Applied the suggested changes and also left some notes about reentrancy. I talked with @manodasanW as well and in this scenario this should be fine. We're only ever doing this context switch to release an object reference on its original context, and nothing else. There is no way that should/could then somehow switch back to this starting thread (it wouldn't even know which one that was) to then try to schedule another context callback recursively. So this should be fine. Also in ASTA scenarios this would also just be flat out impossible by construction (because ASTA blocks reentrant calls into it, so even if hypothetically we somehow ended up there, that second context callback would just throw before being scheduled). But then again either case should just never happen. Definitely a good point though and worth writing some additional comments about this, thank you! 😄

src/WinRT.Runtime/Interop/IContextCallback.cs Outdated Show resolved Hide resolved
@Sergio0694 Sergio0694 changed the title Fix a GC hole in 'IContextCallback' dispatch logic Stop relying on UB in 'IContextCallback' dispatch logic Nov 10, 2024

// We use a thread local static field to efficiently store the state that's used by the callback. Note that this
// is safe with respect to reentrancy, as the target callback will never try to switch back on the original thread.
// We're only ever switching once on the original context, only to release the object reference that is passed as
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Release can run arbitrary code, is that right? I do not see what guarantees that the arbitrary code cannot switch threads at will.

Also, this seems to be used for more than just releasing the object reference:

Context.CallInContext(
_contextCallbackPtr,
_contextToken,
#if NET && CsWinRT_LANG_11_FEATURES
&InitAgileReference,
#else
InitAgileReference,
#endif
null,
this);

It would be more correct by construction to implement this as usual pool - clear the cached instance when it is rented, so that there is no way for one instance to be rented multiple times at the same time.

Copy link

@hamarb123 hamarb123 Nov 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would have assumed that IContextCallback::ContextCallback with ICallbackWithNoReentrancyToApplicationSTA means it shouldn't be able to run any arbitrary code on this thread as a result of that call (I'm not familiar with these APIs though, so I don't actually know), and nothing else from when we set the field to when we clear it seems problematic. If that thing doesn't guarantee this however, then something should be done to allow multiple current things at once potentially existing (e.g., using a List<object> or similar (probably manually via array for resizing purposes) would do the job).

Copy link

@hamarb123 hamarb123 Nov 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or do you mean the callback might call back into this thread & somehow end up back here? That would make sense I suppose. Is that possible @Sergio0694? Actually, I think in this case it would still be fine, as long as the callback is called before anything else silly happens, since it immediately reads the field & thus wouldn't get the new field value (assuming appropriate barrier or whatever - for which a volatile read would certainly be enough, but probably none is "needed"); then it would just be set to null twice, but the meaningful value of the field would already be read & that version would have already been read & had the correct value.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"as long as the callback is called before anything else silly happens, since it immediately reads the field

That's actually a good point. Even if we somehow recursively ended up here (which I don't believe is possible), each callback would've already read the target state before invoking the user-provided callback anyway.

"Release can run arbitrary code"

Not really, I mean yes from the point of view of this API, but the context callback is only ever used internally to release IObjectReference objects, which are implemented in CsWinRT only. And we're only ever using these to pass the Release pointers which simply do a release on the tracker ref and the native object on the target context:

static void Release(object state)
{
ObjectReferenceWithContext<T> @this = Unsafe.As<ObjectReferenceWithContext<T>>(state);
@this.ReleaseFromBase();
}
static void ReleaseWithoutContext(object state)
{
ObjectReferenceWithContext<T> @this = Unsafe.As<ObjectReferenceWithContext<T>>(state);
@this.ReleaseWithoutContext();
}

@Sergio0694 Sergio0694 force-pushed the user/sergiopedri/fix-context-gchole branch from 5af3fc4 to 7dd4c5d Compare November 11, 2024 02:21
Copy link

@hamarb123 hamarb123 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks fine now (assuming we don't need volatile read in the callback), assuming it's not possible to come back into ContextCallback while another one is executing on the same thread.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
gc Related to garbage collection
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants