Merge "Disable clang for mips/mips64 libc."
diff --git a/libc/Android.bp b/libc/Android.bp
index d1228af..2de8cf6 100644
--- a/libc/Android.bp
+++ b/libc/Android.bp
@@ -177,6 +177,7 @@
         "-DALL_STATE",
         // Include tzsetwall, timelocal, timegm, time2posix, and posix2time.
         "-DSTD_INSPIRED",
+        // Obviously, we want to be thread-safe.
         "-DTHREAD_SAFE",
         // The name of the tm_gmtoff field in our struct tm.
         "-DTM_GMTOFF=tm_gmtoff",
@@ -184,6 +185,9 @@
         "-DTZDIR=\\\"/system/usr/share/zoneinfo\\\"",
         // Include timezone and daylight globals.
         "-DUSG_COMPAT=1",
+        // Use the empty string (instead of "   ") as the timezone abbreviation
+        // fallback.
+        "-DWILDABBR=\\\"\\\"",
         "-DNO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU",
         "-Dlint",
     ],
@@ -1460,9 +1464,13 @@
     cflags: ["-Wframe-larger-than=2048"],
     cppflags: ["-Wold-style-cast"],
     include_dirs: ["bionic/libstdc++/include"],
-    // TODO: Clang tries to use __tls_get_addr which is not supported yet
-    // remove after it is implemented.
-    clang: false,
+
+    arch: {
+        arm64: {
+            // b/25662915, clang compiled __cxa_thread_atexit_impl.cpp still failed.
+            clang: false,
+        },
+    },
 }
 
 // ========================================================
@@ -1478,6 +1486,7 @@
     srcs: [
         "bionic/pthread_atfork.cpp",
         "bionic/pthread_attr.cpp",
+        "bionic/pthread_barrier.cpp",
         "bionic/pthread_cond.cpp",
         "bionic/pthread_create.cpp",
         "bionic/pthread_detach.cpp",
@@ -1497,6 +1506,7 @@
         "bionic/pthread_setname_np.cpp",
         "bionic/pthread_setschedparam.cpp",
         "bionic/pthread_sigmask.cpp",
+        "bionic/pthread_spinlock.cpp",
     ],
     cflags: ["-Wframe-larger-than=2048"],
 
diff --git a/libc/Android.mk b/libc/Android.mk
index 601a3d7..2d97f35 100644
--- a/libc/Android.mk
+++ b/libc/Android.mk
@@ -564,6 +564,7 @@
 libc_pthread_src_files := \
     bionic/pthread_atfork.cpp \
     bionic/pthread_attr.cpp \
+    bionic/pthread_barrier.cpp \
     bionic/pthread_cond.cpp \
     bionic/pthread_create.cpp \
     bionic/pthread_detach.cpp \
@@ -583,6 +584,7 @@
     bionic/pthread_setname_np.cpp \
     bionic/pthread_setschedparam.cpp \
     bionic/pthread_sigmask.cpp \
+    bionic/pthread_spinlock.cpp \
 
 libc_thread_atexit_impl_src_files := \
     bionic/__cxa_thread_atexit_impl.cpp \
diff --git a/libc/bionic/libc_init_common.cpp b/libc/bionic/libc_init_common.cpp
index a683748..8f1ee95 100644
--- a/libc/bionic/libc_init_common.cpp
+++ b/libc/bionic/libc_init_common.cpp
@@ -110,6 +110,7 @@
   // Initialize libc globals that are needed in both the linker and in libc.
   // In dynamic binaries, this is run at least twice for different copies of the
   // globals, once for the linker's copy and once for the one in libc.so.
+  __libc_auxv = args.auxv;
   __libc_globals.initialize();
   __libc_globals.mutate([&args](libc_globals* globals) {
     __libc_init_vdso(globals, args);
@@ -121,7 +122,6 @@
   // Initialize various globals.
   environ = args.envp;
   errno = 0;
-  __libc_auxv = args.auxv;
   __progname = args.argv[0] ? args.argv[0] : "<unknown>";
   __abort_message_ptr = args.abort_message_ptr;
 
diff --git a/libc/bionic/mmap.cpp b/libc/bionic/mmap.cpp
index 9bc80a2..57a8cdf 100644
--- a/libc/bionic/mmap.cpp
+++ b/libc/bionic/mmap.cpp
@@ -27,6 +27,7 @@
  */
 
 #include <errno.h>
+#include <stdint.h>
 #include <sys/mman.h>
 #include <unistd.h>
 
@@ -53,10 +54,14 @@
     return MAP_FAILED;
   }
 
-  bool is_private_anonymous = (flags & (MAP_PRIVATE | MAP_ANONYMOUS)) != 0;
+  bool is_private_anonymous =
+      (flags & (MAP_PRIVATE | MAP_ANONYMOUS)) == (MAP_PRIVATE | MAP_ANONYMOUS);
+  bool is_stack_or_grows_down = (flags & (MAP_STACK | MAP_GROWSDOWN)) != 0;
+
   void* result = __mmap2(addr, size, prot, flags, fd, offset >> MMAP2_SHIFT);
 
-  if (result != MAP_FAILED && kernel_has_MADV_MERGEABLE && is_private_anonymous) {
+  if (result != MAP_FAILED && kernel_has_MADV_MERGEABLE &&
+      is_private_anonymous && !is_stack_or_grows_down) {
     ErrnoRestorer errno_restorer;
     int rc = madvise(result, size, MADV_MERGEABLE);
     if (rc == -1 && errno == EINVAL) {
diff --git a/libc/bionic/mremap.cpp b/libc/bionic/mremap.cpp
index 4892b1d..6653d43 100644
--- a/libc/bionic/mremap.cpp
+++ b/libc/bionic/mremap.cpp
@@ -26,12 +26,24 @@
  * SUCH DAMAGE.
  */
 
+#include <errno.h>
 #include <sys/mman.h>
 #include <stdarg.h>
+#include <stdint.h>
+#include <unistd.h>
+
+#include "private/bionic_macros.h"
 
 extern "C" void* ___mremap(void*, size_t, size_t, int, void*);
 
 void* mremap(void* old_address, size_t old_size, size_t new_size, int flags, ...) {
+  // prevent allocations large enough for `end - start` to overflow
+  size_t rounded = BIONIC_ALIGN(new_size, PAGE_SIZE);
+  if (rounded < new_size || rounded > PTRDIFF_MAX) {
+    errno = ENOMEM;
+    return MAP_FAILED;
+  }
+
   void* new_address = nullptr;
   // The optional argument is only valid if the MREMAP_FIXED flag is set,
   // so we assume it's not present otherwise.
diff --git a/libc/bionic/pthread_barrier.cpp b/libc/bionic/pthread_barrier.cpp
new file mode 100644
index 0000000..3227daf
--- /dev/null
+++ b/libc/bionic/pthread_barrier.cpp
@@ -0,0 +1,183 @@
+/*
+ * Copyright (C) 2015 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.
+ */
+
+#include <pthread.h>
+#include <stdatomic.h>
+#include <stdint.h>
+
+#include "private/bionic_futex.h"
+
+int pthread_barrierattr_init(pthread_barrierattr_t* attr) {
+  *attr = 0;
+  return 0;
+}
+
+int pthread_barrierattr_destroy(pthread_barrierattr_t* attr) {
+  *attr = 0;
+  return 0;
+}
+
+int pthread_barrierattr_getpshared(pthread_barrierattr_t* attr, int* pshared) {
+  *pshared = (*attr & 1) ? PTHREAD_PROCESS_SHARED : PTHREAD_PROCESS_PRIVATE;
+  return 0;
+}
+
+int pthread_barrierattr_setpshared(pthread_barrierattr_t* attr, int pshared) {
+  if (pshared == PTHREAD_PROCESS_SHARED) {
+    *attr |= 1;
+  } else {
+    *attr &= ~1;
+  }
+  return 0;
+}
+
+enum BarrierState {
+  WAIT,
+  RELEASE,
+};
+
+struct pthread_barrier_internal_t {
+  // One barrier can be used for unlimited number of cycles. In each cycle, [init_count]
+  // threads must call pthread_barrier_wait() before any of them successfully return from
+  // the call. It is undefined behavior if there are more than [init_count] threads call
+  // pthread_barrier_wait() in one cycle.
+  uint32_t init_count;
+  // Barrier state. It is WAIT if waiting for more threads to enter the barrier in this cycle,
+  // otherwise threads are leaving the barrier.
+  _Atomic(BarrierState) state;
+  // Number of threads having entered but not left the barrier in this cycle.
+  atomic_uint wait_count;
+  // Whether the barrier is shared across processes.
+  bool pshared;
+  uint32_t __reserved[4];
+};
+
+static_assert(sizeof(pthread_barrier_t) == sizeof(pthread_barrier_internal_t),
+              "pthread_barrier_t should actually be pthread_barrier_internal_t in implementation."
+              );
+
+static_assert(alignof(pthread_barrier_t) >= 4,
+              "pthread_barrier_t should fulfill the alignment of pthread_barrier_internal_t.");
+
+static inline pthread_barrier_internal_t* __get_internal_barrier(pthread_barrier_t* barrier) {
+  return reinterpret_cast<pthread_barrier_internal_t*>(barrier);
+}
+
+int pthread_barrier_init(pthread_barrier_t* barrier_interface, const pthread_barrierattr_t* attr,
+                         unsigned count) {
+  pthread_barrier_internal_t* barrier = __get_internal_barrier(barrier_interface);
+  if (count == 0) {
+    return EINVAL;
+  }
+  barrier->init_count = count;
+  atomic_init(&barrier->state, WAIT);
+  atomic_init(&barrier->wait_count, 0);
+  barrier->pshared = false;
+  if (attr != nullptr && (*attr & 1)) {
+    barrier->pshared = true;
+  }
+  return 0;
+}
+
+// According to POSIX standard, pthread_barrier_wait() synchronizes memory between participating
+// threads. It means all memory operations made by participating threads before calling
+// pthread_barrier_wait() can be seen by all participating threads after the function call.
+// We establish this by making a happens-before relation between all threads entering the barrier
+// with the last thread entering the barrier, and a happens-before relation between the last
+// thread entering the barrier with all threads leaving the barrier.
+int pthread_barrier_wait(pthread_barrier_t* barrier_interface) {
+  pthread_barrier_internal_t* barrier = __get_internal_barrier(barrier_interface);
+
+  // Wait until all threads for the previous cycle have left the barrier. This is needed
+  // as a participating thread can call pthread_barrier_wait() again before other
+  // threads have left the barrier. Use acquire operation here to synchronize with
+  // the last thread leaving the previous cycle, so we can read correct wait_count below.
+  while(atomic_load_explicit(&barrier->state, memory_order_acquire) == RELEASE) {
+    __futex_wait_ex(&barrier->state, barrier->pshared, RELEASE, nullptr);
+  }
+
+  uint32_t prev_wait_count = atomic_load_explicit(&barrier->wait_count, memory_order_relaxed);
+  while (true) {
+    // It happens when there are more than [init_count] threads trying to enter the barrier
+    // at one cycle. We read the POSIX standard as disallowing this, since additional arriving
+    // threads are not synchronized with respect to the barrier reset. We also don't know of
+    // any reasonable cases in which this would be intentional.
+    if (prev_wait_count >= barrier->init_count) {
+      return EINVAL;
+    }
+    // Use memory_order_acq_rel operation here to synchronize between all threads entering
+    // the barrier with the last thread entering the barrier.
+    if (atomic_compare_exchange_weak_explicit(&barrier->wait_count, &prev_wait_count,
+                                              prev_wait_count + 1u, memory_order_acq_rel,
+                                              memory_order_relaxed)) {
+      break;
+    }
+  }
+
+  int result = 0;
+  if (prev_wait_count + 1 == barrier->init_count) {
+    result = PTHREAD_BARRIER_SERIAL_THREAD;
+    if (prev_wait_count != 0) {
+      // Use release operation here to synchronize between the last thread entering the
+      // barrier with all threads leaving the barrier.
+      atomic_store_explicit(&barrier->state, RELEASE, memory_order_release);
+      __futex_wake_ex(&barrier->state, barrier->pshared, prev_wait_count);
+    }
+  } else {
+    // Use acquire operation here to synchronize between the last thread entering the
+    // barrier with all threads leaving the barrier.
+    while (atomic_load_explicit(&barrier->state, memory_order_acquire) == WAIT) {
+      __futex_wait_ex(&barrier->state, barrier->pshared, WAIT, nullptr);
+    }
+  }
+  // Use release operation here to make it not reordered with previous operations.
+  if (atomic_fetch_sub_explicit(&barrier->wait_count, 1, memory_order_release) == 1) {
+    // Use release operation here to synchronize with threads entering the barrier for
+    // the next cycle, or the thread calling pthread_barrier_destroy().
+    atomic_store_explicit(&barrier->state, WAIT, memory_order_release);
+    __futex_wake_ex(&barrier->state, barrier->pshared, barrier->init_count);
+  }
+  return result;
+}
+
+int pthread_barrier_destroy(pthread_barrier_t* barrier_interface) {
+  pthread_barrier_internal_t* barrier = __get_internal_barrier(barrier_interface);
+  if (barrier->init_count == 0) {
+    return EINVAL;
+  }
+  // Use acquire operation here to synchronize with the last thread leaving the barrier.
+  // So we can read correct wait_count below.
+  while (atomic_load_explicit(&barrier->state, memory_order_acquire) == RELEASE) {
+    __futex_wait_ex(&barrier->state, barrier->pshared, RELEASE, nullptr);
+  }
+  if (atomic_load_explicit(&barrier->wait_count, memory_order_relaxed) != 0) {
+    return EBUSY;
+  }
+  barrier->init_count = 0;
+  return 0;
+}
diff --git a/libc/bionic/pthread_mutex.cpp b/libc/bionic/pthread_mutex.cpp
index 851fc3d..3da4bcf 100644
--- a/libc/bionic/pthread_mutex.cpp
+++ b/libc/bionic/pthread_mutex.cpp
@@ -166,11 +166,14 @@
 #define  MUTEX_STATE_BITS_LOCKED_UNCONTENDED  MUTEX_STATE_TO_BITS(MUTEX_STATE_LOCKED_UNCONTENDED)
 #define  MUTEX_STATE_BITS_LOCKED_CONTENDED    MUTEX_STATE_TO_BITS(MUTEX_STATE_LOCKED_CONTENDED)
 
-/* return true iff the mutex if locked with no waiters */
-#define  MUTEX_STATE_BITS_IS_LOCKED_UNCONTENDED(v)  (((v) & MUTEX_STATE_MASK) == MUTEX_STATE_BITS_LOCKED_UNCONTENDED)
+// Return true iff the mutex is unlocked.
+#define MUTEX_STATE_BITS_IS_UNLOCKED(v) (((v) & MUTEX_STATE_MASK) == MUTEX_STATE_BITS_UNLOCKED)
 
-/* return true iff the mutex if locked with maybe waiters */
-#define  MUTEX_STATE_BITS_IS_LOCKED_CONTENDED(v)   (((v) & MUTEX_STATE_MASK) == MUTEX_STATE_BITS_LOCKED_CONTENDED)
+// Return true iff the mutex is locked with no waiters.
+#define MUTEX_STATE_BITS_IS_LOCKED_UNCONTENDED(v)  (((v) & MUTEX_STATE_MASK) == MUTEX_STATE_BITS_LOCKED_UNCONTENDED)
+
+// return true iff the mutex is locked with maybe waiters.
+#define MUTEX_STATE_BITS_IS_LOCKED_CONTENDED(v)   (((v) & MUTEX_STATE_MASK) == MUTEX_STATE_BITS_LOCKED_CONTENDED)
 
 /* used to flip from LOCKED_UNCONTENDED to LOCKED_CONTENDED */
 #define  MUTEX_STATE_BITS_FLIP_CONTENTION(v)      ((v) ^ (MUTEX_STATE_BITS_LOCKED_CONTENDED ^ MUTEX_STATE_BITS_LOCKED_UNCONTENDED))
@@ -637,10 +640,14 @@
 }
 
 int pthread_mutex_destroy(pthread_mutex_t* mutex_interface) {
-    // Use trylock to ensure that the mutex is valid and not already locked.
-    int error = pthread_mutex_trylock(mutex_interface);
-    if (error != 0) {
-        return error;
+    pthread_mutex_internal_t* mutex = __get_internal_mutex(mutex_interface);
+    uint16_t old_state = atomic_load_explicit(&mutex->state, memory_order_relaxed);
+    // Store 0xffff to make the mutex unusable. Although POSIX standard says it is undefined
+    // behavior to destroy a locked mutex, we prefer not to change mutex->state in that situation.
+    if (MUTEX_STATE_BITS_IS_UNLOCKED(old_state) &&
+        atomic_compare_exchange_strong_explicit(&mutex->state, &old_state, 0xffff,
+                                                memory_order_relaxed, memory_order_relaxed)) {
+      return 0;
     }
-    return 0;
+    return EBUSY;
 }
diff --git a/libc/bionic/pthread_spinlock.cpp b/libc/bionic/pthread_spinlock.cpp
new file mode 100644
index 0000000..f26f283
--- /dev/null
+++ b/libc/bionic/pthread_spinlock.cpp
@@ -0,0 +1,81 @@
+/*
+ * Copyright (C) 2015 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.
+ */
+#include <pthread.h>
+
+#include "private/bionic_lock.h"
+
+// User-level spinlocks can be hazardous to battery life on Android.
+// We implement a simple compromise that behaves mostly like a spinlock,
+// but prevents excessively long spinning.
+
+struct pthread_spinlock_internal_t {
+  Lock lock;
+};
+
+static_assert(sizeof(pthread_spinlock_t) == sizeof(pthread_spinlock_internal_t),
+              "pthread_spinlock_t should actually be pthread_spinlock_internal_t.");
+
+static_assert(alignof(pthread_spinlock_t) >= 4,
+              "pthread_spinlock_t should fulfill the alignment of pthread_spinlock_internal_t.");
+
+static inline pthread_spinlock_internal_t* __get_internal_spinlock(pthread_spinlock_t* lock) {
+  return reinterpret_cast<pthread_spinlock_internal_t*>(lock);
+}
+
+int pthread_spin_init(pthread_spinlock_t* lock_interface, int pshared) {
+  pthread_spinlock_internal_t* lock = __get_internal_spinlock(lock_interface);
+  lock->lock.init(pshared);
+  return 0;
+}
+
+int pthread_spin_destroy(pthread_spinlock_t* lock_interface) {
+  pthread_spinlock_internal_t* lock = __get_internal_spinlock(lock_interface);
+  return lock->lock.trylock() ? 0 : EBUSY;
+}
+
+int pthread_spin_trylock(pthread_spinlock_t* lock_interface) {
+  pthread_spinlock_internal_t* lock = __get_internal_spinlock(lock_interface);
+  return lock->lock.trylock() ? 0 : EBUSY;
+}
+
+int pthread_spin_lock(pthread_spinlock_t* lock_interface) {
+  pthread_spinlock_internal_t* lock = __get_internal_spinlock(lock_interface);
+  for (int i = 0; i < 10000; ++i) {
+    if (lock->lock.trylock()) {
+      return 0;
+    }
+  }
+  lock->lock.lock();
+  return 0;
+}
+
+int pthread_spin_unlock(pthread_spinlock_t* lock_interface) {
+  pthread_spinlock_internal_t* lock = __get_internal_spinlock(lock_interface);
+  lock->lock.unlock();
+  return 0;
+}
diff --git a/libc/include/pthread.h b/libc/include/pthread.h
index 260ae5b..21d34fb 100644
--- a/libc/include/pthread.h
+++ b/libc/include/pthread.h
@@ -96,6 +96,26 @@
 
 #define PTHREAD_ONCE_INIT 0
 
+typedef struct {
+#if defined(__LP64__)
+  int64_t __private[4];
+#else
+  int32_t __private[8];
+#endif
+} pthread_barrier_t;
+
+typedef int pthread_barrierattr_t;
+
+#define PTHREAD_BARRIER_SERIAL_THREAD -1
+
+typedef struct {
+#if defined(__LP64__)
+  int64_t __private;
+#else
+  int32_t __private[2];
+#endif
+} pthread_spinlock_t;
+
 #if defined(__LP64__)
 #define PTHREAD_STACK_MIN (4 * PAGE_SIZE)
 #else
@@ -130,7 +150,7 @@
 int pthread_attr_setschedpolicy(pthread_attr_t*, int) __nonnull((1));
 int pthread_attr_setscope(pthread_attr_t*, int) __nonnull((1));
 int pthread_attr_setstack(pthread_attr_t*, void*, size_t) __nonnull((1));
-int pthread_attr_setstacksize(pthread_attr_t*, size_t stack_size) __nonnull((1));
+int pthread_attr_setstacksize(pthread_attr_t*, size_t) __nonnull((1));
 
 int pthread_condattr_destroy(pthread_condattr_t*) __nonnull((1));
 int pthread_condattr_getclock(const pthread_condattr_t*, clockid_t*) __nonnull((1, 2));
@@ -205,9 +225,24 @@
 int pthread_rwlock_timedwrlock(pthread_rwlock_t*, const struct timespec*) __nonnull((1, 2));
 int pthread_rwlock_tryrdlock(pthread_rwlock_t*) __nonnull((1));
 int pthread_rwlock_trywrlock(pthread_rwlock_t*) __nonnull((1));
-int pthread_rwlock_unlock(pthread_rwlock_t *rwlock) __nonnull((1));
+int pthread_rwlock_unlock(pthread_rwlock_t *) __nonnull((1));
 int pthread_rwlock_wrlock(pthread_rwlock_t*) __nonnull((1));
 
+int pthread_barrierattr_init(pthread_barrierattr_t* attr) __nonnull((1));
+int pthread_barrierattr_destroy(pthread_barrierattr_t* attr) __nonnull((1));
+int pthread_barrierattr_getpshared(pthread_barrierattr_t* attr, int* pshared) __nonnull((1, 2));
+int pthread_barrierattr_setpshared(pthread_barrierattr_t* attr, int pshared) __nonnull((1));
+
+int pthread_barrier_init(pthread_barrier_t*, const pthread_barrierattr_t*, unsigned) __nonnull((1));
+int pthread_barrier_destroy(pthread_barrier_t*) __nonnull((1));
+int pthread_barrier_wait(pthread_barrier_t*) __nonnull((1));
+
+int pthread_spin_destroy(pthread_spinlock_t*) __nonnull((1));
+int pthread_spin_init(pthread_spinlock_t*, int) __nonnull((1));
+int pthread_spin_lock(pthread_spinlock_t*) __nonnull((1));
+int pthread_spin_trylock(pthread_spinlock_t*) __nonnull((1));
+int pthread_spin_unlock(pthread_spinlock_t*) __nonnull((1));
+
 pthread_t pthread_self(void) __pure2;
 
 int pthread_setname_np(pthread_t, const char*) __nonnull((2));
diff --git a/libc/libc.arm.map b/libc/libc.arm.map
index b632d3b..69aa272 100644
--- a/libc/libc.arm.map
+++ b/libc/libc.arm.map
@@ -1316,6 +1316,18 @@
     preadv;
     preadv64;
     prlimit; # arm mips x86
+    pthread_barrierattr_destroy;
+    pthread_barrierattr_getpshared;
+    pthread_barrierattr_init;
+    pthread_barrierattr_setpshared;
+    pthread_barrier_destroy;
+    pthread_barrier_init;
+    pthread_barrier_wait;
+    pthread_spin_destroy;
+    pthread_spin_init;
+    pthread_spin_lock;
+    pthread_spin_trylock;
+    pthread_spin_unlock;
     pwritev;
     pwritev64;
     scandirat;
diff --git a/libc/libc.arm64.map b/libc/libc.arm64.map
index 38023e5..763f77e 100644
--- a/libc/libc.arm64.map
+++ b/libc/libc.arm64.map
@@ -1161,6 +1161,18 @@
     getgrnam_r;
     preadv;
     preadv64;
+    pthread_barrierattr_destroy;
+    pthread_barrierattr_getpshared;
+    pthread_barrierattr_init;
+    pthread_barrierattr_setpshared;
+    pthread_barrier_destroy;
+    pthread_barrier_init;
+    pthread_barrier_wait;
+    pthread_spin_destroy;
+    pthread_spin_init;
+    pthread_spin_lock;
+    pthread_spin_trylock;
+    pthread_spin_unlock;
     pwritev;
     pwritev64;
     scandirat;
diff --git a/libc/libc.map.txt b/libc/libc.map.txt
index c378456..76cb168 100644
--- a/libc/libc.map.txt
+++ b/libc/libc.map.txt
@@ -1343,6 +1343,18 @@
     preadv;
     preadv64;
     prlimit; # arm mips x86
+    pthread_barrierattr_destroy;
+    pthread_barrierattr_getpshared;
+    pthread_barrierattr_init;
+    pthread_barrierattr_setpshared;
+    pthread_barrier_destroy;
+    pthread_barrier_init;
+    pthread_barrier_wait;
+    pthread_spin_destroy;
+    pthread_spin_init;
+    pthread_spin_lock;
+    pthread_spin_trylock;
+    pthread_spin_unlock;
     pwritev;
     pwritev64;
     scandirat;
diff --git a/libc/libc.mips.map b/libc/libc.mips.map
index 23123dc..e268bad 100644
--- a/libc/libc.mips.map
+++ b/libc/libc.mips.map
@@ -1279,6 +1279,18 @@
     preadv;
     preadv64;
     prlimit; # arm mips x86
+    pthread_barrierattr_destroy;
+    pthread_barrierattr_getpshared;
+    pthread_barrierattr_init;
+    pthread_barrierattr_setpshared;
+    pthread_barrier_destroy;
+    pthread_barrier_init;
+    pthread_barrier_wait;
+    pthread_spin_destroy;
+    pthread_spin_init;
+    pthread_spin_lock;
+    pthread_spin_trylock;
+    pthread_spin_unlock;
     pwritev;
     pwritev64;
     scandirat;
diff --git a/libc/libc.mips64.map b/libc/libc.mips64.map
index 38023e5..763f77e 100644
--- a/libc/libc.mips64.map
+++ b/libc/libc.mips64.map
@@ -1161,6 +1161,18 @@
     getgrnam_r;
     preadv;
     preadv64;
+    pthread_barrierattr_destroy;
+    pthread_barrierattr_getpshared;
+    pthread_barrierattr_init;
+    pthread_barrierattr_setpshared;
+    pthread_barrier_destroy;
+    pthread_barrier_init;
+    pthread_barrier_wait;
+    pthread_spin_destroy;
+    pthread_spin_init;
+    pthread_spin_lock;
+    pthread_spin_trylock;
+    pthread_spin_unlock;
     pwritev;
     pwritev64;
     scandirat;
diff --git a/libc/libc.x86.map b/libc/libc.x86.map
index f13ffb0..6578370 100644
--- a/libc/libc.x86.map
+++ b/libc/libc.x86.map
@@ -1277,6 +1277,18 @@
     preadv;
     preadv64;
     prlimit; # arm mips x86
+    pthread_barrierattr_destroy;
+    pthread_barrierattr_getpshared;
+    pthread_barrierattr_init;
+    pthread_barrierattr_setpshared;
+    pthread_barrier_destroy;
+    pthread_barrier_init;
+    pthread_barrier_wait;
+    pthread_spin_destroy;
+    pthread_spin_init;
+    pthread_spin_lock;
+    pthread_spin_trylock;
+    pthread_spin_unlock;
     pwritev;
     pwritev64;
     scandirat;
diff --git a/libc/libc.x86_64.map b/libc/libc.x86_64.map
index 38023e5..763f77e 100644
--- a/libc/libc.x86_64.map
+++ b/libc/libc.x86_64.map
@@ -1161,6 +1161,18 @@
     getgrnam_r;
     preadv;
     preadv64;
+    pthread_barrierattr_destroy;
+    pthread_barrierattr_getpshared;
+    pthread_barrierattr_init;
+    pthread_barrierattr_setpshared;
+    pthread_barrier_destroy;
+    pthread_barrier_init;
+    pthread_barrier_wait;
+    pthread_spin_destroy;
+    pthread_spin_init;
+    pthread_spin_lock;
+    pthread_spin_trylock;
+    pthread_spin_unlock;
     pwritev;
     pwritev64;
     scandirat;
diff --git a/libc/private/bionic_lock.h b/libc/private/bionic_lock.h
index a36d1ed..238ace4 100644
--- a/libc/private/bionic_lock.h
+++ b/libc/private/bionic_lock.h
@@ -30,6 +30,7 @@
 
 #include <stdatomic.h>
 #include "private/bionic_futex.h"
+#include "private/bionic_macros.h"
 
 // Lock is used in places like pthread_rwlock_t, which can be initialized without calling
 // an initialization function. So make sure Lock can be initialized by setting its memory to 0.
@@ -49,6 +50,12 @@
     this->process_shared = process_shared;
   }
 
+  bool trylock() {
+    LockState old_state = Unlocked;
+    return __predict_true(atomic_compare_exchange_strong_explicit(&state, &old_state,
+                        LockedWithoutWaiter, memory_order_acquire, memory_order_relaxed));
+  }
+
   void lock() {
     LockState old_state = Unlocked;
     if (__predict_true(atomic_compare_exchange_strong_explicit(&state, &old_state,
diff --git a/linker/linker.cpp b/linker/linker.cpp
index 826aa5e..1ecf65b 100644
--- a/linker/linker.cpp
+++ b/linker/linker.cpp
@@ -125,9 +125,11 @@
 
 static const char* const kDefaultLdPaths[] = {
 #if defined(__LP64__)
+  "/odm/lib64",
   "/vendor/lib64",
   "/system/lib64",
 #else
+  "/odm/lib",
   "/vendor/lib",
   "/system/lib",
 #endif
@@ -136,11 +138,15 @@
 
 static const char* const kAsanDefaultLdPaths[] = {
 #if defined(__LP64__)
+  "/data/odm/lib64",
+  "/odm/lib64",
   "/data/vendor/lib64",
   "/vendor/lib64",
   "/data/lib64",
   "/system/lib64",
 #else
+  "/data/odm/lib",
+  "/odm/lib",
   "/data/vendor/lib",
   "/vendor/lib",
   "/data/lib",
@@ -469,7 +475,7 @@
 
 static void split_path(const char* path, const char* delimiters,
                        std::vector<std::string>* paths) {
-  if (path != nullptr) {
+  if (path != nullptr && path[0] != 0) {
     *paths = android::base::Split(path, delimiters);
   }
 }
@@ -2255,9 +2261,12 @@
     g_public_namespace.clear();
   });
 
-  soinfo* candidate;
   for (const auto& soname : sonames) {
-    if (!find_loaded_library_by_soname(&g_default_namespace, soname.c_str(), &candidate)) {
+    soinfo* candidate = nullptr;
+
+    find_loaded_library_by_soname(&g_default_namespace, soname.c_str(), &candidate);
+
+    if (candidate == nullptr) {
       DL_ERR("Error initializing public namespace: \"%s\" was not found"
              " in the default namespace", soname.c_str());
       return false;
@@ -2731,14 +2740,14 @@
         MARK(rel->r_offset);
         TRACE_TYPE(RELO, "RELO R_X86_64_32 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
                    static_cast<size_t>(sym_addr), sym_name);
-        *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend;
+        *reinterpret_cast<Elf32_Addr*>(reloc) = sym_addr + addend;
         break;
       case R_X86_64_64:
         count_relocation(kRelocRelative);
         MARK(rel->r_offset);
         TRACE_TYPE(RELO, "RELO R_X86_64_64 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
                    static_cast<size_t>(sym_addr), sym_name);
-        *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend;
+        *reinterpret_cast<Elf64_Addr*>(reloc) = sym_addr + addend;
         break;
       case R_X86_64_PC32:
         count_relocation(kRelocRelative);
@@ -2746,7 +2755,7 @@
         TRACE_TYPE(RELO, "RELO R_X86_64_PC32 %08zx <- +%08zx (%08zx - %08zx) %s",
                    static_cast<size_t>(reloc), static_cast<size_t>(sym_addr - reloc),
                    static_cast<size_t>(sym_addr), static_cast<size_t>(reloc), sym_name);
-        *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - reloc;
+        *reinterpret_cast<Elf32_Addr*>(reloc) = sym_addr + addend - reloc;
         break;
 #elif defined(__arm__)
       case R_ARM_ABS32:
diff --git a/tests/dlext_test.cpp b/tests/dlext_test.cpp
index 83bd5cc..a56c771 100644
--- a/tests/dlext_test.cpp
+++ b/tests/dlext_test.cpp
@@ -707,6 +707,8 @@
   dlclose(handle2);
 }
 
+extern "C" void android_set_application_target_sdk_version(uint32_t target);
+
 TEST(dlext, ns_isolated) {
   static const char* root_lib = "libnstest_root_not_isolated.so";
   std::string path = std::string("libc.so:libc++.so:libdl.so:libm.so:") + g_public_lib;
@@ -715,6 +717,8 @@
   void* handle_public = dlopen((lib_path + "/public_namespace_libs/" + g_public_lib).c_str(), RTLD_NOW);
   ASSERT_TRUE(handle_public != nullptr) << dlerror();
 
+  android_set_application_target_sdk_version(42U); // something > 23
+
   ASSERT_TRUE(android_init_public_namespace(path.c_str())) << dlerror();
 
   android_namespace_t* ns_not_isolated = android_create_namespace("private", nullptr, (lib_path + "/private_namespace_libs").c_str(), false);
diff --git a/tests/pthread_test.cpp b/tests/pthread_test.cpp
index e764283..e237d26 100755
--- a/tests/pthread_test.cpp
+++ b/tests/pthread_test.cpp
@@ -721,11 +721,11 @@
   ASSERT_EQ(0, pthread_rwlock_destroy(&l));
 }
 
-static void WaitUntilThreadSleep(std::atomic<pid_t>& pid) {
-  while (pid == 0) {
+static void WaitUntilThreadSleep(std::atomic<pid_t>& tid) {
+  while (tid == 0) {
     usleep(1000);
   }
-  std::string filename = android::base::StringPrintf("/proc/%d/stat", pid.load());
+  std::string filename = android::base::StringPrintf("/proc/%d/stat", tid.load());
   std::regex regex {R"(\s+S\s+)"};
 
   while (true) {
@@ -1665,3 +1665,128 @@
   kill(getpid(), SIGUSR1);
   ASSERT_TRUE(signal_handler_on_altstack_done);
 }
+
+TEST(pthread, pthread_barrierattr_smoke) {
+  pthread_barrierattr_t attr;
+  ASSERT_EQ(0, pthread_barrierattr_init(&attr));
+  int pshared;
+  ASSERT_EQ(0, pthread_barrierattr_getpshared(&attr, &pshared));
+  ASSERT_EQ(PTHREAD_PROCESS_PRIVATE, pshared);
+  ASSERT_EQ(0, pthread_barrierattr_setpshared(&attr, PTHREAD_PROCESS_SHARED));
+  ASSERT_EQ(0, pthread_barrierattr_getpshared(&attr, &pshared));
+  ASSERT_EQ(PTHREAD_PROCESS_SHARED, pshared);
+  ASSERT_EQ(0, pthread_barrierattr_destroy(&attr));
+}
+
+struct BarrierTestHelperArg {
+  std::atomic<pid_t> tid;
+  pthread_barrier_t* barrier;
+  size_t iteration_count;
+};
+
+static void BarrierTestHelper(BarrierTestHelperArg* arg) {
+  arg->tid = gettid();
+  for (size_t i = 0; i < arg->iteration_count; ++i) {
+    ASSERT_EQ(0, pthread_barrier_wait(arg->barrier));
+  }
+}
+
+TEST(pthread, pthread_barrier_smoke) {
+  const size_t BARRIER_ITERATION_COUNT = 10;
+  const size_t BARRIER_THREAD_COUNT = 10;
+  pthread_barrier_t barrier;
+  ASSERT_EQ(0, pthread_barrier_init(&barrier, nullptr, BARRIER_THREAD_COUNT + 1));
+  std::vector<pthread_t> threads(BARRIER_THREAD_COUNT);
+  std::vector<BarrierTestHelperArg> args(threads.size());
+  for (size_t i = 0; i < threads.size(); ++i) {
+    args[i].tid = 0;
+    args[i].barrier = &barrier;
+    args[i].iteration_count = BARRIER_ITERATION_COUNT;
+    ASSERT_EQ(0, pthread_create(&threads[i], nullptr,
+                                reinterpret_cast<void* (*)(void*)>(BarrierTestHelper), &args[i]));
+  }
+  for (size_t iteration = 0; iteration < BARRIER_ITERATION_COUNT; ++iteration) {
+    for (size_t i = 0; i < threads.size(); ++i) {
+      WaitUntilThreadSleep(args[i].tid);
+    }
+    ASSERT_EQ(PTHREAD_BARRIER_SERIAL_THREAD, pthread_barrier_wait(&barrier));
+  }
+  for (size_t i = 0; i < threads.size(); ++i) {
+    ASSERT_EQ(0, pthread_join(threads[i], nullptr));
+  }
+  ASSERT_EQ(0, pthread_barrier_destroy(&barrier));
+}
+
+TEST(pthread, pthread_barrier_destroy) {
+  pthread_barrier_t barrier;
+  ASSERT_EQ(0, pthread_barrier_init(&barrier, nullptr, 2));
+  pthread_t thread;
+  BarrierTestHelperArg arg;
+  arg.tid = 0;
+  arg.barrier = &barrier;
+  arg.iteration_count = 1;
+  ASSERT_EQ(0, pthread_create(&thread, nullptr,
+                              reinterpret_cast<void* (*)(void*)>(BarrierTestHelper), &arg));
+  WaitUntilThreadSleep(arg.tid);
+  ASSERT_EQ(EBUSY, pthread_barrier_destroy(&barrier));
+  ASSERT_EQ(PTHREAD_BARRIER_SERIAL_THREAD, pthread_barrier_wait(&barrier));
+  // Verify if the barrier can be destroyed directly after pthread_barrier_wait().
+  ASSERT_EQ(0, pthread_barrier_destroy(&barrier));
+  ASSERT_EQ(0, pthread_join(thread, nullptr));
+#if defined(__BIONIC__)
+  ASSERT_EQ(EINVAL, pthread_barrier_destroy(&barrier));
+#endif
+}
+
+struct BarrierOrderingTestHelperArg {
+  pthread_barrier_t* barrier;
+  size_t* array;
+  size_t array_length;
+  size_t id;
+};
+
+void BarrierOrderingTestHelper(BarrierOrderingTestHelperArg* arg) {
+  const size_t ITERATION_COUNT = 10000;
+  for (size_t i = 1; i <= ITERATION_COUNT; ++i) {
+    arg->array[arg->id] = i;
+    int ret = pthread_barrier_wait(arg->barrier);
+    ASSERT_TRUE(ret == 0 || ret == PTHREAD_BARRIER_SERIAL_THREAD);
+    for (size_t j = 0; j < arg->array_length; ++j) {
+      ASSERT_EQ(i, arg->array[j]);
+    }
+    ret = pthread_barrier_wait(arg->barrier);
+    ASSERT_TRUE(ret == 0 || ret == PTHREAD_BARRIER_SERIAL_THREAD);
+  }
+}
+
+TEST(pthread, pthread_barrier_check_ordering) {
+  const size_t THREAD_COUNT = 4;
+  pthread_barrier_t barrier;
+  ASSERT_EQ(0, pthread_barrier_init(&barrier, nullptr, THREAD_COUNT));
+  size_t array[THREAD_COUNT];
+  std::vector<pthread_t> threads(THREAD_COUNT);
+  std::vector<BarrierOrderingTestHelperArg> args(THREAD_COUNT);
+  for (size_t i = 0; i < THREAD_COUNT; ++i) {
+    args[i].barrier = &barrier;
+    args[i].array = array;
+    args[i].array_length = THREAD_COUNT;
+    args[i].id = i;
+    ASSERT_EQ(0, pthread_create(&threads[i], nullptr,
+                                reinterpret_cast<void* (*)(void*)>(BarrierOrderingTestHelper),
+                                &args[i]));
+  }
+  for (size_t i = 0; i < THREAD_COUNT; ++i) {
+    ASSERT_EQ(0, pthread_join(threads[i], nullptr));
+  }
+}
+
+TEST(pthread, pthread_spinlock_smoke) {
+  pthread_spinlock_t lock;
+  ASSERT_EQ(0, pthread_spin_init(&lock, 0));
+  ASSERT_EQ(0, pthread_spin_trylock(&lock));
+  ASSERT_EQ(0, pthread_spin_unlock(&lock));
+  ASSERT_EQ(0, pthread_spin_lock(&lock));
+  ASSERT_EQ(EBUSY, pthread_spin_trylock(&lock));
+  ASSERT_EQ(0, pthread_spin_unlock(&lock));
+  ASSERT_EQ(0, pthread_spin_destroy(&lock));
+}
diff --git a/tests/sys_mman_test.cpp b/tests/sys_mman_test.cpp
index dffb646..ddb6c77 100644
--- a/tests/sys_mman_test.cpp
+++ b/tests/sys_mman_test.cpp
@@ -17,6 +17,7 @@
 #include <gtest/gtest.h>
 
 #include <sys/mman.h>
+#include <sys/user.h>
 #include <sys/types.h>
 #include <unistd.h>
 
@@ -219,3 +220,15 @@
 TEST(sys_mman, mremap) {
   ASSERT_EQ(MAP_FAILED, mremap(nullptr, 0, 0, 0));
 }
+
+const size_t huge = size_t(PTRDIFF_MAX) + 1;
+
+TEST(sys_mman, mmap_PTRDIFF_MAX) {
+  ASSERT_EQ(MAP_FAILED, mmap(nullptr, huge, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0));
+}
+
+TEST(sys_mman, mremap_PTRDIFF_MAX) {
+  void* map = mmap(nullptr, PAGE_SIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+  ASSERT_NE(MAP_FAILED, map);
+  ASSERT_EQ(MAP_FAILED, mremap(map, PAGE_SIZE, huge, MREMAP_MAYMOVE));
+}