Two GC allocations are two blocks again under optimization - #392
Merged
Merged
Conversation
The collector's allocators (GC_malloc, GC_malloc_atomic, GC_memalign, GC_malloc_explicitly_typed) were declared `memory(write, inaccessiblemem: none)` plus `allockind`, which #188 relied on to keep LLVM from merging two identical calls. With the current LLVM that no longer holds: two GC_malloc(8) calls - the storage of two empty arrays - became one, both arrays grew through GC_realloc of the same block, and the heap was corrupted. It surfaced as `s += a` producing an empty string, only with optimization (every JIT run, and --opt builds). The allocator's state is now inaccessible memory the call reads and writes, as LLVM declares malloc, and the calls stay apart. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This was referenced Sep 28, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Under optimization (every JIT run, and
--optbuilds) with-mm=gc, this printed an empty line where it should print1,:Cause: the optimizer merged two
GC_malloc(8)calls, the storage of the two empty arraysnumsand the temporary built for[...src], into one. Both arrays then grew throughGC_reallocof that one block, the second time through a pointer the first realloc had already freed. The heap was corrupted, and the corruption showed up later as a string that came out empty.The unoptimized IR has two calls; after
opt -O3there is one. The allocators were declaredmemory(write, inaccessiblemem: none)plusallockind("alloc,zeroed"). #188 relied on thatallockindto prevent exactly this merge, and with the current LLVM it no longer does. On the saved IR, declaringinaccessiblemem: readwrite, which is how LLVM declaresmalloc(the allocator reads and writes its own hidden state), keeps both calls.Fix: that memory effect is set on
GC_malloc,GC_malloc_atomicandGC_memalign(GCPass.cpp), and onGC_malloc_explicitly_typed, used fornew(LowerToLLVM.cpp).Test:
00gc_allocations_distinct.ts, registered for compile, jit and the corpus. It covers:The repro part fails on main.
Results: the suite passes locally, 3017/3017, and the DefaultLib tests pass 156/156 in both jit and compile modes.
🤖 Generated with Claude Code