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