From 383a2de9d1d91bb31ae1e7626fd71c7a601a2b71 Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Tue, 13 May 2025 11:16:43 -0700 Subject: [PATCH] Initialize the stack bounds prior to checking the cookie Without this change reading of the cookie value calls into __asan_loadN which then tries to use SP, but SP is not yet initialzed, so the asan code itself trigger a STACK_OVERFLOW_CHECK error. This fixes the asan.test_stack test which was broken by #24314 but went unnoticed because we don't run all the asan tests in emscripten CI. --- .circleci/config.yml | 1 + src/postamble_minimal.js | 2 +- src/preamble.js | 8 ++++---- test/core/test_stack.c | 6 ++++++ 4 files changed, 12 insertions(+), 5 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 6cfce5c20d8a3..5d9cf5ed800e1 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -539,6 +539,7 @@ jobs: title: "asan+lsan" test_targets: " asan.test_stat + asan.test_stack asan.test_float_builtins asan.test_embind* asan.test_abort_on_exceptions diff --git a/src/postamble_minimal.js b/src/postamble_minimal.js index 40661ce31220b..5434052200d3c 100644 --- a/src/postamble_minimal.js +++ b/src/postamble_minimal.js @@ -60,10 +60,10 @@ function initRuntime(wasmExports) { #if STACK_OVERFLOW_CHECK _emscripten_stack_init(); - writeStackCookie(); #if STACK_OVERFLOW_CHECK >= 2 setStackLimits(); #endif + writeStackCookie(); #endif #if PTHREADS diff --git a/src/preamble.js b/src/preamble.js index 0268d3edc65a1..59237dec0ba2f 100644 --- a/src/preamble.js +++ b/src/preamble.js @@ -199,14 +199,14 @@ function initRuntime() { if (ENVIRONMENT_IS_PTHREAD) return startWorker(Module); #endif -#if STACK_OVERFLOW_CHECK - checkStackCookie(); -#endif - #if STACK_OVERFLOW_CHECK >= 2 setStackLimits(); #endif +#if STACK_OVERFLOW_CHECK + checkStackCookie(); +#endif + #if RELOCATABLE callRuntimeCallbacks(__RELOC_FUNCS__); #endif diff --git a/test/core/test_stack.c b/test/core/test_stack.c index 15121db14bda6..9c89f4df3396b 100644 --- a/test/core/test_stack.c +++ b/test/core/test_stack.c @@ -5,7 +5,9 @@ * found in the LICENSE file. */ +#include #include + int test(int i) { int x = 10; int ret = (long)&x; // both for the number, and forces x to not be nativized @@ -17,11 +19,15 @@ int test(int i) { } return ret; } + int main(int argc, char **argv) { // We should get the same value for the first and last - stack has unwound + printf("in main\n"); int x1 = test(argc - 2); int x2 = test(100); int x3 = test((argc - 2) / 4); + assert(x2 != x1); + assert(x3 == x1); printf("*%d,%d*\n", x3 - x1, x2 != x1); return 0; }