Skip to content

Commit

Permalink
Tweak to allow exit code 0 to panic to prevent executing the LLVM unr…
Browse files Browse the repository at this point in the history
…eachable instruction.

Signed-off-by: Phil Kedy <[email protected]>
  • Loading branch information
pkedy committed Jan 21, 2025
1 parent e3298ca commit 0c6afb5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
9 changes: 4 additions & 5 deletions imports/wasi_snapshot_preview1/proc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions imports/wasi_snapshot_preview1/proc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 0c6afb5

Please sign in to comment.