forked from bytecodealliance/wasmtime-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrap_test.go
95 lines (88 loc) · 2.02 KB
/
trap_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package wasmtime
import "testing"
func TestTrap(t *testing.T) {
trap := NewTrap("message")
if trap.Message() != "message" {
panic("wrong message")
}
}
func TestTrapFrames(t *testing.T) {
store := NewStore(NewEngine())
wasm, err := Wat2Wasm(`
(func call $foo)
(func $foo call $bar)
(func $bar unreachable)
(start 0)
`)
assertNoError(err)
module, err := NewModule(store.Engine, wasm)
assertNoError(err)
i, err := NewInstance(store, module, []AsExtern{})
if i != nil {
panic("expected failure")
}
if err == nil {
panic("expected failure")
}
trap := err.(*Trap)
frames := trap.Frames()
if len(frames) != 3 {
panic("expected 3 frames")
}
if *frames[0].FuncName() != "bar" {
panic("bad function name")
}
if *frames[1].FuncName() != "foo" {
panic("bad function name")
}
if frames[2].FuncName() != nil {
panic("bad function name")
}
if frames[0].FuncIndex() != 2 {
panic("bad function index")
}
if frames[1].FuncIndex() != 1 {
panic("bad function index")
}
if frames[2].FuncIndex() != 0 {
panic("bad function index")
}
expected := `wasm trap: wasm ` + "`unreachable`" + ` instruction executed
wasm backtrace:
0: 0x26 - <unknown>!bar
1: 0x21 - <unknown>!foo
2: 0x1c - <unknown>!<wasm function 0>
`
if trap.Error() != expected {
t.Fatalf("expected\n%s\ngot\n%s", expected, trap.Error())
}
expCode := UnreachableCodeReached
if code := trap.Code(); code != nil && *code != expCode {
t.Fatalf("expected %v got %v", expCode, code)
}
}
func TestTrapModuleName(t *testing.T) {
store := NewStore(NewEngine())
wasm, err := Wat2Wasm(`(module $f
(func unreachable)
(start 0)
)`)
assertNoError(err)
module, err := NewModule(store.Engine, wasm)
assertNoError(err)
i, err := NewInstance(store, module, []AsExtern{})
if i != nil {
panic("expected failure")
}
if err == nil {
panic("expected failure")
}
trap := err.(*Trap)
frames := trap.Frames()
if len(frames) != 1 {
panic("expected 3 frames")
}
if *frames[0].ModuleName() != "f" {
panic("bad module name")
}
}