Skip to content

Commit 383a2de

Browse files
committed
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 emscripten-core#24314 but went unnoticed because we don't run all the asan tests in emscripten CI.
1 parent 8e3d62c commit 383a2de

File tree

4 files changed

+12
-5
lines changed

4 files changed

+12
-5
lines changed

.circleci/config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,7 @@ jobs:
539539
title: "asan+lsan"
540540
test_targets: "
541541
asan.test_stat
542+
asan.test_stack
542543
asan.test_float_builtins
543544
asan.test_embind*
544545
asan.test_abort_on_exceptions

src/postamble_minimal.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ function initRuntime(wasmExports) {
6060

6161
#if STACK_OVERFLOW_CHECK
6262
_emscripten_stack_init();
63-
writeStackCookie();
6463
#if STACK_OVERFLOW_CHECK >= 2
6564
setStackLimits();
6665
#endif
66+
writeStackCookie();
6767
#endif
6868

6969
#if PTHREADS

src/preamble.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,14 +199,14 @@ function initRuntime() {
199199
if (ENVIRONMENT_IS_PTHREAD) return startWorker(Module);
200200
#endif
201201

202-
#if STACK_OVERFLOW_CHECK
203-
checkStackCookie();
204-
#endif
205-
206202
#if STACK_OVERFLOW_CHECK >= 2
207203
setStackLimits();
208204
#endif
209205

206+
#if STACK_OVERFLOW_CHECK
207+
checkStackCookie();
208+
#endif
209+
210210
#if RELOCATABLE
211211
callRuntimeCallbacks(__RELOC_FUNCS__);
212212
#endif

test/core/test_stack.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
* found in the LICENSE file.
66
*/
77

8+
#include <assert.h>
89
#include <stdio.h>
10+
911
int test(int i) {
1012
int x = 10;
1113
int ret = (long)&x; // both for the number, and forces x to not be nativized
@@ -17,11 +19,15 @@ int test(int i) {
1719
}
1820
return ret;
1921
}
22+
2023
int main(int argc, char **argv) {
2124
// We should get the same value for the first and last - stack has unwound
25+
printf("in main\n");
2226
int x1 = test(argc - 2);
2327
int x2 = test(100);
2428
int x3 = test((argc - 2) / 4);
29+
assert(x2 != x1);
30+
assert(x3 == x1);
2531
printf("*%d,%d*\n", x3 - x1, x2 != x1);
2632
return 0;
2733
}

0 commit comments

Comments
 (0)