File: pointers.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 (186 lines) | stat: -rw-r--r-- 5,727 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
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
use proptest::prelude::*;
use wiggle::{GuestMemory, GuestPtr};
use wiggle_test::{impl_errno, HostMemory, MemArea, WasiCtx};

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

impl_errno!(types::Errno);

impl<'a> pointers::Pointers for WasiCtx<'a> {
    fn pointers_and_enums(
        &mut self,
        memory: &mut GuestMemory<'_>,
        input1: types::Excuse,
        input2_ptr: GuestPtr<types::Excuse>,
        input3_ptr: GuestPtr<types::Excuse>,
        input4_ptr_ptr: GuestPtr<GuestPtr<types::Excuse>>,
    ) -> Result<(), types::Errno> {
        println!("BAZ input1 {input1:?}");
        let input2: types::Excuse = memory.read(input2_ptr).map_err(|e| {
            eprintln!("input2_ptr error: {e}");
            types::Errno::InvalidArg
        })?;
        println!("input2 {input2:?}");

        // Read enum value from immutable ptr:
        let input3 = memory.read(input3_ptr).map_err(|e| {
            eprintln!("input3_ptr error: {e}");
            types::Errno::InvalidArg
        })?;
        println!("input3 {input3:?}");

        // Write enum to mutable ptr:
        memory.write(input2_ptr, input3).map_err(|e| {
            eprintln!("input2_ptr error: {e}");
            types::Errno::InvalidArg
        })?;
        println!("wrote to input2_ref {input3:?}");

        // Read ptr value from mutable ptr:
        let input4_ptr: GuestPtr<types::Excuse> = memory.read(input4_ptr_ptr).map_err(|e| {
            eprintln!("input4_ptr_ptr error: {e}");
            types::Errno::InvalidArg
        })?;

        // Read enum value from that ptr:
        let input4: types::Excuse = memory.read(input4_ptr).map_err(|e| {
            eprintln!("input4_ptr error: {e}");
            types::Errno::InvalidArg
        })?;
        println!("input4 {input4:?}");

        // Write ptr value to mutable ptr:
        memory.write(input4_ptr_ptr, input2_ptr).map_err(|e| {
            eprintln!("input4_ptr_ptr error: {e}");
            types::Errno::InvalidArg
        })?;

        Ok(())
    }
}

fn excuse_strat() -> impl Strategy<Value = types::Excuse> {
    prop_oneof![
        Just(types::Excuse::DogAte),
        Just(types::Excuse::Traffic),
        Just(types::Excuse::Sleeping),
    ]
    .boxed()
}

#[derive(Debug)]
struct PointersAndEnumsExercise {
    pub input1: types::Excuse,
    pub input2: types::Excuse,
    pub input2_loc: MemArea,
    pub input3: types::Excuse,
    pub input3_loc: MemArea,
    pub input4: types::Excuse,
    pub input4_loc: MemArea,
    pub input4_ptr_loc: MemArea,
}

impl PointersAndEnumsExercise {
    pub fn strat() -> BoxedStrategy<Self> {
        (
            excuse_strat(),
            excuse_strat(),
            HostMemory::mem_area_strat(4),
            excuse_strat(),
            HostMemory::mem_area_strat(4),
            excuse_strat(),
            HostMemory::mem_area_strat(4),
            HostMemory::mem_area_strat(4),
        )
            .prop_map(
                |(
                    input1,
                    input2,
                    input2_loc,
                    input3,
                    input3_loc,
                    input4,
                    input4_loc,
                    input4_ptr_loc,
                )| PointersAndEnumsExercise {
                    input1,
                    input2,
                    input2_loc,
                    input3,
                    input3_loc,
                    input4,
                    input4_loc,
                    input4_ptr_loc,
                },
            )
            .prop_filter("non-overlapping pointers", |e| {
                MemArea::non_overlapping_set(&[
                    e.input2_loc,
                    e.input3_loc,
                    e.input4_loc,
                    e.input4_ptr_loc,
                ])
            })
            .boxed()
    }
    pub fn test(&self) {
        let mut ctx = WasiCtx::new();
        let mut host_memory = HostMemory::new();
        let mut memory = host_memory.guest_memory();

        memory
            .write(GuestPtr::new(self.input2_loc.ptr), self.input2)
            .expect("input2 ref_mut");

        memory
            .write(GuestPtr::new(self.input3_loc.ptr), self.input3)
            .expect("input3 ref_mut");

        memory
            .write(GuestPtr::new(self.input4_loc.ptr), self.input4)
            .expect("input4 ref_mut");

        memory
            .write(GuestPtr::new(self.input4_ptr_loc.ptr), self.input4_loc.ptr)
            .expect("input4 ptr ref_mut");

        let e = pointers::pointers_and_enums(
            &mut ctx,
            &mut memory,
            self.input1 as i32,
            self.input2_loc.ptr as i32,
            self.input3_loc.ptr as i32,
            self.input4_ptr_loc.ptr as i32,
        )
        .unwrap();
        assert_eq!(e, types::Errno::Ok as i32, "errno");

        // Implementation of pointers_and_enums writes input3 to the input2_loc:
        let written_to_input2_loc: i32 = memory
            .read(GuestPtr::new(self.input2_loc.ptr))
            .expect("input2 ref");

        assert_eq!(
            written_to_input2_loc, self.input3 as i32,
            "pointers_and_enums written to input2"
        );

        // Implementation of pointers_and_enums writes input2_loc to input4_ptr_loc:
        let written_to_input4_ptr: u32 = memory
            .read(GuestPtr::new(self.input4_ptr_loc.ptr))
            .expect("input4_ptr_loc ref");

        assert_eq!(
            written_to_input4_ptr, self.input2_loc.ptr,
            "pointers_and_enums written to input4_ptr"
        );
    }
}
proptest! {
    #[test]
    fn pointers_and_enums(e in PointersAndEnumsExercise::strat()) {
        e.test();
    }
}