From 7d31b974567a8d89c435b77fa22036bc2728ff61 Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Fri, 20 Nov 2026 16:28:16 -0500 Subject: [PATCH] test(ghidra): consolidate load-image snapshot attestation (#12926) --- src/ghidra/GhidraDolLoadImage.test.ts | 88 +++++++++++++++--------- src/ghidra/GhidraLoadImageAttest.test.ts | 52 ++++++-------- 2 files changed, 74 insertions(+), 66 deletions(-) diff --git a/src/ghidra/GhidraDolLoadImage.test.ts b/src/ghidra/GhidraDolLoadImage.test.ts index ec1de9ecba..bacdb878a7 100644 --- a/src/ghidra/GhidraDolLoadImage.test.ts +++ b/src/ghidra/GhidraDolLoadImage.test.ts @@ -1,7 +1,7 @@ import { createHash } from "node:crypto"; import { writeFile } from "node:fs/promises"; import { join } from "node:path"; -import { describe, expect, it } from "vitest"; +import { describe, expect, it, vi } from "vitest"; import { parseBinaryTarget } from "../application/BinaryTargetResolver.js"; import { createTestTempDirectory } from "../../tests/fixtures/temporaryDirectory.js"; @@ -13,7 +13,8 @@ import type { NativeLoadImage, } from "../domain/native/nativeLoadImage.js"; import { attestGhidraDolLoadImage } from "./GhidraDolLoadImage.js"; -import type { Result } from "../domain/result.js"; +import { ok, type Result } from "../domain/result.js"; +import { attestGhidraNativeLoadImage } from "./GhidraLoadImageAttest.js"; type DolTarget = Extract; type DolImage = Extract; @@ -138,41 +139,60 @@ const checkNamed = ( name: string, ) => checks.find((check) => check.name === name); -describe("GameCube DOL load-image attestation", () => { - it("verifies file-backed sections and only the BSS gaps outside data", async () => { - const bytes = overlappingDol(); - const target = await dolTarget(bytes); - expect(target.dol.bssOverlaps).toEqual([0]); +it("verifies DOL section ranges and only the BSS gaps outside data", async () => { + const bytes = overlappingDol(); + const target = await dolTarget(bytes); + expect(target.dol.bssOverlaps).toEqual([0]); - const result = attestGhidraDolLoadImage( - snapshotOf(bytes, target), - target, - observationsFor(bytes, target.dol), - undefined, - ); - expect(result).toMatchObject({ - ok: true, - value: { - status: "verified", - format: "gamecube-dol", - architecture: "powerpc", - entry: { address: "0x80003100" }, - bss: { address: "0x8000fff8", size: "0x20" }, - }, - }); - const image = attestedDol(result); - expect(image.sections).toHaveLength(2); - expect(image.checks.every((check) => check.matched)).toBe(true); - expect(checkNamed(image.checks, "bss_mappings")).toMatchObject({ - matched: true, - expected: EXPECTED_GAPS, - observed: EXPECTED_GAPS, - }); - expect( - checkNamed(image.checks, "initialized_unbacked_mappings"), - ).toMatchObject({ matched: true, observed: 0 }); + const snapshot = snapshotOf(bytes, target); + const hashRanges = vi.fn(async () => + ok({ + sha256: snapshot.sha256, + bytes: snapshot.bytes, + rangeSha256: snapshot.sectionSha256, + }), + ); + const result = await attestGhidraNativeLoadImage({ + target, + operation: "inspect_native_load_image", + measured: observationsFor(bytes, target.dol), + client: { hashTargetSnapshotRanges: hashRanges }, + mapSessionError: (failure) => { + throw failure; + }, }); + expect(hashRanges).toHaveBeenCalledExactlyOnceWith( + [ + { start: 0x100, end: 0x120 }, + { start: 0x120, end: 0x130 }, + ], + undefined, + ); + expect(result).toMatchObject({ + ok: true, + value: { + status: "verified", + format: "gamecube-dol", + architecture: "powerpc", + entry: { address: "0x80003100" }, + bss: { address: "0x8000fff8", size: "0x20" }, + }, + }); + if (!result.ok) throw result.error; + const image = attestedDol(result); + expect(image.sections).toHaveLength(2); + expect(image.checks.every((check) => check.matched)).toBe(true); + expect(checkNamed(image.checks, "bss_mappings")).toMatchObject({ + matched: true, + expected: EXPECTED_GAPS, + observed: EXPECTED_GAPS, + }); + expect( + checkNamed(image.checks, "initialized_unbacked_mappings"), + ).toMatchObject({ matched: true, observed: 0 }); +}); +describe("GameCube DOL load-image attestation", () => { it("flags a file-backed mapping whose sha256 differs from its section bytes", async () => { const bytes = overlappingDol(); const target = await dolTarget(bytes); diff --git a/src/ghidra/GhidraLoadImageAttest.test.ts b/src/ghidra/GhidraLoadImageAttest.test.ts index 00b1650ce2..98ce58f762 100644 --- a/src/ghidra/GhidraLoadImageAttest.test.ts +++ b/src/ghidra/GhidraLoadImageAttest.test.ts @@ -10,10 +10,7 @@ import { expect, it, vi } from "vitest"; import type { BinaryTarget } from "../domain/binaryTargetTypes.js"; import { err, ok } from "../domain/result.js"; import { ProviderAdapterError } from "../domain/providerAdapterError.js"; -import { - nativeLoadImageSchema, - nativeLoadImagePartialObservationSchema, -} from "../domain/native/nativeLoadImage.js"; +import { nativeLoadImagePartialObservationSchema } from "../domain/native/nativeLoadImage.js"; import { fixtureDosLoadImage, compareDosClientFixture, @@ -95,10 +92,7 @@ it.each(["dos-mz", "dos-com", "raw-binary"] as const)( }); if (result.ok) throw new Error("Expected cancellation after snapshot completion"); - expect(result.error).toMatchObject({ - _tag: "AnalysisCancelledError", - cause: reason, - }); + expect(result.error._tag).toBe("AnalysisCancelledError"); expect(result.error.cause).toBe(reason); expect( nativeLoadImagePartialObservationSchema.parse( @@ -148,6 +142,7 @@ it("declines another format with the owned verification reason", async () => { status: "unsupported", reason: ghidraUnverifiedLoadImageReason(), }); + expect(result.value.observations).toEqual(observation); }); it.each([null, "caller stopped", new Error("caller stopped")])( @@ -185,12 +180,9 @@ it.each([null, "caller stopped", new Error("caller stopped")])( it("preserves a snapshot failure before late cancellation", async () => { const fixture = dosFixture("dos-mz"); const caller = new AbortController(); - const source = new GhidraSessionError("protocol", "controlled read failure"); - const projected = new ProviderAdapterError( - "ghidra", - "inspect_native_load_image", - { cause: source }, - ); + const source = new GhidraSessionError("protocol", "controlled read failure", { + snapshot_stage: "read", + }); const result = await attestGhidraNativeLoadImage({ operation: "inspect_native_load_image", target: fixture.target, @@ -201,28 +193,24 @@ it("preserves a snapshot failure before late cancellation", async () => { return err(source); }, }, - mapSessionError: () => projected, + mapSessionError: (failure) => + new ProviderAdapterError("ghidra", "inspect_native_load_image", { + cause: failure, + diagnostics: failure.diagnostics, + }), signal: caller.signal, }); - expect(result).toEqual(err(projected)); -}); - -it("keeps ordinary DOS verification valid", async () => { - const fixture = dosFixture("dos-mz"); - const result = await attestGhidraNativeLoadImage({ - operation: "inspect_native_load_image", - target: fixture.target, - measured: fixture.observation, - client: { - attestDosTargetSnapshot: async (observations) => - compareDosClientFixture(fixture.bytes, fixture.sha256, observations), - }, - mapSessionError: () => { - throw new Error("No source failure expected"); + expect(result).toMatchObject({ + ok: false, + error: { + _tag: "ProviderAdapterError", + cause: { kind: "protocol", message: "controlled read failure" }, + diagnostics: { + snapshot_stage: "read", + load_image_observations: fixture.observation, + }, }, }); - if (!result.ok) throw result.error; - expect(nativeLoadImageSchema.parse(result.value).status).toBe("verified"); }); it("retains completed NativeAOT facts when cancellation precedes result consumption", async () => {