This crate provides a cross-platform Rust API for allocating, querying and
manipulating virtual memory. It is a thin abstraction over platform APIs such as
VirtualQuery/VirtualAlloc/VirtualLock on Windows and
mprotect/mmap/mlock (and friends) on Unix-like systems.
Continuously tested against:
- Linux (
gnu/musl) - Windows (
gnu/msvc) - macOS
- FreeBSD
- OpenBSD
- NetBSD
Also checked / supported where practical:
- Android (compile-checked; full cross tests currently blocked by toolchain linking)
- Illumos
- GNU/Hurd (compile-checked via nightly
-Zbuild-std=core,alloc; no prebuiltrust-std) - Redox (allocation/protection; region querying is not yet available)
[dependencies]
region = "4.0.0"The default feature set includes std. Disable it to use the crate as
#![no_std] + alloc:
region = { version = "4.0.0", default-features = false }use region::Protection;
let data = [0xDE, 0xAD, 0xBE, 0xEF];
// Page size
let pz = region::page::size();
// VirtualQuery | '/proc/self/maps'
let q = region::query(data.as_ptr())?;
let qr = region::query_range(data.as_ptr(), data.len())?;
// VirtualAlloc | mmap
let alloc = region::alloc(100, Protection::READ_WRITE)?;
// VirtualProtect | mprotect
unsafe {
region::protect(data.as_ptr(), data.len(), Protection::READ_WRITE_EXECUTE)?;
}
// Temporarily change one or more pages' protection
let handle = unsafe {
region::protect_with_handle(data.as_ptr(), data.len(), Protection::READ_WRITE_EXECUTE)?
};
// VirtualLock | mlock
let guard = region::lock(data.as_ptr(), data.len())?;- This is a major release (
4.0.0): Rust edition is2024, MSRV is1.85, and a few APIs/error types changed forno_stdfriendliness and safer locking semantics. bitflagsis now2.x. PreferProtection::from_bits_retainover the deprecatedfrom_bits_unchecked.windows-sysaccepts>=0.52, <=0.61so dependents can unify versions.