About
Reloaded.Memory.Buffers
is a library for allocating memory between a given minimum and maximum memory address, for C# and Rust
With the following properties:
- Memory Efficient: No wasted memory.
- Shared: Can be found and read/written to by multiple users.
- Static: Allocated data never moves, or is overwritten.
- Permanent: Allocated data lasts the lifetime of the process.
- Concurrent: Multiple users can access at the same time.
- Large Address Aware: On Windows, the library can correctly leverage all 4GB in 32-bit processes.
- Cross Platform: Supports Windows, OSX and Linux.
Note: Rust/C port also works with FreeBSD (untested), and has partial (limited) Android support.
Example Use Cases
These are just examples.
- Hooks: Hooking libraries like Reloaded.Hooks can reduce amount of bytes stolen from functions.
- Libraries: Libraries like Reloaded.Assembler require memory be allocated in first 2GB for x64 FASM.
Usage
The library provides a simple high level API to use.
Both C# and Rust ports expose the same APIs.
Get A Buffer
Gets a buffer where you can allocate 4096 bytes in first 2GiB of address space.
var settings = new BufferSearchSettings()
{
MinAddress = 0,
MaxAddress = int.MaxValue,
Size = 4096
};
// Make sure to dispose, so lock gets released.
using var item = Buffers.GetBuffer(settings);
// Write some data, get pointer back.
var ptr = item->Append(data);
let settings = BufferSearchSettings {
min_address: 0 as usize,
max_address: i32::MAX as usize,
size: 4096,
};
// Automatically dropped.
let item = Buffers::get_buffer(&settings)?;
// Append some data.
unsafe {
item.append_bytes(data);
}
BufferSearchSettings settings;
settings.MinAddress = 0;
settings.MaxAddress = INT_MAX;
settings.Size = 4096;
// Automatically dropped.
GetBufferResult result = buffers_get_buffer(&settings);
// Append some data.
unsigned char data[4096] = {0}; // some data from heap, or something :wink:
if (result.IsOk) {
locatoritem_append_bytes(result.Ok, data, sizeof(data));
free_get_buffer_result(result);
}
Get A Buffer (With Proximity)
Gets a buffer where 4096 bytes written will be within 2GiB of 0x140000000.
var settings = BufferSearchSettings.FromProximity(int.MaxValue, (nuint)0x140000000, 4096);
// Make sure to dispose, so lock gets released.
using var item = Buffers.GetBuffer(settings);
// Write some data, get pointer back.
var ptr = item->Append(data);
let settings = BufferSearchSettings::from_proximity(i32::MAX, 0x140000000 as usize, 4096);
// Automatically dropped.
let item = Buffers::get_buffer(settings)?;
// Append some data.
unsafe {
item?.append_bytes(data);
}
// Get the buffer
BufferSearchSettings settings = buffersearchsettings_from_proximity(INT32_MAX, base_address, SIZE);
GetBufferResult result = buffers_get_buffer(&settings);
// Append some data.
locatoritem_append_bytes(result.Ok, &data[0], data.Length);
// Dispose
free_get_buffer_result(result);
Use append_code
instead of append_bytes
if you need to add executable code. (Currently unavailable in C# port)
Allocate Memory
Allows you to temporarily allocate memory within a specific address range and size constraints.
// Arrange
var settings = new BufferAllocatorSettings()
{
MinAddress = 0,
MaxAddress = int.MaxValue,
Size = 4096
};
using var item = Buffers.AllocatePrivateMemory(settings);
// You have allocated memory in first 2GiB of address space.
// Disposing this memory (via `using` statement) will free it.
item.BaseAddress.Should().NotBeNull();
item.Size.Should().BeGreaterOrEqualTo(settings.Size);
let mut settings = BufferAllocatorSettings::new();
settings.min_address = 0;
settings.max_address = i32::MAX as usize;
settings.size = 4096;
let item = Buffers::allocate_private_memory(&mut settings).unwrap();
// You have allocated memory in first 2GiB of address space.
assert!(item.base_address.as_ptr() != std::ptr::null_mut());
assert!(item.size >= settings.size as usize);
BufferSearchSettings settings;
settings.MinAddress = 0;
settings.MaxAddress = INT_MAX;
settings.Size = 4096;
AllocationResult item = allocate_private_memory(&mut settings);
// You have allocated memory in first 2GiB of address space.
You can specify another process with TargetProcess = someProcess
in BufferAllocatorSettings
, but this is only supported on Windows.
Overwriting Allocated Instructions
On non-x86 architectures, some extra actions may be needed when overwriting executable code allocated with append_code
.
This involves clearing instruction cache, and abiding by Write XOR Execute restrictions.
Self::overwrite_allocated_code(source, target, size);
Self::overwrite_allocated_code_ex(source, target, size, |src, tgt, sz| {
// Do stuff with executable code
});
void do_stuff_with_executable_code(char* source, char* target, size_t size) {
// Modify executable code in buffer
}
overwrite_allocated_code(source, target, size);
overwrite_allocated_code_ex(source, target, size, do_stuff_with_executable_code);
Alternative overload also allows you to pass a 'context' variable.
Not currently available in C# version. Submit an issue request or PR if you need this.
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.
Community Feedback
If you have questions/bug reports/etc. feel free to Open an Issue.
Contributions are welcome and encouraged. Feel free to implement new features, make bug fixes or suggestions so long as they meet the quality standards set by the existing code in the repository.
For an idea as to how things are set up, see Reloaded Project Configurations.
Happy Hacking 💜