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
|
/*
* nanowasm, a tiny WebAssembly/Wasm interpreter
* Copyright (C) 2023-2025 Xavier Del Campo Romero
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#include <nanowasm/nw.h>
#include <nanowasm/types.h>
#include <nw/interp.h>
#include <nw/io.h>
#include <nw/log.h>
#include <nw/routines.h>
#include <nw/stack.h>
#include <nw/types.h>
static enum nw_state get_param_type(struct nw_interp *);
static enum nw_state seek_pc(struct nw_interp *const i)
{
const struct nw_i_sm_type *const t = &i->sm.type;
const struct nw_io_cfg *const cfg = &i->cfg.io;
const enum nw_state n = cfg->seek(t->pc, cfg->user);
if (n)
return n;
i->next = t->next;
return NW_AGAIN;
}
static enum nw_state get_return_type(struct nw_interp *const i)
{
nw_varint7 type;
struct nw_i_sm_type *const t = &i->sm.type;
const struct nw_io_cfg *const cfg = &i->cfg.io;
const enum nw_state n = nwp_varint7(cfg, &t->leb128, &type, cfg->user);
if (n)
return n;
else if (nwp_get_type(type, &t->out.ret.type))
{
static const char *const exc = "invalid type";
i->exception = exc;
#ifdef NW_LOG
nwp_log("%s: %#x\n", exc, (unsigned)type);
#endif
return NW_FATAL;
}
i->next = seek_pc;
return NW_AGAIN;
}
static enum nw_state get_return_count(struct nw_interp *const i)
{
const struct nw_io_cfg *const cfg = &i->cfg.io;
struct nw_i_sm_type *const t = &i->sm.type;
struct nw_sm_leb128 *const l = &t->leb128;
nw_varuint1 *const return_count = &t->out.ret.count;
const enum nw_state n = nwp_varuint1(cfg, l, return_count, cfg->user);
if (n)
return n;
else if (*return_count)
i->next = get_return_type;
else
t->next(i);
return NW_AGAIN;
}
static enum nw_state get_param_type(struct nw_interp *const i)
{
const struct nw_io_cfg *const cfg = &i->cfg.io;
struct nw_i_sm_type *const t = &i->sm.type;
nw_varint7 type;
enum nw_type param_type;
const enum nw_state n = nwp_varint7(cfg, &t->leb128, &type, cfg->user);
if (n)
return n;
else if (nwp_get_type(type, ¶m_type))
{
i->exception = "invalid type";
return NW_FATAL;
}
else if (++t->param_i >= t->out.param_count)
i->next = get_return_count;
return NW_AGAIN;
}
static enum nw_state tell_param_types(struct nw_interp *const i)
{
struct nw_i_sm_type *const t = &i->sm.type;
const struct nw_io_cfg *const cfg = &i->cfg.io;
const enum nw_state n = cfg->tell(&t->out.param_types, cfg->user);
if (n)
return n;
i->next = get_param_type;
return NW_AGAIN;
}
static enum nw_state get_param_count(struct nw_interp *const i)
{
const struct nw_io_cfg *const cfg = &i->cfg.io;
struct nw_i_sm_type *const t = &i->sm.type;
struct nw_sm_leb128 *const l = &t->leb128;
nw_varuint32 *const count = &t->out.param_count;
const enum nw_state n = nwp_varuint32(cfg, l, count, cfg->user);
if (n)
return n;
i->next = *count ? tell_param_types : get_return_count;
return NW_AGAIN;
}
static enum nw_state get_form(struct nw_interp *const i)
{
const struct nw_io_cfg *const cfg = &i->cfg.io;
struct nw_i_sm_type *const t = &i->sm.type;
struct nw_sm_leb128 *const l = &t->leb128;
nw_varint7 form;
const enum nw_state n = nwp_varint7(cfg, l, &form, cfg->user);
if (n)
return n;
else if (form != 0x60)
{
static const char *const exc = "type index not a function";
i->exception = exc;
#ifdef NW_LOG
nwp_log("%s: %#x\n", exc, (unsigned)form);
#endif
return NW_FATAL;
}
i->next = get_param_count;
return NW_AGAIN;
}
static enum nw_state seek_type(struct nw_interp *const i)
{
const struct nw_io_cfg *const cfg = &i->cfg.io;
const struct nw_i_sm_type *const t = &i->sm.type;
const unsigned long to = nwp_leuint32(&t->to);
const enum nw_state n = cfg->seek(to, cfg->user);
if (n)
return n;
i->next = get_form;
return NW_AGAIN;
}
static enum nw_state get_to(struct nw_interp *const i)
{
const struct nw_io_cfg *const cfg = &i->cfg.io;
struct nw_i_sm_type *const t = &i->sm.type;
const enum nw_state n = nwp_io_read(cfg, &t->io, cfg->user);
if (n)
return n;
i->next = seek_type;
return NW_AGAIN;
}
static enum nw_state seek_to(struct nw_interp *const i)
{
const struct nw_interp_cfg *const icfg = &i->cfg;
struct nw_i_sm_type *const t = &i->sm.type;
const struct nw_mod *const m = icfg->m;
long offset = m->c_sections[NW_CUSTOM_TO];
const unsigned long fti = nwp_leuint32(&t->fti);
const struct nw_io_cfg *const cfg = &icfg->io;
enum nw_state n;
if (fti >= m->type_count)
{
static const char *const exc = "invalid type index";
i->exception = exc;
#ifdef NW_LOG
nwp_log("%s: %lu\n", exc, fti);
#endif
return NW_FATAL;
}
else if (!offset)
{
static const char *const exc = "nw_to section not found";
i->exception = exc;
#ifdef NW_LOG
nwp_log("%s\n", exc);
#endif
return NW_FATAL;
}
offset += sizeof t->to * fti;
if ((n = cfg->seek(offset, cfg->user)))
return n;
else
{
struct nw_sm_io io = {0};
io.buf = &t->to;
io.n = sizeof t->to;
t->io = io;
i->next = get_to;
}
return NW_AGAIN;
}
static enum nw_state get_fti(struct nw_interp *const i)
{
const struct nw_io_cfg *const cfg = &i->cfg.io;
struct nw_i_sm_type *const t = &i->sm.type;
const enum nw_state n = nwp_io_read(cfg, &t->io, cfg->user);
if (n)
return n;
i->next = seek_to;
return NW_AGAIN;
}
static enum nw_state seek_fti(struct nw_interp *const i)
{
const struct nw_interp_cfg *const icfg = &i->cfg;
const struct nw_io_cfg *const cfg = &icfg->io;
struct nw_i_sm_type *const t = &i->sm.type;
long offset = icfg->m->c_sections[NW_CUSTOM_FTI];
enum nw_state n;
if (!offset)
{
static const char *const exc = "nw_fti section not found";
#ifdef NW_LOG
nwp_log("%s: %s\n", exc, "function type index");
#endif
i->exception = exc;
return NW_FATAL;
}
offset += sizeof t->fti * t->fn_index;
if ((n = cfg->seek(offset, cfg->user)))
return n;
else
{
struct nw_sm_io io = {0};
io.buf = &t->fti;
io.n = sizeof t->fti;
t->io = io;
i->next = get_fti;
}
return NW_AGAIN;
}
static enum nw_state get_index(struct nw_interp *const i)
{
struct nw_i_sm_type *const t = &i->sm.type;
const struct nw_get_import_type_out *const out = &t->git.out;
struct nw_sm_leb128 *const l = &t->leb128;
nw_varuint32 index;
const struct nw_io_cfg *const cfg = &i->cfg.io;
enum nw_state n;
if (out->kind != NW_KIND_FUNCTION)
{
static const char *const exc = "import type not a function";
i->exception = exc;
#ifdef NW_LOG
nwp_log("%s: %#x\n", exc, (unsigned)out->kind);
#endif
return NW_FATAL;
}
else if ((n = nwp_varuint32(cfg, l, &index, cfg->user)))
return n;
nwp_toleuint32(index, &t->fti);
i->next = seek_to;
return NW_AGAIN;
}
static enum nw_state tell(struct nw_interp *const i)
{
struct nw_i_sm_type *const t = &i->sm.type;
const struct nw_io_cfg *const cfg = &i->cfg.io;
const nw_varuint32 icnt = i->cfg.m->import_count, index = t->out.index;
const enum nw_state n = cfg->tell(&t->pc, cfg->user);
if (n)
return n;
else if (index >= icnt)
{
t->fn_index = index - icnt;
i->next = seek_fti;
}
else
nwp_get_import_type(i, &t->git, index, get_index);
return NW_AGAIN;
}
void nwp_get_function_type(struct nw_interp *const i, const nw_varuint32 index,
enum nw_state (*const next)(struct nw_interp *))
{
const struct nw_i_sm_type t = {0};
struct nw_i_sm_type *const pt = &i->sm.type;
*pt = t;
pt->out.index = index;
pt->next = next;
i->next = tell;
}
|