plat/plib.rs#
Source: lib/plat/plib.rs
plib -- plugin libraries and the crash guard: the portable part
[src/plat/plib.s; docs/modules/PLUGIN_LOADER.md]
lib_open(path) -> Lib / Err(LIBE_*) Windows: LoadLibraryExW without DllMain / imports
(win_lib.rs); Linux: own ELF ET_DYN loader
(linux_elfload.rs)
lib_sym(h, name) -> address | 0 exported symbol (own export / .dynsym lookup)
lib_close(h) unload (also when a Lib is dropped)
lib_seg(h, addr) -> (end of the image segment holding addr (0 = outside), LIBA_*)
-- check every pointer a plugin hands over (data:
LIBA_R, code: LIBA_X) before dereferencing it
guard_init() install the OS fault handler once (VEH on
Windows, SIGSEGV / SIGBUS / SIGILL / SIGFPE /
SIGTRAP on Linux; linux_guard.rs)
guard_call(f, a1, a2, a3, a4, info) -> (rax, rdx)
calls the code at address f with the plugin ABI (rt::plugin_abi!: Win64 on x86-64) as
f(a1, a2, a3, a4); a fault inside it on this thread (also in nested host code)
resumes here with rax = rdx = ERR_FAULT and info (GuardInfo) filled; otherwise
info.kind = 0 and rax / rdx are those of f. Nested calls are allowed. ERR_LIMIT
when the thread table is full (GT_MAX threads).
guard_tag() -> u64 info.tag of this thread's innermost active guard
(0 = none)
guard_call pushes a guard record (rsp / rbp of its trampoline, xmm6-15, MXCSR, x87
control word, tag) and links it as the innermost guard of the calling thread in a table
{OS thread id -> record} (thread id = TEB ClientId on Windows, gettid on Linux). The
handlers find the thread's entry by its id, unlink the innermost record (a second fault
goes to the enclosing guard), store the fault into it and resume the thread at
jfgui_guard_fault_ip with rsp / rbp of the trampoline's body. That code restores x87,
MXCSR, DF and xmm6-15 and returns through the epilogue, which restores the caller's
callee-saved registers. NOT a sandbox: the guard catches faults, nothing else (see the
docs: corrupt state stays corrupt, hangs are not detected).
Safety contract: guard_call runs untrusted machine code at an arbitrary address; it is a
safe function only in the sense the assembly API is: the caller hands it addresses of
loaded plugin code (lib_sym) or of its own `rt::plugin_abi!` functions. Pointers a plugin
returns are checked with lib_seg before the host reads through them.
ERR_FAULT: i32 = -64 // the guarded call faulted (plugin disabled)
LIBA_R: u32 = 1 // lib_seg flags
LIBA_W: u32 = 2
LIBA_X: u32 = 4
LIBE_IO: u32 = 1 // file missing / unreadable / larger than LIB_FILE_MAX
LIBE_FORMAT: u32 = 2 // not an ELF shared object / PE DLL, or damaged
LIBE_DEPS: u32 = 3 // needs other libraries / undefined symbols
LIBE_RELOC: u32 = 4 // unsupported relocation
LIBE_NOMEM: u32 = 5
LIBE_INIT: u32 = 6 // TLS, initialisers or an interpreter (not supported)
LIBE_MAP: u32 = 7 // mmap / mprotect failed
LIBE_ARCH: u32 = 8 // built for another processor: x86-64 on ARM64 or ARM64 on x86-64 (docs/ARM64.md)
LIB_FILE_MAX: u64 = 64 << 20
LH_SEGMAX: usize = 16
GK_NONE: u32 = 0
GK_ACCESS: u32 = 1 // access violation / SIGSEGV
GK_ILLEGAL: u32 = 2 // illegal / privileged instruction / SIGILL
GK_ARITH: u32 = 3 // division by zero, overflow, FP trap / SIGFPE
GK_BUS: u32 = 4 // misalignment / SIGBUS
GK_BREAK: u32 = 5 // breakpoint (int3) / SIGTRAP
GK_STACK: u32 = 6 // stack overflow (Windows)
GT_MAX: usize = 128
struct LibSegone segment of a loaded image: [start, end), LIBA_*start: usizeend: usizeflags: u32
struct Liba loaded plugin library (LIBH)
impl Lib
fn base(&self) -> usizethe image basefn span(&self) -> usizebytes mapped from the basefn segs(&self) -> &[LibSeg]the segments (Windows: the headers, then every section)fn lib_close(h: Lib)unload a library (dropping the Lib does the same) [plat_lib_close]fn lib_seg(h: &Lib, addr: usize) -> (usize, u32)the end of the segment of h holding addr (0 = none) and its LIBA_* flags [plat_lib_seg]struct GuardInfoGUARDINFO: what a guarded call hit; tag is an input (guard_tag)kind: u32-- GK_* (0 = no fault)code: u32-- NTSTATUS (Windows) / signal number (Linux)pc: u64-- faulting instructionaddr: u64-- faulting data address (access faults) or 0tag: u64-- in: tag of this guard (guard_tag)
fn guard_init() -> i32install the fault handler (idempotent, thread safe) [plat_guard_init]fn guard_tag() -> u64info.tag of the innermost active guard of this thread (0 = none) [plat_guard_tag]fn guard_call(f: usize, a1: u64, a2: u64, a3: u64, a4: u64, info: Option<&mut GuardInfo>) -> (u64, u64)f(a1, a2, a3, a4) under the crash guard (see the file header) [plat_guard_call]fn guard_run<R, F: FnOnce() -> R>(f: F, info: Option<&mut GuardInfo>) -> Result<R, i32>run the Rust closure f under the crash guard -> Ok(result), Err(ERR_FAULT) when it faulted (info filled), Err(ERR_LIMIT) when the thread table is full (f did not run). A fault abandons f's frames without running destructors (what they own leaks): meant for host code that may touch plugin memory (the loader fuzz runs lib_open / lib_sym under it).fn guard_read(addr: usize, len: usize) -> Option<alloc::vec::Vec<u8>>copy len bytes at any address under the crash guard -> None when addr is 0, the copy faulted or the memory for the copy is missing (pn-sys)fn guard_write(addr: usize, data: &[u8]) -> boolwrite data at any address under the crash guard -> false when addr is 0 or it faulted (pn-sys)fn lib_read(h: &Lib, addr: usize, len: usize) -> Option<alloc::vec::Vec<u8>>copy len bytes at addr of the image of h -- only when they lie in one readable segment (lib_seg), else None (pointers a plugin hands over)fn guard_test_code(k: u32) -> usize(test aid) the address of guard test function k (see above), 0 for an unknown kfn guard_test_state() -> (u32, u32, bool)(test aid) this thread's MXCSR, x87 control word and direction flag