Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent module from closing when proc_exit is called with a 0 exit code #2367

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions imports/wasi_snapshot_preview1/proc.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ var procExit = &wasm.HostFunc{
func procExitFn(ctx context.Context, mod api.Module, params []uint64) {
exitCode := uint32(params[0])

// Ensure other callers see the exit code.
_ = mod.CloseWithExitCode(ctx, exitCode)
// TinyGo 0.35.0 calls proc_exit from _start, even for exit code 0.
if exitCode != 0 {
// 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.
Expand Down
14 changes: 9 additions & 5 deletions imports/wasi_snapshot_preview1/proc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,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
Loading