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
|
/*
* Copyright 2019 The Emscripten Authors. All rights reserved.
* Emscripten is available under two separate licenses, the MIT license and the
* University of Illinois/NCSA Open Source License. Both these licenses can be
* found in the LICENSE file.
*/
#define _GNU_SOURCE
#include <assert.h>
#include <errno.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <malloc.h>
#include <syscall_arch.h>
#include <time.h>
#include <unistd.h>
#include <emscripten.h>
#include <emscripten/heap.h>
#include <emscripten/console.h>
#include <wasi/api.h>
#include <wasi/wasi-helpers.h>
#include "lock.h"
#include "emscripten_internal.h"
#include "unwind.h"
/*
* WASI support code. These are compiled with the program, and call out
* using wasi APIs, which can be provided either by a wasi VM or by our
* emitted JS.
*/
// libc
void _abort_js(void) {
__builtin_trap();
/* Beyond this point should be unreachable. */
_Exit(117);
}
_Static_assert(CLOCK_REALTIME == __WASI_CLOCKID_REALTIME, "must match");
_Static_assert(CLOCK_MONOTONIC == __WASI_CLOCKID_MONOTONIC, "must match");
_Static_assert(CLOCK_PROCESS_CPUTIME_ID == __WASI_CLOCKID_PROCESS_CPUTIME_ID, "must match");
_Static_assert(CLOCK_THREAD_CPUTIME_ID == __WASI_CLOCKID_THREAD_CPUTIME_ID, "must match");
// mmap support is nonexistent. TODO: emulate simple mmaps using
// stdio + malloc, which is slow but may help some things?
// Mark these as weak so that wasmfs does not collide with it. That is, if
// wasmfs is in use, we want to use that and not this.
weak int _mmap_js(size_t length,
int prot,
int flags,
int fd,
off_t offset,
int* allocated,
void** addr) {
return -ENOSYS;
}
weak int _munmap_js(
intptr_t addr, size_t length, int prot, int flags, int fd, off_t offset) {
return -ENOSYS;
}
// open(), etc. - we just support the standard streams, with no
// corner case error checking; everything else is not permitted.
// TODO: full file support for WASI, or an option for it
// open()
weak int __syscall_openat(int dirfd, intptr_t path, int flags, ...) {
if (!strcmp((const char*)path, "/dev/stdin")) {
return STDIN_FILENO;
}
if (!strcmp((const char*)path, "/dev/stdout")) {
return STDOUT_FILENO;
}
if (!strcmp((const char*)path, "/dev/stderr")) {
return STDERR_FILENO;
}
return -EPERM;
}
weak int __syscall_ioctl(int fd, int op, ...) {
return -ENOSYS;
}
weak int __syscall_fcntl64(int fd, int cmd, ...) {
return -ENOSYS;
}
weak int __syscall_fstat64(int fd, intptr_t buf) {
return -ENOSYS;
}
weak int __syscall_stat64(intptr_t path, intptr_t buf) {
return -ENOSYS;
}
weak int __syscall_dup(int fd) {
return -ENOSYS;
}
weak int __syscall_mkdirat(int dirfd, intptr_t path, int mode) {
return -ENOSYS;
}
weak int __syscall_newfstatat(int dirfd, intptr_t path, intptr_t buf, int flags) {
return -ENOSYS;
}
weak int __syscall_lstat64(intptr_t path, intptr_t buf) {
return -ENOSYS;
}
// Emscripten additions
size_t emscripten_get_heap_max() {
// In standalone mode we don't have any wasm instructions to access the max
// memory size so the best we can do (without calling an import) is return
// the current heap size.
return emscripten_get_heap_size();
}
int emscripten_resize_heap(size_t size) {
#if defined(EMSCRIPTEN_MEMORY_GROWTH)
size_t old_size = __builtin_wasm_memory_size(0) * WASM_PAGE_SIZE;
assert(old_size < size);
ssize_t diff = (size - old_size + WASM_PAGE_SIZE - 1) / WASM_PAGE_SIZE;
size_t result = __builtin_wasm_memory_grow(0, diff);
if (result != (size_t)-1) {
#if !defined(EMSCRIPTEN_PURE_WASI)
// Success, update JS (see https://github.com/WebAssembly/WASI/issues/82)
emscripten_notify_memory_growth(0);
#endif
return 1;
}
#endif
return 0;
}
// Call clock_gettime with a particular clock and return the result in ms.
static double clock_gettime_ms(clockid_t clock) {
struct timespec ts;
if (clock_gettime(clock, &ts)) {
return 0;
}
return (double)ts.tv_sec * 1000 + (double)ts.tv_nsec / 1000000;
}
weak double emscripten_get_now(void) {
return clock_gettime_ms(CLOCK_MONOTONIC);
}
weak double emscripten_date_now(void) {
return clock_gettime_ms(CLOCK_REALTIME);
}
// C++ ABI
#if EMSCRIPTEN_NOCATCH
// When exception catching is disabled, we stub out calls to `__cxa_throw`.
// Otherwise, `__cxa_throw` will be imported from the host.
void __cxa_throw(void* ptr, void* type, void* destructor) {
abort();
}
#endif
// WasmFS integration. We stub out file preloading and such, that are not
// expected to work anyhow.
size_t _wasmfs_get_num_preloaded_files() { return 0; }
size_t _wasmfs_get_num_preloaded_dirs() { return 0; }
int _wasmfs_get_preloaded_file_size(int index) { return 0; }
int _wasmfs_get_preloaded_file_mode(int index) { return 0; }
void _wasmfs_copy_preloaded_file_data(int index, void* buffer) {}
void _wasmfs_get_preloaded_parent_path(int index, void* buffer) {}
void _wasmfs_get_preloaded_child_path(int index, void* buffer) {}
void _wasmfs_get_preloaded_path_name(int index, void* buffer) {}
// Import the VM's fd_write under a different name. Then we can interpose in
// between it and WasmFS's fd_write. That is, libc calls fd_write, which WasmFS
// implements. And WasmFS will forward actual writing to stdout/stderr to the
// VM's fd_write. (This allows WasmFS to do work in the middle, for example, it
// could support embedded files and other functionality.)
__attribute__((import_module("wasi_snapshot_preview1"),
import_name("fd_write"))) __wasi_errno_t
imported__wasi_fd_write(__wasi_fd_t fd,
const __wasi_ciovec_t* iovs,
size_t iovs_len,
__wasi_size_t* nwritten);
// Write a buffer + a newline.
static void wasi_writeln_n(__wasi_fd_t fd, const char* buffer, size_t len) {
struct __wasi_ciovec_t iovs[2];
iovs[0].buf = (uint8_t*)buffer;
iovs[0].buf_len = len;
iovs[1].buf = (uint8_t*)"\n";
iovs[1].buf_len = 1;
__wasi_size_t nwritten;
imported__wasi_fd_write(fd, iovs, 2, &nwritten);
}
static void wasi_writeln(__wasi_fd_t fd, const char* buffer) {
return wasi_writeln_n(fd, buffer, strlen(buffer));
}
weak void emscripten_out(const char* text) { wasi_writeln(1, text); }
weak void emscripten_err(const char* text) { wasi_writeln(2, text); }
weak void emscripten_dbg(const char* text) { wasi_writeln(2, text); }
weak void emscripten_outn(const char* text, size_t len) {
wasi_writeln_n(1, text, len);
}
weak void emscripten_errn(const char* text, size_t len) {
wasi_writeln_n(2, text, len);
}
weak void emscripten_dbgn(const char* text, size_t len) {
wasi_writeln_n(2, text, len);
}
__attribute__((import_module("wasi_snapshot_preview1"),
import_name("fd_read"))) __wasi_errno_t
imported__wasi_fd_read(__wasi_fd_t fd,
const __wasi_ciovec_t* iovs,
size_t iovs_len,
__wasi_size_t* nread);
int _wasmfs_stdin_get_char(void) {
char c;
struct __wasi_ciovec_t iov;
iov.buf = (uint8_t*)&c;
iov.buf_len = 1;
__wasi_size_t nread;
imported__wasi_fd_read(0, &iov, 1, &nread);
if (nread == 0) {
return -1;
}
return c;
}
// In the non-standalone build we define this helper function in JS to avoid
// signture mismatch issues.
// See: https://github.com/emscripten-core/posixtestsuite/issues/6
void __call_sighandler(sighandler_t handler, int sig) {
handler(sig);
}
int _setitimer_js(int which, double timeout) {
// There is no API to let us set timers in standalone mode atm. Return an
// error.
errno = ENOTSUP;
return -1;
}
weak uintptr_t emscripten_stack_snapshot(void) {
return 0;
}
weak uint32_t emscripten_stack_unwind_buffer(uintptr_t pc,
uintptr_t* buffer,
uint32_t depth) {
return 0;
}
weak const char* emscripten_pc_get_function(uintptr_t pc) {
return NULL;
}
weak const char* emscripten_pc_get_file(uintptr_t pc) {
return NULL;
}
weak int emscripten_pc_get_line(uintptr_t pc) {
return 0;
}
weak int emscripten_pc_get_column(uintptr_t pc) {
return 0;
}
weak void* emscripten_return_address(int level) {
return NULL;
}
weak int _emscripten_sanitizer_use_colors(void) {
return 1;
}
weak char* _emscripten_sanitizer_get_option(const char* name) {
return strdup("");
}
weak void _emscripten_get_progname(char* buf, int length) {
strncpy(buf, "<unknown>", length);
}
weak void _emscripten_runtime_keepalive_clear() {}
void __throw_exception_with_stack_trace(_Unwind_Exception* exception_object) {
_Unwind_RaiseException(exception_object);
}
|