Defensive code for alloc/dealloc during TLS teardown (#161)

* Defensive code for alloc/dealloc during TLS teardown

If an allocation or deallocation occurs during TLS teardown, then it is
possible for a new allocator to be created and then this is leaked. On
the mimalloc-bench mstressN benchmark this was observed leading to a
large memory leak.

This fix, detects if we are in the TLS teardown phase, and if so,
the calls to alloc or dealloc must return the allocator once they have
perform the specific operation.

Uses a separate variable to represent if a thread_local's destructor has
run already.  This is used to detect thread teardown to put the
allocator into a special slow path to avoid leaks.

* Added some printing first operation to track progress

* Improve error messages on posix

Flush errors, print assert details, and present stack traces.

* Detect incorrect use of pool.

* Clang format.

* Replace broken LL/SC implementation

LL/SC implementation was broken, this replaces it with
a locking implementation. Changes the API to support LL/SC
for future implementation on ARM.

* Improve TLS teardown.

* Make std::function fully inlined.

* Factor out PALLinux stack trace.

* Add checks for leaking allocators.

* Add release build of Windows Clang
This commit is contained in:
Matthew Parkinson
2020-04-07 15:37:26 +01:00
committed by GitHub
parent d87888096e
commit 74657d9dbc
13 changed files with 359 additions and 147 deletions

View File

@@ -29,9 +29,9 @@ namespace snmalloc
do
{
T* top = ABAT::ptr(cmp);
T* top = cmp.ptr();
last->next.store(top, std::memory_order_release);
} while (!stack.compare_exchange(cmp, first));
} while (!cmp.store_conditional(first));
}
T* pop()
@@ -44,13 +44,13 @@ namespace snmalloc
do
{
top = ABAT::ptr(cmp);
top = cmp.ptr();
if (top == nullptr)
break;
next = top->next.load(std::memory_order_acquire);
} while (!stack.compare_exchange(cmp, next));
} while (!cmp.store_conditional(next));
return top;
}
@@ -63,11 +63,11 @@ namespace snmalloc
do
{
top = ABAT::ptr(cmp);
top = cmp.ptr();
if (top == nullptr)
break;
} while (!stack.compare_exchange(cmp, nullptr));
} while (!cmp.store_conditional(nullptr));
return top;
}