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

Wasmer instance gets garbage collected while still in use #364

Open nicolasmattelaer opened 2 years ago

nicolasmattelaer commented 2 years ago

Thanks for the bug report!

Describe the bug

If, after using a wasmer instance, the garbage collector runs, it sweeps the instance and causes the program to crash when calling wasm functions with a segmentation fault.

Steps to reproduce

  1. Running the example code at https://pkg.go.dev/github.com/wasmerio/wasmer-go/wasmer, but adding runtime.GC() right below the sum, err := instance.Exports.GetFunction("sum") line

Expected behavior

Program runs and finishes as normal

Actual behavior

You get a segmentation fault:

fatal error: unexpected signal during runtime execution
[signal SIGSEGV: segmentation violation code=0x1 addr=0x7f9d78702263 pc=0x7f9d78702263]

due to instance having been garbage collected.

Additional context

I suggest adding defer instance.Close() to the example at https://pkg.go.dev/github.com/wasmerio/wasmer-go/wasmer. This will prevent the bug but only because a reference to instance is kept, not because the instance.Close() function does anything to prevent it.