Skip to content

Commit 881c194

Browse files
authored
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.
1 parent a9d2097 commit 881c194

14 files changed

Lines changed: 64 additions & 58 deletions

File tree

cranelift/jit/src/backend.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -874,7 +874,7 @@ fn lookup_with_dlsym(name: &str) -> Option<*const u8> {
874874
// try to find the searched symbol in the currently running executable
875875
ptr::null_mut(),
876876
// try to find the searched symbol in local c runtime
877-
winapi::um::libloaderapi::GetModuleHandleA(MSVCRT_DLL.as_ptr() as *const i8),
877+
winapi::um::libloaderapi::GetModuleHandleA(MSVCRT_DLL.as_ptr().cast::<i8>()),
878878
];
879879

880880
for handle in &handles {

crates/jit/src/code_memory.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -200,11 +200,7 @@ unsafe fn register_unwind_info(obj: &File, text: &[u8]) -> Result<Option<UnwindR
200200
return Ok(None);
201201
}
202202
Ok(Some(
203-
UnwindRegistration::new(
204-
text.as_ptr() as *mut _,
205-
unwind_info.as_ptr() as *mut _,
206-
unwind_info.len(),
207-
)
208-
.context("failed to create unwind info registration")?,
203+
UnwindRegistration::new(text.as_ptr(), unwind_info.as_ptr(), unwind_info.len())
204+
.context("failed to create unwind info registration")?,
209205
))
210206
}

crates/jit/src/instantiate.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -387,16 +387,13 @@ impl CompiledModule {
387387
#[inline]
388388
pub fn finished_functions(
389389
&self,
390-
) -> impl ExactSizeIterator<Item = (DefinedFuncIndex, *mut [VMFunctionBody])> + '_ {
390+
) -> impl ExactSizeIterator<Item = (DefinedFuncIndex, *const [VMFunctionBody])> + '_ {
391391
let code = self.code();
392392
self.funcs.iter().map(move |(i, info)| {
393393
let func = &code[info.start as usize..][..info.length as usize];
394394
(
395395
i,
396-
std::ptr::slice_from_raw_parts_mut(
397-
func.as_ptr() as *mut VMFunctionBody,
398-
func.len(),
399-
),
396+
std::ptr::slice_from_raw_parts(func.as_ptr().cast::<VMFunctionBody>(), func.len()),
400397
)
401398
})
402399
}
@@ -423,7 +420,7 @@ impl CompiledModule {
423420
/// memory with the stack maps associated with those bytes.
424421
pub fn stack_maps(
425422
&self,
426-
) -> impl Iterator<Item = (*mut [VMFunctionBody], &[StackMapInformation])> {
423+
) -> impl Iterator<Item = (*const [VMFunctionBody], &[StackMapInformation])> {
427424
self.finished_functions()
428425
.map(|(_, f)| f)
429426
.zip(self.funcs.values().map(|f| f.stack_maps.as_slice()))

crates/jit/src/profiling/jitdump_linux.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ impl State {
292292
let tid = pid; // ThreadId does appear to track underlying thread. Using PID.
293293

294294
for (idx, func) in module.finished_functions() {
295-
let (addr, len) = unsafe { ((*func).as_ptr() as *const u8, (*func).len()) };
295+
let (addr, len) = unsafe { ((*func).as_ptr().cast::<u8>(), (*func).len()) };
296296
if let Some(img) = &dbg_image {
297297
if let Err(err) = self.dump_from_debug_image(img, "wasm", addr, len, pid, tid) {
298298
println!(

crates/jit/src/profiling/vtune_linux.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ impl State {
130130
.unwrap_or_else(|| format!("wasm_module_{}", global_module_id));
131131

132132
for (idx, func) in module.finished_functions() {
133-
let (addr, len) = unsafe { ((*func).as_ptr() as *const u8, (*func).len()) };
133+
let (addr, len) = unsafe { ((*func).as_ptr().cast::<u8>(), (*func).len()) };
134134
let method_name = super::debug_name(module.module(), idx);
135135
let method_id = self.get_method_id();
136136
log::trace!(

crates/jit/src/unwind/systemv.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ impl UnwindRegistration {
2121
/// describe an in-memory representation of a `.eh_frame` section. This is
2222
/// typically arranged for by the `wasmtime-obj` crate.
2323
pub unsafe fn new(
24-
_base_address: *mut u8,
25-
unwind_info: *mut u8,
24+
_base_address: *const u8,
25+
unwind_info: *const u8,
2626
unwind_len: usize,
2727
) -> Result<UnwindRegistration> {
2828
debug_assert_eq!(

crates/jit/src/unwind/winx64.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ pub struct UnwindRegistration {
1111

1212
impl UnwindRegistration {
1313
pub unsafe fn new(
14-
base_address: *mut u8,
15-
unwind_info: *mut u8,
14+
base_address: *const u8,
15+
unwind_info: *const u8,
1616
unwind_len: usize,
1717
) -> Result<UnwindRegistration> {
1818
assert!(unwind_info as usize % 4 == 0);

crates/runtime/src/externref.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ impl VMExternRef {
422422
/// or `PartialEq` implementation of the pointed-to values.
423423
#[inline]
424424
pub fn eq(a: &Self, b: &Self) -> bool {
425-
ptr::eq(a.0.as_ptr() as *const _, b.0.as_ptr() as *const _)
425+
ptr::eq(a.0.as_ptr(), b.0.as_ptr())
426426
}
427427

428428
/// Hash a given `VMExternRef`.
@@ -434,7 +434,7 @@ impl VMExternRef {
434434
where
435435
H: Hasher,
436436
{
437-
ptr::hash(externref.0.as_ptr() as *const _, hasher);
437+
ptr::hash(externref.0.as_ptr(), hasher);
438438
}
439439

440440
/// Compare two `VMExternRef`s.
@@ -566,8 +566,8 @@ impl VMExternRefActivationsTable {
566566

567567
/// Create a new `VMExternRefActivationsTable`.
568568
pub fn new() -> Self {
569-
let chunk = Self::new_chunk(Self::CHUNK_SIZE);
570-
let next = chunk.as_ptr() as *mut TableElem;
569+
let mut chunk = Self::new_chunk(Self::CHUNK_SIZE);
570+
let next = chunk.as_mut_ptr().cast::<TableElem>();
571571
let end = unsafe { next.add(chunk.len()) };
572572

573573
VMExternRefActivationsTable {
@@ -703,7 +703,7 @@ impl VMExternRefActivationsTable {
703703
precise_stack_roots: &mut HashSet<VMExternRefWithTraits>,
704704
root: NonNull<VMExternData>,
705705
) {
706-
let root = unsafe { VMExternRef::clone_from_raw(root.as_ptr() as *mut _) };
706+
let root = unsafe { VMExternRef::clone_from_raw(root.as_ptr().cast()) };
707707
precise_stack_roots.insert(VMExternRefWithTraits(root));
708708
}
709709

@@ -730,7 +730,7 @@ impl VMExternRefActivationsTable {
730730

731731
// Reset our `next` finger to the start of the bump allocation chunk.
732732
unsafe {
733-
let next = self.alloc.chunk.as_ptr() as *mut TableElem;
733+
let next = self.alloc.chunk.as_mut_ptr().cast::<TableElem>();
734734
debug_assert!(!next.is_null());
735735
*self.alloc.next.get() = NonNull::new_unchecked(next);
736736
}

crates/runtime/src/instance.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ impl Instance {
100100
/// Helper function to access various locations offset from our `*mut
101101
/// VMContext` object.
102102
unsafe fn vmctx_plus_offset<T>(&self, offset: u32) -> *mut T {
103-
(self.vmctx_ptr() as *mut u8)
103+
(self.vmctx_ptr().cast::<u8>())
104104
.add(usize::try_from(offset).unwrap())
105105
.cast()
106106
}
@@ -420,7 +420,8 @@ impl Instance {
420420

421421
// Keep the `VMContext` pointers used by compiled Wasm code up to
422422
// date.
423-
self.set_table(table_index, self.tables[table_index].vmtable());
423+
let element = self.tables[table_index].vmtable();
424+
self.set_table(table_index, element);
424425

425426
result
426427
}

crates/runtime/src/memory.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -476,10 +476,10 @@ impl Memory {
476476
}
477477

478478
/// Return a `VMMemoryDefinition` for exposing the memory to compiled wasm code.
479-
pub fn vmmemory(&self) -> VMMemoryDefinition {
479+
pub fn vmmemory(&mut self) -> VMMemoryDefinition {
480480
match self {
481481
Memory::Static { base, size, .. } => VMMemoryDefinition {
482-
base: base.as_ptr() as *mut _,
482+
base: base.as_mut_ptr().cast(),
483483
current_length: *size,
484484
},
485485
Memory::Dynamic(mem) => mem.vmmemory(),

0 commit comments

Comments
 (0)