aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorElliott Hughes <enh@google.com>2017-09-15 16:09:22 -0700
committerElliott Hughes <enh@google.com>2017-09-15 16:18:49 -0700
commit7b0af7ad82fcf88e800d1a553d81fda29dc064bd (patch)
tree4cf982c55c48b49864bcb4f986c7400d640930e0
parent3b64f8ecb3db49574cc5b7c6030be98c159be4d4 (diff)
downloadbionic-7b0af7ad82fcf88e800d1a553d81fda29dc064bd.tar.gz
Always log errno when aborting.
(Where errno is relevant.) Also consistently use -1 as the fd for anonymous mmaps. (It doesn't matter, but it's more common, and potentially more intention-revealing.) Bug: http://b/65608572 Test: ran tests Change-Id: Ie9a207632d8242f42086ba3ca862519014c3c102
-rw-r--r--libc/bionic/pthread_attr.cpp2
-rw-r--r--libc/bionic/pthread_create.cpp4
-rw-r--r--libc/upstream-openbsd/android/include/arc4random.h6
-rw-r--r--linker/linker.cpp3
-rw-r--r--linker/linker_allocator.cpp14
-rw-r--r--linker/linker_allocator.h6
-rw-r--r--linker/linker_block_allocator.cpp2
-rw-r--r--linker/linker_main.cpp5
-rw-r--r--tests/sys_mman_test.cpp2
9 files changed, 18 insertions, 26 deletions
diff --git a/libc/bionic/pthread_attr.cpp b/libc/bionic/pthread_attr.cpp
index 4b6a8f2ae..914dd3682 100644
--- a/libc/bionic/pthread_attr.cpp
+++ b/libc/bionic/pthread_attr.cpp
@@ -164,7 +164,7 @@ static int __pthread_attr_getstack_main_thread(void** stack_base, size_t* stack_
// Hunt for the region that contains that address.
FILE* fp = fopen("/proc/self/maps", "re");
if (fp == nullptr) {
- async_safe_fatal("couldn't open /proc/self/maps");
+ async_safe_fatal("couldn't open /proc/self/maps: %s", strerror(errno));
}
char line[BUFSIZ];
while (fgets(line, sizeof(line), fp) != NULL) {
diff --git a/libc/bionic/pthread_create.cpp b/libc/bionic/pthread_create.cpp
index 9be86f19f..5010a64e3 100644
--- a/libc/bionic/pthread_create.cpp
+++ b/libc/bionic/pthread_create.cpp
@@ -59,13 +59,13 @@ void __init_tls(pthread_internal_t* thread) {
size_t allocation_size = BIONIC_TLS_SIZE + 2 * PAGE_SIZE;
void* allocation = mmap(nullptr, allocation_size, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (allocation == MAP_FAILED) {
- async_safe_fatal("failed to allocate TLS");
+ async_safe_fatal("failed to allocate TLS: %s", strerror(errno));
}
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) {
- async_safe_fatal("failed to mprotect TLS");
+ async_safe_fatal("failed to mprotect TLS: %s", strerror(errno));
}
prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, thread->bionic_tls, BIONIC_TLS_SIZE, "bionic TLS");
}
diff --git a/libc/upstream-openbsd/android/include/arc4random.h b/libc/upstream-openbsd/android/include/arc4random.h
index d006045f6..4c4be0e9a 100644
--- a/libc/upstream-openbsd/android/include/arc4random.h
+++ b/libc/upstream-openbsd/android/include/arc4random.h
@@ -45,10 +45,8 @@ extern int __register_atfork(void (*)(void), void(*)(void), void (*)(void), void
#define _ARC4_ATFORK(f) pthread_atfork(NULL, NULL, (f))
#endif
-static inline void
-_getentropy_fail(void)
-{
- async_safe_fatal("getentropy failed");
+static inline void _getentropy_fail(void) {
+ async_safe_fatal("getentropy failed: %s", strerror(errno));
}
volatile sig_atomic_t _rs_forked;
diff --git a/linker/linker.cpp b/linker/linker.cpp
index 58a52d05b..a843b7b7f 100644
--- a/linker/linker.cpp
+++ b/linker/linker.cpp
@@ -286,8 +286,7 @@ soinfo* soinfo_alloc(android_namespace_t* ns, const char* name,
struct stat* file_stat, off64_t file_offset,
uint32_t rtld_flags) {
if (strlen(name) >= PATH_MAX) {
- DL_ERR("library name \"%s\" too long", name);
- return nullptr;
+ async_safe_fatal("library name \"%s\" too long", name);
}
TRACE("name %s: allocating soinfo for ns=%p", name, ns);
diff --git a/linker/linker_allocator.cpp b/linker/linker_allocator.cpp
index fd6f4964b..a37e910b5 100644
--- a/linker/linker_allocator.cpp
+++ b/linker/linker_allocator.cpp
@@ -200,12 +200,10 @@ void LinkerSmallObjectAllocator::create_page_record(void* page_addr, size_t free
}
void LinkerSmallObjectAllocator::alloc_page() {
- static_assert(sizeof(page_info) % 16 == 0,
- "sizeof(page_info) is not multiple of 16");
- void* map_ptr = mmap(nullptr, PAGE_SIZE,
- PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
+ static_assert(sizeof(page_info) % 16 == 0, "sizeof(page_info) is not multiple of 16");
+ void* map_ptr = mmap(nullptr, PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
if (map_ptr == MAP_FAILED) {
- async_safe_fatal("mmap failed");
+ async_safe_fatal("mmap failed: %s", strerror(errno));
}
prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, map_ptr, PAGE_SIZE, "linker_alloc_small_objects");
@@ -246,11 +244,11 @@ void LinkerMemoryAllocator::initialize_allocators() {
void* LinkerMemoryAllocator::alloc_mmap(size_t size) {
size_t allocated_size = PAGE_END(size + sizeof(page_info));
- void* map_ptr = mmap(nullptr, allocated_size,
- PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
+ void* map_ptr = mmap(nullptr, allocated_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS,
+ -1, 0);
if (map_ptr == MAP_FAILED) {
- async_safe_fatal("mmap failed");
+ async_safe_fatal("mmap failed: %s", strerror(errno));
}
prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, map_ptr, allocated_size, "linker_alloc_lob");
diff --git a/linker/linker_allocator.h b/linker/linker_allocator.h
index 80ae5080c..9c16828ef 100644
--- a/linker/linker_allocator.h
+++ b/linker/linker_allocator.h
@@ -88,12 +88,12 @@ class linker_vector_allocator {
T* allocate(size_t n, const T* hint = nullptr) {
size_t size = n * sizeof(T);
- void* ptr = mmap(const_cast<T*>(hint), size,
- PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
+ void* ptr = mmap(const_cast<T*>(hint), size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS,
+ -1, 0);
if (ptr == MAP_FAILED) {
// Spec says we need to throw std::bad_alloc here but because our
// code does not support exception handling anyways - we are going to abort.
- async_safe_fatal("mmap failed");
+ async_safe_fatal("mmap failed: %s", strerror(errno));
}
prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, ptr, size, "linker_alloc_vector");
diff --git a/linker/linker_block_allocator.cpp b/linker/linker_block_allocator.cpp
index 605e18562..abb1ebd08 100644
--- a/linker/linker_block_allocator.cpp
+++ b/linker/linker_block_allocator.cpp
@@ -117,7 +117,7 @@ void LinkerBlockAllocator::create_new_page() {
"Invalid sizeof(LinkerBlockAllocatorPage)");
LinkerBlockAllocatorPage* page = reinterpret_cast<LinkerBlockAllocatorPage*>(
- mmap(nullptr, PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0));
+ mmap(nullptr, PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0));
if (page == MAP_FAILED) {
abort(); // oom
diff --git a/linker/linker_main.cpp b/linker/linker_main.cpp
index e8b02d675..3862d8ca3 100644
--- a/linker/linker_main.cpp
+++ b/linker/linker_main.cpp
@@ -276,11 +276,8 @@ static ElfW(Addr) __linker_init_post_relocation(KernelArgumentBlock& args) {
const char* executable_path = get_executable_path();
soinfo* si = soinfo_alloc(&g_default_namespace, executable_path, &file_stat, 0, RTLD_GLOBAL);
- if (si == nullptr) {
- async_safe_fatal("Couldn't allocate soinfo: out of memory?");
- }
- /* bootstrap the link map, the main exe always needs to be first */
+ // Bootstrap the link map, the main exe always needs to be first.
si->set_main_executable();
link_map* map = &(si->link_map_head);
diff --git a/tests/sys_mman_test.cpp b/tests/sys_mman_test.cpp
index 62401a6c2..e44bfed38 100644
--- a/tests/sys_mman_test.cpp
+++ b/tests/sys_mman_test.cpp
@@ -235,7 +235,7 @@ TEST(sys_mman, mremap_PTRDIFF_MAX) {
TEST(sys_mman, mmap_bug_27265969) {
char* base = reinterpret_cast<char*>(mmap(nullptr, PAGE_SIZE * 2, PROT_EXEC | PROT_READ,
- MAP_ANONYMOUS | MAP_PRIVATE, 0, 0));
+ MAP_ANONYMOUS | MAP_PRIVATE, -1, 0));
// Some kernels had bugs that would cause segfaults here...
__builtin___clear_cache(base, base + (PAGE_SIZE * 2));
}