mirror of
https://github.com/morluto/rea.git
synced 2026-11-21 14:05:49 +00:00
test(process): verify shared helper publication and cancellation at ownership boundary
563 lines
21 KiB
TypeScript
563 lines
21 KiB
TypeScript
import { execFile } from "node:child_process";
|
|
import {
|
|
chmod,
|
|
lstat,
|
|
mkdir,
|
|
readFile,
|
|
readdir,
|
|
realpath,
|
|
rm,
|
|
stat,
|
|
symlink,
|
|
utimes,
|
|
writeFile,
|
|
} from "node:fs/promises";
|
|
import { basename, dirname, join } from "node:path";
|
|
import { promisify } from "node:util";
|
|
import { expect, it, vi } from "vitest";
|
|
|
|
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
|
import { writeFakeXcrun } from "../../fixtures/darwinHelperToolchain.js";
|
|
import { waitFor } from "../../support/testWait.js";
|
|
import {
|
|
aclListingGrantsWrite,
|
|
createDarwinHelperCache,
|
|
darwinHelperCacheDirectory,
|
|
defaultDarwinHelperCacheDirectory,
|
|
} from "../../../src/process/DarwinHelperCache.js";
|
|
import { createDarwinProcessRunTokenReader } from "../../../src/process/DarwinProcessRunTokenReader.js";
|
|
import { createSystemProcessOwnershipHost } from "../../../src/process/ProcessOwnershipObservation.js";
|
|
|
|
const execFileAsync = promisify(execFile);
|
|
const posix = process.platform !== "win32";
|
|
const onDarwin = process.platform === "darwin";
|
|
|
|
const exists = (path: string): Promise<boolean> =>
|
|
lstat(path).then(
|
|
() => true,
|
|
() => false,
|
|
);
|
|
|
|
/** One cache over fixture sources and toolchain, plus a helper to publish. */
|
|
const fixtureCache = async (
|
|
prefix: string,
|
|
{ rootParent = "cache", stalledLookups = false } = {},
|
|
) => {
|
|
const directory = await createTestTempDirectory(prefix);
|
|
const source = join(directory, "Reader.swift");
|
|
await writeFile(source, "// helper source v1\n");
|
|
const compiled = join(directory, "compiled-reader");
|
|
await writeFile(compiled, "#!/bin/sh\necho compiled\n");
|
|
await chmod(compiled, 0o755);
|
|
const xcrun = await writeFakeXcrun(directory, false, stalledLookups);
|
|
const root = join(directory, rootParent, "process-helper");
|
|
const create = () =>
|
|
createDarwinHelperCache({
|
|
directory: root,
|
|
sources: [source],
|
|
xcrun: xcrun.executable,
|
|
environment: process.env,
|
|
});
|
|
return { directory, source, compiled, root, xcrun, create };
|
|
};
|
|
|
|
const missKey = async (
|
|
cache: ReturnType<typeof createDarwinHelperCache>,
|
|
): Promise<string> => {
|
|
const lookup = await cache.lookup();
|
|
if (lookup.kind !== "miss")
|
|
throw new Error(`Expected a miss: ${lookup.kind}`);
|
|
return lookup.key;
|
|
};
|
|
|
|
it.skipIf(!posix)(
|
|
"keys entries on helper sources and the toolchain xcrun resolves",
|
|
async () => {
|
|
const fixture = await fixtureCache("rea-helper-cache-key-");
|
|
const keys = [await missKey(fixture.create())];
|
|
await writeFile(fixture.source, "// helper source v2\n");
|
|
keys.push(await missKey(fixture.create()));
|
|
// An in-place compiler or SDK update keeps paths but changes identity.
|
|
await writeFile(fixture.xcrun.swiftc, "compiler v2 (longer)");
|
|
keys.push(await missKey(fixture.create()));
|
|
const later = new Date(Date.now() + 60_000);
|
|
await utimes(join(fixture.xcrun.sdk, "SDKSettings.json"), later, later);
|
|
keys.push(await missKey(fixture.create()));
|
|
expect(new Set(keys).size).toBe(keys.length);
|
|
},
|
|
);
|
|
|
|
it.skipIf(!posix)(
|
|
"never executes or removes an invalid, linked, or loosened entry",
|
|
async () => {
|
|
const fixture = await fixtureCache("rea-helper-cache-invalid-");
|
|
const corruptions: Record<string, (entry: string) => Promise<void>> = {
|
|
"changed helper bytes": (entry) =>
|
|
writeFile(join(entry, "reader"), "#!/bin/sh\necho replaced\n"),
|
|
"manifest for another key": async (entry) => {
|
|
const manifest = JSON.parse(
|
|
await readFile(join(entry, "manifest.json"), "utf8"),
|
|
) as Record<string, unknown>;
|
|
await writeFile(
|
|
join(entry, "manifest.json"),
|
|
JSON.stringify({ ...manifest, key: "0".repeat(64) }),
|
|
);
|
|
},
|
|
"group-writable entry": (entry) => chmod(entry, 0o770),
|
|
"group-writable helper": (entry) => chmod(join(entry, "reader"), 0o770),
|
|
"malformed manifest": (entry) =>
|
|
writeFile(join(entry, "manifest.json"), "{"),
|
|
"linked helper": async (entry) => {
|
|
const target = join(fixture.directory, "elsewhere");
|
|
await writeFile(target, "#!/bin/sh\necho elsewhere\n");
|
|
await chmod(target, 0o700);
|
|
await rm(join(entry, "reader"));
|
|
await symlink(target, join(entry, "reader"));
|
|
},
|
|
"manifest FIFO": async (entry) => {
|
|
await rm(join(entry, "manifest.json"));
|
|
await execFileAsync("/usr/bin/mkfifo", [join(entry, "manifest.json")]);
|
|
},
|
|
};
|
|
for (const [name, corrupt] of Object.entries(corruptions)) {
|
|
const cache = fixture.create();
|
|
const key = await missKey(cache);
|
|
expect(await cache.publish(key, fixture.compiled), name).toBeDefined();
|
|
await corrupt(join(fixture.root, key));
|
|
// Opening the FIFO must not wait for a writer.
|
|
expect(await fixture.create().lookup(), name).toEqual({
|
|
kind: "unavailable",
|
|
});
|
|
// Removal could race another process that verified this path.
|
|
expect(await exists(join(fixture.root, key)), name).toBe(true);
|
|
await chmod(join(fixture.root, key), 0o700);
|
|
await rm(join(fixture.root, key), { recursive: true, force: true });
|
|
}
|
|
},
|
|
);
|
|
|
|
it.skipIf(!posix)(
|
|
"never trusts a linked entry, an untrusted root, or an ancestor others can write",
|
|
async () => {
|
|
const fixture = await fixtureCache("rea-helper-cache-untrusted-");
|
|
const key = await missKey(fixture.create());
|
|
const valid = join(fixture.directory, "valid-entry");
|
|
await mkdir(valid, { mode: 0o700 });
|
|
await symlink(valid, join(fixture.root, key));
|
|
expect(await fixture.create().lookup()).toEqual({ kind: "unavailable" });
|
|
expect((await lstat(join(fixture.root, key))).isSymbolicLink()).toBe(true);
|
|
|
|
const loose = await fixtureCache("rea-helper-cache-loose-root-");
|
|
await mkdir(loose.root, { recursive: true });
|
|
await chmod(loose.root, 0o777);
|
|
const looseCache = loose.create();
|
|
expect(await looseCache.lookup()).toEqual({ kind: "unavailable" });
|
|
expect(await looseCache.publish("a".repeat(64), loose.compiled)).toBe(
|
|
undefined,
|
|
);
|
|
expect(await readdir(loose.root)).toEqual([]);
|
|
|
|
// Another user could rename the verified entry out of a shared parent.
|
|
const shared = await fixtureCache("rea-helper-cache-shared-parent-", {
|
|
rootParent: "shared/cache",
|
|
});
|
|
await mkdir(join(shared.directory, "shared"), { recursive: true });
|
|
await chmod(join(shared.directory, "shared"), 0o777);
|
|
expect(await shared.create().lookup()).toEqual({ kind: "unavailable" });
|
|
|
|
const blocked = await fixtureCache("rea-helper-cache-blocked-root-");
|
|
await writeFile(join(blocked.directory, "cache"), "not a directory");
|
|
expect(await blocked.create().lookup()).toEqual({ kind: "unavailable" });
|
|
},
|
|
);
|
|
|
|
it.skipIf(!posix)(
|
|
"keeps the first concurrent publication and leaves no staging directory",
|
|
async () => {
|
|
const fixture = await fixtureCache("rea-helper-cache-race-");
|
|
const key = await missKey(fixture.create());
|
|
const original = await readFile(fixture.compiled, "utf8");
|
|
const alternative = join(fixture.directory, "alternative-reader");
|
|
const different = "#!/bin/sh\necho alternative\n";
|
|
await writeFile(alternative, different);
|
|
const caches = [fixture.create(), fixture.create()] as const;
|
|
try {
|
|
const results = await Promise.allSettled([
|
|
caches[0].publish(key, fixture.compiled),
|
|
caches[1].publish(key, alternative),
|
|
]);
|
|
const published = join(await realpath(fixture.root), key, "reader");
|
|
const result = { status: "fulfilled", value: published };
|
|
expect(results).toEqual([result, result]);
|
|
const winner = await readFile(published, "utf8");
|
|
expect([original, different]).toContain(winner);
|
|
const loser = winner === original ? alternative : fixture.compiled;
|
|
expect(await caches[1].publish(key, loser)).toBe(published);
|
|
expect(await readFile(published, "utf8")).toBe(winner);
|
|
expect(await readdir(fixture.root)).toEqual([key]);
|
|
} finally {
|
|
await Promise.all(caches.map((cache) => cache.close()));
|
|
}
|
|
},
|
|
);
|
|
|
|
it.skipIf(!posix)(
|
|
"returns no shared helper when the compiled output or toolchain is unusable",
|
|
async () => {
|
|
const fixture = await fixtureCache("rea-helper-cache-unusable-");
|
|
const key = await missKey(fixture.create());
|
|
expect(
|
|
await fixture.create().publish(key, join(fixture.directory, "absent")),
|
|
).toBeUndefined();
|
|
const empty = join(fixture.directory, "empty");
|
|
await writeFile(empty, "");
|
|
expect(await fixture.create().publish(key, empty)).toBeUndefined();
|
|
expect(await readdir(fixture.root)).toEqual([]);
|
|
await rm(fixture.xcrun.swiftc);
|
|
expect(await fixture.create().lookup()).toEqual({ kind: "unavailable" });
|
|
},
|
|
);
|
|
|
|
it("shares a helper only for the default toolchain and an absolute home", () => {
|
|
const select = (
|
|
overrides: Partial<Parameters<typeof darwinHelperCacheDirectory>[0]>,
|
|
) =>
|
|
darwinHelperCacheDirectory({
|
|
home: "/Users/analyst",
|
|
compilerSelected: false,
|
|
moduleCachePath: undefined,
|
|
...overrides,
|
|
});
|
|
expect(select({})).toBe("/Users/analyst/Library/Caches/rea/process-helper");
|
|
// An empty module cache setting keeps the default, as documented.
|
|
expect(select({ moduleCachePath: "" })).toBe(
|
|
"/Users/analyst/Library/Caches/rea/process-helper",
|
|
);
|
|
expect(select({ moduleCachePath: "/boundary/modules" })).toBeUndefined();
|
|
expect(select({ compilerSelected: true })).toBeUndefined();
|
|
expect(select({ home: "relative/home" })).toBeUndefined();
|
|
expect(select({ home: undefined })).toBeUndefined();
|
|
});
|
|
|
|
it("keeps the caller's home path segments instead of normalizing them", () => {
|
|
// A symlink followed by `..` must resolve on the filesystem, not lexically.
|
|
expect(defaultDarwinHelperCacheDirectory("/Users/analyst/link/../home")).toBe(
|
|
"/Users/analyst/link/../home/Library/Caches/rea/process-helper",
|
|
);
|
|
});
|
|
|
|
it.skipIf(!posix).each(["cache lookup", "reader preparation"] as const)(
|
|
"cancels both stalled toolchain commands during %s",
|
|
async (operation) => {
|
|
const { create, xcrun, root } = await fixtureCache(
|
|
"rea-helper-cache-stall-",
|
|
{
|
|
stalledLookups: true,
|
|
},
|
|
);
|
|
const owner =
|
|
operation === "cache lookup"
|
|
? create()
|
|
: createDarwinProcessRunTokenReader({
|
|
compiler: { kind: "xcrun", executable: xcrun.executable },
|
|
helperCacheDirectory: root,
|
|
});
|
|
const controller = new AbortController();
|
|
const reason = new Error("caller cancelled");
|
|
let settled = false;
|
|
const result = (
|
|
"lookup" in owner
|
|
? owner.lookup(controller.signal)
|
|
: owner.prepare(controller.signal)
|
|
)
|
|
.catch((cause: unknown) => cause)
|
|
.finally(() => {
|
|
settled = true;
|
|
});
|
|
try {
|
|
const started = await waitFor(async () => {
|
|
const calls = await xcrun.stalledLookups();
|
|
if (!settled && calls.length < 2)
|
|
throw new Error("Waiting for both toolchain commands");
|
|
return calls;
|
|
});
|
|
expect(started.sort()).toEqual([
|
|
'["--find","swiftc"]',
|
|
'["--show-sdk-path"]',
|
|
]);
|
|
expect(settled).toBe(false);
|
|
const cancelledAt = performance.now();
|
|
controller.abort(reason);
|
|
const outcome = await result;
|
|
if (operation === "cache lookup")
|
|
expect(outcome).toEqual({ kind: "unavailable" });
|
|
else
|
|
expect(outcome).toMatchObject({
|
|
_tag: "ProcessOwnershipInspectionCancelled",
|
|
reason,
|
|
});
|
|
// Cancellation wins while both commands are held past their acknowledgement.
|
|
expect(performance.now() - cancelledAt).toBeLessThan(5_000);
|
|
expect(await xcrun.compilations()).toBe(0);
|
|
} finally {
|
|
controller.abort(reason);
|
|
await Promise.all([result, owner.close()]);
|
|
}
|
|
},
|
|
);
|
|
|
|
it("rejects only ACL entries that allow another principal to write", () => {
|
|
const cases = [
|
|
[" 0: group:everyone deny delete", false],
|
|
[" 0: group:everyone inherited deny delete", false],
|
|
[" 0: user:nobody allow read,search", false],
|
|
[" 0: FFFFEEEE-DDDD-CCCC-BBBB-AAAAFFFFFFFE allow write,append", true],
|
|
[" 1: user:nobody inherited allow add_file,file_inherit", true],
|
|
] as const;
|
|
// Default macOS deny-delete entries must leave the cache available.
|
|
for (const [entry, writable] of cases) {
|
|
const listing = `drwx------+ 3 analyst staff 96 31 Oct 09:00 /Users/analyst/x\n${entry}\n`;
|
|
expect(aclListingGrantsWrite(listing), entry).toBe(writable);
|
|
}
|
|
expect(aclListingGrantsWrite("-rw------- 1 analyst staff 0 x\n")).toBe(false);
|
|
});
|
|
|
|
it.skipIf(!onDarwin)(
|
|
"never trusts an entry or ancestor whose ACL lets another user write",
|
|
async () => {
|
|
const grants: Record<
|
|
string,
|
|
(fixture: {
|
|
root: string;
|
|
key: string;
|
|
directory: string;
|
|
}) => Promise<void>
|
|
> = {
|
|
"helper write": ({ root, key }) =>
|
|
execFileAsync("/bin/chmod", [
|
|
"+a",
|
|
"user:nobody allow write",
|
|
join(root, key, "reader"),
|
|
]).then(() => undefined),
|
|
"entry add_file": ({ root, key }) =>
|
|
execFileAsync("/bin/chmod", [
|
|
"+a",
|
|
"user:nobody allow add_file,delete_child",
|
|
join(root, key),
|
|
]).then(() => undefined),
|
|
"ancestor delete_child": ({ directory }) =>
|
|
execFileAsync("/bin/chmod", [
|
|
"+a",
|
|
"user:nobody allow delete_child",
|
|
join(directory, "cache"),
|
|
]).then(() => undefined),
|
|
};
|
|
for (const [name, grant] of Object.entries(grants)) {
|
|
const fixture = await fixtureCache(`rea-helper-cache-acl-`);
|
|
const cache = fixture.create();
|
|
const key = await missKey(cache);
|
|
expect(await cache.publish(key, fixture.compiled), name).toBeDefined();
|
|
// The default home ACL keeps sharing.
|
|
await execFileAsync("/bin/chmod", [
|
|
"+a",
|
|
"group:everyone deny delete",
|
|
fixture.root,
|
|
]);
|
|
expect((await fixture.create().lookup()).kind, name).toBe("hit");
|
|
await grant({ root: fixture.root, key, directory: fixture.directory });
|
|
expect(await fixture.create().lookup(), name).toEqual({
|
|
kind: "unavailable",
|
|
});
|
|
await execFileAsync("/bin/chmod", ["-RN", fixture.directory]);
|
|
}
|
|
},
|
|
);
|
|
|
|
const fixtureProcess = {
|
|
pid: 123,
|
|
parentPid: 1,
|
|
processGroupId: 123,
|
|
state: "S",
|
|
command: "fixture",
|
|
};
|
|
|
|
it.skipIf(!posix)(
|
|
"publishes one private helper that later readers execute and keep after close",
|
|
async () => {
|
|
const directory = await createTestTempDirectory("rea-helper-cache-reader-");
|
|
const helperCacheDirectory = join(directory, "process-helper");
|
|
const xcrun = await writeFakeXcrun(directory);
|
|
const create = () =>
|
|
createDarwinProcessRunTokenReader({
|
|
compiler: { kind: "xcrun", executable: xcrun.executable },
|
|
helperCacheDirectory,
|
|
});
|
|
const first = create();
|
|
let shared: string;
|
|
try {
|
|
shared = await first.prepare();
|
|
const root = await realpath(helperCacheDirectory);
|
|
const entry = dirname(shared);
|
|
expect(dirname(entry)).toBe(root);
|
|
expect(shared).toBe(join(entry, "reader"));
|
|
expect(await readdir(root)).toEqual([basename(entry)]);
|
|
const modes = await Promise.all(
|
|
[root, entry, shared, join(entry, "manifest.json")].map(
|
|
async (path) => (await stat(path)).mode & 0o777,
|
|
),
|
|
);
|
|
expect(modes).toEqual([0o700, 0o700, 0o700, 0o600]);
|
|
expect((await first.read([fixtureProcess])).get(123)).toEqual({
|
|
state: "readable",
|
|
runId: "fixture",
|
|
});
|
|
} finally {
|
|
await first.close();
|
|
}
|
|
const [compileRoot] = await xcrun.roots();
|
|
expect(compileRoot).toBeDefined();
|
|
expect(await exists(compileRoot ?? "")).toBe(false);
|
|
expect(await exists(shared)).toBe(true);
|
|
|
|
const second = create();
|
|
try {
|
|
expect(await second.prepare()).toBe(shared);
|
|
expect((await second.read([fixtureProcess])).get(123)).toEqual({
|
|
state: "readable",
|
|
runId: "fixture",
|
|
});
|
|
} finally {
|
|
await second.close();
|
|
}
|
|
expect(await xcrun.compilations()).toBe(1);
|
|
expect(await exists(shared)).toBe(true);
|
|
},
|
|
);
|
|
|
|
it.skipIf(!posix)(
|
|
"falls back to its own helper when a verified shared copy cannot launch",
|
|
async () => {
|
|
const directory = await createTestTempDirectory("rea-helper-cache-noexec-");
|
|
const helperCacheDirectory = join(directory, "process-helper");
|
|
const xcrun = await writeFakeXcrun(directory);
|
|
const create = () =>
|
|
createDarwinProcessRunTokenReader({
|
|
compiler: { kind: "xcrun", executable: xcrun.executable },
|
|
helperCacheDirectory,
|
|
});
|
|
const first = create();
|
|
let shared: string;
|
|
try {
|
|
shared = await first.prepare();
|
|
} finally {
|
|
await first.close();
|
|
}
|
|
const second = create();
|
|
try {
|
|
expect(await second.prepare()).toBe(shared);
|
|
// The verified copy is refused at launch, as on a noexec volume.
|
|
await chmod(shared, 0o600);
|
|
// Concurrent reads that both launched the shared copy both fall back.
|
|
const reads = await Promise.all([
|
|
second.read([fixtureProcess]),
|
|
second.read([fixtureProcess]),
|
|
]);
|
|
for (const read of reads)
|
|
expect(read.get(123)).toEqual({ state: "readable", runId: "fixture" });
|
|
} finally {
|
|
await second.close();
|
|
}
|
|
expect(await xcrun.compilations()).toBe(2);
|
|
expect((await stat(shared)).mode & 0o777).toBe(0o600);
|
|
},
|
|
);
|
|
|
|
it.skipIf(!posix)(
|
|
"publishes nothing when helper compilation is cancelled",
|
|
async () => {
|
|
const directory = await createTestTempDirectory("rea-helper-cache-cancel-");
|
|
const helperCacheDirectory = join(directory, "process-helper");
|
|
const xcrun = await writeFakeXcrun(directory, true);
|
|
const reader = createDarwinProcessRunTokenReader({
|
|
compiler: { kind: "xcrun", executable: xcrun.executable },
|
|
helperCacheDirectory,
|
|
});
|
|
const controller = new AbortController();
|
|
const reason = new Error("caller cancelled");
|
|
let settled = false;
|
|
const prepared = reader
|
|
.prepare(controller.signal)
|
|
.catch((cause: unknown) => cause)
|
|
.finally(() => {
|
|
settled = true;
|
|
});
|
|
try {
|
|
const root = await waitFor(async () => {
|
|
const [reported] = await xcrun.roots();
|
|
if (reported === undefined && !settled)
|
|
throw new Error("Waiting for the compiler's owned root");
|
|
return reported;
|
|
});
|
|
if (root === undefined)
|
|
throw new Error("Preparation settled before compilation started");
|
|
expect(settled).toBe(false);
|
|
expect((await stat(root)).isDirectory()).toBe(true);
|
|
controller.abort(reason);
|
|
expect(await prepared).toMatchObject({
|
|
_tag: "ProcessOwnershipInspectionCancelled",
|
|
reason,
|
|
});
|
|
await reader.close();
|
|
expect(await readdir(helperCacheDirectory)).toEqual([]);
|
|
await expect(lstat(root)).rejects.toMatchObject({ code: "ENOENT" });
|
|
} finally {
|
|
controller.abort(reason);
|
|
await Promise.all([prepared, reader.close()]);
|
|
}
|
|
},
|
|
);
|
|
|
|
it.skipIf(!onDarwin)(
|
|
"reuses the real default-toolchain helper in a later host without compiling",
|
|
async () => {
|
|
const home = await createTestTempDirectory("rea-helper-cache-home-");
|
|
const cacheRoot = join(home, "Library/Caches/rea/process-helper");
|
|
const environment: NodeJS.ProcessEnv = { ...process.env, HOME: home };
|
|
delete environment.REA_DARWIN_SWIFTC_PATH;
|
|
delete environment.REA_DARWIN_MODULE_CACHE_PATH;
|
|
const prepare = async (selected: NodeJS.ProcessEnv) => {
|
|
const host = createSystemProcessOwnershipHost("darwin", selected);
|
|
try {
|
|
await host.prepare?.(AbortSignal.timeout(180_000));
|
|
const current = (await host.listProcesses()).find(
|
|
({ pid }) => pid === process.pid,
|
|
);
|
|
if (current === undefined || host.processIdentities === undefined)
|
|
throw new Error("Darwin host omitted the current process identity");
|
|
expect(
|
|
(await host.processIdentities([current])).get(process.pid),
|
|
).toMatchObject({ state: "readable" });
|
|
} finally {
|
|
await host.close?.();
|
|
}
|
|
};
|
|
await prepare(environment);
|
|
const [key, ...others] = await readdir(cacheRoot);
|
|
expect(others).toEqual([]);
|
|
const helper = join(cacheRoot, key ?? "", "reader");
|
|
const published = await stat(helper);
|
|
// A compile would need a per-run root under TMPDIR; deny it.
|
|
const noTemporary = join(home, "no-temporary");
|
|
await mkdir(noTemporary, { mode: 0o500 });
|
|
vi.stubEnv("TMPDIR", noTemporary);
|
|
try {
|
|
await prepare(environment);
|
|
} finally {
|
|
vi.unstubAllEnvs();
|
|
}
|
|
expect((await stat(helper)).ino).toBe(published.ino);
|
|
expect(await readdir(cacheRoot)).toEqual([key]);
|
|
expect(await readdir(noTemporary)).toEqual([]);
|
|
},
|
|
);
|