ic-wasi-polyfill 0.8.0

The project provides polyfill implementation of *wasi_snapshot_preview1* functions using IC System API.
Documentation
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
use crate::{init, test::common::libc, wasi};

#[test]
fn test_file_read_write() {
    init(&[], &[]);

    let dir_fd = 3;

    unsafe {
        // Create a file in the scratch directory.
        let file_fd = wasi::path_open(
            dir_fd,
            0,
            "file",
            wasi::OFLAGS_CREAT,
            wasi::RIGHTS_FD_READ | wasi::RIGHTS_FD_WRITE,
            0,
            0,
        )
        .expect("opening a file");
        assert!(
            file_fd > libc::STDERR_FILENO as wasi::Fd,
            "file descriptor range check",
        );

        let contents = &[0u8, 1, 2, 3];
        let ciovec = wasi::Ciovec {
            buf: contents.as_ptr() as *const _,
            buf_len: contents.len(),
        };
        let mut nwritten = wasi::fd_write(file_fd, &[ciovec]).expect("writing bytes at offset 0");
        assert_eq!(nwritten, 4, "nwritten bytes check");

        let contents = &mut [0u8; 4];
        let iovec = wasi::Iovec {
            buf: contents.as_mut_ptr() as *mut _,
            buf_len: contents.len(),
        };
        wasi::fd_seek(file_fd, 0, wasi::WHENCE_SET).expect("seeking to offset 0");
        let mut nread = wasi::fd_read(file_fd, &[iovec]).expect("reading bytes at offset 0");
        assert_eq!(nread, 4, "nread bytes check");
        assert_eq!(contents, &[0u8, 1, 2, 3], "written bytes equal read bytes");

        // Write all the data through multiple iovecs.
        //
        // Note that this needs to be done with a loop, because some
        // platforms do not support writing multiple iovecs at once.
        // See https://github.com/rust-lang/rust/issues/74825.
        let contents = &[0u8, 1, 2, 3];
        let mut offset = 0usize;
        wasi::fd_seek(file_fd, 0, wasi::WHENCE_SET).expect("seeking to offset 0");
        loop {
            let mut ciovecs: Vec<wasi::Ciovec> = Vec::new();
            let mut remaining = contents.len() - offset;
            if remaining > 2 {
                ciovecs.push(wasi::Ciovec {
                    buf: contents[offset..].as_ptr() as *const _,
                    buf_len: 2,
                });
                remaining -= 2;
            }
            ciovecs.push(wasi::Ciovec {
                buf: contents[contents.len() - remaining..].as_ptr() as *const _,
                buf_len: remaining,
            });

            nwritten =
                wasi::fd_write(file_fd, ciovecs.as_slice()).expect("writing bytes at offset 0");

            offset += nwritten;
            if offset == contents.len() {
                break;
            }
        }
        assert_eq!(offset, 4, "nread bytes check");

        // Read all the data through multiple iovecs.
        //
        // Note that this needs to be done with a loop, because some
        // platforms do not support reading multiple iovecs at once.
        // See https://github.com/rust-lang/rust/issues/74825.
        let contents = &mut [0u8; 4];
        let mut offset = 0usize;
        wasi::fd_seek(file_fd, 0, wasi::WHENCE_SET).expect("seeking to offset 0");
        loop {
            let buffer = &mut [0u8; 4];
            let iovecs = &[
                wasi::Iovec {
                    buf: buffer.as_mut_ptr() as *mut _,
                    buf_len: 2,
                },
                wasi::Iovec {
                    buf: buffer[2..].as_mut_ptr() as *mut _,
                    buf_len: 2,
                },
            ];
            nread = wasi::fd_read(file_fd, iovecs).expect("reading bytes at offset 0");
            if nread == 0 {
                break;
            }
            contents[offset..offset + nread].copy_from_slice(&buffer[0..nread]);
            offset += nread;
        }
        assert_eq!(offset, 4, "nread bytes check");
        assert_eq!(contents, &[0u8, 1, 2, 3], "file cursor was overwritten");

        let contents = &mut [0u8; 4];
        let iovec = wasi::Iovec {
            buf: contents.as_mut_ptr() as *mut _,
            buf_len: contents.len(),
        };
        wasi::fd_seek(file_fd, 2, wasi::WHENCE_SET).expect("seeking to offset 2");
        nread = wasi::fd_read(file_fd, &[iovec]).expect("reading bytes at offset 2");
        assert_eq!(nread, 2, "nread bytes check");
        assert_eq!(contents, &[2u8, 3, 0, 0], "file cursor was overwritten");

        let contents = &[1u8, 0];
        let ciovec = wasi::Ciovec {
            buf: contents.as_ptr() as *const _,
            buf_len: contents.len(),
        };
        wasi::fd_seek(file_fd, 2, wasi::WHENCE_SET).expect("seeking to offset 2");
        nwritten = wasi::fd_write(file_fd, &[ciovec]).expect("writing bytes at offset 2");
        assert_eq!(nwritten, 2, "nwritten bytes check");

        let contents = &mut [0u8; 4];
        let iovec = wasi::Iovec {
            buf: contents.as_mut_ptr() as *mut _,
            buf_len: contents.len(),
        };
        wasi::fd_seek(file_fd, 0, wasi::WHENCE_SET).expect("seeking to offset 0");
        nread = wasi::fd_read(file_fd, &[iovec]).expect("reading bytes at offset 0");
        assert_eq!(nread, 4, "nread bytes check");
        assert_eq!(contents, &[0u8, 1, 1, 0], "file cursor was overwritten");

        wasi::fd_close(file_fd).expect("closing a file");
        wasi::path_unlink_file(dir_fd, "file").expect("removing a file");
    }
}

#[test]
fn test_file_write_and_file_pos() {
    init(&[], &[]);

    let dir_fd = 3;

    unsafe {
        let path = "file2";
        let file_fd = wasi::path_open(
            dir_fd,
            0,
            path,
            wasi::OFLAGS_CREAT,
            wasi::RIGHTS_FD_READ | wasi::RIGHTS_FD_WRITE,
            0,
            0,
        )
        .expect("opening a file");
        assert!(
            file_fd > libc::STDERR_FILENO as wasi::Fd,
            "file descriptor range check",
        );

        // Perform a 0-sized pwrite at an offset beyond the end of the file. Unix
        // semantics should pop out where nothing is actually written and the size
        // of the file isn't modified.
        assert_eq!(wasi::fd_tell(file_fd).unwrap(), 0);
        let ciovec = wasi::Ciovec {
            buf: [].as_ptr(),
            buf_len: 0,
        };
        wasi::fd_seek(file_fd, 2, wasi::WHENCE_SET).expect("seeking to offset 2");
        let n = wasi::fd_write(file_fd, &[ciovec]).expect("writing bytes at offset 2");
        assert_eq!(n, 0);

        assert_eq!(wasi::fd_tell(file_fd).unwrap(), 2);
        let stat = wasi::fd_filestat_get(file_fd).unwrap();
        assert_eq!(stat.size, 0);

        // Now write a single byte and make sure it actually works
        let buf = [0];
        let ciovec = wasi::Ciovec {
            buf: buf.as_ptr(),
            buf_len: buf.len(),
        };
        wasi::fd_seek(file_fd, 50, wasi::WHENCE_SET).expect("seeking to offset 50");
        let n = wasi::fd_write(file_fd, &[ciovec]).expect("writing bytes at offset 50");
        assert_eq!(n, 1);

        assert_eq!(wasi::fd_tell(file_fd).unwrap(), 51);
        let stat = wasi::fd_filestat_get(file_fd).unwrap();
        assert_eq!(stat.size, 51);

        wasi::fd_close(file_fd).expect("closing a file");
        wasi::path_unlink_file(dir_fd, path).expect("removing a file");
    }
}

#[test]
fn test_file_pread_pwrite() {
    init(&[], &[]);

    let dir_fd = 3;

    unsafe {
        // Create a file in the scratch directory.
        let file_fd = wasi::path_open(
            dir_fd,
            0,
            "file",
            wasi::OFLAGS_CREAT,
            wasi::RIGHTS_FD_READ | wasi::RIGHTS_FD_WRITE,
            0,
            0,
        )
        .expect("opening a file");
        assert!(
            file_fd > libc::STDERR_FILENO as wasi::Fd,
            "file descriptor range check",
        );

        let contents = &[0u8, 1, 2, 3];
        let ciovec = wasi::Ciovec {
            buf: contents.as_ptr() as *const _,
            buf_len: contents.len(),
        };
        let mut nwritten =
            wasi::fd_pwrite(file_fd, &[ciovec], 0).expect("writing bytes at offset 0");
        assert_eq!(nwritten, 4, "nwritten bytes check");

        let contents = &mut [0u8; 4];
        let iovec = wasi::Iovec {
            buf: contents.as_mut_ptr() as *mut _,
            buf_len: contents.len(),
        };
        let mut nread = wasi::fd_pread(file_fd, &[iovec], 0).expect("reading bytes at offset 0");
        assert_eq!(nread, 4, "nread bytes check");
        assert_eq!(contents, &[0u8, 1, 2, 3], "written bytes equal read bytes");

        // Write all the data through multiple iovecs.
        let contents = &[0u8, 1, 2, 3];
        let mut offset = 0usize;
        loop {
            let mut ciovecs: Vec<wasi::Ciovec> = Vec::new();
            let mut remaining = contents.len() - offset;
            if remaining > 2 {
                ciovecs.push(wasi::Ciovec {
                    buf: contents[offset..].as_ptr() as *const _,
                    buf_len: 2,
                });
                remaining -= 2;
            }
            ciovecs.push(wasi::Ciovec {
                buf: contents[contents.len() - remaining..].as_ptr() as *const _,
                buf_len: remaining,
            });

            nwritten = wasi::fd_pwrite(file_fd, ciovecs.as_slice(), offset.try_into().unwrap())
                .expect("writing bytes at offset 0");

            offset += nwritten;
            if offset == contents.len() {
                break;
            }
        }
        assert_eq!(offset, 4, "nread bytes check");

        // Read all the data through multiple iovecs.
        let contents = &mut [0u8; 4];
        let mut offset = 0usize;
        loop {
            let buffer = &mut [0u8; 4];
            let iovecs = &[
                wasi::Iovec {
                    buf: buffer.as_mut_ptr() as *mut _,
                    buf_len: 2,
                },
                wasi::Iovec {
                    buf: buffer[2..].as_mut_ptr() as *mut _,
                    buf_len: 2,
                },
            ];
            nread =
                wasi::fd_pread(file_fd, iovecs, offset as _).expect("reading bytes at offset 0");
            if nread == 0 {
                break;
            }
            contents[offset..offset + nread].copy_from_slice(&buffer[0..nread]);
            offset += nread;
        }
        assert_eq!(offset, 4, "nread bytes check");
        assert_eq!(contents, &[0u8, 1, 2, 3], "file cursor was overwritten");

        let contents = &mut [0u8; 4];
        let iovec = wasi::Iovec {
            buf: contents.as_mut_ptr() as *mut _,
            buf_len: contents.len(),
        };
        nread = wasi::fd_pread(file_fd, &[iovec], 2).expect("reading bytes at offset 2");
        assert_eq!(nread, 2, "nread bytes check");
        assert_eq!(contents, &[2u8, 3, 0, 0], "file cursor was overwritten");

        let contents = &[1u8, 0];
        let ciovec = wasi::Ciovec {
            buf: contents.as_ptr() as *const _,
            buf_len: contents.len(),
        };
        nwritten = wasi::fd_pwrite(file_fd, &[ciovec], 2).expect("writing bytes at offset 2");
        assert_eq!(nwritten, 2, "nwritten bytes check");

        let contents = &mut [0u8; 4];
        let iovec = wasi::Iovec {
            buf: contents.as_mut_ptr() as *mut _,
            buf_len: contents.len(),
        };
        nread = wasi::fd_pread(file_fd, &[iovec], 0).expect("reading bytes at offset 0");
        assert_eq!(nread, 4, "nread bytes check");
        assert_eq!(contents, &[0u8, 1, 1, 0], "file cursor was overwritten");

        wasi::fd_close(file_fd).expect("closing a file");
        wasi::path_unlink_file(dir_fd, "file").expect("removing a file");
    }
}

#[test]
fn test_file_pwrite_and_file_pos() {
    init(&[], &[]);

    let dir_fd = 3;

    unsafe {
        let path = "file2";
        let file_fd = wasi::path_open(
            dir_fd,
            0,
            path,
            wasi::OFLAGS_CREAT,
            wasi::RIGHTS_FD_READ | wasi::RIGHTS_FD_WRITE,
            0,
            0,
        )
        .expect("opening a file");
        assert!(
            file_fd > libc::STDERR_FILENO as wasi::Fd,
            "file descriptor range check",
        );

        // Perform a 0-sized pwrite at an offset beyond the end of the file. Unix
        // semantics should pop out where nothing is actually written and the size
        // of the file isn't modified.
        assert_eq!(wasi::fd_tell(file_fd).unwrap(), 0);
        let ciovec = wasi::Ciovec {
            buf: [].as_ptr(),
            buf_len: 0,
        };
        let n = wasi::fd_pwrite(file_fd, &[ciovec], 50).expect("writing bytes at offset 2");
        assert_eq!(n, 0);

        assert_eq!(wasi::fd_tell(file_fd).unwrap(), 0);
        let stat = wasi::fd_filestat_get(file_fd).unwrap();
        assert_eq!(stat.size, 0);

        // Now write a single byte and make sure it actually works
        let buf = [0];
        let ciovec = wasi::Ciovec {
            buf: buf.as_ptr(),
            buf_len: buf.len(),
        };
        let n = wasi::fd_pwrite(file_fd, &[ciovec], 50).expect("writing bytes at offset 50");
        assert_eq!(n, 1);

        assert_eq!(wasi::fd_tell(file_fd).unwrap(), 0);
        let stat = wasi::fd_filestat_get(file_fd).unwrap();
        assert_eq!(stat.size, 51);

        wasi::fd_close(file_fd).expect("closing a file");
        wasi::path_unlink_file(dir_fd, path).expect("removing a file");
    }
}

#[test]
fn test_file_pwrite_and_append() {
    init(&[], &[]);

    let dir_fd = 3;

    unsafe {
        let path = "file3";
        let file_fd = wasi::path_open(
            dir_fd,
            0,
            path,
            wasi::OFLAGS_CREAT,
            wasi::RIGHTS_FD_READ | wasi::RIGHTS_FD_WRITE,
            0,
            wasi::FDFLAGS_APPEND,
        )
        .expect("opening a file");

        // Inherit linux semantics for `pwrite` where if the file is opened in
        // append mode then the offset to `pwrite` is ignored.
        let buf = [0];
        let ciovec = wasi::Ciovec {
            buf: buf.as_ptr(),
            buf_len: buf.len(),
        };
        let n = wasi::fd_pwrite(file_fd, &[ciovec], 50).expect("writing bytes at offset 50");
        assert_eq!(n, 1);

        let stat = wasi::fd_filestat_get(file_fd).unwrap();
        assert_eq!(stat.size, 1);

        let n = wasi::fd_pwrite(file_fd, &[ciovec], 0).expect("writing bytes at offset 50");
        assert_eq!(n, 1);

        let stat = wasi::fd_filestat_get(file_fd).unwrap();
        assert_eq!(stat.size, 2);

        wasi::fd_close(file_fd).expect("closing a file");
        wasi::path_unlink_file(dir_fd, path).expect("removing a file");
    }
}

#[test]
fn test_file_seek_tell() {
    init(&[], &[]);

    let dir_fd = 3;

    unsafe {
        // Create a file in the scratch directory.
        let file_fd = wasi::path_open(
            dir_fd,
            0,
            "file",
            wasi::OFLAGS_CREAT,
            wasi::RIGHTS_FD_READ | wasi::RIGHTS_FD_WRITE,
            0,
            0,
        )
        .expect("opening a file");
        assert!(
            file_fd > libc::STDERR_FILENO as wasi::Fd,
            "file descriptor range check",
        );

        // Check current offset
        let mut offset = wasi::fd_tell(file_fd).expect("getting initial file offset");
        assert_eq!(offset, 0, "current offset should be 0");

        // Write to file
        let data = &[0u8; 100];
        let iov = wasi::Ciovec {
            buf: data.as_ptr() as *const _,
            buf_len: data.len(),
        };
        let nwritten = wasi::fd_write(file_fd, &[iov]).expect("writing to a file");
        assert_eq!(nwritten, 100, "should write 100 bytes to file");

        // Check current offset
        offset = wasi::fd_tell(file_fd).expect("getting file offset after writing");
        assert_eq!(offset, 100, "offset after writing should be 100");

        // Seek to middle of the file
        let mut newoffset =
            wasi::fd_seek(file_fd, -50, wasi::WHENCE_CUR).expect("seeking to the middle of a file");
        assert_eq!(
            newoffset, 50,
            "offset after seeking to the middle should be at 50"
        );

        // Seek to the beginning of the file
        newoffset = wasi::fd_seek(file_fd, 0, wasi::WHENCE_SET)
            .expect("seeking to the beginning of the file");
        assert_eq!(
            newoffset, 0,
            "offset after seeking to the beginning of the file should be at 0"
        );

        // Seek beyond the file should be possible
        wasi::fd_seek(file_fd, 1000, wasi::WHENCE_CUR).expect("seeking beyond the end of the file");

        // Seek before byte 0 is an error though
        assert_eq!(
            wasi::fd_seek(file_fd, -2000, wasi::WHENCE_CUR)
                .expect_err("seeking before byte 0 should be an error"),
            wasi::ERRNO_INVAL
        );

        // Check that fd_read properly updates the file offset
        wasi::fd_seek(file_fd, 0, wasi::WHENCE_SET)
            .expect("seeking to the beginning of the file again");

        let buffer = &mut [0u8; 100];
        let iovec = wasi::Iovec {
            buf: buffer.as_mut_ptr(),
            buf_len: buffer.len(),
        };
        let nread = wasi::fd_read(file_fd, &[iovec]).expect("reading file");
        assert_eq!(nread, buffer.len(), "should read {} bytes", buffer.len());

        offset = wasi::fd_tell(file_fd).expect("getting file offset after reading");
        assert_eq!(offset, 100, "offset after reading should be 100");

        wasi::fd_close(file_fd).expect("closing a file");
        wasi::path_unlink_file(dir_fd, "file").expect("deleting a file");
    }
}

// Test that when a file is opened with `O_APPEND` that acquiring the current
// position indicates the end of the file.
#[test]
fn seek_and_o_append() {
    init(&[], &[]);

    let dir_fd = 3;

    unsafe {
        let path = "file2";
        let file_fd = wasi::path_open(
            dir_fd,
            0,
            path,
            wasi::OFLAGS_CREAT,
            wasi::RIGHTS_FD_READ | wasi::RIGHTS_FD_WRITE,
            0,
            wasi::FDFLAGS_APPEND,
        )
        .expect("opening a file");
        assert!(
            file_fd > libc::STDERR_FILENO as wasi::Fd,
            "file descriptor range check",
        );

        let mut offset = wasi::fd_seek(file_fd, 0, wasi::WHENCE_CUR).unwrap();
        assert_eq!(offset, 0);
        offset = wasi::fd_tell(file_fd).unwrap();
        assert_eq!(offset, 0);

        let data = &[0u8; 100];
        let iov = wasi::Ciovec {
            buf: data.as_ptr() as *const _,
            buf_len: data.len(),
        };
        let nwritten = wasi::fd_write(file_fd, &[iov]).unwrap();
        assert_eq!(nwritten, 100);

        let mut offset = wasi::fd_seek(file_fd, 0, wasi::WHENCE_CUR).unwrap();
        assert_eq!(offset, 100);
        offset = wasi::fd_tell(file_fd).unwrap();
        assert_eq!(offset, 100);

        wasi::fd_close(file_fd).unwrap();
        wasi::path_unlink_file(dir_fd, path).unwrap();
    }
}

#[test]
fn test_directory_seek() {
    init(&[], &[]);

    let dir_fd = 3;

    unsafe {
        // Create a directory in the scratch directory.
        wasi::path_create_directory(dir_fd, "dir").expect("failed to make directory");

        // Open the directory and attempt to request rights for seeking.
        let fd = wasi::path_open(dir_fd, 0, "dir", wasi::OFLAGS_DIRECTORY, 0, 0, 0)
            .expect("failed to open file");
        assert!(
            fd > libc::STDERR_FILENO as wasi::Fd,
            "file descriptor range check",
        );

        // Attempt to seek.
        assert_eq!(
            wasi::fd_seek(fd, 0, wasi::WHENCE_CUR).expect_err("seek on a directory"),
            wasi::ERRNO_BADF
        );

        // Clean up.
        wasi::fd_close(fd).expect("failed to close fd");
        wasi::path_remove_directory(dir_fd, "dir").expect("failed to remove dir");
    }
}

#[test]
fn test_file_unbuffered_write() {
    init(&[], &[]);

    let dir_fd = 3;

    unsafe {
        // Create and open file for reading
        let fd_read = wasi::path_open(
            dir_fd,
            0,
            "file",
            wasi::OFLAGS_CREAT,
            wasi::RIGHTS_FD_READ,
            0,
            0,
        )
        .expect("create and open file for reading");
        assert!(
            fd_read > libc::STDERR_FILENO as wasi::Fd,
            "file descriptor range check",
        );

        // Open the same file but for writing
        let fd_write = wasi::path_open(dir_fd, 0, "file", 0, wasi::RIGHTS_FD_WRITE, 0, 0)
            .expect("opening file for writing");
        assert!(
            fd_write > libc::STDERR_FILENO as wasi::Fd,
            "file descriptor range check",
        );

        // Write to file
        let contents = &[1u8];
        let ciovec = wasi::Ciovec {
            buf: contents.as_ptr() as *const _,
            buf_len: contents.len(),
        };
        let nwritten = wasi::fd_write(fd_write, &[ciovec]).expect("writing byte to file");
        assert_eq!(nwritten, 1, "nwritten bytes check");

        // Read from file
        let contents = &mut [0u8; 1];
        let iovec = wasi::Iovec {
            buf: contents.as_mut_ptr() as *mut _,
            buf_len: contents.len(),
        };
        let nread = wasi::fd_read(fd_read, &[iovec]).expect("reading bytes from file");
        assert_eq!(nread, 1, "nread bytes check");
        assert_eq!(contents, &[1u8], "written bytes equal read bytes");

        // Clean up
        wasi::fd_close(fd_write).expect("closing a file");
        wasi::fd_close(fd_read).expect("closing a file");
        wasi::path_unlink_file(dir_fd, "file").expect("removing a file");
    }
}

#[test]
fn test_file_long_write() {
    init(&[], &[]);

    let dir_fd = 3;
    let filename: &str = "long_write.txt";

    unsafe {
        // Open a file for writing
        let file_fd = wasi::path_open(
            dir_fd,
            0,
            filename,
            wasi::OFLAGS_CREAT,
            wasi::RIGHTS_FD_WRITE,
            0,
            0,
        )
        .expect("creating a file for writing");

        let mut content = Vec::new();
        // 16 byte string, 4096 times, is 64k
        for n in 0..4096 {
            let chunk = format!("123456789 {n:05} ");
            assert_eq!(chunk.as_str().len(), 16);
            content.extend_from_slice(chunk.as_bytes());
        }

        // Write to the file
        let nwritten = wasi::fd_write(
            file_fd,
            &[wasi::Ciovec {
                buf: content.as_slice().as_ptr() as *const _,
                buf_len: content.len(),
            }],
        )
        .expect("writing file content");
        assert_eq!(nwritten, content.len(), "nwritten bytes check");

        let stat = wasi::fd_filestat_get(file_fd).expect("reading file stats");
        assert_eq!(
            stat.size,
            content.len() as u64,
            "file should be size of content",
        );

        wasi::fd_close(file_fd).expect("closing the file");
        // Open the file for reading
        let file_fd = wasi::path_open(dir_fd, 0, filename, 0, wasi::RIGHTS_FD_READ, 0, 0)
            .expect("open the file for reading");

        // Read the file's contents
        let buffer = &mut [0u8; 100];
        let nread = wasi::fd_read(
            file_fd,
            &[wasi::Iovec {
                buf: buffer.as_mut_ptr(),
                buf_len: buffer.len(),
            }],
        )
        .expect("reading first chunk file content");

        assert_eq!(nread, buffer.len(), "read first chunk");
        assert_eq!(
            buffer,
            &content[..buffer.len()],
            "contents of first read chunk"
        );

        let end_cursor = content.len() - buffer.len();
        wasi::fd_seek(file_fd, end_cursor as i64, wasi::WHENCE_SET)
            .expect("seeking to end of file minus buffer size");

        let nread = wasi::fd_read(
            file_fd,
            &[wasi::Iovec {
                buf: buffer.as_mut_ptr(),
                buf_len: buffer.len(),
            }],
        )
        .expect("reading end chunk of file content");

        assert_eq!(nread, buffer.len(), "read end chunk len");
        assert_eq!(buffer, &content[end_cursor..], "contents of end read chunk");

        wasi::fd_close(file_fd).expect("closing the file");

        // Open a file for writing
        let filename = "test-zero-write-fails.txt";
        let file_fd = wasi::path_open(
            dir_fd,
            0,
            filename,
            wasi::OFLAGS_CREAT,
            wasi::RIGHTS_FD_WRITE,
            0,
            0,
        )
        .expect("creating a file for writing");
        wasi::fd_close(file_fd).expect("closing the file");
        let file_fd = wasi::path_open(dir_fd, 0, filename, 0, wasi::RIGHTS_FD_READ, 0, 0)
            .expect("opening a file for writing");
        let res = wasi::fd_write(
            file_fd,
            &[wasi::Ciovec {
                buf: 3 as *const u8,
                buf_len: 0,
            }],
        );
        assert!(
            res == Err(wasi::ERRNO_BADF) || res == Err(wasi::ERRNO_PERM),
            "bad result {res:?}"
        )
    }
}