Skip to content

fix(native): retain every repeated otool load-command field - #4048

Merged
kaoru0822-kitauji merged 1 commit into
mainfrom
fix/4008-macho-linker-options
Oct 15, 2026
Merged

kaoru0822-kitauji merged 1 commit into
mainfrom
fix/4008-macho-linker-options

Conversation

@kaoru0822-kitauji

@kaoru0822-kitauji kaoru0822-kitauji commented Oct 15, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Fixes #4008. inspect-macho lost every LC_LINKER_OPTION string except the last one. The surviving value kept otool's #n prefix, and the collection still reported exhaustive: true with no limitations. Each string is now retained exactly, in order, and checked against the declared count. The same root cause also dropped all but the last tool from a multi-tool LC_BUILD_VERSION record, and that is fixed too.

Problem and expected behavior

parseFields in src/native/parsers/otool.ts turned every otool -l row into one entry of a flat key → value record, so a key printed twice overwrote the earlier value. Reproduced on main with the issue's command and a real clang object:

{"cmd":"LC_LINKER_OPTION","cmdsize":40,"count":2,"string":"#2 Foundation"}

otool -l prints string #1 -framework and string #2 Foundation. A three-tool LC_BUILD_VERSION (built with vtool) kept only tool: 2, version: "6.0" in fields. Its build_metadata was already complete, because that path re-reads the block.

Change and scope

  • At the producing boundary (parseFields / parseField):
    • Linker-option rows keep otool's own ordinal label as the key (string #1, string #2, …), at every count. The value is the exact option text, including surrounding spaces, and is never coerced to a number.
    • Any other key printed more than once is labeled key #n in print order. Keys printed once are unchanged, so existing single-tool and other records keep their shape.
  • New validateLinkerOptionLines: count must be unique, and the remaining rows must be exactly string #1..count, in order. A mismatch, a repeated or out-of-order ordinal, or an unlabeled row throws, the same way the existing build-tool, section-count and dylib framing checks do.
  • validateDylibCommandLines now checks encoding-field presence on the parsed rows rather than the labeled record. Repeated options rows are still rejected with the existing reason.
  • docs/native-investigation.md documents the name #n and string #n keys.
  • No schema change: fields remains record<string, string | number | null>.

Contract and boundary impact

  • Semantic owner and earliest changed stage: src/native/parsers/otool.ts (parseOtoolLoadCommands), which owns the parsing of otool load-command text. No downstream code reads fields keys.

  • Contribution scope fit and linked maintainer decision: a bug fix within the existing static, read-only boundary; not applicable.

  • CLI and MCP/tool-catalog contract: output schema unchanged. load_commands.items[].fields now keeps every repeated row. A single string key on LC_LINKER_OPTION becomes string #n.

  • Provider, bridge, target-format, or platform compatibility: macOS native provider (otool) only.

  • Evidence, artifact, provenance, or reconstruction contract: removes silent loss from exhaustive Evidence. Malformed linker-option framing now fails closed instead of returning a lossy record. On such input llvm-nm already fails the inspection.

  • 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:
    • tests/boundary/cli/nativeMachoLinkerOptionsCli.test.ts (macOS only): a public CLI workflow on a real xcrun clang -c object with two linker options.
    • tests/boundary/providers/native/nativeLoadKinds.golden.test.ts:
      • exact, ordered strings, including surrounding spaces and a numeric-looking option;
      • rejection of fewer or more strings than count, out-of-order or repeated ordinals, and an unlabeled row;
      • full fields of the existing two-tool LC_BUILD_VERSION case, plus the unchanged single-tool shape.
  • Base reproduction: with main's otool.ts restored, these tests fail (8 failed / 47 passed across the two files). With the fix, they pass.
  • User-visible CLI output, real object from the issue plus a three-string option, inspect-macho --json:
before after
LC_LINKER_OPTION (count 2) "string":"#2 Foundation" "string #1":"-framework","string #2":"Foundation"
LC_LINKER_OPTION (count 3) "string":"#3 -ObjC" "string #1":"-weak_framework","string #2":"Security","string #3":"-ObjC"
LC_BUILD_VERSION (3 tools, vtool) "tool":2,"version":"6.0" "tool #1":3,"version #1":"1100.1",…,"tool #3":2,"version #3":"6.0"
Evidence size (options.o) 7,093 bytes 7,171 bytes (+78, the previously lost strings)
  • Performance: parseOtoolLoadCommands on /opt/homebrew/bin/node's otool -h -l output (45 commands, 364 lines, no repeated keys) has a median of 0.234 ms on base and 0.294 ms with the fix, over 200 runs after warm-up. The serialized commands are byte-identical (9,542 bytes). The CLI inspection takes ~1.1 s either way, dominated by the external tools.
  • Remaining proof gaps: no CI coverage on Intel macOS. LC_LINKER_OPTION strings containing a newline (which otool prints ambiguously) now fail closed rather than being guessed. That behavior is exercised only through the unlabeled-row test.

Validation performed

  • npm run test:focused -- <8 native Mach-O parser/CLI test files> — 126 passed.
  • npx vitest run tests/boundary/providers/native tests/boundary/native tests/boundary/cli/nativeMacho tests/boundary/cli/dylib tests/boundary/mcp/analysisView — 38 files, 375 passed.
  • The same two new or extended test files against main's parser — 8 failed, as expected.
  • npm run check:fast, npx oxfmt --check on the changed files, npm run docs:check — passed.
  • Real macOS arm64 workflow: Apple clang .linker_option object, a vtool three-tool executable, and /opt/homebrew/bin/node, run through the built CLI (node scripts/rea.mjs inspect-macho … --json).

Compatibility, safety, and release

  • Breaking changes or migration steps: none to the advertised schema. fields is an untyped raw-row record. LC_LINKER_OPTION records now carry string #1..count instead of one string key. That key only ever held the last option, with otool's #n prefix, so it never carried a complete value. Records whose keys appear once are unchanged. Maintainers may prefer a ! marker; I can add one on request.
  • Real Hopper/Ghidra, browser, or OS coverage: macOS arm64 with the Command Line Tools otool and llvm-nm.
  • Package or release metadata impact: none.
  • Security, privacy, process, or containment review: static parsing only. No new processes or file effects.

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.

🤖 Generated with Claude Code

`inspect-macho` stored each `otool -l` row in a flat record, so a key
printed more than once kept only its last value. A two-string
LC_LINKER_OPTION lost `-framework` and kept `#2 Foundation`, and a
multi-tool LC_BUILD_VERSION kept only its last tool, while the
collection still claimed exhaustive coverage.

Linker-option strings are now keyed by otool's own `string #n` label
with the exact option text and must match the declared count. Any other
repeated key is labeled `key #n` in print order. Dylib encoding checks
read the parsed rows, so repeated `options` rows are still rejected.

Fixes #4008

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@kaoru0822-kitauji kaoru0822-kitauji added the track:perf Performance track PRs (issues & perf loop) label Oct 15, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 15, 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 ✅ Completed 2026-10-15T09:22:05.793668Z f6c07f3 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 Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

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

Summary

Preserves repeated otool fields instead of overwriting earlier values.

  • Mach-O load-command fields keep every repeated otool row.

No actionable defects or new security issues were found. Tests were not run during this review. Superseded status could not be checked because GitHub authentication was unavailable.

kaoru0822-kitauji explicitly identifies rejection of newline-containing options and renamed public keys as intentional in the PR description.

Reviews (1) · Last reviewed commit: "fix(native): retain every repeated otool..." · Reviewed by Greptile

@kaoru0822-kitauji
kaoru0822-kitauji merged commit 47b4628 into main Oct 15, 2026
33 of 34 checks passed
@kaoru0822-kitauji
kaoru0822-kitauji deleted the fix/4008-macho-linker-options branch October 15, 2026 09:29
@morluto morluto added area: native Native analysis and decompilation, including Ghidra, IDA, and Hopper analysis behavior bug Something isn't working labels 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 bug Something isn't working track:perf Performance track PRs (issues & perf loop)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Mach-O load-command evidence drops earlier LC_LINKER_OPTION strings

2 participants