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
|
/*
* 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 <nw/interp.h>
#include <nw/io.h>
#include <nw/log.h>
#include <nw/routines.h>
#include <string.h>
static enum nw_state get_index(struct nw_mod *);
static enum nw_state skip_bytes(struct nw_mod *const m)
{
struct nw_sm_d *const d = &m->sm.data;
const struct nw_io_cfg *const cfg = &m->cfg.io;
const long offset = d->offset + d->size;
const enum nw_state n = cfg->seek(offset, cfg->user);
if (n)
return n;
m->next = ++d->entry_i >= m->data_count ? nwp_section_exit : get_index;
return NW_AGAIN;
}
static enum nw_state tell(struct nw_mod *const m)
{
const struct nw_io_cfg *const cfg = &m->cfg.io;
struct nw_sm_d *const d = &m->sm.data;
const enum nw_state n = cfg->tell(&d->offset, cfg->user);
if (n)
return n;
m->next = skip_bytes;
return NW_AGAIN;
}
static enum nw_state get_size(struct nw_mod *const m)
{
const struct nw_io_cfg *const cfg = &m->cfg.io;
struct nw_sm_d *const d = &m->sm.data;
struct nw_sm_leb128 *const l = &d->leb128;
const enum nw_state n = nwp_varuint32(cfg, l, &d->size, cfg->user);
if (n)
return n;
else if (!d->size)
{
#ifdef NW_LOG
nwp_log("unexpected zero size for data entry %lu\n",
(unsigned long)d->entry_i);
#endif
return NW_FATAL;
}
m->next = tell;
return NW_AGAIN;
}
static int push(const void *const src, const size_t n, void *const user)
{
struct nw_mod *const m = user;
struct nw_sm_d *const d = &m->sm.data;
if (n > sizeof d->value)
{
#ifdef NW_LOG
nwp_log("stack overflow\n");
#endif
return -1;
}
memcpy(&d->value, src, n);
return n;
}
static enum nw_state loop(struct nw_mod *const m)
{
struct nw_sm_d *const d = &m->sm.data;
const enum nw_state n = nwp_interp_run(&d->interp);
if (n)
return n;
m->next = get_size;
return NW_AGAIN;
}
static enum nw_state setup_initexpr(struct nw_mod *const m)
{
struct nw_interp_cfg icfg = {0};
struct nw_sm_d *const d = &m->sm.data;
icfg.m = m;
icfg.io = m->cfg.io;
icfg.user = m;
icfg.stack.push = push;
if (nwp_interp_start(&d->interp, &icfg, &nwp_interp_data_set))
{
#ifdef NW_LOG
nwp_log("nw_interp_start failed\n");
#endif
return NW_FATAL;
}
m->next = loop;
return NW_AGAIN;
}
static enum nw_state get_index(struct nw_mod *const m)
{
const struct nw_io_cfg *const cfg = &m->cfg.io;
struct nw_sm_d *const d = &m->sm.data;
struct nw_sm_leb128 *const l = &d->leb128;
nw_varuint32 *const index = &d->index;
const enum nw_state n = nwp_varuint32(cfg, l, index, cfg->user);
if (n)
return n;
else if (*index)
{
#ifdef NW_LOG
nwp_log("expected memory index 0 in data entry %lu, got %lu\n",
(unsigned long)d->entry_i, *index);
#endif
return NW_FATAL;
}
return setup_initexpr(m);
}
static enum nw_state get_count(struct nw_mod *const m)
{
const struct nw_io_cfg *const cfg = &m->cfg.io;
const long dc = m->sections[NW_SECTION_DATA_COUNT];
struct nw_sm_d *const d = &m->sm.data;
struct nw_sm_leb128 *const l = &d->leb128;
nw_varuint32 count, *const pcount = dc ? &count : &m->data_count;
enum nw_state n;
if (!m->sections[NW_SECTION_MEMORY])
{
#ifdef NW_LOG
nwp_log("data section found before memory section\n");
#endif
return NW_FATAL;
}
else if ((n = nwp_varuint32(cfg, l, pcount, cfg->user)))
return n;
else if (dc && count != m->data_count)
{
#ifdef NW_LOG
nwp_log("data count mismatch, expected %lu, got %lu\n",
(unsigned long)m->data_count, (unsigned long)count);
#endif
return NW_FATAL;
}
m->next = m->data_count ? get_index : nwp_section_exit;
return NW_AGAIN;
}
void nwp_section_data(struct nw_mod *const m)
{
const struct nw_sm_d d = {0};
m->sm.data = d;
m->next = get_count;
}
|