Files
wazero/internal/integration_test/fuzz/wazerolib/validate.go
Nuno Cruces 6503e82d3c Follow unsafe rules. (#1449)
Signed-off-by: Nuno Cruces <ncruces@users.noreply.github.com>
2023-05-09 15:34:08 +01:00

42 lines
1.0 KiB
Go

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()
compiler := wazero.NewRuntimeWithConfig(ctx, wazero.NewRuntimeConfigCompiler())
_, _ = compiler.CompileModule(ctx, wasmBin)
}