Skip to content

Commit aa130a4

Browse files
authored
Add memset to emscripten_with_builtin_malloc (#11734)
This was previously causing crashes due to ASan-instrumented memset being used beneath the ASan boundary in the implementation of mmap. Unfortunately, these crashes only happened when the allocation sequence set the heap up in such a manner that the previously-instrumented memset would intersect a poison region on the heap, so we don't have a reproducer small enough to make a good test. As a drive-by, also remove the use of __sys_pipe from libasan so that it can link successfully with NO_FILESYSTEM.
1 parent bd98714 commit aa130a4

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed

emcc.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1912,9 +1912,15 @@ def include_and_export(name):
19121912

19131913
if sanitize:
19141914
shared.Settings.USE_OFFSET_CONVERTER = 1
1915-
shared.Settings.EXPORTED_FUNCTIONS += ['_memalign', '_emscripten_builtin_memalign',
1916-
'_emscripten_builtin_malloc', '_emscripten_builtin_free',
1917-
'___data_end', '___heap_base', '___global_base']
1915+
shared.Settings.EXPORTED_FUNCTIONS += [
1916+
'_memalign',
1917+
'_emscripten_builtin_memalign',
1918+
'_emscripten_builtin_malloc',
1919+
'_emscripten_builtin_free',
1920+
'___data_end',
1921+
'___heap_base',
1922+
'___global_base'
1923+
]
19181924

19191925
if not shared.Settings.WASM_BACKEND:
19201926
exit_with_error('Sanitizers are not compatible with the fastcomp backend. Please upgrade to the upstream wasm backend by following these instructions: https://v8.dev/blog/emscripten-llvm-wasm#testing')
@@ -1936,6 +1942,7 @@ def include_and_export(name):
19361942
shared.Settings.USE_ASAN = 1
19371943

19381944
shared.Settings.EXPORTED_FUNCTIONS += [
1945+
'_emscripten_builtin_memset',
19391946
'_asan_c_load_1', '_asan_c_load_1u',
19401947
'_asan_c_load_2', '_asan_c_load_2u',
19411948
'_asan_c_load_4', '_asan_c_load_4u',

src/library.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4455,7 +4455,11 @@ LibraryManager.library = {
44554455
#endif
44564456
},
44574457

4458-
$withBuiltinMalloc__deps: ['emscripten_builtin_malloc', 'emscripten_builtin_free', 'emscripten_builtin_memalign'],
4458+
$withBuiltinMalloc__deps: ['emscripten_builtin_malloc', 'emscripten_builtin_free', 'emscripten_builtin_memalign'
4459+
#if USE_ASAN
4460+
, 'emscripten_builtin_memset'
4461+
#endif
4462+
],
44594463
$withBuiltinMalloc__docs: '/** @suppress{checkTypes} */',
44604464
$withBuiltinMalloc: function (func) {
44614465
var prev_malloc = typeof _malloc !== 'undefined' ? _malloc : undefined;
@@ -4464,12 +4468,19 @@ LibraryManager.library = {
44644468
_malloc = _emscripten_builtin_malloc;
44654469
_memalign = _emscripten_builtin_memalign;
44664470
_free = _emscripten_builtin_free;
4471+
#if USE_ASAN
4472+
var prev_memset = typeof _memset !== 'undefined' ? _memset : undefined
4473+
_memset = _emscripten_builtin_memset;
4474+
#endif
44674475
try {
44684476
return func();
44694477
} finally {
44704478
_malloc = prev_malloc;
44714479
_memalign = prev_memalign;
44724480
_free = prev_free;
4481+
#if USE_ASAN
4482+
_memset = prev_memset;
4483+
#endif
44734484
}
44744485
},
44754486

system/lib/compiler-rt/lib/sanitizer_common/sanitizer_posix_libcdep.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,11 @@ bool SignalContext::IsStackOverflow() const {
277277
#endif // SANITIZER_GO
278278

279279
bool IsAccessibleMemoryRange(uptr beg, uptr size) {
280+
#if SANITIZER_EMSCRIPTEN
281+
// Avoid pulling in __sys_pipe for the trick below, which doesn't work on
282+
// WebAssembly anyways because there are no memory protections.
283+
return true;
284+
#else
280285
uptr page_size = GetPageSizeCached();
281286
// Checking too large memory ranges is slow.
282287
CHECK_LT(size, page_size * 10);
@@ -296,6 +301,7 @@ bool IsAccessibleMemoryRange(uptr beg, uptr size) {
296301
internal_close(sock_pair[0]);
297302
internal_close(sock_pair[1]);
298303
return result;
304+
#endif // SANITIZER_EMSCRIPTEN
299305
}
300306

301307
void PlatformPrepareForSandboxing(__sanitizer_sandbox_arguments *args) {

tests/test_other.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9563,6 +9563,10 @@ def test_mmap_and_munmap(self):
95639563
def test_mmap_and_munmap_anonymous(self):
95649564
self.do_other_test('mmap_and_munmap_anonymous', emcc_args=['-s', 'NO_FILESYSTEM'])
95659565

9566+
@no_fastcomp('asan is not supported on fastcomp')
9567+
def test_mmap_and_munmap_anonymous_asan(self):
9568+
self.do_other_test('mmap_and_munmap_anonymous', emcc_args=['-s', 'NO_FILESYSTEM', '-fsanitize=address', '-s', 'ALLOW_MEMORY_GROWTH=1'])
9569+
95669570
def test_mmap_memorygrowth(self):
95679571
self.do_other_test('mmap_memorygrowth', ['-s', 'ALLOW_MEMORY_GROWTH=1'])
95689572

0 commit comments

Comments
 (0)