fix(gpu): bound embedded section reads

Fixes #6657
This commit is contained in:
morluto committed 2026-10-23 19:18:47 +08:00
1 parent b443b5a89b
commit 8695ca8257
3 files changed
+86 -2

No files matched your search

+3 -1
View File
@@ -128,7 +128,9 @@ A location with `container_id: null` uses original file offsets. Other locations
use that inventoried object's decoded byte coordinates. Follow its parent, use that inventoried object's decoded byte coordinates. Follow its parent,
encoded location and digest to trace decompressed metadata back to a native encoded location and digest to trace decompressed metadata back to a native
section and the original file. Decoded offsets are never presented as physical section and the original file. Decoded offsets are never presented as physical
file offsets. file offsets. Reads from an ELF or PE GPU section stay within that declared
section extent, so neighboring native section bytes cannot complete a
truncated embedded bundle header.
The CLI uses the same service and contracts: The CLI uses the same service and contracts:
+4 -1
View File
@@ -197,7 +197,10 @@ export class GpuInventory {
const borrowed: GpuSource = { const borrowed: GpuSource = {
size, size,
containerId: source.containerId, containerId: source.containerId,
read: (start, length) => source.read(offset + start, length), read: (start, length) => {
checkGpuRange(size, start, length);
return source.read(offset + start, length);
},
}; };
// A section is a coordinate space with an explicit physical backing, including PE short names. // A section is a coordinate space with an explicit physical backing, including PE short names.
const row = gpuObject(source, { const row = gpuObject(source, {
@@ -20,6 +20,7 @@ import {
amdFixture, amdFixture,
cubinFixture, cubinFixture,
hipFixture, hipFixture,
elfFixture,
} from "../fixtures/gpu/codeObjects.js"; } from "../fixtures/gpu/codeObjects.js";
cliTest( cliTest(
@@ -64,6 +65,84 @@ cliTest(
}, },
); );
cliTest(
"keeps embedded Clang bundle reads inside their native section",
async ({ cli }) => {
const root = await createTestTempDirectory("rea-gpu-section-range-");
const magic = Buffer.from("__CLANG_OFFLOAD_BUNDLE__"),
truncated = magic,
complete = Buffer.concat([magic, Buffer.alloc(8)]);
const cases = [
["truncated-zero-neighbor", truncated, Buffer.alloc(8)],
[
"truncated-one-neighbor",
truncated,
Buffer.from([1, ...Array(7).fill(0)]),
],
["complete", complete, Buffer.alloc(8)],
] as const;
const reports = [];
for (const [name, bundle, adjacent] of cases) {
const bytes = elfFixture(62, [
{ name: ".hip_fatbin", bytes: bundle },
{ name: ".data", bytes: adjacent },
]),
path = join(root, name);
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"),
);
reports.push(report);
}
const [zeroNeighbor, oneNeighbor, completeReport] = reports;
expect(zeroNeighbor!.coverage).toMatchObject({
discovery: "partial",
payloads: "partial",
});
expect(oneNeighbor!.coverage).toMatchObject({
discovery: "partial",
payloads: "partial",
});
expect(zeroNeighbor!.objects[0]).toMatchObject({
kind: "native-section",
inspection: "malformed",
location: { size: 24, section_name: ".hip_fatbin" },
});
expect(oneNeighbor!.objects[0]).toMatchObject({
kind: "native-section",
inspection: "malformed",
location: { size: 24, section_name: ".hip_fatbin" },
});
expect(completeReport!.coverage).toMatchObject({
discovery: "complete",
payloads: "complete",
});
expect(completeReport!.objects[0]).toMatchObject({
kind: "native-section",
inspection: "complete",
location: { size: 32, section_name: ".hip_fatbin" },
});
},
);
cliTest( cliTest(
"compares equivalent PTX targets and retains family-specific CUDA targets", "compares equivalent PTX targets and retains family-specific CUDA targets",
async ({ cli }) => { async ({ cli }) => {