From 907edacd00387ed4631ecde4731315f4c2fc082d Mon Sep 17 00:00:00 2001 From: Evian-Zhang Date: Thu, 28 Nov 2024 08:01:53 +0800 Subject: [PATCH] Fix more Rust 2024 edition warnings --- Cargo.lock | 4 +- src/os/macos.rs | 106 ++++++++++++++++++++++++++-------------------- src/os/windows.rs | 8 ++-- 3 files changed, 67 insertions(+), 51 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index babd29e..059099b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,9 +4,9 @@ version = 4 [[package]] name = "clear-cache" -version = "0.1.0" +version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "20d9da0bc09582e0aa48f88476160bfe0f5f82cf97fc47eef1f701b7f59e27d2" +checksum = "fe05a1a3c09033e9e87f7ba13af35cf2508148d3a97fb6b16475df84207993fa" dependencies = [ "libc", "windows", diff --git a/src/os/macos.rs b/src/os/macos.rs index b91e46b..d7e9463 100644 --- a/src/os/macos.rs +++ b/src/os/macos.rs @@ -46,77 +46,91 @@ impl CodeManipulator for ArchCodeManipulator { let mut remap_addr = 0; let mut cur_prot = 0; let mut max_prot = 0; - let self_task = mach2::traps::mach_task_self(); + let self_task = unsafe { mach2::traps::mach_task_self() }; let length = L as u64; // 1. Remap the page somewhere else - let ret = mach2::vm::mach_vm_remap( - self_task, - &mut remap_addr, - length, - 0, - mach2::vm_statistics::VM_FLAGS_ANYWHERE | VM_FLAGS_RETURN_DATA_ADDR, - self_task, - addr as u64, - 0, - &mut cur_prot, - &mut max_prot, - mach2::vm_inherit::VM_INHERIT_NONE, - ); + let ret = unsafe { + mach2::vm::mach_vm_remap( + self_task, + &mut remap_addr, + length, + 0, + mach2::vm_statistics::VM_FLAGS_ANYWHERE | VM_FLAGS_RETURN_DATA_ADDR, + self_task, + addr as u64, + 0, + &mut cur_prot, + &mut max_prot, + mach2::vm_inherit::VM_INHERIT_NONE, + ) + }; if ret != mach2::kern_return::KERN_SUCCESS { panic!("mach_vm_remap to new failed"); } // 2. Reprotect the page to rw- (needs VM_PROT_COPY because the max protection is currently r-x) - let ret = mach2::vm::mach_vm_protect( - self_task, - remap_addr, - length, - 0, - mach2::vm_prot::VM_PROT_READ - | mach2::vm_prot::VM_PROT_WRITE - | mach2::vm_prot::VM_PROT_COPY, - ); + let ret = unsafe { + mach2::vm::mach_vm_protect( + self_task, + remap_addr, + length, + 0, + mach2::vm_prot::VM_PROT_READ + | mach2::vm_prot::VM_PROT_WRITE + | mach2::vm_prot::VM_PROT_COPY, + ) + }; if ret != mach2::kern_return::KERN_SUCCESS { panic!("mach_vm_protect to write failed"); } // 3. Write the changes - core::ptr::copy_nonoverlapping(data.as_ptr(), remap_addr as *mut _, L); + unsafe { + core::ptr::copy_nonoverlapping(data.as_ptr(), remap_addr as *mut _, L); + } // 4. Flush the data cache - sys_dcache_flush(addr, L); + unsafe { + sys_dcache_flush(addr, L); + } // 5. Reprotect the page to r-x - let ret = mach2::vm::mach_vm_protect( - self_task, - remap_addr, - length, - 0, - mach2::vm_prot::VM_PROT_READ | mach2::vm_prot::VM_PROT_EXECUTE, - ); + let ret = unsafe { + mach2::vm::mach_vm_protect( + self_task, + remap_addr, + length, + 0, + mach2::vm_prot::VM_PROT_READ | mach2::vm_prot::VM_PROT_EXECUTE, + ) + }; if ret != mach2::kern_return::KERN_SUCCESS { panic!("mach_vm_protect to execute failed"); } // 6. Invalidate the instruction cache - sys_icache_invalidate(addr, L); + unsafe { + sys_icache_invalidate(addr, L); + } // 7. Remap the page back over the original let mut origin_addr = addr as u64; - let ret = mach2::vm::mach_vm_remap( - self_task, - &mut origin_addr, - length, - 0, - mach2::vm_statistics::VM_FLAGS_OVERWRITE | VM_FLAGS_RETURN_DATA_ADDR, - self_task, - remap_addr, - 0, - &mut cur_prot, - &mut max_prot, - mach2::vm_inherit::VM_INHERIT_NONE, - ); + let ret = unsafe { + mach2::vm::mach_vm_remap( + self_task, + &mut origin_addr, + length, + 0, + mach2::vm_statistics::VM_FLAGS_OVERWRITE | VM_FLAGS_RETURN_DATA_ADDR, + self_task, + remap_addr, + 0, + &mut cur_prot, + &mut max_prot, + mach2::vm_inherit::VM_INHERIT_NONE, + ) + }; if ret != mach2::kern_return::KERN_SUCCESS { panic!("mach_vm_remap to origin failed"); } diff --git a/src/os/windows.rs b/src/os/windows.rs index 45bfd73..b085cc7 100644 --- a/src/os/windows.rs +++ b/src/os/windows.rs @@ -22,10 +22,10 @@ macro_rules! os_static_key_sec_name_attr { // See https://stackoverflow.com/a/14783759 and https://devblogs.microsoft.com/oldnewthing/20181107-00/?p=100155 /// Address of this static is the start address of .stks section -#[link_section = ".stks$a"] +#[unsafe(link_section = ".stks$a")] pub static mut JUMP_ENTRY_START: JumpEntry = JumpEntry::dummy(); /// Address of this static is the end address of .stks section -#[link_section = ".stks$c"] +#[unsafe(link_section = ".stks$c")] pub static mut JUMP_ENTRY_STOP: JumpEntry = JumpEntry::dummy(); /// Arch-specific [`CodeManipulator`] using `VirtualProtect`. @@ -58,7 +58,9 @@ impl CodeManipulator for ArchCodeManipulator { if res.is_err() { panic!("Unable to make code region writable"); } - core::ptr::copy_nonoverlapping(data.as_ptr(), addr.cast(), L); + unsafe { + core::ptr::copy_nonoverlapping(data.as_ptr(), addr.cast(), L); + } let mut old_protect = PAGE_PROTECTION_FLAGS::default(); let res = unsafe { VirtualProtect(