wasi-common 0.9.0

WASI implementation in Rust
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
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
#![allow(non_camel_case_types)]
use super::fs_helpers::path_get;
use crate::old::snapshot_0::ctx::WasiCtx;
use crate::old::snapshot_0::fdentry::{Descriptor, FdEntry};
use crate::old::snapshot_0::helpers::*;
use crate::old::snapshot_0::memory::*;
use crate::old::snapshot_0::sys::fdentry_impl::determine_type_rights;
use crate::old::snapshot_0::sys::hostcalls_impl::fs_helpers::path_open_rights;
use crate::old::snapshot_0::sys::{host_impl, hostcalls_impl};
use crate::old::snapshot_0::{helpers, host, wasi, wasi32, Error, Result};
use crate::sandboxed_tty_writer::SandboxedTTYWriter;
use filetime::{set_file_handle_times, FileTime};
use log::trace;
use std::fs::File;
use std::io::{self, Read, Seek, SeekFrom, Write};
use std::ops::DerefMut;
use std::time::{Duration, SystemTime, UNIX_EPOCH};

pub(crate) unsafe fn fd_close(wasi_ctx: &mut WasiCtx, fd: wasi::__wasi_fd_t) -> Result<()> {
    trace!("fd_close(fd={:?})", fd);

    if let Ok(fe) = wasi_ctx.get_fd_entry(fd) {
        // can't close preopened files
        if fe.preopen_path.is_some() {
            return Err(Error::ENOTSUP);
        }
    }

    wasi_ctx.remove_fd_entry(fd)?;
    Ok(())
}

pub(crate) unsafe fn fd_datasync(wasi_ctx: &WasiCtx, fd: wasi::__wasi_fd_t) -> Result<()> {
    trace!("fd_datasync(fd={:?})", fd);

    let fd = wasi_ctx
        .get_fd_entry(fd)?
        .as_descriptor(wasi::__WASI_RIGHTS_FD_DATASYNC, 0)?
        .as_file()?;

    fd.sync_data().map_err(Into::into)
}

pub(crate) unsafe fn fd_pread(
    wasi_ctx: &WasiCtx,
    memory: &mut [u8],
    fd: wasi::__wasi_fd_t,
    iovs_ptr: wasi32::uintptr_t,
    iovs_len: wasi32::size_t,
    offset: wasi::__wasi_filesize_t,
    nread: wasi32::uintptr_t,
) -> Result<()> {
    trace!(
        "fd_pread(fd={:?}, iovs_ptr={:#x?}, iovs_len={:?}, offset={}, nread={:#x?})",
        fd,
        iovs_ptr,
        iovs_len,
        offset,
        nread
    );

    let fd = wasi_ctx
        .get_fd_entry(fd)?
        .as_descriptor(wasi::__WASI_RIGHTS_FD_READ | wasi::__WASI_RIGHTS_FD_SEEK, 0)?
        .as_file()?;

    let iovs = dec_iovec_slice(memory, iovs_ptr, iovs_len)?;

    if offset > i64::max_value() as u64 {
        return Err(Error::EIO);
    }
    let buf_size = iovs.iter().map(|v| v.buf_len).sum();
    let mut buf = vec![0; buf_size];
    let host_nread = hostcalls_impl::fd_pread(fd, &mut buf, offset)?;
    let mut buf_offset = 0;
    let mut left = host_nread;
    for iov in &iovs {
        if left == 0 {
            break;
        }
        let vec_len = std::cmp::min(iov.buf_len, left);
        std::slice::from_raw_parts_mut(iov.buf as *mut u8, vec_len)
            .copy_from_slice(&buf[buf_offset..buf_offset + vec_len]);
        buf_offset += vec_len;
        left -= vec_len;
    }

    trace!("     | *nread={:?}", host_nread);

    enc_usize_byref(memory, nread, host_nread)
}

pub(crate) unsafe fn fd_pwrite(
    wasi_ctx: &WasiCtx,
    memory: &mut [u8],
    fd: wasi::__wasi_fd_t,
    iovs_ptr: wasi32::uintptr_t,
    iovs_len: wasi32::size_t,
    offset: wasi::__wasi_filesize_t,
    nwritten: wasi32::uintptr_t,
) -> Result<()> {
    trace!(
        "fd_pwrite(fd={:?}, iovs_ptr={:#x?}, iovs_len={:?}, offset={}, nwritten={:#x?})",
        fd,
        iovs_ptr,
        iovs_len,
        offset,
        nwritten
    );

    let fd = wasi_ctx
        .get_fd_entry(fd)?
        .as_descriptor(
            wasi::__WASI_RIGHTS_FD_WRITE | wasi::__WASI_RIGHTS_FD_SEEK,
            0,
        )?
        .as_file()?;
    let iovs = dec_ciovec_slice(memory, iovs_ptr, iovs_len)?;

    if offset > i64::max_value() as u64 {
        return Err(Error::EIO);
    }
    let buf_size = iovs.iter().map(|v| v.buf_len).sum();
    let mut buf = Vec::with_capacity(buf_size);
    for iov in &iovs {
        buf.extend_from_slice(std::slice::from_raw_parts(
            iov.buf as *const u8,
            iov.buf_len,
        ));
    }
    let host_nwritten = hostcalls_impl::fd_pwrite(fd, &buf, offset)?;

    trace!("     | *nwritten={:?}", host_nwritten);

    enc_usize_byref(memory, nwritten, host_nwritten)
}

pub(crate) unsafe fn fd_read(
    wasi_ctx: &mut WasiCtx,
    memory: &mut [u8],
    fd: wasi::__wasi_fd_t,
    iovs_ptr: wasi32::uintptr_t,
    iovs_len: wasi32::size_t,
    nread: wasi32::uintptr_t,
) -> Result<()> {
    trace!(
        "fd_read(fd={:?}, iovs_ptr={:#x?}, iovs_len={:?}, nread={:#x?})",
        fd,
        iovs_ptr,
        iovs_len,
        nread
    );

    let mut iovs = dec_iovec_slice(memory, iovs_ptr, iovs_len)?;
    let mut iovs: Vec<io::IoSliceMut> = iovs
        .iter_mut()
        .map(|vec| host::iovec_to_host_mut(vec))
        .collect();

    let maybe_host_nread = match wasi_ctx
        .get_fd_entry_mut(fd)?
        .as_descriptor_mut(wasi::__WASI_RIGHTS_FD_READ, 0)?
    {
        Descriptor::OsHandle(file) => file.read_vectored(&mut iovs),
        Descriptor::Stdin => io::stdin().read_vectored(&mut iovs),
        _ => return Err(Error::EBADF),
    };

    let host_nread = maybe_host_nread?;

    trace!("     | *nread={:?}", host_nread);

    enc_usize_byref(memory, nread, host_nread)
}

pub(crate) unsafe fn fd_renumber(
    wasi_ctx: &mut WasiCtx,
    from: wasi::__wasi_fd_t,
    to: wasi::__wasi_fd_t,
) -> Result<()> {
    trace!("fd_renumber(from={:?}, to={:?})", from, to);

    if !wasi_ctx.contains_fd_entry(from) {
        return Err(Error::EBADF);
    }

    // Don't allow renumbering over a pre-opened resource.
    // TODO: Eventually, we do want to permit this, once libpreopen in
    // userspace is capable of removing entries from its tables as well.
    let from_fe = wasi_ctx.get_fd_entry(from)?;
    if from_fe.preopen_path.is_some() {
        return Err(Error::ENOTSUP);
    }
    if let Ok(to_fe) = wasi_ctx.get_fd_entry(to) {
        if to_fe.preopen_path.is_some() {
            return Err(Error::ENOTSUP);
        }
    }

    let fe = wasi_ctx.remove_fd_entry(from)?;
    wasi_ctx.insert_fd_entry_at(to, fe);

    Ok(())
}

pub(crate) unsafe fn fd_seek(
    wasi_ctx: &mut WasiCtx,
    memory: &mut [u8],
    fd: wasi::__wasi_fd_t,
    offset: wasi::__wasi_filedelta_t,
    whence: wasi::__wasi_whence_t,
    newoffset: wasi32::uintptr_t,
) -> Result<()> {
    trace!(
        "fd_seek(fd={:?}, offset={:?}, whence={}, newoffset={:#x?})",
        fd,
        offset,
        wasi::whence_to_str(whence),
        newoffset
    );

    let rights = if offset == 0 && whence == wasi::__WASI_WHENCE_CUR {
        wasi::__WASI_RIGHTS_FD_TELL
    } else {
        wasi::__WASI_RIGHTS_FD_SEEK | wasi::__WASI_RIGHTS_FD_TELL
    };
    let fd = wasi_ctx
        .get_fd_entry_mut(fd)?
        .as_descriptor_mut(rights, 0)?
        .as_file_mut()?;

    let pos = match whence {
        wasi::__WASI_WHENCE_CUR => SeekFrom::Current(offset),
        wasi::__WASI_WHENCE_END => SeekFrom::End(offset),
        wasi::__WASI_WHENCE_SET => SeekFrom::Start(offset as u64),
        _ => return Err(Error::EINVAL),
    };
    let host_newoffset = fd.seek(pos)?;

    trace!("     | *newoffset={:?}", host_newoffset);

    enc_filesize_byref(memory, newoffset, host_newoffset)
}

pub(crate) unsafe fn fd_tell(
    wasi_ctx: &mut WasiCtx,
    memory: &mut [u8],
    fd: wasi::__wasi_fd_t,
    newoffset: wasi32::uintptr_t,
) -> Result<()> {
    trace!("fd_tell(fd={:?}, newoffset={:#x?})", fd, newoffset);

    let fd = wasi_ctx
        .get_fd_entry_mut(fd)?
        .as_descriptor_mut(wasi::__WASI_RIGHTS_FD_TELL, 0)?
        .as_file_mut()?;

    let host_offset = fd.seek(SeekFrom::Current(0))?;

    trace!("     | *newoffset={:?}", host_offset);

    enc_filesize_byref(memory, newoffset, host_offset)
}

pub(crate) unsafe fn fd_fdstat_get(
    wasi_ctx: &WasiCtx,
    memory: &mut [u8],
    fd: wasi::__wasi_fd_t,
    fdstat_ptr: wasi32::uintptr_t, // *mut wasi::__wasi_fdstat_t
) -> Result<()> {
    trace!("fd_fdstat_get(fd={:?}, fdstat_ptr={:#x?})", fd, fdstat_ptr);

    let mut fdstat = dec_fdstat_byref(memory, fdstat_ptr)?;
    let host_fd = wasi_ctx
        .get_fd_entry(fd)?
        .as_descriptor(0, 0)?
        .as_os_handle();

    let fs_flags = hostcalls_impl::fd_fdstat_get(&host_fd)?;

    let fe = wasi_ctx.get_fd_entry(fd)?;
    fdstat.fs_filetype = fe.file_type;
    fdstat.fs_rights_base = fe.rights_base;
    fdstat.fs_rights_inheriting = fe.rights_inheriting;
    fdstat.fs_flags = fs_flags;

    trace!("     | *buf={:?}", fdstat);

    enc_fdstat_byref(memory, fdstat_ptr, fdstat)
}

pub(crate) unsafe fn fd_fdstat_set_flags(
    wasi_ctx: &WasiCtx,
    fd: wasi::__wasi_fd_t,
    fdflags: wasi::__wasi_fdflags_t,
) -> Result<()> {
    trace!("fd_fdstat_set_flags(fd={:?}, fdflags={:#x?})", fd, fdflags);

    let fd = wasi_ctx
        .get_fd_entry(fd)?
        .as_descriptor(0, 0)?
        .as_os_handle();

    hostcalls_impl::fd_fdstat_set_flags(&fd, fdflags)
}

pub(crate) unsafe fn fd_fdstat_set_rights(
    wasi_ctx: &mut WasiCtx,
    fd: wasi::__wasi_fd_t,
    fs_rights_base: wasi::__wasi_rights_t,
    fs_rights_inheriting: wasi::__wasi_rights_t,
) -> Result<()> {
    trace!(
        "fd_fdstat_set_rights(fd={:?}, fs_rights_base={:#x?}, fs_rights_inheriting={:#x?})",
        fd,
        fs_rights_base,
        fs_rights_inheriting
    );

    let fe = wasi_ctx.get_fd_entry_mut(fd)?;
    if fe.rights_base & fs_rights_base != fs_rights_base
        || fe.rights_inheriting & fs_rights_inheriting != fs_rights_inheriting
    {
        return Err(Error::ENOTCAPABLE);
    }
    fe.rights_base = fs_rights_base;
    fe.rights_inheriting = fs_rights_inheriting;

    Ok(())
}

pub(crate) unsafe fn fd_sync(wasi_ctx: &WasiCtx, fd: wasi::__wasi_fd_t) -> Result<()> {
    trace!("fd_sync(fd={:?})", fd);

    let fd = wasi_ctx
        .get_fd_entry(fd)?
        .as_descriptor(wasi::__WASI_RIGHTS_FD_SYNC, 0)?
        .as_file()?;
    fd.sync_all().map_err(Into::into)
}

pub(crate) unsafe fn fd_write(
    wasi_ctx: &mut WasiCtx,
    memory: &mut [u8],
    fd: wasi::__wasi_fd_t,
    iovs_ptr: wasi32::uintptr_t,
    iovs_len: wasi32::size_t,
    nwritten: wasi32::uintptr_t,
) -> Result<()> {
    trace!(
        "fd_write(fd={:?}, iovs_ptr={:#x?}, iovs_len={:?}, nwritten={:#x?})",
        fd,
        iovs_ptr,
        iovs_len,
        nwritten
    );

    let iovs = dec_ciovec_slice(memory, iovs_ptr, iovs_len)?;
    let iovs: Vec<io::IoSlice> = iovs.iter().map(|vec| host::ciovec_to_host(vec)).collect();

    // perform unbuffered writes
    let entry = wasi_ctx.get_fd_entry_mut(fd)?;
    let isatty = entry.isatty();
    let desc = entry.as_descriptor_mut(wasi::__WASI_RIGHTS_FD_WRITE, 0)?;
    let host_nwritten = match desc {
        Descriptor::OsHandle(file) => {
            if isatty {
                SandboxedTTYWriter::new(file.deref_mut()).write_vectored(&iovs)?
            } else {
                file.write_vectored(&iovs)?
            }
        }
        Descriptor::Stdin => return Err(Error::EBADF),
        Descriptor::Stdout => {
            // lock for the duration of the scope
            let stdout = io::stdout();
            let mut stdout = stdout.lock();
            let nwritten = if isatty {
                SandboxedTTYWriter::new(&mut stdout).write_vectored(&iovs)?
            } else {
                stdout.write_vectored(&iovs)?
            };
            stdout.flush()?;
            nwritten
        }
        // Always sanitize stderr, even if it's not directly connected to a tty,
        // because stderr is meant for diagnostics rather than binary output,
        // and may be redirected to a file which could end up being displayed
        // on a tty later.
        Descriptor::Stderr => SandboxedTTYWriter::new(&mut io::stderr()).write_vectored(&iovs)?,
    };

    trace!("     | *nwritten={:?}", host_nwritten);

    enc_usize_byref(memory, nwritten, host_nwritten)
}

pub(crate) unsafe fn fd_advise(
    wasi_ctx: &WasiCtx,
    fd: wasi::__wasi_fd_t,
    offset: wasi::__wasi_filesize_t,
    len: wasi::__wasi_filesize_t,
    advice: wasi::__wasi_advice_t,
) -> Result<()> {
    trace!(
        "fd_advise(fd={:?}, offset={}, len={}, advice={:?})",
        fd,
        offset,
        len,
        advice
    );

    let fd = wasi_ctx
        .get_fd_entry(fd)?
        .as_descriptor(wasi::__WASI_RIGHTS_FD_ADVISE, 0)?
        .as_file()?;

    hostcalls_impl::fd_advise(fd, advice, offset, len)
}

pub(crate) unsafe fn fd_allocate(
    wasi_ctx: &WasiCtx,
    fd: wasi::__wasi_fd_t,
    offset: wasi::__wasi_filesize_t,
    len: wasi::__wasi_filesize_t,
) -> Result<()> {
    trace!("fd_allocate(fd={:?}, offset={}, len={})", fd, offset, len);

    let fd = wasi_ctx
        .get_fd_entry(fd)?
        .as_descriptor(wasi::__WASI_RIGHTS_FD_ALLOCATE, 0)?
        .as_file()?;

    let metadata = fd.metadata()?;

    let current_size = metadata.len();
    let wanted_size = offset.checked_add(len).ok_or(Error::E2BIG)?;
    // This check will be unnecessary when rust-lang/rust#63326 is fixed
    if wanted_size > i64::max_value() as u64 {
        return Err(Error::E2BIG);
    }

    if wanted_size > current_size {
        fd.set_len(wanted_size).map_err(Into::into)
    } else {
        Ok(())
    }
}

pub(crate) unsafe fn path_create_directory(
    wasi_ctx: &WasiCtx,
    memory: &mut [u8],
    dirfd: wasi::__wasi_fd_t,
    path_ptr: wasi32::uintptr_t,
    path_len: wasi32::size_t,
) -> Result<()> {
    trace!(
        "path_create_directory(dirfd={:?}, path_ptr={:#x?}, path_len={})",
        dirfd,
        path_ptr,
        path_len,
    );

    let path = dec_slice_of_u8(memory, path_ptr, path_len).and_then(helpers::path_from_slice)?;

    trace!("     | (path_ptr,path_len)='{}'", path);

    let rights = wasi::__WASI_RIGHTS_PATH_OPEN | wasi::__WASI_RIGHTS_PATH_CREATE_DIRECTORY;
    let fe = wasi_ctx.get_fd_entry(dirfd)?;
    let resolved = path_get(fe, rights, 0, 0, path, false)?;

    hostcalls_impl::path_create_directory(resolved)
}

pub(crate) unsafe fn path_link(
    wasi_ctx: &WasiCtx,
    memory: &mut [u8],
    old_dirfd: wasi::__wasi_fd_t,
    old_flags: wasi::__wasi_lookupflags_t,
    old_path_ptr: wasi32::uintptr_t,
    old_path_len: wasi32::size_t,
    new_dirfd: wasi::__wasi_fd_t,
    new_path_ptr: wasi32::uintptr_t,
    new_path_len: wasi32::size_t,
) -> Result<()> {
    trace!(
        "path_link(old_dirfd={:?}, old_flags={:?}, old_path_ptr={:#x?}, old_path_len={}, new_dirfd={:?}, new_path_ptr={:#x?}, new_path_len={})",
        old_dirfd,
        old_flags,
        old_path_ptr,
        old_path_len,
        new_dirfd,
        new_path_ptr,
        new_path_len,
    );

    let old_path = dec_slice_of_u8(memory, old_path_ptr, old_path_len).and_then(path_from_slice)?;
    let new_path = dec_slice_of_u8(memory, new_path_ptr, new_path_len).and_then(path_from_slice)?;

    trace!("     | (old_path_ptr,old_path_len)='{}'", old_path);
    trace!("     | (new_path_ptr,new_path_len)='{}'", new_path);

    let old_fe = wasi_ctx.get_fd_entry(old_dirfd)?;
    let new_fe = wasi_ctx.get_fd_entry(new_dirfd)?;
    let resolved_old = path_get(
        old_fe,
        wasi::__WASI_RIGHTS_PATH_LINK_SOURCE,
        0,
        0,
        old_path,
        false,
    )?;
    let resolved_new = path_get(
        new_fe,
        wasi::__WASI_RIGHTS_PATH_LINK_TARGET,
        0,
        0,
        new_path,
        false,
    )?;

    hostcalls_impl::path_link(resolved_old, resolved_new)
}

pub(crate) unsafe fn path_open(
    wasi_ctx: &mut WasiCtx,
    memory: &mut [u8],
    dirfd: wasi::__wasi_fd_t,
    dirflags: wasi::__wasi_lookupflags_t,
    path_ptr: wasi32::uintptr_t,
    path_len: wasi32::size_t,
    oflags: wasi::__wasi_oflags_t,
    fs_rights_base: wasi::__wasi_rights_t,
    fs_rights_inheriting: wasi::__wasi_rights_t,
    fs_flags: wasi::__wasi_fdflags_t,
    fd_out_ptr: wasi32::uintptr_t,
) -> Result<()> {
    trace!(
        "path_open(dirfd={:?}, dirflags={:?}, path_ptr={:#x?}, path_len={:?}, oflags={:#x?}, fs_rights_base={:#x?}, fs_rights_inheriting={:#x?}, fs_flags={:#x?}, fd_out_ptr={:#x?})",
        dirfd,
        dirflags,
        path_ptr,
        path_len,
        oflags,
        fs_rights_base,
        fs_rights_inheriting,
        fs_flags,
        fd_out_ptr
    );

    // pre-encode fd_out_ptr to -1 in case of error in opening a path
    enc_fd_byref(memory, fd_out_ptr, wasi::__wasi_fd_t::max_value())?;

    let path = dec_slice_of_u8(memory, path_ptr, path_len).and_then(path_from_slice)?;

    trace!("     | (path_ptr,path_len)='{}'", path);

    let (needed_base, needed_inheriting) =
        path_open_rights(fs_rights_base, fs_rights_inheriting, oflags, fs_flags);
    let fe = wasi_ctx.get_fd_entry(dirfd)?;
    let resolved = path_get(
        fe,
        needed_base,
        needed_inheriting,
        dirflags,
        path,
        oflags & wasi::__WASI_OFLAGS_CREAT != 0,
    )?;

    // which open mode do we need?
    let read = fs_rights_base & (wasi::__WASI_RIGHTS_FD_READ | wasi::__WASI_RIGHTS_FD_READDIR) != 0;
    let write = fs_rights_base
        & (wasi::__WASI_RIGHTS_FD_DATASYNC
            | wasi::__WASI_RIGHTS_FD_WRITE
            | wasi::__WASI_RIGHTS_FD_ALLOCATE
            | wasi::__WASI_RIGHTS_FD_FILESTAT_SET_SIZE)
        != 0;

    let fd = hostcalls_impl::path_open(resolved, read, write, oflags, fs_flags)?;

    // Determine the type of the new file descriptor and which rights contradict with this type
    let (_ty, max_base, max_inheriting) = determine_type_rights(&fd)?;
    let mut fe = FdEntry::from(fd)?;
    fe.rights_base &= max_base;
    fe.rights_inheriting &= max_inheriting;
    let guest_fd = wasi_ctx.insert_fd_entry(fe)?;

    trace!("     | *fd={:?}", guest_fd);

    enc_fd_byref(memory, fd_out_ptr, guest_fd)
}

pub(crate) unsafe fn path_readlink(
    wasi_ctx: &WasiCtx,
    memory: &mut [u8],
    dirfd: wasi::__wasi_fd_t,
    path_ptr: wasi32::uintptr_t,
    path_len: wasi32::size_t,
    buf_ptr: wasi32::uintptr_t,
    buf_len: wasi32::size_t,
    buf_used: wasi32::uintptr_t,
) -> Result<()> {
    trace!(
        "path_readlink(dirfd={:?}, path_ptr={:#x?}, path_len={:?}, buf_ptr={:#x?}, buf_len={}, buf_used={:#x?})",
        dirfd,
        path_ptr,
        path_len,
        buf_ptr,
        buf_len,
        buf_used,
    );

    enc_usize_byref(memory, buf_used, 0)?;

    let path = dec_slice_of_u8(memory, path_ptr, path_len).and_then(helpers::path_from_slice)?;

    trace!("     | (path_ptr,path_len)='{}'", &path);

    let fe = wasi_ctx.get_fd_entry(dirfd)?;
    let resolved = path_get(fe, wasi::__WASI_RIGHTS_PATH_READLINK, 0, 0, &path, false)?;

    let mut buf = dec_slice_of_mut_u8(memory, buf_ptr, buf_len)?;

    let host_bufused = hostcalls_impl::path_readlink(resolved, &mut buf)?;

    trace!("     | (buf_ptr,*buf_used)={:?}", buf);
    trace!("     | *buf_used={:?}", host_bufused);

    enc_usize_byref(memory, buf_used, host_bufused)
}

pub(crate) unsafe fn path_rename(
    wasi_ctx: &WasiCtx,
    memory: &mut [u8],
    old_dirfd: wasi::__wasi_fd_t,
    old_path_ptr: wasi32::uintptr_t,
    old_path_len: wasi32::size_t,
    new_dirfd: wasi::__wasi_fd_t,
    new_path_ptr: wasi32::uintptr_t,
    new_path_len: wasi32::size_t,
) -> Result<()> {
    trace!(
        "path_rename(old_dirfd={:?}, old_path_ptr={:#x?}, old_path_len={:?}, new_dirfd={:?}, new_path_ptr={:#x?}, new_path_len={:?})",
        old_dirfd,
        old_path_ptr,
        old_path_len,
        new_dirfd,
        new_path_ptr,
        new_path_len,
    );

    let old_path = dec_slice_of_u8(memory, old_path_ptr, old_path_len).and_then(path_from_slice)?;
    let new_path = dec_slice_of_u8(memory, new_path_ptr, new_path_len).and_then(path_from_slice)?;

    trace!("     | (old_path_ptr,old_path_len)='{}'", old_path);
    trace!("     | (new_path_ptr,new_path_len)='{}'", new_path);

    let old_fe = wasi_ctx.get_fd_entry(old_dirfd)?;
    let new_fe = wasi_ctx.get_fd_entry(new_dirfd)?;
    let resolved_old = path_get(
        old_fe,
        wasi::__WASI_RIGHTS_PATH_RENAME_SOURCE,
        0,
        0,
        old_path,
        true,
    )?;
    let resolved_new = path_get(
        new_fe,
        wasi::__WASI_RIGHTS_PATH_RENAME_TARGET,
        0,
        0,
        new_path,
        true,
    )?;

    log::debug!("path_rename resolved_old={:?}", resolved_old);
    log::debug!("path_rename resolved_new={:?}", resolved_new);

    hostcalls_impl::path_rename(resolved_old, resolved_new)
}

pub(crate) unsafe fn fd_filestat_get(
    wasi_ctx: &WasiCtx,
    memory: &mut [u8],
    fd: wasi::__wasi_fd_t,
    filestat_ptr: wasi32::uintptr_t,
) -> Result<()> {
    trace!(
        "fd_filestat_get(fd={:?}, filestat_ptr={:#x?})",
        fd,
        filestat_ptr
    );

    let fd = wasi_ctx.get_fd_entry(fd)?.as_descriptor(0, 0)?.as_file()?;
    let host_filestat = hostcalls_impl::fd_filestat_get(fd)?;

    trace!("     | *filestat_ptr={:?}", host_filestat);

    enc_filestat_byref(memory, filestat_ptr, host_filestat)
}

pub(crate) unsafe fn fd_filestat_set_times(
    wasi_ctx: &WasiCtx,
    fd: wasi::__wasi_fd_t,
    st_atim: wasi::__wasi_timestamp_t,
    st_mtim: wasi::__wasi_timestamp_t,
    fst_flags: wasi::__wasi_fstflags_t,
) -> Result<()> {
    trace!(
        "fd_filestat_set_times(fd={:?}, st_atim={}, st_mtim={}, fst_flags={:#x?})",
        fd,
        st_atim,
        st_mtim,
        fst_flags
    );

    let fd = wasi_ctx
        .get_fd_entry(fd)?
        .as_descriptor(wasi::__WASI_RIGHTS_FD_FILESTAT_SET_TIMES, 0)?
        .as_file()?;

    fd_filestat_set_times_impl(fd, st_atim, st_mtim, fst_flags)
}

pub(crate) fn fd_filestat_set_times_impl(
    fd: &File,
    st_atim: wasi::__wasi_timestamp_t,
    st_mtim: wasi::__wasi_timestamp_t,
    fst_flags: wasi::__wasi_fstflags_t,
) -> Result<()> {
    let set_atim = fst_flags & wasi::__WASI_FSTFLAGS_ATIM != 0;
    let set_atim_now = fst_flags & wasi::__WASI_FSTFLAGS_ATIM_NOW != 0;
    let set_mtim = fst_flags & wasi::__WASI_FSTFLAGS_MTIM != 0;
    let set_mtim_now = fst_flags & wasi::__WASI_FSTFLAGS_MTIM_NOW != 0;

    if (set_atim && set_atim_now) || (set_mtim && set_mtim_now) {
        return Err(Error::EINVAL);
    }
    let atim = if set_atim {
        let time = UNIX_EPOCH + Duration::from_nanos(st_atim);
        Some(FileTime::from_system_time(time))
    } else if set_atim_now {
        let time = SystemTime::now();
        Some(FileTime::from_system_time(time))
    } else {
        None
    };

    let mtim = if set_mtim {
        let time = UNIX_EPOCH + Duration::from_nanos(st_mtim);
        Some(FileTime::from_system_time(time))
    } else if set_mtim_now {
        let time = SystemTime::now();
        Some(FileTime::from_system_time(time))
    } else {
        None
    };
    set_file_handle_times(fd, atim, mtim).map_err(Into::into)
}

pub(crate) unsafe fn fd_filestat_set_size(
    wasi_ctx: &WasiCtx,
    fd: wasi::__wasi_fd_t,
    st_size: wasi::__wasi_filesize_t,
) -> Result<()> {
    trace!("fd_filestat_set_size(fd={:?}, st_size={})", fd, st_size);

    let fd = wasi_ctx
        .get_fd_entry(fd)?
        .as_descriptor(wasi::__WASI_RIGHTS_FD_FILESTAT_SET_SIZE, 0)?
        .as_file()?;

    // This check will be unnecessary when rust-lang/rust#63326 is fixed
    if st_size > i64::max_value() as u64 {
        return Err(Error::E2BIG);
    }
    fd.set_len(st_size).map_err(Into::into)
}

pub(crate) unsafe fn path_filestat_get(
    wasi_ctx: &WasiCtx,
    memory: &mut [u8],
    dirfd: wasi::__wasi_fd_t,
    dirflags: wasi::__wasi_lookupflags_t,
    path_ptr: wasi32::uintptr_t,
    path_len: wasi32::size_t,
    filestat_ptr: wasi32::uintptr_t,
) -> Result<()> {
    trace!(
        "path_filestat_get(dirfd={:?}, dirflags={:?}, path_ptr={:#x?}, path_len={}, filestat_ptr={:#x?})",
        dirfd,
        dirflags,
        path_ptr,
        path_len,
        filestat_ptr
    );

    let path = dec_slice_of_u8(memory, path_ptr, path_len).and_then(path_from_slice)?;

    trace!("     | (path_ptr,path_len)='{}'", path);

    let fe = wasi_ctx.get_fd_entry(dirfd)?;
    let resolved = path_get(
        fe,
        wasi::__WASI_RIGHTS_PATH_FILESTAT_GET,
        0,
        dirflags,
        path,
        false,
    )?;
    let host_filestat = hostcalls_impl::path_filestat_get(resolved, dirflags)?;

    trace!("     | *filestat_ptr={:?}", host_filestat);

    enc_filestat_byref(memory, filestat_ptr, host_filestat)
}

pub(crate) unsafe fn path_filestat_set_times(
    wasi_ctx: &WasiCtx,
    memory: &mut [u8],
    dirfd: wasi::__wasi_fd_t,
    dirflags: wasi::__wasi_lookupflags_t,
    path_ptr: wasi32::uintptr_t,
    path_len: wasi32::size_t,
    st_atim: wasi::__wasi_timestamp_t,
    st_mtim: wasi::__wasi_timestamp_t,
    fst_flags: wasi::__wasi_fstflags_t,
) -> Result<()> {
    trace!(
        "path_filestat_set_times(dirfd={:?}, dirflags={:?}, path_ptr={:#x?}, path_len={}, st_atim={}, st_mtim={}, fst_flags={:#x?})",
        dirfd,
        dirflags,
        path_ptr,
        path_len,
        st_atim, st_mtim,
        fst_flags
    );

    let path = dec_slice_of_u8(memory, path_ptr, path_len).and_then(path_from_slice)?;

    trace!("     | (path_ptr,path_len)='{}'", path);

    let fe = wasi_ctx.get_fd_entry(dirfd)?;
    let resolved = path_get(
        fe,
        wasi::__WASI_RIGHTS_PATH_FILESTAT_SET_TIMES,
        0,
        dirflags,
        path,
        false,
    )?;

    hostcalls_impl::path_filestat_set_times(resolved, dirflags, st_atim, st_mtim, fst_flags)
}

pub(crate) unsafe fn path_symlink(
    wasi_ctx: &WasiCtx,
    memory: &mut [u8],
    old_path_ptr: wasi32::uintptr_t,
    old_path_len: wasi32::size_t,
    dirfd: wasi::__wasi_fd_t,
    new_path_ptr: wasi32::uintptr_t,
    new_path_len: wasi32::size_t,
) -> Result<()> {
    trace!(
        "path_symlink(old_path_ptr={:#x?}, old_path_len={}, dirfd={:?}, new_path_ptr={:#x?}, new_path_len={})",
        old_path_ptr,
        old_path_len,
        dirfd,
        new_path_ptr,
        new_path_len
    );

    let old_path = dec_slice_of_u8(memory, old_path_ptr, old_path_len).and_then(path_from_slice)?;
    let new_path = dec_slice_of_u8(memory, new_path_ptr, new_path_len).and_then(path_from_slice)?;

    trace!("     | (old_path_ptr,old_path_len)='{}'", old_path);
    trace!("     | (new_path_ptr,new_path_len)='{}'", new_path);

    let fe = wasi_ctx.get_fd_entry(dirfd)?;
    let resolved_new = path_get(fe, wasi::__WASI_RIGHTS_PATH_SYMLINK, 0, 0, new_path, true)?;

    hostcalls_impl::path_symlink(old_path, resolved_new)
}

pub(crate) unsafe fn path_unlink_file(
    wasi_ctx: &WasiCtx,
    memory: &mut [u8],
    dirfd: wasi::__wasi_fd_t,
    path_ptr: wasi32::uintptr_t,
    path_len: wasi32::size_t,
) -> Result<()> {
    trace!(
        "path_unlink_file(dirfd={:?}, path_ptr={:#x?}, path_len={})",
        dirfd,
        path_ptr,
        path_len
    );

    let path = dec_slice_of_u8(memory, path_ptr, path_len).and_then(path_from_slice)?;

    trace!("     | (path_ptr,path_len)='{}'", path);

    let fe = wasi_ctx.get_fd_entry(dirfd)?;
    let resolved = path_get(fe, wasi::__WASI_RIGHTS_PATH_UNLINK_FILE, 0, 0, path, false)?;

    hostcalls_impl::path_unlink_file(resolved)
}

pub(crate) unsafe fn path_remove_directory(
    wasi_ctx: &WasiCtx,
    memory: &mut [u8],
    dirfd: wasi::__wasi_fd_t,
    path_ptr: wasi32::uintptr_t,
    path_len: wasi32::size_t,
) -> Result<()> {
    trace!(
        "path_remove_directory(dirfd={:?}, path_ptr={:#x?}, path_len={})",
        dirfd,
        path_ptr,
        path_len
    );

    let path = dec_slice_of_u8(memory, path_ptr, path_len).and_then(path_from_slice)?;

    trace!("     | (path_ptr,path_len)='{}'", path);

    let fe = wasi_ctx.get_fd_entry(dirfd)?;
    let resolved = path_get(
        fe,
        wasi::__WASI_RIGHTS_PATH_REMOVE_DIRECTORY,
        0,
        0,
        path,
        true,
    )?;

    log::debug!("path_remove_directory resolved={:?}", resolved);

    hostcalls_impl::path_remove_directory(resolved)
}

pub(crate) unsafe fn fd_prestat_get(
    wasi_ctx: &WasiCtx,
    memory: &mut [u8],
    fd: wasi::__wasi_fd_t,
    prestat_ptr: wasi32::uintptr_t,
) -> Result<()> {
    trace!(
        "fd_prestat_get(fd={:?}, prestat_ptr={:#x?})",
        fd,
        prestat_ptr
    );

    // TODO: should we validate any rights here?
    let fe = wasi_ctx.get_fd_entry(fd)?;
    let po_path = fe.preopen_path.as_ref().ok_or(Error::ENOTSUP)?;
    if fe.file_type != wasi::__WASI_FILETYPE_DIRECTORY {
        return Err(Error::ENOTDIR);
    }

    let path = host_impl::path_from_host(po_path.as_os_str())?;

    enc_prestat_byref(
        memory,
        prestat_ptr,
        host::__wasi_prestat_t {
            pr_type: wasi::__WASI_PREOPENTYPE_DIR,
            u: host::__wasi_prestat_u_t {
                dir: host::__wasi_prestat_dir_t {
                    pr_name_len: path.len(),
                },
            },
        },
    )
}

pub(crate) unsafe fn fd_prestat_dir_name(
    wasi_ctx: &WasiCtx,
    memory: &mut [u8],
    fd: wasi::__wasi_fd_t,
    path_ptr: wasi32::uintptr_t,
    path_len: wasi32::size_t,
) -> Result<()> {
    trace!(
        "fd_prestat_dir_name(fd={:?}, path_ptr={:#x?}, path_len={})",
        fd,
        path_ptr,
        path_len
    );

    // TODO: should we validate any rights here?
    let fe = wasi_ctx.get_fd_entry(fd)?;
    let po_path = fe.preopen_path.as_ref().ok_or(Error::ENOTSUP)?;
    if fe.file_type != wasi::__WASI_FILETYPE_DIRECTORY {
        return Err(Error::ENOTDIR);
    }

    let path = host_impl::path_from_host(po_path.as_os_str())?;

    if path.len() > dec_usize(path_len) {
        return Err(Error::ENAMETOOLONG);
    }

    trace!("     | (path_ptr,path_len)='{}'", path);

    enc_slice_of_u8(memory, path.as_bytes(), path_ptr)
}

pub(crate) unsafe fn fd_readdir(
    wasi_ctx: &mut WasiCtx,
    memory: &mut [u8],
    fd: wasi::__wasi_fd_t,
    buf: wasi32::uintptr_t,
    buf_len: wasi32::size_t,
    cookie: wasi::__wasi_dircookie_t,
    buf_used: wasi32::uintptr_t,
) -> Result<()> {
    trace!(
        "fd_readdir(fd={:?}, buf={:#x?}, buf_len={}, cookie={:#x?}, buf_used={:#x?})",
        fd,
        buf,
        buf_len,
        cookie,
        buf_used,
    );

    enc_usize_byref(memory, buf_used, 0)?;

    let file = wasi_ctx
        .get_fd_entry_mut(fd)?
        .as_descriptor_mut(wasi::__WASI_RIGHTS_FD_READDIR, 0)?
        .as_file_mut()?;
    let mut host_buf = dec_slice_of_mut_u8(memory, buf, buf_len)?;

    trace!("     | (buf,buf_len)={:?}", host_buf);

    let iter = hostcalls_impl::fd_readdir(file, cookie)?;
    let mut host_bufused = 0;
    for dirent in iter {
        let dirent_raw = dirent?.to_wasi_raw()?;
        let offset = dirent_raw.len();
        if host_buf.len() < offset {
            break;
        } else {
            host_buf[0..offset].copy_from_slice(&dirent_raw);
            host_bufused += offset;
            host_buf = &mut host_buf[offset..];
        }
    }

    trace!("     | *buf_used={:?}", host_bufused);

    enc_usize_byref(memory, buf_used, host_bufused)
}