Skip to content

Don't truncate GC frame root counts to 32 bits - #282

Draft
charnik wants to merge 2 commits into
v1.10.2+RAIfrom
bn-gc-frame-nroots-overflow
Draft

charnik wants to merge 2 commits into
v1.10.2+RAIfrom
bn-gc-frame-nroots-overflow

Conversation

@charnik

@charnik charnik commented Oct 22, 2026 •

Copy link
Copy Markdown

PR Description

Fixes engine segfaults that occur while the GC runs finalizers (run_finalizer crashing in dispatch on a garbage type tag, in __gmpz_clear, or in uvfinalize), seen on ~250 engines over the past year, mostly during TPC-DS benchmarks that divide FixedDecimal{Int128} values.

The GC marks a GC frame's roots using a 32-bit root count. The list of pending finalizers is rooted with a single frame, so with 2^29 or more pending finalizers the count wrapped, a GC that ran while those finalizers were still being processed freed most of the pending objects, and their finalizers then ran on freed memory. Each BigInt registers a finalizer, so many threads doing FixedDecimal{Int128} division (which goes through BigInt on Julia 1.10) easily exceed that number between two collections.

This PR keeps the root count at full width and marks frames with more than GC_CHUNK_BATCH_SIZE roots in chunks, like the global finalizer lists. Pushing all roots of such a frame onto the mark queue at once would otherwise overflow the queue's 32-bit capacity and abort with "Cannot allocate memory".

Before this change, a single-threaded reproducer (see below) that makes N finalizers pending and triggers a GC from inside the first one crashes for N = 2^29 + 2^20 and is clean for N = 2^29 - 2^20. Upstream master has the same code.

Reproducer

The reproducer can be found at repro-finalizer-overflow.jl.zip. It needs a lot of memory: peak resident memory is about 72 bytes per finalizer plus ~250 MB, i.e. about 36 GiB for the default N (measured at smaller N and extrapolated), so run it on a machine with plenty of free RAM (e.g., 64 GiB or more). Expect the following output from a good run:

$> julia -t 1 repro-finalizer-overflow.jl
0.0s: N=537919488 FIRE_AT=1000 threads=1 gcthreads=1
0.1s: entries=1075838976 wraps=true predicted_marked_entries=2097152
42.4s: created 537919488 objects, live_mib=12320
111.6s: first GC done: finalized=537919488 inner_gc_done=true bad_magic=0
113.3s: final: finalized=537919488 expected=537919488 bad_magic=0
CLEAN

Checklist

Requirements for merging:

`gc_mark_stack` took the root count of a GC frame as a `uint32_t`, so an
encoded count (`n << 2`) of 2^32 or more wrapped and only the first
`n mod 2^30` roots of the frame were marked.

`jl_gc_run_finalizers_in_list` roots the whole list of pending finalizers
(two entries each) with a single frame. With 2^29 or more pending
finalizers, a GC that ran while they were still being processed therefore
freed most of the pending objects, and their finalizers then ran on freed
or reused memory (segfaults in `run_finalizer`, e.g., while dispatching on
a garbage type tag, or in `__gmpz_clear` freeing a garbage pointer).

Keep the root count at full width. The asserts that guarded the narrowing
were compiled out in release builds.
Comment thread src/gc.c
Comment on lines +2502 to +2505
// `nroots` must not be narrowed: `jl_gc_run_finalizers_in_list` roots the
// whole list of pending finalizers (two entries each) with a single frame,
// which can hold more than 2^30 roots, i.e. an encoded `nroots` that does not
// fit in 32 bits.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better remove these extra comments.

With the full root count, `gc_mark_stack` pushes every root of a frame
onto the mark queue before draining it. For the frame that roots a list of
2^29 or more pending finalizers, the queue has to grow past 2^30 entries,
and the doubling of its `int32_t` capacity overflows, so `malloc_s` aborts
with "Cannot allocate memory".

Mark direct frames with more than `GC_CHUNK_BATCH_SIZE` roots with
`gc_mark_finlist_`, which marks the global finalizer lists in batches and
pushes the remainder as a chunk, so that the mark queue stays bounded and
other GC threads can steal the work.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant