Skip to content

Commit 12938fc

Browse files
authored
Make Inst use f32 on 32-bit platforms + fix miri warning (#1997)
* fix miri warning about int-to-ptr cast * use f32 for Inst on 32-bit platforms * add inline annotations to Inst methods
1 parent 461de46 commit 12938fc

1 file changed

Lines changed: 24 additions & 8 deletions

File tree

  • crates/wasmi/src/engine/executor/handler

crates/wasmi/src/engine/executor/handler/state.rs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -151,22 +151,35 @@ pub struct Inst {
151151
///
152152
/// # Note
153153
///
154-
/// - We use a `f64` to represent [`Inst`] to avoid using a
154+
/// - We use a float to represent [`Inst`] to avoid using a
155155
/// general purpose (integer) register as they are not as
156156
/// available as floating point registers on most platforms.
157157
/// - Since [`Inst`] is only accessed by operators that are
158158
/// considered "slow" anyways an additional conversion between
159159
/// integer and float won't be a terrible trade-off.
160-
value: f64,
161-
/// Indicates to the compiler that this type is similar in behavior as
162-
/// a non-owning, non-lifetime restricted `*const InstanceEntity` type.
160+
value: InstRepr,
161+
/// Marks `Inst` as logically containing a shared raw pointer.
163162
marker: PhantomData<*const InstanceEntity>,
164163
}
165164

165+
/// The underlying float representation of `Inst` for 64-bit platforms.
166+
#[cfg(target_pointer_width = "64")]
167+
type InstRepr = f64;
168+
169+
/// The underlying float representation of `Inst` for 32-bit platforms.
170+
#[cfg(target_pointer_width = "32")]
171+
type InstRepr = f32;
172+
173+
const _: () = {
174+
use core::mem::size_of;
175+
assert!(size_of::<InstRepr>() == size_of::<usize>());
176+
};
177+
166178
impl From<&'_ InstanceEntity> for Inst {
179+
#[inline]
167180
fn from(entity: &'_ InstanceEntity) -> Self {
168-
let value =
169-
f64::from_ne_bytes(((entity as *const InstanceEntity as usize) as u64).to_ne_bytes());
181+
let addr = (entity as *const InstanceEntity).expose_provenance();
182+
let value = InstRepr::from_ne_bytes((addr).to_ne_bytes());
170183
Self {
171184
value,
172185
marker: PhantomData,
@@ -175,6 +188,7 @@ impl From<&'_ InstanceEntity> for Inst {
175188
}
176189

177190
impl PartialEq for Inst {
191+
#[inline]
178192
fn eq(&self, other: &Self) -> bool {
179193
ptr::addr_eq(self.as_ptr(), other.as_ptr())
180194
}
@@ -183,9 +197,10 @@ impl Eq for Inst {}
183197

184198
impl Inst {
185199
/// Converts the underlying representation back into its original pointer value.
200+
#[inline]
186201
fn as_ptr(&self) -> *const InstanceEntity {
187-
let bits = u64::from_ne_bytes(self.value.to_ne_bytes());
188-
bits as usize as *const InstanceEntity
202+
let bits = usize::from_ne_bytes(self.value.to_ne_bytes());
203+
ptr::with_exposed_provenance::<InstanceEntity>(bits)
189204
}
190205

191206
/// Returns a shared reference to the referenced [`InstanceEntity`].
@@ -199,6 +214,7 @@ impl Inst {
199214
/// - The referenced [`InstanceEntity`] remains alive and is not
200215
/// mutably accessed for the entire duration of the returned
201216
/// reference.
217+
#[inline]
202218
pub unsafe fn as_ref(&self) -> &InstanceEntity {
203219
unsafe { &*self.as_ptr() }
204220
}

0 commit comments

Comments
 (0)