Skip to content

Commit 3021e16

Browse files
aykevldeadprogram
authored andcommitted
wasm: call __stdio_exit on exit
This flushes stdio, so that functions like puts and printf write out all buffered data even if the output isn't connected to a terminal. For discussion, see: bytecodealliance/wasmtime#7833
1 parent 55f7d21 commit 3021e16

File tree

3 files changed

+10
-0
lines changed

3 files changed

+10
-0
lines changed

src/runtime/runtime_tinygowasm.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ func fd_write(id uint32, iovs *__wasi_iovec_t, iovs_len uint, nwritten *uint) (e
2121
//go:wasmimport wasi_snapshot_preview1 proc_exit
2222
func proc_exit(exitcode uint32)
2323

24+
// Flush stdio on exit.
25+
//
26+
//export __stdio_exit
27+
func __stdio_exit()
28+
2429
const (
2530
putcharBufferSize = 120
2631
stdout = 1
@@ -72,6 +77,9 @@ func abort() {
7277

7378
//go:linkname syscall_Exit syscall.Exit
7479
func syscall_Exit(code int) {
80+
// TODO: should we call __stdio_exit here?
81+
// It's a low-level exit (syscall.Exit) so doing any libc stuff seems
82+
// unexpected, but then where else should stdio buffers be flushed?
7583
proc_exit(uint32(code))
7684
}
7785

src/runtime/runtime_wasip1.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ func _start() {
1919
heapStart = uintptr(unsafe.Pointer(&heapStartSymbol))
2020
heapEnd = uintptr(wasm_memory_size(0) * wasmPageSize)
2121
run()
22+
__stdio_exit()
2223
}
2324

2425
// Read the command line arguments from WASI.

src/runtime/runtime_wasm_js.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ func _start() {
1818

1919
wasmNested = true
2020
run()
21+
__stdio_exit()
2122
wasmNested = false
2223
}
2324

0 commit comments

Comments
 (0)