-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathwasmtime.cc
701 lines (614 loc) · 25 KB
/
wasmtime.cc
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
// Copyright 2020 Google LLC
//
// 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.
#include "include/proxy-wasm/wasmtime.h"
#include <array>
#include <cassert>
#include <cstring>
#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include <unordered_map>
#include <vector>
#include "src/wasmtime/types.h"
#include "include/wasm.h"
namespace proxy_wasm {
namespace wasmtime {
struct HostFuncData {
HostFuncData(std::string name) : name_(std::move(name)) {}
std::string name_;
WasmFuncPtr callback_;
void *raw_func_{};
WasmVm *vm_{};
};
using HostFuncDataPtr = std::unique_ptr<HostFuncData>;
wasm_engine_t *engine() {
static const auto engine = WasmEnginePtr(wasm_engine_new());
return engine.get();
}
class Wasmtime : public WasmVm {
public:
Wasmtime() = default;
std::string_view getEngineName() override { return "wasmtime"; }
Cloneable cloneable() override { return Cloneable::CompiledBytecode; }
std::string_view getPrecompiledSectionName() override { return ""; }
bool load(std::string_view bytecode, std::string_view precompiled,
const std::unordered_map<uint32_t, std::string> &function_names) override;
bool link(std::string_view debug_name) override;
std::unique_ptr<WasmVm> clone() override;
uint64_t getMemorySize() override;
std::optional<std::string_view> getMemory(uint64_t pointer, uint64_t size) override;
bool setMemory(uint64_t pointer, uint64_t size, const void *data) override;
bool getWord(uint64_t pointer, Word *word) override;
bool setWord(uint64_t pointer, Word word) override;
size_t getWordSize() override { return sizeof(uint32_t); };
#define _REGISTER_HOST_FUNCTION(T) \
void registerCallback(std::string_view module_name, std::string_view function_name, T, \
typename ConvertFunctionTypeWordToUint32<T>::type f) override { \
registerHostFunctionImpl(module_name, function_name, f); \
};
FOR_ALL_WASM_VM_IMPORTS(_REGISTER_HOST_FUNCTION)
#undef _REGISTER_HOST_FUNCTION
#define _GET_MODULE_FUNCTION(T) \
void getFunction(std::string_view function_name, T *f) override { \
getModuleFunctionImpl(function_name, f); \
};
FOR_ALL_WASM_VM_EXPORTS(_GET_MODULE_FUNCTION)
#undef _GET_MODULE_FUNCTION
private:
template <typename... Args>
void registerHostFunctionImpl(std::string_view module_name, std::string_view function_name,
void (*function)(Args...));
template <typename R, typename... Args>
void registerHostFunctionImpl(std::string_view module_name, std::string_view function_name,
R (*function)(Args...));
template <typename... Args>
void getModuleFunctionImpl(std::string_view function_name,
std::function<void(ContextBase *, Args...)> *function);
template <typename R, typename... Args>
void getModuleFunctionImpl(std::string_view function_name,
std::function<R(ContextBase *, Args...)> *function);
void terminate() override {}
WasmStorePtr store_;
WasmModulePtr module_;
WasmSharedModulePtr shared_module_;
WasmInstancePtr instance_;
WasmMemoryPtr memory_;
WasmTablePtr table_;
std::unordered_map<std::string, HostFuncDataPtr> host_functions_;
std::unordered_map<std::string, WasmFuncPtr> module_functions_;
};
bool Wasmtime::load(std::string_view bytecode, std::string_view /*precompiled*/,
const std::unordered_map<uint32_t, std::string> & /*function_names*/) {
store_ = wasm_store_new(engine());
if (store_ == nullptr) {
return false;
}
WasmByteVec vec;
wasm_byte_vec_new(vec.get(), bytecode.size(), bytecode.data());
module_ = wasm_module_new(store_.get(), vec.get());
if (module_ == nullptr) {
return false;
}
shared_module_ = wasm_module_share(module_.get());
if (shared_module_ == nullptr) {
return false;
}
return true;
}
std::unique_ptr<WasmVm> Wasmtime::clone() {
assert(shared_module_ != nullptr);
auto clone = std::make_unique<Wasmtime>();
if (clone == nullptr) {
return nullptr;
}
clone->store_ = wasm_store_new(engine());
if (clone->store_ == nullptr) {
return nullptr;
}
clone->module_ = wasm_module_obtain(clone->store_.get(), shared_module_.get());
if (clone->module_ == nullptr) {
return nullptr;
}
auto *integration_clone = integration()->clone();
if (integration_clone == nullptr) {
return nullptr;
}
clone->integration().reset(integration_clone);
return clone;
}
static bool equalValTypes(const wasm_valtype_vec_t *left, const wasm_valtype_vec_t *right) {
if (left->size != right->size) {
return false;
}
for (size_t i = 0; i < left->size; i++) {
if (wasm_valtype_kind(left->data[i]) != wasm_valtype_kind(right->data[i])) {
return false;
}
}
return true;
}
static std::string printValue(const wasm_val_t &value) {
switch (value.kind) {
case WASM_I32:
return std::to_string(value.of.i32);
case WASM_I64:
return std::to_string(value.of.i64);
case WASM_F32:
return std::to_string(value.of.f32);
case WASM_F64:
return std::to_string(value.of.f64);
default:
return "unknown";
}
}
static std::string printValues(const wasm_val_vec_t *values) {
if (values->size == 0) {
return "";
}
std::string s;
for (size_t i = 0; i < values->size; i++) {
if (i != 0U) {
s.append(", ");
}
s.append(printValue(values->data[i]));
}
return s;
}
static const char *printValKind(wasm_valkind_t kind) {
switch (kind) {
case WASM_I32:
return "i32";
case WASM_I64:
return "i64";
case WASM_F32:
return "f32";
case WASM_F64:
return "f64";
case WASM_ANYREF:
return "anyref";
case WASM_FUNCREF:
return "funcref";
default:
return "unknown";
}
}
static std::string printValTypes(const wasm_valtype_vec_t *types) {
if (types->size == 0) {
return "void";
}
std::string s;
s.reserve(types->size * 8 /* max size + " " */ - 1);
for (size_t i = 0; i < types->size; i++) {
if (i != 0U) {
s.append(" ");
}
s.append(printValKind(wasm_valtype_kind(types->data[i])));
}
return s;
}
bool Wasmtime::link(std::string_view /*debug_name*/) {
assert(module_ != nullptr);
WasmImporttypeVec import_types;
wasm_module_imports(module_.get(), import_types.get());
std::vector<wasm_extern_t *> imports;
for (size_t i = 0; i < import_types.get()->size; i++) {
const wasm_name_t *module_name_ptr = wasm_importtype_module(import_types.get()->data[i]);
const wasm_name_t *name_ptr = wasm_importtype_name(import_types.get()->data[i]);
const wasm_externtype_t *extern_type = wasm_importtype_type(import_types.get()->data[i]);
std::string_view module_name(module_name_ptr->data, module_name_ptr->size);
std::string_view name(name_ptr->data, name_ptr->size);
assert(name_ptr->size > 0);
switch (wasm_externtype_kind(extern_type)) {
case WASM_EXTERN_FUNC: {
auto it = host_functions_.find(std::string(module_name) + "." + std::string(name));
if (it == host_functions_.end()) {
fail(FailState::UnableToInitializeCode,
std::string("Failed to load Wasm module due to a missing import: ") +
std::string(module_name) + "." + std::string(name));
return false;
}
auto *func = it->second->callback_.get();
const wasm_functype_t *exp_type = wasm_externtype_as_functype_const(extern_type);
WasmFunctypePtr actual_type = wasm_func_type(it->second->callback_.get());
if (!equalValTypes(wasm_functype_params(exp_type), wasm_functype_params(actual_type.get())) ||
!equalValTypes(wasm_functype_results(exp_type),
wasm_functype_results(actual_type.get()))) {
fail(
FailState::UnableToInitializeCode,
std::string("Failed to load Wasm module due to an import type mismatch for function ") +
std::string(module_name) + "." + std::string(name) +
", want: " + printValTypes(wasm_functype_params(exp_type)) + " -> " +
printValTypes(wasm_functype_results(exp_type)) +
", but host exports: " + printValTypes(wasm_functype_params(actual_type.get())) +
" -> " + printValTypes(wasm_functype_results(actual_type.get())));
return false;
}
imports.push_back(wasm_func_as_extern(func));
} break;
case WASM_EXTERN_GLOBAL: {
// TODO(mathetake): add support when/if needed.
fail(FailState::UnableToInitializeCode,
"Failed to load Wasm module due to a missing import: " + std::string(module_name) + "." +
std::string(name));
return false;
} break;
case WASM_EXTERN_MEMORY: {
assert(memory_ == nullptr);
const wasm_memorytype_t *memory_type =
wasm_externtype_as_memorytype_const(extern_type); // owned by `extern_type`
if (memory_type == nullptr) {
return false;
}
memory_ = wasm_memory_new(store_.get(), memory_type);
if (memory_ == nullptr) {
return false;
}
imports.push_back(wasm_memory_as_extern(memory_.get()));
} break;
case WASM_EXTERN_TABLE: {
assert(table_ == nullptr);
const wasm_tabletype_t *table_type =
wasm_externtype_as_tabletype_const(extern_type); // owned by `extern_type`
if (table_type == nullptr) {
return false;
}
table_ = wasm_table_new(store_.get(), table_type, nullptr);
if (table_ == nullptr) {
return false;
}
imports.push_back(wasm_table_as_extern(table_.get()));
} break;
}
}
if (import_types.get()->size != imports.size()) {
return false;
}
wasm_extern_vec_t imports_vec = {imports.size(), imports.data()};
instance_ = wasm_instance_new(store_.get(), module_.get(), &imports_vec, nullptr);
if (instance_ == nullptr) {
fail(FailState::UnableToInitializeCode, "Failed to create new Wasm instance");
return false;
}
WasmExportTypeVec export_types;
wasm_module_exports(module_.get(), export_types.get());
WasmExternVec exports;
wasm_instance_exports(instance_.get(), exports.get());
for (size_t i = 0; i < export_types.get()->size; i++) {
const wasm_externtype_t *exp_extern_type = wasm_exporttype_type(export_types.get()->data[i]);
wasm_extern_t *actual_extern = exports.get()->data[i];
wasm_externkind_t kind = wasm_extern_kind(actual_extern);
assert(kind == wasm_externtype_kind(exp_extern_type));
switch (kind) {
case WASM_EXTERN_FUNC: {
WasmFuncPtr func = wasm_func_copy(wasm_extern_as_func(actual_extern));
const wasm_name_t *name_ptr = wasm_exporttype_name(export_types.get()->data[i]);
module_functions_.insert_or_assign(std::string(name_ptr->data, name_ptr->size),
std::move(func));
} break;
case WASM_EXTERN_GLOBAL: {
// TODO(mathetake): add support when/if needed.
} break;
case WASM_EXTERN_MEMORY: {
assert(memory_ == nullptr);
memory_ = wasm_memory_copy(wasm_extern_as_memory(actual_extern));
if (memory_ == nullptr) {
return false;
}
} break;
case WASM_EXTERN_TABLE: {
// TODO(mathetake): add support when/if needed.
} break;
}
}
return true;
}
uint64_t Wasmtime::getMemorySize() { return wasm_memory_data_size(memory_.get()); }
std::optional<std::string_view> Wasmtime::getMemory(uint64_t pointer, uint64_t size) {
assert(memory_ != nullptr);
if (pointer + size > wasm_memory_data_size(memory_.get())) {
return std::nullopt;
}
return std::string_view(wasm_memory_data(memory_.get()) + pointer, size);
}
bool Wasmtime::setMemory(uint64_t pointer, uint64_t size, const void *data) {
assert(memory_ != nullptr);
if (pointer + size > wasm_memory_data_size(memory_.get())) {
return false;
}
::memcpy(wasm_memory_data(memory_.get()) + pointer, data, size);
return true;
}
bool Wasmtime::getWord(uint64_t pointer, Word *word) {
assert(memory_ != nullptr);
constexpr auto size = sizeof(uint32_t);
if (pointer + size > wasm_memory_data_size(memory_.get())) {
return false;
}
uint32_t word32;
::memcpy(&word32, wasm_memory_data(memory_.get()) + pointer, size);
word->u64_ = wasmtoh(word32);
return true;
}
bool Wasmtime::setWord(uint64_t pointer, Word word) {
constexpr auto size = sizeof(uint32_t);
if (pointer + size > wasm_memory_data_size(memory_.get())) {
return false;
}
uint32_t word32 = htowasm(word.u32());
::memcpy(wasm_memory_data(memory_.get()) + pointer, &word32, size);
return true;
}
template <typename T> void assignVal(T t, wasm_val_t &val);
template <> void assignVal<Word>(Word t, wasm_val_t &val) {
val.kind = WASM_I32;
val.of.i32 = static_cast<int32_t>(t.u64_);
}
template <> void assignVal(uint32_t t, wasm_val_t &val) {
val.kind = WASM_I32;
val.of.i32 = static_cast<int32_t>(t);
}
template <> void assignVal(uint64_t t, wasm_val_t &val) {
val.kind = WASM_I64;
val.of.i64 = static_cast<int64_t>(t);
}
template <> void assignVal(double t, wasm_val_t &val) {
val.kind = WASM_F64;
val.of.f64 = t;
}
template <typename T> wasm_val_t makeVal(T t) {
wasm_val_t val{};
assignVal(t, val);
return val;
}
template <typename T> struct ConvertWordType {
using type = T; // NOLINT(readability-identifier-naming)
};
template <> struct ConvertWordType<Word> {
using type = uint32_t; // NOLINT(readability-identifier-naming)
};
template <typename T> auto convertArgToValTypePtr();
template <> auto convertArgToValTypePtr<Word>() { return wasm_valtype_new_i32(); };
template <> auto convertArgToValTypePtr<uint32_t>() { return wasm_valtype_new_i32(); };
template <> auto convertArgToValTypePtr<int64_t>() { return wasm_valtype_new_i64(); };
template <> auto convertArgToValTypePtr<uint64_t>() { return wasm_valtype_new_i64(); };
template <> auto convertArgToValTypePtr<double>() { return wasm_valtype_new_f64(); };
template <typename T> T convertValueTypeToArg(wasm_val_t val);
template <> uint32_t convertValueTypeToArg<uint32_t>(wasm_val_t val) {
return static_cast<uint32_t>(val.of.i32);
}
template <> Word convertValueTypeToArg<Word>(wasm_val_t val) { return val.of.i32; }
template <> int64_t convertValueTypeToArg<int64_t>(wasm_val_t val) { return val.of.i64; }
template <> uint64_t convertValueTypeToArg<uint64_t>(wasm_val_t val) {
return static_cast<uint64_t>(val.of.i64);
}
template <> double convertValueTypeToArg<double>(wasm_val_t val) { return val.of.f64; }
template <typename T, typename U, std::size_t... I>
constexpr T convertValTypesToArgsTuple(const U &vec, std::index_sequence<I...> /*comptime*/) {
return std::make_tuple(
convertValueTypeToArg<typename ConvertWordType<std::tuple_element_t<I, T>>::type>(
vec->data[I])...);
}
template <typename T, std::size_t... I>
void convertArgsTupleToValTypesImpl(wasm_valtype_vec_t *types,
std::index_sequence<I...> /*comptime*/) {
auto size = std::tuple_size<T>::value;
auto ps = std::array<wasm_valtype_t *, std::tuple_size<T>::value>{
convertArgToValTypePtr<typename std::tuple_element<I, T>::type>()...};
wasm_valtype_vec_new(types, size, ps.data());
}
template <typename T, typename Is = std::make_index_sequence<std::tuple_size<T>::value>>
void convertArgsTupleToValTypes(wasm_valtype_vec_t *types) {
convertArgsTupleToValTypesImpl<T>(types, Is());
}
template <typename R, typename T> WasmFunctypePtr newWasmNewFuncType() {
WasmValtypeVec params;
WasmValtypeVec results;
convertArgsTupleToValTypes<T>(params.get());
convertArgsTupleToValTypes<std::tuple<R>>(results.get());
return wasm_functype_new(params.get(), results.get());
}
template <typename T> WasmFunctypePtr newWasmNewFuncType() {
WasmValtypeVec params;
WasmValtypeVec results;
convertArgsTupleToValTypes<T>(params.get());
convertArgsTupleToValTypes<std::tuple<>>(results.get());
return wasm_functype_new(params.get(), results.get());
}
template <typename... Args>
void Wasmtime::registerHostFunctionImpl(std::string_view module_name,
std::string_view function_name, void (*function)(Args...)) {
auto data =
std::make_unique<HostFuncData>(std::string(module_name) + "." + std::string(function_name));
WasmFunctypePtr type = newWasmNewFuncType<std::tuple<Args...>>();
WasmFuncPtr func = wasm_func_new_with_env(
store_.get(), type.get(),
[](void *data, const wasm_val_vec_t *params, wasm_val_vec_t * /*results*/) -> wasm_trap_t * {
auto *func_data = reinterpret_cast<HostFuncData *>(data);
const bool log = func_data->vm_->cmpLogLevel(LogLevel::trace);
if (log) {
func_data->vm_->integration()->trace("[vm->host] " + func_data->name_ + "(" +
printValues(params) + ")");
}
auto args = convertValTypesToArgsTuple<std::tuple<Args...>>(
params, std::make_index_sequence<sizeof...(Args)>{});
auto fn = reinterpret_cast<void (*)(Args...)>(func_data->raw_func_);
std::apply(fn, args);
if (log) {
func_data->vm_->integration()->trace("[vm<-host] " + func_data->name_ + " return: void");
}
return nullptr;
},
data.get(), nullptr);
data->vm_ = this;
data->callback_ = std::move(func);
data->raw_func_ = reinterpret_cast<void *>(function);
host_functions_.insert_or_assign(std::string(module_name) + "." + std::string(function_name),
std::move(data));
};
template <typename R, typename... Args>
void Wasmtime::registerHostFunctionImpl(std::string_view module_name,
std::string_view function_name, R (*function)(Args...)) {
auto data =
std::make_unique<HostFuncData>(std::string(module_name) + "." + std::string(function_name));
WasmFunctypePtr type = newWasmNewFuncType<R, std::tuple<Args...>>();
WasmFuncPtr func = wasm_func_new_with_env(
store_.get(), type.get(),
[](void *data, const wasm_val_vec_t *params, wasm_val_vec_t *results) -> wasm_trap_t * {
auto *func_data = reinterpret_cast<HostFuncData *>(data);
const bool log = func_data->vm_->cmpLogLevel(LogLevel::trace);
if (log) {
func_data->vm_->integration()->trace("[vm->host] " + func_data->name_ + "(" +
printValues(params) + ")");
}
auto args = convertValTypesToArgsTuple<std::tuple<Args...>>(
params, std::make_index_sequence<sizeof...(Args)>{});
auto fn = reinterpret_cast<R (*)(Args...)>(func_data->raw_func_);
R res = std::apply(fn, args);
assignVal<R>(res, results->data[0]);
if (log) {
func_data->vm_->integration()->trace("[vm<-host] " + func_data->name_ +
" return: " + std::to_string(res));
}
return nullptr;
},
data.get(), nullptr);
data->vm_ = this;
data->callback_ = std::move(func);
data->raw_func_ = reinterpret_cast<void *>(function);
host_functions_.insert_or_assign(std::string(module_name) + "." + std::string(function_name),
std::move(data));
};
template <typename... Args>
void Wasmtime::getModuleFunctionImpl(std::string_view function_name,
std::function<void(ContextBase *, Args...)> *function) {
auto it = module_functions_.find(std::string(function_name));
if (it == module_functions_.end()) {
*function = nullptr;
return;
}
WasmValtypeVec exp_args;
WasmValtypeVec exp_returns;
convertArgsTupleToValTypes<std::tuple<Args...>>(exp_args.get());
convertArgsTupleToValTypes<std::tuple<>>(exp_returns.get());
wasm_func_t *func = it->second.get();
WasmFunctypePtr func_type = wasm_func_type(func);
if (!equalValTypes(wasm_functype_params(func_type.get()), exp_args.get()) ||
!equalValTypes(wasm_functype_results(func_type.get()), exp_returns.get())) {
fail(FailState::UnableToInitializeCode,
"Bad function signature for: " + std::string(function_name) + ", want: " +
printValTypes(exp_args.get()) + " -> " + printValTypes(exp_returns.get()) +
", but the module exports: " + printValTypes(wasm_functype_params(func_type.get())) +
" -> " + printValTypes(wasm_functype_results(func_type.get())));
return;
}
*function = [func, function_name, this](ContextBase *context, Args... args) -> void {
const bool log = cmpLogLevel(LogLevel::trace);
SaveRestoreContext saved_context(context);
wasm_val_vec_t results = WASM_EMPTY_VEC;
WasmTrapPtr trap;
// Workaround for MSVC++ not supporting zero-sized arrays.
if constexpr (sizeof...(args) > 0) {
wasm_val_t params_arr[] = {makeVal(args)...};
wasm_val_vec_t params = WASM_ARRAY_VEC(params_arr);
if (log) {
integration()->trace("[host->vm] " + std::string(function_name) + "(" +
printValues(¶ms) + ")");
}
trap.reset(wasm_func_call(func, ¶ms, &results));
} else {
wasm_val_vec_t params = WASM_EMPTY_VEC;
if (log) {
integration()->trace("[host->vm] " + std::string(function_name) + "()");
}
trap.reset(wasm_func_call(func, ¶ms, &results));
}
if (trap) {
WasmByteVec error_message;
wasm_trap_message(trap.get(), error_message.get());
fail(FailState::RuntimeError,
"Function: " + std::string(function_name) + " failed:\n" +
std::string(error_message.get()->data, error_message.get()->size));
return;
}
if (log) {
integration()->trace("[host<-vm] " + std::string(function_name) + " return: void");
}
};
};
template <typename R, typename... Args>
void Wasmtime::getModuleFunctionImpl(std::string_view function_name,
std::function<R(ContextBase *, Args...)> *function) {
auto it = module_functions_.find(std::string(function_name));
if (it == module_functions_.end()) {
*function = nullptr;
return;
}
WasmValtypeVec exp_args;
WasmValtypeVec exp_returns;
convertArgsTupleToValTypes<std::tuple<Args...>>(exp_args.get());
convertArgsTupleToValTypes<std::tuple<R>>(exp_returns.get());
wasm_func_t *func = it->second.get();
WasmFunctypePtr func_type = wasm_func_type(func);
if (!equalValTypes(wasm_functype_params(func_type.get()), exp_args.get()) ||
!equalValTypes(wasm_functype_results(func_type.get()), exp_returns.get())) {
fail(FailState::UnableToInitializeCode,
"Bad function signature for: " + std::string(function_name) + ", want: " +
printValTypes(exp_args.get()) + " -> " + printValTypes(exp_returns.get()) +
", but the module exports: " + printValTypes(wasm_functype_params(func_type.get())) +
" -> " + printValTypes(wasm_functype_results(func_type.get())));
return;
}
*function = [func, function_name, this](ContextBase *context, Args... args) -> R {
const bool log = cmpLogLevel(LogLevel::trace);
SaveRestoreContext saved_context(context);
wasm_val_t results_arr[1];
wasm_val_vec_t results = WASM_ARRAY_VEC(results_arr);
WasmTrapPtr trap;
// Workaround for MSVC++ not supporting zero-sized arrays.
if constexpr (sizeof...(args) > 0) {
wasm_val_t params_arr[] = {makeVal(args)...};
wasm_val_vec_t params = WASM_ARRAY_VEC(params_arr);
if (log) {
integration()->trace("[host->vm] " + std::string(function_name) + "(" +
printValues(¶ms) + ")");
}
trap.reset(wasm_func_call(func, ¶ms, &results));
} else {
wasm_val_vec_t params = WASM_EMPTY_VEC;
if (log) {
integration()->trace("[host->vm] " + std::string(function_name) + "()");
}
trap.reset(wasm_func_call(func, ¶ms, &results));
}
if (trap) {
WasmByteVec error_message;
wasm_trap_message(trap.get(), error_message.get());
fail(FailState::RuntimeError,
"Function: " + std::string(function_name) + " failed:\n" +
std::string(error_message.get()->data, error_message.get()->size));
return R{};
}
R ret = convertValueTypeToArg<R>(results.data[0]);
if (log) {
integration()->trace("[host<-vm] " + std::string(function_name) +
" return: " + std::to_string(ret));
}
return ret;
};
};
} // namespace wasmtime
bool initWasmtimeEngine() { return wasmtime::engine() != nullptr; }
std::unique_ptr<WasmVm> createWasmtimeVm() { return std::make_unique<wasmtime::Wasmtime>(); }
} // namespace proxy_wasm