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
|
/*
* 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/sections.h>
#include <nw/fstring.h>
#include <nw/log.h>
#include <nw/types.h>
#include <errno.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
static int dump_import_name(FILE *const f, const varuint32 len)
{
for (varuint32 i = 0; i < len; i++)
{
uint8_t byte;
if (!fread(&byte, sizeof byte, 1, f))
{
LOG("%s: fread(3) failed, feof=%d, ferror=%d\n",
__func__, feof(f), ferror(f));
return -1;
}
LOG("%c", (char)byte);
}
return 0;
}
static int check_module_string(const struct nw_mod_cfg *const cfg,
FILE *const f)
{
varuint32 len;
if (varuint32_read(f, &len))
{
LOG("%s: varuint32_read failed\n", __func__);
return -1;
}
for (size_t i = 0; i < cfg->n_imports; i++)
{
const struct nw_import *const im = &cfg->imports[i];
const char *const mod = im->module;
if (strlen(mod) == len)
{
const long offset = ftell(f);
if (offset < 0)
{
LOG("%s: ftell(3): %s\n", __func__, strerror(errno));
return -1;
}
else if (!fstrcmp(mod, f, true))
return 0;
else if (fseek(f, offset, SEEK_SET))
{
LOG("%s: fseek(3): %s\n", __func__, strerror(errno));
return -1;
}
}
}
LOG("%s: required imported module ", __func__);
dump_import_name(f, len);
LOG(" not found\n");
return -1;
}
static int check_field_string(const struct nw_mod_cfg *const cfg,
FILE *const f)
{
varuint32 len;
if (varuint32_read(f, &len))
{
LOG("%s: varuint32_read failed\n", __func__);
return -1;
}
for (size_t i = 0; i < cfg->n_imports; i++)
{
const struct nw_import *const im = &cfg->imports[i];
const char *const field = im->field;
if (strlen(field) == len)
{
const long offset = ftell(f);
if (offset < 0)
{
LOG("%s: ftell(3): %s\n", __func__, strerror(errno));
return -1;
}
else if (!fstrcmp(field, f, true))
return 0;
else if (fseek(f, offset, SEEK_SET))
{
LOG("%s: fseek(3): %s\n", __func__, strerror(errno));
return -1;
}
}
}
LOG("%s: required imported field ", __func__);
dump_import_name(f, len);
LOG(" not found\n");
return -1;
}
static int check_function_kind(FILE *const f)
{
varuint32 type;
if (varuint32_read(f, &type))
{
LOG("%s: varuint32_read failed\n", __func__);
return -1;
}
return 0;
}
static int check_table_kind(FILE *const f)
{
varint7 type;
if (varint7_read(f, &type))
{
LOG("%s: varint7_read failed\n", __func__);
return -1;
}
return 0;
}
static int check_memory_kind(FILE *const f)
{
/* TODO. */
return -1;
}
static int check_global_kind(FILE *const f)
{
/* TODO. */
return -1;
}
static int check_import_entry(const struct nw_mod_cfg *const cfg,
FILE *const f)
{
if (check_module_string(cfg, f))
{
LOG("%s: check_module_string failed\n", __func__);
return -1;
}
else if (check_field_string(cfg, f))
{
LOG("%s: check_field_string failed\n", __func__);
return -1;
}
uint8_t kind;
if (!fread(&kind, sizeof kind, 1, f))
{
LOG("%s: fread(3) failed, feof=%d, ferror=%d\n",
__func__, feof(f), ferror(f));
return -1;
}
static int (*const fn[])(FILE *) =
{
[NW_KIND_FUNCTION] = check_function_kind,
[NW_KIND_TABLE] = check_table_kind,
[NW_KIND_MEMORY] = check_memory_kind,
[NW_KIND_GLOBAL] = check_global_kind
};
if (kind >= sizeof fn / sizeof *fn)
{
LOG("%s: unexpected kind %#" PRIx8 "\n", __func__, kind);
return -1;
}
return fn[kind](f);
}
static int check(const struct nw_mod_cfg *const cfg, FILE *const f,
const unsigned long len)
{
const long start = ftell(f);
if (start < 0)
{
LOG("%s: ftell(3): %s\n", __func__, strerror(errno));
return -1;
}
varuint32 count;
if (varuint32_read(f, &count))
{
LOG("%s: varuint32_read failed\n", __func__);
return -1;
}
for (varuint32 i = 0; i < count; i++)
if (check_import_entry(cfg, f))
{
LOG("%s: check_import_entry 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;
}
return 0;
}
int section_import_check(const struct section *const s, struct nw_mod *m,
const unsigned long len)
{
const struct nw_mod_cfg *const cfg = s->cfg;
FILE *const f = s->f;
if (m->sections.import)
{
LOG("%s: ignoring duplicate section\n", __func__);
return fseek(f, len, SEEK_CUR);
}
const long offset = ftell(f);
if (offset < 0)
{
LOG("%s: ftell(3): %s\n", __func__, strerror(errno));
return -1;
}
else if (check(cfg, f, len))
{
LOG("%s: check failed\n", __func__);
return -1;
}
m->sections.import = offset;
return 0;
}
|