Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/lib/libpthread.js
Original file line number Diff line number Diff line change
Expand Up @@ -978,7 +978,7 @@ var LibraryPThread = {

$establishStackSpace__internal: true,
$establishStackSpace__deps: ['$stackRestore', 'emscripten_stack_set_limits'],
$establishStackSpace: (pthread_ptr) => {
$establishStackSpace: function (pthread_ptr) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why did this change?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was the easy way to add another node.id.name check to skip the establishStackSpace helper too, as we skip specific function declarations.

I could instead add a match to find variable declarator with a specific name and with arrow function expression initializer, but it would look somewhat more complex.

var stackHigh = {{{ makeGetValue('pthread_ptr', C_STRUCTS.pthread.stack, '*') }}};
var stackSize = {{{ makeGetValue('pthread_ptr', C_STRUCTS.pthread.stack_size, '*') }}};
var stackLow = stackHigh - stackSize;
Expand Down
17 changes: 11 additions & 6 deletions src/runtime_asan.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,23 @@
// ASan instrumentation on them. However, until the wasm module is ready, we
// must access things directly.

function _asan_js_load(arr, index) {
function _asan_js_check_index(arr, index, asanFn) {
#if EXIT_RUNTIME
if (runtimeInitialized && !runtimeExited) {
#else
if (runtimeInitialized) {
#endif
const elemSize = arr.BYTES_PER_ELEMENT;
___asan_loadN(index * elemSize, elemSize);
asanFn(index * elemSize, elemSize);
}
}

function _asan_js_load(arr, index) {
_asan_js_check_index(arr, index, ___asan_loadN);
return arr[index];
}

function _asan_js_store(arr, index, value) {
if (runtimeInitialized) {
const elemSize = arr.BYTES_PER_ELEMENT;
___asan_storeN(index * elemSize, elemSize);
}
_asan_js_check_index(arr, index, ___asan_storeN);
return arr[index] = value;
}
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_minimal_pthreads.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3785
3786
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_minimal_pthreads.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7816
7827
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3985
3989
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8247
8258
File renamed without changes.
8 changes: 0 additions & 8 deletions test/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -9084,14 +9084,6 @@ def test_asan_modularized_with_closure(self):
self.set_setting('INITIAL_MEMORY', '300mb')
self.do_run_in_out_file_test('hello_world.c')

@no_asan('SAFE_HEAP cannot be used with ASan')
@no_2gb('asan doesnt support GLOBAL_BASE')
@no_esm_integration('sanitizers do not support WASM_ESM_INTEGRATION')
def test_safe_heap_user_js(self):
self.set_setting('SAFE_HEAP')
self.do_runf('core/test_safe_heap_user_js.c',
expected_output=['Aborted(segmentation fault storing 1 bytes at address 0)'], assert_returncode=NON_ZERO)

def test_safe_stack(self):
self.set_setting('STACK_OVERFLOW_CHECK', 2)
self.set_setting('STACK_SIZE', 1024)
Expand Down
18 changes: 18 additions & 0 deletions test/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -12235,6 +12235,24 @@ def test_asan_strncpy(self):
# https://github.com/emscripten-core/emscripten/issues/14618
self.do_runf('other/test_asan_strncpy.c', emcc_args=['-fsanitize=address'])

@parameterized({
'asan': ['AddressSanitizer: null-pointer-dereference', '-fsanitize=address'],
'safe_heap': ['Aborted(segmentation fault storing 1 bytes at address 0)', '-sSAFE_HEAP'],
})
@parameterized({
'': [],
'memgrowth': ['-pthread', '-sALLOW_MEMORY_GROWTH', '-Wno-pthreads-mem-growth'],
})
def test_null_deref_via_js(self, expected_output, *args):
# Multiple JS transforms look for pattern like `HEAPxx[...]` and transform it.
# This test ensures that one of the transforms doesn't produce a pattern that
# another pass can't find anymore, that is that features can work in conjunction.
self.do_runf(
'other/test_safe_heap_user_js.c',
emcc_args=args,
assert_returncode=NON_ZERO,
expected_output=[expected_output])

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps a single test with all 3 features enabled is enough?

@RReverser RReverser May 12, 2025

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be enough to reproduce the issue, but I found that having separate tests helps to narrow down which particular transform is at fault. Just really putting "unit" into the "unit tests".


@node_pthreads
def test_proxy_to_pthread_stack(self):
# Check that the proxied main gets run with STACK_SIZE setting and not
Expand Down
5 changes: 4 additions & 1 deletion tools/acorn-optimizer.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1372,7 +1372,10 @@ function isHEAPAccess(node) {
function asanify(ast) {
recursiveWalk(ast, {
FunctionDeclaration(node, c) {
if (node.id.type === 'Identifier' && node.id.name.startsWith('_asan_js_')) {
if (
node.id.type === 'Identifier' &&
(node.id.name.startsWith('_asan_js_') || node.id.name === 'establishStackSpace')

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How did we get away without this before this change?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without fixes in this PR some heap patterns (in particular, in memory growth + asan tests) were never asanified.

That's what the PR was testing & fixing after all, and once fixed, it started hitting new code paths.

) {
// do not recurse into this js impl function, which we use during
// startup before the wasm is ready
} else {
Expand Down
22 changes: 12 additions & 10 deletions tools/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -2322,31 +2322,33 @@ def phase_binaryen(target, options, wasm_target):
# after generating the wasm, do some final operations

if final_js:
if settings.SUPPORT_BIG_ENDIAN:
with ToolchainProfiler.profile_block('little_endian_heap'):
final_js = building.little_endian_heap(final_js)

# >=2GB heap support requires pointers in JS to be unsigned. rather than
# require all pointers to be unsigned by default, which increases code size
# a little, keep them signed, and just unsign them here if we need that.
if settings.CAN_ADDRESS_2GB:
with ToolchainProfiler.profile_block('use_unsigned_pointers_in_js'):
final_js = building.use_unsigned_pointers_in_js(final_js)

if settings.USE_ASAN:
final_js = building.instrument_js_for_asan(final_js)

if settings.SAFE_HEAP:
final_js = building.instrument_js_for_safe_heap(final_js)

# shared memory growth requires some additional JS fixups.
# note that we must do this after handling of unsigned pointers. unsigning
# adds some >>> 0 things, while growth will replace a HEAP8 with a call to
# a method to get the heap, and that call would not be recognized by the
# unsigning pass
# unsigning pass.
# we also must do this after the asan or safe_heap instrumentation, as they
# wouldn't be able to recognize patterns produced by the growth pass.
if settings.SHARED_MEMORY and settings.ALLOW_MEMORY_GROWTH:
with ToolchainProfiler.profile_block('apply_wasm_memory_growth'):
final_js = building.apply_wasm_memory_growth(final_js)

if settings.USE_ASAN:
final_js = building.instrument_js_for_asan(final_js)

if settings.SAFE_HEAP:
final_js = building.instrument_js_for_safe_heap(final_js)
if settings.SUPPORT_BIG_ENDIAN:
with ToolchainProfiler.profile_block('little_endian_heap'):
final_js = building.little_endian_heap(final_js)

if settings.OPT_LEVEL >= 2 and settings.DEBUG_LEVEL <= 2:
# minify the JS. Do not minify whitespace if Closure is used, so that
Expand Down