Skip to content

coredump in function bitmap_set in bitmap.h #2875

Open

Description

@ChrisZcu

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?

Activity

  1. spredolac commented on Apr 1, 2026

    @spredolac
    Contributor

    Can you provide some repro?

  2. LD-RW commented on Apr 10, 2026

    @LD-RW

    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;              // READ
    g ^= 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:

    1. Thread A calls bitmap_sfu(), traverses the tree, selects bit N as the first free bit.
    2. 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).
    3. Both call bitmap_set() for bit N.
    4. Thread A writes first. Thread B then hits:
    assert(!bitmap_get(bitmap, binfo, bit));   // bitmap.h:220

    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:

    assert(g & (ZU(1) << (bit & BITMAP_GROUP_NBITS_MASK)));  // bitmap.h:237

    Either way → coredump.


    Where the gap is in the source

    bin_slab_reg_alloc() in src/bin.c is the function that calls bitmap_sfu(). It has no malloc_mutex_assert_owner() call:

    void *
    bin_slab_reg_alloc(edata_t *slab, const bin_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:

    void *
    bin_slab_reg_alloc(tsdn_t *tsdn, bin_t *bin,
        edata_t *slab, const bin_info_t *bin_info) {
        malloc_mutex_assert_owner(tsdn, &bin->lock);  // ← enforce contract
        ...

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions