From 304348af197f30b3bf0e0764b97eb9699a376c68 Mon Sep 17 00:00:00 2001 From: Yabin Cui Date: Thu, 3 Dec 2015 13:01:42 -0800 Subject: [PATCH] Clear pthread_internal_t allocated on user provided stack. Several parts in pthread_internal_t should be initialized to zero, like tls, key_data and thread_local_dtors. So just clear the whole pthread_internal_t is more convenient. Bug: 25990348 Change-Id: Ibb6d1200ea5e6e1afbc77971f179197e8239f6ea --- libc/bionic/pthread_create.cpp | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/libc/bionic/pthread_create.cpp b/libc/bionic/pthread_create.cpp index ce430097e..34826dba6 100644 --- a/libc/bionic/pthread_create.cpp +++ b/libc/bionic/pthread_create.cpp @@ -53,13 +53,6 @@ extern "C" int __isthreaded; // This code is used both by each new pthread and the code that initializes the main thread. void __init_tls(pthread_internal_t* thread) { - if (thread->mmap_size == 0) { - // If the TLS area was not allocated by mmap(), it may not have been cleared to zero. - // So assume the worst and zero the TLS area. - memset(thread->tls, 0, sizeof(thread->tls)); - memset(thread->key_data, 0, sizeof(thread->key_data)); - } - // Slot 0 must point to itself. The x86 Linux kernel reads the TLS from %fs:0. thread->tls[TLS_SLOT_SELF] = thread->tls; thread->tls[TLS_SLOT_THREAD_ID] = thread; @@ -175,6 +168,11 @@ static int __allocate_thread(pthread_attr_t* attr, pthread_internal_t** threadp, (reinterpret_cast(stack_top) - sizeof(pthread_internal_t)) & ~0xf); pthread_internal_t* thread = reinterpret_cast(stack_top); + if (mmap_size == 0) { + // If thread was not allocated by mmap(), it may not have been cleared to zero. + // So assume the worst and zero it. + memset(thread, 0, sizeof(pthread_internal_t)); + } attr->stack_size = stack_top - reinterpret_cast(attr->stack_base); thread->mmap_size = mmap_size;