mirror of
https://github.com/morluto/rea.git
synced 2026-11-21 14:05:49 +00:00
test(process): verify Darwin snapshot and helper reply ownership (#12904)
This commit is contained in:
1 parent
3a002fb708
commit
6e99f607f7
2 files changed
+121
-23
No files matched your search
@@ -24,6 +24,7 @@ import { promisify } from "node:util";
|
||||
import { _electron as electron } from "playwright-core";
|
||||
import { expect, it, onTestFinished, vi } from "vitest";
|
||||
import { z } from "zod";
|
||||
import { writeFakeXcrun } from "../../fixtures/darwinHelperToolchain.js";
|
||||
import { awaitAcknowledgement } from "../../fixtures/lifecycleAcknowledgement.js";
|
||||
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
||||
import { processTest } from "../../support/process/processFixture.js";
|
||||
@@ -488,6 +489,85 @@ const existsPath = async (target: string) =>
|
||||
() => false,
|
||||
);
|
||||
|
||||
it.skipIf(process.platform === "win32").each(["token", "identity"] as const)(
|
||||
"admits a complete PID-keyed %s reply and rejects malformed or mismatched replies",
|
||||
async (mode) => {
|
||||
const directory = await createTestTempDirectory("rea-helper-reply-");
|
||||
const xcrun = await writeFakeXcrun(directory);
|
||||
const reader = createDarwinProcessRunTokenReader({
|
||||
compiler: { kind: "xcrun", executable: xcrun.executable },
|
||||
});
|
||||
const tokenMode = mode === "token";
|
||||
const reply = join(directory, "reply");
|
||||
const arguments_ = tokenMode ? ["71", "84"] : ["--identities", "71", "84"];
|
||||
const read = tokenMode ? reader.read : reader.identities;
|
||||
const processes = [{ pid: 71 }, { pid: 84 }];
|
||||
const row = (pid: number, value: string) => ({
|
||||
pid,
|
||||
state: "readable",
|
||||
...(tokenMode ? { run_id: value } : { identity: value }),
|
||||
});
|
||||
const response = (...rows: ReturnType<typeof row>[]) =>
|
||||
JSON.stringify({ results: rows });
|
||||
const first = row(71, "first");
|
||||
const second = row(84, "second");
|
||||
try {
|
||||
// Use the real helper launcher with a controlled reply from the fixture.
|
||||
const helper = await reader.prepare();
|
||||
await writeFile(
|
||||
helper,
|
||||
`#!/usr/bin/env node
|
||||
const expected = ${JSON.stringify(arguments_)};
|
||||
if (JSON.stringify(process.argv.slice(2)) !== JSON.stringify(expected)) process.exit(64);
|
||||
process.stdout.write(process.getBuiltinModule("node:fs").readFileSync(${JSON.stringify(reply)}));
|
||||
`,
|
||||
);
|
||||
await writeFile(reply, response(second, first));
|
||||
const observed = (value: string) => ({
|
||||
state: "readable",
|
||||
...(tokenMode ? { runId: value } : { identity: value }),
|
||||
});
|
||||
expect(await read(processes)).toEqual(
|
||||
new Map([
|
||||
[71, observed("first")],
|
||||
[84, observed("second")],
|
||||
]),
|
||||
);
|
||||
for (const [scenario, body, code] of [
|
||||
["invalid JSON", "{", "json_invalid"],
|
||||
[
|
||||
"wrong row type",
|
||||
JSON.stringify({ results: [{ ...first, pid: "71" }, second] }),
|
||||
"response_invalid",
|
||||
],
|
||||
[
|
||||
"foreign PID in complete reply",
|
||||
response(first, row(97, "foreign")),
|
||||
"pid_set",
|
||||
],
|
||||
[
|
||||
"duplicate PID in complete reply",
|
||||
response(first, first, second),
|
||||
"pid_set",
|
||||
],
|
||||
["omitted PID", response(first), "pid_incomplete"],
|
||||
] as const) {
|
||||
await writeFile(reply, body);
|
||||
const failure = await read(processes).then(
|
||||
() => undefined,
|
||||
(cause: unknown) => cause,
|
||||
);
|
||||
expect(failure, scenario).toMatchObject({
|
||||
_tag: "ProcessAdmissionError",
|
||||
code: `${mode}_${code}`,
|
||||
});
|
||||
}
|
||||
} finally {
|
||||
await reader.close();
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
it
|
||||
.skipIf(process.platform === "win32")
|
||||
.each([
|
||||
@@ -1434,6 +1514,11 @@ it.skipIf(!onDarwin)(
|
||||
env: { ...process.env, REA_PROCESS_RUN_ID: "rea-snapshot-probe" },
|
||||
timeout: 10_000,
|
||||
});
|
||||
const invalidated = {
|
||||
state: "unavailable",
|
||||
reason: "process_identity_changed_during_token_read",
|
||||
tokenPresent: false,
|
||||
};
|
||||
expect(JSON.parse(stdout)).toEqual({
|
||||
preservedOtherErrno: true,
|
||||
retriedAfterFreshSize: true,
|
||||
@@ -1446,13 +1531,17 @@ it.skipIf(!onDarwin)(
|
||||
absentRunTokens: [
|
||||
{ pid: 42, state: "unavailable", reason: "process_not_in_snapshot" },
|
||||
],
|
||||
failedConfirmingRunTokens: [
|
||||
{
|
||||
state: "unavailable",
|
||||
reason: "process_table_failed_5",
|
||||
tokenPresent: false,
|
||||
},
|
||||
],
|
||||
refusedConfirmations: {
|
||||
failed: [
|
||||
{
|
||||
state: "unavailable",
|
||||
reason: "process_table_failed_5",
|
||||
tokenPresent: false,
|
||||
},
|
||||
],
|
||||
changedGeneration: [invalidated],
|
||||
changedPointerWidth: [invalidated],
|
||||
},
|
||||
confirmedRunTokens: [{ state: "readable", matchesFixtureToken: true }],
|
||||
successfulSnapshotMissingPidIsAbsent: true,
|
||||
failedSnapshotMissingPidIsUnavailable: true,
|
||||
|
||||
+25
-16
@@ -51,27 +51,36 @@ struct ProcessRunTokenSnapshotProbe {
|
||||
}
|
||||
|
||||
let ownPid = getpid()
|
||||
var confirmationSizeQueries = 0
|
||||
let singleProcessTable: ProcessTableSysctlCall = { _, output, size in
|
||||
if let output = output {
|
||||
func observeToken(
|
||||
confirmationError: Int32? = nil,
|
||||
changeConfirmation: (UnsafeMutablePointer<kinfo_proc>) -> Void = { _ in }
|
||||
) -> [RunTokenObservation] {
|
||||
var sizeQueries = 0
|
||||
return readRunTokens(pids: [ownPid]) { _, output, size in
|
||||
size = MemoryLayout<kinfo_proc>.stride
|
||||
guard let output else {
|
||||
sizeQueries += 1
|
||||
if sizeQueries == 2, let confirmationError {
|
||||
errno = confirmationError
|
||||
return -1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
let process = output.assumingMemoryBound(to: kinfo_proc.self)
|
||||
process.pointee.kp_proc.p_pid = ownPid
|
||||
process.pointee.kp_proc.p_flag = P_LP64
|
||||
if sizeQueries == 2 { changeConfirmation(process) }
|
||||
return 0
|
||||
}
|
||||
size = MemoryLayout<kinfo_proc>.stride
|
||||
return 0
|
||||
}
|
||||
let failedConfirmingTokens = readRunTokens(pids: [ownPid]) { mib, output, size in
|
||||
if output == nil {
|
||||
confirmationSizeQueries += 1
|
||||
if confirmationSizeQueries == 2 {
|
||||
errno = EIO
|
||||
return -1
|
||||
}
|
||||
}
|
||||
return singleProcessTable(&mib, output, &size)
|
||||
let confirmedTokens = observeToken()
|
||||
let refusedConfirmations = [
|
||||
"failed": observeToken(confirmationError: EIO),
|
||||
"changedGeneration": observeToken { $0.pointee.kp_proc.p_starttime.tv_usec = 1 },
|
||||
"changedPointerWidth": observeToken { $0.pointee.kp_proc.p_flag = 0 },
|
||||
].mapValues { tokens in
|
||||
tokens.map { ["state": $0.state, "reason": $0.reason ?? "", "tokenPresent": $0.run_id != nil] }
|
||||
}
|
||||
let confirmedTokens = readRunTokens(pids: [ownPid], sysctlCall: singleProcessTable)
|
||||
|
||||
let absentIdentities = readProcessIdentities(
|
||||
pids: [42],
|
||||
@@ -99,7 +108,7 @@ struct ProcessRunTokenSnapshotProbe {
|
||||
"openingRunTokenSnapshotReads": openingSizeQueries,
|
||||
"failedOpeningRunTokens": failedOpeningTokens.map { ["pid": $0.pid, "state": $0.state, "reason": $0.reason ?? ""] },
|
||||
"absentRunTokens": absentTokens.map { ["pid": $0.pid, "state": $0.state, "reason": $0.reason ?? ""] },
|
||||
"failedConfirmingRunTokens": failedConfirmingTokens.map { ["state": $0.state, "reason": $0.reason ?? "", "tokenPresent": $0.run_id != nil] },
|
||||
"refusedConfirmations": refusedConfirmations,
|
||||
"confirmedRunTokens": confirmedTokens.map { ["state": $0.state, "matchesFixtureToken": $0.run_id == "rea-snapshot-probe"] },
|
||||
"successfulSnapshotMissingPidIsAbsent": absentIdentities.first?.state == "absent" && absentIdentities.first?.reason == "not-in-snapshot",
|
||||
"failedSnapshotMissingPidIsUnavailable": failedIdentities.first?.state == "unavailable" && failedIdentities.first?.reason == "process_table_failed_13",
|
||||
|
||||
Reference in new issue
Block a user