-
Notifications
You must be signed in to change notification settings - Fork 22
/
wasmtime.c
413 lines (330 loc) · 12 KB
/
wasmtime.c
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
/*
* Copyright 2022 Shenzhen ZhiLiu Technology Co., Ltd.
*
* 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 <wasi.h>
#include <wasm.h>
#include <wasmtime.h>
#include <http/ngx_http_wasm_api_wasmtime.h>
#include "vm.h"
typedef struct {
wasmtime_module_t *module;
wasmtime_store_t *store;
wasmtime_context_t *context;
wasmtime_linker_t *linker;
wasmtime_instance_t instance;
wasmtime_memory_t memory;
} ngx_wasm_wasmtime_plugin_t;
static ngx_str_t vm_name = ngx_string("wasmtime");
static wasm_engine_t *vm_engine;
static wasmtime_val_t param_int32[1] = {{ .kind = WASMTIME_I32 }};
static wasmtime_val_t param_int32_int32[2] = {{ .kind = WASMTIME_I32 }, { .kind = WASMTIME_I32 }};
static wasmtime_val_t param_int32_int32_int32[3] = {
{ .kind = WASMTIME_I32 }, { .kind = WASMTIME_I32 }, { .kind = WASMTIME_I32 },
};
static wasmtime_val_t param_int32_int32_int32_int32[4] = {
{ .kind = WASMTIME_I32 }, { .kind = WASMTIME_I32 }, { .kind = WASMTIME_I32 },
{ .kind = WASMTIME_I32 },
};
static wasmtime_val_t param_int32_int32_int32_int32_int32[5] = {
{ .kind = WASMTIME_I32 }, { .kind = WASMTIME_I32 }, { .kind = WASMTIME_I32 },
{ .kind = WASMTIME_I32 }, { .kind = WASMTIME_I32 },
};
static ngx_wasm_wasmtime_plugin_t *cur_plugin;
static wasm_functype_t *
ngx_http_wasmtime_host_api_func(const ngx_wasm_wasmtime_host_api_t *api)
{
int i;
wasm_valtype_vec_t param_vec, result_vec;
wasm_valtype_t *param[MAX_WASM_API_ARG];
wasm_valtype_t *result[1];
wasm_functype_t *f;
for (i = 0; i < api->param_num; i++) {
param[i] = wasm_valtype_new(api->param_type[i]);
}
result[0] = wasm_valtype_new(WASM_I32);
wasm_valtype_vec_new(¶m_vec, api->param_num, param);
wasm_valtype_vec_new(&result_vec, 1, result);
f = wasm_functype_new(¶m_vec, &result_vec);
for (i = 0; i < api->param_num; i++) {
wasm_valtype_delete(param[i]);
}
wasm_valtype_delete(result[0]);
return f;
}
static void
ngx_wasm_wasmtime_report_error(ngx_log_t *log, const char *message,
wasmtime_error_t *error, wasm_trap_t *trap)
{
wasm_byte_vec_t error_message;
if (error != NULL) {
wasmtime_error_message(error, &error_message);
wasmtime_error_delete(error);
} else {
wasm_trap_message(trap, &error_message);
wasm_trap_delete(trap);
}
ngx_log_error(NGX_LOG_ERR, log, 0, "%s%*s",
message, error_message.size, error_message.data);
wasm_byte_vec_delete(&error_message);
}
static ngx_int_t
ngx_wasm_wasmtime_init(void)
{
ngx_log_error(NGX_LOG_NOTICE, ngx_cycle->log, 0, "init wasm vm: wasmtime");
vm_engine = wasm_engine_new();
if (vm_engine == NULL) {
return NGX_DECLINED;
}
return NGX_OK;
}
static void
ngx_wasm_wasmtime_cleanup(void)
{
if (vm_engine == NULL) {
return;
}
ngx_log_error(NGX_LOG_NOTICE, ngx_cycle->log, 0, "cleanup wasm vm: wasmtime");
wasm_engine_delete(vm_engine);
}
static void *
ngx_wasm_wasmtime_load(const char *bytecode, size_t size)
{
size_t i;
bool ok;
wasm_trap_t *trap = NULL;
wasmtime_module_t *module;
wasmtime_store_t *store;
wasmtime_context_t *context;
wasmtime_linker_t *linker;
wasi_config_t *wasi_config;
wasmtime_error_t *error;
ngx_wasm_wasmtime_plugin_t *plugin;
wasmtime_extern_t item;
error = wasmtime_module_new(vm_engine, (const uint8_t*) bytecode, size, &module);
if (error != NULL) {
ngx_wasm_wasmtime_report_error(ngx_cycle->log, "failed to new module: ", error, NULL);
return NULL;
}
store = wasmtime_store_new(vm_engine, NULL, NULL);
if (store == NULL) {
goto free_module;
}
context = wasmtime_store_context(store);
wasi_config = wasi_config_new();
if (wasi_config == NULL) {
goto free_store;
}
wasi_config_inherit_env(wasi_config);
wasi_config_inherit_stdin(wasi_config);
wasi_config_inherit_stdout(wasi_config);
wasi_config_inherit_stderr(wasi_config);
error = wasmtime_context_set_wasi(context, wasi_config);
if (error != NULL) {
ngx_wasm_wasmtime_report_error(ngx_cycle->log, "failed to init WASI: ", error, NULL);
goto free_store;
}
linker = wasmtime_linker_new(vm_engine);
if (linker == NULL) {
goto free_store;
}
error = wasmtime_linker_define_wasi(linker);
if (error != NULL) {
ngx_wasm_wasmtime_report_error(ngx_cycle->log, "failed to init WASI: ", error, NULL);
goto free_linker;
}
for (i = 0; host_apis[i].name.len; i++) {
ngx_wasm_wasmtime_host_api_t *api = &host_apis[i];
wasm_functype_t *f;
ngx_log_debug1(NGX_LOG_DEBUG_CORE, ngx_cycle->log, 0,
"define wasm host API %V", &api->name);
f = ngx_http_wasmtime_host_api_func(api);
if (f == NULL) {
goto free_linker;
}
error = wasmtime_linker_define_func(linker, "env", 3,
(const char *) api->name.data, api->name.len,
f,
api->cb, NULL, NULL);
wasm_functype_delete(f);
if (error != NULL) {
ngx_wasm_wasmtime_report_error(ngx_cycle->log, "failed to define API ", error, NULL);
goto free_linker;
}
}
plugin = ngx_alloc(sizeof(ngx_wasm_wasmtime_plugin_t), ngx_cycle->log);
if (plugin == NULL) {
goto free_linker;
}
error = wasmtime_linker_instantiate(linker, context, module, &plugin->instance, &trap);
if (error != NULL) {
ngx_wasm_wasmtime_report_error(ngx_cycle->log, "failed to new instance: ", error, NULL);
goto free_plugin;
}
ok = wasmtime_instance_export_get(context, &plugin->instance, "memory", strlen("memory"), &item);
if (!ok || item.kind != WASMTIME_EXTERN_MEMORY) {
ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, 0, "the wasm plugin doesn't export memory");
goto free_plugin;
}
plugin->memory = item.of.memory;
plugin->module = module;
plugin->store = store;
plugin->context = context;
plugin->linker = linker;
ngx_log_error(NGX_LOG_INFO, ngx_cycle->log, 0, "loaded wasm plugin");
return plugin;
free_plugin:
ngx_free(plugin);
free_linker:
wasmtime_linker_delete(linker);
free_store:
wasmtime_store_delete(store);
free_module:
wasmtime_module_delete(module);
return NULL;
}
static void
ngx_wasm_wasmtime_unload(void *data)
{
ngx_wasm_wasmtime_plugin_t *plugin = data;
wasmtime_module_delete(plugin->module);
wasmtime_store_delete(plugin->store);
wasmtime_linker_delete(plugin->linker);
ngx_free(plugin);
ngx_log_error(NGX_LOG_INFO, ngx_cycle->log, 0, "unloaded wasm plugin");
}
static ngx_int_t
ngx_wasm_wasmtime_call(void *data, ngx_str_t *name, bool has_result, int param_type, ...)
{
ngx_wasm_wasmtime_plugin_t *plugin = data;
wasmtime_extern_t func;
wasm_trap_t *trap = NULL;
wasmtime_error_t *error;
bool found;
va_list args;
wasmtime_val_t *params = NULL;
size_t param_num = 0;
wasmtime_val_t results[1];
ngx_int_t rc;
ngx_log_debug1(NGX_LOG_DEBUG_CORE, ngx_cycle->log, 0, "wasmtime call function %V", name);
if (plugin == NULL) {
plugin = cur_plugin;
} else {
cur_plugin = plugin;
}
found = wasmtime_instance_export_get(plugin->context, &plugin->instance,
(const char *) name->data, name->len, &func);
if (!found) {
ngx_log_debug1(NGX_LOG_DEBUG_CORE, ngx_cycle->log, 0,
"wasmtime function %V not defined", name);
return NGX_OK;
}
va_start(args, param_type);
switch (param_type) {
case NGX_WASM_PARAM_VOID:
break;
case NGX_WASM_PARAM_I32:
params = param_int32;
params[0].of.i32 = va_arg(args, int32_t);
param_num = 1;
break;
case NGX_WASM_PARAM_I32_I32:
params = param_int32_int32;
params[0].of.i32 = va_arg(args, int32_t);
params[1].of.i32 = va_arg(args, int32_t);
param_num = 2;
break;
case NGX_WASM_PARAM_I32_I32_I32:
params = param_int32_int32_int32;
params[0].of.i32 = va_arg(args, int32_t);
params[1].of.i32 = va_arg(args, int32_t);
params[2].of.i32 = va_arg(args, int32_t);
param_num = 3;
break;
case NGX_WASM_PARAM_I32_I32_I32_I32:
params = param_int32_int32_int32_int32;
params[0].of.i32 = va_arg(args, int32_t);
params[1].of.i32 = va_arg(args, int32_t);
params[2].of.i32 = va_arg(args, int32_t);
params[3].of.i32 = va_arg(args, int32_t);
param_num = 4;
break;
case NGX_WASM_PARAM_I32_I32_I32_I32_I32:
params = param_int32_int32_int32_int32_int32;
params[0].of.i32 = va_arg(args, int32_t);
params[1].of.i32 = va_arg(args, int32_t);
params[2].of.i32 = va_arg(args, int32_t);
params[3].of.i32 = va_arg(args, int32_t);
params[4].of.i32 = va_arg(args, int32_t);
param_num = 5;
break;
default:
ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, 0, "unknown param type: %d", param_type);
va_end(args);
return NGX_ERROR;
}
va_end(args);
error = wasmtime_func_call(plugin->context, &func.of.func, params, param_num, results,
has_result ? 1 : 0, &trap);
if (error != NULL || trap != NULL) {
ngx_wasm_wasmtime_report_error(ngx_cycle->log, "failed to call function: ", error, trap);
return NGX_ERROR;
}
if (!has_result) {
ngx_log_debug0(NGX_LOG_DEBUG_CORE, ngx_cycle->log, 0,
"wasmtime call function done");
return NGX_OK;
}
if (results[0].kind != WASMTIME_I32) {
ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, 0, "function returns unexpected type: %d",
results[0].kind);
return NGX_ERROR;
}
rc = results[0].of.i32;
ngx_log_debug1(NGX_LOG_DEBUG_CORE, ngx_cycle->log, 0,
"wasmtime call function result: %d", rc);
return rc;
}
static bool
ngx_wasm_wasmtime_has(void *data, ngx_str_t *name)
{
ngx_wasm_wasmtime_plugin_t *plugin = data;
wasmtime_extern_t func;
return wasmtime_instance_export_get(plugin->context, &plugin->instance,
(const char *) name->data, name->len, &func);
}
u_char *
ngx_wasm_wasmtime_get_memory(ngx_log_t *log, int32_t addr, int32_t size)
{
size_t bound;
bound = wasmtime_memory_data_size(cur_plugin->context, &cur_plugin->memory);
if (bound < (size_t) (addr + size)) {
ngx_log_error(NGX_LOG_ERR, log, 0,
"access memory addr %d with size %d, but the max addr is %z",
addr, size, bound);
return NULL;
}
return wasmtime_memory_data(cur_plugin->context, &cur_plugin->memory) + addr;
}
ngx_wasm_vm_t ngx_wasm_wasmtime_vm = {
&vm_name,
ngx_wasm_wasmtime_init,
ngx_wasm_wasmtime_cleanup,
ngx_wasm_wasmtime_load,
ngx_wasm_wasmtime_unload,
ngx_wasm_wasmtime_get_memory,
ngx_wasm_wasmtime_call,
ngx_wasm_wasmtime_has,
};