From 23cf85ef24fbc598125d0d3a2cddb42eeb2337b5 Mon Sep 17 00:00:00 2001 From: Anurag Singh Date: Sat, 21 Nov 2026 19:12:49 +0530 Subject: [PATCH] fix(gpu): decode UTF-8 CUBIN kernel symbol names (#13101) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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> --- src/gpu/GpuElfKernels.ts | 20 ++++++++++++++++++-- tests/boundary/gpu/codeObjects.test.ts | 16 +++++++++++++++- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/gpu/GpuElfKernels.ts b/src/gpu/GpuElfKernels.ts index 37df71fae4..cc32f56b37 100644 --- a/src/gpu/GpuElfKernels.ts +++ b/src/gpu/GpuElfKernels.ts @@ -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 ( diff --git a/tests/boundary/gpu/codeObjects.test.ts b/tests/boundary/gpu/codeObjects.test.ts index b3db8671de..f0de8df2b2 100644 --- a/tests/boundary/gpu/codeObjects.test.ts +++ b/tests/boundary/gpu/codeObjects.test.ts @@ -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])(