Debian Codegraph @

imports/emscripten/emscripten_test.go

raw
1 package emscripten
2
3 import (
4 "bytes"
5 "context"
6 _ "embed"
7 "testing"
8
9 "github.com/tetratelabs/wazero"
10 "github.com/tetratelabs/wazero/experimental"
11 "github.com/tetratelabs/wazero/experimental/logging"
12 "github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
13 internal "github.com/tetratelabs/wazero/internal/emscripten"
14 "github.com/tetratelabs/wazero/internal/testing/binaryencoding"
15 "github.com/tetratelabs/wazero/internal/testing/require"
16 "github.com/tetratelabs/wazero/internal/wasm"
17 )
18
19 const (
20 i64 = wasm.ValueTypeI64
21 f32 = wasm.ValueTypeF32
22 f64 = wasm.ValueTypeF64
23 )
24
25 // growWasm was compiled from testdata/grow.cc
26 //
27 //go:embed testdata/grow.wasm
28 var growWasm []byte
29
30 // invokeWasm was generated by the following:
31 //
32 // cd testdata; wasm-tools parse invoke.wat -o invoke.wasm
33 //
34 //go:embed testdata/invoke.wasm
35 var invokeWasm []byte
36
37 type arbitrary struct{}
38
39 // testCtx is an arbitrary, non-default context. Non-nil also prevents linter errors.
40 var testCtx = context.WithValue(context.Background(), arbitrary{}, "arbitrary")
41
42 // TestGrow is an integration test until we have an Emscripten example.
43 func TestGrow(t *testing.T) {
44 var log bytes.Buffer
45
46 // Set context to one that has an experimental listener
47 ctx := experimental.WithFunctionListenerFactory(testCtx,
48 logging.NewHostLoggingListenerFactory(&log, logging.LogScopeMemory))
49
50 r := wazero.NewRuntime(ctx)
51 defer r.Close(ctx)
52
53 wasi_snapshot_preview1.MustInstantiate(ctx, r)
54
55 _, err := Instantiate(ctx, r)
56 require.NoError(t, err)
57
58 // Emscripten exits main with zero by default, which coerces to nul.
59 _, err = r.Instantiate(ctx, growWasm)
60 require.Nil(t, err)
61
62 // We expect the memory no-op memory growth hook to be invoked as wasm.
63 require.Contains(t, log.String(), "==> env.emscripten_notify_memory_growth(memory_index=0)")
64 }
65
66 func TestNewFunctionExporterForModule(t *testing.T) {
67 tests := []struct {
68 name string
69 input *wasm.Module
70 expected emscriptenFns
71 }{
72 {
73 name: "empty",
74 input: &wasm.Module{},
75 expected: emscriptenFns{},
76 },
77 {
78 name: internal.FunctionNotifyMemoryGrowth,
79 input: &wasm.Module{
80 TypeSection: []wasm.FunctionType{
81 {Params: []wasm.ValueType{i32}},
82 },
83 ImportSection: []wasm.Import{
84 {
85 Module: "env", Name: internal.FunctionNotifyMemoryGrowth,
86 Type: wasm.ExternTypeFunc,
87 DescFunc: 0,
88 },
89 },
90 },
91 expected: []*wasm.HostFunc{internal.NotifyMemoryGrowth},
92 },
93 {
94 name: "all result types",
95 input: &wasm.Module{
96 TypeSection: []wasm.FunctionType{
97 {Params: []wasm.ValueType{i32}},
98 {Params: []wasm.ValueType{i32}, Results: []wasm.ValueType{i32}},
99 {Params: []wasm.ValueType{i32}, Results: []wasm.ValueType{i64}},
100 {Params: []wasm.ValueType{i32}, Results: []wasm.ValueType{f32}},
101 {Params: []wasm.ValueType{i32}, Results: []wasm.ValueType{f64}},
102 },
103 ImportSection: []wasm.Import{
104 {
105 Module: "env", Name: "invoke_v",
106 Type: wasm.ExternTypeFunc,
107 DescFunc: 0,
108 },
109 {
110 Module: "env", Name: "invoke_i",
111 Type: wasm.ExternTypeFunc,
112 DescFunc: 1,
113 },
114 {
115 Module: "env", Name: "invoke_p",
116 Type: wasm.ExternTypeFunc,
117 DescFunc: 1,
118 },
119 {
120 Module: "env", Name: "invoke_j",
121 Type: wasm.ExternTypeFunc,
122 DescFunc: 2,
123 },
124 {
125 Module: "env", Name: "invoke_f",
126 Type: wasm.ExternTypeFunc,
127 DescFunc: 3,
128 },
129 {
130 Module: "env", Name: "invoke_d",
131 Type: wasm.ExternTypeFunc,
132 DescFunc: 4,
133 },
134 },
135 },
136 expected: []*wasm.HostFunc{
137 {
138 ExportName: "invoke_v",
139 ParamTypes: []wasm.ValueType{i32},
140 ParamNames: []string{"index"},
141 Code: wasm.Code{GoFunc: &internal.InvokeFunc{FunctionType: &wasm.FunctionType{}}},
142 },
143 {
144 ExportName: "invoke_i",
145 ParamTypes: []wasm.ValueType{i32},
146 ParamNames: []string{"index"},
147 ResultTypes: []wasm.ValueType{i32},
148 Code: wasm.Code{GoFunc: &internal.InvokeFunc{FunctionType: &wasm.FunctionType{Results: []wasm.ValueType{i32}}}},
149 },
150 {
151 ExportName: "invoke_p",
152 ParamTypes: []wasm.ValueType{i32},
153 ParamNames: []string{"index"},
154 ResultTypes: []wasm.ValueType{i32},
155 Code: wasm.Code{GoFunc: &internal.InvokeFunc{FunctionType: &wasm.FunctionType{Results: []wasm.ValueType{i32}}}},
156 },
157 {
158 ExportName: "invoke_j",
159 ParamTypes: []wasm.ValueType{i32},
160 ParamNames: []string{"index"},
161 ResultTypes: []wasm.ValueType{i64},
162 Code: wasm.Code{GoFunc: &internal.InvokeFunc{FunctionType: &wasm.FunctionType{Results: []wasm.ValueType{i64}}}},
163 },
164 {
165 ExportName: "invoke_f",
166 ParamTypes: []wasm.ValueType{i32},
167 ParamNames: []string{"index"},
168 ResultTypes: []wasm.ValueType{f32},
169 Code: wasm.Code{GoFunc: &internal.InvokeFunc{FunctionType: &wasm.FunctionType{Results: []wasm.ValueType{f32}}}},
170 },
171 {
172 ExportName: "invoke_d",
173 ParamTypes: []wasm.ValueType{i32},
174 ParamNames: []string{"index"},
175 ResultTypes: []wasm.ValueType{f64},
176 Code: wasm.Code{GoFunc: &internal.InvokeFunc{FunctionType: &wasm.FunctionType{Results: []wasm.ValueType{f64}}}},
177 },
178 },
179 },
180 {
181 name: "ignores other imports",
182 input: &wasm.Module{
183 TypeSection: []wasm.FunctionType{
184 {Params: []wasm.ValueType{i32}},
185 },
186 ImportSection: []wasm.Import{
187 {
188 Module: "anv", Name: "invoke_v",
189 Type: wasm.ExternTypeFunc,
190 DescFunc: 0,
191 },
192 {
193 Module: "env", Name: "invoke_v",
194 Type: wasm.ExternTypeFunc,
195 DescFunc: 0,
196 },
197 {
198 Module: "env", Name: "grow",
199 Type: wasm.ExternTypeFunc,
200 DescFunc: 0,
201 },
202 },
203 },
204 expected: []*wasm.HostFunc{
205 {
206 ExportName: "invoke_v",
207 ParamTypes: []wasm.ValueType{i32},
208 ParamNames: []string{"index"},
209 Code: wasm.Code{GoFunc: &internal.InvokeFunc{FunctionType: &wasm.FunctionType{}}},
210 },
211 },
212 },
213 {
214 name: "invoke_v and " + internal.FunctionNotifyMemoryGrowth,
215 input: &wasm.Module{
216 TypeSection: []wasm.FunctionType{{Params: []wasm.ValueType{i32}}},
217 ImportSection: []wasm.Import{
218 {
219 Module: "env", Name: "invoke_v",
220 Type: wasm.ExternTypeFunc,
221 DescFunc: 0,
222 },
223 {
224 Module: "env", Name: internal.FunctionNotifyMemoryGrowth,
225 Type: wasm.ExternTypeFunc,
226 DescFunc: 0,
227 },
228 },
229 },
230 expected: []*wasm.HostFunc{
231 {
232 ExportName: "invoke_v",
233 ParamTypes: []wasm.ValueType{i32},
234 ParamNames: []string{"index"},
235 Code: wasm.Code{GoFunc: &internal.InvokeFunc{FunctionType: &wasm.FunctionType{}}},
236 },
237 internal.NotifyMemoryGrowth,
238 },
239 },
240 {
241 name: "invoke_vi",
242 input: &wasm.Module{
243 TypeSection: []wasm.FunctionType{
244 {Params: []wasm.ValueType{i32, i32}},
245 },
246 ImportSection: []wasm.Import{
247 {
248 Module: "env", Name: "invoke_vi",
249 Type: wasm.ExternTypeFunc,
250 DescFunc: 0,
251 },
252 },
253 },
254 expected: []*wasm.HostFunc{
255 {
256 ExportName: "invoke_vi",
257 ParamTypes: []wasm.ValueType{i32, i32},
258 ParamNames: []string{"index", "a1"},
259 Code: wasm.Code{GoFunc: &internal.InvokeFunc{FunctionType: &wasm.FunctionType{Params: []wasm.ValueType{i32}}}},
260 },
261 },
262 },
263 {
264 name: "invoke_iiiii",
265 input: &wasm.Module{
266 TypeSection: []wasm.FunctionType{
267 {
268 Params: []wasm.ValueType{i32, i32, i32, i32, i32},
269 Results: []wasm.ValueType{i32},
270 },
271 },
272 ImportSection: []wasm.Import{
273 {
274 Module: "env", Name: "invoke_iiiii",
275 Type: wasm.ExternTypeFunc,
276 DescFunc: 0,
277 },
278 },
279 },
280 expected: []*wasm.HostFunc{
281 {
282 ExportName: "invoke_iiiii",
283 ParamTypes: []wasm.ValueType{i32, i32, i32, i32, i32},
284 ParamNames: []string{"index", "a1", "a2", "a3", "a4"},
285 ResultTypes: []wasm.ValueType{i32},
286 Code: wasm.Code{GoFunc: &internal.InvokeFunc{FunctionType: &wasm.FunctionType{
287 Params: []wasm.ValueType{i32, i32, i32, i32},
288 Results: []wasm.ValueType{i32},
289 }}},
290 },
291 },
292 },
293 {
294 name: "invoke_viiiddiiiiii",
295 input: &wasm.Module{
296 TypeSection: []wasm.FunctionType{
297 {
298 Params: []wasm.ValueType{i32, i32, i32, i32, f64, f64, i32, i32, i32, i32, i32, i32},
299 },
300 },
301 ImportSection: []wasm.Import{
302 {
303 Module: "env", Name: "invoke_viiiddiiiiii",
304 Type: wasm.ExternTypeFunc,
305 DescFunc: 0,
306 },
307 },
308 },
309 expected: []*wasm.HostFunc{
310 {
311 ExportName: "invoke_viiiddiiiiii",
312 ParamTypes: []wasm.ValueType{i32, i32, i32, i32, f64, f64, i32, i32, i32, i32, i32, i32},
313 ParamNames: []string{"index", "a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8", "a9", "a10", "a11"},
314 Code: wasm.Code{GoFunc: &internal.InvokeFunc{FunctionType: &wasm.FunctionType{
315 Params: []wasm.ValueType{i32, i32, i32, f64, f64, i32, i32, i32, i32, i32, i32},
316 }}},
317 },
318 },
319 },
320 }
321
322 for _, tt := range tests {
323 tc := tt
324
325 t.Run(tc.name, func(t *testing.T) {
326 r := wazero.NewRuntime(testCtx)
327 defer r.Close(testCtx)
328
329 guest, err := r.CompileModule(testCtx, binaryencoding.EncodeModule(tc.input))
330 require.NoError(t, err)
331
332 exporter, err := NewFunctionExporterForModule(guest)
333 require.NoError(t, err)
334 actual := exporter.(emscriptenFns)
335
336 require.Equal(t, len(tc.expected), len(actual))
337 for i, expected := range tc.expected {
338 require.Equal(t, expected, actual[i], actual[i].ExportName)
339 }
340 })
341 }
342 }
343
344 func TestInstantiateForModule(t *testing.T) {
345 var log bytes.Buffer
346
347 // Set context to one that has an experimental listener
348 ctx := experimental.WithFunctionListenerFactory(testCtx, logging.NewLoggingListenerFactory(&log))
349
350 r := wazero.NewRuntime(ctx)
351 defer r.Close(ctx)
352
353 compiled, err := r.CompileModule(ctx, invokeWasm)
354 require.NoError(t, err)
355
356 _, err = InstantiateForModule(ctx, r, compiled)
357 require.NoError(t, err)
358
359 mod, err := r.InstantiateModule(ctx, compiled, wazero.NewModuleConfig())
360 require.NoError(t, err)
361
362 tests := []struct {
363 name, funcName string
364 tableOffset int
365 params, expectedResults []uint64
366 expectedLog string
367 }{
368 {
369 name: "invoke_i",
370 funcName: "call_v_i32",
371 expectedResults: []uint64{42},
372 expectedLog: `--> .call_v_i32(0)
373 ==> env.invoke_i(index=0)
374 --> .stackSave()
375 <-- 65536
376 --> .v_i32()
377 <-- 42
378 <== 42
379 <-- 42
380 `,
381 },
382 {
383 name: "invoke_ii",
384 funcName: "call_i32_i32",
385 tableOffset: 2,
386 params: []uint64{42},
387 expectedResults: []uint64{42},
388 expectedLog: `--> .call_i32_i32(2,42)
389 ==> env.invoke_ii(index=2,a1=42)
390 --> .stackSave()
391 <-- 65536
392 --> .i32_i32(42)
393 <-- 42
394 <== 42
395 <-- 42
396 `,
397 },
398 {
399 name: "invoke_iii",
400 funcName: "call_i32i32_i32",
401 tableOffset: 4,
402 params: []uint64{1, 2},
403 expectedResults: []uint64{3},
404 expectedLog: `--> .call_i32i32_i32(4,1,2)
405 ==> env.invoke_iii(index=4,a1=1,a2=2)
406 --> .stackSave()
407 <-- 65536
408 --> .i32i32_i32(1,2)
409 <-- 3
410 <== 3
411 <-- 3
412 `,
413 },
414 {
415 name: "invoke_iiii",
416 funcName: "call_i32i32i32_i32",
417 tableOffset: 6,
418 params: []uint64{1, 2, 4},
419 expectedResults: []uint64{7},
420 expectedLog: `--> .call_i32i32i32_i32(6,1,2,4)
421 ==> env.invoke_iiii(index=6,a1=1,a2=2,a3=4)
422 --> .stackSave()
423 <-- 65536
424 --> .i32i32i32_i32(1,2,4)
425 <-- 7
426 <== 7
427 <-- 7
428 `,
429 },
430 {
431 name: "invoke_iiiii",
432 funcName: "calli32_i32i32i32i32_i32",
433 tableOffset: 8,
434 params: []uint64{1, 2, 4, 8},
435 expectedResults: []uint64{15},
436 expectedLog: `--> .calli32_i32i32i32i32_i32(8,1,2,4,8)
437 ==> env.invoke_iiiii(index=8,a1=1,a2=2,a3=4,a4=8)
438 --> .stackSave()
439 <-- 65536
440 --> .i32i32i32i32_i32(1,2,4,8)
441 <-- 15
442 <== 15
443 <-- 15
444 `,
445 },
446 {
447 name: "invoke_v",
448 funcName: "call_v_v",
449 tableOffset: 10,
450 expectedLog: `--> .call_v_v(10)
451 ==> env.invoke_v(index=10)
452 --> .stackSave()
453 <-- 65536
454 --> .v_v()
455 <--
456 <==
457 <--
458 `,
459 },
460 {
461 name: "invoke_vi",
462 funcName: "call_i32_v",
463 tableOffset: 12,
464 params: []uint64{42},
465 expectedLog: `--> .call_i32_v(12,42)
466 ==> env.invoke_vi(index=12,a1=42)
467 --> .stackSave()
468 <-- 65536
469 --> .i32_v(42)
470 <--
471 <==
472 <--
473 `,
474 },
475 {
476 name: "invoke_vii",
477 funcName: "call_i32i32_v",
478 tableOffset: 14,
479 params: []uint64{1, 2},
480 expectedLog: `--> .call_i32i32_v(14,1,2)
481 ==> env.invoke_vii(index=14,a1=1,a2=2)
482 --> .stackSave()
483 <-- 65536
484 --> .i32i32_v(1,2)
485 <--
486 <==
487 <--
488 `,
489 },
490 {
491 name: "invoke_viii",
492 funcName: "call_i32i32i32_v",
493 tableOffset: 16,
494 params: []uint64{1, 2, 4},
495 expectedLog: `--> .call_i32i32i32_v(16,1,2,4)
496 ==> env.invoke_viii(index=16,a1=1,a2=2,a3=4)
497 --> .stackSave()
498 <-- 65536
499 --> .i32i32i32_v(1,2,4)
500 <--
501 <==
502 <--
503 `,
504 },
505 {
506 name: "invoke_viiii",
507 funcName: "calli32_i32i32i32i32_v",
508 tableOffset: 18,
509 params: []uint64{1, 2, 4, 8},
510 expectedLog: `--> .calli32_i32i32i32i32_v(18,1,2,4,8)
511 ==> env.invoke_viiii(index=18,a1=1,a2=2,a3=4,a4=8)
512 --> .stackSave()
513 <-- 65536
514 --> .i32i32i32i32_v(1,2,4,8)
515 <--
516 <==
517 <--
518 `,
519 },
520 {
521 name: "invoke_v_with_longjmp",
522 funcName: "call_invoke_v_with_longjmp_throw",
523 tableOffset: 20,
524 params: []uint64{},
525 expectedLog: `--> .call_invoke_v_with_longjmp_throw(20)
526 ==> env.invoke_v(index=20)
527 --> .stackSave()
528 <-- 42
529 --> .call_longjmp_throw()
530 ==> env._emscripten_throw_longjmp()
531 --> .stackRestore(42)
532 <--
533 --> .setThrew(1,0)
534 <--
535 <==
536 <--
537 `,
538 },
539 }
540
541 for _, tt := range tests {
542 tc := tt
543
544 t.Run(tc.name, func(t *testing.T) {
545 defer log.Reset()
546
547 params := tc.params
548 params = append([]uint64{uint64(tc.tableOffset)}, params...)
549
550 results, err := mod.ExportedFunction(tc.funcName).Call(testCtx, params...)
551 require.NoError(t, err)
552 l := len(tc.expectedResults)
553 require.Equal(t, l, len(results))
554 if l > 0 {
555 require.Equal(t, tc.expectedResults, results)
556 }
557
558 // We expect to see the dynamic function call target
559 require.Equal(t, tc.expectedLog, log.String())
560
561 // We expect an unreachable function to err
562 params[0]++
563 _, err = mod.ExportedFunction(tc.funcName).Call(testCtx, params...)
564 require.Error(t, err)
565 })
566 }
567 }