wasmerio / wasmer-go

🐹🕸️ WebAssembly runtime for Go
https://pkg.go.dev/github.com/wasmerio/wasmer-go
MIT License
2.84k stars 161 forks source link

Allocate memory fail? #387

Open orangeC23 opened 1 year ago

orangeC23 commented 1 year ago

Describe the bug

The wat file:

(module 
 (type $0 (func))
 (func (export "start") (type $0))
 (memory (;0;) 65536)
)

The go file:

package main

import (
    "fmt"
    "github.com/wasmerio/wasmer-go/wasmer"
    "io/ioutil"
)

func main() {
    wasmBytes, _ := ioutil.ReadFile("./tmp.wasm")

    engine := wasmer.NewEngine()
    store := wasmer.NewStore(engine)

    // Compiles the module
    module, err := wasmer.NewModule(store, wasmBytes)

    if err != nil {
        fmt.Println("Failed to compile module:", err)
    }

    importObject := wasmer.NewImportObject()
    instance, err := wasmer.NewInstance(module, importObject)

    if err != nil {
        panic(fmt.Sprintln("Failed to instantiate the module:", err))
    }

    func1, err := instance.Exports.GetFunction("start")
    if err != nil {
        panic(fmt.Sprintln("Failed to get the `start` function:", err))
    }

    result, err := func1()
    fmt.Println(result)
}

Steps to reproduce

wat2wasm tmp.wat
go run tmp.go

Expected behavior

Successfully allocate memory in the wat file.

Actual behavior

wasmer-go prints:

# command-line-arguments
ld: warning: -no_pie is deprecated when targeting new OS versions
ld: warning: non-standard -pagezero_size is deprecated when targeting macOS 13.0 or later
thread '<unnamed>' panicked at 'called `Result::unwrap()` on an `Err` value: TryFromIntError(())', lib/vm/src/memory.rs:269:62
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
fatal runtime error: failed to initiate panic, error 5
SIGABRT: abort
PC=0x7ff80454422a m=0 sigcode=0
signal arrived during cgo execution

goroutine 1 [syscall, locked to thread]:
runtime.cgocall(0x409a750, 0xc000065da0)
    /usr/local/Cellar/go/1.17.5/libexec/src/runtime/cgocall.go:156 +0x5c fp=0xc000065d78 sp=0xc000065d40 pc=0x400453c
github.com/wasmerio/wasmer-go/wasmer._Cfunc_wasm_instance_new(0x600000c040c0, 0x600000008470, 0xc000012260, 0xc00001a0a8)
    _cgo_gotypes.go:1676 +0x4d fp=0xc000065da0 sp=0xc000065d78 pc=0x4091e0d
github.com/wasmerio/wasmer-go/wasmer.NewInstance.func1.1(0xc000065e78, 0xc000012260, 0xc000012230)
    /Users/zhangyixuan/go/src/github.com/wasmerio/wasmer-go/wasmer/instance.go:44 +0xd1 fp=0xc000065df8 sp=0xc000065da0 pc=0x4096491
github.com/wasmerio/wasmer-go/wasmer.NewInstance.func1()
    /Users/zhangyixuan/go/src/github.com/wasmerio/wasmer-go/wasmer/instance.go:44 +0x33 fp=0xc000065e30 sp=0xc000065df8 pc=0x4096373
github.com/wasmerio/wasmer-go/wasmer.maybeNewErrorFromWasmer(0xc000065e98)
    /Users/zhangyixuan/go/src/github.com/wasmerio/wasmer-go/wasmer/error.go:44 +0x57 fp=0xc000065e58 sp=0xc000065e30 pc=0x4092d37
github.com/wasmerio/wasmer-go/wasmer.NewInstance(0xc000070020, 0xc000010030)
    /Users/zhangyixuan/go/src/github.com/wasmerio/wasmer-go/wasmer/instance.go:39 +0xae fp=0xc000065ed0 sp=0xc000065e58 pc=0x409620e
main.main()
    /Users/zhangyixuan/WASM/myProject/EmpiricalStudyArtifact/artifact/detector/A.BackendCompilation/[A.8]Validation_error/[A.8]-1/wasmer-go-A8-1.go:23 +0x10b fp=0xc000065f80 sp=0xc000065ed0 pc=0x40995cb
runtime.main()
    /usr/local/Cellar/go/1.17.5/libexec/src/runtime/proc.go:255 +0x227 fp=0xc000065fe0 sp=0xc000065f80 pc=0x4034ae7
runtime.goexit()
    /usr/local/Cellar/go/1.17.5/libexec/src/runtime/asm_amd64.s:1581 +0x1 fp=0xc000065fe8 sp=0xc000065fe0 pc=0x405e381

rax    0x0
rbx    0x7ff847eb9680
rcx    0x7ff7bfefd8c8
rdx    0x0
rdi    0x103
rsi    0x6
rbp    0x7ff7bfefd8f0
rsp    0x7ff7bfefd8c8
r8     0x7ff7bfefd70f
r9     0x0
r10    0x0
r11    0x246
r12    0x103
r13    0x0
r14    0x6
r15    0x16
rip    0x7ff80454422a
rflags 0x246
cs     0x7
fs     0x0
gs     0x0
exit status 2

However, if execute the command 'wasmer run tmp.wasm -i start', wasmer could successfully allocate the memory.