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
use wasmer_wasix_types::wasi::SiFlags;

use crate::syscalls::sock_send_internal;

use super::*;

impl JournalEffector {
    pub fn save_sock_send<M: MemorySize>(
        ctx: &FunctionEnvMut<'_, WasiEnv>,
        fd: Fd,
        sent: usize,
        iovs: WasmPtr<__wasi_ciovec_t<M>, M>,
        iovs_len: M::Offset,
        si_flags: SiFlags,
    ) -> anyhow::Result<()> {
        let env = ctx.data();
        let memory = unsafe { env.memory_view(&ctx) };
        let iovs_arr = iovs.slice(&memory, iovs_len)?;

        let iovs_arr = iovs_arr.access().map_err(mem_error_to_wasi)?;
        let mut remaining: M::Offset = TryFrom::<usize>::try_from(sent).unwrap_or_default();
        for iovs in iovs_arr.iter() {
            let sub = iovs.buf_len.min(remaining);
            if sub == M::ZERO {
                continue;
            }
            remaining -= sub;

            let buf = WasmPtr::<u8, M>::new(iovs.buf)
                .slice(&memory, sub)
                .map_err(mem_error_to_wasi)?
                .access()
                .map_err(mem_error_to_wasi)?;
            ctx.data()
                .active_journal()?
                .write(JournalEntry::SocketSendV1 {
                    fd,
                    data: Cow::Borrowed(buf.as_ref()),
                    is_64bit: M::is_64bit(),
                    flags: si_flags,
                })
                .map_err(map_snapshot_err)?;
        }
        Ok(())
    }

    pub fn apply_sock_send<M: MemorySize>(
        ctx: &FunctionEnvMut<'_, WasiEnv>,
        sock: Fd,
        si_data: Cow<'_, [u8]>,
        si_flags: SiFlags,
    ) -> anyhow::Result<()> {
        let data_len = si_data.len();
        sock_send_internal(ctx, sock, FdWriteSource::<'_, M>::Buffer(si_data), si_flags)?.map_err(
            |err| {
                anyhow::format_err!(
                    "journal restore error: failed to send on socket (fd={}, data.len={}) - {}",
                    sock,
                    data_len,
                    err
                )
            },
        )?;
        Ok(())
    }
}