diff --git a/docs/gpu-code-objects.md b/docs/gpu-code-objects.md index 6be80c3846..54f8896ca5 100644 --- a/docs/gpu-code-objects.md +++ b/docs/gpu-code-objects.md @@ -194,7 +194,9 @@ whitespace, after which `.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 with an unknown target. A file longer than the scan budget, including one that ends mid-character at the -budget, gives partial discovery. +budget, gives partial discovery. An unmatched `}` or a body still open at the end +of the text also leaves the target unknown, matching the unbalanced body that +kernel inspection rejects. ## Read PTX text diff --git a/src/gpu/PtxKernels.ts b/src/gpu/PtxKernels.ts index e653bf754e..ff90e27525 100644 --- a/src/gpu/PtxKernels.ts +++ b/src/gpu/PtxKernels.ts @@ -679,7 +679,11 @@ export const readPtxTarget = (text: string): PtxTargetFacts => { continue; } if (token.value === "}") { - if (depth > 0) depth--; + // The kernel reader rejects an unmatched close as an unbalanced body. + // Keep scanning so the object stays recognized, but the target is not + // established from structure that inspection cannot accept. + if (depth === 0) tracker.markMalformed(); + else depth--; continue; } if (depth !== 0) continue; @@ -687,6 +691,8 @@ export const readPtxTarget = (text: string): PtxTargetFacts => { const value = stream.next(); tracker.observe(value.done ? undefined : value.value.value); } + // A body still open at end of text is truncated, as kernel inspection reports. + if (depth !== 0) tracker.markMalformed(); } catch (cause) { if (!(cause instanceof BinaryFormatFailure)) throw cause; tracker.markMalformed(); diff --git a/tests/boundary/gpu/codeObjects.test.ts b/tests/boundary/gpu/codeObjects.test.ts index dd9f4275d6..b41e772ed6 100644 --- a/tests/boundary/gpu/codeObjects.test.ts +++ b/tests/boundary/gpu/codeObjects.test.ts @@ -1474,6 +1474,47 @@ it("reports invalid standalone PTX encoding in inventory and kernel inspection", } }); +it.each([ + [ + "an unmatched close", + ".version 8.0\n.target sm_80\n.entry k() { ret; }}\n.target sm_90\n", + ], + // The first target follows the stray close, so recognition depends on the + // scan continuing past it. + ["a close before the first target", ".version 8.0\n}\n.target sm_90\n"], + [ + "a truncated entry body", + ".version 8.0\n.target sm_80\n.target sm_90\n.entry k() { ret;\n", + ], +])("keeps the standalone PTX target unknown after %s", async (_label, text) => { + const malformed = Buffer.from(text); + const valid = Buffer.from( + ".version 8.0\n.target sm_80\n.entry k() { ret; }\n.target sm_90\n", + ); + for (const analysis of ["inventory", "kernels"] as const) { + const result = await inspect(malformed, { analysis }); + if (!result.ok) throw result.error; + expect(result.value.coverage.discovery).toBe("partial"); + expect(result.value.objects).toMatchObject([{ kind: "ptx", target: null }]); + expect(result.value.objects[0]!.inspection).not.toBe("complete"); + expect(result.value.objects[0]!.limitations).toContain( + "Malformed PTX text prevents complete target discovery.", + ); + + const control = await inspect(valid, { analysis }); + if (!control.ok) throw control.error; + expect(control.value.coverage.discovery).toBe("complete"); + expect(control.value.objects).toMatchObject([ + { + kind: "ptx", + target: "sm_90", + inspection: analysis === "kernels" ? "complete" : "inventory", + limitations: [], + }, + ]); + } +}); + 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// ");