Skip to content

CopyContext in Thread::RedirectCurrentThreadAtHandledJITCase sometimes fails #65776

Closed

Description

@janvorli

I have noticed that on my machine the CopyContext call in the Thread::RedirectCurrentThreadAtHandledJITCase sometimes fails with the ERROR_INSUFFICIENT_BUFFER. I have debugged it and found the culprit. The issue is that the target context that we've just allocated in AllocateOSContextHelper doesn't have enough space in the XState due to the fact that we set too small XState compaction mask in the InitializeContext2 call. We set just XSTATE_MASK_LEGACY | XSTATE_MASK_AVX, but the source context that we get from the vectored exception handler was created with XSTATE_MASK_LEGACY | XSTATE_MASK_AVX | XSTATE_MASK_AVX512 | XSTATE_MASK_MPX (in other words, the masks returned by the GetEnabledXStateFeatures(). While the CopyContext can copy only parts of the legacy context based on the context flags, it obviously cannot copy only part of the xstate from the source context.

I am not sure what makes OS to call the vectored exception handler with this full AVX state only in some cases, because I hit this problem only with one of the coreclr tests and only when tiered compilation is disabled.

It seems that we need to pass the result of the GetEnabledXStateFeatures() as xStateCompactionMask to the InitializeContext2.

Activity

  1. added this to the 7.0.0 milestone on Feb 23, 2022
  2. ghost added
    untriaged
    on Feb 23, 2022
  3. janvorli commented on Feb 23, 2022

    @janvorli
    MemberAuthor
  4. removed
    untriaged
    on Feb 23, 2022
  5. VSadov commented on Feb 23, 2022

    @VSadov
    Member

    Is this on x86?

  6. janvorli commented on Feb 23, 2022

    @janvorli
    MemberAuthor

    I have hit it on x64. In fact, not just locally, but even in the CI running on my PR yesterday. I've forgotten to mention that the test is running with GC stress C and it is the jit\Regression\JitBlue\Runtime_45090. It seems that somehow my change triggers that behavior and that it would happen in every app with GC stress C and tiered jitting disabled, since it happens during runtime initialization:

    0:000> k
     # Child-SP          RetAddr           Call Site
    00 00000098`e777c220 00007fff`d322e33c coreclr!Thread::RedirectCurrentThreadAtHandledJITCase+0x34f [F:\git\runtime4\src\coreclr\vm\threadsuspend.cpp @ 3030] 
    01 00000098`e777c350 00007fff`d2ffb4d8 coreclr!Thread::CheckForAndDoRedirectForGCStress+0xec [F:\git\runtime4\src\coreclr\vm\threadsuspend.cpp @ 3144] 
    02 00000098`e777c430 00007fff`d2c53e34 coreclr!OnGcCoverageInterrupt+0x338 [F:\git\runtime4\src\coreclr\vm\gccover.cpp @ 1320] 
    03 00000098`e777c590 00007fff`d2c401ae coreclr!IsGcMarker+0x134 [F:\git\runtime4\src\coreclr\vm\excep.cpp @ 6509] 
    04 00000098`e777c690 00007ff8`b491c13a coreclr!CLRVectoredExceptionHandlerShim+0x14e [F:\git\runtime4\src\coreclr\vm\excep.cpp @ 7941] 
    05 00000098`e777c790 00007ff8`b48d58f2 ntdll!RtlpCallVectoredHandlers+0x112 [minkernel\ntdll\vectxcpt.c @ 204] 
    06 (Inline Function) --------`-------- ntdll!RtlCallVectoredExceptionHandlers+0xe [minkernel\ntdll\vectxcpt.c @ 358] 
    07 00000098`e777c830 00007ff8`b49476fe ntdll!RtlDispatchException+0x62 [minkernel\ntos\rtl\amd64\exdsptch.c @ 390] 
    08 00000098`e777ca80 00007fff`7472070d ntdll!KiUserExceptionDispatch+0x2e [minkernel\ntos\rtl\amd64\trampoln.asm @ 751] 
    09 00000098`e777d850 00007fff`ee8481db System_Private_CoreLib!System.SpanHelpers.IndexOf(Char ByRef, Char, Int32)+0x4d [F:\git\runtime4\src\libraries\System.Private.CoreLib\src\System\SpanHelpers.Char.cs @ 512] 
    0a 00000098`e777deb0 00007fff`ee8574cc System_Private_CoreLib!System.String.Ctor(Char*)+0x2b [F:\git\runtime4\src\libraries\System.Private.CoreLib\src\System\String.cs @ 118] 
    0b 00000098`e777def0 00007fff`d34ff1c3 System_Private_CoreLib!System.AppContext.Setup(Char**, Char**, Int32)+0x8c [F:\git\runtime4\src\libraries\System.Private.CoreLib\src\System\AppContext.cs @ 139] 
    0c 00000098`e777df60 00007fff`d2f1c25e coreclr!CallDescrWorkerInternal+0x83
    0d 00000098`e777dfa0 00007fff`d2f1ceb0 coreclr!CallDescrWorkerWithHandler+0x12e [F:\git\runtime4\src\coreclr\vm\callhelpers.cpp @ 70] 
    0e 00000098`e777e000 00007fff`d2a90704 coreclr!MethodDescCallSite::CallTargetWorker+0xc40 [F:\git\runtime4\src\coreclr\vm\callhelpers.cpp @ 541] 
    0f 00000098`e777e860 00007fff`d2be2d5b coreclr!MethodDescCallSite::Call+0x24 [F:\git\runtime4\src\coreclr\vm\callhelpers.h @ 458] 
    10 00000098`e777e890 00007fff`d3826363 coreclr!CorHost2::CreateAppDomainWithManager+0x55b [F:\git\runtime4\src\coreclr\vm\corhost.cpp @ 630] 
    11 00000098`e777ee00 00007ff7`2c6e9f8f coreclr!coreclr_initialize+0x433 [F:\git\runtime4\src\coreclr\dlls\mscoree\unixinterface.cpp @ 254] 
    12 00000098`e777f000 00007ff7`2c6ef4da corerun!run+0xcff [F:\git\runtime4\src\coreclr\hosts\corerun\corerun.cpp @ 348] 
    13 00000098`e777fa10 00007ff7`2c7002a9 corerun!wmain+0x12a [F:\git\runtime4\src\coreclr\hosts\corerun\corerun.cpp @ 563] 
    14 00000098`e777fb70 00007ff7`2c70014e corerun!invoke_main+0x39 [d:\a01\_work\43\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl @ 91] 
    15 00000098`e777fbc0 00007ff7`2c70000e corerun!__scrt_common_main_seh+0x12e [d:\a01\_work\43\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl @ 288] 
    16 00000098`e777fc30 00007ff7`2c70033e corerun!__scrt_common_main+0xe [d:\a01\_work\43\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl @ 331] 
    17 00000098`e777fc60 00007ff8`b37854e0 corerun!wmainCRTStartup+0xe [d:\a01\_work\43\s\src\vctools\crt\vcstartup\src\startup\exe_wmain.cpp @ 17] 
    18 00000098`e777fc90 00007ff8`b48a485b KERNEL32!BaseThreadInitThunk+0x10 [clientcore\base\win32\client\thread.c @ 75] 
    19 00000098`e777fcc0 00000000`00000000 ntdll!RtlUserThreadStart+0x2b [minkernel\ntdll\rtlstrt.c @ 1152] 
    
  7. jkotas commented on Feb 23, 2022

    @jkotas
    Member

    I am not sure what makes OS to call the vectored exception handler with this full AVX state only in some cases

    The OS is trying to optimize saving/restoring of the context. The processor has bits to track whether particular part of the context was used. It allows OS to preserve only parts of the context that are actually used.

    it happens during runtime initialization

    I think this means that something on the startup path (e.g. some vectorized path in CRT) used the extended context.

  8. VSadov commented on Feb 23, 2022

    @VSadov
    Member

    I saw XSTATE_MASK_MPX and thought - x86 should not have that, so it must be x64, but why are we dealing with a state from an exception context on x64?
    Yes, GC stress - that explains it.

  9. VSadov commented on Feb 23, 2022

    @VSadov
    Member

    I think a quick fix would be just returning FALSE when the copy did not work. If a quick fix is needed.
    This helper is used only for GC stress and GC stress expects that sometime redirection may not work.

  10. ghost added
    in-pr
    on Feb 24, 2022
  11. ghost removed
    in-pr
    on Feb 25, 2022
  12. ghost locked as resolved and limited conversation to collaborators on Mar 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions