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
|
/*
* 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 <nanowasm/nw.h>
#include <errno.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static int io_read(void *const buf, const size_t n, void *const user)
{
FILE *const f = user;
const size_t r = fread(buf, 1, 1, f);
if (!r && ferror(f))
{
fprintf(stderr, "%s: ferror\n", __func__);
return -1;
}
return r;
}
static enum nw_state io_seek(const long offset, const enum nw_whence whence,
void *const user)
{
static const int trans[] =
{
[NW_SEEK_END] = SEEK_END,
[NW_SEEK_CUR] = SEEK_CUR,
[NW_SEEK_SET] = SEEK_SET
};
if (fseek(user, offset, trans[whence]))
{
fprintf(stderr, "%s: fseek(3): %s\n", __func__, strerror(errno));
return NW_FATAL;
}
return NW_OK;
}
static enum nw_state io_tell(long *const out, void *const user)
{
const long offset = ftell(user);
if (offset < 0)
{
fprintf(stderr, "%s: ftell(3): %s\n", __func__, strerror(errno));
return NW_FATAL;
}
*out = offset;
return NW_OK;
}
static int io_eof(void *const user)
{
FILE *const f = user;
return feof(f);
}
int main(int argc, char *argv[])
{
int ret = EXIT_FAILURE;
FILE *f = NULL;
if (argc != 2)
{
fprintf(stderr, "%s <wasm>\n", *argv);
goto end;
}
const char *const path = argv[1];
if (!(f = fopen(path, "rb")))
{
fprintf(stderr, "%s: fopen(3) %s: %s\n", __func__, path,
strerror(errno));
goto end;
}
static const struct nw_import imports[] =
{
{
.kind = NW_KIND_FUNCTION,
.module = "wasi_snapshot_preview1",
.field = "proc_exit"
}
};
const struct nw_io_cfg io =
{
.read = io_read,
.seek = io_seek,
.tell = io_tell,
.eof = io_eof,
.user = f
};
const struct nw_mod_cfg cfg =
{
.imports = imports,
.n_imports = sizeof imports / sizeof *imports,
.io = io
};
struct nw_mod m;
nw_init(&m, &cfg);
again:
switch (nw_load(&m))
{
case NW_OK:
break;
case NW_AGAIN:
goto again;
case NW_FATAL:
fprintf(stderr, "%s: nw_load failed\n", __func__);
goto end;
}
const struct nw_inst_cfg icfg =
{
.interp_cfg =
{
.global = NW_BUF(1),
.heap = NW_BUF(1),
.stack = NW_BUF(128),
.io = io,
.m = &m
}
};
struct nw_inst inst;
if (nw_start(&icfg, &inst))
{
fprintf(stderr, "%s: nw_start failed\n", __func__);
goto end;
}
again2:
switch (nw_run(&inst))
{
case NW_OK:
break;
case NW_AGAIN:
goto again2;
case NW_FATAL:
fprintf(stderr, "%s: nw_run failed: %s\n", __func__,
nw_exception(&inst));
goto end;
}
ret = EXIT_SUCCESS;
end:
if (f && fclose(f))
{
fprintf(stderr, "%s: fclose(3) %s: %s\n", __func__, path,
strerror(errno));
goto end;
}
return ret;
}
|