#! @file unwind.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains the writer of how x86-64 code on Windows unwinds, which the system #! reads to unwind a thread's stack past each function, as exceptions and #! debuggers do: each function's record of how its prologue made its frame, as #! `.xdata` holds them, and the table of each function's code and its record, #! sorted by address, as `.pdata` holds it and the exception directory names. #! #! A record lists what the prologue did, each at the offset just past its #! instruction, the latest first: registers pushed, bytes taken from the stack, #! the frame register set, and registers saved in the frame. Unwinding past #! the prologue undoes each in turn; within it, only those done yet. import std::containers; import std::io; module binary::pe; # What each code of a record does, in its low four bits. const push_nonvolatile: u16 = 0; const allocate_large: u16 = 1; const allocate_small: u16 = 2; const set_frame: u16 = 3; const save_nonvolatile: u16 = 4; const save_far: u16 = 5; #! What a function's prologue did, as its record of unwinding says it. pub struct unwind_info { var codes: vector; #! Each code's slots, in the order the prologue did them. var starts: vector; #! Where each code's slots begin. var prologue: u8; #! The bytes of the prologue. var register: u8; #! The frame register, or 0 for none. var offset: u8; #! What the frame register points past the frame's base, in 16 bytes. } #! Makes the record of a prologue that did nothing, as a function that leaves #! the stack as its caller left it has. #! @returns The record. pub def unwind_info::new(): unwind_info { return unwind_info { codes = vector::::new(), starts = vector::::new(), prologue = 0, register = 0, offset = 0 }; } # Adds a code done just before an offset, and its operand's slots after. def unwind_info::code(&mut this, at: u8, op: u16, info: u16): void { this.starts.append(this.codes.length); this.codes.append(at as u16 | (op << 8) | (info << 12)); if at > this.prologue { this.prologue = at; } } #! The prologue pushed a register, just before an offset. #! @param this The record. #! @param at Where the push ends, from where the function begins. #! @param register The register, as x86-64 numbers it: `rbp` is 5. pub def unwind_info::pushed(&mut this, at: u8, register: u8): void { this.code(at, push_nonvolatile, register as u16); } #! The prologue took bytes from the stack, just before an offset. #! @param this The record. #! @param at Where the instruction taking them ends. #! @param bytes How many, a multiple of 8. pub def unwind_info::allocated(&mut this, at: u8, bytes: u64): void { if bytes <= 128 { this.code(at, allocate_small, (bytes / 8 - 1) as u16); } else if bytes <= 512 * 1024 - 8 { this.code(at, allocate_large, 0); this.codes.append((bytes / 8) as u16); } else { this.code(at, allocate_large, 1); this.codes.append((bytes & 0xFFFF) as u16); this.codes.append((bytes >> 16) as u16); } } #! The prologue pointed a register at the frame, past its base by an offset, #! just before an offset of the code. The frame is found from that register #! from then on, however the stack moves. #! @param this The record. #! @param at Where the instruction ends. #! @param register The register. #! @param offset How far past the frame's base it points, a multiple of 16 #! up to 240. pub def unwind_info::framed(&mut this, at: u8, register: u8, offset: u64): void { this.code(at, set_frame, 0); this.register = register; this.offset = (offset / 16) as u8; } #! The prologue saved a register in the frame, just before an offset. #! @param this The record. #! @param at Where the instruction saving it ends. #! @param register The register. #! @param offset Where it is, past the frame's base, a multiple of 8. pub def unwind_info::saved(&mut this, at: u8, register: u8, offset: u64): void { if offset / 8 <= 0xFFFF { this.code(at, save_nonvolatile, register as u16); this.codes.append((offset / 8) as u16); } else { this.code(at, save_far, register as u16); this.codes.append((offset & 0xFFFF) as u16); this.codes.append((offset >> 16) as u16); } } #! The record as `.xdata` holds it: version 1, the prologue's bytes, how many #! slots its codes take, the frame register and its offset, then the codes, #! the latest first, padded to an even number of slots. #! @param this The record. #! @returns The record's bytes. pub def unwind_info::bytes(&this): byte_writer { var made := byte_writer::new(); made.byte(1); made.byte(this.prologue); made.byte(this.codes.length as u8); made.byte(this.register | (this.offset << 4)); var k := this.starts.length; while k > 0 { k--; val end := k + 1 < this.starts.length ? this.starts[k + 1] : this.codes.length; for s in this.starts[k]..end { made.u16(this.codes[s]); } } if this.codes.length % 2 != 0 { made.u16(0); } return made; } #! The records of how functions unwind, as `.xdata` holds them, each once and #! aligned to four bytes, however many functions share it. pub struct unwind_table { pub var bytes: byte_writer; #! The section's bytes. var held: interner; #! Each record, by its ID. var at: vector; #! Where each is, by its ID. } #! Makes a table holding no records. #! @returns The table. pub def unwind_table::new(): unwind_table { return unwind_table { bytes = byte_writer::new(), held = interner::::new(), at = vector::::new() }; } #! Where a record is in the table, adding it where it is new. #! @param this The table. #! @param info The record. #! @returns Where it is, from where the table begins. pub def unwind_table::add(&mut this, info: &unwind_info): u32 { val made := info.bytes(); val id := this.held.intern_view(made.view()); if id < this.at.length { return this.at[id]; } this.bytes.align_to(4); val at := this.bytes.length as u32; this.bytes.put(made.view()); this.at.append(at); return at; } #! A function's code and its record of unwinding, each as an address past the #! image's base. struct runtime_function { var begin: u32; var end: u32; var unwind: u32; } #! The table of each function's code and its record of unwinding, as `.pdata` #! holds it. pub struct function_table { var entries: vector; } #! Makes a table naming no functions. #! @returns The table. pub def function_table::new(): function_table { return function_table { entries = vector::::new() }; } #! Names a function's code, which no other's overlaps, and its record. #! @param this The table. #! @param begin Where its code begins, past the image's base. #! @param end Where it ends. #! @param unwind Where its record is, past the image's base. pub def function_table::add(&mut this, begin: u32, end: u32, unwind: u32): void { this.entries.append(runtime_function { begin = begin, end = end, unwind = unwind }); } # Whether one function's code begins before another's. def begins_before(a: &runtime_function, b: &runtime_function): bool { return a.begin < b.begin; } #! The table as `.pdata` holds it, sorted by where each function begins. #! @param this The table. #! @returns Its bytes. pub def function_table::bytes(&mut this): byte_writer { this.entries.sort_by(begins_before); var made := byte_writer::new(); for &each in this.entries { made.u32(each.begin); made.u32(each.end); made.u32(each.unwind); } return made; }