[PR #5582] [CLOSED] fix(apple): trace LC_LAZY_LOAD_DYLIB_INFO lazy dylib dependencies #6979

Closed
opened 2026-11-01 21:44:27 +00:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/morluto/rea/pull/5582
Author: @kaoru0822-kitauji
Created: 10/20/2026
Status: ❌ Closed

Base: main ← Head: fix/dylib-lazy-load-commands


📝 Commits (1)

  • 1ede7a0 fix(apple): trace LC_LAZY_LOAD_DYLIB_INFO lazy dylib dependencies

📊 Changes

8 files changed (+364 additions, -21 deletions)

View changed files

📝 docs/apple-application-analysis.md (+6 -3)
📝 src/artifacts/apple/MachoImage.fixture.ts (+67 -2)
📝 src/artifacts/apple/MachoLoadCommandReader.test.ts (+93 -0)
📝 src/artifacts/apple/MachoLoadCommandReader.ts (+109 -6)
📝 src/domain/apple/dylibResolution.test.ts (+1 -0)
📝 src/domain/apple/dylibResolution.ts (+13 -5)
📝 src/domain/apple/dylibResolutionFindings.ts (+6 -4)
📝 tests/boundary/cli/dylibResolutionCli.test.ts (+69 -1)

📄 Description

Summary

Fixes #5551

trace-dylib-resolution dropped dylibs linked with -lazy_library by current Apple linkers, but still reported coverage.status: "complete". These linkers encode the dependency as LC_LAZY_LOAD_DYLIB_INFO (0x3a) rather than the legacy LC_LAZY_LOAD_DYLIB (0x20). The dependency now shows up as a lazy edge. It resolves when the dylib is present. When the dylib is absent, the result has a lazy-load-unresolved finding, which stays distinct from a launch failure.

Problem and expected behavior

LC_LAZY_LOAD_DYLIB_INFO is a linkedit_data_command that points to a __LINKEDIT blob. That blob is dyld's LazyLoadDylibLinkEdit record: loadPathOffset, flagImageOffset, flags, pointerFormat, chainStartImageOffset, symbolsCount, symbolStringArrayOffset, followed by the path and symbol strings. The format is defined in apple-oss-distributions/dyld mach_o/LazyLoadDylib.{h,cpp}. MachoLoadCommandReader skipped unrecognized commands, so it never emitted this dependency. The trace then derived complete coverage from the dependencies it did emit.

Change and scope

  • Parser (owning boundary): MachoLoadCommandReader now reads each LC_LAZY_LOAD_DYLIB_INFO payload, in load-command order, as a dependency with command: "LC_LAZY_LOAD_DYLIB_INFO" and encoding: "lazy_load_dylib_info".
    • Validation follows LazyLoadDylib::valid(): header size, path offset and termination, symbol-array bounds, and data inside the slice. It skips the flag and chain-start VM-offset checks, because they need the VM layout and do not change which dylib is named.
    • flags & 1 (dylibMayBeMissing) maps to weak.
    • Payloads over the existing 16 MiB limit are reported as unsupported.
  • Lazy flag set once in the parser: dependencies get a parsed lazy boolean, set for both lazy commands. traceProcess and the findings now use lazy instead of comparing command names.
  • Weak lazy dylibs: a missing weak lazy-info dylib gets a "continues without it" explanation, because dyld's _dyld_lazy_load halts only when the dylib is not weak.
  • Null versions: lazy-info records carry no dylib versions, so current_version and compatibility_version are now nullable and are null for this encoding.
  • Not included: native inspect_macho, which goes through otool -l, still does not list lazy-info dependencies. That is a separate parser and tool contract, left for a follow-up.

Scope: an ordinary fix within existing boundaries; no maintainer scope decision is needed.

Contract and boundary impact

  • Semantic owner and earliest changed stage: src/artifacts/apple/MachoLoadCommandReader.ts (Mach-O load-command parsing).
  • Contribution scope fit and linked maintainer decision: not applicable (bug fix inside the existing static-parsing tool).
  • CLI and MCP/tool-catalog contract: additive changes to the trace_dylib_resolution output schema:
    • a new command enum value and a new encoding enum value;
    • a new lazy boolean on each edge;
    • current_version and compatibility_version are now nullable.
  • Provider, bridge, target-format, or platform compatibility: Mach-O only; no Apple tool required.
  • Evidence, artifact, provenance, or reconstruction contract: no change.
  • Target selection, authority, upstream behavior, and cleanup details: not applicable.
  • Generated metadata, package, or installation impact: none committed.

Expected effects:

  • 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:
    • tests/boundary/cli/dylibResolutionCli.test.ts: a CLI test with a lazy-info executable. With the dylib present, the edge resolves and there are no findings. With it absent, the trace reports lazy-load-unresolved. Coverage stays complete in both cases.
    • src/artifacts/apple/MachoLoadCommandReader.test.ts: decoding, including weak flags and multiple symbols, plus each malformed-payload rejection.
    • The MachoImage.fixture.ts builder now lays out the payload exactly as Apple's linker did for the issue repro.
  • Base reproduction: I rebuilt the issue repro on macOS 26.7.1 arm64 with Apple clang 21.0.0 and ld-27037.1, using -mmacosx-version-min=27.0. This linker ignores lazy-load at lower deployment targets. otool -l shows LC_LAZY_LOAD_DYLIB_INFO, datasize 56, and the payload bytes match the fixture layout. With the reader change reverted, the new reader tests fail: 4 failed / 26 passed.
  • CLI output on that real binary after the fix:
    • Dylib present: edges = [LC_LOAD_DYLIB /usr/lib/libSystem.B.dylib undetermined] and [LC_LAZY_LOAD_DYLIB_INFO @rpath/libl.dylib lazy=true resolved lib/libl.dylib]; findings: []; coverage.status: "complete".
    • Dylib moved away: the lazy edge is unresolved, with lazy-load-unresolved: "No candidate for lazily loaded @rpath/libl.dylib exists in the analyzed root; dyld loads it on first use, which would fail unless the image is supplied elsewhere."
  • Remaining proof gaps:
    • I did not run the binary. Its minos is 27.0 and this host runs 26.7.
    • The weak lazy-info path is checked only with synthetic bytes; no linker-produced weak lazy-info binary was tested.
    • Native inspect_macho coverage of lazy-info is not addressed.

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:focused -- tests/boundary/cli/dylibResolutionCli.test.ts tests/boundary/mcp/dylibResolutionMcp.test.ts src/artifacts/apple/MachoLoadCommandReader.test.ts src/domain/apple/dylibResolution.test.ts: 4 files, 153 tests passed.
  • npm run test:local -- src/contracts/toolOutputReferences.test.ts src/contracts/tupleSchemaParity.test.ts: 59 passed.
  • npm run check:fast: typecheck and lint passed.
  • npm run docs:check: passed.
  • node scripts/rea.mjs trace-dylib-resolution <clang-built lazy binary> --json, with the dylib present and absent: output as shown above.

Compatibility, safety, and release

  • Breaking changes or migration steps: none intended. Output enums gain values and the version fields become nullable; a strict consumer that assumed version strings would now see null for lazy-info edges.
  • Real Hopper/Ghidra, browser, or OS coverage: macOS 26.7.1 arm64 static trace of a real Apple-linker binary; no providers involved.
  • Package or release metadata impact: none.
  • Security, privacy, process, or containment review: read-only parsing with bounded reads (16 MiB payload limit, bounds-checked offsets).

Author readiness

Human owner: @kaoru0822-kitauji

  • 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/5582 **Author:** [@kaoru0822-kitauji](https://github.com/kaoru0822-kitauji) **Created:** 10/20/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `fix/dylib-lazy-load-commands` --- ### 📝 Commits (1) - [`1ede7a0`](https://github.com/morluto/rea/commit/1ede7a08647b485cdffe9918fdae278830bedcb8) fix(apple): trace LC_LAZY_LOAD_DYLIB_INFO lazy dylib dependencies ### 📊 Changes **8 files changed** (+364 additions, -21 deletions) <details> <summary>View changed files</summary> 📝 `docs/apple-application-analysis.md` (+6 -3) 📝 `src/artifacts/apple/MachoImage.fixture.ts` (+67 -2) 📝 `src/artifacts/apple/MachoLoadCommandReader.test.ts` (+93 -0) 📝 `src/artifacts/apple/MachoLoadCommandReader.ts` (+109 -6) 📝 `src/domain/apple/dylibResolution.test.ts` (+1 -0) 📝 `src/domain/apple/dylibResolution.ts` (+13 -5) 📝 `src/domain/apple/dylibResolutionFindings.ts` (+6 -4) 📝 `tests/boundary/cli/dylibResolutionCli.test.ts` (+69 -1) </details> ### 📄 Description ## Summary Fixes #5551 `trace-dylib-resolution` dropped dylibs linked with `-lazy_library` by current Apple linkers, but still reported `coverage.status: "complete"`. These linkers encode the dependency as `LC_LAZY_LOAD_DYLIB_INFO` (`0x3a`) rather than the legacy `LC_LAZY_LOAD_DYLIB` (`0x20`). The dependency now shows up as a lazy edge. It resolves when the dylib is present. When the dylib is absent, the result has a `lazy-load-unresolved` finding, which stays distinct from a launch failure. ## Problem and expected behavior `LC_LAZY_LOAD_DYLIB_INFO` is a `linkedit_data_command` that points to a `__LINKEDIT` blob. That blob is dyld's `LazyLoadDylibLinkEdit` record: `loadPathOffset`, `flagImageOffset`, `flags`, `pointerFormat`, `chainStartImageOffset`, `symbolsCount`, `symbolStringArrayOffset`, followed by the path and symbol strings. The format is defined in apple-oss-distributions/dyld `mach_o/LazyLoadDylib.{h,cpp}`. `MachoLoadCommandReader` skipped unrecognized commands, so it never emitted this dependency. The trace then derived complete coverage from the dependencies it did emit. ## Change and scope - **Parser (owning boundary):** `MachoLoadCommandReader` now reads each `LC_LAZY_LOAD_DYLIB_INFO` payload, in load-command order, as a dependency with `command: "LC_LAZY_LOAD_DYLIB_INFO"` and `encoding: "lazy_load_dylib_info"`. - Validation follows `LazyLoadDylib::valid()`: header size, path offset and termination, symbol-array bounds, and data inside the slice. It skips the flag and chain-start VM-offset checks, because they need the VM layout and do not change which dylib is named. - `flags & 1` (`dylibMayBeMissing`) maps to `weak`. - Payloads over the existing 16 MiB limit are reported as `unsupported`. - **Lazy flag set once in the parser:** dependencies get a parsed `lazy` boolean, set for both lazy commands. `traceProcess` and the findings now use `lazy` instead of comparing command names. - **Weak lazy dylibs:** a missing weak lazy-info dylib gets a "continues without it" explanation, because dyld's `_dyld_lazy_load` halts only when the dylib is not weak. - **Null versions:** lazy-info records carry no dylib versions, so `current_version` and `compatibility_version` are now nullable and are null for this encoding. - **Not included:** native `inspect_macho`, which goes through `otool -l`, still does not list lazy-info dependencies. That is a separate parser and tool contract, left for a follow-up. Scope: an ordinary fix within existing boundaries; no maintainer scope decision is needed. ## Contract and boundary impact - Semantic owner and earliest changed stage: `src/artifacts/apple/MachoLoadCommandReader.ts` (Mach-O load-command parsing). - Contribution scope fit and linked maintainer decision: not applicable (bug fix inside the existing static-parsing tool). - CLI and MCP/tool-catalog contract: additive changes to the `trace_dylib_resolution` output schema: - a new `command` enum value and a new `encoding` enum value; - a new `lazy` boolean on each edge; - `current_version` and `compatibility_version` are now nullable. - Provider, bridge, target-format, or platform compatibility: Mach-O only; no Apple tool required. - Evidence, artifact, provenance, or reconstruction contract: no change. - Target selection, authority, upstream behavior, and cleanup details: not applicable. - Generated metadata, package, or installation impact: none committed. Expected effects: - [x] 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:** - `tests/boundary/cli/dylibResolutionCli.test.ts`: a CLI test with a lazy-info executable. With the dylib present, the edge resolves and there are no findings. With it absent, the trace reports `lazy-load-unresolved`. Coverage stays complete in both cases. - `src/artifacts/apple/MachoLoadCommandReader.test.ts`: decoding, including weak flags and multiple symbols, plus each malformed-payload rejection. - The `MachoImage.fixture.ts` builder now lays out the payload exactly as Apple's linker did for the issue repro. - **Base reproduction:** I rebuilt the issue repro on macOS 26.7.1 arm64 with Apple clang 21.0.0 and ld-27037.1, using `-mmacosx-version-min=27.0`. This linker ignores lazy-load at lower deployment targets. `otool -l` shows `LC_LAZY_LOAD_DYLIB_INFO`, `datasize 56`, and the payload bytes match the fixture layout. With the reader change reverted, the new reader tests fail: 4 failed / 26 passed. - **CLI output on that real binary after the fix:** - Dylib present: `edges` = `[LC_LOAD_DYLIB /usr/lib/libSystem.B.dylib undetermined]` and `[LC_LAZY_LOAD_DYLIB_INFO @rpath/libl.dylib lazy=true resolved lib/libl.dylib]`; `findings: []`; `coverage.status: "complete"`. - Dylib moved away: the lazy edge is `unresolved`, with `lazy-load-unresolved`: "No candidate for lazily loaded @rpath/libl.dylib exists in the analyzed root; dyld loads it on first use, which would fail unless the image is supplied elsewhere." - **Remaining proof gaps:** - I did not run the binary. Its minos is 27.0 and this host runs 26.7. - The weak lazy-info path is checked only with synthetic bytes; no linker-produced weak lazy-info binary was tested. - Native `inspect_macho` coverage of lazy-info is not addressed. 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:focused -- tests/boundary/cli/dylibResolutionCli.test.ts tests/boundary/mcp/dylibResolutionMcp.test.ts src/artifacts/apple/MachoLoadCommandReader.test.ts src/domain/apple/dylibResolution.test.ts`: 4 files, 153 tests passed. - `npm run test:local -- src/contracts/toolOutputReferences.test.ts src/contracts/tupleSchemaParity.test.ts`: 59 passed. - `npm run check:fast`: typecheck and lint passed. - `npm run docs:check`: passed. - `node scripts/rea.mjs trace-dylib-resolution <clang-built lazy binary> --json`, with the dylib present and absent: output as shown above. ## Compatibility, safety, and release - Breaking changes or migration steps: none intended. Output enums gain values and the version fields become nullable; a strict consumer that assumed version strings would now see `null` for lazy-info edges. - Real Hopper/Ghidra, browser, or OS coverage: macOS 26.7.1 arm64 static trace of a real Apple-linker binary; no providers involved. - Package or release metadata impact: none. - Security, privacy, process, or containment review: read-only parsing with bounded reads (16 MiB payload limit, bounds-checked offsets). ## Author readiness Human owner: @kaoru0822-kitauji - [x] 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. - [x] I checked the evidence above, confirmed it supports the current change, and recorded meaningful validation gaps. - [x] 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-11-01 21:44:27 +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#6979
No description provided.