Skip to content

malloc_tsd_dalloc called before C++ thread_local callbacks #2825

Open

Description

@veselink1

On Windows, when using dynamic linking, I can see that the DLL_THREAD_DETACH events are received by jemalloc before the C++ thread_local destructors run.

During that DLL_THREAD_DETACH processing, the tsd_wrapper is deallocated using malloc_tsd_dalloc. Then calls into jemalloc from C++ thread_local destructors will use the dangling pointer to the tsd_wrapper, which is still accessible via TlsGetValue.

This will lead to hard to track down corruption, since in some cases, the tsd_wrapper memory is not clobbered by further allocations, so the state is still readable.

However, with the right setup, I've managed to reproduce it using:

#include <thread>
#include <functional>
#include <jemalloc/jemalloc.h>

struct ScopeGuard {
    std::function<void()> function;
    ~ScopeGuard() {
        if (function) {
            function();
        }
    }
};

int main(int argc, char** argv) {
    std::thread t([]() {
        void* ptr = je_mallocx(8, 0); // set up tsd
        je_dallocx(ptr, 0);
        // Run after jemalloc malloc_tsd_dalloc
        thread_local ScopeGuard guard{[] {
            void* ptr = je_mallocx(2688, 0); // << ALLOCATES OVER THE TSD
            memset(ptr, /* tsd_state_reincarnated */ 0x05, 2688);
            je_dallocx(ptr, 0); // << CRASH HERE
        }};
    });
    t.join();
}

In my testing, the jemalloc tsd_t is freed before the thread_local destructor runs. Then, the je_mallocx(2688, ...) call will recycle the same memory by calling down into extent_recycle_extract.

At this point, you cannot really tell that anything is wrong, but when we free, the tsd state is now corrupted.

If you use junk filling, and assertions are enabled, an assertion will fire on the state. If you don't use junk filling, corruption happens later on.

Some logs from jemalloc build with extra logging, hopefully readable:

jemalloc: Thread=13752 registering TSD cleanup function 00007FF87CC06CA0
Thread=13752 tsd_wrapper_set=00007FF87CC1F180
Thread=13752 tsd_wrapper_get=00007FF87CC1F180
Thread=13752 tsd_wrapper_get=00007FF87CC1F180
Thread=13752 last_event=0, next_event=65536, interval=65536, current_bytes=0
Thread=13752 last_event=0, next_event=65536, interval=65536, current_bytes=0
Thread=13752 malloc_tsd_malloc=00000208CE480000
Thread=13752 tsd_wrapper_get=00007FF87CC1F180
Thread=13752 tsd_wrapper_set=00000208CE480000
Thread=13752 tsd_wrapper_get=00000208CE480000
Thread=13752 tsd_wrapper_get=00000208CE480000
Thread=13752 last_event=0, next_event=65536, interval=65536, current_bytes=0
Thread=13752 last_event=0, next_event=65536, interval=65536, current_bytes=0
Thread=13752 tsd_wrapper_get=00000208CE480000
Thread=13752 tsd_wrapper_get=00000208CE480000
Thread=13752 last_event=0, next_event=65536, interval=65536, current_bytes=0
Thread=13752 last_event=0, next_event=65536, interval=65536, current_bytes=0
Thread=13752 last_event=0, next_event=65536, interval=65536, current_bytes=8
Thread=13752 last_event=0, next_event=65536, interval=65536, current_bytes=0
Thread=13752 tsd_wrapper_get=00000208CE480000
Thread=13752 last_event=0, next_event=65536, interval=65536, current_bytes=8
Thread=13752 last_event=0, next_event=65536, interval=65536, current_bytes=0
Thread=13752 last_event=0, next_event=65536, interval=65536, current_bytes=8
Thread=13752 last_event=0, next_event=65536, interval=65536, current_bytes=8

jemalloc: Thread=13752 received THREAD_DETACH

jemalloc: Thread=13752 calling TSD cleanup function 00007FF87CC06CA0
Thread=13752 tsd_cleanup_wrapper=00000208CE480000
Thread=13752 tsd_wrapper_get=00000208CE480000

jemalloc: Thread=13752 calling TSD cleanup function 00007FF87CC06CA0
Thread=13752 tsd_cleanup_wrapper=00000208CE480000
Thread=13752 malloc_tsd_dalloc=00000208CE480000
Thread=13752 tsd_wrapper_get=00000208CE480000
Thread=13752 tsd_wrapper_get=00000208CE480000
Thread=13752 last_event=8, next_event=65544, interval=65536, current_bytes=8
Thread=13752 last_event=8, next_event=65544, interval=65536, current_bytes=8
Thread=13752 last_event=8, next_event=65544, interval=65536, current_bytes=8
Thread=13752 last_event=8, next_event=65544, interval=65536, current_bytes=8
Thread=13752 last_event=8, next_event=65544, interval=65536, current_bytes=3080
Thread=13752 last_event=8, next_event=65544, interval=65536, current_bytes=8
<jemalloc>: c:\users\administrator\desktop\jemalloc\include\jemalloc\internal\tsd.h:344: Failed assertion: "state == tsd_state_nominal || state == tsd_state_nominal_slow || state == tsd_state_nominal_recompute || state == tsd_state_reincarnated || state == tsd_state_minimal_initialized"

This seems like a very easy to hit issue, so I'm second guessing myself. Is there something that needs to be configured differently to order the jemalloc DLL_THREAD_DETACH after the C++ thread_locals are destroyed?

Activity

  1. veselink1 commented on Apr 10, 2025

    @veselink1
    Author

    In lieu of an upstream fix, we've been considering fixing up the tsd_cleanup_wrapper on Windows by clearing the tsd_tsd TLS key. This avoids the use of a dangling pointer, since jemalloc will re-create the tsd_wrapper_t on access later, but it also leaks that re-created wrapper, since there is no additional thread cleanup callback.

    In our case, it is fairly easy to determine when the C++ thread has exited and will no longer call into jemalloc, so we can manually clean up jemalloc's resources by calling _malloc_thread_cleanup manually (veselink1@fc0eda6).

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