Skip to content

Commit 8d6fd04

Browse files
authored
Add setting do disable cookie at address zero (#19487)
This allows folks to depend on the contents of address zero always being zero. Fixes: #19389
1 parent b87f15a commit 8d6fd04

File tree

5 files changed

+22
-7
lines changed

5 files changed

+22
-7
lines changed

ChangeLog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ See docs/process.md for more on how version tagging works.
2020

2121
3.1.41 (in development)
2222
-----------------------
23+
- A new setting (`CHECK_NULL_WRITES`) was added to disabled the checking of
24+
address zero that is normally done when `STACK_OVERFLOW_CHECK` is enabled.
25+
(#19487)
2326

2427
3.1.40 - 05/30/23
2528
-----------------

emcc.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2733,6 +2733,10 @@ def check_memory_setting(setting):
27332733
# by SAFE_HEAP as a null pointer dereference.
27342734
exit_with_error('ASan does not work with SAFE_HEAP')
27352735

2736+
if settings.USE_ASAN or settings.SAFE_HEAP:
2737+
# ASan and SAFE_HEAP check address 0 themselves
2738+
settings.CHECK_NULL_WRITES = 0
2739+
27362740
if sanitize and settings.GENERATE_SOURCE_MAP:
27372741
settings.LOAD_SOURCE_MAP = 1
27382742

src/runtime_stack_check.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ function writeStackCookie() {
1515
assert((max & 3) == 0);
1616
#endif
1717
// If the stack ends at address zero we write our cookies 4 bytes into the
18-
// stack. This prevents interference with the (separate) address-zero check
19-
// below.
18+
// stack. This prevents interference with SAFE_HEAP and ASAN which also
19+
// monitor writes to address zero.
2020
if (max == 0) {
2121
max += 4;
2222
}
@@ -25,9 +25,9 @@ function writeStackCookie() {
2525
// ever overwritten.
2626
{{{ makeSetValue('max', 0, '0x02135467', 'u32') }}};
2727
{{{ makeSetValue('max', 4, '0x89BACDFE', 'u32') }}};
28-
#if !USE_ASAN && !SAFE_HEAP // ASan and SAFE_HEAP check address 0 themselves
28+
#if CHECK_NULL_WRITES
2929
// Also test the global address 0 for integrity.
30-
HEAPU32[0] = 0x63736d65; /* 'emsc' */
30+
{{{ makeSetValue(0, 0, 0x63736d65 /* 'emsc' */, 'u32') }}};
3131
#endif
3232
}
3333

@@ -48,9 +48,9 @@ function checkStackCookie() {
4848
if (cookie1 != 0x02135467 || cookie2 != 0x89BACDFE) {
4949
abort(`Stack overflow! Stack cookie has been overwritten at ${ptrToString(max)}, expected hex dwords 0x89BACDFE and 0x2135467, but received ${ptrToString(cookie2)} ${ptrToString(cookie1)}`);
5050
}
51-
#if !USE_ASAN && !SAFE_HEAP // ASan and SAFE_HEAP check address 0 themselves
51+
#if CHECK_NULL_WRITES
5252
// Also test the global address 0 for integrity.
53-
if (HEAPU32[0] !== 0x63736d65 /* 'emsc' */) {
53+
if ({{{ makeGetValue(0, 0, 'u32') }}} != 0x63736d65 /* 'emsc' */) {
5454
abort('Runtime error: The application has corrupted its heap memory area (address zero)!');
5555
}
5656
#endif

src/settings.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ var ASSERTIONS = 1;
6363
// [link]
6464
var STACK_OVERFLOW_CHECK = 0;
6565

66+
// When STACK_OVERFLOW_CHECK is enabled we also check writes to address zero.
67+
// This can help detect NULL pointer usage. If you want to skip this extra
68+
// check (for example, if you want reads from the address zero to always return
69+
// zero) you can disabled this here. This setting has no effect when
70+
// STACK_OVERFLOW_CHECK is disabled.
71+
var CHECK_NULL_WRITES = true;
72+
6673
// When set to 1, will generate more verbose output during compilation.
6774
// [general]
6875
var VERBOSE = false;

test/test_other.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,8 @@ def test_emcc_4(self, compiler):
521521
generated = read_file('something.js')
522522
main = self.get_func(generated, '_main') if 'function _main' in generated else generated
523523
assert 'new Uint16Array' in generated and 'new Uint32Array' in generated, 'typed arrays 2 should be used by default'
524-
assert 'SAFE_HEAP' not in generated, 'safe heap should not be used by default'
524+
assert 'SAFE_HEAP_LOAD' not in generated, 'safe heap should not be used by default'
525+
assert 'SAFE_HEAP_STORE' not in generated, 'safe heap should not be used by default'
525526
assert ': while(' not in main, 'when relooping we also js-optimize, so there should be no labelled whiles'
526527
if closure:
527528
if opt_level == 0:

0 commit comments

Comments
 (0)