From 2a30219f194cb094b18b5a19ffc5991b47924088 Mon Sep 17 00:00:00 2001 From: Dotta Date: Mon, 28 Sep 2026 15:13:37 -0500 Subject: [PATCH] Bound parallel copies when freezing native provider distributions Keep descriptor, digest, identity, and immutable-file checks for every entry while copying at most eight files and 32 MiB of buffers per batch. Larger admitted files run alone. Await every copy before cleanup and preserve canonical manifest order. Co-Authored-By: Paperclip --- .../native-distribution-integrity.test.ts | 126 +++++++++++++++++- .../acpx/native-distribution-integrity.ts | 26 +++- 2 files changed, 146 insertions(+), 6 deletions(-) diff --git a/packages/paperclip-runner/src/drivers/acpx/native-distribution-integrity.test.ts b/packages/paperclip-runner/src/drivers/acpx/native-distribution-integrity.test.ts index b837f847279..d140717ae31 100644 --- a/packages/paperclip-runner/src/drivers/acpx/native-distribution-integrity.test.ts +++ b/packages/paperclip-runner/src/drivers/acpx/native-distribution-integrity.test.ts @@ -1,17 +1,22 @@ import { createHash } from "node:crypto"; import { once } from "node:events"; -import { chmod, copyFile, mkdtemp, readFile, readdir, rm, symlink, writeFile } from "node:fs/promises"; +import { chmod, copyFile, mkdtemp, open, readFile, readdir, rm, stat, symlink, writeFile, type FileHandle } from "node:fs/promises"; import { createServer, type Server } from "node:net"; import { tmpdir } from "node:os"; import { dirname, join } from "node:path"; import type { ChildProcess } from "node:child_process"; -import { afterEach, describe, expect, it } from "vitest"; +import { afterEach, describe, expect, it, vi } from "vitest"; import { awaitVerifiedAcpxProviderExit, awaitVerifiedAcpxProviderOwnership, verifyNativeAcpxInstallation } from "./installation-integrity.js"; -import { parseNativeAcpxDistributionEntries, type NativeAcpxDistributionInput, type NativeAcpxDistributionEntry } from "./native-distribution-integrity.js"; +import { createNativeAcpxDistributionSnapshot, readNativeAcpxDistributionEntries, parseNativeAcpxDistributionEntries, type NativeAcpxDistributionInput, type NativeAcpxDistributionEntry } from "./native-distribution-integrity.js"; + +vi.mock("node:fs/promises", async (importOriginal) => { + const original = await importOriginal(); + return { ...original, rm: vi.fn(original.rm) }; +}); const roots: string[] = []; const hash = (value: string | Buffer) => createHash("sha256").update(value).digest("hex"); -afterEach(async () => { await Promise.all(roots.splice(0).map(root => rm(root, { recursive: true, force: true }))); }); +afterEach(async () => { vi.restoreAllMocks(); await Promise.all(roots.splice(0).map(root => rm(root, { recursive: true, force: true }))); }); async function fixture(options: { node?: boolean; script?: string } = {}): Promise { const root = await mkdtemp(join(tmpdir(), "native-acpx-test-")); roots.push(root); @@ -39,6 +44,29 @@ async function fixture(options: { node?: boolean; script?: string } = {}): Promi await writeFile(manifestPath, JSON.stringify({ entries })); return { distributionRoot: root, manifestPath, expectedClosureSha256: hash(JSON.stringify(entries)), executable: "runtime", ...(options.node ? { entrypoint: "entry.cjs" } : {}), fixedArguments: options.node ? [] : ["fixed"] }; } +async function manyFileFixture(sizes: number[]) { + const declaration = await fixture(); + const entries = await readNativeAcpxDistributionEntries(declaration); + for (let index = 0; index < sizes.length; index++) { + const path = `file-${String(index).padStart(2, "0")}`; + const bytes = Buffer.alloc(sizes[index]!, index + 1); + await writeFile(join(declaration.distributionRoot, path), bytes, { mode: 0o600 }); + entries.push({ path, sha256: hash(bytes), size: bytes.length, executable: false }); + } + entries.sort((a, b) => a.path < b.path ? -1 : a.path > b.path ? 1 : 0); + await writeFile(declaration.manifestPath, JSON.stringify({ entries })); + return { declaration: { ...declaration, expectedClosureSha256: hash(JSON.stringify(entries)) }, entries }; +} +async function filePrototype(path: string): Promise { + const probe = await open(path, "r"); const prototype = Object.getPrototypeOf(probe) as FileHandle; + await probe.close(); return prototype; +} +function gate() { + let release!: () => void; + const promise = new Promise((resolve) => { release = resolve; }); + return { promise, release }; +} + async function output(child: ChildProcess): Promise<{ text: string; error: string; code: number | null }> { let text = ""; let error = ""; child.stdout!.on("data", value => { text += String(value); }); @@ -89,6 +117,96 @@ describe("native ACPX execution closure", () => { const denied = await output((await (await verifyNativeAcpxInstallation(evil)).openCommand()).spawn()); expect(denied.code).not.toBe(0); expect(denied.error).toContain("escaped its closed distribution"); }, 30_000); + it("copies concurrently within its descriptor bound and retains canonical manifest order", async () => { + const { declaration, entries } = await manyFileFixture(Array.from({ length: 12 }, (_, index) => 100 + index)); + const prototype = await filePrototype(join(declaration.distributionRoot, "runtime")); + const originalRead = prototype.read; + const hold = gate(); const sizes: number[] = []; let active = 0; let peak = 0; + vi.spyOn(prototype, "read").mockImplementation(async function (this: FileHandle, ...args: any[]): Promise { + sizes.push(args[0].length); active++; peak = Math.max(peak, active); + try { await hold.promise; return await originalRead.apply(this, args as never); } + finally { active--; } + }); + const creating = createNativeAcpxDistributionSnapshot(declaration, entries); + try { + await vi.waitFor(() => expect(sizes).toHaveLength(8)); + expect(sizes.toSorted()).toEqual(Array.from({ length: 8 }, (_, index) => 100 + index)); + } finally { hold.release(); } + const created = await creating; + try { + expect(peak).toBe(8); + expect(Object.keys(created.snapshot.digests).slice(0, entries.length)).toEqual(entries.map(entry => join(created.snapshot.roots[0]!, entry.path))); + for (const entry of entries) expect(hash(await readFile(join(created.snapshot.roots[0]!, entry.path)))).toBe(entry.sha256); + } finally { await created.commandDirectory.close(); await created.snapshot.close(); } + }); + it("bounds simultaneous buffers and gives an oversized admitted file exclusive capacity", async () => { + const mib = 1024 * 1024; + const { declaration, entries } = await manyFileFixture([17 * mib, 17 * mib, 33 * mib]); + const prototype = await filePrototype(join(declaration.distributionRoot, "runtime")); + const originalRead = prototype.read; + const retained = new Map(); const observed: number[] = []; + vi.spyOn(prototype, "read").mockImplementation(async function (this: FileHandle, ...args: any[]): Promise { + if (!retained.has(this)) { + const file = this; const originalClose = file.close.bind(file); + file.close = async () => { try { await originalClose(); } finally { retained.delete(file); } }; + } + retained.set(this, args[0].length); + const total = [...retained.values()].reduce((sum, size) => sum + size, 0); observed.push(total); + expect(total).toBeLessThanOrEqual(Math.max(32 * mib, args[0].length)); + if (args[0].length > 32 * mib) expect(retained.size).toBe(1); + return originalRead.apply(this, args as never); + }); + const created = await createNativeAcpxDistributionSnapshot(declaration, entries); + try { expect(Math.max(...observed)).toBe(33 * mib); } + finally { await created.commandDirectory.close(); await created.snapshot.close(); } + }); + it("drains every admitted copy before failed-snapshot cleanup and schedules no later batch", async () => { + const { declaration, entries } = await manyFileFixture(Array.from({ length: 10 }, (_, index) => 91 + index)); + await writeFile(join(declaration.distributionRoot, "file-00"), Buffer.alloc(91, 99)); + const prototype = await filePrototype(join(declaration.distributionRoot, "runtime")); + const originalRead = prototype.read; + const hold = gate(); const entered = gate(); const invalidClosed = gate(); + const readSizes: number[] = []; const removalStart = vi.mocked(rm).mock.calls.length; + vi.spyOn(prototype, "read").mockImplementation(async function (this: FileHandle, ...args: any[]): Promise { + const size = args[0].length; readSizes.push(size); + if (size === 91) { + const originalClose = this.close.bind(this); + this.close = async () => { await originalClose(); invalidClosed.release(); }; + await entered.promise; + } + if (size === 92) { entered.release(); await hold.promise; } + return originalRead.apply(this, args as never); + }); + const creating = createNativeAcpxDistributionSnapshot(declaration, entries); + let settled = false; + void creating.then(() => { settled = true; }, () => { settled = true; }); + try { + await entered.promise; await invalidClosed.promise; + expect(settled).toBe(false); + } finally { hold.release(); } + await expect(creating).rejects.toThrow("digest mismatch: file-00"); + expect(readSizes).not.toContain(99); + const removals = vi.mocked(rm).mock.calls.slice(removalStart).map(([path]) => String(path)).filter(path => /paperclip-acpx-native-/.test(path)); + expect(removals).toHaveLength(1); + await new Promise(resolve => setImmediate(resolve)); + await expect(stat(removals[0]!)).rejects.toMatchObject({ code: "ENOENT" }); + }); + it("rejects a source mutation while another file is being copied", async () => { + const { declaration, entries } = await manyFileFixture([101, 102]); + const prototype = await filePrototype(join(declaration.distributionRoot, "runtime")); + const originalRead = prototype.read; const entered = gate(); const hold = gate(); + vi.spyOn(prototype, "read").mockImplementation(async function (this: FileHandle, ...args: any[]): Promise { + if (args[0].length === 101) { entered.release(); await hold.promise; } + return originalRead.apply(this, args as never); + }); + const creating = createNativeAcpxDistributionSnapshot(declaration, entries); + const rejected = expect(creating).rejects.toThrow("changed while read"); + try { + await entered.promise; + await writeFile(join(declaration.distributionRoot, "file-00"), Buffer.alloc(101, 7)); + } finally { hold.release(); } + await rejected; + }); it("uses the existing guardian ownership and provider-exit proof for native children", async () => { const declaration = await fixture({ script: '#!/bin/sh\nprintf "ready"\nwhile :; do sleep 1; done\n' }); const fences = await Promise.all([listen(), listen()]); diff --git a/packages/paperclip-runner/src/drivers/acpx/native-distribution-integrity.ts b/packages/paperclip-runner/src/drivers/acpx/native-distribution-integrity.ts index 0c1d366273b..d8afb68cb70 100644 --- a/packages/paperclip-runner/src/drivers/acpx/native-distribution-integrity.ts +++ b/packages/paperclip-runner/src/drivers/acpx/native-distribution-integrity.ts @@ -26,6 +26,10 @@ export interface NativeAcpxDistributionSnapshot { const MAX_NATIVE_TREE_BYTES = 1024 * 1024 * 1024; const MAX_NATIVE_FILE_BYTES = 384 * 1024 * 1024; const MAX_NATIVE_MANIFEST_BYTES = 4 * 1024 * 1024; +// Bound both file descriptors and buffers. A single larger admitted file runs +// alone and remains subject to MAX_NATIVE_FILE_BYTES. +const NATIVE_COPY_CONCURRENCY = 8; +const NATIVE_COPY_BUFFER_BYTES = 32 * 1024 * 1024; const BOOTSTRAP = ".paperclip-native-entry.cjs"; const GUARD = ".paperclip-native-module-guard.cjs"; export const NATIVE_ACPX_BOOTSTRAP_NAME = BOOTSTRAP; @@ -93,7 +97,7 @@ export async function createNativeAcpxDistributionSnapshot(input: NativeAcpxDist if (!same(rootBefore, await heldRoot.stat({ bigint: true }))) throw new Error("Native ACPX distribution root changed before snapshot"); await mkdir(packageRoot, { mode: 0o700 }); if (input.isolatedCacheEnvironmentName) await mkdir(cacheRoot, { mode: 0o700 }); - for (const entry of entries) { + const copyEntry = async (entry: NativeAcpxDistributionEntry): Promise => { const path = join(source, ...entry.path.split("/")); if (await realpath(path) !== path) throw new Error("Native ACPX closure contains a symbolic link"); const before = await lstat(path, { bigint: true }); @@ -115,8 +119,26 @@ export async function createNativeAcpxDistributionSnapshot(input: NativeAcpxDist let directory = dirname(target); while (directory !== privateRoot) { directories.add(directory); directory = dirname(directory); } await writeFile(target, bytes, { mode: entry.executable ? 0o500 : 0o400, flag: "wx" }); - digests[target] = entry.sha256; } finally { await file.close(); } + }; + for (let start = 0; start < entries.length;) { + let end = start; + let bytes = 0; + while (end < entries.length && end - start < NATIVE_COPY_CONCURRENCY) { + const size = entries[end]!.size; + if (end > start && bytes + size > NATIVE_COPY_BUFFER_BYTES) break; + bytes += size; + end++; + } + const batch = entries.slice(start, end); + // Never clean the private root while another worker can still write or + // close a descriptor. Stop scheduling new batches after any rejection. + const copied = await Promise.allSettled(batch.map(copyEntry)); + const failure = copied.find((result) => result.status === "rejected"); + if (failure?.status === "rejected") throw failure.reason; + // Worker completion order must not change the module guard or manifest. + for (const entry of batch) digests[join(packageRoot, ...entry.path.split("/"))] = entry.sha256; + start = end; } if (!same(rootBefore, await heldRoot.stat({ bigint: true })) || !same(rootBefore, await lstat(source, { bigint: true }))) throw new Error("Native ACPX distribution root changed during snapshot"); const executable = join(packageRoot, ...input.executable.split("/"));