Skip to content

Commit

Permalink
feat: Implement ValidateModule.
Browse files Browse the repository at this point in the history
It depends on It requires
wasmerio/wasmer#1636. The `include/` file is
already from this PR.
  • Loading branch information
Hywan committed Sep 18, 2020
1 parent 5e11202 commit 48a49a9
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
Binary file modified wasmer/include/libwasmer_c_api.dylib
Binary file not shown.
37 changes: 34 additions & 3 deletions wasmer/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ package wasmer
//
// return wasm_module_new(store, &wasm_bytes);
// }
//
// bool to_wasm_module_validate(wasm_store_t *store, uint8_t *bytes, size_t bytes_length) {
// wasm_byte_vec_t wasm_bytes;
// wasm_bytes.size = bytes_length;
// wasm_bytes.data = (wasm_byte_t*) bytes;
//
// return wasm_module_validate(store, &wasm_bytes);
// }
import "C"
import (
"runtime"
Expand All @@ -38,9 +46,6 @@ type Module struct {
}

func NewModule(store *Store, bytes []byte) (*Module, error) {
// If `bytes` contains a Wasm module with the WAT format,
// compile it to Wasm bytes.
// If it does not, it will return the same bytes.
wasmBytes, err := Wat2Wasm(string(bytes))

if err != nil {
Expand Down Expand Up @@ -71,6 +76,32 @@ func NewModule(store *Store, bytes []byte) (*Module, error) {
return module, nil
}

func ValidateModule(store *Store, bytes []byte) error {
wasmBytes, err := Wat2Wasm(string(bytes))

if err != nil {
return err
}

var wasmBytesPtr *C.uint8_t
wasmBytesLength := len(wasmBytes)

if wasmBytesLength > 0 {
wasmBytesPtr = (*C.uint8_t)(unsafe.Pointer(&wasmBytes[0]))
}

isValid := C.to_wasm_module_validate(store.inner(), wasmBytesPtr, C.size_t(wasmBytesLength))

runtime.KeepAlive(bytes)
runtime.KeepAlive(wasmBytes)

if !isValid {
return newErrorFromWasmer()
}

return nil
}

func (module *Module) inner() *C.wasm_module_t {
return module._inner
}
8 changes: 8 additions & 0 deletions wasmer/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,11 @@ func TestModule(t *testing.T) {

assert.NoError(t, err)
}

func TestValidateModule(t *testing.T) {
engine := NewEngine()
store := NewStore(engine)
err := ValidateModule(store, []byte("(module)"))

assert.NoError(t, err)
}

0 comments on commit 48a49a9

Please sign in to comment.