You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
CopyContext in Thread::RedirectCurrentThreadAtHandledJITCase sometimes fails #65776
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.
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:
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.
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.
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.
ghost added
in-prThere is an active PR which will close this issue when it is merged
Activity
janvorli commentedon Feb 23, 2022
cc: @VSadov
VSadov commentedon Feb 23, 2022
Is this on x86?
janvorli commentedon Feb 23, 2022
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:
jkotas commentedon Feb 23, 2022
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.
I think this means that something on the startup path (e.g. some vectorized path in CRT) used the extended context.
VSadov commentedon Feb 23, 2022
I saw
XSTATE_MASK_MPXand 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.
VSadov commentedon Feb 23, 2022
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.