Repository navigation
feat(native): list Mach-O function starts and data-in-code entries offline - #14897
Conversation
…fline The diff adds a static reader for LC_FUNCTION_STARTS and LC_DATA_IN_CODE on one thin Mach-O or one selected slice of a universal file. Starts are absolute VM addresses from ULEB128 deltas off the __TEXT vmaddr, and each data-in-code entry reports offset, length, kind, and the function start that owns its address, with one offset and limit applied to each selected table and counts covering every entry. inspect_macho_function_starts and inspect-macho-function-starts expose that report, require architecture only for a universal file, and refuse a combined table above max_entries with no partial result. The shared load-command walk now yields each command once and stores the Mach-O segment name and vmaddr on load regions, and the guides and limit docs record coverage, paging, and the read and entry budgets.
Codex Review Summary: ✅ CompletedThis 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 reviewed changes appear safe to merge, with no new actionable issue found. SummaryAdds offline Mach-O function-start and data-in-code inspection through CLI and MCP.
Review used source checks; no tests were run. The supersession check could not complete because GitHub access was unavailable. Diagram%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[CLI or MCP request] --> B[Shared service]
B --> C[Provider opens identity-checked file]
C --> D[Select thin image or universal slice]
D --> E[Shared load-command reader]
E --> F[Load both tables]
F --> G[Check entry budget and decode]
G --> H[Count issues and build requested pages]
H --> I[Return observed Evidence]
Reviews (10) · Last reviewed commit: "fix(native): report a non-Mach-O input a..." · Reviewed by Greptile |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 2fae00a190
ℹ️ 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".
- Check each start against an interval index of executable segments, built once, instead of scanning every segment per start. - Walk LC_FUNCTION_STARTS once. Count and check every start and data-in-code entry, but build result objects only for the requested page, and resolve owners for that page during the same walk. - Keep every declared Mach-O segment in the shared reader (macho_segments), so a __TEXT with no file bytes still anchors the deltas, as llvm-objdump does. LoadRegion no longer carries macho_segment. - On 32-bit ARM, clear the Thumb tag (bit 0) from published starts and report it as `thumb`, keeping the tagged value for the next delta, as ld64 encodes and LLDB decodes. Wrap 32-bit images at 2^32. - Intersect Evidence file ranges with the selected universal slice.
|
Thanks for this reader. I pushed one commit on top of your branch, 2801fc2, to address the six review threads. Your history is unchanged.
The semantics were checked against these references:
Two places where this reader is stricter than llvm-objdump, which this PR already did:
|
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 2801fc209f
ℹ️ 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".
- One table, MACHO_DICE_KIND_NAMES, names data-in-code kinds for the reader, the schema and the report check, which each had their own copy. - Each linkedit command's dataoff/datasize is normalized once against the slice (end, 32-bit fit, in-slice bytes) instead of the overflow check and the in-slice length being recomputed in three helpers. - Name CPU_TYPE_ARM, VM_PROT_EXECUTE and UINT32_MAX after their headers. - Report coverage.text_not_at_header when __TEXT does not map file offset 0: llvm-objdump anchors the deltas on the __TEXT vmaddr and dyld on the mach_header address, and they disagree there. - Pin the fixture's expected starts to literal addresses.
# Conflicts: # docs/reference/limits-and-recovery.md
|
Follow-up: a brittleness pass, in aa09348. I also merged Found and fixed:
Left as is, on purpose:
Sources:
|
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 628153ddd8
ℹ️ 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".
The architecture help for four commands said "as lipo -thin does", which the CLI option-alias check reads as a registered short flag `-thin`. Describe the lipo behavior without spelling its flag.
This reverts commit 68f25ad.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: b89c576b63
ℹ️ 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".
…the header - Refuse a data table above max_entries from its byte count, and stop the start walk at the remaining allowance, so the allowance bounds decoding work. The refusal no longer claims a required_value it did not measure. - Map each data-in-code offset as the mach_header's VM address plus the offset, the way ld64 encodes it and dyld documents it. MH_OBJECT does not map its header, so its base is 0. The shared container now carries mach_header.filetype. - Use the same header address for text_not_at_header, which also catches a header shifted by SG_HIGHVM. - Report a list that ends without its zero terminator as unterminated_starts. - Accept an observed empty section name in both result schemas.
|
Second follow-up, in b6a68c3, covering the six new review threads.
Two sources were added for the data-in-code semantics: ld64 |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: b6a68c3604
ℹ️ 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".
…ry total - Count LC_FUNCTION_STARTS and LC_DATA_IN_CODE commands during the shared load-command walk into a compact tally (two 32-bit words per command) instead of retaining a view and an object for each one. The shared visitor now hands over the image's byte order. - When the image has no starts to decode, a data table above max_entries is refused with its exact count as required_value.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: d57b7eefee
ℹ️ 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".
Ask the shared Mach-O header recognizer before the thin reader. An ELF or PE file passed without `architecture` has no Mach-O magic, so it is an unrecognized target for this operation. A recognized Mach-O with a broken or cut-off header still fails as malformed or truncated.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: a61679120e
ℹ️ 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".
Summary
Most Mach-O images, stripped ones included, carry
LC_FUNCTION_STARTS: the linker's ULEB128 delta list of every function start in__TEXT. Many also carryLC_DATA_IN_CODE, which marks jump tables and literal data inside the text section. REA reads neither today, so there is no offline way to list a stripped binary's function boundaries or to tell which bytes inside a function are data and not instructions.This adds an offline
inspect_macho_function_startstool and the CLI commandrea inspect-macho-function-starts. It decodes both tables from one thin Mach-O or one slice of a universal file, reports each start's VM address and section and each data-in-code entry's offset, address, length, kind, section and owning start, and counts every structural problem it meets. The image is not disassembled or executed. No issue exists for this; it is a read-only format-inspection addition within the supported contribution scope.Problem and expected behavior
Use case: given a stripped
program, list the function starts the linker recorded, compare them with a provider's procedure list, and find the jump tables and literal pools so they are not decoded as instructions.Observable outcome:
function_startsanddata_in_codereport each command'sstatus(presentorabsent),dataoff,datasize, containerfile_offsetand whether the range sits in__LINKEDIT.starts[]holds absolute VM addresses (the first delta is added to the__TEXTvmaddr; a zero delta ends the list) and the containing section.data_in_code_entries[]holds the fileoffset, VM address,length,kind(DICE_KIND_DATA,JUMP_TABLE8,JUMP_TABLE16,JUMP_TABLE32,ABS_JUMP_TABLE32, or the raw number), containing section, andowner, the greatest decoded start at or before that address.coveragecounts starts and entries plus duplicate and mis-sized commands, tables outside__LINKEDIT, commands past the file, truncated or overflowing ULEB128, wrapped and out-of-text starts, a missing__TEXTanchor, trailing bytes, entries that cross a section, unsorted, unmapped and zero-length entries. Any nonzero counter makescoverage.statuspartialwith a limitation naming it.pageselectskind(all,starts,data),offsetandlimit(limit: 0returns counts only) and reports the next offset for each table.max_entries(default 2,097,152, at most 16,777,216) bounds starts plus data-in-code entries combined. A larger pair is refused whole withdetails.limitand the combined count asrequired_value.Change and scope
src/domain/native/machoFunctionStarts.ts: input and result schemas, limitation text, and the issue predicate.src/native/apple/MachoFunctionStarts.ts: slice selection, command loading, ULEB128 walk, data-in-code decoding and paging. It reuses the shared Mach-O load-command walk and file mappings;readMachoImageFromReadergains an optionalvisitcallback so this reader seesLC_FUNCTION_STARTSandLC_DATA_IN_CODEduring the one shared walk, andLoadRegiongains an optionalmacho_segment(name and declaredvmaddr) used to find the__TEXTanchor. Other callers are unchanged.src/native/apple/MachoAddressIndex.ts: interval indexes for the smallest containing section and the file-offset-to-VM translation, built once per call, so each start and entry is one binary search. Overlapping sections resolve as a scan would (smallest wins, first declared breaks a tie), and an offset covered by two different translations is reported unmapped. Section names for starts are computed only for the returned page, and decoding yields and checks cancellation every 4,096 entries.docs/guides/native/macho-function-starts.md, CLI examples indocs/getting-started/cli.md,docs/reference/limits-and-recovery.md, the docs index and sidebar, and the skill references under.agents/skills/reverse-engineer-anything/and.cursor-plugin/. The existing fixed-ceiling registry entry for the 4 GiB identity ceiling now names this reader, sincedataoffand FAT32 slice offsets are 32-bit.Not included: disassembly, symbolization of starts,
LC_FUNCTION_VARIANTS, and dyld shared cache images.Contract and boundary impact
MachoFunctionStarts.tsdecodes both tables from the shared Mach-O walk; the provider and service consume its result without reparsing. The shared walk inmachoImage.tsremains the single owner of load-command framing and segment mappings.inspect_macho_function_startsand one new CLI commandinspect-macho-function-startswith--architecture,--kind,--offset,--limit,--max-entries,--max-file-bytesand--max-structure-bytes. Existing tools are unchanged.rea-macho-function-starts; no existing provider changes behavior.confidence: observed; evidence locations name the two table ranges.architecture;architectureon a thin image is refused.docs/public/product-catalog.json), package, or installation impact (CI generates and validates ignored catalogs andskills/; edit authored instructions in.agents/skills/reverse-engineer-anything/and never commit build outputs): none committed; authored skill references updated under.agents/skills/reverse-engineer-anything/and.cursor-plugin/.Expected effects (check known effects; use documentation-only on its own).
Unchecked items do not establish absence; leave uncertainties visible.
Evidence and regression coverage
src/native/apple/MachoFunctionStarts.test.ts(11 tests: starts and entries from fixture images, universal slice selection, absent commands, duplicate and mis-sized commands, tables outside__LINKEDIT, truncated and overflowing ULEB128, wrapped and out-of-text starts, section crossings, unsorted, unmapped and zero-length entries, ambiguous file offsets, paging and the entry budget),src/native/apple/MachoAddressIndex.test.ts(4 tests: the section index matches a linear scan over nested, overlapping, tied and empty sections; conflicting and identical file translations),src/native/apple/MachoFunctionStartsProvider.test.ts(4),src/application/binaryDiagnostics/MachoFunctionStartsService.test.ts(4),src/domain/native/machoFunctionStarts.test.ts(2),tests/acceptance/machoFunctionStartsCli.test.ts,tests/boundary/mcp/machoFunctionStartsMcp.test.ts(3),tests/boundary/filesystem/machoFunctionStartsFinalization.test.ts, plus entries intests/boundary/mcp/absoluteLocalPathTools.test.ts,tests/acceptance/tools/localToolContracts.test.tsandtests/conformance/providerCapabilityCases.ts. Fixtures are built insrc/native/apple/MachoFunctionStarts.fixture.ts.rea inspect-macho-function-starts demo --jsonon an arm64 executable built withclang -O1from a small C file with four functions. The function-starts payload is the 8 bytesb0 09 20 98 01 2c 00 00. Excerpt ofnormalized_result:{ "function_starts": { "status": "present", "dataoff": 32968, "datasize": 8, "file_offset": 32968, "in_linkedit": true }, "data_in_code": { "status": "present", "dataoff": 32976, "datasize": 0, "file_offset": 32976, "in_linkedit": true }, "coverage": { "status": "complete", "total_starts": 4, "total_data_in_code": 0 }, "starts": [ { "index": 0, "vmaddr": "0x1000004b0", "section": "__text", "issues": [] }, { "index": 1, "vmaddr": "0x1000004d0", "section": "__text", "issues": [] }, { "index": 2, "vmaddr": "0x100000568", "section": "__text", "issues": [] }, { "index": 3, "vmaddr": "0x100000594", "section": "__text", "issues": [] } ] }Those four addresses are exactly the
nm -n demoaddresses of_classify,_dispatch,_checksumand_main(all other coverage counters were 0).Clang emitted an empty
LC_DATA_IN_CODEfor that arm64 build, so data-in-code was exercised on a synthetic arm64 image written with the test fixture builder (sample.macho, four starts and four entries, the last placed at the end of__texton purpose):{"index":0,"offset":4160,"vmaddr":"0x100001040","length":16,"kind":"JUMP_TABLE8","section":"__text","owner":{"index":0,"vmaddr":"0x100001000"},"issues":[]} {"index":1,"offset":4480,"vmaddr":"0x100001180","length":8,"kind":"DICE_KIND_DATA","section":"__text","owner":{"index":1,"vmaddr":"0x100001100"},"issues":[]} {"index":2,"offset":4864,"vmaddr":"0x100001300","length":32,"kind":"ABS_JUMP_TABLE32","section":"__text","owner":{"index":2,"vmaddr":"0x100001280"},"issues":[]} {"index":3,"offset":6136,"vmaddr":"0x1000017f8","length":16,"kind":"JUMP_TABLE32","section":"__text","owner":{"index":3,"vmaddr":"0x100001480"},"issues":["crosses_section"]}Coverage was
partialwithsection_crossings: 1(every other counter 0) and the limitation "A data-in-code entry is not contained in one section."Paging:
--kind data --limit 2returnedreturned_data: 2,total_data: 4,next_data_offset: 2and no starts.Budget refusal:
--max-entries 4exited 1 with"code":"resource_constraint", "Mach-O function starts and data-in-code entries total 8, above max_entries (4)", anddetails.limit{"parameter":"max_entries","effective_value":4,"required_value":8,"cli_flag":"--max-entries"}.LC_DATA_IN_CODEwas available on this arm64 host, so data-in-code decoding is shown on fixture images only. Universal files were exercised by the fixture tests, not by a shipped universal binary.Not a boundary fix. The new tool's boundary tests cover MCP absolute-path handling, the finalization of the admitted file, and the provider lifecycle registration.
For evidence-bearing changes:
Validation performed
npx vitest runon the ten test files above: 10 files, 42 tests passed.npm run lint: exit 0, including the numericREA_limits and fixed-ceilings verification.node scripts/rea.mjs inspect-macho-function-startson the clang-built and fixture-built images: output as shown above.Compatibility, safety, and release
visitcallback andmacho_segmentfield are optional additions to shared types.max_entriesbefore any per-entry work, section and mapping lookups are logarithmic, and decoding yields and honors cancellation.AI was used for assistance.
Author readiness
Human owner: @mvanhorn
Author checklist (completed by the PR author before requesting review)
type(scope): outcome.REA_runtime setting with a default, floor, and maximum, unless a fixed ceiling has a stated reason. See Adding a runtime setting.Demo