fix(gpu): validate standalone PTX encoding during discovery (#12707)

This commit is contained in:
morluto authored and GitHub committed 2026-11-19 21:44:06 -05:00
1 parent 8fb9456019
commit e53c1a17fc
3 files changed
+114 -32

No files matched your search

+6 -5
View File
@@ -226,8 +226,9 @@ conflicting values remain unknown. Parameter sections are parsed once per ELF.
Missing CUBIN symbol tables or unresolved section names produce `partial`
object, report and target coverage even when some kernel facts survive. AMD note
failures preserve earlier successfully parsed declarations and the original
failure reason. Standalone PTX recognition tries a small header first, then
continues within the existing 16 MiB text budget when leading comments or
whitespace obscure its directives; admitted full-file probe bytes are reused.
After those comments and whitespace, `.version` must be the first PTX module
token; later PTX-looking text does not identify an arbitrary binary as PTX.
failure reason. Standalone PTX recognition scans up to 16 MiB, allowing for
leading comments and whitespace. After that leading text, `.version` must be
the first module token. Inventory and kernel inspection use strict UTF-8 and
reuse a complete, valid scan. Invalid encoding can leave a recognizable PTX
object whose effective target is unknown. If a file extends beyond the byte
budget, discovery is partial, including when the scan ends mid-character.
+50 -27
View File
@@ -1,4 +1,4 @@
import { decodeStrictUtf8 } from "../domain/strictUtf8.js";
import { decodeStrictUtf8, strictUtf8Decoder } from "../domain/strictUtf8.js";
import type { KnownCoverageStatus } from "../domain/coverage.js";
import type {
GpuCodeObjectsInput,
@@ -71,6 +71,22 @@ import {
} from "./PtxKernels.js";
import type { AmdGpuTargetIdentity } from "./AmdGpuTarget.js";
const PTX_INVALID_UTF8 = "PTX is not valid UTF-8 text";
const readPtxText = (
bytes: Buffer,
coverage: "complete" | "prefix",
): string | null => {
try {
if (coverage === "complete") return decodeStrictUtf8(bytes, "preserve");
// The probe can end in the middle of an otherwise valid scalar.
return strictUtf8Decoder("preserve").decode(bytes, { stream: true });
} catch (cause) {
if (!(cause instanceof TypeError)) throw cause;
return null;
}
};
/** File declarations only. Every child range stays in its named parent coordinate space. */
export class GpuInventory {
readonly objects: GpuObject[] = [];
@@ -259,39 +275,43 @@ export class GpuInventory {
return directory.format;
}
// PTX is positively identified by its directives, never by extension or arbitrary embedded text.
let probe = await source.read(0, Math.min(source.size, 64 * 1024));
let targetFacts = readPtxTarget(probe.toString("utf8"));
if (probe.length < Math.min(source.size, GPU_REPORT_BYTES)) {
probe = await source.read(0, Math.min(source.size, GPU_REPORT_BYTES));
targetFacts = readPtxTarget(probe.toString("utf8"));
}
const probe = await source.read(0, Math.min(source.size, GPU_REPORT_BYTES));
const complete = probe.length === source.size;
const text = readPtxText(probe, complete ? "complete" : "prefix");
// Replacement decoding may identify a malformed candidate. Only strict
// text can establish its target or become retained PTX text evidence.
const targetFacts = readPtxTarget(text ?? probe.toString("utf8"));
if (targetFacts.recognized) {
const target = probe.length === source.size ? targetFacts.target : null;
const limitation =
probe.length < source.size
? "PTX target scan reached the 16 MiB text budget before all module directives were examined."
: targetFacts.limitation;
const row = gpuObject(source, {
offset: 0,
size: source.size,
kind: "ptx",
});
row.target = target;
if (limitation !== null) {
row.inspection = "partial";
row.limitations.push(limitation);
if (text === null) row.limitations.push(PTX_INVALID_UTF8);
if (!complete)
row.limitations.push(
"PTX target scan reached the 16 MiB text budget before all module directives were examined.",
);
if (complete && text !== null) {
row.target = targetFacts.target;
if (targetFacts.limitation !== null)
row.limitations.push(targetFacts.limitation);
}
if (row.limitations.length > 0) {
row.inspection = text === null ? "malformed" : "partial";
this.discovery = "partial";
}
this.#add(row);
await this.#payload(
probe.length === source.size
? new GpuBufferSource(probe, source.containerId)
: source,
complete ? new GpuBufferSource(probe, source.containerId) : source,
row,
complete
? { standalonePtx: text === null ? null : { text, targetFacts } }
: undefined,
);
return "ptx";
}
if (probe.length < source.size)
if (!complete)
throw new BinaryResourceFailure(
"structure",
GPU_REPORT_BYTES,
@@ -323,9 +343,14 @@ export class GpuInventory {
{
amdMetadata,
declaredDecodedSize,
standalonePtx,
}: {
amdMetadata?: AmdElfMetadata;
declaredDecodedSize?: number | bigint;
standalonePtx?: {
text: string;
targetFacts: ReturnType<typeof readPtxTarget>;
} | null;
} = {},
): Promise<void> {
if (this.input.analysis !== "kernels") return;
@@ -390,18 +415,16 @@ export class GpuInventory {
this.#decoded(row, sha256Hex(bytes), bytes.length);
const decoded = new GpuBufferSource(bytes, row.id);
if (row.kind === "ptx") {
// Standalone discovery already retained this encoding failure.
if (standalonePtx === null) return;
// NVIDIA appends NUL alignment padding to an uncompressed PTX entry.
let length = bytes.length;
if (row.raw_flags !== null)
while (length > 0 && bytes[length - 1] === 0) length--;
const content = bytes.subarray(0, length);
let text: string;
try {
text = decodeStrictUtf8(content, "preserve");
} catch {
return rejectGpu("PTX is not valid UTF-8 text");
}
const target = readPtxTarget(text);
const text = standalonePtx?.text ?? readPtxText(content, "complete");
if (text === null) return rejectGpu(PTX_INVALID_UTF8);
const target = standalonePtx?.targetFacts ?? readPtxTarget(text);
const conflict = this.#target(row, {
value: target.target,
key: target.target,
+58
View File
@@ -1073,6 +1073,64 @@ it("uses a later module target when selecting standalone PTX", async () => {
expect(inventory.value.objects[0]!.target).toBe("sm_90");
expect(selected.value.kernels.map(({ name }) => name)).toEqual(["first"]);
});
it("reports invalid standalone PTX encoding in inventory and kernel inspection", async () => {
const header = Buffer.from(
".version 8.0\n.target sm_80\n.address_size 64\n// ",
);
const entry = Buffer.from("\n.visible .entry hello() { ret; }\n");
const malformed = Buffer.concat([header, Buffer.from([0xff]), entry]);
const valid = Buffer.concat([header, Buffer.from("café"), entry]);
for (const analysis of ["inventory", "kernels"] as const) {
const result = await inspect(malformed, { analysis });
if (!result.ok) throw result.error;
expect(result.value).toMatchObject({
format: "ptx",
artifact: {
sha256: createHash("sha256").update(malformed).digest("hex"),
},
coverage: {
discovery: "partial",
payloads: analysis === "kernels" ? "partial" : "not-requested",
},
objects: [{ kind: "ptx", target: null, inspection: "malformed" }],
kernels: [],
ptx_artifacts: [],
});
expect(result.value.objects[0]!.limitations).toContain(
"PTX is not valid UTF-8 text",
);
const control = await inspect(valid, { analysis });
if (!control.ok) throw control.error;
expect(control.value.coverage.discovery).toBe("complete");
expect(control.value.objects[0]!.target).toBe("sm_80");
if (analysis === "kernels") {
expect(control.value.kernels.map(({ name }) => name)).toEqual(["hello"]);
expect(
Buffer.from(control.value.ptx_artifacts[0]!.text).equals(valid),
).toBe(true);
}
}
});
it("keeps a UTF-8 character split by the standalone PTX scan budget unexamined", async () => {
const budget = 16 * 1024 * 1024;
const header = Buffer.from(".version 8.0\n.target sm_80\n// ");
const bytes = Buffer.concat([
header,
Buffer.alloc(budget - header.length - 1, 0x20),
Buffer.from("é\n"),
]);
const result = await inspect(bytes, { analysis: "inventory" });
if (!result.ok) throw result.error;
expect(result.value.coverage.discovery).toBe("partial");
expect(result.value.objects).toMatchObject([
{ kind: "ptx", target: null, inspection: "partial" },
]);
expect(result.value.objects[0]!.limitations).toEqual([
"PTX target scan reached the 16 MiB text budget before all module directives were examined.",
]);
});
it.each(["truncated", "invalid", "extension"] as const)(
"retains earlier AMD kernel declarations after a later %s note",
async (kind) => {