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

Interpreter: Incorrect i32.ne results for value from host function (negative numbers only) #2371

Open
AndSDev opened this issue Feb 4, 2025 · 1 comment
Labels
bug Something isn't working

Comments

@AndSDev
Copy link

AndSDev commented Feb 4, 2025

Describe the bug
Incorrect i32.ne results for value from host function (negative numbers only).

Host func result put on stack as uint64 (see internal/wasm.callGoFunc).

For -1 its 0xFFFFFFFFFFFFFFFF.
But on comaration with i32(-1) from wasm its comapres with 0x00000000FFFFFFFF.

To Reproduce

Golang code:

package main

import (
	"context"
	"encoding/base64"
	"log"

	"github.com/tetratelabs/wazero"
)

func main() {
	wasm, _ := base64.StdEncoding.DecodeString("AGFzbQEAAAABCgJgAX8Bf2AAAX8CDAEDZW52BGVjaG8AAAMCAQEHBwEDcnVuAAEKCwEJAEF/EABBf0cLABoEbmFtZQEMAgAEZWNobwEDcnVuAgUCAAABAA==")

	config := wazero.NewRuntimeConfigInterpreter()
	ctx := context.Background()
	r := wazero.NewRuntimeWithConfig(ctx, config)

	_, _ = r.NewHostModuleBuilder("env").
		NewFunctionBuilder().
		WithFunc(func(v int32) int32 {
			return v
		}).Export("echo").
		Instantiate(ctx)

	mod, _ := r.Instantiate(ctx, wasm)
	run := mod.ExportedFunction("run")
	result, err := run.Call(ctx)
	if err != nil {
		log.Fatalf("failed to call run: %v", err)
	}

	if result[0] == 0 {
		println("OK: -1 != -1 is false")
	} else {
		panic("FAIL, -1 != -1 is true")
	}
}

Wat code for WASM

(module
  (import "env" "echo" (func $echo (param i32) (result i32)))
  (func $run (result i32)
    i32.const -1
    call $echo
    i32.const -1
    i32.ne
  )
  (export "run" (func $run))
)

Expected behavior
After run, the message "OK: -1 != -1 is false" should be displayed, but an error occurs.

Screenshots

Screenshot from debugger

Image

Environment (please complete the relevant information):

  • Go version: go1.23.5 linux/amd6
  • wazero Version: v1.8.2
  • Host architecture: amd64
  • Runtime mode: interpreter

Additional context
In compiler mode its work fine.

@mathetake
Copy link
Member

mathetake commented Feb 4, 2025

This is an expected behavior as explicitly documented in

wazero/api/wasm.go

Lines 383 to 384 in 4231c48

// To safely encode/decode params/results expressed as uint64, users are encouraged to
// use api.EncodeXXX or DecodeXXX functions. See the docs on api.ValueType.

wazero/api/wasm.go

Lines 451 to 452 in 4231c48

// To safely decode/encode values from/to the uint64 stack, users are encouraged to use
// api.EncodeXXX or api.DecodeXXX functions. See the docs on api.ValueType.

@mathetake mathetake closed this as not planned Won't fix, can't repro, duplicate, stale Feb 4, 2025
@tetratelabs tetratelabs locked as resolved and limited conversation to collaborators Feb 4, 2025
@mathetake mathetake reopened this Feb 19, 2025
@mathetake mathetake reopened this Feb 19, 2025
@tetratelabs tetratelabs unlocked this conversation Feb 19, 2025
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

Successfully merging a pull request may close this issue.

2 participants