Merge "Add benchmarks for heap size retrieval"
diff --git a/libc/Android.bp b/libc/Android.bp
index cc0a2bb..1fc3062 100644
--- a/libc/Android.bp
+++ b/libc/Android.bp
@@ -792,6 +792,16 @@
                     "arch-arm/cortex-a15/bionic/__strcpy_chk.S",
                 ],
             },
+            cortex_a76: {
+                srcs: [
+                    "arch-arm/denver/bionic/__strcat_chk.S",
+                    "arch-arm/denver/bionic/__strcpy_chk.S",
+                ],
+                exclude_srcs: [
+                    "arch-arm/cortex-a15/bionic/__strcat_chk.S",
+                    "arch-arm/cortex-a15/bionic/__strcpy_chk.S",
+                ],
+            },
             denver: {
                 srcs: [
                     "arch-arm/denver/bionic/__strcat_chk.S",
@@ -964,6 +974,19 @@
                     "arch-arm/cortex-a15/bionic/strcmp.S",
                 ],
             },
+            cortex_a76: {
+                srcs: [
+                    "arch-arm/cortex-a7/bionic/memset.S",
+                    "arch-arm/denver/bionic/memcpy.S",
+
+                    "arch-arm/krait/bionic/strcmp.S",
+                ],
+                exclude_srcs: [
+                    "arch-arm/cortex-a15/bionic/memset.S",
+                    "arch-arm/cortex-a15/bionic/memcpy.S",
+                    "arch-arm/cortex-a15/bionic/strcmp.S",
+                ],
+            },
             denver: {
                 srcs: [
                     "arch-arm/denver/bionic/memcpy.S",
diff --git a/libc/bionic/fdsan.cpp b/libc/bionic/fdsan.cpp
index 11ebf52..9a9fee2 100644
--- a/libc/bionic/fdsan.cpp
+++ b/libc/bionic/fdsan.cpp
@@ -188,6 +188,8 @@
     async_safe_fatal_va_list("fdsan", fmt, va);
   } else {
     async_safe_format_log_va_list(ANDROID_LOG_ERROR, "fdsan", fmt, va);
+    va_end(va);
+    va_start(va, fmt);
     size_t len =
         async_safe_format_buffer_va_list(abort_message.buf, sizeof(abort_message.buf), fmt, va);
     abort_message.size = len + sizeof(size_t);
diff --git a/libc/bionic/malloc_common.cpp b/libc/bionic/malloc_common.cpp
index 8bf44a1..61b3f33 100644
--- a/libc/bionic/malloc_common.cpp
+++ b/libc/bionic/malloc_common.cpp
@@ -42,6 +42,7 @@
 //   write_malloc_leak_info: Writes the leak info data to a file.
 
 #include <pthread.h>
+#include <stdatomic.h>
 
 #include <private/bionic_config.h>
 #include <private/bionic_globals.h>
@@ -68,6 +69,17 @@
 #define Malloc(function)  je_ ## function
 #endif
 
+template <typename T>
+static T* RemoveConst(const T* x) {
+  return const_cast<T*>(x);
+}
+
+// RemoveConst is a workaround for bug in current libcxx. Fix in
+// https://reviews.llvm.org/D47613
+#define atomic_load_explicit_const(obj, order) atomic_load_explicit(RemoveConst(obj), order)
+
+static constexpr memory_order default_read_memory_order = memory_order_acquire;
+
 static constexpr MallocDispatch __libc_malloc_default_dispatch
   __attribute__((unused)) = {
     Malloc(calloc),
@@ -104,7 +116,9 @@
 // Allocation functions
 // =============================================================================
 extern "C" void* calloc(size_t n_elements, size_t elem_size) {
-  auto _calloc = __libc_globals->malloc_dispatch.calloc;
+  auto _calloc = atomic_load_explicit_const(
+      &__libc_globals->malloc_dispatch.calloc,
+      default_read_memory_order);
   if (__predict_false(_calloc != nullptr)) {
     return _calloc(n_elements, elem_size);
   }
@@ -112,7 +126,9 @@
 }
 
 extern "C" void free(void* mem) {
-  auto _free = __libc_globals->malloc_dispatch.free;
+  auto _free = atomic_load_explicit_const(
+      &__libc_globals->malloc_dispatch.free,
+      default_read_memory_order);
   if (__predict_false(_free != nullptr)) {
     _free(mem);
   } else {
@@ -121,7 +137,9 @@
 }
 
 extern "C" struct mallinfo mallinfo() {
-  auto _mallinfo = __libc_globals->malloc_dispatch.mallinfo;
+  auto _mallinfo = atomic_load_explicit_const(
+      &__libc_globals->malloc_dispatch.mallinfo,
+      default_read_memory_order);
   if (__predict_false(_mallinfo != nullptr)) {
     return _mallinfo();
   }
@@ -129,7 +147,9 @@
 }
 
 extern "C" int mallopt(int param, int value) {
-  auto _mallopt = __libc_globals->malloc_dispatch.mallopt;
+  auto _mallopt = atomic_load_explicit_const(
+      &__libc_globals->malloc_dispatch.mallopt,
+      default_read_memory_order);
   if (__predict_false(_mallopt != nullptr)) {
     return _mallopt(param, value);
   }
@@ -137,7 +157,9 @@
 }
 
 extern "C" void* malloc(size_t bytes) {
-  auto _malloc = __libc_globals->malloc_dispatch.malloc;
+  auto _malloc = atomic_load_explicit_const(
+      &__libc_globals->malloc_dispatch.malloc,
+      default_read_memory_order);
   if (__predict_false(_malloc != nullptr)) {
     return _malloc(bytes);
   }
@@ -145,7 +167,9 @@
 }
 
 extern "C" size_t malloc_usable_size(const void* mem) {
-  auto _malloc_usable_size = __libc_globals->malloc_dispatch.malloc_usable_size;
+  auto _malloc_usable_size = atomic_load_explicit_const(
+      &__libc_globals->malloc_dispatch.malloc_usable_size,
+      default_read_memory_order);
   if (__predict_false(_malloc_usable_size != nullptr)) {
     return _malloc_usable_size(mem);
   }
@@ -153,7 +177,9 @@
 }
 
 extern "C" void* memalign(size_t alignment, size_t bytes) {
-  auto _memalign = __libc_globals->malloc_dispatch.memalign;
+  auto _memalign = atomic_load_explicit_const(
+      &__libc_globals->malloc_dispatch.memalign,
+      default_read_memory_order);
   if (__predict_false(_memalign != nullptr)) {
     return _memalign(alignment, bytes);
   }
@@ -161,7 +187,9 @@
 }
 
 extern "C" int posix_memalign(void** memptr, size_t alignment, size_t size) {
-  auto _posix_memalign = __libc_globals->malloc_dispatch.posix_memalign;
+  auto _posix_memalign = atomic_load_explicit_const(
+      &__libc_globals->malloc_dispatch.posix_memalign,
+      default_read_memory_order);
   if (__predict_false(_posix_memalign != nullptr)) {
     return _posix_memalign(memptr, alignment, size);
   }
@@ -169,7 +197,9 @@
 }
 
 extern "C" void* aligned_alloc(size_t alignment, size_t size) {
-  auto _aligned_alloc = __libc_globals->malloc_dispatch.aligned_alloc;
+  auto _aligned_alloc = atomic_load_explicit_const(
+      &__libc_globals->malloc_dispatch.aligned_alloc,
+      default_read_memory_order);
   if (__predict_false(_aligned_alloc != nullptr)) {
     return _aligned_alloc(alignment, size);
   }
@@ -177,7 +207,9 @@
 }
 
 extern "C" void* realloc(void* old_mem, size_t bytes) {
-  auto _realloc = __libc_globals->malloc_dispatch.realloc;
+  auto _realloc = atomic_load_explicit_const(
+      &__libc_globals->malloc_dispatch.realloc,
+      default_read_memory_order);
   if (__predict_false(_realloc != nullptr)) {
     return _realloc(old_mem, bytes);
   }
@@ -195,7 +227,9 @@
 
 #if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
 extern "C" void* pvalloc(size_t bytes) {
-  auto _pvalloc = __libc_globals->malloc_dispatch.pvalloc;
+  auto _pvalloc = atomic_load_explicit_const(
+      &__libc_globals->malloc_dispatch.pvalloc,
+      default_read_memory_order);
   if (__predict_false(_pvalloc != nullptr)) {
     return _pvalloc(bytes);
   }
@@ -203,7 +237,9 @@
 }
 
 extern "C" void* valloc(size_t bytes) {
-  auto _valloc = __libc_globals->malloc_dispatch.valloc;
+  auto _valloc = atomic_load_explicit_const(
+      &__libc_globals->malloc_dispatch.valloc,
+      default_read_memory_order);
   if (__predict_false(_valloc != nullptr)) {
     return _valloc(bytes);
   }
@@ -233,6 +269,10 @@
 static const char* DEBUG_PROPERTY_PROGRAM = "libc.debug.malloc.program";
 static const char* DEBUG_ENV_OPTIONS = "LIBC_DEBUG_MALLOC_OPTIONS";
 
+static const char* HEAPPROFD_SHARED_LIB = "heapprofd_client.so";
+static const char* HEAPPROFD_PREFIX = "heapprofd";
+static const int HEAPPROFD_SIGNAL = __SIGRTMIN + 4;
+
 enum FunctionEnum : uint8_t {
   FUNC_INITIALIZE,
   FUNC_FINALIZE,
@@ -313,7 +353,7 @@
 // =============================================================================
 
 template<typename FunctionType>
-static bool InitMallocFunction(void* malloc_impl_handler, FunctionType* func, const char* prefix, const char* suffix) {
+static bool InitMallocFunction(void* malloc_impl_handler, _Atomic(FunctionType)* func, const char* prefix, const char* suffix) {
   char symbol[128];
   snprintf(symbol, sizeof(symbol), "%s_%s", prefix, suffix);
   *func = reinterpret_cast<FunctionType>(dlsym(malloc_impl_handler, symbol));
@@ -325,10 +365,16 @@
 }
 
 static bool InitMallocFunctions(void* impl_handler, MallocDispatch* table, const char* prefix) {
-  if (!InitMallocFunction<MallocCalloc>(impl_handler, &table->calloc, prefix, "calloc")) {
+  // We initialize free first to prevent the following situation:
+  // Heapprofd's MallocMalloc is installed, and an allocation is observed
+  // and logged to the heap dump. The corresponding free happens before
+  // heapprofd's MallocFree is installed, and is not logged in the heap
+  // dump. This leads to the allocation wrongly being active in the heap
+  // dump indefinitely.
+  if (!InitMallocFunction<MallocFree>(impl_handler, &table->free, prefix, "free")) {
     return false;
   }
-  if (!InitMallocFunction<MallocFree>(impl_handler, &table->free, prefix, "free")) {
+  if (!InitMallocFunction<MallocCalloc>(impl_handler, &table->calloc, prefix, "calloc")) {
     return false;
   }
   if (!InitMallocFunction<MallocMallinfo>(impl_handler, &table->mallinfo, prefix, "mallinfo")) {
@@ -465,24 +511,8 @@
   return impl_handle;
 }
 
-// Initializes memory allocation framework once per process.
-static void malloc_init_impl(libc_globals* globals) {
-  const char* prefix;
-  const char* shared_lib;
-  char prop[PROP_VALUE_MAX];
-  char* options = prop;
-  // Prefer malloc debug since it existed first and is a more complete
-  // malloc interceptor than the hooks.
-  if (CheckLoadMallocDebug(&options)) {
-    prefix = "debug";
-    shared_lib = DEBUG_SHARED_LIB;
-  } else if (CheckLoadMallocHooks(&options)) {
-    prefix = "hooks";
-    shared_lib = HOOKS_SHARED_LIB;
-  } else {
-    return;
-  }
-
+static void install_hooks(libc_globals* globals, const char* options,
+                          const char* prefix, const char* shared_lib) {
   MallocDispatch dispatch_table;
   void* impl_handle = LoadSharedLibrary(shared_lib, prefix, &dispatch_table);
   if (impl_handle == nullptr) {
@@ -510,11 +540,89 @@
   }
 }
 
+extern "C" void InstallInitHeapprofdHook(int);
+
+// Initializes memory allocation framework once per process.
+static void malloc_init_impl(libc_globals* globals) {
+  struct sigaction action = {};
+  action.sa_handler = InstallInitHeapprofdHook;
+  sigaction(HEAPPROFD_SIGNAL, &action, nullptr);
+
+  const char* prefix;
+  const char* shared_lib;
+  char prop[PROP_VALUE_MAX];
+  char* options = prop;
+  // Prefer malloc debug since it existed first and is a more complete
+  // malloc interceptor than the hooks.
+  if (CheckLoadMallocDebug(&options)) {
+    prefix = "debug";
+    shared_lib = DEBUG_SHARED_LIB;
+  } else if (CheckLoadMallocHooks(&options)) {
+    prefix = "hooks";
+    shared_lib = HOOKS_SHARED_LIB;
+  } else {
+    return;
+  }
+  install_hooks(globals, options, prefix, shared_lib);
+}
+
 // Initializes memory allocation framework.
 // This routine is called from __libc_init routines in libc_init_dynamic.cpp.
 __LIBC_HIDDEN__ void __libc_init_malloc(libc_globals* globals) {
   malloc_init_impl(globals);
 }
+
+// The logic for triggering heapprofd below is as following.
+// 1. HEAPPROFD_SIGNAL is received by the process.
+// 2a. If the signal is currently being handled (g_heapprofd_init_in_progress
+//     is true), no action is taken.
+// 2b. Otherwise, The signal handler (InstallInitHeapprofdHook) installs a
+//     temporary malloc hook (InitHeapprofdHook).
+// 3. When this hook gets run the first time, it uninstalls itself and spawns
+//    a thread running InitHeapprofd that loads heapprofd.so and installs the
+//    hooks within.
+//
+// This roundabout way is needed because we are running non AS-safe code, so
+// we cannot run it directly in the signal handler. The other approach of
+// running a standby thread and signalling through write(2) and read(2) would
+// significantly increase the number of active threads in the system.
+
+static _Atomic bool g_heapprofd_init_in_progress = false;
+static _Atomic bool g_init_heapprofd_ran = false;
+
+static void* InitHeapprofd(void*) {
+  __libc_globals.mutate([](libc_globals* globals) {
+    install_hooks(globals, nullptr, HEAPPROFD_PREFIX, HEAPPROFD_SHARED_LIB);
+  });
+  atomic_store(&g_heapprofd_init_in_progress, false);
+  return nullptr;
+}
+
+static void* InitHeapprofdHook(size_t bytes) {
+  if (!atomic_exchange(&g_init_heapprofd_ran, true)) {
+    __libc_globals.mutate([](libc_globals* globals) {
+      atomic_store(&globals->malloc_dispatch.malloc, nullptr);
+    });
+
+    pthread_t thread_id;
+    if (pthread_create(&thread_id, nullptr, InitHeapprofd, nullptr) == -1)
+      error_log("%s: heapprofd: failed to pthread_create.", getprogname());
+    else if (pthread_detach(thread_id) == -1)
+      error_log("%s: heapprofd: failed to pthread_detach", getprogname());
+    if (pthread_setname_np(thread_id, "heapprofdinit") == -1)
+      error_log("%s: heapprod: failed to pthread_setname_np", getprogname());
+  }
+  return Malloc(malloc)(bytes);
+}
+
+extern "C" void InstallInitHeapprofdHook(int) {
+  if (!atomic_exchange(&g_heapprofd_init_in_progress, true)) {
+    __libc_globals.mutate([](libc_globals* globals) {
+      globals->malloc_dispatch.malloc = InitHeapprofdHook;
+    });
+  }
+}
+
 #endif  // !LIBC_STATIC
 
 // =============================================================================
@@ -525,7 +633,9 @@
 // [base, base+size).  Must be called between malloc_disable and malloc_enable.
 extern "C" int malloc_iterate(uintptr_t base, size_t size,
     void (*callback)(uintptr_t base, size_t size, void* arg), void* arg) {
-  auto _iterate = __libc_globals->malloc_dispatch.iterate;
+  auto _iterate = atomic_load_explicit_const(
+      &__libc_globals->malloc_dispatch.iterate,
+      default_read_memory_order);
   if (__predict_false(_iterate != nullptr)) {
     return _iterate(base, size, callback, arg);
   }
@@ -535,7 +645,9 @@
 // Disable calls to malloc so malloc_iterate gets a consistent view of
 // allocated memory.
 extern "C" void malloc_disable() {
-  auto _malloc_disable = __libc_globals->malloc_dispatch.malloc_disable;
+  auto _malloc_disable = atomic_load_explicit_const(
+     & __libc_globals->malloc_dispatch.malloc_disable,
+      default_read_memory_order);
   if (__predict_false(_malloc_disable != nullptr)) {
     return _malloc_disable();
   }
@@ -544,7 +656,9 @@
 
 // Re-enable calls to malloc after a previous call to malloc_disable.
 extern "C" void malloc_enable() {
-  auto _malloc_enable = __libc_globals->malloc_dispatch.malloc_enable;
+  auto _malloc_enable = atomic_load_explicit_const(
+      &__libc_globals->malloc_dispatch.malloc_enable,
+      default_read_memory_order);
   if (__predict_false(_malloc_enable != nullptr)) {
     return _malloc_enable();
   }
diff --git a/libc/bionic/pthread_kill.cpp b/libc/bionic/pthread_kill.cpp
index 3761a75..1531574 100644
--- a/libc/bionic/pthread_kill.cpp
+++ b/libc/bionic/pthread_kill.cpp
@@ -36,7 +36,9 @@
   ErrnoRestorer errno_restorer;
 
   pid_t tid = pthread_gettid_np(t);
-  if (tid == -1) return ESRCH;
+
+  // tid gets reset to 0 on thread exit by CLONE_CHILD_CLEARTID.
+  if (tid == 0 || tid == -1) return ESRCH;
 
   return (tgkill(getpid(), tid, sig) == -1) ? errno : 0;
 }
diff --git a/libc/bionic/strerror_r.cpp b/libc/bionic/strerror_r.cpp
index 1cf2abc..29d902c 100644
--- a/libc/bionic/strerror_r.cpp
+++ b/libc/bionic/strerror_r.cpp
@@ -31,9 +31,139 @@
 }
 
 static const Pair _sys_error_strings[] = {
-#define  __BIONIC_ERRDEF(x,y,z)  { x, z },
-#include "private/bionic_errdefs.h"
-  { 0, nullptr }
+    {0, "Success"},
+    {EPERM, "Operation not permitted"},
+    {ENOENT, "No such file or directory"},
+    {ESRCH, "No such process"},
+    {EINTR, "Interrupted system call"},
+    {EIO, "I/O error"},
+    {ENXIO, "No such device or address"},
+    {E2BIG, "Argument list too long"},
+    {ENOEXEC, "Exec format error"},
+    {EBADF, "Bad file descriptor"},
+    {ECHILD, "No child processes"},
+    {EAGAIN, "Try again"},
+    {ENOMEM, "Out of memory"},
+    {EACCES, "Permission denied"},
+    {EFAULT, "Bad address"},
+    {ENOTBLK, "Block device required"},
+    {EBUSY, "Device or resource busy"},
+    {EEXIST, "File exists"},
+    {EXDEV, "Cross-device link"},
+    {ENODEV, "No such device"},
+    {ENOTDIR, "Not a directory"},
+    {EISDIR, "Is a directory"},
+    {EINVAL, "Invalid argument"},
+    {ENFILE, "File table overflow"},
+    {EMFILE, "Too many open files"},
+    {ENOTTY, "Not a typewriter"},
+    {ETXTBSY, "Text file busy"},
+    {EFBIG, "File too large"},
+    {ENOSPC, "No space left on device"},
+    {ESPIPE, "Illegal seek"},
+    {EROFS, "Read-only file system"},
+    {EMLINK, "Too many links"},
+    {EPIPE, "Broken pipe"},
+    {EDOM, "Math argument out of domain of func"},
+    {ERANGE, "Math result not representable"},
+    {EDEADLK, "Resource deadlock would occur"},
+    {ENAMETOOLONG, "File name too long"},
+    {ENOLCK, "No record locks available"},
+    {ENOSYS, "Function not implemented"},
+    {ENOTEMPTY, "Directory not empty"},
+    {ELOOP, "Too many symbolic links encountered"},
+    {ENOMSG, "No message of desired type"},
+    {EIDRM, "Identifier removed"},
+    {ECHRNG, "Channel number out of range"},
+    {EL2NSYNC, "Level 2 not synchronized"},
+    {EL3HLT, "Level 3 halted"},
+    {EL3RST, "Level 3 reset"},
+    {ELNRNG, "Link number out of range"},
+    {EUNATCH, "Protocol driver not attached"},
+    {ENOCSI, "No CSI structure available"},
+    {EL2HLT, "Level 2 halted"},
+    {EBADE, "Invalid exchange"},
+    {EBADR, "Invalid request descriptor"},
+    {EXFULL, "Exchange full"},
+    {ENOANO, "No anode"},
+    {EBADRQC, "Invalid request code"},
+    {EBADSLT, "Invalid slot"},
+    {EBFONT, "Bad font file format"},
+    {ENOSTR, "Device not a stream"},
+    {ENODATA, "No data available"},
+    {ETIME, "Timer expired"},
+    {ENOSR, "Out of streams resources"},
+    {ENONET, "Machine is not on the network"},
+    {ENOPKG, "Package not installed"},
+    {EREMOTE, "Object is remote"},
+    {ENOLINK, "Link has been severed"},
+    {EADV, "Advertise error"},
+    {ESRMNT, "Srmount error"},
+    {ECOMM, "Communication error on send"},
+    {EPROTO, "Protocol error"},
+    {EMULTIHOP, "Multihop attempted"},
+    {EDOTDOT, "RFS specific error"},
+    {EBADMSG, "Not a data message"},
+    {EOVERFLOW, "Value too large for defined data type"},
+    {ENOTUNIQ, "Name not unique on network"},
+    {EBADFD, "File descriptor in bad state"},
+    {EREMCHG, "Remote address changed"},
+    {ELIBACC, "Can not access a needed shared library"},
+    {ELIBBAD, "Accessing a corrupted shared library"},
+    {ELIBSCN, ".lib section in a.out corrupted"},
+    {ELIBMAX, "Attempting to link in too many shared libraries"},
+    {ELIBEXEC, "Cannot exec a shared library directly"},
+    {EILSEQ, "Illegal byte sequence"},
+    {ERESTART, "Interrupted system call should be restarted"},
+    {ESTRPIPE, "Streams pipe error"},
+    {EUSERS, "Too many users"},
+    {ENOTSOCK, "Socket operation on non-socket"},
+    {EDESTADDRREQ, "Destination address required"},
+    {EMSGSIZE, "Message too long"},
+    {EPROTOTYPE, "Protocol wrong type for socket"},
+    {ENOPROTOOPT, "Protocol not available"},
+    {EPROTONOSUPPORT, "Protocol not supported"},
+    {ESOCKTNOSUPPORT, "Socket type not supported"},
+    {EOPNOTSUPP, "Operation not supported on transport endpoint"},
+    {EPFNOSUPPORT, "Protocol family not supported"},
+    {EAFNOSUPPORT, "Address family not supported by protocol"},
+    {EADDRINUSE, "Address already in use"},
+    {EADDRNOTAVAIL, "Cannot assign requested address"},
+    {ENETDOWN, "Network is down"},
+    {ENETUNREACH, "Network is unreachable"},
+    {ENETRESET, "Network dropped connection because of reset"},
+    {ECONNABORTED, "Software caused connection abort"},
+    {ECONNRESET, "Connection reset by peer"},
+    {ENOBUFS, "No buffer space available"},
+    {EISCONN, "Transport endpoint is already connected"},
+    {ENOTCONN, "Transport endpoint is not connected"},
+    {ESHUTDOWN, "Cannot send after transport endpoint shutdown"},
+    {ETOOMANYREFS, "Too many references: cannot splice"},
+    {ETIMEDOUT, "Connection timed out"},
+    {ECONNREFUSED, "Connection refused"},
+    {EHOSTDOWN, "Host is down"},
+    {EHOSTUNREACH, "No route to host"},
+    {EALREADY, "Operation already in progress"},
+    {EINPROGRESS, "Operation now in progress"},
+    {ESTALE, "Stale NFS file handle"},
+    {EUCLEAN, "Structure needs cleaning"},
+    {ENOTNAM, "Not a XENIX named type file"},
+    {ENAVAIL, "No XENIX semaphores available"},
+    {EISNAM, "Is a named type file"},
+    {EREMOTEIO, "Remote I/O error"},
+    {EDQUOT, "Quota exceeded"},
+    {ENOMEDIUM, "No medium found"},
+    {EMEDIUMTYPE, "Wrong medium type"},
+    {ECANCELED, "Operation Canceled"},
+    {ENOKEY, "Required key not available"},
+    {EKEYEXPIRED, "Key has expired"},
+    {EKEYREVOKED, "Key has been revoked"},
+    {EKEYREJECTED, "Key was rejected by service"},
+    {EOWNERDEAD, "Owner died"},
+    {ENOTRECOVERABLE, "State not recoverable"},
+    {ERFKILL, "Operation not possible due to RF-kill"},
+    {EHWPOISON, "Memory page has hardware error"},
+    {0, nullptr}
 };
 
 extern "C" __LIBC_HIDDEN__ const char* __strerror_lookup(int error_number) {
diff --git a/libc/include/android/api-level.h b/libc/include/android/api-level.h
index ebce900..3a8f926 100644
--- a/libc/include/android/api-level.h
+++ b/libc/include/android/api-level.h
@@ -51,12 +51,6 @@
  * compiler/build system based on the API level you claimed to target.
  */
 #define __ANDROID_API__ __ANDROID_API_FUTURE__
-#else
-/**
- * `__ANDROID_NDK__` is defined for code that's built by the NDK
- * rather than as part of the OS.
- */
-#define __ANDROID_NDK__ 1
 #endif
 
 /** Names the Gingerbread API level (9), for comparisons against __ANDROID_API__. */
diff --git a/libc/include/android/legacy_signal_inlines.h b/libc/include/android/legacy_signal_inlines.h
index 44c2f4f..8219759 100644
--- a/libc/include/android/legacy_signal_inlines.h
+++ b/libc/include/android/legacy_signal_inlines.h
@@ -52,7 +52,7 @@
 
 static __inline int __ndk_legacy___libc_current_sigrtmin() {
   if (__libc_current_sigrtmin) return __libc_current_sigrtmin();
-  return __SIGRTMIN + 4; /* Should match __libc_current_sigrtmin. */
+  return __SIGRTMIN + 5; /* Should match __libc_current_sigrtmin. */
 }
 
 #undef SIGRTMAX
diff --git a/libc/private/bionic_errdefs.h b/libc/private/bionic_errdefs.h
deleted file mode 100644
index 3c3c9d7..0000000
--- a/libc/private/bionic_errdefs.h
+++ /dev/null
@@ -1,170 +0,0 @@
-/*
- * Copyright (C) 2008 The Android Open Source Project
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *  * Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- *  * Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in
- *    the documentation and/or other materials provided with the
- *    distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
- * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
- * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
- * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
- * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-/* the following corresponds to the error codes of the Linux kernel used by the Android platform
- * these are distinct from the OpenBSD ones, which is why we need to redeclare them here
- *
- * this file may be included several times to define either error constants or their
- * string representation
- */
-
-#ifndef __BIONIC_ERRDEF
-#error "__BIONIC_ERRDEF must be defined before including this file"
-#endif
-__BIONIC_ERRDEF( 0              ,   0, "Success" )
-__BIONIC_ERRDEF( EPERM          ,   1, "Operation not permitted" )
-__BIONIC_ERRDEF( ENOENT         ,   2, "No such file or directory" )
-__BIONIC_ERRDEF( ESRCH          ,   3, "No such process" )
-__BIONIC_ERRDEF( EINTR          ,   4, "Interrupted system call" )
-__BIONIC_ERRDEF( EIO            ,   5, "I/O error" )
-__BIONIC_ERRDEF( ENXIO          ,   6, "No such device or address" )
-__BIONIC_ERRDEF( E2BIG          ,   7, "Argument list too long" )
-__BIONIC_ERRDEF( ENOEXEC        ,   8, "Exec format error" )
-__BIONIC_ERRDEF( EBADF          ,   9, "Bad file descriptor" )
-__BIONIC_ERRDEF( ECHILD         ,  10, "No child processes" )
-__BIONIC_ERRDEF( EAGAIN         ,  11, "Try again" )
-__BIONIC_ERRDEF( ENOMEM         ,  12, "Out of memory" )
-__BIONIC_ERRDEF( EACCES         ,  13, "Permission denied" )
-__BIONIC_ERRDEF( EFAULT         ,  14, "Bad address" )
-__BIONIC_ERRDEF( ENOTBLK        ,  15, "Block device required" )
-__BIONIC_ERRDEF( EBUSY          ,  16, "Device or resource busy" )
-__BIONIC_ERRDEF( EEXIST         ,  17, "File exists" )
-__BIONIC_ERRDEF( EXDEV          ,  18, "Cross-device link" )
-__BIONIC_ERRDEF( ENODEV         ,  19, "No such device" )
-__BIONIC_ERRDEF( ENOTDIR        ,  20, "Not a directory" )
-__BIONIC_ERRDEF( EISDIR         ,  21, "Is a directory" )
-__BIONIC_ERRDEF( EINVAL         ,  22, "Invalid argument" )
-__BIONIC_ERRDEF( ENFILE         ,  23, "File table overflow" )
-__BIONIC_ERRDEF( EMFILE         ,  24, "Too many open files" )
-__BIONIC_ERRDEF( ENOTTY         ,  25, "Not a typewriter" )
-__BIONIC_ERRDEF( ETXTBSY        ,  26, "Text file busy" )
-__BIONIC_ERRDEF( EFBIG          ,  27, "File too large" )
-__BIONIC_ERRDEF( ENOSPC         ,  28, "No space left on device" )
-__BIONIC_ERRDEF( ESPIPE         ,  29, "Illegal seek" )
-__BIONIC_ERRDEF( EROFS          ,  30, "Read-only file system" )
-__BIONIC_ERRDEF( EMLINK         ,  31, "Too many links" )
-__BIONIC_ERRDEF( EPIPE          ,  32, "Broken pipe" )
-__BIONIC_ERRDEF( EDOM           ,  33, "Math argument out of domain of func" )
-__BIONIC_ERRDEF( ERANGE         ,  34, "Math result not representable" )
-__BIONIC_ERRDEF( EDEADLK        ,  35, "Resource deadlock would occur" )
-__BIONIC_ERRDEF( ENAMETOOLONG   ,  36, "File name too long" )
-__BIONIC_ERRDEF( ENOLCK         ,  37, "No record locks available" )
-__BIONIC_ERRDEF( ENOSYS         ,  38, "Function not implemented" )
-__BIONIC_ERRDEF( ENOTEMPTY      ,  39, "Directory not empty" )
-__BIONIC_ERRDEF( ELOOP          ,  40, "Too many symbolic links encountered" )
-__BIONIC_ERRDEF( ENOMSG         ,  42, "No message of desired type" )
-__BIONIC_ERRDEF( EIDRM          ,  43, "Identifier removed" )
-__BIONIC_ERRDEF( ECHRNG         ,  44, "Channel number out of range" )
-__BIONIC_ERRDEF( EL2NSYNC       ,  45, "Level 2 not synchronized" )
-__BIONIC_ERRDEF( EL3HLT         ,  46, "Level 3 halted" )
-__BIONIC_ERRDEF( EL3RST         ,  47, "Level 3 reset" )
-__BIONIC_ERRDEF( ELNRNG         ,  48, "Link number out of range" )
-__BIONIC_ERRDEF( EUNATCH        ,  49, "Protocol driver not attached" )
-__BIONIC_ERRDEF( ENOCSI         ,  50, "No CSI structure available" )
-__BIONIC_ERRDEF( EL2HLT         ,  51, "Level 2 halted" )
-__BIONIC_ERRDEF( EBADE          ,  52, "Invalid exchange" )
-__BIONIC_ERRDEF( EBADR          ,  53, "Invalid request descriptor" )
-__BIONIC_ERRDEF( EXFULL         ,  54, "Exchange full" )
-__BIONIC_ERRDEF( ENOANO         ,  55, "No anode" )
-__BIONIC_ERRDEF( EBADRQC        ,  56, "Invalid request code" )
-__BIONIC_ERRDEF( EBADSLT        ,  57, "Invalid slot" )
-__BIONIC_ERRDEF( EBFONT         ,  59, "Bad font file format" )
-__BIONIC_ERRDEF( ENOSTR         ,  60, "Device not a stream" )
-__BIONIC_ERRDEF( ENODATA        ,  61, "No data available" )
-__BIONIC_ERRDEF( ETIME          ,  62, "Timer expired" )
-__BIONIC_ERRDEF( ENOSR          ,  63, "Out of streams resources" )
-__BIONIC_ERRDEF( ENONET         ,  64, "Machine is not on the network" )
-__BIONIC_ERRDEF( ENOPKG         ,  65, "Package not installed" )
-__BIONIC_ERRDEF( EREMOTE        ,  66, "Object is remote" )
-__BIONIC_ERRDEF( ENOLINK        ,  67, "Link has been severed" )
-__BIONIC_ERRDEF( EADV           ,  68, "Advertise error" )
-__BIONIC_ERRDEF( ESRMNT         ,  69, "Srmount error" )
-__BIONIC_ERRDEF( ECOMM          ,  70, "Communication error on send" )
-__BIONIC_ERRDEF( EPROTO         ,  71, "Protocol error" )
-__BIONIC_ERRDEF( EMULTIHOP      ,  72, "Multihop attempted" )
-__BIONIC_ERRDEF( EDOTDOT        ,  73, "RFS specific error" )
-__BIONIC_ERRDEF( EBADMSG        ,  74, "Not a data message" )
-__BIONIC_ERRDEF( EOVERFLOW      ,  75, "Value too large for defined data type" )
-__BIONIC_ERRDEF( ENOTUNIQ       ,  76, "Name not unique on network" )
-__BIONIC_ERRDEF( EBADFD         ,  77, "File descriptor in bad state" )
-__BIONIC_ERRDEF( EREMCHG        ,  78, "Remote address changed" )
-__BIONIC_ERRDEF( ELIBACC        ,  79, "Can not access a needed shared library" )
-__BIONIC_ERRDEF( ELIBBAD        ,  80, "Accessing a corrupted shared library" )
-__BIONIC_ERRDEF( ELIBSCN        ,  81, ".lib section in a.out corrupted" )
-__BIONIC_ERRDEF( ELIBMAX        ,  82, "Attempting to link in too many shared libraries" )
-__BIONIC_ERRDEF( ELIBEXEC       ,  83, "Cannot exec a shared library directly" )
-__BIONIC_ERRDEF( EILSEQ         ,  84, "Illegal byte sequence" )
-__BIONIC_ERRDEF( ERESTART       ,  85, "Interrupted system call should be restarted" )
-__BIONIC_ERRDEF( ESTRPIPE       ,  86, "Streams pipe error" )
-__BIONIC_ERRDEF( EUSERS         ,  87, "Too many users" )
-__BIONIC_ERRDEF( ENOTSOCK       ,  88, "Socket operation on non-socket" )
-__BIONIC_ERRDEF( EDESTADDRREQ   ,  89, "Destination address required" )
-__BIONIC_ERRDEF( EMSGSIZE       ,  90, "Message too long" )
-__BIONIC_ERRDEF( EPROTOTYPE     ,  91, "Protocol wrong type for socket" )
-__BIONIC_ERRDEF( ENOPROTOOPT    ,  92, "Protocol not available" )
-__BIONIC_ERRDEF( EPROTONOSUPPORT,  93, "Protocol not supported" )
-__BIONIC_ERRDEF( ESOCKTNOSUPPORT,  94, "Socket type not supported" )
-__BIONIC_ERRDEF( EOPNOTSUPP     ,  95, "Operation not supported on transport endpoint" )
-__BIONIC_ERRDEF( EPFNOSUPPORT   ,  96, "Protocol family not supported" )
-__BIONIC_ERRDEF( EAFNOSUPPORT   ,  97, "Address family not supported by protocol" )
-__BIONIC_ERRDEF( EADDRINUSE     ,  98, "Address already in use" )
-__BIONIC_ERRDEF( EADDRNOTAVAIL  ,  99, "Cannot assign requested address" )
-__BIONIC_ERRDEF( ENETDOWN       , 100, "Network is down" )
-__BIONIC_ERRDEF( ENETUNREACH    , 101, "Network is unreachable" )
-__BIONIC_ERRDEF( ENETRESET      , 102, "Network dropped connection because of reset" )
-__BIONIC_ERRDEF( ECONNABORTED   , 103, "Software caused connection abort" )
-__BIONIC_ERRDEF( ECONNRESET     , 104, "Connection reset by peer" )
-__BIONIC_ERRDEF( ENOBUFS        , 105, "No buffer space available" )
-__BIONIC_ERRDEF( EISCONN        , 106, "Transport endpoint is already connected" )
-__BIONIC_ERRDEF( ENOTCONN       , 107, "Transport endpoint is not connected" )
-__BIONIC_ERRDEF( ESHUTDOWN      , 108, "Cannot send after transport endpoint shutdown" )
-__BIONIC_ERRDEF( ETOOMANYREFS   , 109, "Too many references: cannot splice" )
-__BIONIC_ERRDEF( ETIMEDOUT      , 110, "Connection timed out" )
-__BIONIC_ERRDEF( ECONNREFUSED   , 111, "Connection refused" )
-__BIONIC_ERRDEF( EHOSTDOWN      , 112, "Host is down" )
-__BIONIC_ERRDEF( EHOSTUNREACH   , 113, "No route to host" )
-__BIONIC_ERRDEF( EALREADY       , 114, "Operation already in progress" )
-__BIONIC_ERRDEF( EINPROGRESS    , 115, "Operation now in progress" )
-__BIONIC_ERRDEF( ESTALE         , 116, "Stale NFS file handle" )
-__BIONIC_ERRDEF( EUCLEAN        , 117, "Structure needs cleaning" )
-__BIONIC_ERRDEF( ENOTNAM        , 118, "Not a XENIX named type file" )
-__BIONIC_ERRDEF( ENAVAIL        , 119, "No XENIX semaphores available" )
-__BIONIC_ERRDEF( EISNAM         , 120, "Is a named type file" )
-__BIONIC_ERRDEF( EREMOTEIO      , 121, "Remote I/O error" )
-__BIONIC_ERRDEF( EDQUOT         , 122, "Quota exceeded" )
-__BIONIC_ERRDEF( ENOMEDIUM      , 123, "No medium found" )
-__BIONIC_ERRDEF( EMEDIUMTYPE    , 124, "Wrong medium type" )
-__BIONIC_ERRDEF( ECANCELED      , 125, "Operation Canceled" )
-__BIONIC_ERRDEF( ENOKEY         , 126, "Required key not available" )
-__BIONIC_ERRDEF( EKEYEXPIRED    , 127, "Key has expired" )
-__BIONIC_ERRDEF( EKEYREVOKED    , 128, "Key has been revoked" )
-__BIONIC_ERRDEF( EKEYREJECTED   , 129, "Key was rejected by service" )
-__BIONIC_ERRDEF( EOWNERDEAD     , 130, "Owner died" )
-__BIONIC_ERRDEF( ENOTRECOVERABLE, 131, "State not recoverable" )
-
-#undef __BIONIC_ERRDEF
diff --git a/libc/private/bionic_malloc_dispatch.h b/libc/private/bionic_malloc_dispatch.h
index 0dce03d..f15b72c 100644
--- a/libc/private/bionic_malloc_dispatch.h
+++ b/libc/private/bionic_malloc_dispatch.h
@@ -31,6 +31,7 @@
 
 #include <stddef.h>
 #include <stdint.h>
+#include <stdatomic.h>
 #include <private/bionic_config.h>
 
 // Entry in malloc dispatch table.
@@ -54,25 +55,25 @@
 #endif
 
 struct MallocDispatch {
-  MallocCalloc calloc;
-  MallocFree free;
-  MallocMallinfo mallinfo;
-  MallocMalloc malloc;
-  MallocMallocUsableSize malloc_usable_size;
-  MallocMemalign memalign;
-  MallocPosixMemalign posix_memalign;
+  _Atomic MallocCalloc calloc;
+  _Atomic MallocFree free;
+  _Atomic MallocMallinfo mallinfo;
+  _Atomic MallocMalloc malloc;
+  _Atomic MallocMallocUsableSize malloc_usable_size;
+  _Atomic MallocMemalign memalign;
+  _Atomic MallocPosixMemalign posix_memalign;
 #if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
-  MallocPvalloc pvalloc;
+  _Atomic MallocPvalloc pvalloc;
 #endif
-  MallocRealloc realloc;
+  _Atomic MallocRealloc realloc;
 #if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
-  MallocValloc valloc;
+  _Atomic MallocValloc valloc;
 #endif
-  MallocIterate iterate;
-  MallocMallocDisable malloc_disable;
-  MallocMallocEnable malloc_enable;
-  MallocMallopt mallopt;
-  MallocAlignedAlloc aligned_alloc;
+  _Atomic MallocIterate iterate;
+  _Atomic MallocMallocDisable malloc_disable;
+  _Atomic MallocMallocEnable malloc_enable;
+  _Atomic MallocMallopt mallopt;
+  _Atomic MallocAlignedAlloc aligned_alloc;
 } __attribute__((aligned(32)));
 
 #endif
diff --git a/libc/private/sigrtmin.h b/libc/private/sigrtmin.h
index 431a1dd..67bd864 100644
--- a/libc/private/sigrtmin.h
+++ b/libc/private/sigrtmin.h
@@ -39,11 +39,12 @@
 //   33 (__SIGRTMIN + 1)        libbacktrace
 //   34 (__SIGRTMIN + 2)        libcore
 //   35 (__SIGRTMIN + 3)        debuggerd -b
+//   36 (__SIGRTMIN + 4)        heapprofd
 //
 // If you change this, also change __ndk_legacy___libc_current_sigrtmin
 // in <android/legacy_signal_inlines.h> to match.
 
-#define __SIGRT_RESERVED 4
+#define __SIGRT_RESERVED 5
 static inline __always_inline sigset64_t filter_reserved_signals(sigset64_t sigset, int how) {
   int (*block)(sigset64_t*, int);
   int (*unblock)(sigset64_t*, int);
@@ -68,5 +69,6 @@
   unblock(&sigset, __SIGRTMIN + 1);
   unblock(&sigset, __SIGRTMIN + 2);
   unblock(&sigset, __SIGRTMIN + 3);
+  unblock(&sigset, __SIGRTMIN + 4);
   return sigset;
 }
diff --git a/tests/grp_pwd_test.cpp b/tests/grp_pwd_test.cpp
index 24a8648..1c36dcf 100644
--- a/tests/grp_pwd_test.cpp
+++ b/tests/grp_pwd_test.cpp
@@ -33,6 +33,10 @@
 #include <android-base/strings.h>
 #include <private/android_filesystem_config.h>
 
+#if defined(__BIONIC__)
+#include <android-base/properties.h>
+#endif
+
 // Generated android_ids array
 #include "generated_android_ids.h"
 
@@ -217,6 +221,7 @@
   check_get_passwd("u1_i0", 199000, TYPE_APP);
 }
 
+#if defined(__BIONIC__)
 template <typename T>
 static void expect_ids(const T& ids) {
   std::set<typename T::key_type> expected_ids;
@@ -243,6 +248,12 @@
   expect_range(AID_SHARED_GID_START, AID_SHARED_GID_END);
   expect_range(AID_ISOLATED_START, AID_ISOLATED_END);
 
+  // Upgrading devices launched before API level 28 may not comply with the below check.
+  // Due to the difficulty in changing uids after launch, it is waived for these devices.
+  if (android::base::GetIntProperty("ro.product.first_api_level", 0) < 28) {
+    return;
+  }
+
   // Ensure that no other ids were returned.
   auto return_differences = [&ids, &expected_ids] {
     std::vector<typename T::key_type> missing_from_ids;
@@ -263,6 +274,7 @@
   };
   EXPECT_EQ(expected_ids, ids) << return_differences();
 }
+#endif
 
 TEST(pwd, getpwent_iterate) {
 #if defined(__BIONIC__)
diff --git a/tests/pthread_test.cpp b/tests/pthread_test.cpp
index fc8945c..e68f1ff 100644
--- a/tests/pthread_test.cpp
+++ b/tests/pthread_test.cpp
@@ -31,6 +31,7 @@
 #include <unwind.h>
 
 #include <atomic>
+#include <future>
 #include <vector>
 
 #include <android-base/parseint.h>
@@ -539,6 +540,25 @@
   ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM));
 }
 
+TEST(pthread, pthread_kill__exited_thread) {
+  static std::promise<pid_t> tid_promise;
+  pthread_t thread;
+  ASSERT_EQ(0, pthread_create(&thread, nullptr,
+                              [](void*) -> void* {
+                                tid_promise.set_value(gettid());
+                                return nullptr;
+                              },
+                              nullptr));
+
+  pid_t tid = tid_promise.get_future().get();
+  while (TEMP_FAILURE_RETRY(syscall(__NR_tgkill, getpid(), tid, 0)) != -1) {
+    continue;
+  }
+  ASSERT_EQ(ESRCH, errno);
+
+  ASSERT_EQ(ESRCH, pthread_kill(thread, 0));
+}
+
 TEST_F(pthread_DeathTest, pthread_detach__no_such_thread) {
   pthread_t dead_thread;
   MakeDeadThread(dead_thread);