diff options
Diffstat (limited to 'README.md')
| -rw-r--r-- | README.md | 39 |
1 files changed, 39 insertions, 0 deletions
@@ -69,6 +69,7 @@ is any of them, in any order: time travel is an index. From a debugger's recording, `zt.telescope`: `instruction(m, addr, buf)` is an instruction's text, its source `line`, and for a direct call its callee's `params` (DWARF's); `frames(m, regs, out)` is the call chain with each frame's extent on the stack, `low` to `high` (a `stack()` frame's is up to `sp + 8`), walked by the call frame information the recording holds (below), else by frame pointer; `slots(m, regs, low, out)` is what each stack word from `low` is (`out[i]` the word at `low + 8 i`), a `Slot`: its `frame`, an index into `frames` (null in none), and its `role`: `.saved` (the register a function saved for its caller there: `"rbx"`, `"rbp"`, `"r12"` to `"r15"`), `.return_address`, `.local` (a variable or parameter: `name`, `size`, and `offset`, where the word is in it), or the process's first stack, `.argc`, `.argv` i, `.envp` i (i = argc, envc: the zero that ends them) and `.auxv` i (the auxiliary vector's i-th word, two an entry); `.none` where the recording does not say; `function(m, pc)` is the function that holds a pc as the recorded walk went through it, its `start` and `len` by its call frame information (a PLT stub's 16 bytes), for every frame of every stop; `got(m, slot)` is what a GOT slot holds and whether that is still `lazy` (its name has `@plt`: the way back into the PLT, so the next call runs the loader's resolver) or bound. For a call followed down, each frame has its module (`mapping`), whether it has source (its instructions' `line`), its function's bounds (`function`), and, from a caller, three instructions round its call: the return address, the call `back` from it, and the row before the call `back` from that. +A function as a graph (IDA's, Binary Ninja's): `blocks(m, pc, out)` gives the basic blocks of the function that holds `pc`, a `Graph`: its bounds, its blocks in address order, the one that holds the pc (`here`), and whether every instruction's text is recorded (`text`; then `instruction` reads each block's rows). A `Block` has its bounds, how it ends (`Debugger.End`: `fall` into the next, an unconditional `jump`, a conditional `branch`, `ret`, an `indirect` jump whose targets are not known, a `tail` call out of the function, a call that does not return, `noreturn`: the function's last instruction, or `abort`, `exit`, `__stack_chk_fail` and their kin) and its edges: `to`, where a jump, a taken branch, a tail call or a call that does not return goes, and `falls`, the block after it for `fall` and a branch not taken. A call that returns ends no block. The debugger records them once a function, at the first stop in it, decoded from its own instructions (its bounds from its symbol, else its unwind information: a stripped library's function has them too), with every instruction's text and the names of what its calls and its jumps out of it reach, up to `Debugger.function_text` instructions (2048); past that the blocks without the text (`sqlite3VdbeExec`, 11 077 instructions: 2 510 blocks). Code with no bounds (the C runtime's start, a JIT's) has none. Which edges a run took: by instruction, a stop whose instruction is a jump or a return has a `branch` event (a2 its pc, a0 where it goes, a1 1 when it will go there); its pc is the last instruction of the block that holds it, and the next stop is at `to` when a1 is 1, else at `falls` (a `ret`'s, at its caller). By line or by breakpoint there are no stops between, so a reader knows the blocks the stops are in and, at a stop on a jump, the edge it is about to take; the path between two stops it does not know. What it costs: the list of `anim/tools` by instruction, 243 bytes a moment against 224 (every function stopped in, recorded whole once) and 25 600 moments a second against 26 300; the first stop in `sqlite3BtreeInsert` (1 045 instructions) 118 KB and 2 to 4 ms, in `sqlite3VdbeExec` 80 KB (its blocks alone) and 6 ms; SQLite by breakpoint on `sqlite3BtreeInsert` 317 bytes a moment against 310. Code is named at every pc, a function's first instruction too, and a PLT stub and a GOT slot for the function they lead to: `open@plt+0`, `open@got+0` (from the relocations and the dynamic symbols; an IFUNC's in libc by the function its resolver is, `libc.so.6!strlen@plt+0`); of the names libc gives one address (`open`, `open64`, `__open64`) the one the program's relocations use. A slot is named when an instruction jumps or calls through it. With `--alloc`, every live allocation and the 16 bytes of allocator header before it are in `read` at each stop (its first 4 KiB; what changed, a stop at a time; 256 KiB of them at most, lowest addresses first: `Debugger.allocation_watch`). Recorded `--by insn`, the instructions that ran between two moments are the `rip` of each moment between them; `--by line` does not keep them. @@ -366,6 +367,44 @@ By instruction from `main` it costs a quarter of the moments a second and 2 bytes a moment: `targets/heap.c` 55 000 to 41 000 and 112 to 114 bytes, the list of `anim/tools` 48 000 to 41 000 and 170 to 173. +## AddressSanitizer + +`ztrace record --asan` records ASan's shadow beside every region it +records (the stack window, watched regions, live allocations, what +instructions touch and `--around` round it, what pointers lead to): a +byte for 8, at `(addr >> 3) + offset`, the offset the runtime exports +(`__asan_shadow_memory_dynamic_address`: 0x7fff8000 here, for gcc's +`libasan.so` and for clang's runtime linked in), found at the first stop +after the runtime set it and recorded once as the string +`Debugger.asan_id`. `zt.telescope.shadow(m, addr)` is the shadow byte's +address and what it holds: 00 addressable, 1 to 7 that many first bytes, +f1 f2 f3 a frame's left, middle and right redzones, f5 a frame returned +from, fa a heap block's redzone, fd freed. A program not built with +`-fsanitize=address` is refused. It costs a second read of each region: 4 +bytes a moment and a quarter of the moments a second (by instruction on +`targets/overflow.c`, 243 to 248 bytes and 42 000 to 31 000). + +`targets/overflow.c` writes 40 bytes into a 16-byte buffer. Built with +ASan by gcc or clang (`zig build test-debugger` builds both), at the stop +on `__asan_report_store1`'s first instruction its argument, the first byte +past the buffer, has `f3` for shadow, the buffer `00 00` and the 32 bytes +before it `f1`: the frame comes from ASan's fake stack +(`__asan_stack_malloc_0`); the branch into the report is a `branch` event +(gcc's through `__asan_report_store1@plt`), and the chain goes back from +inside the report through `fill` to `main`. The report itself is more +than a million instructions (`ListOfModules::init` clearing its buffers +a byte at a time): record by instruction up to it (`--steps`), and the +rest `--by syscall`, where the report is the `write`s to fd 2 with their +text (`-s 4096`: the line of `=`, then `ERROR: AddressSanitizer: +stack-buffer-overflow on address ...`) and the run ends in +`exit_group(1)`. ASan's leak check needs `ASAN_OPTIONS=detect_leaks=0` +under a tracer. Built plain at -O0 the same loop has its own `i` and `n` +above the buffer and overwrites them, and writes on to the end of the +stack: a SIGSEGV at the loop's store, its fault address the stack's end +(built at -O1 the loop is a `memset` and `fill` tail-calls `printf`, +whose `ret` pops 0x4141414141414141: a SIGSEGV at that `ret`, fault +address 0). + ## The debugger `ztrace dbg -- PROGRAM` starts a program stopped and reads commands (from a |
