Skip to content

Commit c6dc9f1

Browse files
committed
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 #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.
1 parent 6384eca commit c6dc9f1

File tree

10 files changed

+86
-96
lines changed

10 files changed

+86
-96
lines changed

builder/build.go

+1
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ func Build(pkgName, outpath, tmpdir string, config *compileopts.Config) (BuildRe
200200
MaxStackAlloc: config.MaxStackAlloc(),
201201
NeedsStackObjects: config.NeedsStackObjects(),
202202
Debug: !config.Options.SkipDWARF, // emit DWARF except when -internal-nodwarf is passed
203+
PanicStrategy: config.PanicStrategy(),
203204
}
204205

205206
// Load the target machine, which is the LLVM object that contains all

compiler/compiler.go

+8
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ type Config struct {
5656
MaxStackAlloc uint64
5757
NeedsStackObjects bool
5858
Debug bool // Whether to emit debug information in the LLVM module.
59+
PanicStrategy string
5960
}
6061

6162
// compilerContext contains function-independent data that should still be
@@ -1855,6 +1856,13 @@ func (b *builder) createFunctionCall(instr *ssa.CallCommon) (llvm.Value, error)
18551856
supportsRecover = 1
18561857
}
18571858
return llvm.ConstInt(b.ctx.Int1Type(), supportsRecover, false), nil
1859+
case name == "runtime.panicStrategy":
1860+
// These constants are defined in src/runtime/panic.go.
1861+
panicStrategy := map[string]uint64{
1862+
"print": 1, // panicStrategyPrint
1863+
"trap": 2, // panicStrategyTrap
1864+
}[b.Config.PanicStrategy]
1865+
return llvm.ConstInt(b.ctx.Int8Type(), panicStrategy, false), nil
18581866
case name == "runtime/interrupt.New":
18591867
return b.createInterruptGlobal(instr)
18601868
}

main_test.go

+60
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ import (
1515
"reflect"
1616
"regexp"
1717
"runtime"
18+
"slices"
1819
"strings"
1920
"sync"
2021
"testing"
2122
"time"
2223

24+
"github.com/aykevl/go-wasm"
2325
"github.com/tinygo-org/tinygo/builder"
2426
"github.com/tinygo-org/tinygo/compileopts"
2527
"github.com/tinygo-org/tinygo/goenv"
@@ -404,6 +406,64 @@ func runTestWithConfig(name string, t *testing.T, options compileopts.Options, c
404406
}
405407
}
406408

409+
// Test WebAssembly files for certain properties.
410+
func TestWebAssembly(t *testing.T) {
411+
t.Parallel()
412+
type testCase struct {
413+
name string
414+
panicStrategy string
415+
imports []string
416+
}
417+
for _, tc := range []testCase{
418+
// Test whether there really are no imports when using -panic=trap. This
419+
// tests the bugfix for https://github.com/tinygo-org/tinygo/issues/4161.
420+
{name: "panic-default", imports: []string{"wasi_snapshot_preview1.fd_write"}},
421+
{name: "panic-trap", panicStrategy: "trap", imports: []string{}},
422+
} {
423+
tc := tc
424+
t.Run(tc.name, func(t *testing.T) {
425+
t.Parallel()
426+
tmpdir := t.TempDir()
427+
options := optionsFromTarget("wasi", sema)
428+
options.PanicStrategy = tc.panicStrategy
429+
config, err := builder.NewConfig(&options)
430+
if err != nil {
431+
t.Fatal(err)
432+
}
433+
434+
result, err := builder.Build("testdata/trivialpanic.go", ".wasm", tmpdir, config)
435+
if err != nil {
436+
t.Fatal("failed to build binary:", err)
437+
}
438+
f, err := os.Open(result.Binary)
439+
if err != nil {
440+
t.Fatal("could not open output binary:", err)
441+
}
442+
defer f.Close()
443+
module, err := wasm.Parse(f)
444+
if err != nil {
445+
t.Fatal("could not parse output binary:", err)
446+
}
447+
448+
// Test the list of imports.
449+
if tc.imports != nil {
450+
var imports []string
451+
for _, section := range module.Sections {
452+
switch section := section.(type) {
453+
case *wasm.SectionImport:
454+
for _, symbol := range section.Entries {
455+
imports = append(imports, symbol.Module+"."+symbol.Field)
456+
}
457+
}
458+
}
459+
if !slices.Equal(imports, tc.imports) {
460+
t.Errorf("import list not as expected!\nexpected: %v\nactual: %v", tc.imports, imports)
461+
}
462+
}
463+
})
464+
}
465+
}
466+
407467
func TestTest(t *testing.T) {
408468
t.Parallel()
409469

src/runtime/panic.go

+16
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ func tinygo_longjmp(frame *deferFrame)
2121
// Returns whether recover is supported on the current architecture.
2222
func supportsRecover() bool
2323

24+
const (
25+
panicStrategyPrint = 1
26+
panicStrategyTrap = 2
27+
)
28+
29+
// Compile intrinsic.
30+
// Returns which strategy is used. This is usually "print" but can be changed
31+
// using the -panic= compiler flag.
32+
func panicStrategy() uint8
33+
2434
// DeferFrame is a stack allocated object that stores information for the
2535
// current "defer frame", which is used in functions that use the `defer`
2636
// keyword.
@@ -37,6 +47,9 @@ type deferFrame struct {
3747

3848
// Builtin function panic(msg), used as a compiler intrinsic.
3949
func _panic(message interface{}) {
50+
if panicStrategy() == panicStrategyTrap {
51+
trap()
52+
}
4053
if supportsRecover() {
4154
frame := (*deferFrame)(task.Current().DeferFrame)
4255
if frame != nil {
@@ -60,6 +73,9 @@ func runtimePanic(msg string) {
6073
}
6174

6275
func runtimePanicAt(addr unsafe.Pointer, msg string) {
76+
if panicStrategy() == panicStrategyTrap {
77+
trap()
78+
}
6379
if hasReturnAddr {
6480
printstring("panic: runtime error at ")
6581
printptr(uintptr(addr) - callInstSize)

transform/optimizer.go

-4
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,6 @@ func Optimize(mod llvm.Module, config *compileopts.Config) []error {
4040
fn.SetLinkage(llvm.ExternalLinkage)
4141
}
4242

43-
if config.PanicStrategy() == "trap" {
44-
ReplacePanicsWithTrap(mod) // -panic=trap
45-
}
46-
4743
// run a check of all of our code
4844
if config.VerifyIR() {
4945
errs := ircheck.Module(mod)

transform/panic.go

-33
This file was deleted.

transform/panic_test.go

-12
This file was deleted.

transform/testdata/panic.ll

-22
This file was deleted.

transform/testdata/panic.out.ll

-25
This file was deleted.

transform/transform_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ func compileGoFileForTesting(t *testing.T, filename string) llvm.Module {
137137
Scheduler: config.Scheduler(),
138138
AutomaticStackSize: config.AutomaticStackSize(),
139139
Debug: true,
140+
PanicStrategy: config.PanicStrategy(),
140141
}
141142
machine, err := compiler.NewTargetMachine(compilerConfig)
142143
if err != nil {

0 commit comments

Comments
 (0)