-
Notifications
You must be signed in to change notification settings - Fork 8
/
wasi.c
340 lines (315 loc) · 9.83 KB
/
wasi.c
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
/*
* WASI implementation for toywasm
*
* This is a bit relaxed implementation of WASI snapshot preview1.
*
* - The "rights" stuff is not implemented. mendokusai.
* Also, it's being removed in preview2.
*
* - The "openat" family API is intentionally not used in favor
* of portability.
* Note: It makes this implementation considerably complex/incomplete
* in some places because WASI is basically a copy of the openat API
* family.
*
* - O_RESOLVE_BENEATH behavior is not implemented.
* For isolation purposes, maybe you can use a separate filesystem instead.
* See TOYWASM_ENABLE_WASI_LITTLEFS for an example.
*
* References:
* https://github.com/WebAssembly/WASI/tree/main/phases/snapshot/witx
* https://github.com/WebAssembly/wasi-libc/blob/main/libc-bottom-half/headers/public/wasi/api.h
*/
#define _DARWIN_C_SOURCE
#define _GNU_SOURCE
#define _NETBSD_SOURCE
#include <errno.h>
#include <inttypes.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "mem.h"
#include "nbio.h"
#include "wasi.h"
#include "wasi_impl.h"
#include "xlog.h"
#include "wasi_host_subr.h"
#include "wasi_hostfuncs.h"
#include "wasi_vfs_impl_host.h"
#define WASI_API(a, b) WASI_HOST_FUNC(a, b),
#define WASI_API2(a, b, c) WASI_HOST_FUNC2(a, b, c),
const struct host_func wasi_funcs[] = {
#include "wasi_preview1.h"
};
/*
* a few incompatibilities between wasi_unstable and
* wasi_snapshot_preview1:
*
* | | unstable | preview1 |
* |----------------|----------|----------|
* | SEEK_CUR | 0 | 1 |
* | SEEK_END | 1 | 2 |
* | SEEK_SET | 2 | 0 |
* | filestat nlink | 32-bit | 64-bit |
*/
const struct host_func wasi_unstable_funcs[] = {
#include "wasi_unstable.h"
};
#undef WASI_API
#undef WASI_API2
int
wasi_instance_add_hostfd(struct wasi_instance *inst, uint32_t wasmfd,
int hostfd)
{
struct wasi_fdinfo *fdinfo = NULL;
int ret;
const enum wasi_table_idx tblidx = WASI_TABLE_FILES;
toywasm_mutex_lock(&inst->lock);
ret = wasi_table_expand(inst, tblidx, wasmfd);
if (ret != 0) {
goto fail;
}
ret = wasi_table_lookup_locked(inst, tblidx, wasmfd, &fdinfo);
if (ret == 0) {
ret = EBUSY;
goto fail;
}
if (ret != EBADF) {
goto fail;
}
ret = wasi_fdinfo_alloc_host(&fdinfo);
if (ret != 0) {
goto fail;
}
/*
* we make hostfds non-blocking.
*
* XXX should restore on failure
* XXX should restore when we are done
* XXX this affects other programs sharing files.
* (eg. shell pipelines)
*
* a fragile hack:
*
* tty is often shared with other processes.
* making such files non blocking breaks other
* processes.
* eg. when you run a shell command like
* "toywasm | more", the tty is toywasm's stdin
* and also more's stdout.
*
* IMO, it's a design mistake (or at least a compromise)
* to control non-blocking with fcntl(). It should be
* a per-operation flag instead. eg. MSG_DONTWAIT.
* Unfortunately, not all operations/platforms have
* flags like that.
*/
if (!isatty(hostfd)) {
ret = set_nonblocking(hostfd, true, NULL);
if (ret != 0) {
xlog_error("set_nonblocking failed on fd %d with %d",
hostfd, ret);
goto fail;
}
}
int dupfd;
#if defined(__wasi__) /* wasi has no dup */
dupfd = hostfd;
#else
dupfd = dup(hostfd);
#endif
wasi_fdinfo_to_host(fdinfo)->hostfd = dupfd;
if (dupfd == -1) {
xlog_trace("failed to dup: wasm fd %" PRIu32
" host fd %u with errno %d",
wasmfd, hostfd, errno);
}
wasi_table_affix(inst, tblidx, wasmfd, fdinfo);
fdinfo = NULL;
ret = 0;
fail:
toywasm_mutex_unlock(&inst->lock);
wasi_fdinfo_release(inst, fdinfo);
return ret;
}
int
wasi_instance_populate_stdio_with_hostfd(struct wasi_instance *inst)
{
uint32_t nfds = 3;
uint32_t i;
int ret;
for (i = 0; i < nfds; i++) {
ret = wasi_instance_add_hostfd(inst, i, i);
if (ret != 0) {
xlog_error("wasi_instance_add_hostfd failed on fd %d "
"with %d",
i, ret);
goto fail;
}
}
ret = 0;
fail:
return ret;
}
int
wasi_instance_create(struct mem_context *mctx,
struct wasi_instance **instp) NO_THREAD_SAFETY_ANALYSIS
{
struct wasi_instance *inst;
inst = mem_zalloc(mctx, sizeof(*inst));
if (inst == NULL) {
return ENOMEM;
}
inst->mctx = mctx;
toywasm_mutex_init(&inst->lock);
toywasm_cv_init(&inst->cv);
/* the first three slots are reserved for stdin, stdout, stderr */
inst->fdtable[WASI_TABLE_FILES].reserved_slots = 3;
*instp = inst;
return 0;
}
void
wasi_instance_set_memory(struct wasi_instance *inst, struct meminst *mem)
{
wasi_memory(inst) = mem;
}
void
wasi_instance_set_args(struct wasi_instance *inst, int argc,
const char *const *argv)
{
inst->argc = argc;
inst->argv = argv;
#if defined(TOYWASM_ENABLE_TRACING)
xlog_trace("%s argc = %u", __func__, argc);
int i;
for (i = 0; i < argc; i++) {
xlog_trace("%s arg[%u] = \"%s\"", __func__, i, argv[i]);
}
#endif
}
void
wasi_instance_set_environ(struct wasi_instance *inst, int nenvs,
const char *const *envs)
{
inst->nenvs = nenvs;
inst->envs = envs;
#if defined(TOYWASM_ENABLE_TRACING)
xlog_trace("%s nenvs = %u", __func__, nenvs);
int i;
for (i = 0; i < nenvs; i++) {
xlog_trace("%s env[%u] = \"%s\"", __func__, i, envs[i]);
}
#endif
}
int
wasi_instance_prestat_add_vfs(struct wasi_instance *wasi, const char *path,
struct wasi_vfs *vfs)
{
struct wasi_fdinfo *fdinfo = NULL;
char *host_path = NULL;
char *wasm_path = NULL;
uint32_t wasifd;
int ret;
xlog_trace("prestat adding mapdir %s", path);
const char *coloncolon = strstr(path, "::");
if (coloncolon != NULL) {
/*
* <host dir>::<wasm dir>
*
* intended to be compatible with wasmtime's --dir
*
* cf.
* https://github.com/bytecodealliance/wasmtime/pull/7301
*/
host_path = strndup(path, coloncolon - path);
wasm_path = strdup(coloncolon + 2);
if (wasm_path == NULL) {
ret = ENOMEM;
goto fail;
}
} else {
host_path = strdup(path);
}
if (host_path == NULL) {
ret = ENOMEM;
goto fail;
}
ret = wasi_fdinfo_alloc_prestat(&fdinfo);
if (ret != 0) {
goto fail;
}
struct wasi_fdinfo_prestat *fdinfo_prestat =
wasi_fdinfo_to_prestat(fdinfo);
fdinfo_prestat->vfs = vfs;
fdinfo_prestat->prestat_path = host_path;
fdinfo_prestat->wasm_path = wasm_path;
host_path = NULL;
wasm_path = NULL;
ret = wasi_table_fdinfo_add(wasi, WASI_TABLE_FILES, fdinfo, &wasifd);
if (ret != 0) {
goto fail;
}
xlog_trace("prestat added %s (%s)", path,
fdinfo_prestat->prestat_path);
return 0;
fail:
free(host_path);
free(wasm_path);
wasi_fdinfo_free(fdinfo);
return ret;
}
int
wasi_instance_prestat_add(struct wasi_instance *wasi, const char *path)
{
struct wasi_vfs *vfs = wasi_get_vfs_host();
return wasi_instance_prestat_add_vfs(wasi, path, vfs);
}
uint32_t
wasi_instance_exit_code(struct wasi_instance *wasi)
{
uint32_t exit_code;
toywasm_mutex_lock(&wasi->lock);
exit_code = wasi->exit_code;
toywasm_mutex_unlock(&wasi->lock);
return exit_code;
}
void
wasi_instance_destroy(struct wasi_instance *inst)
{
unsigned int i;
for (i = 0; i < WASI_NTABLES; i++) {
wasi_table_clear(inst, i);
}
toywasm_cv_destroy(&inst->cv);
toywasm_mutex_destroy(&inst->lock);
mem_free(inst->mctx, inst, sizeof(*inst));
}
static const struct name wasi_snapshot_preview1 =
NAME_FROM_CSTR_LITERAL("wasi_snapshot_preview1");
static const struct name wasi_unstable =
NAME_FROM_CSTR_LITERAL("wasi_unstable");
static const struct host_module module_wasi[] = {
{
.module_name = &wasi_snapshot_preview1,
.funcs = wasi_funcs,
.nfuncs = ARRAYCOUNT(wasi_funcs),
},
{
.module_name = &wasi_unstable,
.funcs = wasi_unstable_funcs,
.nfuncs = ARRAYCOUNT(wasi_unstable_funcs),
},
{
.module_name = &wasi_unstable,
.funcs = wasi_funcs,
.nfuncs = ARRAYCOUNT(wasi_funcs),
},
};
int
import_object_create_for_wasi(struct mem_context *mctx,
struct wasi_instance *wasi,
struct import_object **impp)
{
return import_object_create_for_host_funcs(
mctx, module_wasi, ARRAYCOUNT(module_wasi), &wasi->hi, impp);
}