Use CopyContext to restore saved context on X86 (#65490)
authorVladimir Sadov <vsadov@microsoft.com>
Fri, 18 Feb 2022 07:09:20 +0000 (23:09 -0800)
committerGitHub <noreply@github.com>
Fri, 18 Feb 2022 07:09:20 +0000 (23:09 -0800)
* Use CopyContext to restore saved context on X86

* PR feedback

* more PR feedback

src/coreclr/vm/threadsuspend.cpp

index b65cd74..3c6f46b 100644 (file)
@@ -2509,6 +2509,7 @@ void RedirectedThreadFrame::ExceptionUnwind()
 #ifndef TARGET_UNIX
 
 #ifdef TARGET_X86
+
 //****************************************************************************************
 // This will check who caused the exception.  If it was caused by the the redirect function,
 // the reason is to resume the thread back at the point it was redirected in the first
@@ -2577,7 +2578,15 @@ int RedirectedHandledJITCaseExceptionFilter(
     pFrame->Pop();
 
     // Copy the saved context record into the EH context;
-    ReplaceExceptionContextRecord(pExcepPtrs->ContextRecord, pCtx);
+    // NB: cannot use ReplaceExceptionContextRecord here.
+    //     these contexts may contain extended registers and may have different format
+    //     for reasons such as alignment or context compaction
+    CONTEXT* pTarget = pExcepPtrs->ContextRecord;
+    if (!CopyContext(pTarget, pTarget->ContextFlags, pCtx))
+    {
+        STRESS_LOG1(LF_SYNC, LL_ERROR, "ERROR: Could not set context record, lastError = 0x%x\n", GetLastError());
+        EEPOLICY_HANDLE_FATAL_ERROR(COR_E_EXECUTIONENGINE);
+    }
 
     DWORD espValue = pCtx->Esp;