aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/syscall/libc_wasip2.go
blob: ea3d3327dae75621e7bab43489c2b1c4ab16b5f5 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
//go:build wasip2

// mini libc wrapping wasi preview2 calls in a libc api

package syscall

import (
	"unsafe"

	"internal/wasi/cli/v0.2.0/environment"
	"internal/wasi/cli/v0.2.0/stderr"
	"internal/wasi/cli/v0.2.0/stdin"
	"internal/wasi/cli/v0.2.0/stdout"
	wallclock "internal/wasi/clocks/v0.2.0/wall-clock"
	"internal/wasi/filesystem/v0.2.0/preopens"
	"internal/wasi/filesystem/v0.2.0/types"
	ioerror "internal/wasi/io/v0.2.0/error"
	"internal/wasi/io/v0.2.0/streams"
	"internal/wasi/random/v0.2.0/random"

	"github.com/ydnar/wasm-tools-go/cm"
)

func goString(cstr *byte) string {
	return unsafe.String(cstr, strlen(cstr))
}

//export strlen
func strlen(cstr *byte) uintptr {
	if cstr == nil {
		return 0
	}
	ptr := unsafe.Pointer(cstr)
	var i uintptr
	for p := (*byte)(ptr); *p != 0; p = (*byte)(unsafe.Add(unsafe.Pointer(p), 1)) {
		i++
	}
	return i
}

// ssize_t write(int fd, const void *buf, size_t count)
//
//export write
func write(fd int32, buf *byte, count uint) int {
	if stream, ok := wasiStreams[fd]; ok {
		return writeStream(stream, buf, count, 0)
	}

	stream, ok := wasiFiles[fd]
	if !ok {
		libcErrno = EBADF
		return -1
	}
	if stream.d == cm.ResourceNone {
		libcErrno = EBADF
		return -1
	}

	n := pwrite(fd, buf, count, int64(stream.offset))
	if n == -1 {
		return -1
	}
	stream.offset += int64(n)
	return int(n)
}

// ssize_t read(int fd, void *buf, size_t count);
//
//export read
func read(fd int32, buf *byte, count uint) int {
	if stream, ok := wasiStreams[fd]; ok {
		return readStream(stream, buf, count, 0)
	}

	stream, ok := wasiFiles[fd]
	if !ok {
		libcErrno = EBADF
		return -1
	}
	if stream.d == cm.ResourceNone {
		libcErrno = EBADF
		return -1
	}

	n := pread(fd, buf, count, int64(stream.offset))
	if n == -1 {
		// error during pread
		return -1
	}
	stream.offset += int64(n)
	return int(n)
}

// At the moment, each time we have a file read or write we create a new stream.  Future implementations
// could change the current in or out file stream lazily.  We could do this by tracking input and output
// offsets individually, and if they don't match the current main offset, reopen the file stream at that location.

type wasiFile struct {
	d      types.Descriptor
	oflag  int32 // orignal open flags: O_RDONLY, O_WRONLY, O_RDWR
	offset int64 // current fd offset; updated with each read/write
	refs   int
}

// Need to figure out which system calls we're using:
//   stdin/stdout/stderr want streams, so we use stream read/write
//   but for regular files we can use the descriptor and explicitly write a buffer to the offset?
//   The mismatch comes from trying to combine these.

var wasiFiles map[int32]*wasiFile = make(map[int32]*wasiFile)

func findFreeFD() int32 {
	var newfd int32
	for wasiStreams[newfd] != nil || wasiFiles[newfd] != nil {
		newfd++
	}
	return newfd
}

var wasiErrno ioerror.Error

type wasiStream struct {
	in   *streams.InputStream
	out  *streams.OutputStream
	refs int
}

// This holds entries for stdin/stdout/stderr.

var wasiStreams map[int32]*wasiStream

func init() {
	sin := stdin.GetStdin()
	sout := stdout.GetStdout()
	serr := stderr.GetStderr()
	wasiStreams = map[int32]*wasiStream{
		0: &wasiStream{
			in:   &sin,
			refs: 1,
		},
		1: &wasiStream{
			out:  &sout,
			refs: 1,
		},
		2: &wasiStream{
			out:  &serr,
			refs: 1,
		},
	}
}

func readStream(stream *wasiStream, buf *byte, count uint, offset int64) int {
	if stream.in == nil {
		// not a stream we can read from
		libcErrno = EBADF
		return -1
	}

	if offset != 0 {
		libcErrno = EINVAL
		return -1
	}

	libcErrno = 0
	result := stream.in.BlockingRead(uint64(count))
	if err := result.Err(); err != nil {
		if err.Closed() {
			libcErrno = 0
			return 0
		} else if err := err.LastOperationFailed(); err != nil {
			wasiErrno = *err
			libcErrno = EWASIERROR
		}
		return -1
	}

	dst := unsafe.Slice(buf, count)
	list := result.OK()
	copy(dst, list.Slice())
	return int(list.Len())
}

func writeStream(stream *wasiStream, buf *byte, count uint, offset int64) int {
	if stream.out == nil {
		// not a stream we can write to
		libcErrno = EBADF
		return -1
	}

	if offset != 0 {
		libcErrno = EINVAL
		return -1
	}

	src := unsafe.Slice(buf, count)
	var remaining = count

	// The blocking-write-and-flush call allows a maximum of 4096 bytes at a time.
	// We loop here by instead of doing subscribe/check-write/poll-one/write by hand.
	for remaining > 0 {
		len := uint(4096)
		if len > remaining {
			len = remaining
		}
		result := stream.out.BlockingWriteAndFlush(cm.ToList(src[:len]))
		if err := result.Err(); err != nil {
			if err.Closed() {
				libcErrno = 0
				return 0
			} else if err := err.LastOperationFailed(); err != nil {
				wasiErrno = *err
				libcErrno = EWASIERROR
			}
			return -1
		}
		remaining -= len
	}

	return int(count)
}

//go:linkname memcpy runtime.memcpy
func memcpy(dst, src unsafe.Pointer, size uintptr)

// ssize_t pread(int fd, void *buf, size_t count, off_t offset);
//
//export pread
func pread(fd int32, buf *byte, count uint, offset int64) int {
	// TODO(dgryski): Need to be consistent about all these checks; EBADF/EINVAL/... ?

	if stream, ok := wasiStreams[fd]; ok {
		return readStream(stream, buf, count, offset)

	}

	streams, ok := wasiFiles[fd]
	if !ok {
		// TODO(dgryski): EINVAL?
		libcErrno = EBADF
		return -1
	}
	if streams.d == cm.ResourceNone {
		libcErrno = EBADF
		return -1
	}
	if streams.oflag&O_RDONLY == 0 {
		libcErrno = EBADF
		return -1
	}

	result := streams.d.Read(types.FileSize(count), types.FileSize(offset))
	if err := result.Err(); err != nil {
		libcErrno = errorCodeToErrno(*err)
		return -1
	}

	list := result.OK().F0
	copy(unsafe.Slice(buf, count), list.Slice())

	// TODO(dgryski): EOF bool is ignored?
	return int(list.Len())
}

// ssize_t pwrite(int fd, void *buf, size_t count, off_t offset);
//
//export pwrite
func pwrite(fd int32, buf *byte, count uint, offset int64) int {
	// TODO(dgryski): Need to be consistent about all these checks; EBADF/EINVAL/... ?
	if stream, ok := wasiStreams[fd]; ok {
		return writeStream(stream, buf, count, 0)
	}

	streams, ok := wasiFiles[fd]
	if !ok {
		// TODO(dgryski): EINVAL?
		libcErrno = EBADF
		return -1
	}
	if streams.d == cm.ResourceNone {
		libcErrno = EBADF
		return -1
	}
	if streams.oflag&O_WRONLY == 0 {
		libcErrno = EBADF
		return -1
	}

	result := streams.d.Write(cm.NewList(buf, count), types.FileSize(offset))
	if err := result.Err(); err != nil {
		// TODO(dgryski):
		libcErrno = errorCodeToErrno(*err)
		return -1
	}

	return int(*result.OK())
}

// ssize_t lseek(int fd, off_t offset, int whence);
//
//export lseek
func lseek(fd int32, offset int64, whence int) int64 {
	if _, ok := wasiStreams[fd]; ok {
		// can't lseek a stream
		libcErrno = EBADF
		return -1
	}

	stream, ok := wasiFiles[fd]
	if !ok {
		libcErrno = EBADF
		return -1
	}
	if stream.d == cm.ResourceNone {
		libcErrno = EBADF
		return -1
	}

	switch whence {
	case 0: // SEEK_SET
		stream.offset = offset
	case 1: // SEEK_CUR
		stream.offset += offset
	case 2: // SEEK_END
		result := stream.d.Stat()
		if err := result.Err(); err != nil {
			libcErrno = errorCodeToErrno(*err)
			return -1
		}
		stream.offset = int64(result.OK().Size) + offset
	}

	return int64(stream.offset)
}

// int close(int fd)
//
//export close
func close(fd int32) int32 {
	if streams, ok := wasiStreams[fd]; ok {
		if streams.out != nil {
			// ignore any error
			streams.out.BlockingFlush()
		}

		if streams.refs--; streams.refs == 0 {
			if streams.out != nil {
				streams.out.ResourceDrop()
				streams.out = nil
			}
			if streams.in != nil {
				streams.in.ResourceDrop()
				streams.in = nil
			}
		}

		delete(wasiStreams, fd)
		return 0
	}

	streams, ok := wasiFiles[fd]
	if !ok {
		libcErrno = EBADF
		return -1
	}
	if streams.refs--; streams.refs == 0 && streams.d != cm.ResourceNone {
		streams.d.ResourceDrop()
		streams.d = 0
	}
	delete(wasiFiles, fd)

	return 0
}

// int dup(int fd)
//
//export dup
func dup(fd int32) int32 {
	// is fd a stream?
	if stream, ok := wasiStreams[fd]; ok {
		newfd := findFreeFD()
		stream.refs++
		wasiStreams[newfd] = stream
		return newfd
	}

	// is fd a file?
	if file, ok := wasiFiles[fd]; ok {
		// scan for first free file descriptor
		newfd := findFreeFD()
		file.refs++
		wasiFiles[newfd] = file
		return newfd
	}

	// unknown file descriptor
	libcErrno = EBADF
	return -1
}

// void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset);
//
//export mmap
func mmap(addr unsafe.Pointer, length uintptr, prot, flags, fd int32, offset uintptr) unsafe.Pointer {
	libcErrno = ENOSYS
	return unsafe.Pointer(^uintptr(0))
}

// int munmap(void *addr, size_t length);
//
//export munmap
func munmap(addr unsafe.Pointer, length uintptr) int32 {
	libcErrno = ENOSYS
	return -1
}

// int mprotect(void *addr, size_t len, int prot);
//
//export mprotect
func mprotect(addr unsafe.Pointer, len uintptr, prot int32) int32 {
	libcErrno = ENOSYS
	return -1
}

// int chmod(const char *pathname, mode_t mode);
//
//export chmod
func chmod(pathname *byte, mode uint32) int32 {
	return 0
}

// int mkdir(const char *pathname, mode_t mode);
//
//export mkdir
func mkdir(pathname *byte, mode uint32) int32 {

	path := goString(pathname)
	dir, relPath := findPreopenForPath(path)
	if dir.d == cm.ResourceNone {
		libcErrno = EACCES
		return -1
	}

	result := dir.d.CreateDirectoryAt(relPath)
	if err := result.Err(); err != nil {
		libcErrno = errorCodeToErrno(*err)
		return -1
	}

	return 0
}

// int rmdir(const char *pathname);
//
//export rmdir
func rmdir(pathname *byte) int32 {
	path := goString(pathname)
	dir, relPath := findPreopenForPath(path)
	if dir.d == cm.ResourceNone {
		libcErrno = EACCES
		return -1
	}

	result := dir.d.RemoveDirectoryAt(relPath)
	if err := result.Err(); err != nil {
		libcErrno = errorCodeToErrno(*err)
		return -1
	}

	return 0
}

// int rename(const char *from, *to);
//
//export rename
func rename(from, to *byte) int32 {
	fromPath := goString(from)
	fromDir, fromRelPath := findPreopenForPath(fromPath)
	if fromDir.d == cm.ResourceNone {
		libcErrno = EACCES
		return -1
	}

	toPath := goString(to)
	toDir, toRelPath := findPreopenForPath(toPath)
	if toDir.d == cm.ResourceNone {
		libcErrno = EACCES
		return -1
	}

	result := fromDir.d.RenameAt(fromRelPath, toDir.d, toRelPath)
	if err := result.Err(); err != nil {
		libcErrno = errorCodeToErrno(*err)
		return -1
	}

	return 0
}

// int symlink(const char *from, *to);
//
//export symlink
func symlink(from, to *byte) int32 {
	fromPath := goString(from)
	fromDir, fromRelPath := findPreopenForPath(fromPath)
	if fromDir.d == cm.ResourceNone {
		libcErrno = EACCES
		return -1
	}

	toPath := goString(to)
	toDir, toRelPath := findPreopenForPath(toPath)
	if toDir.d == cm.ResourceNone {
		libcErrno = EACCES
		return -1
	}

	if fromDir.d != toDir.d {
		libcErrno = EACCES
		return -1
	}

	// TODO(dgryski): check fromDir == toDir?

	result := fromDir.d.SymlinkAt(fromRelPath, toRelPath)
	if err := result.Err(); err != nil {
		libcErrno = errorCodeToErrno(*err)
		return -1
	}

	return 0
}

// int link(const char *from, *to);
//
//export link
func link(from, to *byte) int32 {
	fromPath := goString(from)
	fromDir, fromRelPath := findPreopenForPath(fromPath)
	if fromDir.d == cm.ResourceNone {
		libcErrno = EACCES
		return -1
	}

	toPath := goString(to)
	toDir, toRelPath := findPreopenForPath(toPath)
	if toDir.d == cm.ResourceNone {
		libcErrno = EACCES
		return -1
	}

	if fromDir.d != toDir.d {
		libcErrno = EACCES
		return -1
	}

	// TODO(dgryski): check fromDir == toDir?

	result := fromDir.d.LinkAt(0, fromRelPath, toDir.d, toRelPath)
	if err := result.Err(); err != nil {
		libcErrno = errorCodeToErrno(*err)
		return -1
	}

	return 0
}

// int fsync(int fd);
//
//export fsync
func fsync(fd int32) int32 {
	if _, ok := wasiStreams[fd]; ok {
		// can't sync a stream
		libcErrno = EBADF
		return -1
	}

	streams, ok := wasiFiles[fd]
	if !ok {
		libcErrno = EBADF
		return -1
	}
	if streams.d == cm.ResourceNone {
		libcErrno = EBADF
		return -1
	}
	if streams.oflag&O_WRONLY == 0 {
		libcErrno = EBADF
		return -1
	}

	result := streams.d.SyncData()
	if err := result.Err(); err != nil {
		libcErrno = errorCodeToErrno(*err)
		return -1
	}

	return 0
}

// ssize_t readlink(const char *path, void *buf, size_t count);
//
//export readlink
func readlink(pathname *byte, buf *byte, count uint) int {
	path := goString(pathname)
	dir, relPath := findPreopenForPath(path)
	if dir.d == cm.ResourceNone {
		libcErrno = EACCES
		return -1
	}

	result := dir.d.ReadLinkAt(relPath)
	if err := result.Err(); err != nil {
		libcErrno = errorCodeToErrno(*err)
		return -1
	}

	s := *result.OK()
	size := uintptr(count)
	if size > uintptr(len(s)) {
		size = uintptr(len(s))
	}

	memcpy(unsafe.Pointer(buf), unsafe.Pointer(unsafe.StringData(s)), size)
	return int(size)
}

// int unlink(const char *pathname);
//
//export unlink
func unlink(pathname *byte) int32 {
	path := goString(pathname)
	dir, relPath := findPreopenForPath(path)
	if dir.d == cm.ResourceNone {
		libcErrno = EACCES
		return -1
	}

	result := dir.d.UnlinkFileAt(relPath)
	if err := result.Err(); err != nil {
		libcErrno = errorCodeToErrno(*err)
		return -1
	}

	return 0
}

// int getpagesize(void);
//
//export getpagesize
func getpagesize() int {
	return 65536
}

// int stat(const char *path, struct stat * buf);
//
//export stat
func stat(pathname *byte, dst *Stat_t) int32 {
	path := goString(pathname)
	dir, relPath := findPreopenForPath(path)
	if dir.d == cm.ResourceNone {
		libcErrno = EACCES
		return -1
	}

	result := dir.d.StatAt(0, relPath)
	if err := result.Err(); err != nil {
		libcErrno = errorCodeToErrno(*err)
		return -1
	}

	setStatFromWASIStat(dst, result.OK())

	return 0
}

// int fstat(int fd, struct stat * buf);
//
//export fstat
func fstat(fd int32, dst *Stat_t) int32 {
	if _, ok := wasiStreams[fd]; ok {
		// TODO(dgryski): fill in stat buffer for stdin etc
		return -1
	}

	stream, ok := wasiFiles[fd]
	if !ok {
		libcErrno = EBADF
		return -1
	}
	if stream.d == cm.ResourceNone {
		libcErrno = EBADF
		return -1
	}
	result := stream.d.Stat()
	if err := result.Err(); err != nil {
		libcErrno = errorCodeToErrno(*err)
		return -1
	}

	setStatFromWASIStat(dst, result.OK())

	return 0
}

func setStatFromWASIStat(sstat *Stat_t, wstat *types.DescriptorStat) {
	// This will cause problems for people who want to compare inodes
	sstat.Dev = 0
	sstat.Ino = 0
	sstat.Rdev = 0

	sstat.Nlink = uint64(wstat.LinkCount)

	sstat.Mode = p2fileTypeToStatType(wstat.Type)

	// No uid/gid
	sstat.Uid = 0
	sstat.Gid = 0
	sstat.Size = int64(wstat.Size)

	// made up numbers
	sstat.Blksize = 512
	sstat.Blocks = (sstat.Size + 511) / int64(sstat.Blksize)

	setOptTime := func(t *Timespec, o *wallclock.DateTime) {
		t.Sec = 0
		t.Nsec = 0
		if o != nil {
			t.Sec = int32(o.Seconds)
			t.Nsec = int64(o.Nanoseconds)
		}
	}

	setOptTime(&sstat.Atim, wstat.DataAccessTimestamp.Some())
	setOptTime(&sstat.Mtim, wstat.DataModificationTimestamp.Some())
	setOptTime(&sstat.Ctim, wstat.StatusChangeTimestamp.Some())
}

// int lstat(const char *path, struct stat * buf);
//
//export lstat
func lstat(pathname *byte, dst *Stat_t) int32 {
	path := goString(pathname)
	dir, relPath := findPreopenForPath(path)
	if dir.d == cm.ResourceNone {
		libcErrno = EACCES
		return -1
	}

	result := dir.d.StatAt(0, relPath)
	if err := result.Err(); err != nil {
		libcErrno = errorCodeToErrno(*err)
		return -1
	}

	setStatFromWASIStat(dst, result.OK())

	return 0
}

func init() {
	populateEnvironment()
	populatePreopens()
}

type wasiDir struct {
	d    types.Descriptor // wasip2 descriptor
	root string           // root path for this descriptor
	rel  string           // relative path under root
}

var libcCWD wasiDir

var wasiPreopens map[string]types.Descriptor

func populatePreopens() {

	var cwd string

	// find CWD
	result := environment.InitialCWD()
	if s := result.Some(); s != nil {
		cwd = *s
	} else if s, _ := Getenv("PWD"); s != "" {
		cwd = s
	}

	dirs := preopens.GetDirectories().Slice()
	preopens := make(map[string]types.Descriptor, len(dirs))
	for _, tup := range dirs {
		desc, path := tup.F0, tup.F1
		if path == cwd {
			libcCWD.d = desc
			libcCWD.root = path
			libcCWD.rel = ""
		}
		preopens[path] = desc
	}
	wasiPreopens = preopens
}

// -- BEGIN fs_wasip1.go --
// The following section has been taken from upstream Go with the following copyright:
// Copyright 2023 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:nosplit
func appendCleanPath(buf []byte, path string, lookupParent bool) ([]byte, bool) {
	i := 0
	for i < len(path) {
		for i < len(path) && path[i] == '/' {
			i++
		}

		j := i
		for j < len(path) && path[j] != '/' {
			j++
		}

		s := path[i:j]
		i = j

		switch s {
		case "":
			continue
		case ".":
			continue
		case "..":
			if !lookupParent {
				k := len(buf)
				for k > 0 && buf[k-1] != '/' {
					k--
				}
				for k > 1 && buf[k-1] == '/' {
					k--
				}
				buf = buf[:k]
				if k == 0 {
					lookupParent = true
				} else {
					s = ""
					continue
				}
			}
		default:
			lookupParent = false
		}

		if len(buf) > 0 && buf[len(buf)-1] != '/' {
			buf = append(buf, '/')
		}
		buf = append(buf, s...)
	}
	return buf, lookupParent
}

// joinPath concatenates dir and file paths, producing a cleaned path where
// "." and ".." have been removed, unless dir is relative and the references
// to parent directories in file represented a location relative to a parent
// of dir.
//
// This function is used for path resolution of all wasi functions expecting
// a path argument; the returned string is heap allocated, which we may want
// to optimize in the future. Instead of returning a string, the function
// could append the result to an output buffer that the functions in this
// file can manage to have allocated on the stack (e.g. initializing to a
// fixed capacity). Since it will significantly increase code complexity,
// we prefer to optimize for readability and maintainability at this time.
func joinPath(dir, file string) string {
	buf := make([]byte, 0, len(dir)+len(file)+1)
	if isAbs(dir) {
		buf = append(buf, '/')
	}

	buf, lookupParent := appendCleanPath(buf, dir, true)
	buf, _ = appendCleanPath(buf, file, lookupParent)
	// The appendCleanPath function cleans the path so it does not inject
	// references to the current directory. If both the dir and file args
	// were ".", this results in the output buffer being empty so we handle
	// this condition here.
	if len(buf) == 0 {
		buf = append(buf, '.')
	}
	// If the file ended with a '/' we make sure that the output also ends
	// with a '/'. This is needed to ensure that programs have a mechanism
	// to represent dereferencing symbolic links pointing to directories.
	if buf[len(buf)-1] != '/' && isDir(file) {
		buf = append(buf, '/')
	}
	return unsafe.String(&buf[0], len(buf))
}

func isAbs(path string) bool {
	return hasPrefix(path, "/")
}

func isDir(path string) bool {
	return hasSuffix(path, "/")
}

func hasPrefix(s, p string) bool {
	return len(s) >= len(p) && s[:len(p)] == p
}

func hasSuffix(s, x string) bool {
	return len(s) >= len(x) && s[len(s)-len(x):] == x
}

// findPreopenForPath finds which preopen it relates to and return that descriptor/root and the path relative to that directory descriptor/root
func findPreopenForPath(path string) (wasiDir, string) {
	dir := "/"
	var wasidir wasiDir

	if !isAbs(path) {
		dir = libcCWD.root
		wasidir = libcCWD
		if libcCWD.rel != "" && libcCWD.rel != "." && libcCWD.rel != "./" {
			path = libcCWD.rel + "/" + path
		}
	}
	path = joinPath(dir, path)

	var best string
	for k, v := range wasiPreopens {
		if len(k) > len(best) && hasPrefix(path, k) {
			wasidir = wasiDir{d: v, root: k}
			best = wasidir.root
		}
	}

	if hasPrefix(path, wasidir.root) {
		path = path[len(wasidir.root):]
	}
	for isAbs(path) {
		path = path[1:]
	}
	if len(path) == 0 {
		path = "."
	}

	return wasidir, path
}

// -- END fs_wasip1.go --

// int open(const char *pathname, int flags, mode_t mode);
//
//export open
func open(pathname *byte, flags int32, mode uint32) int32 {
	path := goString(pathname)
	dir, relPath := findPreopenForPath(path)
	if dir.d == cm.ResourceNone {
		libcErrno = EACCES
		return -1
	}

	var dflags types.DescriptorFlags
	if (flags & O_RDONLY) == O_RDONLY {
		dflags |= types.DescriptorFlagsRead
	}
	if (flags & O_WRONLY) == O_WRONLY {
		dflags |= types.DescriptorFlagsWrite
	}

	var oflags types.OpenFlags
	if flags&O_CREAT == O_CREAT {
		oflags |= types.OpenFlagsCreate
	}
	if flags&O_DIRECTORY == O_DIRECTORY {
		oflags |= types.OpenFlagsDirectory
	}
	if flags&O_EXCL == O_EXCL {
		oflags |= types.OpenFlagsExclusive
	}
	if flags&O_TRUNC == O_TRUNC {
		oflags |= types.OpenFlagsTruncate
	}

	// By default, follow symlinks for open() unless O_NOFOLLOW was passed
	var pflags types.PathFlags = types.PathFlagsSymlinkFollow
	if flags&O_NOFOLLOW == O_NOFOLLOW {
		// O_NOFOLLOW was passed, so turn off SymlinkFollow
		pflags &^= types.PathFlagsSymlinkFollow
	}

	result := dir.d.OpenAt(pflags, relPath, oflags, dflags)
	if err := result.Err(); err != nil {
		libcErrno = errorCodeToErrno(*err)
		return -1
	}

	stream := wasiFile{
		d:     *result.OK(),
		oflag: flags,
		refs:  1,
	}

	if flags&(O_WRONLY|O_APPEND) == (O_WRONLY | O_APPEND) {
		result := stream.d.Stat()
		if err := result.Err(); err != nil {
			libcErrno = errorCodeToErrno(*err)
			return -1
		}
		stream.offset = int64(result.OK().Size)
	}

	libcfd := findFreeFD()

	wasiFiles[libcfd] = &stream

	return int32(libcfd)
}

func errorCodeToErrno(err types.ErrorCode) Errno {
	switch err {
	case types.ErrorCodeAccess:
		return EACCES
	case types.ErrorCodeWouldBlock:
		return EAGAIN
	case types.ErrorCodeAlready:
		return EALREADY
	case types.ErrorCodeBadDescriptor:
		return EBADF
	case types.ErrorCodeBusy:
		return EBUSY
	case types.ErrorCodeDeadlock:
		return EDEADLK
	case types.ErrorCodeQuota:
		return EDQUOT
	case types.ErrorCodeExist:
		return EEXIST
	case types.ErrorCodeFileTooLarge:
		return EFBIG
	case types.ErrorCodeIllegalByteSequence:
		return EILSEQ
	case types.ErrorCodeInProgress:
		return EINPROGRESS
	case types.ErrorCodeInterrupted:
		return EINTR
	case types.ErrorCodeInvalid:
		return EINVAL
	case types.ErrorCodeIO:
		return EIO
	case types.ErrorCodeIsDirectory:
		return EISDIR
	case types.ErrorCodeLoop:
		return ELOOP
	case types.ErrorCodeTooManyLinks:
		return EMLINK
	case types.ErrorCodeMessageSize:
		return EMSGSIZE
	case types.ErrorCodeNameTooLong:
		return ENAMETOOLONG
	case types.ErrorCodeNoDevice:
		return ENODEV
	case types.ErrorCodeNoEntry:
		return ENOENT
	case types.ErrorCodeNoLock:
		return ENOLCK
	case types.ErrorCodeInsufficientMemory:
		return ENOMEM
	case types.ErrorCodeInsufficientSpace:
		return ENOSPC
	case types.ErrorCodeNotDirectory:
		return ENOTDIR
	case types.ErrorCodeNotEmpty:
		return ENOTEMPTY
	case types.ErrorCodeNotRecoverable:
		return ENOTRECOVERABLE
	case types.ErrorCodeUnsupported:
		return ENOSYS
	case types.ErrorCodeNoTTY:
		return ENOTTY
	case types.ErrorCodeNoSuchDevice:
		return ENXIO
	case types.ErrorCodeOverflow:
		return EOVERFLOW
	case types.ErrorCodeNotPermitted:
		return EPERM
	case types.ErrorCodePipe:
		return EPIPE
	case types.ErrorCodeReadOnly:
		return EROFS
	case types.ErrorCodeInvalidSeek:
		return ESPIPE
	case types.ErrorCodeTextFileBusy:
		return ETXTBSY
	case types.ErrorCodeCrossDevice:
		return EXDEV
	}
	return Errno(err)
}

type libc_DIR struct {
	d types.DirectoryEntryStream
}

// DIR *fdopendir(int);
//
//export fdopendir
func fdopendir(fd int32) unsafe.Pointer {
	if _, ok := wasiStreams[fd]; ok {
		libcErrno = EBADF
		return nil
	}

	stream, ok := wasiFiles[fd]
	if !ok {
		libcErrno = EBADF
		return nil
	}
	if stream.d == cm.ResourceNone {
		libcErrno = EBADF
		return nil
	}

	result := stream.d.ReadDirectory()
	if err := result.Err(); err != nil {
		libcErrno = errorCodeToErrno(*err)
		return nil
	}

	return unsafe.Pointer(&libc_DIR{d: *result.OK()})
}

// int fdclosedir(DIR *);
//
//export fdclosedir
func fdclosedir(dirp unsafe.Pointer) int32 {
	if dirp == nil {
		return 0

	}
	dir := (*libc_DIR)(dirp)
	if dir.d == cm.ResourceNone {
		return 0
	}

	dir.d.ResourceDrop()
	dir.d = cm.ResourceNone

	return 0
}

// struct dirent *readdir(DIR *);
//
//export readdir
func readdir(dirp unsafe.Pointer) *Dirent {
	if dirp == nil {
		return nil

	}
	dir := (*libc_DIR)(dirp)
	if dir.d == cm.ResourceNone {
		return nil
	}

	result := dir.d.ReadDirectoryEntry()
	if err := result.Err(); err != nil {
		libcErrno = errorCodeToErrno(*err)
		return nil
	}

	entry := result.OK().Some()
	if entry == nil {
		libcErrno = 0
		return nil
	}

	// The dirent C struct uses a flexible array member to indicate that the
	// directory name is laid out in memory right after the struct data:
	//
	// struct dirent {
	//   ino_t d_ino;
	//   unsigned char d_type;
	//   char d_name[];
	// };
	buf := make([]byte, unsafe.Sizeof(Dirent{})+uintptr(len(entry.Name)))
	dirent := (*Dirent)((unsafe.Pointer)(&buf[0]))

	// No inodes in wasi
	dirent.Ino = 0
	dirent.Type = p2fileTypeToDirentType(entry.Type)
	copy(buf[unsafe.Offsetof(dirent.Type)+1:], entry.Name)

	return dirent
}

func p2fileTypeToDirentType(t types.DescriptorType) uint8 {
	switch t {
	case types.DescriptorTypeUnknown:
		return DT_UNKNOWN
	case types.DescriptorTypeBlockDevice:
		return DT_BLK
	case types.DescriptorTypeCharacterDevice:
		return DT_CHR
	case types.DescriptorTypeDirectory:
		return DT_DIR
	case types.DescriptorTypeFIFO:
		return DT_FIFO
	case types.DescriptorTypeSymbolicLink:
		return DT_LNK
	case types.DescriptorTypeRegularFile:
		return DT_REG
	case types.DescriptorTypeSocket:
		return DT_FIFO
	}

	return DT_UNKNOWN
}

func p2fileTypeToStatType(t types.DescriptorType) uint32 {
	switch t {
	case types.DescriptorTypeUnknown:
		return 0
	case types.DescriptorTypeBlockDevice:
		return S_IFBLK
	case types.DescriptorTypeCharacterDevice:
		return S_IFCHR
	case types.DescriptorTypeDirectory:
		return S_IFDIR
	case types.DescriptorTypeFIFO:
		return S_IFIFO
	case types.DescriptorTypeSymbolicLink:
		return S_IFLNK
	case types.DescriptorTypeRegularFile:
		return S_IFREG
	case types.DescriptorTypeSocket:
		return S_IFSOCK
	}

	return 0
}

var libc_envs map[string]string

func populateEnvironment() {
	libc_envs = make(map[string]string)
	for _, kv := range environment.GetEnvironment().Slice() {
		libc_envs[kv[0]] = kv[1]
	}
}

// char * getenv(const char *name);
//
//export getenv
func getenv(key *byte) *byte {
	k := goString(key)

	v, ok := libc_envs[k]
	if !ok {
		return nil
	}

	// The new allocation is zero-filled; allocating an extra byte and then
	// copying the data over will leave the last byte untouched,
	// null-terminating the string.
	vbytes := make([]byte, len(v)+1)
	copy(vbytes, v)
	return unsafe.SliceData(vbytes)
}

// int setenv(const char *name, const char *value, int overwrite);
//
//export setenv
func setenv(key, value *byte, overwrite int) int {
	k := goString(key)
	if _, ok := libc_envs[k]; ok && overwrite == 0 {
		return 0
	}

	v := goString(value)
	libc_envs[k] = v

	return 0
}

// int unsetenv(const char *name);
//
//export unsetenv
func unsetenv(key *byte) int {
	k := goString(key)
	delete(libc_envs, k)
	return 0
}

// void arc4random_buf (void *, size_t);
//
//export arc4random_buf
func arc4random_buf(p unsafe.Pointer, l uint) {
	result := random.GetRandomBytes(uint64(l))
	s := result.Slice()
	memcpy(unsafe.Pointer(p), unsafe.Pointer(unsafe.SliceData(s)), uintptr(l))
}

// int chdir(char *name)
//
//export chdir
func chdir(name *byte) int {
	path := goString(name) + "/"

	if !isAbs(path) {
		path = joinPath(libcCWD.root+"/"+libcCWD.rel+"/", path)
	}

	if path == "." {
		return 0
	}

	dir, rel := findPreopenForPath(path)
	if dir.d == cm.ResourceNone {
		libcErrno = EACCES
		return -1
	}

	result := dir.d.OpenAt(types.PathFlagsSymlinkFollow, rel, types.OpenFlagsDirectory, types.DescriptorFlagsRead)
	if err := result.Err(); err != nil {
		libcErrno = errorCodeToErrno(*err)
		return -1
	}

	libcCWD = dir
	// keep the same cwd base but update "rel" to point to new base path
	libcCWD.rel = rel

	return 0
}

// char *getcwd(char *buf, size_t size)
//
//export getcwd
func getcwd(buf *byte, size uint) *byte {

	cwd := libcCWD.root
	if libcCWD.rel != "" && libcCWD.rel != "." && libcCWD.rel != "./" {
		cwd += libcCWD.rel
	}

	if buf == nil {
		b := make([]byte, len(cwd)+1)
		buf = unsafe.SliceData(b)
	} else if size == 0 {
		libcErrno = EINVAL
		return nil
	}

	if size < uint(len(cwd)+1) {
		libcErrno = ERANGE
		return nil
	}

	s := unsafe.Slice(buf, size)
	s[size-1] = 0 // Enforce NULL termination
	copy(s, cwd)
	return buf
}