Skip to content

refactor(macho): name load-command codes through the shared LC table - #10714

Merged
morluto merged 12 commits into
mainfrom
refactor/macho-load-command-literals
Nov 15, 2026
Merged

morluto merged 12 commits into
mainfrom
refactor/macho-load-command-literals

Conversation

@morluto

@morluto morluto commented Nov 15, 2026 •

Copy link
Copy Markdown
Owner

Summary

Mach-O load-command readers now name every command code through the shared LC table in src/domain/apple/machoLoadCommandCodes.ts instead of hex literals. The table gains LC_REQ_DYLD. A new counted architecture guard, macho-load-command-literal, fails when a load-command reader compares a command against a literal again. It starts at zero.

Part of plan W7 (parser kernel: one format vocabulary table).

This PR also carries collapsed stacked PRs:

Problem and expected behavior

machoLoadCommandCodes.ts says it is the one table of load-command codes, but six readers still restated codes as literals:

  • src/native/MachoCommandNames.ts: 18 comparisons, plus the dylib and lc_str command sets.
  • src/native/metadata/MachoImage.ts: 3.
  • src/native/AppleMachoSelection.ts: 2.
  • src/domain/apple/machoLoadCommands.ts: the fixed-word layout table keys.
  • src/domain/apple/machoDylibCommand.ts: the dependency command map.
  • src/artifacts/apple/MachoLoadCommandReader.ts: the VERSION_MIN_* platform map.
  • src/native/MachoBinaryProjection.ts: two literal code lists, an indexOf platform lookup, and the string-field map keys.

A reader of frame.command === 0x80000028 has to know that this is LC_MAIN, and a typo in one copy goes unnoticed.

Expected: every load-command reader uses LC.<loader.h name>, and the guard keeps new literals out.

Change and scope

  • Replaces each literal with its LC member. The values are identical, so behavior is unchanged.
  • MachoCommandNames.ts also accepts the dylib codes without LC_REQ_DYLD (0x18, 0x1f, 0x23). These are now written withoutRequiredByDyld(LC.LOAD_WEAK_DYLIB) and so on, using the new LC_REQ_DYLD export. The accepted set is unchanged.
  • The LC_VERSION_MIN_* -> platform map moves to its domain owner src/domain/apple/machoPlatform.ts (MACHO_VERSION_MIN_PLATFORMS). MachoCommandNames records the platform in legacyBuild.platform, and MachoBinaryProjection reads that fact instead of re-deriving it from the command code.
  • scripts/verify/architecture-guards.mjs adds macho-load-command-literal. It applies to files that use MachoLoadCommandCursor, readMachoLoadCommand* or import machoLoadCommandCodes, and counts command, frame.command, command.kind or cmd compared against a numeric literal or looked up in a literal code list ([..].includes(..) / .indexOf(..)). The baseline is empty, so any new site fails. Run against the main versions, the rule finds 18 matches in MachoCommandNames.ts and 4 in MachoImage.ts.

Not included (separate W7 items):

Contract and boundary impact

  • Semantic owner and earliest changed stage: src/domain/apple/machoLoadCommandCodes.ts stays the one owner of load-command codes. The six readers consume it.

  • Contribution scope fit and linked maintainer decision: not applicable. This is an internal refactor.

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

  • Provider, bridge, target-format, or platform compatibility: none. Every value is unchanged.

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

Evidence and regression coverage

  • Tests added or updated: MachoCommandNames.test.ts and MachoBinaryProjection.test.ts now expect legacyBuild.platform. The values are otherwise unchanged, and the existing Mach-O tests and goldens cover every touched reader. The new guard is the regression check for the duplication itself.
  • Base reproduction or other evidence: the guard's pattern, run against the main versions of MachoCommandNames.ts and MachoImage.ts, finds 18 and 4 literal comparisons. It finds 0 on this branch. Restoring this branch's pre-review MachoCommandNames.ts and MachoBinaryProjection.ts makes the guard fail (2 and 1 matches of the list spelling).
  • User-visible CLI/MCP output: none.
  • Remaining proof gaps: the guard matches the common variable names (command, frame.command, command.kind, cmd) in load-command readers. A literal compared through another name, or used as a Map key, would not be caught.

Validation performed

  • npm run test:focused -- src/artifacts/apple/MachoEncryptionReader.test.ts src/artifacts/apple/machoContainer.test.ts src/native/MachoProjectionBudget.test.ts src/native/MachoCommandNames.test.ts src/native/NativeMachoInspection.test.ts src/artifacts/apple/MachoLoadCommandReader.test.ts src/native/AppleMachoSelection.test.ts src/native/MachoBinaryProjection.test.ts src/domain/apple/machoLoadCommands.test.ts tests/boundary/go/goBinaryImage.test.ts tests/boundary/providers/native/nativeLoadText.golden.test.ts tests/boundary/providers/native/nativeLoadKinds.golden.test.ts tests/boundary/providers/native/nativeMachoHeader.golden.test.ts src/native/NativeMachoSourceProvider.test.ts: 14 files, 465 tests passed.

  • node scripts/verify/architecture-guards.mjs: passed.

  • After the review fix: npm run test:local -- src/native/MachoBinaryProjection.test.ts src/native/MachoCommandNames.test.ts src/native/NativeMachoInspection.test.ts src/artifacts/apple/MachoLoadCommandReader.test.ts: 133 passed; npm run check:fast and the guards passed.

  • npm run check:fast: typecheck and lint passed.

  • npx oxfmt --check on the changed folders: passed.

  • Follow-up (62c1f68): src/domain/apple/plistValue.test.ts still expected RangeError from the nesting limit, which refactor(plist): report property-list and keyed-archive failures through ParseFailure #10822 made a PlistNestingLimitError (ParseFailureError limit). It now expects PlistNestingLimitError. npm run test:local over the 27 sibling tests of the changed sources had failed only this test, and it passes after the fix.

  • Review fix (541793e, Greptile): the selected Mach-O's parse failures now go through parseFailureAnalysisError. A malformed or truncated selected Mach-O is reported as invalid input on path (invalid_request, invalid_format) instead of unreadable_output, and an unsupported one as unsupported_target. npm run test:local over 8 Mach-O source test files passed (176). npm run test:focused over the native provider boundary tests passed (nativeProvider, nativeMachoUuidlessInspection, nativeSelectedArchitecture, objcClassMetadataFallback, nativeMachoMissingUuidCli, foreignUniversalMachoCli, nativeMachoSummaryMcp, lipoFieldOwnership, nativeInventory.golden, nativeMachoEncryptionBinding, nativeReexport.golden, binaryTarget, nativeExports). After merging current main: npx oxfmt --check ., npm run check:fast and node scripts/verify/architecture-guards.mjs passed.

Compatibility, safety, and release

  • Breaking changes or migration steps: none.
  • Real Hopper/Ghidra, browser, or OS coverage: not applicable.
  • Package or release metadata impact: none.
  • Security, privacy, process, or containment review: no change.

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-15T10:12:13.573461Z d4abb73 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.

@greptile-apps

greptile-apps Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5 Tier: plus

[High impact] This PR appears safe to merge; no new actionable issues were found.

Summary

The PR replaces repeated Mach-O command numbers with the shared LC table and moves legacy platform lookup to its domain owner. It also moves the stacked parser changes onto ParseFailureError.

  • Mach-O readers name load commands through one shared table.
  • Format readers report input failures through shared parse categories.
  • Malformed selected Mach-O files are reported as input failures.
  • Bridge response streams return invalid UTF-8 as a parse failure.
  • Managed graph input without an inspection result returns an input error.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart LR
  LC["Shared LC table"] --> Readers["Mach-O readers"]
  Platform["Shared platform map"] --> Readers
  Readers --> Facts["Parsed command facts"]
  Facts --> Projection["Binary projection"]
  Parsers["Format readers"] --> Failure["ParseFailureError"]
  Failure --> Owner["Owning caller"]
  Owner --> Result["Input refusal, fallback, or named limit"]
Loading

Reviews (11) · Last reviewed commit: "Merge remote-tracking branch 'origin/mai..." · Reviewed by Greptile

Comment thread scripts/verify/architecture-guards.mjs Resolved
…gh ParseFailure (#10760)

* refactor(parsers): report XML, web artifact and Mach-O failures through ParseFailure

* refactor(xml): type XML parse failures as malformed or unsupported only
…es (#10794)

* refactor(parsers): report apktool strings and XML plist source failures through ParseFailure

* refactor(apktool): drop the unused ParseFailureError import
@morluto
morluto force-pushed the refactor/macho-load-command-literals branch from 8e7d93e to 2245574 Compare November 15, 2026 07:42
…graph failure classes (#10818)

* refactor(parsers): retire the Mach-O, source-map, NDJSON and managed-graph failure classes

* refactor(plist): report property-list and keyed-archive failures through ParseFailure (#10822)

* refactor(plist): report property-list and keyed-archive failures through ParseFailure

* refactor(kuna): report Kuna document failures through ParseFailure (#10824)

* refactor(kuna): report Kuna document failures through ParseFailure

* test(kuna): pin the limit and malformed facts of Kuna document failures

* refactor(plist): report plist decode budgets through ParseFailure (#10842)

* refactor(plist): report plist decode budgets through ParseFailure

The plist preflight owner throws a ParseFailure limit (plist-decode-bytes) for its byte budget, the shared PlistNestingLimitError for nesting depth, and a malformed failure for a reference cycle, instead of a per-format PropertyListDecodeBudgetFailure whose meaning each caller re-mapped through a fail callback. The fail option, PropertyListDecodeBudgetFailure and interfaceBuilderDecodeBudgetFailure are removed; the parser-format-failure-class baseline reaches zero.

* refactor(plist): report keyed-archive allowances as typed limit failures (#10844)

keyedArchive.ts and decodeKeyedArchiveBytes throw decodeLimit parse failures (keyed-archive-bytes, -object-table-entries, -values-per-object, -reference-edges) and malformed parse failures instead of plain RangeError/TypeError. KeyedArchiveReader no longer classifies every RangeError as a limit: only a limit parse failure or a host stack/capacity limit is one. The unreachable 128-level scan nesting check is removed; projectPlistValue already refuses the archive first.
…seFailure and defects through invariantViolation (#10895)

* refactor(plist): report plist and NIBArchive input faults through ParseFailure and defects through invariantViolation

* fix(plist): report a cut-short NIB header or plist length record as truncated
…ommand-literals

# Conflicts:
#	scripts/verify/architecture-guard-baselines.json
…ommand-literals

# Conflicts:
#	src/ghidra/GhidraClient.ts
@morluto

morluto commented Nov 15, 2026

Copy link
Copy Markdown
Owner Author

Merged current main (2ab693a). Two fixes:

  • GhidraClient.ts import conflict: kept main's AbortedWait and SharedStartupAttempt API, and this PR's removal of Utf8NdjsonDecodingError.
  • The broadened parser-format-failure-class guard here now matches UnsupportedEvidenceFormatError extends TypeError in src/domain/evidenceFormat.ts, which main added after this PR was written. It is counted in the baseline at 1 rather than migrated here, because that file belongs to the evidence-format workstream. Converting it to ParseFailure is follow-up work.

Validation on the merged tree: oxfmt --check . clean, npm run check:fast passed, architecture-guards.mjs passed, npm run test:local -- src/ghidra/GhidraResponseRouter.test.ts src/process/Utf8NdjsonLineBuffer.test.ts 11 passed, and briefs/local-gate.sh 10714 exited 0.

Comment thread src/native/NativeMacOSProvider.ts Resolved
# Conflicts:
#	scripts/verify/architecture-guard-baselines.json
… shared owner

nativeMachoSourcePrimary mapped every non-limit ParseFailureError to
AnalysisOutputError, so a malformed or truncated selected Mach-O was
reported as unreadable tool output and an unsupported one lost its
category. It now uses parseFailureAnalysisError like the ELF, Go and
other byte-format providers: malformed and truncated bytes are an
invalid path, unsupported bytes an unsupported target, and a limit
still falls through to its producer's budget.
# Conflicts:
#	scripts/verify/architecture-guard-baselines.json
#	src/kuna/KunaDocumentError.ts
#	src/kuna/KunaProvider.ts
@morluto
morluto merged commit 78fd365 into main Nov 15, 2026
7 checks passed
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