diff --git a/docs/gpu-code-objects.md b/docs/gpu-code-objects.md index fb02ac7bf7..f57edcc61b 100644 --- a/docs/gpu-code-objects.md +++ b/docs/gpu-code-objects.md @@ -44,14 +44,23 @@ such as `gfx11-generic` and `gfx9-4-generic`. Notes with the empty or explicit `unknown` environment use the same processor and feature comparison while retaining their raw triple spelling. CUDA's legacy fatbinary compression flag is retained as an unsupported codec with its encoded and decoded extents; -compressed bytes are not treated as PTX text. CCOB is recognized by the exact -four signature bytes `CCOB`; high-bit-corrupted bytes are not decoded as ASCII +compressed bytes are not treated as PTX text. A CUDA entry whose declared +decoded length exceeds the exact integer range of `decoded_size` retains +its encoded range and reports the exact length in a limitation. Kernel decoding +reports `capacity` for a selected entry with a supported codec. +CCOB is recognized by the exact four signature bytes `CCOB`; +high-bit-corrupted bytes are not decoded as ASCII and accepted as a bundle. CCOB v1 has no stored total size: the completed zlib or Zstd stream determines its encoded extent. Embedded `CCOB` text is ordinary compressed data; zero-byte alignment after the stream belongs to neither payload. If decoding fails or its codec is unsupported, a later `CCOB` marker is only an uncertain recovery candidate. The affected row retains that limitation and discovery remains partial. +CCOB v2 and v3 declare the full encoded extent in their headers. If a decode +exceeds the output budget, the row reports `capacity` and inspection can +continue at the next bundle. A v3 decoded length outside the exact integer +range of `decoded_size` leaves that field null and is recorded in the row's +limitation. The declared decoded length bounds decompression. An understated length can stop decoding before codec completion, so any subsequent recovery extent stays diff --git a/src/gpu/ClangOffloadBundle.ts b/src/gpu/ClangOffloadBundle.ts index 6c617e0346..d0b91b5cb6 100644 --- a/src/gpu/ClangOffloadBundle.ts +++ b/src/gpu/ClangOffloadBundle.ts @@ -139,23 +139,23 @@ export const readCcobHeader = (bytes: Buffer, remaining: number) => { const reader = new BinaryReader(bytes, true, "ccob"); const version = reader.u16(4), method = reader.u16(6); - let width: number, total: number, decoded: number, hash: bigint; + let width: number, total: number, decoded: bigint, hash: bigint; if (version === 1) { width = 20; total = remaining; - decoded = reader.u32(8); + decoded = reader.word(8, 32); hash = reader.word(12, 64); } else if (version === 2) { width = 24; if (bytes.length < width) rejectGpu("CCOB v2 header is truncated"); total = reader.u32(8); - decoded = reader.u32(12); + decoded = reader.word(12, 32); hash = reader.word(16, 64); } else if (version === 3) { width = 32; if (bytes.length < width) rejectGpu("CCOB v3 header is truncated"); total = gpuCount(reader.word(8, 64)); - decoded = gpuCount(reader.word(16, 64)); + decoded = reader.word(16, 64); hash = reader.word(24, 64); } else throw new BinaryFormatFailure( diff --git a/src/gpu/CudaFatbinary.ts b/src/gpu/CudaFatbinary.ts index 20a26c329e..2d574ef4cc 100644 --- a/src/gpu/CudaFatbinary.ts +++ b/src/gpu/CudaFatbinary.ts @@ -7,12 +7,12 @@ import { rejectGpu, type GpuSource, } from "./GpuSource.js"; -import { gpuObject } from "./GpuObject.js"; +import { gpuObject, recordDeclaredGpuDecodedSize } from "./GpuObject.js"; const CUDA_FATBINARY = "cuda-fatbinary"; export async function* readCudaFatbinary( source: GpuSource, signal?: AbortSignal, -): AsyncGenerator { +): AsyncGenerator<{ row: GpuObject; declaredDecodedSize: number | bigint }> { let base = 0; while (base < source.size) { const header = new BinaryReader( @@ -74,8 +74,8 @@ export async function* readCudaFatbinary( ? "unsupported" : "none"; row.compression_id = compressed ? `cuda-flags:${row.raw_flags}` : null; - row.decoded_size = compressed ? gpuCount(h.word(56, 64)) : padded; - if (compressed && (encoded === 0 || row.decoded_size === 0)) + const declaredDecodedSize = compressed ? h.word(56, 64) : padded; + if (compressed && (encoded === 0 || declaredDecodedSize === 0n)) rejectGpu("Compressed CUDA entry has an empty declared extent"); if (version !== 0x101) { row.compression = "unsupported"; @@ -84,12 +84,14 @@ export async function* readCudaFatbinary( row.limitations.push( `CUDA entry version 0x${version.toString(16)} is not decoded`, ); + } else { + recordDeclaredGpuDecodedSize(row, declaredDecodedSize); } if (row.compression === "unsupported") row.limitations.push( "This CUDA compression/version is not supported; original encoded bytes and target declarations remain located", ); - yield row; + yield { row, declaredDecodedSize }; position += width + padded; } base = await nextGpuContainerOffset(source, end); diff --git a/src/gpu/GpuInventory.ts b/src/gpu/GpuInventory.ts index 47a950f232..544620a06c 100644 --- a/src/gpu/GpuInventory.ts +++ b/src/gpu/GpuInventory.ts @@ -13,7 +13,12 @@ import { readClangBundleDirectory, readCcobHeader, } from "./ClangOffloadBundle.js"; -import { gpuObject, gpuLocation, type GpuPayloadTarget } from "./GpuObject.js"; +import { + gpuObject, + gpuLocation, + recordDeclaredGpuDecodedSize, + type GpuPayloadTarget, +} from "./GpuObject.js"; import { readCudaTarget } from "./CudaTarget.js"; import { readCudaFatbinary } from "./CudaFatbinary.js"; import { decodeGpuPayload, decodeGpuPayloadStream } from "./GpuPayloadCodec.js"; @@ -117,18 +122,18 @@ export class GpuInventory { row.limitations.push(...messages); } /** Reserve input/output work atomically, before any payload allocation or read. */ - #budgetWork(...sizes: number[]): void { - const total = sizes.reduce((sum, size) => sum + size, 0); - if ( - sizes.some((size) => size > GPU_OBJECT_BYTES) || - total > 512 * 1024 * 1024 - this.#workBytes - ) - throw new BinaryResourceFailure( - "structure", - GPU_OBJECT_BYTES, - "GPU reads/decoding exceed 128 MiB per buffer or 512 MiB per inspection", - ); - this.#workBytes += total; + #budgetWork(...sizes: (number | bigint)[]): void { + let workBytes = this.#workBytes; + for (const size of sizes) { + if (size > Math.min(GPU_OBJECT_BYTES, 512 * 1024 * 1024 - workBytes)) + throw new BinaryResourceFailure( + "structure", + GPU_OBJECT_BYTES, + "GPU reads/decoding exceed 128 MiB per buffer or 512 MiB per inspection", + ); + workBytes += Number(size); + } + this.#workBytes = workBytes; } #selected(row: GpuObject): boolean { return ( @@ -303,7 +308,13 @@ export class GpuInventory { async #payload( source: GpuSource, row: GpuObject, - preparedAmd?: AmdElfMetadata, + { + amdMetadata, + declaredDecodedSize, + }: { + amdMetadata?: AmdElfMetadata; + declaredDecodedSize?: number | bigint; + } = {}, ): Promise { if (this.input.analysis !== "kernels") return; if (!this.#selected(row)) { @@ -315,10 +326,11 @@ export class GpuInventory { return; } try { + const decodedSize = declaredDecodedSize ?? row.decoded_size; let coverage: "complete" | "partial" = "complete"; if ( row.kind === "ptx" && - (row.decoded_size === null || row.decoded_size > GPU_REPORT_BYTES) + (decodedSize === null || decodedSize > GPU_REPORT_BYTES) ) throw new BinaryResourceFailure( "structure", @@ -330,11 +342,11 @@ export class GpuInventory { compression === "zstd" || compression === "zlib" || compression === "lz4"; - if (compressed && row.decoded_size === null) + if (compressed && decodedSize === null) rejectGpu("Compressed GPU object has no decoded length"); this.#budgetWork( row.location.size, - ...(compressed ? [row.decoded_size!] : []), + ...(compressed ? [decodedSize!] : []), ); let bytes = await source.read(row.location.offset, row.location.size); if (compressed) { @@ -342,7 +354,7 @@ export class GpuInventory { bytes = await decodeGpuPayload( bytes, compression, - row.decoded_size!, + Number(decodedSize), this.signal, ); } catch (cause) { @@ -400,7 +412,7 @@ export class GpuInventory { } else if (row.kind === "cubin" || row.kind === "amdgpu") { if (row.kind === "amdgpu" && isLlvmBitcode(bytes)) throw new BinaryFormatFailure("unsupported", LLVM_BITCODE_LIMITATION); - coverage = await this.#elfFacts(decoded, row, row.kind, preparedAmd); + coverage = await this.#elfFacts(decoded, row, row.kind, amdMetadata); } else { row.inspection = "unsupported"; this.#objectLimitations(row, [ @@ -523,7 +535,7 @@ export class GpuInventory { } const payloadSelected = this.input.analysis === "kernels" && this.#selected(row); - await this.#payload(source, row, prepared); + await this.#payload(source, row, { amdMetadata: prepared }); if (!payloadSelected && prepared.limitations.length > 0) this.#objectLimitations(row, prepared.limitations); } catch (cause) { @@ -555,9 +567,12 @@ export class GpuInventory { return conflict !== undefined ? "partial" : parsed.coverage; } async #cuda(source: GpuSource): Promise { - for await (const row of readCudaFatbinary(source, this.signal)) { + for await (const { row, declaredDecodedSize } of readCudaFatbinary( + source, + this.signal, + )) { this.#add(row); - await this.#payload(source, row); + await this.#payload(source, row, { declaredDecodedSize }); } } async #hip(source: GpuSource, depth: number): Promise { @@ -589,7 +604,7 @@ export class GpuInventory { if (!prefix.subarray(0, CCOB_MAGIC.length).equals(CCOB_MAGIC)) rejectGpu("Unexpected bytes between Clang offload bundles"); const header = readCcobHeader(prefix, source.size - base), - { width, decoded, hash, version, method } = header; + { width, decoded: declaredDecodedSize, hash, version, method } = header; let total = header.total; let extentKnown = version !== 1; const row = gpuObject(source, { @@ -601,7 +616,7 @@ export class GpuInventory { row.compression = method === 0 ? "zlib" : method === 1 ? "zstd" : "unsupported"; row.compression_id = `ccob:${version}:${method}`; - row.decoded_size = decoded; + recordDeclaredGpuDecodedSize(row, declaredDecodedSize); if (extentKnown) this.#add(row); if (row.compression === "unsupported") { row.inspection = "unsupported"; @@ -623,16 +638,17 @@ export class GpuInventory { start >= legacyWindow.offset && start < legacyWindow.offset + legacyWindow.bytes.length; const readWindow = async (): Promise => { - this.#budgetWork(inputSize, decoded); + this.#budgetWork(inputSize, declaredDecodedSize); legacyWindow = undefined; const bytes = await source.read(start, inputSize); legacyWindow = { offset: start, bytes }; return bytes; }; - if (cached) this.#budgetWork(decoded); + if (cached) this.#budgetWork(declaredDecodedSize); let encoded: Buffer | undefined = cached ? legacyWindow!.bytes.subarray(start - legacyWindow!.offset) : await readWindow(); + const decoded = Number(declaredDecodedSize); const available = encoded.length; const compression = row.compression; const completed = await decodeGpuPayloadStream( @@ -669,11 +685,11 @@ export class GpuInventory { "GPU decompressed length disagrees with its declaration", ); } else { - this.#budgetWork(inputSize, decoded); + this.#budgetWork(inputSize, declaredDecodedSize); data = await decodeGpuPayload( await source.read(base + width, inputSize), row.compression, - decoded, + Number(declaredDecodedSize), this.signal, ); } diff --git a/src/gpu/GpuObject.ts b/src/gpu/GpuObject.ts index 75935fbd74..5b37a37ef7 100644 --- a/src/gpu/GpuObject.ts +++ b/src/gpu/GpuObject.ts @@ -1,6 +1,21 @@ import type { GpuLocation, GpuObject } from "../domain/gpu/gpuCodeObjects.js"; import type { GpuSource } from "./GpuSource.js"; import type { AmdGpuTargetIdentity } from "./AmdGpuTarget.js"; +/** Retain an exact decoded-size declaration in the public row when representable. */ +export const recordDeclaredGpuDecodedSize = ( + row: GpuObject, + size: number | bigint, +): void => { + if (size > Number.MAX_SAFE_INTEGER) { + row.decoded_size = null; + row.limitations.push( + `Declared decoded size is ${String(size)} bytes, outside the exact integer range of decoded_size`, + ); + } else { + row.decoded_size = Number(size); + } +}; + export type GpuPayloadTarget = { readonly value: string | null; readonly amdMetadata?: GpuObject["amd_target_metadata"]; diff --git a/tests/boundary/gpu/codeObjects.test.ts b/tests/boundary/gpu/codeObjects.test.ts index c939c37c24..e5fb572d6d 100644 --- a/tests/boundary/gpu/codeObjects.test.ts +++ b/tests/boundary/gpu/codeObjects.test.ts @@ -112,6 +112,53 @@ it("rejects a CUDA LZ4 entry whose declared decoded length disagrees", async () expect(result.value.coverage.payloads).toBe("partial"); expect(result.value.ptx_artifacts).toEqual([]); }); +it("preserves CUDA inventory and selection across an oversized declared decoded length", async () => { + const bytes = cudaFixture("zstd", [ + { kind: 2, target: 90, bytes: cubinFixture({ architecture: 90 }) }, + { kind: 1, target: 80, bytes: Buffer.from(ptxFixture) }, + ]); + const firstEntry = 16; + const declared = 1n << 53n; + const encoded = bytes.readUInt32LE(firstEntry + 16); + bytes.writeBigUInt64LE(declared, firstEntry + 56); + const inventory = await inspect(bytes, { analysis: "inventory" }); + if (!inventory.ok) throw inventory.error; + expect(inventory.value.artifact).toMatchObject({ + path: expect.stringContaining("no-format-extension"), + file_size: bytes.length, + sha256: createHash("sha256").update(bytes).digest("hex"), + }); + expect(inventory.value.objects).toMatchObject([ + { + target: "sm_90", + inspection: "inventory", + encoded_size: encoded, + decoded_size: null, + location: { offset: firstEntry + 64, size: encoded }, + limitations: [expect.stringContaining(String(declared))], + }, + { target: "sm_80", inspection: "inventory" }, + ]); + for (const [targets, inspection] of [ + [undefined, "capacity"], + [["sm_80"], "not-selected"], + ] as const) { + const result = await inspect( + bytes, + targets ? { targets: [...targets] } : {}, + ); + if (!result.ok) throw result.error; + expect(result.value.objects).toMatchObject([ + { id: inventory.value.objects[0]!.id, target: "sm_90", inspection }, + { + id: inventory.value.objects[1]!.id, + target: "sm_80", + inspection: "complete", + }, + ]); + expect(result.value.kernels.map(({ name }) => name)).toEqual(["add_one"]); + } +}); it("retains the unsupported legacy CUDA compression declaration and extents", async () => { const result = await inspect( cudaFixture("legacy", [ @@ -1301,6 +1348,43 @@ it("continues at the declared CCOB boundary after an unsupported codec", async ( expect(result.value.coverage.discovery).toBe("partial"); }); +it.each([ + { label: "exact JSON integer", decoded: 1n << 28n, projected: 1 << 28 }, + { + label: "outside exact JSON integer range", + decoded: 1n << 53n, + projected: null, + }, +])( + "keeps later CCOB v3 bundles after a decoded size $label exceeds capacity", + async ({ decoded, projected }) => { + const first = ccobFixture(3, "zlib"); + first.writeBigUInt64LE(decoded, 16); + const result = await inspect( + Buffer.concat([first, ccobFixture(3, "zlib")]), + ); + if (!result.ok) throw result.error; + const wrappers = result.value.objects.filter( + ({ kind }) => kind === "hip-bundle", + ); + expect(wrappers).toMatchObject([ + { + inspection: "capacity", + encoded_size: first.length - 32, + decoded_size: projected, + location: { offset: 32, size: first.length - 32 }, + }, + { inspection: "complete", metadata_location: { offset: first.length } }, + ]); + if (projected === null) + expect(wrappers[0]?.limitations).toContain( + `Declared decoded size is ${String(decoded)} bytes, outside the exact integer range of decoded_size`, + ); + expect(result.value.coverage.discovery).toBe("partial"); + expect(result.value.kernels.map(({ name }) => name)).toEqual(["add_one"]); + }, +); + it("refuses escaped PTX report expansion while preserving bounded interrupted facts", async () => { const text = ptxFixture + "\n//" + "\u0001".repeat(3 * 1024 * 1024); const result = await inspect(Buffer.from(text));