在 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.


+
+    /* 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`?


+    context.Ebp = ((uintptr_t *)__builtin_frame_address(0))[0]; /* msvc uses 
*((ULONG *)_AddressOfReturnAddress()-1) */
  #elif defined(__x86_64__)
... ...
+        context.Rip = 
(uintptr_t)__builtin_extract_return_addr(__builtin_return_address(0)); /* msvc 
uses _ReturnAddress() */
+#if defined(__clang__)
+        uintptr_t cfa = (uintptr_t)&reserved - 32;
+#else
+        uintptr_t cfa = (uintptr_t)__builtin_dwarf_cfa();
+#endif
+        context.Rsp = cfa; /* msvc uses _AddressOfReturnAddress()+8 */
+        context.Rbp = ((uintptr_t *)cfa)[-2]; /* not filled by msvc */

This code path for x86-64 no longer contains a call to `__builtin_frame_address()`, so it's unlikely that there will be a frame pointer. It is probably better to reuse `((uintptr_t *)__builtin_frame_address(0))[0]`.



--
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