Skip to content

Commit

Permalink
Prevent module from closing when proc_exit is called with a 0 exit …
Browse files Browse the repository at this point in the history
…code.

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

// Ensure other callers see the exit code.
_ = mod.CloseWithExitCode(ctx, exitCode)
Expand Down
22 changes: 12 additions & 10 deletions imports/wasi_snapshot_preview1/proc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@ func Test_procExit(t *testing.T) {
expectedLog string
}{
{
name: "success (exitcode 0)",
exitCode: 0,
expectedLog: `
==> wasi_snapshot_preview1.proc_exit(rval=0)
`,
name: "success (exitcode 0)",
exitCode: 0,
expectedLog: ``,
},
{
name: "arbitrary non-zero exitcode",
Expand All @@ -42,11 +40,15 @@ func Test_procExit(t *testing.T) {

// Since procExit panics, any opcodes afterwards cannot be reached.
_, err := mod.ExportedFunction(wasip1.ProcExitName).Call(testCtx, uint64(tc.exitCode))
require.Error(t, err)
sysErr, ok := err.(*sys.ExitError)
require.True(t, ok, err)
require.Equal(t, tc.exitCode, sysErr.ExitCode())
require.Equal(t, tc.expectedLog, "\n"+log.String())
if tc.expectedLog != "" {
require.Error(t, err)
sysErr, ok := err.(*sys.ExitError)
require.True(t, ok, err)
require.Equal(t, tc.exitCode, sysErr.ExitCode())
require.Equal(t, tc.expectedLog, "\n"+log.String())
} else {
require.NoError(t, err)
}
})
}
}
Expand Down

0 comments on commit e3298ca

Please sign in to comment.