Skip to content

fix(ghidra): expose bit-field base type identity - #8602

Merged
morluto merged 5 commits into
mainfrom
codex/ghidra-bitfield-type-identity
Nov 4, 2026
Merged

morluto merged 5 commits into
mainfrom
codex/ghidra-bitfield-type-identity

Conversation

@morluto

@morluto morluto commented Nov 1, 2026 •

Copy link
Copy Markdown
Owner

Summary

A Ghidra bit-field component can report a type path such as /uint:3 that is not separately registered, so following that path fails and the underlying /uint type is hidden. For #8224, report the observed base type, declared bit width, and exact database lookup outcomes alongside the existing effective width and offset.

Contract and scope

The Ghidra bridge owns these observations. bit_field is omitted when details were not reported; its lookup outcomes distinguish an equivalent registered type, a different type at the path, and no registered type. The lookup comparison is Ghidra's type equivalence, not identity or source-language authority. Existing component paths, sizes, and offsets are retained. Document the meaning of bit offset and effective versus declared width. The shared Ghidra analysis profile now includes the data-type observation revision: older Ghidra analysis snapshots fail profile admission and must be rebuilt; standalone historical Evidence remains distinct from a fresh observation. No target execution, mutation, provider selection, or installation behavior changes.

Evidence and validation

  • Real Ghidra 12.1.4 on macOS arm64, caller-owned existing project: the packed struct's /uint:3 component reported base /uint, declared width 3 and not-found for the component lookup; following the component path returned unavailable and following /uint returned the scalar. Full and compact stdio MCP output validated against the advertised SDK schemas. The executable and original project files were unchanged and the executable was not run.
  • The source-built inventory.c bit-field case was added to verifyNativeTypeLayout. With the type-layout case temporarily run first, real Ghidra completed the new checks and their follow-up type queries; restoring the original bridge made that same check fail because the bit-field facts were absent. The temporary verifier ordering was not committed. The normal npm run verify:ghidra fails earlier at compare_functions because the in-process MCP server uses the 1,024-element input allowance ([Bug] Ghidra verification sends function Evidence beyond the default MCP input allowance #8172). Rerunning with REA_MCP_MAX_INPUT_ELEMENTS=65536 still reported 1,024; it did not reach the new case. This is not a passing full verifier run.
  • A full npm run verify:ghidra passed on macOS arm64 with Ghidra 12.1.4/JDK 21 in a temporary combination of this PR 00e823b9c and verifier-only changes from fix(verification): compare retained Ghidra function evidence #8541 2bf92eb9, test(ghidra): check retained historical snapshot evidence #8381 64e42396, test(ghidra): check structured snapshot profile mismatch #8536 78b6c662, and test(ghidra): check regex recovery at its owning boundary #8578 dd281594. The report includes the type-layout fixture, 439 successful and 52 rejected boundary calls, complete cleanup, and verified process lineage with no descendants. This is a combined run, not a pass of this PR alone. With fix(verification): compare retained Ghidra function evidence #8541 alone, the run passed the original comparison limit but stopped at the later legacy-snapshot assertion addressed by test(ghidra): check structured snapshot profile mismatch #8536.
  • npm run check:fast and npm run docs:check — passed. Hosted Static checks failed format:check on 16 files outside this PR, matching the baseline formatting issue addressed by fix(test): restore the two checks #8573 left red on main #8592.
  • Big- and little-endian existing MIPS projects could not reach type inspection because of the pre-existing profile mismatch [Bug] Existing MIPS Ghidra projects fail with a misleading profile mismatch #8225, also reproduced on the unchanged base. Other providers and hosts remain unverified. A separate disposable Ghidra project control exercised typedef and enum bases, a zero-width field, and a field declared 40 bits with effective width 32. Direct Ghidra script output agreed with full and compact stdio MCP; each exact base query succeeded and output validated against its advertised schema. The source-built executable and selected project remained unchanged during REA inspection. This is a manually authored database fixture, not evidence of compiler ABI interpretation or an automated regression.

Author readiness

Human owner:

  • I reviewed the final diff and relevant existing code, understand the change and its edge cases, and take responsibility for the complete PR, including AI-assisted work.

  • I checked the evidence above, confirmed it supports the current change, and recorded meaningful validation gaps.

  • I declare this PR ready for initial maintainer review.

  • Big-endian fresh-import control: the source-built PowerPC fixture produced 22000000 at the bit-field storage address. Real Ghidra 12.1.4 through full and compact stdio MCP reported the 3- and 5-bit fields at offsets 5 and 0 and successfully inspected /uint; both advertised output schemas validated. LLVM readelf independently confirmed the binary is big-endian PowerPC, and the unexecuted target SHA-256 e9001062492e07d9cd679406870d3a53a119154bcf28409b4ec35c31d6dc647e was unchanged. The same fixture and checks were added to verify:ghidra:ppc; the complete npm run verify:ghidra:ppc lane passed on PR head 00e823b9c, using Homebrew LLVM 23 and GNU readelf 2.47 in a temporary directory. The readelf bottle matched Homebrew metadata before its local loader paths were adjusted. Its report includes the bit-field check, CLI/MCP and snapshot identity, verified process lineage and no descendants. tests/conformance/ghidra/ppc-checks.test.ts passed 12 tests.

  • Snapshot control on the final profile change: real Ghidra MCP created a snapshot on base d3ab0faf5 containing the old bit-field result. The earlier candidate replayed that record without the new field, marked as snapshot replay. The revised profile rejects that snapshot with evidence_integrity_mismatch; a fresh analysis saved a new snapshot and replayed the new base-type fact. Focused profile/snapshot checks passed 21 tests and check:fast passed. The revision intentionally changes the shared Ghidra profile and invalidates old Ghidra snapshots, not only type entries. Separately, the old snapshot's Evidence bundle imported into a fresh MCP server and its original record remained retrievable with the original ID and omitted bit-field detail.

Combined saved-project control: in a temporary checkout of #8605 f49c5f4fc with #8602 00e823b9c applied, real Ghidra 12.1.4 full and compact stdio MCP inspected the caller-authored bit fields in both original MIPS projects. Big-endian offsets were 5/0, little-endian offsets 0/3, each /uint base was inspectable, the profiles preserved target byte order and unknown original analysis settings, and original project/target hashes were unchanged. A different target with the selected project was refused by the imported-byte digest check. The combined tree passed 96 focused MIPS/profile/snapshot tests and check:fast. This verifies a local combination; neither PR includes the other.

The same temporary combination also passed the complete npm run verify:ghidra:mips lane: all six fixtures passed, including both saved-project controls, with unchanged source project and target hashes, verified process lineage, and no remaining descendants. This is a combined result, not a claim that either PR contains the other.

@greptile-apps

greptile-apps Bot commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no actionable issues were found.

Summary

Exposes Ghidra bit-field base types, declared widths, and exact database lookup outcomes without changing existing component identities.

  • Bit-field results now point from a component path to its base type.
  • Ghidra reuses snapshots only when their data-type observation rules match.

Reviews (5) · Last reviewed commit: "merge: integrate main into bit-field bas..." · Reviewed by Greptile

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 4, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ⚠️ Failed 2026-11-04T19:22:20.109983Z
We couldn’t complete this review. Comment @codex review to try again.
a1cc76a New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

Keep byte-search snapshot admission and recompute raw profile digests
for the combined policy, including bit-field base and lookup observations.
@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.

@morluto
morluto merged commit 9ef7069 into main Nov 4, 2026
49 checks passed
@morluto
morluto deleted the codex/ghidra-bitfield-type-identity branch November 4, 2026 20:03
Entrpi added a commit to Entrpi/rea that referenced this pull request Nov 7, 2026
main's byte-search (morluto#8840), data-type (morluto#8602) and reference revisions change every
Ghidra profile. With those revisions reversed, the old pins hold.
@morluto morluto added bug Something isn't working provider: ghidra Analysis provider / engine: ghidra labels Nov 22, 2026
morluto added a commit that referenced this pull request Nov 24, 2026
…ce (#8464)

* fix(ghidra)!: load ESP-IDF flash code in Ghidra's default address space

ESP-IDF's linker script reserves the addresses of .flash.text with the
NOBITS section .flash_rodata_dummy. Ghidra 12.1.4's ELF loader gives the
default address space to the later section, so .flash.text lands in an
overlay that its DWARF import, call-frame processing and default-space
references do not reach. Flash functions lose their DWARF signatures,
locals and no-return flags, each gets a one-byte shadow function in the
reservation, and pseudocode runs past calls such as esp_restart().

The ELF reader now records allocated ranges that an executable section
shares with another section, through one section-table visitor that the
RISC-V ISA reader also uses. When a NOBITS section starts with one
executable section and covers it, the RISC-V lanes commit both sections
to the analysis profile. ReaGhidraPrepareElfLoad.java then gives the code
the default address space before auto-analysis and keeps the reservation
as an uninitialized overlay; bytes, file offsets, symbols, functions and
entry points are kept. The session reports the outcome, and any memory
map that differs from the committed plan is refused. Images without such
an overlap keep their analysis profiles byte for byte.

BREAKING CHANGE: RISC-V images with this layout, such as ESP-IDF
ESP32-C6, H2 and H21 builds, report flash code at its ELF address
(0x42...) instead of .flash.text:0x42..., and their analysis profiles
change. RISC-V images with any other section overlap that involves
executable code are now refused with the overlapping sections named.
These include ESP-IDF v5.5.2 ESP32-C5 and C61 builds, whose second
reservation, .ext_ram.dummy, also covers .flash.text. See
docs/ghidra-riscv.md.

* fix(ghidra): bound the overlap sweep and check the load preparation

Domain:
- Find section overlaps with one sort and sweep that stops at the second
  overlap involving executable code, and read the ISA and the overlap in
  one section-table pass.
- Record the two sections that cause a refusal as witnesses. The Ghidra
  lane owns the refusal text and names them with their ranges.

Ghidra lane:
- Check the preparation report before the first lazily started analysis
  call, as for the calling-convention report.
- The pre-analysis script removes the code's overlay entry points, checks
  that no symbol or reference remains in the old overlay range, and reports
  the counts of the result. Its report echoes the blocks it left, which the
  client compares with the committed sections. A failed move refuses and
  rethrows. Reservation labels mark no entry points.
- Session limitations no longer state a planned move after a canonical
  outcome.

Verification:
- The ESP fixture defines ESP-IDF's absolute text bounds and the
  reservation's label. The lane checks their addresses, the entry points,
  file-offset uniqueness and snapshot replay, a reservation-first build,
  a pre-analysis refusal through analyzeHeadless and a truncated
  double-float section table.
- Boundary tests cover the refusal witnesses, a 10,000-section table and
  an ELF64 table.

* fix(ghidra): retarget references into the moved ESP-IDF flash code

The move takes the references from the code's range but not the
references into it. Besides the entry points, Ghidra 12.1.4's loader
records one data reference from the .flash.text section header to the
code, which the new post-condition refused in the RISC-V lane. Retarget
plain memory references and entry points into the old range to the moved
code; any other kind stays behind and is refused.

Removing the reservation block keeps the references into its range, so
the pinned symbols' entry points need no re-add.

* fix(ghidra): move labels past the ESP-IDF flash code's end with it

Ghidra 12.1.4 places a section-relative symbol at the section's end, such
as ESP-IDF's _etext, in the code's overlay space one byte past the block.
The move covers the block only, so the label stayed in the overlay and
ended up in the reservation. Everything in the code's overlay space now
follows the code: references are retargeted, labels outside the block are
created again in the default space, and the post-condition checks the
whole space. The script refuses when another block shares that space.

The ESP fixture defines _etext, and the lane checks its address.

* fix(ghidra): check prepared blocks by coordinates and address spaces

The client compared the pre-script's echoed block names with the ELF
section names, although section names are display only: an unnamed
section's empty name never matches the name Ghidra gives its block, so
that preparation was refused. It also accepted any reported address
spaces. The check now compares addresses, sizes and the code's file
offset, and requires moved code to go from an overlay into the
program's default space while its reservation leaves it, and canonical
code to stay there. An unnamed section is labelled by its index.

The docs and the pre-script now state that a failure during the move
may be refused without its reason, and that an entry mark on a
reservation label stays at its default-space address. The docs give
the llvm-objcopy route for the refused C5 and C61 builds. The lane
checks that no other block maps any of the code's file bytes, and the
overlap tests add the C5 and C61 section shape.

* fix(ghidra): bound the sections the overlap check keeps

The overlap check kept one record per allocated, non-empty section, and
only the file size limited their number. NOBITS headers take no file
bytes, so a crafted 1 GB ELF32 table holds about 27 million of them.
The check now keeps at most 65,280 (SHN_LORESERVE, from which ELF
counts sections through section zero) and reports the overlap as
unavailable beyond that, which the RISC-V lanes refuse as they refuse
a table that cannot be read.

* fix(ghidra): check RISC-V handshake reports before any analysis call

A RISC-V session that committed neither a calling-convention policy nor
a load preparation started its bridge lazily on the first query other
than `health`. An `elf_load_preparation` or calling-convention report
that the profile did not commit was then never checked, and the query
returned its result. Every RISC-V session now starts through the
checked handshake, so such a report closes the session before any
analysis call, whatever the first query is. A double-float session that
reports a preparation, and one that reports a calling-convention
policy, are now tested with `list_procedures`; both fail without the
fix.

Admission, profile parameters and profile digests do not change. If
Ghidra exits during the first query of a double-float session, the
error now tells the caller to reopen the session, as other checked
sessions do.

* fix(ghidra): skip analysis after a refused ELF load preparation

A refused memory map only wrote the refusal, so headless Ghidra still
ran seeds, auto-analysis and post-script analysis on a program that REA
then discarded. The preparation now turns off headless auto-analysis when
it refuses; later pre-scripts and the bridge still run, so the session
reports the reason.

The swap also renamed the reservation's overlay space to the section's
raw name, so a reservation named like another address space, such as
the RISC-V default space ram, or containing characters Ghidra rejects,
failed after the move. The space now takes the name Ghidra's memory map
derives for an overlay block: ':' and whitespace become '_', and a name
in use gains .1, .2 and so on.

verify:ghidra:riscv drops -noanalysis from the refusal run and checks
that Ghidra analyzed nothing, and a seventh fixture names the
reservation ram and checks that it moves into the overlay space ram.1.

* test(ghidra): read the load preparation report before Ghidra's log suffix

* test(ghidra): pin unchanged RISC-V profiles at main's revisions

main's byte-search (#8840), data-type (#8602) and reference revisions change every
Ghidra profile. With those revisions reversed, the old pins hold.

* fix(ghidra): name the moved RISC-V guide in load-layout diagnostics

The overlap refusal, the load-layout limitation and the bridge comments
named docs/ghidra-riscv.md, which main moved to docs/providers/.

* test(ghidra): check load-preparation facts instead of sentences

The refusal, report-check and handshake tests compared whole messages.
They now check the sections, coordinates, spaces and counts each message
must state, and the handshake compares the session's limitations with
ghidraRiscvLoadPreparationLimitations. Rewording the messages passes;
dropping a count fails.

* fix(ghidra): give the ESP-IDF load preparation unused temporary names

The preparation created its temporary blocks as <name>.rea, so a disjoint
section already named that refused an otherwise admitted image. It now
takes the first unused <name>.rea[.N]. verify:ghidra:riscv gains an
esp-edge-names fixture with such a section.

* fix(ghidra): keep both final block names free during the ESP-IDF swap

The code's temporary block could take the removed reservation's name,
such as a reservation named <code>.rea, and the reservation's rename
back then collided. The picker now skips both final names.
verify:ghidra:riscv's esp-edge-names fixture names its reservation
.flash.text.rea beside a disjoint .flash.text.rea.1.

* test(ghidra): repin the overlap-free RISC-V digests for main's #8340

main's #8340 bumps call_relationship_coverage from v5 to v7, which changes
every Ghidra profile digest. With the revision reverted in place, the old
pins hold (79 of 79).

* test(ghidra): repin unchanged RISC-V digests for main's value-flow budgets

main's #9706 adds native_value_flow_projection to the shared analysis
semantics, which changes the profiles of images without a section overlap.
With that one revision removed, the previous pins hold (79 of 79).

* fix(ghidra): inspect RISC-V PIE section overlaps and refuse an unverified PIE reservation

Main admits RISC-V PIE, but this lane requires a section-overlap read
that the resolver made only for ET_EXEC, so every PIE was refused. A PIE's
section addresses are fixed offsets from its load base, so overlaps do not
depend on the base: read them for ET_DYN too. The NOBITS reservation
preparation is verified for ET_EXEC only, so a PIE that needs it is
refused. Main's producer identity now digests the bridge sources, so the
test that compared profile digests with main's is replaced by checking
that images without an overlap commit no load preparation.

* fix(ghidra): find an ESP-IDF reservation that Ghidra 12.2 marks executable

Ghidra 12.2 gives each section block the permissions of the first PT_LOAD
that contains its address, so a NOBITS reservation that shares the code's
addresses comes out executable and the preparation refused every ESP-IDF
image. Tell the reservation from the code by its missing bytes alone; the
displaced reservation is still made non-executable. The PIE refusal test
now uses the ELF64 lane, which admits PIE.

* fix(verify): read the ESP-IDF fixtures' identity where main's #11416 moved it

Evidence subjects and snapshot targets now carry their digest under
identity, as the rest of the RISC-V verifier reads it.

* docs(ghidra): describe the ESP-IDF load layout as verified on Ghidra 12.2

The real RISC-V lane now passes on Ghidra 12.2, whose ELF loader still puts
the code in an overlay when its reservation follows it. Name 12.2 where the
lane exercised that behaviour, and note that 12.2 can mark the reservation
executable. Measurements of Ghidra APIs the lane did not re-check keep
their 12.1.4 attribution.

* docs(domain): align RISC-V section-overlap JSDoc with ET_DYN inspection

---------

Co-authored-by: morluto <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working provider: ghidra Analysis provider / engine: ghidra

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants