Skip to content

refactor(parsers): select once-only sections and JSON fields through one keyed owner - #10740

Merged
morluto merged 1 commit into
mainfrom
refactor/keyed-duplicate-owner
Nov 15, 2026
Merged

morluto merged 1 commit into
mainfrom
refactor/keyed-duplicate-owner

Conversation

@morluto

@morluto morluto commented Nov 15, 2026

Copy link
Copy Markdown
Owner

Summary

Duplicate handling in REA's parsers is decided by whichever lookup a parser author happened to use: find keeps the first declaration, Map.set and JSON.parse keep the last, and hand-written filter(...).length > 1 checks reject. This PR adds one owner for keyed lookups, src/domain/keyedCollection.ts, and moves the first slice onto it: the JSON reader and the section lookups that must not lose a declaration. It also records the target design so later slices follow the same shape.

Refs the W7 duplicate-key workstream (structural audit report 06, finding 5).

Keyed<T> design (target model)

  • A parser that produces a keyed table (section table, plist dictionary, JSON object, FDT node) keeps every declaration: Keyed<T> = { entries: { key, ordinal, value }[], duplicates: key[] }, built once by keyedBy(values, keyOf). Members without a key (unnamed sections) are not keyed.
  • Consumers choose what a repeat means through a selector, never through find/Map.set defaults:
    • uniqueKeyed(keyed, key): the format allows the key once. Returns Result<T | undefined, { kind: "duplicate", key, ordinals }>. Absent is undefined; repeated is a typed failure carrying every ordinal.
    • all (later slice): every declaration, for tables where repeats are legal (__objc_classlist in several segments, ELF named() helpers).
    • first / last (later slice): only where the format's own consumer defines precedence (JSON client configs: last), and they take a declared reason.
  • Text formats whose library collapses keys before REA sees them (JSON, plist) detect repeats from the source keys at the decoder: JSON through findDuplicateJsonMember (src/domain/jsonDuplicateMember.ts), plists through fix(plist): record dictionary keys declared more than once #10737.
  • Each format's output records its policy where it keeps going (as apiContract reports duplicate_json_members), or fails malformed where the format allows one.
  • Guard (later slice, once the native find sites are migrated): a lint rule banning sections.find( on parsed native tables, with a counted baseline.

Selectors are added when a migrated consumer needs them; this slice only needs uniqueKeyed.

Problem and expected behavior

Trigger: a Go Mach-O whose __go_buildinfo section is declared twice, the second in an unmapped segment.

Before: the build-info search read the first declaration, then the second, and failed with "Mach-O build-info section is in an unmapped segment"; whether ambiguity or a per-section defect was reported depended on declaration order (ELF checked ambiguity inside the loop, Mach-O after it).

After: both formats select the build-info section once, through uniqueKeyed, and a repeat fails with "... ambiguous build-info sections" before either declaration is read. The Rust cargo auditable dependency section uses the same owner, and its dependency JSON drops a private jsonc-parser visitor that reimplemented duplicate-member detection in favour of findDuplicateJsonMember.

Change and scope

  • src/domain/keyedCollection.ts (new): Keyed<T>, keyedBy, uniqueKeyed, DuplicateKey.
  • src/go/GoBuildInfoSearch.ts: ELF .go.buildinfo and Mach-O __go_buildinfo selection through uniqueKeyed; the two ad hoc ambiguity checks are removed. Per-section validation is unchanged.
  • src/rust/RustBinaryImage.ts: dependency section (__DATA,.dep-v0 / .dep-v0) through uniqueKeyed, replacing filter(...).length > 1.
  • src/rust/RustAuditableReader.ts: the dependency JSON is parsed with JSON.parse and checked with findDuplicateJsonMember; the jsonc-parser visitor copy is deleted. Same failure kinds and details.

Not included (later slices): the other JSON detectors (losslessJson.ts, CaptureJson.ts, SafetensorsHeader.ts, GgufHeader.ts parse their own trees and already reject inline); first-match native section find sites (ELF/PE: NativeExportParser, ElfHardening, ElfUnwindTables, nativeAotPe, ManagedPeReader); all helpers (ElfUnwindTables.named, KernelModuleParser.sectionsNamed); the lint guard.

Contribution scope: ordinary refactor within existing boundaries; not applicable.

Contract and boundary impact

  • Semantic owner and earliest changed stage: keyedCollection.ts owns once-only selection; jsonDuplicateMember.ts owns JSON repeated-member detection. Consumers: Go build-info search, Rust dependency section and JSON. Superseded: Go's two inline ambiguity checks, Rust's filter count check and its jsonc-parser duplicate visitor.

  • Contribution scope fit and linked maintainer decision: W7 part 6 ("short Keyed<T> design in the PR body, first slice").

  • CLI and MCP/tool-catalog contract: none.

  • Provider, bridge, target-format, or platform compatibility: a Go binary with a repeated build-info section whose later copy is defective now reports ambiguity instead of the defect; both are malformed.

  • Evidence, artifact, provenance, or reconstruction contract: none.

  • 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/domain/keyedCollection.test.ts: source order and ordinals, unkeyed members skipped, single/absent selection, typed refusal with every ordinal.
    • tests/boundary/go/goBinaryImage.test.ts: the unmapped-segment fixture now builds both cases; "rejects a repeated Mach-O build-info section before reading either" fails on the base revision (it reported "unmapped"), and the unmapped case keeps its coverage with a single declaration.
    • Existing src/rust/RustAuditableReader.test.ts duplicate-field cases (packages, name, dependencies) and tests/boundary/rust/rustBinaryImage.test.ts pass unchanged.
  • Base reproduction: shown above for Go.

  • Remaining proof gaps: no real Go/Rust binary with repeated sections was available; fixtures only.

  • 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/domain/keyedCollection.test.ts src/rust/RustAuditableReader.test.ts — passed.
  • npm run test:focused -- tests/boundary/go/goBinaryImage.test.ts — 113 passed; with GoBuildInfoSearch.ts reverted, the new repeated-section test fails.
  • npm run test:focused -- tests/boundary/rust/rustBinaryImage.test.ts — passed.
  • npm run typecheck — passed. npx oxlint on changed files — no errors. npx oxfmt --check src tests — clean.
  • node scripts/verify/architecture-guards.mjs and npm run verify:module-boundaries — passed.

Compatibility, safety, and release

  • Breaking changes or migration steps: none; failure kind unchanged, message differs only for repeated build-info sections.
  • Real Hopper/Ghidra, browser, or OS coverage: not applicable.
  • Package or release metadata impact: none.
  • Security, privacy, process, or containment review: none.

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.

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.

@chatgpt-codex-connector

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

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-15T02:03:28.868353Z 82d3193 PR opened
ℹ️ 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.

@greptile-apps

greptile-apps Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The reviewed changes appear safe to merge.

Summary

This PR gives Go and Rust one shared way to select sections that may appear only once.

  • Go and Rust binary readers reject repeated named sections before choosing a copy.
  • Rust dependency JSON checks repeated fields with the shared JSON scanner.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Parsed native sections] --> B[keyedBy retains named declarations]
  B --> C[uniqueKeyed selects requested key]
  C --> D[Absent: existing fallback or no metadata]
  C --> E[One: check section and read metadata]
  C --> F[Repeated: malformed failure]
  E --> G[Rust dependency JSON]
  G --> H[Check depth and parse JSON]
  H --> I[findDuplicateJsonMember]
  I --> J[Repeated: malformed failure]
  I --> K[Distinct: check schema and graph limits]
Loading

Reviews (2) · Last reviewed commit: "refactor(parsers): select once-only sect..." · Reviewed by Greptile

@morluto
morluto merged commit eca039b into main Nov 15, 2026
53 checks passed
@morluto
morluto deleted the refactor/keyed-duplicate-owner branch November 15, 2026 05:45
@morluto morluto added the area: native Native analysis and decompilation, including Ghidra, IDA, and Hopper analysis behavior label Nov 26, 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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant