Use ptr::cast instead of as casts in several places. (#3507)
`ptr::cast` has the advantage of being unable to silently cast `*const T` to `*mut T`. This turned up several places that were performing such casts, which this PR also fixes.
This commit is contained in:
@@ -422,7 +422,7 @@ impl VMExternRef {
|
||||
/// or `PartialEq` implementation of the pointed-to values.
|
||||
#[inline]
|
||||
pub fn eq(a: &Self, b: &Self) -> bool {
|
||||
ptr::eq(a.0.as_ptr() as *const _, b.0.as_ptr() as *const _)
|
||||
ptr::eq(a.0.as_ptr(), b.0.as_ptr())
|
||||
}
|
||||
|
||||
/// Hash a given `VMExternRef`.
|
||||
@@ -434,7 +434,7 @@ impl VMExternRef {
|
||||
where
|
||||
H: Hasher,
|
||||
{
|
||||
ptr::hash(externref.0.as_ptr() as *const _, hasher);
|
||||
ptr::hash(externref.0.as_ptr(), hasher);
|
||||
}
|
||||
|
||||
/// Compare two `VMExternRef`s.
|
||||
@@ -566,8 +566,8 @@ impl VMExternRefActivationsTable {
|
||||
|
||||
/// Create a new `VMExternRefActivationsTable`.
|
||||
pub fn new() -> Self {
|
||||
let chunk = Self::new_chunk(Self::CHUNK_SIZE);
|
||||
let next = chunk.as_ptr() as *mut TableElem;
|
||||
let mut chunk = Self::new_chunk(Self::CHUNK_SIZE);
|
||||
let next = chunk.as_mut_ptr().cast::<TableElem>();
|
||||
let end = unsafe { next.add(chunk.len()) };
|
||||
|
||||
VMExternRefActivationsTable {
|
||||
@@ -703,7 +703,7 @@ impl VMExternRefActivationsTable {
|
||||
precise_stack_roots: &mut HashSet<VMExternRefWithTraits>,
|
||||
root: NonNull<VMExternData>,
|
||||
) {
|
||||
let root = unsafe { VMExternRef::clone_from_raw(root.as_ptr() as *mut _) };
|
||||
let root = unsafe { VMExternRef::clone_from_raw(root.as_ptr().cast()) };
|
||||
precise_stack_roots.insert(VMExternRefWithTraits(root));
|
||||
}
|
||||
|
||||
@@ -730,7 +730,7 @@ impl VMExternRefActivationsTable {
|
||||
|
||||
// Reset our `next` finger to the start of the bump allocation chunk.
|
||||
unsafe {
|
||||
let next = self.alloc.chunk.as_ptr() as *mut TableElem;
|
||||
let next = self.alloc.chunk.as_mut_ptr().cast::<TableElem>();
|
||||
debug_assert!(!next.is_null());
|
||||
*self.alloc.next.get() = NonNull::new_unchecked(next);
|
||||
}
|
||||
|
||||
@@ -100,7 +100,7 @@ impl Instance {
|
||||
/// Helper function to access various locations offset from our `*mut
|
||||
/// VMContext` object.
|
||||
unsafe fn vmctx_plus_offset<T>(&self, offset: u32) -> *mut T {
|
||||
(self.vmctx_ptr() as *mut u8)
|
||||
(self.vmctx_ptr().cast::<u8>())
|
||||
.add(usize::try_from(offset).unwrap())
|
||||
.cast()
|
||||
}
|
||||
@@ -420,7 +420,8 @@ impl Instance {
|
||||
|
||||
// Keep the `VMContext` pointers used by compiled Wasm code up to
|
||||
// date.
|
||||
self.set_table(table_index, self.tables[table_index].vmtable());
|
||||
let element = self.tables[table_index].vmtable();
|
||||
self.set_table(table_index, element);
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
@@ -476,10 +476,10 @@ impl Memory {
|
||||
}
|
||||
|
||||
/// Return a `VMMemoryDefinition` for exposing the memory to compiled wasm code.
|
||||
pub fn vmmemory(&self) -> VMMemoryDefinition {
|
||||
pub fn vmmemory(&mut self) -> VMMemoryDefinition {
|
||||
match self {
|
||||
Memory::Static { base, size, .. } => VMMemoryDefinition {
|
||||
base: base.as_ptr() as *mut _,
|
||||
base: base.as_mut_ptr().cast(),
|
||||
current_length: *size,
|
||||
},
|
||||
Memory::Dynamic(mem) => mem.vmmemory(),
|
||||
|
||||
@@ -398,14 +398,14 @@ impl Table {
|
||||
}
|
||||
|
||||
/// Return a `VMTableDefinition` for exposing the table to compiled wasm code.
|
||||
pub fn vmtable(&self) -> VMTableDefinition {
|
||||
pub fn vmtable(&mut self) -> VMTableDefinition {
|
||||
match self {
|
||||
Table::Static { data, size, .. } => VMTableDefinition {
|
||||
base: data.as_ptr() as *mut _,
|
||||
base: data.as_mut_ptr().cast(),
|
||||
current_elements: *size,
|
||||
},
|
||||
Table::Dynamic { elements, .. } => VMTableDefinition {
|
||||
base: elements.as_ptr() as _,
|
||||
base: elements.as_mut_ptr().cast(),
|
||||
current_elements: elements.len().try_into().unwrap(),
|
||||
},
|
||||
}
|
||||
|
||||
@@ -340,145 +340,157 @@ impl VMGlobalDefinition {
|
||||
/// Return a reference to the value as an i32.
|
||||
#[allow(clippy::cast_ptr_alignment)]
|
||||
pub unsafe fn as_i32(&self) -> &i32 {
|
||||
&*(self.storage.as_ref().as_ptr() as *const i32)
|
||||
&*(self.storage.as_ref().as_ptr().cast::<i32>())
|
||||
}
|
||||
|
||||
/// Return a mutable reference to the value as an i32.
|
||||
#[allow(clippy::cast_ptr_alignment)]
|
||||
pub unsafe fn as_i32_mut(&mut self) -> &mut i32 {
|
||||
&mut *(self.storage.as_mut().as_mut_ptr() as *mut i32)
|
||||
&mut *(self.storage.as_mut().as_mut_ptr().cast::<i32>())
|
||||
}
|
||||
|
||||
/// Return a reference to the value as a u32.
|
||||
#[allow(clippy::cast_ptr_alignment)]
|
||||
pub unsafe fn as_u32(&self) -> &u32 {
|
||||
&*(self.storage.as_ref().as_ptr() as *const u32)
|
||||
&*(self.storage.as_ref().as_ptr().cast::<u32>())
|
||||
}
|
||||
|
||||
/// Return a mutable reference to the value as an u32.
|
||||
#[allow(clippy::cast_ptr_alignment)]
|
||||
pub unsafe fn as_u32_mut(&mut self) -> &mut u32 {
|
||||
&mut *(self.storage.as_mut().as_mut_ptr() as *mut u32)
|
||||
&mut *(self.storage.as_mut().as_mut_ptr().cast::<u32>())
|
||||
}
|
||||
|
||||
/// Return a reference to the value as an i64.
|
||||
#[allow(clippy::cast_ptr_alignment)]
|
||||
pub unsafe fn as_i64(&self) -> &i64 {
|
||||
&*(self.storage.as_ref().as_ptr() as *const i64)
|
||||
&*(self.storage.as_ref().as_ptr().cast::<i64>())
|
||||
}
|
||||
|
||||
/// Return a mutable reference to the value as an i64.
|
||||
#[allow(clippy::cast_ptr_alignment)]
|
||||
pub unsafe fn as_i64_mut(&mut self) -> &mut i64 {
|
||||
&mut *(self.storage.as_mut().as_mut_ptr() as *mut i64)
|
||||
&mut *(self.storage.as_mut().as_mut_ptr().cast::<i64>())
|
||||
}
|
||||
|
||||
/// Return a reference to the value as an u64.
|
||||
#[allow(clippy::cast_ptr_alignment)]
|
||||
pub unsafe fn as_u64(&self) -> &u64 {
|
||||
&*(self.storage.as_ref().as_ptr() as *const u64)
|
||||
&*(self.storage.as_ref().as_ptr().cast::<u64>())
|
||||
}
|
||||
|
||||
/// Return a mutable reference to the value as an u64.
|
||||
#[allow(clippy::cast_ptr_alignment)]
|
||||
pub unsafe fn as_u64_mut(&mut self) -> &mut u64 {
|
||||
&mut *(self.storage.as_mut().as_mut_ptr() as *mut u64)
|
||||
&mut *(self.storage.as_mut().as_mut_ptr().cast::<u64>())
|
||||
}
|
||||
|
||||
/// Return a reference to the value as an f32.
|
||||
#[allow(clippy::cast_ptr_alignment)]
|
||||
pub unsafe fn as_f32(&self) -> &f32 {
|
||||
&*(self.storage.as_ref().as_ptr() as *const f32)
|
||||
&*(self.storage.as_ref().as_ptr().cast::<f32>())
|
||||
}
|
||||
|
||||
/// Return a mutable reference to the value as an f32.
|
||||
#[allow(clippy::cast_ptr_alignment)]
|
||||
pub unsafe fn as_f32_mut(&mut self) -> &mut f32 {
|
||||
&mut *(self.storage.as_mut().as_mut_ptr() as *mut f32)
|
||||
&mut *(self.storage.as_mut().as_mut_ptr().cast::<f32>())
|
||||
}
|
||||
|
||||
/// Return a reference to the value as f32 bits.
|
||||
#[allow(clippy::cast_ptr_alignment)]
|
||||
pub unsafe fn as_f32_bits(&self) -> &u32 {
|
||||
&*(self.storage.as_ref().as_ptr() as *const u32)
|
||||
&*(self.storage.as_ref().as_ptr().cast::<u32>())
|
||||
}
|
||||
|
||||
/// Return a mutable reference to the value as f32 bits.
|
||||
#[allow(clippy::cast_ptr_alignment)]
|
||||
pub unsafe fn as_f32_bits_mut(&mut self) -> &mut u32 {
|
||||
&mut *(self.storage.as_mut().as_mut_ptr() as *mut u32)
|
||||
&mut *(self.storage.as_mut().as_mut_ptr().cast::<u32>())
|
||||
}
|
||||
|
||||
/// Return a reference to the value as an f64.
|
||||
#[allow(clippy::cast_ptr_alignment)]
|
||||
pub unsafe fn as_f64(&self) -> &f64 {
|
||||
&*(self.storage.as_ref().as_ptr() as *const f64)
|
||||
&*(self.storage.as_ref().as_ptr().cast::<f64>())
|
||||
}
|
||||
|
||||
/// Return a mutable reference to the value as an f64.
|
||||
#[allow(clippy::cast_ptr_alignment)]
|
||||
pub unsafe fn as_f64_mut(&mut self) -> &mut f64 {
|
||||
&mut *(self.storage.as_mut().as_mut_ptr() as *mut f64)
|
||||
&mut *(self.storage.as_mut().as_mut_ptr().cast::<f64>())
|
||||
}
|
||||
|
||||
/// Return a reference to the value as f64 bits.
|
||||
#[allow(clippy::cast_ptr_alignment)]
|
||||
pub unsafe fn as_f64_bits(&self) -> &u64 {
|
||||
&*(self.storage.as_ref().as_ptr() as *const u64)
|
||||
&*(self.storage.as_ref().as_ptr().cast::<u64>())
|
||||
}
|
||||
|
||||
/// Return a mutable reference to the value as f64 bits.
|
||||
#[allow(clippy::cast_ptr_alignment)]
|
||||
pub unsafe fn as_f64_bits_mut(&mut self) -> &mut u64 {
|
||||
&mut *(self.storage.as_mut().as_mut_ptr() as *mut u64)
|
||||
&mut *(self.storage.as_mut().as_mut_ptr().cast::<u64>())
|
||||
}
|
||||
|
||||
/// Return a reference to the value as an u128.
|
||||
#[allow(clippy::cast_ptr_alignment)]
|
||||
pub unsafe fn as_u128(&self) -> &u128 {
|
||||
&*(self.storage.as_ref().as_ptr() as *const u128)
|
||||
&*(self.storage.as_ref().as_ptr().cast::<u128>())
|
||||
}
|
||||
|
||||
/// Return a mutable reference to the value as an u128.
|
||||
#[allow(clippy::cast_ptr_alignment)]
|
||||
pub unsafe fn as_u128_mut(&mut self) -> &mut u128 {
|
||||
&mut *(self.storage.as_mut().as_mut_ptr() as *mut u128)
|
||||
&mut *(self.storage.as_mut().as_mut_ptr().cast::<u128>())
|
||||
}
|
||||
|
||||
/// Return a reference to the value as u128 bits.
|
||||
#[allow(clippy::cast_ptr_alignment)]
|
||||
pub unsafe fn as_u128_bits(&self) -> &[u8; 16] {
|
||||
&*(self.storage.as_ref().as_ptr() as *const [u8; 16])
|
||||
&*(self.storage.as_ref().as_ptr().cast::<[u8; 16]>())
|
||||
}
|
||||
|
||||
/// Return a mutable reference to the value as u128 bits.
|
||||
#[allow(clippy::cast_ptr_alignment)]
|
||||
pub unsafe fn as_u128_bits_mut(&mut self) -> &mut [u8; 16] {
|
||||
&mut *(self.storage.as_mut().as_mut_ptr() as *mut [u8; 16])
|
||||
&mut *(self.storage.as_mut().as_mut_ptr().cast::<[u8; 16]>())
|
||||
}
|
||||
|
||||
/// Return a reference to the value as an externref.
|
||||
#[allow(clippy::cast_ptr_alignment)]
|
||||
pub unsafe fn as_externref(&self) -> &Option<VMExternRef> {
|
||||
&*(self.storage.as_ref().as_ptr() as *const Option<VMExternRef>)
|
||||
&*(self.storage.as_ref().as_ptr().cast::<Option<VMExternRef>>())
|
||||
}
|
||||
|
||||
/// Return a mutable reference to the value as an externref.
|
||||
#[allow(clippy::cast_ptr_alignment)]
|
||||
pub unsafe fn as_externref_mut(&mut self) -> &mut Option<VMExternRef> {
|
||||
&mut *(self.storage.as_mut().as_mut_ptr() as *mut Option<VMExternRef>)
|
||||
&mut *(self
|
||||
.storage
|
||||
.as_mut()
|
||||
.as_mut_ptr()
|
||||
.cast::<Option<VMExternRef>>())
|
||||
}
|
||||
|
||||
/// Return a reference to the value as an anyfunc.
|
||||
#[allow(clippy::cast_ptr_alignment)]
|
||||
pub unsafe fn as_anyfunc(&self) -> *const VMCallerCheckedAnyfunc {
|
||||
*(self.storage.as_ref().as_ptr() as *const *const VMCallerCheckedAnyfunc)
|
||||
*(self
|
||||
.storage
|
||||
.as_ref()
|
||||
.as_ptr()
|
||||
.cast::<*const VMCallerCheckedAnyfunc>())
|
||||
}
|
||||
|
||||
/// Return a mutable reference to the value as an anyfunc.
|
||||
#[allow(clippy::cast_ptr_alignment)]
|
||||
pub unsafe fn as_anyfunc_mut(&mut self) -> &mut *const VMCallerCheckedAnyfunc {
|
||||
&mut *(self.storage.as_mut().as_mut_ptr() as *mut *const VMCallerCheckedAnyfunc)
|
||||
&mut *(self
|
||||
.storage
|
||||
.as_mut()
|
||||
.as_mut_ptr()
|
||||
.cast::<*const VMCallerCheckedAnyfunc>())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user