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

Error print? #384

Open orangeC23 opened 1 year ago

orangeC23 commented 1 year ago

Describe the bug

The wat file:

(module
  (import "env" "assert_eq_i32" (func $assert_eq_i32 (param i32 i32)))
  (import "env" "print_input" (func $print_input (param i32)))

  (memory 1)
  (data 0 (offset (i32.const 0)) "abc")
  (data 0 (offset (i32.const 100)) "hello")
  (data 0 (offset (i32.const 1000)) "Good morning !")
  (data 0 (offset (i32.const 2400)) "123")

  (start $main)
  (func $main (export "main")
    (call $print_input (i32.load offset=0 (i32.const 0)))

    (call $assert_eq_i32 (i32.load offset=100 (i32.const 0)) (i32.const 0x6C6C_6568))
    (call $print_input (i32.load offset=100 (i32.const 0)))

    (call $assert_eq_i32 (i32.load offset=0 (i32.const 100)) (i32.const 0x6C6C_6568))
    (call $print_input (i32.load offset=0 (i32.const 100)))

    (call $print_input (i32.load offset=0 (i32.const 1000)))

    (call $print_input (i32.load offset=1000 (i32.const 0)))

    (call $print_input (i32.load offset=1000 (i32.const 1400)))
  )
)

The go file is:

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)
    }

    fmt.Println("Creating the imported function...")
    type MyEnvironment struct{}

    environment := MyEnvironment{}
    assert_eq := func(environment interface{}, args []wasmer.Value) ([]wasmer.Value, error) {
        if args[0]==args[1] {
            fmt.Println("equals i32 !")
        }else{
            fmt.Println("Not equals i32 !")
        }
        return []wasmer.Value{}, nil
    }
    assert_eq_i32 := wasmer.NewFunctionWithEnvironment(
        store,
        wasmer.NewFunctionType(wasmer.NewValueTypes(wasmer.I32, wasmer.I32), wasmer.NewValueTypes()),
        environment,
        assert_eq,
    )

    environment2 := MyEnvironment{}
    print_in := func(environment interface{}, args []wasmer.Value) ([]wasmer.Value, error) {
        fmt.Println("Print_input_host ", args[0])
        return []wasmer.Value{}, nil
    }
    print_input := wasmer.NewFunctionWithEnvironment(
        store,
        wasmer.NewFunctionType(wasmer.NewValueTypes(wasmer.I32), wasmer.NewValueTypes()),
        environment2,
        print_in,
    )

    // Instantiates the module
    importObject := wasmer.NewImportObject()
    importObject.Register(
        "env",
        map[string]wasmer.IntoExtern{
            "assert_eq_i32": assert_eq_i32,
            "print_input":  print_input,
        },
    )

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

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

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

    result, err := func1()

    if err != nil {
        panic(fmt.Sprintln("Failed to call the `main` function:", err))
    }

    fmt.Println(result)

}
wat2wasm tmp.wat
 go run tmp.go

Expected behavior

Print: Creating the imported function... Print_input_host 6513249 equals i32 ! Print_input_host 1819043176 equals i32 ! Print_input_host 1819043176 Print_input_host 1685024583 Print_input_host 1685024583 Print_input_host 3355185

Actual behavior

In macOS, it print:

Creating the imported function...
Print_input_host  {0x600000014300}
Not equals i32 !
Print_input_host  {0x6000000143f0}
Not equals i32 !
Print_input_host  {0x600000014400}
Print_input_host  {0x600000014410}
Print_input_host  {0x600000014420}
Print_input_host  {0x600000014430}
Print_input_host  {0x6000000144c0}
Not equals i32 !
Print_input_host  {0x6000000144d0}
Not equals i32 !
Print_input_host  {0x6000000144e0}
Print_input_host  {0x6000000144f0}
Print_input_host  {0x600000014500}
Print_input_host  {0x600000014510}
<nil>

In ubuntu 20.04, it prints:

Creating the imported function...
Print_input_host  {0x1459a80}
Not equals i32 !
Print_input_host  {0x13a5a40}
Not equals i32 !
Print_input_host  {0x13a04e0}
Print_input_host  {0x144d8f0}
Print_input_host  {0x1455a20}
Print_input_host  {0x1455a40}
Print_input_host  {0x139f8d0}
Not equals i32 !
Print_input_host  {0x139f920}
Not equals i32 !
Print_input_host  {0x139d4e0}
Print_input_host  {0x139f940}
Print_input_host  {0x139d500}
Print_input_host  {0x139d520}
<nil>

The call of host functions in wat file are executed twice, and the results are wrong.