Files
reverse-engineering-anything/src/process/LinuxResourceLimits.ts
T
morluto cb4e5cfbff feat(analysis): make resource budgets recoverable across CLI and MCP (#9706)
* refactor(analysis): centralize resource budgets and recovery facts

* feat(analysis): expose ASAR and Ghidra runtime admission controls

* feat(artifacts): expose reader admission and preserve limited recognition

* feat(javascript): expose recoverable semantic precision budgets

* feat(web): add recoverable source-map budgets and compact retained views

* feat(web): make historical capture decoding budgets recoverable

* fix(desktop): enforce selected view policies across capture and export

* feat(runtime): make Frida and LLDB retention budgets selectable

* feat(native): expose diagnostic budgets and preserve exhaustion causes

* feat(managed): expose recoverable snapshot and representation budgets

* feat(metadata): make Go and Rust inspection budgets recoverable

* feat(wasm): expose representation and producer admissions

* feat(evm): expose per-call worker resource budgets

* feat(android): make resource and string-pool omissions recoverable

* feat(producers): expose command and recorded-capture budgets

* docs(analysis): record recovery contracts and integrated validation

* fix(docs): link calibration oracle from published verification pages

* fix(docs): restore desktop task anchors and receipt links

* test(analysis): align fixtures with selectable budget contracts

* fix(analysis): remove source-location projector dependency cycle

* test(analysis): refresh budget recovery and verifier regressions

* test(analysis): preserve format and publication recovery expectations

* test(windows): align conformance with owned runtime admission

* fix(desktop): preserve selected alias kinds in exported state

* test(analysis): align provider fixtures with recoverable budgets

* test(desktop): validate emitted alias metadata structurally

* fix(ghidra): classify bounded Windows endpoint refusals

* test(cli): assert selectable artifact admission recovery

* fix(cli): preserve capture provenance in environment builds
2026-11-10 05:11:20 -05:00

87 lines
3.1 KiB
TypeScript

import { ProcessAdmissionError } from "./ProcessAdmissionError.js";
import { open } from "node:fs/promises";
import { readFileHandleRange } from "../filesystem/readFileHandleRange.js";
import { withArtifactFileHandle } from "../artifacts/ArtifactFileOperation.js";
import type { ArtifactResourceScope } from "../artifacts/ArtifactResourceScope.js";
/** Per-process ceilings inherited by an owned Linux producer. */
export interface LinuxResourceLimits {
readonly address_space_bytes: number;
readonly file_bytes: number;
readonly cpu_seconds: number;
}
const rows = {
address_space_bytes: "Max address space",
file_bytes: "Max file size",
cpu_seconds: "Max cpu time",
} as const;
/**
* Pin soft and hard ceilings together, ending at the prlimit option terminator.
* Callers that append the producer command themselves use this prefix.
*/
export const prlimitCeilingArguments = (
limits: LinuxResourceLimits,
): string[] => [
`--as=${limits.address_space_bytes}:${limits.address_space_bytes}`,
`--fsize=${limits.file_bytes}:${limits.file_bytes}`,
`--cpu=${limits.cpu_seconds}:${limits.cpu_seconds}`,
"--core=0:0",
"--",
];
/** Pin soft and hard ceilings together, then hand the producer argv to the limiter. */
export const prlimitArgumentVector = (
limits: LinuxResourceLimits,
command: string,
): string[] => [...prlimitCeilingArguments(limits), command];
/** Never raise either inherited soft policy or hard ceiling. */
export const clampLinuxResourceLimits = (
configured: LinuxResourceLimits,
report: string,
): LinuxResourceLimits => {
const effective = { ...configured };
for (const key of Object.keys(rows) as (keyof LinuxResourceLimits)[]) {
const matches = report
.split("\n")
.filter((line) => line.startsWith(`${rows[key]} `));
const values = matches[0]?.slice(rows[key].length).trim().split(/\s+/u);
if (matches.length !== 1 || values === undefined || values.length !== 3)
throw new ProcessAdmissionError("limit_unreadable", { limit: key });
let ceiling = BigInt(configured[key]);
for (const token of values.slice(0, 2)) {
if (token === "unlimited") continue;
if (!/^[0-9]+$/u.test(token))
throw new ProcessAdmissionError("limit_invalid", { limit: key });
const value = BigInt(token);
if (value < ceiling) ceiling = value;
}
effective[key] = Number(ceiling);
}
return effective;
};
/** Read the trusted kernel report with a fixed allocation before spawning. */
export const readLinuxResourceLimits = async (
configured: LinuxResourceLimits,
resourceScope?: ArtifactResourceScope,
): Promise<LinuxResourceLimits> => {
const file = await open("/proc/self/limits", "r");
return withArtifactFileHandle(
"/proc/self/limits",
file,
resourceScope,
undefined,
async (handle) => {
const reportBudget = 16 * 1024;
const buffer = await readFileHandleRange(handle, 0, reportBudget);
if (buffer.length === reportBudget)
throw new ProcessAdmissionError("report_exceeds_budget", {
budget_bytes: reportBudget,
});
return clampLinuxResourceLimits(configured, buffer.toString("utf8"));
},
);
};