Files
reverse-engineering-anything/src/domain/reverseEngineering.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

163 lines
6.1 KiB
TypeScript

import { timerDurationMsSchema } from "./analysisResourceLimits.js";
import type { AnalysisControlLimit } from "./analysisSettings.js";
import { z } from "zod";
import { constants as bufferConstants } from "node:buffer";
import { commandEvidenceDetailSchema } from "./commandObservation.js";
import { localPathStringSchema } from "./localPath.js";
import { RAW_BINARY_ENTRY_POINT } from "./rawBinary.js";
import { rejectUnpairedSurrogates } from "./stringPatterns.js";
/** Omitted one-shot objdump/rizin wall clock. Preflight shares it; the command gets it again. */
export const DEFAULT_REVERSE_COMMAND_TIMEOUT_MS = 120_000;
/** Combined stdout/stderr admission, before UTF-8 decoding and Evidence expansion. */
export const DEFAULT_REVERSE_COMMAND_OUTPUT_BYTES = 16 * 1024 * 1024;
/** Both raw and normalized output are JSON-escaped in one Evidence string. */
export const MAX_REVERSE_COMMAND_OUTPUT_BYTES = Math.floor(
(bufferConstants.MAX_STRING_LENGTH - 1024 * 1024) / (2 * 6),
);
/**
* The per-call control that bounded a one-shot command's captured output,
* named by its contract field; each adapter names the option that sets it.
*/
export const reverseCommandOutputLimit = (
bytes: number,
): AnalysisControlLimit => ({
control_kind: "parameter",
parameter: "max_output_bytes",
effective_value: bytes,
unit: "bytes",
minimum: 1,
maximum: MAX_REVERSE_COMMAND_OUTPUT_BYTES,
default_value: DEFAULT_REVERSE_COMMAND_OUTPUT_BYTES,
applies_at: "next_call",
});
const reverseCommandOutputBytesSchema = z
.number()
.int()
.min(1)
.max(MAX_REVERSE_COMMAND_OUTPUT_BYTES)
.exactOptional()
.describe(
`Combined stdout/stderr byte budget before decoding (default ${String(DEFAULT_REVERSE_COMMAND_OUTPUT_BYTES)}, range 1..${String(MAX_REVERSE_COMMAND_OUTPUT_BYTES)}). Applies on the next call without restarting REA. The maximum reserves JSON-escaping space for both Evidence mirrors under Node's single-string ceiling. Reaching it stops collection and requests provider termination; completion remains unknown. Confirm effects and cleanup before repeating a command. Does not raise the MCP client's receive buffer.`,
);
/** Per-call deadline for one-shot objdump and rizin commands. */
const reverseCommandTimeoutSchema = timerDurationMsSchema
.exactOptional()
.describe(
"Wall-clock budget in milliseconds. Target preflight shares one budget across stat and digest, and the one-shot command receives that budget again (default 120000 each). Omitted means 120000. Applies on the next call. Output admission is separately controlled by max_output_bytes; does not raise Ghidra or Hopper startup deadlines or debugger session deadlines.",
);
const commandExecutionFields = {
command_timeout_ms: reverseCommandTimeoutSchema,
max_output_bytes: reverseCommandOutputBytesSchema,
};
export const OBJDUMP_ARTIFACT_OPERATIONS = [
"file_headers",
"section_headers",
"symbols",
"relocations",
"dwarf",
"disassemble",
"disassemble_all",
] as const;
const objdumpArtifactInputSchema = z
.object({
path: localPathStringSchema,
operation: z.enum(OBJDUMP_ARTIFACT_OPERATIONS).exclude(["disassemble"]),
follow_debug_links: z.boolean().default(false),
...commandExecutionFields,
})
.strict();
const objdumpDisassemblyInputSchema = objdumpArtifactInputSchema.extend({
operation: z.literal("disassemble"),
symbol: rejectUnpairedSurrogates(
z
.string()
.min(1)
.regex(/^[^\x00]*$/u),
"Symbol must be well-formed Unicode.",
)
.describe(
"Native symbol to disassemble, expressed as well-formed Unicode. Matching and symbol boundaries follow the selected executable; omitted means all code sections.",
)
.optional(),
});
const objdumpArchitecturesInputSchema = z.strictObject({
operation: z.literal("architectures"),
...commandExecutionFields,
});
/** Caller-selected native selection and independent Evidence delivery. */
export const objdumpInputSchema = z.discriminatedUnion("operation", [
objdumpArtifactInputSchema.extend({ detail: commandEvidenceDetailSchema }),
objdumpDisassemblyInputSchema.extend({ detail: commandEvidenceDetailSchema }),
objdumpArchitecturesInputSchema.extend({
detail: commandEvidenceDetailSchema,
}),
]);
// Rizin owns its arch and CPU vocabulary, and the plugins differ between
// builds, so REA checks only the spelling. Rizin sets no length limit.
const rizinNameSchema = z
.string()
.regex(
/^[A-Za-z0-9][A-Za-z0-9_.+\-]*$/u,
"must be a Rizin name: letters, digits, '_', '.', '+' or '-', starting with a letter or digit",
);
/** Caller-selected Rizin command against one local artifact. */
export const rizinInputSchema = z
.object({
path: localPathStringSchema,
command: z
.string()
.min(1)
.refine(
(value) => !/[\r\n\u0000]/u.test(value),
"A single command line is required",
),
asm_arch: rizinNameSchema
.exactOptional()
.describe(
"Rizin arch plugin, passed as -a, such as arm. Plugins differ between builds; e asm.arch=? lists them.",
),
// Rizin's widest decoders are 64-bit.
asm_bits: z
.number()
.int()
.min(1)
.max(64)
.exactOptional()
.describe(
"Rizin asm.bits, passed as -b, such as 16 for Thumb. Omitted, Rizin's default applies.",
),
asm_cpu: rizinNameSchema
.exactOptional()
.describe(
"Rizin asm.cpu, passed as -e asm.cpu=, such as cortexm; e asm.cpu=? lists those of the selected arch.",
),
base_address: z
.string()
.regex(RAW_BINARY_ENTRY_POINT, {
message: "base_address must be a hexadecimal address of the form 0x…",
})
.exactOptional()
.describe(
"Address of file offset 0. REA passes -n and -m, so Rizin maps the file there as a raw image, reads no format headers, and keeps its default decoder unless asm_arch and asm_bits select one.",
),
...commandExecutionFields,
})
.strict();
export type ObjdumpSelection =
| z.input<typeof objdumpArtifactInputSchema>
| z.input<typeof objdumpDisassemblyInputSchema>
| z.input<typeof objdumpArchitecturesInputSchema>;
export type RizinInput = z.output<typeof rizinInputSchema>;