Skip to content

Commit 295ddd9

Browse files
authored
Skip export wrapper for stackRestore function. (#21554)
The `stackRestore` function used by `invoke_xxx` wrappers and gets called as the stack unwinds after a call to `exit`. Alternatively we could add a check in each of the `invoke_xxx` wrappers to avoid calling `stackRestore`, but that doesn't seem worth it. Fixes: #21474
1 parent 1201861 commit 295ddd9

File tree

4 files changed

+46
-9
lines changed

4 files changed

+46
-9
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
void foo() {
5+
printf("foo\n");
6+
exit(0);
7+
}
8+
9+
int main() {
10+
printf("main\n");
11+
try {
12+
// `foo` calls `exit()` which triggers the runtime to exit and then
13+
// the stack to unwind.
14+
// This test verifies that we don't error during the unwinding due
15+
// to the runtime having already exited.
16+
foo();
17+
} catch(...) {
18+
printf("caught\n");
19+
}
20+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
main
2+
foo

test/test_other.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8998,6 +8998,19 @@ def test_exceptions_rethrow_stack_trace_and_message(self, wasm_eh):
89988998
expected_output=rethrow_stack_trace_checks, regex=True)
89998999
self.assertNotContained('important_function', err)
90009000

9001+
@parameterized({
9002+
'': (False,),
9003+
'wasm': (True,),
9004+
})
9005+
def test_exceptions_exit_runtime(self, wasm_eh):
9006+
self.set_setting('EXIT_RUNTIME')
9007+
if wasm_eh:
9008+
self.require_wasm_eh()
9009+
self.emcc_args.append('-fwasm-exceptions')
9010+
else:
9011+
self.set_setting('DISABLE_EXCEPTION_CATCHING', 0)
9012+
self.do_other_test('test_exceptions_exit_runtime.cpp')
9013+
90019014
@requires_node
90029015
def test_jsrun(self):
90039016
print(config.NODE_JS)

tools/emscripten.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -822,18 +822,20 @@ def make_export_wrappers(function_exports):
822822

823823
wrappers = []
824824

825-
# The emscripten stack functions are called very early (by writeStackCookie) before
826-
# the runtime is initialized so we can't create these wrappers that check for
827-
# runtimeInitialized.
828-
# Likewise `__trap` can occur before the runtime is initialized since it is used in
829-
# abort.
830-
# pthread_self and _emscripten_proxy_execute_task_queue are currently called in some
831-
# cases after the runtime has exited.
832-
# TODO: Look into removing these, and improving our robustness around thread termination.
833825
def install_wrapper(sym):
826+
# The emscripten stack functions are called very early (by writeStackCookie) before
827+
# the runtime is initialized so we can't create these wrappers that check for
828+
# runtimeInitialized.
834829
if sym.startswith('_asan_') or sym.startswith('emscripten_stack_'):
835830
return False
836-
if sym in ('__trap', 'pthread_self', '_emscripten_proxy_execute_task_queue'):
831+
# Likewise `__trap` can occur before the runtime is initialized since it is used in
832+
# abort.
833+
# stackRestore is often called during stack unwinding (e.g. by invoke_xxx function) which
834+
# runs after the runtime has exited.
835+
# pthread_self and _emscripten_proxy_execute_task_queue are currently called in some
836+
# cases after the runtime has exited.
837+
# TODO: Look into removing these, and improving our robustness around thread termination.
838+
if sym in ('__trap', 'pthread_self', '_emscripten_proxy_execute_task_queue', 'stackRestore'):
837839
return False
838840
return True
839841

0 commit comments

Comments
 (0)