Debian Codegraph @

internal/integration_test/fuzz/wazerolib/extern.go

raw symbols SCIP
1 package main
2
3 import "C"
4 import (
5 "context"
6 "math"
7 "reflect"
8 "strings"
9 "unsafe"
10
11 "github.com/tetratelabs/wazero"
12 "github.com/tetratelabs/wazero/api"
13 "github.com/tetratelabs/wazero/experimental"
14 "github.com/tetratelabs/wazero/internal/testing/binaryencoding"
15 "github.com/tetratelabs/wazero/internal/testing/nodiff"
16 "github.com/tetratelabs/wazero/internal/wasm"
17 )
18
19 func main() {}
20
21 // require_no_diff ensures that the behavior is the same between the compiler and the interpreter for any given binary.
22 // And if there's diff, this also saves the problematic binary and wat into testdata directory.
23 //
24 //export require_no_diff
25 func require_no_diff(binaryPtr uintptr, binarySize int, checkMemory bool, checkLogging bool) {
26 var wasmBin []byte
27 wasmHdr := (*reflect.SliceHeader)(unsafe.Pointer(&wasmBin))
28 wasmHdr.Data = binaryPtr
29 wasmHdr.Len = binarySize
30 wasmHdr.Cap = binarySize
31
32 failed := true
33 defer func() {
34 if failed {
35 // If the test fails, we save the binary and wat into testdata directory.
36 saveFailedBinary(wasmBin, "TestReRunFailedRequireNoDiffCase")
37 }
38 }()
39
40 nodiff.RequireNoDiff(wasmBin, checkMemory, checkLogging, func(err error) {
41 if err != nil {
42 panic(err)
43 }
44 })
45
46 failed = false
47 }
48
49 // validate accepts maybe-invalid Wasm module bytes and ensures that our validation phase works correctly
50 // as well as the compiler doesn't panic during compilation!
51 //
52 //export validate
53 func validate(binaryPtr uintptr, binarySize int) {
54 var wasmBin []byte
55 wasmHdr := (*reflect.SliceHeader)(unsafe.Pointer(&wasmBin))
56 wasmHdr.Data = binaryPtr
57 wasmHdr.Len = binarySize
58 wasmHdr.Cap = binarySize
59
60 failed := true
61 defer func() {
62 if failed {
63 // If the test fails, we save the binary and wat into testdata directory.
64 saveFailedBinary(wasmBin, "TestReRunFailedValidateCase")
65 }
66 }()
67
68 tryCompile(wasmBin)
69 failed = false
70 }
71
72 //export test_signal_stack
73 func test_signal_stack() {
74 // (module
75 // (func (export "long_loop")
76 // (loop
77 // global.get 0
78 // i32.eqz
79 // if ;; label = @1
80 // unreachable
81 // end
82 // global.get 0
83 // i32.const 1
84 // i32.sub
85 // global.set 0
86 // br 0
87 // )
88 // )
89 // (global (;0;) (mut i32) i32.const 2147483648)
90 // )
91 bin := binaryencoding.EncodeModule(&wasm.Module{
92 TypeSection: []wasm.FunctionType{{}},
93 FunctionSection: []wasm.Index{0},
94 GlobalSection: []wasm.Global{{
95 Type: wasm.GlobalType{ValType: wasm.ValueTypeI32, Mutable: true},
96 Init: wasm.NewConstantExpressionFromI32(math.MaxInt32),
97 }},
98 ExportSection: []wasm.Export{{Type: wasm.ExternTypeFunc, Name: "long_loop", Index: 0}},
99 CodeSection: []wasm.Code{
100 {
101 Body: []byte{
102 wasm.OpcodeLoop, 0,
103 wasm.OpcodeGlobalGet, 0,
104 wasm.OpcodeI32Eqz,
105 wasm.OpcodeIf, 0,
106 wasm.OpcodeUnreachable,
107 wasm.OpcodeEnd,
108 wasm.OpcodeGlobalGet, 0,
109 wasm.OpcodeI32Const, 1,
110 wasm.OpcodeI32Sub,
111 wasm.OpcodeGlobalSet, 0,
112 wasm.OpcodeBr, 0,
113 wasm.OpcodeEnd,
114 wasm.OpcodeEnd,
115 },
116 },
117 },
118 })
119 ctx := context.Background()
120 config := wazero.NewRuntimeConfigCompiler().WithCoreFeatures(api.CoreFeaturesV2 | experimental.CoreFeaturesTailCall)
121
122 r := wazero.NewRuntimeWithConfig(ctx, config)
123 module, err := r.Instantiate(ctx, bin)
124 if err != nil {
125 panic(err)
126 }
127 defer func() {
128 if err = module.Close(ctx); err != nil {
129 panic(err)
130 }
131 }()
132
133 _, err = module.ExportedFunction("long_loop").Call(ctx)
134 if !strings.Contains(err.Error(), "unreachable") {
135 panic("long_loop should be unreachable")
136 }
137 }