Skip to content

Thread exiting calls mallocx and hits assert(dopts->arena_ind == ARENA_IND_AUTOMATIC) as reentrancy_level > 0 #2791

Open

Description

@jimwwalker

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/)

https://github.com/couchbasedeps/jemalloc/blob/a15528790026b0858d4c8a485730652579fb878e/src/jemalloc.c#L2534

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

  1. jimwwalker commented on Jan 27, 2025

    @jimwwalker
    Author

    Sorry tcache I is automatic (not none)

  2. jimwwalker commented on Jan 28, 2025

    @jimwwalker
    Author

    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.

    [jimw@33bc7b3b0166:build]$ uname -a
    Linux 33bc7b3b0166 6.10.11-linuxkit #1 SMP Thu Oct  3 10:17:28 UTC 2024 aarch64 aarch64 aarch64 GNU/Linux
    [jimw@33bc7b3b0166:build]$ /opt/gcc-13.2.0/bin/g++ /mnt/jimw/platform/tests/arena_malloc/test.cpp -o ./test -isystem /mnt/jimw/build/tlm/deps/jemalloc.exploded/include/ -ldl  tlm/deps/jemalloc.exploded/lib/libjemallocd.so -lpthread
    [jimw@33bc7b3b0166:build]$ LD_LIBRARY_PATH=tlm/deps/jemalloc.exploded/lib/ ./test
    <jemalloc>: src/jemalloc.c:2534: Failed assertion: "dopts->arena_ind == ARENA_IND_AUTOMATIC"
    Aborted (core dumped)
    
    (gdb) r
    Starting program: /mnt/couchbase/build/test
    [Thread debugging using libthread_db enabled]
    Using host libthread_db library "/lib64/libthread_db.so.1".
    [New Thread 0xffffb9bff780 (LWP 2361)]
    <jemalloc>: src/jemalloc.c:2534: Failed assertion: "dopts->arena_ind == ARENA_IND_AUTOMATIC"
    
    Thread 2 "test" received signal SIGABRT, Aborted.
    [Switching to Thread 0xffffb9bff780 (LWP 2361)]
    0x0000ffffba6af238 in raise () from /lib64/libc.so.6
    (gdb) bt
    #0  0x0000ffffba6af238 in raise () from /lib64/libc.so.6
    #1  0x0000ffffba6b08b0 in abort () from /lib64/libc.so.6
    #2  0x0000ffffbac19f84 in imalloc_body (sopts=sopts@entry=0xffffb9bfee20, dopts=dopts@entry=0xffffb9bfede8, tsd=tsd@entry=0xffffb9bffe90) at src/jemalloc.c:2534
    #3  0x0000ffffbac1a868 in imalloc (sopts=0xffffb9bfee20, dopts=0xffffb9bfede8) at src/jemalloc.c:2694
    #4  0x0000ffffbac1be70 in je_mallocx (size=8, flags=8388864) at src/jemalloc.c:3424
    #5  0x0000000000401f14 in Foo::~Foo() ()
    #6  0x00000000004021dc in std::default_delete<Foo>::operator()(Foo*) const ()
    #7  0x00000000004020bc in std::unique_ptr<Foo, std::default_delete<Foo> >::~unique_ptr() ()
    #8  0x0000ffffba8ae390 in (anonymous namespace)::run (p=<optimized out>) at /tmp/deploy/objdir/../gcc-13.2.0/libstdc++-v3/libsupc++/atexit_thread.cc:98
    #9  0x0000ffffbaf76c00 in __nptl_deallocate_tsd () from /lib64/libpthread.so.0
    #10 0x0000ffffbaf76d44 in start_thread () from /lib64/libpthread.so.0
    #11 0x0000ffffba759690 in thread_start () from /lib64/libc.so.6
    

    test.cpp -> https://gist.github.com/jimwwalker/b82fd50b119cd6091899c54dfcd8d6cd

  3. jimwwalker commented on Jan 28, 2025

    @jimwwalker
    Author

    Can confirm this issue occurs with a vanilla jemalloc 5.3.0 (source build)

    (gdb) bt
    #0  0x0000ffff836af238 in raise () from /lib64/libc.so.6
    #1  0x0000ffff836b08b0 in abort () from /lib64/libc.so.6
    #2  0x0000ffff83c2354c in imalloc_body (sopts=0xffff82bfedd8, dopts=0xffff82bfeda0, tsd=0xffff82bffe50) at src/jemalloc.c:2555
    #3  0x0000ffff83c23be0 in imalloc (sopts=0xffff82bfedd8, dopts=0xffff82bfeda0) at src/jemalloc.c:2716
    #4  0x0000ffff83c25a4c in je_mallocx (size=8, flags=1048832) at src/jemalloc.c:3363
    #5  0x0000000000401f14 in Foo::~Foo() ()
    #6  0x00000000004021dc in std::default_delete<Foo>::operator()(Foo*) const ()
    #7  0x00000000004020bc in std::unique_ptr<Foo, std::default_delete<Foo> >::~unique_ptr() ()
    #8  0x0000ffff838ae390 in (anonymous namespace)::run (p=<optimized out>) at /tmp/deploy/objdir/../gcc-13.2.0/libstdc++-v3/libsupc++/atexit_thread.cc:98
    #9  0x0000ffff84071c00 in __nptl_deallocate_tsd () from /lib64/libpthread.so.0
    #10 0x0000ffff84071d44 in start_thread () from /lib64/libpthread.so.0
    #11 0x0000ffff83759690 in thread_start () from /lib64/libc.so.6
    (gdb) up
    #1  0x0000ffff836b08b0 in abort () from /lib64/libc.so.6
    (gdb)
    #2  0x0000ffff83c2354c in imalloc_body (sopts=0xffff82bfedd8, dopts=0xffff82bfeda0, tsd=0xffff82bffe50) at src/jemalloc.c:2555
    2555			assert(dopts->arena_ind == ARENA_IND_AUTOMATIC);
    (gdb) info locals
    allocation = 0x0
    size = 8
    ind = 0
    usize = 8
    reentrancy_level = 1 '\001'
    (gdb) print *dopts
    $1 = {result = 0xffff82bfee00, usize = 8, num_items = 1, item_size = 8, alignment = 0, zero = false, tcache_ind = 4294967295, arena_ind = 0}
    
  4. interwq commented on Jan 28, 2025

    @interwq
    Contributor

    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:

    if (sopts->slow && unlikely(reentrancy_level > 0)) {

    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.

  5. jimwwalker commented on Jan 29, 2025

    @jimwwalker
    Author

    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.

  6. added a commit that references this issue on Jan 29, 2025
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