forked from bytecodealliance/wasmtime-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
trap_test.go
73 lines (60 loc) · 1.97 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
package wasmtime
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestTrap(t *testing.T) {
trap := NewTrap("message")
require.Equal(t, trap.Message(), "message", "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)
`)
require.NoError(t, err)
module, err := NewModule(store.Engine, wasm)
require.NoError(t, err)
inst, err := NewInstance(store, module, []AsExtern{})
require.Nil(t, inst, "expected failure")
require.Error(t, err)
trap := err.(*Trap)
frames := trap.Frames()
require.Len(t, frames, 3, "expected 3 frames")
require.Equal(t, "bar", *frames[0].FuncName(), "bad function name")
require.Equal(t, "foo", *frames[1].FuncName(), "bad function name")
require.Nil(t, frames[2].FuncName(), "bad function name")
require.Equal(t, uint32(2), frames[0].FuncIndex(), "bad function index")
require.Equal(t, uint32(1), frames[1].FuncIndex(), "bad function index")
require.Equal(t, uint32(0), frames[2].FuncIndex(), "bad function index")
expected := `error while executing at wasm backtrace:
0: 0x26 - <unknown>!bar
1: 0x21 - <unknown>!foo
2: 0x1c - <unknown>!<wasm function 0>
Caused by:
wasm trap: wasm ` + "`unreachable` instruction executed"
require.Equal(t, expected, trap.Error())
code := trap.Code()
require.NotNil(t, code)
require.Equal(t, *code, UnreachableCodeReached)
}
func TestTrapModuleName(t *testing.T) {
store := NewStore(NewEngine())
wasm, err := Wat2Wasm(`(module $f
(func unreachable)
(start 0)
)`)
require.NoError(t, err)
module, err := NewModule(store.Engine, wasm)
require.NoError(t, err)
inst, err := NewInstance(store, module, []AsExtern{})
require.Nil(t, inst, "expected failure")
require.Error(t, err)
trap := err.(*Trap)
frames := trap.Frames()
require.Len(t, frames, 1, "expected 1 frame")
require.Equal(t, "f", *frames[0].ModuleName(), "bad function name")
}