forked from tetratelabs/wazero
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wasm_test.go
524 lines (460 loc) · 15 KB
/
wasm_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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
package wazero
import (
"context"
_ "embed"
"fmt"
"math"
"testing"
"github.com/tetratelabs/wazero/api"
"github.com/tetratelabs/wazero/internal/leb128"
"github.com/tetratelabs/wazero/internal/testing/require"
"github.com/tetratelabs/wazero/internal/wasm"
"github.com/tetratelabs/wazero/internal/wasm/binary"
"github.com/tetratelabs/wazero/sys"
)
// testCtx is an arbitrary, non-default context. Non-nil also prevents linter errors.
var testCtx = context.WithValue(context.Background(), struct{}{}, "arbitrary")
func TestRuntime_CompileModule(t *testing.T) {
tests := []struct {
name string
runtime Runtime
source []byte
expectedName string
}{
{
name: "text - no name",
source: []byte(`(module)`),
},
{
name: "text - empty name",
source: []byte(`(module $)`),
},
{
name: "text - name",
source: []byte(`(module $test)`),
expectedName: "test",
},
{
name: "binary - no name section",
source: binary.EncodeModule(&wasm.Module{}),
},
{
name: "binary - empty NameSection.ModuleName",
source: binary.EncodeModule(&wasm.Module{NameSection: &wasm.NameSection{}}),
},
{
name: "binary - NameSection.ModuleName",
source: binary.EncodeModule(&wasm.Module{NameSection: &wasm.NameSection{ModuleName: "test"}}),
expectedName: "test",
},
}
r := NewRuntime()
for _, tt := range tests {
tc := tt
t.Run(tc.name, func(t *testing.T) {
code, err := r.CompileModule(testCtx, tc.source)
require.NoError(t, err)
defer code.Close(testCtx)
if tc.expectedName != "" {
require.Equal(t, tc.expectedName, code.module.NameSection.ModuleName)
}
require.Equal(t, r.(*runtime).store.Engine, code.compiledEngine)
})
}
t.Run("text - memory", func(t *testing.T) {
r := NewRuntimeWithConfig(NewRuntimeConfig().
WithMemoryCapacityPages(func(minPages uint32, maxPages *uint32) uint32 { return 2 }))
source := []byte(`(module (memory 1 3))`)
code, err := r.CompileModule(testCtx, source)
require.NoError(t, err)
defer code.Close(testCtx)
require.Equal(t, &wasm.Memory{
Min: 1,
Cap: 2, // Uses capacity function
Max: 3,
IsMaxEncoded: true,
}, code.module.MemorySection)
})
}
func TestRuntime_CompileModule_Errors(t *testing.T) {
tests := []struct {
name string
runtime Runtime
source []byte
expectedErr string
}{
{
name: "nil",
expectedErr: "source == nil",
},
{
name: "invalid binary",
source: append(binary.Magic, []byte("yolo")...),
expectedErr: "invalid version header",
},
{
name: "invalid text",
source: []byte(`(modular)`),
expectedErr: "1:2: unexpected field: modular",
},
{
name: "RuntimeConfig.memoryLimitPages too large",
runtime: NewRuntimeWithConfig(NewRuntimeConfig().WithMemoryLimitPages(math.MaxUint32)),
source: []byte(`(module)`),
expectedErr: "memoryLimitPages 4294967295 (3 Ti) > specification max 65536 (4 Gi)",
},
{
name: "memory has too many pages - text",
runtime: NewRuntimeWithConfig(NewRuntimeConfig().WithMemoryLimitPages(2)),
source: []byte(`(module (memory 3))`),
expectedErr: "1:17: min 3 pages (192 Ki) over limit of 2 pages (128 Ki) in module.memory[0]",
},
{
name: "memory cap < min", // only one test to avoid duplicating tests in module_test.go
runtime: NewRuntimeWithConfig(NewRuntimeConfig().
WithMemoryCapacityPages(func(minPages uint32, maxPages *uint32) uint32 { return 1 })),
source: []byte(`(module (memory 3))`),
expectedErr: "memory[0] capacity 1 pages (64 Ki) less than minimum 3 pages (192 Ki)",
},
{
name: "memory cap < min - exported", // only one test to avoid duplicating tests in module_test.go
runtime: NewRuntimeWithConfig(NewRuntimeConfig().
WithMemoryCapacityPages(func(minPages uint32, maxPages *uint32) uint32 { return 1 })),
source: []byte(`(module (memory 3) (export "memory" (memory 0)))`),
expectedErr: "memory[memory] capacity 1 pages (64 Ki) less than minimum 3 pages (192 Ki)",
},
{
name: "memory has too many pages - binary",
runtime: NewRuntimeWithConfig(NewRuntimeConfig().WithMemoryLimitPages(2)),
source: binary.EncodeModule(&wasm.Module{MemorySection: &wasm.Memory{Min: 2, Max: 3, IsMaxEncoded: true}}),
expectedErr: "section memory: max 3 pages (192 Ki) over limit of 2 pages (128 Ki)",
},
}
r := NewRuntime()
for _, tt := range tests {
tc := tt
if tc.runtime == nil {
tc.runtime = r
}
t.Run(tc.name, func(t *testing.T) {
_, err := tc.runtime.CompileModule(testCtx, tc.source)
require.EqualError(t, err, tc.expectedErr)
})
}
}
func TestRuntime_setMemoryCapacity(t *testing.T) {
tests := []struct {
name string
runtime *runtime
mem *wasm.Memory
expectedErr string
}{
{
name: "cap ok",
runtime: &runtime{memoryCapacityPages: func(minPages uint32, maxPages *uint32) uint32 {
return 3
}, memoryLimitPages: 3},
mem: &wasm.Memory{Min: 2},
},
{
name: "cap < min",
runtime: &runtime{memoryCapacityPages: func(minPages uint32, maxPages *uint32) uint32 {
return 1
}, memoryLimitPages: 3},
mem: &wasm.Memory{Min: 2},
expectedErr: "memory[memory] capacity 1 pages (64 Ki) less than minimum 2 pages (128 Ki)",
},
{
name: "cap > maxLimit",
runtime: &runtime{memoryCapacityPages: func(minPages uint32, maxPages *uint32) uint32 {
return 4
}, memoryLimitPages: 3},
mem: &wasm.Memory{Min: 2},
expectedErr: "memory[memory] capacity 4 pages (256 Ki) over limit of 3 pages (192 Ki)",
},
}
for _, tt := range tests {
tc := tt
t.Run(tc.name, func(t *testing.T) {
err := tc.runtime.setMemoryCapacity("memory", tc.mem)
if tc.expectedErr == "" {
require.NoError(t, err)
} else {
require.EqualError(t, err, tc.expectedErr)
}
})
}
}
// TestModule_Memory only covers a couple cases to avoid duplication of internal/wasm/runtime_test.go
func TestModule_Memory(t *testing.T) {
tests := []struct {
name string
builder func(Runtime) ModuleBuilder
expected bool
expectedLen uint32
}{
{
name: "no memory",
builder: func(r Runtime) ModuleBuilder {
return r.NewModuleBuilder(t.Name())
},
},
{
name: "memory exported, one page",
builder: func(r Runtime) ModuleBuilder {
return r.NewModuleBuilder(t.Name()).ExportMemory("memory", 1)
},
expected: true,
expectedLen: 65536,
},
}
for _, tt := range tests {
tc := tt
r := NewRuntime()
t.Run(tc.name, func(t *testing.T) {
// Instantiate the module and get the export of the above memory
module, err := tc.builder(r).Instantiate(testCtx)
require.NoError(t, err)
defer module.Close(testCtx)
mem := module.ExportedMemory("memory")
if tc.expected {
require.Equal(t, tc.expectedLen, mem.Size(testCtx))
} else {
require.Nil(t, mem)
}
})
}
}
// TestModule_Global only covers a couple cases to avoid duplication of internal/wasm/global_test.go
func TestModule_Global(t *testing.T) {
globalVal := int64(100) // intentionally a value that differs in signed vs unsigned encoding
tests := []struct {
name string
module *wasm.Module // module as wat doesn't yet support globals
builder func(Runtime) ModuleBuilder
expected, expectedMutable bool
}{
{
name: "no global",
module: &wasm.Module{},
},
{
name: "global not exported",
module: &wasm.Module{
GlobalSection: []*wasm.Global{
{
Type: &wasm.GlobalType{ValType: wasm.ValueTypeI64, Mutable: true},
Init: &wasm.ConstantExpression{Opcode: wasm.OpcodeI64Const, Data: leb128.EncodeInt64(globalVal)},
},
},
},
},
{
name: "global exported",
builder: func(r Runtime) ModuleBuilder {
return r.NewModuleBuilder(t.Name()).ExportGlobalI64("global", globalVal)
},
expected: true,
},
{
name: "global exported and mutable",
module: &wasm.Module{
GlobalSection: []*wasm.Global{
{
Type: &wasm.GlobalType{ValType: wasm.ValueTypeI64, Mutable: true},
Init: &wasm.ConstantExpression{Opcode: wasm.OpcodeI64Const, Data: leb128.EncodeInt64(globalVal)},
},
},
ExportSection: []*wasm.Export{
{Type: wasm.ExternTypeGlobal, Name: "global"},
},
},
expected: true,
expectedMutable: true,
},
}
for _, tt := range tests {
tc := tt
r := NewRuntime().(*runtime)
t.Run(tc.name, func(t *testing.T) {
var code *CompiledCode
if tc.module != nil {
code = &CompiledCode{module: tc.module}
} else {
code, _ = tc.builder(r).Build(testCtx)
}
err := r.store.Engine.CompileModule(testCtx, code.module)
require.NoError(t, err)
// Instantiate the module and get the export of the above global
module, err := r.InstantiateModule(testCtx, code)
require.NoError(t, err)
defer module.Close(testCtx)
global := module.ExportedGlobal("global")
if !tc.expected {
require.Nil(t, global)
return
}
require.Equal(t, uint64(globalVal), global.Get(testCtx))
mutable, ok := global.(api.MutableGlobal)
require.Equal(t, tc.expectedMutable, ok)
if ok {
mutable.Set(testCtx, 2)
require.Equal(t, uint64(2), global.Get(testCtx))
}
})
}
}
func TestFunction_Context(t *testing.T) {
tests := []struct {
name string
ctx context.Context
expected context.Context
}{
{
name: "nil defaults to context.Background",
ctx: nil,
expected: context.Background(),
},
{
name: "set context",
ctx: testCtx,
expected: testCtx,
},
}
for _, tt := range tests {
tc := tt
t.Run(tc.name, func(t *testing.T) {
r := NewRuntime()
// Define a host function so that we can catch the context propagated from a module function call
functionName := "fn"
expectedResult := uint64(math.MaxUint64)
hostFn := func(ctx context.Context) uint64 {
require.Equal(t, tc.expected, ctx)
return expectedResult
}
source, closer := requireImportAndExportFunction(t, r, hostFn, functionName)
defer closer(testCtx) // nolint
// Instantiate the module and get the export of the above hostFn
module, err := r.InstantiateModuleFromCodeWithConfig(tc.ctx, source, NewModuleConfig().WithName(t.Name()))
require.NoError(t, err)
defer module.Close(testCtx)
// This fails if the function wasn't invoked, or had an unexpected context.
results, err := module.ExportedFunction(functionName).Call(tc.ctx)
require.NoError(t, err)
require.Equal(t, expectedResult, results[0])
})
}
}
func TestRuntime_InstantiateModule_UsesContext(t *testing.T) {
r := NewRuntime()
// Define a function that will be set as the start function
var calledStart bool
start := func(ctx context.Context) {
calledStart = true
require.Equal(t, testCtx, ctx)
}
env, err := r.NewModuleBuilder("env").
ExportFunction("start", start).
Instantiate(testCtx)
require.NoError(t, err)
defer env.Close(testCtx)
code, err := r.CompileModule(testCtx, []byte(`(module $runtime_test.go
(import "env" "start" (func $start))
(start $start)
)`))
require.NoError(t, err)
defer code.Close(testCtx)
// Instantiate the module, which calls the start function. This will fail if the context wasn't as intended.
m, err := r.InstantiateModule(testCtx, code)
require.NoError(t, err)
defer m.Close(testCtx)
require.True(t, calledStart)
}
// TestInstantiateModuleFromCode_DoesntEnforce_Start ensures wapc-go work when modules import WASI, but don't export "_start".
func TestInstantiateModuleFromCode_DoesntEnforce_Start(t *testing.T) {
r := NewRuntime()
mod, err := r.InstantiateModuleFromCode(testCtx, []byte(`(module $wasi_test.go
(memory 1)
(export "memory" (memory 0))
)`))
require.NoError(t, err)
require.NoError(t, mod.Close(testCtx))
}
func TestRuntime_InstantiateModuleFromCode_UsesContext(t *testing.T) {
r := NewRuntime()
// Define a function that will be re-exported as the WASI function: _start
var calledStart bool
start := func(ctx context.Context) {
calledStart = true
require.Equal(t, testCtx, ctx)
}
host, err := r.NewModuleBuilder("").
ExportFunction("start", start).
Instantiate(testCtx)
require.NoError(t, err)
defer host.Close(testCtx)
// Start the module as a WASI command. This will fail if the context wasn't as intended.
mod, err := r.InstantiateModuleFromCode(testCtx, []byte(`(module $start
(import "" "start" (func $start))
(memory 1)
(export "_start" (func $start))
(export "memory" (memory 0))
)`))
require.NoError(t, err)
defer mod.Close(testCtx)
require.True(t, calledStart)
}
// TestInstantiateModuleWithConfig_WithName tests that we can pre-validate (cache) a module and instantiate it under
// different names. This pattern is used in wapc-go.
func TestInstantiateModuleWithConfig_WithName(t *testing.T) {
r := NewRuntime()
base, err := r.CompileModule(testCtx, []byte(`(module $0 (memory 1))`))
require.NoError(t, err)
defer base.Close(testCtx)
require.Equal(t, "0", base.module.NameSection.ModuleName)
// Use the same runtime to instantiate multiple modules
internal := r.(*runtime).store
m1, err := r.InstantiateModuleWithConfig(testCtx, base, NewModuleConfig().WithName("1"))
require.NoError(t, err)
defer m1.Close(testCtx)
require.Nil(t, internal.Module("0"))
require.Equal(t, internal.Module("1"), m1)
m2, err := r.InstantiateModuleWithConfig(testCtx, base, NewModuleConfig().WithName("2"))
require.NoError(t, err)
defer m2.Close(testCtx)
require.Nil(t, internal.Module("0"))
require.Equal(t, internal.Module("2"), m2)
}
func TestInstantiateModuleWithConfig_ExitError(t *testing.T) {
r := NewRuntime()
start := func(ctx context.Context, m api.Module) {
require.NoError(t, m.CloseWithExitCode(ctx, 2))
}
_, err := r.NewModuleBuilder("env").ExportFunction("_start", start).Instantiate(testCtx)
// Ensure the exit error propagated and didn't wrap.
require.Equal(t, err, sys.NewExitError("env", 2))
}
// requireImportAndExportFunction re-exports a host function because only host functions can see the propagated context.
func requireImportAndExportFunction(t *testing.T, r Runtime, hostFn func(ctx context.Context) uint64, functionName string) ([]byte, func(context.Context) error) {
mod, err := r.NewModuleBuilder("host").ExportFunction(functionName, hostFn).Instantiate(testCtx)
require.NoError(t, err)
return []byte(fmt.Sprintf(
`(module (import "host" "%[1]s" (func (result i64))) (export "%[1]s" (func 0)))`, functionName,
)), mod.Close
}
type mockEngine struct {
name string
cachedModules map[*wasm.Module]struct{}
}
// NewModuleEngine implements the same method as documented on wasm.Engine.
func (e *mockEngine) NewModuleEngine(_ string, _ *wasm.Module, _, _ []*wasm.FunctionInstance, _ *wasm.TableInstance, _ map[wasm.Index]wasm.Index) (wasm.ModuleEngine, error) {
return nil, nil
}
// DeleteCompiledModule implements the same method as documented on wasm.Engine.
func (e *mockEngine) DeleteCompiledModule(module *wasm.Module) {
delete(e.cachedModules, module)
}
func (e *mockEngine) CompileModule(_ context.Context, module *wasm.Module) error {
e.cachedModules[module] = struct{}{}
return nil
}