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:
https://github.com/bytecodealliance/wasmtime/issues/7833
This commit is contained in:
Ayke van Laethem
2024-02-16 16:33:07 +01:00
committed by Ron Evans
parent 55f7d21ff5
commit 3021e16bbf
3 changed files with 10 additions and 0 deletions
+8
View File
@@ -21,6 +21,11 @@ func fd_write(id uint32, iovs *__wasi_iovec_t, iovs_len uint, nwritten *uint) (e
//go:wasmimport wasi_snapshot_preview1 proc_exit
func proc_exit(exitcode uint32)
// Flush stdio on exit.
//
//export __stdio_exit
func __stdio_exit()
const (
putcharBufferSize = 120
stdout = 1
@@ -72,6 +77,9 @@ func abort() {
//go:linkname syscall_Exit syscall.Exit
func syscall_Exit(code int) {
// TODO: should we call __stdio_exit here?
// It's a low-level exit (syscall.Exit) so doing any libc stuff seems
// unexpected, but then where else should stdio buffers be flushed?
proc_exit(uint32(code))
}
+1
View File
@@ -19,6 +19,7 @@ func _start() {
heapStart = uintptr(unsafe.Pointer(&heapStartSymbol))
heapEnd = uintptr(wasm_memory_size(0) * wasmPageSize)
run()
__stdio_exit()
}
// Read the command line arguments from WASI.
+1
View File
@@ -18,6 +18,7 @@ func _start() {
wasmNested = true
run()
__stdio_exit()
wasmNested = false
}