Skip to content

Commit a8db2bb

Browse files
authored
Revert "Use __stdio_exit rather then fflush(0) on exit (#16130)" (#17046)
This reverts commit eff9ae7. I decided to go this route rather than try to keeping `__stdio_exit` as in #17044.
1 parent 085b378 commit a8db2bb

17 files changed

+60
-28
lines changed

ChangeLog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ See docs/process.md for more on how version tagging works.
2222
------
2323
- Fix crash, introduced in 3.1.11, which occurred when using pointer types
2424
(types ending in `*`) with getValue/setValue library functions. (#17028)
25+
- Fix possible deadlock in multi-threaded builds that use EXIT_RUNTIME=0 with
26+
ASSERTIONS enabled. This was introduced in 3.1.3 as part of #16130. (#17044)
2527

2628
3.1.11 - 05/21/2022
2729
-------------------

emcc.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2100,12 +2100,11 @@ def phase_linker_setup(options, state, newargs, user_settings):
21002100
]
21012101

21022102
if not settings.STANDALONE_WASM and (settings.EXIT_RUNTIME or settings.ASSERTIONS):
2103-
# We use __stdio_exit to shut down musl's stdio subsystems and flush
2104-
# streams on exit.
2105-
# We only include it if the runtime is exitable, or when ASSERTIONS
2103+
# to flush streams on FS exit, we need to be able to call fflush
2104+
# we only include it if the runtime is exitable, or when ASSERTIONS
21062105
# (ASSERTIONS will check that streams do not need to be flushed,
21072106
# helping people see when they should have enabled EXIT_RUNTIME)
2108-
settings.EXPORT_IF_DEFINED += ['__stdio_exit']
2107+
settings.EXPORT_IF_DEFINED += ['fflush']
21092108

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

src/library_fs.js

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

src/library_wasi.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,8 @@ var WasiLibrary = {
231231
$flush_NO_FILESYSTEM__deps: ['$printChar', '$printCharBuffers'],
232232
$flush_NO_FILESYSTEM: function() {
233233
// flush anything remaining in the buffers during shutdown
234-
#if hasExportedFunction('___stdio_exit')
235-
___stdio_exit();
234+
#if hasExportedFunction('_fflush')
235+
_fflush(0);
236236
#endif
237237
if (printCharBuffers[1].length) printChar(1, {{{ charCode("\n") }}});
238238
if (printCharBuffers[2].length) printChar(2, {{{ charCode("\n") }}});

src/postamble.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -371,11 +371,10 @@ function checkUnflushedContent() {
371371
has = true;
372372
}
373373
try { // it doesn't matter if it fails
374-
#if SYSCALLS_REQUIRE_FILESYSTEM == 0
375-
var flush = {{{ '$flush_NO_FILESYSTEM' in addedLibraryItems ? 'flush_NO_FILESYSTEM' : 'null' }}};
376-
if (flush) flush();
377-
#elif hasExportedFunction('___stdio_exit')
378-
___stdio_exit();
374+
#if SYSCALLS_REQUIRE_FILESYSTEM == 0 && '$flush_NO_FILESYSTEM' in addedLibraryItems
375+
flush_NO_FILESYSTEM();
376+
#elif hasExportedFunction('_fflush')
377+
_fflush(0);
379378
#endif
380379
#if '$FS' in addedLibraryItems && '$TTY' in addedLibraryItems
381380
// also flush in the JS FS layer

src/postamble_minimal.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ function run() {
3232

3333
#endif
3434

35-
#if IN_TEST_HARNESS && hasExportedFunction('___stdio_exit')
35+
#if IN_TEST_HARNESS && hasExportedFunction('_flush')
3636
// flush any stdio streams for test harness, since there are existing
3737
// tests that depend on this behavior.
3838
// For production use, instead print full lines to avoid this kind of lazy
3939
// behavior.
40-
___stdio_exit();
40+
_fflush();
4141
#endif
4242

4343
#if EXIT_RUNTIME

src/runtime_wasm64.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function instrumentWasmExportsForMemory64(exports) {
2828
var r = Number(original(BigInt(x ? x : 0)));
2929
return r;
3030
};
31-
} else if (['setThrew', 'free', 'stackRestore', '__cxa_is_pointer_type'].includes(name)) {
31+
} else if (['fflush', 'setThrew', 'free', 'stackRestore', '__cxa_is_pointer_type'].includes(name)) {
3232
// get one i64
3333
replacement = (x) => {
3434
original(BigInt(x));

tests/other/metadce/test_metadce_hello_O0.exports

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
__errno_location
22
__indirect_function_table
3-
__stdio_exit
43
__wasm_call_ctors
54
dynCall_jiji
65
emscripten_stack_get_base
76
emscripten_stack_get_end
87
emscripten_stack_get_free
98
emscripten_stack_init
9+
fflush
1010
main
1111
memory
1212
stackAlloc

tests/other/metadce/test_metadce_hello_O0.funcs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,23 @@ $__lockfile
1010
$__lshrti3
1111
$__memcpy
1212
$__ofl_lock
13+
$__ofl_unlock
1314
$__original_main
14-
$__stdio_exit
1515
$__stdio_write
1616
$__syscall_getpid
1717
$__towrite
1818
$__trunctfdf2
19+
$__unlock
1920
$__unlockfile
2021
$__vfprintf_internal
2122
$__wasi_syscall_ret
2223
$__wasm_call_ctors
23-
$close_file
2424
$dynCall_jiji
2525
$emscripten_stack_get_base
2626
$emscripten_stack_get_end
2727
$emscripten_stack_get_free
2828
$emscripten_stack_init
29+
$fflush
2930
$fmt_fp
3031
$fmt_o
3132
$fmt_u
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
27043
1+
26996

0 commit comments

Comments
 (0)