You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Thousands of threads run and coredump occurs in bitmap_set() in file bitmap.h, it happens several times in week, no memory problem in application, any ideas?
I traced this through the allocator path and can reproduce the logic that causes the abort.
Root cause: non-atomic read-modify-write in bitmap_set() with a missing lock assertion at the call boundary.
What bitmap_set() actually does
bitmap_set() in include/jemalloc/internal/bitmap.h performs a plain read-modify-write on every level of the bitmap tree — no atomics, no barriers:
gp=&bitmap[goff];
g=*gp; // READg ^= ZU(1) << ...; // MODIFY (thread-local)*gp=g; // WRITE BACK — plain store, no atomic, no barrier
This is intentional and correct as long as the caller holds bin->lock for the entire duration of bitmap_sfu() → bitmap_set(). That contract is documented nowhere and enforced nowhere at the function boundary.
The race
With thousands of threads:
Thread A calls bitmap_sfu(), traverses the tree, selects bit N as the first free bit.
Thread B does the same on the same slab bitmap and also selects bit N (it read the same group word before A wrote it back).
because the bit is already set — and abort() fires.
A second variant: if the two threads happen to share a group word (i.e. their bits are within the same BITMAP_GROUP_NBITS-wide word), the XOR writes race and one silently discards the other. On the next alloc that clobbered bit still appears free; bitmap_sfu() returns it again; the second bitmap_set() aborts on the tree-propagation assert:
bin_slab_reg_alloc() in src/bin.c is the function that calls bitmap_sfu(). It has nomalloc_mutex_assert_owner() call:
void*bin_slab_reg_alloc(edata_t*slab, constbin_info_t*bin_info) {
// ← no lock assertion here
...
regind=bitmap_sfu(slab_data->bitmap, &bin_info->bitmap_info);
Compare with every other function in the same file that requires the lock — bin_dalloc_slab_prepare (line 261), bin_refill_slabcur_no_fresh_slab (line 318) — all of which call malloc_mutex_assert_owner(tsdn, &bin->lock) on entry. bin_slab_reg_alloc() is the odd one out.
Notably, test/unit/bin.c also called bin_slab_reg_alloc() in 4 places without holding bin->lock — which is exactly the class of bug this fix is designed to catch. Those test call sites have been updated as part of the fix.
Fix
Thread tsdn_t *tsdn and bin_t *bin through bin_slab_reg_alloc() and add the assertion as its first statement:
Update all callers (2 in src/bin.c, 5 in test/unit/bin.c). bin_slab_reg_alloc_batch() is intentionally left alone — its caller in arena.c (arena_fill_small_fresh, line 1030) operates on a freshly allocated slab not yet visible to any other thread, so no lock is needed there.
A patch is up at PR #2883. make check passes 17/17 tests.
Activity
spredolac commentedon Apr 1, 2026
Can you provide some repro?
bin: enforce bin->lock ownership in bin_slab_reg_alloc()
LD-RW commentedon Apr 10, 2026
I traced this through the allocator path and can reproduce the logic that causes the abort.
Root cause: non-atomic read-modify-write in
bitmap_set()with a missing lock assertion at the call boundary.What
bitmap_set()actually doesbitmap_set()ininclude/jemalloc/internal/bitmap.hperforms a plain read-modify-write on every level of the bitmap tree — no atomics, no barriers:This is intentional and correct as long as the caller holds
bin->lockfor the entire duration ofbitmap_sfu() → bitmap_set(). That contract is documented nowhere and enforced nowhere at the function boundary.The race
With thousands of threads:
bitmap_sfu(), traverses the tree, selects bit N as the first free bit.bitmap_set()for bit N.because the bit is already set — and
abort()fires.A second variant: if the two threads happen to share a group word (i.e. their bits are within the same
BITMAP_GROUP_NBITS-wide word), the XOR writes race and one silently discards the other. On the next alloc that clobbered bit still appears free;bitmap_sfu()returns it again; the secondbitmap_set()aborts on the tree-propagation assert:Either way → coredump.
Where the gap is in the source
bin_slab_reg_alloc()insrc/bin.cis the function that callsbitmap_sfu(). It has nomalloc_mutex_assert_owner()call:Compare with every other function in the same file that requires the lock —
bin_dalloc_slab_prepare(line 261),bin_refill_slabcur_no_fresh_slab(line 318) — all of which callmalloc_mutex_assert_owner(tsdn, &bin->lock)on entry.bin_slab_reg_alloc()is the odd one out.Notably,
test/unit/bin.calso calledbin_slab_reg_alloc()in 4 places without holdingbin->lock— which is exactly the class of bug this fix is designed to catch. Those test call sites have been updated as part of the fix.Fix
Thread
tsdn_t *tsdnandbin_t *binthroughbin_slab_reg_alloc()and add the assertion as its first statement:Update all callers (2 in
src/bin.c, 5 intest/unit/bin.c).bin_slab_reg_alloc_batch()is intentionally left alone — its caller inarena.c(arena_fill_small_fresh, line 1030) operates on a freshly allocated slab not yet visible to any other thread, so no lock is needed there.A patch is up at PR #2883.
make checkpasses 17/17 tests.