File: handles.rs

package info (click to toggle)
rust-wasmtime 26.0.1%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 48,492 kB
  • sloc: ansic: 4,003; sh: 561; javascript: 542; cpp: 254; asm: 175; ml: 96; makefile: 55
file content (78 lines) | stat: -rw-r--r-- 2,109 bytes parent folder | download
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
use proptest::prelude::*;
use wiggle::{GuestMemory, GuestPtr, GuestType};
use wiggle_test::{impl_errno, HostMemory, MemArea, WasiCtx};

const FD_VAL: u32 = 123;

wiggle::from_witx!({
    witx: ["$CARGO_MANIFEST_DIR/tests/handles.witx"],
});

impl_errno!(types::Errno);

impl<'a> handle_examples::HandleExamples for WasiCtx<'a> {
    fn fd_create(&mut self, _memory: &mut GuestMemory<'_>) -> Result<types::Fd, types::Errno> {
        Ok(types::Fd::from(FD_VAL))
    }
    fn fd_consume(
        &mut self,
        _memory: &mut GuestMemory<'_>,
        fd: types::Fd,
    ) -> Result<(), types::Errno> {
        println!("FD_CONSUME {fd}");
        if fd == types::Fd::from(FD_VAL) {
            Ok(())
        } else {
            Err(types::Errno::InvalidArg)
        }
    }
}

#[derive(Debug)]
struct HandleExercise {
    pub return_loc: MemArea,
}

impl HandleExercise {
    pub fn test(&self) {
        let mut ctx = WasiCtx::new();
        let mut host_memory = HostMemory::new();
        let mut memory = host_memory.guest_memory();

        let e =
            handle_examples::fd_create(&mut ctx, &mut memory, self.return_loc.ptr as i32).unwrap();

        assert_eq!(e, types::Errno::Ok as i32, "fd_create error");

        let h_got: u32 = memory
            .read(GuestPtr::new(self.return_loc.ptr))
            .expect("return ref_mut");

        assert_eq!(h_got, 123, "fd_create return val");

        let e = handle_examples::fd_consume(&mut ctx, &mut memory, h_got as i32).unwrap();

        assert_eq!(e, types::Errno::Ok as i32, "fd_consume error");

        let e = handle_examples::fd_consume(&mut ctx, &mut memory, h_got as i32 + 1).unwrap();

        assert_eq!(
            e,
            types::Errno::InvalidArg as i32,
            "fd_consume invalid error"
        );
    }

    pub fn strat() -> BoxedStrategy<Self> {
        (HostMemory::mem_area_strat(types::Fd::guest_size()))
            .prop_map(|return_loc| HandleExercise { return_loc })
            .boxed()
    }
}

proptest! {
    #[test]
    fn handle_exercise(e in HandleExercise::strat()) {
        e.test()
    }
}