Files
reverse-engineering-anything/src/gpu/CudaFatbinary.ts
T
RRFZ 2e59f08992 feat(gpu)!: add per-call object, work, report, metadata, structure, and parser budgets to inspect_gpu_code_objects (#14882)
* feat(gpu)!: add per-call object, work, report, metadata, structure, and parser budgets to inspect_gpu_code_objects

Replace the pending GPU object and parser ceilings in GpuCodeObjectsProvider
with domain-owned optional per-call parameters (max_object_bytes, max_work_bytes,
max_report_bytes, max_metadata_bytes, max_structure_bytes, max_parser_entries,
max_parser_visits, and max_token_bytes) reporting details.limit and
adjustable_budget, remove superseded hardcoded constants, and register only the
recursive container and AMDGPU MessagePack nesting caps as a fixed
representation allowance.

BREAKING CHANGE: inspect_gpu_code_objects adds optional per-call budget
parameters and reports details.limit with details.refusal.kind
"adjustable_budget" instead of "fixed_ceiling" on top-level object, work,
report, metadata, structure, and parser budget refusals.

* fix(gpu): clamp padding scans, classify multi-frame CCOB budget cuts, and keep max_work_bytes at 512 MiB

* fix(gpu): align --max-work-bytes help text, release partial decode before refill, and admit floats under small token budgets

* test(gpu): keep multi-frame CCOB v1 decoded size within cut budgets

* fix(gpu): report decoder inputExhausted to refill and bound trailing Zstandard frames

* fix(gpu): peek next Zstd frame, charge AMD structure reads to work budget, and keep 128-token PTX limit

* fix(gpu): fix CCOB refill work double-charge, meter section reads, decouple non-GPU recognition, and align frame/recovery bounds

* refactor(gpu): drop redundant lookahead on non-refill path and pin refill/readBounded regressions

* fix(gpu): keep standalone PTX peak heap at 1x, use explicit scan reads, forward CUDA cancel signal, and report max_report_bytes required_value

* fix(gpu): report lower-bound report bytes in reported_limits and enforce max_token_bytes on recognized PTX headers
2026-11-27 16:14:15 +00:00

100 lines
3.6 KiB
TypeScript

import { BinaryReader } from "../domain/binaryContainer.js";
import type { GpuObject } from "../domain/gpu/gpuCodeObjects.js";
import { assertArtifactReadActive } from "../resources/index.js";
import {
gpuCount,
nextGpuContainerOffset,
rejectGpu,
type GpuSource,
} from "./GpuSource.js";
import { gpuObject, recordDeclaredGpuDecodedSize } from "./GpuObject.js";
const CUDA_FATBINARY = "cuda-fatbinary";
export async function* readCudaFatbinary(
source: GpuSource,
signal?: AbortSignal,
): AsyncGenerator<{ row: GpuObject; declaredDecodedSize: number | bigint }> {
let base = 0;
while (base < source.size) {
const header = new BinaryReader(
await source.read(base, 16),
true,
CUDA_FATBINARY,
);
if (header.u32(0) !== 0xba55ed50)
rejectGpu("Unexpected bytes between CUDA fatbinary records");
if (header.u16(4) !== 1 || header.u16(6) < 16)
rejectGpu("Unsupported CUDA fatbinary header");
const start = base + header.u16(6),
length = gpuCount(header.word(8, 64));
if (start > source.size || length > source.size - start)
rejectGpu("CUDA fatbinary extent is truncated");
const end = start + length;
for (let position = start; position < end;) {
assertArtifactReadActive(signal);
if (end - position < 64) rejectGpu("CUDA entry header is truncated");
const h = new BinaryReader(
await source.read(position, 64),
true,
CUDA_FATBINARY,
);
const kind = h.u16(0),
version = h.u16(2),
width = h.u32(4);
const padded = gpuCount(h.word(8, 64)),
encoded = h.u32(16),
flags = h.word(40, 64);
if (
width < 64 ||
width > end - position ||
padded > end - position - width ||
encoded > padded
)
rejectGpu("CUDA entry extent is invalid");
const compressed = (flags & 0xb000n) !== 0n;
const row = gpuObject(source, {
offset: position + width,
size: compressed ? encoded : padded,
kind: kind === 1 ? "ptx" : kind === 2 ? "cubin" : "unknown",
metadata: { offset: position, size: width },
});
const architectureSuffix =
(flags & 0x100000n) !== 0n
? "a"
: (flags & 0x200000n) !== 0n
? "f"
: "";
row.target = `sm_${h.u32(28)}${architectureSuffix}`;
row.raw_flags = `0x${flags.toString(16)}`;
row.compression =
(flags & 0x8000n) !== 0n && (flags & 0x2000n) === 0n
? "zstd"
: (flags & 0x2000n) !== 0n && (flags & 0x8000n) === 0n
? "lz4"
: compressed
? "unsupported"
: "none";
row.compression_id = compressed ? `cuda-flags:${row.raw_flags}` : null;
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";
row.target = null;
row.decoded_size = null;
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, declaredDecodedSize };
position += width + padded;
}
base = await nextGpuContainerOffset(source, end, signal);
}
}