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
diff --git a/libc/bionic/pthread_attr.cpp b/libc/bionic/pthread_attr.cpp
index 4b6a8f2..914dd36 100644
--- a/libc/bionic/pthread_attr.cpp
+++ b/libc/bionic/pthread_attr.cpp
@@ -164,7 +164,7 @@
   // 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 9be86f1..5010a64 100644
--- a/libc/bionic/pthread_create.cpp
+++ b/libc/bionic/pthread_create.cpp
@@ -59,13 +59,13 @@
   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 d006045..4c4be0e 100644
--- a/libc/upstream-openbsd/android/include/arc4random.h
+++ b/libc/upstream-openbsd/android/include/arc4random.h
@@ -45,10 +45,8 @@
 #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 58a52d0..a843b7b 100644
--- a/linker/linker.cpp
+++ b/linker/linker.cpp
@@ -286,8 +286,7 @@
                      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 fd6f496..a37e910 100644
--- a/linker/linker_allocator.cpp
+++ b/linker/linker_allocator.cpp
@@ -200,12 +200,10 @@
 }
 
 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::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 80ae508..9c16828 100644
--- a/linker/linker_allocator.h
+++ b/linker/linker_allocator.h
@@ -88,12 +88,12 @@
 
   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 605e185..abb1ebd 100644
--- a/linker/linker_block_allocator.cpp
+++ b/linker/linker_block_allocator.cpp
@@ -117,7 +117,7 @@
                 "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 e8b02d6..3862d8c 100644
--- a/linker/linker_main.cpp
+++ b/linker/linker_main.cpp
@@ -276,11 +276,8 @@
 
   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 62401a6..e44bfed 100644
--- a/tests/sys_mman_test.cpp
+++ b/tests/sys_mman_test.cpp
@@ -235,7 +235,7 @@
 
 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));
 }