Skip to content

Commit eff9ae7

Browse files
authored
Use __stdio_exit rather then fflush(0) on exit (#16130)
This is how musl internally closes down stdio after main returns (and its how we do it in standalone mode).
1 parent 3b43fe8 commit eff9ae7

14 files changed

+27
-26
lines changed

emcc.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1985,12 +1985,13 @@ def default_setting(name, new_default):
19851985
]
19861986

19871987
if settings.FILESYSTEM and not settings.STANDALONE_WASM:
1988-
# to flush streams on FS exit, we need to be able to call fflush
1989-
# we only include it if the runtime is exitable, or when ASSERTIONS
1988+
# We use __stdio_exit to shut down musl's stdio subsystems and flush
1989+
# streams on exit.
1990+
# We only include it if the runtime is exitable, or when ASSERTIONS
19901991
# (ASSERTIONS will check that streams do not need to be flushed,
19911992
# helping people see when they should have enabled EXIT_RUNTIME)
19921993
if settings.EXIT_RUNTIME or settings.ASSERTIONS:
1993-
settings.EXPORT_IF_DEFINED += ['fflush']
1994+
settings.EXPORT_IF_DEFINED += ['__stdio_exit']
19941995

19951996
if settings.SUPPORT_ERRNO:
19961997
# so setErrNo JS library function can report errno back to C

src/library_fs.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1490,9 +1490,10 @@ FS.staticInit();` +
14901490
},
14911491
quit: () => {
14921492
FS.init.initialized = false;
1493-
// force-flush all streams, so we get musl std streams printed out
1494-
#if hasExportedFunction('_fflush')
1495-
_fflush(0);
1493+
// Call musl-internal function to close all stdio streams, so nothing is
1494+
// left in internal buffers.
1495+
#if hasExportedFunction('___stdio_exit')
1496+
___stdio_exit();
14961497
#endif
14971498
// close all of our streams
14981499
for (var i = 0; i < FS.streams.length; i++) {

src/library_wasi.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,9 @@ var WasiLibrary = {
183183
#if SYSCALLS_REQUIRE_FILESYSTEM == 0 && (!MINIMAL_RUNTIME || EXIT_RUNTIME)
184184
$flush_NO_FILESYSTEM: function() {
185185
// flush anything remaining in the buffers during shutdown
186-
if (typeof _fflush !== 'undefined') _fflush({{{ pointerT(0) }}});
186+
#if hasExportedFunction('___stdio_exit')
187+
___stdio_exit();
188+
#endif
187189
var buffers = SYSCALLS.buffers;
188190
if (buffers[1].length) SYSCALLS.printChar(1, {{{ charCode("\n") }}});
189191
if (buffers[2].length) SYSCALLS.printChar(2, {{{ charCode("\n") }}});

src/postamble.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,8 +364,8 @@ function checkUnflushedContent() {
364364
#if SYSCALLS_REQUIRE_FILESYSTEM == 0
365365
var flush = {{{ '$flush_NO_FILESYSTEM' in addedLibraryItems ? 'flush_NO_FILESYSTEM' : 'null' }}};
366366
if (flush) flush();
367-
#elif hasExportedFunction('_fflush')
368-
_fflush(0);
367+
#elif hasExportedFunction('___stdio_exit')
368+
___stdio_exit();
369369
#endif
370370
#if '$FS' in addedLibraryItems && '$TTY' in addedLibraryItems
371371
// also flush in the JS FS layer

src/postamble_minimal.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ function run() {
3131
PThread.terminateAllThreads();
3232
#endif
3333

34-
#if IN_TEST_HARNESS
35-
// fflush() filesystem stdio for test harness, since there are existing
34+
#if IN_TEST_HARNESS && hasExportedFunction('___stdio_exit')
35+
// flush any stdio streams for test harness, since there are existing
3636
// tests that depend on this behavior.
3737
// For production use, instead print full lines to avoid this kind of lazy
3838
// behavior.
39-
if (typeof _fflush !== 'undefined') _fflush();
39+
___stdio_exit();
4040
#endif
4141

4242
#if ASSERTIONS

tests/other/metadce/hello_world.exports

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
__errno_location
22
__indirect_function_table
3+
__stdio_exit
34
__wasm_call_ctors
45
dynCall_jiji
56
emscripten_stack_get_end
67
emscripten_stack_get_free
78
emscripten_stack_init
8-
fflush
99
main
1010
memory
1111
stackAlloc

tests/other/metadce/hello_world.funcs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,21 @@ $__lockfile
1010
$__lshrti3
1111
$__memcpy
1212
$__ofl_lock
13-
$__ofl_unlock
1413
$__original_main
14+
$__stdio_exit
1515
$__stdio_write
1616
$__syscall_getpid
1717
$__towrite
1818
$__trunctfdf2
19-
$__unlock
2019
$__unlockfile
2120
$__vfprintf_internal
2221
$__wasi_syscall_ret
2322
$__wasm_call_ctors
23+
$close_file
2424
$dynCall_jiji
2525
$emscripten_stack_get_end
2626
$emscripten_stack_get_free
2727
$emscripten_stack_init
28-
$fflush
2928
$fmt_fp
3029
$fmt_o
3130
$fmt_u
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
125367
1+
125354

tests/other/metadce/hello_world.size

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
12066
1+
11910
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
58444
1+
58390

0 commit comments

Comments
 (0)