diff --git a/internal/integration_test/fuzz/wazerolib/extern.go b/internal/integration_test/fuzz/wazerolib/extern.go new file mode 100644 index 0000000000..792530d7d3 --- /dev/null +++ b/internal/integration_test/fuzz/wazerolib/extern.go @@ -0,0 +1,62 @@ +package main + +import "C" +import ( + "reflect" + "unsafe" +) + +func main() {} + +// require_no_diff ensures that the behavior is the same between the compiler and the interpreter for any given binary. +// And if there's diff, this also saves the problematic binary and wat into testdata directory. +// +//export require_no_diff +func require_no_diff(binaryPtr uintptr, binarySize int, checkMemory bool) { + // TODO: use unsafe.Slice after flooring Go 1.20. + var wasmBin []byte + wasmHdr := (*reflect.SliceHeader)(unsafe.Pointer(&wasmBin)) + wasmHdr.Data = binaryPtr + wasmHdr.Len = binarySize + wasmHdr.Cap = binarySize + + failed := true + defer func() { + if failed { + // If the test fails, we save the binary and wat into testdata directory. + saveFailedBinary(wasmBin, "TestReRunFailedRequireNoDiffCase") + } + }() + + requireNoDiff(wasmBin, checkMemory, func(err error) { + if err != nil { + panic(err) + } + }) + + failed = false +} + +// validate accepts maybe-invalid Wasm module bytes and ensures that our validation phase works correctly +// as well as the compiler doesn't panic during compilation! +// +//export validate +func validate(binaryPtr uintptr, binarySize int) { + // TODO: use unsafe.Slice after flooring Go 1.20. + var wasmBin []byte + wasmHdr := (*reflect.SliceHeader)(unsafe.Pointer(&wasmBin)) + wasmHdr.Data = binaryPtr + wasmHdr.Len = binarySize + wasmHdr.Cap = binarySize + + failed := true + defer func() { + if failed { + // If the test fails, we save the binary and wat into testdata directory. + saveFailedBinary(wasmBin, "TestReRunFailedValidateCase") + } + }() + + tryCompile(wasmBin) + failed = false +} diff --git a/internal/integration_test/fuzz/wazerolib/lib.go b/internal/integration_test/fuzz/wazerolib/lib.go index f6a2bc7a7c..ca0620a297 100644 --- a/internal/integration_test/fuzz/wazerolib/lib.go +++ b/internal/integration_test/fuzz/wazerolib/lib.go @@ -1,6 +1,5 @@ package main -import "C" import ( "context" "crypto/sha256" @@ -19,8 +18,6 @@ import ( "github.com/tetratelabs/wazero/internal/wasm" ) -func main() {} - const failedCasesDir = "wazerolib/testdata" // saveFailedBinary writes binary and wat into failedCasesDir so that it is easy to reproduce the error. diff --git a/internal/integration_test/fuzz/wazerolib/nodiff.go b/internal/integration_test/fuzz/wazerolib/nodiff.go index ae20c5e410..037488bc89 100644 --- a/internal/integration_test/fuzz/wazerolib/nodiff.go +++ b/internal/integration_test/fuzz/wazerolib/nodiff.go @@ -1,12 +1,10 @@ package main -import "C" import ( "bytes" "context" "errors" "fmt" - "reflect" "sort" "strings" "unsafe" @@ -18,35 +16,6 @@ import ( "github.com/tetratelabs/wazero/internal/wasm" ) -// require_no_diff ensures that the behavior is the same between the compiler and the interpreter for any given binary. -// And if there's diff, this also saves the problematic binary and wat into testdata directory. -// -//export require_no_diff -func require_no_diff(binaryPtr uintptr, binarySize int, checkMemory bool) { - // TODO: use unsafe.Slice after flooring Go 1.20. - var wasmBin []byte - wasmHdr := (*reflect.SliceHeader)(unsafe.Pointer(&wasmBin)) - wasmHdr.Data = binaryPtr - wasmHdr.Len = binarySize - wasmHdr.Cap = binarySize - - failed := true - defer func() { - if failed { - // If the test fails, we save the binary and wat into testdata directory. - saveFailedBinary(wasmBin, "TestReRunFailedRequireNoDiffCase") - } - }() - - requireNoDiff(wasmBin, checkMemory, func(err error) { - if err != nil { - panic(err) - } - }) - - failed = false -} - // We haven't had public APIs for referencing all the imported entries from wazero.CompiledModule, // so we use the unsafe.Pointer and the internal memory layout to get the internal *wasm.Module // from wazero.CompiledFunction. This must be synced with the struct definition of wazero.compiledModule (internal one). diff --git a/internal/integration_test/fuzz/wazerolib/testdata/test.wasm b/internal/integration_test/fuzz/wazerolib/testdata/test.wasm index c0282923b7..7ba12f9429 100644 Binary files a/internal/integration_test/fuzz/wazerolib/testdata/test.wasm and b/internal/integration_test/fuzz/wazerolib/testdata/test.wasm differ diff --git a/internal/integration_test/fuzz/wazerolib/validate.go b/internal/integration_test/fuzz/wazerolib/validate.go index 4f39e5eec6..b7ca229553 100644 --- a/internal/integration_test/fuzz/wazerolib/validate.go +++ b/internal/integration_test/fuzz/wazerolib/validate.go @@ -1,38 +1,11 @@ package main -import "C" import ( "context" - "reflect" - "unsafe" "github.com/tetratelabs/wazero" ) -// validate accepts maybe-invalid Wasm module bytes and ensures that our validation phase works correctly -// as well as the compiler doesn't panic during compilation! -// -//export validate -func validate(binaryPtr uintptr, binarySize int) { - // TODO: use unsafe.Slice after flooring Go 1.20. - var wasmBin []byte - wasmHdr := (*reflect.SliceHeader)(unsafe.Pointer(&wasmBin)) - wasmHdr.Data = binaryPtr - wasmHdr.Len = binarySize - wasmHdr.Cap = binarySize - - failed := true - defer func() { - if failed { - // If the test fails, we save the binary and wat into testdata directory. - saveFailedBinary(wasmBin, "TestReRunFailedValidateCase") - } - }() - - tryCompile(wasmBin) - failed = false -} - // Ensure that validation and compilation do not panic! func tryCompile(wasmBin []byte) { ctx := context.Background()