Skip to content

Reproducible crash in realloc with libcurl and libopenssl #1304

Closed

Description

@HSNB

The following code will always crash in realloc().

Specifically, in _mi_theap_realloc_zero() on this line:

Image
void curl_get_url()
{
	if (const auto curl = curl_easy_init()) {
		curl_easy_setopt(curl, CURLOPT_URL, "https://example.com");
		curl_easy_perform(curl);
		curl_easy_cleanup(curl);
	}
}

void main()
{
	curl_global_init(CURL_GLOBAL_ALL);

	std::list<std::future<void>> futures;

	for (int i = 0; i < 1024; ++i)
		futures.emplace_back(std::async(std::launch::async, curl_get_url));

	futures.clear();

	curl_global_cleanup();
}

The crash may occur inside OpenSSL, but sometimes also in libcurl. It's always crashing in this realloc function on this specific line in mimalloc though. Which makes me think it's mimallocs fault. The code does not crash without mimalloc.

Environment:

  • Visual Studio 2026
  • mimalloc version 3.3.2 static library (mimalloc-override-static-lib project in the given mimalloc.sln). linking to mimalloc-static.lib. Retargeted the project to v145 Platform Toolset on first open.
  • /MT build
  • mimalloc-static.lib is the first in the list of "Additional Dependencies"
  • mi_version is specified in "Force Symbol References"
  • mi_malloc is linked correctly, I've tested with mi_is_in_heap_region()
  • curl: static library (/MT)
  • openssl: static library (/MT)

From my observations while trying to understand why it's crashing, it seems mimalloc's realloc is not handling (expired?) threads well.

I checked some other issues and tried the dev3 branch. It also crashes.

Activity

  1. will-extrahop commented on Jun 4, 2026

    @will-extrahop

    https://linux.die.net/man/3/curl_easy_init says:

    If you did not already call curl_global_init(3), curl_easy_init(3) does it automatically. This may be lethal in multi-threaded cases, since curl_global_init(3) is not thread-safe, and it may result in resource problems because there is no corresponding cleanup.

    You are strongly advised to not allow this automatic behaviour, by calling curl_global_init(3) yourself properly. See the description in libcurl(3) of global environment requirements for details of how to use this function.

    Am I missing something or is that likely the problem?

  2. HSNB commented on Jun 4, 2026

    @HSNB
    Author

    New observations:

    1. No crash when I remove MI_GUARDED=1 from the Preprocessor Definitions (which is there by default in the mimalloc-override-static-lib project that comes in the mimalloc.sln).
    2. No crash when I add MI_DEBUG=1 (alongside MI_GUARDED=1).

    (at least it doesn't crash immediately like it does with default settings. I haven't tested longer-duration processes)

    So maybe there are bugs in the latest version of libcurl and openssl instead? AI says MI_GUARDED is to detect use-after-free and writing outside bounds.. so that could mean it's actually libcurl AND openssl having such bugs. Crazy!

  3. HSNB commented on Jun 5, 2026

    @HSNB
    Author

    https://linux.die.net/man/3/curl_easy_init says:

    If you did not already call curl_global_init(3), curl_easy_init(3) does it automatically. This may be lethal in multi-threaded cases, since curl_global_init(3) is not thread-safe, and it may result in resource problems because there is no corresponding cleanup.
    You are strongly advised to not allow this automatic behaviour, by calling curl_global_init(3) yourself properly. See the description in libcurl(3) of global environment requirements for details of how to use this function.

    Am I missing something or is that likely the problem?

    Ah my bad I left out an initializing function that calls the curl init function in the code piece. I've added it now.

  4. dimitarcl commented on Jun 17, 2026

    @dimitarcl

    We are hitting the same or similar crash in realloc in

    _mi_memcpy(newp, p, copysize);

    .
    Using version 3.3.2.
    Reproduction rate is roughly 1 in 20.

    In two of the reproductions:

    1. copysize is 69552, while memcpy fails to read an address that is at offset 65456 from p.
    2. copysize is 640, while memcpy fails to read an address that is at offset 608 from p
  5. gbaraldi commented on Jun 18, 2026

    @gbaraldi

    We found this while trying in Julia. The robot minimized it

    // Reproducer for mimalloc 3.x crash: mi_realloc as the FIRST mimalloc call on a
    // fresh thread dereferences a NULL thread-heap (theap), while mi_malloc would
    // have lazily initialized it. On macOS the fixed-slot TLS model sets
    // MI_THEAP_INITASNULL, so _mi_theap_default() returns NULL on an uninitialized
    // thread; the malloc path tolerates that (routes to the generic init path), but
    // _mi_theap_realloc_zero() does not and crashes in _mi_theap_heap(NULL).
    //
    // Mirrors Julia's @threadcall(:jl_malloc) ; @threadcall(:jl_realloc) test:
    // the buffer is allocated on one thread and realloc'd on a fresh worker thread.
    //
    //   ./mim_repro malloc-first   -> worker inits via mi_malloc, then realloc: OK
    //   ./mim_repro realloc-first  -> worker's first call is mi_realloc: CRASH
    
    #include <stdio.h>
    #include <string.h>
    #include <pthread.h>
    #include <mimalloc.h>
    
    static void *shared_ptr;
    
    // The realloc must FIT within the original usable block (newsize <= usable &&
    // newsize >= usable/2) so that mimalloc takes the in-place fast path, which is
    // the branch that evaluates `_mi_theap_heap(theap)` and dereferences the NULL
    // theap. A growing realloc would instead skip to mi_theap_umalloc (no crash).
    static void *worker_realloc_first(void *arg) {
        (void)arg;
        fprintf(stderr, "[worker] first mimalloc call on this thread is mi_realloc(%p, fit)...\n", shared_ptr);
        fflush(stderr);
        void *q = mi_realloc(shared_ptr, 48); // 48 <= usable(64), >= 32 -> in-place -> deref NULL theap -> crash
        fprintf(stderr, "[worker] mi_realloc returned %p (no crash)\n", q);
        fflush(stderr);
        return q;
    }
    
    static void *worker_malloc_first(void *arg) {
        (void)arg;
        void *a = mi_malloc(16);              // initializes this thread's heap
        void *q = mi_realloc(shared_ptr, 48); // now safe
        fprintf(stderr, "[worker] malloc-first ok: a=%p realloc=%p\n", a, q);
        fflush(stderr);
        return q;
    }
    
    int main(int argc, char **argv) {
        shared_ptr = mi_malloc(64);           // valid mimalloc pointer from the main thread
        fprintf(stderr, "[main] shared_ptr = %p\n", shared_ptr);
        fflush(stderr);
    
        int realloc_first = (argc > 1 && strcmp(argv[1], "realloc-first") == 0);
    
        pthread_t t;
        pthread_create(&t, NULL, realloc_first ? worker_realloc_first : worker_malloc_first, NULL);
        pthread_join(t, NULL);
    
        fprintf(stderr, "[main] done\n");
        return 0;
    }
  6. daanx commented on Jun 18, 2026

    @daanx
    Collaborator

    Ah, this is indeed a bug in mimalloc :-( Thank you for the tests and report -- and thank you for the PR Gabriel Baraldi (@gbaraldi) ! I pushed a fix based on your PR. I think this will fix indeed the crash for libcurl and libopenssl as well. Let me know.
    (the plan is to do a fresh release early next week)

  7. HSNB commented on Jun 19, 2026

    @HSNB
    Author

    Unfortunately, the crash persists using the latest code from the dev3 branch.

    The code bit provided by Gabriel Baraldi (@gbaraldi) does not fix the issue either, and causes compiler warnings due to the #if placement (I did the test by just removing the #if statement).

    Note that theap is not NULL when the crash occurs.

  8. daanx commented on Jun 19, 2026

    @daanx
    Collaborator

    I will keep looking; just to confirm: the bug only occurs when MI_GUARDED=1 is defined?

    We are hitting the same or similar crash in realloc in

    Dimitar Trendafilov (@dimitarcl): is this also the case for you? (that MI_GUARDED=1 is defined?).

  9. HSNB commented on Jun 19, 2026

    @HSNB
    Author

    Yes when I remove MI_GUARDED=1 then there's no crash.

    I've found an easier code path to make the crash happen: (though it seems to be a less reliable way to force a crash)

    void *shared_ptr = nullptr;
    std::mutex shared_ptr_mutex;
    
    void thread_realloc(int i)
    {
    	std::unique_lock lock(shared_ptr_mutex);
    	shared_ptr = mi_realloc(shared_ptr, 64 * i);
    }
    
    void main()
    {
    	std::vector<std::jthread> threads;
    
    	for (int iterations = 0; iterations < 64; ++iterations)
    	{
    		for (int i = 0; i < 1024; ++i )
    			threads.emplace_back(std::jthread(std::bind_front(thread_realloc, i + 1)));
    
    		threads.clear();
    	}
    }
  10. daanx commented on Jun 19, 2026

    @daanx
    Collaborator

    Thanks! I am able to reproduce it now on my system -- I can see the size is off (when using MI_GUARDED);

  11. HSNB commented on Jun 19, 2026

    @HSNB
    Author

    I've edited my previous comment's code to get a more reliable crash. (I can't get it to crash 100% of the times with this new code as opposed to the original post's code which does crash 100% of the time)

  12. daanx commented on Jun 19, 2026

    @daanx
    Collaborator

    I think I found the issue and pushed a fix; let me know if it works for you as well.

  13. HSNB commented on Jun 19, 2026

    @HSNB
    Author

    Yes it seems to be fixed :) Original post's code no longer crashes. Hope I'm not flagged for DoS'ing the example.com website :p

  14. daanx commented on Jun 19, 2026

    @daanx
    Collaborator

    Great -- thanks for the report. Strange we didn't catch it earlier :-( .. but glad this is fixed.

  15. HSNB commented on Jun 19, 2026

    @HSNB
    Author

    Now that we're here, I'm wondering if MI_GUARDED should be on in Release?

    mimalloc's Readme says it's on by default in the Debug build (which is true, via the MI_DEBUG macro). But the configuration for the given override-static-lib project has it on by default for the Release build, so I'm guessing that shouldn't be the case if the Readme explicitly says "Debug build" but doesn't mention the Release build.

  16. daanx commented on Jun 19, 2026

    @daanx
    Collaborator

    Yes, the MI_GUARDED=1 in the visual studio project should not be defined in release mode. I'll fix that too. I am also going to update the test workflow to include MI_GUARDED in release modes though, and if you are ok, maybe add a modified version of your simplified repro code.

  17. HSNB commented on Jun 19, 2026

    @HSNB
    Author

    Sure, thanks :)

  18. HSNB commented on Jun 19, 2026

    @HSNB
    Author

    fwiw I checked if doing it single threaded crashes and it turns out it does:

    void main()
    {
    	void* shared_ptr = nullptr;
    
    	for (int iterations = 0; iterations < 64; ++iterations) {
    		for (int i = 0; i < 1024; ++i) {
    			shared_ptr = mi_realloc(shared_ptr, i * 64);
    		}
    	}
    }
  19. added a commit that references this issue on Jul 8, 2026
  20. HSNB commented on Jul 8, 2026

    @HSNB
    Author

    Yes, the MI_GUARDED=1 in the visual studio project should not be defined in release mode. I'll fix that too. I am also going to update the test workflow to include MI_GUARDED in release modes though, and if you are ok, maybe add a modified version of your simplified repro code.

    MI_GUARDED=1 is still set in the mimalloc-override-static-lib project, I assume you forgot that one.

  21. daanx commented on Jul 8, 2026

    @daanx
    Collaborator

    Thanks! I forgot!

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