From dd074dbafd6d31b924a1a1e414b01d1c6a242ccf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Balv=C3=ADn?= Date: Tue, 6 Oct 2026 23:54:16 +0200 Subject: [PATCH] fix(asm): reject an out-of-range GLOBL size with a diagnostic Assisted-by: GLM 5.3 Flash --- asm/assembler_fuzz_test.go | 1 + asm/link.go | 11 +++++++++++ 2 files changed, 12 insertions(+) diff --git a/asm/assembler_fuzz_test.go b/asm/assembler_fuzz_test.go index 366f047..9bc4048 100644 --- a/asm/assembler_fuzz_test.go +++ b/asm/assembler_fuzz_test.go @@ -82,6 +82,7 @@ func FuzzAssembleAMD64(f *testing.F) { // above never reach. f.Add("TEXT ·f(SB), $0\n\tBOGUSINSTR AX, BX\n\tRET\n") f.Add("GLOBL d(SB), $-8\n") + f.Add("GLOBL d(SB), $0x7FFFFFFFFFFFFFFF\n") f.Add("DATA d+0(SB)/9, $1\nGLOBL d(SB), $8\n") f.Add("TEXT ·f(SB), NOSPLIT, $0\n\tADJSP $16\n\tRET\n") f.Add("#define A A\nA\n") diff --git a/asm/link.go b/asm/link.go index ef28d22..bdfa9ad 100644 --- a/asm/link.go +++ b/asm/link.go @@ -607,6 +607,14 @@ func checkDuplicateDecls(f *ast.File) error { return nil } +// maxSymbolSize is the ceiling on one GLOBL's declared size. The image +// materialises the symbol's bytes at assembly time, where the toolchain +// defers the cost to its linker, so the bound is the toolchain's own: obj +// rejects anything over 2,000,000,000 bytes as "symbol too large", and a +// negative size, which the toolchain's int64 field tolerates and its linker +// refuses, is diagnosed here rather than attempted. +const maxSymbolSize = 2_000_000_000 + // collectData gathers the file's static symbols (GLOBL) and their initial // contents (DATA) into byte buffers. Two passes: the Plan 9 convention puts // every DATA line before its symbol's GLOBL, so the symbols are registered @@ -631,6 +639,9 @@ func collectData(f *ast.File) ([]dataSym, error) { size := 0 if gd.Size != nil && gd.Size.Imm.HasVal { size = int(gd.Size.Imm.Val) + if size < 0 || size > maxSymbolSize { + return nil, fmt.Errorf("GLOBL %q: symbol too large (%d bytes > %d bytes)", name, size, maxSymbolSize) + } } index[name] = len(syms) ds := dataSym{