Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error print? #384

Open
orangeC23 opened this issue Apr 11, 2023 · 0 comments
Open

Error print? #384

orangeC23 opened this issue Apr 11, 2023 · 0 comments
Labels
🐞 bug Something isn't working

Comments

@orangeC23
Copy link

orangeC23 commented Apr 11, 2023

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.

@orangeC23 orangeC23 added the 🐞 bug Something isn't working label Apr 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐞 bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant