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.
Kirill run CI testing on github: https://github.com/maiddaisuki/mingw-w64/actions/runs/36254715839
>From 4d18968d267b340f3637a7d83691235cc4a67f17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pali=20Roh=C3=A1r?= <[email protected]> Date: Sun, 1 Feb 2026 14:01:33 +0100 Subject: [PATCH 01/12] crt: Fill all i386 jmp_buf members in mingw-w64 _setjmp() function Inspect current SEH record and its try level, like the real msvcrt/UCRT _setjmp() function. --- mingw-w64-crt/misc/setjmp.S | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/mingw-w64-crt/misc/setjmp.S b/mingw-w64-crt/misc/setjmp.S index a2f1dc0bb61d..ee194856046d 100644 --- a/mingw-w64-crt/misc/setjmp.S +++ b/mingw-w64-crt/misc/setjmp.S @@ -45,6 +45,14 @@ __MINGW_USYMBOL(_setjmp): movl %esp,16(%ecx) /* jmp_buf.Esp */ movl 0(%esp),%eax movl %eax,20(%ecx) /* jmp_buf.Eip */ + movl %fs:0,%eax /* current SEH record */ + movl %eax,24(%ecx) /* jmp_buf.Registration */ + movl $-1,%edx + cmpl $-1,%eax /* SEH is top level -> do not inspect SEH try_level */ + je .L_setjmp_skip_SEH_try_level + movl 12(%eax),%edx + .L_setjmp_skip_SEH_try_level: + movl %edx,28(%ecx) /* jmp_buf.TryLevel */ xorl %eax, %eax retl -- 2.20.1 >From 426d97023c8625b359b09f04f0d505ba4a2572b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pali=20Roh=C3=A1r?= <[email protected]> Date: Sun, 1 Feb 2026 14:05:33 +0100 Subject: [PATCH 02/12] crt: Properly implement i386 mingw-w64 _setjmp3() function i386 _setjmp3() function takes more parameters and fills more members in jmp_buf. Also it allows to skip inspection of current SEH record. As lot of code is common between _setjmp() and _setjmp3() functions, create a new common local function setjmp_common() and call it from both exported functions. --- mingw-w64-crt/misc/setjmp.S | 55 +++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) 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 + calll __MINGW_LSYMBOL(setjmp_common) + popl %ecx /* jmp_buf */ + + movl %esp,16(%ecx) /* jmp_buf.Esp */ + movl 0(%esp),%eax + movl %eax,20(%ecx) /* jmp_buf.Eip */ + movl $0x56433230,32(%ecx) /* jmp_buf.Cookie */ + movl $0,36(%ecx) /* jmp_buf.UnwindFunc */ + + movl 24(%ecx),%eax /* load jmp_buf.Registration */ + cmpl $-1,%eax + je .L_setjmp3_end + + movl 8(%esp),%eax /* _Count */ + cmpl $0,%eax + je .L_setjmp3_end + + movl 12(%esp),%edx /* _UnwindFunc */ + movl %edx,36(%ecx) /* jmp_buf.UnwindFunc */ + decl %eax + jz .L_setjmp3_end + + movl 16(%esp),%edx /* _TryLevel */ + movl %edx,28(%ecx) /* jmp_buf.TryLevel */ + decl %eax + jz .L_setjmp3_end + + 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: + pushl %esi + pushl %edi + leal 28(%esp),%esi /* _UnwindData */ + leal 40(%ecx),%edi /* jmp_buf.UnwindData */ + movl %eax,%ecx + rep movsl (%esi),(%edi) + popl %edi + popl %esi + + .L_setjmp3_end: + xorl %eax,%eax + retl + __MINGW_USYMBOL(__intrinsic_setjmp): __MINGW_USYMBOL(_setjmp): + movl $0,%edx /* edx != -1 -> setjmp_common will inspect SEH try_level */ +__MINGW_LSYMBOL(setjmp_common): movl 4(%esp),%ecx /* jmp_buf */ movl %ebp,0(%ecx) /* jmp_buf.Ebp */ movl %ebx,4(%ecx) /* jmp_buf.Ebx */ @@ -47,6 +100,8 @@ __MINGW_USYMBOL(_setjmp): movl %eax,20(%ecx) /* jmp_buf.Eip */ movl %fs:0,%eax /* current SEH record */ movl %eax,24(%ecx) /* jmp_buf.Registration */ + cmpl $-1,%edx /* setjmp_common was called to not inspect SEH try_level */ + je .L_setjmp_skip_SEH_try_level movl $-1,%edx cmpl $-1,%eax /* SEH is top level -> do not inspect SEH try_level */ je .L_setjmp_skip_SEH_try_level -- 2.20.1 >From 780576a593f71d7879a093ac9291339f2cd9a688 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pali=20Roh=C3=A1r?= <[email protected]> Date: Sat, 27 Dec 2025 14:24:01 +0100 Subject: [PATCH 03/12] crt: Provide _setjmp3() function for crtdll.dll and msvcrt10.dll builds Implement _setjmp3() function as wrapper around the _setjmp() function which is available also in crtdll.dll and msvcrt10.dll CRT libraries. --- mingw-w64-crt/Makefile.am | 1 + mingw-w64-crt/def-include/crt-aliases.def.in | 14 -------------- mingw-w64-crt/lib32/crtdll.def.in | 1 - mingw-w64-crt/lib32/msvcrt10.def.in | 1 - mingw-w64-crt/misc/_setjmp3.S | 16 ++++++++++++++++ mingw-w64-crt/misc/setjmp.S | 8 ++++++++ 6 files changed, 25 insertions(+), 16 deletions(-) create mode 100644 mingw-w64-crt/misc/_setjmp3.S diff --git a/mingw-w64-crt/Makefile.am b/mingw-w64-crt/Makefile.am index b1963724edd3..9d4e1fe7f284 100644 --- a/mingw-w64-crt/Makefile.am +++ b/mingw-w64-crt/Makefile.am @@ -915,6 +915,7 @@ src_pre_msvcrt20=\ misc/__p__winver.c \ misc/__timezone.c \ misc/__tzname.c \ + misc/_setjmp3.S \ stdio/_wfindfirst32.c \ stdio/_wfindnext32.c \ stdio/_wstat32.c \ diff --git a/mingw-w64-crt/def-include/crt-aliases.def.in b/mingw-w64-crt/def-include/crt-aliases.def.in index 64e53d555b96..177402215b05 100644 --- a/mingw-w64-crt/def-include/crt-aliases.def.in +++ b/mingw-w64-crt/def-include/crt-aliases.def.in @@ -335,20 +335,6 @@ strncasecmp == _strnicmp ; Some symbols in some version of CRT library were added and some other symbols were removed or renamed ; This list provides some level of backward and forward compatibility -#ifdef WITH_SETJMP3_ALIAS -; crtdll.dll and msvcrt10.dll have only old _setjmp function which does not take -; additional variadic arguments and uses smaller jmpbuf structure. -; mingw-w64 calls _setjmp3 only with zero additional arguments and because number -; of additional arguments is passed on the stack which is cleanup by the caller, -; it means that the mingw-w64 usage of _setjmp3 is ABI compatible with the old -; _setjmp function which is available also in crtdll.dll and msvcrt10.dll libs. -; It heavily depends on the mingw-w64-headers/crt/setjmp.h implementation. -; So this definition allows crtdll.dll and msvcrt10.dll applications to call -; setjmp() macro from setjmp.h, which expands to _setjmp3() function call and -; which is aliased to _setjmp symbol for crtdll.dll and msvcrt10.dll libraries. -F_I386(_setjmp3 == _setjmp) -#endif - #ifdef UCRTBASE F_NON_ARM64(_setjmp == __intrinsic_setjmp) F64(_setjmpex == __intrinsic_setjmpex) diff --git a/mingw-w64-crt/lib32/crtdll.def.in b/mingw-w64-crt/lib32/crtdll.def.in index 5dd3c996420b..020a7595224c 100644 --- a/mingw-w64-crt/lib32/crtdll.def.in +++ b/mingw-w64-crt/lib32/crtdll.def.in @@ -640,5 +640,4 @@ _pgmptr DATA == _pgmptr_dll #define NO_STRCMPI_ALIAS #define WITH_NEXTAFTER_ALIAS #define WITH_GET_PUT_WCHAR_ALIASES -#define WITH_SETJMP3_ALIAS #include "crt-aliases.def.in" diff --git a/mingw-w64-crt/lib32/msvcrt10.def.in b/mingw-w64-crt/lib32/msvcrt10.def.in index 4ba98bfffdf3..4bb693245e23 100644 --- a/mingw-w64-crt/lib32/msvcrt10.def.in +++ b/mingw-w64-crt/lib32/msvcrt10.def.in @@ -1302,5 +1302,4 @@ wscanf #define NO_STRCMPI_ALIAS #define WITH_NEXTAFTER_ALIAS #define WITH_GET_PUT_WCHAR_ALIASES -#define WITH_SETJMP3_ALIAS #include "crt-aliases.def.in" diff --git a/mingw-w64-crt/misc/_setjmp3.S b/mingw-w64-crt/misc/_setjmp3.S new file mode 100644 index 000000000000..9bc254f02d15 --- /dev/null +++ b/mingw-w64-crt/misc/_setjmp3.S @@ -0,0 +1,16 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the mingw-w64 runtime package. + * No warranty is given; refer to the file DISCLAIMER.PD within this package. + */ + +#include <_mingw_mac.h> + +/* Define _setjmp3 as wrapper around the _setjmp */ +#define I386_SETJMP3_ONLY +#include "setjmp.S" + +.data +.globl __MINGW_USYMBOL(__MINGW_IMP_SYMBOL(_setjmp3)) +__MINGW_USYMBOL(__MINGW_IMP_SYMBOL(_setjmp3)): +.long __MINGW_USYMBOL(_setjmp3) diff --git a/mingw-w64-crt/misc/setjmp.S b/mingw-w64-crt/misc/setjmp.S index 5173986b7837..7751dbbd39bc 100644 --- a/mingw-w64-crt/misc/setjmp.S +++ b/mingw-w64-crt/misc/setjmp.S @@ -36,6 +36,11 @@ #if defined(__i386__) __MINGW_USYMBOL(_setjmp3): pushl 4(%esp) +#ifdef I386_SETJMP3_ONLY + /* local setjmp_common function is not being compiled, and we have only CRT _setjmp function */ + /* _setjmp always inspects SEH try_level and we will overwrite jmp_buf.TryLevel below if needed */ + calll *__MINGW_USYMBOL(__MINGW_IMP_SYMBOL(_setjmp)) +#else /* 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 */ @@ -43,6 +48,7 @@ __MINGW_USYMBOL(_setjmp3): setl %dl neg %edx calll __MINGW_LSYMBOL(setjmp_common) +#endif popl %ecx /* jmp_buf */ movl %esp,16(%ecx) /* jmp_buf.Esp */ @@ -86,6 +92,7 @@ __MINGW_USYMBOL(_setjmp3): xorl %eax,%eax retl +#ifndef I386_SETJMP3_ONLY __MINGW_USYMBOL(__intrinsic_setjmp): __MINGW_USYMBOL(_setjmp): movl $0,%edx /* edx != -1 -> setjmp_common will inspect SEH try_level */ @@ -110,6 +117,7 @@ __MINGW_LSYMBOL(setjmp_common): movl %edx,28(%ecx) /* jmp_buf.TryLevel */ xorl %eax, %eax retl +#endif #elif defined(__x86_64__) __MINGW_USYMBOL(setjmp): -- 2.20.1 >From 8fc1ece60a3911c5942e90ae84e2e65be570862e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pali=20Roh=C3=A1r?= <[email protected]> Date: Wed, 23 Sep 2026 14:04:46 +0200 Subject: [PATCH 04/12] crt: Provide _setjmpex() function for arm32 UCRT builds Function _setjmpex is available in arm32 msvcr110.dll, msvcr120.dll, msvcr120_app.dll and in msvcrt.dll DLL libs. It is also available in arm32 vcruntime140.dll DLL library which msvc toolchain uses for linking UCRT applications. For some unknown reasons this function is not available in arm32 api-ms-win-crt-private-l1-1-0.dll and ucrtbase.dll libraries which mingw-w64 uses for UCRT builds. But it is available in arm64 versions of those libraries. When application includes setjmpex.h header file, the _setjmpex symbol is emitted for setjmp() calls. So without the _setjmpex symbol in libucrt.a library, the final linking for UCRT builds on arm32 is failing. As the mingw-w64 do not want to link to vcruntime140.dll runtime library, provide simple _setjmpex implementation for arm32 UCRT builds. This implementation will just call _setjmp symbol and manually stores the frame pointer (second function argument) which is the only difference between those two _setjmp and _setjmpex functions on arm32. Due to calling the _setjmp function via trampoline, it is needed to adjust Sp and Pc members. --- mingw-w64-crt/Makefile.am | 1 + mingw-w64-crt/misc/ucrt_arm32__setjmpex.c | 40 +++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 mingw-w64-crt/misc/ucrt_arm32__setjmpex.c diff --git a/mingw-w64-crt/Makefile.am b/mingw-w64-crt/Makefile.am index 9d4e1fe7f284..9ad8e861efae 100644 --- a/mingw-w64-crt/Makefile.am +++ b/mingw-w64-crt/Makefile.am @@ -566,6 +566,7 @@ src_ucrtbase64=\ # Files included in libucrt*.a on arm32 src_ucrtbasearm32=\ + misc/ucrt_arm32__setjmpex.c \ $(src_ucrtbase) # Files included in libucrt*.a on arm64 diff --git a/mingw-w64-crt/misc/ucrt_arm32__setjmpex.c b/mingw-w64-crt/misc/ucrt_arm32__setjmpex.c new file mode 100644 index 000000000000..dcd446454ac8 --- /dev/null +++ b/mingw-w64-crt/misc/ucrt_arm32__setjmpex.c @@ -0,0 +1,40 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the mingw-w64 runtime package. + * No warranty is given; refer to the file DISCLAIMER.PD within this package. + */ + +#if defined(__arm__) + +#include <setjmp.h> + +/* arm32 _setjmp() function ignores it second frame argument and always stores + * the NULL pointer into the Frame member of _JUMP_BUFFER structure. + * Implement our _setjmpex() function as a wrapper around the _setjmp() function + * and manually stores the frame pointer into the Frame member. + * Note that we need to fixup Sp and Pc members as they were not filled with + * respect to the caller of our _setjmpex() function. + */ +int __cdecl __attribute__((naked)) __attribute__((nothrow, returns_twice)) _setjmpex(jmp_buf buf, void *frame) +{ + asm( + "push.w {r4, r5, lr}\n\t" + "mov r4, r0\n\t" + "mov r5, r1\n\t" + "bl _setjmp\n\t" + "mov r2, r4\n\t" + "mov r1, r5\n\t" + "pop.w {r4, r5, lr}\n\t" + "str r1, [r2, #0x00]\n\t" /* jmp_buf->Frame */ + "str sp, [r2, #0x24]\n\t" /* jmp_buf->Sp */ + "str lr, [r2, #0x28]\n\t" /* jmp_buf->Pc */ + "bx lr\n\t" + ); +} +int (__cdecl *__MINGW_IMP_SYMBOL(_setjmpex))(jmp_buf buf, void *frame) = _setjmpex; + +/* In UCRT is the _setjmpex() function aliased to __intrinsic_setjmpex() function. */ +int __attribute__((alias("_setjmpex"))) __cdecl __intrinsic_setjmpex(jmp_buf buf, void *frame); +extern int __attribute__((alias(__MINGW64_STRINGIFY(__MINGW_IMP_SYMBOL(_setjmpex)))))(__cdecl *__MINGW_IMP_SYMBOL(__intrinsic_setjmpex))(jmp_buf buf, void *frame); + +#endif -- 2.20.1 >From 2a53d0a89423a884e361365decbf37295b272162 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pali=20Roh=C3=A1r?= <[email protected]> Date: Sat, 31 Jan 2026 15:14:03 +0100 Subject: [PATCH 05/12] crt: Provide msvcrt/UCRT compatible longjmp and _longjmpex symbols for crtdll.dll builds In msvcrt and UCRT the longjmp() function is calling SEH unwind and basically is aliased to _longjmpex() function. crtdll.dll does not provide _longjmpex() function and its longjmp() function is not calling SEH unwind. So rename its longjmp symbol to __crtdll_longjmp and provide longjmp and _longjmpex implementations by mingw-w64. --- mingw-w64-crt/Makefile.am | 1 + mingw-w64-crt/lib32/crtdll.def.in | 2 +- mingw-w64-crt/misc/crtdll__longjmpex.c | 50 ++++++++++++++++++++++++++ mingw-w64-headers/crt/setjmp.h | 4 +-- 4 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 mingw-w64-crt/misc/crtdll__longjmpex.c diff --git a/mingw-w64-crt/Makefile.am b/mingw-w64-crt/Makefile.am index 9ad8e861efae..0bd73d8bd97e 100644 --- a/mingw-w64-crt/Makefile.am +++ b/mingw-w64-crt/Makefile.am @@ -1106,6 +1106,7 @@ src_crtdll=\ $(src_pre_msvcr120) \ misc/crtdll_stat.h \ misc/crtdll__getmainargs.c \ + misc/crtdll__longjmpex.c \ misc/crtdll_fstat.c \ misc/crtdll_stat.c \ misc/__p__mbctype.c diff --git a/mingw-w64-crt/lib32/crtdll.def.in b/mingw-w64-crt/lib32/crtdll.def.in index 020a7595224c..a467d136cc8f 100644 --- a/mingw-w64-crt/lib32/crtdll.def.in +++ b/mingw-w64-crt/lib32/crtdll.def.in @@ -417,7 +417,7 @@ localeconv localtime log log10 -longjmp +__crtdll_longjmp == longjmp ; crtdll.dll's longjmp is incompatible with msvcrt.dll longjmp, real longjmp provided as wrapper malloc mblen mbstowcs diff --git a/mingw-w64-crt/misc/crtdll__longjmpex.c b/mingw-w64-crt/misc/crtdll__longjmpex.c new file mode 100644 index 000000000000..fb070ac7594e --- /dev/null +++ b/mingw-w64-crt/misc/crtdll__longjmpex.c @@ -0,0 +1,50 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the mingw-w64 runtime package. + * No warranty is given; refer to the file DISCLAIMER.PD within this package. + */ + +#if defined(__i386__) + +#include <setjmp.h> +#include <windows.h> + +void __cdecl _global_unwind2(EXCEPTION_REGISTRATION_RECORD *frame); +void __cdecl _local_unwind2(EXCEPTION_REGISTRATION_RECORD *frame, int trylevel); +__MINGW_ATTRIB_NORETURN __attribute__((nothrow)) void __cdecl __crtdll_longjmp(jmp_buf buf, int value); + +__MINGW_ATTRIB_NORETURN +__attribute__((nothrow)) +void __cdecl _longjmpex(jmp_buf buf, int value) +{ + _JUMP_BUFFER *bufptr = (void *)buf; + + _Static_assert(sizeof(bufptr->Registration) == sizeof(void *), "_JUMP_BUFFER.Registration has not pointer size"); + _Static_assert(sizeof(bufptr->UnwindFunc) == sizeof(void *), "_JUMP_BUFFER.UnwindFunc has not pointer size"); + + /* crtdll longjmp() completely ignores Registration, TryLevel and UnwindFunc fields. + * So do all unwinds manually before calling the real crtdll longjmp(). + */ + + if (bufptr->Registration != __readfsdword(0)) + _global_unwind2((void *)bufptr->Registration); + + if (bufptr->Registration != 0) { + if (!IsBadReadPtr(&bufptr->Cookie, sizeof(bufptr->Cookie)) && bufptr->Cookie == 0x56433230) { + if (bufptr->UnwindFunc != 0) + ((void(__stdcall*)(const _JUMP_BUFFER *))bufptr->UnwindFunc)(bufptr); + } else { + _local_unwind2((void *)bufptr->Registration, bufptr->TryLevel); + } + } + + __crtdll_longjmp(buf, value); +} +void (__cdecl *__MINGW_IMP_SYMBOL(_longjmpex))(jmp_buf buf, int value) = _longjmpex; + +/* In msvcrt and UCRT the longjmp() function is calling SEH unwind and is aliased to _longjmpex() function. */ +#undef longjmp +void __attribute__((alias("_longjmpex"))) __cdecl longjmp(jmp_buf buf, int value); +extern void __attribute__((alias(__MINGW64_STRINGIFY(__MINGW_IMP_SYMBOL(_longjmpex)))))(__cdecl *__MINGW_IMP_SYMBOL(longjmp))(jmp_buf buf, int value); + +#endif diff --git a/mingw-w64-headers/crt/setjmp.h b/mingw-w64-headers/crt/setjmp.h index 49e29a0e0160..aadb1f8a0baf 100644 --- a/mingw-w64-headers/crt/setjmp.h +++ b/mingw-w64-headers/crt/setjmp.h @@ -25,8 +25,8 @@ _CRT_BEGIN_C_HEADER unsigned long Registration; unsigned long TryLevel; /* Following fields are only for new _setjmp3(), the are not for old _setjmp(). */ - unsigned long Cookie; - unsigned long UnwindFunc; + unsigned long Cookie; /* 0x56433230 */ + unsigned long UnwindFunc; /* void(__stdcall*)(const _JUMP_BUFFER *) */ unsigned long UnwindData[6]; } _JUMP_BUFFER; -- 2.20.1 >From 40822f360c2f852be1d392bd682e393889261e5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pali=20Roh=C3=A1r?= <[email protected]> Date: Wed, 19 Aug 2026 21:27:16 +0200 Subject: [PATCH 06/12] crt: Provide msvcrt/UCRT compatible longjmp and _longjmpex symbols for msvcrt10.dll builds msvcrt10.dll longjmp() function is mostly compatible with the msvcrt.dll _longjmpex() function. It only does not support calling jmp_buf UnwindFunc and is always calling local_unwind2() even for incompatible SEH establisher frames. Add mingw-w64 wrapper around the msvcrt10.dll longjmp() function, process unsupported Cookie and UnwindFunc. And set TryLevel to the expected value by local_unwind2() which immediately stops the local_unwind2() execution. --- mingw-w64-crt/Makefile.am | 3 +- mingw-w64-crt/lib32/msvcrt10.def.in | 2 +- mingw-w64-crt/misc/msvcrt10__longjmpex.c | 77 ++++++++++++++++++++++++ 3 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 mingw-w64-crt/misc/msvcrt10__longjmpex.c diff --git a/mingw-w64-crt/Makefile.am b/mingw-w64-crt/Makefile.am index 0bd73d8bd97e..481cea5ecbbf 100644 --- a/mingw-w64-crt/Makefile.am +++ b/mingw-w64-crt/Makefile.am @@ -1122,7 +1122,8 @@ src_msvcrt10=\ $(src_pre_msvcr100) \ $(src_pre_msvcr110) \ $(src_pre_msvcr120) \ - misc/crtdll__getmainargs.c + misc/crtdll__getmainargs.c \ + misc/msvcrt10__longjmpex.c src_msvcrt20=\ $(src_pre_msvcrt40) \ diff --git a/mingw-w64-crt/lib32/msvcrt10.def.in b/mingw-w64-crt/lib32/msvcrt10.def.in index 4bb693245e23..243d12a39663 100644 --- a/mingw-w64-crt/lib32/msvcrt10.def.in +++ b/mingw-w64-crt/lib32/msvcrt10.def.in @@ -1192,7 +1192,7 @@ localeconv localtime log log10 -longjmp +__msvcrt10_longjmp == longjmp ; msvcrt10.dll's longjmp is incompatible with msvcrt.dll longjmp, real longjmp provided as wrapper malloc mblen mbstowcs diff --git a/mingw-w64-crt/misc/msvcrt10__longjmpex.c b/mingw-w64-crt/misc/msvcrt10__longjmpex.c new file mode 100644 index 000000000000..7b54b129a2cb --- /dev/null +++ b/mingw-w64-crt/misc/msvcrt10__longjmpex.c @@ -0,0 +1,77 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the mingw-w64 runtime package. + * No warranty is given; refer to the file DISCLAIMER.PD within this package. + */ + +#if defined(__i386__) + +#include <setjmp.h> +#include <windows.h> + +void __cdecl _global_unwind2(EXCEPTION_REGISTRATION_RECORD *frame); +__MINGW_ATTRIB_NORETURN __attribute__((nothrow)) void __cdecl __msvcrt10_longjmp(jmp_buf buf, int value); + +struct msvcrt10_establisher_frame { + EXCEPTION_REGISTRATION_RECORD ExceptionRecord; + const void *ScopeTable; + DWORD TryLevel; +}; + +__MINGW_ATTRIB_NORETURN +__attribute__((nothrow)) +void __cdecl _longjmpex(jmp_buf buf, int value) +{ + _JUMP_BUFFER *bufptr = (void *)buf; + + _Static_assert(sizeof(bufptr->Registration) == sizeof(void *), "_JUMP_BUFFER.Registration has not pointer size"); + _Static_assert(sizeof(bufptr->UnwindFunc) == sizeof(void *), "_JUMP_BUFFER.UnwindFunc has not pointer size"); + + /* msvcrt10 longjmp() for non-zero bufptr->Registration is unconditionally + * calling local_unwind2() with bufptr->Registration as SEH establisher frame + * and with bufptr->TryLevel as try level. + * + * msvcrt10 local_unwind2() is expecting that the passed SEH establisher frame + * is of msvcrt10 format which contains additional fields: scope table and try level. + * It will do nothing if the SEH establisher frame's try level is -1 or if it is + * same as passed try level from function argument. Otherwise it will dereference + * scope table pointer and calls specified unwind handlers. + * + * Therefore the msvcrt10 longjmp() can be called only if the bufptr->Registration + * is valid SEH establisher frame in msvcrt10 format (with additional fields). + * All SEH handlers registered by mingw-w64 are just standard SEH establisher + * frame structure without any additional msvcrt10 fields. To prevent using and + * dereferencing the scope table pointer (which is after the standard SEH + * establisher frame), set the bufptr->TryLevel to value which msvcrt10 + * local_unwind2() function reads, to the value from address where is the + * try level in the msvcrt10 SEH establisher frame format. It is some value + * on the stack which does not belong to the SEH establisher frame, but reading + * it should not cause any memory corruption. + * + * As msvcrt10 longjmp() does not support bufptr->Cookie and bufptr->UnwindFunc + * fields, for the non-zero bufptr->UnwindFunc case, it is needed to do all + * unwinds manually. msvcrt10 longjmp() is always calling the _global_unwind2(), + * but the bufptr->UnwindFunc has to be called after _global_unwind2(). + * As _global_unwind2() is unregistering SEH handler (up to the specified one), + * it is idempotent and so can be called multiple times. + */ + + if (bufptr->Registration != 0 && !IsBadReadPtr(&bufptr->Cookie, sizeof(bufptr->Cookie)) && bufptr->Cookie == 0x56433230) { + if (bufptr->UnwindFunc != 0) { + if (bufptr->Registration != __readfsdword(0)) + _global_unwind2((void *)bufptr->Registration); + ((void(__stdcall*)(const _JUMP_BUFFER *))bufptr->UnwindFunc)(bufptr); + } + bufptr->TryLevel = ((struct msvcrt10_establisher_frame *)bufptr->Registration)->TryLevel; + } + + __msvcrt10_longjmp(buf, value); +} +void (__cdecl *__MINGW_IMP_SYMBOL(_longjmpex))(jmp_buf buf, int value) = _longjmpex; + +/* In msvcrt and UCRT the longjmp() function is calling SEH unwind and is aliased to _longjmpex() function. */ +#undef longjmp +void __attribute__((alias("_longjmpex"))) __cdecl longjmp(jmp_buf buf, int value); +extern void __attribute__((alias(__MINGW64_STRINGIFY(__MINGW_IMP_SYMBOL(_longjmpex)))))(__cdecl *__MINGW_IMP_SYMBOL(longjmp))(jmp_buf buf, int value); + +#endif -- 2.20.1 >From 950c226c12e066a275fdd4933bee7b34d5a457dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pali=20Roh=C3=A1r?= <[email protected]> Date: Sat, 27 Dec 2025 14:25:14 +0100 Subject: [PATCH 07/12] headers: Fix definition of setjmp for 32-bit x86 builds If UnwindFunc and TryLevel arguments for 32-bit x86 _setjmp3() function are not specified then CRT _setjmp3() function automatically fills them from the current SEH registration frame. It expects that the SEH frame data are compatible with the _except_handler<N>, something which is automatically setup by MS CL compiler with __try / __except / __finally support. These expectations do not apply for mingw-w64 code generally as in the most cases there is registered only one top level SEH frame, which does not have SEH frame data of _except_handler<N> format. UnwindFunc and TryLevel arguments also have to be in the _except_handler<N> format and CRT can pass them to the _local_unwind function. If some non-CRT SEH handler is registered then all those local CRT unwinding parts related to nested __try blocks have to be avoided. It can be achieved by explicitly setting the local UnwindFunc to NULL and current TryLevel to -1. So explicitly set UnwindFunc to NULL and TryLevel to -1 in our setjmp() macro. This prevents _setjmp3() to inspect content of current SEH frame. --- mingw-w64-headers/crt/setjmp.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mingw-w64-headers/crt/setjmp.h b/mingw-w64-headers/crt/setjmp.h index aadb1f8a0baf..b7c11461a44e 100644 --- a/mingw-w64-headers/crt/setjmp.h +++ b/mingw-w64-headers/crt/setjmp.h @@ -194,7 +194,7 @@ _CRTIMP __MINGW_ATTRIB_NORETURN __attribute__ ((__nothrow__)) void __cdecl _long #ifdef __i386__ _CRTIMP int __cdecl __attribute__ ((__nothrow__,__returns_twice__)) _setjmp(jmp_buf _Buf); /* old i386 crtdll setjmp.h */ -_CRTIMP int __cdecl __attribute__ ((__nothrow__,__returns_twice__)) _setjmp3(jmp_buf _Buf, int _Count, ...); /* new i386 msvcrt20+ setjmp.h and setjmpex.h */ +_CRTIMP int __cdecl __attribute__ ((__nothrow__,__returns_twice__)) _setjmp3(jmp_buf _Buf, int _Count, ... /*void *_UnwindFunc, int _TryLevel, 6xDWORD _UnwindData...*/); /* new i386 msvcrt20+ setjmp.h and setjmpex.h */ #else #ifndef __aarch64__ _CRTIMP int __cdecl __attribute__ ((__nothrow__,__returns_twice__)) _setjmp(jmp_buf _Buf, void *_Frame); /* for non-i386 and non-aarch64 setjmp.h */ @@ -227,7 +227,7 @@ _CRT_END_C_HEADER # undef setjmp #endif #if defined(__i386__) -# define setjmp(BUF) _setjmp3((BUF), 0) +# define setjmp(BUF) _setjmp3((BUF), 2, NULL /*UnwindFunc*/, -1 /*TryLevel*/) #elif !defined(_INC_SETJMPEX) && (!defined(__SEH__) || defined(__USE_MINGW_SETJMP_NON_SEH)) # if defined(__arm__) || defined(__aarch64__) # define setjmp(BUF) __mingw_setjmp((BUF)) -- 2.20.1 >From fa13132deaee54107e33aece3da2c59bebb4ef42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pali=20Roh=C3=A1r?= <[email protected]> Date: Sat, 27 Dec 2025 15:25:07 +0100 Subject: [PATCH 08/12] crt: Fix mingw-w64 longjmp() implementation when calling with value 0 ISO C for longjmp says: The longjmp function cannot cause the setjmp macro to return the value 0; if val is 0, the setjmp macro returns the value 1. Add missing handling of value 0. --- mingw-w64-crt/misc/longjmp.S | 10 +++++++++- mingw-w64-crt/misc/mingw_longjmp.S | 6 +++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/mingw-w64-crt/misc/longjmp.S b/mingw-w64-crt/misc/longjmp.S index b5f81cffc0f0..a7f04d529b7f 100644 --- a/mingw-w64-crt/misc/longjmp.S +++ b/mingw-w64-crt/misc/longjmp.S @@ -14,6 +14,8 @@ __MINGW_USYMBOL(longjmp): #if defined(__i386__) movl 4(%esp),%ecx /* jmp_buf */ movl 8(%esp),%eax /* retval */ + cmpl $1,%eax /* check that retval is non-zero */ + adcl $0,%eax /* if not then increase by one via CF */ movl 0(%ecx),%ebp /* jmp_buf.Ebp */ movl 4(%ecx),%ebx /* jmp_buf.Ebx */ movl 8(%ecx),%edi /* jmp_buf.Edi */ @@ -23,6 +25,8 @@ __MINGW_USYMBOL(longjmp): jmp *20(%ecx) /* jmp_buf.Eip */ #elif defined(__x86_64__) movq %rdx,%rax /* retval */ + cmpq $1,%rax /* check that retval is non-zero */ + adcq $0,%rax /* if not then increase by one via CF */ movq 0x8(%rcx),%rbx /* jmp_buf->Rbx */ movq 0x18(%rcx),%rbp /* jmp_buf->Rbp */ movq 0x20(%rcx),%rsi /* jmp_buf->Rsi */ @@ -69,6 +73,9 @@ __MINGW_USYMBOL(longjmp): vldr d14, [r0, #0x60] /* jmp_buf->D[6] */ vldr d15, [r0, #0x68] /* jmp_buf->D[7] */ mov r0, r1 /* retval */ + cmp r1, #1 + it ls + movls r0, #1 bx r2 #elif defined(__aarch64__) ldp x19, x20, [x0, #0x10] /* jmp_buf->X19, X20 */ @@ -87,7 +94,8 @@ __MINGW_USYMBOL(longjmp): ldp d10, d11, [x0, #0x90] /* jmp_buf->D[2-3] */ ldp d12, d13, [x0, #0xa0] /* jmp_buf->D[4-5] */ ldp d14, d15, [x0, #0xb0] /* jmp_buf->D[6-7] */ - mov x0, x1 /* retval */ + cmp x1, #1 + csinc x0, x1, xzr, hi /* retval */ ret #endif diff --git a/mingw-w64-crt/misc/mingw_longjmp.S b/mingw-w64-crt/misc/mingw_longjmp.S index bec0ae157a29..5863f0d88652 100644 --- a/mingw-w64-crt/misc/mingw_longjmp.S +++ b/mingw-w64-crt/misc/mingw_longjmp.S @@ -81,6 +81,9 @@ __MINGW_USYMBOL(__mingw_longjmp): vldr d14, [r0, #0x60] /* jmp_buf->D[6] */ vldr d15, [r0, #0x68] /* jmp_buf->D[7] */ mov r0, r1 /* retval */ + cmp r1, #1 + it ls + movls r0, #1 bx r2 #elif defined(_ARM64_) || defined(__aarch64__) ldp x19, x20, [x0, #0x10] /* jmp_buf->X19, X20 */ @@ -99,7 +102,8 @@ __MINGW_USYMBOL(__mingw_longjmp): ldp d10, d11, [x0, #0x90] /* jmp_buf->D[2-3] */ ldp d12, d13, [x0, #0xa0] /* jmp_buf->D[4-5] */ ldp d14, d15, [x0, #0xb0] /* jmp_buf->D[6-7] */ - mov x0, x1 /* retval */ + cmp x1, #1 + csinc x0, x1, xzr, hi /* retval */ ret #endif #endif -- 2.20.1 >From d816539ba92ce6b5aba98e28011ced0ce2680837 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pali=20Roh=C3=A1r?= <[email protected]> Date: Sat, 27 Dec 2025 16:53:18 +0100 Subject: [PATCH 09/12] crt: Implement x86-64 SEH-safe case in mingw-w64 longjmp() function --- mingw-w64-crt/misc/longjmp.S | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) 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 */ -- 2.20.1 >From 8703cd7b241862422d909a9c2c6e1403aa9ee786 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pali=20Roh=C3=A1r?= <[email protected]> Date: Sun, 23 Aug 2026 20:59:52 +0200 Subject: [PATCH 10/12] crt: test: Extend t_setjmp test with different longjmp values --- mingw-w64-crt/testcases/t_setjmp.c | 36 +++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/mingw-w64-crt/testcases/t_setjmp.c b/mingw-w64-crt/testcases/t_setjmp.c index 0a455771e2cf..b6abb21ffc1c 100644 --- a/mingw-w64-crt/testcases/t_setjmp.c +++ b/mingw-w64-crt/testcases/t_setjmp.c @@ -6,26 +6,40 @@ jmp_buf buf; -void __attribute__((noreturn)) foo (void) +void __attribute__((noreturn)) call_longjmp (int val) { - printf ("Call longjmp\n"); - longjmp (buf, 1); + printf ("Call longjmp with %d\n", val); + longjmp (buf, val); } int main () { mingw_test_init (); - if (setjmp (buf)) + for (volatile int in_val = -300; in_val <= 300; in_val++) { - printf ("Caught\n"); - } - else - { - foo (); - printf ("Should not be reached.\n"); - return 1; + printf ("Call setjmp\n"); + volatile int in_longjmp = 0; + int out_val = setjmp (buf); + if ((in_val != 0 && out_val == in_val) || (in_val == 0 && out_val == 1)) /* longjmp (0) cause that setjmp () returns 1 */ + { + printf ("Caught %d\n", out_val); + } + else if (!in_longjmp && out_val == 0) + { + in_longjmp = 1; + call_longjmp (in_val); + printf ("Should not be reached.\n"); + return 1; + } + else + { + printf ("Unexpected value %d from %s\n", out_val, in_longjmp ? "longjmp" : "setjmp"); + return 1; + } + printf ("\n"); } + printf ("SUCCESS\n"); return 0; } -- 2.20.1 >From e51973894b5669b7a69d393e6d8fbc20424f5de0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pali=20Roh=C3=A1r?= <[email protected]> Date: Thu, 20 Aug 2026 18:10:21 +0200 Subject: [PATCH 11/12] crt: test: Add new t_setjmpex test which is using setjmpex.h --- mingw-w64-crt/testcases/Makefile.am | 1 + mingw-w64-crt/testcases/t_setjmp.c | 4 ++++ mingw-w64-crt/testcases/t_setjmpex.c | 2 ++ 3 files changed, 7 insertions(+) create mode 100644 mingw-w64-crt/testcases/t_setjmpex.c diff --git a/mingw-w64-crt/testcases/Makefile.am b/mingw-w64-crt/testcases/Makefile.am index 41ce4677b985..02c7f1c00bd5 100644 --- a/mingw-w64-crt/testcases/Makefile.am +++ b/mingw-w64-crt/testcases/Makefile.am @@ -155,6 +155,7 @@ testcase_progs = \ t_readdir \ t_safe_flush \ t_setjmp \ + t_setjmpex \ t_sigfpe \ t_sigv \ t_snprintf \ diff --git a/mingw-w64-crt/testcases/t_setjmp.c b/mingw-w64-crt/testcases/t_setjmp.c index b6abb21ffc1c..3986b12792d4 100644 --- a/mingw-w64-crt/testcases/t_setjmp.c +++ b/mingw-w64-crt/testcases/t_setjmp.c @@ -1,4 +1,8 @@ +#ifdef TEST_SETJMPEX +#include <setjmpex.h> +#else #include <setjmp.h> +#endif #include <stdio.h> /* mingw-w64 headers */ diff --git a/mingw-w64-crt/testcases/t_setjmpex.c b/mingw-w64-crt/testcases/t_setjmpex.c new file mode 100644 index 000000000000..6cdba9839a70 --- /dev/null +++ b/mingw-w64-crt/testcases/t_setjmpex.c @@ -0,0 +1,2 @@ +#define TEST_SETJMPEX +#include "t_setjmp.c" -- 2.20.1 >From 355e10e1233d2856b3eccc6b75b8073867c11fd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pali=20Roh=C3=A1r?= <[email protected]> Date: Wed, 19 Aug 2026 21:44:14 +0200 Subject: [PATCH 12/12] crt: test: Add new t_longjmp_unwind test which verifies that unwind handler is called from longjmp exactly once --- mingw-w64-crt/testcases/Makefile.am | 1 + mingw-w64-crt/testcases/t_longjmp_unwind.c | 231 +++++++++++++++++++++ 2 files changed, 232 insertions(+) create mode 100644 mingw-w64-crt/testcases/t_longjmp_unwind.c diff --git a/mingw-w64-crt/testcases/Makefile.am b/mingw-w64-crt/testcases/Makefile.am index 02c7f1c00bd5..18b0aecf45b3 100644 --- a/mingw-w64-crt/testcases/Makefile.am +++ b/mingw-w64-crt/testcases/Makefile.am @@ -126,6 +126,7 @@ testcase_progs = \ t_iswctype \ t_ivma \ t_lfs \ + t_longjmp_unwind \ t_lseeki64 \ t_main \ t_main_assert \ diff --git a/mingw-w64-crt/testcases/t_longjmp_unwind.c b/mingw-w64-crt/testcases/t_longjmp_unwind.c new file mode 100644 index 000000000000..692f0ec6c40d --- /dev/null +++ b/mingw-w64-crt/testcases/t_longjmp_unwind.c @@ -0,0 +1,231 @@ +#include <stdio.h> +#include <stdlib.h> +#include <setjmp.h> +#include <excpt.h> + +#define WIN32_LEAN_AND_MEAN +#include <windows.h> + +/* mingw-w64 headers */ +#include "libtest.h" + +#if defined(__SEH__) && (!defined(__clang__) || __clang_major__ >= 7) +#define SEH_INLINE_ASM +#ifdef __arm__ +#define ASM_SEH_UNWIND "%%unwind" +#elif defined(__aarch64__) || defined(__arm64ec__) +#define ASM_SEH_UNWIND "@unwind, @except" /* on arm64 the unwind handler is called when both @unwind and @except are specified */ +#else +#define ASM_SEH_UNWIND "@unwind" +#endif +#endif + +static int outer_unwind_called = 0; +static int target_unwind_called = 0; +static int inner_unwind_called = 0; +static jmp_buf buf; + +#if defined(__i386__) +/* We need to make sure that we align the stack to 16 bytes for the sake of SSE */ +__attribute__((force_align_arg_pointer)) +#endif +static EXCEPTION_DISPOSITION __cdecl outer_unwind_handler(EXCEPTION_RECORD *ExceptionRecord, PVOID EstablisherFrame __attribute__((unused)), CONTEXT *ContextRecord __attribute__((unused)), PVOID DispatcherContext __attribute__((unused))) +{ + printf("System called outer SEH unwind handler with flags 0x%lx\n", ExceptionRecord->ExceptionFlags); + if (ExceptionRecord->ExceptionFlags & EXCEPTION_UNWINDING) + outer_unwind_called++; + return ExceptionContinueSearch; +} + +#if defined(__i386__) +/* We need to make sure that we align the stack to 16 bytes for the sake of SSE */ +__attribute__((force_align_arg_pointer)) +#endif +static EXCEPTION_DISPOSITION __cdecl target_unwind_handler(EXCEPTION_RECORD *ExceptionRecord, PVOID EstablisherFrame __attribute__((unused)), CONTEXT *ContextRecord __attribute__((unused)), PVOID DispatcherContext __attribute__((unused))) +{ + printf("System called target SEH unwind handler with flags 0x%lx\n", ExceptionRecord->ExceptionFlags); + if ((ExceptionRecord->ExceptionFlags & (EXCEPTION_UNWINDING | EXCEPTION_TARGET_UNWIND)) == (EXCEPTION_UNWINDING | EXCEPTION_TARGET_UNWIND)) + target_unwind_called++; + return ExceptionContinueSearch; +} + +#if defined(__i386__) +/* We need to make sure that we align the stack to 16 bytes for the sake of SSE */ +__attribute__((force_align_arg_pointer)) +#endif +static EXCEPTION_DISPOSITION __cdecl inner_unwind_handler(EXCEPTION_RECORD *ExceptionRecord, PVOID EstablisherFrame __attribute__((unused)), CONTEXT *ContextRecord __attribute__((unused)), PVOID DispatcherContext __attribute__((unused))) +{ + printf("System called inner SEH unwind handler with flags 0x%lx\n", ExceptionRecord->ExceptionFlags); + if (ExceptionRecord->ExceptionFlags & EXCEPTION_UNWINDING) + inner_unwind_called++; + return ExceptionContinueSearch; +} + +#ifdef __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wmissing-noreturn" +#endif +#if !defined(__i386__) && defined(SEH_INLINE_ASM) +__attribute__((noinline)) /* required for asm .seh_handler directive */ +#endif +#if defined(__i386__) +__attribute__((noinline)) /* prevents optimizing out exception_record structure before calling longjmp() */ +#endif +static void register_inner_unwind_handler_and_call_longjmp(void) +{ +#if defined(__i386__) + EXCEPTION_REGISTRATION_RECORD inner_exception_record = { + .Next = (EXCEPTION_REGISTRATION_RECORD *)__readfsdword(0), + .Handler = (PEXCEPTION_ROUTINE)(INT_PTR)inner_unwind_handler, + }; + __writefsdword(0, (DWORD)&inner_exception_record); +#elif defined(SEH_INLINE_ASM) + asm volatile (".seh_handler %c0, " ASM_SEH_UNWIND :: "i" (inner_unwind_handler)); +#else + return; /* should not happen; triggers runtime error */ +#endif + + printf("Calling longjmp() and unregistering inner SEH unwind handler\n"); + longjmp(buf, 1); + + /* NOTE: longjmp does not return and on i386 it unregister all SEH handlers up to the target one */ + + printf("Unregistering inner SEH unwind handler\n"); +#if defined(__i386__) + if (__readfsdword(0) != (DWORD)&inner_exception_record) { + printf("ERROR: Cannot unregister inner SEH unwind handler because of corrupted list of SEH records\n"); + _exit(1); + } + __writefsdword(0, (DWORD)inner_exception_record.Next); +#endif +} +#ifdef __clang__ +#pragma clang diagnostic pop +#endif + +#if !defined(__i386__) && defined(SEH_INLINE_ASM) +__attribute__((noinline)) /* required for asm .seh_handler directive */ +#endif +static void register_target_unwind_handler_and_call_setjmp_and_register_inner_unwind_handler_and_call_longjmp(void) +{ +#if defined(__i386__) + EXCEPTION_REGISTRATION_RECORD target_exception_record = { + .Next = (EXCEPTION_REGISTRATION_RECORD *)__readfsdword(0), + .Handler = (PEXCEPTION_ROUTINE)(INT_PTR)target_unwind_handler, + }; + __writefsdword(0, (DWORD)&target_exception_record); +#elif defined(SEH_INLINE_ASM) + asm volatile (".seh_handler %c0, " ASM_SEH_UNWIND :: "i" (target_unwind_handler)); +#else + return; /* should not happen; triggers runtime error */ +#endif + + printf("Calling setjmp()\n"); + + if (!setjmp(buf)) { + printf("Registering inner SEH unwind handler\n"); + register_inner_unwind_handler_and_call_longjmp(); + printf("ERROR: longjmp() returned\n"); + _exit(1); + } + + printf("setjmp() returned from longjmp()\n"); + + printf("Unregistering target SEH unwind handler\n"); +#if defined(__i386__) + if (__readfsdword(0) != (DWORD)&target_exception_record) { + printf("ERROR: Cannot unregister target SEH unwind handler because of corrupted list of SEH records\n"); + _exit(1); + } + __writefsdword(0, (DWORD)target_exception_record.Next); +#endif +} + +#if !defined(__i386__) && defined(SEH_INLINE_ASM) +__attribute__((noinline)) /* required for asm .seh_handler directive */ +#endif +static void register_outer_unwind_handler_and_register_target_unwind_handler_and_call_setjmp_and_register_inner_unwind_handler_and_call_longjmp(void) +{ +#if defined(__i386__) + EXCEPTION_REGISTRATION_RECORD outer_exception_record = { + .Next = (EXCEPTION_REGISTRATION_RECORD *)__readfsdword(0), + .Handler = (PEXCEPTION_ROUTINE)(INT_PTR)outer_unwind_handler, + }; + __writefsdword(0, (DWORD)&outer_exception_record); +#elif defined(SEH_INLINE_ASM) + asm volatile (".seh_handler %c0, " ASM_SEH_UNWIND :: "i" (outer_unwind_handler)); +#else + return; /* should not happen; triggers runtime error */ +#endif + + printf("Registering target SEH unwind handler\n"); + register_target_unwind_handler_and_call_setjmp_and_register_inner_unwind_handler_and_call_longjmp(); + + printf("Unregistering outer SEH unwind handler\n"); +#if defined(__i386__) + if (__readfsdword(0) != (DWORD)&outer_exception_record) { + printf("ERROR: Cannot unregister outer SEH unwind handler because of corrupted list of SEH records\n"); + _exit(1); + } + __writefsdword(0, (DWORD)outer_exception_record.Next); +#endif +} + +int main(int argc, char **argv) +{ + mingw_test_init(); + +#if !defined(__i386__) && !defined(SEH_INLINE_ASM) + printf("SKIP: No SEH support\n"); + return 77; +#endif + + outer_unwind_called = 0; + target_unwind_called = 0; + inner_unwind_called = 0; + + int ret = 0; + + printf("Registering outer SEH unwind handler\n"); + register_outer_unwind_handler_and_register_target_unwind_handler_and_call_setjmp_and_register_inner_unwind_handler_and_call_longjmp(); + + if (outer_unwind_called != 0) { + printf("ERROR: Outer SEH unwind handler was called\n"); + ret = 1; + } else { + printf("OK: Outer SEH unwind handler was not called\n"); + } + +#ifdef __i386__ + /* i386 fs:0 based SEH does not call the last (target) handler */ + if (target_unwind_called != 0) { + printf("ERROR: Target SEH unwind handler was called\n"); + ret = 1; + } else { + printf("OK: Target SEH unwind handler was not called\n"); + } +#else + /* non-i386 .pdata based SEH always calls the last target handler with the EXCEPTION_TARGET_UNWIND flag */ + if (target_unwind_called == 0) { + printf("ERROR: Target SEH unwind handler was not called\n"); + ret = 1; + } else if (target_unwind_called != 1) { + printf("ERROR: Target SEH unwind handler was called more times (%d)\n", target_unwind_called); + ret = 1; + } else { + printf("OK: Target SEH unwind handler was called exactly once\n"); + } +#endif + + if (inner_unwind_called == 0) { + printf("ERROR: Inner SEH unwind handler was not called\n"); + ret = 1; + } else if (inner_unwind_called != 1) { + printf("ERROR: Inner SEH unwind handler was called more times (%d)\n", inner_unwind_called); + ret = 1; + } else { + printf("OK: Inner SEH unwind handler was called exactly once\n"); + } + + return ret; +} -- 2.20.1
_______________________________________________ Mingw-w64-public mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/mingw-w64-public
