fix(gpu): decode UTF-8 CUBIN kernel symbol names (#13101)

CUBIN kernel names were always decoded as Latin-1, so a valid UTF-8
symbol such as kérnel_日本 was reported as mojibake while inspection
stayed complete. Reuse the ELF name decoding owned by native exports:
keep valid UTF-8, fall back to lossless Latin-1 otherwise, and disclose
the fallback with the original bytes as a kernel limitation.

Co-authored-by: Anurag Singh <211784907+Anurag-5ingh@users.noreply.github.com>
This commit is contained in:
Anurag SinghandAnurag Singh authored and GitHub committed 2026-11-21 08:42:49 -05:00
1 parent 3ef94f1f33
commit 23cf85ef24
2 files changed
+33 -3

No files matched your search

+18 -2
View File
@@ -29,6 +29,7 @@ import {
type NativeSectionDirectory,
} from "../domain/native/nativeSectionDirectory.js";
import { readElfStringBytes } from "../domain/native/elfStringBytes.js";
import { decodeElfExportName } from "../domain/native/nativeExports.js";
import { elfSymbolSize, readElfSymbol } from "../domain/elfSymbol.js";
import {
ELF_SHN_XINDEX,
@@ -97,6 +98,21 @@ const blankKernel = (
sources: { parameters: null, registers: null, shared: null, private: null },
limitations: [],
});
/** CUDA symbol names are ELF bytes; keep valid UTF-8 and disclose a Latin-1 fallback. */
const cudaSymbolKernel = (
source: GpuSource,
id: string,
nameBytes: Buffer,
location: GpuLocation,
): GpuKernel => {
const decoded = decodeElfExportName(nameBytes);
const kernel = blankKernel(source, id, decoded.name, location);
if (!decoded.name_utf8_decodable)
kernel.limitations.push(
`CUDA kernel symbol name is not valid UTF-8; name shows its bytes as Latin-1 (base64 ${decoded.name_bytes_base64!})`,
);
return kernel;
};
export const readGpuElfKernels = async (
source: GpuSource,
kind: "cubin" | "amdgpu",
@@ -333,10 +349,10 @@ const cudaKernels = async (
: undefined;
if (text === undefined || text.type !== 1 || (text.flags & 4n) === 0n)
rejectGpu("CUBIN kernel symbol has no executable section");
const kernel = blankKernel(
const kernel = cudaSymbolKernel(
source,
`sym:${section.index}:${offset / width}`,
readElfStringBytes(names, symbol.name_offset).toString("latin1"),
readElfStringBytes(names, symbol.name_offset),
site(source, section, offset, width),
);
if (
+15 -1
View File
@@ -1240,6 +1240,16 @@ it.each(["section-names", "symbols"] as const)(
);
},
);
it.each(["add_one", "kérnel_日本"])(
"decodes UTF-8 CUBIN symbol name %s without a name limitation",
async (name) => {
const result = await inspect(cubinFixture({ name }));
if (!result.ok) throw result.error;
const [kernel] = result.value.kernels;
expect(kernel).toMatchObject({ name, symbol: name });
expect(kernel!.limitations.join(" ")).not.toContain("not valid UTF-8");
},
);
it("preserves non-UTF-8 CUBIN symbol bytes in the kernel name", async () => {
const bytes = cubinFixture();
const symbolName = bytes.indexOf(Buffer.from("\0add_one\0"));
@@ -1247,7 +1257,11 @@ it("preserves non-UTF-8 CUBIN symbol bytes in the kernel name", async () => {
bytes[symbolName + 1] = 0xff;
const result = await inspect(bytes);
if (!result.ok) throw result.error;
expect(result.value.kernels[0]?.name).toBe("ÿdd_one");
const [kernel] = result.value.kernels;
expect(kernel?.name).toBe("ÿdd_one");
expect(kernel!.limitations).toContain(
`CUDA kernel symbol name is not valid UTF-8; name shows its bytes as Latin-1 (base64 ${Buffer.from("\xffdd_one", "latin1").toString("base64")})`,
);
});
it.each([9, 10])(