`jmp dword ptr [reg*4 + disp]` dispatches through a table of code
pointers that MSVC parks inline, inside the function body. linear_sweep
has no way to know, so it decoded the pointers as instructions and came
out the far side out of phase -- every byte after the table belonged to
an instruction that did not exist, until the stream happened to resync.
That routinely ate the function's own epilogue, so the `pop esi / pop
edi` balancing the prologue was never lifted and *every caller silently
lost those registers*. Half-Life 2 hits it in the CRT: memcpy and memmove
both carry a tail-copy table, and the C++ static-initialiser driver walks
its constructor array with esi as the cursor and edi as the limit. Both
came back clobbered from its own callees, so the loop stopped after 8% of
the list and Source had no registered interfaces to hand
CreateInterface. 219 tables in that one binary.
Three parts:
- record the table address of every indexed indirect jump, measure the
table (entries must point inside the table's own section -- XBEs mark
.rdata and .data executable, so "points at executable" alone would
let an array of data pointers pass), drop the instructions the sweep
hallucinated over it, and decode_at() past the end;
- extend function bounds by the table's entries. The case bodies are
jump targets like any other, just held in memory rather than encoded
in the instruction, and without them the function ended on its own
dispatch;
- step over the table when the boundary walk lands on it, since there
are deliberately no instructions there now.
memcpy went from ending at its first switch to covering all 664 bytes
through the epilogue.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BuW5WGcXFFrLAYEfvCNGs1
_find_function_end stored forward branch targets in max_addr, which is the
function's *exclusive* end. The coverage test then asked
`addr + size >= max_addr`, which is satisfied while sitting exactly on the
target -- so the walk stopped at the first instruction the function was
required to contain.
MSVC emits the triggering shape constantly: a conditional branch forward, a
body, an unconditional `jmp` backwards, and the branch target parked out of
line after it. Halo's get_edge_vertex (0x00107EC0) ended at 0x00107FB8,
which is its own tail. The tail was then lifted as a separate function and
the `jne` to it became a tail call that returned without running the
epilogue, leaking the 28-byte frame (push ebp / sub esp,0xc / push
ebx,esi,edi) on every call. Three calls in, the caller's saved esi/edi/ebx
came back as garbage.
Track branch targets in their own max_target and require the walk to decode
strictly past it. Landing on a target is not coverage.
Halo: get_edge_vertex 248 -> 402 bytes, and unresolved call targets
(addresses called but with no detected function) drop 1989 -> 175.
tools/disasm/test_function_end.py covers the out-of-line tail, a plain
function still ending at its first ret, and next_func still bounding the walk.