knqyf263 / go-plugin

Go Plugin System over WebAssembly
MIT License
586 stars 30 forks source link

Warning/Error with host functions #60

Closed amlwwalker closed 10 months ago

amlwwalker commented 10 months ago

Hi, I have

// The host functions embedded into the plugin
// go:plugin type=host
service HostService {
  rpc Log(LogRequest) returns (google.protobuf.Empty) {}
}
message LogRequest {
  string message = 1;
}

and on compilation with

    protoc -I pkg/plugins/interop/service --go-plugin_out=pkg/plugins/interop --go-plugin_opt=paths=source_relative pkg/plugins/interop/service/interop.proto

and generating my plugin with

    tinygo build -o pkg/plugins/examples/basic.wasm -scheduler=none -target=wasi --no-debug pkg/plugins/examples/basic.go

I then have in my plugin

func main() {
    interop.RegisterPluginService(MyPlugin{})
}

type MyPlugin struct{}

func (m MyPlugin) SendMessage(ctx context.Context, request *interop.DataMessage) (*interop.DataMessage, error) {

    hostFunctions := interop.NewHostService()
    hostFunctions.Log(ctx, &interop.LogRequest{
        Message: "Sending a log...",
    })

where SendMessage is one of the plugin functions that the host calls, I then want to call the logger on the host as you can see.

In my host I have

func (PluginFunctions) Log(ctx context.Context, request *interop.LogRequest) (*emptypb.Empty, error) {
    // Use the host logger
    log.Println("logging ", request.GetMessage())
    return &emptypb.Empty{}, nil
}

where the plugin was registered with
        if this, err := p.Load(ctx, path, PluginFunctions{}); err != nil {

now, If I remove in the plugin the `hostFunctions.Log` call everything works as expected. However when I add this back and compile the plugin I get

tinygo build -o pkg/plugins/examples/basic.wasm -scheduler=none -target=wasi --no-debug pkg/plugins/examples/basic.go tinygo:wasm-ld: warning: function signature mismatch: log

defined as (i32, i32) -> i64 in lto.tmp defined as (f64) -> f64 in /opt/homebrew/Cellar/tinygo/0.30.0/lib/wasi-libc/sysroot/lib/wasm32-wasi/libc.a(log.o)

its a warning, however when i run the host I get

2024/01/11 12:04:49 error starting plugin service wasm error: unreachable
wasm stack trace:
        .(main.MyPlugin).SendMessage(i32)
        .plugin_service_send_message(i32,i32) i64
inliquid commented 10 months ago

Try to rename Log to HostLog or something to not interfere with log from libc (this worked with older versions of tinygo because of bug, that many other packages start to rely on it at some point).

Also please take a look at related issues:

amlwwalker commented 10 months ago

nice thanks. Solved!