-
Notifications
You must be signed in to change notification settings - Fork 243
/
fs_test.go
1353 lines (1228 loc) · 39.8 KB
/
fs_test.go
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
package wasi_snapshot_preview1
import (
"bytes"
"io"
"io/fs"
"math"
"os"
"path"
"testing"
"testing/fstest"
"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/api"
"github.com/tetratelabs/wazero/internal/testing/require"
"github.com/tetratelabs/wazero/internal/wasm"
)
// Test_fdAdvise only tests it is stubbed for GrainLang per #271
func Test_fdAdvise(t *testing.T) {
log := requireErrnoNosys(t, functionFdAdvise, 0, 0, 0, 0)
require.Equal(t, `
--> proxy.fd_advise(fd=0,offset=0,len=0,result.advice=0)
--> wasi_snapshot_preview1.fd_advise(fd=0,offset=0,len=0,result.advice=0)
<-- ENOSYS
<-- (52)
`, log)
}
// Test_fdAllocate only tests it is stubbed for GrainLang per #271
func Test_fdAllocate(t *testing.T) {
log := requireErrnoNosys(t, functionFdAllocate, 0, 0, 0)
require.Equal(t, `
--> proxy.fd_allocate(fd=0,offset=0,len=0)
--> wasi_snapshot_preview1.fd_allocate(fd=0,offset=0,len=0)
<-- ENOSYS
<-- (52)
`, log)
}
func Test_fdClose(t *testing.T) {
// fd_close needs to close an open file descriptor. Open two files so that we can tell which is closed.
path1, path2 := "a", "b"
testFS := fstest.MapFS{path1: {Data: make([]byte, 0)}, path2: {Data: make([]byte, 0)}}
mod, r, log := requireProxyModule(t, wazero.NewModuleConfig().WithFS(testFS))
defer r.Close(testCtx)
// open both paths without using WASI
fsc := mod.(*wasm.CallContext).Sys.FS(testCtx)
fdToClose, err := fsc.OpenFile(testCtx, path1)
require.NoError(t, err)
fdToKeep, err := fsc.OpenFile(testCtx, path2)
require.NoError(t, err)
// Close
requireErrno(t, ErrnoSuccess, mod, functionFdClose, uint64(fdToClose))
require.Equal(t, `
--> proxy.fd_close(fd=4)
==> wasi_snapshot_preview1.fd_close(fd=4)
<== ESUCCESS
<-- (0)
`, "\n"+log.String())
// Verify fdToClose is closed and removed from the opened FDs.
_, ok := fsc.OpenedFile(testCtx, fdToClose)
require.False(t, ok)
// Verify fdToKeep is not closed
_, ok = fsc.OpenedFile(testCtx, fdToKeep)
require.True(t, ok)
log.Reset()
t.Run("ErrnoBadF for an invalid FD", func(t *testing.T) {
requireErrno(t, ErrnoBadf, mod, functionFdClose, uint64(42)) // 42 is an arbitrary invalid FD
require.Equal(t, `
--> proxy.fd_close(fd=42)
==> wasi_snapshot_preview1.fd_close(fd=42)
<== EBADF
<-- (8)
`, "\n"+log.String())
})
}
// Test_fdDatasync only tests it is stubbed for GrainLang per #271
func Test_fdDatasync(t *testing.T) {
log := requireErrnoNosys(t, functionFdDatasync, 0)
require.Equal(t, `
--> proxy.fd_datasync(fd=0)
--> wasi_snapshot_preview1.fd_datasync(fd=0)
<-- ENOSYS
<-- (52)
`, log)
}
func Test_fdFdstatGet(t *testing.T) {
file, dir := "a", "b"
testFS := fstest.MapFS{file: {Data: make([]byte, 0)}, dir: {Mode: fs.ModeDir}}
mod, r, log := requireProxyModule(t, wazero.NewModuleConfig().WithFS(testFS))
defer r.Close(testCtx)
memorySize := mod.Memory().Size(testCtx)
// open both paths without using WASI
fsc := mod.(*wasm.CallContext).Sys.FS(testCtx)
fileFd, err := fsc.OpenFile(testCtx, file)
require.NoError(t, err)
dirFd, err := fsc.OpenFile(testCtx, dir)
require.NoError(t, err)
tests := []struct {
name string
fd, resultStat uint32
// TODO: expectedMem
expectedErrno Errno
expectedLog string
}{
{
name: "file",
fd: fileFd,
// TODO: expectedMem for a file
expectedLog: `
--> proxy.fd_fdstat_get(fd=4,result.stat=0)
==> wasi_snapshot_preview1.fd_fdstat_get(fd=4,result.stat=0)
<== ESUCCESS
<-- (0)
`,
},
{
name: "dir",
fd: dirFd,
// TODO: expectedMem for a dir
expectedLog: `
--> proxy.fd_fdstat_get(fd=5,result.stat=0)
==> wasi_snapshot_preview1.fd_fdstat_get(fd=5,result.stat=0)
<== ESUCCESS
<-- (0)
`,
},
{
name: "bad FD",
fd: math.MaxUint32,
expectedErrno: ErrnoBadf,
expectedLog: `
--> proxy.fd_fdstat_get(fd=4294967295,result.stat=0)
==> wasi_snapshot_preview1.fd_fdstat_get(fd=4294967295,result.stat=0)
<== EBADF
<-- (8)
`,
},
{
name: "resultStat exceeds the maximum valid address by 1",
fd: dirFd,
resultStat: memorySize - 24 + 1,
// TODO: ErrnoFault
expectedLog: `
--> proxy.fd_fdstat_get(fd=5,result.stat=65513)
==> wasi_snapshot_preview1.fd_fdstat_get(fd=5,result.stat=65513)
<== ESUCCESS
<-- (0)
`,
},
}
for _, tt := range tests {
tc := tt
t.Run(tc.name, func(t *testing.T) {
defer log.Reset()
requireErrno(t, tc.expectedErrno, mod, functionFdFdstatGet, uint64(tc.fd), uint64(tc.resultStat))
require.Equal(t, tc.expectedLog, "\n"+log.String())
})
}
}
// Test_fdFdstatSetFlags only tests it is stubbed for GrainLang per #271
func Test_fdFdstatSetFlags(t *testing.T) {
log := requireErrnoNosys(t, functionFdFdstatSetFlags, 0, 0)
require.Equal(t, `
--> proxy.fd_fdstat_set_flags(fd=0,flags=0)
--> wasi_snapshot_preview1.fd_fdstat_set_flags(fd=0,flags=0)
<-- ENOSYS
<-- (52)
`, log)
}
// Test_fdFdstatSetRights only tests it is stubbed for GrainLang per #271
func Test_fdFdstatSetRights(t *testing.T) {
log := requireErrnoNosys(t, functionFdFdstatSetRights, 0, 0, 0)
require.Equal(t, `
--> proxy.fd_fdstat_set_rights(fd=0,fs_rights_base=0,fs_rights_inheriting=0)
--> wasi_snapshot_preview1.fd_fdstat_set_rights(fd=0,fs_rights_base=0,fs_rights_inheriting=0)
<-- ENOSYS
<-- (52)
`, log)
}
// Test_fdFilestatGet only tests it is stubbed for GrainLang per #271
func Test_fdFilestatGet(t *testing.T) {
log := requireErrnoNosys(t, functionFdFilestatGet, 0, 0)
require.Equal(t, `
--> proxy.fd_filestat_get(fd=0,result.buf=0)
--> wasi_snapshot_preview1.fd_filestat_get(fd=0,result.buf=0)
<-- ENOSYS
<-- (52)
`, log)
}
// Test_fdFilestatSetSize only tests it is stubbed for GrainLang per #271
func Test_fdFilestatSetSize(t *testing.T) {
log := requireErrnoNosys(t, functionFdFilestatSetSize, 0, 0)
require.Equal(t, `
--> proxy.fd_filestat_set_size(fd=0,size=0)
--> wasi_snapshot_preview1.fd_filestat_set_size(fd=0,size=0)
<-- ENOSYS
<-- (52)
`, log)
}
// Test_fdFilestatSetTimes only tests it is stubbed for GrainLang per #271
func Test_fdFilestatSetTimes(t *testing.T) {
log := requireErrnoNosys(t, functionFdFilestatSetTimes, 0, 0, 0, 0)
require.Equal(t, `
--> proxy.fd_filestat_set_times(fd=0,atim=0,mtim=0,fst_flags=0)
--> wasi_snapshot_preview1.fd_filestat_set_times(fd=0,atim=0,mtim=0,fst_flags=0)
<-- ENOSYS
<-- (52)
`, log)
}
// Test_fdPread only tests it is stubbed for GrainLang per #271
func Test_fdPread(t *testing.T) {
log := requireErrnoNosys(t, functionFdPread, 0, 0, 0, 0, 0)
require.Equal(t, `
--> proxy.fd_pread(fd=0,iovs=0,iovs_len=0,offset=0,result.nread=0)
--> wasi_snapshot_preview1.fd_pread(fd=0,iovs=0,iovs_len=0,offset=0,result.nread=0)
<-- ENOSYS
<-- (52)
`, log)
}
func Test_fdPrestatGet(t *testing.T) {
pathName := "/tmp"
mod, fd, log, r := requireOpenFile(t, pathName, nil)
defer r.Close(testCtx)
resultPrestat := uint32(1) // arbitrary offset
expectedMemory := []byte{
'?', // resultPrestat after this
0, // 8-bit tag indicating `prestat_dir`, the only available tag
0, 0, 0, // 3-byte padding
// the result path length field after this
byte(len(pathName)), 0, 0, 0, // = in little endian encoding
'?',
}
maskMemory(t, testCtx, mod, len(expectedMemory))
requireErrno(t, ErrnoSuccess, mod, functionFdPrestatGet, uint64(fd), uint64(resultPrestat))
require.Equal(t, `
--> proxy.fd_prestat_get(fd=4,result.prestat=1)
==> wasi_snapshot_preview1.fd_prestat_get(fd=4,result.prestat=1)
<== ESUCCESS
<-- (0)
`, "\n"+log.String())
actual, ok := mod.Memory().Read(testCtx, 0, uint32(len(expectedMemory)))
require.True(t, ok)
require.Equal(t, expectedMemory, actual)
}
func Test_fdPrestatGet_Errors(t *testing.T) {
pathName := "/tmp"
mod, fd, log, r := requireOpenFile(t, pathName, nil)
defer r.Close(testCtx)
memorySize := mod.Memory().Size(testCtx)
tests := []struct {
name string
fd uint32
resultPrestat uint32
expectedErrno Errno
expectedLog string
}{
{
name: "invalid FD",
fd: 42, // arbitrary invalid FD
resultPrestat: 0, // valid offset
expectedErrno: ErrnoBadf,
expectedLog: `
--> proxy.fd_prestat_get(fd=42,result.prestat=0)
==> wasi_snapshot_preview1.fd_prestat_get(fd=42,result.prestat=0)
<== EBADF
<-- (8)
`,
},
{
name: "out-of-memory resultPrestat",
fd: fd,
resultPrestat: memorySize,
expectedErrno: ErrnoFault,
expectedLog: `
--> proxy.fd_prestat_get(fd=4,result.prestat=65536)
==> wasi_snapshot_preview1.fd_prestat_get(fd=4,result.prestat=65536)
<== EFAULT
<-- (21)
`,
},
// TODO: non pre-opened file == api.ErrnoBadf
}
for _, tt := range tests {
tc := tt
t.Run(tc.name, func(t *testing.T) {
defer log.Reset()
requireErrno(t, tc.expectedErrno, mod, functionFdPrestatGet, uint64(tc.fd), uint64(tc.resultPrestat))
require.Equal(t, tc.expectedLog, "\n"+log.String())
})
}
}
func Test_fdPrestatDirName(t *testing.T) {
pathName := "/tmp"
mod, fd, log, r := requireOpenFile(t, pathName, nil)
defer r.Close(testCtx)
path := uint32(1) // arbitrary offset
pathLen := uint32(3) // shorter than len("/tmp") to test the path is written for the length of pathLen
expectedMemory := []byte{
'?',
'/', 't', 'm',
'?', '?', '?',
}
maskMemory(t, testCtx, mod, len(expectedMemory))
requireErrno(t, ErrnoSuccess, mod, functionFdPrestatDirName, uint64(fd), uint64(path), uint64(pathLen))
require.Equal(t, `
--> proxy.fd_prestat_dir_name(fd=4,path=1,path_len=3)
==> wasi_snapshot_preview1.fd_prestat_dir_name(fd=4,path=1,path_len=3)
<== ESUCCESS
<-- (0)
`, "\n"+log.String())
actual, ok := mod.Memory().Read(testCtx, 0, uint32(len(expectedMemory)))
require.True(t, ok)
require.Equal(t, expectedMemory, actual)
}
func Test_fdPrestatDirName_Errors(t *testing.T) {
pathName := "/tmp"
mod, fd, log, r := requireOpenFile(t, pathName, nil)
defer r.Close(testCtx)
memorySize := mod.Memory().Size(testCtx)
validAddress := uint32(0) // Arbitrary valid address as arguments to fd_prestat_dir_name. We chose 0 here.
pathLen := uint32(len("/tmp"))
tests := []struct {
name string
fd uint32
path uint32
pathLen uint32
expectedErrno Errno
expectedLog string
}{
{
name: "out-of-memory path",
fd: fd,
path: memorySize,
pathLen: pathLen,
expectedErrno: ErrnoFault,
expectedLog: `
--> proxy.fd_prestat_dir_name(fd=4,path=65536,path_len=4)
==> wasi_snapshot_preview1.fd_prestat_dir_name(fd=4,path=65536,path_len=4)
<== EFAULT
<-- (21)
`,
},
{
name: "path exceeds the maximum valid address by 1",
fd: fd,
path: memorySize - pathLen + 1,
pathLen: pathLen,
expectedErrno: ErrnoFault,
expectedLog: `
--> proxy.fd_prestat_dir_name(fd=4,path=65533,path_len=4)
==> wasi_snapshot_preview1.fd_prestat_dir_name(fd=4,path=65533,path_len=4)
<== EFAULT
<-- (21)
`,
},
{
name: "pathLen exceeds the length of the dir name",
fd: fd,
path: validAddress,
pathLen: pathLen + 1,
expectedErrno: ErrnoNametoolong,
expectedLog: `
--> proxy.fd_prestat_dir_name(fd=4,path=0,path_len=5)
==> wasi_snapshot_preview1.fd_prestat_dir_name(fd=4,path=0,path_len=5)
<== ENAMETOOLONG
<-- (37)
`,
},
{
name: "invalid fd",
fd: 42, // arbitrary invalid fd
path: validAddress,
pathLen: pathLen,
expectedErrno: ErrnoBadf,
expectedLog: `
--> proxy.fd_prestat_dir_name(fd=42,path=0,path_len=4)
==> wasi_snapshot_preview1.fd_prestat_dir_name(fd=42,path=0,path_len=4)
<== EBADF
<-- (8)
`,
},
// TODO: non pre-opened file == ErrnoBadf
}
for _, tt := range tests {
tc := tt
t.Run(tc.name, func(t *testing.T) {
defer log.Reset()
requireErrno(t, tc.expectedErrno, mod, functionFdPrestatDirName, uint64(tc.fd), uint64(tc.path), uint64(tc.pathLen))
require.Equal(t, tc.expectedLog, "\n"+log.String())
})
}
}
// Test_fdPwrite only tests it is stubbed for GrainLang per #271
func Test_fdPwrite(t *testing.T) {
log := requireErrnoNosys(t, functionFdPwrite, 0, 0, 0, 0, 0)
require.Equal(t, `
--> proxy.fd_pwrite(fd=0,iovs=0,iovs_len=0,offset=0,result.nwritten=0)
--> wasi_snapshot_preview1.fd_pwrite(fd=0,iovs=0,iovs_len=0,offset=0,result.nwritten=0)
<-- ENOSYS
<-- (52)
`, log)
}
func Test_fdRead(t *testing.T) {
mod, fd, log, r := requireOpenFile(t, "/test_path", []byte("wazero"))
defer r.Close(testCtx)
iovs := uint32(1) // arbitrary offset
initialMemory := []byte{
'?', // `iovs` is after this
18, 0, 0, 0, // = iovs[0].offset
4, 0, 0, 0, // = iovs[0].length
23, 0, 0, 0, // = iovs[1].offset
2, 0, 0, 0, // = iovs[1].length
'?',
}
iovsCount := uint32(2) // The count of iovs
resultSize := uint32(26) // arbitrary offset
expectedMemory := append(
initialMemory,
'w', 'a', 'z', 'e', // iovs[0].length bytes
'?', // iovs[1].offset is after this
'r', 'o', // iovs[1].length bytes
'?', // resultSize is after this
6, 0, 0, 0, // sum(iovs[...].length) == length of "wazero"
'?',
)
maskMemory(t, testCtx, mod, len(expectedMemory))
ok := mod.Memory().Write(testCtx, 0, initialMemory)
require.True(t, ok)
requireErrno(t, ErrnoSuccess, mod, functionFdRead, uint64(fd), uint64(iovs), uint64(iovsCount), uint64(resultSize))
require.Equal(t, `
--> proxy.fd_read(fd=4,iovs=1,iovs_len=2,result.size=26)
==> wasi_snapshot_preview1.fd_read(fd=4,iovs=1,iovs_len=2,result.size=26)
<== ESUCCESS
<-- (0)
`, "\n"+log.String())
actual, ok := mod.Memory().Read(testCtx, 0, uint32(len(expectedMemory)))
require.True(t, ok)
require.Equal(t, expectedMemory, actual)
}
func Test_fdRead_Errors(t *testing.T) {
mod, fd, log, r := requireOpenFile(t, "/test_path", []byte("wazero"))
defer r.Close(testCtx)
tests := []struct {
name string
fd, iovs, iovsCount, resultSize uint32
memory []byte
expectedErrno Errno
expectedLog string
}{
{
name: "invalid fd",
fd: 42, // arbitrary invalid fd
expectedErrno: ErrnoBadf,
expectedLog: `
--> proxy.fd_read(fd=42,iovs=65536,iovs_len=65536,result.size=65536)
==> wasi_snapshot_preview1.fd_read(fd=42,iovs=65536,iovs_len=65536,result.size=65536)
<== EBADF
<-- (8)
`,
},
{
name: "out-of-memory reading iovs[0].offset",
fd: fd,
iovs: 1,
memory: []byte{'?'},
expectedErrno: ErrnoFault,
expectedLog: `
--> proxy.fd_read(fd=4,iovs=65536,iovs_len=65535,result.size=65535)
==> wasi_snapshot_preview1.fd_read(fd=4,iovs=65536,iovs_len=65535,result.size=65535)
<== EFAULT
<-- (21)
`,
},
{
name: "out-of-memory reading iovs[0].length",
fd: fd,
iovs: 1, iovsCount: 1,
memory: []byte{
'?', // `iovs` is after this
9, 0, 0, 0, // = iovs[0].offset
},
expectedErrno: ErrnoFault,
expectedLog: `
--> proxy.fd_read(fd=4,iovs=65532,iovs_len=65532,result.size=65531)
==> wasi_snapshot_preview1.fd_read(fd=4,iovs=65532,iovs_len=65532,result.size=65531)
<== EFAULT
<-- (21)
`,
},
{
name: "iovs[0].offset is outside memory",
fd: fd,
iovs: 1, iovsCount: 1,
memory: []byte{
'?', // `iovs` is after this
0, 0, 0x1, 0, // = iovs[0].offset on the second page
1, 0, 0, 0, // = iovs[0].length
},
expectedErrno: ErrnoFault,
expectedLog: `
--> proxy.fd_read(fd=4,iovs=65528,iovs_len=65528,result.size=65527)
==> wasi_snapshot_preview1.fd_read(fd=4,iovs=65528,iovs_len=65528,result.size=65527)
<== EFAULT
<-- (21)
`,
},
{
name: "length to read exceeds memory by 1",
fd: fd,
iovs: 1, iovsCount: 1,
memory: []byte{
'?', // `iovs` is after this
9, 0, 0, 0, // = iovs[0].offset
0, 0, 0x1, 0, // = iovs[0].length on the second page
'?',
},
expectedErrno: ErrnoFault,
expectedLog: `
--> proxy.fd_read(fd=4,iovs=65527,iovs_len=65527,result.size=65526)
==> wasi_snapshot_preview1.fd_read(fd=4,iovs=65527,iovs_len=65527,result.size=65526)
<== EFAULT
<-- (21)
`,
},
{
name: "resultSize offset is outside memory",
fd: fd,
iovs: 1, iovsCount: 1,
resultSize: 10, // 1 past memory
memory: []byte{
'?', // `iovs` is after this
9, 0, 0, 0, // = iovs[0].offset
1, 0, 0, 0, // = iovs[0].length
'?',
},
expectedErrno: ErrnoFault,
expectedLog: `
--> proxy.fd_read(fd=4,iovs=65527,iovs_len=65527,result.size=65536)
==> wasi_snapshot_preview1.fd_read(fd=4,iovs=65527,iovs_len=65527,result.size=65536)
<== EFAULT
<-- (21)
`,
},
}
for _, tt := range tests {
tc := tt
t.Run(tc.name, func(t *testing.T) {
defer log.Reset()
offset := uint32(wasm.MemoryPagesToBytesNum(testMemoryPageSize) - uint64(len(tc.memory)))
memoryWriteOK := mod.Memory().Write(testCtx, offset, tc.memory)
require.True(t, memoryWriteOK)
requireErrno(t, tc.expectedErrno, mod, functionFdRead, uint64(tc.fd), uint64(tc.iovs+offset), uint64(tc.iovsCount+offset), uint64(tc.resultSize+offset))
require.Equal(t, tc.expectedLog, "\n"+log.String())
})
}
}
func Test_fdRead_shouldContinueRead(t *testing.T) {
tests := []struct {
name string
n, l uint32
err error
expectedOk bool
expectedErrno Errno
}{
{
name: "break when nothing to read",
n: 0,
l: 0,
},
{
name: "break when nothing read",
n: 0,
l: 4,
},
{
name: "break on partial read",
n: 3,
l: 4,
},
{
name: "continue on full read",
n: 4,
l: 4,
expectedOk: true,
},
{
name: "break on EOF on nothing to read",
err: io.EOF,
},
{
name: "break on EOF on nothing read",
l: 4,
err: io.EOF,
},
{
name: "break on EOF on partial read",
n: 3,
l: 4,
err: io.EOF,
},
{
name: "break on EOF on full read",
n: 4,
l: 4,
err: io.EOF,
},
{
name: "return ErrnoIo on error on nothing to read",
err: io.ErrClosedPipe,
expectedErrno: ErrnoIo,
},
{
name: "return ErrnoIo on error on nothing read",
l: 4,
err: io.ErrClosedPipe,
expectedErrno: ErrnoIo,
},
{ // Special case, allows processing data before err
name: "break on error on partial read",
n: 3,
l: 4,
err: io.ErrClosedPipe,
},
{ // Special case, allows processing data before err
name: "break on error on full read",
n: 4,
l: 4,
err: io.ErrClosedPipe,
},
}
for _, tt := range tests {
tc := tt
t.Run(tc.name, func(t *testing.T) {
ok, errno := fdRead_shouldContinueRead(tc.n, tc.l, tc.err)
require.Equal(t, tc.expectedOk, ok)
require.Equal(t, tc.expectedErrno, errno)
})
}
}
// Test_fdReaddir only tests it is stubbed for GrainLang per #271
func Test_fdReaddir(t *testing.T) {
log := requireErrnoNosys(t, functionFdReaddir, 0, 0, 0, 0, 0)
require.Equal(t, `
--> proxy.fd_readdir(fd=0,buf=0,buf_len=0,cookie=0,result.bufused=0)
--> wasi_snapshot_preview1.fd_readdir(fd=0,buf=0,buf_len=0,cookie=0,result.bufused=0)
<-- ENOSYS
<-- (52)
`, log)
}
// Test_fdRenumber only tests it is stubbed for GrainLang per #271
func Test_fdRenumber(t *testing.T) {
log := requireErrnoNosys(t, functionFdRenumber, 0, 0)
require.Equal(t, `
--> proxy.fd_renumber(fd=0,to=0)
--> wasi_snapshot_preview1.fd_renumber(fd=0,to=0)
<-- ENOSYS
<-- (52)
`, log)
}
func Test_fdSeek(t *testing.T) {
mod, fd, log, r := requireOpenFile(t, "/test_path", []byte("wazero"))
defer r.Close(testCtx)
resultNewoffset := uint32(1) // arbitrary offset in api.Memory for the new offset value
tests := []struct {
name string
offset int64
whence int
expectedOffset int64
expectedMemory []byte
expectedLog string
}{
{
name: "SeekStart",
offset: 4, // arbitrary offset
whence: io.SeekStart,
expectedOffset: 4, // = offset
expectedMemory: []byte{
'?', // resultNewoffset is after this
4, 0, 0, 0, 0, 0, 0, 0, // = expectedOffset
'?',
},
expectedLog: `
--> proxy.fd_seek(fd=4,offset=4,whence=0,result.newoffset=1)
==> wasi_snapshot_preview1.fd_seek(fd=4,offset=4,whence=0,result.newoffset=1)
<== ESUCCESS
<-- (0)
`,
},
{
name: "SeekCurrent",
offset: 1, // arbitrary offset
whence: io.SeekCurrent,
expectedOffset: 2, // = 1 (the initial offset of the test file) + 1 (offset)
expectedMemory: []byte{
'?', // resultNewoffset is after this
2, 0, 0, 0, 0, 0, 0, 0, // = expectedOffset
'?',
},
expectedLog: `
--> proxy.fd_seek(fd=4,offset=1,whence=1,result.newoffset=1)
==> wasi_snapshot_preview1.fd_seek(fd=4,offset=1,whence=1,result.newoffset=1)
<== ESUCCESS
<-- (0)
`,
},
{
name: "SeekEnd",
offset: -1, // arbitrary offset, note that offset can be negative
whence: io.SeekEnd,
expectedOffset: 5, // = 6 (the size of the test file with content "wazero") + -1 (offset)
expectedMemory: []byte{
'?', // resultNewoffset is after this
5, 0, 0, 0, 0, 0, 0, 0, // = expectedOffset
'?',
},
expectedLog: `
--> proxy.fd_seek(fd=4,offset=18446744073709551615,whence=2,result.newoffset=1)
==> wasi_snapshot_preview1.fd_seek(fd=4,offset=18446744073709551615,whence=2,result.newoffset=1)
<== ESUCCESS
<-- (0)
`,
},
}
for _, tt := range tests {
tc := tt
t.Run(tc.name, func(t *testing.T) {
defer log.Reset()
maskMemory(t, testCtx, mod, len(tc.expectedMemory))
// Since we initialized this file, we know it is a seeker (because it is a MapFile)
fsc := mod.(*wasm.CallContext).Sys.FS(testCtx)
f, ok := fsc.OpenedFile(testCtx, fd)
require.True(t, ok)
seeker := f.File.(io.Seeker)
// set the initial offset of the file to 1
offset, err := seeker.Seek(1, io.SeekStart)
require.NoError(t, err)
require.Equal(t, int64(1), offset)
requireErrno(t, ErrnoSuccess, mod, functionFdSeek, uint64(fd), uint64(tc.offset), uint64(tc.whence), uint64(resultNewoffset))
require.Equal(t, tc.expectedLog, "\n"+log.String())
actual, ok := mod.Memory().Read(testCtx, 0, uint32(len(tc.expectedMemory)))
require.True(t, ok)
require.Equal(t, tc.expectedMemory, actual)
offset, err = seeker.Seek(0, io.SeekCurrent)
require.NoError(t, err)
require.Equal(t, tc.expectedOffset, offset) // test that the offset of file is actually updated.
})
}
}
func Test_fdSeek_Errors(t *testing.T) {
mod, fd, log, r := requireOpenFile(t, "/test_path", []byte("wazero"))
defer r.Close(testCtx)
memorySize := mod.Memory().Size(testCtx)
tests := []struct {
name string
fd uint32
offset uint64
whence, resultNewoffset uint32
expectedErrno Errno
expectedLog string
}{
{
name: "invalid fd",
fd: 42, // arbitrary invalid fd
expectedErrno: ErrnoBadf,
expectedLog: `
--> proxy.fd_seek(fd=42,offset=0,whence=0,result.newoffset=0)
==> wasi_snapshot_preview1.fd_seek(fd=42,offset=0,whence=0,result.newoffset=0)
<== EBADF
<-- (8)
`,
},
{
name: "invalid whence",
fd: fd,
whence: 3, // invalid whence, the largest whence io.SeekEnd(2) + 1
expectedErrno: ErrnoInval,
expectedLog: `
--> proxy.fd_seek(fd=4,offset=0,whence=3,result.newoffset=0)
==> wasi_snapshot_preview1.fd_seek(fd=4,offset=0,whence=3,result.newoffset=0)
<== EINVAL
<-- (28)
`,
},
{
name: "out-of-memory writing resultNewoffset",
fd: fd,
resultNewoffset: memorySize,
expectedErrno: ErrnoFault,
expectedLog: `
--> proxy.fd_seek(fd=4,offset=0,whence=0,result.newoffset=65536)
==> wasi_snapshot_preview1.fd_seek(fd=4,offset=0,whence=0,result.newoffset=65536)
<== EFAULT
<-- (21)
`,
},
}
for _, tt := range tests {
tc := tt
t.Run(tc.name, func(t *testing.T) {
defer log.Reset()
requireErrno(t, tc.expectedErrno, mod, functionFdSeek, uint64(tc.fd), tc.offset, uint64(tc.whence), uint64(tc.resultNewoffset))
require.Equal(t, tc.expectedLog, "\n"+log.String())
})
}
}
// Test_fdSync only tests it is stubbed for GrainLang per #271
func Test_fdSync(t *testing.T) {
log := requireErrnoNosys(t, functionFdSync, 0)
require.Equal(t, `
--> proxy.fd_sync(fd=0)
--> wasi_snapshot_preview1.fd_sync(fd=0)
<-- ENOSYS
<-- (52)
`, log)
}
// Test_fdTell only tests it is stubbed for GrainLang per #271
func Test_fdTell(t *testing.T) {
log := requireErrnoNosys(t, functionFdTell, 0, 0)
require.Equal(t, `
--> proxy.fd_tell(fd=0,result.offset=0)
--> wasi_snapshot_preview1.fd_tell(fd=0,result.offset=0)
<-- ENOSYS
<-- (52)
`, log)
}
func Test_fdWrite(t *testing.T) {
tmpDir := t.TempDir() // open before loop to ensure no locking problems.
pathName := "test_path"
mod, fd, log, r := requireOpenWritableFile(t, tmpDir, pathName)
defer r.Close(testCtx)
iovs := uint32(1) // arbitrary offset
initialMemory := []byte{
'?', // `iovs` is after this
18, 0, 0, 0, // = iovs[0].offset
4, 0, 0, 0, // = iovs[0].length
23, 0, 0, 0, // = iovs[1].offset
2, 0, 0, 0, // = iovs[1].length
'?', // iovs[0].offset is after this
'w', 'a', 'z', 'e', // iovs[0].length bytes
'?', // iovs[1].offset is after this
'r', 'o', // iovs[1].length bytes
'?',
}
iovsCount := uint32(2) // The count of iovs
resultSize := uint32(26) // arbitrary offset
expectedMemory := append(
initialMemory,
6, 0, 0, 0, // sum(iovs[...].length) == length of "wazero"
'?',
)
maskMemory(t, testCtx, mod, len(expectedMemory))
ok := mod.Memory().Write(testCtx, 0, initialMemory)
require.True(t, ok)
requireErrno(t, ErrnoSuccess, mod, functionFdWrite, uint64(fd), uint64(iovs), uint64(iovsCount), uint64(resultSize))
require.Equal(t, `
--> proxy.fd_write(fd=4,iovs=1,iovs_len=2,result.size=26)
==> wasi_snapshot_preview1.fd_write(fd=4,iovs=1,iovs_len=2,result.size=26)
<== ESUCCESS
<-- (0)
`, "\n"+log.String())
actual, ok := mod.Memory().Read(testCtx, 0, uint32(len(expectedMemory)))
require.True(t, ok)
require.Equal(t, expectedMemory, actual)
// Since we initialized this file, we know we can read it by path
buf, err := os.ReadFile(path.Join(tmpDir, pathName))
require.NoError(t, err)
require.Equal(t, []byte("wazero"), buf) // verify the file was actually written
}
func Test_fdWrite_Errors(t *testing.T) {
tmpDir := t.TempDir() // open before loop to ensure no locking problems.
pathName := "test_path"
mod, fd, log, r := requireOpenWritableFile(t, tmpDir, pathName)
defer r.Close(testCtx)
// Setup valid test memory
iovs, iovsCount := uint32(0), uint32(1)
memory := []byte{
8, 0, 0, 0, // = iovs[0].offset (where the data "hi" begins)
2, 0, 0, 0, // = iovs[0].length (how many bytes are in "hi")
'h', 'i', // iovs[0].length bytes
}
tests := []struct {
name string
fd, resultSize uint32
memory []byte
expectedErrno Errno
expectedLog string
}{
{
name: "invalid fd",
fd: 42, // arbitrary invalid fd
expectedErrno: ErrnoBadf,
expectedLog: `
--> proxy.fd_write(fd=42,iovs=0,iovs_len=1,result.size=0)
==> wasi_snapshot_preview1.fd_write(fd=42,iovs=0,iovs_len=1,result.size=0)
<== EBADF
<-- (8)
`,
},
{
name: "out-of-memory reading iovs[0].offset",
fd: fd,
memory: []byte{},
expectedErrno: ErrnoFault,
expectedLog: `
--> proxy.fd_write(fd=4,iovs=0,iovs_len=1,result.size=0)
==> wasi_snapshot_preview1.fd_write(fd=4,iovs=0,iovs_len=1,result.size=0)
<== EFAULT
<-- (21)
`,