Skip to content

Commit

Permalink
Adds deny(clippy::ptr_as_ptr).
Browse files Browse the repository at this point in the history
  • Loading branch information
Lichtso committed Dec 6, 2023
1 parent 1709f6e commit 91c6bde
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ impl<T: std::fmt::Debug, E: std::fmt::Debug> StableResult<T, E> {
allow(dead_code)
)]
pub(crate) fn discriminant(&self) -> u64 {
unsafe { *(self as *const _ as *const u64) }
unsafe { *std::ptr::addr_of!(*self).cast::<u64>() }
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/jit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl JitProgram {
let raw = allocate_pages(pc_loc_table_size + over_allocated_code_size)?;
Ok(Self {
page_size,
pc_section: std::slice::from_raw_parts_mut(raw as *mut usize, pc),
pc_section: std::slice::from_raw_parts_mut(raw.cast::<usize>(), pc),
text_section: std::slice::from_raw_parts_mut(
raw.add(pc_loc_table_size),
over_allocated_code_size,
Expand Down Expand Up @@ -80,7 +80,7 @@ impl JitProgram {
self.text_section =
std::slice::from_raw_parts_mut(raw.add(pc_loc_table_size), text_section_usage);
protect_pages(
self.pc_section.as_mut_ptr() as *mut u8,
self.pc_section.as_mut_ptr().cast::<u8>(),
pc_loc_table_size,
false,
)?;
Expand Down Expand Up @@ -119,7 +119,7 @@ impl JitProgram {
"pop rbp",
"pop rbx",
host_stack_pointer = in(reg) &mut vm.host_stack_pointer,
inlateout("rdi") (vm as *mut _ as *mut u64).offset(get_runtime_environment_key() as isize) => _,
inlateout("rdi") std::ptr::addr_of_mut!(*vm).cast::<u64>().offset(get_runtime_environment_key() as isize) => _,
inlateout("rax") (vm.previous_instruction_meter as i64).wrapping_add(registers[11] as i64) => _,
inlateout("r10") self.pc_section[registers[11] as usize] => _,
inlateout("r11") &registers => _,
Expand Down Expand Up @@ -1269,7 +1269,7 @@ impl<'a, C: ContextObject> JitCompiler<'a, C> {
}

fn emit_set_exception_kind(&mut self, err: EbpfError) {
let err_kind = unsafe { *(&err as *const _ as *const u64) };
let err_kind = unsafe { *std::ptr::addr_of!(err).cast::<u64>() };
let err_discriminant = ProgramResult::Err(err).discriminant();
self.emit_ins(X86Instruction::lea(OperandSize::S64, REGISTER_PTR_TO_VM, REGISTER_OTHER_SCRATCH, Some(X86IndirectAccess::Offset(self.slot_in_vm(RuntimeEnvironmentSlot::ProgramResult)))));
self.emit_ins(X86Instruction::store_immediate(OperandSize::S64, REGISTER_OTHER_SCRATCH, X86IndirectAccess::Offset(0), err_discriminant as i64)); // result.discriminant = err_discriminant;
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
html_favicon_url = "https://raw.githubusercontent.com/qmonnet/rbpf/master/misc/rbpf.ico"
)]
#![deny(clippy::arithmetic_side_effects)]
#![deny(clippy::ptr_as_ptr)]

extern crate byteorder;
extern crate combine;
Expand Down
10 changes: 5 additions & 5 deletions src/memory_management.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,16 @@ pub unsafe fn allocate_pages(size_in_bytes: usize) -> Result<*mut u8, EbpfError>
winnt::MEM_RESERVE | winnt::MEM_COMMIT,
winnt::PAGE_READWRITE,
);
Ok(raw as *mut u8)
Ok(raw.cast::<u8>())
}

pub unsafe fn free_pages(raw: *mut u8, size_in_bytes: usize) -> Result<(), EbpfError> {
#[cfg(not(target_os = "windows"))]
libc_error_guard!(munmap, raw as *mut _, size_in_bytes);
libc_error_guard!(munmap, raw.cast::<c_void>(), size_in_bytes);
#[cfg(target_os = "windows")]
winapi_error_guard!(
VirtualFree,
raw as *mut _,
raw.cast::<c_void>(),
size_in_bytes,
winnt::MEM_RELEASE, // winnt::MEM_DECOMMIT
);
Expand All @@ -138,7 +138,7 @@ pub unsafe fn protect_pages(
{
libc_error_guard!(
mprotect,
raw as *mut _,
raw.cast::<c_void>(),
size_in_bytes,
if executable_flag {
libc::PROT_EXEC | libc::PROT_READ
Expand All @@ -153,7 +153,7 @@ pub unsafe fn protect_pages(
let ptr_old: *mut minwindef::DWORD = &mut old;
winapi_error_guard!(
VirtualProtect,
raw as *mut _,
raw.cast::<c_void>(),
size_in_bytes,
if executable_flag {
winnt::PAGE_EXECUTE_READ
Expand Down
4 changes: 2 additions & 2 deletions src/memory_region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ impl<'a> UnalignedMemoryMapping<'a> {
let initial_len = len;
let initial_vm_addr = vm_addr;
let mut value = 0u64;
let mut ptr = &mut value as *mut _ as *mut u8;
let mut ptr = std::ptr::addr_of_mut!(value).cast::<u8>();

while len > 0 {
let load_len = len.min(region.vm_addr_end.saturating_sub(vm_addr));
Expand Down Expand Up @@ -440,7 +440,7 @@ impl<'a> UnalignedMemoryMapping<'a> {
// guaranteed to be unique.
let cache = unsafe { &mut *self.cache.get() };

let mut src = &value as *const _ as *const u8;
let mut src = std::ptr::addr_of!(value).cast::<u8>();

let mut region = match self.find_region(cache, vm_addr) {
Some(region) if ensure_writable_region(region, &self.cow_cb) => {
Expand Down
2 changes: 1 addition & 1 deletion src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ macro_rules! declare_builtin_function {
) {
use $crate::vm::ContextObject;
let vm = unsafe {
&mut *(($vm as *mut u64).offset(-($crate::vm::get_runtime_environment_key() as isize)) as *mut $crate::vm::EbpfVm<$ContextObject>)
&mut *($vm.cast::<u64>().offset(-($crate::vm::get_runtime_environment_key() as isize)).cast::<$crate::vm::EbpfVm<$ContextObject>>())
};
let config = vm.loader.get_config();
if config.enable_instruction_meter {
Expand Down
6 changes: 4 additions & 2 deletions src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,8 +426,10 @@ impl<'a, C: ContextObject> EbpfVm<'a, C> {
pub fn invoke_function(&mut self, function: BuiltinFunction<C>) {
function(
unsafe {
(self as *mut _ as *mut u64).offset(get_runtime_environment_key() as isize)
as *mut _
std::ptr::addr_of_mut!(*self)
.cast::<u64>()
.offset(get_runtime_environment_key() as isize)
.cast::<Self>()
},
self.registers[1],
self.registers[2],
Expand Down

0 comments on commit 91c6bde

Please sign in to comment.