Skip to content

Commit

Permalink
Fix: A lot of compile warnings on various platforms.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sewer56 committed Jun 24, 2024
1 parent f04fd9e commit 366ca52
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 17 deletions.
1 change: 1 addition & 0 deletions src-rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
11 changes: 11 additions & 0 deletions src-rust/build.rs
Original file line number Diff line number Diff line change
@@ -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\"");
}
}
3 changes: 1 addition & 2 deletions src-rust/src/buffers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,6 @@ mod tests {
structs::params::{BufferAllocatorSettings, BufferSearchSettings},
utilities::cached::get_sys_info,
};
use std;

#[cfg(not(target_os = "macos"))]
#[test]
Expand Down Expand Up @@ -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,
));
Expand Down
2 changes: 1 addition & 1 deletion src-rust/src/internal/buffer_allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
26 changes: 19 additions & 7 deletions src-rust/src/internal/buffer_allocator_windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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
}
}
}

Expand All @@ -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 {
Expand Down Expand Up @@ -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"))]
Expand Down
4 changes: 2 additions & 2 deletions src-rust/src/internal/memory_mapped_file_windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
3 changes: 3 additions & 0 deletions src-rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 2 additions & 0 deletions src-rust/src/structs/internal/locator_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
9 changes: 6 additions & 3 deletions src-rust/src/structs/private_allocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src-rust/src/utilities/disable_write_xor_execute.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// An utility to disable write xor execute protection on a memory region.
// This method contains the code to disable W^X on platforms where it's enforced.

#[cfg(target_os = "macos")]
#[cfg(all(target_os = "macos", target_arch = "aarch64"))]
use {
libc::mach_task_self, mach::vm::mach_vm_protect, mach::vm_prot::VM_PROT_EXECUTE,
mach::vm_prot::VM_PROT_READ, mach::vm_prot::VM_PROT_WRITE, mach::vm_types::mach_vm_size_t,
Expand Down
1 change: 0 additions & 1 deletion src-rust/src/utilities/map_parser_utilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T: MemoryMapEntryTrait>(regions: &[T]) -> Vec<MemoryMapEntry> {
let mut last_end_address: usize = 0;
Expand Down

0 comments on commit 366ca52

Please sign in to comment.