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

[NativeAOT] Allow reverse pinvoke in DoNotTriggerGc thread state regardless of coop mode. #85435

Merged
merged 6 commits into from
Apr 28, 2023

Conversation

VSadov
Copy link
Member

@VSadov VSadov commented Apr 27, 2023

Fixes: #85377

GC makes no promises about coop mode when GC callouts are invoked.
Background GC, for example, may invoke callouts in preemptive mode. Foreground GC with WKS will do it in coop.

@ghost
Copy link

ghost commented Apr 27, 2023

Tagging subscribers to this area: @agocke, @MichalStrehovsky, @jkotas
See info in area-owners.md if you want to be subscribed.

Issue Details

Fixes: #85377

GC makes no promises about coop mode when GC callouts are invoked.
Background GC, for example, may invoke callouts in preemptive mode.

Author: VSadov
Assignees: -
Labels:

area-NativeAOT-coreclr

Milestone: -

@@ -1133,16 +1133,19 @@ FORCEINLINE bool Thread::InlineTryFastReversePInvoke(ReversePInvokeFrame * pFram
// a do not trigger mode. The exception to the rule allows us to have [UnmanagedCallersOnly] methods that are called via
// the "restricted GC callouts" as well as from native, which is necessary because the methods are CCW vtable
// methods on interfaces passed to native.
if (IsCurrentThreadInCooperativeMode())
// We will allow threads in DoNotTriggerGc mode to do reverse PInvoke regardless of their coop state.
if (IsDoNotTriggerGcSet())
Copy link
Member

Choose a reason for hiding this comment

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

I think we want to do this before the TSF_Attached check. We do not want to attach untached GC threads as a side-effect.

Copy link
Member Author

Choose a reason for hiding this comment

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

Right, on server GC this could be unattached thread.

Copy link
Member Author

Choose a reason for hiding this comment

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

is there a way to pass ServerGarbageCollection=true to src\tests\build.cmd ?

Copy link
Member

Choose a reason for hiding this comment

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

You can set DOTNET_gcServer=1

Copy link
Member Author

Choose a reason for hiding this comment

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

Turns out src\tests\build.cmd can pass through paramters to msbuild, but src/tests/build.proj was not propagating some parameters that could be useful.

ASSERT(ThreadStore::IsTrapThreadsRequested());
// make transition to coop mode without waiting for GC (noop if already in coop mode)
pFrame->m_savedPInvokeTransitionFrame = m_pTransitionFrame;
m_pTransitionFrame = NULL;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
m_pTransitionFrame = NULL;

This should not be need.

// We expect this scenario only when EE is stopped.
ASSERT(ThreadStore::IsTrapThreadsRequested());
// make transition to coop mode without waiting for GC (noop if already in coop mode)
pFrame->m_savedPInvokeTransitionFrame = m_pTransitionFrame;
Copy link
Member

Choose a reason for hiding this comment

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

We can do this as very first thing in the method to avoid duplication. It will make the code a bit smaller.

@@ -581,6 +581,8 @@
<GroupBuildCmd>$(GroupBuildCmd) "/p:PackageOS=$(PackageOS)"</GroupBuildCmd>
<GroupBuildCmd>$(GroupBuildCmd) "/p:RuntimeFlavor=$(RuntimeFlavor)"</GroupBuildCmd>
<GroupBuildCmd>$(GroupBuildCmd) "/p:RuntimeVariant=$(RuntimeVariant)"</GroupBuildCmd>
<GroupBuildCmd>$(GroupBuildCmd) "/p:ServerGarbageCollection=$(ServerGarbageCollection)"</GroupBuildCmd>
<GroupBuildCmd>$(GroupBuildCmd) "/p:StripSymbols=$(StripSymbols)"</GroupBuildCmd>
Copy link
Member

Choose a reason for hiding this comment

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

Looks like this makes the dwarf warnings test to fail

Copy link
Member Author

Choose a reason for hiding this comment

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

Something up the stack sets StripSymbols and once propagated it breaks the test.
StripSymbols is a very useful setting if someone needs to debug tests, but we can deal with that later.

Copy link
Member

@jkotas jkotas left a comment

Choose a reason for hiding this comment

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

Thanks!

@VSadov
Copy link
Member Author

VSadov commented Apr 28, 2023

Thanks!

@VSadov VSadov merged commit c04933d into dotnet:main Apr 28, 2023
@VSadov VSadov deleted the Fix85377 branch April 28, 2023 13:32
@ghost ghost locked as resolved and limited conversation to collaborators May 28, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ReversePInvoke transition should be no-op in DoNotTriggerGc region on NativeAOT
2 participants