-
Notifications
You must be signed in to change notification settings - Fork 786
Expand file tree
/
Copy pathposix.c
More file actions
3445 lines (2985 loc) · 109 KB
/
posix.c
File metadata and controls
3445 lines (2985 loc) · 109 KB
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Part of the Wasmtime Project, under the Apache License v2.0 with LLVM
// Exceptions. See
// https://github.com/bytecodealliance/wasmtime/blob/main/LICENSE for license
// information.
//
// Significant parts of this file are derived from cloudabi-utils. See
// https://github.com/bytecodealliance/wasmtime/blob/main/lib/wasi/sandboxed-system-primitives/src/LICENSE
// for license information.
//
// The upstream file contains the following copyright notice:
//
// Copyright (c) 2016-2018 Nuxi, https://nuxi.nl/
#include "ssp_config.h"
#include "bh_platform.h"
#include "blocking_op.h"
#include "wasmtime_ssp.h"
#include "libc_errno.h"
#include "locking.h"
#include "posix.h"
#include "random.h"
#include "refcount.h"
#include "rights.h"
#include "str.h"
/* Some platforms (e.g. Windows) already define `min()` macro.
We're undefing it here to make sure the `min` call does exactly
what we want it to do. */
#ifdef min
#undef min
#endif
static inline size_t
min(size_t a, size_t b)
{
return a > b ? b : a;
}
#if 0 /* TODO: -std=gnu99 causes compile error, comment them first */
// struct iovec must have the same layout as __wasi_iovec_t.
static_assert(offsetof(struct iovec, iov_base) ==
offsetof(__wasi_iovec_t, buf),
"Offset mismatch");
static_assert(sizeof(((struct iovec *)0)->iov_base) ==
sizeof(((__wasi_iovec_t *)0)->buf),
"Size mismatch");
static_assert(offsetof(struct iovec, iov_len) ==
offsetof(__wasi_iovec_t, buf_len),
"Offset mismatch");
static_assert(sizeof(((struct iovec *)0)->iov_len) ==
sizeof(((__wasi_iovec_t *)0)->buf_len),
"Size mismatch");
static_assert(sizeof(struct iovec) == sizeof(__wasi_iovec_t),
"Size mismatch");
// struct iovec must have the same layout as __wasi_ciovec_t.
static_assert(offsetof(struct iovec, iov_base) ==
offsetof(__wasi_ciovec_t, buf),
"Offset mismatch");
static_assert(sizeof(((struct iovec *)0)->iov_base) ==
sizeof(((__wasi_ciovec_t *)0)->buf),
"Size mismatch");
static_assert(offsetof(struct iovec, iov_len) ==
offsetof(__wasi_ciovec_t, buf_len),
"Offset mismatch");
static_assert(sizeof(((struct iovec *)0)->iov_len) ==
sizeof(((__wasi_ciovec_t *)0)->buf_len),
"Size mismatch");
static_assert(sizeof(struct iovec) == sizeof(__wasi_ciovec_t),
"Size mismatch");
#endif
static bool
ns_lookup_list_search(char **list, const char *host)
{
size_t host_len = strlen(host), suffix_len;
while (*list) {
if (*list[0] == '*') {
suffix_len = strlen(*list) - 1;
if (suffix_len <= host_len
&& strncmp(host + host_len - suffix_len, *list + 1, suffix_len)
== 0) {
return true;
}
}
else {
if (strcmp(*list, host) == 0) {
return true;
}
}
list++;
}
return false;
}
#if !defined(BH_PLATFORM_WINDOWS) && CONFIG_HAS_CLOCK_NANOSLEEP
static bool
wasi_clockid_to_clockid(__wasi_clockid_t in, clockid_t *out)
{
switch (in) {
case __WASI_CLOCK_MONOTONIC:
*out = CLOCK_MONOTONIC;
return true;
#if defined(CLOCK_PROCESS_CPUTIME_ID)
case __WASI_CLOCK_PROCESS_CPUTIME_ID:
*out = CLOCK_PROCESS_CPUTIME_ID;
return true;
#endif
case __WASI_CLOCK_REALTIME:
*out = CLOCK_REALTIME;
return true;
#if defined(CLOCK_THREAD_CPUTIME_ID)
case __WASI_CLOCK_THREAD_CPUTIME_ID:
*out = CLOCK_THREAD_CPUTIME_ID;
return true;
#endif
default:
return false;
}
}
#endif
static void
wasi_addr_to_bh_sockaddr(const __wasi_addr_t *wasi_addr,
bh_sockaddr_t *sockaddr)
{
if (wasi_addr->kind == IPv4) {
sockaddr->addr_buffer.ipv4 = (wasi_addr->addr.ip4.addr.n0 << 24)
| (wasi_addr->addr.ip4.addr.n1 << 16)
| (wasi_addr->addr.ip4.addr.n2 << 8)
| wasi_addr->addr.ip4.addr.n3;
sockaddr->is_ipv4 = true;
sockaddr->port = wasi_addr->addr.ip4.port;
}
else {
sockaddr->addr_buffer.ipv6[0] = wasi_addr->addr.ip6.addr.n0;
sockaddr->addr_buffer.ipv6[1] = wasi_addr->addr.ip6.addr.n1;
sockaddr->addr_buffer.ipv6[2] = wasi_addr->addr.ip6.addr.n2;
sockaddr->addr_buffer.ipv6[3] = wasi_addr->addr.ip6.addr.n3;
sockaddr->addr_buffer.ipv6[4] = wasi_addr->addr.ip6.addr.h0;
sockaddr->addr_buffer.ipv6[5] = wasi_addr->addr.ip6.addr.h1;
sockaddr->addr_buffer.ipv6[6] = wasi_addr->addr.ip6.addr.h2;
sockaddr->addr_buffer.ipv6[7] = wasi_addr->addr.ip6.addr.h3;
sockaddr->is_ipv4 = false;
sockaddr->port = wasi_addr->addr.ip6.port;
}
}
// Converts an IPv6 binary address object to WASI address object.
static void
bh_sockaddr_to_wasi_addr(const bh_sockaddr_t *sockaddr,
__wasi_addr_t *wasi_addr)
{
if (sockaddr->is_ipv4) {
wasi_addr->kind = IPv4;
wasi_addr->addr.ip4.port = sockaddr->port;
wasi_addr->addr.ip4.addr.n0 =
(sockaddr->addr_buffer.ipv4 & 0xFF000000) >> 24;
wasi_addr->addr.ip4.addr.n1 =
(sockaddr->addr_buffer.ipv4 & 0x00FF0000) >> 16;
wasi_addr->addr.ip4.addr.n2 =
(sockaddr->addr_buffer.ipv4 & 0x0000FF00) >> 8;
wasi_addr->addr.ip4.addr.n3 = (sockaddr->addr_buffer.ipv4 & 0x000000FF);
}
else {
wasi_addr->kind = IPv6;
wasi_addr->addr.ip6.port = sockaddr->port;
wasi_addr->addr.ip6.addr.n0 = sockaddr->addr_buffer.ipv6[0];
wasi_addr->addr.ip6.addr.n1 = sockaddr->addr_buffer.ipv6[1];
wasi_addr->addr.ip6.addr.n2 = sockaddr->addr_buffer.ipv6[2];
wasi_addr->addr.ip6.addr.n3 = sockaddr->addr_buffer.ipv6[3];
wasi_addr->addr.ip6.addr.h0 = sockaddr->addr_buffer.ipv6[4];
wasi_addr->addr.ip6.addr.h1 = sockaddr->addr_buffer.ipv6[5];
wasi_addr->addr.ip6.addr.h2 = sockaddr->addr_buffer.ipv6[6];
wasi_addr->addr.ip6.addr.h3 = sockaddr->addr_buffer.ipv6[7];
}
}
static void
wasi_addr_ip_to_bh_ip_addr_buffer(__wasi_addr_ip_t *addr,
bh_ip_addr_buffer_t *out)
{
if (addr->kind == IPv4) {
out->ipv4 = htonl((addr->addr.ip4.n0 << 24) | (addr->addr.ip4.n1 << 16)
| (addr->addr.ip4.n2 << 8) | addr->addr.ip4.n3);
}
else {
out->ipv6[0] = htons(addr->addr.ip6.n0);
out->ipv6[1] = htons(addr->addr.ip6.n1);
out->ipv6[2] = htons(addr->addr.ip6.n2);
out->ipv6[3] = htons(addr->addr.ip6.n3);
out->ipv6[4] = htons(addr->addr.ip6.h0);
out->ipv6[5] = htons(addr->addr.ip6.h1);
out->ipv6[6] = htons(addr->addr.ip6.h2);
out->ipv6[7] = htons(addr->addr.ip6.h3);
}
}
struct fd_prestat {
const char *dir;
};
bool
fd_prestats_init(struct fd_prestats *pt)
{
if (!rwlock_initialize(&pt->lock))
return false;
pt->prestats = NULL;
pt->size = 0;
pt->used = 0;
return true;
}
// Grows the preopened resource table to a required lower bound and a
// minimum number of free preopened resource table entries.
static __wasi_errno_t
fd_prestats_grow(struct fd_prestats *pt, size_t min, size_t incr)
REQUIRES_EXCLUSIVE(pt->lock)
{
if (pt->size <= min || pt->size < (pt->used + incr) * 2) {
// Keep on doubling the table size until we've met our constraints.
size_t size = pt->size == 0 ? 1 : pt->size;
while (size <= min || size < (pt->used + incr) * 2)
size *= 2;
// Grow the file descriptor table's allocation.
struct fd_prestat *prestats =
wasm_runtime_malloc((uint32)(sizeof(*prestats) * size));
if (prestats == NULL)
return __WASI_ENOMEM;
if (pt->prestats && pt->size > 0) {
bh_memcpy_s(prestats, (uint32)(sizeof(*prestats) * size),
pt->prestats, (uint32)(sizeof(*prestats) * pt->size));
}
if (pt->prestats)
wasm_runtime_free(pt->prestats);
// Mark all new file descriptors as unused.
for (size_t i = pt->size; i < size; ++i)
prestats[i].dir = NULL;
pt->prestats = prestats;
pt->size = size;
}
return __WASI_ESUCCESS;
}
static __wasi_errno_t
fd_prestats_insert_locked(struct fd_prestats *pt, const char *dir,
__wasi_fd_t fd)
{
// Grow the preopened resource table if needed.
__wasi_errno_t error = fd_prestats_grow(pt, fd, 1);
if (error != __WASI_ESUCCESS) {
return error;
}
pt->prestats[fd].dir = bh_strdup(dir);
if (pt->prestats[fd].dir == NULL)
return __WASI_ENOMEM;
return __WASI_ESUCCESS;
}
// Inserts a preopened resource record into the preopened resource table.
bool
fd_prestats_insert(struct fd_prestats *pt, const char *dir, __wasi_fd_t fd)
{
rwlock_wrlock(&pt->lock);
__wasi_errno_t error = fd_prestats_insert_locked(pt, dir, fd);
rwlock_unlock(&pt->lock);
return error == __WASI_ESUCCESS;
}
// Looks up a preopened resource table entry by number.
static __wasi_errno_t
fd_prestats_get_entry(struct fd_prestats *pt, __wasi_fd_t fd,
struct fd_prestat **ret) REQUIRES_SHARED(pt->lock)
{
// Test for file descriptor existence.
if ((size_t)fd >= pt->size)
return __WASI_EBADF;
struct fd_prestat *prestat = &pt->prestats[fd];
if (prestat->dir == NULL)
return __WASI_EBADF;
*ret = prestat;
return 0;
}
// Remove a preopened resource record from the preopened resource table by
// number
static __wasi_errno_t
fd_prestats_remove_entry(struct fd_prestats *pt, __wasi_fd_t fd)
{
// Test for file descriptor existence.
if ((size_t)fd >= pt->size)
return __WASI_EBADF;
struct fd_prestat *prestat = &pt->prestats[fd];
if (prestat->dir != NULL) {
wasm_runtime_free((void *)prestat->dir);
prestat->dir = NULL;
}
return __WASI_ESUCCESS;
}
struct fd_object {
struct refcount refcount;
__wasi_filetype_t type;
os_file_handle file_handle;
// Keep track of whether this fd object refers to a stdio stream so we know
// whether to close the underlying file handle when releasing the object.
bool is_stdio;
union {
// Data associated with directory file descriptors.
struct {
struct mutex lock; // Lock to protect members below.
os_dir_stream handle; // Directory handle.
__wasi_dircookie_t offset; // Offset of the directory.
} directory;
};
};
struct fd_entry {
struct fd_object *object;
__wasi_rights_t rights_base;
__wasi_rights_t rights_inheriting;
};
bool
fd_table_init(struct fd_table *ft)
{
if (!rwlock_initialize(&ft->lock))
return false;
ft->entries = NULL;
ft->size = 0;
ft->used = 0;
return true;
}
// Looks up a file descriptor table entry by number and required rights.
static __wasi_errno_t
fd_table_get_entry(struct fd_table *ft, __wasi_fd_t fd,
__wasi_rights_t rights_base,
__wasi_rights_t rights_inheriting, struct fd_entry **ret)
REQUIRES_SHARED(ft->lock)
{
// Test for file descriptor existence.
if ((size_t)fd >= ft->size) {
return __WASI_EBADF;
}
struct fd_entry *fe = &ft->entries[fd];
if (fe->object == NULL) {
return __WASI_EBADF;
}
// Validate rights.
if ((~fe->rights_base & rights_base) != 0
|| (~fe->rights_inheriting & rights_inheriting) != 0) {
return __WASI_ENOTCAPABLE;
}
*ret = fe;
return 0;
}
// Grows the file descriptor table to a required lower bound and a
// minimum number of free file descriptor table entries.
static bool
fd_table_grow(struct fd_table *ft, size_t min, size_t incr)
REQUIRES_EXCLUSIVE(ft->lock)
{
if (ft->size <= min || ft->size < (ft->used + incr) * 2) {
// Keep on doubling the table size until we've met our constraints.
size_t size = ft->size == 0 ? 1 : ft->size;
while (size <= min || size < (ft->used + incr) * 2)
size *= 2;
// Grow the file descriptor table's allocation.
struct fd_entry *entries =
wasm_runtime_malloc((uint32)(sizeof(*entries) * size));
if (entries == NULL)
return false;
if (ft->entries && ft->size > 0) {
bh_memcpy_s(entries, (uint32)(sizeof(*entries) * size), ft->entries,
(uint32)(sizeof(*entries) * ft->size));
}
if (ft->entries)
wasm_runtime_free(ft->entries);
// Mark all new file descriptors as unused.
for (size_t i = ft->size; i < size; ++i)
entries[i].object = NULL;
ft->entries = entries;
ft->size = size;
}
return true;
}
// Allocates a new file descriptor object.
static __wasi_errno_t
fd_object_new(__wasi_filetype_t type, bool is_stdio, struct fd_object **fo)
TRYLOCKS_SHARED(0, (*fo)->refcount)
{
*fo = wasm_runtime_malloc(sizeof(**fo));
if (*fo == NULL)
return __WASI_ENOMEM;
refcount_init(&(*fo)->refcount, 1);
(*fo)->type = type;
(*fo)->file_handle = os_get_invalid_handle();
(*fo)->is_stdio = is_stdio;
return 0;
}
// Attaches a file descriptor to the file descriptor table.
static void
fd_table_attach(struct fd_table *ft, __wasi_fd_t fd, struct fd_object *fo,
__wasi_rights_t rights_base, __wasi_rights_t rights_inheriting)
REQUIRES_EXCLUSIVE(ft->lock) CONSUMES(fo->refcount)
{
bh_assert(ft->size <= INT_MAX
&& "Unsigned value is out of signed int range");
bh_assert((int32_t)ft->size > fd && "File descriptor table too small");
struct fd_entry *fe = &ft->entries[fd];
bh_assert(fe->object == NULL
&& "Attempted to overwrite an existing descriptor");
fe->object = fo;
fe->rights_base = rights_base;
fe->rights_inheriting = rights_inheriting;
++ft->used;
bh_assert(ft->size >= ft->used * 2 && "File descriptor too full");
}
// Detaches a file descriptor from the file descriptor table.
static void
fd_table_detach(struct fd_table *ft, __wasi_fd_t fd, struct fd_object **fo)
REQUIRES_EXCLUSIVE(ft->lock) PRODUCES((*fo)->refcount)
{
bh_assert(ft->size <= INT_MAX
&& "Unsigned value is out of signed int range");
bh_assert((int32_t)ft->size > fd && "File descriptor table too small");
struct fd_entry *fe = &ft->entries[fd];
*fo = fe->object;
bh_assert(*fo != NULL && "Attempted to detach nonexistent descriptor");
fe->object = NULL;
bh_assert(ft->used > 0 && "Reference count mismatch");
--ft->used;
}
// Determines the type of a file descriptor and its maximum set of
// rights that should be attached to it.
static __wasi_errno_t
fd_determine_type_rights(os_file_handle fd, __wasi_filetype_t *type,
__wasi_rights_t *rights_base,
__wasi_rights_t *rights_inheriting)
{
struct __wasi_filestat_t buf;
__wasi_errno_t error;
if (os_is_stdin_handle(fd)) {
*rights_base = RIGHTS_STDIN;
*rights_inheriting = RIGHTS_STDIN;
return __WASI_ESUCCESS;
}
if (os_is_stdout_handle(fd)) {
*rights_base = RIGHTS_STDOUT;
*rights_inheriting = RIGHTS_STDOUT;
return __WASI_ESUCCESS;
}
if (os_is_stderr_handle(fd)) {
*rights_base = RIGHTS_STDERR;
*rights_inheriting = RIGHTS_STDERR;
return __WASI_ESUCCESS;
}
error = os_fstat(fd, &buf);
if (error != __WASI_ESUCCESS)
return error;
*type = buf.st_filetype;
switch (buf.st_filetype) {
case __WASI_FILETYPE_BLOCK_DEVICE:
*rights_base = RIGHTS_BLOCK_DEVICE_BASE;
*rights_inheriting = RIGHTS_BLOCK_DEVICE_INHERITING;
break;
case __WASI_FILETYPE_CHARACTER_DEVICE:
error = os_isatty(fd);
if (error == __WASI_ESUCCESS) {
*rights_base = RIGHTS_TTY_BASE;
*rights_inheriting = RIGHTS_TTY_INHERITING;
}
else {
*rights_base = RIGHTS_CHARACTER_DEVICE_BASE;
*rights_inheriting = RIGHTS_CHARACTER_DEVICE_INHERITING;
}
break;
case __WASI_FILETYPE_DIRECTORY:
*rights_base = RIGHTS_DIRECTORY_BASE;
*rights_inheriting = RIGHTS_DIRECTORY_INHERITING;
break;
case __WASI_FILETYPE_REGULAR_FILE:
*rights_base = RIGHTS_REGULAR_FILE_BASE;
*rights_inheriting = RIGHTS_REGULAR_FILE_INHERITING;
break;
case __WASI_FILETYPE_SOCKET_DGRAM:
case __WASI_FILETYPE_SOCKET_STREAM:
*rights_base = RIGHTS_SOCKET_BASE;
*rights_inheriting = RIGHTS_SOCKET_INHERITING;
break;
case __WASI_FILETYPE_SYMBOLIC_LINK:
case __WASI_FILETYPE_UNKNOWN:
// If we don't know the type, allow for the maximum set of
// rights
*rights_base = RIGHTS_ALL;
*rights_inheriting = RIGHTS_ALL;
break;
default:
return __WASI_EINVAL;
}
wasi_libc_file_access_mode access_mode;
error = os_file_get_access_mode(fd, &access_mode);
if (error != __WASI_ESUCCESS)
return error;
// Strip off read/write bits based on the access mode.
switch (access_mode) {
case WASI_LIBC_ACCESS_MODE_READ_ONLY:
*rights_base &= ~(__wasi_rights_t)__WASI_RIGHT_FD_WRITE;
break;
case WASI_LIBC_ACCESS_MODE_WRITE_ONLY:
*rights_base &= ~(__wasi_rights_t)__WASI_RIGHT_FD_READ;
break;
}
return error;
}
// Lowers the reference count on a file descriptor object. When the
// reference count reaches zero, its resources are cleaned up.
static __wasi_errno_t
fd_object_release(wasm_exec_env_t env, struct fd_object *fo)
UNLOCKS(fo->refcount)
{
__wasi_errno_t error = __WASI_ESUCCESS;
if (refcount_release(&fo->refcount)) {
int saved_errno = errno;
switch (fo->type) {
case __WASI_FILETYPE_DIRECTORY:
// For directories we may keep track of a DIR object.
// Calling os_closedir() on it also closes the underlying file
// descriptor.
mutex_destroy(&fo->directory.lock);
if (os_is_dir_stream_valid(&fo->directory.handle)) {
error = os_closedir(fo->directory.handle);
break;
}
// Fallthrough.
default:
// The env == NULL case is for
// fd_table_destroy, path_get, path_put,
// fd_table_insert_existing
error = (env == NULL) ? os_close(fo->file_handle, fo->is_stdio)
: blocking_op_close(env, fo->file_handle,
fo->is_stdio);
break;
}
wasm_runtime_free(fo);
errno = saved_errno;
}
return error;
}
// Inserts an already existing file descriptor into the file descriptor
// table.
bool
fd_table_insert_existing(struct fd_table *ft, __wasi_fd_t in,
os_file_handle out, bool is_stdio)
{
__wasi_filetype_t type = __WASI_FILETYPE_UNKNOWN;
__wasi_rights_t rights_base = 0, rights_inheriting = 0;
struct fd_object *fo;
__wasi_errno_t error;
error =
fd_determine_type_rights(out, &type, &rights_base, &rights_inheriting);
if (error != 0) {
#ifdef BH_PLATFORM_EGO
/**
* since it is an already opened file and we can assume the opened
* file has all necessary rights no matter how to get
*/
if (error != __WASI_ENOTSUP)
return false;
#else
return false;
#endif
}
error = fd_object_new(type, is_stdio, &fo);
if (error != 0)
return false;
fo->file_handle = out;
if (type == __WASI_FILETYPE_DIRECTORY) {
if (!mutex_init(&fo->directory.lock)) {
fd_object_release(NULL, fo);
return false;
}
fo->directory.handle = os_get_invalid_dir_stream();
}
// Grow the file descriptor table if needed.
rwlock_wrlock(&ft->lock);
if (!fd_table_grow(ft, in, 1)) {
rwlock_unlock(&ft->lock);
fd_object_release(NULL, fo);
return false;
}
fd_table_attach(ft, in, fo, rights_base, rights_inheriting);
rwlock_unlock(&ft->lock);
return true;
}
// Picks an unused slot from the file descriptor table.
static __wasi_errno_t
fd_table_unused(struct fd_table *ft, __wasi_fd_t *out) REQUIRES_SHARED(ft->lock)
{
bh_assert(ft->size > ft->used && "File descriptor table has no free slots");
for (;;) {
uintmax_t random_fd = 0;
__wasi_errno_t error = random_uniform(ft->size, &random_fd);
if (error != __WASI_ESUCCESS)
return error;
if (ft->entries[(__wasi_fd_t)random_fd].object == NULL) {
*out = (__wasi_fd_t)random_fd;
return error;
}
}
}
// Inserts a file descriptor object into an unused slot of the file
// descriptor table.
static __wasi_errno_t
fd_table_insert(wasm_exec_env_t exec_env, struct fd_table *ft,
struct fd_object *fo, __wasi_rights_t rights_base,
__wasi_rights_t rights_inheriting, __wasi_fd_t *out)
REQUIRES_UNLOCKED(ft->lock) UNLOCKS(fo->refcount)
{
// Grow the file descriptor table if needed.
rwlock_wrlock(&ft->lock);
if (!fd_table_grow(ft, 0, 1)) {
rwlock_unlock(&ft->lock);
fd_object_release(exec_env, fo);
return convert_errno(errno);
}
__wasi_errno_t error = fd_table_unused(ft, out);
if (error != __WASI_ESUCCESS) {
rwlock_unlock(&ft->lock);
return error;
}
fd_table_attach(ft, *out, fo, rights_base, rights_inheriting);
rwlock_unlock(&ft->lock);
return error;
}
// Inserts a numerical file descriptor into the file descriptor table.
static __wasi_errno_t
fd_table_insert_fd(wasm_exec_env_t exec_env, struct fd_table *ft,
os_file_handle in, __wasi_filetype_t type,
__wasi_rights_t rights_base,
__wasi_rights_t rights_inheriting, __wasi_fd_t *out)
REQUIRES_UNLOCKED(ft->lock)
{
struct fd_object *fo;
__wasi_errno_t error = fd_object_new(type, false, &fo);
if (error != 0) {
os_close(in, false);
return error;
}
fo->file_handle = in;
if (type == __WASI_FILETYPE_DIRECTORY) {
if (!mutex_init(&fo->directory.lock)) {
fd_object_release(exec_env, fo);
return (__wasi_errno_t)-1;
}
fo->directory.handle = os_get_invalid_dir_stream();
}
return fd_table_insert(exec_env, ft, fo, rights_base, rights_inheriting,
out);
}
__wasi_errno_t
wasmtime_ssp_fd_prestat_get(struct fd_prestats *prestats, __wasi_fd_t fd,
__wasi_prestat_t *buf)
{
rwlock_rdlock(&prestats->lock);
struct fd_prestat *prestat;
__wasi_errno_t error = fd_prestats_get_entry(prestats, fd, &prestat);
if (error != 0) {
rwlock_unlock(&prestats->lock);
return error;
}
*buf = (__wasi_prestat_t){
.pr_type = __WASI_PREOPENTYPE_DIR,
};
buf->u.dir.pr_name_len = strlen(prestat->dir);
rwlock_unlock(&prestats->lock);
return 0;
}
__wasi_errno_t
wasmtime_ssp_fd_prestat_dir_name(struct fd_prestats *prestats, __wasi_fd_t fd,
char *path, size_t path_len)
{
rwlock_rdlock(&prestats->lock);
struct fd_prestat *prestat;
__wasi_errno_t error = fd_prestats_get_entry(prestats, fd, &prestat);
if (error != 0) {
rwlock_unlock(&prestats->lock);
return error;
}
const size_t prestat_dir_len = strlen(prestat->dir);
if (path_len < prestat_dir_len) {
rwlock_unlock(&prestats->lock);
return __WASI_EINVAL;
}
bh_memcpy_s(path, (uint32)path_len, prestat->dir, (uint32)prestat_dir_len);
rwlock_unlock(&prestats->lock);
return 0;
}
__wasi_errno_t
wasmtime_ssp_fd_close(wasm_exec_env_t exec_env, struct fd_table *curfds,
struct fd_prestats *prestats, __wasi_fd_t fd)
{
// Validate the file descriptor.
struct fd_table *ft = curfds;
rwlock_wrlock(&ft->lock);
rwlock_wrlock(&prestats->lock);
struct fd_entry *fe;
__wasi_errno_t error = fd_table_get_entry(ft, fd, 0, 0, &fe);
if (error != 0) {
rwlock_unlock(&prestats->lock);
rwlock_unlock(&ft->lock);
return error;
}
// Remove it from the file descriptor table.
struct fd_object *fo;
fd_table_detach(ft, fd, &fo);
// Remove it from the preopened resource table if it exists
error = fd_prestats_remove_entry(prestats, fd);
rwlock_unlock(&prestats->lock);
rwlock_unlock(&ft->lock);
fd_object_release(exec_env, fo);
// Ignore the error if there is no preopen associated with this fd
if (error == __WASI_EBADF) {
return __WASI_ESUCCESS;
}
return error;
}
// Look up a file descriptor object in a locked file descriptor table
// and increases its reference count.
static __wasi_errno_t
fd_object_get_locked(struct fd_object **fo, struct fd_table *ft, __wasi_fd_t fd,
__wasi_rights_t rights_base,
__wasi_rights_t rights_inheriting)
TRYLOCKS_EXCLUSIVE(0, (*fo)->refcount) REQUIRES_EXCLUSIVE(ft->lock)
{
// Test whether the file descriptor number is valid.
struct fd_entry *fe;
__wasi_errno_t error =
fd_table_get_entry(ft, fd, rights_base, rights_inheriting, &fe);
if (error != 0)
return error;
// Increase the reference count on the file descriptor object. A copy
// of the rights are also stored, so callers can still access those if
// needed.
*fo = fe->object;
refcount_acquire(&(*fo)->refcount);
return 0;
}
// Temporarily locks the file descriptor table to look up a file
// descriptor object, increases its reference count and drops the lock.
static __wasi_errno_t
fd_object_get(struct fd_table *curfds, struct fd_object **fo, __wasi_fd_t fd,
__wasi_rights_t rights_base, __wasi_rights_t rights_inheriting)
TRYLOCKS_EXCLUSIVE(0, (*fo)->refcount)
{
struct fd_table *ft = curfds;
rwlock_rdlock(&ft->lock);
__wasi_errno_t error =
fd_object_get_locked(fo, ft, fd, rights_base, rights_inheriting);
rwlock_unlock(&ft->lock);
return error;
}
__wasi_errno_t
wasmtime_ssp_fd_datasync(wasm_exec_env_t exec_env, struct fd_table *curfds,
__wasi_fd_t fd)
{
struct fd_object *fo;
__wasi_errno_t error =
fd_object_get(curfds, &fo, fd, __WASI_RIGHT_FD_DATASYNC, 0);
if (error != 0)
return error;
error = os_fdatasync(fo->file_handle);
fd_object_release(exec_env, fo);
return error;
}
__wasi_errno_t
wasmtime_ssp_fd_pread(wasm_exec_env_t exec_env, struct fd_table *curfds,
__wasi_fd_t fd, const __wasi_iovec_t *iov, size_t iovcnt,
__wasi_filesize_t offset, size_t *nread)
{
if (iovcnt == 0)
return __WASI_EINVAL;
struct fd_object *fo;
__wasi_errno_t error =
fd_object_get(curfds, &fo, fd, __WASI_RIGHT_FD_READ, 0);
if (error != 0)
return error;
error = blocking_op_preadv(exec_env, fo->file_handle, iov, (int)iovcnt,
offset, nread);
fd_object_release(exec_env, fo);
return error;
}
__wasi_errno_t
wasmtime_ssp_fd_pwrite(wasm_exec_env_t exec_env, struct fd_table *curfds,
__wasi_fd_t fd, const __wasi_ciovec_t *iov,
size_t iovcnt, __wasi_filesize_t offset,
size_t *nwritten)
{
struct fd_object *fo;
__wasi_errno_t error =
fd_object_get(curfds, &fo, fd, __WASI_RIGHT_FD_WRITE, 0);
if (error != 0)
return error;
error = blocking_op_pwritev(exec_env, fo->file_handle, iov, (int)iovcnt,
offset, nwritten);
fd_object_release(exec_env, fo);
return error;
}
__wasi_errno_t
wasmtime_ssp_fd_read(wasm_exec_env_t exec_env, struct fd_table *curfds,
__wasi_fd_t fd, const __wasi_iovec_t *iov, size_t iovcnt,
size_t *nread)
{
struct fd_object *fo;
__wasi_errno_t error =
fd_object_get(curfds, &fo, fd, __WASI_RIGHT_FD_READ, 0);
if (error != 0)
return error;
error =
blocking_op_readv(exec_env, fo->file_handle, iov, (int)iovcnt, nread);
fd_object_release(exec_env, fo);
return error;
}
__wasi_errno_t
wasmtime_ssp_fd_renumber(wasm_exec_env_t exec_env, struct fd_table *curfds,
struct fd_prestats *prestats, __wasi_fd_t from,
__wasi_fd_t to)
{
struct fd_table *ft = curfds;
rwlock_wrlock(&ft->lock);
rwlock_wrlock(&prestats->lock);
struct fd_entry *fe_from;
__wasi_errno_t error = fd_table_get_entry(ft, from, 0, 0, &fe_from);
if (error != 0) {
rwlock_unlock(&prestats->lock);
rwlock_unlock(&ft->lock);
return error;
}
struct fd_entry *fe_to;
error = fd_table_get_entry(ft, to, 0, 0, &fe_to);
if (error != 0) {
rwlock_unlock(&prestats->lock);
rwlock_unlock(&ft->lock);
return error;
}
struct fd_object *fo;
fd_table_detach(ft, to, &fo);
refcount_acquire(&fe_from->object->refcount);
fd_table_attach(ft, to, fe_from->object, fe_from->rights_base,
fe_from->rights_inheriting);
fd_object_release(exec_env, fo);
// Remove the old fd from the file descriptor table.
fd_table_detach(ft, from, &fo);
fd_object_release(exec_env, fo);
--ft->used;
// Handle renumbering of any preopened resources
struct fd_prestat *prestat_from;
__wasi_errno_t prestat_from_error =
fd_prestats_get_entry(prestats, from, &prestat_from);
struct fd_prestat *prestat_to;
__wasi_errno_t prestat_to_error =
fd_prestats_get_entry(prestats, to, &prestat_to);
// Renumbering over two preopened resources.
if (prestat_from_error == __WASI_ESUCCESS
&& prestat_to_error == __WASI_ESUCCESS) {
(void)fd_prestats_remove_entry(prestats, to);
error = fd_prestats_insert_locked(prestats, prestat_from->dir, to);
if (error == __WASI_ESUCCESS) {
(void)fd_prestats_remove_entry(prestats, from);
}
else {
(void)fd_prestats_remove_entry(prestats, to);
}
}
// Renumbering from a non-preopened fd to a preopened fd. In this case,
// we can't a keep the destination fd entry in the preopened table so
// remove it entirely.
else if (prestat_from_error != __WASI_ESUCCESS
&& prestat_to_error == __WASI_ESUCCESS) {
(void)fd_prestats_remove_entry(prestats, to);
}
// Renumbering from a preopened fd to a non-preopened fd
else if (prestat_from_error == __WASI_ESUCCESS
&& prestat_to_error != __WASI_ESUCCESS) {
error = fd_prestats_insert_locked(prestats, prestat_from->dir, to);
if (error == __WASI_ESUCCESS) {
(void)fd_prestats_remove_entry(prestats, from);
}
else {
(void)fd_prestats_remove_entry(prestats, to);
}
}
rwlock_unlock(&prestats->lock);
rwlock_unlock(&ft->lock);