From 0054150a68d882f450292b29c456501fac87f950 Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Thu, 17 Oct 2024 10:51:20 -0700 Subject: [PATCH] Fix STACK_OVERFLOW_CHECK + WASM_WORKERS (#22756) We we not calling `writeStackCookie()` in the right place which means that `checkStackCookie()` would always fail in wasm workers. Required for #22721 --- src/library_wasm_worker.js | 6 ++++++ src/postamble.js | 11 ++++------- test/wasm_worker/hello_wasm_worker.c | 1 + 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/library_wasm_worker.js b/src/library_wasm_worker.js index 1bda126e3a21..2b50c5ffab15 100644 --- a/src/library_wasm_worker.js +++ b/src/library_wasm_worker.js @@ -110,6 +110,12 @@ addToLibrary({ ___set_stack_limits(_emscripten_stack_get_base(), _emscripten_stack_get_end()); #endif +#if STACK_OVERFLOW_CHECK + // Write the stack cookie last, after we have set up the proper bounds and + // current position of the stack. + writeStackCookie(); +#endif + #if AUDIO_WORKLET // Audio Worklets do not have postMessage()ing capabilities. if (typeof AudioWorkletGlobalScope === 'undefined') { diff --git a/src/postamble.js b/src/postamble.js index 2008856af4cc..3678bcd50d2b 100644 --- a/src/postamble.js +++ b/src/postamble.js @@ -162,13 +162,6 @@ function run() { return; } -#if STACK_OVERFLOW_CHECK -#if PTHREADS - if (!ENVIRONMENT_IS_PTHREAD) -#endif - stackCheckInit(); -#endif - #if WASM_WORKERS if (ENVIRONMENT_IS_WASM_WORKER) { #if MODULARIZE @@ -192,6 +185,10 @@ function run() { } #endif +#if STACK_OVERFLOW_CHECK + stackCheckInit(); +#endif + if (!calledPrerun) { calledPrerun = 1; preRun(); diff --git a/test/wasm_worker/hello_wasm_worker.c b/test/wasm_worker/hello_wasm_worker.c index 0bfcec6902af..b0ede8202f82 100644 --- a/test/wasm_worker/hello_wasm_worker.c +++ b/test/wasm_worker/hello_wasm_worker.c @@ -7,6 +7,7 @@ void run_in_worker() { emscripten_console_log("Hello from wasm worker!\n"); + EM_ASM(typeof checkStackCookie == 'function' && checkStackCookie()); #ifdef REPORT_RESULT REPORT_RESULT(0); #endif