Merge "Remove outdated list of abi bugs"
diff --git a/libc/bionic/debug_stacktrace.cpp b/libc/bionic/debug_stacktrace.cpp
index b86e2af..7d3c76e 100644
--- a/libc/bionic/debug_stacktrace.cpp
+++ b/libc/bionic/debug_stacktrace.cpp
@@ -44,12 +44,7 @@
#define PAD_PTR "08" PRIxPTR
#endif
-/* depends how the system includes define this */
-#ifdef HAVE_UNWIND_CONTEXT_STRUCT
typedef struct _Unwind_Context __unwind_context;
-#else
-typedef _Unwind_Context __unwind_context;
-#endif
static mapinfo_t* g_map_info = NULL;
static void* g_demangler;
diff --git a/libc/bionic/libc_init_common.cpp b/libc/bionic/libc_init_common.cpp
index 15b3fd5..94b7dd2 100644
--- a/libc/bionic/libc_init_common.cpp
+++ b/libc/bionic/libc_init_common.cpp
@@ -85,8 +85,10 @@
// because things like environment variables with global scope live on it.
// We also can't free the pthread_internal_t itself, since that lives on the main
// thread's stack rather than on the heap.
+ // The main thread has no mmap allocated space for stack or pthread_internal_t.
+ main_thread.mmap_size = 0;
pthread_attr_init(&main_thread.attr);
- main_thread.attr.flags = PTHREAD_ATTR_FLAG_USER_ALLOCATED_STACK | PTHREAD_ATTR_FLAG_MAIN_THREAD;
+ main_thread.attr.flags = PTHREAD_ATTR_FLAG_MAIN_THREAD;
main_thread.attr.guard_size = 0; // The main thread has no guard page.
main_thread.attr.stack_size = 0; // User code should never see this; we'll compute it when asked.
// TODO: the main thread's sched_policy and sched_priority need to be queried.
diff --git a/libc/bionic/pthread_create.cpp b/libc/bionic/pthread_create.cpp
index 9b45161..e4abee9 100644
--- a/libc/bionic/pthread_create.cpp
+++ b/libc/bionic/pthread_create.cpp
@@ -52,8 +52,9 @@
// 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->user_allocated_stack()) {
- // We don't know where the user got their stack, so assume the worst and zero the TLS area.
+ 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], 0, BIONIC_TLS_SLOTS * sizeof(void*));
}
@@ -106,62 +107,63 @@
return error;
}
-static void* __create_thread_stack(size_t stack_size, size_t guard_size) {
+static void* __create_thread_mapped_space(size_t mmap_size, size_t stack_guard_size) {
// Create a new private anonymous map.
int prot = PROT_READ | PROT_WRITE;
int flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE;
- void* stack = mmap(NULL, stack_size, prot, flags, -1, 0);
- if (stack == MAP_FAILED) {
+ void* space = mmap(NULL, mmap_size, prot, flags, -1, 0);
+ if (space == MAP_FAILED) {
__libc_format_log(ANDROID_LOG_WARN,
"libc",
- "pthread_create failed: couldn't allocate %zd-byte stack: %s",
- stack_size, strerror(errno));
+ "pthread_create failed: couldn't allocate %zu-bytes mapped space: %s",
+ mmap_size, strerror(errno));
return NULL;
}
- // Set the guard region at the end of the stack to PROT_NONE.
- if (mprotect(stack, guard_size, PROT_NONE) == -1) {
+ // Stack is at the lower end of mapped space, stack guard region is at the lower end of stack.
+ // Set the stack guard region to PROT_NONE, so we can detect thread stack overflow.
+ if (mprotect(space, stack_guard_size, PROT_NONE) == -1) {
__libc_format_log(ANDROID_LOG_WARN, "libc",
- "pthread_create failed: couldn't mprotect PROT_NONE %zd-byte stack guard region: %s",
- guard_size, strerror(errno));
- munmap(stack, stack_size);
+ "pthread_create failed: couldn't mprotect PROT_NONE %zu-byte stack guard region: %s",
+ stack_guard_size, strerror(errno));
+ munmap(space, mmap_size);
return NULL;
}
- return stack;
+ return space;
}
static int __allocate_thread(pthread_attr_t* attr, pthread_internal_t** threadp, void** child_stack) {
- size_t allocate_stack_size;
+ size_t mmap_size;
uint8_t* stack_top;
if (attr->stack_base == NULL) {
// The caller didn't provide a stack, so allocate one.
// Make sure the stack size and guard size are multiples of PAGE_SIZE.
- allocate_stack_size = BIONIC_ALIGN(attr->stack_size + sizeof(pthread_internal_t), PAGE_SIZE);
+ mmap_size = BIONIC_ALIGN(attr->stack_size + sizeof(pthread_internal_t), PAGE_SIZE);
attr->guard_size = BIONIC_ALIGN(attr->guard_size, PAGE_SIZE);
- attr->stack_base = __create_thread_stack(allocate_stack_size, attr->guard_size);
+ attr->stack_base = __create_thread_mapped_space(mmap_size, attr->guard_size);
if (attr->stack_base == NULL) {
return EAGAIN;
}
- stack_top = reinterpret_cast<uint8_t*>(attr->stack_base) + allocate_stack_size;
+ stack_top = reinterpret_cast<uint8_t*>(attr->stack_base) + mmap_size;
} else {
- // The caller did provide a stack, so remember we're not supposed to free it.
- attr->flags |= PTHREAD_ATTR_FLAG_USER_ALLOCATED_STACK;
- allocate_stack_size = 0;
+ // Remember the mmap size is zero and we don't need to free it.
+ mmap_size = 0;
stack_top = reinterpret_cast<uint8_t*>(attr->stack_base) + attr->stack_size;
}
- // Thread stack is used for two sections:
- // pthread_internal_t.
- // regular stack, from top to down.
+ // Mapped space(or user allocated stack) is used for:
+ // thread_internal_t (including tls array)
+ // thread stack (including guard page)
stack_top -= sizeof(pthread_internal_t);
pthread_internal_t* thread = reinterpret_cast<pthread_internal_t*>(stack_top);
+ attr->stack_size = stack_top - reinterpret_cast<uint8_t*>(attr->stack_base);
// No need to check stack_top alignment. The size of pthread_internal_t is 16-bytes aligned,
// and user allocated stack is guaranteed by pthread_attr_setstack.
- thread->allocated_stack_size = allocate_stack_size;
+ thread->mmap_size = mmap_size;
thread->attr = *attr;
__init_tls(thread);
@@ -248,8 +250,8 @@
// be unblocked, but we're about to unmap the memory the mutex is stored in, so this serves as a
// reminder that you can't rewrite this function to use a ScopedPthreadMutexLocker.
pthread_mutex_unlock(&thread->startup_handshake_mutex);
- if (!thread->user_allocated_stack()) {
- munmap(thread->attr.stack_base, thread->allocated_stack_size);
+ if (thread->mmap_size != 0) {
+ munmap(thread->attr.stack_base, thread->mmap_size);
}
__libc_format_log(ANDROID_LOG_WARN, "libc", "pthread_create failed: clone failed: %s", strerror(errno));
return clone_errno;
diff --git a/libc/bionic/pthread_exit.cpp b/libc/bionic/pthread_exit.cpp
index ee76e2b..9603a79 100644
--- a/libc/bionic/pthread_exit.cpp
+++ b/libc/bionic/pthread_exit.cpp
@@ -87,30 +87,23 @@
thread->alternate_signal_stack = NULL;
}
- // Keep track of what we need to know about the stack before we lose the pthread_internal_t.
- void* stack_base = thread->attr.stack_base;
- size_t stack_size = thread->allocated_stack_size;
- bool free_stack = false;
-
+ bool free_mapped_space = false;
pthread_mutex_lock(&g_thread_list_lock);
if ((thread->attr.flags & PTHREAD_ATTR_FLAG_DETACHED) != 0) {
- // The thread is detached, so we can free the pthread_internal_t.
+ // The thread is detached, no one will use pthread_internal_t after pthread_exit.
+ // So we can free mapped space, which includes pthread_internal_t and thread stack.
// First make sure that the kernel does not try to clear the tid field
// because we'll have freed the memory before the thread actually exits.
__set_tid_address(NULL);
// pthread_internal_t is freed below with stack, not here.
_pthread_internal_remove_locked(thread, false);
- if (!thread->user_allocated_stack()) {
- free_stack = true;
- }
+ free_mapped_space = true;
}
pthread_mutex_unlock(&g_thread_list_lock);
- // Detached threads exit with stack teardown, and everything deallocated here.
- // Threads that can be joined exit but leave their stacks for the pthread_join caller to clean up.
- if (free_stack) {
- // We need to munmap the stack we're running on before calling exit.
+ if (free_mapped_space && thread->mmap_size != 0) {
+ // We need to free mapped space for detached threads when they exit.
// That's not something we can do in C.
// We don't want to take a signal after we've unmapped the stack.
@@ -119,8 +112,10 @@
sigfillset(&mask);
sigprocmask(SIG_SETMASK, &mask, NULL);
- _exit_with_stack_teardown(stack_base, stack_size);
+ _exit_with_stack_teardown(thread->attr.stack_base, thread->mmap_size);
} else {
+ // No need to free mapped space. Either there was no space mapped, or it is left for
+ // the pthread_join caller to clean up.
__exit(0);
}
}
diff --git a/libc/bionic/pthread_internal.h b/libc/bionic/pthread_internal.h
index 62ec543..80002e9 100644
--- a/libc/bionic/pthread_internal.h
+++ b/libc/bionic/pthread_internal.h
@@ -35,11 +35,8 @@
/* Has the thread been detached by a pthread_join or pthread_detach call? */
#define PTHREAD_ATTR_FLAG_DETACHED 0x00000001
-/* Was the thread's stack allocated by the user rather than by us? */
-#define PTHREAD_ATTR_FLAG_USER_ALLOCATED_STACK 0x00000002
-
/* Has the thread been joined by another thread? */
-#define PTHREAD_ATTR_FLAG_JOINED 0x00000004
+#define PTHREAD_ATTR_FLAG_JOINED 0x00000002
/* Is this the main thread? */
#define PTHREAD_ATTR_FLAG_MAIN_THREAD 0x80000000
@@ -70,10 +67,6 @@
return (*cached_pid != 0);
}
- bool user_allocated_stack() {
- return (attr.flags & PTHREAD_ATTR_FLAG_USER_ALLOCATED_STACK) != 0;
- }
-
pthread_attr_t attr;
__pthread_cleanup_t* cleanup_stack;
@@ -86,8 +79,7 @@
pthread_mutex_t startup_handshake_mutex;
- /* Store real allocated stack size, including thread stack and pthread_internal_t. */
- int allocated_stack_size;
+ size_t mmap_size;
void* tls[BIONIC_TLS_SLOTS];
diff --git a/libc/bionic/pthread_internals.cpp b/libc/bionic/pthread_internals.cpp
index a0a8df0..14061d1 100644
--- a/libc/bionic/pthread_internals.cpp
+++ b/libc/bionic/pthread_internals.cpp
@@ -51,11 +51,9 @@
g_thread_list = thread->next;
}
- // For threads using user allocated stack (including the main thread), the pthread_internal_t
- // can't be freed since it is on the stack.
- if (free_thread && !thread->user_allocated_stack()) {
- // Use one munmap to free allocated stack size, including thread stack and pthread_internal_t.
- munmap(thread->attr.stack_base, thread->allocated_stack_size);
+ if (free_thread && thread->mmap_size != 0) {
+ // Free mapped space, including thread stack and pthread_internal_t.
+ munmap(thread->attr.stack_base, thread->mmap_size);
}
}
diff --git a/libc/dns/net/getaddrinfo.c b/libc/dns/net/getaddrinfo.c
index f0d522a..c73c085 100644
--- a/libc/dns/net/getaddrinfo.c
+++ b/libc/dns/net/getaddrinfo.c
@@ -324,7 +324,11 @@
{
struct addrinfo *next;
- assert(ai != NULL);
+#if __ANDROID__
+ if (ai == NULL) return;
+#else
+ _DIAGASSERT(ai != NULL);
+#endif
do {
next = ai->ai_next;
diff --git a/linker/linker.cpp b/linker/linker.cpp
index babefeb..e2f514e 100644
--- a/linker/linker.cpp
+++ b/linker/linker.cpp
@@ -36,6 +36,7 @@
#include <string.h>
#include <sys/mman.h>
#include <sys/param.h>
+#include <sys/personality.h>
#include <unistd.h>
#include <new>
@@ -2663,6 +2664,12 @@
ldpreload_env = linker_env_get("LD_PRELOAD");
}
+#if !defined(__LP64__)
+ if (personality(PER_LINUX32) == -1) {
+ __libc_fatal("error setting PER_LINUX32 personality: %s", strerror(errno));
+ }
+#endif
+
INFO("[ android linker & debugger ]");
soinfo* si = soinfo_alloc(args.argv[0], nullptr, 0, RTLD_GLOBAL);
@@ -2717,7 +2724,10 @@
somain = si;
- si->prelink_image();
+ if (!si->prelink_image()) {
+ __libc_format_fd(2, "CANNOT LINK EXECUTABLE: %s\n", linker_get_error_buffer());
+ exit(EXIT_FAILURE);
+ }
// add somain to global group
si->set_dt_flags_1(si->get_dt_flags_1() | DF_1_GLOBAL);
diff --git a/tests/Android.mk b/tests/Android.mk
index dfb89d2..38d85f8 100644
--- a/tests/Android.mk
+++ b/tests/Android.mk
@@ -93,6 +93,7 @@
sstream_test.cpp \
sys_epoll_test.cpp \
sys_mman_test.cpp \
+ sys_personality_test.cpp \
sys_resource_test.cpp \
sys_select_test.cpp \
sys_sendfile_test.cpp \
diff --git a/tests/netdb_test.cpp b/tests/netdb_test.cpp
index ab5b487..5660bbd 100644
--- a/tests/netdb_test.cpp
+++ b/tests/netdb_test.cpp
@@ -23,6 +23,11 @@
#include <sys/socket.h>
#include <netinet/in.h>
+// https://code.google.com/p/android/issues/detail?id=13228
+TEST(netdb, freeaddrinfo_NULL) {
+ freeaddrinfo(NULL);
+}
+
TEST(netdb, getaddrinfo_NULL_host) {
// It's okay for the host argument to be NULL, as long as service isn't.
addrinfo* ai = NULL;
diff --git a/tests/pthread_test.cpp b/tests/pthread_test.cpp
index 1418c76..2398f23 100644
--- a/tests/pthread_test.cpp
+++ b/tests/pthread_test.cpp
@@ -32,7 +32,6 @@
#include <time.h>
#include <unistd.h>
-
TEST(pthread, pthread_key_create) {
pthread_key_t key;
ASSERT_EQ(0, pthread_key_create(&key, NULL));
@@ -633,18 +632,18 @@
ASSERT_EQ(default_stack_size, stack_size);
ASSERT_GE(GetActualStackSize(attributes), default_stack_size);
- // Large enough and a multiple of the page size.
+ // Large enough and a multiple of the page size; may be rounded up by pthread_create.
ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024));
ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
ASSERT_EQ(32*1024U, stack_size);
- ASSERT_EQ(GetActualStackSize(attributes), 32*1024U);
+ ASSERT_GE(GetActualStackSize(attributes), 32*1024U);
- // Large enough but not a multiple of the page size; will be rounded up by pthread_create.
+ // Large enough but not aligned; will be rounded up by pthread_create.
ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024 + 1));
ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
ASSERT_EQ(32*1024U + 1, stack_size);
#if defined(__BIONIC__)
- ASSERT_EQ(GetActualStackSize(attributes), 32*1024U + 1);
+ ASSERT_GT(GetActualStackSize(attributes), 32*1024U + 1);
#else // __BIONIC__
// glibc rounds down, in violation of POSIX. They document this in their BUGS section.
ASSERT_EQ(GetActualStackSize(attributes), 32*1024U);
@@ -939,6 +938,29 @@
ASSERT_EQ(6666U, stack_size);
}
+static void pthread_attr_getstack_18908062_helper(void*) {
+ char local_variable;
+ pthread_attr_t attributes;
+ pthread_getattr_np(pthread_self(), &attributes);
+ void* stack_base;
+ size_t stack_size;
+ pthread_attr_getstack(&attributes, &stack_base, &stack_size);
+
+ // Test whether &local_variable is in [stack_base, stack_base + stack_size).
+ ASSERT_LE(reinterpret_cast<char*>(stack_base), &local_variable);
+ ASSERT_LT(&local_variable, reinterpret_cast<char*>(stack_base) + stack_size);
+}
+
+// Check whether something on stack is in the range of
+// [stack_base, stack_base + stack_size). see b/18908062.
+TEST(pthread, pthread_attr_getstack_18908062) {
+ pthread_t t;
+ ASSERT_EQ(0, pthread_create(&t, NULL,
+ reinterpret_cast<void* (*)(void*)>(pthread_attr_getstack_18908062_helper),
+ NULL));
+ pthread_join(t, NULL);
+}
+
#if defined(__BIONIC__)
static void* pthread_gettid_np_helper(void* arg) {
*reinterpret_cast<pid_t*>(arg) = gettid();
diff --git a/tests/sys_personality_test.cpp b/tests/sys_personality_test.cpp
new file mode 100644
index 0000000..55a023d
--- /dev/null
+++ b/tests/sys_personality_test.cpp
@@ -0,0 +1,33 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <gtest/gtest.h>
+
+#include <sys/personality.h>
+
+TEST(sys_personality, current_persona) {
+ int persona = personality(0xffffffff);
+#if defined(__BIONIC__)
+#if defined(__LP64__)
+ ASSERT_EQ(PER_LINUX, persona);
+#else
+ ASSERT_EQ(PER_LINUX32, persona);
+#endif
+#else
+ // GLIBC does not set persona prior process startup - it is always PER_LINUX;
+ ASSERT_EQ(PER_LINUX, persona);
+#endif
+}