Skip to content

Commit 1b0a261

Browse files
committed
Prevent runtime exit while main is running
See #17376 for the original report inspiration for the modified test.
1 parent 2286ce3 commit 1b0a261

File tree

4 files changed

+18
-7
lines changed

4 files changed

+18
-7
lines changed

emcc.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2492,6 +2492,10 @@ def check_memory_setting(setting):
24922492
# MINIMAL_RUNTIME only needs callRuntimeCallbacks in certain cases, but the normal runtime
24932493
# always does.
24942494
settings.DEFAULT_LIBRARY_FUNCS_TO_INCLUDE += ['$callRuntimeCallbacks']
2495+
if settings.EXIT_RUNTIME:
2496+
# Use by callMain
2497+
# TODO(sbc): have callMain use callUserCallback instead.
2498+
settings.DEFAULT_LIBRARY_FUNCS_TO_INCLUDE += ['$runtimeKeepalivePush', '$runtimeKeepalivePop']
24952499

24962500
if settings.EXIT_RUNTIME and not settings.STANDALONE_WASM:
24972501
# Internal function implemented in musl that calls any functions registered

src/postamble.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,9 @@ function callMain(args) {
167167
// that if we get here main returned zero.
168168
var ret = 0;
169169
#else
170+
{{{ runtimeKeepalivePush() }}}
170171
var ret = entryFunction(argc, argv);
172+
{{{ runtimeKeepalivePop() }}}
171173
#endif // STANDALONE_WASM
172174

173175
#if BENCHMARK

tests/browser/test_glfw3.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,14 @@ assert(glfwSet##Function(Value) == Value); /* The previously set callback */
4141
assert(glfwSet##Function(Window, Value) == NULL); /* Default value (no callback was set) */ \
4242
assert(glfwSet##Function(Window, Value) == Value); /* The previously set callback */
4343

44-
int main()
45-
{
44+
static int exited = 0;
45+
46+
__attribute__((destructor))
47+
void onExit() {
48+
exited = 1;
49+
}
50+
51+
int main() {
4652
GLFWwindow *window;
4753
char *userptr = "userptr";
4854

@@ -121,9 +127,11 @@ int main()
121127
glfwSetWindowSize(window, 1, 1);
122128
glfwGetWindowSize(window, &w, &h);
123129
assert(w == 1 && h == 1);
130+
assert(exited == 0);
124131

125132
glfwSetWindowSize(window, 640, 480);
126133
glfwGetFramebufferSize(window, &w, &h);
134+
assert(exited == 0);
127135

128136
// XXX: not implemented
129137
// glfwIconifyWindow(window);
@@ -210,9 +218,6 @@ int main()
210218
#endif
211219

212220
glfwTerminate();
213-
214-
#ifdef REPORT_RESULT
215-
REPORT_RESULT(1);
216-
#endif
221+
printf("done\n");
217222
return 0;
218223
}

tests/test_browser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2881,7 +2881,7 @@ def in_html(expected):
28812881
def test_glfw3(self, args):
28822882
for opts in [[], ['-sLEGACY_GL_EMULATION'], ['-Os', '--closure=1']]:
28832883
print(opts)
2884-
self.btest(test_file('browser/test_glfw3.c'), args=['-sUSE_GLFW=3', '-lglfw', '-lGL'] + args + opts, expected='1')
2884+
self.btest_exit(test_file('browser/test_glfw3.c'), args=['-sUSE_GLFW=3', '-lglfw', '-lGL'] + args + opts)
28852885

28862886
@requires_graphics_hardware
28872887
def test_glfw_events(self):

0 commit comments

Comments
 (0)