From 075bf61b22469c63d61d595c100e84ce7cd89184 Mon Sep 17 00:00:00 2001 From: 2SinceJuly Date: Thu, 19 Nov 2026 13:12:31 +0800 Subject: [PATCH] test(gpu): protect CUDA 0x2000 LZ4 payload identity (#10339) * test(gpu): protect CUDA 0x2000 LZ4 payload identity CUDA LZ4 entries were decodable through the shared block decoder, but no public workflow selected a 0x2000 entry and checked the retained PTX bytes. The fixture builder offered only none/zstd/legacy, so the profile dispatch in readCudaFatbinary had no decoded-payload control. Add an lz4 fixture profile backed by a single literal sequence, extend the existing boundary matrix to it, and add boundary plus CLI/MCP regressions that assert compression_id, decoded size, retained PTX text and the decoded digest. A malformed decoded-length control covers the rejection path. Temporarily disabling the 0x2000 profile selection makes all three new cases fail. Verified with npm run check:fast and the GPU boundary, acceptance, inventory and LZ4 suites (161 passed). No native liblz4 or CUDA compiler is required. * style(gpu): apply repository formatting * test(gpu): follow the shared CLI session rename in the code-object test Upstream #10412 removed runFileCli from the CLI fixture and moved every acceptance test onto the file-level shared process (cli.run). Rebasing onto current main left this new LZ4 assertion calling the deleted helper, so the whole file failed with a ReferenceError and the shared worker was torn down for the rest of the suite. --- tests/acceptance/gpuCodeObjectsCli.test.ts | 57 ++++++++++++++++++++++ tests/boundary/gpu/codeObjects.test.ts | 45 ++++++++++++++++- tests/fixtures/gpu/codeObjects.ts | 28 +++++++++-- 3 files changed, 126 insertions(+), 4 deletions(-) diff --git a/tests/acceptance/gpuCodeObjectsCli.test.ts b/tests/acceptance/gpuCodeObjectsCli.test.ts index 8960c73e18..7f3410c6a4 100644 --- a/tests/acceptance/gpuCodeObjectsCli.test.ts +++ b/tests/acceptance/gpuCodeObjectsCli.test.ts @@ -72,6 +72,63 @@ cliTest( }, ); +cliTest( + "reports the decoded payload identity of a CUDA 0x2000 LZ4 entry", + async ({ cli }) => { + const root = await createTestTempDirectory("rea-cuda-lz4-"), + path = join(root, "application"), + ptx = Buffer.from(ptxFixture), + bytes = cudaFixture("lz4", [{ kind: 1, target: 80, bytes: ptx }]); + await writeFile(path, bytes); + const response = await cli.run({ + arguments: [ + "inspect-gpu-code-objects", + path, + "--analysis", + "kernels", + "--detail", + "complete", + "--json", + ], + }); + expect(response.exitCode, response.stderr).toBe(0); + const evidence = parseEvidence( + toolContract("inspect_gpu_code_objects").outputSchema.parse( + response.json, + ), + ); + const report = gpuCodeObjectsSchema.parse(evidence.normalized_result); + expect(report.artifact.sha256).toBe( + createHash("sha256").update(bytes).digest("hex"), + ); + expect(report.objects[0]).toMatchObject({ + kind: "ptx", + compression: "lz4", + compression_id: "cuda-flags:0x2011", + decoded_size: ptx.length, + sha256: createHash("sha256").update(ptx).digest("hex"), + inspection: "complete", + }); + expect(report.ptx_artifacts.map((row) => row.text)).toEqual([ptxFixture]); + expect(report.coverage.payloads).toBe("complete"); + const { call } = await connectLocalToolsMcp(); + const reply = await call("inspect_gpu_code_objects", { + path, + analysis: "kernels", + detail: "complete", + }); + expect(reply.isError, JSON.stringify(reply)).not.toBe(true); + expect( + gpuCodeObjectsSchema.parse( + parseEvidence(reply.structuredContent).normalized_result, + ).objects[0], + ).toMatchObject({ + compression: "lz4", + sha256: createHash("sha256").update(ptx).digest("hex"), + }); + }, +); + cliTest( "reports an exhausted ELF symbol string scan as capacity instead of corruption", async ({ cli }) => { diff --git a/tests/boundary/gpu/codeObjects.test.ts b/tests/boundary/gpu/codeObjects.test.ts index cb0a931c32..85321b4dda 100644 --- a/tests/boundary/gpu/codeObjects.test.ts +++ b/tests/boundary/gpu/codeObjects.test.ts @@ -35,7 +35,7 @@ const inspect = async ( onTestFinished(() => provider.close()); return provider.inspect({ path, analysis: "kernels", ...options }); }; -it.each(["none", "zstd"] as const)( +it.each(["none", "zstd", "lz4"] as const)( "reads selected CUDA metadata and PTX with %s compression", async (compression) => { const result = await inspect(cudaFixture(compression)); @@ -69,6 +69,49 @@ it.each(["none", "zstd"] as const)( ]); }, ); +it("decodes a CUDA 0x2000 LZ4 entry to the exact declared payload", async () => { + const ptx = Buffer.from(ptxFixture), + bytes = cudaFixture("lz4", [{ kind: 1, target: 80, bytes: ptx }]), + // The first fatbinary header is 16 bytes, so the first entry starts there. + entry = 16; + // The fixture must really encode: a stored-profile fallback would leave the + // LZ4 selection path unexercised while this control still passed. + expect(bytes.readBigUInt64LE(entry + 40)).toBe(0x2011n); + const result = await inspect(bytes); + if (!result.ok) throw result.error; + expect(result.value.objects[0]).toMatchObject({ + kind: "ptx", + compression: "lz4", + compression_id: "cuda-flags:0x2011", + decoded_size: ptx.length, + inspection: "complete", + sha256: createHash("sha256").update(ptx).digest("hex"), + }); + expect(result.value.coverage.payloads).toBe("complete"); + expect(result.value.ptx_artifacts.map((row) => row.text)).toEqual([ + ptxFixture, + ]); +}); +it("rejects a CUDA LZ4 entry whose declared decoded length disagrees", async () => { + const bytes = cudaFixture("lz4", [ + { kind: 1, target: 80, bytes: Buffer.from(ptxFixture) }, + ]), + entry = 16; + // Grow only the declared output length; the encoded literals cannot reach it. + bytes.writeBigUInt64LE( + BigInt(Buffer.byteLength(ptxFixture) + 16), + entry + 56, + ); + const result = await inspect(bytes); + if (!result.ok) throw result.error; + expect(result.value.objects[0]).toMatchObject({ + compression: "lz4", + inspection: "malformed", + sha256: null, + }); + expect(result.value.coverage.payloads).toBe("partial"); + expect(result.value.ptx_artifacts).toEqual([]); +}); it("retains the unsupported legacy CUDA compression declaration and extents", async () => { const result = await inspect( cudaFixture("legacy", [ diff --git a/tests/fixtures/gpu/codeObjects.ts b/tests/fixtures/gpu/codeObjects.ts index e77207f6b0..f34b9825fc 100644 --- a/tests/fixtures/gpu/codeObjects.ts +++ b/tests/fixtures/gpu/codeObjects.ts @@ -264,8 +264,24 @@ export const amdFixture = ( }, ]); }; +/** + * One final LZ4 literal sequence, the block form CUDA's 0x2000 profile stores. + * A single sequence is valid LZ4: separate sequences need an intervening match. + */ +const lz4LiteralBlock = (data: Buffer): Buffer => { + const head = [Math.min(data.length, 15) << 4]; + if (data.length >= 15) { + let extra = data.length - 15; + while (extra >= 255) { + head.push(255); + extra -= 255; + } + head.push(extra); + } + return Buffer.concat([Buffer.from(head), data]); +}; export const cudaFixture = ( - compression: "none" | "zstd" | "legacy" = "none", + compression: "none" | "zstd" | "lz4" | "legacy" = "none", entries: { kind: number; target: number; bytes: Buffer; flags?: number }[] = [ { kind: 1, target: 80, bytes: Buffer.from(ptxFixture) }, { kind: 2, target: 90, bytes: cubinFixture({ architecture: 90 }) }, @@ -278,13 +294,19 @@ export const cudaFixture = ( ? zstdCompressSync(bytes) : compression === "legacy" ? deflateSync(bytes) - : bytes, + : compression === "lz4" + ? lz4LiteralBlock(bytes) + : bytes, + // CUDA tags each supported profile with its own flag bit; 0x2000 alone is + // the LZ4 profile and 0x1000 alone stays an unsupported declaration. compressionFlag = compression === "zstd" ? 0x8000n : compression === "legacy" ? 0x1000n - : 0n; + : compression === "lz4" + ? 0x2000n + : 0n; const result = Buffer.alloc(64 + Math.ceil(data.length / 8) * 8); result.writeUInt16LE(kind, 0); result.writeUInt16LE(0x101, 2);