Repository navigation
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=134mimalloc: 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 withif (theap!=NULL).mi_free_block_mt()— uses the value only in the comparisontheap==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
sagaceilo commentedon Sep 15, 2026
Getting same issue (
dev3).Callstack:
daanx commentedon Sep 15, 2026
Fixed now (by reverting the last commit)