Skip to content

Commit 5130645

Browse files
authored
null-check the redirect context before using. (#65910)
* null-check the redirect context before using. * tweak the comment * do not allocate context if InitializeContext has unexpected results.
1 parent b40b31a commit 5130645

1 file changed

Lines changed: 15 additions & 4 deletions

File tree

src/coreclr/vm/threadsuspend.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1983,9 +1983,14 @@ CONTEXT* AllocateOSContextHelper(BYTE** contextBuffer)
19831983
pfnInitializeContext2(NULL, context, NULL, &contextSize, xStateCompactionMask) :
19841984
InitializeContext(NULL, context, NULL, &contextSize);
19851985

1986-
// The following assert is valid, but gets triggered in some Win7 runs with no impact on functionality.
1987-
// commenting this out to reduce noise, as long as Win7 is supported.
1988-
// _ASSERTE(!success && GetLastError() == ERROR_INSUFFICIENT_BUFFER);
1986+
// Spec mentions that we may get a different error (it was observed on Windows7).
1987+
// In such case the contextSize is undefined.
1988+
if (success || GetLastError() != ERROR_INSUFFICIENT_BUFFER)
1989+
{
1990+
STRESS_LOG2(LF_SYNC, LL_INFO1000, "AllocateOSContextHelper: Unexpected result from InitializeContext (success: %d, error: %d).\n",
1991+
success, GetLastError());
1992+
return NULL;
1993+
}
19891994

19901995
// So now allocate a buffer of that size and call InitializeContext again
19911996
BYTE* buffer = new (nothrow)BYTE[contextSize];
@@ -2896,9 +2901,15 @@ BOOL Thread::RedirectThreadAtHandledJITCase(PFN_REDIRECTTARGET pTgt)
28962901
if (!pCtx)
28972902
{
28982903
pCtx = m_pSavedRedirectContext = ThreadStore::GrabOSContext(&m_pOSContextBuffer);
2899-
_ASSERTE(GetSavedRedirectContext() != NULL);
29002904
}
29012905

2906+
// We may not have a preallocated context. Could be short on memory when we tried to preallocate.
2907+
// We cannot allocate here since we have a thread stopped in a random place, possibly holding locks
2908+
// that we would need while allocating.
2909+
// Other ways and attempts at suspending may yet succeed, but this redirection cannot continue.
2910+
if (!pCtx)
2911+
return (FALSE);
2912+
29022913
//////////////////////////////////////
29032914
// Get and save the thread's context
29042915
BOOL bRes = true;

0 commit comments

Comments
 (0)