diff --git a/docs/testing.md b/docs/testing.md index 3f1b2d68b..1685b361d 100644 --- a/docs/testing.md +++ b/docs/testing.md @@ -385,8 +385,10 @@ installation, scoped doctor, real CLI/MCP analysis, rejection of a modified launcher, idempotent setup without requesting a reinstall, and owned child cleanup. Before installation and after teardown, a clean-runner process scan also rejects surviving Hopper demo helpers or Xvfb groups, including orphans -whose launcher exited. This scan observes processes without signaling them; -the dedicated runner must have no unrelated Hopper/Xvfb sessions. It requires +whose launcher exited. This scan observes processes without signaling them. +Hopper detection compares executable file identity through `/proc`, covering +alternate or configured launcher paths even after an invoked symlink is removed. +The dedicated runner must have no unrelated Hopper/Xvfb sessions. It requires the repository's pinned development toolchain and an existing host C compiler, `file`, `nm`, and `strings`. Use `verify:hopper:linux` instead when Hopper is already installed. diff --git a/scripts/lib/clean-linux-hopper-runtime.d.mts b/scripts/lib/clean-linux-hopper-runtime.d.mts index c860c876d..dd1010aa1 100644 --- a/scripts/lib/clean-linux-hopper-runtime.d.mts +++ b/scripts/lib/clean-linux-hopper-runtime.d.mts @@ -1 +1,3 @@ -export function assertCleanLinuxHopperRuntime(): Promise; +export function assertCleanLinuxHopperRuntime( + launcherPaths?: readonly string[], +): Promise; diff --git a/scripts/lib/clean-linux-hopper-runtime.mjs b/scripts/lib/clean-linux-hopper-runtime.mjs index 2fc8fbfad..f7556ec67 100644 --- a/scripts/lib/clean-linux-hopper-runtime.mjs +++ b/scripts/lib/clean-linux-hopper-runtime.mjs @@ -1,5 +1,6 @@ import { execFile } from "node:child_process"; import { promisify } from "node:util"; +import { readdir, stat } from "node:fs/promises"; const execFileAsync = promisify(execFile); @@ -8,8 +9,51 @@ const execFileAsync = promisify(execFile); * groups reparented after their launcher exits. This is not ownership proof and * never authorizes signaling; unrelated runtime processes also block this lane. */ -export const assertCleanLinuxHopperRuntime = async () => { +export const assertCleanLinuxHopperRuntime = async ( + launcherPaths = [ + "/opt/hopper/bin/Hopper", + ...(process.env.HOPPER_LAUNCHER_PATH === undefined + ? [] + : [process.env.HOPPER_LAUNCHER_PATH]), + ], +) => { const { stdout } = await execFileAsync("ps", ["-ax", "-o", "command="]); if (/\/opt\/hopper\/bin\/Hopper|\bXvfb\b|hopper-demo-x11\.py/u.test(stdout)) throw new Error("Clean Linux runner retained a Hopper demo process"); + const launchers = []; + for (const path of launcherPaths) { + try { + launchers.push(await stat(path, { bigint: true })); + } catch (error) { + if (error?.code !== "ENOENT") throw error; + } + } + if (launchers.length === 0) return; + // /proc exposes the executed file even when argv[0] names a removed symlink. + // Compare file identity, not a display path or a process name. + for (const entry of await readdir("/proc", { withFileTypes: true })) { + if (!entry.isDirectory() || !/^\d+$/u.test(entry.name)) continue; + let executable; + try { + executable = await stat(`/proc/${entry.name}/exe`, { bigint: true }); + } catch (error) { + if (error?.code === "ENOENT" || error?.code === "ESRCH") continue; + // Other users' executables may be unreadable. The setup lane launches + // providers as the caller; unreadable caller-owned processes fail closed. + if (error?.code === "EACCES") { + const owner = await stat(`/proc/${entry.name}`).catch((cause) => { + if (cause?.code === "ENOENT") return undefined; + throw cause; + }); + if (owner === undefined || owner.uid !== process.getuid()) continue; + } + throw error; + } + if ( + launchers.some( + ({ dev, ino }) => dev === executable.dev && ino === executable.ino, + ) + ) + throw new Error("Clean Linux runner retained a Hopper demo process"); + } }; diff --git a/tests/process-global/cleanLinuxHopperRuntime.test.ts b/tests/process-global/cleanLinuxHopperRuntime.test.ts index af16911fb..bc86aea77 100644 --- a/tests/process-global/cleanLinuxHopperRuntime.test.ts +++ b/tests/process-global/cleanLinuxHopperRuntime.test.ts @@ -1,5 +1,16 @@ import { execFile } from "node:child_process"; import { randomUUID } from "node:crypto"; +import { + chmod, + copyFile, + mkdtemp, + readFile, + rm, + symlink, + unlink, +} from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; import { promisify } from "node:util"; import { expect, it } from "vitest"; @@ -10,21 +21,34 @@ import { cleanupOwnedProcessGroup } from "../../src/process/ProcessOwnership.js" const execFileAsync = promisify(execFile); -it.runIf(process.platform === "linux")( - "rejects a reparented demo process without signaling it", - async () => { +it + .runIf(process.platform === "linux") + .each(["demo helper", "alternate launcher"])( + "rejects a reparented %s without signaling it", + async (mode) => { const runId = randomUUID(); const childCode = "process.title = 'Xvfb'; setInterval(() => {}, 1000); process.send('ready');"; + const directory = await mkdtemp(join(tmpdir(), "rea-clean-runtime-test-")); + const launcher = join(directory, "Hopper"); + const alternate = join(directory, "Hopper-alternate"); + const alternateMode = mode === "alternate launcher"; + if (alternateMode) { + await copyFile("/usr/bin/sleep", launcher); + await chmod(launcher, 0o700); + await symlink(launcher, alternate); + } + const command = alternateMode ? alternate : process.execPath; + const arguments_ = alternateMode ? ["30"] : ["-e", childCode]; const parentCode = ` const { spawn } = require('node:child_process'); - const child = spawn(process.execPath, ['-e', ${JSON.stringify(childCode)}], { - detached: true, stdio: ['ignore', 'ignore', 'ignore', 'ipc'] + const child = spawn(${JSON.stringify(command)}, ${JSON.stringify(arguments_)}, { + detached: true, stdio: ${JSON.stringify(alternateMode ? "ignore" : ["ignore", "ignore", "ignore", "ipc"])} }); child.once('error', () => process.exit(1)); - child.once('message', () => { + child.once(${JSON.stringify(alternateMode ? "spawn" : "message")}, () => { process.stdout.write(JSON.stringify({ pid: child.pid, parentPid: process.pid })); - child.disconnect(); + if (child.connected) child.disconnect(); child.unref(); }); `; @@ -64,9 +88,17 @@ it.runIf(process.platform === "linux")( lineage.process_lineage.descendants.some((p) => p.pid === pid), ).toBe(false); - await expect(assertCleanLinuxHopperRuntime()).rejects.toThrow( - "Clean Linux runner retained a Hopper demo process", - ); + if (alternateMode) { + const commandLine = await readFile( + `/proc/${String(pid)}/cmdline`, + "utf8", + ); + expect(commandLine.split("\0")[0]).toBe(alternate); + await unlink(alternate); + } + await expect( + assertCleanLinuxHopperRuntime(alternateMode ? [launcher] : []), + ).rejects.toThrow("Clean Linux runner retained a Hopper demo process"); // Observation must not terminate the fixture, even though it resembles Xvfb. const { stdout: alive } = await execFileAsync("ps", [ "-p", @@ -84,6 +116,7 @@ it.runIf(process.platform === "linux")( }); expect(cleanup.cleaned, JSON.stringify(cleanup)).toBe(true); } + await rm(directory, { recursive: true, force: true }); } }, );