Repository navigation
fix(apple): trace LC_LAZY_LOAD_DYLIB_INFO lazy dylib dependencies - #5582
kaoru0822-kitauji wants to merge 1 commit into
Conversation
Apple's linker encodes -lazy_library as LC_LAZY_LOAD_DYLIB_INFO (0x3a), a linkedit_data_command whose __LINKEDIT payload is dyld's LazyLoadDylibLinkEdit record. The Mach-O load-command reader ignored the command, so trace-dylib-resolution dropped the dependency while reporting complete coverage. Decode the record at the owning parser, validating it as dyld's LazyLoadDylib::valid() does for the fields that name the dylib, and normalize lazy semantics once as a dependency 'lazy' flag that the trace and findings consume instead of matching command names. Lazy-info records carry no dylib versions, so those fields are null for them. Fixes #5551 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
[Medium impact] The change affects the bounded Apple Mach-O dylib tracing workflow and its output contract. Add a shared payload-read budget before merging so crafted files cannot tie up the reader. FindingsFix with agent prompt### Issue 1
src/artifacts/apple/MachoLoadCommandReader.ts:534-539
Each `LC_LAZY_LOAD_DYLIB_INFO` command can read another 16 MiB, but nothing limits the total across commands. A crafted file can contain 65,536 commands pointing to the same 16 MiB payload. That roughly 17 MiB file makes the reader allocate and read 1 TiB before finishing. Previously, these commands caused no extra reads.
Add a shared read/work budget and return `unsupported` when it is exhausted. The per-record limit alone does not bound this parser.
**How this was verified:** File-controlled commands each trigger a fresh payload allocation and file read, while the bounds checks limit only each individual read.
### Issue 2
src/artifacts/apple/MachoLoadCommandReader.test.ts:359-365
The new lazy-info tests cover only thin images. This is the reader's new path for fetching dependency data outside the load commands, and it depends on adding `sliceOffset` to `dataOffset`. The existing FAT tests use only in-command strings, so they would not catch reading a lazy payload from the wrong place.
Add a FAT fixture with lazy-info payloads in nonzero-offset slices and assert their decoded paths.
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.SummaryAdds
Review used source inspection; no runtime tests were run. GitHub status and superseding PRs could not be checked because authentication was unavailable. Acknowledged scope and proof gaps:
Diagram%%{init: {'theme': 'neutral'}}%%
flowchart LR
A[Mach-O commands] --> B[Read lazy-info payload]
B --> C[Dependency: lazy true]
C --> D[Resolve candidate]
D --> E[Emit edge and findings]
E --> F[Skip launch traversal]
Reviews (1) · Last reviewed commit: "fix(apple): trace LC_LAZY_LOAD_DYLIB_INF..." · Reviewed by Greptile |
| const blob = await readExact( | ||
| readAt, | ||
| slice.sliceOffset + dataOffset, | ||
| dataSize, | ||
| `load command ${index} LC_LAZY_LOAD_DYLIB_INFO data`, | ||
| ); |
There was a problem hiding this comment.
Each LC_LAZY_LOAD_DYLIB_INFO command can read another 16 MiB, but nothing limits the total across commands. A crafted file can contain 65,536 commands pointing to the same 16 MiB payload. That roughly 17 MiB file makes the reader allocate and read 1 TiB before finishing. Previously, these commands caused no extra reads.
Add a shared read/work budget and return unsupported when it is exhausted. The per-record limit alone does not bound this parser.
How this was verified: File-controlled commands each trigger a fresh payload allocation and file read, while the bounds checks limit only each individual read.
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/artifacts/apple/MachoLoadCommandReader.ts
Line: 534-539
Comment:
**Small files force huge reads**
Each `LC_LAZY_LOAD_DYLIB_INFO` command can read another 16 MiB, but nothing limits the total across commands. A crafted file can contain 65,536 commands pointing to the same 16 MiB payload. That roughly 17 MiB file makes the reader allocate and read 1 TiB before finishing. Previously, these commands caused no extra reads.
Add a shared read/work budget and return `unsupported` when it is exhausted. The per-record limit alone does not bound this parser.
**How this was verified:** File-controlled commands each trigger a fresh payload allocation and file read, while the bounds checks limit only each individual read.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.| const facts = await read( | ||
| machoImage({ | ||
| commands: [dylibCommand(LC.LOAD_DYLIB, "/usr/lib/libSystem.B.dylib")], | ||
| linkedit: [ | ||
| { | ||
| command: LC.LAZY_LOAD_DYLIB_INFO, | ||
| data: lazyLoadDylibInfo("@rpath/libl.dylib"), |
There was a problem hiding this comment.
Universal binaries lack test coverage
The new lazy-info tests cover only thin images. This is the reader's new path for fetching dependency data outside the load commands, and it depends on adding sliceOffset to dataOffset. The existing FAT tests use only in-command strings, so they would not catch reading a lazy payload from the wrong place.
Add a FAT fixture with lazy-info payloads in nonzero-offset slices and assert their decoded paths.
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/artifacts/apple/MachoLoadCommandReader.test.ts
Line: 359-365
Comment:
**Universal binaries lack test coverage**
The new lazy-info tests cover only thin images. This is the reader's new path for fetching dependency data outside the load commands, and it depends on adding `sliceOffset` to `dataOffset`. The existing FAT tests use only in-command strings, so they would not catch reading a lazy payload from the wrong place.
Add a FAT fixture with lazy-info payloads in nonzero-offset slices and assert their decoded paths.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 1ede7a0864
ℹ️ 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".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| const blob = await readExact( | ||
| readAt, | ||
| slice.sliceOffset + dataOffset, | ||
| dataSize, |
There was a problem hiding this comment.
Cap duplicate lazy-info payload reads
When an input contains many 16-byte LC_LAZY_LOAD_DYLIB_INFO commands that point at the same payload, every command performs a fresh readAt here. The 16 MiB load-command limit still permits about one million such commands, each referring to a 16 MiB valid payload, so a roughly 32 MiB accepted Mach-O can force approximately 16 TiB of repeated file reads and allocations before tracing completes. Cache payloads by (sliceOffset, dataOffset, dataSize) or apply a cumulative payload-read budget.
AGENTS.md reference: AGENTS.md:L48-L52
Useful? React with 👍 / 👎.
|
Superseded by #5650 ( |
Summary
Fixes #5551
trace-dylib-resolutiondropped dylibs linked with-lazy_libraryby current Apple linkers, but still reportedcoverage.status: "complete". These linkers encode the dependency asLC_LAZY_LOAD_DYLIB_INFO(0x3a) rather than the legacyLC_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 alazy-load-unresolvedfinding, which stays distinct from a launch failure.Problem and expected behavior
LC_LAZY_LOAD_DYLIB_INFOis alinkedit_data_commandthat points to a__LINKEDITblob. That blob is dyld'sLazyLoadDylibLinkEditrecord:loadPathOffset,flagImageOffset,flags,pointerFormat,chainStartImageOffset,symbolsCount,symbolStringArrayOffset, followed by the path and symbol strings. The format is defined in apple-oss-distributions/dyldmach_o/LazyLoadDylib.{h,cpp}.MachoLoadCommandReaderskipped unrecognized commands, so it never emitted this dependency. The trace then derived complete coverage from the dependencies it did emit.Change and scope
MachoLoadCommandReadernow reads eachLC_LAZY_LOAD_DYLIB_INFOpayload, in load-command order, as a dependency withcommand: "LC_LAZY_LOAD_DYLIB_INFO"andencoding: "lazy_load_dylib_info".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 toweak.unsupported.lazyboolean, set for both lazy commands.traceProcessand the findings now uselazyinstead of comparing command names._dyld_lazy_loadhalts only when the dylib is not weak.current_versionandcompatibility_versionare now nullable and are null for this encoding.inspect_macho, which goes throughotool -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
src/artifacts/apple/MachoLoadCommandReader.ts(Mach-O load-command parsing).trace_dylib_resolutionoutput schema:commandenum value and a newencodingenum value;lazyboolean on each edge;current_versionandcompatibility_versionare now nullable.Expected effects:
Evidence and regression coverage
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 reportslazy-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.MachoImage.fixture.tsbuilder now lays out the payload exactly as Apple's linker did for the issue repro.-mmacosx-version-min=27.0. This linker ignores lazy-load at lower deployment targets.otool -lshowsLC_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.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".unresolved, withlazy-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."inspect_machocoverage of lazy-info is not addressed.For evidence-bearing changes:
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
nullfor lazy-info edges.Author readiness
Human owner: @kaoru0822-kitauji
Before merge
type(scope): outcome.🤖 Generated with Claude Code