From 583411a057aa67c5e94fb466019e24437441b36c Mon Sep 17 00:00:00 2001 From: "Ronald G. Minnich" Date: Sat, 25 Nov 2023 13:08:09 -0800 Subject: [PATCH] runtime: allow panic on syscall.Exit instead of halting In some environments, such as u-root, code can be imported which provides a shell. The shell calls functions, instead of trying to use exec.Command (which is not available). An example can be found in github.com:u-root/u-root/tools/tinygobb. When the command exits, the shell needs to resume operation, which is not possible when runtime.Exit calls halt. On tamago, we achieve this by setting a normally nil function, runtime.Exit, to a funtion which panics. The shell has a recover() to catch the panics. There are many ways in which setting runtime.Exit can go wrong, including different functions setting it to different functions. tinygo allows a simpler, and hence more robust, model: instead of halting, we can panic. But in most cases, halting is ok. Add a function, runtime.PanicOnExit, which enables a panic on exit. This function is idempotent, and once panic on exit is enabled, it can not be disabled. This is restrictive by design. It is hoped that by making it simple and non-revocable, we can avoid the problems that can occur with more complex mechanisms, but still provide an option to halting. A function is provided, instead of just a variable, to allow simpler access from assembly, i.e., both variables and functions have been used in bare metal environments and, for several reasons, functions have proved more flexible. Signed-off-by: Ronald G. Minnich --- src/runtime/baremetal.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/runtime/baremetal.go b/src/runtime/baremetal.go index 173d0db2..ff13d3e5 100644 --- a/src/runtime/baremetal.go +++ b/src/runtime/baremetal.go @@ -3,6 +3,7 @@ package runtime import ( + "internal/itoa" "unsafe" ) @@ -59,8 +60,30 @@ func runtime_putchar(c byte) { putchar(c) } +// ErrExitPanic implements error +type ErrExitPanic int + +func (e ErrExitPanic) Error() string { + return "errno " + itoa.Itoa(int(e)) +} + +var panicOnExit bool + +// PanicOnExit enables panic when syscall.Exit is called, +// as opposed to halting. It can be set by, e.g., a u-root +// shell to regain control when commands exit. +// This in turn makes it easier for building unmodified +// commands into u-root, for bare metal environments. +func PanicOnExit() { + panicOnExit = true +} + //go:linkname syscall_Exit syscall.Exit func syscall_Exit(code int) { + if panicOnExit { + panic(ErrExitPanic(code)) + } + exit(code) }