mirror of
https://github.com/morluto/rea.git
synced 2026-12-05 06:21:12 +00:00
* feat(native): list Mach-O function starts and data-in-code entries offline
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.
* build: add advertised schema summary for inspect_macho_function_starts
* Address review feedback
* fix(native): bound function-start decoding and page before building rows
- 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.
* refactor(native): normalize function-start format facts once
- 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.
* build: refresh the inspect_macho_function_starts schema summary after merging main
* fix(cli): keep lipo's flag name out of --architecture help text
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.
* Revert "fix(cli): keep lipo's flag name out of --architecture help text"
This reverts commit 68f25adf74.
* refactor(cli): use the shared universal-slice help for inspect-macho-function-starts
* fix(native): bound decoding by max_entries and map data-in-code from 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.
* fix(native): tally LINKEDIT commands compactly and report a known entry 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.
* fix(native): report a non-Mach-O input as unrecognized, not malformed
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.
---------
Co-authored-by: Matt Van Horn <455140+mvanhorn@users.noreply.github.com>
Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
184 lines
7.0 KiB
TypeScript
184 lines
7.0 KiB
TypeScript
import { Client, InMemoryTransport } from "@modelcontextprotocol/client";
|
|
import { Ajv2020 } from "ajv/dist/2020.js";
|
|
import type { AnySchema } from "ajv";
|
|
import { expect, it } from "vitest";
|
|
|
|
import { JAVASCRIPT_RECOVERY_TOOL_CONTRACTS } from "../../../src/contracts/javascript/javascriptRecoveryToolContracts.js";
|
|
import { BINARY_DIAGNOSTICS_TOOL_CONTRACTS } from "../../../src/contracts/native/binaryDiagnosticsToolContracts.js";
|
|
import { V8_CODE_CACHE_TOOL_CONTRACTS } from "../../../src/contracts/v8/v8CodeCacheToolContracts.js";
|
|
import { MAVEN_GRADLE_METADATA_TOOL_CONTRACTS } from "../../../src/contracts/java/mavenGradleMetadataToolContracts.js";
|
|
import { WEB_SCRIPT_TOOL_CONTRACTS } from "../../../src/contracts/webScriptToolContracts.js";
|
|
import { API_CONTRACT_TOOL_CONTRACTS } from "../../../src/contracts/apiContractToolContracts.js";
|
|
import { PLAYWRIGHT_TRACE_TOOL_CONTRACTS } from "../../../src/contracts/playwrightTraceToolContracts.js";
|
|
import { PYTHON_PYC_TOOL_CONTRACTS } from "../../../src/contracts/python/pythonPycToolContracts.js";
|
|
import { DEB_PACKAGE_TOOL_CONTRACTS } from "../../../src/contracts/deb/debPackageToolContracts.js";
|
|
import { NUGET_NUPKG_TOOL_CONTRACTS } from "../../../src/contracts/nuget/nupkgToolContracts.js";
|
|
import { VSIX_PACKAGE_TOOL_CONTRACTS } from "../../../src/contracts/vsix/vsixPackageToolContracts.js";
|
|
import { OCI_IMAGE_LAYOUT_TOOL_CONTRACTS } from "../../../src/contracts/oci/ociImageLayoutToolContracts.js";
|
|
import { YARA_TOOL_CONTRACTS } from "../../../src/contracts/yara/yaraToolContracts.js";
|
|
import { RequestTrackingMcpServer } from "../../../src/server/RequestTrackingMcpServer.js";
|
|
import { toolRegistrationOptions } from "../../../src/server/toolRegistrationOptions.js";
|
|
import { createMcpSchemaValidation } from "../../fixtures/mcpSchemaValidation.js";
|
|
|
|
const CONTRACTS = [
|
|
...BINARY_DIAGNOSTICS_TOOL_CONTRACTS,
|
|
...WEB_SCRIPT_TOOL_CONTRACTS,
|
|
...JAVASCRIPT_RECOVERY_TOOL_CONTRACTS,
|
|
...API_CONTRACT_TOOL_CONTRACTS,
|
|
...PLAYWRIGHT_TRACE_TOOL_CONTRACTS,
|
|
...PYTHON_PYC_TOOL_CONTRACTS,
|
|
...DEB_PACKAGE_TOOL_CONTRACTS,
|
|
...NUGET_NUPKG_TOOL_CONTRACTS,
|
|
...VSIX_PACKAGE_TOOL_CONTRACTS,
|
|
...OCI_IMAGE_LAYOUT_TOOL_CONTRACTS,
|
|
...YARA_TOOL_CONTRACTS,
|
|
...V8_CODE_CACHE_TOOL_CONTRACTS,
|
|
...MAVEN_GRADLE_METADATA_TOOL_CONTRACTS,
|
|
];
|
|
|
|
const PATH_FIELDS = {
|
|
inspect_apple_ips: ["path"],
|
|
inspect_elf_notes: ["path"],
|
|
inspect_elf_debug_link: ["path", "candidate_path"],
|
|
inspect_elf_unwind_ranges: ["path"],
|
|
inspect_macho_function_starts: ["path"],
|
|
inspect_kernel_module: ["path"],
|
|
inspect_byte_entropy: ["path"],
|
|
inspect_api_contract: ["path", "operations_path"],
|
|
inspect_devicetree: ["path"],
|
|
inspect_playwright_trace: ["path"],
|
|
inspect_python_pyc: ["path"],
|
|
inspect_deb_package: ["path"],
|
|
inspect_nuget_nupkg: ["path"],
|
|
inspect_vsix_package: ["path"],
|
|
inspect_oci_image_layout: ["path"],
|
|
inspect_yara_matches: ["path", "rule_paths"],
|
|
inspect_v8_code_cache: ["path"],
|
|
inspect_maven_gradle_metadata: ["path"],
|
|
export_web_scripts: ["capture_path", "output_directory"],
|
|
recover_javascript_sources: ["path", "output_directory", "additional_paths"],
|
|
} as const;
|
|
|
|
const absoluteFile =
|
|
process.platform === "win32" ? "C:\\sample.bin" : "/tmp/sample.bin";
|
|
const absoluteDirectory = process.platform === "win32" ? "C:\\out" : "/tmp/out";
|
|
|
|
const contractFor = (name: keyof typeof PATH_FIELDS) => {
|
|
const contract = CONTRACTS.find((candidate) => candidate.name === name);
|
|
if (contract === undefined) throw new Error(`Missing contract ${name}`);
|
|
return contract;
|
|
};
|
|
|
|
const validInput = (
|
|
name: keyof typeof PATH_FIELDS,
|
|
): Record<string, unknown> => {
|
|
if (name === "inspect_api_contract")
|
|
return {
|
|
format: "graphql-sdl",
|
|
path: absoluteFile,
|
|
operations_path: absoluteFile,
|
|
};
|
|
if (name === "export_web_scripts")
|
|
return {
|
|
capture_path: absoluteFile,
|
|
output_directory: absoluteDirectory,
|
|
};
|
|
if (name === "recover_javascript_sources")
|
|
return {
|
|
path: absoluteFile,
|
|
output_directory: absoluteDirectory,
|
|
additional_paths: [absoluteFile],
|
|
};
|
|
if (name === "inspect_yara_matches")
|
|
return { path: absoluteFile, rule_paths: [absoluteFile] };
|
|
return { path: absoluteFile };
|
|
};
|
|
|
|
const withRelativeField = (
|
|
name: keyof typeof PATH_FIELDS,
|
|
field: string,
|
|
): Record<string, unknown> => ({
|
|
...validInput(name),
|
|
[field]:
|
|
field === "additional_paths" || field === "rule_paths"
|
|
? ["sample.bin"]
|
|
: "sample.bin",
|
|
});
|
|
|
|
it("rejects one relative path field at a time on tools/list", async () => {
|
|
const server = new RequestTrackingMcpServer({
|
|
name: "absolute-path-schema",
|
|
version: "1",
|
|
});
|
|
for (const name of Object.keys(PATH_FIELDS) as (keyof typeof PATH_FIELDS)[]) {
|
|
const contract = contractFor(name);
|
|
server.registerTool(
|
|
contract.name,
|
|
toolRegistrationOptions(contract),
|
|
() => ({ content: [] }),
|
|
);
|
|
}
|
|
const client = new Client(
|
|
{ name: "absolute-path-client", version: "1" },
|
|
{ jsonSchemaValidator: createMcpSchemaValidation() },
|
|
);
|
|
const [clientTransport, serverTransport] =
|
|
InMemoryTransport.createLinkedPair();
|
|
const ajv = new Ajv2020({ strict: false, validateFormats: false });
|
|
const checked: string[] = [];
|
|
try {
|
|
await server.connect(serverTransport);
|
|
await client.connect(clientTransport);
|
|
const listed = await client.listTools();
|
|
for (const name of Object.keys(
|
|
PATH_FIELDS,
|
|
) as (keyof typeof PATH_FIELDS)[]) {
|
|
const schema = listed.tools.find(
|
|
(tool) => tool.name === name,
|
|
)?.inputSchema;
|
|
expect(schema, name).toBeDefined();
|
|
if (schema === undefined) throw new Error(`Missing schema ${name}`);
|
|
const validate = ajv.compile(schema as AnySchema);
|
|
expect(validate(validInput(name)), `${name} valid`).toBe(true);
|
|
for (const field of PATH_FIELDS[name]) {
|
|
expect(
|
|
validate(withRelativeField(name, field)),
|
|
`${name}.${field}`,
|
|
).toBe(false);
|
|
checked.push(`${name}.${field}`);
|
|
}
|
|
}
|
|
} finally {
|
|
await client.close();
|
|
await server.close();
|
|
}
|
|
expect(checked).toEqual([
|
|
"inspect_apple_ips.path",
|
|
"inspect_elf_notes.path",
|
|
"inspect_elf_debug_link.path",
|
|
"inspect_elf_debug_link.candidate_path",
|
|
"inspect_elf_unwind_ranges.path",
|
|
"inspect_macho_function_starts.path",
|
|
"inspect_kernel_module.path",
|
|
"inspect_byte_entropy.path",
|
|
"inspect_api_contract.path",
|
|
"inspect_api_contract.operations_path",
|
|
"inspect_devicetree.path",
|
|
"inspect_playwright_trace.path",
|
|
"inspect_python_pyc.path",
|
|
"inspect_deb_package.path",
|
|
"inspect_nuget_nupkg.path",
|
|
"inspect_vsix_package.path",
|
|
"inspect_oci_image_layout.path",
|
|
"inspect_yara_matches.path",
|
|
"inspect_yara_matches.rule_paths",
|
|
"inspect_v8_code_cache.path",
|
|
"inspect_maven_gradle_metadata.path",
|
|
"export_web_scripts.capture_path",
|
|
"export_web_scripts.output_directory",
|
|
"recover_javascript_sources.path",
|
|
"recover_javascript_sources.output_directory",
|
|
"recover_javascript_sources.additional_paths",
|
|
]);
|
|
});
|