Debian Codegraph @

internal/testing/nodiff/nodiff.go

raw symbols SCIP
1 package nodiff
2
3 import (
4 "bytes"
5 "context"
6 "errors"
7 "fmt"
8 "sort"
9 "strings"
10 "testing"
11 "unsafe"
12
13 "github.com/tetratelabs/wazero"
14 "github.com/tetratelabs/wazero/api"
15 "github.com/tetratelabs/wazero/experimental"
16 "github.com/tetratelabs/wazero/experimental/logging"
17 "github.com/tetratelabs/wazero/internal/testing/binaryencoding"
18 "github.com/tetratelabs/wazero/internal/testing/require"
19 "github.com/tetratelabs/wazero/internal/wasm"
20 )
21
22 // We haven't had public APIs for referencing all the imported entries from wazero.CompiledModule,
23 // so we use the unsafe.Pointer and the internal memory layout to get the internal *wasm.Module
24 // from wazero.CompiledFunction. This must be synced with the struct definition of wazero.compiledModule (internal one).
25 func extractInternalWasmModuleFromCompiledModule(c wazero.CompiledModule) (*wasm.Module, error) {
26 // This is the internal representation of interface in Go.
27 // https://research.swtch.com/interfaces
28 type iface struct {
29 tp *byte
30 data unsafe.Pointer
31 }
32
33 // This corresponds to the unexported wazero.compiledModule to get *wasm.Module from wazero.CompiledModule interface.
34 type compiledModule struct {
35 module *wasm.Module
36 }
37
38 ciface := (*iface)(unsafe.Pointer(&c))
39 if ciface == nil {
40 return nil, errors.New("invalid pointer")
41 }
42 cm := (*compiledModule)(ciface.data)
43 return cm.module, nil
44 }
45
46 // RequireNoDiffT is a wrapper of RequireNoDiff for testing.T.
47 func RequireNoDiffT(t *testing.T, wasmBin []byte, checkMemory, loggingCheck bool) {
48 RequireNoDiff(wasmBin, checkMemory, loggingCheck, func(err error) { require.NoError(t, err) })
49 }
50
51 // RequireNoDiff ensures that the behavior is the same between the compiler and the interpreter for any given binary.
52 func RequireNoDiff(wasmBin []byte, checkMemory, loggingCheck bool, requireNoError func(err error)) {
53 const features = api.CoreFeaturesV2 | experimental.CoreFeaturesThreads | experimental.CoreFeaturesTailCall | experimental.CoreFeaturesExtendedConst | experimental.CoreFeaturesExceptionHandling | experimental.CoreFeaturesTypedFunctionReferences
54 compiler := wazero.NewRuntimeWithConfig(context.Background(), wazero.NewRuntimeConfigCompiler().WithCoreFeatures(features))
55 interpreter := wazero.NewRuntimeWithConfig(context.Background(), wazero.NewRuntimeConfigInterpreter().WithCoreFeatures(features))
56 defer compiler.Close(context.Background())
57 defer interpreter.Close(context.Background())
58
59 interpreterCtx, compilerCtx := context.Background(), context.Background()
60 var interPreterLoggingBuf, compilerLoggingBuf bytes.Buffer
61 var errorDuringInvocation bool
62 if loggingCheck {
63 interpreterCtx = experimental.WithFunctionListenerFactory(interpreterCtx, logging.NewLoggingListenerFactory(&interPreterLoggingBuf))
64 compilerCtx = experimental.WithFunctionListenerFactory(compilerCtx, logging.NewLoggingListenerFactory(&compilerLoggingBuf))
65 defer func() {
66 if !errorDuringInvocation {
67 if !bytes.Equal(compilerLoggingBuf.Bytes(), interPreterLoggingBuf.Bytes()) {
68 requireNoError(fmt.Errorf("logging mismatch\ncompiler: %s\ninterpreter: %s",
69 compilerLoggingBuf.String(), interPreterLoggingBuf.String()))
70 }
71 }
72 }()
73 }
74
75 compilerCompiled, err := compiler.CompileModule(compilerCtx, wasmBin)
76 if err != nil && strings.Contains(err.Error(), "has an empty module name") {
77 // This is the limitation wazero imposes to allow special-casing of anonymous modules.
78 return
79 }
80 requireNoError(err)
81
82 interpreterCompiled, err := interpreter.CompileModule(interpreterCtx, wasmBin)
83 requireNoError(err)
84
85 internalMod, err := extractInternalWasmModuleFromCompiledModule(compilerCompiled)
86 requireNoError(err)
87
88 if skip := ensureDummyImports(compiler, internalMod, requireNoError); skip {
89 return
90 }
91 ensureDummyImports(interpreter, internalMod, requireNoError)
92
93 // Instantiate module.
94 compilerMod, compilerInstErr := compiler.InstantiateModule(compilerCtx, compilerCompiled,
95 wazero.NewModuleConfig().WithName(string(internalMod.ID[:])))
96 interpreterMod, interpreterInstErr := interpreter.InstantiateModule(interpreterCtx, interpreterCompiled,
97 wazero.NewModuleConfig().WithName(string(internalMod.ID[:])))
98
99 okToInvoke, err := ensureInstantiationError(compilerInstErr, interpreterInstErr)
100 requireNoError(err)
101
102 if okToInvoke {
103 err, errorDuringInvocation = ensureInvocationResultMatch(
104 compilerCtx, interpreterCtx,
105 compilerMod, interpreterMod, interpreterCompiled.ExportedFunctions())
106 requireNoError(err)
107
108 compilerMem, _ := compilerMod.Memory().(*wasm.MemoryInstance)
109 interpreterMem, _ := interpreterMod.Memory().(*wasm.MemoryInstance)
110 if checkMemory && compilerMem != nil && interpreterMem != nil {
111 if !bytes.Equal(compilerMem.Buffer, interpreterMem.Buffer) {
112 requireNoError(errors.New("memory state mimsmatch"))
113 }
114 }
115 ensureMutableGlobalsMatch(compilerMod, interpreterMod, requireNoError)
116 }
117 }
118
119 func ensureMutableGlobalsMatch(compilerMod, interpreterMod api.Module, requireNoError func(err error)) {
120 ci, ii := compilerMod.(*wasm.ModuleInstance), interpreterMod.(*wasm.ModuleInstance)
121 if len(ci.Globals) == 0 {
122 return
123 }
124 var es []string
125 for i := range ci.Globals[:len(ci.Globals)-1] { // The last global is the fuel, so we can ignore it.
126 cg := ci.Globals[i]
127 ig := ii.Globals[i]
128 if !cg.Type.Mutable {
129 continue
130 }
131
132 cVal, cValHi := cg.Value()
133 iVal, iValHi := ig.Value()
134
135 var ok bool
136 switch ig.Type.ValType {
137 case wasm.ValueTypeI32, wasm.ValueTypeF32:
138 ok = uint32(cVal) == uint32(iVal)
139 case wasm.ValueTypeI64, wasm.ValueTypeF64:
140 ok = cVal == iVal
141 case wasm.ValueTypeV128:
142 ok = cVal == iVal && cValHi == iValHi
143 default:
144 ok = true // Ignore other types.
145 }
146
147 if !ok {
148 if typ := ig.Type.ValType; typ == wasm.ValueTypeV128 {
149 es = append(es, fmt.Sprintf("\t[%d] %s: (%v,%v) != (%v,%v)",
150 i, wasm.ValueTypeName(wasm.ValueTypeV128), cVal, cValHi, iVal, iValHi))
151 } else {
152 es = append(es, fmt.Sprintf("\t[%d] %s: %v != %v",
153 i, wasm.ValueTypeName(typ), cVal, iVal))
154 }
155 }
156 }
157 if len(es) > 0 {
158 requireNoError(fmt.Errorf("mutable globals mismatch:\n%s", strings.Join(es, "\n")))
159 }
160 }
161
162 // ensureDummyImports instantiates the modules which are required imports by `origin` *wasm.Module.
163 func ensureDummyImports(r wazero.Runtime, origin *wasm.Module, requireNoError func(err error)) (skip bool) {
164 impMods := make(map[string][]wasm.Import)
165 for _, imp := range origin.ImportSection {
166 if imp.Module == "" {
167 // Importing empty modules are forbidden as future work will allow multiple anonymous modules.
168 skip = true
169 return
170 }
171 impMods[imp.Module] = append(impMods[imp.Module], imp)
172 }
173
174 for mName, impMod := range impMods {
175 usedName := make(map[string]struct{}, len(impMod))
176 m := &wasm.Module{NameSection: &wasm.NameSection{ModuleName: mName}}
177
178 for _, imp := range impMod {
179 _, ok := usedName[imp.Name]
180 if ok {
181 // Import segment can have duplicated "{module_name}.{name}" pair while it is prohibited for exports.
182 // Decision on allowing modules with these "ill" imports or not is up to embedder, and wazero chooses
183 // not to allow. Hence, we skip the entire case.
184 // See "Note" at https://www.w3.org/TR/wasm-core-2/syntax/modules.html#imports
185 return true
186 } else {
187 usedName[imp.Name] = struct{}{}
188 }
189
190 var index uint32
191 switch imp.Type {
192 case wasm.ExternTypeFunc:
193 tp := origin.TypeSection[imp.DescFunc]
194 typeIdx := uint32(len(m.TypeSection))
195 index = uint32(len(m.FunctionSection))
196 m.FunctionSection = append(m.FunctionSection, typeIdx)
197 m.TypeSection = append(m.TypeSection, tp)
198 body := bytes.NewBuffer(nil)
199 for _, vt := range tp.Results {
200 switch vt {
201 case wasm.ValueTypeI32:
202 body.WriteByte(wasm.OpcodeI32Const)
203 body.WriteByte(0)
204 case wasm.ValueTypeI64:
205 body.WriteByte(wasm.OpcodeI64Const)
206 body.WriteByte(0)
207 case wasm.ValueTypeF32:
208 body.Write([]byte{wasm.OpcodeF32Const, 0, 0, 0, 0})
209 case wasm.ValueTypeF64:
210 body.Write([]byte{wasm.OpcodeF64Const, 0, 0, 0, 0, 0, 0, 0, 0})
211 case wasm.ValueTypeV128:
212 body.Write([]byte{
213 wasm.OpcodeVecPrefix, wasm.OpcodeVecV128Const,
214 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
215 })
216 case wasm.ValueTypeExternref:
217 body.Write([]byte{wasm.OpcodeRefNull, wasm.RefTypeExternref.Kind()})
218 case wasm.ValueTypeFuncref:
219 body.Write([]byte{wasm.OpcodeRefNull, wasm.RefTypeFuncref.Kind()})
220 case wasm.ValueTypeExnref:
221 body.Write([]byte{wasm.OpcodeRefNull, wasm.ValueTypeExnref.Kind()})
222 default:
223 if vt.IsRef() {
224 body.Write([]byte{wasm.OpcodeRefNull, wasm.RefTypeFuncref.Kind()})
225 }
226 }
227 }
228 body.WriteByte(wasm.OpcodeEnd)
229 m.CodeSection = append(m.CodeSection, wasm.Code{Body: body.Bytes()})
230 case wasm.ExternTypeGlobal:
231 index = uint32(len(m.GlobalSection))
232 var data []byte
233 var opcode byte
234 switch imp.DescGlobal.ValType {
235 case wasm.ValueTypeI32:
236 opcode = wasm.OpcodeI32Const
237 data = []byte{0}
238 case wasm.ValueTypeI64:
239 opcode = wasm.OpcodeI64Const
240 data = []byte{0}
241 case wasm.ValueTypeF32:
242 opcode = wasm.OpcodeF32Const
243 data = []byte{0, 0, 0, 0}
244 case wasm.ValueTypeF64:
245 opcode = wasm.OpcodeF64Const
246 data = []byte{0, 0, 0, 0, 0, 0, 0, 0}
247 case wasm.ValueTypeV128:
248 opcode = wasm.OpcodeVecPrefix
249 data = []byte{wasm.OpcodeVecV128Const, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
250 case wasm.ValueTypeExternref:
251 opcode = wasm.OpcodeRefNull
252 data = []byte{wasm.RefTypeExternref.Kind()}
253 case wasm.ValueTypeFuncref:
254 opcode = wasm.OpcodeRefNull
255 data = []byte{wasm.RefTypeFuncref.Kind()}
256 case wasm.ValueTypeExnref:
257 opcode = wasm.OpcodeRefNull
258 data = []byte{wasm.ValueTypeExnref.Kind()}
259 default:
260 if imp.DescGlobal.ValType.IsRef() {
261 opcode = wasm.OpcodeRefNull
262 data = []byte{wasm.RefTypeFuncref.Kind()}
263 }
264 }
265 m.GlobalSection = append(m.GlobalSection, wasm.Global{
266 Type: imp.DescGlobal, Init: wasm.NewConstantExpressionFromOpcode(opcode, data),
267 })
268 case wasm.ExternTypeMemory:
269 m.MemorySection = imp.DescMem
270 index = 0
271 case wasm.ExternTypeTable:
272 index = uint32(len(m.TableSection))
273 m.TableSection = append(m.TableSection, imp.DescTable)
274 }
275 m.ExportSection = append(m.ExportSection, wasm.Export{Type: imp.Type, Name: imp.Name, Index: index})
276 }
277 _, err := r.Instantiate(context.Background(), binaryencoding.EncodeModule(m))
278 requireNoError(err)
279 }
280 return
281 }
282
283 const valueTypeVector = 0x7b
284
285 // ensureInvocationResultMatch invokes all the exported functions from the module, and compare all the results between compiler vs interpreter.
286 func ensureInvocationResultMatch(
287 compilerCtx, interpreterCtx context.Context, compiledMod, interpreterMod api.Module,
288 exportedFunctions map[string]api.FunctionDefinition,
289 ) (err error, errorDuringInvocation bool) {
290 // In order to do the deterministic execution, we need to sort the exported functions.
291 var names []string
292 for f := range exportedFunctions {
293 names = append(names, f)
294 }
295 sort.Strings(names)
296
297 outer:
298 for _, name := range names {
299 def := exportedFunctions[name]
300 resultTypes := def.ResultTypes()
301 for _, rt := range resultTypes {
302 switch rt {
303 case api.ValueTypeI32, api.ValueTypeI64, api.ValueTypeF32, api.ValueTypeF64, valueTypeVector:
304 default:
305 // For the sake of simplicity in the assertion, we only invoke the function with the basic types.
306 continue outer
307 }
308 }
309
310 cmpF := compiledMod.ExportedFunction(name)
311 intF := interpreterMod.ExportedFunction(name)
312
313 params := getDummyValues(def.ParamTypes())
314 cmpRes, cmpErr := cmpF.Call(compilerCtx, params...)
315 intRes, intErr := intF.Call(interpreterCtx, params...)
316 errorDuringInvocation = errorDuringInvocation || cmpErr != nil || intErr != nil
317 if errMismatch := ensureInvocationError(cmpErr, intErr); errMismatch != nil {
318 err = errors.Join(err, fmt.Errorf("error mismatch on invoking %s: %v", name, errMismatch))
319 continue
320 }
321
322 matched := true
323 var typesIndex int
324 for i := 0; i < len(cmpRes); i++ {
325 switch resultTypes[typesIndex] {
326 case api.ValueTypeI32, api.ValueTypeF32:
327 matched = matched && uint32(cmpRes[i]) == uint32(intRes[i])
328 case api.ValueTypeI64, api.ValueTypeF64:
329 matched = matched && cmpRes[i] == intRes[i]
330 case valueTypeVector:
331 matched = matched && cmpRes[i] == intRes[i] && cmpRes[i+1] == intRes[i+1]
332 i++ // We need to advance twice (lower and higher 64bits)
333 }
334 typesIndex++
335 }
336
337 if !matched {
338 err = errors.Join(err, fmt.Errorf("result mismatch on invoking '%s':\n\tinterpreter got: %v\n\tcompiler got: %v", name, intRes, cmpRes))
339 }
340 }
341 return
342 }
343
344 // getDummyValues returns a dummy input values for function invocations.
345 func getDummyValues(valueTypes []api.ValueType) (ret []uint64) {
346 for _, vt := range valueTypes {
347 if vt != 0x7b { // v128
348 ret = append(ret, 0)
349 } else {
350 ret = append(ret, 0, 0)
351 }
352 }
353 return
354 }
355
356 // ensureInvocationError ensures that function invocation errors returned by interpreter and compiler match each other's.
357 func ensureInvocationError(compilerErr, interpErr error) error {
358 if compilerErr == nil && interpErr == nil {
359 return nil
360 } else if compilerErr == nil && interpErr != nil {
361 return fmt.Errorf("compiler returned no error, but interpreter got: %w", interpErr)
362 } else if compilerErr != nil && interpErr == nil {
363 return fmt.Errorf("interpreter returned no error, but compiler got: %w", compilerErr)
364 }
365
366 compilerErrMsg, interpErrMsg := compilerErr.Error(), interpErr.Error()
367 if idx := strings.Index(compilerErrMsg, "\n"); idx >= 0 {
368 compilerErrMsg = compilerErrMsg[:strings.Index(compilerErrMsg, "\n")]
369 }
370 if idx := strings.Index(interpErrMsg, "\n"); idx >= 0 {
371 interpErrMsg = interpErrMsg[:strings.Index(interpErrMsg, "\n")]
372 }
373
374 if compiledStackOverFlow := strings.Contains(compilerErrMsg, "stack overflow"); compiledStackOverFlow && strings.Contains(interpErrMsg, "unreachable") {
375 // Compiler is more likely to reach stack overflow than interpreter, so we allow this case. This case is most likely
376 // that interpreter reached the unreachable out of "fuel".
377 return nil
378 } else if interpreterStackOverFlow := strings.Contains(interpErrMsg, "stack overflow"); compiledStackOverFlow && interpreterStackOverFlow {
379 // Both compiler and interpreter reached stack overflow, so we ignore diff in the content of the traces.
380 return nil
381 }
382
383 if compilerErrMsg != interpErrMsg {
384 return fmt.Errorf("error mismatch:\n\tinterpreter: %v\n\tcompiler: %v", interpErr, compilerErr)
385 }
386 return nil
387 }
388
389 // ensureInstantiationError ensures that instantiation errors returned by interpreter and compiler match each other's.
390 func ensureInstantiationError(compilerErr, interpErr error) (okToInvoke bool, err error) {
391 if compilerErr == nil && interpErr == nil {
392 return true, nil
393 } else if compilerErr == nil && interpErr != nil {
394 return false, fmt.Errorf("compiler returned no error, but interpreter got: %w", interpErr)
395 } else if compilerErr != nil && interpErr == nil {
396 return false, fmt.Errorf("interpreter returned no error, but compiler got: %w", compilerErr)
397 }
398
399 compilerErrMsg, interpErrMsg := compilerErr.Error(), interpErr.Error()
400 if idx := strings.Index(compilerErrMsg, "\n"); idx >= 0 {
401 compilerErrMsg = compilerErrMsg[:strings.Index(compilerErrMsg, "\n")]
402 }
403 if idx := strings.Index(interpErrMsg, "\n"); idx >= 0 {
404 interpErrMsg = interpErrMsg[:strings.Index(interpErrMsg, "\n")]
405 }
406
407 if strings.Contains(compilerErrMsg, "stack overflow") && strings.Contains(interpErrMsg, "unreachable") {
408 // This is the case where the compiler reached stack overflow, but the interpreter reached the unreachable out of "fuel" during
409 // start function invocation. This is fine.
410 return false, nil
411 }
412
413 if !allowedErrorDuringInstantiation(compilerErrMsg) {
414 return false, fmt.Errorf("invalid error occur with compiler: %v vs interpreter: %v", compilerErr, interpErr)
415 } else if !allowedErrorDuringInstantiation(interpErrMsg) {
416 return false, fmt.Errorf("invalid error occur with interpreter: %v vs compiler: %v", interpErr, compilerErr)
417 }
418
419 if compilerErrMsg != interpErrMsg {
420 return false, fmt.Errorf("error mismatch:\n\tinterpreter: %v\n\tcompiler: %v", interpErr, compilerErr)
421 }
422 return false, nil
423 }
424
425 // allowedErrorDuringInstantiation checks if the error message is considered sane.
426 func allowedErrorDuringInstantiation(errMsg string) bool {
427 // This happens when data segment causes out of bound, but it is considered as runtime-error in WebAssembly 2.0
428 // which is fine.
429 if strings.HasPrefix(errMsg, "data[") && strings.HasSuffix(errMsg, "]: out of bounds memory access") {
430 return true
431 }
432
433 // Start function failure is neither instantiation nor compilation error, but rather a runtime error, so that is fine.
434 if strings.HasPrefix(errMsg, "start function[") && strings.Contains(errMsg, "failed: wasm error:") {
435 return true
436 }
437 return false
438 }