1 use core::ops::{Deref, DerefMut};
2 use core::{mem, slice};
3 use crate::flag::{EventFlags, MapFlags, PtraceFlags, SigActionFlags};
4 
5 #[derive(Copy, Clone, Debug, Default)]
6 #[repr(C)]
7 pub struct Event {
8     pub id: usize,
9     pub flags: EventFlags,
10     pub data: usize
11 }
12 
13 impl Deref for Event {
14     type Target = [u8];
deref(&self) -> &[u8]15     fn deref(&self) -> &[u8] {
16         unsafe {
17             slice::from_raw_parts(self as *const Event as *const u8, mem::size_of::<Event>())
18         }
19     }
20 }
21 
22 impl DerefMut for Event {
deref_mut(&mut self) -> &mut [u8]23     fn deref_mut(&mut self) -> &mut [u8] {
24         unsafe {
25             slice::from_raw_parts_mut(self as *mut Event as *mut u8, mem::size_of::<Event>())
26         }
27     }
28 }
29 
30 #[derive(Copy, Clone, Debug, Default)]
31 #[repr(C)]
32 pub struct ITimerSpec {
33     pub it_interval: TimeSpec,
34     pub it_value: TimeSpec,
35 }
36 
37 impl Deref for ITimerSpec {
38     type Target = [u8];
deref(&self) -> &[u8]39     fn deref(&self) -> &[u8] {
40         unsafe {
41             slice::from_raw_parts(self as *const ITimerSpec as *const u8,
42                                   mem::size_of::<ITimerSpec>())
43         }
44     }
45 }
46 
47 impl DerefMut for ITimerSpec {
deref_mut(&mut self) -> &mut [u8]48     fn deref_mut(&mut self) -> &mut [u8] {
49         unsafe {
50             slice::from_raw_parts_mut(self as *mut ITimerSpec as *mut u8,
51                                       mem::size_of::<ITimerSpec>())
52         }
53     }
54 }
55 
56 #[derive(Copy, Clone, Debug, Default)]
57 #[repr(C)]
58 pub struct OldMap {
59     pub offset: usize,
60     pub size: usize,
61     pub flags: MapFlags,
62 }
63 
64 impl Deref for OldMap {
65     type Target = [u8];
deref(&self) -> &[u8]66     fn deref(&self) -> &[u8] {
67         unsafe {
68             slice::from_raw_parts(self as *const OldMap as *const u8, mem::size_of::<OldMap>())
69         }
70     }
71 }
72 
73 impl DerefMut for OldMap {
deref_mut(&mut self) -> &mut [u8]74     fn deref_mut(&mut self) -> &mut [u8] {
75         unsafe {
76             slice::from_raw_parts_mut(self as *mut OldMap as *mut u8, mem::size_of::<OldMap>())
77         }
78     }
79 }
80 #[derive(Copy, Clone, Debug, Default)]
81 #[repr(C)]
82 pub struct Map {
83     /// The offset inside the file that is being mapped.
84     pub offset: usize,
85 
86     /// The size of the memory map.
87     pub size: usize,
88 
89     /// Contains both prot and map flags.
90     pub flags: MapFlags,
91 
92     /// Functions as a hint to where in the virtual address space of the running process, to place
93     /// the memory map. If [`MapFlags::MAP_FIXED`] is set, then this address must be the address to
94     /// map to.
95     pub address: usize,
96 }
97 
98 impl Deref for Map {
99     type Target = [u8];
deref(&self) -> &[u8]100     fn deref(&self) -> &[u8] {
101         unsafe {
102             slice::from_raw_parts(self as *const Map as *const u8, mem::size_of::<Map>())
103         }
104     }
105 }
106 
107 impl DerefMut for Map {
deref_mut(&mut self) -> &mut [u8]108     fn deref_mut(&mut self) -> &mut [u8] {
109         unsafe {
110             slice::from_raw_parts_mut(self as *mut Map as *mut u8, mem::size_of::<Map>())
111         }
112     }
113 }
114 
115 #[derive(Copy, Clone, Debug, Default)]
116 #[repr(C)]
117 pub struct Packet {
118     pub id: u64,
119     pub pid: usize,
120     pub uid: u32,
121     pub gid: u32,
122     pub a: usize,
123     pub b: usize,
124     pub c: usize,
125     pub d: usize
126 }
127 
128 impl Deref for Packet {
129     type Target = [u8];
deref(&self) -> &[u8]130     fn deref(&self) -> &[u8] {
131         unsafe {
132             slice::from_raw_parts(self as *const Packet as *const u8, mem::size_of::<Packet>())
133         }
134     }
135 }
136 
137 impl DerefMut for Packet {
deref_mut(&mut self) -> &mut [u8]138     fn deref_mut(&mut self) -> &mut [u8] {
139         unsafe {
140             slice::from_raw_parts_mut(self as *mut Packet as *mut u8, mem::size_of::<Packet>())
141         }
142     }
143 }
144 
145 #[derive(Copy, Clone, Debug, Default, PartialEq)]
146 #[repr(C)]
147 pub struct SigAction {
148     pub sa_handler: Option<extern "C" fn(usize)>,
149     pub sa_mask: [u64; 2],
150     pub sa_flags: SigActionFlags,
151 }
152 
153 #[allow(dead_code)]
_assert_size_of_function_is_sane()154 unsafe fn _assert_size_of_function_is_sane() {
155     // Transmuting will complain *at compile time* if sizes differ.
156     // Rust forbids a fn-pointer from being 0 so to allow SIG_DFL to
157     // exist, we use Option<extern "C" fn(usize)> which will mean 0
158     // becomes None
159     let _ = mem::transmute::<Option<extern "C" fn(usize)>, usize>(None);
160 }
161 
162 #[derive(Copy, Clone, Debug, Default, PartialEq)]
163 #[repr(C)]
164 pub struct Stat {
165     pub st_dev: u64,
166     pub st_ino: u64,
167     pub st_mode: u16,
168     pub st_nlink: u32,
169     pub st_uid: u32,
170     pub st_gid: u32,
171     pub st_size: u64,
172     pub st_blksize: u32,
173     pub st_blocks: u64,
174     pub st_mtime: u64,
175     pub st_mtime_nsec: u32,
176     pub st_atime: u64,
177     pub st_atime_nsec: u32,
178     pub st_ctime: u64,
179     pub st_ctime_nsec: u32,
180 }
181 
182 impl Deref for Stat {
183     type Target = [u8];
deref(&self) -> &[u8]184     fn deref(&self) -> &[u8] {
185         unsafe {
186             slice::from_raw_parts(self as *const Stat as *const u8,
187                                   mem::size_of::<Stat>())
188         }
189     }
190 }
191 
192 impl DerefMut for Stat {
deref_mut(&mut self) -> &mut [u8]193     fn deref_mut(&mut self) -> &mut [u8] {
194         unsafe {
195             slice::from_raw_parts_mut(self as *mut Stat as *mut u8,
196                                       mem::size_of::<Stat>())
197         }
198     }
199 }
200 
201 #[derive(Copy, Clone, Debug, Default, PartialEq)]
202 #[repr(C)]
203 pub struct StatVfs {
204     pub f_bsize: u32,
205     pub f_blocks: u64,
206     pub f_bfree: u64,
207     pub f_bavail: u64,
208 }
209 
210 impl Deref for StatVfs {
211     type Target = [u8];
deref(&self) -> &[u8]212     fn deref(&self) -> &[u8] {
213         unsafe {
214             slice::from_raw_parts(self as *const StatVfs as *const u8,
215                                   mem::size_of::<StatVfs>())
216         }
217     }
218 }
219 
220 impl DerefMut for StatVfs {
deref_mut(&mut self) -> &mut [u8]221     fn deref_mut(&mut self) -> &mut [u8] {
222         unsafe {
223             slice::from_raw_parts_mut(self as *mut StatVfs as *mut u8,
224                                       mem::size_of::<StatVfs>())
225         }
226     }
227 }
228 
229 #[derive(Copy, Clone, Debug, Default, PartialEq)]
230 #[repr(C)]
231 pub struct TimeSpec {
232     pub tv_sec: i64,
233     pub tv_nsec: i32,
234 }
235 
236 impl Deref for TimeSpec {
237     type Target = [u8];
deref(&self) -> &[u8]238     fn deref(&self) -> &[u8] {
239         unsafe {
240             slice::from_raw_parts(self as *const TimeSpec as *const u8,
241                                   mem::size_of::<TimeSpec>())
242         }
243     }
244 }
245 
246 impl DerefMut for TimeSpec {
deref_mut(&mut self) -> &mut [u8]247     fn deref_mut(&mut self) -> &mut [u8] {
248         unsafe {
249             slice::from_raw_parts_mut(self as *mut TimeSpec as *mut u8,
250                                       mem::size_of::<TimeSpec>())
251         }
252     }
253 }
254 
255 #[derive(Clone, Copy, Debug, Default)]
256 #[repr(C)]
257 pub struct PtraceEvent {
258     pub cause: PtraceFlags,
259     pub a: usize,
260     pub b: usize,
261     pub c: usize,
262     pub d: usize,
263     pub e: usize,
264     pub f: usize
265 }
266 
267 impl Deref for PtraceEvent {
268     type Target = [u8];
deref(&self) -> &[u8]269     fn deref(&self) -> &[u8] {
270         unsafe {
271             slice::from_raw_parts(self as *const PtraceEvent as *const u8, mem::size_of::<PtraceEvent>())
272         }
273     }
274 }
275 
276 impl DerefMut for PtraceEvent {
deref_mut(&mut self) -> &mut [u8]277     fn deref_mut(&mut self) -> &mut [u8] {
278         unsafe {
279             slice::from_raw_parts_mut(self as *mut PtraceEvent as *mut u8, mem::size_of::<PtraceEvent>())
280         }
281     }
282 }
283 
284 #[macro_export]
285 macro_rules! ptrace_event {
286     ($cause:expr $(, $a:expr $(, $b:expr $(, $c:expr)?)?)?) => {
287         $crate::data::PtraceEvent {
288             cause: $cause,
289             $(a: $a,
290               $(b: $b,
291                 $(c: $c,)?
292               )?
293             )?
294             ..Default::default()
295         }
296     }
297 }
298