Skip to content

feat(ghidra): recover signed AArch64 relative jump tables - #1592

Merged
morluto merged 4 commits into
morluto:mainfrom
RishTheFish21:fix/487-signed-relative-tables
Oct 11, 2026
Merged

morluto merged 4 commits into
morluto:mainfrom
RishTheFish21:fix/487-signed-relative-tables

Conversation

@RishTheFish21

Copy link
Copy Markdown
Contributor

Summary

Part of #487. REA's AArch64 relative jump-table matcher only recognised one shape: unsigned ldrb/ldrh entries, lsl #2 offsets and a b.hi bound. Hand-written code like the RenderScript blend kernel from the issue thread uses ldrsh, a uxtw index, plain byte offsets, a b.cs bound and a cbz that skips zero entries. This PR extends the matcher to those forms, still only when the bound, registers and table bytes check out exactly.

Problem and expected behavior

On the new fixture (same instruction shape as rsdIntrinsicBlend_K), main maps cases 0, 1, 2 and 4 from Ghidra's typed case labels. Case 3 is a zero entry that the cbz sends to its own path. Ghidra doesn't model the cbz, so it decodes that entry as a branch to the table's base address, which the br can never reach. REA then reported the base as a target with an unknown case, and case 3's real destination was missing.

Expected: every case maps to the code it actually reaches, with evidence, and anything REA can't prove stays unknown.

Change and scope

  • The matcher accepts signed byte/halfword entries, uxtw indexes, byte or lsl #2 offsets, b.hi or b.cs/b.hs bounds, and an optional cbz on the loaded entry. It still requires one straight-line path with no other branches into it, and an index bounded by the compare.
  • It now also runs when Ghidra exposes its own load table. Its result is only used when both agree on table address, entry size and count; otherwise Ghidra's view is kept unchanged.
  • Zero entries routed by cbz map to the cbz target. The base address Ghidra decodes for them is no longer listed as an unknown target; a limitation explains why it's unreachable.
  • New fixture tests/conformance/ghidra/relative-switch-signed.S, wired into the aarch64-jump-table lane as ELF, plus Mach-O on arm64 hosts.

Not included: reporting computed branches Ghidra can't resolve, and returning a partial dossier when the decompiler fails. Both are planned as a follow-up.

Contract and boundary impact

  • Semantic owner and earliest changed stage: Ghidra bridge (ReaGhidraBridge.java), jump-table inference.
  • CLI and MCP/tool-catalog contract: none. No schema changes; only evidence and limitation text and which mappings are filled in.
  • Provider, bridge, target-format, or platform compatibility: AArch64 only; other architectures are untouched.
  • Evidence, artifact, provenance, or reconstruction contract: uses the existing provenance values. More cases can now be mapped with high confidence; a decoded result never overrides Ghidra when they disagree.
  • Process execution, authorization, cleanup, or containment impact: none.
  • Generated metadata, package, or installation impact: none.

Evidence and regression coverage

  • Tests added or updated: the new fixture and verifyRelativeSignedSwitch, which checks every case target against the immediate written in the source.
  • Base reproduction or other evidence: with the matcher not running (current main behavior when Ghidra exposes a load table), the new check fails with Signed relative switch case 3 was not verified, and 0x100030 is reported as an unlabelled target.
  • User-visible CLI/MCP output: inspect_native_api now reports case 3 → the cbz target with high confidence, plus a limitation naming the unreachable base address.
  • Remaining proof gaps: see Validation.

For evidence-bearing changes:

  • Observed, derived, and inferred claims remain distinguishable.
  • Artifact identity, source provenance, and failed attempts remain preserved.
  • Unsupported, incomplete, unavailable, or uncertain outcomes remain visible.

Validation performed

  • npm run verify:ghidra:aarch64-jump-table on an Apple Silicon Mac (Ghidra 12.1.4, JDK 21, decompiler built with buildNatives): passed. relative-signed was verified for ELF and Mach-O; the existing relative-1/relative-2 fixtures and the cross-format AArch64 fixture still pass.
  • npm run check:

I ran the lane with the entry-alias boundary check skipped locally (not part of this PR). That check already fails on macOS arm64 on main: the fixture's interior label lands on the function entry, and even when moved, the Mach-O linker records it as a function start, so Ghidra makes it a function. I'll open a separate issue. Because of the skip, the imported_entry_alias_selection: true in the report is not evidence. Not run against the real librenderscript-toolkit.so, only against a fixture with the same shape. Not run on Linux.

Compatibility, safety, and release

  • Breaking changes or migration steps: none.
  • Real Hopper/Ghidra, browser, or OS coverage: real Ghidra 12.1.4 on macOS arm64 (see above). No Linux run.
  • Package or release metadata impact: none.
  • Security, privacy, process, or containment review: static analysis only; no new process or file access.

Review checklist

  • This PR addresses a concrete problem or an agreed enhancement.
  • The PR has one focused outcome and the title follows type(scope): outcome.
  • Related issue is linked, or the reason for not linking one is stated above.
  • Tests cover changed observable behavior and meaningful failure paths.
  • Owning docs, contracts, and generated metadata are updated where needed.
  • User-visible CLI/MCP changes include representative output.
  • I checked the final diff for secrets, unrelated cleanup, and unsupported claims.

Generalise the AArch64 relative-table matcher to signed byte/halfword entries, UXTW indexes, byte or LSL morluto#2 offsets, B.CS/B.HS bounds and a CBZ route for zero entries. Cross-check it against Ghidra's own load table when one exists, and add a fixture modelled on the RenderScript blend kernel.

Refs morluto#487
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Repo admins can enable using credits for code reviews in their settings.

@CassieFox23

CassieFox23 commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

TL;DR: On Linux the new signed-table check passes. All five cases map to the right code, including case 3 (the cbz zero entry). The entry-alias check from #1597 passes too, but both needed one fix from main first (#1374).

Results

I tested head 8ab1453e5 against its merge-base 810107a2a, on Linux with Ghidra 12.1.4.

On this box the Ghidra lanes do not reach the PR's code as shipped. GCC here is 16.2.1, and the -O0 debug fixture has no jump table. Both trees stop in about 18 seconds. Main already fixes this in #1374, so I added that change to both trees. The cross-format lane needs one more main fix, #1382, so I added it too.

Check Base 810107a2a PR 8ab1453e5
verify:ghidra:aarch64-jump-table, as shipped Fail, exit 1, 18.7 s, no dense jump table Fail, exit 1, 18.4 s, same stop as the base
Same lane, plus #1374 Pass, exit 0, 379 s, 7 fixtures, cleanup: complete Pass, exit 0, 387 s, 8 fixtures (adds relative-signed ELF), cleanup: complete
Jump table of relative-switch-signed.S, same .o in both Case 3 missing. 0x100030 listed with no case All 5 cases high. Case 3 maps to 0x100044
Entry-alias check (#1597) Pass Pass
verify:ghidra:cross-format, plus #1374 only Not run Fail, exit 1, 366 s: cross-arm64-elf did not recover the multi-block callee
verify:ghidra:cross-format, plus #1374 and #1382 Pass, exit 0, 378 s, 7 fixtures Pass, exit 0, 386 s, 7 fixtures
5 existing Ghidra test files, Node 22, 24, 26 Not run 69 pass and 1 skipped on each
npm run check:fast, Node 24 Not run Pass

The PR adds no unit tests. The five test files are existing ones that sit near the Ghidra code. On the PR, the cross-format failure goes away when only cross-format.c changes (#1382). That file is the same in the base and the PR.

Case 3, checked by hand. I built the fixture with the lane's clang command. Its sha256 matches the lane's file (e3396bd7…). The table bytes decode to -12, -4, +4, 0 and +12 from Lsigned_base at 0x30. That gives targets 0x24, 0x2c, 0x34, 0x44 and 0x3c. A disassembler (gdb, AArch64 mode) shows mov w0, #200, 201, 202, 250 and 204 there. REA's targets are the same offsets plus the 0x100000 image base, and its decoded immediates match. I also ran the fixture under qemu-aarch64-static. Selectors 0 to 4 return 200, 201, 202, 250 and 204, as the source says.

Entry alias (#1597). The check passes on Linux, and it ran unmodified. My added changes touch only the two verifier scripts and cross-format.c. The lane ends with imported_entry_alias_selection: true, which is set only after the check runs. With cc here (GCC 16.2.1) the interior label sits 4 bytes after the entry (0x40044a and 0x400446). Main has not changed the check since the base.

One thing stays unknown. The default path (selector 5 and up, through b.cs) has no target. The limitation text says so, which matches the PR's own scope.

Not run

  • macOS and Apple Silicon, so the Mach-O relative-signed fixture did not run. This box is x86-64, and the lane builds that fixture on arm64 hosts only.
  • Windows.
  • The real librenderscript-toolkit.so.
  • A merge with main. The PR is marked conflicting, so I tested the PR head with the added fixes above.
  • The Ghidra lanes on Node 22 and 26. They ran on Node 24.18.0 only.
  • npm run check (the full gate) and the full test suite.
  • The other Ghidra lanes: swift, switch, dos, com, nativeaot and windows.
  • The unmodified base for cross-format, and the base for the test files and check:fast.

Thanks, RishTheFish21, for a fixture that carries its own answers in the source, and for filing #1597.

Conflicts

GitHub marks the PR as conflicting. I did not merge, because the mirror is read-only here. Since 810107a2a, main changed four of the five PR files: bridge/ghidra/ReaGhidraBridge.java (#1438), docs/testing.md (14 commits), and the two verifier scripts (#1374).

A plain patch dry run of main's changes onto the PR files, against main c1217e711 (#1577):

Git's own merge may disagree with patch on the other files. For the lanes above I hand-merged that one hunk, which adds a jump-table branch to the chain.

What I added to both trees
Commands
# trees: git archive <sha> | tar -x, then on Node 24.18.0
npm ci && npm run build
# one Ghidra lane at a time, GHIDRA_INSTALL_DIR and JAVA_HOME set
npm run verify:ghidra:aarch64-jump-table
npm run verify:ghidra:cross-format
# Node 22, 24 and 26
npm run test:focused -- tests/boundary/providers/ghidra/ghidraLauncher.test.ts \
  tests/boundary/providers/ghidra/ghidraClient.test.ts \
  tests/boundary/mcp/ghidraNativeApiMcp.test.ts \
  src/ghidra/GhidraFunctionValues.test.ts \
  src/application/native/NativeApiInspection.test.ts
# check:fast needs a git checkout, so it ran in a clone at 8ab1453e5
npm run check:fast
# by hand
clang --target=aarch64-linux-gnu -c tests/conformance/ghidra/relative-switch-signed.S -o rs.o
nm -n rs.o; readelf -r rs.o; readelf -x .rodata rs.o
gdb --batch -ex 'set architecture aarch64' -ex 'file rs.o' -ex 'disassemble /r 0x0,0x54'

The case-3 numbers on both trees came from a short MCP script: open_binary, analyze_function and inspect_native_instruction on the same rs.o.

Raw output: the new table on the PR
dispatch 0x100020, table 0x100054, 5 entries of 2 bytes (ghidra-decompiler-load-table)
case 3 -> 0x100044 high   cbz zero entry         mov 0xfa
case 0 -> 0x100024 high                          mov 0xc8
case 1 -> 0x10002c high                          mov 0xc9
case 2 -> 0x100034 high                          mov 0xca
case 4 -> 0x10003c high                          mov 0xcc
default targets: none

Base, same rs.o:

case 0, 1, 2, 4 -> same four targets, high
case none -> 0x100030 medium   (decodes to ret)
Test setup

🤖 Generated with Claude Code (Sonnet 5.5, verified by Opus 5.5) 🦊

@morluto
morluto merged commit 3749cce into morluto:main Oct 11, 2026
2 checks passed
@morluto

morluto commented Oct 11, 2026

Copy link
Copy Markdown
Owner

Welcome, @RishTheFish21! Thank you for the signed AArch64 jump-table work. The zero-entry cbz case is a good catch, and the assembly fixture gives us a concrete way to check that REA reports the right destination.

@morluto morluto added enhancement New feature or request area: native Native analysis and decompilation, including Ghidra, IDA, and Hopper analysis behavior labels Oct 14, 2026
@morluto morluto added the provider: ghidra Analysis provider / engine: ghidra label Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: native Native analysis and decompilation, including Ghidra, IDA, and Hopper analysis behavior enhancement New feature or request provider: ghidra Analysis provider / engine: ghidra

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants