Debian Codegraph @

imports/wasi_snapshot_preview1/wasi_test.go

raw symbols SCIP
1 package wasi_snapshot_preview1_test
2
3 import (
4 "bytes"
5 "context"
6 _ "embed"
7 "testing"
8 "time"
9
10 "github.com/tetratelabs/wazero"
11 "github.com/tetratelabs/wazero/api"
12 "github.com/tetratelabs/wazero/experimental"
13 "github.com/tetratelabs/wazero/experimental/logging"
14 "github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
15 "github.com/tetratelabs/wazero/internal/testing/proxy"
16 "github.com/tetratelabs/wazero/internal/testing/require"
17 "github.com/tetratelabs/wazero/internal/wasip1"
18 "github.com/tetratelabs/wazero/sys"
19 )
20
21 type arbitrary struct{}
22
23 // testCtx is an arbitrary, non-default context. Non-nil also prevents linter errors.
24 var testCtx = context.WithValue(context.Background(), arbitrary{}, "arbitrary")
25
26 const testMemoryPageSize = 1
27
28 // exitOnStartUnstableWasm was generated by the following:
29 //
30 // cd testdata; wat2wasm --debug-names exit_on_start_unstable.wat
31 //
32 //go:embed testdata/exit_on_start_unstable.wasm
33 var exitOnStartUnstableWasm []byte
34
35 func TestNewFunctionExporter(t *testing.T) {
36 t.Run("export as wasi_unstable", func(t *testing.T) {
37 r := wazero.NewRuntime(testCtx)
38 defer r.Close(testCtx)
39
40 // Instantiate the current WASI functions under the wasi_unstable
41 // instead of wasi_snapshot_preview1.
42 wasiBuilder := r.NewHostModuleBuilder("wasi_unstable")
43 wasi_snapshot_preview1.NewFunctionExporter().ExportFunctions(wasiBuilder)
44 _, err := wasiBuilder.Instantiate(testCtx)
45 require.NoError(t, err)
46
47 // Instantiate our test binary, but using the old import names.
48 _, err = r.Instantiate(testCtx, exitOnStartUnstableWasm)
49
50 // Ensure the test binary worked. It should return exit code 2.
51 require.Equal(t, uint32(2), err.(*sys.ExitError).ExitCode())
52 })
53
54 t.Run("override function", func(t *testing.T) {
55 r := wazero.NewRuntime(testCtx)
56 defer r.Close(testCtx)
57
58 // Export the default WASI functions
59 wasiBuilder := r.NewHostModuleBuilder(wasi_snapshot_preview1.ModuleName)
60 wasi_snapshot_preview1.NewFunctionExporter().ExportFunctions(wasiBuilder)
61
62 // Override proc_exit to prove the point that you can add or replace
63 // functions like this.
64 wasiBuilder.NewFunctionBuilder().
65 WithFunc(func(ctx context.Context, mod api.Module, exitCode uint32) {
66 require.Equal(t, uint32(2), exitCode)
67 // ignore the code instead!
68 mod.Close(ctx)
69 }).Export("proc_exit")
70
71 _, err := wasiBuilder.Instantiate(testCtx)
72 require.NoError(t, err)
73
74 // Instantiate our test binary which will use our modified WASI.
75 _, err = r.Instantiate(testCtx, exitOnStartWasm)
76
77 // Ensure the modified function was used!
78 require.Nil(t, err)
79 })
80 }
81
82 // maskMemory sets the first memory in the store to '?' * size, so tests can see what's written.
83 func maskMemory(t *testing.T, mod api.Module, size int) {
84 for i := uint32(0); i < uint32(size); i++ {
85 require.True(t, mod.Memory().WriteByte(i, '?'))
86 }
87 }
88
89 func requireProxyModule(t *testing.T, config wazero.ModuleConfig) (api.Module, api.Closer, *bytes.Buffer) {
90 return requireProxyModuleWithContext(testCtx, t, config)
91 }
92
93 func requireProxyModuleWithContext(ctx context.Context, t *testing.T, config wazero.ModuleConfig) (api.Module, api.Closer, *bytes.Buffer) {
94 var log bytes.Buffer
95
96 // Set context to one that has an experimental listener
97 ctx = experimental.WithFunctionListenerFactory(ctx,
98 proxy.NewLoggingListenerFactory(&log, logging.LogScopeAll))
99
100 r := wazero.NewRuntime(ctx)
101
102 wasiModuleCompiled, err := wasi_snapshot_preview1.NewBuilder(r).Compile(ctx)
103 require.NoError(t, err)
104
105 _, err = r.InstantiateModule(ctx, wasiModuleCompiled, config)
106 require.NoError(t, err)
107
108 proxyBin := proxy.NewModuleBinary(wasi_snapshot_preview1.ModuleName, wasiModuleCompiled)
109
110 proxyCompiled, err := r.CompileModule(ctx, proxyBin)
111 require.NoError(t, err)
112
113 mod, err := r.InstantiateModule(ctx, proxyCompiled, config)
114 require.NoError(t, err)
115
116 return mod, r, &log
117 }
118
119 // requireErrnoNosys ensures a call of the given function returns errno. The log
120 // message returned can verify the output is wasm `-->` or a host `==>`
121 // function.
122 func requireErrnoNosys(t *testing.T, funcName string, params ...uint64) string {
123 var log bytes.Buffer
124
125 // Set context to one that has an experimental listener
126 ctx := experimental.WithFunctionListenerFactory(testCtx,
127 proxy.NewLoggingListenerFactory(&log, logging.LogScopeAll))
128
129 r := wazero.NewRuntime(ctx)
130 defer r.Close(ctx)
131
132 // Instantiate the wasi module.
133 wasiModuleCompiled, err := wasi_snapshot_preview1.NewBuilder(r).Compile(ctx)
134 require.NoError(t, err)
135
136 _, err = r.InstantiateModule(ctx, wasiModuleCompiled, wazero.NewModuleConfig())
137 require.NoError(t, err)
138
139 proxyBin := proxy.NewModuleBinary(wasi_snapshot_preview1.ModuleName, wasiModuleCompiled)
140
141 proxyCompiled, err := r.CompileModule(ctx, proxyBin)
142 require.NoError(t, err)
143
144 mod, err := r.InstantiateModule(ctx, proxyCompiled, wazero.NewModuleConfig())
145 require.NoError(t, err)
146
147 requireErrnoResult(t, wasip1.ErrnoNosys, mod, funcName, params...)
148 return "\n" + log.String()
149 }
150
151 func requireErrnoResult(t *testing.T, expectedErrno wasip1.Errno, mod api.Closer, funcName string, params ...uint64) {
152 results, err := mod.(api.Module).ExportedFunction(funcName).Call(testCtx, params...)
153 require.NoError(t, err)
154 errno := wasip1.Errno(results[0])
155 require.Equal(t, expectedErrno, errno, "want %s but have %s", wasip1.ErrnoName(expectedErrno), wasip1.ErrnoName(errno))
156 }
157
158 func newBlockingReader(t *testing.T) blockingReader {
159 timeout, cancelFunc := context.WithTimeout(testCtx, 5*time.Second)
160 t.Cleanup(cancelFunc)
161 return blockingReader{ctx: timeout}
162 }
163
164 // blockingReader is an io.Reader that never terminates its read
165 // unless the embedded context is Done()
166 type blockingReader struct {
167 ctx context.Context
168 }
169
170 // Read implements io.Reader
171 func (b blockingReader) Read(buf []byte) (n int, err error) {
172 <-b.ctx.Done()
173 return 0, nil
174 }