Files
reverse-engineering-anything/src/rust/RustBinaryImage.ts
T
morluto 2430b081c9 refactor(providers): bind provider refusals' per-call controls instead of naming CLI flags (#11455)
* refactor(native): bind producer_limits and max_input_bytes as the per-call controls of pwntools and Apple dispatch refusals

producerLimitControl in src/domain/producerResourceLimits.ts names one producer budget as producer_limits.<field>; the shared provider outcome classifier now carries a resource signal's limit. Pwntools file-size, CPU and output refusals and the Apple dispatch input refusal bind their control, and their remediation no longer names CLI flags.

* fix(native): report the selected output allowance as the omitted file_bytes default

* fix(native): bind producer_limits.diagnostic_bytes as the pwntools command output-limit control

* refactor(rust): bind producer_limits as the per-call control of Rust input and report refusals (#11458)

* refactor(rust): bind producer_limits as the per-call control of Rust input and report refusals

* test(rust): cover the complete-report refusal's producer_limits control

* refactor(browser): bind caller-selectable browser deadlines as per-call controls instead of naming CLI flags (#11466)

* refactor(rust): bind producer_limits as the per-call control of Rust input and report refusals

* test(rust): cover the complete-report refusal's producer_limits control

* refactor(browser): bind caller-selectable browser deadlines as per-call controls instead of naming CLI flags

deadlineParameterLimit in src/domain/analysisResourceLimits.ts owns the millisecond deadline control that nativeDeadlineRecovery and the browser recoveries share. CDP command and discovery, web bundle per-script and total, network capture and source-map decode deadlines now carry it; browserDeadlineIncrease no longer takes CLI flag text.

* fix(browser): record a per-script deadline's parameter control in partial web bundle results

* refactor(providers): bind Wabt, SQLite, EVMole, Apktool and Go controls instead of naming CLI commands (#11472)

* refactor(providers): bind Wabt, SQLite, EVMole and Apktool deadline and byte controls instead of naming CLI commands

These remediation strings named rea commands and flags through template text the cli-option guard did not count. Wabt byte refusals and output limits bind max_input_bytes, max_tool_bytes or max_output_bytes while a larger value is selectable; SQLite worker_timeout_ms, EVMole timeout_ms and Apktool decode_timeout_ms bind through deadlineParameterLimit; the shared provider outcome classifier now carries an output limit's control.

* refactor(go): bind Go analysis budgets as per-call controls instead of naming CLI flags

* fix(providers): keep the typed primary when Wabt, EVMole or pwntools command cleanup fails

* ci(guards): count template-built CLI option and command text outside the CLI layer (#11478)

* refactor(providers): bind Wabt, SQLite, EVMole and Apktool deadline and byte controls instead of naming CLI commands

These remediation strings named rea commands and flags through template text the cli-option guard did not count. Wabt byte refusals and output limits bind max_input_bytes, max_tool_bytes or max_output_bytes while a larger value is selectable; SQLite worker_timeout_ms, EVMole timeout_ms and Apktool decode_timeout_ms bind through deadlineParameterLimit; the shared provider outcome classifier now carries an output limit's control.

* refactor(go): bind Go analysis budgets as per-call controls instead of naming CLI flags

* fix(providers): keep the typed primary when Wabt, EVMole or pwntools command cleanup fails

* ci(guards): count template-built CLI option and command text outside the CLI layer

The cli-option-text-outside-cli pattern now also matches "(CLI ...", "on the CLI", "or use rea ..." and backticked `rea <command>` references, so remediation built from a template such as --${name} or rea inspect-... <path> is counted. Doctor.ts is exempt: setup and doctor run only through the CLI. The baseline records the newly counted pre-existing sites (22 -> 45 across 21 files).

* ci(guards): extract the CLI text matcher, exempt host-only commands with written reasons, and test each spelling

* ci(guards): exempt a host-only command and its own arguments in any matched CLI phrase

* fix(native): bind producer_limits.output_bytes for oversized pwndbg debugger replies through one owner

* fix(cli): name an Evidence target source failure's control by the source option that sets it

* feat(cli): render the flags of result-recorded controls in the full-output envelope (#11507)

* feat(cli): render the flags of result-recorded controls in the full-output envelope

* fix(cli): collect result controls only for a full-output envelope, in its requested format, past undefined values

* ci(guards): end a host-only command's arguments before the next CLI command

* refactor(reverse): record the output budget that stopped a one-shot command as a parameter control (#11528)

* fix(cli): read result controls only at positions the output schema declares as recorded controls

* refactor(providers): record the controls that bounded Rust compiler paths and kernel module text instead of naming CLI options (#11532)

* refactor(rust): record the control that bounded compiler-path coverage instead of naming CLI options

* refactor(native): record the controls that omitted kernel module .modinfo text instead of naming CLI options (#11534)

* fix(rust): describe compiler coverage from the retained result after report trimming

* refactor(providers): record managed representation and JavaScript semantic ceiling controls instead of naming CLI options (#11537)

* refactor(managed): record the representation budget on a resource-limit issue instead of naming its CLI option

* refactor(javascript): record the semantic retention ceilings that stopped a graph instead of naming CLI options (#11540)

* fix(cli): name a composed command's source options for the controls its result records

* fix(cli): read result controls from the delivered Evidence when a summary wraps it

* fix(rust): keep the report-byte control recorded when trimming omits every compiler path

When the complete-report allowance left room for no compiler path, rustc_commit became null and its coverage.limit was lost, so --full-output could not name the option that raises the allowance. The report now records rustc_commit_omission { reason, limit } outside the dropped record, and the summary view carries it.

* refactor(flutter): record the string-pool budgets that left a projection partial instead of naming the CLI option (#11541)

* refactor(flutter): record the string-pool budgets that left a projection partial instead of naming the CLI option

* test(flutter): assert the complete set of recorded string-pool budgets

* feat(cli): name the options of controls a failure's partial Evidence records (#11557)

meta.cli_controls was collected only from a successful result. A failure whose partial observation is Evidence of the command's own operation is now read the same way, at the positions the output schema declares as recorded controls; Evidence of another operation or a retained reference is not read. When --evidence_output publishes the partial, the delivery records it before replacing it.

* refactor(javascript): record the evaluator budgets that limited sources instead of naming the CLI option (#11564)

Per-value unknown reasons and the Electron wrapper limitation named --semantic-precision. The static examination now records the semantic_precision controls that limited a source (examination.controls), and analyze_javascript_application's semantic_graph.coverage.controls records every evaluator budget that limited any source (examination and semantic IR coverage), beside the retention ceilings. The CLI renders --semantic-precision from those records in meta.cli_controls; the reason texts name only the contract field.
2026-11-16 07:12:16 +08:00

253 lines
8.0 KiB
TypeScript

import {
BinaryResourceFailure,
type BinaryContainer,
type ImageSection,
} from "../domain/binaryContainer.js";
import { malformedInput, unsupportedInput } from "../domain/parseFailure.js";
import { keyedBy, uniqueKeyed } from "../domain/keyedCollection.js";
import { readBinaryContainer } from "../domain/native/readBinaryContainer.js";
import type {
RustAuditableMetadata,
RustBinary,
} from "../domain/rust/rustBinary.js";
import {
DEFAULT_RUSTC_HASH_LIMIT,
rustcPartialCoverage,
} from "../domain/rust/rustBinary.js";
import { RUST_PRODUCER_DEFAULTS } from "../domain/rust/rustResourceLimits.js";
import {
readAuditableSection,
RUST_AUDITABLE_FORMAT,
RustAuditableResourceFailure,
} from "./RustAuditableReader.js";
const RUSTC_PREFIX = Buffer.from("/rustc/");
const HASH_BYTES = 40;
/** Container identity plus optional dependency metadata and inferred rustc commits. */
export interface RustBinaryImage {
readonly format: RustBinary["format"];
readonly architecture: string;
readonly bits: 32 | 64;
readonly byte_order: RustBinary["byte_order"];
readonly auditable_metadata: RustAuditableMetadata | null;
readonly rustc_commit: RustBinary["rustc_commit"];
readonly section_names_limitation?: string;
}
/** Read dependency metadata from container sections without executing the image. */
export const readRustBinaryImage = (
bytes: Buffer,
maxRustcHashes = DEFAULT_RUSTC_HASH_LIMIT,
maxOutputBytes: number = RUST_PRODUCER_DEFAULTS.output_bytes,
): RustBinaryImage => {
const container = readContainer(bytes);
const dependency = dependencySection(container.format, container.sections);
return {
format: container.format,
architecture: container.architecture,
bits: container.bits,
byte_order: container.byte_order,
auditable_metadata:
dependency === null
? null
: readDependency(bytes, container.format, dependency, maxOutputBytes),
rustc_commit: rustcCommit(bytes, container, maxRustcHashes, maxOutputBytes),
...(container.section_names_limitation === undefined
? {}
: { section_names_limitation: container.section_names_limitation }),
};
};
const readContainer = (bytes: Buffer) => {
try {
return readBinaryContainer(bytes);
} catch (cause) {
// A container format failure is already a ParseFailureError; it keeps
// its own kind and facts instead of being restated as a Rust failure.
if (cause instanceof BinaryResourceFailure)
throw new RustAuditableResourceFailure(
{
resource: "memory",
reported_limits: { maximum_bytes: cause.maximum_bytes },
},
cause.message,
{
cause,
},
);
throw cause;
}
};
const dependencySection = (
format: RustBinaryImage["format"],
sections: readonly ImageSection[],
): ImageSection | null => {
const expected = format === "macho" ? "__DATA,.dep-v0" : ".dep-v0";
const selected = uniqueKeyed(
keyedBy(sections, ({ name }) => name),
expected,
);
if (!selected.ok)
throw malformedInput(
RUST_AUDITABLE_FORMAT,
"Image contains more than one dependency section",
);
const section = selected.value;
if (section === undefined) return null;
if (section.compressed)
throw unsupportedInput(
RUST_AUDITABLE_FORMAT,
"Compressed dependency sections are unsupported",
{ undecodedContent: true },
);
if (!section.file_backed)
throw malformedInput(
RUST_AUDITABLE_FORMAT,
"Dependency section is not file-backed",
);
return section;
};
const readDependency = (
bytes: Buffer,
format: RustBinaryImage["format"],
section: ImageSection,
maxOutputBytes: number,
): RustAuditableMetadata =>
readAuditableSection(
format === "macho" ? "__DATA,.dep-v0" : ".dep-v0",
section.offset,
bytes.subarray(section.offset, section.offset + section.size),
{
maxOutputBytes,
peFileAlignment:
section.format === "pe" &&
section.virtual_size === 0 &&
section.file_alignment > 0 &&
section.raw_size % section.file_alignment === 0
? section.file_alignment
: undefined,
},
);
const rustcCommit = (
bytes: Buffer,
container: BinaryContainer,
maxHashes: number,
maxOutputBytes: number,
): RustBinaryImage["rustc_commit"] => {
const retention: RustcPathRetention = {
hashes: [],
seen: new Set(),
maxHashes,
maxBytes: maxOutputBytes,
bytes: 2,
reason: undefined,
};
const sections = container.sections
.filter((section) => section.read_only)
.map(({ offset, size }) => ({ offset, size }));
const loadRegions =
container.format === "elf"
? (container.load_regions ?? [])
.filter((region) => region.read && !region.write && !region.execute)
.map(({ mapping }) => mapping)
: [];
// Overlapping section and ELF load ranges are scanned once, so total work
// stays bounded by the file size however many mappings reference a range.
for (const range of mergedReadOnlyFileRanges([...sections, ...loadRegions])) {
collectHashes(bytes, range, retention);
if (retention.reason !== undefined) break;
}
return retention.hashes.length === 0
? null
: {
evidence_kind: "inferred",
hashes: retention.hashes,
...(retention.reason !== undefined
? {
coverage: rustcPartialCoverage(
retention.reason,
retention.reason === "report_bytes"
? maxOutputBytes
: maxHashes,
),
}
: {}),
};
};
/** One owner for compiler-path uniqueness, retained output and exhaustion. */
interface RustcPathRetention {
readonly hashes: { hash: string; file_offset: number }[];
readonly seen: Set<string>;
readonly maxHashes: number;
readonly maxBytes: number;
bytes: number;
reason: "max_rustc_hashes" | "report_bytes" | undefined;
}
const mergedReadOnlyFileRanges = (
ranges: readonly { readonly offset: number; readonly size: number }[],
): { readonly offset: number; readonly end: number }[] => {
const sorted = ranges
.filter(({ size }) => size > 0)
.map(({ offset, size }) => ({ offset, end: offset + size }))
.sort((left, right) => left.offset - right.offset);
const merged: { offset: number; end: number }[] = [];
for (const range of sorted) {
const last = merged.at(-1);
if (last !== undefined && range.offset <= last.end)
last.end = Math.max(last.end, range.end);
else merged.push({ ...range });
}
return merged;
};
const collectHashes = (
bytes: Buffer,
range: { readonly offset: number; readonly end: number },
retention: RustcPathRetention,
): void => {
const region = bytes.subarray(range.offset, range.end);
let cursor = 0;
while (cursor < region.length) {
const at = region.indexOf(RUSTC_PREFIX, cursor);
if (at < 0) return;
const start = at + RUSTC_PREFIX.length;
const hash = hashAt(region, start);
if (hash !== null && !retention.seen.has(hash)) {
if (retention.seen.size >= retention.maxHashes) {
retention.reason = "max_rustc_hashes";
return;
}
const entry = { hash, file_offset: range.offset + at };
const entryBytes = Buffer.byteLength(JSON.stringify(entry)) + 1;
if (entryBytes > retention.maxBytes - retention.bytes) {
retention.reason = "report_bytes";
return;
}
retention.bytes += entryBytes;
retention.seen.add(hash);
retention.hashes.push(entry);
}
cursor = at + 1;
}
};
const hashAt = (region: Buffer, start: number): string | null => {
const end = start + HASH_BYTES;
// Windows builds join the remapped `/rustc/<hash>` prefix with `\`.
if (end >= region.length || (region[end] !== 0x2f && region[end] !== 0x5c))
return null;
for (let index = start; index < end; index += 1) {
const byte = region[index] ?? 0;
const hex =
(byte >= 0x30 && byte <= 0x39) || (byte >= 0x61 && byte <= 0x66);
if (!hex) return null;
}
return region.toString("latin1", start, end);
};