From 6936304bc7fd4111da4905d04483b07b8524bb71 Mon Sep 17 00:00:00 2001 From: Karol Herbst Date: Fri, 10 Feb 2023 17:01:37 +0100 Subject: [PATCH] rusticl/icd: Make it work in case Rustc shuffles struct around Nothing guarentees fields are in order or anything like that. So do proper offset math Signed-off-by: Karol Herbst Part-of: --- src/gallium/frontends/rusticl/api/icd.rs | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/gallium/frontends/rusticl/api/icd.rs b/src/gallium/frontends/rusticl/api/icd.rs index 575a82797ef..f493748ca85 100644 --- a/src/gallium/frontends/rusticl/api/icd.rs +++ b/src/gallium/frontends/rusticl/api/icd.rs @@ -290,26 +290,25 @@ pub trait ReferenceCountedAPIPointer { #[macro_export] macro_rules! impl_cl_type_trait { - ($cl: ident, $t: ty, $err: ident) => { + ($cl: ident, $t: path, $err: ident) => { impl $crate::api::icd::ReferenceCountedAPIPointer<$t, $err> for $cl { fn get_ptr(&self) -> CLResult<*const $t> { type Base = $crate::api::icd::CLObjectBase<$err>; Base::check_ptr(self.cast())?; - // Now that we've verified the object, it should be safe to - // dereference it. As one more double check, make sure that - // the CLObjectBase is at the start of the object - let obj_ptr: *const $t = self.cast(); - unsafe { - let base_ptr = ::std::ptr::addr_of!((*obj_ptr).base); - assert!((obj_ptr as usize) == (base_ptr as usize)); - } - - Ok(obj_ptr) + let offset = ::mesa_rust_util::offset_of!($t, base); + let mut obj_ptr: *const u8 = self.cast(); + // SAFETY: We offset the pointer back from the ICD specified base type to our + // internal type. + unsafe { obj_ptr = obj_ptr.sub(offset) } + Ok(obj_ptr.cast()) } fn from_ptr(ptr: *const $t) -> Self { - ptr as Self + let offset = ::mesa_rust_util::offset_of!($t, base); + // SAFETY: The resulting pointer is safe as we simply offset into the ICD specified + // base type. + unsafe { (ptr as *const u8).add(offset) as Self } } }