add thread-id check for thread local FLS callbacks on Windows with static linking; found by @jasongibson

This commit is contained in:
daan 2020-05-04 14:31:32 -07:00
parent fd9faee5d4
commit 7c24edfeb0
2 changed files with 66 additions and 28 deletions

View file

@ -216,23 +216,25 @@ static bool _mi_heap_done(mi_heap_t* heap) {
heap = heap->tld->heap_backing; heap = heap->tld->heap_backing;
if (!mi_heap_is_initialized(heap)) return false; if (!mi_heap_is_initialized(heap)) return false;
// check thread-id as on Windows shutdown with FLS the main (exit) thread may call this on thread-local heaps
// delete all non-backing heaps in this thread if (heap->thread_id == _mi_thread_id()) {
mi_heap_t* curr = heap->tld->heaps; // delete all non-backing heaps in this thread
while (curr != NULL) { mi_heap_t* curr = heap->tld->heaps;
mi_heap_t* next = curr->next; // save `next` as `curr` will be freed while (curr != NULL) {
if (curr != heap) { mi_heap_t* next = curr->next; // save `next` as `curr` will be freed
mi_assert_internal(!mi_heap_is_backing(curr)); if (curr != heap) {
mi_heap_delete(curr); mi_assert_internal(!mi_heap_is_backing(curr));
mi_heap_delete(curr);
}
curr = next;
} }
curr = next; mi_assert_internal(heap->tld->heaps == heap && heap->next == NULL);
} mi_assert_internal(mi_heap_is_backing(heap));
mi_assert_internal(heap->tld->heaps == heap && heap->next == NULL);
mi_assert_internal(mi_heap_is_backing(heap));
// collect if not the main thread // collect if not the main thread
if (heap != &_mi_heap_main) { if (heap != &_mi_heap_main) {
_mi_heap_collect_abandon(heap); _mi_heap_collect_abandon(heap);
}
} }
// merge stats // merge stats
@ -240,7 +242,7 @@ static bool _mi_heap_done(mi_heap_t* heap) {
// free if not the main thread // free if not the main thread
if (heap != &_mi_heap_main) { if (heap != &_mi_heap_main) {
mi_assert_internal(heap->tld->segments.count == 0); mi_assert_internal(heap->tld->segments.count == 0 || heap->thread_id != _mi_thread_id());
_mi_os_free(heap, sizeof(mi_thread_data_t), &_mi_stats_main); _mi_os_free(heap, sizeof(mi_thread_data_t), &_mi_stats_main);
} }
#if 0 #if 0
@ -294,6 +296,10 @@ static void _mi_thread_done(mi_heap_t* default_heap);
#endif #endif
static DWORD mi_fls_key = (DWORD)(-1); static DWORD mi_fls_key = (DWORD)(-1);
static void NTAPI mi_fls_done(PVOID value) { static void NTAPI mi_fls_done(PVOID value) {
if (mi_fls_key != -1 && _mi_is_main_thread()) {
FlsSetValue(mi_fls_key, NULL); // null out once to prevent recursion on the main thread
mi_fls_key = -1;
}
if (value!=NULL) _mi_thread_done((mi_heap_t*)value); if (value!=NULL) _mi_thread_done((mi_heap_t*)value);
} }
#elif defined(MI_USE_PTHREADS) #elif defined(MI_USE_PTHREADS)

View file

@ -7,11 +7,15 @@
#include <mimalloc.h> #include <mimalloc.h>
#include <new> #include <new>
#include <vector> #include <vector>
#include <future>
#include <iostream>
#include <thread> #include <thread>
#include <mimalloc.h> #include <mimalloc.h>
#include <assert.h> #include <assert.h>
#include <mimalloc-new-delete.h>
#ifdef _WIN32 #ifdef _WIN32
#include <windows.h> #include <windows.h>
static void msleep(unsigned long msecs) { Sleep(msecs); } static void msleep(unsigned long msecs) { Sleep(msecs); }
@ -25,14 +29,16 @@ void heap_no_delete(); // issue #202
void heap_late_free(); // issue #204 void heap_late_free(); // issue #204
void padding_shrink(); // issue #209 void padding_shrink(); // issue #209
void various_tests(); void various_tests();
void test_mt_shutdown();
int main() { int main() {
mi_stats_reset(); // ignore earlier allocations mi_stats_reset(); // ignore earlier allocations
heap_thread_free_large(); heap_thread_free_large();
heap_no_delete(); heap_no_delete();
heap_late_free(); heap_late_free();
padding_shrink(); padding_shrink();
various_tests(); various_tests();
//test_mt_shutdown();
mi_stats_print(NULL); mi_stats_print(NULL);
return 0; return 0;
} }
@ -53,20 +59,20 @@ public:
}; };
void various_tests() { void various_tests() {
atexit(free_p); atexit(free_p);
void* p1 = malloc(78); void* p1 = malloc(78);
void* p2 = mi_malloc_aligned(16,24); void* p2 = mi_malloc_aligned(16, 24);
free(p1); free(p1);
p1 = malloc(8); p1 = malloc(8);
char* s = mi_strdup("hello\n"); char* s = mi_strdup("hello\n");
//char* s = _strdup("hello\n"); //char* s = _strdup("hello\n");
//char* buf = NULL; //char* buf = NULL;
//size_t len; //size_t len;
//_dupenv_s(&buf,&len,"MIMALLOC_VERBOSE"); //_dupenv_s(&buf,&len,"MIMALLOC_VERBOSE");
//mi_free(buf); //mi_free(buf);
mi_free(p2); mi_free(p2);
p2 = malloc(16); p2 = malloc(16);
p1 = realloc(p1, 32); p1 = realloc(p1, 32);
@ -76,7 +82,7 @@ void various_tests() {
Test* t = new Test(42); Test* t = new Test(42);
delete t; delete t;
t = new (std::nothrow) Test(42); t = new (std::nothrow) Test(42);
delete t; delete t;
} }
class Static { class Static {
@ -105,7 +111,7 @@ bool test_stl_allocator1() {
struct some_struct { int i; int j; double z; }; struct some_struct { int i; int j; double z; };
bool test_stl_allocator2() { bool test_stl_allocator2() {
std::vector<some_struct, mi_stl_allocator<some_struct> > vec; std::vector<some_struct, mi_stl_allocator<some_struct> > vec;
vec.push_back(some_struct()); vec.push_back(some_struct());
vec.pop_back(); vec.pop_back();
@ -117,13 +123,13 @@ bool test_stl_allocator2() {
// Issue #202 // Issue #202
void heap_no_delete_worker() { void heap_no_delete_worker() {
mi_heap_t* heap = mi_heap_new(); mi_heap_t* heap = mi_heap_new();
void* q = mi_heap_malloc(heap,1024); void* q = mi_heap_malloc(heap, 1024);
// mi_heap_delete(heap); // uncomment to prevent assertion // mi_heap_delete(heap); // uncomment to prevent assertion
} }
void heap_no_delete() { void heap_no_delete() {
auto t1 = std::thread(heap_no_delete_worker); auto t1 = std::thread(heap_no_delete_worker);
t1.join(); t1.join();
} }
@ -173,3 +179,29 @@ void heap_thread_free_large() {
t1.join(); t1.join();
} }
} }
void test_mt_shutdown()
{
const int threads = 5;
std::vector< std::future< std::vector< char* > > > ts;
auto fn = [&]()
{
std::vector< char* > ps;
ps.reserve(1000);
for (int i = 0; i < 1000; i++)
ps.emplace_back(new char[1]);
return ps;
};
for (int i = 0; i < threads; i++)
ts.emplace_back(std::async(std::launch::async, fn));
for (auto& f : ts)
for (auto& p : f.get())
delete[] p;
std::cout << "done" << std::endl;
}