| 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 |
|
| 24 |
var testCtx = context.WithValue(context.Background(), arbitrary{}, "arbitrary") |
| 25 |
|
| 26 |
const testMemoryPageSize = 1 |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 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 |
|
| 41 |
|
| 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 |
|
| 48 |
_, err = r.Instantiate(testCtx, exitOnStartUnstableWasm) |
| 49 |
|
| 50 |
|
| 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 |
|
| 59 |
wasiBuilder := r.NewHostModuleBuilder(wasi_snapshot_preview1.ModuleName) |
| 60 |
wasi_snapshot_preview1.NewFunctionExporter().ExportFunctions(wasiBuilder) |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
wasiBuilder.NewFunctionBuilder(). |
| 65 |
WithFunc(func(ctx context.Context, mod api.Module, exitCode uint32) { |
| 66 |
require.Equal(t, uint32(2), exitCode) |
| 67 |
|
| 68 |
mod.Close(ctx) |
| 69 |
}).Export("proc_exit") |
| 70 |
|
| 71 |
_, err := wasiBuilder.Instantiate(testCtx) |
| 72 |
require.NoError(t, err) |
| 73 |
|
| 74 |
|
| 75 |
_, err = r.Instantiate(testCtx, exitOnStartWasm) |
| 76 |
|
| 77 |
|
| 78 |
require.Nil(t, err) |
| 79 |
}) |
| 80 |
} |
| 81 |
|
| 82 |
|
| 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 |
|
| 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 |
|
| 120 |
|
| 121 |
|
| 122 |
func requireErrnoNosys(t *testing.T, funcName string, params ...uint64) string { |
| 123 |
var log bytes.Buffer |
| 124 |
|
| 125 |
|
| 126 |
ctx := experimental.WithFunctionListenerFactory(testCtx, |
| 127 |
proxy.NewLoggingListenerFactory(&log, logging.LogScopeAll)) |
| 128 |
|
| 129 |
r := wazero.NewRuntime(ctx) |
| 130 |
defer r.Close(ctx) |
| 131 |
|
| 132 |
|
| 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 |
|
| 165 |
|
| 166 |
type blockingReader struct { |
| 167 |
ctx context.Context |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
func (b blockingReader) Read(buf []byte) (n int, err error) { |
| 172 |
<-b.ctx.Done() |
| 173 |
return 0, nil |
| 174 |
} |