Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/gpu/ClangOffloadBundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,16 @@ export const readClangBundleDirectory = async (
declarations: { object: GpuObject; amdTarget: AmdGpuTargetIdentity | null }[];
end: number;
}> => {
const header = await source.read(base, 32),
count = gpuCount(header.readBigUInt64LE(24));
const header = await source.read(base, 32);
if (!header.subarray(0, 24).equals(CLANG_BUNDLE_MAGIC))
rejectGpu("Invalid Clang offload bundle magic");
const declaredCount = header.readBigUInt64LE(24);
// Every entry needs three 64-bit words before its variable-length ID.
// Compare before numeric conversion so even a uint64 maximum stays a
// malformed extent rather than a capacity or host-precision refusal.
if (declaredCount > BigInt(Math.floor((source.size - base - 32) / 24)))
rejectGpu("Clang offload bundle directory is truncated");
const count = gpuCount(declaredCount);
if (count > 65536)
throw new BinaryResourceFailure(
"structure",
Expand Down
28 changes: 28 additions & 0 deletions tests/boundary/gpu/codeObjects.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1224,3 +1224,31 @@ it("refuses escaped PTX report expansion while preserving bounded interrupted fa
expect(partial.objects).toHaveLength(1);
expect(partial.artifact.sha256).toBeNull();
});

it("distinguishes impossible Clang bundle counts from a framed over-capacity directory", async () => {
const empty = Buffer.alloc(32);
Buffer.from("__CLANG_OFFLOAD_BUNDLE__").copy(empty);
const emptyResult = await inspect(empty, { analysis: "inventory" });
if (!emptyResult.ok) throw emptyResult.error;
expect(emptyResult.value.objects).toEqual([]);
const truncated = hipFixture().subarray(0, 32);
for (const count of [65537n, 0xffffffffffffffffn]) {
const bytes = Buffer.from(truncated);
bytes.writeBigUInt64LE(count, 24);
const result = await inspect(bytes, { analysis: "inventory" });
if (result.ok) throw new Error("Expected malformed directory");
expect(result.error._tag).toBe("BinaryTargetError");
}
const framed = Buffer.alloc(32 + 65537 * 25);
Buffer.from("__CLANG_OFFLOAD_BUNDLE__").copy(framed);
framed.writeBigUInt64LE(65537n, 24);
for (let index = 0; index < 65537; index++) {
const row = 32 + index * 25;
framed.writeBigUInt64LE(BigInt(framed.length), row);
framed.writeBigUInt64LE(1n, row + 16);
framed[row + 24] = 0x78;
}
const result = await inspect(framed, { analysis: "inventory" });
if (result.ok) throw new Error("Expected directory capacity failure");
expect(result.error._tag).toBe("AnalysisResourceConstraintError");
});