Skip to content

Commit

Permalink
Remaining work for migration to no_std
Browse files Browse the repository at this point in the history
  • Loading branch information
Sewer56 committed Nov 28, 2023
1 parent 8dac996 commit 6fa716c
Show file tree
Hide file tree
Showing 22 changed files with 132 additions and 107 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ You can specify another process with `TargetProcess = someProcess` in `BufferAll

## Crate Features (Rust)

- `std`: [Enabled by Default] Enables use of standard library.
- `external_processes`: Support external processes (windows only).
- `no_format`: Disables formatting code in errors, saving ~8kB of space.
- `size_opt`: Makes cold paths optimized for size instead of optimized for speed. [Requires 'nightly' Rust]
Expand Down
2 changes: 2 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,8 @@ Alternative overload also allows you to pass a 'context' variable.

### Crate Features (Rust)

- `std`: [Enabled by Default] Enables use of standard library.
- `external_processes`: Support external processes (windows only).
- `no_format`: Disables formatting code in errors, saving ~8kB of space.
- `size_opt`: Makes cold paths optimized for size instead of optimized for speed. [Requires 'nightly' Rust]
- `c_exports` Provides C exports for the library.
Expand Down
1 change: 0 additions & 1 deletion src-rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion src-rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ license = "GPL-3.0"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[features]
default = ["std"]
std = []
external_processes = []
c_exports = []
no_format = []
Expand All @@ -19,7 +21,6 @@ nightly = [] # Optimizations for nightly builds.

[dependencies]
memoffset = "0.9.0"
lazy_static = "1.4.0"
dirs = "5.0.1"
errno = "0.3.3"
spin = "0.9.8"
Expand Down
24 changes: 12 additions & 12 deletions src-rust/src/buffers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ mod tests {
use crate::{
internal::locator_header_finder::LocatorHeaderFinder,
structs::params::{BufferAllocatorSettings, BufferSearchSettings},
utilities::cached::CACHED,
utilities::cached::get_sys_info,
};
use std;

Expand All @@ -253,8 +253,8 @@ mod tests {
#[test]
fn allocate_private_memory_up_to_max_address() {
let mut settings = BufferAllocatorSettings::new();
settings.min_address = CACHED.max_address / 2;
settings.max_address = CACHED.max_address;
settings.min_address = get_sys_info().max_address / 2;
settings.max_address = get_sys_info().max_address;

let result = Buffers::allocate_private_memory(&mut settings);
assert!(result.is_ok());
Expand All @@ -267,8 +267,8 @@ mod tests {
#[test]
fn get_buffer_baseline() {
let settings = BufferSearchSettings {
min_address: (CACHED.max_address / 2),
max_address: CACHED.max_address,
min_address: (get_sys_info().max_address / 2),
max_address: get_sys_info().max_address,
size: 4096,
};

Expand All @@ -286,8 +286,8 @@ mod tests {
#[test]
fn memory_is_executable_x64() {
let settings = BufferSearchSettings {
min_address: (CACHED.max_address / 2),
max_address: CACHED.max_address,
min_address: (get_sys_info().max_address / 2),
max_address: get_sys_info().max_address,
size: 4096,
};

Expand Down Expand Up @@ -317,8 +317,8 @@ mod tests {
#[test]
fn memory_is_executable_aarch64() {
let settings = BufferSearchSettings {
min_address: (CACHED.max_address / 2),
max_address: CACHED.max_address,
min_address: (get_sys_info().max_address / 2),
max_address: get_sys_info().max_address,
size: 4096,
};

Expand Down Expand Up @@ -352,8 +352,8 @@ mod tests {
#[case(256)]
fn get_buffer_aligned_test(#[case] alignment: u32) {
let settings = BufferSearchSettings {
min_address: (CACHED.max_address / 2),
max_address: CACHED.max_address,
min_address: (get_sys_info().max_address / 2),
max_address: get_sys_info().max_address,
size: 4096,
};

Expand Down Expand Up @@ -382,7 +382,7 @@ mod tests {
#[test]
fn get_buffer_with_proximity() {
const SIZE: usize = 4096;
let base_address = CACHED.max_address - (std::i32::MAX as usize);
let base_address = get_sys_info().max_address - (std::i32::MAX as usize);

unsafe {
LocatorHeaderFinder::reset();
Expand Down
16 changes: 8 additions & 8 deletions src-rust/src/c/buffers_c_buffers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ mod tests {
use crate::{
internal::locator_header_finder::LocatorHeaderFinder,
structs::params::{BufferAllocatorSettings, BufferSearchSettings},
utilities::cached::CACHED,
utilities::cached::get_sys_info,
};
use rstest::rstest;
use std;
Expand All @@ -366,8 +366,8 @@ mod tests {
#[test]
fn allocate_private_memory_up_to_max_address() {
let mut settings = BufferAllocatorSettings::new();
settings.min_address = CACHED.max_address / 2;
settings.max_address = CACHED.max_address;
settings.min_address = get_sys_info().max_address / 2;
settings.max_address = get_sys_info().max_address;

let result = buffers_allocate_private_memory(&mut settings);
assert!(result.is_ok);
Expand All @@ -380,8 +380,8 @@ mod tests {
#[test]
fn get_buffer_baseline() {
let settings = BufferSearchSettings {
min_address: (CACHED.max_address / 2),
max_address: CACHED.max_address,
min_address: (get_sys_info().max_address / 2),
max_address: get_sys_info().max_address,
size: 4096,
};

Expand All @@ -403,8 +403,8 @@ mod tests {
#[case(256)]
fn get_buffer_aligned_test(#[case] alignment: u32) {
let settings = BufferSearchSettings {
min_address: (CACHED.max_address / 2),
max_address: CACHED.max_address,
min_address: (get_sys_info().max_address / 2),
max_address: get_sys_info().max_address,
size: 4096,
};

Expand All @@ -431,7 +431,7 @@ mod tests {
#[test]
fn get_buffer_with_proximity() {
const SIZE: usize = 4096;
let base_address = CACHED.max_address - (std::i32::MAX as usize);
let base_address = get_sys_info().max_address - (std::i32::MAX as usize);

unsafe {
LocatorHeaderFinder::reset();
Expand Down
10 changes: 5 additions & 5 deletions src-rust/src/internal/buffer_allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ mod tests {
use super::*;
#[cfg(target_os = "windows")]
use crate::internal::buffer_allocator_windows::{Kernel32, LocalKernel32};
use crate::utilities::cached::CACHED;
use crate::utilities::cached::get_sys_info;
use std::ffi::c_void;

const ALLOCATION_GRANULARITY: usize = 65536; // Assuming 64KB Allocation Granularity
Expand Down Expand Up @@ -298,7 +298,7 @@ mod tests {
min_address: 0,
max_address: i32::MAX as usize,
size: 4096,
target_process_id: CACHED.this_process_id,
target_process_id: get_sys_info().this_process_id,
retry_count: 8,
brute_force: false,
};
Expand All @@ -313,10 +313,10 @@ mod tests {
#[test]
fn can_allocate_up_to_max_address() {
let mut settings = BufferAllocatorSettings {
min_address: CACHED.max_address / 2,
max_address: CACHED.max_address,
min_address: get_sys_info().max_address / 2,
max_address: get_sys_info().max_address,
size: 4096,
target_process_id: CACHED.this_process_id,
target_process_id: get_sys_info().this_process_id,
retry_count: 8,
brute_force: false,
};
Expand Down
4 changes: 2 additions & 2 deletions src-rust/src/internal/buffer_allocator_linux.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::structs::errors::BufferAllocationError;
use crate::structs::internal::LocatorItem;
use crate::structs::params::BufferAllocatorSettings;
use crate::utilities::cached::CACHED;
use crate::utilities::cached::get_sys_info;
use crate::utilities::linux_map_parser::get_free_regions_from_process_id;
use crate::{
internal::buffer_allocator::get_possible_buffer_addresses,
Expand Down Expand Up @@ -49,7 +49,7 @@ unsafe fn try_allocate_buffer(
entry.start_address,
entry.end_address,
settings.size as usize,
CACHED.allocation_granularity as usize,
get_sys_info().allocation_granularity as usize,
buffer,
) {
let allocated = mmap(
Expand Down
4 changes: 2 additions & 2 deletions src-rust/src/internal/buffer_allocator_mmap_rs.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::structs::errors::BufferAllocationError;
use crate::structs::internal::LocatorItem;
use crate::structs::params::BufferAllocatorSettings;
use crate::utilities::cached::CACHED;
use crate::utilities::cached::get_sys_info;
use crate::utilities::map_parser_utilities::get_free_regions;
use crate::{
internal::buffer_allocator::get_possible_buffer_addresses,
Expand Down Expand Up @@ -68,7 +68,7 @@ unsafe fn try_allocate_buffer(
entry.start_address,
entry.end_address,
settings.size as usize,
CACHED.get_allocation_granularity() as usize,
get_sys_info().get_allocation_granularity() as usize,
buffer,
) {
let mmapoptions = MmapOptions::new(settings.size as usize)
Expand Down
6 changes: 3 additions & 3 deletions src-rust/src/internal/buffer_allocator_osx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::internal::buffer_allocator::get_possible_buffer_addresses;
use crate::structs::errors::BufferAllocationError;
use crate::structs::internal::LocatorItem;
use crate::structs::params::BufferAllocatorSettings;
use crate::utilities::cached::CACHED;
use crate::utilities::cached::get_sys_info;
use crate::utilities::wrappers::Unaligned;
use core::cmp::min;
use core::mem;
Expand All @@ -17,7 +17,7 @@ use mach::vm_types::mach_vm_address_t;
pub fn allocate_osx(
settings: &BufferAllocatorSettings,
) -> Result<LocatorItem, BufferAllocationError> {
let max_address = min(CACHED.max_address, settings.max_address);
let max_address = min(get_sys_info().max_address, settings.max_address);
let min_address = settings.min_address;

unsafe {
Expand Down Expand Up @@ -179,7 +179,7 @@ fn get_buffer_pointers_in_page_range(
) -> &[usize] {
let page_start = page_address;
let page_end = page_address + page_size;
let allocation_granularity = CACHED.allocation_granularity;
let allocation_granularity = get_sys_info().allocation_granularity;
unsafe {
get_possible_buffer_addresses(
minimum_ptr,
Expand Down
12 changes: 6 additions & 6 deletions src-rust/src/internal/buffer_allocator_windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::internal::buffer_allocator::get_possible_buffer_addresses;
use crate::structs::errors::BufferAllocationError;
use crate::structs::internal::LocatorItem;
use crate::structs::params::BufferAllocatorSettings;
use crate::utilities::cached::CACHED;
use crate::utilities::cached::get_sys_info;
use crate::utilities::mathematics::min;
use crate::utilities::wrappers::Unaligned;
use core::ffi::c_void;
Expand Down Expand Up @@ -127,11 +127,11 @@ impl Drop for ProcessHandle {
// Helpers //

fn get_max_windows_address(process_id: u32, handle: HANDLE) -> usize {
if CACHED.this_process_id == process_id {
if get_sys_info().this_process_id == process_id {
// Note: In WOW64 mode, the following rules apply:
// - If current process is large address aware, this will return 0xFFFEFFFF.
// - If it is not LAA, this should return 0x7FFEFFFF.
return CACHED.max_address;
return get_sys_info().max_address;
}

unsafe {
Expand Down Expand Up @@ -172,7 +172,7 @@ pub fn allocate_windows(

#[cfg(feature = "external_process")]
{
return if CACHED.this_process_id == settings.target_process_id {
return 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)
Expand Down Expand Up @@ -229,7 +229,7 @@ fn allocate_fast<T: Kernel32>(
match try_allocate_buffer(k32, &mut memory_information, settings) {
Some(item) => return Ok(item),
None => {
current_address += CACHED.allocation_granularity as usize;
current_address += get_sys_info().allocation_granularity as usize;
}
};
}
Expand Down Expand Up @@ -291,7 +291,7 @@ fn get_buffer_pointers_in_page_range<'a>(
) -> &'a [usize] {
let page_start = page_info.BaseAddress as usize;
let page_end = page_info.BaseAddress as usize + page_info.RegionSize;
let allocation_granularity = CACHED.allocation_granularity;
let allocation_granularity = get_sys_info().allocation_granularity;

unsafe {
get_possible_buffer_addresses(
Expand Down
17 changes: 7 additions & 10 deletions src-rust/src/internal/locator_header_finder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ use core::ptr::null_mut;

use crate::internal::memory_mapped_file::MemoryMappedFile;
use crate::structs::internal::LocatorHeader;
use crate::utilities::cached::CACHED;
use crate::utilities::cached::get_sys_info;
use alloc::boxed::Box;
use alloc::string::String;
use alloc::string::ToString;
use lazy_static::lazy_static;
use spin::Mutex;

#[cfg(unix)]
Expand All @@ -23,9 +22,7 @@ pub struct LocatorHeaderFinder {}

static mut LOCATOR_HEADER_ADDRESS: *mut LocatorHeader = null_mut();
static mut MMF: Option<Box<dyn MemoryMappedFile>> = None;
lazy_static! {
static ref GLOBAL_LOCK: Mutex<()> = Mutex::new(());
}
static GLOBAL_LOCK: Mutex<()> = Mutex::new(());

/// The reason the variable was last found.
#[cfg(test)]
Expand Down Expand Up @@ -55,18 +52,18 @@ impl LocatorHeaderFinder {
fn open_or_create_memory_mapped_file() -> Box<dyn MemoryMappedFile> {
// no_std
let mut name = String::from("/Reloaded.Memory.Buffers.MemoryBuffer, PID ");
name.push_str(&CACHED.this_process_id.to_string());
name.push_str(&get_sys_info().this_process_id.to_string());

#[cfg(target_os = "windows")]
return Box::new(WindowsMemoryMappedFile::new(
&name,
CACHED.allocation_granularity as usize,
get_sys_info().allocation_granularity as usize,
));

#[cfg(unix)]
return Box::new(UnixMemoryMappedFile::new(
&name,
CACHED.allocation_granularity as usize,
get_sys_info().allocation_granularity as usize,
));
}

Expand Down Expand Up @@ -202,7 +199,7 @@ mod tests {
use crate::internal::locator_header_finder::LAST_FIND_REASON;
use crate::structs::internal::locator_header::LENGTH_OF_PREALLOCATED_CHUNKS;
use crate::structs::internal::LocatorHeader;
use crate::utilities::cached::CACHED;
use crate::utilities::cached::get_sys_info;

#[test]
#[cfg(not(target_os = "android"))]
Expand Down Expand Up @@ -261,7 +258,7 @@ mod tests {
assert!(!address.is_null());

let header = &*address;
let expected_num_items = ((CACHED.allocation_granularity
let expected_num_items = ((get_sys_info().allocation_granularity
- std::mem::size_of::<LocatorHeader>() as i32)
as f64
/ LENGTH_OF_PREALLOCATED_CHUNKS as f64)
Expand Down
Loading

0 comments on commit 6fa716c

Please sign in to comment.