mirror of
https://github.com/morluto/rea.git
synced 2026-11-19 13:12:26 +00:00
* refactor(kernel): give error-to-message text one owner in a new src/kernel layer * fix(kernel): keep the regex worker's own error text inside its isolated source * refactor(kernel): exclude copies that run in another realm from the error-message guard by name The regex worker's source string and the page.evaluate fetch in PlaywrightScenarioRequest cannot import errorMessage. The guard now names them, so the counted baseline does not invite a later slice to rewrite them. * refactor(kernel): migrate the long tail of inline error-message copies to errorMessage (#11552) Every provider and adapter folder with six or fewer copies now calls errorMessage from src/kernel. Local helpers whose whole body was the copy (failureMessage, gdbErrorMessage, message, describe, reasonOf, recoveryFailureMessage and four local errorMessage definitions) are removed and their call sites call the owner. Refs #11548 * refactor(kernel): give byte-to-hex formatting one owner in src/kernel (#11560) byteToHex returns the two lowercase hexadecimal digits of one byte. All eight inline copies (native, contracts, domain firmware and desktop, pcap) call it, keeping their own 0x, % or \x notation, and a counted guard with an empty baseline keeps new copies out. Refs #11548 * refactor(kernel): give one-shot SHA-256 hex digests one owner in src/kernel (#11562) sha256Hex digests one in-memory value. A counted guard records the one-shot createHash("sha256").update(x).digest("hex") chains (115 in 101 files) and the first slice migrates domain, contracts and process (35 copies), removing local wrappers (sha256, sha256Text, hash) and the exported sha256Text. Incremental and streamed hashing is not counted. Refs #11548 * refactor(kernel): migrate the application layer's inline error-message copies to errorMessage (#11563) All 69 copies in src/application call errorMessage from src/kernel. Local helpers whose whole body was the copy (errorDetail, causeReason and five local errorMessage definitions) are removed and their call sites call the owner. Refs #11548 * refactor(kernel): migrate the native provider's inline error-message copies to errorMessage (#11567) All 49 copies in src/native call errorMessage from src/kernel. The local errorReason helpers in LldbCallTracer and LldbRetainedObservations, whose whole body was the copy, are removed and their call sites call the owner. Refs #11548 * refactor(kernel): migrate the remaining inline error-message copies to errorMessage (#11569)
191 lines
6.8 KiB
TypeScript
191 lines
6.8 KiB
TypeScript
import { createHash, randomUUID } from "node:crypto";
|
|
import { createReadStream } from "node:fs";
|
|
|
|
import { HOST_PLATFORM } from "../platform/HostPlatform.js";
|
|
import { open, realpath, stat, type FileHandle } from "node:fs/promises";
|
|
import { tmpdir } from "node:os";
|
|
import { basename, isAbsolute, join, normalize } from "node:path";
|
|
import { absoluteCallerPath } from "../filesystem/callerPath.js";
|
|
import type { BinaryTarget } from "../domain/binaryTargetTypes.js";
|
|
import { AnalysisProtocolError } from "../domain/analysisErrorCore.js";
|
|
import { ConfigurationError } from "../domain/configurationErrors.js";
|
|
import { AnalysisError } from "../domain/analysisErrorBase.js";
|
|
import { PrivateRuntimeRoot } from "../process/PrivateRuntimeRoot.js";
|
|
import {
|
|
ArtifactResourceScope,
|
|
assertArtifactReadActive,
|
|
withArtifactFileHandle,
|
|
} from "../resources/index.js";
|
|
import { errorMessage } from "../kernel/errorMessage.js";
|
|
|
|
/** Read a file digest with bounded memory rather than retaining binary bytes. */
|
|
export const idaFileDigest = async (path: string): Promise<string> => {
|
|
const hash = createHash("sha256");
|
|
for await (const chunk of createReadStream(path)) hash.update(chunk);
|
|
return hash.digest("hex");
|
|
};
|
|
|
|
/** Compare host-local spellings without collapsing POSIX symlink/.. traversal. */
|
|
export const sameIdaHostPath = (left: string, right: string): boolean =>
|
|
HOST_PLATFORM.platform === "win32"
|
|
? normalize(left).toLowerCase() === normalize(right).toLowerCase()
|
|
: isAbsolute(left) === isAbsolute(right) &&
|
|
absoluteCallerPath(left) === absoluteCallerPath(right);
|
|
|
|
/** Admit the selected lookup before canonicalizing it for readiness and allocation.
|
|
* Native realpath can collapse file/.. without rejecting the non-directory.
|
|
*/
|
|
export const resolveIdaWorkspaceParent = async (
|
|
root = tmpdir(),
|
|
): Promise<string> => {
|
|
await stat(root);
|
|
return realpath(root);
|
|
};
|
|
|
|
/** Private input copy isolates upstream database and sidecar writes from the original. */
|
|
export class IdaWorkspace {
|
|
readonly sessionId = `rea-${randomUUID()}`;
|
|
#inputSha256: string | undefined;
|
|
readonly #files = new ArtifactResourceScope();
|
|
private constructor(
|
|
private readonly runtime: PrivateRuntimeRoot,
|
|
readonly inputPath: string,
|
|
private readonly target: BinaryTarget,
|
|
) {}
|
|
|
|
/** Filesystem root owned exclusively by this headless session. */
|
|
get directory(): string {
|
|
return this.runtime.path;
|
|
}
|
|
|
|
/** SHA-256 of the private input bytes copyInput admitted; undefined until prepared. */
|
|
get inputSha256(): string | undefined {
|
|
return this.#inputSha256;
|
|
}
|
|
|
|
/** Allocate a protected workspace so its owner can retain it before input preparation. */
|
|
static async allocate(
|
|
target: BinaryTarget,
|
|
root = tmpdir(),
|
|
): Promise<IdaWorkspace> {
|
|
// Upstream reports canonical paths (macOS /var -> /private/var), so the
|
|
// private input path must already be canonical for identity checks.
|
|
let runtime: PrivateRuntimeRoot;
|
|
try {
|
|
runtime = await PrivateRuntimeRoot.create({
|
|
parent: await resolveIdaWorkspaceParent(root),
|
|
prefix: "rea-ida-",
|
|
});
|
|
} catch (cause: unknown) {
|
|
if (cause instanceof AnalysisError) throw cause;
|
|
throw new ConfigurationError(
|
|
"IDA workspaceRoot is not a usable private workspace parent.",
|
|
{
|
|
cause,
|
|
settings: [
|
|
{
|
|
setting: "workspaceRoot",
|
|
constraint: `Select an existing writable and searchable directory: ${errorMessage(cause)}`,
|
|
},
|
|
],
|
|
diagnostics: { workspace_root: root },
|
|
},
|
|
);
|
|
}
|
|
const inputPath = join(runtime.path, basename(target.path));
|
|
return new IdaWorkspace(runtime, inputPath, target);
|
|
}
|
|
|
|
/**
|
|
* Copy and verify admitted bytes after the session owns this workspace's
|
|
* cleanup. Universal Mach-O containers are thinned to the selected slice so
|
|
* the upstream engine analyzes the architecture admission reported; the
|
|
* whole-source digest still anchors the copy against admission-time changes.
|
|
*/
|
|
copyInput(signal?: AbortSignal): Promise<void> {
|
|
return this.#files.run(async () => {
|
|
assertArtifactReadActive(signal);
|
|
const source = await open(this.target.path, "r");
|
|
await withArtifactFileHandle(
|
|
this.target.path,
|
|
source,
|
|
this.#files,
|
|
signal,
|
|
async (source) => {
|
|
assertArtifactReadActive(signal);
|
|
const input = await open(this.inputPath, "wx");
|
|
await withArtifactFileHandle(
|
|
this.inputPath,
|
|
input,
|
|
this.#files,
|
|
signal,
|
|
(input) => this.#copyBytes(source, input, signal),
|
|
);
|
|
},
|
|
);
|
|
});
|
|
}
|
|
|
|
async #copyBytes(
|
|
source: FileHandle,
|
|
input: FileHandle,
|
|
signal: AbortSignal | undefined,
|
|
): Promise<void> {
|
|
const slice =
|
|
this.target.format === "mach-o-universal"
|
|
? this.target.universalSlice
|
|
: undefined;
|
|
const sourceHash = createHash("sha256");
|
|
const inputHash = createHash("sha256");
|
|
const chunk = Buffer.allocUnsafe(64 * 1024);
|
|
let position = 0;
|
|
for (;;) {
|
|
assertArtifactReadActive(signal);
|
|
const observed = await source.read(chunk, 0, chunk.length, position);
|
|
if (observed.bytesRead === 0) break;
|
|
assertArtifactReadActive(signal);
|
|
const bytes = chunk.subarray(0, observed.bytesRead);
|
|
sourceHash.update(bytes);
|
|
const start =
|
|
slice === undefined ? position : Math.max(position, slice.offset);
|
|
const end =
|
|
slice === undefined
|
|
? position + bytes.length
|
|
: Math.min(position + bytes.length, slice.offset + slice.size);
|
|
// A chunk after the slice has end < position, and a negative subarray
|
|
// bound would count from the chunk's end instead of being empty.
|
|
if (end > start) {
|
|
const admitted = bytes.subarray(start - position, end - position);
|
|
inputHash.update(admitted);
|
|
let offset = 0;
|
|
while (offset < admitted.length) {
|
|
assertArtifactReadActive(signal);
|
|
const { bytesWritten } = await input.write(
|
|
admitted,
|
|
offset,
|
|
admitted.length - offset,
|
|
);
|
|
if (bytesWritten === 0)
|
|
throw new AnalysisProtocolError(
|
|
"IDA input copy write made no progress",
|
|
);
|
|
offset += bytesWritten;
|
|
}
|
|
assertArtifactReadActive(signal);
|
|
}
|
|
position += observed.bytesRead;
|
|
}
|
|
if (sourceHash.digest("hex") !== this.target.sha256)
|
|
throw new AnalysisProtocolError(
|
|
"IDA target changed between admission and the private input copy; reopen the target.",
|
|
);
|
|
this.#inputSha256 = inputHash.digest("hex");
|
|
}
|
|
|
|
/** Delete only this object's privately allocated directory after worker cleanup. */
|
|
async remove(): Promise<void> {
|
|
await this.#files.close();
|
|
await this.runtime.close();
|
|
}
|
|
}
|