xur_

> module

std::mem

struct layout, raw memory and page allocation

use std::mem; in the browser

std::mem provides size and alignment queries, binary conversion for structs with a layout (see xur.std.reflect(7)), raw reads and writes of process memory, byte pattern scanning, and memory page allocation for assembling and executing machine code at runtime.

The raw memory functions trust their arguments: a bad address or size crashes the process instead of raising an error.

> functions

# align_of() -> i64

The alignment in bytes of the type argument, called as mem::align_of::<T>().

# alloc_page(size: usize) -> ptr

Allocates size bytes of zeroed read-write memory and returns its address. It uses mmap on Unix and VirtualAlloc on Windows. Failure is a runtime error.

# free_page(addr: ptr, size: usize) -> Result<(), Error>

Frees memory from alloc_page. Returns Err(mem::Error::Os(message)) if the OS refuses.

# from_bytes(data: bytes) -> T

Called as mem::from_bytes::<T>(data). Reads a T from the start of data using T's layout.

# protect(addr: ptr, size: usize, mode: Protection | i64 | str)

Changes the protection of the pages at addr. mode is a Protection value, an integer of Protection bits, or one of the strings "r", "rw", "rx" and "rwx". Failure is a runtime error.

# read_bytes(addr: ptr, len: usize) -> bytes

Copies len bytes from memory at addr.

# scan(addr: ptr | bytes, size: usize, pattern: str) -> Option<ptr | i64>

Finds the first match of an IDA-style byte pattern such as "48 8B ?? 05", where ?? matches any byte (the compact form "488b??05" also works). Called with a pointer and size it searches that memory and returns Some with the address of the match; called as scan(data, pattern) on bytes it returns Some with the offset. Returns None if there is no match.

# scan_all(addr: ptr | bytes, size: usize, pattern: str) -> list<ptr | i64>

Like scan, but returns every match: addresses for a pointer, offsets for bytes.

# size_of() -> i64

The size in bytes of the type argument, called as mem::size_of::<T>(). T must be a type with a layout, such as a struct.

# size_of_val(value: any) -> i64

The size in bytes of a struct value's type.

# to_bytes(value: any) -> bytes

Encodes a struct value as bytes using its type's layout.

# write_bytes(addr: ptr, data: bytes)

Copies data into memory at addr.

> types

┌ enum Error

A recoverable memory error: an OS allocation/free/protect operation failed. Match it as `Err(mem::Error::Os(msg))`.

Error::Os(str)
┌ struct Protection

An opaque value; see the functions that return it.

> struct methods

Every struct and enum value also has these methods. The layout used by to_bytes and byte_size is the type's layout from xur.std.reflect(7): fields in order with C alignment and padding unless attributes say otherwise. A field whose type has no layout, such as a str, raises a runtime error.

> protection

mem::Protection is a #[repr(i64)] flags enum: Read (1), Write (2) and Execute (4). Combine them with |:

mem::Protection::Read | mem::Protection::Execute

scan and scan_all need the re-demangle feature; the memory functions are missing from the browser build.

> examples

use std::assert;
use std::io;
use std::mem;

#[repr(packed)]
#[endian(Big)]
struct Header {
    magic: u16
    version: u8
    flags: u8
    length: u32
}

fn main() {
    io::println("size: {}", mem::size_of::<Header>());   // 8
    let h = Header { magic: 0xCAFE, version: 1, flags: 0x80, length: 256 };
    let data = mem::to_bytes(h);
    io::println("bytes: {}", data.to_hex());
    let back = mem::from_bytes::<Header>(data) as Header;
    assert::is_eq(h, back);
}
use std::bytes;
use std::io;
use std::mem;

fn main() {
    let page = mem::alloc_page(4096);
    mem::write_bytes(page.offset(100), bytes::from_hex("48 8B C1 05").unwrap());
    let found = mem::scan(page, 4096, "48 8B ?? 05").unwrap();
    io::println("match at offset {}", found.to_int() - page.to_int());  // 100
    mem::free_page(page, 4096);

    let buf = bytes::from_hex("00 00 48 8B AA 05").unwrap();
    io::println("{}", mem::scan(buf, "48 8B ?? 05"));   // Some(2)
}