Skip to content

Commit a252039

Browse files
authored
Test out -D__USE_MINGW_SETJMP_NON_SEH (bytecodealliance#9929)
* Test out `-D__USE_MINGW_SETJMP_NON_SEH` With respect to bytecodealliance#9688 prtest:full * Add a comment for the workaround
1 parent e9ecab4 commit a252039

File tree

2 files changed

+13
-55
lines changed

2 files changed

+13
-55
lines changed

crates/wasmtime/build.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ fn build_c_helpers() {
3636
build.define("FEATURE_DEBUG_BUILTINS", None);
3737
}
3838

39+
// On MinGW targets work around a bug in the MinGW compiler described at
40+
// https://github.com/bytecodealliance/wasmtime/pull/9688#issuecomment-2573367719
41+
if std::env::var("CARGO_CFG_WINDOWS").is_ok()
42+
&& std::env::var("CARGO_CFG_TARGET_ENV").ok().as_deref() == Some("gnu")
43+
{
44+
build.define("__USE_MINGW_SETJMP_NON_SEH", None);
45+
}
46+
3947
println!("cargo:rerun-if-changed=src/runtime/vm/helpers.c");
4048
build.file("src/runtime/vm/helpers.c");
4149
build.compile("wasmtime-helpers");

crates/wasmtime/src/runtime/vm/helpers.c

Lines changed: 5 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -61,65 +61,15 @@ typedef sigjmp_buf platform_jmp_buf;
6161
#define CONCAT(a, b) CONCAT2(a, b)
6262
#define VERSIONED_SYMBOL(a) CONCAT(a, VERSIONED_SUFFIX)
6363

64-
// Define one function here, `wasmtime_setjmp_inverted`, which returns the
65-
// negation of whether the call succeeded. Define then the actual import below
66-
// of `wasmtime_setjmp_*` which returns the negation of this negation which
67-
// means it returns whether the function invocation succeeded or not.
68-
//
69-
// Why in the world would we do this? For now: MinGW. In
70-
// bytecodealliance/wasmtime#9675 that PR was originally failing CI only on
71-
// MinGW and seems to be fixed by this. In that PR the signature of `body` here
72-
// changed from a `void` return to a `bool` returned. That means that the body
73-
// of this function changed from the historical:
74-
//
75-
// body(payload, callee);
76-
// return 1;
77-
//
78-
// to what we actually want:
79-
//
80-
// return body(payload, callee);
81-
//
82-
// For some reason though this causes issues when unwinding via `longjmp` on
83-
// Windows. Tests would exit with the error message:
84-
//
85-
// code 0xc0000028: An invalid or unaligned stack was encountered during an
86-
// unwind operation. (os error 543)
87-
//
88-
// Debugging revealed that if this:
89-
//
90-
// return body(payload, callee);
91-
//
92-
// were written as:
93-
//
94-
// bool ret = body(payload, callee);
95-
// return ret;
96-
//
97-
// then the bug would be "fixed". This "fix" didn't work in release mode
98-
// however, leading to the current fix. For whatever reason it seems that
99-
// unwinding is broken if there's not code between the `body(...)` indirect
100-
// call and the function return. The `!` here below, inverting the return value,
101-
// is the source of that "code".
102-
//
103-
// Ideally this `*_inverted` shim would go away and get past CI. It's unclear
104-
// whether we're dealing with a miscompile in GCC, bad unwinding information
105-
// generated by Cranelift for JIT code, or what. For now "this seems to work"
106-
// but we'll also be in the process of forwarding this to some other Windows
107-
// folks to see better what's going on.
108-
static bool wasmtime_setjmp_inverted(void **buf_storage,
109-
bool (*body)(void *, void *),
110-
void *payload, void *callee) {
64+
bool VERSIONED_SYMBOL(wasmtime_setjmp)(void **buf_storage,
65+
bool (*body)(void *, void *),
66+
void *payload, void *callee) {
11167
platform_jmp_buf buf;
11268
if (platform_setjmp(buf) != 0) {
113-
return true;
69+
return false;
11470
}
11571
*buf_storage = &buf;
116-
return !body(payload, callee);
117-
}
118-
119-
bool VERSIONED_SYMBOL(wasmtime_setjmp)(void **buf_storage,
120-
bool (*body)(void *, void *),
121-
void *payload, void *callee) {
122-
return !wasmtime_setjmp_inverted(buf_storage, body, payload, callee);
72+
return body(payload, callee);
12373
}
12474

12575
void VERSIONED_SYMBOL(wasmtime_longjmp)(void *JmpBuf) {

0 commit comments

Comments
 (0)