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
|
/*
* 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 <nw/types.h>
#include <limits.h>
#include <string.h>
static enum nw_state get_content_type(struct nw_mod *);
static int push(const void *const src, const size_t n, void *const user)
{
struct nw_mod *const m = user;
struct nw_sm_gl *const gl = &m->sm.global;
if (n > sizeof gl->value)
{
#ifdef NW_LOG
nwp_log("stack overflow\n");
#endif
return -1;
}
memcpy(&gl->value, src, n);
return n;
}
static enum nw_state loop(struct nw_mod *const m)
{
struct nw_sm_gl *const gl = &m->sm.global;
const enum nw_state n = nwp_interp_run(&gl->interp);
if (n)
return n;
m->next = ++gl->entry_i >= m->global_count ?
nwp_section_exit : get_content_type;
return NW_AGAIN;
}
static enum nw_state get_global(struct nw_mod *const m)
{
struct nw_sm_gl *const gl = &m->sm.global;
struct nw_interp_cfg cfg = {0};
cfg.m = m;
cfg.io = m->cfg.io;
cfg.user = m;
cfg.stack.push = push;
if (nwp_interp_start(&gl->interp, &cfg, &nwp_interp_initexpr_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_mutability(struct nw_mod *const m)
{
nw_varuint1 mutability;
const struct nw_io_cfg *const cfg = &m->cfg.io;
struct nw_sm_gl *const gl = &m->sm.global;
struct nw_sm_leb128 *const l = &gl->leb128;
const enum nw_state n = nwp_varuint1(cfg, l, &mutability, cfg->user);
if (n)
return n;
m->next = get_global;
return NW_AGAIN;
}
static enum nw_state get_content_type(struct nw_mod *const m)
{
nw_varint7 content_type;
const struct nw_io_cfg *const cfg = &m->cfg.io;
struct nw_sm_gl *const gl = &m->sm.global;
struct nw_sm_leb128 *const l = &gl->leb128;
const enum nw_state n = nwp_varint7(cfg, l, &content_type, cfg->user);
enum nw_type type;
/* TODO: do not ignore type */
if (n)
return n;
else if (nwp_get_type(content_type, &type))
{
#ifdef NW_LOG
nwp_log("nwp_get_type failed\n");
#endif
return NW_FATAL;
}
m->next = get_mutability;
return NW_AGAIN;
}
static enum nw_state get_count(struct nw_mod *const m)
{
const struct nw_io_cfg *const cfg = &m->cfg.io;
struct nw_sm_gl *const gl = &m->sm.global;
struct nw_sm_leb128 *const l = &gl->leb128;
enum nw_state n;
if (!m->sections[NW_SECTION_IMPORT])
{
#ifdef NW_LOG
nwp_log("global section found before import section\n");
#endif
return NW_FATAL;
}
else if ((n = nwp_varuint32(cfg, l, &m->global_count, cfg->user)))
return n;
else if (m->global_count > ULONG_MAX / sizeof (struct nw_global))
{
#ifdef NW_LOG
nwp_log("global_count too large: %lu\n",
(unsigned long)m->global_count);
#endif
return NW_FATAL;
}
m->out.global = m->global_count * sizeof (struct nw_global);
m->next = m->global_count ? get_content_type : nwp_section_exit;
return NW_AGAIN;
}
void nwp_section_global(struct nw_mod *const m)
{
const struct nw_sm_gl g = {0};
m->sm.global = g;
m->next = get_count;
}
|