second-state / WasmEdge-go

The GO language SDK and API for WasmEdge
https://www.secondstate.io/articles/extend-golang-app-with-webassembly-rust/
Apache License 2.0
107 stars 16 forks source link

ast.ListImports() and ListExports() panic with invalid memory address or nil pointer dereference #37

Closed loredanacirstea closed 1 year ago

loredanacirstea commented 1 year ago

I was following the docs from https://wasmedge.org/book/en/sdk/go/ref.html#ast-module:

loader := wasmedge.NewLoader()
ast, err := loader.LoadBuffer(wasmbuffer)
if err != nil {
    fmt.Println("Load WASM from file FAILED:", err.Error())
}

imports := ast.ListImports()
for _, mimport := range imports {
    fmt.Println("Import:", mimport.GetModuleName(), mimport.GetExternalName())
}

// List the exports.
exports := ast.ListExports()
for _, mexport := range exports {
    fmt.Println("Export:", mexport.GetExternalName())
}

ast.Release()
loader.Release()

The panic comes from https://github.com/second-state/WasmEdge-go/blob/746946a0f856f0ddb6523fb3c9780d19493cb56a/wasmedge/ast.go#L63, respectively https://github.com/second-state/WasmEdge-go/blob/746946a0f856f0ddb6523fb3c9780d19493cb56a/wasmedge/ast.go#L83

When initializing imptype = make([]*ImportType, uint(ltypes)), imptype gets initialized with nil. So the solution is to initialize imptype[i] = &ImportType{} before line 63.

From the debugger:

Screenshot 2023-03-14 at 11 35 19 AM