> module
std::mem
struct layout, raw memory and page allocation
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
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.
Frees memory from alloc_page. Returns Err(mem::Error::Os(message)) if the OS refuses.
Called as mem::from_bytes::<T>(data). Reads a T from the start of data using T's layout.
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.
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.
Like scan, but returns every match: addresses for a pointer, offsets for bytes.
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.
> types
A recoverable memory error: an OS allocation/free/protect operation failed. Match it as `Err(mem::Error::Os(msg))`.
| Error::Os(str) |
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::Executescan 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;
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)
}