Skip to content

Commit

Permalink
extern
Browse files Browse the repository at this point in the history
Signed-off-by: Takeshi Yoneda <[email protected]>
  • Loading branch information
mathetake committed Feb 9, 2024
1 parent 00e684e commit e5b302c
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 61 deletions.
62 changes: 62 additions & 0 deletions internal/integration_test/fuzz/wazerolib/extern.go
Original file line number Diff line number Diff line change
@@ -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
}
3 changes: 0 additions & 3 deletions internal/integration_test/fuzz/wazerolib/lib.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package main

import "C"
import (
"context"
"crypto/sha256"
Expand All @@ -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.
Expand Down
31 changes: 0 additions & 31 deletions internal/integration_test/fuzz/wazerolib/nodiff.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package main

import "C"
import (
"bytes"
"context"
"errors"
"fmt"
"reflect"
"sort"
"strings"
"unsafe"
Expand All @@ -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).
Expand Down
Binary file modified internal/integration_test/fuzz/wazerolib/testdata/test.wasm
Binary file not shown.
27 changes: 0 additions & 27 deletions internal/integration_test/fuzz/wazerolib/validate.go
Original file line number Diff line number Diff line change
@@ -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()
Expand Down

0 comments on commit e5b302c

Please sign in to comment.