feat(dev): reverse-engineering tools for finding hook sites: a code index, a function-hit trace and a write-watch #2924

Merged
HumanBean merged 1 commit from re/tools into mod-api 2026-12-01 00:28:27 +00:00
Owner

Summary

Finding a new hook site meant reading lifted code by hand. This adds three development-only tools that take a behaviour
of the game to the function and instruction a site goes on, and a page on how to use them
(docs/dev/reference/reverse-engineering.md, with a worked synthetic example and how the site's signature or
fingerprint is then written):

  • A code index. longshot-builder dev facts writes what the lift's own discovery knows of each function (calls past
    PLT stubs, virtual-call slots, string and data references, constants and floats, function pointers and vtables,
    exports, guest stores), and dev disasm decodes a range. scripts/re.py index loads the facts into SQLite under
    ~/.cache/longshot-dev/re. It then answers callers/callees, xrefs, strings, const, float, vtable, vslot, vcalls,
    path, dis and ir queries, as text or --json.
  • A function-hit trace. The guest profile's instrumented build counts the functions. The control bridge's new
    trace command marks a window; re.py trace diff lists the functions that ran in one window and not in another.
  • A write-watch. With LONGSHOT_WATCH_STORES=1, the lift tests every guest store against a watched range. The
    bridge's new watch command sets the range and writes what hit it: each writing function and instruction, the
    values and the frames. watch needs HOS_CONTROL_MEM.
  • dev.py rt --re builds both into build-linux/rt-<checkout>-re, from a work folder of its own.

Read first: builder/crates/llvm/src/lib.rs (watch), host/re_watch.c, host/control.c (cmd_re),
builder/crates/longshot-builder/src/dev/facts.rs, scripts/re.py.

Everything the tools make (the index, the traces, the watches) is made from the game and stays on the PC, under
~/.cache/longshot-dev/re. Nothing of it is in this PR; the tests use synthetic images, facts and windows.

Testing

Tested: Linux, RTX 3090. Tested in game: the index on the real game (built from this PC's game, every query
answered). In one shared run of the --re runtime, the trace windows diffed (walking against idle, rolling
against walking, each naming the functions only in the second), and a write-watch on the player's position named one
writer with its three stores, a hit every frame while he walked; 12 more watches on other objects named their writers.
The game-free parts of trace and watch are also covered by the tests below.

Checks, in one build-mode job (retools-check), all pass:

  • dev.py rt: the normal runtime builds over the base.
  • scripts/check_host_compile.py: 82 host units, 0 failed.
  • cargo test and cargo clippy -D warnings for llvm and longshot-builder. llvm has a new watch-IR test, compiled by
    clang; with the option off, nothing of it is emitted.
  • run_all --exact: test_re, test_re_dev, test_control (the control and re_dev C tests, and control.c's llvm-mingw
    compile), test_doc_commands, test_doc_anchors.
  • host/re_watch.c and host/guest_profile.c compiled with llvm-mingw (-Wall -Wextra -Werror).

Earlier, outside the queue: cargo test -p lift, -p build-driver, test_guest_profile_write, test_switch_table,
pre-commit.

Still to test:

  • This PC (Linux, RTX 3090): dev.py rt --re, then two trace windows and a watch of a known field, expecting a short
    diff and the field's writers named (by hand, in the first site-finding session).

Measurements

Nothing to measure: with the switch off the lifted IR is unchanged (a test checks it), so normal builds and fps are unchanged; the -re build is development only. Every lift cache is rebuilt once, as with any change to the lifter's sources.

Risks

Development only. LONGSHOT_WATCH_STORES is off unless set to 1, and trace and watch answer unavailable in a
normal build. A revert is enough. Players notice nothing, apart from the lift rebuilding once after the update.

Checklist

Ready for review

  • One topic, on current mod-api; the title is a Conventional Commit.
  • Tested on my own PC, in the game for a change the game runs, as Testing says.
  • mise run test passes (any failure that also fails on main is named). (the tests that read the changed files pass in retools-check; the whole suite is not run)
  • Measured against main, or Measurements says why there is nothing to measure.
  • No game files, build output or hand-edited generated files; third_party/ untouched; nothing GPL-incompatible.

When it applies

  • A check under tests/ that reads changed source is updated, not weakened.
  • Host code builds on Windows (llvm-mingw) and Linux, with UTF-8 paths.
  • Docs updated.
  • AI assistance recorded with Assisted-by: / Co-Authored-By: trailers.

Ready to merge

  • Everything in Still to test has run, its results in Tested.
  • Tier 1 hardware it can affect is tested (docs/dev/standards.md, "Hardware tiers").
  • Approved in review, every comment answered.
  • CI passes on the head; on current main, measured again or a comment says why the numbers still hold.

🤖 Generated with Claude Code

https://claude.ai/code/session_016o1k1SQVyAWsujjFVd8Wuc

## Summary Finding a new hook site meant reading lifted code by hand. This adds three development-only tools that take a behaviour of the game to the function and instruction a site goes on, and a page on how to use them (docs/dev/reference/reverse-engineering.md, with a worked synthetic example and how the site's signature or fingerprint is then written): - **A code index.** `longshot-builder dev facts` writes what the lift's own discovery knows of each function (calls past PLT stubs, virtual-call slots, string and data references, constants and floats, function pointers and vtables, exports, guest stores), and `dev disasm` decodes a range. `scripts/re.py index` loads the facts into SQLite under `~/.cache/longshot-dev/re`. It then answers callers/callees, xrefs, strings, const, float, vtable, vslot, vcalls, path, dis and ir queries, as text or `--json`. - **A function-hit trace.** The guest profile's instrumented build counts the functions. The control bridge's new `trace` command marks a window; `re.py trace diff` lists the functions that ran in one window and not in another. - **A write-watch.** With `LONGSHOT_WATCH_STORES=1`, the lift tests every guest store against a watched range. The bridge's new `watch` command sets the range and writes what hit it: each writing function and instruction, the values and the frames. `watch` needs `HOS_CONTROL_MEM`. - `dev.py rt --re` builds both into `build-linux/rt-<checkout>-re`, from a work folder of its own. Read first: `builder/crates/llvm/src/lib.rs` (`watch`), `host/re_watch.c`, `host/control.c` (`cmd_re`), `builder/crates/longshot-builder/src/dev/facts.rs`, `scripts/re.py`. Everything the tools make (the index, the traces, the watches) is made from the game and stays on the PC, under `~/.cache/longshot-dev/re`. Nothing of it is in this PR; the tests use synthetic images, facts and windows. ## Testing **Tested:** Linux, RTX 3090. Tested in game: the index on the real game (built from this PC's game, every query answered). In one shared run of the `--re` runtime, the trace windows diffed (walking against idle, rolling against walking, each naming the functions only in the second), and a write-watch on the player's position named one writer with its three stores, a hit every frame while he walked; 12 more watches on other objects named their writers. The game-free parts of trace and watch are also covered by the tests below. Checks, in one build-mode job (retools-check), all pass: - `dev.py rt`: the normal runtime builds over the base. - `scripts/check_host_compile.py`: 82 host units, 0 failed. - `cargo test` and `cargo clippy -D warnings` for llvm and longshot-builder. llvm has a new watch-IR test, compiled by clang; with the option off, nothing of it is emitted. - `run_all --exact`: test_re, test_re_dev, test_control (the control and re_dev C tests, and control.c's llvm-mingw compile), test_doc_commands, test_doc_anchors. - `host/re_watch.c` and `host/guest_profile.c` compiled with llvm-mingw (`-Wall -Wextra -Werror`). Earlier, outside the queue: `cargo test -p lift`, `-p build-driver`, test_guest_profile_write, test_switch_table, pre-commit. **Still to test:** - This PC (Linux, RTX 3090): `dev.py rt --re`, then two trace windows and a watch of a known field, expecting a short diff and the field's writers named (by hand, in the first site-finding session). ## Measurements Nothing to measure: with the switch off the lifted IR is unchanged (a test checks it), so normal builds and fps are unchanged; the `-re` build is development only. Every lift cache is rebuilt once, as with any change to the lifter's sources. ## Risks Development only. `LONGSHOT_WATCH_STORES` is off unless set to `1`, and `trace` and `watch` answer `unavailable` in a normal build. A revert is enough. Players notice nothing, apart from the lift rebuilding once after the update. ## Checklist ### Ready for review - [x] One topic, on current `mod-api`; the title is a Conventional Commit. - [x] Tested on my own PC, in the game for a change the game runs, as Testing says. - [ ] `mise run test` passes (any failure that also fails on `main` is named). (the tests that read the changed files pass in retools-check; the whole suite is not run) - [x] Measured against `main`, or Measurements says why there is nothing to measure. - [x] No game files, build output or hand-edited generated files; `third_party/` untouched; nothing GPL-incompatible. ### When it applies - [x] A check under `tests/` that reads changed source is updated, not weakened. - [x] Host code builds on Windows (llvm-mingw) and Linux, with UTF-8 paths. - [x] Docs updated. - [x] AI assistance recorded with `Assisted-by:` / `Co-Authored-By:` trailers. ### Ready to merge - [ ] Everything in Still to test has run, its results in Tested. - [ ] Tier 1 hardware it can affect is tested (docs/dev/standards.md, "Hardware tiers"). - [ ] Approved in review, every comment answered. - [ ] CI passes on the head; on current `main`, measured again or a comment says why the numbers still hold. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_016o1k1SQVyAWsujjFVd8Wuc
feat(dev): reverse-engineering tools for finding hook sites: a code index, a function-hit trace and a write-watch
Some checks failed
PR description / Title and description (pull_request_target) Has been cancelled
0d9cbc565a
A later agent goes from a behaviour of the game to the function and instruction a hook site goes on
(docs/dev/reference/reverse-engineering.md, with a worked synthetic example and how the site is then written):

- The code index: `longshot-builder dev facts` writes what the lift's own discovery knows of each function (calls past
  PLT stubs, vcall slots, adrp pairs' strings and data, constants and fmov floats, function pointers and vtables,
  exports, guest stores) and `dev disasm` decodes a range; `scripts/re.py index` loads them into SQLite under
  ~/.cache/longshot-dev/re and answers callers/callees, xrefs, strings, const, float, vtable, vslot, vcalls, path, dis
  and ir, as text or `--json`.
- The function-hit trace: the guest profile's instrumented build (`--guest-profile generate`); the control bridge's new
  `trace` command clears its counters at a window's start and writes them at its end (host/guest_profile.c's
  hos_re_trace); `re.py trace start|stop|hits|diff` names the functions that ran in one window and not another.
- The write-watch: `LONGSHOT_WATCH_STORES=1` (llvm `Options::watch_stores`) gives each unit a watched range and tests
  every guest store against it, calling host/re_watch.c on a hit (function, block, place, address, value, frame); the
  bridge's `watch` (guest memory reads needed) sets it and writes the hits; `re.py watch show` names each writer's
  instruction. Done in the lift rather than by page protection: exact per store, by guest function, and nothing in a
  normal build.
- `dev.py rt --re` builds both into build-linux/rt-<checkout>-re from a work folder of its own.

Off, nothing changes: the IR is as without the option (llvm test), the bridge only gains two commands. Everything the
tools make stays on the PC; the tests use synthetic images, facts and windows.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016o1k1SQVyAWsujjFVd8Wuc
Author
Owner

Checked without a game run: dev.py rt built; cargo test and clippy -D warnings on llvm and longshot-builder pass; run_all --exact test_re, test_re_dev, test_control (with the C tests), test_doc_commands and test_doc_anchors pass; the host compiles on Linux (82 units, 0 failed), and re_watch.c and guest_profile.c compile with llvm-mingw for Windows under -Werror. Tested in game: Linux, RTX 3090, the code index built from this PC's game and every query answered. The trace and write-watch get their first real-game run in the first site-finding session.

Checked without a game run: dev.py rt built; cargo test and clippy -D warnings on llvm and longshot-builder pass; run_all --exact test_re, test_re_dev, test_control (with the C tests), test_doc_commands and test_doc_anchors pass; the host compiles on Linux (82 units, 0 failed), and re_watch.c and guest_profile.c compile with llvm-mingw for Windows under -Werror. Tested in game: Linux, RTX 3090, the code index built from this PC's game and every query answered. The trace and write-watch get their first real-game run in the first site-finding session.
Author
Owner

Tested in game (Linux, RTX 3090, one shared run of the --re runtime): trace windows diffed (walking vs idle, rolling vs walking), and a write-watch on the player's position named its writer and three stores, a hit every frame; 12 more watches named their writers. Note for users: two idle windows already differ (background work), so take several windows per side.

Tested in game (Linux, RTX 3090, one shared run of the --re runtime): trace windows diffed (walking vs idle, rolling vs walking), and a write-watch on the player's position named its writer and three stores, a hit every frame; 12 more watches named their writers. Note for users: two idle windows already differ (background work), so take several windows per side.
HumanBean changed title from WIP: feat(dev): reverse-engineering tools for finding hook sites: a code index, a function-hit trace and a write-watch to feat(dev): reverse-engineering tools for finding hook sites: a code index, a function-hit trace and a write-watch 2026-12-01 00:28:14 +00:00
HumanBean merged commit 5780eedaec into mod-api 2026-12-01 00:28:27 +00:00
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
MagicBeanDev/Longshot!2924
No description provided.