tetratelabs / wazero

wazero: the zero dependency WebAssembly runtime for Go developers
https://wazero.io
Apache License 2.0
4.86k stars 255 forks source link

failed to compile: invalid register [nil] #665

Closed olivierlemasle closed 2 years ago

olivierlemasle commented 2 years ago

Describe the bug Wazero fails to compile a wasm module I've compiled from Rust, which is compiled and executed successfully with Wasmtime and Wasmer.

To Reproduce wasm module: https://github.com/olivierlemasle/wasm-text2png/releases/download/v0.1.0/wasm-text2png.wasm It is a WebAssembly+WASI module compiled from Rust; its source code is in https://github.com/olivierlemasle/wasm-text2png (it simply uses the pure-Rust crate text-to-png to format a text given as parameter to PNG.) It is compiled with target wasm32-wasi using cargo-wasi.

I can compile and execute this wasm module successfully with Wasmtime and Wasmer.

My host code:

package main

import (
    "context"
    "fmt"
    "log"
    "os"

    "github.com/tetratelabs/wazero"
    "github.com/tetratelabs/wazero/wasi_snapshot_preview1"
)

func main() {
    if len(os.Args) < 2 {
        fmt.Println("Required arg: WASM file path")
        return
    }
    file := os.Args[1]

    ctx := context.Background()

    rConfig := wazero.NewRuntimeConfig().WithWasmCore2()
    r := wazero.NewRuntimeWithConfig(rConfig)
    defer r.Close(ctx)

    if _, err := wasi_snapshot_preview1.Instantiate(ctx, r); err != nil {
        log.Panicln(err)
    }

    wasm, err := os.ReadFile(file)
    if err != nil {
        log.Panicln(err)
    }

    _, err = r.CompileModule(ctx, wasm, wazero.NewCompileConfig())
    if err != nil {
        log.Panicln(err)
    }
    log.Println("Successfully compiled")
}

When executing with go run . wasm-text2png.wasm, it fails with:

panic: function[42/1870] failed to compile: invalid register [nil]

Expected behavior I would expect this module to compile, as with Wasmtime or Wasmer, or fail with a more helpful message / debugging info.

Environment (please complete the relevant information):

mathetake commented 2 years ago

Thanks, will dig into it

codefromthecrypt commented 2 years ago

ps if you want to make sure things work otherwise, switch to NewRuntimeConfigInterpreter() temporarily until the fix is in!

mathetake commented 2 years ago

OK found the root cause (I had the wrong assumption on register allocation which are both applied on amd64 and arm64). Will raise an fix shortly!

olivierlemasle commented 2 years ago

Thank you @mathetake (for the fix and for the work on this project!)

mathetake commented 2 years ago

ok here's the fix https://github.com/tetratelabs/wazero/pull/666 I will land it tomorrow (morning in Japanese time) :D