-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathhostfunc.c
256 lines (235 loc) · 7.91 KB
/
hostfunc.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
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <toywasm/cconv.h>
#include <toywasm/cell.h>
#include <toywasm/endian.h>
#include <toywasm/exec_context.h>
#include <toywasm/exec_debug.h>
#include <toywasm/host_instance.h>
#include <toywasm/restart.h>
static void
print_backtrace(const char *func, const struct exec_context *ctx)
{
printf("===== start backtrace (%s) =====\n", func);
print_trace(ctx);
printf("===== end backtrace (%s) =======\n", func);
}
static int
load(struct exec_context *ctx, struct host_instance *hi, uint32_t pp,
uint32_t *resultp)
{
int host_ret;
uint32_t le32;
/*
* *resultp = *(*pp)++
*/
host_ret =
host_func_copyin(ctx, host_func_memory(hi), &le32, pp, 4, 4);
if (host_ret != 0) {
goto fail;
}
uint32_t p = le32_to_host(le32);
host_ret = host_func_copyin(ctx, host_func_memory(hi), &le32, p, 4, 4);
if (host_ret != 0) {
goto fail;
}
uint32_t result = le32_to_host(le32);
p += 4;
le32 = host_to_le32(p);
host_ret =
host_func_copyout(ctx, host_func_memory(hi), &le32, pp, 4, 4);
if (host_ret != 0) {
goto fail;
}
*resultp = result;
fail:
return host_ret;
}
static int
load_func(struct exec_context *ctx, struct host_instance *hi,
const struct functype *ft, uint32_t pp, const struct funcinst **fip)
{
int host_ret;
uint32_t funcptr;
host_ret = load(ctx, hi, pp, &funcptr);
if (host_ret != 0) {
goto fail;
}
const struct funcinst *func;
host_ret = cconv_deref_func_ptr(ctx, host_func_func_table(hi), funcptr,
ft, &func);
if (host_ret != 0) {
goto fail;
}
*fip = func;
fail:
return host_ret;
}
static int
my_host_inst_load(struct exec_context *ctx, struct host_instance *hi,
const struct functype *ft, const struct cell *params,
struct cell *results)
{
HOST_FUNC_CONVERT_PARAMS(ft, params);
uint32_t pp = HOST_FUNC_PARAM(ft, params, 0, i32);
int host_ret;
print_backtrace(__func__, ctx);
uint32_t result;
host_ret = load(ctx, hi, pp, &result);
if (host_ret == 0) {
HOST_FUNC_RESULT_SET(ft, results, 0, i32, result);
}
HOST_FUNC_FREE_CONVERTED_PARAMS();
return host_ret;
}
static int
my_host_inst_load_call(struct exec_context *ctx, struct host_instance *hi,
const struct functype *ft, const struct cell *params,
struct cell *results)
{
HOST_FUNC_CONVERT_PARAMS(ft, params);
uint32_t pp = HOST_FUNC_PARAM(ft, params, 0, i32);
int host_ret;
print_backtrace(__func__, ctx);
const struct funcinst *func;
host_ret = load_func(ctx, hi, ft, pp, &func);
if (host_ret != 0) {
goto fail;
}
/* tail call with the same argument */
ctx->event_u.call.func = func;
ctx->event = EXEC_EVENT_CALL;
host_ret = ETOYWASMRESTART;
fail:
HOST_FUNC_FREE_CONVERTED_PARAMS();
return host_ret;
}
static int
my_host_inst_load_call_add(struct exec_context *ctx, struct host_instance *hi,
const struct functype *ft,
const struct cell *params, struct cell *results)
{
/*
* this function is a bit complicated as it calls other functions.
* the callee functions can be wasm functions or host functions.
*
* sum = 0;
* f1 = load_func(pp);
* v1 = f1(pp); // this might need a restart
* step1:
* sum += v1;
* f2 = load_func(pp);
* v2 = f2(pp); // this might need a restart
* step2:
* sum += v2;
* return sum;
*/
HOST_FUNC_CONVERT_PARAMS(ft, params);
uint32_t pp = HOST_FUNC_PARAM(ft, params, 0, i32);
int host_ret;
struct restart_hostfunc *hf;
uint32_t result;
uint32_t sum = 0;
uint32_t i;
print_backtrace(__func__, ctx);
host_ret = restart_info_prealloc(ctx);
if (host_ret != 0) {
return host_ret;
}
struct restart_info *restart = &VEC_NEXTELEM(ctx->restarts);
if (restart->restart_type != RESTART_NONE) {
assert(restart->restart_type == RESTART_HOSTFUNC);
hf = &restart->restart_u.hostfunc;
uint32_t step = hf->user1;
sum = hf->user2;
restart_info_clear(ctx);
switch (step) {
case 1:
case 2:
assert(ctx->stack.psize - ctx->stack.lsize >=
hf->stack_adj);
i = step - 1;
/*
* adjust the stack offset to make exec_pop_vals
* below pop the correct values.
*/
ctx->stack.lsize += hf->stack_adj;
goto after_return;
default:
assert(false);
}
assert(false);
}
sum = 0;
for (i = 0; i < 2; i++) {
/*
* Note: we know the function has the same type as
* ours. (ft)
*/
const struct funcinst *func;
host_ret = load_func(ctx, hi, ft, pp, &func);
if (host_ret != 0) {
goto fail;
}
/*
* call the function
*/
struct val a[1] = {
{
.u.i32 = pp,
},
};
host_ret = exec_push_vals(ctx, &ft->parameter, a);
if (host_ret != 0) {
goto fail;
}
/*
* set up the restart info so that the function can
* return to us.
*/
host_ret = schedule_call_from_hostfunc(ctx, restart, func);
/* save extra context */
hf = &restart->restart_u.hostfunc;
hf->user1 = i + 1; /* step */
hf->user2 = sum;
assert(host_ret == ETOYWASMRESTART);
goto fail; /* not a failure */
after_return:;
struct val r[1];
exec_pop_vals(ctx, &ft->result, r);
uint32_t v1 = r[0].u.i32;
sum += v1;
}
result = sum;
host_ret = 0;
fail:
assert(IS_RESTARTABLE(host_ret) ||
restart->restart_type == RESTART_NONE);
if (host_ret == 0) {
HOST_FUNC_RESULT_SET(ft, results, 0, i32, result);
}
HOST_FUNC_FREE_CONVERTED_PARAMS();
return host_ret;
}
static const struct host_func my_host_inst_funcs[] = {
HOST_FUNC_PREFIX(my_host_inst_, load, "(i)i"),
HOST_FUNC_PREFIX(my_host_inst_, load_call, "(i)i"),
HOST_FUNC_PREFIX(my_host_inst_, load_call_add, "(i)i"),
};
static const struct name name_my_host_inst =
NAME_FROM_CSTR_LITERAL("my-host-func");
static const struct host_module module_my_host_inst[] = {{
.module_name = &name_my_host_inst,
.funcs = my_host_inst_funcs,
.nfuncs = ARRAYCOUNT(my_host_inst_funcs),
}};
int
import_object_create_for_my_host_inst(struct mem_context *mctx, void *inst,
struct import_object **impp)
{
return import_object_create_for_host_funcs(
mctx, module_my_host_inst, ARRAYCOUNT(module_my_host_inst),
inst, impp);
}