all: move -panic=trap support to the compiler/runtime

Support for `-panic=trap` was previously a pass in the optimization
pipeline. This change moves it to the compiler and runtime, which in my
opinion is a much better place.

As a side effect, it also fixes
https://github.com/tinygo-org/tinygo/issues/4161 by trapping inside
runtime.runtimePanicAt and not just runtime.runtimePanic.

This change also adds a test for the list of imported functions. This is
a more generic test where it's easy to add more tests for WebAssembly
file properties, such as exported functions.
This commit is contained in:
Ayke van Laethem
2024-03-16 16:31:36 +01:00
committed by Ron Evans
parent 6384ecace0
commit ad4d722f54
10 changed files with 86 additions and 96 deletions
+16
View File
@@ -21,6 +21,16 @@ func tinygo_longjmp(frame *deferFrame)
// Returns whether recover is supported on the current architecture.
func supportsRecover() bool
const (
panicStrategyPrint = 1
panicStrategyTrap = 2
)
// Compile intrinsic.
// Returns which strategy is used. This is usually "print" but can be changed
// using the -panic= compiler flag.
func panicStrategy() uint8
// DeferFrame is a stack allocated object that stores information for the
// current "defer frame", which is used in functions that use the `defer`
// keyword.
@@ -37,6 +47,9 @@ type deferFrame struct {
// Builtin function panic(msg), used as a compiler intrinsic.
func _panic(message interface{}) {
if panicStrategy() == panicStrategyTrap {
trap()
}
if supportsRecover() {
frame := (*deferFrame)(task.Current().DeferFrame)
if frame != nil {
@@ -60,6 +73,9 @@ func runtimePanic(msg string) {
}
func runtimePanicAt(addr unsafe.Pointer, msg string) {
if panicStrategy() == panicStrategyTrap {
trap()
}
if hasReturnAddr {
printstring("panic: runtime error at ")
printptr(uintptr(addr) - callInstSize)