mirror of
https://github.com/morluto/rea.git
synced 2026-11-16 11:43:20 +00:00
* build(scripts): type-check the shared verifier run, fixture, cleanup and source loader The five scripts/lib modules join tsconfig.scripts.json (69 errors). Named types come from their existing declaration files. The source loader probes the runtime's TypeScript transform once and refuses a Node.js without transform mode (Node.js 26 removed it) with the pinned-runtime message instead of failing on the first owned source file. * build(scripts): type-check the managed PE and function dossier fixtures (#11611) * build(scripts): type-check the managed PE and function dossier fixtures pe.mjs and function-dossier.mjs join tsconfig.scripts.json (68 errors). Named option types come from pe.d.mts; heap names are paired with their rows instead of read back by index; optional metadata tables are added to the row map directly; a placed body's RVA is computed by bodyRva, which refuses an unplaced body. Fixture bytes are unchanged. * build(scripts): type-check the managed conformance entry point and support (#11612) conformance.mjs, support.mjs and config.mjs join tsconfig.scripts.json (34 errors), completing the managed conformance closure. Result checks narrow with assert.ok, Evidence results are read through their owners' schemas, the native function subject is an EvidenceSubjectTarget, the comparison check no longer dereferences a right-only method's null left side, and the fixture callback returns the report instead of assigning outer variables.
260 lines
8.2 KiB
JavaScript
260 lines
8.2 KiB
JavaScript
import { spawn } from "node:child_process";
|
|
import { randomUUID } from "node:crypto";
|
|
import { readFile, readdir } from "node:fs/promises";
|
|
|
|
// Named types come from the declaration file TypeScript consumers import.
|
|
/** @typedef {import("./verifier-run.mjs").VerifierRunStart} VerifierRunStart */
|
|
/** @typedef {import("./verifier-run.mjs").VerifierRun} VerifierRun */
|
|
/** @typedef {import("./verifier-run.mjs").VerifierProcessLineage} VerifierProcessLineage */
|
|
/** @typedef {{ readonly pid: number; readonly parentPid: number; readonly processGroupId: number }} ProcessMember */
|
|
|
|
/** @type {VerifierRunStart | undefined} */
|
|
let activeVerifierRun;
|
|
|
|
/**
|
|
* Allocate one verifier identity before work and propagate it to child processes.
|
|
* @returns {VerifierRunStart}
|
|
*/
|
|
export const createVerifierRun = () => {
|
|
if (activeVerifierRun !== undefined) {
|
|
process.env.REA_PROCESS_RUN_ID = activeVerifierRun.run_id;
|
|
return activeVerifierRun;
|
|
}
|
|
activeVerifierRun = Object.freeze({
|
|
run_id: randomUUID(),
|
|
verifier_pid: process.pid,
|
|
parent_pid: process.ppid,
|
|
});
|
|
process.env.REA_PROCESS_RUN_ID = activeVerifierRun.run_id;
|
|
return activeVerifierRun;
|
|
};
|
|
|
|
/**
|
|
* Complete a verifier report with a token-verified point-in-time lineage.
|
|
*
|
|
* An empty descendant list means none were live during this observation. It
|
|
* does not prove that the verifier never launched children earlier in the run.
|
|
* @param {VerifierRunStart} run
|
|
* @returns {Promise<VerifierRun>}
|
|
*/
|
|
export const completeVerifierRun = async (run) => {
|
|
const observedAt = new Date().toISOString();
|
|
return {
|
|
...run,
|
|
process_lineage: await observeVerifierLineage(run, observedAt),
|
|
};
|
|
};
|
|
|
|
/**
|
|
* Require positive process-lineage evidence before claiming cleanup complete.
|
|
* @param {VerifierProcessLineage} processLineage
|
|
*/
|
|
export const assertVerifierCleanupComplete = (processLineage) => {
|
|
if (processLineage.status !== "verified")
|
|
throw new Error(
|
|
`Verifier cleanup could not be verified: ${processLineage.reason}`,
|
|
);
|
|
if (processLineage.descendants.length !== 0)
|
|
throw new Error("Owned descendants survived verifier cleanup");
|
|
};
|
|
|
|
/**
|
|
* @param {VerifierRunStart} run
|
|
* @param {string} observedAt
|
|
* @returns {Promise<VerifierProcessLineage>}
|
|
*/
|
|
const observeVerifierLineage = async (run, observedAt) => {
|
|
if (process.platform === "win32")
|
|
return unavailableLineage(
|
|
run,
|
|
observedAt,
|
|
null,
|
|
"The verifier has no retained native Job Object lease for lineage observation; provider jobs are verified by their native owners",
|
|
);
|
|
try {
|
|
const { members, observerPid } = await processSnapshot();
|
|
const launcher = members.find(({ pid }) => pid === run.verifier_pid);
|
|
if (launcher === undefined)
|
|
return unavailableLineage(
|
|
run,
|
|
observedAt,
|
|
null,
|
|
"verifier launcher was absent from the process snapshot",
|
|
);
|
|
if (launcher.parentPid !== run.parent_pid)
|
|
return unavailableLineage(
|
|
run,
|
|
observedAt,
|
|
launcher.processGroupId,
|
|
"verifier launcher parent changed before lineage observation",
|
|
);
|
|
const descendants = descendantMembers(
|
|
members.filter(({ pid }) => pid !== observerPid),
|
|
run.verifier_pid,
|
|
);
|
|
for (const member of descendants) {
|
|
let memberRunId;
|
|
try {
|
|
memberRunId = await processRunId(member.pid);
|
|
} catch {
|
|
return unavailableLineage(
|
|
run,
|
|
observedAt,
|
|
launcher.processGroupId,
|
|
`descendant ${String(member.pid)} environment was unreadable`,
|
|
);
|
|
}
|
|
if (memberRunId !== run.run_id)
|
|
return unavailableLineage(
|
|
run,
|
|
observedAt,
|
|
launcher.processGroupId,
|
|
`descendant ${String(member.pid)} run token did not match`,
|
|
);
|
|
}
|
|
return {
|
|
status: "verified",
|
|
observed_at: observedAt,
|
|
launcher_pid: run.verifier_pid,
|
|
launcher_parent_pid: run.parent_pid,
|
|
process_group_id: launcher.processGroupId,
|
|
descendants: descendants
|
|
.map(({ pid, parentPid, processGroupId }) => ({
|
|
pid,
|
|
parent_pid: parentPid,
|
|
process_group_id: processGroupId,
|
|
}))
|
|
.sort((left, right) => left.pid - right.pid),
|
|
};
|
|
} catch {
|
|
return unavailableLineage(
|
|
run,
|
|
observedAt,
|
|
null,
|
|
"operating-system process lineage could not be inspected",
|
|
);
|
|
}
|
|
};
|
|
|
|
/**
|
|
* @param {VerifierRunStart} run
|
|
* @param {string} observedAt
|
|
* @param {number | null} processGroupId
|
|
* @param {string} reason
|
|
* @returns {VerifierProcessLineage}
|
|
*/
|
|
const unavailableLineage = (run, observedAt, processGroupId, reason) => ({
|
|
status: "unavailable",
|
|
observed_at: observedAt,
|
|
launcher_pid: run.verifier_pid,
|
|
launcher_parent_pid: run.parent_pid,
|
|
process_group_id: processGroupId,
|
|
reason,
|
|
});
|
|
|
|
/** @returns {Promise<{ members: ProcessMember[]; observerPid: number | null }>} */
|
|
const processSnapshot = async () => {
|
|
if (process.platform === "linux") return linuxProcessSnapshot();
|
|
return posixProcessSnapshot();
|
|
};
|
|
|
|
const linuxProcessSnapshot = async () => {
|
|
const entries = await readdir("/proc", { withFileTypes: true });
|
|
/** @type {ProcessMember[]} */
|
|
const members = [];
|
|
for (const entry of entries) {
|
|
if (!entry.isDirectory() || !/^\d+$/u.test(entry.name)) continue;
|
|
try {
|
|
const stat = await readFile(`/proc/${entry.name}/stat`, "utf8");
|
|
const match = /^\d+ \(.*\) (\S) (\d+) (\d+) /u.exec(stat);
|
|
if (match === null || match[1] === "Z") continue;
|
|
members.push({
|
|
pid: Number(entry.name),
|
|
parentPid: Number(match[2]),
|
|
processGroupId: Number(match[3]),
|
|
});
|
|
} catch {
|
|
// Processes can exit while /proc is being sampled.
|
|
}
|
|
}
|
|
return { members, observerPid: null };
|
|
};
|
|
|
|
const posixProcessSnapshot = async () => {
|
|
const child = spawn("ps", ["-axo", "pid=,ppid=,pgid=,stat="], {
|
|
stdio: ["ignore", "pipe", "pipe"],
|
|
});
|
|
/** @type {Buffer[]} */
|
|
const stdout = [];
|
|
/** @type {Buffer[]} */
|
|
const stderr = [];
|
|
child.stdout.on("data", (chunk) => stdout.push(chunk));
|
|
child.stderr.on("data", (chunk) => stderr.push(chunk));
|
|
const code = await new Promise((resolve, reject) => {
|
|
child.once("error", reject);
|
|
child.once("close", resolve);
|
|
});
|
|
if (code !== 0)
|
|
throw new Error(`ps failed: ${Buffer.concat(stderr).toString("utf8")}`);
|
|
const members = Buffer.concat(stdout)
|
|
.toString("utf8")
|
|
.split("\n")
|
|
.flatMap((line) => {
|
|
const match = /\s*(\d+)\s+(\d+)\s+(\d+)\s+(\S+)/u.exec(line);
|
|
return match === null || match[4]?.startsWith("Z")
|
|
? []
|
|
: [
|
|
{
|
|
pid: Number(match[1]),
|
|
parentPid: Number(match[2]),
|
|
processGroupId: Number(match[3]),
|
|
},
|
|
];
|
|
});
|
|
return { members, observerPid: child.pid ?? null };
|
|
};
|
|
|
|
/**
|
|
* @param {readonly ProcessMember[]} members
|
|
* @param {number} launcherPid
|
|
*/
|
|
const descendantMembers = (members, launcherPid) => {
|
|
const descendantPids = new Set([launcherPid]);
|
|
let previousSize = -1;
|
|
while (previousSize !== descendantPids.size) {
|
|
previousSize = descendantPids.size;
|
|
for (const member of members)
|
|
if (descendantPids.has(member.parentPid)) descendantPids.add(member.pid);
|
|
}
|
|
descendantPids.delete(launcherPid);
|
|
return members.filter(({ pid }) => descendantPids.has(pid));
|
|
};
|
|
|
|
/**
|
|
* @param {number} pid
|
|
* @returns {Promise<string | undefined>}
|
|
*/
|
|
const processRunId = async (pid) => {
|
|
if (process.platform === "linux") {
|
|
const environment = await readFile(`/proc/${String(pid)}/environ`, "utf8");
|
|
return environment
|
|
.split("\0")
|
|
.find((entry) => entry.startsWith("REA_PROCESS_RUN_ID="))
|
|
?.slice("REA_PROCESS_RUN_ID=".length);
|
|
}
|
|
const child = spawn("ps", ["eww", "-p", String(pid)], {
|
|
stdio: ["ignore", "pipe", "ignore"],
|
|
});
|
|
/** @type {Buffer[]} */
|
|
const stdout = [];
|
|
child.stdout.on("data", (chunk) => stdout.push(chunk));
|
|
const code = await new Promise((resolve, reject) => {
|
|
child.once("error", reject);
|
|
child.once("close", resolve);
|
|
});
|
|
if (code !== 0) throw new Error("process environment unavailable");
|
|
return /(?:^|\s)REA_PROCESS_RUN_ID=([^\s]*)/u.exec(
|
|
Buffer.concat(stdout).toString("utf8"),
|
|
)?.[1];
|
|
};
|