1 | // Copyright (c) 2017 Gilad Naaman |
2 | // |
3 | // Permission is hereby granted, free of charge, to any person obtaining a copy |
4 | // of this software and associated documentation files (the "Software"), to deal |
5 | // in the Software without restriction, including without limitation the rights |
6 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
7 | // copies of the Software, and to permit persons to whom the Software is |
8 | // furnished to do so, subject to the following conditions: |
9 | // |
10 | // The above copyright notice and this permission notice shall be included in all |
11 | // copies or substantial portions of the Software. |
12 | // |
13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
14 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
15 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
16 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
19 | // SOFTWARE. |
20 | |
21 | /// Macro to create a local `base_ptr` raw pointer of the given type, avoiding UB as |
22 | /// much as is possible currently. |
23 | #[cfg (maybe_uninit)] |
24 | #[macro_export ] |
25 | #[doc (hidden)] |
26 | macro_rules! _memoffset__let_base_ptr { |
27 | ($name:ident, $type:ty) => { |
28 | // No UB here, and the pointer does not dangle, either. |
29 | // But we have to make sure that `uninit` lives long enough, |
30 | // so it has to be in the same scope as `$name`. That's why |
31 | // `let_base_ptr` declares a variable (several, actually) |
32 | // instead of returning one. |
33 | let uninit = $crate::__priv::mem::MaybeUninit::<$type>::uninit(); |
34 | let $name: *const $type = uninit.as_ptr(); |
35 | }; |
36 | } |
37 | #[cfg (not(maybe_uninit))] |
38 | #[macro_export ] |
39 | #[doc (hidden)] |
40 | macro_rules! _memoffset__let_base_ptr { |
41 | ($name:ident, $type:ty) => { |
42 | // No UB right here, but we will later dereference this pointer to |
43 | // offset into a field, and that is UB because the pointer is dangling. |
44 | let $name = $crate::__priv::mem::align_of::<$type>() as *const $type; |
45 | }; |
46 | } |
47 | |
48 | /// Macro to compute the distance between two pointers. |
49 | #[cfg (feature = "unstable_const" )] |
50 | #[macro_export ] |
51 | #[doc (hidden)] |
52 | macro_rules! _memoffset_offset_from_unsafe { |
53 | ($field:expr, $base:expr) => {{ |
54 | let field = $field; // evaluate $field outside the `unsafe` block |
55 | let base = $base; // evaluate $base outside the `unsafe` block |
56 | // Compute offset, with unstable `offset_from` for const-compatibility. |
57 | // (Requires the pointers to not dangle, but we already need that for `raw_field!` anyway.) |
58 | unsafe { (field as *const u8).offset_from(base as *const u8) as usize } |
59 | }}; |
60 | } |
61 | #[cfg (not(feature = "unstable_const" ))] |
62 | #[macro_export ] |
63 | #[doc (hidden)] |
64 | macro_rules! _memoffset_offset_from_unsafe { |
65 | ($field:expr, $base:expr) => { |
66 | // Compute offset. |
67 | ($field as usize) - ($base as usize) |
68 | }; |
69 | } |
70 | |
71 | /// Calculates the offset of the specified field from the start of the named struct. |
72 | /// |
73 | /// ## Examples |
74 | /// ``` |
75 | /// use memoffset::offset_of; |
76 | /// |
77 | /// #[repr(C, packed)] |
78 | /// struct Foo { |
79 | /// a: u32, |
80 | /// b: u64, |
81 | /// c: [u8; 5] |
82 | /// } |
83 | /// |
84 | /// fn main() { |
85 | /// assert_eq!(offset_of!(Foo, a), 0); |
86 | /// assert_eq!(offset_of!(Foo, b), 4); |
87 | /// } |
88 | /// ``` |
89 | /// |
90 | /// ## Notes |
91 | /// Rust's ABI is unstable, and [type layout can be changed with each |
92 | /// compilation](https://doc.rust-lang.org/reference/type-layout.html). |
93 | /// |
94 | /// Using `offset_of!` with a `repr(Rust)` struct will return the correct offset of the |
95 | /// specified `field` for a particular compilation, but the exact value may change |
96 | /// based on the compiler version, concrete struct type, time of day, or rustc's mood. |
97 | /// |
98 | /// As a result, the value should not be retained and used between different compilations. |
99 | #[macro_export (local_inner_macros)] |
100 | macro_rules! offset_of { |
101 | ($parent:path, $field:tt) => {{ |
102 | // Get a base pointer (non-dangling if rustc supports `MaybeUninit`). |
103 | _memoffset__let_base_ptr!(base_ptr, $parent); |
104 | // Get field pointer. |
105 | let field_ptr = raw_field!(base_ptr, $parent, $field); |
106 | // Compute offset. |
107 | _memoffset_offset_from_unsafe!(field_ptr, base_ptr) |
108 | }}; |
109 | } |
110 | |
111 | /// Calculates the offset of the specified field from the start of the tuple. |
112 | /// |
113 | /// ## Examples |
114 | /// ``` |
115 | /// use memoffset::offset_of_tuple; |
116 | /// |
117 | /// fn main() { |
118 | /// assert!(offset_of_tuple!((u8, u32), 1) >= 0, "Tuples do not have a defined layout" ); |
119 | /// } |
120 | /// ``` |
121 | #[cfg (tuple_ty)] |
122 | #[macro_export (local_inner_macros)] |
123 | macro_rules! offset_of_tuple { |
124 | ($parent:ty, $field:tt) => {{ |
125 | // Get a base pointer (non-dangling if rustc supports `MaybeUninit`). |
126 | _memoffset__let_base_ptr!(base_ptr, $parent); |
127 | // Get field pointer. |
128 | let field_ptr = raw_field_tuple!(base_ptr, $parent, $field); |
129 | // Compute offset. |
130 | _memoffset_offset_from_unsafe!(field_ptr, base_ptr) |
131 | }}; |
132 | } |
133 | |
134 | /// Calculates the offset of the specified union member from the start of the union. |
135 | /// |
136 | /// ## Examples |
137 | /// ``` |
138 | /// use memoffset::offset_of_union; |
139 | /// |
140 | /// #[repr(C, packed)] |
141 | /// union Foo { |
142 | /// foo32: i32, |
143 | /// foo64: i64, |
144 | /// } |
145 | /// |
146 | /// fn main() { |
147 | /// assert!(offset_of_union!(Foo, foo64) == 0); |
148 | /// } |
149 | /// ``` |
150 | /// |
151 | /// ## Note |
152 | /// Due to macro_rules limitations, this macro will accept structs with a single field as well as unions. |
153 | /// This is not a stable guarantee, and future versions of this crate might fail |
154 | /// on any use of this macro with a struct, without a semver bump. |
155 | #[macro_export (local_inner_macros)] |
156 | macro_rules! offset_of_union { |
157 | ($parent:path, $field:tt) => {{ |
158 | // Get a base pointer (non-dangling if rustc supports `MaybeUninit`). |
159 | _memoffset__let_base_ptr!(base_ptr, $parent); |
160 | // Get field pointer. |
161 | let field_ptr = raw_field_union!(base_ptr, $parent, $field); |
162 | // Compute offset. |
163 | _memoffset_offset_from_unsafe!(field_ptr, base_ptr) |
164 | }}; |
165 | } |
166 | |
167 | #[cfg (test)] |
168 | mod tests { |
169 | #[test ] |
170 | fn offset_simple() { |
171 | #[repr (C)] |
172 | struct Foo { |
173 | a: u32, |
174 | b: [u8; 2], |
175 | c: i64, |
176 | } |
177 | |
178 | assert_eq!(offset_of!(Foo, a), 0); |
179 | assert_eq!(offset_of!(Foo, b), 4); |
180 | assert_eq!(offset_of!(Foo, c), 8); |
181 | } |
182 | |
183 | #[test ] |
184 | #[cfg_attr (miri, ignore)] // this creates unaligned references |
185 | fn offset_simple_packed() { |
186 | #[repr (C, packed)] |
187 | struct Foo { |
188 | a: u32, |
189 | b: [u8; 2], |
190 | c: i64, |
191 | } |
192 | |
193 | assert_eq!(offset_of!(Foo, a), 0); |
194 | assert_eq!(offset_of!(Foo, b), 4); |
195 | assert_eq!(offset_of!(Foo, c), 6); |
196 | } |
197 | |
198 | #[test ] |
199 | fn tuple_struct() { |
200 | #[repr (C)] |
201 | struct Tup(i32, i32); |
202 | |
203 | assert_eq!(offset_of!(Tup, 0), 0); |
204 | assert_eq!(offset_of!(Tup, 1), 4); |
205 | } |
206 | |
207 | #[test ] |
208 | fn offset_union() { |
209 | // Since we're specifying repr(C), all fields are supposed to be at offset 0 |
210 | #[repr (C)] |
211 | union Foo { |
212 | a: u32, |
213 | b: [u8; 2], |
214 | c: i64, |
215 | } |
216 | |
217 | assert_eq!(offset_of_union!(Foo, a), 0); |
218 | assert_eq!(offset_of_union!(Foo, b), 0); |
219 | assert_eq!(offset_of_union!(Foo, c), 0); |
220 | } |
221 | |
222 | #[test ] |
223 | fn path() { |
224 | mod sub { |
225 | #[repr (C)] |
226 | pub struct Foo { |
227 | pub x: u32, |
228 | } |
229 | } |
230 | |
231 | assert_eq!(offset_of!(sub::Foo, x), 0); |
232 | } |
233 | |
234 | #[test ] |
235 | fn inside_generic_method() { |
236 | struct Pair<T, U>(T, U); |
237 | |
238 | fn foo<T, U>(_: Pair<T, U>) -> usize { |
239 | offset_of!(Pair<T, U>, 1) |
240 | } |
241 | |
242 | assert_eq!(foo(Pair(0, 0)), 4); |
243 | } |
244 | |
245 | #[cfg (tuple_ty)] |
246 | #[test ] |
247 | fn test_tuple_offset() { |
248 | let f = (0i32, 0.0f32, 0u8); |
249 | let f_ptr = &f as *const _; |
250 | let f1_ptr = &f.1 as *const _; |
251 | |
252 | assert_eq!( |
253 | f1_ptr as usize - f_ptr as usize, |
254 | offset_of_tuple!((i32, f32, u8), 1) |
255 | ); |
256 | } |
257 | |
258 | #[test ] |
259 | fn test_raw_field() { |
260 | #[repr (C)] |
261 | struct Foo { |
262 | a: u32, |
263 | b: [u8; 2], |
264 | c: i64, |
265 | } |
266 | |
267 | let f: Foo = Foo { |
268 | a: 0, |
269 | b: [0, 0], |
270 | c: 0, |
271 | }; |
272 | let f_ptr = &f as *const _; |
273 | assert_eq!(f_ptr as usize + 0, raw_field!(f_ptr, Foo, a) as usize); |
274 | assert_eq!(f_ptr as usize + 4, raw_field!(f_ptr, Foo, b) as usize); |
275 | assert_eq!(f_ptr as usize + 8, raw_field!(f_ptr, Foo, c) as usize); |
276 | } |
277 | |
278 | #[cfg (tuple_ty)] |
279 | #[test ] |
280 | fn test_raw_field_tuple() { |
281 | let t = (0u32, 0u8, false); |
282 | let t_ptr = &t as *const _; |
283 | let t_addr = t_ptr as usize; |
284 | |
285 | assert_eq!( |
286 | &t.0 as *const _ as usize - t_addr, |
287 | raw_field_tuple!(t_ptr, (u32, u8, bool), 0) as usize - t_addr |
288 | ); |
289 | assert_eq!( |
290 | &t.1 as *const _ as usize - t_addr, |
291 | raw_field_tuple!(t_ptr, (u32, u8, bool), 1) as usize - t_addr |
292 | ); |
293 | assert_eq!( |
294 | &t.2 as *const _ as usize - t_addr, |
295 | raw_field_tuple!(t_ptr, (u32, u8, bool), 2) as usize - t_addr |
296 | ); |
297 | } |
298 | |
299 | #[test ] |
300 | fn test_raw_field_union() { |
301 | #[repr (C)] |
302 | union Foo { |
303 | a: u32, |
304 | b: [u8; 2], |
305 | c: i64, |
306 | } |
307 | |
308 | let f = Foo { a: 0 }; |
309 | let f_ptr = &f as *const _; |
310 | assert_eq!(f_ptr as usize + 0, raw_field_union!(f_ptr, Foo, a) as usize); |
311 | assert_eq!(f_ptr as usize + 0, raw_field_union!(f_ptr, Foo, b) as usize); |
312 | assert_eq!(f_ptr as usize + 0, raw_field_union!(f_ptr, Foo, c) as usize); |
313 | } |
314 | |
315 | #[cfg (feature = "unstable_const" )] |
316 | #[test ] |
317 | fn const_offset() { |
318 | #[repr (C)] |
319 | struct Foo { |
320 | a: u32, |
321 | b: [u8; 2], |
322 | c: i64, |
323 | } |
324 | |
325 | assert_eq!([0; offset_of!(Foo, b)].len(), 4); |
326 | } |
327 | |
328 | #[cfg (feature = "unstable_const" )] |
329 | #[test ] |
330 | fn const_offset_interior_mutable() { |
331 | #[repr (C)] |
332 | struct Foo { |
333 | a: u32, |
334 | b: core::cell::Cell<u32>, |
335 | } |
336 | |
337 | assert_eq!([0; offset_of!(Foo, b)].len(), 4); |
338 | } |
339 | |
340 | #[cfg (feature = "unstable_const" )] |
341 | #[test ] |
342 | fn const_fn_offset() { |
343 | const fn test_fn() -> usize { |
344 | #[repr (C)] |
345 | struct Foo { |
346 | a: u32, |
347 | b: [u8; 2], |
348 | c: i64, |
349 | } |
350 | |
351 | offset_of!(Foo, b) |
352 | } |
353 | |
354 | assert_eq!([0; test_fn()].len(), 4); |
355 | } |
356 | } |
357 | |