/
greet_host.pb.go
284 lines (243 loc) · 6.98 KB
/
greet_host.pb.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
//go:build !tinygo.wasm
// Code generated by protoc-gen-go-plugin. DO NOT EDIT.
// versions:
// protoc-gen-go-plugin v0.1.0
// protoc v4.25.2
// source: examples/host-functions/greeting/greet.proto
package greeting
import (
context "context"
errors "errors"
fmt "fmt"
wasm "github.com/khulnasoft-lab/go-plugin/wasm"
wazero "github.com/tetratelabs/wazero"
api "github.com/tetratelabs/wazero/api"
sys "github.com/tetratelabs/wazero/sys"
os "os"
)
const (
i32 = api.ValueTypeI32
i64 = api.ValueTypeI64
)
type _hostFunctions struct {
HostFunctions
}
// Instantiate a Go-defined module named "env" that exports host functions.
func (h _hostFunctions) Instantiate(ctx context.Context, r wazero.Runtime) error {
envBuilder := r.NewHostModuleBuilder("env")
envBuilder.NewFunctionBuilder().
WithGoModuleFunction(api.GoModuleFunc(h._HttpGet), []api.ValueType{i32, i32}, []api.ValueType{i64}).
WithParameterNames("offset", "size").
Export("http_get")
envBuilder.NewFunctionBuilder().
WithGoModuleFunction(api.GoModuleFunc(h._Log), []api.ValueType{i32, i32}, []api.ValueType{i64}).
WithParameterNames("offset", "size").
Export("log")
_, err := envBuilder.Instantiate(ctx)
return err
}
// Sends a HTTP GET request
func (h _hostFunctions) _HttpGet(ctx context.Context, m api.Module, stack []uint64) {
offset, size := uint32(stack[0]), uint32(stack[1])
buf, err := wasm.ReadMemory(m.Memory(), offset, size)
if err != nil {
panic(err)
}
request := new(HttpGetRequest)
err = request.UnmarshalVT(buf)
if err != nil {
panic(err)
}
resp, err := h.HttpGet(ctx, request)
if err != nil {
panic(err)
}
buf, err = resp.MarshalVT()
if err != nil {
panic(err)
}
ptr, err := wasm.WriteMemory(ctx, m, buf)
if err != nil {
panic(err)
}
ptrLen := (ptr << uint64(32)) | uint64(len(buf))
stack[0] = ptrLen
}
// Shows a log message
func (h _hostFunctions) _Log(ctx context.Context, m api.Module, stack []uint64) {
offset, size := uint32(stack[0]), uint32(stack[1])
buf, err := wasm.ReadMemory(m.Memory(), offset, size)
if err != nil {
panic(err)
}
request := new(LogRequest)
err = request.UnmarshalVT(buf)
if err != nil {
panic(err)
}
resp, err := h.Log(ctx, request)
if err != nil {
panic(err)
}
buf, err = resp.MarshalVT()
if err != nil {
panic(err)
}
ptr, err := wasm.WriteMemory(ctx, m, buf)
if err != nil {
panic(err)
}
ptrLen := (ptr << uint64(32)) | uint64(len(buf))
stack[0] = ptrLen
}
const GreeterPluginAPIVersion = 1
type GreeterPlugin struct {
newRuntime func(context.Context) (wazero.Runtime, error)
moduleConfig wazero.ModuleConfig
}
func NewGreeterPlugin(ctx context.Context, opts ...wazeroConfigOption) (*GreeterPlugin, error) {
o := &WazeroConfig{
newRuntime: DefaultWazeroRuntime(),
moduleConfig: wazero.NewModuleConfig(),
}
for _, opt := range opts {
opt(o)
}
return &GreeterPlugin{
newRuntime: o.newRuntime,
moduleConfig: o.moduleConfig,
}, nil
}
type greeter interface {
Close(ctx context.Context) error
Greeter
}
func (p *GreeterPlugin) Load(ctx context.Context, pluginPath string, hostFunctions HostFunctions) (greeter, error) {
b, err := os.ReadFile(pluginPath)
if err != nil {
return nil, err
}
// Create a new runtime so that multiple modules will not conflict
r, err := p.newRuntime(ctx)
if err != nil {
return nil, err
}
h := _hostFunctions{hostFunctions}
if err := h.Instantiate(ctx, r); err != nil {
return nil, err
}
// Compile the WebAssembly module using the default configuration.
code, err := r.CompileModule(ctx, b)
if err != nil {
return nil, err
}
// InstantiateModule runs the "_start" function, WASI's "main".
module, err := r.InstantiateModule(ctx, code, p.moduleConfig)
if err != nil {
// Note: Most compilers do not exit the module after running "_start",
// unless there was an Error. This allows you to call exported functions.
if exitErr, ok := err.(*sys.ExitError); ok && exitErr.ExitCode() != 0 {
return nil, fmt.Errorf("unexpected exit_code: %d", exitErr.ExitCode())
} else if !ok {
return nil, err
}
}
// Compare API versions with the loading plugin
apiVersion := module.ExportedFunction("greeter_api_version")
if apiVersion == nil {
return nil, errors.New("greeter_api_version is not exported")
}
results, err := apiVersion.Call(ctx)
if err != nil {
return nil, err
} else if len(results) != 1 {
return nil, errors.New("invalid greeter_api_version signature")
}
if results[0] != GreeterPluginAPIVersion {
return nil, fmt.Errorf("API version mismatch, host: %d, plugin: %d", GreeterPluginAPIVersion, results[0])
}
greet := module.ExportedFunction("greeter_greet")
if greet == nil {
return nil, errors.New("greeter_greet is not exported")
}
malloc := module.ExportedFunction("malloc")
if malloc == nil {
return nil, errors.New("malloc is not exported")
}
free := module.ExportedFunction("free")
if free == nil {
return nil, errors.New("free is not exported")
}
return &greeterPlugin{
runtime: r,
module: module,
malloc: malloc,
free: free,
greet: greet,
}, nil
}
func (p *greeterPlugin) Close(ctx context.Context) (err error) {
if r := p.runtime; r != nil {
r.Close(ctx)
}
return
}
type greeterPlugin struct {
runtime wazero.Runtime
module api.Module
malloc api.Function
free api.Function
greet api.Function
}
func (p *greeterPlugin) Greet(ctx context.Context, request *GreetRequest) (*GreetReply, error) {
data, err := request.MarshalVT()
if err != nil {
return nil, err
}
dataSize := uint64(len(data))
var dataPtr uint64
// If the input data is not empty, we must allocate the in-Wasm memory to store it, and pass to the plugin.
if dataSize != 0 {
results, err := p.malloc.Call(ctx, dataSize)
if err != nil {
return nil, err
}
dataPtr = results[0]
// This pointer is managed by TinyGo, but TinyGo is unaware of external usage.
// So, we have to free it when finished
defer p.free.Call(ctx, dataPtr)
// The pointer is a linear memory offset, which is where we write the name.
if !p.module.Memory().Write(uint32(dataPtr), data) {
return nil, fmt.Errorf("Memory.Write(%d, %d) out of range of memory size %d", dataPtr, dataSize, p.module.Memory().Size())
}
}
ptrSize, err := p.greet.Call(ctx, dataPtr, dataSize)
if err != nil {
return nil, err
}
resPtr := uint32(ptrSize[0] >> 32)
resSize := uint32(ptrSize[0])
var isErrResponse bool
if (resSize & (1 << 31)) > 0 {
isErrResponse = true
resSize &^= (1 << 31)
}
// We don't need the memory after deserialization: make sure it is freed.
if resPtr != 0 {
defer p.free.Call(ctx, uint64(resPtr))
}
// The pointer is a linear memory offset, which is where we write the name.
bytes, ok := p.module.Memory().Read(resPtr, resSize)
if !ok {
return nil, fmt.Errorf("Memory.Read(%d, %d) out of range of memory size %d",
resPtr, resSize, p.module.Memory().Size())
}
if isErrResponse {
return nil, errors.New(string(bytes))
}
response := new(GreetReply)
if err = response.UnmarshalVT(bytes); err != nil {
return nil, err
}
return response, nil
}