Skip to content

Commit 205c01a

Browse files
authored
Fix stack cookie location used by STACK_OVERFLOW_CHECK. NFC (#16422)
I believe these values have always been written the wrong place (off by one word) since they were adapted for the downward growing stack in #8811. In the downward growing stack that last work in the stack lives a `max` and not `max + 4`.
1 parent 1521aad commit 205c01a

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/runtime_stack_check.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ function writeStackCookie() {
1111
#if ASSERTIONS
1212
assert((max & 3) == 0);
1313
#endif
14-
// The stack grows downwards
15-
{{{ makeSetValue('max + 4', 0, '0x2135467', 'i32' ) }}};
16-
{{{ makeSetValue('max + 8', 0, '0x89BACDFE', 'i32' ) }}};
14+
// The stack grow downwards towards _emscripten_stack_get_end.
15+
// We write cookies to the final two words in the stack and detect if they are
16+
// ever overwritten.
17+
{{{ makeSetValue('max', 0, '0x2135467', 'i32' ) }}};
18+
{{{ makeSetValue('max', 4, '0x89BACDFE', 'i32' ) }}};
1719
#if !USE_ASAN && !SAFE_HEAP // ASan and SAFE_HEAP check address 0 themselves
1820
// Also test the global address 0 for integrity.
1921
HEAP32[0] = 0x63736d65; /* 'emsc' */
@@ -25,8 +27,8 @@ function checkStackCookie() {
2527
if (ABORT) return;
2628
#endif
2729
var max = _emscripten_stack_get_end();
28-
var cookie1 = {{{ makeGetValue('max + 4', '0', 'i32', 0, true) }}};
29-
var cookie2 = {{{ makeGetValue('max + 8', '0', 'i32', 0, true) }}};
30+
var cookie1 = {{{ makeGetValue('max', 0, 'i32', 0, true) }}};
31+
var cookie2 = {{{ makeGetValue('max', 4, 'i32', 0, true) }}};
3032
if (cookie1 != 0x2135467 || cookie2 != 0x89BACDFE) {
3133
abort('Stack overflow! Stack cookie has been overwritten, expected hex dwords 0x89BACDFE and 0x2135467, but received 0x' + cookie2.toString(16) + ' 0x' + cookie1.toString(16));
3234
}

0 commit comments

Comments
 (0)