From cff69781e13a29d03e972bc50ec9ee0350830a3f Mon Sep 17 00:00:00 2001 From: Thays Grazia Date: Mon, 31 Mar 2025 15:10:06 -0300 Subject: [PATCH 1/3] Fixing get incomplete context information and assigning invalid information to the context later. --- src/coreclr/debug/di/process.cpp | 6 +++++- src/coreclr/debug/di/rsthread.cpp | 13 ++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/coreclr/debug/di/process.cpp b/src/coreclr/debug/di/process.cpp index f12b6166019a04..d5bdf5a6b51508 100644 --- a/src/coreclr/debug/di/process.cpp +++ b/src/coreclr/debug/di/process.cpp @@ -12676,7 +12676,11 @@ Reaction CordbProcess::TriageExcep1stChanceAndInit(CordbUnmanagedThread * pUnman DT_CONTEXT context; - context.ContextFlags = DT_CONTEXT_FULL; +#ifdef TARGET_X86 + tempContext.ContextFlags = DT_CONTEXT_FULL | DT_CONTEXT_EXTENDED_REGISTERS; +#else + tempContext.ContextFlags = DT_CONTEXT_FULL; +#endif BOOL fSuccess = DbiGetThreadContext(pUnmanagedThread->m_handle, &context); diff --git a/src/coreclr/debug/di/rsthread.cpp b/src/coreclr/debug/di/rsthread.cpp index 619b37c87ef8b8..478a637c4087f0 100644 --- a/src/coreclr/debug/di/rsthread.cpp +++ b/src/coreclr/debug/di/rsthread.cpp @@ -3721,9 +3721,15 @@ HRESULT CordbUnmanagedThread::SetupFirstChanceHijackForSync() LOG((LF_CORDB, LL_INFO10000, "CUT::SFCHFS: hijackCtx started as:\n")); LogContext(GetHijackCtx()); - // Save the thread's full context. + // Save the thread's full context for all platforms except for x86 because we need the + // DT_CONTEXT_EXTENDED_REGISTERS to avoid getting incomplete information and corrupt the thread context DT_CONTEXT context; +#ifdef TARGET_X86 + context.ContextFlags = DT_CONTEXT_FULL | DT_CONTEXT_EXTENDED_REGISTERS;; +#else context.ContextFlags = DT_CONTEXT_FULL; +#endif + BOOL succ = DbiGetThreadContext(m_handle, &context); _ASSERTE(succ); // for debugging when GetThreadContext fails @@ -3733,7 +3739,12 @@ HRESULT CordbUnmanagedThread::SetupFirstChanceHijackForSync() LOG((LF_CORDB, LL_ERROR, "CUT::SFCHFS: DbiGetThreadContext error=0x%x\n", error)); } +#ifdef TARGET_X86 + GetHijackCtx()->ContextFlags = DT_CONTEXT_FULL | DT_CONTEXT_EXTENDED_REGISTERS; +#else GetHijackCtx()->ContextFlags = DT_CONTEXT_FULL; +#endif + CORDbgCopyThreadContext(GetHijackCtx(), &context); LOG((LF_CORDB, LL_INFO10000, "CUT::SFCHFS: thread=0x%x Hijacking for sync. Original context is:\n", this)); LogContext(GetHijackCtx()); From a3133a0633e61ddd9f096db4ad7e9fa98ba4cbf9 Mon Sep 17 00:00:00 2001 From: Thays Grazia Date: Mon, 31 Mar 2025 15:21:24 -0300 Subject: [PATCH 2/3] Fixing the copy and paste. --- src/coreclr/debug/di/process.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/coreclr/debug/di/process.cpp b/src/coreclr/debug/di/process.cpp index d5bdf5a6b51508..6d82de9b5cdf2a 100644 --- a/src/coreclr/debug/di/process.cpp +++ b/src/coreclr/debug/di/process.cpp @@ -12676,11 +12676,7 @@ Reaction CordbProcess::TriageExcep1stChanceAndInit(CordbUnmanagedThread * pUnman DT_CONTEXT context; -#ifdef TARGET_X86 - tempContext.ContextFlags = DT_CONTEXT_FULL | DT_CONTEXT_EXTENDED_REGISTERS; -#else - tempContext.ContextFlags = DT_CONTEXT_FULL; -#endif + context.ContextFlags = DT_CONTEXT_FULL; BOOL fSuccess = DbiGetThreadContext(pUnmanagedThread->m_handle, &context); @@ -13381,7 +13377,11 @@ void CordbProcess::HandleDebugEventForInteropDebugging(const DEBUG_EVENT * pEven { LOG((LF_CORDB, LL_INFO100000, "W32ET::W32EL: hijack complete will restore context...\n")); DT_CONTEXT tempContext = { 0 }; +#ifdef TARGET_X86 + tempContext.ContextFlags = DT_CONTEXT_FULL | DT_CONTEXT_EXTENDED_REGISTERS; +#else tempContext.ContextFlags = DT_CONTEXT_FULL; +#endif HRESULT hr = pUnmanagedThread->GetThreadContext(&tempContext); _ASSERTE(SUCCEEDED(hr)); From 253d038cad6f9ebb06442b31426c8f072e18f867 Mon Sep 17 00:00:00 2001 From: Thays Grazia Date: Mon, 31 Mar 2025 15:26:42 -0300 Subject: [PATCH 3/3] remove extra ; --- src/coreclr/debug/di/rsthread.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/coreclr/debug/di/rsthread.cpp b/src/coreclr/debug/di/rsthread.cpp index 478a637c4087f0..cd7f79867a54d3 100644 --- a/src/coreclr/debug/di/rsthread.cpp +++ b/src/coreclr/debug/di/rsthread.cpp @@ -3725,7 +3725,7 @@ HRESULT CordbUnmanagedThread::SetupFirstChanceHijackForSync() // DT_CONTEXT_EXTENDED_REGISTERS to avoid getting incomplete information and corrupt the thread context DT_CONTEXT context; #ifdef TARGET_X86 - context.ContextFlags = DT_CONTEXT_FULL | DT_CONTEXT_EXTENDED_REGISTERS;; + context.ContextFlags = DT_CONTEXT_FULL | DT_CONTEXT_EXTENDED_REGISTERS; #else context.ContextFlags = DT_CONTEXT_FULL; #endif