This repository has been archived by the owner on Jan 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
runtime_test.go
833 lines (728 loc) · 23.6 KB
/
runtime_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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
package wazero
import (
"context"
_ "embed"
"errors"
"sync"
"testing"
"time"
"github.com/wasilibs/wazerox/api"
"github.com/wasilibs/wazerox/experimental"
"github.com/wasilibs/wazerox/internal/filecache"
"github.com/wasilibs/wazerox/internal/leb128"
"github.com/wasilibs/wazerox/internal/platform"
"github.com/wasilibs/wazerox/internal/testing/binaryencoding"
"github.com/wasilibs/wazerox/internal/testing/require"
"github.com/wasilibs/wazerox/internal/wasm"
"github.com/wasilibs/wazerox/sys"
)
var (
binaryNamedZero = binaryencoding.EncodeModule(&wasm.Module{NameSection: &wasm.NameSection{ModuleName: "0"}})
// testCtx is an arbitrary, non-default context. Non-nil also prevents linter errors.
testCtx = context.WithValue(context.Background(), struct{}{}, "arbitrary")
)
var _ context.Context = &HostContext{}
// HostContext contain the content will be used in host function call
type HostContext struct {
Content string
}
func (h *HostContext) Deadline() (deadline time.Time, ok bool) { return }
func (h *HostContext) Done() <-chan struct{} { return nil }
func (h *HostContext) Err() error { return nil }
func (h *HostContext) Value(key interface{}) interface{} { return nil }
func TestRuntime_CompileModule(t *testing.T) {
tests := []struct {
name string
runtime Runtime
wasm *wasm.Module
moduleBuilder HostModuleBuilder
expected func(CompiledModule)
}{
{
name: "no name section",
wasm: &wasm.Module{},
},
{
name: "empty NameSection.ModuleName",
wasm: &wasm.Module{NameSection: &wasm.NameSection{}},
},
{
name: "NameSection.ModuleName",
wasm: &wasm.Module{NameSection: &wasm.NameSection{ModuleName: "test"}},
expected: func(compiled CompiledModule) {
require.Equal(t, "test", compiled.Name())
},
},
{
name: "FunctionSection, but not exported",
wasm: &wasm.Module{
TypeSection: []wasm.FunctionType{{Params: []api.ValueType{api.ValueTypeI32}}},
FunctionSection: []wasm.Index{0},
CodeSection: []wasm.Code{{Body: []byte{wasm.OpcodeEnd}}},
},
expected: func(compiled CompiledModule) {
require.Nil(t, compiled.ImportedFunctions())
require.Zero(t, len(compiled.ExportedFunctions()))
},
},
{
name: "FunctionSection exported",
wasm: &wasm.Module{
TypeSection: []wasm.FunctionType{{Params: []api.ValueType{api.ValueTypeI32}}},
FunctionSection: []wasm.Index{0},
CodeSection: []wasm.Code{{Body: []byte{wasm.OpcodeEnd}}},
ExportSection: []wasm.Export{{
Type: wasm.ExternTypeFunc,
Name: "function",
Index: 0,
}},
},
expected: func(compiled CompiledModule) {
require.Nil(t, compiled.ImportedFunctions())
f := compiled.ExportedFunctions()["function"]
require.Equal(t, []api.ValueType{api.ValueTypeI32}, f.ParamTypes())
},
},
{
name: "MemorySection, but not exported",
wasm: &wasm.Module{
MemorySection: &wasm.Memory{Min: 2, Max: 3, IsMaxEncoded: true},
},
expected: func(compiled CompiledModule) {
require.Nil(t, compiled.ImportedMemories())
require.Zero(t, len(compiled.ExportedMemories()))
},
},
{
name: "MemorySection exported",
wasm: &wasm.Module{
MemorySection: &wasm.Memory{Min: 2, Max: 3, IsMaxEncoded: true},
ExportSection: []wasm.Export{{
Type: wasm.ExternTypeMemory,
Name: "memory",
Index: 0,
}},
},
expected: func(compiled CompiledModule) {
require.Nil(t, compiled.ImportedMemories())
mem := compiled.ExportedMemories()["memory"]
require.Equal(t, uint32(2), mem.Min())
max, ok := mem.Max()
require.Equal(t, uint32(3), max)
require.True(t, ok)
},
},
}
_r := NewRuntime(testCtx)
defer _r.Close(testCtx)
r := _r.(*runtime)
for _, tt := range tests {
tc := tt
t.Run(tc.name, func(t *testing.T) {
bin := binaryencoding.EncodeModule(tc.wasm)
m, err := r.CompileModule(testCtx, bin)
require.NoError(t, err)
if tc.expected == nil {
tc.expected = func(CompiledModule) {}
}
tc.expected(m)
require.Equal(t, r.store.Engine, m.(*compiledModule).compiledEngine)
// TypeIDs must be assigned to compiledModule.
expTypeIDs, err := r.store.GetFunctionTypeIDs(tc.wasm.TypeSection)
require.NoError(t, err)
require.Equal(t, expTypeIDs, m.(*compiledModule).typeIDs)
})
}
}
func TestRuntime_CompileModule_Errors(t *testing.T) {
tests := []struct {
name string
wasm []byte
expectedErr string
}{
{
name: "nil",
expectedErr: "invalid magic number",
},
{
name: "invalid binary",
wasm: append(binaryencoding.Magic, []byte("yolo")...),
expectedErr: "invalid version header",
},
{
name: "memory has too many pages",
wasm: binaryencoding.EncodeModule(&wasm.Module{MemorySection: &wasm.Memory{Min: 2, Cap: 2, Max: 70000, IsMaxEncoded: true}}),
expectedErr: "section memory: max 70000 pages (4 Gi) over limit of 65536 pages (4 Gi)",
},
}
r := NewRuntime(testCtx)
defer r.Close(testCtx)
for _, tt := range tests {
tc := tt
t.Run(tc.name, func(t *testing.T) {
_, err := r.CompileModule(testCtx, tc.wasm)
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
wasm []byte
expected bool
expectedLen uint32
}{
{
name: "no memory",
wasm: binaryencoding.EncodeModule(&wasm.Module{}),
},
{
name: "memory exported, one page",
wasm: binaryencoding.EncodeModule(&wasm.Module{
MemorySection: &wasm.Memory{Min: 1},
ExportSection: []wasm.Export{{Name: "memory", Type: api.ExternTypeMemory}},
}),
expected: true,
expectedLen: 65536,
},
}
for _, tt := range tests {
tc := tt
t.Run(tc.name, func(t *testing.T) {
r := NewRuntime(testCtx)
defer r.Close(testCtx)
// Instantiate the module and get the export of the above memory
module, err := r.Instantiate(testCtx, tc.wasm)
require.NoError(t, err)
mem := module.ExportedMemory("memory")
if tc.expected {
require.Equal(t, tc.expectedLen, mem.Size())
defs := module.ExportedMemoryDefinitions()
require.Equal(t, 1, len(defs))
def := defs["memory"]
require.Equal(t, tc.expectedLen>>16, def.Min())
} else {
require.Nil(t, mem)
require.Zero(t, len(module.ExportedMemoryDefinitions()))
}
})
}
}
// 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
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",
module: &wasm.Module{
GlobalSection: []wasm.Global{
{
Type: wasm.GlobalType{ValType: wasm.ValueTypeI64},
Init: wasm.ConstantExpression{Opcode: wasm.OpcodeI64Const, Data: leb128.EncodeInt64(globalVal)},
},
},
Exports: map[string]*wasm.Export{
"global": {Type: wasm.ExternTypeGlobal, Name: "global"},
},
},
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)},
},
},
Exports: map[string]*wasm.Export{
"global": {Type: wasm.ExternTypeGlobal, Name: "global"},
},
},
expected: true,
expectedMutable: true,
},
}
for _, tt := range tests {
tc := tt
t.Run(tc.name, func(t *testing.T) {
r := NewRuntime(testCtx).(*runtime)
defer r.Close(testCtx)
code := &compiledModule{module: tc.module}
err := r.store.Engine.CompileModule(testCtx, code.module, nil, false)
require.NoError(t, err)
// Instantiate the module and get the export of the above global
module, err := r.InstantiateModule(testCtx, code, NewModuleConfig())
require.NoError(t, err)
global := module.ExportedGlobal("global")
if !tc.expected {
require.Nil(t, global)
return
}
require.Equal(t, uint64(globalVal), global.Get())
mutable, ok := global.(api.MutableGlobal)
require.Equal(t, tc.expectedMutable, ok)
if ok {
mutable.Set(2)
require.Equal(t, uint64(2), global.Get())
}
})
}
}
func TestRuntime_InstantiateModule_UsesContext(t *testing.T) {
r := NewRuntime(testCtx)
defer r.Close(testCtx)
// 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)
}
_, err := r.NewHostModuleBuilder("env").
NewFunctionBuilder().WithFunc(start).Export("start").
Instantiate(testCtx)
require.NoError(t, err)
one := uint32(1)
binary := binaryencoding.EncodeModule(&wasm.Module{
TypeSection: []wasm.FunctionType{{}},
ImportSection: []wasm.Import{{Module: "env", Name: "start", Type: wasm.ExternTypeFunc, DescFunc: 0}},
FunctionSection: []wasm.Index{0},
CodeSection: []wasm.Code{
{Body: []byte{wasm.OpcodeCall, 0, wasm.OpcodeEnd}}, // Call the imported env.start.
},
StartSection: &one,
})
code, err := r.CompileModule(testCtx, binary)
require.NoError(t, err)
// Instantiate the module, which calls the start function. This will fail if the context wasn't as intended.
mod, err := r.InstantiateModule(testCtx, code, NewModuleConfig())
require.NoError(t, err)
require.True(t, calledStart)
// Closing the module shouldn't remove the compiler cache
require.NoError(t, mod.Close(testCtx))
require.Equal(t, uint32(2), r.(*runtime).store.Engine.CompiledModuleCount())
}
// TestRuntime_Instantiate_DoesntEnforce_Start ensures wapc-go work when modules import WASI, but don't
// export "_start".
func TestRuntime_Instantiate_DoesntEnforce_Start(t *testing.T) {
r := NewRuntime(testCtx)
defer r.Close(testCtx)
binary := binaryencoding.EncodeModule(&wasm.Module{
MemorySection: &wasm.Memory{Min: 1},
ExportSection: []wasm.Export{{Name: "memory", Type: wasm.ExternTypeMemory, Index: 0}},
})
mod, err := r.Instantiate(testCtx, binary)
require.NoError(t, err)
require.NoError(t, mod.Close(testCtx))
}
func TestRuntime_Instantiate_ErrorOnStart(t *testing.T) {
tests := []struct {
name, wasm string
}{
{
name: "_start function",
wasm: `(module
(import "" "start" (func $start))
(export "_start" (func $start))
)`,
},
{
name: ".start function",
wasm: `(module
(import "" "start" (func $start))
(start $start)
)`,
},
}
for _, tt := range tests {
tc := tt
t.Run(tc.name, func(t *testing.T) {
r := NewRuntime(testCtx)
defer r.Close(testCtx)
start := func() {
panic(errors.New("ice cream"))
}
host, err := r.NewHostModuleBuilder("host").
NewFunctionBuilder().WithFunc(start).Export("start").
Instantiate(testCtx)
require.NoError(t, err)
// Start the module as a WASI command. We expect it to fail.
_, err = r.Instantiate(testCtx, []byte(tc.wasm))
require.Error(t, err)
// Close the imported module, which should remove its compiler cache.
require.NoError(t, host.Close(testCtx))
// The compiler cache of the importing module should be removed on error.
require.Zero(t, r.(*runtime).store.Engine.CompiledModuleCount())
})
}
}
// TestRuntime_InstantiateModule_WithName tests that we can pre-validate (cache) a module and instantiate it under
// different names. This pattern is used in wapc-go.
func TestRuntime_InstantiateModule_WithName(t *testing.T) {
r := NewRuntime(testCtx)
defer r.Close(testCtx)
base, err := r.CompileModule(testCtx, binaryNamedZero)
require.NoError(t, err)
require.Equal(t, "0", base.(*compiledModule).module.NameSection.ModuleName)
// Use the same runtime to instantiate multiple modules
internal := r.(*runtime)
m1, err := r.InstantiateModule(testCtx, base, NewModuleConfig().WithName("1"))
require.NoError(t, err)
require.Equal(t, "1", m1.Name())
require.Nil(t, internal.Module("0"))
require.Equal(t, internal.Module("1"), m1)
m2, err := r.InstantiateModule(testCtx, base, NewModuleConfig().WithName("2"))
require.NoError(t, err)
require.Equal(t, "2", m2.Name())
require.Nil(t, internal.Module("0"))
require.Equal(t, internal.Module("2"), m2)
// Empty name module shouldn't be returned via Module() for future optimization.
m3, err := r.InstantiateModule(testCtx, base, NewModuleConfig().WithName(""))
require.NoError(t, err)
require.Equal(t, "", m3.Name())
ret := internal.Module("")
require.Nil(t, ret)
}
func TestRuntime_InstantiateModule_ExitError(t *testing.T) {
r := NewRuntime(testCtx)
defer r.Close(testCtx)
tests := []struct {
name string
exitCode uint32
export bool
expectedErr error
}{
{
name: "start: exit code 0",
exitCode: 0,
expectedErr: sys.NewExitError(0),
},
{
name: "start: exit code 2",
exitCode: 2,
expectedErr: sys.NewExitError(2),
},
{
name: "_start: exit code 0",
exitCode: 0,
export: true,
},
{
name: "_start: exit code 2",
exitCode: 2,
export: true,
expectedErr: sys.NewExitError(2),
},
}
for _, tt := range tests {
tc := tt
t.Run(tc.name, func(t *testing.T) {
start := func(ctx context.Context, m api.Module) {
require.NoError(t, m.CloseWithExitCode(ctx, tc.exitCode))
}
env, err := r.NewHostModuleBuilder("env").
NewFunctionBuilder().WithFunc(start).Export("exit").
Instantiate(testCtx)
require.NoError(t, err)
defer env.Close(testCtx)
mod := &wasm.Module{
TypeSection: []wasm.FunctionType{{}},
ImportSection: []wasm.Import{{Module: "env", Name: "exit", Type: wasm.ExternTypeFunc, DescFunc: 0}},
FunctionSection: []wasm.Index{0},
CodeSection: []wasm.Code{
{Body: []byte{wasm.OpcodeCall, 0, wasm.OpcodeEnd}}, // Call the imported env.start.
},
}
if tc.export {
mod.ExportSection = []wasm.Export{
{Name: "_start", Type: wasm.ExternTypeFunc, Index: 1},
}
} else {
one := uint32(1)
mod.StartSection = &one
}
binary := binaryencoding.EncodeModule(mod)
// Instantiate the module, which calls the start function.
m, err := r.InstantiateWithConfig(testCtx, binary,
NewModuleConfig().WithName("call-exit"))
// Ensure the exit error propagated and didn't wrap.
require.Equal(t, tc.expectedErr, err)
// Ensure calling close again doesn't break
if err == nil {
require.NoError(t, m.Close(testCtx))
}
})
}
}
func TestRuntime_CloseWithExitCode(t *testing.T) {
bin := binaryencoding.EncodeModule(&wasm.Module{
TypeSection: []wasm.FunctionType{{}},
FunctionSection: []wasm.Index{0},
CodeSection: []wasm.Code{{Body: []byte{wasm.OpcodeEnd}}},
ExportSection: []wasm.Export{{Type: wasm.ExternTypeFunc, Index: 0, Name: "func"}},
})
tests := []struct {
name string
exitCode uint32
}{
{
name: "exit code 0",
exitCode: uint32(0),
},
{
name: "exit code 2",
exitCode: uint32(2),
},
}
for _, tt := range tests {
tc := tt
t.Run(tc.name, func(t *testing.T) {
r := NewRuntime(testCtx)
code, err := r.CompileModule(testCtx, bin)
require.NoError(t, err)
// Instantiate two modules.
m1, err := r.InstantiateModule(testCtx, code, NewModuleConfig().WithName("mod1"))
require.NoError(t, err)
m2, err := r.InstantiateModule(testCtx, code, NewModuleConfig().WithName("mod2"))
require.NoError(t, err)
func1 := m1.ExportedFunction("func")
require.Equal(t, map[string]api.FunctionDefinition{"func": func1.Definition()},
m1.ExportedFunctionDefinitions())
func2 := m2.ExportedFunction("func")
require.Equal(t, map[string]api.FunctionDefinition{"func": func2.Definition()},
m2.ExportedFunctionDefinitions())
// Modules not closed so calls succeed
_, err = func1.Call(testCtx)
require.NoError(t, err)
_, err = func2.Call(testCtx)
require.NoError(t, err)
if tc.exitCode == 0 {
err = r.Close(testCtx)
} else {
err = r.CloseWithExitCode(testCtx, tc.exitCode)
}
require.NoError(t, err)
// Modules closed so calls fail
_, err = func1.Call(testCtx)
require.ErrorIs(t, err, sys.NewExitError(tc.exitCode))
_, err = func2.Call(testCtx)
require.ErrorIs(t, err, sys.NewExitError(tc.exitCode))
})
}
}
func TestHostFunctionWithCustomContext(t *testing.T) {
for _, tc := range []struct {
name string
config RuntimeConfig
}{
{name: "compiler", config: NewRuntimeConfigCompiler()},
{name: "interpreter", config: NewRuntimeConfigInterpreter()},
} {
t.Run(tc.name, func(t *testing.T) {
const fistString = "hello"
const secondString = "hello call"
hostCtx := &HostContext{fistString}
r := NewRuntimeWithConfig(hostCtx, tc.config)
defer r.Close(hostCtx)
// Define a function that will be set as the start function
var calledStart bool
var calledCall bool
start := func(ctx context.Context, module api.Module) {
hts, ok := ctx.(*HostContext)
if !ok {
t.Fatal("decorate call context could effect host ctx cast failed, please consider it.")
}
calledStart = true
require.NotNil(t, hts)
require.Equal(t, fistString, hts.Content)
}
callFunc := func(ctx context.Context, module api.Module) {
hts, ok := ctx.(*HostContext)
if !ok {
t.Fatal("decorate call context could effect host ctx cast failed, please consider it.")
}
calledCall = true
require.NotNil(t, hts)
require.Equal(t, secondString, hts.Content)
}
_, err := r.NewHostModuleBuilder("env").
NewFunctionBuilder().WithFunc(start).Export("host").
NewFunctionBuilder().WithFunc(callFunc).Export("host2").
Instantiate(hostCtx)
require.NoError(t, err)
startFnIndex := uint32(2)
binary := binaryencoding.EncodeModule(&wasm.Module{
TypeSection: []wasm.FunctionType{{}},
ImportSection: []wasm.Import{
{Module: "env", Name: "host", Type: wasm.ExternTypeFunc, DescFunc: 0},
{Module: "env", Name: "host2", Type: wasm.ExternTypeFunc, DescFunc: 0},
},
FunctionSection: []wasm.Index{0, 0},
CodeSection: []wasm.Code{
{Body: []byte{wasm.OpcodeCall, 0, wasm.OpcodeEnd}}, // Call the imported env.host.
{Body: []byte{wasm.OpcodeCall, 1, wasm.OpcodeEnd}}, // Call the imported env.host.
},
ExportSection: []wasm.Export{
{Type: api.ExternTypeFunc, Name: "callHost", Index: uint32(3)},
},
StartSection: &startFnIndex,
})
// Instantiate the module, which calls the start function. This will fail if the context wasn't as intended.
ins, err := r.Instantiate(hostCtx, binary)
require.NoError(t, err)
require.True(t, calledStart)
// add the new context content for call with used in host function
hostCtx.Content = secondString
_, err = ins.ExportedFunction("callHost").Call(hostCtx)
require.NoError(t, err)
require.True(t, calledCall)
})
}
}
func TestRuntime_Close_ClosesCompiledModules(t *testing.T) {
for _, tc := range []struct {
name string
withCompilationCache bool
}{
{name: "with cache", withCompilationCache: true},
{name: "without cache", withCompilationCache: false},
} {
t.Run(tc.name, func(t *testing.T) {
engine := &mockEngine{name: "mock", cachedModules: map[*wasm.Module]struct{}{}}
conf := *engineLessConfig
conf.newEngine = func(context.Context, api.CoreFeatures, filecache.Cache) wasm.Engine { return engine }
if tc.withCompilationCache {
conf.cache = NewCompilationCache()
}
r := NewRuntimeWithConfig(testCtx, &conf)
defer r.Close(testCtx)
// Normally compiled modules are closed when instantiated but this is never instantiated.
_, err := r.CompileModule(testCtx, binaryNamedZero)
require.NoError(t, err)
require.Equal(t, uint32(1), engine.CompiledModuleCount())
err = r.Close(testCtx)
require.NoError(t, err)
// Closing the runtime should remove the compiler cache if cache is not configured.
require.Equal(t, !tc.withCompilationCache, engine.closed)
})
}
}
// TestRuntime_Closed ensures invocation of closed Runtime's methods is safe.
func TestRuntime_Closed(t *testing.T) {
for _, tc := range []struct {
name string
errFunc func(r Runtime, mod CompiledModule) error
}{
{
name: "InstantiateModule",
errFunc: func(r Runtime, mod CompiledModule) error {
_, err := r.InstantiateModule(testCtx, mod, NewModuleConfig())
return err
},
},
{
name: "Instantiate",
errFunc: func(r Runtime, mod CompiledModule) error {
_, err := r.Instantiate(testCtx, binaryNamedZero)
return err
},
},
{
name: "CompileModule",
errFunc: func(r Runtime, mod CompiledModule) error {
_, err := r.CompileModule(testCtx, binaryNamedZero)
return err
},
},
} {
t.Run(tc.name, func(t *testing.T) {
engine := &mockEngine{name: "mock", cachedModules: map[*wasm.Module]struct{}{}}
conf := *engineLessConfig
conf.newEngine = func(context.Context, api.CoreFeatures, filecache.Cache) wasm.Engine { return engine }
r := NewRuntimeWithConfig(testCtx, &conf)
defer r.Close(testCtx)
// Normally compiled modules are closed when instantiated but this is never instantiated.
mod, err := r.CompileModule(testCtx, binaryNamedZero)
require.NoError(t, err)
require.Equal(t, uint32(1), engine.CompiledModuleCount())
err = r.Close(testCtx)
require.NoError(t, err)
// Closing the runtime should remove the compiler cache if cache is not configured.
require.True(t, engine.closed)
require.EqualError(t, tc.errFunc(r, mod), "runtime closed with exit_code(0)")
})
}
}
type mockEngine struct {
name string
cachedModules map[*wasm.Module]struct{}
closed bool
}
// CompileModule implements the same method as documented on wasm.Engine.
func (e *mockEngine) CompileModule(_ context.Context, module *wasm.Module, _ []experimental.FunctionListener, _ bool) error {
e.cachedModules[module] = struct{}{}
return nil
}
// CompiledModuleCount implements the same method as documented on wasm.Engine.
func (e *mockEngine) CompiledModuleCount() uint32 {
return uint32(len(e.cachedModules))
}
// DeleteCompiledModule implements the same method as documented on wasm.Engine.
func (e *mockEngine) DeleteCompiledModule(module *wasm.Module) {
delete(e.cachedModules, module)
}
// NewModuleEngine implements the same method as documented on wasm.Engine.
func (e *mockEngine) NewModuleEngine(_ *wasm.Module, _ *wasm.ModuleInstance) (wasm.ModuleEngine, error) {
return nil, nil
}
// NewModuleEngine implements the same method as documented on wasm.Close.
func (e *mockEngine) Close() (err error) {
e.closed = true
return
}
// TestNewRuntime_concurrent ensures that concurrent execution of NewRuntime is race-free.
// This depends on -race flag.
func TestNewRuntime_concurrent(t *testing.T) {
const num = 100
var wg sync.WaitGroup
c := NewCompilationCache()
// If available, uses two engine configurations for the single compilation cache.
configs := [2]RuntimeConfig{NewRuntimeConfigInterpreter().WithCompilationCache(c)}
if platform.CompilerSupported() {
configs[1] = NewRuntimeConfigCompiler().WithCompilationCache(c)
} else {
configs[1] = NewRuntimeConfigInterpreter().WithCompilationCache(c)
}
wg.Add(num)
for i := 0; i < num; i++ {
i := i
go func() {
defer wg.Done()
r := NewRuntimeWithConfig(testCtx, configs[i%2])
err := r.Close(testCtx)
require.NoError(t, err)
}()
}
wg.Wait()
}