Skip to content
This repository has been archived by the owner on May 11, 2020. It is now read-only.

wasm: functions in FunctionIndexSpace include wrong contents #190

Open
hajimehoshi opened this issue Feb 27, 2020 · 2 comments
Open

wasm: functions in FunctionIndexSpace include wrong contents #190

hajimehoshi opened this issue Feb 27, 2020 · 2 comments

Comments

@hajimehoshi
Copy link
Contributor

  1. Build a hello-world wasm:
package main

func main() {
        println("Hello, World!")
}
GOOS=js GOARCH=wasm go build -o test.wasm helloworld.go 
  1. Run the below code:
package main

import (
        "fmt"
        "os"

        "github.com/go-interpreter/wagon/wasm"
)

func main() {
        f, err := os.Open("test.wasm")
        if err != nil {
                panic(err)
        }
        defer f.Close()

        mod, err := wasm.ReadModule(f, nil)
        if err != nil {
                panic(err)
        }

        fn := mod.FunctionIndexSpace[0]
        fmt.Printf("name: %q\n", fn.Name) // This is empty. That's fine since this is an imported function 'debug'.                                                                                                                           
        fmt.Printf("len(code): %d\n", len(fn.Body.Code)) // This is not empty. That's unexpected. This content is for the first function as 'go.buildid'.                                                                                     
}

It looks like the function's index and its instructions don't match. Some of the first functions should be imported functions, but include instructions for other functions. I think the function bodies are shifted.

@hajimehoshi
Copy link
Contributor Author

hajimehoshi commented Feb 27, 2020

wagon/wasm/index.go

Lines 76 to 80 in cbbdf08

fn := Function{
Sig: &m.Types.Entries[typeIndex],
Body: &m.Code.Bodies[codeIndex],
Name: names[uint32(codeIndex+numImports)], // Add the name string if we have it
}

The names are shifted with numImports, which is correct, while Sig and Body are not shifted, maybe? I'm not confident.

@zhangzhiqiangcs
Copy link
Contributor

Hello, I read in detail, found that the problem comes with numImports, when you don't provide a imported module but the wasm file need it, the numImports is not equal to len(FunctionIndexSpace).

wagon/wasm/index.go

Lines 84 to 85 in dc6758c

// Add the functions from the wasm itself to the function list
numImports := len(m.FunctionIndexSpace)

So I had a pr #193
I am not sure if we can do it in this way.
Thanks.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants