Repository navigation
Description
We've been able to hit an assert in jemalloc from our allocator wrapper that uses the mallocx API. This was noticed recently as noticed some tests weren't running with the correct debug libraries.
The assert suggests an internal allocation is being performed, yet the backtrace goes to our mallocx call.
I'll post to this issue the backtrace and some background as to what our code is doing, we don't have much more intel at this point.
jemalloc asserted here (note we have a clone of 5.3 with 3 changes https://github.com/couchbasedeps/jemalloc/commits/5.3.0-couchbase/)
The code block there uses reentrancy_level to detect an internal allocation and then asserts that automatic arena selection is in use, it is not.
Our backtrace though shows that our allocation wrapper is calling mallocx (so not internal) and our wrapper is there to direct calls to explicit arenas - hence why automatic arena selection is not in use.
Here's the crash backtrace, which is happening in the thread exit path. The backtrace shows folly doing an allocation and new replacement directs the allocation through our wrapper but then entering the code block guarded by reentrancy_level > 0 and asserting.
(gdb) bt
#0 0x000072177ea32387 in raise () from /lib64/libc.so.6
#1 0x000072177ea33a78 in abort () from /lib64/libc.so.6
#2 0x000072177f36bd65 in imalloc_body (sopts=sopts@entry=0x7217757f5380, dopts=dopts@entry=0x7217757f5340, tsd=tsd@entry=0x7217757f5fc0) at src/jemalloc.c:2534
#3 0x000072177f36c605 in imalloc (sopts=0x7217757f5380, dopts=0x7217757f5340) at src/jemalloc.c:2694
#4 0x000072177f36e096 in je_mallocx (size=4, flags=8388864) at src/jemalloc.c:3424
#5 0x00000000023953a4 in cb::_JEArenaMalloc<cb::JEArenaSimpleTracker>::malloc (size=4) at /home/compile/trunk/platform/src/je_arena_malloc.cc:373
#6 0x00000000005881cd in cb::_ArenaMalloc<cb::_JEArenaMalloc<cb::JEArenaSimpleTracker> >::malloc (size=4) at /home/compile/trunk/platform/include/platform/cb_arena_malloc.h:219
#7 0x0000000000587f09 in cb_malloc (size=4) at /home/compile/trunk/platform/src/cb_malloc_arena.cc:39
#8 0x00000000005882cd in operator new (count=4) at /home/compile/trunk/platform/src/global_new_replacement.cc:52
#9 0x000000000245f40b in folly::detail::thunk::make<folly::detail::(anonymous namespace)::thread_is_dying_global> () at /home/compile/trunk/tlm/deps/packages/build/folly/folly.debug-prefix/src/folly.debug/folly/lang/Thunk.h:43
#10 0x0000000002455b44 in folly::detail::(anonymous namespace)::StaticSingletonManagerWithRttiImpl::Entry::create (this=0x72177e9d9400, make=@0x245f3f4: {void *(void)} 0x245f3f4 <folly::detail::thunk::make<folly::detail::(anonymous namespace)::thread_is_dying_global>()>, debug=@0x38eb0e0: 0x0) at /home/compile/trunk/tlm/deps/packages/build/folly/folly.debug-prefix/src/folly.debug/folly/detail/StaticSingletonManager.cpp:74
#11 0x000000000245635a in folly::detail::(anonymous namespace)::StaticSingletonManagerWithRttiImpl::create<folly::detail::StaticSingletonManagerWithRtti::Arg> (arg=...) at /home/compile/trunk/tlm/deps/packages/build/folly/folly.debug-prefix/src/folly.debug/folly/detail/StaticSingletonManager.cpp:54
#12 0x000000000245754e in folly::detail::StaticSingletonManagerWithRtti::create_<false> (arg=...) at /home/compile/trunk/tlm/deps/packages/build/folly/folly.debug-prefix/src/folly.debug/folly/detail/StaticSingletonManager.cpp:112
#13 0x000000000245f3b2 in folly::detail::StaticSingletonManagerWithRtti::create<folly::detail::(anonymous namespace)::thread_is_dying_global, false> (arg=...) at /home/compile/trunk/tlm/deps/packages/build/folly/folly.debug-prefix/src/folly.debug/folly/detail/StaticSingletonManager.h:267
#14 folly::detail::StaticSingletonManagerWithRtti::create<folly::detail::(anonymous namespace)::thread_is_dying_global, void> () at /home/compile/trunk/tlm/deps/packages/build/folly/folly.debug-prefix/src/folly.debug/folly/detail/StaticSingletonManager.h:236
#15 folly::detail::createGlobal<folly::detail::(anonymous namespace)::thread_is_dying_global, void> () at /home/compile/trunk/tlm/deps/packages/build/folly/folly.debug-prefix/src/folly.debug/folly/detail/StaticSingletonManager.h:294
#16 folly::detail::thread_is_dying_mark () at /home/compile/trunk/tlm/deps/packages/build/folly/folly.debug-prefix/src/folly.debug/folly/detail/thread_local_globals.cpp:63
#17 0x00000000024580de in folly::threadlocal_detail::StaticMetaBase::onThreadExit (ptr=0x72177be72080) at /home/compile/trunk/tlm/deps/packages/build/folly/folly.debug-prefix/src/folly.debug/folly/detail/ThreadLocalDetail.cpp:148
#18 0x000072177fd79ca2 in __nptl_deallocate_tsd () from /lib64/libpthread.so.0
#19 0x000072177fd79eb3 in start_thread () from /lib64/libpthread.so.0
#20 0x000072177eafab0d in clone () from /lib64/libc.so.6
Our je_mallocx call passed flags=8388864 which decode to MALLOCX_TCACHE_NONE | MALLOCX_ARENA(0)
/*
* Bias tcache index bits so that 0 encodes "automatic tcache management", and 1
* encodes MALLOCX_TCACHE_NONE.
*/
#define MALLOCX_TCACHE(tc) ((int)(((tc)+2) << 8))
#define MALLOCX_TCACHE_NONE MALLOCX_TCACHE(-1)
/*
* Bias arena index bits so that 0 encodes "use an automatically chosen arena".
*/
#define MALLOCX_ARENA(a) ((((int)(a))+1) << 23)
Other tunings here are the following options
narenas:1,prof:true,prof_active:false
Activity
jimwwalker commentedon Jan 27, 2025
Sorry tcache I is automatic (not none)
jimwwalker commentedon Jan 28, 2025
Reproduced outside of our build system with a much more focused test.
The test has a thread_local unique_ptr which will be destructed on shutdown, the reproduction also required new/delete replacement enabling, although the memory allocation in the back-trace is from a direct je_mallocx call.
test.cpp -> https://gist.github.com/jimwwalker/b82fd50b119cd6091899c54dfcd8d6cd
jimwwalker commentedon Jan 28, 2025
Can confirm this issue occurs with a vanilla jemalloc 5.3.0 (source build)
interwq commentedon Jan 28, 2025
We might be too restrictive with that assertion about assuming it must be arena 0. In fact any already initialized arena would work. If you try remove this block:
jemalloc/src/jemalloc.c
Line 2548 in 20cc983
It likely will work just fine.
However I'd also suggest followup with the mallocx usage in the dtor -- is the mallocx necessary, especially if the dtor may happen on thread destruction. The reason we have so many assertions (some maybe be overly careful) on edge cases like the shutdown path, is because we have had a number of tricky issues. Doing the simple thing in these cases might be the better fix here.
jimwwalker commentedon Jan 29, 2025
Thanks for the reply.
We are patching our jemalloc for now to tolerate the input arena of 0 (we can see soon after the assert, the code would select 0).
As for removing the malloc in dtor - in the original crash the mallocx occurs in the thread shutdown as another library (folly) is allocating memory (and new replacement pushes allocation to mallocx) - that's a much trickier issue for us to avoid as we don't control that code or fully understand what we could change in there.
Allow arena 0 to be specified explicitly
Allow arena 0 to be specified explicitly