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.
>
> >
> > >
> > > > +
> > > > + /* 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`.
Yes, with -4 it should be same. If I understood correctly then the Esp
context member should be set to the "esp" register value at the time
when function entered its address (before prologue started execution).
So at this time the "esp" register points to the address where is stored
return address.
We are again missing __builtin_sponentry() (on x86) which should return
this value and which is available on arm (but not on x86 or amd64).
_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public