bytecodealliance / wasmtime-go

Go WebAssembly runtime powered by Wasmtime
https://pkg.go.dev/github.com/bytecodealliance/wasmtime-go
Apache License 2.0
790 stars 79 forks source link

unknown import: `wasi::thread-spawn` has not been defined #218

Open owenthereal opened 6 months ago

owenthereal commented 6 months ago

I'm trying to run a wasi program with threads enabled, but I'm getting the following when running it with wasmtime-go:

package main

import (
    "fmt"
    "log"
    "os"
    "path/filepath"

    "github.com/bytecodealliance/wasmtime-go"
)

func main() {
    dir, err := os.MkdirTemp("", "out")
    if err != nil {
        log.Fatal(err)
    }
    defer os.RemoveAll(dir)
    stdoutPath := filepath.Join(dir, "stdout")

    f, err := os.ReadFile(os.Args[1])
    if err != nil {
        log.Fatal(err)
    }

    cfg := wasmtime.NewConfig()
    cfg.SetWasmThreads(true)

    engine := wasmtime.NewEngineWithConfig(cfg)

    module, err := wasmtime.NewModule(engine, f)
    if err != nil {
        log.Fatal(err)
    }

    linker := wasmtime.NewLinker(engine)
    err = linker.DefineWasi()
    if err != nil {
        log.Fatal(err)
    }

    wasiConfig := wasmtime.NewWasiConfig()
    wasiConfig.SetStdoutFile(stdoutPath)

    store := wasmtime.NewStore(engine)
    store.SetWasi(wasiConfig)
    instance, err := linker.Instantiate(store, module)
    if err != nil {
        log.Fatal(err)
    }

    nom := instance.GetFunc(store, "_start")
    _, err = nom.Call(store)
    if err != nil {
        log.Fatal(err)
    }

    // Print WASM stdout
    out, err := os.ReadFile(stdoutPath)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Print(string(out))
}

I was able to run it with wasmtime though: wasmtime --wasm threads --wasi threads program.wasm

alexcrichton commented 6 months ago

Thanks for the report! This is expected though in that this import is part of the wasi-threads proposal which has not been implemented in the C API (nor standardized to a large degree)