forked from tetratelabs/proxy-wasm-go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathwasmwrapper.go
736 lines (689 loc) · 32.2 KB
/
wasmwrapper.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
// Copyright 2020-2022 Tetrate
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package proxytest
import (
"context"
"io"
"os"
"unsafe"
"github.com/proxy-wasm/proxy-wasm-go-sdk/proxywasm/internal"
"github.com/proxy-wasm/proxy-wasm-go-sdk/proxywasm/types"
"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/api"
"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
)
type guestABI struct {
proxyOnVMStart api.Function
proxyOnContextCreate api.Function
proxyOnConfigure api.Function
proxyOnDone api.Function
proxyOnQueueReady api.Function
proxyOnTick api.Function
proxyOnRequestHeaders api.Function
proxyOnRequestBody api.Function
proxyOnRequestTrailers api.Function
proxyOnResponseHeaders api.Function
proxyOnResponseBody api.Function
proxyOnResponseTrailers api.Function
proxyOnLog api.Function
}
// WasmVMContext is a VMContext that delegates execution to a compiled wasm binary.
type WasmVMContext interface {
types.VMContext
io.Closer
}
// vmContext implements WasmVMContext.
type vmContext struct {
runtime wazero.Runtime
abi guestABI
ctx context.Context
}
// NewWasmVMContext returns a types.VMContext that delegates plugin invocations to the provided compiled wasm binary.
// proxytest can be run with a compiled wasm binary by passing this to proxytest.WithVMContext.
//
// Running proxytest with the compiled wasm binary helps to ensure that the plugin will run when actually compiled with
// TinyGo, however stack traces and other debug features will be much worse. It is recommended to run unit tests both
// with Go and with wasm. Tests will run much faster under Go for quicker development cycles, and the wasm runner can
// confirm the behavior matches when actually compiled.
//
// For example, this snippet allows determining the types.VMContext based on a test case flag.
//
// var vm types.VMContext
// switch runner {
// case "go":
// vm = &vmContext{}
// case "wasm":
// wasm, err := os.ReadFile("plugin.wasm")
// if err != nil {
// t.Skip("wasm not found")
// }
// v, err := proxytest.NewWasmVMContext(wasm)
// require.NoError(t, err)
// vm = v
// }
//
// Note: Currently only HTTP plugins are supported.
func NewWasmVMContext(wasm []byte) (WasmVMContext, error) {
ctx := context.Background()
r := wazero.NewRuntime(ctx)
_, err := wasi_snapshot_preview1.Instantiate(ctx, r)
if err != nil {
return nil, err
}
err = exportHostABI(ctx, r)
if err != nil {
return nil, err
}
compiled, err := r.CompileModule(ctx, wasm)
if err != nil {
return nil, err
}
wazeroconfig := wazero.NewModuleConfig().
WithStartFunctions("_initialize", "_start", "main").
WithStdout(os.Stderr).
WithStderr(os.Stderr)
mod, err := r.InstantiateModule(ctx, compiled, wazeroconfig)
if err != nil {
return nil, err
}
abi := guestABI{
proxyOnVMStart: mod.ExportedFunction("proxy_on_vm_start"),
proxyOnContextCreate: mod.ExportedFunction("proxy_on_context_create"),
proxyOnConfigure: mod.ExportedFunction("proxy_on_configure"),
proxyOnDone: mod.ExportedFunction("proxy_on_done"),
proxyOnQueueReady: mod.ExportedFunction("proxy_on_queue_ready"),
proxyOnTick: mod.ExportedFunction("proxy_on_tick"),
proxyOnRequestHeaders: mod.ExportedFunction("proxy_on_request_headers"),
proxyOnRequestBody: mod.ExportedFunction("proxy_on_request_body"),
proxyOnRequestTrailers: mod.ExportedFunction("proxy_on_request_trailers"),
proxyOnResponseHeaders: mod.ExportedFunction("proxy_on_response_headers"),
proxyOnResponseBody: mod.ExportedFunction("proxy_on_response_body"),
proxyOnResponseTrailers: mod.ExportedFunction("proxy_on_response_trailers"),
proxyOnLog: mod.ExportedFunction("proxy_on_log"),
}
return &vmContext{
runtime: r,
abi: abi,
ctx: ctx,
}, nil
}
// OnVMStart implements the same method on types.VMContext.
func (v *vmContext) OnVMStart(vmConfigurationSize int) types.OnVMStartStatus {
rootContextID := uint64(0) // unused
res, err := v.abi.proxyOnVMStart.Call(v.ctx, rootContextID, uint64(vmConfigurationSize))
handleErr(err)
return res[0] == 1
}
// NewPluginContext implements the same method on types.VMContext.
func (v *vmContext) NewPluginContext(contextID uint32) types.PluginContext {
_, err := v.abi.proxyOnContextCreate.Call(v.ctx, uint64(contextID), 0)
handleErr(err)
return &pluginContext{
id: uint64(contextID),
abi: v.abi,
ctx: withPluginContextID(v.ctx, contextID),
}
}
// Close implements the same method on io.Closer.
func (v *vmContext) Close() error {
return v.runtime.Close(v.ctx)
}
// pluginContext implements types.PluginContext.
type pluginContext struct {
id uint64
abi guestABI
ctx context.Context
}
// OnPluginStart implements the same method on types.PluginContext.
func (p *pluginContext) OnPluginStart(pluginConfigurationSize int) types.OnPluginStartStatus {
res, err := p.abi.proxyOnConfigure.Call(p.ctx, p.id, uint64(pluginConfigurationSize))
handleErr(err)
return res[0] == 1
}
// OnPluginDone implements the same method on types.PluginContext.
func (p *pluginContext) OnPluginDone() bool {
res, err := p.abi.proxyOnDone.Call(p.ctx, p.id)
handleErr(err)
return res[0] == 1
}
// OnQueueReady implements the same method on types.PluginContext.
func (p *pluginContext) OnQueueReady(queueID uint32) {
_, err := p.abi.proxyOnQueueReady.Call(p.ctx, p.id, uint64(queueID))
handleErr(err)
}
// OnTick implements the same method on types.PluginContext.
func (p *pluginContext) OnTick() {
_, err := p.abi.proxyOnTick.Call(p.ctx, p.id)
handleErr(err)
}
// NewTcpContext implements the same method on types.PluginContext.
func (p *pluginContext) NewTcpContext(uint32) types.TcpContext {
return nil
}
// NewHttpContext implements the same method on types.PluginContext.
func (p *pluginContext) NewHttpContext(contextID uint32) types.HttpContext {
_, err := p.abi.proxyOnContextCreate.Call(p.ctx, uint64(contextID), p.id)
handleErr(err)
return &httpContext{
id: uint64(contextID),
abi: p.abi,
ctx: p.ctx,
}
}
// httpContext implements types.HttpContext.
type httpContext struct {
id uint64
abi guestABI
ctx context.Context
}
// OnHttpRequestHeaders implements the same method on types.HttpContext.
func (h *httpContext) OnHttpRequestHeaders(numHeaders int, endOfStream bool) types.Action {
res, err := h.abi.proxyOnRequestHeaders.Call(h.ctx, h.id, uint64(numHeaders), wasmBool(endOfStream))
handleErr(err)
return types.Action(res[0])
}
// OnHttpRequestBody implements the same method on types.HttpContext.
func (h *httpContext) OnHttpRequestBody(bodySize int, endOfStream bool) types.Action {
res, err := h.abi.proxyOnRequestBody.Call(h.ctx, h.id, uint64(bodySize), wasmBool(endOfStream))
handleErr(err)
return types.Action(res[0])
}
// OnHttpRequestTrailers implements the same method on types.HttpContext.
func (h *httpContext) OnHttpRequestTrailers(numTrailers int) types.Action {
res, err := h.abi.proxyOnRequestTrailers.Call(h.ctx, h.id, uint64(numTrailers))
handleErr(err)
return types.Action(res[0])
}
// OnHttpResponseHeaders implements the same method on types.HttpContext.
func (h *httpContext) OnHttpResponseHeaders(numHeaders int, endOfStream bool) types.Action {
res, err := h.abi.proxyOnResponseHeaders.Call(h.ctx, h.id, uint64(numHeaders), wasmBool(endOfStream))
handleErr(err)
return types.Action(res[0])
}
// OnHttpResponseBody implements the same method on types.HttpContext.
func (h *httpContext) OnHttpResponseBody(bodySize int, endOfStream bool) types.Action {
res, err := h.abi.proxyOnResponseBody.Call(h.ctx, h.id, uint64(bodySize), wasmBool(endOfStream))
handleErr(err)
return types.Action(res[0])
}
// OnHttpResponseTrailers implements the same method on types.HttpContext.
func (h *httpContext) OnHttpResponseTrailers(numTrailers int) types.Action {
res, err := h.abi.proxyOnResponseTrailers.Call(h.ctx, h.id, uint64(numTrailers))
handleErr(err)
return types.Action(res[0])
}
// OnHttpStreamDone implements the same method on types.HttpContext.
func (h *httpContext) OnHttpStreamDone() {
_, err := h.abi.proxyOnLog.Call(h.ctx, h.id)
handleErr(err)
}
func handleErr(err error) {
if err != nil {
panic(err)
}
}
func handleMemoryStatus(ok bool) {
if !ok {
panic("could not access memory")
}
}
func wasmBytePtr(mod api.Module, off uint32, size uint32) *byte {
if size == 0 {
return nil
}
buf, ok := mod.Memory().Read(off, size)
handleMemoryStatus(ok)
return &buf[0]
}
func copyBytesToWasm(ctx context.Context, mod api.Module, hostPtr *byte, size int32, wasmPtrPtr uint32, wasmSizePtr uint32) {
if size == 0 {
return
}
hostSlice := unsafe.Slice(hostPtr, size)
alloc := mod.ExportedFunction("proxy_on_memory_allocate")
res, err := alloc.Call(ctx, uint64(size))
handleErr(err)
buf, ok := mod.Memory().Read(uint32(res[0]), uint32(size))
handleMemoryStatus(ok)
copy(buf, hostSlice)
ok = mod.Memory().WriteUint32Le(wasmPtrPtr, uint32(res[0]))
handleMemoryStatus(ok)
ok = mod.Memory().WriteUint32Le(wasmSizePtr, uint32(size))
handleMemoryStatus(ok)
}
func wasmBool(b bool) uint64 {
if b {
return 1
}
return 0
}
func exportHostABI(ctx context.Context, r wazero.Runtime) error {
_, err := r.NewHostModuleBuilder("env").
// proxy_log logs a message at the given log_level.
// See https://github.com/proxy-wasm/spec/tree/master/abi-versions/vNEXT#proxy_log
NewFunctionBuilder().
WithParameterNames("log_level", "message_data", "message_size").
WithResultNames("call_result").
WithFunc(func(ctx context.Context, mod api.Module, logLevel, messageData, messageSize uint32) uint32 {
messageDataPtr := wasmBytePtr(mod, messageData, messageSize)
return uint32(internal.ProxyLog(internal.LogLevel(logLevel), messageDataPtr, int32(messageSize)))
}).
Export("proxy_log").
// proxy_set_property sets a property value.
// See https://github.com/proxy-wasm/spec/tree/master/abi-versions/vNEXT#proxy_set_property
NewFunctionBuilder().
WithParameterNames("property_path_data", "property_path_size", "property_value_data",
"property_value_size").
WithResultNames("call_result").
WithFunc(func(ctx context.Context, mod api.Module, pathData, pathSize, valueData, valueSize uint32) uint32 {
pathDataPtr := wasmBytePtr(mod, pathData, pathSize)
valueDataPtr := wasmBytePtr(mod, valueData, valueSize)
return uint32(internal.ProxySetProperty(pathDataPtr, int32(pathSize), valueDataPtr, int32(valueSize)))
}).
Export("proxy_set_property").
// proxy_get_property gets a property value.
// See https://github.com/proxy-wasm/spec/tree/master/abi-versions/vNEXT#proxy_get_property
NewFunctionBuilder().
WithParameterNames("property_path_data", "property_path_size", "return_property_value_data",
"return_property_value_size").
WithResultNames("call_result").
WithFunc(func(ctx context.Context, mod api.Module, pathData, pathSize, returnValueData,
returnValueSize uint32) uint32 {
pathDataPtr := wasmBytePtr(mod, pathData, pathSize)
var returnValueHostPtr *byte
var returnValueSizePtr int32
ret := uint32(internal.ProxyGetProperty(pathDataPtr, int32(pathSize), unsafe.Pointer(&returnValueHostPtr), &returnValueSizePtr))
copyBytesToWasm(ctx, mod, returnValueHostPtr, returnValueSizePtr, returnValueData, returnValueSize)
return ret
}).
Export("proxy_get_property").
// proxy_send_local_response sends an HTTP response without forwarding request to the upstream.
//
// Note: proxy-wasm spec calls this proxy_send_http_response. See
// https://github.com/proxy-wasm/spec/tree/master/abi-versions/vNEXT#proxy_send_http_response
NewFunctionBuilder().
WithParameterNames("response_code", "response_code_details_data", "response_code_details_size",
"response_body_data", "response_body_size", "additional_headers_map_data", "additional_headers_size",
"grpc_status").
WithResultNames("call_result").
WithFunc(func(ctx context.Context, mod api.Module, statusCode, statusCodeDetailData, statusCodeDetailsSize,
bodyData, bodySize, headersData, headersSize, grpcStatus uint32) uint32 {
statusCodeDetailDataPtr := wasmBytePtr(mod, statusCodeDetailData, statusCodeDetailsSize)
bodyDataPtr := wasmBytePtr(mod, bodyData, bodySize)
headersDataPtr := wasmBytePtr(mod, headersData, headersSize)
return uint32(internal.ProxySendLocalResponse(statusCode, statusCodeDetailDataPtr,
int32(statusCodeDetailsSize), bodyDataPtr, int32(bodySize), headersDataPtr, int32(headersSize), int32(grpcStatus)))
}).
Export("proxy_send_local_response").
// proxy_get_shared_data gets shared data identified by a key. The compare-and-switch value is returned and can
// be used when updating the value with proxy_set_shared_data.
// See https://github.com/proxy-wasm/spec/tree/master/abi-versions/vNEXT#proxy_get_shared_data
NewFunctionBuilder().
WithParameterNames("key_data", "key_size", "return_value_data", "return_value_size", "return_cas").
WithResultNames("call_result").
WithFunc(func(ctx context.Context, mod api.Module, keyData, keySize, returnValueData, returnValueSize,
returnCas uint32) uint32 {
keyDataPtr := wasmBytePtr(mod, keyData, keySize)
var returnValueHostPtr *byte
var returnValueSizePtr int32
var returnCasPtr uint32
ret := uint32(internal.ProxyGetSharedData(keyDataPtr, int32(keySize), unsafe.Pointer(&returnValueHostPtr),
&returnValueSizePtr, &returnCasPtr))
copyBytesToWasm(ctx, mod, returnValueHostPtr, returnValueSizePtr, returnValueData, returnValueSize)
handleMemoryStatus(mod.Memory().WriteUint32Le(returnCas, returnCasPtr))
return ret
}).
Export("proxy_get_shared_data").
// proxy_set_shared_data sets the value of shared data using its key. If compare-and-switch value is set, it
// must match the current value.
// See https://github.com/proxy-wasm/spec/tree/master/abi-versions/vNEXT#proxy_set_shared_data
NewFunctionBuilder().
WithParameterNames("key_data", "key_size", "value_data", "value_size", "cas").
WithResultNames("call_result").
WithFunc(func(ctx context.Context, mod api.Module, keyData, keySize, valueData, valueSize, cas uint32) uint32 {
keyDataPtr := wasmBytePtr(mod, keyData, keySize)
valueDataPtr := wasmBytePtr(mod, valueData, valueSize)
return uint32(internal.ProxySetSharedData(keyDataPtr, int32(keySize), valueDataPtr, int32(valueSize), cas))
}).
Export("proxy_set_shared_data").
// proxy_register_shared_queue registers a shared queue using a given name. It can be referred to in
// proxy_enqueue_shared_queue and proxy_dequeue_shared_queue using the returned ID.
// See https://github.com/proxy-wasm/spec/tree/master/abi-versions/vNEXT#proxy_register_shared_queue
NewFunctionBuilder().
WithParameterNames("queue_name_data", "queue_name_size", "return_queue_id").
WithResultNames("call_result").
WithFunc(func(ctx context.Context, mod api.Module, nameData, nameSize, returnID uint32) uint32 {
namePtr := wasmBytePtr(mod, nameData, nameSize)
var returnIDPtr uint32
ret := uint32(internal.ProxyRegisterSharedQueue(namePtr, int32(nameSize), &returnIDPtr))
handleMemoryStatus(mod.Memory().WriteUint32Le(returnID, returnIDPtr))
return ret
}).
Export("proxy_register_shared_queue").
// proxy_resolve_shared_queue resolves existing shared queue using a given name. It can be referred to in
// proxy_enqueue_shared_queue and proxy_dequeue_shared_queue using the returned ID.
// See https://github.com/proxy-wasm/spec/tree/master/abi-versions/vNEXT#proxy_resolve_shared_queue
//
// Note: The "vm_id_data" and "vm_id_size" parameters are not documented in proxy-wasm spec.
NewFunctionBuilder().
WithParameterNames("vm_id_data", "vm_id_size", "queue_name_data", "queue_name_size", "return_queue_id").
WithResultNames("call_result").
WithFunc(func(ctx context.Context, mod api.Module, vmIDData, vmIDSize, nameData, nameSize, returnID uint32) uint32 {
vmID := wasmBytePtr(mod, vmIDData, vmIDSize)
namePtr := wasmBytePtr(mod, nameData, nameSize)
var returnIDPtr uint32
ret := uint32(internal.ProxyResolveSharedQueue(vmID, int32(vmIDSize), namePtr, int32(nameSize), &returnIDPtr))
handleMemoryStatus(mod.Memory().WriteUint32Le(returnID, returnIDPtr))
return ret
}).
Export("proxy_resolve_shared_queue").
// proxy_dequeue_shared_queue gets data from the end of the queue.
// See https://github.com/proxy-wasm/spec/tree/master/abi-versions/vNEXT#proxy_dequeue_shared_queue
NewFunctionBuilder().
WithParameterNames("queue_id", "payload_data", "payload_size").
WithResultNames("call_result").
WithFunc(func(ctx context.Context, mod api.Module, queueID, returnValueData, returnValueSize uint32) uint32 {
var returnValueHostPtr *byte
var returnValueSizePtr int32
ret := uint32(internal.ProxyDequeueSharedQueue(queueID, unsafe.Pointer(&returnValueHostPtr), &returnValueSizePtr))
copyBytesToWasm(ctx, mod, returnValueHostPtr, returnValueSizePtr, returnValueData, returnValueSize)
return ret
}).
Export("proxy_dequeue_shared_queue").
// proxy_enqueue_shared_queue adds data to the front of the queue.
// See https://github.com/proxy-wasm/spec/tree/master/abi-versions/vNEXT#proxy_enqueue_shared_queue
NewFunctionBuilder().
WithParameterNames("queue_id", "payload_data", "payload_size").
WithResultNames("call_result").
WithFunc(func(ctx context.Context, mod api.Module, queueID, valueData, valueSize uint32) uint32 {
valuePtr := wasmBytePtr(mod, valueData, valueSize)
return uint32(internal.ProxyEnqueueSharedQueue(queueID, valuePtr, int32(valueSize)))
}).
Export("proxy_enqueue_shared_queue").
// proxy_get_header_map_value gets the content of key from a given map.
//
// Note: proxy-wasm-spec calls this proxy_get_map_value. See
// https://github.com/proxy-wasm/spec/tree/master/abi-versions/vNEXT#proxy_get_map_value
NewFunctionBuilder().
WithParameterNames("map_type", "key_data", "key_size", "return_value_data", "return_value_size").
WithResultNames("call_result").
WithFunc(func(ctx context.Context, mod api.Module, mapType, keyData, keySize, returnValueData,
returnValueSize uint32) uint32 {
keyPtr := wasmBytePtr(mod, keyData, keySize)
var retValDataHostPtr *byte
var retValSizePtr int32
ret := uint32(internal.ProxyGetHeaderMapValue(internal.MapType(mapType), keyPtr, int32(keySize), unsafe.Pointer(&retValDataHostPtr), &retValSizePtr))
copyBytesToWasm(ctx, mod, retValDataHostPtr, retValSizePtr, returnValueData, returnValueSize)
return ret
}).
Export("proxy_get_header_map_value").
// proxy_add_header_map_value adds a value to the key of a given map.
//
// Note: proxy-wasm-spec calls this proxy_add_map_value. See
// https://github.com/proxy-wasm/spec/tree/master/abi-versions/vNEXT#proxy_add_map_value
NewFunctionBuilder().
WithParameterNames("map_type", "key_data", "key_size", "value_data", "value_size").
WithResultNames("call_result").
WithFunc(func(ctx context.Context, mod api.Module, mapType, keyData, keySize, valueData, valueSize uint32) uint32 {
keyPtr := wasmBytePtr(mod, keyData, keySize)
valuePtr := wasmBytePtr(mod, valueData, valueSize)
return uint32(internal.ProxyAddHeaderMapValue(internal.MapType(mapType), keyPtr, int32(keySize), valuePtr, int32(valueSize)))
}).
Export("proxy_add_header_map_value").
// proxy_replace_header_map_value replaces any value of the key in a given map.
//
// Note: proxy-wasm-spec calls this proxy_set_map_value. See
// https://github.com/proxy-wasm/spec/tree/master/abi-versions/vNEXT#proxy_set_map_value
NewFunctionBuilder().
WithParameterNames("map_type", "key_data", "key_size", "value_data", "value_size").
WithResultNames("call_result").
WithFunc(func(ctx context.Context, mod api.Module, mapType, keyData, keySize, valueData, valueSize uint32) uint32 {
keyPtr := wasmBytePtr(mod, keyData, keySize)
valuePtr := wasmBytePtr(mod, valueData, valueSize)
return uint32(internal.ProxyReplaceHeaderMapValue(internal.MapType(mapType), keyPtr, int32(keySize), valuePtr, int32(valueSize)))
}).
Export("proxy_replace_header_map_value").
// proxy_continue_stream resume processing of paused stream.
//
// Note: This is similar to proxy_resume_downstream, proxy_resume_upstream, proxy_resume_http_request and
// proxy_resume_http_response in proxy-wasm spec. See
// https://github.com/proxy-wasm/spec/tree/master/abi-versions/vNEXT#proxy_continue_stream
NewFunctionBuilder().
WithParameterNames("stream_type").
WithResultNames("call_result").
WithFunc(func(streamType uint32) uint32 {
return uint32(internal.ProxyContinueStream(internal.StreamType(streamType)))
}).
Export("proxy_continue_stream").
// proxy_close_stream closes a stream.
//
// Note: This is undocumented in proxy-wasm spec.
NewFunctionBuilder().
WithParameterNames("stream_type").
WithResultNames("call_result").
WithFunc(func(streamType uint32) uint32 {
return uint32(internal.ProxyCloseStream(internal.StreamType(streamType)))
}).
Export("proxy_close_stream").
// proxy_remove_header_map_value removes all values of the key in a given map.
//
// Note: proxy-wasm-spec calls this proxy_remove_map_value. See
// https://github.com/proxy-wasm/spec/tree/master/abi-versions/vNEXT#proxy_remove_map_value
NewFunctionBuilder().
WithParameterNames("map_type", "key_data", "key_size").
WithResultNames("call_result").
WithFunc(func(ctx context.Context, mod api.Module, mapType, keyData, keySize uint32) uint32 {
keyPtr := wasmBytePtr(mod, keyData, keySize)
return uint32(internal.ProxyRemoveHeaderMapValue(internal.MapType(mapType), keyPtr, int32(keySize)))
}).
Export("proxy_remove_header_map_value").
// proxy_get_header_map_pairs gets all key-value pairs from a given map.
//
// Note: proxy-wasm-spec calls this proxy_get_map. See
// https://github.com/proxy-wasm/spec/tree/master/abi-versions/vNEXT#proxy_get_map
NewFunctionBuilder().
WithParameterNames("map_type", "return_map_data", "return_map_size").
WithResultNames("call_result").
WithFunc(func(ctx context.Context, mod api.Module, mapType, returnValueData, returnValueSize uint32) uint32 {
var returnValueHostPtr *byte
var returnValueSizePtr int32
ret := uint32(internal.ProxyGetHeaderMapPairs(internal.MapType(mapType), unsafe.Pointer(&returnValueHostPtr), &returnValueSizePtr))
copyBytesToWasm(ctx, mod, returnValueHostPtr, returnValueSizePtr, returnValueData, returnValueSize)
return ret
}).
Export("proxy_get_header_map_pairs").
// proxy_set_header_map_pairs gets all key-value pairs from a given map.
//
// Note: proxy-wasm-spec calls this proxy_set_map. See
// https://github.com/proxy-wasm/spec/tree/master/abi-versions/vNEXT#proxy_set_map
NewFunctionBuilder().
WithParameterNames("map_type", "map_data", "map_size").
WithResultNames("call_result").
WithFunc(func(ctx context.Context, mod api.Module, mapType, mapData, mapSize uint32) uint32 {
mapPtr := wasmBytePtr(mod, mapData, mapSize)
return uint32(internal.ProxySetHeaderMapPairs(internal.MapType(mapType), mapPtr, int32(mapSize)))
}).
Export("proxy_set_header_map_pairs").
// proxy_get_buffer_bytes gets up to max_size bytes from the buffer, starting from offset.
//
// Note: proxy-wasm-spec calls this proxy_get_buffer, but the signature is incompatible. See
// https://github.com/proxy-wasm/spec/tree/master/abi-versions/vNEXT#proxy_get_buffer
NewFunctionBuilder().
WithParameterNames("buffer_type", "offset", "max_size", "return_buffer_data", "return_buffer_size").
WithResultNames("call_result").
WithFunc(func(ctx context.Context, mod api.Module, bufferType, start, maxSize, returnBufferData,
returnBufferSize uint32) uint32 {
var returnBufferDataHostPtr *byte
var returnBufferSizePtr int32
ret := uint32(internal.ProxyGetBufferBytes(internal.BufferType(bufferType), int32(start), int32(maxSize), unsafe.Pointer(&returnBufferDataHostPtr), &returnBufferSizePtr))
copyBytesToWasm(ctx, mod, returnBufferDataHostPtr, returnBufferSizePtr, returnBufferData, returnBufferSize)
return ret
}).
Export("proxy_get_buffer_bytes").
// proxy_set_buffer_bytes replaces a byte range of the given buffer type.
//
// Note: proxy-wasm-spec calls this proxy_set_buffer, but the signature is incompatible. See
// https://github.com/proxy-wasm/spec/tree/master/abi-versions/vNEXT#proxy_set_buffer
NewFunctionBuilder().
WithParameterNames("buffer_type", "offset", "size", "buffer_data", "buffer_size").
WithResultNames("call_result").
WithFunc(func(ctx context.Context, mod api.Module, bufferType, start, maxSize, bufferData,
bufferSize uint32) uint32 {
bufferPtr := wasmBytePtr(mod, bufferData, bufferSize)
return uint32(internal.ProxySetBufferBytes(internal.BufferType(bufferType), int32(start), int32(maxSize), bufferPtr, int32(bufferSize)))
}).
Export("proxy_set_buffer_bytes").
// proxy_http_call dispatches an HTTP call to upstream. Once the response is returned to the host,
// proxy_on_http_call_response will be called with a unique call identifier (return_callout_id).
//
// Note: proxy-wasm-spec calls this proxy_dispatch_http_call. See
// https://github.com/proxy-wasm/spec/tree/master/abi-versions/vNEXT#proxy_dispatch_http_call
NewFunctionBuilder().
WithParameterNames("upstream_name_data", "upstream_name_size", "headers_map_data", "headers_map_size",
"body_data", "body_size", "trailers_map_data", "trailers_map_size", "timeout_milliseconds",
"return_callout_id").
WithResultNames("call_result").
WithFunc(func(ctx context.Context, mod api.Module, upstreamData, upstreamSize, headerData, headerSize, bodyData,
bodySize, trailersData, trailersSize, timeout, calloutIDPtr uint32) uint32 {
upstreamPtr := wasmBytePtr(mod, upstreamData, upstreamSize)
headerPtr := wasmBytePtr(mod, headerData, headerSize)
bodyPtr := wasmBytePtr(mod, bodyData, bodySize)
trailersPtr := wasmBytePtr(mod, trailersData, trailersSize)
var calloutID uint32
ret := uint32(internal.ProxyHttpCall(upstreamPtr, int32(upstreamSize), headerPtr, int32(headerSize), bodyPtr, int32(bodySize), trailersPtr, int32(trailersSize), timeout, &calloutID))
handleMemoryStatus(mod.Memory().WriteUint32Le(calloutIDPtr, calloutID))
// Finishing proxy_http_call executes a callback, not a plugin lifecycle method, unlike every other host function which would then end up in wasm.
// We can work around this by registering a callback here to go back to the wasm.
internal.RegisterHttpCallout(calloutID, func(numHeaders, bodySize, numTrailers int) {
proxyOnHttpCallResponse := mod.ExportedFunction("proxy_on_http_call_response")
_, err := proxyOnHttpCallResponse.Call(ctx, uint64(getPluginContextID(ctx)), uint64(calloutID), uint64(numHeaders), uint64(bodySize), uint64(numTrailers))
if err != nil {
panic(err)
}
})
return ret
}).
Export("proxy_http_call").
// proxy_call_foreign_function calls a registered foreign function.
//
// See https://github.com/proxy-wasm/spec/tree/master/abi-versions/vNEXT#proxy_call_foreign_function
NewFunctionBuilder().
WithParameterNames("function_name_data", "function_name_size", "parameters_data", "parameters_size",
"return_results_data", "return_results_size").
WithResultNames("call_result").
WithFunc(func(ctx context.Context, mod api.Module, funcNamePtr, funcNameSize, paramPtr, paramSize, returnData,
returnSize uint32) uint32 {
funcName := wasmBytePtr(mod, funcNamePtr, funcNameSize)
paramHostPtr := wasmBytePtr(mod, paramPtr, paramSize)
var returnDataHostPtr *byte
var returnDataSizePtr int32
ret := uint32(internal.ProxyCallForeignFunction(funcName, int32(funcNameSize), paramHostPtr, int32(paramSize), unsafe.Pointer(&returnDataHostPtr), &returnDataSizePtr))
copyBytesToWasm(ctx, mod, returnDataHostPtr, returnDataSizePtr, returnData, returnSize)
return ret
}).
Export("proxy_call_foreign_function").
// proxy_set_tick_period_milliseconds sets the timer period. Once set, the host environment will call
// proxy_on_tick every tick_period milliseconds.
//
// Note: proxy-wasm spec calls this proxy_set_tick_period. See
// https://github.com/proxy-wasm/spec/tree/master/abi-versions/vNEXT#proxy_set_tick_period
NewFunctionBuilder().
WithParameterNames("tick_period").
WithResultNames("call_result").
WithFunc(func(period uint32) uint32 {
return uint32(internal.ProxySetTickPeriodMilliseconds(period))
}).
Export("proxy_set_tick_period_milliseconds").
// proxy_set_effective_context changes the effective context. This function is usually used to change the
// context after receiving proxy_on_http_call_response, proxy_on_grpc_call_response or proxy_on_queue_ready.
//
// See https://github.com/proxy-wasm/spec/tree/master/abi-versions/vNEXT#proxy_set_effective_context
NewFunctionBuilder().
WithParameterNames("context_id").
WithResultNames("call_result").
WithFunc(func(contextID uint32) uint32 {
return uint32(internal.ProxySetEffectiveContext(contextID))
}).
Export("proxy_set_effective_context").
// proxy_done indicates to the host environment that Wasm VM side is done processing current context. This can
// be used after returning false in proxy_on_done.
//
// See https://github.com/proxy-wasm/spec/tree/master/abi-versions/vNEXT#proxy_done
NewFunctionBuilder().
WithResultNames("call_result").
WithFunc(func() uint32 {
return uint32(internal.ProxyDone())
}).
Export("proxy_done").
// proxy_define_metric defines a metric using a given name. It can be referred to in proxy_get_metric,
// proxy_increment_metric and proxy_record_metric using returned ID.
//
// See https://github.com/proxy-wasm/spec/tree/master/abi-versions/vNEXT#proxy_define_metric
NewFunctionBuilder().
WithParameterNames("metric_type", "metric_name_data", "metric_name_size", "return_metric_id").
WithResultNames("call_result").
WithFunc(func(ctx context.Context, mod api.Module, metricType, metricNameData, metricNameSize,
returnMetricIDPtr uint32) uint32 {
metricName := wasmBytePtr(mod, metricNameData, metricNameSize)
var returnMetricID uint32
ret := uint32(internal.ProxyDefineMetric(internal.MetricType(metricType), metricName, int32(metricNameSize), &returnMetricID))
handleMemoryStatus(mod.Memory().WriteUint32Le(returnMetricIDPtr, returnMetricID))
return ret
}).
Export("proxy_define_metric").
// proxy_increment_metric increments or decrements a metric value using an offset.
//
// See https://github.com/proxy-wasm/spec/tree/master/abi-versions/vNEXT#proxy_increment_metric
NewFunctionBuilder().
WithParameterNames("metric_id", "offset").
WithResultNames("call_result").
WithFunc(func(metricID uint32, offset int64) uint32 {
return uint32(internal.ProxyIncrementMetric(metricID, offset))
}).
Export("proxy_increment_metric").
// proxy_record_metric sets the value of a metric.
//
// See https://github.com/proxy-wasm/spec/tree/master/abi-versions/vNEXT#proxy_record_metric
NewFunctionBuilder().
WithParameterNames("metric_id", "value").
WithResultNames("call_result").
WithFunc(func(metricID uint32, value uint64) uint32 {
return uint32(internal.ProxyRecordMetric(metricID, value))
}).
Export("proxy_record_metric").
// proxy_get_metric gets the value of a metric.
//
// See https://github.com/proxy-wasm/spec/tree/master/abi-versions/vNEXT#proxy_get_metric
NewFunctionBuilder().
WithParameterNames("metric_id", "return_value").
WithResultNames("call_result").
WithFunc(func(ctx context.Context, mod api.Module, metricID, returnMetricValue uint32) uint32 {
var returnMetricValuePtr uint64
ret := uint32(internal.ProxyGetMetric(metricID, &returnMetricValuePtr))
handleMemoryStatus(mod.Memory().WriteUint64Le(returnMetricValue, returnMetricValuePtr))
return ret
}).
Export("proxy_get_metric").
Instantiate(ctx)
return err
}
type pluginContextIDKeyType struct{}
var pluginContextIDKey = pluginContextIDKeyType{}
func withPluginContextID(ctx context.Context, id uint32) context.Context {
return context.WithValue(ctx, pluginContextIDKey, id)
}
func getPluginContextID(ctx context.Context) uint32 {
id, _ := ctx.Value(pluginContextIDKey).(uint32)
return id
}