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.
This commit is contained in:
2SinceJuly authored and GitHub committed 2026-11-19 00:12:31 -05:00
1 parent ce0e3b3b9e
commit 075bf61b22
3 files changed
+126 -4

No files matched your search

@@ -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 }) => {
+44 -1
View File
@@ -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", [
+25 -3
View File
@@ -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);