wasmer_wasix/syscalls/legacy/
snapshot0.rs

1#![allow(clippy::result_large_err)]
2
3use tracing::{field, instrument, trace_span};
4use wasmer::{AsStoreMut, AsStoreRef, FunctionEnvMut, Memory, WasmPtr};
5use wasmer_wasix_types::wasi::{
6    Errno, Event, EventFdReadwrite, Eventrwflags, Eventtype, ExitCode, Fd, Filesize, Filestat,
7    Filetype, Snapshot0Event, Snapshot0Filestat, Snapshot0Subscription, Snapshot0Whence,
8    Subscription, Whence,
9};
10
11use crate::{
12    Memory32, MemorySize, WasiEnv, WasiError, mem_error_to_wasi,
13    os::task::thread::WasiThread,
14    state::{PollEventBuilder, PollEventSet},
15    syscalls::types,
16    syscalls::{self, handle_rewind},
17};
18
19/// Wrapper around `syscalls::fd_filestat_get` for old Snapshot0
20#[instrument(level = "trace", skip_all, ret)]
21pub fn fd_filestat_get(
22    mut ctx: FunctionEnvMut<WasiEnv>,
23    fd: Fd,
24    buf: WasmPtr<Snapshot0Filestat, Memory32>,
25) -> Errno {
26    syscalls::fd_filestat_get_old::<Memory32>(ctx.as_mut(), fd, buf)
27}
28
29/// Wrapper around `syscalls::path_filestat_get` for old Snapshot0
30#[instrument(level = "trace", skip_all, ret)]
31pub fn path_filestat_get(
32    mut ctx: FunctionEnvMut<WasiEnv>,
33    fd: Fd,
34    flags: types::LookupFlags,
35    path: WasmPtr<u8, Memory32>,
36    path_len: u32,
37    buf: WasmPtr<Snapshot0Filestat, Memory32>,
38) -> Errno {
39    syscalls::path_filestat_get_old::<Memory32>(ctx.as_mut(), fd, flags, path, path_len, buf)
40}
41
42/// Wrapper around `syscalls::fd_seek` with extra logic to remap the values
43/// of `Whence`
44#[instrument(level = "trace", skip_all, ret)]
45pub fn fd_seek(
46    ctx: FunctionEnvMut<WasiEnv>,
47    fd: Fd,
48    offset: types::FileDelta,
49    whence: Snapshot0Whence,
50    newoffset: WasmPtr<Filesize, Memory32>,
51) -> Result<Errno, WasiError> {
52    let new_whence = match whence {
53        Snapshot0Whence::Cur => Whence::Cur,
54        Snapshot0Whence::End => Whence::End,
55        Snapshot0Whence::Set => Whence::Set,
56        Snapshot0Whence::Unknown => return Ok(Errno::Inval),
57    };
58    syscalls::fd_seek::<Memory32>(ctx, fd, offset, new_whence, newoffset)
59}
60
61/// Wrapper around `syscalls::poll_oneoff` with extra logic to add the removed
62/// userdata field back
63#[instrument(level = "trace", skip_all, fields(timeout_ms = field::Empty, fd_guards = field::Empty, seen = field::Empty), ret)]
64pub fn poll_oneoff<M: MemorySize>(
65    mut ctx: FunctionEnvMut<WasiEnv>,
66    in_: WasmPtr<Snapshot0Subscription, Memory32>,
67    out_: WasmPtr<Snapshot0Event, Memory32>,
68    nsubscriptions: u32,
69    nevents: WasmPtr<u32, Memory32>,
70) -> Result<Errno, WasiError> {
71    WasiEnv::do_pending_operations(&mut ctx)?;
72
73    let env = ctx.data();
74    let memory = unsafe { env.memory_view(&ctx) };
75
76    let mut subscriptions = Vec::new();
77    let in_origs = wasi_try_mem_ok!(in_.slice(&memory, nsubscriptions));
78    let in_origs = wasi_try_mem_ok!(in_origs.read_to_vec());
79    for in_orig in in_origs {
80        subscriptions.push((
81            None,
82            PollEventSet::default(),
83            Into::<Subscription>::into(in_orig),
84        ));
85    }
86
87    // Function to invoke once the poll is finished
88    let process_events = |ctx: &FunctionEnvMut<'_, WasiEnv>, triggered_events: Vec<Event>| {
89        let env = ctx.data();
90        let memory = unsafe { env.memory_view(&ctx) };
91
92        // Process all the events that were triggered
93        let mut events_seen: u32 = 0;
94        let event_array = wasi_try_mem!(out_.slice(&memory, nsubscriptions));
95        for event in triggered_events {
96            let event = Snapshot0Event {
97                userdata: event.userdata,
98                error: event.error,
99                type_: Eventtype::FdRead,
100                fd_readwrite: match event.type_ {
101                    Eventtype::FdRead => unsafe { event.u.fd_readwrite },
102                    Eventtype::FdWrite => unsafe { event.u.fd_readwrite },
103                    Eventtype::Clock => EventFdReadwrite {
104                        nbytes: 0,
105                        flags: Eventrwflags::empty(),
106                    },
107                    Eventtype::Unknown => return Errno::Inval,
108                },
109            };
110            wasi_try_mem!(event_array.index(events_seen as u64).write(event));
111            events_seen += 1;
112        }
113        let out_ptr = nevents.deref(&memory);
114        wasi_try_mem!(out_ptr.write(events_seen));
115        Errno::Success
116    };
117
118    // We clear the number of events
119    wasi_try_mem_ok!(nevents.write(&memory, 0));
120
121    // Poll and receive all the events that triggered
122    syscalls::poll_oneoff_internal::<M, _>(ctx, subscriptions, process_events)
123}