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
|
/*
* Copyright (c) 2023, Ali Mohammad Pur <mpfard@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/DistinctNumeric.h>
#include <AK/Endian.h>
#include <AK/Function.h>
#include <AK/LexicalPath.h>
#include <AK/RedBlackTree.h>
#include <AK/String.h>
#include <AK/Vector.h>
#include <LibWasm/AbstractMachine/AbstractMachine.h>
#include <LibWasm/Forward.h>
namespace Wasm::Wasi::ABI {
// NOTE: The "real" ABI used in the wild is described by [api.h from libc-bottom-half](https://github.com/WebAssembly/wasi-libc/blob/2c2fc9a2fddd0927a66f1c142e65c8dab6f5c5d7/libc-bottom-half/headers/public/wasi/api.h)
// This is *not* the same ABI as the one described in the WASI spec, nor is it the same ABI as api.h on wasi-libc/master.
// The highlights of the ABI are:
// - (most) structs are passed as pointers to heap.
// - arrays are fat pointers splat across two arguments
// - return object locations are also passed as arguments, the number of arguments depends on the return type itself:
// - ArgsSizes / EnvironSizes / the return type of sock_recv use two arguments
// - everything else is passed like a normal struct
template<auto impl>
struct InvocationOf {
HostFunction operator()(Implementation&, StringView name);
};
template<typename T, size_t N>
void serialize(T const&, Array<Bytes, N>);
template<typename T, size_t N>
T deserialize(Array<ReadonlyBytes, N> const&);
template<typename T>
struct ToCompatibleValue {
using Type = void;
};
template<typename T>
struct CompatibleValue {
typename ToCompatibleValue<T>::Type value;
Wasm::Value to_wasm_value() const;
};
template<typename T>
CompatibleValue<T> to_compatible_value(Wasm::Value const&);
template<typename T>
T deserialize(CompatibleValue<T> const&);
}
namespace Wasm::Wasi {
// NOTE: This is a copy of LittleEndian from Endian.h,
// we can't use those because they have a packed attribute, and depend on it;
// but we want proper alignment on these types.
template<typename T>
class alignas(T) LittleEndian {
public:
constexpr LittleEndian() = default;
constexpr LittleEndian(T value)
: m_value(AK::convert_between_host_and_little_endian(value))
{
}
constexpr operator T() const { return AK::convert_between_host_and_little_endian(m_value); }
constexpr T value() const { return AK::convert_between_host_and_little_endian(m_value); }
LittleEndian& operator+=(T other)
{
m_value = AK::convert_between_host_and_little_endian(AK::convert_between_host_and_little_endian(m_value) + other);
return *this;
}
// This returns the internal representation. In this case, that is the value stored in little endian format.
constexpr Bytes bytes() { return Bytes { &m_value, sizeof(m_value) }; }
constexpr ReadonlyBytes bytes() const { return ReadonlyBytes { &m_value, sizeof(m_value) }; }
void serialize_into(Array<Bytes, 1> bytes) const;
static LittleEndian read_from(Array<ReadonlyBytes, 1> const& bytes);
private:
T m_value { 0 };
};
using Size = LittleEndian<u32>;
using FileSize = LittleEndian<u64>;
using Timestamp = LittleEndian<u64>;
namespace Detail {
template<typename>
struct __Pointer_tag;
template<typename>
struct __ConstPointer_tag;
}
// NOTE: Might need to be updated if WASI ever supports memory64.
using UnderlyingPointerType = u32;
template<typename T>
using Pointer = DistinctNumeric<LittleEndian<UnderlyingPointerType>, Detail::__Pointer_tag<T>, AK::DistinctNumericFeature::Comparison>;
template<typename T>
using ConstPointer = DistinctNumeric<LittleEndian<UnderlyingPointerType>, Detail::__ConstPointer_tag<T>, AK::DistinctNumericFeature::Comparison>;
// https://github.com/WebAssembly/wasi-libc/blob/2c2fc9a2fddd0927a66f1c142e65c8dab6f5c5d7/libc-bottom-half/headers/public/wasi/api.h#L70
enum class ClockID : u32 {
Realtime,
Monotonic,
ProcessCPUTimeID,
ThreadCPUTimeID,
};
// https://github.com/WebAssembly/wasi-libc/blob/2c2fc9a2fddd0927a66f1c142e65c8dab6f5c5d7/libc-bottom-half/headers/public/wasi/api.h#L105
enum class Errno : u16 {
Success,
TooBig,
Access,
AddressInUse,
AddressNotAvailable,
AFNotSupported,
Again,
Already,
BadF,
BadMessage,
Busy,
Canceled,
Child,
ConnectionAborted,
ConnectionRefused,
ConnectionReset,
Deadlock,
DestinationAddressRequired,
Domain,
DQuot, // Reserved, Unused.
Exist,
Fault,
FBig,
HostUnreachable,
IdentifierRemoved,
IllegalSequence,
InProgress,
Interrupted,
Invalid,
IO,
IsConnected,
IsDirectory,
Loop,
MFile,
MLink,
MessageSize,
MultiHop, // Reserved, Unused.
NameTooLong,
NetworkDown,
NetworkReset,
NetworkUnreachable,
NFile,
NoBufferSpace,
NoDevice,
NoEntry,
NoExec,
NoLock,
NoLink,
NoMemory,
NoMessage,
NoProtocolOption,
NoSpace,
NoSys,
NotConnected,
NotDirectory,
NotEmpty,
NotRecoverable,
NotSocket,
NotSupported,
NoTTY,
NXIO,
Overflow,
OwnerDead,
Permission,
Pipe,
Protocol,
ProtocolNotSupported,
ProtocolType,
Range,
ReadOnlyFS,
SPipe,
SRCH,
Stale,
TimedOut,
TextBusy,
XDev,
NotCapable,
};
// https://github.com/WebAssembly/wasi-libc/blob/2c2fc9a2fddd0927a66f1c142e65c8dab6f5c5d7/libc-bottom-half/headers/public/wasi/api.h#L498
struct Rights {
using CompatibleType = u64;
struct Bits {
bool fd_datasync : 1;
bool fd_read : 1;
bool fd_seek : 1;
bool fd_fdstat_set_flags : 1;
bool fd_sync : 1;
bool fd_tell : 1;
bool fd_write : 1;
bool fd_advise : 1;
bool fd_allocate : 1;
bool path_create_directory : 1;
bool path_create_file : 1;
bool path_link_source : 1;
bool path_link_target : 1;
bool path_open : 1;
bool fd_readdir : 1;
bool path_readlink : 1;
bool path_rename_source : 1;
bool path_rename_target : 1;
bool path_filestat_get : 1;
bool path_filestat_set_size : 1;
bool path_filestat_set_times : 1;
bool fd_filestat_get : 1;
bool fd_filestat_set_size : 1;
bool fd_filestat_set_times : 1;
bool path_symlink : 1;
bool path_remove_directory : 1;
bool path_unlink_file : 1;
bool poll_fd_readwrite : 1;
bool sock_shutdown : 1;
bool sock_accept : 1;
u64 _unused : 34;
};
static_assert(sizeof(Bits) == sizeof(CompatibleType));
union {
Bits bits;
LittleEndian<CompatibleType> data;
};
void serialize_into(Array<Bytes, 1> bytes) const;
static Rights read_from(Array<ReadonlyBytes, 1> const& bytes);
};
// https://github.com/WebAssembly/wasi-libc/blob/2c2fc9a2fddd0927a66f1c142e65c8dab6f5c5d7/libc-bottom-half/headers/public/wasi/api.h#L663
using FD = DistinctNumeric<LittleEndian<u32>, struct __FD_tag, AK::DistinctNumericFeature::Comparison>;
// https://github.com/WebAssembly/wasi-libc/blob/2c2fc9a2fddd0927a66f1c142e65c8dab6f5c5d7/libc-bottom-half/headers/public/wasi/api.h#L671
struct IOVec {
Pointer<u8> buf;
Size buf_len;
static IOVec read_from(Array<ReadonlyBytes, 1> const& bytes);
};
// https://github.com/WebAssembly/wasi-libc/blob/2c2fc9a2fddd0927a66f1c142e65c8dab6f5c5d7/libc-bottom-half/headers/public/wasi/api.h#L692
struct CIOVec {
ConstPointer<u8> buf;
Size buf_len;
static CIOVec read_from(Array<ReadonlyBytes, 1> const& bytes);
};
// https://github.com/WebAssembly/wasi-libc/blob/2c2fc9a2fddd0927a66f1c142e65c8dab6f5c5d7/libc-bottom-half/headers/public/wasi/api.h#L713
using FileDelta = LittleEndian<i64>;
// https://github.com/WebAssembly/wasi-libc/blob/2c2fc9a2fddd0927a66f1c142e65c8dab6f5c5d7/libc-bottom-half/headers/public/wasi/api.h#L721
enum class Whence : u8 {
Set,
Cur,
End,
};
// https://github.com/WebAssembly/wasi-libc/blob/2c2fc9a2fddd0927a66f1c142e65c8dab6f5c5d7/libc-bottom-half/headers/public/wasi/api.h#L746
using DirCookie = LittleEndian<u64>;
// https://github.com/WebAssembly/wasi-libc/blob/2c2fc9a2fddd0927a66f1c142e65c8dab6f5c5d7/libc-bottom-half/headers/public/wasi/api.h#L754
using DirNameLen = LittleEndian<u32>;
// https://github.com/WebAssembly/wasi-libc/blob/2c2fc9a2fddd0927a66f1c142e65c8dab6f5c5d7/libc-bottom-half/headers/public/wasi/api.h#L762
using INode = LittleEndian<u64>;
// https://github.com/WebAssembly/wasi-libc/blob/2c2fc9a2fddd0927a66f1c142e65c8dab6f5c5d7/libc-bottom-half/headers/public/wasi/api.h#L770
enum class FileType : u8 {
Unknown,
BlockDevice,
CharacterDevice,
Directory,
RegularFile,
SocketDGram,
SocketStream,
SymbolicLink,
};
// https://github.com/WebAssembly/wasi-libc/blob/2c2fc9a2fddd0927a66f1c142e65c8dab6f5c5d7/libc-bottom-half/headers/public/wasi/api.h#L818
struct DirEnt {
DirCookie d_next;
INode d_ino;
DirNameLen d_namlen;
FileType d_type;
u8 _padding[3] { 0 }; // Not part of the API, but the struct is required to be 24 bytes - even though it has no explicit padding.
};
static_assert(sizeof(DirEnt) == 24);
// https://github.com/WebAssembly/wasi-libc/blob/2c2fc9a2fddd0927a66f1c142e65c8dab6f5c5d7/libc-bottom-half/headers/public/wasi/api.h#L851
enum class Advice : u8 {
Normal,
Sequential,
Random,
WillNeed,
DontNeed,
NoReuse,
};
// https://github.com/WebAssembly/wasi-libc/blob/2c2fc9a2fddd0927a66f1c142e65c8dab6f5c5d7/libc-bottom-half/headers/public/wasi/api.h#L889
struct FDFlags {
using CompatibleType = u16;
struct Bits {
bool append : 1;
bool dsync : 1;
bool nonblock : 1;
bool rsync : 1;
bool sync : 1;
u16 _unused : 11;
};
static_assert(sizeof(Bits) == sizeof(CompatibleType));
union {
Bits bits;
LittleEndian<CompatibleType> data;
};
void serialize_into(Array<Bytes, 1> bytes) const;
static FDFlags read_from(Array<ReadonlyBytes, 1> const& bytes);
};
// https://github.com/WebAssembly/wasi-libc/blob/2c2fc9a2fddd0927a66f1c142e65c8dab6f5c5d7/libc-bottom-half/headers/public/wasi/api.h#L924
struct FDStat {
FileType fs_filetype;
FDFlags fs_flags;
Rights fs_rights_base;
Rights fs_rights_inheriting;
void serialize_into(Array<Bytes, 1> bytes) const;
};
static_assert(sizeof(FDStat) == 24);
// https://github.com/WebAssembly/wasi-libc/blob/2c2fc9a2fddd0927a66f1c142e65c8dab6f5c5d7/libc-bottom-half/headers/public/wasi/api.h#L959
using Device = LittleEndian<u64>;
// https://github.com/WebAssembly/wasi-libc/blob/2c2fc9a2fddd0927a66f1c142e65c8dab6f5c5d7/libc-bottom-half/headers/public/wasi/api.h#L967
struct FSTFlags {
using CompatibleType = u16;
struct Bits {
bool atim : 1;
bool atim_now : 1;
bool mtim : 1;
bool mtim_now : 1;
u16 _unused : 12;
};
static_assert(sizeof(Bits) == sizeof(CompatibleType));
union {
Bits bits;
LittleEndian<CompatibleType> data;
};
void serialize_into(Array<Bytes, 1> bytes) const;
static FSTFlags read_from(Array<ReadonlyBytes, 1> const& bytes);
};
static_assert(sizeof(FSTFlags) == 2);
// https://github.com/WebAssembly/wasi-libc/blob/2c2fc9a2fddd0927a66f1c142e65c8dab6f5c5d7/libc-bottom-half/headers/public/wasi/api.h#L995
struct LookupFlags {
using CompatibleType = u32;
struct Bits {
bool symlink_follow : 1;
u32 _unused : 31;
};
static_assert(sizeof(Bits) == sizeof(CompatibleType));
union {
Bits bits;
LittleEndian<CompatibleType> data;
};
void serialize_into(Array<Bytes, 1> bytes) const;
static LookupFlags read_from(Array<ReadonlyBytes, 1> const& bytes);
};
static_assert(sizeof(LookupFlags) == 4);
// https://github.com/WebAssembly/wasi-libc/blob/2c2fc9a2fddd0927a66f1c142e65c8dab6f5c5d7/libc-bottom-half/headers/public/wasi/api.h#L1008
struct OFlags {
using CompatibleType = u16;
struct Bits {
bool creat : 1;
bool directory : 1;
bool excl : 1;
bool trunc : 1;
u16 _unused : 12;
};
static_assert(sizeof(Bits) == sizeof(CompatibleType));
union {
Bits bits;
LittleEndian<CompatibleType> data;
};
static OFlags read_from(Array<ReadonlyBytes, 1> const& bytes);
};
static_assert(sizeof(OFlags) == 2);
// https://github.com/WebAssembly/wasi-libc/blob/2c2fc9a2fddd0927a66f1c142e65c8dab6f5c5d7/libc-bottom-half/headers/public/wasi/api.h#L1036
using LinkCount = LittleEndian<u64>;
// https://github.com/WebAssembly/wasi-libc/blob/2c2fc9a2fddd0927a66f1c142e65c8dab6f5c5d7/libc-bottom-half/headers/public/wasi/api.h#L1044
struct FileStat {
Device dev;
INode ino;
FileType filetype;
LinkCount nlink;
FileSize size;
Timestamp atim;
Timestamp mtim;
Timestamp ctim;
void serialize_into(Array<Bytes, 1> bytes) const;
};
static_assert(sizeof(FileStat) == 64);
// https://github.com/WebAssembly/wasi-libc/blob/2c2fc9a2fddd0927a66f1c142e65c8dab6f5c5d7/libc-bottom-half/headers/public/wasi/api.h#L1102
using UserData = LittleEndian<u64>;
// https://github.com/WebAssembly/wasi-libc/blob/2c2fc9a2fddd0927a66f1c142e65c8dab6f5c5d7/libc-bottom-half/headers/public/wasi/api.h#L1110
enum class EventType : u8 {
Clock,
FDRead,
FDWrite,
};
// https://github.com/WebAssembly/wasi-libc/blob/2c2fc9a2fddd0927a66f1c142e65c8dab6f5c5d7/libc-bottom-half/headers/public/wasi/api.h#L1137
struct EventRWFlags {
using CompatibleType = u16;
struct Bits {
bool fd_readwrite_hangup : 1;
u16 _unused : 15;
};
static_assert(sizeof(Bits) == sizeof(CompatibleType));
union {
Bits bits;
LittleEndian<CompatibleType> data;
};
void serialize_into(Array<Bytes, 1> bytes) const;
static EventRWFlags read_from(Array<ReadonlyBytes, 1> const& bytes);
};
// https://github.com/WebAssembly/wasi-libc/blob/2c2fc9a2fddd0927a66f1c142e65c8dab6f5c5d7/libc-bottom-half/headers/public/wasi/api.h#L1151
struct EventFDReadWrite {
FileSize nbytes;
EventRWFlags flags;
void serialize_into(Array<Bytes, 1> bytes) const;
static EventFDReadWrite read_from(Array<ReadonlyBytes, 1> const& bytes);
};
// https://github.com/WebAssembly/wasi-libc/blob/2c2fc9a2fddd0927a66f1c142e65c8dab6f5c5d7/libc-bottom-half/headers/public/wasi/api.h#L1186
struct Event {
UserData userdata;
Errno errno_;
EventType type;
EventFDReadWrite fd_readwrite;
void serialize_into(Array<Bytes, 1> bytes) const;
static Event read_from(Array<ReadonlyBytes, 1> const& bytes);
};
// https://github.com/WebAssembly/wasi-libc/blob/2c2fc9a2fddd0927a66f1c142e65c8dab6f5c5d7/libc-bottom-half/headers/public/wasi/api.h#L1220
struct SubClockFlags {
using CompatibleType = u16;
struct Bits {
bool subscription_clock_abstime : 1;
u16 _unused : 15;
};
static_assert(sizeof(Bits) == sizeof(CompatibleType));
union {
Bits bits;
LittleEndian<CompatibleType> data;
};
void serialize_into(Array<Bytes, 1> bytes) const;
static SubClockFlags read_from(Array<ReadonlyBytes, 1> const& bytes);
};
// https://github.com/WebAssembly/wasi-libc/blob/2c2fc9a2fddd0927a66f1c142e65c8dab6f5c5d7/libc-bottom-half/headers/public/wasi/api.h#L1237
struct SubscriptionClock {
ClockID id;
Timestamp timeout;
Timestamp precision;
SubClockFlags flags;
void serialize_into(Array<Bytes, 1> bytes) const;
static SubscriptionClock read_from(Array<ReadonlyBytes, 1> const& bytes);
};
// https://github.com/WebAssembly/wasi-libc/blob/2c2fc9a2fddd0927a66f1c142e65c8dab6f5c5d7/libc-bottom-half/headers/public/wasi/api.h#L1272
struct SubscriptionFDReadWrite {
FD file_descriptor;
void serialize_into(Array<Bytes, 1> bytes) const;
static SubscriptionFDReadWrite read_from(Array<ReadonlyBytes, 1> const& bytes);
};
// https://github.com/WebAssembly/wasi-libc/blob/2c2fc9a2fddd0927a66f1c142e65c8dab6f5c5d7/libc-bottom-half/headers/public/wasi/api.h#L1287
struct SubscriptionU {
u8 tag;
union {
SubscriptionClock clock;
SubscriptionFDReadWrite fd_read;
SubscriptionFDReadWrite fd_write;
};
void serialize_into(Array<Bytes, 1> bytes) const;
static SubscriptionU read_from(Array<ReadonlyBytes, 1> const& bytes);
};
// https://github.com/WebAssembly/wasi-libc/blob/2c2fc9a2fddd0927a66f1c142e65c8dab6f5c5d7/libc-bottom-half/headers/public/wasi/api.h#L1306
struct Subscription {
UserData userdata;
SubscriptionU u;
void serialize_into(Array<Bytes, 1> bytes) const;
static Subscription read_from(Array<ReadonlyBytes, 1> const& bytes);
};
static_assert(sizeof(Subscription) == 48);
static_assert(alignof(Subscription) == 8);
// https://github.com/WebAssembly/wasi-libc/blob/2c2fc9a2fddd0927a66f1c142e65c8dab6f5c5d7/libc-bottom-half/headers/public/wasi/api.h#L1334
using ExitCode = LittleEndian<u32>;
// https://github.com/WebAssembly/wasi-libc/blob/2c2fc9a2fddd0927a66f1c142e65c8dab6f5c5d7/libc-bottom-half/headers/public/wasi/api.h#L1342
enum class Signal : u8 {
None,
HUP,
INT,
QUIT,
ILL,
TRAP,
ABRT,
BUS,
FPE,
KILL,
USR1,
SEGV,
USR2,
PIPE,
ALRM,
TERM,
CHLD,
CONT,
STOP,
TSTP,
TTIN,
TTOU,
URG,
XCPU,
XFSZ,
VTALRM,
PROF,
WINCH,
POLL,
PWR,
SYS,
};
// https://github.com/WebAssembly/wasi-libc/blob/2c2fc9a2fddd0927a66f1c142e65c8dab6f5c5d7/libc-bottom-half/headers/public/wasi/api.h#L1536
struct RIFlags {
using CompatibleType = u16;
struct Bits {
bool recv_peek : 1;
bool recv_waitall : 1;
u16 _unused : 14;
};
static_assert(sizeof(Bits) == sizeof(CompatibleType));
union {
Bits bits;
LittleEndian<CompatibleType> data;
};
static RIFlags read_from(Array<ReadonlyBytes, 1> const& bytes);
};
// https://github.com/WebAssembly/wasi-libc/blob/2c2fc9a2fddd0927a66f1c142e65c8dab6f5c5d7/libc-bottom-half/headers/public/wasi/api.h#L1554
struct ROFlags {
using CompatibleType = u16;
struct Bits {
bool recv_data_truncated : 1;
u16 _unused : 15;
};
static_assert(sizeof(Bits) == sizeof(CompatibleType));
union {
Bits bits;
LittleEndian<CompatibleType> data;
};
void serialize_into(Array<Bytes, 1> bytes) const;
};
// https://github.com/WebAssembly/wasi-libc/blob/2c2fc9a2fddd0927a66f1c142e65c8dab6f5c5d7/libc-bottom-half/headers/public/wasi/api.h#L1568
using SIFlags = LittleEndian<u16>;
// https://github.com/WebAssembly/wasi-libc/blob/2c2fc9a2fddd0927a66f1c142e65c8dab6f5c5d7/libc-bottom-half/headers/public/wasi/api.h#L1576
struct SDFlags {
using CompatibleType = u8;
struct Bits {
bool rd : 1;
bool wr : 1;
u8 _unused : 6;
};
static_assert(sizeof(Bits) == sizeof(CompatibleType));
union {
Bits bits;
LittleEndian<CompatibleType> data;
};
void serialize_into(Array<Bytes, 1> bytes) const;
static SDFlags read_from(Array<ReadonlyBytes, 1> const& bytes);
};
// https://github.com/WebAssembly/wasi-libc/blob/2c2fc9a2fddd0927a66f1c142e65c8dab6f5c5d7/libc-bottom-half/headers/public/wasi/api.h#L1594
enum class PreOpenType : u8 {
Dir,
};
// https://github.com/WebAssembly/wasi-libc/blob/2c2fc9a2fddd0927a66f1c142e65c8dab6f5c5d7/libc-bottom-half/headers/public/wasi/api.h#L1607
struct PreStatDir {
Size pr_name_len;
void serialize_into(Array<Bytes, 1> bytes) const;
static PreStatDir read_from(Array<ReadonlyBytes, 1> const& bytes);
};
// https://github.com/WebAssembly/wasi-libc/blob/2c2fc9a2fddd0927a66f1c142e65c8dab6f5c5d7/libc-bottom-half/headers/public/wasi/api.h#L1636
struct PreStat {
u8 tag;
union {
PreStatDir dir;
};
void serialize_into(Array<Bytes, 1> bytes) const;
};
// https://github.com/WebAssembly/wasi-libc/blob/2c2fc9a2fddd0927a66f1c142e65c8dab6f5c5d7/libc-bottom-half/headers/public/wasi/api.h#L1676
struct ArgsSizes {
Size count;
Size size;
using SerializationComponents = TypeList<Size, Size>;
void serialize_into(Array<Bytes, 2> bytes) const;
};
// https://github.com/WebAssembly/wasi-libc/blob/2c2fc9a2fddd0927a66f1c142e65c8dab6f5c5d7/libc-bottom-half/headers/public/wasi/api.h#L1708
struct EnvironSizes {
Size count;
Size size;
using SerializationComponents = TypeList<Size, Size>;
void serialize_into(Array<Bytes, 2> bytes) const;
};
// https://github.com/WebAssembly/wasi-libc/blob/2c2fc9a2fddd0927a66f1c142e65c8dab6f5c5d7/libc-bottom-half/headers/public/wasi/api.h#L2664
struct SockRecvResult {
Size size;
ROFlags roflags;
using SerializationComponents = TypeList<Size, ROFlags>;
void serialize_into(Array<Bytes, 2> bytes) const;
};
template<typename TResult, typename Tag = u32>
struct Result {
Result(TResult&& result)
: bits {}
, tag(0)
{
new (&bits) TResult(move(result));
}
Result(Errno&& error)
: bits {}
, tag(1)
{
new (&bits) Errno(error);
}
Optional<TResult&> result() const
{
if (tag == 0)
return *bit_cast<TResult*>(&bits[0]);
return {};
}
Optional<Errno&> error() const
{
if (tag == 1)
return *bit_cast<Errno*>(&bits[0]);
return {};
}
bool is_error() const { return tag == 1; }
template<size_t N>
Errno serialize_into(Array<Bytes, N>&& spans) const
{
if (tag == 1)
return error().value();
ABI::serialize(*result(), move(spans));
return Errno::Success;
}
private:
alignas(max(alignof(TResult), alignof(Errno))) u8 bits[max(sizeof(TResult), sizeof(Errno))];
LittleEndian<Tag> tag;
};
template<typename Tag>
struct Result<void, Tag> {
Result()
: error_bits {}
, tag(0)
{
}
Result(Errno&& error)
: error_bits {}
, tag(1)
{
new (&error_bits) Errno(error);
}
Optional<Empty> result() const
{
if (tag == 0)
return { Empty {} };
return {};
}
Optional<Errno&> error() const
{
if (tag == 1)
return *bit_cast<Errno*>(&error_bits[0]);
return {};
}
bool is_error() const { return tag == 1; }
private:
alignas(Errno) u8 error_bits[sizeof(Errno)];
LittleEndian<Tag> tag;
};
struct Implementation {
struct MappedPath {
LexicalPath host_path;
LexicalPath mapped_path;
mutable Optional<int> opened_fd {};
};
struct Details {
Function<Vector<AK::String>()> provide_arguments;
Function<Vector<AK::String>()> provide_environment;
Function<Vector<MappedPath>()> provide_preopened_directories;
};
explicit Implementation(Details&& details)
: provide_arguments(move(details.provide_arguments))
, provide_environment(move(details.provide_environment))
, provide_preopened_directories(move(details.provide_preopened_directories))
{
// Map all of std{in,out,err} by default.
m_fd_map.insert(0, 0);
m_fd_map.insert(1, 1);
m_fd_map.insert(2, 2);
}
ErrorOr<HostFunction> function_by_name(StringView);
private:
template<auto impl>
HostFunction invocation_of(StringView name) { return ABI::InvocationOf<impl> {}(*this, name); }
ErrorOr<Result<void>> impl$args_get(Configuration&, Pointer<Pointer<u8>> argv, Pointer<u8> argv_buf);
ErrorOr<Result<ArgsSizes>> impl$args_sizes_get(Configuration&);
ErrorOr<Result<void>> impl$environ_get(Configuration&, Pointer<Pointer<u8>> environ, Pointer<u8> environ_buf);
ErrorOr<Result<EnvironSizes>> impl$environ_sizes_get(Configuration&);
ErrorOr<Result<Timestamp>> impl$clock_res_get(Configuration&, ClockID id);
ErrorOr<Result<Timestamp>> impl$clock_time_get(Configuration&, ClockID id, Timestamp precision);
ErrorOr<Result<void>> impl$fd_advise(Configuration&, FD, FileSize offset, FileSize len, Advice);
ErrorOr<Result<void>> impl$fd_allocate(Configuration&, FD, FileSize offset, FileSize len);
ErrorOr<Result<void>> impl$fd_close(Configuration&, FD);
ErrorOr<Result<void>> impl$fd_datasync(Configuration&, FD);
ErrorOr<Result<FDStat>> impl$fd_fdstat_get(Configuration&, FD);
ErrorOr<Result<void>> impl$fd_fdstat_set_flags(Configuration&, FD, FDFlags);
ErrorOr<Result<void>> impl$fd_fdstat_set_rights(Configuration&, FD, Rights fs_rights_base, Rights fs_rights_inheriting);
ErrorOr<Result<FileStat>> impl$fd_filestat_get(Configuration&, FD);
ErrorOr<Result<void>> impl$fd_filestat_set_size(Configuration&, FD, FileSize);
ErrorOr<Result<void>> impl$fd_filestat_set_times(Configuration&, FD, Timestamp atim, Timestamp mtim, FSTFlags);
ErrorOr<Result<Size>> impl$fd_pread(Configuration&, FD, Pointer<IOVec> iovs, Size iovs_len, FileSize offset);
ErrorOr<Result<PreStat>> impl$fd_prestat_get(Configuration&, FD);
ErrorOr<Result<void>> impl$fd_prestat_dir_name(Configuration&, FD, Pointer<u8> path, Size path_len);
ErrorOr<Result<Size>> impl$fd_pwrite(Configuration&, FD, Pointer<CIOVec> iovs, Size iovs_len, FileSize offset);
ErrorOr<Result<Size>> impl$fd_read(Configuration&, FD, Pointer<IOVec> iovs, Size iovs_len);
ErrorOr<Result<Size>> impl$fd_readdir(Configuration&, FD, Pointer<u8> buf, Size buf_len, DirCookie cookie);
ErrorOr<Result<void>> impl$fd_renumber(Configuration&, FD from, FD to);
ErrorOr<Result<FileSize>> impl$fd_seek(Configuration&, FD, FileDelta offset, Whence whence);
ErrorOr<Result<void>> impl$fd_sync(Configuration&, FD);
ErrorOr<Result<FileSize>> impl$fd_tell(Configuration&, FD);
ErrorOr<Result<Size>> impl$fd_write(Configuration&, FD, Pointer<CIOVec> iovs, Size iovs_len);
ErrorOr<Result<void>> impl$path_create_directory(Configuration&, FD, Pointer<u8> path, Size path_len);
ErrorOr<Result<FileStat>> impl$path_filestat_get(Configuration&, FD, LookupFlags, ConstPointer<u8> path, Size path_len);
ErrorOr<Result<void>> impl$path_filestat_set_times(Configuration&, FD, LookupFlags, Pointer<u8> path, Size path_len, Timestamp atim, Timestamp mtim, FSTFlags);
ErrorOr<Result<void>> impl$path_link(Configuration&, FD, LookupFlags, Pointer<u8> old_path, Size old_path_len, FD, Pointer<u8> new_path, Size new_path_len);
ErrorOr<Result<FD>> impl$path_open(Configuration&, FD, LookupFlags, Pointer<u8> path, Size path_len, OFlags, Rights fs_rights_base, Rights fs_rights_inheriting, FDFlags fd_flags);
ErrorOr<Result<Size>> impl$path_readlink(Configuration&, FD, LookupFlags, Pointer<u8> path, Size path_len, Pointer<u8> buf, Size buf_len);
ErrorOr<Result<void>> impl$path_remove_directory(Configuration&, FD, Pointer<u8> path, Size path_len);
ErrorOr<Result<void>> impl$path_rename(Configuration&, FD, Pointer<u8> old_path, Size old_path_len, FD, Pointer<u8> new_path, Size new_path_len);
ErrorOr<Result<void>> impl$path_symlink(Configuration&, Pointer<u8> old_path, Size old_path_len, FD, Pointer<u8> new_path, Size new_path_len);
ErrorOr<Result<void>> impl$path_unlink_file(Configuration&, FD, Pointer<u8> path, Size path_len);
ErrorOr<Result<Size>> impl$poll_oneoff(Configuration&, ConstPointer<Subscription> in, Pointer<Event> out, Size nsubscriptions);
ErrorOr<Result<void>> impl$proc_exit(Configuration&, ExitCode); // Note: noreturn.
ErrorOr<Result<void>> impl$proc_raise(Configuration&, Signal);
ErrorOr<Result<void>> impl$sched_yield(Configuration&);
ErrorOr<Result<void>> impl$random_get(Configuration&, Pointer<u8> buf, Size buf_len);
ErrorOr<Result<FD>> impl$sock_accept(Configuration&, FD fd, FDFlags fd_flags);
ErrorOr<Result<SockRecvResult>> impl$sock_recv(Configuration&, FD fd, Pointer<IOVec> ri_data, Size ri_data_len, RIFlags ri_flags);
ErrorOr<Result<Size>> impl$sock_send(Configuration&, FD fd, Pointer<CIOVec> si_data, Size ri_data_len, SIFlags si_flags);
ErrorOr<Result<void>> impl$sock_shutdown(Configuration&, FD fd, SDFlags how);
Vector<AK::String> const& arguments() const;
Vector<AK::String> const& environment() const;
Vector<MappedPath> const& preopened_directories() const;
using PreopenedDirectoryDescriptor = DistinctNumeric<LittleEndian<size_t>, struct PreopenedDirectoryDescriptor_tag, AK::DistinctNumericFeature::Comparison, AK::DistinctNumericFeature::CastToUnderlying, AK::DistinctNumericFeature::Increment>;
using UnmappedDescriptor = DistinctNumeric<LittleEndian<size_t>, struct UnmappedDescriptor_tag, AK::DistinctNumericFeature::Comparison, AK::DistinctNumericFeature::CastToUnderlying>;
using MappedDescriptor = Variant<u32, PreopenedDirectoryDescriptor>;
using Descriptor = Variant<u32, PreopenedDirectoryDescriptor, UnmappedDescriptor>;
Descriptor map_fd(FD);
public:
Function<Vector<AK::String>()> provide_arguments;
Function<Vector<AK::String>()> provide_environment;
Function<Vector<MappedPath>()> provide_preopened_directories;
private:
struct Cache {
Optional<Vector<AK::String>> cached_arguments;
Optional<Vector<AK::String>> cached_environment;
Optional<Vector<MappedPath>> cached_preopened_directories;
};
mutable Cache cache {};
RedBlackTree<u32, MappedDescriptor> m_fd_map;
size_t m_first_unmapped_preopened_directory_index { 0 };
};
#undef IMPL
}
namespace Wasm::Wasi::ABI {
template<typename T, typename... Args>
struct ToCompatibleValue<DistinctNumeric<T, Args...>> {
using Type = typename ToCompatibleValue<T>::Type;
};
template<typename T>
struct ToCompatibleValue<LittleEndian<T>> {
using Type = MakeSigned<T>;
};
template<typename T>
requires(requires { declval<typename T::CompatibleType>(); })
struct ToCompatibleValue<T> {
using Type = MakeSigned<typename T::CompatibleType>;
};
template<Integral T>
struct ToCompatibleValue<T> {
using Type = MakeSigned<T>;
};
template<Enum T>
struct ToCompatibleValue<T> {
using Type = MakeSigned<UnderlyingType<T>>;
};
}
template<typename T>
struct AK::Formatter<Wasm::Wasi::LittleEndian<T>> : AK::Formatter<T> {
ErrorOr<void> format(FormatBuilder& builder, Wasm::Wasi::LittleEndian<T> value)
{
return Formatter<T>::format(builder, value.operator T());
}
};
|