From 9357feb27a2378256256a0290922b19762cf5ede Mon Sep 17 00:00:00 2001 From: Sewer56 Date: Mon, 24 Jun 2024 01:27:35 +0100 Subject: [PATCH] Fix: A lot of compile warnings on various platforms. --- src-rust/Cargo.toml | 1 + src-rust/build.rs | 11 ++++++++ src-rust/src/buffers.rs | 3 +-- src-rust/src/internal/buffer_allocator.rs | 2 +- .../src/internal/buffer_allocator_windows.rs | 26 ++++++++++++++----- .../internal/memory_mapped_file_windows.rs | 4 +-- src-rust/src/lib.rs | 3 +++ .../src/structs/internal/locator_header.rs | 2 ++ src-rust/src/structs/private_allocation.rs | 9 ++++--- .../src/utilities/map_parser_utilities.rs | 1 - 10 files changed, 46 insertions(+), 16 deletions(-) create mode 100644 src-rust/build.rs diff --git a/src-rust/Cargo.toml b/src-rust/Cargo.toml index 1955342..27c05bd 100644 --- a/src-rust/Cargo.toml +++ b/src-rust/Cargo.toml @@ -19,6 +19,7 @@ no_format = [] # Removes string formatting (less detailed errors) for binary siz all_private = [] # No memory mapped files, memory is not shared. size_opt = ["nightly"] nightly = [] # Optimizations for nightly builds. +mmap-rs = [] # Uses mmap-rs for memory mapping. This is auto activated during build. [dependencies] concat-string = "1.0.1" diff --git a/src-rust/build.rs b/src-rust/build.rs new file mode 100644 index 0000000..09908c0 --- /dev/null +++ b/src-rust/build.rs @@ -0,0 +1,11 @@ +fn main() { + // This defines a fallback to mmap-rs if one of the explicit memory mapped file implementations + // is not available. + if !cfg!(any( + target_os = "macos", + target_os = "windows", + target_os = "linux" + )) { + println!("cargo:rustc-cfg=feature=\"mmap-rs\""); + } +} diff --git a/src-rust/src/buffers.rs b/src-rust/src/buffers.rs index 5129a45..f8983be 100644 --- a/src-rust/src/buffers.rs +++ b/src-rust/src/buffers.rs @@ -234,7 +234,6 @@ mod tests { structs::params::{BufferAllocatorSettings, BufferSearchSettings}, utilities::cached::get_sys_info, }; - use std; #[cfg(not(target_os = "macos"))] #[test] @@ -418,7 +417,7 @@ mod tests { } let item = Buffers::get_buffer(&BufferSearchSettings::from_proximity( - std::i32::MAX as usize, + i32::MAX as usize, base_address, SIZE, )); diff --git a/src-rust/src/internal/buffer_allocator.rs b/src-rust/src/internal/buffer_allocator.rs index 0d4b933..4c34a04 100644 --- a/src-rust/src/internal/buffer_allocator.rs +++ b/src-rust/src/internal/buffer_allocator.rs @@ -22,7 +22,7 @@ pub fn allocate( return crate::internal::buffer_allocator_osx::allocate_osx(settings); // Fallback for non-hot-path OSes. - #[cfg(not(any(target_os = "macos", target_os = "windows", target_os = "linux")))] + #[cfg(feature = "mmap-rs")] crate::internal::buffer_allocator_mmap_rs::allocate_mmap_rs(settings) } diff --git a/src-rust/src/internal/buffer_allocator_windows.rs b/src-rust/src/internal/buffer_allocator_windows.rs index e00018f..573df6c 100644 --- a/src-rust/src/internal/buffer_allocator_windows.rs +++ b/src-rust/src/internal/buffer_allocator_windows.rs @@ -69,7 +69,7 @@ impl Kernel32 for RemoteKernel32 { lp_buffer: &mut MEMORY_BASIC_INFORMATION, ) -> usize { unsafe { - VirtualQueryEx( + windows_sys::Win32::System::Memory::VirtualQueryEx( self.handle, lp_address, lp_buffer, @@ -80,7 +80,7 @@ impl Kernel32 for RemoteKernel32 { fn virtual_alloc(&self, lp_address: *const c_void, dw_size: usize) -> *mut c_void { unsafe { - VirtualAllocEx( + windows_sys::Win32::System::Memory::VirtualAllocEx( self.handle, lp_address, dw_size, @@ -91,7 +91,14 @@ impl Kernel32 for RemoteKernel32 { } fn virtual_free(&self, lp_address: *mut c_void, dw_size: usize) -> bool { - unsafe { VirtualFreeEx(self.handle, lp_address, dw_size, MEM_RELEASE) != 0 } + unsafe { + windows_sys::Win32::System::Memory::VirtualFreeEx( + self.handle, + lp_address, + dw_size, + MEM_RELEASE, + ) != 0 + } } } @@ -111,6 +118,11 @@ impl ProcessHandle { pub fn is_valid(&self) -> bool { self.handle != 0 } + + #[cfg(feature = "external_processes")] + pub fn get_raw_handle(&self) -> HANDLE { + self.handle + } } impl Drop for ProcessHandle { @@ -172,11 +184,11 @@ pub fn allocate_windows( #[cfg(feature = "external_processes")] { - return if get_sys_info().this_process_id == settings.target_process_id { - allocate_fast(&LocalKernel32 {}, max_address, &settings) + if get_sys_info().this_process_id == settings.target_process_id { + allocate_fast(&LocalKernel32 {}, max_address, settings) } else { - allocate_fast(&RemoteKernel32 { handle }, max_address, &settings) - }; + allocate_fast(&RemoteKernel32 { handle }, max_address, settings) + } } #[cfg(not(feature = "external_processes"))] diff --git a/src-rust/src/internal/memory_mapped_file_windows.rs b/src-rust/src/internal/memory_mapped_file_windows.rs index 19289ad..567e954 100644 --- a/src-rust/src/internal/memory_mapped_file_windows.rs +++ b/src-rust/src/internal/memory_mapped_file_windows.rs @@ -92,12 +92,12 @@ mod tests { let file_length = get_sys_info().allocation_granularity as usize; let mmf = WindowsMemoryMappedFile::new(&file_name, file_length); - assert_eq!(mmf.already_existed, false); + assert!(!mmf.already_existed); assert_eq!(mmf.length, file_length); // Assert the file can be opened again (i.e., it exists) let mmf_existing = WindowsMemoryMappedFile::new(&file_name, file_length); - assert_eq!(mmf_existing.already_existed, true); + assert!(mmf_existing.already_existed); } #[test] diff --git a/src-rust/src/lib.rs b/src-rust/src/lib.rs index bae3de9..dbb844f 100644 --- a/src-rust/src/lib.rs +++ b/src-rust/src/lib.rs @@ -132,7 +132,10 @@ pub(crate) mod utilities { pub mod address_range; pub mod cached; pub mod icache_clear; + + #[cfg(any(target_os = "linux", feature = "mmap-rs"))] pub mod map_parser_utilities; + pub mod mathematics; pub mod wrappers; diff --git a/src-rust/src/structs/internal/locator_header.rs b/src-rust/src/structs/internal/locator_header.rs index 7ba8873..26cc083 100644 --- a/src-rust/src/structs/internal/locator_header.rs +++ b/src-rust/src/structs/internal/locator_header.rs @@ -8,6 +8,7 @@ use crate::utilities::cached::get_sys_info; use crate::utilities::wrappers::Unaligned; use core::alloc::Layout; use core::cell::Cell; +#[cfg(not(all(target_os = "macos", target_arch = "aarch64")))] use core::cmp::min; use core::mem::size_of; use core::ptr::null_mut; @@ -88,6 +89,7 @@ impl LocatorHeader { self.num_items = 0; } + #[cfg(not(all(target_os = "macos", target_arch = "aarch64")))] fn initialize_remaining_space_as_buffers(&mut self, mut remaining_bytes: u32) { let mut num_items = 0u8; unsafe { diff --git a/src-rust/src/structs/private_allocation.rs b/src-rust/src/structs/private_allocation.rs index 5cf8dcd..26fcb5a 100644 --- a/src-rust/src/structs/private_allocation.rs +++ b/src-rust/src/structs/private_allocation.rs @@ -98,6 +98,9 @@ impl PrivateAllocation { unsafe { #[cfg(feature = "external_processes")] { + use crate::internal::buffer_allocator_windows::ProcessHandle; + use windows_sys::Win32::System::Memory::VirtualFreeEx; + if self._this_process_id == get_sys_info().this_process_id { let result = VirtualFree(self.base_address.as_ptr() as *mut c_void, 0, MEM_RELEASE); @@ -107,7 +110,7 @@ impl PrivateAllocation { } else { let process_handle = ProcessHandle::open_process(self._this_process_id); let result = VirtualFreeEx( - process_handle.get_handle(), + process_handle.get_raw_handle(), self.base_address.as_ptr() as *mut c_void, 0, MEM_RELEASE, @@ -165,7 +168,7 @@ impl PrivateAllocation { } /// Frees the allocated memory when the `PrivateAllocation` instance is dropped. - #[cfg(not(any(target_os = "macos", target_os = "windows", target_os = "linux")))] + #[cfg(feature = "mmap-rs")] pub(crate) fn drop_mmap_rs(&mut self) { use mmap_rs_with_map_from_existing::MmapOptions; let _map = unsafe { @@ -193,7 +196,7 @@ impl Drop for PrivateAllocation { return PrivateAllocation::drop_macos(self); // non-hot-path-os - #[cfg(not(any(target_os = "macos", target_os = "windows", target_os = "linux")))] + #[cfg(feature = "mmap-rs")] return PrivateAllocation::drop_mmap_rs(self); } } diff --git a/src-rust/src/utilities/map_parser_utilities.rs b/src-rust/src/utilities/map_parser_utilities.rs index cbfd949..f7d7b53 100644 --- a/src-rust/src/utilities/map_parser_utilities.rs +++ b/src-rust/src/utilities/map_parser_utilities.rs @@ -44,7 +44,6 @@ impl MemoryMapEntryTrait for MemoryMapEntry { /// # Arguments /// /// * `regions` - A slice of MemoryMapEntry that contains the regions. - #[cfg_attr(feature = "size_opt", optimize(size))] pub fn get_free_regions(regions: &[T]) -> Vec { let mut last_end_address: usize = 0;