WasmEdge-go icon indicating copy to clipboard operation
WasmEdge-go copied to clipboard

Failed when executing func in goroutines

Open DarumaDocker opened this issue 4 years ago • 0 comments

For https://github.com/second-state/WasmEdge-go-examples/blob/master/go_BindgenFuncs/bindgen_funcs.go

If I put the vm as a global variable, and call the vm.ExecuteBindgen in goroutine:

package main
​
import (
	"fmt"
	"os"
​
	"github.com/second-state/WasmEdge-go/wasmedge"
)
​
var vm *wasmedge.VM
​
func main() {
	/// Expected Args[0]: program name (./bindgen_funcs)
	/// Expected Args[1]: wasm or wasm-so file (rust_bindgen_funcs_lib_bg.wasm))
​
	/// Set not to print debug info
	wasmedge.SetLogErrorLevel()
​
	/// Create configure
	var conf = wasmedge.NewConfigure(wasmedge.WASI)
​
	/// Create VM with configure
	vm = wasmedge.NewVMWithConfig(conf)
​
	/// Init WASI
	var wasi = vm.GetImportObject(wasmedge.WASI)
	wasi.InitWasi(
		os.Args[1:],     /// The args
		os.Environ(),    /// The envs
		[]string{".:."}, /// The mapping directories
		[]string{},      /// The preopens will be empty
	)
​
	/// Instantiate wasm
	vm.LoadWasmFile(os.Args[1])
	vm.Validate()
	vm.Instantiate()
​
	go run()
	run()
​
	vm.Delete()
	conf.Delete()
​
}
​
func run() {
	/// Run bindgen functions
	var res interface{}
	var err error
	/// create_line: array, array, array -> array (inputs are JSON stringified)
	res, err = vm.ExecuteBindgen("create_line", wasmedge.Bindgen_return_array, []byte("{\"x\":1.5,\"y\":3.8}"), []byte("{\"x\":2.5,\"y\":5.8}"), []byte("A thin red line"))
	if err == nil {
		fmt.Println("Run bindgen -- create_line:", string(res.([]byte)))
	} else {
		fmt.Println("Run bindgen -- create_line FAILED")
	}
	/// say: array -> array
	res, err = vm.ExecuteBindgen("say", wasmedge.Bindgen_return_array, []byte("bindgen funcs test"))
	if err == nil {
		fmt.Println("Run bindgen -- say:", string(res.([]byte)))
	} else {
		fmt.Println("Run bindgen -- say FAILED")
	}
	/// obfusticate: array -> array
	res, err = vm.ExecuteBindgen("obfusticate", wasmedge.Bindgen_return_array, []byte("A quick brown fox jumps over the lazy dog"))
	if err == nil {
		fmt.Println("Run bindgen -- obfusticate:", string(res.([]byte)))
	} else {
		fmt.Println("Run bindgen -- obfusticate FAILED")
	}
	/// lowest_common_multiple: i32, i32 -> i32
	res, err = vm.ExecuteBindgen("lowest_common_multiple", wasmedge.Bindgen_return_i32, int32(123), int32(2))
	if err == nil {
		fmt.Println("Run bindgen -- lowest_common_multiple:", res.(int32))
	} else {
		fmt.Println("Run bindgen -- lowest_common_multiple FAILED")
	}
	/// sha3_digest: array -> array
	res, err = vm.ExecuteBindgen("sha3_digest", wasmedge.Bindgen_return_array, []byte("This is an important message"))
	if err == nil {
		fmt.Println("Run bindgen -- sha3_digest:", res.([]byte))
	} else {
		fmt.Println("Run bindgen -- sha3_digest FAILED")
	}
	/// keccak_digest: array -> array
	res, err = vm.ExecuteBindgen("keccak_digest", wasmedge.Bindgen_return_array, []byte("This is an important message"))
	if err == nil {
		fmt.Println("Run bindgen -- keccak_digest:", res.([]byte))
	} else {
		fmt.Println("Run bindgen -- keccak_digest FAILED")
	}
}

Then the program will fail with error:

unexpected signal during runtime execution
[signal SIGSEGV: segmentation violation code=0x1 addr=0x4 pc=0x7f1311798288]

runtime stack:
runtime.throw({0x4b41a6, 0x0})
        /usr/local/go/src/runtime/panic.go:1198 +0x71
runtime.sigpanic()
        /usr/local/go/src/runtime/signal_unix.go:719 +0x396

goroutine 6 [syscall]:
runtime.cgocall(0x493420, 0xc00004acd0)
        /usr/local/go/src/runtime/cgocall.go:156 +0x5c fp=0xc00004ac98 sp=0xc00004ac60 pc=0x40a2dc
github.com/second-state/WasmEdge-go/wasmedge._Cfunc_WasmEdge_VMExecute(0xfbd420, {0xb, 0x4ae60d}, 0xc00010a180, 0x7, 0x0, 0x0)
        _cgo_gotypes.go:1779 +0x49 fp=0xc00004acd0 sp=0xc00004ac98 pc=0x489a89
github.com/second-state/WasmEdge-go/wasmedge.(*VM).ExecuteBindgen.func1(0xc00004afa0, {0x3, 0x4ae60d}, 0x100000000000000, {0xc00010a180, 0x7, 0x18}, 0x7f1310afb108, {0x57d8f0, 0x0, ...})
        /root/github/second-state/WasmEdge-go/wasmedge/vm.go:218 +0xb1 fp=0xc00004ad48 sp=0xc00004acd0 pc=0x48fcf1
github.com/second-state/WasmEdge-go/wasmedge.(*VM).ExecuteBindgen(0xc0000162e0, {0x4ae60d, 0xb}, 0x0, {0xc00004afa0, 0x3, 0x3})
        /root/github/second-state/WasmEdge-go/wasmedge/vm.go:218 +0x169 fp=0xc00004ae00 sp=0xc00004ad48 pc=0x48fb29
main.run()
        /root/github/second-state/WasmEdge-go-examples/go_BindgenFuncs/bindgen_funcs.go:52 +0x196 fp=0xc00004afe0 sp=0xc00004ae00 pc=0x491036
runtime.goexit()
        /usr/local/go/src/runtime/asm_amd64.s:1581 +0x1 fp=0xc00004afe8 sp=0xc00004afe0 pc=0x4630e1
created by main.main
        /root/github/second-state/WasmEdge-go-examples/go_BindgenFuncs/bindgen_funcs.go:39 +0x1d6

goroutine 1 [runnable]:
github.com/second-state/WasmEdge-go/wasmedge._Cfunc_WasmEdge_VMExecute(0xfbd420, {0xb, 0x4ae6c8}, 0xc0000a60c0, 0x3, 0x0, 0x0)
        _cgo_gotypes.go:1779 +0x49
github.com/second-state/WasmEdge-go/wasmedge.(*VM).ExecuteBindgen.func1(0xc000049d30, {0x1, 0x4ae6c8}, 0x10000c000049c80, {0xc0000a60c0, 0x3, 0x18}, 0x7f1310afb5b8, {0x57d8f0, 0x0, ...})
        /root/github/second-state/WasmEdge-go/wasmedge/vm.go:218 +0xb1
github.com/second-state/WasmEdge-go/wasmedge.(*VM).ExecuteBindgen(0xc0000a0120, {0x4ae6c8, 0xb}, 0x2, {0xc000049d30, 0x1, 0x1})
        /root/github/second-state/WasmEdge-go/wasmedge/vm.go:218 +0x169
main.run()
        /root/github/second-state/WasmEdge-go-examples/go_BindgenFuncs/bindgen_funcs.go:80 +0x74b
main.main()
        /root/github/second-state/WasmEdge-go-examples/go_BindgenFuncs/bindgen_funcs.go:40 +0x1db

DarumaDocker avatar Nov 01 '21 15:11 DarumaDocker