[PR #4156] [CLOSED] fix(ghidra): compare DOS relocation windows with final loaded bytes #6094

Closed
opened 2026-10-31 01:11:48 +00:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/morluto/rea/pull/4156
Author: @berelels
Created: 10/15/2026
Status: ❌ Closed

Base: main ← Head: fix/dos-overlapping-relocation-windows


📝 Commits (1)

  • 297a649 fix(ghidra): compare DOS relocation windows with final loaded bytes

📊 Changes

2 files changed (+110 additions, -4 deletions)

View changed files

📝 src/ghidra/GhidraLoadImageValues.test.ts (+101 -0)
📝 src/ghidra/GhidraLoadImageValues.ts (+9 -4)

📄 Description

Summary

Fixes #3981. inspect_native_load_image reported mismatch for a DOS MZ image whose relocation words overlap, even when Ghidra's loaded bytes are correct. The expected memory_bytes_hex for each relocation record was captured mid-application. The bridge reads each record's window from the loaded Program after every fixup, so a later overlapping relocation changed it.

Problem and expected behavior

attestGhidraDosLoadImage applied relocations in table order and recorded each record's memory window right after its own fixup. With records at module offsets 9 then 8, the second fixup rewrites byte 9, so the first record's final window differs from the window captured right after its own fixup. bridge/ghidra/ReaGhidraBridge.java (relocation enumeration) reads currentProgram.getMemory() at observation time, after loading.

Change and scope

  • The fixup loop is unchanged: same order, same values and same original_bytes_hex. Expected memory windows are now read from the fully relocated buffer once every fixup has been applied.
  • Multiplicity and both record addresses are preserved. A deliberately intermediate window still fails relocation_records.
  • Not included: Ghidra's own handling of chained overlaps (see "Remaining proof gaps").

Contribution scope: an ordinary fix within existing boundaries, so no maintainer scope decision is needed.

Contract and boundary impact

  • Semantic owner and earliest changed stage: src/ghidra/GhidraLoadImageValues.ts, DOS MZ expected relocation records.

  • Contribution scope fit and linked maintainer decision: not applicable (bug fix).

  • CLI and MCP/tool-catalog contract: unchanged schemas. A false mismatch becomes verified.

  • Provider, bridge, target-format, or platform compatibility: bridge unchanged.

  • Evidence, artifact, provenance, or reconstruction contract: load-image attestation now matches the documented "current loaded Program" comparison (docs/ghidra-dos.md, Loaded-image evidence).

  • Target selection, authority, upstream behavior, and cleanup details: not applicable.

  • Generated metadata, package, or installation impact: none.

  • Read existing files or captured data.

  • Launch or attach to a process.

  • Inject instrumentation or run scripts/code.

  • Change process memory, registers, files, or configuration.

  • Contact network services.

  • Recover protected code or work around protections.

  • Documentation or usability only; no target interaction.

  • Not sure yet; investigation or maintainer guidance needed.

Evidence and regression coverage

  • Tests added or updated: src/ghidra/GhidraLoadImageValues.test.ts, "overlapping DOS MZ relocations". The fixture uses the module bytes and relocation observations measured from real Ghidra 12.1.4 for relocations at offsets 9 then 8. It asserts verified with both records retained, and that substituting the intermediate 0410 window fails only relocation_records.
  • Base reproduction: the new test fails on main (cd64e24ff) and passes with the fix.
  • Real provider (Ghidra 12.1.4, JDK 21.0.12.1, Linux x64): a stdio MCP probe ran open_binary + inspect_native_load_image on tests/conformance/ghidra/dos-mz-fixture.mjs, with only its relocation table changed.
    • Table 9,8: loaded bytes at module+8..10 are 00 14 10. Base: mismatch (only relocation_records; expected 0410, observed 1410 at 0x10009). This branch: verified.
  • Remaining proof gaps, observed and left out of scope: Ghidra's MzLoader (12.1.4 source) computes each fixup from the original file word and applies fixups in HashSet order. For chained overlaps (table 8,9) and duplicates (9,9), Ghidra's records and bytes differ from cumulative DOS loading, and REA still reports mismatch. That is correct, since the loaded image differs from a DOS load. For table 8,9, the loaded bytes differed between two real runs (00 04 10, then 00 14 10). This deserves its own issue.

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 test:local -- src/ghidra/GhidraLoadImageValues.test.ts: 38 passed + 1 failed on base, 39 passed with the fix.
  • npm run test:focused -- src/ghidra/GhidraLoadImageValues.test.ts src/ghidra/GhidraDosComLoadImage.test.ts src/ghidra/GhidraRawBinaryLoadImage.test.ts src/ghidra/GhidraProvider.behavior.test.ts tests/boundary/cli/nativeEfficiencyComparison.test.ts …: 9 files, 205 tests passed (combined run with the #3970 branch's files).
  • npm run check:fast (pre-push): 2/2 tasks successful.
  • Real Ghidra probe (node scripts/rea.mjs mcp over stdio, built tree): results above, run before and after the fix.

Compatibility, safety, and release

  • Breaking changes or migration steps: none.
  • Real Hopper/Ghidra, browser, or OS coverage: Ghidra 12.1.4 on Linux x64. No macOS or Windows.
  • Package or release metadata impact: none.
  • Security, privacy, process, or containment review: no new process or file effects. The target is analyzed statically, never executed.

Author readiness

Human owner: @berelels

  • 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.

Before merge

  • This PR addresses a concrete problem or an agreed enhancement.
  • Material scope and authority questions are resolved, and any required maintainer scope decision covers the final diff.
  • 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.

🤖 Generated with Claude Code


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/morluto/rea/pull/4156 **Author:** [@berelels](https://github.com/berelels) **Created:** 10/15/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `fix/dos-overlapping-relocation-windows` --- ### 📝 Commits (1) - [`297a649`](https://github.com/morluto/rea/commit/297a64989784230195410a82bc74c60f7b2a318b) fix(ghidra): compare DOS relocation windows with final loaded bytes ### 📊 Changes **2 files changed** (+110 additions, -4 deletions) <details> <summary>View changed files</summary> 📝 `src/ghidra/GhidraLoadImageValues.test.ts` (+101 -0) 📝 `src/ghidra/GhidraLoadImageValues.ts` (+9 -4) </details> ### 📄 Description ## Summary Fixes #3981. `inspect_native_load_image` reported `mismatch` for a DOS MZ image whose relocation words overlap, even when Ghidra's loaded bytes are correct. The expected `memory_bytes_hex` for each relocation record was captured mid-application. The bridge reads each record's window from the loaded Program after every fixup, so a later overlapping relocation changed it. ## Problem and expected behavior `attestGhidraDosLoadImage` applied relocations in table order and recorded each record's memory window right after its own fixup. With records at module offsets 9 then 8, the second fixup rewrites byte 9, so the first record's final window differs from the window captured right after its own fixup. `bridge/ghidra/ReaGhidraBridge.java` (relocation enumeration) reads `currentProgram.getMemory()` at observation time, after loading. ## Change and scope - The fixup loop is unchanged: same order, same `values` and same `original_bytes_hex`. Expected memory windows are now read from the fully relocated buffer once every fixup has been applied. - Multiplicity and both record addresses are preserved. A deliberately intermediate window still fails `relocation_records`. - Not included: Ghidra's own handling of chained overlaps (see "Remaining proof gaps"). Contribution scope: an ordinary fix within existing boundaries, so no maintainer scope decision is needed. ## Contract and boundary impact - Semantic owner and earliest changed stage: `src/ghidra/GhidraLoadImageValues.ts`, DOS MZ expected relocation records. - Contribution scope fit and linked maintainer decision: not applicable (bug fix). - CLI and MCP/tool-catalog contract: unchanged schemas. A false `mismatch` becomes `verified`. - Provider, bridge, target-format, or platform compatibility: bridge unchanged. - Evidence, artifact, provenance, or reconstruction contract: load-image attestation now matches the documented "current loaded Program" comparison (`docs/ghidra-dos.md`, Loaded-image evidence). - Target selection, authority, upstream behavior, and cleanup details: not applicable. - Generated metadata, package, or installation impact: none. - [x] Read existing files or captured data. - [x] Launch or attach to a process. - [ ] Inject instrumentation or run scripts/code. - [ ] Change process memory, registers, files, or configuration. - [ ] Contact network services. - [ ] Recover protected code or work around protections. - [ ] Documentation or usability only; no target interaction. - [ ] Not sure yet; investigation or maintainer guidance needed. ## Evidence and regression coverage - Tests added or updated: `src/ghidra/GhidraLoadImageValues.test.ts`, "overlapping DOS MZ relocations". The fixture uses the module bytes and relocation observations measured from real Ghidra 12.1.4 for relocations at offsets 9 then 8. It asserts `verified` with both records retained, and that substituting the intermediate `0410` window fails only `relocation_records`. - Base reproduction: the new test fails on `main` (cd64e24ff) and passes with the fix. - Real provider (Ghidra 12.1.4, JDK 21.0.12.1, Linux x64): a stdio MCP probe ran `open_binary` + `inspect_native_load_image` on `tests/conformance/ghidra/dos-mz-fixture.mjs`, with only its relocation table changed. - Table `9,8`: loaded bytes at module+8..10 are `00 14 10`. Base: `mismatch` (only `relocation_records`; expected `0410`, observed `1410` at 0x10009). This branch: `verified`. - Remaining proof gaps, observed and left out of scope: Ghidra's `MzLoader` (12.1.4 source) computes each fixup from the *original file word* and applies fixups in `HashSet` order. For chained overlaps (table `8,9`) and duplicates (`9,9`), Ghidra's records and bytes differ from cumulative DOS loading, and REA still reports `mismatch`. That is correct, since the loaded image differs from a DOS load. For table `8,9`, the loaded bytes differed between two real runs (`00 04 10`, then `00 14 10`). This deserves its own issue. For evidence-bearing changes: - [x] Observed, derived, and inferred claims remain distinguishable. - [x] Artifact identity, source provenance, and failed attempts remain preserved. - [x] Unsupported, incomplete, unavailable, or uncertain outcomes remain visible. ## Validation performed - `npm run test:local -- src/ghidra/GhidraLoadImageValues.test.ts`: 38 passed + 1 failed on base, 39 passed with the fix. - `npm run test:focused -- src/ghidra/GhidraLoadImageValues.test.ts src/ghidra/GhidraDosComLoadImage.test.ts src/ghidra/GhidraRawBinaryLoadImage.test.ts src/ghidra/GhidraProvider.behavior.test.ts tests/boundary/cli/nativeEfficiencyComparison.test.ts …`: 9 files, 205 tests passed (combined run with the #3970 branch's files). - `npm run check:fast` (pre-push): 2/2 tasks successful. - Real Ghidra probe (`node scripts/rea.mjs mcp` over stdio, built tree): results above, run before and after the fix. ## Compatibility, safety, and release - Breaking changes or migration steps: none. - Real Hopper/Ghidra, browser, or OS coverage: Ghidra 12.1.4 on Linux x64. No macOS or Windows. - Package or release metadata impact: none. - Security, privacy, process, or containment review: no new process or file effects. The target is analyzed statically, never executed. ## Author readiness Human owner: @berelels - [ ] 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. ## Before merge - [ ] This PR addresses a concrete problem or an agreed enhancement. - [ ] Material scope and authority questions are resolved, and any required maintainer scope decision covers the final diff. - [ ] 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. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-31 01:11:48 +00:00
Sign in to join this conversation.
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
morluto/rea#6094
No description provided.