__pthread_internal_free doesn't happen on threads that are detached,
causing the bionic TLS allocation (and guard pages) to be leaked.
Fix the leak, and name the allocations to make things apparent if this
ever happens again.
Bug: http://b/
36045112
Test: manually ran a program that detached empty threads
Change-Id: Id1c7852b7384474244f7bf5a0f7da54ff962e0a1
if (allocation == MAP_FAILED) {
__libc_fatal("failed to allocate TLS");
}
+ prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, allocation, allocation_size, "bionic TLS guard page");
thread->bionic_tls = reinterpret_cast<bionic_tls*>(static_cast<char*>(allocation) + PAGE_SIZE);
if (mprotect(thread->bionic_tls, BIONIC_TLS_SIZE, PROT_READ | PROT_WRITE) != 0) {
__libc_fatal("failed to mprotect TLS");
}
+ prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, thread->bionic_tls, BIONIC_TLS_SIZE, "bionic TLS");
}
void __init_thread_stack_guard(pthread_internal_t* thread) {
thread->alternate_signal_stack = NULL;
}
+ // Unmap the bionic TLS, including guard pages.
+ void* allocation = reinterpret_cast<char*>(thread->bionic_tls) - PAGE_SIZE;
+ munmap(allocation, BIONIC_TLS_SIZE + 2 * PAGE_SIZE);
+
ThreadJoinState old_state = THREAD_NOT_JOINED;
while (old_state == THREAD_NOT_JOINED &&
!atomic_compare_exchange_weak(&thread->join_state, &old_state, THREAD_EXITED_NOT_JOINED)) {
}
static void __pthread_internal_free(pthread_internal_t* thread) {
- // Unmap the TLS, including guard pages.
- void* allocation = reinterpret_cast<char*>(thread->bionic_tls) - PAGE_SIZE;
- munmap(allocation, BIONIC_TLS_SIZE + 2 * PAGE_SIZE);
-
if (thread->mmap_size != 0) {
// Free mapped space, including thread stack and pthread_internal_t.
munmap(thread->attr.stack_base, thread->mmap_size);