News for hackers. Stuff that matters.
Re:We found a bug in Go's ARM64 compiler by Neywiny on Wednesday October 08, 2025 @11:38AM (#45517365)
That's an incredible find and once I saw the assembly I was right along with them on the debug path. Interestingly it doesn't need to be assembly for this to work, it's just that that's where the split was. The IR could've done it, it just doesn't for very good reasons. So another win for being able to read arm assembly.

Unsure if this would be another way to do it but to save an instruction at the cost of a memory access you could push then pop the stack size maybe? Since presumably you're doing that pair of moves on function entry and exit. I'm not really sure what the garbage collector is looking for so maybe that doesn't work, but I'd be interested to hear some takes on it

Re:We found a bug in Go's ARM64 compiler by pjmlp on Wednesday October 08, 2025 @12:14PM (#45517788)
Usually in runtimes like Java and .NET there are safepoints exactly to avoid changing context in the middle of a set of instructions.
Re:We found a bug in Go's ARM64 compiler by andygocke on Wednesday October 08, 2025 @12:44PM (#45518107)
Yeah but we have codegen bugs in .NET as well. The biggest difference that stood out to me in this write up, is we would have gone straight for “coredump” instead of other investigation tools. Our default mode of investigating memory corruption issues is dumps.
Re:We found a bug in Go's ARM64 compiler by pjmlp on Wednesday October 08, 2025 @01:56PM (#45518810)
Sure, I have experienced them, e.g. once in 2006 using IBM's JVM implementation with Websphere.

However it is probably not as problematic due to the way Go allows for Assembly being used directly.

While the JVM and CLR don't allow for direct access to Assembly code, Go does, thus I assume expecting safepoints everywhere is not an option, as any subroutine call can land on code that was manually written.

Re:We found a bug in Go's ARM64 compiler by yvdriess on Thursday October 09, 2025 @07:34AM (#45526226)
Go users can only insert assembly wrapped in a function call. That might be safety related, I am not entirely sure.

(Well technically there is a way to inject assembly without the function call overhead. That's what https://pkg.go.dev/runtime/internal/atomic is doing. But you will need to modify the runtime and compiler toolchain for it.)

Re:We found a bug in Go's ARM64 compiler by pjmlp on Thursday October 09, 2025 @01:21PM (#45530558)
If you look the docs, they expect the developer to add specific information and use the registers in a specific way, otherwise Go will face runtime issues.

Whereas when you go over CGO, you get a marshaling layer similar to how JNI, P/Invoke work, that take care of those issues.

Re:We found a bug in Go's ARM64 compiler by bloak on Wednesday October 08, 2025 @11:49AM (#45517478)
> So another win for being able to read arm assembly.

Yes, though that weird stuff with dollars in it is not normal AArch64 assembly!

The article could have mentioned the "stack moves once" rule.

Re:We found a bug in Go's ARM64 compiler by pjmlp on Wednesday October 08, 2025 @12:13PM (#45517776)
It is due to the Plan 9 Assembly dialect most likely, because it wasn't enough that we already have differences between AT&T and Intel.

https://go.dev/doc/asm

Still, I find great that Go got back the 1990's tradition that compiled languages have an assembler as part of their tooling, regardless of the syntax.

Re:We found a bug in Go's ARM64 compiler by Neywiny on Wednesday October 08, 2025 @12:43PM (#45518102)
I've never heard of that rule (though tbh I'm not allocating > 64KB of stack when I'm in assembly) and it seems Google hasn't either. While I'm sure it makes sense, I don't think I've ever seen that be enforced. At least in C/C++. Maybe it makes more sense for these stack inspecting garbage collectors but I've also heard of ones that just scan the stack without unwinding anything. I did a test asking Google's AI to generate a complicated C function, put it in godbolt, and there's plenty of push push push push ..... Pop Pop Pop Pop going on
Re:We found a bug in Go's ARM64 compiler by JdeBP on Wednesday October 08, 2025 @02:21PM (#45519067)
You need to look at non-x86 architectures. It was common years ago on MIPS.

* https://jdebp.uk/FGA/function-perilogues.html#StandardMIPS

I wrote up the x86 equivalent of doing just two read-modify-write operations on the stack pointer over 16 years ago.

* https://jdebp.uk/FGA/function-perilogues.html#Standardx86

Re:We found a bug in Go's ARM64 compiler by rcxdude on Wednesday October 08, 2025 @01:20PM (#45518439)
Did you compile with optimisations? I think GCC will do a bunch of activity on the stack with -O0, but it'll generally coalesce everything into one push/pop per function with optimisations (not because of any rule, but just because it's faster). alloca and other dynamic stack allocation may break this, but normal variables should in pretty much all just get turned into one block on the stack (with appropriate re-use of space if variable lifetimes don't overlap)
Re:We found a bug in Go's ARM64 compiler by Neywiny on Wednesday October 08, 2025 @03:36PM (#45519780)
Yes
Re:We found a bug in Go's ARM64 compiler by ori_b on Wednesday October 08, 2025 @07:42PM (#45521888)
It will generate code to touch each page of the stack, because otherwise a very large stack allocation controlled by users (eg, in the case of a variable sized array) can be turned into a pointer to any location in memory by an attacker. Faulting in each page of the stack turns that into a crash.

There was a userspace thread library I came across a long time ago that used variable length arrays to switch between thread stacks; the scheduler would allocate an array of the right size to bump the stack pointer to the different thread's stack.

Re:We found a bug in Go's ARM64 compiler by saagarjha on Wednesday October 08, 2025 @10:40PM (#45522932)
Wow, that’s horrible.
Re:We found a bug in Go's ARM64 compiler by i80and on Thursday October 09, 2025 @12:10PM (#45529681)
The engineers were so preoccupied with whether or not they could that they didn't stop to think if they should
Re:We found a bug in Go's ARM64 compiler by mananaysiempre on Thursday October 09, 2025 @12:39AM (#45523567)
> While I'm sure [bumping the stack pointer atomically] makes sense, I don't think I've ever seen that be enforced. At least in C/C++.

That’s because the C ABI supports unwinding with a fairly expressive set of tools for describing stack-pointer state on a per-instruction level. Even the simpler Microsoft ABI essentially uses bytecode for that[1]; and on the more complicated Itanium ABI, you get DWARF CFI instructions, which make the correct way to preserve a(n x86) register in the function prologue look like

  push rbx
  .cfi_adjust_cfa_offset 8
  .cfi_rel_offset rbx, 8
which are impossible to miss when reading compiler-generated assembly because of the sheer amount of annoying noise they create.

The Go authors decided to sidestep all of this complexity, which is understandable to a degree, but apparently they did not think through all the ramifications of doing so.

[1] https://learn.microsoft.com/en-us/cpp/build/exception-handli...

Re:We found a bug in Go's ARM64 compiler by dwattttt on Thursday October 09, 2025 @03:37AM (#45524676)
MS's ARM64 unwinding ABI looks even more complicated: https://learn.microsoft.com/en-us/cpp/build/arm64-exception-...
Re:We found a bug in Go's ARM64 compiler by mananaysiempre on Thursday October 09, 2025 @11:08AM (#45528777)
Ehh I wouldn’t say so (thanks for the correct link for ARM64 though in any case). What you need to be comparing to here is DWARF[1,2] section 6.4, and while it’s not as bad as other parts of DWARF, I still think it’s plenty complicated.

[1] https://dwarfstd.org/doc/DWARF5.pdf#page=171

[2] Slightly modified by psABI[3] section 3.7 for x86-64 or the LSB[4] section 11.6 for ARM64, but at this point that’s a drop in the bucket as far as overall complexity is concerned.

[3] https://gitlab.com/x86-psABIs/x86-64-ABI/-/jobs/artifacts/ma...

[4] https://refspecs.linuxfoundation.org/LSB_4.0.0/LSB-Core-gene...

Re:We found a bug in Go's ARM64 compiler by dwattttt on Thursday October 09, 2025 @11:56AM (#45529469)
I was actually looking to point out MS's x64 ABI requires a standardised function epilog since this bug occurred during an epilog, only to find ARM64's epilogues are also described by bytecode (at least at a cursory glance).
Re:We found a bug in Go's ARM64 compiler by freep1zza on Thursday October 09, 2025 @05:01AM (#45525225)
> Yes, though that weird stuff with dollars in it is not normal AArch64 assembly!

See the AT&T vs Intel syntax since you aren't familiar with assembly:

https://en.wikipedia.org/wiki/X86_assembly_language#Syntax

Re:We found a bug in Go's ARM64 compiler by dpassens on Thursday October 09, 2025 @06:09AM (#45525653)
That's an x86 thing, though.
Re:We found a bug in Go's ARM64 compiler by indrora on Thursday October 09, 2025 @12:29PM (#45529926)
There are more assembler dialects than I care to remember.

The 2A06 assembler that people who write NES code (and later on SNES/GB/etc) use has some real quirks: $ prefixes a literal hex value but % is binary, but # in front of that is an address, registers are baked into the opcode (ldx -> load into X), and more.

Playstation folks all just used MIPS dialects which are mostly AT&Tish but the PS2 used an Intel style assembler.

Re:We found a bug in Go's ARM64 compiler by titzer on Wednesday October 08, 2025 @11:48AM (#45517468)
I think the right fix is that the compiler should, e.g. load the constant into a register using two moves and then emit a single add. It's one more instruction, but then the adjustment is atomic (i.e. a single instruction). Another option is to do the arithmetic in a temp register and then move it back.
Re:We found a bug in Go's ARM64 compiler by Veserv on Wednesday October 08, 2025 @01:29PM (#45518540)
You would normally use the “LDR Rd, =expr” pseudo-instruction form [1]. For immediates not directly constructible, it puts a copy of the immediate value in a PC-relative memory location, then does a PC-relative load into register.

So that would turn the whole sequence of “add constant to SP” into 2 executable instructions, 1 for constructing immediate and 1 for adding for a total of 8 bytes, and a 4 byte data area for the 17-bit immediate for a total of 12 bytes of binary which is 3 executable instructions worth.

[1] https://developer.arm.com/documentation/dui0801/l/A64-Data-T...

Re:We found a bug in Go's ARM64 compiler by comex on Wednesday October 08, 2025 @07:34PM (#45521830)
I've usually seen compilers handle large constants with MOV/MOVK sequences (encoding 16 bits of data per 32-bit instruction) instead of loading them from memory. Loading from memory was more common on 32-bit ARM.
Re:We found a bug in Go's ARM64 compiler by pklausler on Wednesday October 08, 2025 @01:40PM (#45518661)
I'm a little surprised that this bug wasn't fixed in the assembler as a special case for immediate adds to RSP. If the patch was to the compiler only, other instances of the bug could be lurking out there in aarch64 assembly code.
Re:We found a bug in Go's ARM64 compiler by moefh on Wednesday October 08, 2025 @06:12PM (#45521184)
Would that be wise? The implemented solution uses a temporary register to hold the full value being added to rsp.

I don't know enough about how people use the go assembler, but I imagine it would be very surprising if `add $imm, rsp, rsp` clobbered an unrelated register when `$imm` is large enough. Especially since what's clobbered is the designated "temporary register", which I imagine is used all the time in handwritten go assembly.

Re:We found a bug in Go's ARM64 compiler by pklausler on Wednesday October 08, 2025 @07:34PM (#45521835)
Some architectures, and I believe aarch64 is one, have scratch registers reserved for being clobbered in special situations required by the assembler.
Re:We found a bug in Go's ARM64 compiler by anyfoo on Wednesday October 08, 2025 @08:19PM (#45522096)
Not really, or at least not that I know if in the case of arm64. What you have is calling conventions that specify what one function/procedure/whatever can expect both from the caller and the callee's side.n I.e. some registers are caller-saved, some are callee-saved, which basically means the called function can treat them as "scratch".

Additionally, they call out interactions with the OS/execution environment. For example, x18 is the "platform register", and it's unspecified what the OS does with it. It's entirely possible that it clobbers it on context switch or during an interrupt or whatever. So don't use that one unless you have a contract with the OS itself.

But locally, i.e. "from instruction to instruction", no such convention exists to my knowledge, and you probably don't want to have registers that pseudo-instructions might trash inadvertently in general, because it means you can't optimally use these registers.

It's possible for pseudo-instructions or generally macros to be documented as, e.g., "this macro uses x3 as a temporary register and trashes it", but in my experience most macros that need additional temporary registers actually ask you to specify them as part of the macro invocation.

E.g. suppose you have a macro "weirdhash" that takes two registers and saves some kind of hash of them in a third register, but that also needs an extra register to perform its work. You would call it with:

    weirdhash x9, x10, x11, x0
Where x0 would be the scratch register you don't care about.
Re:We found a bug in Go's ARM64 compiler by adastra22 on Thursday October 09, 2025 @04:11AM (#45524910)
There are some architectures that do, but they're all old RISC chips.
Re:We found a bug in Go's ARM64 compiler by immibis on Thursday October 09, 2025 @01:08PM (#45530382)
On some architectures there is a register reserved for assembler use, and even registers reserved for kernel use which can be changed in interrupt handlers and not changed back.
Re:We found a bug in Go's ARM64 compiler by anyfoo on Thursday October 09, 2025 @07:44PM (#45534171)
Yeah, I mentioned x18 on arm64 as an example for the latter one. Didn't know about the register reserved for assembly use, apparently MIPS indeed had that: $1, also known as $at, is the "assembler temporary".
Re:We found a bug in Go's ARM64 compiler by saagarjha on Wednesday October 08, 2025 @10:38PM (#45522921)
No, I think that’s just a MIPS thing.
Re:We found a bug in Go's ARM64 compiler by Someone on Thursday October 09, 2025 @04:11AM (#45524905)
Is that possible? I think you would have [1] to use a register to build up the immediate value. The assembler cannot/should not default to one, so I think the best one could do is having another macro for ADD that takes that helper register as an argument. That wouldn’t fix other instances in the AArch64 assembly code.

[1] I’m not familiar with AMD64, but maybe, you could use a thread local or reserve space in the stack frame for it, too, but I don’t see those as realistic options