Arm offset macro fix
Fixed offset macro used in Arms code. Previous implementation
dereferenced a null pointer which is newly an error with the Rust
update.
@@ -80,17 +80,15 @@ const NR_FP_VREGS: usize = 32;
// The core register are represented by the user_pt_regs structure. Look for it in
// arch/arm64/include/uapi/asm/ptrace.h.
-// This macro gets the offset of a structure (i.e `str`) member (i.e `field`) without having
-// an instance of that structure.
-// It uses a null pointer to retrieve the offset to the field.
-// Inspired by C solution: `#define offsetof(str, f) ((size_t)(&((str *)0)->f))`.
-// Doing `offset__of!(user_pt_regs, pstate)` in our rust code will trigger the following:
-// unsafe { &(*(0 as *const user_pt_regs)).pstate as *const _ as usize }
-// The dereference expression produces an lvalue, but that lvalue is not actually read from,
-// we're just doing pointer math on it, so in theory, it should be safe.
+// Gets offset of a member (`field`) within a struct (`container`).
+// Same as bindgen offset tests.
macro_rules! offset__of {
- ($str:ty, $field:ident) => {
- unsafe { &(*(std::ptr::null::<$str>())).$field as *const _ as usize }
+ ($container:ty, $field:ident) => {
+ unsafe {
+ let uninit = std::mem::MaybeUninit::<$container>::uninit();
+ let ptr = uninit.as_ptr();
+ std::ptr::addr_of!((*ptr).$field) as usize - ptr as usize
+ }
};
}
