Skip to main content

plat/
plib.rs

1// plib -- plugin libraries and the crash guard: the portable part
2//                                        [src/plat/plib.s; docs/modules/PLUGIN_LOADER.md]
3//
4//   lib_open(path) -> Lib / Err(LIBE_*)    Windows: LoadLibraryExW without DllMain / imports
5//                                          (win_lib.rs); Linux: own ELF ET_DYN loader
6//                                          (linux_elfload.rs)
7//   lib_sym(h, name) -> address | 0        exported symbol (own export / .dynsym lookup)
8//   lib_close(h)                           unload (also when a Lib is dropped)
9//   lib_seg(h, addr) -> (end of the image segment holding addr (0 = outside), LIBA_*)
10//                                          -- check every pointer a plugin hands over (data:
11//                                          LIBA_R, code: LIBA_X) before dereferencing it
12//   guard_init()                           install the OS fault handler once (VEH on
13//                                          Windows, SIGSEGV / SIGBUS / SIGILL / SIGFPE /
14//                                          SIGTRAP on Linux; linux_guard.rs)
15//   guard_call(f, a1, a2, a3, a4, info) -> (rax, rdx)
16//        calls the code at address f with the plugin ABI (rt::plugin_abi!: Win64 on x86-64) as
17//        f(a1, a2, a3, a4); a fault inside it on this thread (also in nested host code)
18//        resumes here with rax = rdx = ERR_FAULT and info (GuardInfo) filled; otherwise
19//        info.kind = 0 and rax / rdx are those of f.  Nested calls are allowed.  ERR_LIMIT
20//        when the thread table is full (GT_MAX threads).
21//   guard_tag() -> u64                     info.tag of this thread's innermost active guard
22//                                          (0 = none)
23//
24// guard_call pushes a guard record (rsp / rbp of its trampoline, xmm6-15, MXCSR, x87
25// control word, tag) and links it as the innermost guard of the calling thread in a table
26// {OS thread id -> record} (thread id = TEB ClientId on Windows, gettid on Linux).  The
27// handlers find the thread's entry by its id, unlink the innermost record (a second fault
28// goes to the enclosing guard), store the fault into it and resume the thread at
29// jfgui_guard_fault_ip with rsp / rbp of the trampoline's body.  That code restores x87,
30// MXCSR, DF and xmm6-15 and returns through the epilogue, which restores the caller's
31// callee-saved registers.  NOT a sandbox: the guard catches faults, nothing else (see the
32// docs: corrupt state stays corrupt, hangs are not detected).
33//
34// Safety contract: guard_call runs untrusted machine code at an arbitrary address; it is a
35// safe function only in the sense the assembly API is: the caller hands it addresses of
36// loaded plugin code (lib_sym) or of its own `rt::plugin_abi!` functions.  Pointers a plugin
37// returns are checked with lib_seg before the host reads through them.
38
39use core::sync::atomic::{AtomicU32, AtomicU64, Ordering};
40
41pub const ERR_FAULT: i32 = -64; // the guarded call faulted (plugin disabled)
42
43pub const LIBA_R: u32 = 1; // lib_seg flags
44pub const LIBA_W: u32 = 2;
45pub const LIBA_X: u32 = 4;
46
47pub const LIBE_IO: u32 = 1; // file missing / unreadable / larger than LIB_FILE_MAX
48pub const LIBE_FORMAT: u32 = 2; // not an ELF shared object / PE DLL, or damaged
49pub const LIBE_DEPS: u32 = 3; // needs other libraries / undefined symbols
50pub const LIBE_RELOC: u32 = 4; // unsupported relocation
51pub const LIBE_NOMEM: u32 = 5;
52pub const LIBE_INIT: u32 = 6; // TLS, initialisers or an interpreter (not supported)
53pub const LIBE_MAP: u32 = 7; // mmap / mprotect failed
54pub const LIBE_ARCH: u32 = 8; // built for another processor: x86-64 on ARM64 or ARM64 on x86-64 (docs/ARM64.md)
55pub const LIB_FILE_MAX: u64 = 64 << 20;
56pub const LH_SEGMAX: usize = 16;
57
58pub const GK_NONE: u32 = 0;
59pub const GK_ACCESS: u32 = 1; // access violation / SIGSEGV
60pub const GK_ILLEGAL: u32 = 2; // illegal / privileged instruction / SIGILL
61pub const GK_ARITH: u32 = 3; // division by zero, overflow, FP trap / SIGFPE
62pub const GK_BUS: u32 = 4; // misalignment / SIGBUS
63pub const GK_BREAK: u32 = 5; // breakpoint (int3) / SIGTRAP
64pub const GK_STACK: u32 = 6; // stack overflow (Windows)
65pub const GT_MAX: usize = 128;
66
67/// one segment of a loaded image: [start, end), LIBA_*
68#[derive(Clone, Copy, Default, Debug)]
69pub struct LibSeg {
70    pub start: usize,
71    pub end: usize,
72    pub flags: u32,
73}
74
75/// a loaded plugin library (LIBH)
76pub struct Lib {
77    pub(crate) base: usize,
78    pub(crate) span: usize,
79    pub(crate) os: usize, // HMODULE (Windows) / 0
80    pub(crate) nseg: u32,
81    #[allow(dead_code)]
82    pub(crate) nsym: u32, // .dynsym entries (Linux)
83    pub(crate) symtab: usize, // .dynsym (Linux) / export directory RVA (Windows)
84    #[allow(dead_code)]
85    pub(crate) strtab: usize, // .dynstr (Linux)
86    pub(crate) strsz: usize, // its size / the export directory's size (Windows)
87    pub(crate) segs: [LibSeg; LH_SEGMAX],
88}
89
90impl Lib {
91    pub(crate) fn empty() -> Lib {
92        Lib { base: 0, span: 0, os: 0, nseg: 0, nsym: 0, symtab: 0, strtab: 0, strsz: 0, segs: [LibSeg::default(); LH_SEGMAX] }
93    }
94
95    /// the image base
96    pub fn base(&self) -> usize {
97        self.base
98    }
99
100    /// bytes mapped from the base
101    pub fn span(&self) -> usize {
102        self.span
103    }
104
105    /// the segments (Windows: the headers, then every section)
106    pub fn segs(&self) -> &[LibSeg] {
107        &self.segs[..(self.nseg as usize).min(LH_SEGMAX)]
108    }
109}
110
111impl Drop for Lib {
112    fn drop(&mut self) {
113        crate::lib_unload(self);
114    }
115}
116
117/// unload a library (dropping the Lib does the same)  [plat_lib_close]
118pub fn lib_close(h: Lib) {
119    drop(h)
120}
121
122/// the end of the segment of h holding addr (0 = none) and its LIBA_* flags  [plat_lib_seg]
123pub fn lib_seg(h: &Lib, addr: usize) -> (usize, u32) {
124    for s in h.segs() {
125        if addr >= s.start && addr < s.end {
126            return (s.end, s.flags);
127        }
128    }
129    (0, 0)
130}
131
132// ---------------------------------------------------------------- the guard
133
134/// GUARDINFO: what a guarded call hit; tag is an input (guard_tag)
135#[repr(C)]
136#[derive(Clone, Copy, Default, Debug)]
137pub struct GuardInfo {
138    pub kind: u32, // GK_* (0 = no fault)
139    pub code: u32, // NTSTATUS (Windows) / signal number (Linux)
140    pub pc: u64,   // faulting instruction
141    pub addr: u64, // faulting data address (access faults) or 0
142    pub tag: u64,  // in: tag of this guard (guard_tag)
143}
144
145/// the guard record (GR_* of plugin.inc; the trampoline and the handlers use these offsets)
146#[repr(C, align(16))]
147pub(crate) struct GuardRec {
148    pub(crate) prev: u64, // 0: enclosing guard of this thread
149    pub(crate) rsp: u64,  // 8: rsp / rbp of the trampoline's body
150    pub(crate) rbp: u64,  // 16
151    pub(crate) ent: u64,  // 24: thread table entry
152    pub(crate) kind: u32, // 32: GK_* (written by the fault handler)
153    pub(crate) code: u32, // 36
154    pub(crate) pc: u64,   // 40
155    pub(crate) addr: u64, // 48
156    tag: u64,             // 56
157    mxcsr: u32,           // 64
158    fcw: u16,             // 68
159    pad: [u8; 10],
160    xmm: [u8; 160], // 80: xmm6..xmm15
161    rax: u64,       // 240: results of the call
162    rdx: u64,       // 248
163}
164
165const _: () = assert!(core::mem::size_of::<GuardRec>() == 256);
166
167/// one thread: OS thread id, innermost active guard record (0 = none)
168#[repr(C)]
169pub(crate) struct GtEnt {
170    pub(crate) tid: AtomicU64,
171    pub(crate) cur: AtomicU64,
172}
173
174#[allow(clippy::declare_interior_mutable_const)]
175const GT0: GtEnt = GtEnt { tid: AtomicU64::new(0), cur: AtomicU64::new(0) };
176static GT: [GtEnt; GT_MAX] = [GT0; GT_MAX];
177static GT_N: AtomicU32 = AtomicU32::new(0);
178static GUARD_ON: AtomicU32 = AtomicU32::new(0);
179
180// The trampoline (Win64 ABI on both OSes): jfgui_guard_run(rec, f, args) -> 0 returned /
181// 1 faulted.  Frame like the assembly's PROLOG 16: [rsp+0..63] outgoing arguments, [rsp+64]
182// the record pointer (the fault path finds it there: only rsp / rbp are restored).
183#[cfg(target_arch = "x86_64")] core::arch::global_asm!(
184    ".globl jfgui_guard_run",
185    ".globl jfgui_guard_fault_ip",
186    "jfgui_guard_run:",
187    "    push rbp",
188    "    mov rbp, rsp",
189    "    push rbx",
190    "    push rsi",
191    "    push rdi",
192    "    push r12",
193    "    push r13",
194    "    push r14",
195    "    push r15",
196    "    sub rsp, 88",
197    "    mov [rsp + 64], rcx",
198    "    mov rsi, rcx",
199    "    mov r12, rdx",
200    "    mov r13, r8",
201    "    mov [rsi + 8], rsp",
202    "    mov [rsi + 16], rbp",
203    "    mov dword ptr [rsi + 64], 0",
204    "    mov dword ptr [rsi + 68], 0",
205    "    stmxcsr dword ptr [rsi + 64]",
206    "    fnstcw word ptr [rsi + 68]",
207    "    movdqu [rsi + 80], xmm6",
208    "    movdqu [rsi + 96], xmm7",
209    "    movdqu [rsi + 112], xmm8",
210    "    movdqu [rsi + 128], xmm9",
211    "    movdqu [rsi + 144], xmm10",
212    "    movdqu [rsi + 160], xmm11",
213    "    movdqu [rsi + 176], xmm12",
214    "    movdqu [rsi + 192], xmm13",
215    "    movdqu [rsi + 208], xmm14",
216    "    movdqu [rsi + 224], xmm15",
217    "    mov rbx, [rsi + 24]",
218    "    mov [rbx + 8], rsi", // armed: GT_CUR = the record
219    "    mov rcx, [r13]",
220    "    mov rdx, [r13 + 8]",
221    "    mov r8, [r13 + 16]",
222    "    mov r9, [r13 + 24]",
223    "    call r12",
224    // normal return: rsp is intact; rbp / xmm6-15 are restored even if f broke the ABI
225    "    mov rsi, [rsp + 64]",
226    "    mov rbp, [rsi + 16]",
227    "    mov [rsi + 240], rax",
228    "    mov [rsi + 248], rdx",
229    "    mov rbx, [rsi + 24]",
230    "    mov rcx, [rsi]",
231    "    mov [rbx + 8], rcx", // unlinked: GT_CUR = the enclosing record
232    "    call 2f",
233    "    cld",
234    "    xor eax, eax",
235    "    jmp 3f",
236    // the fault handlers resume a faulting guarded thread here (rsp, rbp of the body; the
237    // record is already unlinked)
238    "jfgui_guard_fault_ip:",
239    "    cld",
240    "    fninit",
241    "    mov rsi, [rsp + 64]",
242    "    fldcw word ptr [rsi + 68]",
243    "    ldmxcsr dword ptr [rsi + 64]",
244    "    call 2f",
245    "    mov eax, 1",
246    "3:",
247    "    lea rsp, [rbp - 56]",
248    "    pop r15",
249    "    pop r14",
250    "    pop r13",
251    "    pop r12",
252    "    pop rdi",
253    "    pop rsi",
254    "    pop rbx",
255    "    pop rbp",
256    "    ret",
257    // xmm6..xmm15 from the record at rsi; leaf
258    "2:",
259    "    movdqu xmm6, [rsi + 80]",
260    "    movdqu xmm7, [rsi + 96]",
261    "    movdqu xmm8, [rsi + 112]",
262    "    movdqu xmm9, [rsi + 128]",
263    "    movdqu xmm10, [rsi + 144]",
264    "    movdqu xmm11, [rsi + 160]",
265    "    movdqu xmm12, [rsi + 176]",
266    "    movdqu xmm13, [rsi + 192]",
267    "    movdqu xmm14, [rsi + 208]",
268    "    movdqu xmm15, [rsi + 224]",
269    "    ret",
270);
271
272#[cfg(target_arch = "x86_64")] unsafe extern "win64" {
273    fn jfgui_guard_run(rec: *mut GuardRec, f: usize, args: *const [u64; 4]) -> u64;
274    fn jfgui_guard_fault_ip();
275}
276
277/// where the OS fault handlers resume a faulting guarded thread
278pub(crate) fn guard_fault_ip() -> usize {
279    jfgui_guard_fault_ip as *const () as usize
280}
281
282/// the entry of thread tid or None (for the OS fault handlers: no allocation, no locks)
283/// [guard_lookup]
284pub(crate) fn guard_lookup(tid: u64) -> Option<&'static GtEnt> {
285    let n = (GT_N.load(Ordering::SeqCst) as usize).min(GT_MAX);
286    GT[..n].iter().find(|e| e.tid.load(Ordering::SeqCst) == tid)
287}
288
289/// this thread's entry (claimed on first use) or None when the table is full  [guard_self]
290fn guard_self() -> Option<&'static GtEnt> {
291    let tid = crate::plat_tid();
292    if let Some(e) = guard_lookup(tid) {
293        return Some(e);
294    }
295    let i = GT_N.fetch_add(1, Ordering::SeqCst) as usize;
296    if i >= GT_MAX {
297        GT_N.store(GT_MAX as u32, Ordering::SeqCst);
298        return None;
299    }
300    GT[i].cur.store(0, Ordering::SeqCst);
301    GT[i].tid.store(tid, Ordering::SeqCst);
302    Some(&GT[i])
303}
304
305/// install the fault handler (idempotent, thread safe)  [plat_guard_init]
306pub fn guard_init() -> i32 {
307    if GUARD_ON.compare_exchange(0, 1, Ordering::SeqCst, Ordering::SeqCst).is_ok() {
308        crate::guard_os_install();
309    }
310    0
311}
312
313/// info.tag of the innermost active guard of this thread (0 = none)  [plat_guard_tag]
314pub fn guard_tag() -> u64 {
315    let Some(e) = guard_lookup(crate::plat_tid()) else { return 0 };
316    let r = e.cur.load(Ordering::SeqCst);
317    if r == 0 {
318        return 0;
319    }
320    // SAFETY: an active record lives on this thread's stack (guard_call's frame) until the
321    // guarded call returns, and only this thread reads it here.
322    unsafe { (*(r as *const GuardRec)).tag }
323}
324
325/// f(a1, a2, a3, a4) under the crash guard (see the file header)  [plat_guard_call]
326pub fn guard_call(f: usize, a1: u64, a2: u64, a3: u64, a4: u64, info: Option<&mut GuardInfo>) -> (u64, u64) {
327    if GUARD_ON.load(Ordering::SeqCst) == 0 {
328        guard_init();
329    }
330    let Some(ent) = guard_self() else {
331        let e = crate::ERR_LIMIT as i64 as u64;
332        return (e, e);
333    };
334    if ent.cur.load(Ordering::SeqCst) == 0 {
335        crate::guard_thread(); // outermost guard: per-thread setup
336    }
337    let mut rec = GuardRec {
338        prev: ent.cur.load(Ordering::SeqCst),
339        rsp: 0,
340        rbp: 0,
341        ent: ent as *const GtEnt as u64,
342        kind: 0,
343        code: 0,
344        pc: 0,
345        addr: 0,
346        tag: info.as_ref().map_or(0, |i| i.tag),
347        mxcsr: 0,
348        fcw: 0,
349        pad: [0; 10],
350        xmm: [0; 160],
351        rax: 0,
352        rdx: 0,
353    };
354    let args = [a1, a2, a3, a4];
355    // SAFETY: the trampoline links rec into this thread's entry for the call and unlinks it
356    // on both paths (return: itself; fault: the handler); rec and args outlive the call.
357    // What f does is the caller's contract (see the file header).
358    let faulted = unsafe { jfgui_guard_run(&mut rec, f, &args) };
359    let rec = &rec;
360    if faulted == 0 {
361        if let Some(i) = info {
362            i.kind = 0;
363            i.code = 0;
364            i.pc = 0;
365            i.addr = 0;
366        }
367        return (rec.rax, rec.rdx);
368    }
369    if let Some(i) = info {
370        i.kind = rec.kind;
371        i.code = rec.code;
372        i.pc = rec.pc;
373        i.addr = rec.addr;
374    }
375    let e = ERR_FAULT as i64 as u64;
376    (e, e)
377}
378
379/// (for the OS fault handlers) unlink the innermost record of the faulting thread and store
380/// the fault into it -> (rsp, rbp to resume with), None when the thread has no active guard
381pub(crate) fn guard_hit(tid: u64, kind: u32, code: u32, pc: u64, addr: u64) -> Option<(u64, u64)> {
382    let e = guard_lookup(tid)?;
383    let r = e.cur.load(Ordering::SeqCst);
384    if r == 0 {
385        return None;
386    }
387    let gr = r as *mut GuardRec;
388    // SAFETY: an active record of the faulting thread (its stack, above the fault); the
389    // handler runs on that thread.
390    unsafe {
391        e.cur.store((*gr).prev, Ordering::SeqCst); // unlink: a second fault goes further out
392        (*gr).kind = kind;
393        (*gr).code = code;
394        (*gr).pc = pc;
395        (*gr).addr = addr;
396        Some(((*gr).rsp, (*gr).rbp))
397    }
398}
399
400/// run the Rust closure f under the crash guard -> Ok(result), Err(ERR_FAULT) when it faulted
401/// (info filled), Err(ERR_LIMIT) when the thread table is full (f did not run).  A fault
402/// abandons f's frames without running destructors (what they own leaks): meant for host
403/// code that may touch plugin memory (the loader fuzz runs lib_open / lib_sym under it).
404pub fn guard_run<R, F: FnOnce() -> R>(f: F, info: Option<&mut GuardInfo>) -> Result<R, i32> {
405    struct Ctx<F, R> {
406        f: Option<F>,
407        r: Option<R>,
408    }
409    rt::plugin_abi! { fn thunk<R, F: FnOnce() -> R>(p: u64) -> u64 {
410        // SAFETY: p is the Ctx of guard_run below, alive for the whole guarded call.
411        let c = unsafe { &mut *(p as *mut Ctx<F, R>) };
412        if let Some(f) = c.f.take() {
413            c.r = Some(f());
414        }
415        0
416    }}
417    let mut c = Ctx { f: Some(f), r: None };
418    guard_call(thunk::<R, F> as *const () as usize, &mut c as *mut Ctx<F, R> as u64, 0, 0, 0, info);
419    match c.r.take() {
420        Some(r) => Ok(r),
421        None if c.f.is_some() => Err(crate::ERR_LIMIT), // the table was full: f did not run
422        None => Err(ERR_FAULT),                         // f was taken by the faulted call
423    }
424}
425
426// pn-sys: the host side of the plugin API (JFPainter's PNAPI) gets addresses from plugin code
427// -- records it passes back (SURFACE, IMGDEC, JOB), strings, buffers on its stack -- that
428// may lie anywhere; these two touch such memory under the crash guard.
429/// copy len bytes at any address under the crash guard -> None when addr is 0, the copy
430/// faulted or the memory for the copy is missing  (pn-sys)
431pub fn guard_read(addr: usize, len: usize) -> Option<alloc::vec::Vec<u8>> {
432    if addr == 0 {
433        return None;
434    }
435    let mut v = alloc::vec::Vec::new();
436    if v.try_reserve_exact(len).is_err() {
437        return None;
438    }
439    let r = guard_run(
440        || {
441            // SAFETY: a fault inside the copy is caught by the guard (v stays empty: its
442            // length is set only after the copy).
443            let s = unsafe { core::slice::from_raw_parts(addr as *const u8, len) };
444            v.extend_from_slice(s);
445        },
446        None,
447    );
448    if r.is_ok() && v.len() == len { Some(v) } else { None }
449}
450
451/// write data at any address under the crash guard -> false when addr is 0 or it faulted
452/// (pn-sys)
453pub fn guard_write(addr: usize, data: &[u8]) -> bool {
454    if addr == 0 {
455        return false;
456    }
457    // SAFETY: a fault inside the copy is caught by the guard; the caller hands over memory
458    // the plugin API documents as writable (a record or buffer the plugin passed).
459    guard_run(|| unsafe { core::ptr::copy_nonoverlapping(data.as_ptr(), addr as *mut u8, data.len()) }, None).is_ok()
460}
461
462/// copy len bytes at addr of the image of h -- only when they lie in one readable segment
463/// (lib_seg), else None (pointers a plugin hands over)
464pub fn lib_read(h: &Lib, addr: usize, len: usize) -> Option<alloc::vec::Vec<u8>> {
465    let (end, fl) = lib_seg(h, addr);
466    if end == 0 || fl & LIBA_R == 0 || len > end - addr {
467        return None;
468    }
469    // SAFETY: [addr, addr + len) lies in a readable segment of the loaded image.
470    Some(unsafe { core::slice::from_raw_parts(addr as *const u8, len) }.to_vec())
471}
472
473// ---------------------------------------------------------------- test aids (plattest)
474// Win64 functions to run under guard_call: 0 sum (rax = a1 + 2 a2 + 3 a3 + 4 a4, rdx = a1 ^
475// a4), 1 write to [a1], 2 ud2, 3 int3, 4 integer division by zero, 5 endless recursion in
476// 4 KB frames (stack overflow), 6 clobber every callee-saved register, xmm6-15, MXCSR (round
477// down), the x87 control word (single precision) and DF, then write to [a1]
478#[cfg(target_arch = "x86_64")] core::arch::global_asm!(
479    ".globl jfgui_gt_code",
480    "jfgui_gt_code:",
481    "0:",
482    "    lea rax, [rcx + rdx*2]",
483    "    lea r10, [r8 + r8*2]",
484    "    add rax, r10",
485    "    lea rax, [rax + r9*4]",
486    "    mov rdx, rcx",
487    "    xor rdx, r9",
488    "    ret",
489    ".p2align 4",
490    "1:",
491    "    mov dword ptr [rcx], 0x0BADF00D",
492    "    ret",
493    ".p2align 4",
494    "2:",
495    "    ud2",
496    "    ret",
497    ".p2align 4",
498    "3:",
499    "    int3",
500    "    ret",
501    ".p2align 4",
502    "4:",
503    "    xor ecx, ecx",
504    "    mov eax, 1",
505    "    xor edx, edx",
506    "    div ecx",
507    "    ret",
508    ".p2align 4",
509    "5:",
510    "    sub rsp, 4096",
511    "    mov [rsp], rax",
512    "    call 5b",
513    "    add rsp, 4096",
514    "    ret",
515    ".p2align 4",
516    "6:",
517    "    mov rax, 0x5A5A5A5A5A5A5A5A",
518    "    mov rbx, rax",
519    "    mov rsi, rax",
520    "    mov rdi, rax",
521    "    mov rbp, rax",
522    "    mov r12, rax",
523    "    mov r13, rax",
524    "    mov r14, rax",
525    "    mov r15, rax",
526    "    movq xmm6, rax",
527    "    movq xmm7, rax",
528    "    movq xmm8, rax",
529    "    movq xmm9, rax",
530    "    movq xmm10, rax",
531    "    movq xmm11, rax",
532    "    movq xmm12, rax",
533    "    movq xmm13, rax",
534    "    movq xmm14, rax",
535    "    movq xmm15, rax",
536    "    push 0x3F80",
537    "    ldmxcsr dword ptr [rsp]",
538    "    mov dword ptr [rsp], 0x7F",
539    "    fldcw word ptr [rsp]",
540    "    add rsp, 8",
541    "    std",
542    "    mov dword ptr [rcx], eax",
543    "    cld",
544    "    ret",
545    ".p2align 4",
546    ".globl jfgui_gt_table",
547    "jfgui_gt_table:",
548    "    lea rax, [rip + 0b]",
549    "    lea rdx, [rip + 1b]",
550    "    lea r8, [rip + 2b]",
551    "    lea r9, [rip + 3b]",
552    "    mov [rcx], rax",
553    "    mov [rcx + 8], rdx",
554    "    mov [rcx + 16], r8",
555    "    mov [rcx + 24], r9",
556    "    lea rax, [rip + 4b]",
557    "    lea rdx, [rip + 5b]",
558    "    lea r8, [rip + 6b]",
559    "    mov [rcx + 32], rax",
560    "    mov [rcx + 40], rdx",
561    "    mov [rcx + 48], r8",
562    "    ret",
563    // -> rax MXCSR, rdx x87 control word | DF << 16
564    ".globl jfgui_gt_state",
565    "jfgui_gt_state:",
566    "    sub rsp, 8",
567    "    stmxcsr dword ptr [rsp]",
568    "    mov eax, [rsp]",
569    "    fnstcw word ptr [rsp + 4]",
570    "    movzx edx, word ptr [rsp + 4]",
571    "    pushfq",
572    "    pop rcx",
573    "    and ecx, 0x400",
574    "    shl ecx, 6",
575    "    or edx, ecx",
576    "    shl rdx, 32",
577    "    or rax, rdx",
578    "    add rsp, 8",
579    "    ret",
580);
581
582#[cfg(target_arch = "x86_64")] unsafe extern "win64" {
583    fn jfgui_gt_table(t: *mut [usize; 7]);
584    fn jfgui_gt_state() -> u64;
585}
586
587/// (test aid) the address of guard test function k (see above), 0 for an unknown k
588pub fn guard_test_code(k: u32) -> usize {
589    let mut t = [0usize; 7];
590    // SAFETY: fills the 7 addresses.
591    unsafe { jfgui_gt_table(&mut t) };
592    t.get(k as usize).copied().unwrap_or(0)
593}
594
595/// (test aid) this thread's MXCSR, x87 control word and direction flag
596pub fn guard_test_state() -> (u32, u32, bool) {
597    // SAFETY: reads the control registers only.
598    let v = unsafe { jfgui_gt_state() };
599    let d = (v >> 32) as u32;
600    (v as u32, d & 0xFFFF, d >> 16 & 1 != 0)
601}
602
603// ---------------------------------------------------------------- ARM64 (docs/ARM64.md)
604//
605// The trampoline in AAPCS64 (the plugin ABI of ARM64, decision 4 of the plan; Windows on ARM64 passes these
606// arguments alike): jfgui_guard_run(rec = x0, f = x1, args = x2) -> x0: 0 returned / 1 faulted.  Its frame:
607// x29 / x30 at [sp], x19 .. x28 at [sp + 16 .. 96], the record pointer at [sp + 96] (the fault path finds it
608// there: only sp and x29 are restored), 112 bytes, sp 16-aligned.  The record's fields keep their offsets
609// and change their meaning: rsp / rbp = sp / x29 of the body, mxcsr = FPCR, xmm = d8 .. d15 (80 .. 143;
610// AAPCS64 keeps only their low halves), rax / rdx = x0 / x1 of the call.  The fault path (linux_guard.rs:
611// pc, sp, x29 in the signal frame; win_lib.rs: Pc, Sp, Fp in the CONTEXT) restores FPCR and d8 .. d15 from
612// the record; the epilogue restores x19 .. x28, x29 and x30 from the frame; sp comes from x29.  x18 (the
613// TEB on Windows) is never touched.  The jfseh lines are the unwind data of Windows (.pdata / .xdata, ARM5):
614// with them RtlVirtualUnwind steps through the trampoline into the host, as the crash handler does for a
615// fault in a plugin outside a guard; on Linux they assemble to nothing.
616#[cfg(target_arch = "aarch64")]
617core::arch::global_asm!(
618    ".macro jfseh d:vararg",
619    ".if {win}",
620    r"\d",
621    ".endif",
622    ".endm",
623    ".globl jfgui_guard_run",
624    ".globl jfgui_guard_fault_ip",
625    ".p2align 2",
626    "jfgui_guard_run:",
627    "    jfseh .seh_proc jfgui_guard_run",
628    "    stp x29, x30, [sp, #-112]!",
629    "    jfseh .seh_save_fplr_x 112",
630    "    mov x29, sp",
631    "    jfseh .seh_set_fp",
632    "    stp x19, x20, [sp, #16]",
633    "    jfseh .seh_save_regp x19, 16",
634    "    stp x21, x22, [sp, #32]",
635    "    jfseh .seh_save_regp x21, 32",
636    "    stp x23, x24, [sp, #48]",
637    "    jfseh .seh_save_regp x23, 48",
638    "    stp x25, x26, [sp, #64]",
639    "    jfseh .seh_save_regp x25, 64",
640    "    stp x27, x28, [sp, #80]",
641    "    jfseh .seh_save_regp x27, 80",
642    "    jfseh .seh_endprologue",
643    "    str x0, [sp, #96]",
644    "    mov x19, x0",
645    "    mov x20, x1",
646    "    mov x21, x2",
647    "    mov x9, sp",
648    "    str x9, [x19, #8]",
649    "    str x29, [x19, #16]",
650    "    mrs x9, fpcr",
651    "    str x9, [x19, #64]", // FPCR (32 bits used) over mxcsr and fcw
652    "    stp d8, d9, [x19, #80]",
653    "    stp d10, d11, [x19, #96]",
654    "    stp d12, d13, [x19, #112]",
655    "    stp d14, d15, [x19, #128]",
656    "    ldr x9, [x19, #24]",
657    "    str x19, [x9, #8]", // armed: GT_CUR = the record
658    "    ldp x0, x1, [x21]",
659    "    ldp x2, x3, [x21, #16]",
660    "    blr x20",
661    // normal return: sp is intact; x29 / d8 .. d15 are restored even if f broke the ABI
662    "    ldr x9, [sp, #96]",
663    "    ldr x29, [x9, #16]",
664    "    stp x0, x1, [x9, #240]",
665    "    ldr x10, [x9, #24]",
666    "    ldr x11, [x9]",
667    "    str x11, [x10, #8]", // unlinked: GT_CUR = the enclosing record
668    "    bl 2f",
669    "    mov x0, #0",
670    "    b 3f",
671    // the fault handler resumes a faulting guarded thread here (sp, x29 of the body; the record is
672    // already unlinked)
673    "jfgui_guard_fault_ip:",
674    "    ldr x9, [sp, #96]",
675    "    ldr x10, [x9, #64]",
676    "    msr fpcr, x10",
677    "    bl 2f",
678    "    mov x0, #1",
679    "3:",
680    "    jfseh .seh_startepilogue",
681    "    mov sp, x29",
682    "    jfseh .seh_set_fp",
683    "    ldp x19, x20, [sp, #16]",
684    "    jfseh .seh_save_regp x19, 16",
685    "    ldp x21, x22, [sp, #32]",
686    "    jfseh .seh_save_regp x21, 32",
687    "    ldp x23, x24, [sp, #48]",
688    "    jfseh .seh_save_regp x23, 48",
689    "    ldp x25, x26, [sp, #64]",
690    "    jfseh .seh_save_regp x25, 64",
691    "    ldp x27, x28, [sp, #80]",
692    "    jfseh .seh_save_regp x27, 80",
693    "    ldp x29, x30, [sp], #112",
694    "    jfseh .seh_save_fplr_x 112",
695    "    jfseh .seh_endepilogue",
696    "    ret",
697    // d8 .. d15 from the record at x9; leaf
698    "2:",
699    "    ldp d8, d9, [x9, #80]",
700    "    ldp d10, d11, [x9, #96]",
701    "    ldp d12, d13, [x9, #112]",
702    "    ldp d14, d15, [x9, #128]",
703    "    ret",
704    "    jfseh .seh_endproc",
705    ".purgem jfseh",
706    win = const cfg!(windows) as u8,
707);
708
709#[cfg(target_arch = "aarch64")]
710unsafe extern "C" {
711    fn jfgui_guard_run(rec: *mut GuardRec, f: usize, args: *const [u64; 4]) -> u64;
712    fn jfgui_guard_fault_ip();
713}
714
715// Test aids for ARM64, the cases of the x86-64 code above in AAPCS64: 0 sum (x0 = a1 + 2 a2 + 3 a3 + 4 a4,
716// x1 = a1 ^ a4), 1 write to [a1] (the store first: the fault at the function's start), 2 udf (SIGILL /
717// STATUS_ILLEGAL_INSTRUCTION), 3 brk (SIGTRAP / STATUS_BREAKPOINT, which Windows raises for brk #0xF000),
718// 4 division by zero: ARM64's integer division does not trap, so on Linux the code sends itself SIGFPE
719// with tgkill (the signal arrives at the instruction after the svc, +40), on Windows it does what MSVC
720// does, brk #0xF004 when the divisor is 0 (STATUS_INTEGER_DIVIDE_BY_ZERO at the brk, +20; brk #0xF003
721// would be __fastfail: STATUS_STACK_BUFFER_OVERRUN ends the process without any handler), 5 endless
722// recursion in 4 KB frames, 6 clobber x19 .. x29, d8 .. d15 and FPCR (round towards minus infinity), then
723// write to [a1].  jfgui_gt_state -> x0 = FPCR (no x87, no direction flag).  The systems differ in cases 3
724// and 4 only: gt_code_aarch64! takes those lines and the operands they need.
725#[cfg(target_arch = "aarch64")]
726macro_rules! gt_code_aarch64 {
727    ($brk:literal, $($div0:literal,)+ $(; $($operands:tt)+)?) => { core::arch::global_asm!(
728        ".globl jfgui_gt_code",
729        ".p2align 4",
730        "jfgui_gt_code:",
731        "0:",
732        "    add x9, x0, x1, lsl #1",
733        "    add x10, x2, x2, lsl #1",
734        "    add x9, x9, x10",
735        "    add x9, x9, x3, lsl #2",
736        "    eor x1, x0, x3",
737        "    mov x0, x9",
738        "    ret",
739        ".p2align 4",
740        "1:",
741        "    str w0, [x0]",
742        "    ret",
743        ".p2align 4",
744        "2:",
745        "    udf #0",
746        "    ret",
747        ".p2align 4",
748        "3:",
749        $brk,
750        "    ret",
751        ".p2align 4",
752        "4:",
753        $($div0,)+
754        ".p2align 4",
755        "5:",
756        "    sub sp, sp, #4096",
757        "    str x0, [sp]",
758        "    bl 5b",
759        "    add sp, sp, #4096",
760        "    ret",
761        ".p2align 4",
762        "6:",
763        "    mov x9, #0x5A5A",
764        "    movk x9, #0x5A5A, lsl #16",
765        "    movk x9, #0x5A5A, lsl #32",
766        "    movk x9, #0x5A5A, lsl #48",
767        "    mov x19, x9",
768        "    mov x20, x9",
769        "    mov x21, x9",
770        "    mov x22, x9",
771        "    mov x23, x9",
772        "    mov x24, x9",
773        "    mov x25, x9",
774        "    mov x26, x9",
775        "    mov x27, x9",
776        "    mov x28, x9",
777        "    mov x29, x9",
778        "    fmov d8, x9",
779        "    fmov d9, x9",
780        "    fmov d10, x9",
781        "    fmov d11, x9",
782        "    fmov d12, x9",
783        "    fmov d13, x9",
784        "    fmov d14, x9",
785        "    fmov d15, x9",
786        "    mov x10, #0x800000",
787        "    msr fpcr, x10",
788        "    str w9, [x0]",
789        "    ret",
790        ".p2align 4",
791        ".globl jfgui_gt_table",
792        "jfgui_gt_table:",
793        "    adr x9, 0b",
794        "    adr x10, 1b",
795        "    stp x9, x10, [x0]",
796        "    adr x9, 2b",
797        "    adr x10, 3b",
798        "    stp x9, x10, [x0, #16]",
799        "    adr x9, 4b",
800        "    adr x10, 5b",
801        "    stp x9, x10, [x0, #32]",
802        "    adr x9, 6b",
803        "    str x9, [x0, #48]",
804        "    ret",
805        ".globl jfgui_gt_state",
806        "jfgui_gt_state:",
807        "    mrs x0, fpcr",
808        "    mov w0, w0",
809        "    ret" $(, $($operands)+)?
810    ); };
811}
812
813#[cfg(all(target_arch = "aarch64", target_os = "linux"))]
814gt_code_aarch64!(
815    "    brk #0",
816    "    mov x8, #{getpid}",
817    "    svc 0",
818    "    mov x9, x0",
819    "    mov x8, #{gettid}",
820    "    svc 0",
821    "    mov x1, x0",
822    "    mov x0, x9",
823    "    mov x2, #8",
824    "    mov x8, #{tgkill}",
825    "    svc 0",
826    "    ret",
827    ; getpid = const rt::os::SYS_GETPID, gettid = const rt::os::SYS_GETTID, tgkill = const rt::os::SYS_TGKILL
828);
829
830#[cfg(all(target_arch = "aarch64", windows))]
831gt_code_aarch64!(
832    "    brk #0xF000",
833    "    mov w9, #1",
834    "    mov w10, #0",
835    "    cbz w10, 9f",
836    "    udiv w0, w9, w10",
837    "    ret",
838    "9:",
839    "    brk #0xF004",
840);
841
842#[cfg(target_arch = "aarch64")]
843unsafe extern "C" {
844    fn jfgui_gt_table(t: *mut [usize; 7]);
845    fn jfgui_gt_state() -> u64;
846}