-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwasi.c
450 lines (367 loc) · 11.5 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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
#include "./wasi.h"
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/fcntl.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#define PATH_MAX 4096
#define MAX_PREOPENS 32
typedef struct {
uint8_t pr_type;
uint32_t pr_name_len;
} prestat_t;
typedef struct {
int32_t fd;
char *path;
size_t path_len;
} preopen_entry_t;
// Global variables for storing program state
static const char **g_argv = NULL;
static wasm_size_t g_argc = 0;
static const char **g_environ = NULL;
static wasm_size_t g_environ_count = 0;
static preopen_entry_t preopens[MAX_PREOPENS];
static size_t num_preopens = 0;
void set_args(int argc, const char **argv) {
g_argv = argv;
g_argc = argc;
}
int32_t register_preopen(int32_t fd, const char *path) {
if (num_preopens >= MAX_PREOPENS) {
return ENOMEM;
}
size_t path_len = strlen(path);
char *path_copy = strdup(path);
if (!path_copy) {
return ENOMEM;
}
preopens[num_preopens].fd = fd;
preopens[num_preopens].path = path_copy;
preopens[num_preopens].path_len = path_len;
num_preopens++;
return 0;
}
// Implementation of WASI functions
int32_t fd_write(__WASM_MEMORY, int32_t fd, const iovec *iovs,
wasm_size_t iovs_len, wasm_size_t *nwritten) {
wasm_size_t total_written = 0;
for (wasm_size_t i = 0; i < iovs_len; i++) {
ssize_t written = write(fd, memory + iovs[i].buf, iovs[i].buf_len);
if (written < 0) {
return 28;
}
total_written += written;
}
*nwritten = total_written;
return 0;
}
int32_t args_get(__WASM_MEMORY, ptr_32bit_t *argv, char *argv_buf) {
if (!g_argv || !g_argc) {
return 28;
}
for (wasm_size_t i = 0; i < g_argc; i++) {
wasm_size_t len = strlen(g_argv[i]);
memcpy(argv_buf, g_argv[i], len);
argv_buf[len] = '\0';
argv[i].v = argv_buf - (char *)memory;
argv_buf += len + 1;
}
return 0;
}
int32_t args_sizes_get(wasm_size_t *argc, wasm_size_t *argv_buf_size) {
if (!g_argv || !g_argc) {
return 28;
}
*argc = g_argc;
wasm_size_t total_size = 0;
for (wasm_size_t i = 0; i < g_argc; i++) {
total_size += strlen(g_argv[i]) + 1;
}
*argv_buf_size = total_size;
return 0;
}
int32_t environ_get(__WASM_MEMORY, char **environ, char *environ_buf) {
wasm_size_t buf_offset = 0;
for (wasm_size_t i = 0; i < g_environ_count; i++) {
wasm_size_t len = strlen(g_environ[i]) + 1;
memcpy(environ_buf + buf_offset, g_environ[i], len);
environ[i] = environ_buf + buf_offset;
buf_offset += len;
}
return 0;
}
int32_t environ_sizes_get(wasm_size_t *environ_count,
wasm_size_t *environ_buf_size) {
*environ_count = g_environ_count;
wasm_size_t total_size = 0;
for (wasm_size_t i = 0; i < g_environ_count; i++) {
total_size += strlen(g_environ[i]) + 1;
}
*environ_buf_size = total_size;
return 0;
}
int32_t clock_res_get(int32_t clock_id, uint64_t *resolution) {
struct timespec res;
if (clock_getres(CLOCK_MONOTONIC, &res) != 0) {
return 28;
}
*resolution = (uint64_t)res.tv_sec * 1000000000ULL + res.tv_nsec;
return 0;
}
int32_t clock_time_get(int32_t clock_id, uint64_t precision, uint64_t *time) {
struct timespec tp;
if (clock_gettime(CLOCK_MONOTONIC, &tp) != 0) {
return 28;
}
*time = (uint64_t)tp.tv_sec * 1000000000ULL + tp.tv_nsec;
return 0;
}
int32_t fd_close(int32_t fd) {
if (close(fd) != 0) {
return 28;
}
return 0;
}
int32_t fd_fdstat_get(int32_t fd, void *stat) {
fd_fdstat_t *fdstat = (fd_fdstat_t *)stat;
struct stat native_stat;
if (fstat(fd, &native_stat) != 0) {
return 28;
}
// Convert native stat to WASI fdstat
fdstat->fs_filetype = S_ISREG(native_stat.st_mode) ? 1
: S_ISDIR(native_stat.st_mode) ? 2
: 0;
int flags = fcntl(fd, F_GETFL);
if (flags < 0) {
return 28;
}
fdstat->fs_flags = flags;
// Rights would be determined based on file type and permissions
fdstat->fs_rights_base = 0xFFFFFFFFFFFFFFFFULL; // All rights for example
fdstat->fs_rights_inheriting = 0xFFFFFFFFFFFFFFFFULL;
return 0;
}
int32_t fd_fdstat_set_flags(int32_t fd, int32_t flags) {
if (fcntl(fd, F_SETFL, flags) < 0) {
return 28;
}
return 0;
}
int32_t fd_filestat_get(int32_t fd, void *buf) {
filestat_t *filestat = (filestat_t *)buf;
struct stat native_stat;
if (fstat(fd, &native_stat) != 0) {
return 28;
}
filestat->dev = native_stat.st_dev;
filestat->ino = native_stat.st_ino;
filestat->filetype = S_ISREG(native_stat.st_mode) ? 4
: S_ISDIR(native_stat.st_mode) ? 3
: 0;
filestat->nlink = native_stat.st_nlink;
filestat->size = native_stat.st_size;
filestat->atim = native_stat.st_atime * 1000000000ULL;
filestat->mtim = native_stat.st_mtime * 1000000000ULL;
filestat->ctim = native_stat.st_ctime * 1000000000ULL;
return 0;
}
int32_t fd_prestat_get(int32_t fd, void *buf) {
prestat_t *prestat = (prestat_t *)buf;
// Search for the fd in preopens array
for (size_t i = 0; i < num_preopens; i++) {
if (preopens[i].fd == fd) {
prestat->pr_type = 0; // directory type
prestat->pr_name_len = preopens[i].path_len;
return 0;
}
}
return 8; // File descriptor not found in preopens
}
int32_t fd_prestat_dir_name(int32_t fd, char *path, wasm_size_t path_len) {
// Search for the fd in preopens array
for (size_t i = 0; i < num_preopens; i++) {
if (preopens[i].fd == fd) {
if (path_len < preopens[i].path_len) {
return ENOBUFS; // Buffer too small
}
memcpy(path, preopens[i].path, preopens[i].path_len);
return 0;
}
}
return 8; // File descriptor not found in preopens
}
int32_t fd_read(__WASM_MEMORY, int32_t fd, const iovec *iovs,
wasm_size_t iovs_len, wasm_size_t *nread) {
wasm_size_t total_read = 0;
for (wasm_size_t i = 0; i < iovs_len; i++) {
ssize_t bytes_read = read(fd, memory + iovs[i].buf, iovs[i].buf_len);
if (bytes_read < 0) {
return 28;
}
total_read += bytes_read;
if ((wasm_size_t)bytes_read < iovs[i].buf_len) {
break; // End of file reached
}
}
*nread = total_read;
return 0;
}
int32_t fd_readdir(int32_t fd, void *buf, wasm_size_t buf_len, uint64_t cookie,
wasm_size_t *bufused) {
DIR *dir = fdopendir(fd);
if (!dir) {
return 28;
}
if (cookie > 0) {
seekdir(dir, cookie);
}
wasm_size_t used = 0;
struct dirent *entry;
while ((entry = readdir(dir)) != NULL && used < buf_len) {
wasm_size_t name_len = strlen(entry->d_name);
wasm_size_t entry_size =
sizeof(uint64_t) + sizeof(uint8_t) + sizeof(uint32_t) + name_len;
if (used + entry_size > buf_len) {
break;
}
// Write dirent data to buffer
uint8_t *current = (uint8_t *)buf + used;
*(uint64_t *)current = entry->d_ino;
current += sizeof(uint64_t);
*current++ = entry->d_type;
*(uint32_t *)current = name_len;
current += sizeof(uint32_t);
memcpy(current, entry->d_name, name_len);
used += entry_size;
}
closedir(dir);
*bufused = used;
return 0;
}
int32_t fd_seek(int32_t fd, int64_t offset, int32_t whence,
uint64_t *newoffset) {
off_t new_pos = lseek(fd, offset, whence);
if (new_pos < 0) {
return 28;
}
*newoffset = new_pos;
return 0;
}
int32_t path_filestat_get(int32_t fd, int32_t flags, const char *path,
wasm_size_t path_len, void *buf) {
char path_buf[PATH_MAX];
if (path_len >= PATH_MAX) {
return ENAMETOOLONG;
}
memcpy(path_buf, path, path_len);
path_buf[path_len] = '\0';
struct stat native_stat;
if (fstatat(fd, path_buf, &native_stat,
flags & 1 ? AT_SYMLINK_NOFOLLOW : 0) != 0) {
return 28;
}
filestat_t *filestat = (filestat_t *)buf;
filestat->dev = native_stat.st_dev;
filestat->ino = native_stat.st_ino;
filestat->filetype = S_ISREG(native_stat.st_mode) ? 1
: S_ISDIR(native_stat.st_mode) ? 2
: 0;
filestat->nlink = native_stat.st_nlink;
filestat->size = native_stat.st_size;
filestat->atim = native_stat.st_atime * 1000000000ULL;
filestat->mtim = native_stat.st_mtime * 1000000000ULL;
filestat->ctim = native_stat.st_ctime * 1000000000ULL;
return 0;
}
int32_t path_open(int32_t fd, int32_t dirflags, const char *path,
wasm_size_t path_len, int32_t oflags, uint64_t fs_rights_base,
uint64_t fs_rights_inheriting, int32_t fdflags,
int32_t *opened_fd) {
char path_buf[PATH_MAX];
if (path_len >= PATH_MAX) {
return ENAMETOOLONG;
}
memcpy(path_buf, path, path_len);
path_buf[path_len] = '\0';
int flags = O_CLOEXEC; // Always set O_CLOEXEC
// Convert WASI flags to native flags
if (oflags & 1)
flags |= O_CREAT;
if (oflags & 2)
flags |= O_DIRECTORY;
if (oflags & 4)
flags |= O_EXCL;
if (oflags & 8)
flags |= O_TRUNC;
// Handle read/write flags
if ((fs_rights_base & 4) && (fs_rights_base & 2)) {
flags |= O_RDWR;
} else if (fs_rights_base & 4) {
flags |= O_RDONLY;
} else if (fs_rights_base & 2) {
flags |= O_WRONLY;
}
int new_fd = openat(fd, path_buf, flags, 0666);
if (new_fd < 0) {
return 28;
}
*opened_fd = new_fd;
return 0;
}
int32_t path_remove_directory(int32_t fd, const char *path,
wasm_size_t path_len) {
char path_buf[PATH_MAX];
if (path_len >= PATH_MAX) {
return ENAMETOOLONG;
}
memcpy(path_buf, path, path_len);
path_buf[path_len] = '\0';
if (unlinkat(fd, path_buf, AT_REMOVEDIR) != 0) {
return 28;
}
return 0;
}
int32_t path_unlink_file(int32_t fd, const char *path, wasm_size_t path_len) {
char path_buf[PATH_MAX];
if (path_len >= PATH_MAX) {
return ENAMETOOLONG;
}
memcpy(path_buf, path, path_len);
path_buf[path_len] = '\0';
if (unlinkat(fd, path_buf, 0) != 0) {
return 28;
}
return 0;
}
void proc_exit(int32_t rval) { exit(rval); }
int32_t random_get(void *buf, wasm_size_t buf_len) {
// On a real system, this should use a cryptographically secure source
// This is just an example implementation using /dev/urandom
int fd = open("/dev/urandom", O_RDONLY);
if (fd < 0) {
return 28;
}
wasm_size_t total_read = 0;
while (total_read < buf_len) {
ssize_t bytes_read =
read(fd, (char *)buf + total_read, buf_len - total_read);
if (bytes_read < 0) {
close(fd);
return 28;
}
if (bytes_read == 0) {
close(fd);
return EIO; // Unexpected EOF from random source
}
total_read += bytes_read;
}
close(fd);
return 0;
}