Skip to content

Commit ffdc688

Browse files
authored
Adjust initial memory to handle asan shadow size (#14631)
Without this change any user of ALLOW_MEMORY_GROWTH with asan would get a rather cryptic error message from the linker: wasm-ld: error: initial memory too small, 278278624 bytes needed This is because asan increases the memory requirements in proportion to the max memory, and the max memory defaults to 2Gb with `ALLOW_MEMORY_GROWTH` enabled. Also, remove check for `MAXIMUM_MEMORY` == -1 which is no longer a thing since #14088. Fixes: #14621
1 parent 156c9f2 commit ffdc688

File tree

4 files changed

+23
-9
lines changed

4 files changed

+23
-9
lines changed

emcc.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2131,12 +2131,25 @@ def check_memory_setting(setting):
21312131
max_mem = settings.INITIAL_MEMORY
21322132
if settings.ALLOW_MEMORY_GROWTH:
21332133
max_mem = settings.MAXIMUM_MEMORY
2134-
if max_mem == -1:
2135-
exit_with_error('ASan requires a finite MAXIMUM_MEMORY')
21362134

21372135
shadow_size = max_mem // 8
21382136
settings.GLOBAL_BASE = shadow_size
21392137

2138+
sanitizer_mem = (shadow_size + webassembly.WASM_PAGE_SIZE) & ~webassembly.WASM_PAGE_SIZE
2139+
# sanitizers do at least 9 page allocs of a single page during startup.
2140+
sanitizer_mem += webassembly.WASM_PAGE_SIZE * 9
2141+
# we also allocate at least 11 "regions". Each region is kRegionSize (2 << 20) but
2142+
# MmapAlignedOrDieOnFatalError adds another 2 << 20 for alignment.
2143+
sanitizer_mem += (1 << 21) * 11
2144+
# When running in the threaded mode asan needs to allocate an array of kMaxNumberOfThreads
2145+
# (1 << 22) pointers. See compiler-rt/lib/asan/asan_thread.cpp.
2146+
if settings.USE_PTHREADS:
2147+
sanitizer_mem += (1 << 22) * 4
2148+
2149+
# Increase the size of the initial memory according to how much memory
2150+
# we think the sanitizers will use.
2151+
settings.INITIAL_MEMORY += sanitizer_mem
2152+
21402153
if settings.SAFE_HEAP:
21412154
# SAFE_HEAP instruments ASan's shadow memory accesses.
21422155
# Since the shadow memory starts at 0, the act of accessing the shadow memory is detected

tests/common.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ def setUp(self):
400400
super().setUp()
401401
self.settings_mods = {}
402402
self.emcc_args = ['-Werror']
403-
self.node_args = []
403+
self.node_args = ['--stack-trace-limit=50']
404404
self.v8_args = []
405405
self.env = {}
406406
self.temp_files_before_run = []

tests/test_core.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8244,6 +8244,7 @@ def test_pthread_c11_threads(self):
82448244
def test_pthread_cxx_threads(self):
82458245
self.set_setting('PROXY_TO_PTHREAD')
82468246
self.clear_setting('ALLOW_MEMORY_GROWTH')
8247+
self.set_setting('INITIAL_MEMORY', '64Mb')
82478248
self.set_setting('EXIT_RUNTIME')
82488249
self.do_run_in_out_file_test('pthread/test_pthread_cxx_threads.cpp')
82498250

@@ -8600,9 +8601,9 @@ def setUp(self):
86008601
strict = make_run('strict', emcc_args=[], settings={'STRICT': 1})
86018602

86028603
lsan = make_run('lsan', emcc_args=['-fsanitize=leak', '--profiling', '-O2'], settings={'ALLOW_MEMORY_GROWTH': 1})
8603-
asan = make_run('asan', emcc_args=['-fsanitize=address', '--profiling', '-O2'], settings={'ALLOW_MEMORY_GROWTH': 1, 'INITIAL_MEMORY': '500mb'})
8604+
asan = make_run('asan', emcc_args=['-fsanitize=address', '--profiling', '-O2'], settings={'ALLOW_MEMORY_GROWTH': 1})
86048605
asani = make_run('asani', emcc_args=['-fsanitize=address', '--profiling', '-O2', '--pre-js', os.path.join(os.path.dirname(__file__), 'asan-no-leak.js')],
8605-
settings={'ALLOW_MEMORY_GROWTH': 1, 'INITIAL_MEMORY': '500mb'})
8606+
settings={'ALLOW_MEMORY_GROWTH': 1})
86068607

86078608
# Experimental modes (not tested by CI)
86088609
lld = make_run('lld', emcc_args=[], settings={'LLD_REPORT_UNDEFINED': 1})

tests/test_other.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9177,21 +9177,21 @@ def test_lsan_no_stack_trace(self):
91779177

91789178
def test_asan_null_deref(self):
91799179
self.do_smart_test(test_file('other/test_asan_null_deref.c'),
9180-
emcc_args=['-fsanitize=address', '-sALLOW_MEMORY_GROWTH=1', '-sINITIAL_MEMORY=314572800'],
9180+
emcc_args=['-fsanitize=address', '-sALLOW_MEMORY_GROWTH=1'],
91819181
assert_returncode=NON_ZERO, literals=[
91829182
'AddressSanitizer: null-pointer-dereference on address',
91839183
])
91849184

91859185
def test_asan_no_stack_trace(self):
91869186
self.do_smart_test(test_file('other/test_lsan_leaks.c'),
9187-
emcc_args=['-fsanitize=address', '-sALLOW_MEMORY_GROWTH=1', '-sINITIAL_MEMORY=314572800', '-DDISABLE_CONTEXT', '-s', 'EXIT_RUNTIME'],
9187+
emcc_args=['-fsanitize=address', '-sALLOW_MEMORY_GROWTH=1', '-DDISABLE_CONTEXT', '-s', 'EXIT_RUNTIME'],
91889188
assert_returncode=NON_ZERO, literals=[
91899189
'Direct leak of 3427 byte(s) in 3 object(s) allocated from:',
91909190
'SUMMARY: AddressSanitizer: 3427 byte(s) leaked in 3 allocation(s).',
91919191
])
91929192

91939193
def test_asan_pthread_stubs(self):
9194-
self.do_smart_test(test_file('other/test_asan_pthread_stubs.c'), emcc_args=['-fsanitize=address', '-sALLOW_MEMORY_GROWTH=1', '-sINITIAL_MEMORY=314572800'])
9194+
self.do_smart_test(test_file('other/test_asan_pthread_stubs.c'), emcc_args=['-fsanitize=address', '-sALLOW_MEMORY_GROWTH=1'])
91959195

91969196
@node_pthreads
91979197
def test_proxy_to_pthread_stack(self):
@@ -9292,7 +9292,7 @@ def test_mmap_and_munmap_anonymous(self):
92929292
self.do_other_test('test_mmap_and_munmap_anonymous.cpp', emcc_args=['-s', 'NO_FILESYSTEM'])
92939293

92949294
def test_mmap_and_munmap_anonymous_asan(self):
9295-
self.do_other_test('test_mmap_and_munmap_anonymous.cpp', emcc_args=['-s', 'NO_FILESYSTEM', '-fsanitize=address', '-s', 'ALLOW_MEMORY_GROWTH', '-sINITIAL_MEMORY=314572800'])
9295+
self.do_other_test('test_mmap_and_munmap_anonymous.cpp', emcc_args=['-s', 'NO_FILESYSTEM', '-fsanitize=address', '-s', 'ALLOW_MEMORY_GROWTH'])
92969296

92979297
def test_mmap_memorygrowth(self):
92989298
self.do_other_test('test_mmap_memorygrowth.cpp', ['-s', 'ALLOW_MEMORY_GROWTH'])

0 commit comments

Comments
 (0)