Skip to content

dev3: Debug build asserts 'mi_theap_is_initialized(theap)' in mi_page_theap — page theap is cleared before _mi_page_update_stats re-reads it #1402

Closed

Description

Summary

On dev3 at d184163c4 (v3.5.2), a Debug build aborts in mimalloc-test-stress on the assertion
mi_theap_is_initialized(theap) inside mi_page_theap() (include/mimalloc/internal.h:1032).

The cause looks like a clear-then-read: _mi_page_free() nulls the page's theap and then calls
_mi_arenas_page_free(), which re-derives the theap from that same page via the new
_mi_page_update_stats(). A NULL theap is expected on this path, and the consumers are written to
accept it — only the accessor's assertion refuses to return it.

This is distinct from #1398 (that one is mi_theap_malloc / _mi_theap_default() returning null
on a thread's first allocation). The fix for #1398 (8bf78206a) is already contained in the commit
tested here.

Repro

git clone https://github.com/microsoft/mimalloc && cd mimalloc
git checkout d184163c4
cmake -S . -B out/dbg -DCMAKE_BUILD_TYPE=Debug
cmake --build out/dbg -j8
./out/dbg/mimalloc-test-stress          # -> rc=134
mimalloc: assertion failed: at ".../include/mimalloc/internal.h":1032, mi_page_theap
  assertion: "mi_theap_is_initialized(theap)"

Reproduces 100% here. macOS 26.6.2, arm64 (Apple silicon), Apple clang 21.0.0. Nothing but stock
sources and the stock Debug preset.

Two independent call sites

1 — via _mi_page_free → the new stats hook (reached from a plain mi_free):

mi_page_theap                internal.h:1032
mi_theap_of_page             page.c:151
_mi_page_update_stats        page.c:278
_mi_arenas_page_free         arena.c:1316
_mi_page_free                page.c:591
_mi_page_retire              page.c:637
mi_free_block_local          free.c:51
mi_free                      free.c:299

src/page.c:589-591 is the clear-then-read:

mi_theap_t* theap = mi_page_theap(page); mi_assert_internal(theap!=NULL);
mi_page_set_theap(page,NULL);        // cleared here
_mi_arenas_page_free(page, theap);   // -> arena.c:1316 _mi_page_update_stats(page)

_mi_arenas_page_free is given the theap as current_theapx, but _mi_page_update_stats(page)
re-derives it from the page, which is now NULL.

2 — via mi_free_block_mt on an abandoned page (src/free.c:87):

const bool try_reclaim = (allow_reclaim && (counter==1 || theap==mi_page_theap(page))) || ...

Abandoned pages have their theap nulled deliberately
(src/arena.c: mi_page_set_theap(page,NULL); // set threadid to abandoned), so this reads NULL by
design. (Patching only call site 1 still aborts here — that is how I found it.)

Why this looks like the assertion being stale rather than a real NULL-deref

Every consumer on these paths already handles NULL:

  • mi_theap_page_merge_stats() — comment "get heap (as the theap might be NULL)", and it asserts
    theap == NULL || (...).
  • mi_page_update_sample_countdown() — mi_theap_t* theap = mi_theap_of_page(page); if (theap==NULL) return;
  • mi_theap_page_update_stats() — guards the sample countdown with if (theap!=NULL).
  • mi_free_block_mt() — uses the value only in the comparison theap==mi_page_theap(page), where
    NULL is meaningful and safe.

The sibling assertion on the very same accessor was already relaxed for what looks like the same
reason:

static inline mi_theap_t* mi_page_theap(const mi_page_t* page) {
  // mi_assert_internal(!mi_page_is_abandoned(page));     <-- already commented out
  mi_theap_t* theap = mi_atomic_load_ptr_relaxed(mi_theap_t,&page->xtheap);
  mi_assert_internal(mi_theap_is_initialized(theap));     <-- this one fires
  return theap;
}

Possible fix

Commenting out that one line makes the full Debug ctest suite pass 8/8 on d184163c4 here
(from mimalloc-test-stress aborting at rc=134):

 static inline mi_theap_t* mi_page_theap(const mi_page_t* page) {
   // mi_assert_internal(!mi_page_is_abandoned(page));
   mi_theap_t* theap = mi_atomic_load_ptr_relaxed(mi_theap_t,&page->xtheap);
-  mi_assert_internal(mi_theap_is_initialized(theap));
   return theap;
 }

That is the minimal change, but it may not be the one you want: if a non-NULL theap really is an
invariant for some callers, the alternative is to keep the assertion on the accessor and have the
two paths above read page->xtheap directly (or have _mi_page_update_stats take the theap as a
parameter, since _mi_arenas_page_free already has the correct one in hand). I'm happy to send
whichever you prefer as a PR.

Given that d184163c4 is "make page->theap atomic so we can read it unowned", I assume this area
is mid-refactor and you may already have a direction in mind.

Activity

  1. sagaceilo commented on Sep 15, 2026

    @sagaceilo

    Getting same issue ( dev3 ).
    Callstack:

    mi_page_theap(const mi_page_s * page) Line 1032 C++
    mi_theap_of_page(mi_page_s * page) Line 151 C++
    mi_page_update_sample_countdown(mi_page_s * page) Line 299 C++
    _mi_page_free_collect(mi_page_s * page, bool force) Line 413 C++
    _mi_arenas_page_free(mi_page_s * page, mi_theap_s * current_theapx) Line 1316 C++
    _mi_page_free(mi_page_s * page, mi_page_queue_s * pq) Line 591 C++
    mi_page_try_retire(mi_page_queue_s * pq, mi_page_s * page, unsigned __int64 bin, bool force, unsigned __int64 * min, unsigned __int64 * max) Line 667 C++
    _mi_theap_collect_retired(mi_theap_s * theap, bool force) Line 691 C++
    mi_page_queue_find_free_ex(mi_theap_s * theap, mi_page_queue_s * pq, bool first_try) Line 1039 C++
    mi_page_queue_find_free(mi_theap_s * theap, mi_page_queue_s * pq) Line 1093 C++
    mi_find_page(mi_theap_s * theap, unsigned __int64 size, unsigned __int64 huge_alignment) Line 1149 C++
    mi_malloc_generic_fallback(mi_theap_s * theap, unsigned __int64 size, bool zero, unsigned __int64 huge_alignment, mi_page_s * * ppage) Line 1285 C++
    _mi_malloc_generic(mi_theap_s * theap, unsigned __int64 size, unsigned __int64 zero_huge_alignment, mi_page_s * * ppage) Line 1357 C++
    mi_xtheap_malloc_zero_aligned_at_overalloc(mi_theap_s * const xtheap, const unsigned __int64 size, const unsigned __int64 alignment, const unsigned __int64 offset, const bool zero, mi_page_s * * ppage) Line 81 C++
    mi_xtheap_malloc_zero_aligned_at_generic(mi_theap_s * const xtheap, const unsigned __int64 size, const unsigned __int64 alignment, const unsigned __int64 offset, const bool zero, mi_page_s * * ppage) Line 176 C++
    mi_xtheap_malloc_zero_aligned_at(mi_theap_s * const xtheap, const unsigned __int64 size, const unsigned __int64 alignment, const unsigned __int64 offset, const bool zero, mi_page_s * * ppage) Line 226 C++
    mi_theap_malloc_aligned_at(mi_theap_s * xtheap, unsigned __int64 size, unsigned __int64 alignment, unsigned __int64 offset) Line 235 C++
    mi_theap_malloc_aligned(mi_theap_s * xtheap, unsigned __int64 size, unsigned __int64 alignment) Line 239 C++
    mi_malloc_aligned(unsigned __int64 size, unsigned __int64 alignment) Line 270 C++

  2. daanx commented on Sep 15, 2026

    @daanx
    Collaborator

    Fixed now (by reverting the last commit)

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