You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Reproducible crash in realloc with libcurl and libopenssl #1304
The following code will always crash in realloc().
Specifically, in _mi_theap_realloc_zero() on this line:
voidcurl_get_url()
{
if (constauto curl = curl_easy_init()) {
curl_easy_setopt(curl, CURLOPT_URL, "https://example.com");
curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
}
voidmain()
{
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.
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?
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).
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!
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.
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>staticvoid*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).staticvoid*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 -> crashfprintf(stderr, "[worker] mi_realloc returned %p (no crash)\n", q);
fflush(stderr);
returnq;
}
staticvoid*worker_malloc_first(void*arg) {
(void)arg;
void*a=mi_malloc(16); // initializes this thread's heapvoid*q=mi_realloc(shared_ptr, 48); // now safefprintf(stderr, "[worker] malloc-first ok: a=%p realloc=%p\n", a, q);
fflush(stderr);
returnq;
}
intmain(intargc, char**argv) {
shared_ptr=mi_malloc(64); // valid mimalloc pointer from the main threadfprintf(stderr, "[main] shared_ptr = %p\n", shared_ptr);
fflush(stderr);
intrealloc_first= (argc>1&&strcmp(argv[1], "realloc-first") ==0);
pthread_tt;
pthread_create(&t, NULL, realloc_first ? worker_realloc_first : worker_malloc_first, NULL);
pthread_join(t, NULL);
fprintf(stderr, "[main] done\n");
return0;
}
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)
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.
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)
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.
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.
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.
Activity
will-extrahop commentedon Jun 4, 2026
https://linux.die.net/man/3/curl_easy_init says:
Am I missing something or is that likely the problem?
HSNB commentedon Jun 4, 2026
New observations:
MI_GUARDED=1from the Preprocessor Definitions (which is there by default in themimalloc-override-static-libproject that comes in themimalloc.sln).MI_DEBUG=1(alongsideMI_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!
HSNB commentedon Jun 5, 2026
Ah my bad I left out an initializing function that calls the curl init function in the code piece. I've added it now.
dimitarcl commentedon Jun 17, 2026
We are hitting the same or similar crash in realloc in
mimalloc/src/alloc.c
Line 400 in 30b2d9d
.
Using version 3.3.2.
Reproduction rate is roughly 1 in 20.
In two of the reproductions:
copysizeis 69552, whilememcpyfails to read an address that is at offset 65456 fromp.copysizeis 640, whilememcpyfails to read an address that is at offset 608 frompgbaraldi commentedon Jun 18, 2026
We found this while trying in Julia. The robot minimized it
fix NULL dereference in realloc, see issue #1304 and #1312
daanx commentedon Jun 18, 2026
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)
HSNB commentedon Jun 19, 2026
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
theapis not NULL when the crash occurs.daanx commentedon Jun 19, 2026
I will keep looking; just to confirm: the bug only occurs when
MI_GUARDED=1is defined?Dimitar Trendafilov (@dimitarcl): is this also the case for you? (that
MI_GUARDED=1is defined?).HSNB commentedon Jun 19, 2026
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)
daanx commentedon Jun 19, 2026
Thanks! I am able to reproduce it now on my system -- I can see the size is off (when using MI_GUARDED);
HSNB commentedon Jun 19, 2026
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)
fix incorrect calculation of block size in release builds with MI_GUA…
daanx commentedon Jun 19, 2026
I think I found the issue and pushed a fix; let me know if it works for you as well.
HSNB commentedon Jun 19, 2026
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
daanx commentedon Jun 19, 2026
Great -- thanks for the report. Strange we didn't catch it earlier :-( .. but glad this is fixed.
HSNB commentedon Jun 19, 2026
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.
daanx commentedon Jun 19, 2026
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.
HSNB commentedon Jun 19, 2026
Sure, thanks :)
HSNB commentedon Jun 19, 2026
fwiw I checked if doing it single threaded crashes and it turns out it does:
add guarded test, issue #1304
HSNB commentedon Jul 8, 2026
MI_GUARDED=1 is still set in the mimalloc-override-static-lib project, I assume you forgot that one.
daanx commentedon Jul 8, 2026
Thanks! I forgot!