Crate region

source ·
Expand description

A library for manipulating memory regions

This crate provides several functions for handling memory pages and regions. It is implemented using platform specific APIs. The library exposes both low and high level functionality for manipulating pages.

Not all OS specific quirks are abstracted away. For instance; some OSs enforce memory pages to be readable whilst other may prevent pages from becoming executable (i.e DEP).

Note: a region is a collection of one or more pages laying consecutively in memory, with the same properties.

Installation

This crate is on crates.io and can be used by adding region to your dependencies in your project’s Cargo.toml.

[dependencies]
region = "2.2.0"

and this to your crate root:

extern crate region;

Examples

  • Cross-platform equivalents.

    let data = [0xDE, 0xAD, 0xBE, 0xEF];
    
    // Page size
    let pz = region::page::size();
    let pc = region::page::ceil(1234);
    let pf = region::page::floor(1234);
    
    // VirtualQuery | '/proc/self/maps'
    let q  = region::query(data.as_ptr())?;
    let qr = region::query_range(data.as_ptr(), data.len())?;
    
    // VirtualProtect | mprotect
    region::protect(data.as_ptr(), data.len(), Protection::READ_WRITE_EXECUTE)?;
    
    // ... you can also temporarily change a region's protection
    let handle = region::protect_with_handle(data.as_ptr(), data.len(), Protection::READ_WRITE_EXECUTE)?;
    
    // VirtualLock | mlock
    let guard = region::lock(data.as_ptr(), data.len())?;

Modules

Page related functions.

Structs

An RAII implementation of a “scoped lock”. When this structure is dropped (falls out of scope), the virtual lock will be unlocked.
An RAII implementation of “scoped protection”. When this structure is dropped (falls out of scope), the memory region protection will be reset.
Memory page protection constants.
A descriptor for a memory region

Enums

A collection of possible errors.

Functions

Locks one or more memory regions to RAM.
Changes the memory protection of one or more pages.
Changes the memory protection of one or more pages temporarily.
Queries the OS with an address, returning the region it resides within.
Queries the OS with a range, returning the regions it contains.
Unlocks one or more memory regions from RAM.

Type Definitions

The result type used by this library.