From 0c6afb53a55ae30f6cb571f57d06f3a72281f557 Mon Sep 17 00:00:00 2001 From: Phil Kedy Date: Mon, 20 Jan 2025 23:20:40 -0500 Subject: [PATCH] Tweak to allow exit code 0 to panic to prevent executing the LLVM unreachable instruction. Signed-off-by: Phil Kedy --- imports/wasi_snapshot_preview1/proc.go | 9 ++++----- imports/wasi_snapshot_preview1/proc_test.go | 8 +++++--- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/imports/wasi_snapshot_preview1/proc.go b/imports/wasi_snapshot_preview1/proc.go index c8a9ed8d59..fa32f96582 100644 --- a/imports/wasi_snapshot_preview1/proc.go +++ b/imports/wasi_snapshot_preview1/proc.go @@ -28,14 +28,13 @@ var procExit = &wasm.HostFunc{ func procExitFn(ctx context.Context, mod api.Module, params []uint64) { exitCode := uint32(params[0]) + // TinyGo 0.35.0 calls proc_exit from _start, even for exit code 0. - if exitCode == 0 { - return + if exitCode != 0 { + // Ensure other callers see the exit code. + _ = mod.CloseWithExitCode(ctx, exitCode) } - // Ensure other callers see the exit code. - _ = mod.CloseWithExitCode(ctx, exitCode) - // Prevent any code from executing after this function. For example, LLVM // inserts unreachable instructions after calls to exit. // See: https://github.com/emscripten-core/emscripten/issues/12322 diff --git a/imports/wasi_snapshot_preview1/proc_test.go b/imports/wasi_snapshot_preview1/proc_test.go index 0dee6658d5..cbc86ee660 100644 --- a/imports/wasi_snapshot_preview1/proc_test.go +++ b/imports/wasi_snapshot_preview1/proc_test.go @@ -19,9 +19,11 @@ func Test_procExit(t *testing.T) { expectedLog string }{ { - name: "success (exitcode 0)", - exitCode: 0, - expectedLog: ``, + name: "success (exitcode 0)", + exitCode: 0, + expectedLog: ` +==> wasi_snapshot_preview1.proc_exit(rval=0) +`, }, { name: "arbitrary non-zero exitcode",