wasmtime-go icon indicating copy to clipboard operation
wasmtime-go copied to clipboard

unknown import: `wasi_snapshot_preview1::fd_write` has not been defined

Open rayowang opened this issue 2 years ago • 4 comments

I got a strange error when using libwasmtime.dylib on macos: 2022-09-12 23:25:39,764 [ERROR] [wasmtime][instance] Start fail to new wasmtimego instance, err: unknown import: wasi_snapshot_preview1::fd_write has not been defined

rayowang avatar Sep 12 '22 15:09 rayowang

The wasm module code:

package main

import (
	"errors"
	"strings"

	"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm"
	"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm/types"
)

func main() {
	proxywasm.SetVMContext(&vmContext{})
}

type vmContext struct {
	// Embed the default VM context here,
	// so that we don't need to reimplement all the methods.
	types.DefaultVMContext
}

// Override types.DefaultVMContext.
func (*vmContext) NewPluginContext(contextID uint32) types.PluginContext {
	return &pluginContext{}
}

type pluginContext struct {
	// Embed the default plugin context here,
	// so that we don't need to reimplement all the methods.
	types.DefaultPluginContext
}

// Override types.DefaultPluginContext.
func (*pluginContext) NewHttpContext(contextID uint32) types.HttpContext {
	return &httpHeaders{contextID: contextID}
}

type httpHeaders struct {
	// Embed the default http context here,
	// so that we don't need to reimplement all the methods.
	types.DefaultHttpContext
	contextID uint32
}

// Override types.DefaultHttpContext.
func (ctx *httpHeaders) OnHttpRequestBody(bodySize int, endOfStream bool) types.Action {
	//1. get request body
	body, err := proxywasm.GetHttpRequestBody(0, bodySize)
	if err != nil {
		proxywasm.LogErrorf("GetHttpRequestBody failed: %v", err)
		return types.ActionPause
	}

	//2. parse request param
	bookName, err := getQueryParam(string(body), "name")
	if err != nil {
		proxywasm.LogErrorf("param not found: %v", err)
		return types.ActionPause
	}

	//3. request function2 through ABI
	inventories, err := proxywasm.InvokeService("id_2", "", bookName)
	if err != nil {
		proxywasm.LogErrorf("invoke service failed: %v", err)
		return types.ActionPause
	}

	//4. return result
	proxywasm.AppendHttpResponseBody([]byte("There are " + inventories + " inventories for " + bookName + "."))
	return types.ActionContinue
}

func getQueryParam(body string, paramName string) (string, error) {
	kvs := strings.Split(body, "&")
	for _, kv := range kvs {
		param := strings.Split(kv, "=")
		if param[0] == paramName {
			return param[1], nil
		}
	}
	return "", errors.New("not found")
}

// Override types.DefaultHttpContext.
func (ctx *httpHeaders) OnHttpStreamDone() {
	proxywasm.LogInfof("%d finished", ctx.contextID)
}

const ID = "id_1"

// DO NOT MODIFY THE FOLLOWING FUNCTIONS!
//export proxy_get_id
func GetID() {
	_ = ID[len(ID)-1]
	proxywasm.SetCallData([]byte(ID))
`

rayowang avatar Sep 12 '22 15:09 rayowang

Thanks for the report, but this looks like a pretty deep error within the system. Can you explain more why you think the bug is within this module's bindings and not elsewhere?

alexcrichton avatar Sep 12 '22 19:09 alexcrichton

Thanks for the report, but this looks like a pretty deep error within the system. Can you explain more why you think the bug is within this module's bindings and not elsewhere?

I checked some information, such as this issue https://github.com/bytecodealliance/wasmtime-go/issues/55, is this issue #149 also related to the rust command for compiling dynamic libraries?

rayowang avatar Sep 13 '22 02:09 rayowang

Perhaps? I think that's related to the toolchain rather than this module, then.

alexcrichton avatar Sep 13 '22 14:09 alexcrichton