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
|
/*
* Copyright (C) Andrew Clayton
* Copyright (C) F5, Inc.
*/
#include <stdio.h>
#include <stdlib.h>
#include <wasm.h>
#include <wasi.h>
#include <wasmtime.h>
static void exit_with_error(const char *message, wasmtime_error_t *error,
wasm_trap_t *trap)
{
wasm_byte_vec_t error_message;
fprintf(stderr, "error: %s\n", 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);
}
fprintf(stderr, "%.*s\n", (int) error_message.size, error_message.data);
wasm_byte_vec_delete(&error_message);
exit(EXIT_FAILURE);
}
static wasm_trap_t *dummy_func(void *env, wasmtime_caller_t *caller,
const wasmtime_val_t *args, size_t nargs,
wasmtime_val_t *results, size_t nresults)
{
(void)env;
(void)caller;
(void)args;
(void)nargs;
(void)results;
(void)nresults;
return NULL;
}
static wasm_functype_t *create_wasm_func(const wasm_valkind_t *pk,
size_t nr_params,
const wasm_valkind_t *rk,
size_t nr_results)
{
wasm_valtype_t *pt[14];
wasm_valtype_t *rt[1];
wasm_valtype_vec_t params;
wasm_valtype_vec_t results;
if (nr_params > 0) {
for (size_t i = 0; i < nr_params; i++)
pt[i] = wasm_valtype_new(pk[i]);
wasm_valtype_vec_new(¶ms, nr_params, pt);
} else {
wasm_valtype_vec_new_empty(¶ms);
}
if (nr_results > 0) {
for (size_t i = 0; i < nr_results; i++)
rt[i] = wasm_valtype_new(rk[i]);
wasm_valtype_vec_new(&results, nr_results, rt);
} else {
wasm_valtype_vec_new_empty(&results);
}
return wasm_functype_new(¶ms, &results);
}
static inline size_t rg(const char *s)
{
return strlen(s);
}
/*
* Argument counting macros gleamed from
* https://gist.github.com/christophercrouzet/db9bbb07d427381253a91210e7ddc68d
*/
#define ARG( \
_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, ...) \
_14
#define COUNT_ARGS(...) \
ARG(__VA_ARGS__, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0,)
#define SET_VALS(type, ...) \
.type = { __VA_ARGS__ }, \
.nr_##type = COUNT_ARGS(__VA_ARGS__)
#define SET_FUNC(function, function_name, module) \
.func_name = function_name, \
.func_namelen = strlen(function_name), \
.mod_name = module, \
.mod_namelen = strlen(module), \
.func = dummy_func
static void set_function_imports(wasmtime_linker_t *linker)
{
static const struct {
const char *func_name;
int func_namelen;
const char *mod_name;
int mod_namelen;
wasmtime_func_callback_t func;
wasm_valkind_t params[14];
size_t nr_params;
wasm_valkind_t results[1];
size_t nr_results;
} import_functions[] = {
{
SET_FUNC(poll_oneoff, "poll-oneoff", "poll2"),
SET_VALS(params, WASM_I32, WASM_I32, WASM_I32),
}, {
SET_FUNC(read, "read", "streams2"),
SET_VALS(params, WASM_I32, WASM_I64, WASM_I32),
}, {
SET_FUNC(blocking_read, "blocking-read", "streams2"),
SET_VALS(params, WASM_I32, WASM_I64, WASM_I32),
}, {
SET_FUNC(subscribe_to_input_stream,
"subscribe-to-input-stream", "streams2"),
SET_VALS(params, WASM_I32),
SET_VALS(results, WASM_I32),
}, {
SET_FUNC(write, "write", "streams2"),
SET_VALS(params,
WASM_I32, WASM_I32, WASM_I32, WASM_I32),
}, {
SET_FUNC(blocking_write, "blocking-write", "streams2"),
SET_VALS(params,
WASM_I32, WASM_I32, WASM_I32, WASM_I32),
}, {
SET_FUNC(subscribe_to_output_stream,
"subscribe-to-output-stream", "streams2"),
SET_VALS(params, WASM_I32),
SET_VALS(results, WASM_I32),
}, {
SET_FUNC(handle, "handle", "default-outgoing-HTTP2"),
SET_VALS(params,
WASM_I32, WASM_I32, WASM_I32, WASM_I32,
WASM_I32, WASM_I32, WASM_I32, WASM_I32),
SET_VALS(results, WASM_I32),
}, {
SET_FUNC(new_fields, "new-fields", "types2"),
SET_VALS(params, WASM_I32, WASM_I32),
SET_VALS(results, WASM_I32),
}, {
SET_FUNC(fields_entries, "fields-entries", "types2"),
SET_VALS(params, WASM_I32, WASM_I32),
}, {
SET_FUNC(finish_incoming_stream,
"finish-incoming-stream", "types2"),
SET_VALS(params, WASM_I32, WASM_I32),
}, {
SET_FUNC(finish_outgoing_stream,
"finish-outgoing-stream", "types2"),
SET_VALS(params, WASM_I32, WASM_I32, WASM_I32),
}, {
SET_FUNC(incoming_request_method,
"incoming-request-method", "types2"),
SET_VALS(params, WASM_I32, WASM_I32),
}, {
SET_FUNC(incoming_request_path_with_query,
"incoming-request-path-with-query", "types2"),
SET_VALS(params, WASM_I32, WASM_I32),
}, {
SET_FUNC(incoming_request_headers,
"incoming-request-headers", "types2"),
SET_VALS(params, WASM_I32),
SET_VALS(results, WASM_I32),
}, {
SET_FUNC(incoming_request_consume,
"incoming-request-consume", "types2"),
SET_VALS(params, WASM_I32, WASM_I32),
}, {
SET_FUNC(new_outgoing_request, "new-outgoing-request",
"types2"),
SET_VALS(params,
WASM_I32, WASM_I32, WASM_I32, WASM_I32,
WASM_I32, WASM_I32, WASM_I32, WASM_I32,
WASM_I32, WASM_I32, WASM_I32, WASM_I32,
WASM_I32, WASM_I32),
SET_VALS(results, WASM_I32),
}, {
SET_FUNC(outgoing_request_write,
"outgoing-request-write", "types2"),
SET_VALS(params, WASM_I32, WASM_I32),
}, {
SET_FUNC(set_response_outparam,
"set-response-outparam", "types2"),
SET_VALS(params,
WASM_I32, WASM_I32, WASM_I32, WASM_I32,
WASM_I32),
SET_VALS(results, WASM_I32),
}, {
SET_FUNC(incoming_response_status,
"incoming-response-status", "types2"),
SET_VALS(params, WASM_I32),
SET_VALS(results, WASM_I32),
}, {
SET_FUNC(incoming_response_headers,
"incoming-response-headers", "types2"),
SET_VALS(params, WASM_I32),
SET_VALS(results, WASM_I32),
}, {
SET_FUNC(incoming_response_consume,
"incoming-response-consume", "types2"),
SET_VALS(params, WASM_I32, WASM_I32),
}, {
SET_FUNC(new_outgoing_response,
"new-outgoing-response", "types2"),
SET_VALS(params, WASM_I32, WASM_I32),
SET_VALS(results, WASM_I32),
}, {
SET_FUNC(outgoing_response_write,
"outgoing-response-write", "types2"),
SET_VALS(params, WASM_I32, WASM_I32),
}, {
SET_FUNC(future_incoming_response-get,
"future-incoming-response-get", "types2"),
SET_VALS(params, WASM_I32, WASM_I32),
}, {
SET_FUNC(listen_to_fauture_incoming_response,
"listen-to-future-incoming-response",
"types2"),
SET_VALS(params, WASM_I32),
SET_VALS(results, WASM_I32),
},
{ }
}, *imf;
for (imf = import_functions; imf->func_name != NULL; imf++) {
wasm_functype_t *func_ty;
printf(" [%s::%s]\n", imf->mod_name, imf->func_name);
func_ty = create_wasm_func(imf->params, imf->nr_params,
imf->results, imf->nr_results);
wasmtime_linker_define_func(linker,
imf->mod_name, imf->mod_namelen,
imf->func_name, imf->func_namelen,
func_ty, imf->func, NULL, NULL);
wasm_functype_delete(func_ty);
}
}
int main(void)
{
wasm_engine_t *engine;
wasmtime_store_t *store;
wasmtime_context_t *context;
wasmtime_module_t *module = NULL;
wasmtime_linker_t *linker;
wasmtime_error_t *error;
wasm_byte_vec_t wasm;
wasmtime_extern_t item;
wasi_config_t *wasi_config;
FILE *file;
size_t file_size;
bool ok;
printf("Initialising...\n");
engine = wasm_engine_new();
store = wasmtime_store_new(engine, NULL, NULL);
context = wasmtime_store_context(store);
linker = wasmtime_linker_new(engine);
error = wasmtime_linker_define_wasi(linker);
if (error != NULL)
exit_with_error("failed to link wasi", error, NULL);
printf("Loading binary...\n");
file = fopen(WASM_MODULE, "r");
if (!file) {
printf("> Error loading file!\n");
exit(EXIT_FAILURE);
}
fseek(file, 0L, SEEK_END);
file_size = ftell(file);
wasm_byte_vec_new_uninitialized(&wasm, file_size);
fseek(file, 0L, SEEK_SET);
if (fread(wasm.data, file_size, 1, file) != 1) {
printf("> Error loading module!\n");
exit(EXIT_FAILURE);
}
fclose(file);
printf("Compiling module...\n");
error = wasmtime_module_new(engine, (uint8_t*)wasm.data, wasm.size,
&module);
if (!module)
exit_with_error("failed to compile module", error, NULL);
wasm_byte_vec_delete(&wasm);
printf("Setting function imports...\n");
set_function_imports(linker);
printf("Initialising WASI...\n");
wasi_config = wasi_config_new();
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)
exit_with_error("failed to initialise WASI", error, NULL);
printf("Instantiating module...\n");
error = wasmtime_linker_module(linker, context, "", 0, module);
if (error != NULL)
exit_with_error("failed to instantiate module", error, NULL);
printf("Getting function exports...\n");
ok = wasmtime_linker_get(linker, context, "", 0, "HTTP#handle", 11,
&item);
if (!ok) {
printf("> Error getting function export [HTTP#handle]\n");
exit(EXIT_FAILURE);
}
printf(" [HTTP#handle]\n");
ok = wasmtime_linker_get(linker, context, "", 0, "cabi_realloc", 12,
&item);
if (!ok) {
printf("> Error getting function export [cabi_realloc]\n");
exit(EXIT_FAILURE);
}
printf(" [cabi_realloc]\n");
printf("Shutting down...\n");
wasmtime_module_delete(module);
wasmtime_store_delete(store);
wasm_engine_delete(engine);
printf("Done.\n");
exit(EXIT_SUCCESS);;
}
|