wasmer_wasix_types/
types.rs

1#![deny(unused_mut)]
2#![allow(non_camel_case_types, clippy::identity_op)]
3
4//! Wasmer's WASI types implementation.
5//!
6//! Those types aim at being used by [the `wasmer-wasi`
7//! crate](https://github.com/wasmerio/wasmer/blob/main/lib/wasi).
8
9// Needed for #[derive(ValueType)]
10extern crate wasmer_types as wasmer;
11
12pub use crate::types::time::*;
13pub use directory::*;
14pub use file::*;
15pub use io::*;
16pub use net::*;
17pub use signal::*;
18pub use subscription::*;
19
20pub mod file {
21    use crate::wasi::{Fd, Rights};
22
23    pub use crate::wasi::{EventFdFlags, FileDelta, LookupFlags, Oflags};
24
25    pub const __WASI_STDIN_FILENO: Fd = 0;
26    pub const __WASI_STDOUT_FILENO: Fd = 1;
27    pub const __WASI_STDERR_FILENO: Fd = 2;
28
29    pub const EVENT_FD_FLAGS_SEMAPHORE: EventFdFlags = 1;
30    pub const EVENT_FD_FLAGS_NONBLOCK: EventFdFlags = 1 << 2;
31
32    pub const __WASI_LOOKUP_SYMLINK_FOLLOW: LookupFlags = 1;
33
34    /// function for debugging rights issues
35    #[allow(dead_code)]
36    pub fn print_right_set(rights: Rights) {
37        // BTreeSet for consistent order
38        let mut right_set = std::collections::BTreeSet::new();
39        for i in 0..28 {
40            let cur_right = rights & Rights::from_bits(1 << i).unwrap();
41            if !cur_right.is_empty() {
42                right_set.insert(cur_right.to_str().unwrap_or("INVALID RIGHT"));
43            }
44        }
45        println!("{right_set:#?}");
46    }
47}
48
49pub mod directory {
50    use crate::wasi;
51    use std::mem;
52
53    pub const __WASI_DIRCOOKIE_START: wasi::Dircookie = 0;
54
55    pub fn dirent_to_le_bytes(ent: &wasi::Dirent) -> Vec<u8> {
56        let out: Vec<u8> = std::iter::empty()
57            .chain(ent.d_next.to_le_bytes())
58            .chain(ent.d_ino.to_le_bytes())
59            .chain(ent.d_namlen.to_le_bytes())
60            .chain(u32::from(ent.d_type as u8).to_le_bytes())
61            .collect();
62
63        assert_eq!(out.len(), mem::size_of::<wasi::Dirent>());
64        out
65    }
66
67    #[cfg(test)]
68    mod tests {
69        use super::dirent_to_le_bytes;
70        use crate::wasi;
71
72        #[test]
73        fn test_dirent_to_le_bytes() {
74            let s = wasi::Dirent {
75                d_next: 0x0123456789abcdef,
76                d_ino: 0xfedcba9876543210,
77                d_namlen: 0xaabbccdd,
78                d_type: wasi::Filetype::Directory,
79            };
80
81            assert_eq!(
82                vec![
83                    // d_next
84                    0xef,
85                    0xcd,
86                    0xab,
87                    0x89,
88                    0x67,
89                    0x45,
90                    0x23,
91                    0x01,
92                    //
93                    // d_ino
94                    0x10,
95                    0x32,
96                    0x54,
97                    0x76,
98                    0x98,
99                    0xba,
100                    0xdc,
101                    0xfe,
102                    //
103                    // d_namelen
104                    0xdd,
105                    0xcc,
106                    0xbb,
107                    0xaa,
108                    //
109                    // d_type
110                    // plus padding
111                    wasi::Filetype::Directory as u8,
112                    0x00,
113                    0x00,
114                    0x00,
115                ],
116                dirent_to_le_bytes(&s)
117            );
118        }
119    }
120}
121
122pub mod io {
123    use wasmer_derive::ValueType;
124    use wasmer_types::MemorySize;
125
126    pub use crate::wasi::Bool;
127    pub use crate::wasi::Count;
128    pub use crate::wasi::OptionTag;
129    pub use crate::wasi::StdioMode;
130
131    #[derive(Debug, Copy, Clone, PartialEq, Eq, ValueType)]
132    #[repr(C)]
133    pub struct __wasi_ciovec_t<M: MemorySize> {
134        pub buf: M::Offset,
135        pub buf_len: M::Offset,
136    }
137
138    #[derive(Debug, Copy, Clone, PartialEq, Eq, ValueType)]
139    #[repr(C)]
140    pub struct __wasi_iovec_t<M: MemorySize> {
141        pub buf: M::Offset,
142        pub buf_len: M::Offset,
143    }
144}
145
146pub mod time {
147    pub use crate::wasi::OptionTimestamp;
148}
149
150pub mod net {
151    use crate::wasi::Addressfamily;
152    use wasmer_derive::ValueType;
153
154    use crate::wasi::OptionTimestamp;
155
156    pub use crate::wasi::{
157        AddrUnspec, AddrUnspecPort, CidrUnspec, HttpHandles, HttpStatus, RiFlags, RoFlags, SdFlags,
158        SiFlags, SockProto, Timeout,
159    };
160
161    #[derive(Debug, Copy, Clone, PartialEq, Eq, ValueType)]
162    #[repr(C)]
163    pub struct __wasi_hardwareaddress_t {
164        pub octs: [u8; 6],
165    }
166
167    #[derive(Debug, Copy, Clone, PartialEq, Eq, ValueType)]
168    #[repr(C)]
169    pub struct __wasi_addr_ip4_t {
170        pub octs: [u8; 4],
171    }
172
173    #[derive(Debug, Copy, Clone, PartialEq, Eq, ValueType)]
174    #[repr(C)]
175    pub struct __wasi_addr_ip4_port_t {
176        pub port: u16,
177        pub ip: __wasi_addr_ip4_t,
178    }
179
180    #[derive(Debug, Copy, Clone, PartialEq, Eq, ValueType)]
181    #[repr(C)]
182    pub struct __wasi_cidr_ip4_t {
183        pub ip: __wasi_addr_ip4_t,
184        pub prefix: u8,
185    }
186
187    #[derive(Debug, Copy, Clone, PartialEq, Eq, ValueType)]
188    #[repr(C)]
189    pub struct __wasi_addr_unix_t {
190        pub octs: [u8; 16],
191    }
192
193    #[derive(Debug, Copy, Clone, PartialEq, Eq, ValueType)]
194    #[repr(C)]
195    pub struct __wasi_addr_unix_port_t {
196        pub port: u16,
197        pub unix: __wasi_addr_unix_t,
198    }
199
200    #[derive(Debug, Copy, Clone, PartialEq, Eq, ValueType)]
201    #[repr(C)]
202    pub struct __wasi_cidr_unix_t {
203        pub unix: __wasi_addr_unix_t,
204        pub prefix: u8,
205    }
206
207    #[derive(Debug, Copy, Clone, PartialEq, Eq, ValueType)]
208    #[repr(C)]
209    pub struct __wasi_addr_ip6_t {
210        pub segs: [u8; 16],
211    }
212
213    #[derive(Debug, Copy, Clone, PartialEq, Eq, ValueType)]
214    #[repr(C)]
215    pub struct __wasi_addr_ip6_port_t {
216        pub port: u16,
217        pub ip: __wasi_addr_ip6_t,
218    }
219
220    #[derive(Debug, Copy, Clone, PartialEq, Eq, ValueType)]
221    #[repr(C)]
222    pub struct __wasi_cidr_ip6_t {
223        pub ip: __wasi_addr_ip6_t,
224        pub prefix: u8,
225    }
226
227    #[derive(Debug, Copy, Clone, ValueType)]
228    #[repr(C)]
229    pub struct __wasi_addr_u {
230        pub octs: [u8; 16],
231    }
232
233    #[derive(Debug, Copy, Clone, ValueType)]
234    #[repr(C)]
235    pub struct __wasi_addr_t {
236        pub tag: Addressfamily,
237        // C will add a padding byte here which must be set to zero otherwise the tag will corrupt
238        pub _padding: u8,
239        pub u: __wasi_addr_u,
240    }
241
242    #[derive(Debug, Copy, Clone, ValueType)]
243    #[repr(C)]
244    pub struct __wasi_addr_port_u {
245        pub octs: [u8; 18],
246    }
247
248    #[derive(Debug, Copy, Clone, ValueType)]
249    #[repr(C)]
250    pub struct __wasi_addr_port_t {
251        pub tag: Addressfamily,
252        // C will add a padding byte here which must be set to zero otherwise the tag will corrupt
253        pub _padding: u8,
254        pub u: __wasi_addr_port_u,
255    }
256
257    #[derive(Debug, Copy, Clone, ValueType)]
258    #[repr(C)]
259    pub struct __wasi_cidr_u {
260        pub octs: [u8; 17],
261    }
262
263    #[derive(Debug, Copy, Clone, ValueType)]
264    #[repr(C)]
265    pub struct __wasi_cidr_t {
266        pub tag: Addressfamily,
267        // C will add a padding byte here which must be set to zero otherwise the tag will corrupt
268        pub _padding: u8,
269        pub u: __wasi_cidr_u,
270    }
271
272    #[derive(Debug, Copy, Clone, ValueType)]
273    #[repr(C)]
274    pub struct Route {
275        pub cidr: __wasi_cidr_t,
276        pub via_router: __wasi_addr_t,
277        pub preferred_until: OptionTimestamp,
278        pub expires_at: OptionTimestamp,
279    }
280
281    pub const __WASI_SOCK_RECV_INPUT_PEEK: RiFlags = 1 << 0;
282    pub const __WASI_SOCK_RECV_INPUT_WAITALL: RiFlags = 1 << 1;
283    pub const __WASI_SOCK_RECV_INPUT_DATA_TRUNCATED: RiFlags = 1 << 2;
284    pub const __WASI_SOCK_RECV_INPUT_DONT_WAIT: RiFlags = 1 << 3;
285
286    pub const __WASI_SOCK_SEND_INPUT_DONT_WAIT: SiFlags = 1 << 0;
287
288    pub const __WASI_SOCK_RECV_OUTPUT_DATA_TRUNCATED: RoFlags = 1 << 0;
289
290    pub const __WASI_SHUT_RD: SdFlags = 1 << 0;
291    pub const __WASI_SHUT_WR: SdFlags = 1 << 1;
292}
293
294pub mod signal {
295    pub use crate::wasi::Signal;
296}
297
298pub mod subscription {
299    pub use crate::wasi::{Eventtype, SubscriptionFsReadwrite};
300}