在 2026-10-2 21:00, Pali Rohár 写道:
On Friday 02 October 2026 20:44:36 LIU Hao wrote:
在 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.)

Well, if you think that it is fine to remove it then I have no problems.
I just wanted to be sure that no compiler reorder happens, so added
explicit compiler barrier.
I have pushed this patch with these changes:

diff --git a/mingw-w64-crt/misc/_invoke_watson.c 
b/mingw-w64-crt/misc/_invoke_watson.c
index 1ea3e6300..6bc1e2505 100644
--- a/mingw-w64-crt/misc/_invoke_watson.c
+++ b/mingw-w64-crt/misc/_invoke_watson.c
@@ -75,7 +75,6 @@ DECLSPEC_NOINLINE __MINGW_ATTRIB_NORETURN void __cdecl 
_invoke_watson(const wcha
     context.ContextFlags = CONTEXT_CONTROL;
 #else
     RtlCaptureContext(&context);
- asm volatile("" ::: "memory"); /* compiler barrier to prevent executing something before RtlCaptureContext() */
 #endif

     /* Define and fill all other variables after capturing registers. */
@@ -90,15 +89,11 @@ DECLSPEC_NOINLINE __MINGW_ATTRIB_NORETURN void __cdecl 
_invoke_watson(const wcha
 #if defined(__i386__)
     /*
      * 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.
+     * values of caller function: ebp, eip. Therefore ebp of the called
+     * function is dereferenced first value (index 0).
      */
context.Eip = (uintptr_t)__builtin_extract_return_addr(__builtin_return_address(0)); /* msvc uses _ReturnAddress() */ - context.Esp = (uintptr_t)__builtin_frame_address(0) + 2*sizeof(uintptr_t); /* msvc uses _AddressOfReturnAddress() */
+    context.Esp = (uintptr_t)&expression - 4; /* msvc uses 
_AddressOfReturnAddress() */
context.Ebp = ((uintptr_t *)__builtin_frame_address(0))[0]; /* msvc uses *((ULONG *)_AddressOfReturnAddress()-1) */
 #elif defined(__x86_64__)
 #if defined(SEH_INLINE_ASM)
@@ -124,9 +119,9 @@ DECLSPEC_NOINLINE __MINGW_ATTRIB_NORETURN void __cdecl 
_invoke_watson(const wcha
         /* If SEH unwind data table is not available then just fix RIP/RSP/RBP.
          * For gcc we can use __builtin_dwarf_cfa() which returns pointer to 
the
          * original rsp with value before caller issued "call" instruction to
-         * our function. Stack below this address contains: rip, rbp. Therefore
-         * rbp of the called function is derefenced second value in reverse
-         * order [-2] from the original rsp address __builtin_dwarf_cfa().
+         * our function. Like on x86, current stack frame returned by
+         * __builtin_frame_address(0) contains values of caller function: rbp,
+         * rip. Therefore rbp of the called function is dereferenced first 
value.
          * Note that msvc _AddressOfReturnAddress() is address of the rip value
          * which should be &(((uintptr_t *)__builtin_dwarf_cfa())[-1]) and
          * therefore below code for context.Rsp should match the msvc behavior.
@@ -136,16 +131,18 @@ DECLSPEC_NOINLINE __MINGW_ATTRIB_NORETURN void __cdecl 
_invoke_watson(const wcha
          * https://github.com/llvm/llvm-project/issues/227606
          * As a workaround calculate that address from the pointer of 5th
          * function argument which is passed on the stack above the 32 bytes
-         * of shadow space.
+         * of shadow space. __builtin_frame_address() has the same problem, so
+         * it is only used to force rbp to be saved right below the rip value.
          */
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;
+        asm volatile("" : : "r" (__builtin_frame_address(0)));
+        context.Rsp = (uintptr_t)&reserved - 32; /* msvc uses 
_AddressOfReturnAddress()+8 */
+        context.Rbp = ((uintptr_t *)context.Rsp)[-2]; /* not filled by msvc */
 #else
-        uintptr_t cfa = (uintptr_t)__builtin_dwarf_cfa();
+        context.Rsp = (uintptr_t)__builtin_dwarf_cfa(); /* msvc uses 
_AddressOfReturnAddress()+8 */
+        context.Rbp = ((uintptr_t *)__builtin_frame_address(0))[0]; /* not 
filled by msvc */
 #endif
-        context.Rsp = cfa; /* msvc uses _AddressOfReturnAddress()+8 */
-        context.Rbp = ((uintptr_t *)cfa)[-2]; /* not filled by msvc */
     }
 #endif






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