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
|
/*
* 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/search.h>
#include <nw/log.h>
#include <nw/fstring.h>
#include <nw/sections.h>
#include <nanowasm/nw.h>
#include <nw/types.h>
#include <errno.h>
#include <stdint.h>
#include <string.h>
static int ensure_export(const char *const fn,
const struct nw_mod *const m, FILE *const f, varuint32 *const out)
{
if (!m->sections.export)
{
LOG("%s: export section not found", __func__);
return -1;
}
else if (fseek(f, m->sections.export, SEEK_SET))
{
LOG("%s: fseek(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++)
{
varuint32 len;
bool found = false;
if (varuint32_read(f, &len))
{
LOG("%s: varuint32_read failed\n", __func__);
return -1;
}
else if (len != strlen(fn))
{
if (fseek(f, len, SEEK_CUR))
{
LOG("%s: fseek(3): %s\n", __func__, strerror(errno));
return -1;
}
}
else if (!fstrcmp(fn, f, false))
found = true;
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;
}
varuint32 index;
if (varuint32_read(f, &index))
{
LOG("%s: varuint32_read failed\n", __func__);
return -1;
}
else if (found)
{
if (kind != NW_KIND_FUNCTION)
{
LOG("%s: expected kind %d, got %d\n", __func__,
NW_KIND_FUNCTION, kind);
return -1;
}
*out = index;
return 0;
}
}
return -1;
}
static int get_function_index(const struct nw_mod *const m,
const varuint32 index, varuint32 *const findex)
{
const size_t n = m->cfg.n_imports;
if (index < n)
{
LOG("%s: index (%lu) must be greater than number of imports (%zu)\n",
__func__, (unsigned long)index, n);
return -1;
}
*findex = index - n;
return 0;
}
static int get_fn_start(const struct nw_mod *const m, FILE *const f,
const varuint32 index, struct search_fn *const out)
{
const struct section s =
{
.cfg = &m->cfg,
.f = f
};
struct section_function sf;
struct section_code c;
if (section_function(&s, m, index, &sf))
{
LOG("%s: section_function failed\n", __func__);
return -1;
}
else if (section_code(&s, m, index, &c))
{
LOG("%s: section_code failed\n", __func__);
return -1;
}
out->start = c.start;
out->index = index;
return 0;
}
int search_exported_fn(const char *const fn, const struct nw_mod *const m,
FILE *const f, struct search_fn *const out)
{
varuint32 index, findex;
if (ensure_export(fn, m, f, &index))
{
LOG("%s: %s not an exported function\n", __func__, fn);
return -1;
}
else if (get_function_index(m, index, &findex))
{
LOG("%s: get_function_index failed\n", __func__);
return -1;
}
else if (get_fn_start(m, f, findex, out))
{
LOG("%s: get_fn_start %s failed\n", __func__, fn);
return -1;
}
return 0;
}
int search_fn(const varuint32 index, const struct nw_mod *const m,
FILE *const f, struct search_fn *const out)
{
varuint32 findex;
if (get_function_index(m, index, &findex))
{
LOG("%s: get_function_index failed\n", __func__);
return -1;
}
else if (get_fn_start(m, f, findex, out))
{
LOG("%s: get_fn_start failed\n", __func__);
return -1;
}
return 0;
}
|