On Wednesday 30 September 2026 15:33:43 LIU Hao wrote:
> 在 2026-9-27 03:29, Pali Rohár 写道:
> > Hello, in the attachment I'm sending setjmp/longjmp fixes.
> > 
> > It fixes mingw-w64 _setjmp/_setjmp3/longjmp/_longjmpex implementations
> > for various CRT builds. And add new tests for verification.
> > diff --git a/mingw-w64-crt/misc/longjmp.S b/mingw-w64-crt/misc/longjmp.S
> > index a7f04d529b7f..5de492583376 100644
> > --- a/mingw-w64-crt/misc/longjmp.S
> > +++ b/mingw-w64-crt/misc/longjmp.S
> > @@ -27,6 +27,30 @@ __MINGW_USYMBOL(longjmp):
> >          movq    %rdx,%rax            /* retval */
> >          cmpq    $1,%rax              /* check that retval is non-zero */
> >          adcq    $0,%rax              /* if not then increase by one via CF 
> > */
> > +        cmpq    $0,(%rcx)            /* jmp_buf->Frame */
> > +        je      .Llongjmp_skip_seh
> > +        /* Logic of SEH longjmp implementation based on x86_64 wine 
> > longjmp code from:
> > +           
> > https://gitlab.winehq.org/wine/wine/-/blob/wine-10.20/dlls/msvcrt/except_x86_64.c#L94-110
> > +         */
> > +        pushq   %rcx
> > +        pushq   %rdx
> > +        subq    $192,(%rsp)             /* allocate 
> > roundup16(32+sizeof(EXCEPTION_RECORD)) */
> > +        movl    $0x80000026,0x20(%rsp)  /* EXCEPTION_RECORD.ExceptionCode 
> > = STATUS_LONGJUMP */
> > +        movl    $0,0x24(%rsp)           /* EXCEPTION_RECORD.ExceptionFlags 
> > = 0 */
> > +        movq    $0,0x28(%rsp)           /* 
> > EXCEPTION_RECORD.ExceptionRecord = NULL */
> > +        movq    $0,0x30(%rsp)           /* 
> > EXCEPTION_RECORD.ExceptionAddress = NULL */
> > +        movl    $1,0x38(%rsp)           /* 
> > EXCEPTION_RECORD.NumberParameters = 1 */
> > +        movq    %rcx,0x40(%rsp)         /* 
> > EXCEPTION_RECORD.ExceptionInformation[0] = jmp_buf */
> > +        movq    0x50(%rcx),%rdx         /* TargetIp = jmp_buf->Rip */
> > +        movq    (%rcx),%rcx             /* TargetFrame = jmp_buf->Frame */
> > +        leaq    0x20(%rsp),%r8          /* ExceptionRecord = 
> > &EXCEPTION_RECORD */
> > +        movq    %rax,%r9                /* ReturnValue = retval */
> > +        callq   *__MINGW_USYMBOL(__MINGW_IMP_SYMBOL(RtlUnwind))(%rip)
> > +        /* RtlUnwind should not return, if it happens then cleanup stack 
> > and continue */
> > +        addq    $192,(%rsp)
> > +        popq    %rdx
> > +        popq    %rcx
> > +        .Llongjmp_skip_seh:
> >          movq    0x8(%rcx),%rbx       /* jmp_buf->Rbx */
> >          movq    0x18(%rcx),%rbp      /* jmp_buf->Rbp */
> >          movq    0x20(%rcx),%rsi      /* jmp_buf->Rsi */
> 
> I don't know whether this works, but you should not call `RtlUnwind()` like
> that; it requires the function to have valid SEH unwind data, so instead of
> tampering with the stack pointer, allocation of stack storage should be
> moved to the beginning of the function, and be annotated with `.seh_`
> directives.

I have there a mistake, stack allocation needs to be "subq $184,%rsp".
I had there parenthesis (which is dereferencing) and forgot about fact
that the return address (of 8 bytes) is already on the stack and so
rounding to 16 bytes needs to take care of it.

With that change it seems to work. The RtlUnwind function jumps to the
%rdx address, so preserving %rdx and %rcx is not needed.

> 
> > diff --git a/mingw-w64-crt/misc/setjmp.S b/mingw-w64-crt/misc/setjmp.S
> > index ee194856046d..5173986b7837 100644
> > --- a/mingw-w64-crt/misc/setjmp.S
> > +++ b/mingw-w64-crt/misc/setjmp.S
> > @@ -35,8 +35,61 @@
> >  #if defined(__i386__)
> >  __MINGW_USYMBOL(_setjmp3):
> > +        pushl   4(%esp)
> > +        /* edx == -1 -> setjmp_common will NOT inspect SEH try_level */
> > +        /* edx != -1 -> setjmp_common will inspect SEH try_level */
> > +        /* Do not inspect SEH try_level when _Count >= 2 -> means that 
> > try_level is provided as _TryLevel param */
> > +        cmpl    $2,8(%esp)
> > +        setl    %dl
> > +        neg     %edx
> 
> `setl` doesn't set `edx`; it only sets `dl`. Using unsigned arithmetic, this
> can be implemented as cmp+sbb+not.

Good catch. I will change it to this (and hopefully it is correct now):

cmpl    $2,8(%esp)
setge   %dl
movzbl  %dl,%edx
neg     %edx

> > +        cmpl    $0,%eax
> > +        je      .L_setjmp3_end
> 
> This could be `test %eax, %eax`.

Ok.

> > +        cmpl    $6,%eax
> > +        ja      .L_setjmp3_copy_unwind_data
> > +        movl    $6,%eax              /* array size of jmp_buf.UnwindData[] 
> > is 6 */
> > +        .L_setjmp3_copy_unwind_data:
> 
> Looks like `ja` should be `jbe`?

Yes, looks like that. I will change it.

> This is one of the reasons why you should never ever want AT&T syntax.

Personally I do not like neither Intel, nor AT&T. I'm always doing
mistake in both of them because somehow I have fixing them together.

> 
> >  __MINGW_USYMBOL(__intrinsic_setjmp):
> >  __MINGW_USYMBOL(_setjmp):
> > +        movl    $0,%edx              /* edx != -1 -> setjmp_common will 
> > inspect SEH try_level */
> 
> And this could be `xor %edx, %edx`.

Ok.


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

Reply via email to