aboutsummaryrefslogtreecommitdiff
path: root/src/section/code.c
blob: 7062359f7afa63b523f2c1f6c06a1bec0bb4fc5d (plain) (blame)
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
/*
 * 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 run(FILE *const f, const varuint32 idx, struct nw_interp *const in)
{
    return -1;
}

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;
}

static int push_i32(struct nw_interp *const i)
{
    const int32_t value = 0;

    return interp_stack_push(i, &value, sizeof value);
}

static int push_i64(struct nw_interp *const i)
{
    const int64_t value = 0;

    return interp_stack_push(i, &value, sizeof value);
}

static int push_f32(struct nw_interp *const i)
{
    const float value = 0;

    return interp_stack_push(i, &value, sizeof value);
}

static int push_f64(struct nw_interp *const i)
{
    const double value = 0;

    return interp_stack_push(i, &value, sizeof value);
}

static int push_locals(struct nw_interp *const in, 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;
    }

    static int (*const f[])(struct nw_interp *) =
    {
        [VALUE_TYPE_I32] = push_i32,
        [VALUE_TYPE_I64] = push_i64,
        [VALUE_TYPE_F32] = push_f32,
        [VALUE_TYPE_F64] = push_f64
    };

    for (varuint32 i = 0; i < count; i++)
        if (f[vtype](in))
        {
            LOG("%s: local variable push failed, type=%s, index=%lu\n",
                __func__, value_type_tostr(vtype), (unsigned long)i);
            return -1;
        }

    return 0;
}

static int check_local_group(FILE *const f, struct nw_interp *const i)
{
    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 (i && push_locals(i, count, type))
    {
        LOG("%s: push_locals failed\n", __func__);
        return -1;
    }

    return 0;
}

#if 0
static int run(FILE *const f, struct nw_interp *const in)
{
    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_group(f, in))
        {
            LOG("%s: check_local failed\n", __func__);
            return -1;
        }

    return 0;
}
#endif

static int push_frame(struct nw_interp *const i,
    const struct nw_frame *const src)
{
    struct nw_frame *const dst = interp_stackptr(i);

    if (interp_stack_push(i, src, sizeof (*src)))
    {
        LOG("%s: interp_stack_push failed\n", __func__);
        return -1;
    }
    else if (i->fp)
        i->fp->next = dst;

    i->fp = dst;
    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 (run(f, 0, NULL))
    {
        LOG("%s: check_local_groups 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;
}

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;
}

#if 0
static int run(FILE *const f, const varuint32 idx, struct nw_interp *const in)
{
    varuint32 count;

    if (varuint32_read(f, &count))
    {
        LOG("%s: varuint32_read failed\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;
}
#endif

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, struct nw_interp *const i)
{
#if 0
    return run(f, i);
#else
    return run(f, 0, i);
#endif
}