diff --git a/src/application/managed/ManagedMemberComparisonService.ts b/src/application/managed/ManagedMemberComparisonService.ts index dfe9a0da44..910034c350 100644 --- a/src/application/managed/ManagedMemberComparisonService.ts +++ b/src/application/managed/ManagedMemberComparisonService.ts @@ -123,11 +123,10 @@ export const compareManagedMemberPaths = async ( const selectedPath = input[inputField]; let snapshot: Awaited>; try { - snapshot = await readSnapshot( - path, - operation, - resources === undefined ? {} : { resources }, - ); + snapshot = await readSnapshot(path, operation, { + expectedSha256: target.sha256, + ...(resources === undefined ? {} : { resources }), + }); } catch (cause: unknown) { const failure = classifyBinaryTargetFilesystemFailure(path, cause); if (failure !== undefined) throw failure; diff --git a/src/artifacts/NodeSeaArtifactReader.test.ts b/src/artifacts/NodeSeaArtifactReader.test.ts index 57c38f7d9b..21984a69f2 100644 --- a/src/artifacts/NodeSeaArtifactReader.test.ts +++ b/src/artifacts/NodeSeaArtifactReader.test.ts @@ -1,4 +1,5 @@ import { createHash } from "node:crypto"; +import { once } from "node:events"; import { writeFileSync } from "node:fs"; import { buffer } from "node:stream/consumers"; import { createReader } from "./inventory/reader.js"; @@ -618,24 +619,37 @@ it("rejects forged entries and changed carrier identity, then closes idempotentl } }); -it("settles an active member stream when closed", async () => { - const path = await writeCarrier( - "close", - nodeSeaElf({ main: Buffer.alloc(1024 * 1024, 65) }), - ); - const reader = new NodeSeaArtifactReader(path); - try { - const entries = []; - for await (const entry of reader.entries()) entries.push(entry); - const entry = entries[0]; - if (entry === undefined) throw new Error("Missing main script"); - const stream = await reader.open(entry); - await reader.close(); - expect(stream.destroyed).toBe(true); - } finally { - await reader.close(); - } -}); +it.each([8, 1024 * 1024])( + "retires an unread %i-byte member through the shared reader factory", + async (size) => { + const path = await writeCarrier( + "close", + nodeSeaElf({ main: Buffer.alloc(size, 65) }), + ); + const reader = createReader(path, "node-sea", {}); + if (reader === undefined) throw new Error("Missing Node SEA reader"); + try { + const entries = []; + for await (const entry of reader.entries()) entries.push(entry); + const entry = entries[0]; + if (entry === undefined) throw new Error("Missing main script"); + const stream = await reader.open(entry); + const produced = size === 8 ? once(stream, "finish") : undefined; + await once(stream, "readable"); + if (produced !== undefined) await produced; + expect(stream.destroyed).toBe(false); + await reader.close(); + expect(stream.destroyed).toBe(true); + expect(stream.closed).toBe(true); + await expect(buffer(stream)).rejects.toMatchObject({ + reason: "unavailable", + }); + await reader.close(); + } finally { + await reader.close(); + } + }, +); it("honors cancellation before admitting a carrier", async () => { const path = await writeCarrier("cancel", nodeSeaElf()); diff --git a/src/artifacts/NodeSeaArtifactReader.ts b/src/artifacts/NodeSeaArtifactReader.ts index b170a08911..d4b20ed235 100644 --- a/src/artifacts/NodeSeaArtifactReader.ts +++ b/src/artifacts/NodeSeaArtifactReader.ts @@ -138,12 +138,14 @@ export class NodeSeaArtifactReader implements ArtifactReader { const output = this.#memberStream(stored, signal); output.on("error", () => undefined); this.#streams.add(output); + // A finished producer can leave source bytes buffered for its consumer. + // Keep the readable side owned until it closes as well. + output.once("close", () => this.#streams.delete(output)); void this.#scope .run( () => new Promise((resolve) => { pipeline(source, output, () => { - this.#streams.delete(output); resolve(); }); }), diff --git a/src/dotnet/ManagedSnapshot.ts b/src/dotnet/ManagedSnapshot.ts index 0b33ebce6f..5f8247d919 100644 --- a/src/dotnet/ManagedSnapshot.ts +++ b/src/dotnet/ManagedSnapshot.ts @@ -40,6 +40,10 @@ interface ManagedSnapshot extends ManagedSourceObservation { readonly bytes: Buffer; } +interface ManagedSnapshotReadOptions extends RegularFileReadOptions { + readonly expectedSha256?: string; +} + /** Admit one observed source digest before any parser or cache consumes its bytes. */ export const verifyManagedSourceIdentity = ( target: { readonly path: string; readonly sha256: string }, @@ -77,7 +81,7 @@ const managedSnapshotCapacityError = ( export const readManagedSnapshot = async ( path: string, operation: string, - options: RegularFileReadOptions = {}, + options: ManagedSnapshotReadOptions = {}, ): Promise => { return readManagedFile( path, @@ -106,7 +110,7 @@ export const readManagedSnapshot = async ( export const hashManagedSource = async ( path: string, operation: string, - options: RegularFileReadOptions = {}, + options: ManagedSnapshotReadOptions = {}, ): Promise => readManagedFile( path, @@ -142,24 +146,42 @@ const readManagedFile = ( path: string, operation: string, read: (handle: FileHandle, metadata: Stats) => Promise, - options: RegularFileReadOptions, + options: ManagedSnapshotReadOptions, ): Promise => withRegularFileCompletion(path, read, options) .then(({ value, cancellation }) => { - if (cancellation !== undefined) - throw new AnalysisCancelledError(operation, { - cause: cancellation.reason, - partialObservation: { - kind: "file-snapshot-hash", - path, - bytes: value.byteLength, - sha256: value.sha256, - }, + if (cancellation !== undefined) { + const identity = + options.expectedSha256 === undefined + ? undefined + : verifyManagedSourceIdentity( + { path, sha256: options.expectedSha256 }, + value, + operation, + ); + const failure = + identity !== undefined && !identity.ok + ? identity.error + : new AnalysisCancelledError(operation, { + cause: cancellation.reason, + }); + throw failure.retainPartialObservation({ + kind: "file-snapshot-hash", + path, + bytes: value.byteLength, + sha256: value.sha256, }); + } return value; }) .catch((cause: unknown) => { - throw normalizeManagedReadFailure(cause, path, operation, options.signal); + throw normalizeManagedReadFailure( + cause, + path, + operation, + options.signal, + options.expectedSha256, + ); }); /** Keep cleanup ownership and the original typed read failure at this boundary. */ @@ -168,6 +190,7 @@ const normalizeManagedReadFailure = ( fallbackPath: string, operation: string, signal?: AbortSignal, + expectedSha256?: string, ): unknown => { // An exact caller reason is opaque, even if it resembles a cleanup record. if (signal?.aborted === true && cause === signal.reason) @@ -177,6 +200,7 @@ const normalizeManagedReadFailure = ( fallbackPath, operation, signal, + expectedSha256, ); if (cleanupError !== undefined) return cleanupError; return normalizeReadFailure(cause, fallbackPath, operation, signal); @@ -188,6 +212,7 @@ export const managedCleanupError = ( fallbackPath: string, operation: string, signal?: AbortSignal, + expectedSha256?: string, ): AnalysisError | undefined => { if (cause instanceof RegularFileCleanupFailure) { const cancellation = regularFileCancellation(cause); @@ -198,6 +223,14 @@ export const managedCleanupError = ( { cause, operation }, ); bindRegularFileCleanup(cause, cleanup); + const identity = + cause.outcome.kind === "completed" && expectedSha256 !== undefined + ? verifyManagedSourceIdentity( + { path: cause.path, sha256: expectedSha256 }, + cause.outcome.value as ManagedSourceObservation, + operation, + ) + : undefined; const failure = cause.outcome.kind === "failed" ? normalizeReadFailure( @@ -206,11 +239,13 @@ export const managedCleanupError = ( operation, signal, ) - : cancellation === undefined - ? undefined - : new AnalysisCancelledError(operation, { - cause: cancellation.reason, - }); + : identity !== undefined && !identity.ok + ? identity.error + : cancellation === undefined + ? undefined + : new AnalysisCancelledError(operation, { + cause: cancellation.reason, + }); // This boundary produces only these snapshot facts. The descriptor owner // confirms completion independently of a failed final close. if (cause.outcome.kind === "completed") { diff --git a/src/dotnet/ManagedStaticProvider.ts b/src/dotnet/ManagedStaticProvider.ts index 3ffa5d1e6e..d36294f577 100644 --- a/src/dotnet/ManagedStaticProvider.ts +++ b/src/dotnet/ManagedStaticProvider.ts @@ -106,10 +106,12 @@ class ManagedStaticClient implements AnalysisClient { const observed = snapshot === undefined ? await readManagedSnapshot(this.target.path, operation, { + expectedSha256: this.target.sha256, signal: options?.signal, resources: this.#resources, }) : await hashManagedSource(this.target.path, operation, { + expectedSha256: this.target.sha256, signal: options?.signal, resources: this.#resources, }); diff --git a/tests/boundary/providers/managed/managedMemberComparison.test.ts b/tests/boundary/providers/managed/managedMemberComparison.test.ts index b579b83fe6..c8d613d874 100644 --- a/tests/boundary/providers/managed/managedMemberComparison.test.ts +++ b/tests/boundary/providers/managed/managedMemberComparison.test.ts @@ -31,8 +31,63 @@ import { readManagedSnapshot, } from "../../../../src/dotnet/ManagedSnapshot.js"; import * as boundedFileBytes from "../../../../src/process/BoundedFileBytes.js"; +import { primaryAnalysisError } from "../../../../src/domain/analysisErrorBase.js"; import { buildManagedMemberCapacityFixture } from "../../../fixtures/managedMemberCapacity.js"; +it("preserves a selected comparison source digest contradiction through failed close and retry", async () => { + const root = await createTestTempDirectory( + "rea-managed-comparison-identity-close-", + ); + const leftPath = join(root, "left.dll"); + const rightPath = join(root, "right.dll"); + const bytes = buildManagedPeFixture(); + await Promise.all([writeFile(leftPath, bytes), writeFile(rightPath, bytes)]); + const resources = new ArtifactResourceScope(); + let blocked = false; + const nativeClose = OwnedFileHandle.prototype.close; + const close = vi + .spyOn(OwnedFileHandle.prototype, "close") + .mockImplementation(async function (this: OwnedFileHandle) { + if (blocked) throw new Error("comparison descriptor close denied"); + await nativeClose.call(this); + }); + try { + const result = await compareManagedMemberPaths( + { leftPath, rightPath }, + { + readSnapshot: async (path, operation, options) => { + const changed = Buffer.from(bytes); + changed[changed.length - 1] = + changed.readUInt8(changed.length - 1) ^ 1; + await writeFile(path, changed); + blocked = true; + return readManagedSnapshot(path, operation, options); + }, + }, + resources, + ); + if (result.ok) + throw new Error("Expected changed selected comparison source"); + expect(primaryAnalysisError(result.error)._tag).toBe( + "AnalysisArtifactChangedError", + ); + expect(result.error.partialObservation).toMatchObject({ + kind: "file-snapshot-hash", + path: leftPath, + bytes: bytes.length, + }); + expect(result.error.cleanupIncomplete).toBe(true); + await expect(resources.close()).rejects.toBeDefined(); + blocked = false; + await resources.close(); + expect(result.error.cleanupIncomplete).toBe(false); + } finally { + blocked = false; + await resources.close(); + close.mockRestore(); + } +}); + it("keeps missing members unknown when the opposite inspection retains only types", async () => { const directory = await createTestTempDirectory( "rea-managed-type-recovery-comparison-", diff --git a/tests/boundary/providers/managed/managedStaticProvider.test.ts b/tests/boundary/providers/managed/managedStaticProvider.test.ts index 84794d0959..6e0dd5b9b0 100644 --- a/tests/boundary/providers/managed/managedStaticProvider.test.ts +++ b/tests/boundary/providers/managed/managedStaticProvider.test.ts @@ -24,6 +24,144 @@ import { ArtifactResourceScope } from "../../../../src/artifacts/ArtifactResourc import { RegularFileCleanupFailure } from "../../../../src/filesystem/OwnedRegularFile.js"; import { projectAnalysisError } from "../../../../src/domain/analysisErrorProjection.js"; import * as boundedFileBytes from "../../../../src/process/BoundedFileBytes.js"; +import { primaryAnalysisError } from "../../../../src/domain/analysisErrorBase.js"; + +it.each( + [false, true].flatMap((warm) => + [false, true].flatMap((changed) => + [false, true].map((failedClose) => ({ warm, changed, failedClose })), + ), + ), +)( + "preserves selected managed identity through finalization (cached=$warm, changed=$changed, failedClose=$failedClose)", + async ({ warm, changed, failedClose }) => { + const root = await createTestTempDirectory( + "rea-managed-selected-identity-", + ); + const path = join(root, "fixture.dll"); + const bytes = buildManagedPeFixture(); + await writeFile(path, bytes); + const target = await parseBinaryTarget(path); + if (!target.ok) throw target.error; + const client = new ManagedStaticProvider().createClient(target.value); + if (warm) + expect((await client.execute("inspect_managed_artifact", {})).ok).toBe( + true, + ); + const observed = Buffer.from(bytes); + if (changed) { + observed[observed.length - 1] = + observed.readUInt8(observed.length - 1) ^ 1; + await writeFile(path, observed); + } + const controller = new AbortController(); + const reason = { source: "selected managed finalization" }; + let blocked = failedClose; + const owners = new Set(); + const nativeClose = OwnedFileHandle.prototype.close; + const close = vi + .spyOn(OwnedFileHandle.prototype, "close") + .mockImplementation(async function (this: OwnedFileHandle) { + owners.add(this); + controller.abort(reason); + if (blocked) throw new Error("managed source close denied"); + await nativeClose.call(this); + }); + try { + const result = await client.execute( + "inspect_managed_artifact", + {}, + { signal: controller.signal }, + ); + if (result.ok) throw new Error("Expected source finalization failure"); + const primary = primaryAnalysisError(result.error); + expect(primary._tag).toBe( + changed ? "AnalysisArtifactChangedError" : "AnalysisCancelledError", + ); + if (changed) + expect(primary.diagnostics).toMatchObject({ + expected_sha256: target.value.sha256, + observed_sha256: createHash("sha256").update(observed).digest("hex"), + }); + else expect(primary.cause).toBe(reason); + const receipt = { + kind: "file-snapshot-hash", + path, + sha256: createHash("sha256").update(observed).digest("hex"), + bytes: observed.length, + }; + expect(result.error.partialObservation).toEqual(receipt); + expect(result.error.cleanupIncomplete).toBe(failedClose); + if (failedClose) expect((await client.close()).ok).toBe(false); + blocked = false; + expect((await client.close()).ok).toBe(true); + expect(result.error.cleanupIncomplete).toBe(false); + expect([...owners].every((owner) => owner.handle.fd === -1)).toBe(true); + expect(result.error.partialObservation).toEqual(receipt); + } finally { + blocked = false; + await client.close(); + close.mockRestore(); + } + }, +); + +it.each([false, true])( + "preserves physical managed read failure before close cancellation (failedClose=%s)", + async (failedClose) => { + const root = await createTestTempDirectory( + "rea-managed-physical-finalization-", + ); + const path = join(root, "fixture.dll"); + await writeFile(path, buildManagedPeFixture()); + const target = await parseBinaryTarget(path); + if (!target.ok) throw target.error; + const client = new ManagedStaticProvider().createClient(target.value); + const controller = new AbortController(); + const reason = { source: "after failed managed read" }; + const physical = Object.assign(new Error("physical managed read denied"), { + code: "EIO", + }); + const originalRead = boundedFileBytes.readBoundedFileBytes; + const read = vi + .spyOn(boundedFileBytes, "readBoundedFileBytes") + .mockImplementation((handle, ...args) => { + vi.spyOn(handle, "read").mockRejectedValueOnce(physical); + return originalRead(handle, ...args); + }); + const nativeClose = OwnedFileHandle.prototype.close; + let blocked = failedClose; + const close = vi + .spyOn(OwnedFileHandle.prototype, "close") + .mockImplementation(async function (this: OwnedFileHandle) { + controller.abort(reason); + if (blocked) throw new Error("managed descriptor close denied"); + await nativeClose.call(this); + }); + try { + const result = await client.execute( + "inspect_managed_artifact", + {}, + { signal: controller.signal }, + ); + if (result.ok) throw new Error("Expected physical read failure"); + expect(primaryAnalysisError(result.error)).toMatchObject({ + _tag: "BinaryTargetError", + cause: physical, + }); + expect(result.error.partialObservation).toBeUndefined(); + expect(result.error.cleanupIncomplete).toBe(failedClose); + blocked = false; + expect((await client.close()).ok).toBe(true); + expect(result.error.cleanupIncomplete).toBe(false); + } finally { + blocked = false; + await client.close(); + close.mockRestore(); + read.mockRestore(); + } + }, +); it("classifies a source changed during managed snapshot acquisition as artifact_changed", async () => { const directory = await createTestTempDirectory("rea-managed-source-change-");