在 2026-10-2 20:32, Pali Rohár 写道:
On Friday 02 October 2026 20:22:05 LIU Hao wrote:
在 2026-10-2 17:43, Pali Rohár 写道:
Capture CONTEXT registers at the beginning of the function with explicit
compiler memory barrier to prevent compiler from clobbering registers.

Fix filling of Esp/Rsp and Ebp/Rbp registers.

Add explaining comments for most of the function logic.
---
   mingw-w64-crt/misc/_invoke_watson.c | 102 ++++++++++++++++++++++------
   1 file changed, 81 insertions(+), 21 deletions(-)


@@ -71,34 +70,95 @@ DECLSPEC_NOINLINE __MINGW_ATTRIB_NORETURN void __cdecl 
_invoke_watson(const wcha
           "=m" (context.SegEs), "=m" (context.SegFs), "=m" (context.SegGs),
           "=m" (context.EFlags)
         :
-      :
+      : "memory" /* compiler barrier */
       );
+    context.ContextFlags = CONTEXT_CONTROL;
   #else
       RtlCaptureContext(&context);
+    asm volatile("" ::: "memory"); /* compiler barrier to prevent executing 
something before RtlCaptureContext() */
I think this isn't necessary; `RtlCaptureContext()` already writes to memory
and it has the same effect as a compiler barrier.

I was thinking about it... But what if the function is somehow inclined
via LTO and compiler figures out that function does not have side
effects and only writes to the &context memory?

It's a dllimport function, so such transformation is not allowed.

(I'm aware of the fake function in winstorecompat, which just fills the CONTEXT structure with zeros so it probably doesn't matter.)




+
+    /* Define and fill all other variables after capturing registers. */
+    EXCEPTION_RECORD exception_record = { 0, };
+    EXCEPTION_POINTERS exception_pointers = { &exception_record, &context };
+#if defined(__x86_64__)
+    ULONG64 image_base;
+    PRUNTIME_FUNCTION function_entry;
   #endif
       /* Fill additional platform specific fields of the parent (caller) 
function into context. */
   #if defined(__i386__)
-    context.ContextFlags = CONTEXT_CONTROL;
+    /*
+     * Current stack frame returned by __builtin_frame_address(0) contains
+     * values of caller function: ebp, eip. Therefore esp of the called
+     * function is address of stack frame plus size of those 2 registers and
+     * ebp of called function is dereferenced first value (indexed from 1).
+     * Note that msvc _AddressOfReturnAddress() is address of the eip value
+     * which should be &(((uintptr_t *)__builtin_frame_address(0))[1]) and
+     * therefore below code for context.Esp/Ebp should match the msvc behavior.
+     */
       context.Eip = 
(uintptr_t)__builtin_extract_return_addr(__builtin_return_address(0)); /* msvc 
uses _ReturnAddress() */
-    context.Esp = (uintptr_t)__builtin_frame_address(0); /* msvc uses 
_AddressOfReturnAddress() */
-    context.Ebp = *((uintptr_t *)__builtin_frame_address(0)-1); /* msvc uses 
*((ULONG *)_AddressOfReturnAddress()-1) */
+    context.Esp = (uintptr_t)__builtin_frame_address(0) + 2*sizeof(uintptr_t); 
/* msvc uses _AddressOfReturnAddress() */

Can this be just `(uintptr_t) &expression`?

I do not think. (uintptr_t)&expression points to the first parameter on
the stack and _AddressOfReturnAddress() points to the return address on
the stack.


Ugh, it looks like for MSVC it includes the return address, and should be 
`(uintptr_t) &expression - 4`.



--
Best regards,
LIU Hao

Attachment: OpenPGP_signature.asc
Description: OpenPGP digital signature

_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to