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
|
/*
* nanowasm, a tiny WebAssembly/Wasm interpreter
* Copyright (C) 2023-2024 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 <nw/log.h>
#include <nanowasm/nw.h>
#include <nw/opcodes.h>
#include <nw/interp.h>
#include <nw/sections.h>
#include <nw/types.h>
#include <errno.h>
#include <inttypes.h>
#include <limits.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
static int check_body(FILE *const f, const unsigned long n)
{
unsigned long rem = n;
while (rem)
{
const long before = ftell(f);
uint8_t byte;
if (before < 0)
{
LOG("%s: ftell(3) before: %s\n", __func__, strerror(errno));
return -1;
}
else if (!fread(&byte, sizeof byte, 1, f))
{
LOG("%s: fread(3) failed, feof=%d, ferror=%d\n",
__func__, feof(f), ferror(f));
return -1;
}
else if (rem == 1 && byte != OP_END)
{
LOG("%s: unexpected opcode %#" PRIx8 " at body end\n",
__func__, byte);
return -1;
}
else if (interp_check_opcode(byte, f))
{
LOG("%s: interp_check_opcode failed\n", __func__);
return -1;
}
const long after = ftell(f);
if (after < 0)
{
LOG("%s: ftell(3) after: %s\n", __func__, strerror(errno));
return -1;
}
rem -= after - before;
}
return 0;
}
#if 0
static int push_locals(struct nw_interp *const i,
const struct nw_frame *const f)
{
const enum value_type t = f->local_type;
const size_t typesz = get_type_size(t);
const unsigned long n = f->n_locals;
if (mul_overflow(typesz, f->n_locals))
{
LOG("%s: local variables size mul overflow", __func__);
i->exception = "mul overfllow";
return -1;
}
const size_t totalsz = typesz * n, max = i->cfg.stack.n;
if (totalsz > max || i->stack_i > max - totalsz)
{
LOG("%s: cannot allocate locals\n", __func__);
i->exception = "stack overflow";
return 1;
}
init_locals(t, n, interp_stackptr(i));
i->stack_i += totalsz;
return 0;
}
static int push_block(struct nw_interp *const i)
{
const long pc = ftell(i->f);
if (pc < 0)
{
LOG("%s: ftell(3): %s\n", __func__, strerror(errno));
return -1;
}
const struct nw_block b =
{
.pc = pc
};
if (interp_stack_push(i, &b, sizeof b))
{
LOG("%s: interp_stack_push failed\n", __func__);
return -1;
}
struct nw_block *const p = (struct nw_block *)interp_stackptr(i) - 1;
if (i->fp->block)
{
for (struct nw_block *b = i->fp->block; b; b = b->next)
if (!b->next)
{
b->next = p;
break;
}
}
else
i->fp->block = p;
return 0;
}
static int loop_push_blocks(struct nw_interp *const i)
{
for (;;)
{
uint8_t op;
if (!fread(&op, sizeof op, 1, i->f))
{
LOG("%s: fread(3) failed, feof=%d, ferror=%d\n", __func__,
feof(i->f), ferror(i->f));
i->exception = "I/O error";
return -1;
}
else if (op >= sizeof ops / sizeof *ops)
{
LOG("%s: invalid opcode %#" PRIx8 "\n", __func__, op);
i->exception = "invalid opcode";
return -1;
}
else if (interp_check_opcode(op, i->f))
{
LOG("%s: interp_check_opcode failed\n", __func__);
return -1;
}
switch (op)
{
case OP_END:
return 0;
case OP_BLOCK:
if (push_block(i))
{
LOG("%s: push_block failed\n", __func__);
return -1;
}
break;
default:
break;
}
}
}
static int push_blocks(struct nw_interp *const i,
const struct nw_frame *const fr)
{
FILE *const f = i->f;
const long orig = ftell(f);
if (orig < 0)
{
LOG("%s: ftell(3): %s\n", __func__, strerror(errno));
return -1;
}
else if (loop_push_blocks(i))
{
LOG("%s: loop_push_blocks failed\n", __func__);
return -1;
}
else if (fseek(f, orig, SEEK_SET))
{
LOG("%s: fseek(3): %s\n", __func__, strerror(errno));
return -1;
}
return 0;
}
#endif
static int push_local_count(struct nw_frame *const f, const varuint32 count,
const varint7 type)
{
enum value_type vtype;
if (get_value_type(type, &vtype))
{
LOG("%s: get_value_type failed\n", __func__);
return -1;
}
f->n_locals = count;
f->local_type = vtype;
return 0;
}
static int check_local(FILE *const f, struct nw_frame *const fr)
{
varuint32 count;
varint7 type;
if (varuint32_read(f, &count))
{
LOG("%s: varuint32_read failed\n", __func__);
return -1;
}
else if (varint7_read(f, &type))
{
LOG("%s: varint7_read failed\n", __func__);
return -1;
}
else if (fr && push_local_count(fr, count, type))
{
LOG("%s: push_local_count failed\n", __func__);
return -1;
}
return 0;
}
static int check_locals(FILE *const f, struct nw_frame *const fr)
{
varuint32 local_count;
if (varuint32_read(f, &local_count))
{
LOG("%s: varuint32_read local_count failed\n", __func__);
return -1;
}
for (varuint32 i = 0; i < local_count; i++)
if (check_local(f, fr))
{
LOG("%s: check_local failed\n", __func__);
return -1;
}
return 0;
}
static int check_function_body(FILE *const f, long *const out)
{
varuint32 body_size;
if (varuint32_read(f, &body_size))
{
LOG("%s: varuint32_read body_size failed\n", __func__);
return -1;
}
const long start = ftell(f);
if (start < 0)
{
LOG("%s: ftell(3) start: %s\n", __func__, strerror(errno));
return -1;
}
else if (check_locals(f, NULL))
{
LOG("%s: check_locals failed\n", __func__);
return -1;
}
const long end = ftell(f);
if (end < 0)
{
LOG("%s: ftell(3) end: %s\n", __func__, strerror(errno));
return -1;
}
const unsigned long consumed = end - start;
if (consumed > body_size)
{
LOG("%s: exceeded function body size\n", __func__);
return -1;
}
else if (check_body(f, body_size - consumed))
{
LOG("%s: check_body failed\n", __func__);
return -1;
}
*out = start;
return 0;
}
static int run(FILE *const f, const varuint32 idx,
struct section_code *const out)
{
varuint32 count;
if (varuint32_read(f, &count))
{
LOG("%s: varuint32_read failed\n", __func__);
return -1;
}
else if (count > ULONG_MAX - 1)
{
fprintf(stderr, "%s: count overflow\n", __func__);
return -1;
}
for (varuint32 i = 0; i < count; i++)
{
long start;
if (check_function_body(f, &start))
{
LOG("%s: check_function_body failed\n", __func__);
return -1;
}
else if (out && i == idx)
{
out->start = start;
return 0;
}
}
return 0;
}
int section_code_check(const struct section *const s, struct nw_mod *const m,
const unsigned long len)
{
FILE *const f = s->f;
if (m->sections.code)
{
LOG("%s: ignoring duplicate section\n", __func__);
return fseek(f, len, SEEK_CUR);
}
const long start = ftell(f);
if (start < 0)
{
LOG("%s: ftell(3): %s\n", __func__, strerror(errno));
return -1;
}
else if (run(f, 0, NULL))
{
LOG("%s: run failed\n", __func__);
return -1;
}
const long end = ftell(f);
if (end < 0)
{
LOG("%s: ftell(3): %s\n", __func__, strerror(errno));
return -1;
}
const unsigned long size = end - start;
if (size != len)
{
LOG("%s: size exceeded (%lu expected, got %lu)\n",
__func__, len, size);
return -1;
}
m->sections.code = start;
return 0;
}
int section_code(const struct section *const s, const struct nw_mod *const m,
const varuint32 idx, struct section_code *const out)
{
const long offset = m->sections.code;
if (offset <= 0)
{
LOG("%s: code section not found", __func__);
return -1;
}
else if (fseek(s->f, offset, SEEK_SET))
{
LOG("%s: fseek(3): %s\n", __func__, strerror(errno));
return -1;
}
return run(s->f, idx, out);
}
int section_code_push(FILE *const f, const long pc, struct nw_frame *const fr)
{
if (fseek(f, pc, SEEK_SET))
{
LOG("%s: fseek(3): %s\n", __func__, strerror(errno));
return -1;
}
else if (check_locals(f, fr))
{
LOG("%s: check_local failed\n", __func__);
return -1;
}
return 0;
}
|