Files
rea/src/cli/rustCommands.ts
T
Kaoru0822 b928963675 feat(native): inspect a universal Mach-O slice in Go and Rust metadata readers (#6219)
* feat(native): inspect a universal Mach-O slice in Go and Rust metadata readers

inspect_go_binary and inspect_rust_binary refused universal Mach-O files and
offered no slice selection, so hosts without lipo could not inspect them.

Both now accept architecture (x86, x86_64, arm, arm64). One selector reads
REA's own FAT table, validates the slice range, alignment and header CPU,
and returns the thin image with its macho-selected-image-v1 identity. The
universal file stays the artifact; results add selected_slice and keep
every source range a file offset. Missing, absent, duplicate or misplaced
selections fail on architecture and list the slices present. Subject
format mach-o-universal now has one owner shared by the services and the
retained Go/Rust views.

Fixes #4102

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(native): reject damaged universal files and self-contradicting slice reports

A FAT table whose slice could not be read was treated as a non-universal
file, so callers were told to omit architecture. Its reason now fails on
path. Go and Rust reports with a selected slice must be Mach-O and name the
slice's CPU family, through one shared agreement check. The CLI
architecture flag is described in help.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-21 21:18:35 -07:00

120 lines
4.4 KiB
TypeScript

import { z } from "incur";
import { createRustBinaryService } from "../composition/rust.js";
import { CLI_COMMANDS } from "../cliCommandNames.js";
import { logCliCommand } from "../cliLogging.js";
import {
analysisEvidenceDeliveryOptions,
deliverCliAnalysisEvidence,
} from "./analysisEvidenceDelivery.js";
import { inspectRustBinaryRequestSchema } from "../domain/rust/rustBinaryQuery.js";
import { analysisInputErrorFromIssues } from "../domain/inputIssueProjection.js";
import { AnalysisInputError } from "../domain/analysisErrorCore.js";
import { projectAnalysisError } from "../domain/analysisErrorProjection.js";
import { safeParseJson } from "../domain/safeJson.js";
import type { Logger } from "pino";
import type { CliInstance } from "./types.js";
import { withCommandCancellation } from "./commandCancellation.js";
import { absoluteCallerPath } from "../filesystem/callerPath.js";
import { machoSliceArchitectureSchema } from "../domain/apple/machoTargetSelection.js";
/** Resolve caller paths and inspect embedded Rust dependencies through the shared workflow. */
export const registerRustCommands = (
cli: CliInstance,
logger: Logger,
): void => {
cli.command(CLI_COMMANDS.inspectRustBinary, {
description:
"Inspect an embedded cargo-auditable Rust dependency list without target execution",
args: z.object({
path: z
.string()
.describe("Local ELF, PE, thin Mach-O or universal Mach-O image"),
}),
options: z.object({
...analysisEvidenceDeliveryOptions,
architecture: machoSliceArchitectureSchema
.optional()
.describe(
"CPU family of the slice to read from a universal Mach-O; required for universal files",
),
detail: z
.enum(["complete", "summary"])
.optional()
.describe(
"Default complete; a view selects compact delivery with --evidence_output",
),
view: z
.string()
.optional()
.describe(
"JSON package or edge selection; retains complete Evidence with --evidence_output",
),
}),
run: ({ args, options }) =>
withCommandCancellation((signal) =>
logCliCommand(logger, CLI_COMMANDS.inspectRustBinary, async () => {
const decoded =
options.view === undefined
? { ok: true as const, value: undefined }
: safeParseJson(options.view);
if (!decoded.ok) {
return projectAnalysisError(
new AnalysisInputError(
"inspect_rust_binary",
{ cause: decoded.cause },
[
{
path: ["view"],
reason: "invalid_format",
message: "view must be a JSON package or edge selection",
},
],
),
);
}
const view = decoded.value;
const input = inspectRustBinaryRequestSchema.safeParse({
path: absoluteCallerPath(args.path),
...(options.architecture === undefined
? {}
: { architecture: options.architecture }),
...(options.detail === undefined ? {} : { detail: options.detail }),
...(view === undefined ? {} : { view }),
});
if (!input.success)
return projectAnalysisError(
analysisInputErrorFromIssues(
"inspect_rust_binary",
input.error.issues,
{ view },
),
);
const service = createRustBinaryService();
return deliverCliAnalysisEvidence({
run: () =>
service.inspect(
{
path: input.data.path,
...(input.data.architecture === undefined
? {}
: { architecture: input.data.architecture }),
},
{ signal },
),
service,
providerId: "rust-auditable",
operation: "inspect_rust_binary",
signal,
detail:
input.data.detail ??
(input.data.view === undefined ? "complete" : "summary"),
evidenceOutput: options.evidence_output,
...(input.data.view === undefined
? {}
: { view: { kind: "rust" as const, ...input.data.view } }),
});
}),
),
});
};