Merge "Fix foritfy test for clang update."
diff --git a/METADATA b/METADATA
new file mode 100644
index 0000000..d97975c
--- /dev/null
+++ b/METADATA
@@ -0,0 +1,3 @@
+third_party {
+  license_type: NOTICE
+}
diff --git a/PREUPLOAD.cfg b/PREUPLOAD.cfg
index 11d9fe6..b4223ea 100644
--- a/PREUPLOAD.cfg
+++ b/PREUPLOAD.cfg
@@ -1,8 +1,9 @@
-[Hook Scripts]
-notice = tools/update_notice.sh
-
 [Builtin Hooks]
 clang_format = true
 
 [Builtin Hooks Options]
 clang_format = --commit ${PREUPLOAD_COMMIT} --style file --extensions c,h,cc,cpp
+
+[Hook Scripts]
+aosp_hook = ${REPO_ROOT}/frameworks/base/tools/aosp/aosp_sha.sh ${PREUPLOAD_COMMIT} "."
+notice = tools/update_notice.sh
diff --git a/libc/Android.bp b/libc/Android.bp
index 6e5db65..4c36635 100644
--- a/libc/Android.bp
+++ b/libc/Android.bp
@@ -906,6 +906,7 @@
                 "arch-x86/bionic/setjmp.S",
                 "arch-x86/bionic/syscall.S",
                 "arch-x86/bionic/vfork.S",
+                "arch-x86/bionic/__x86.get_pc_thunk.S",
 
                 // ssse3 functions
                 "arch-x86/atom/string/ssse3-strcat-atom.S",
@@ -1021,8 +1022,7 @@
         "bionic/dup.cpp",
         "bionic/environ.cpp",
         "bionic/error.cpp",
-        "bionic/eventfd_read.cpp",
-        "bionic/eventfd_write.cpp",
+        "bionic/eventfd.cpp",
         "bionic/exec.cpp",
         "bionic/faccessat.cpp",
         "bionic/fchmod.cpp",
@@ -1113,6 +1113,7 @@
         "bionic/signal.cpp",
         "bionic/sigprocmask.cpp",
         "bionic/sleep.cpp",
+        "bionic/socketpair.cpp",
         "bionic/spawn.cpp",
         "bionic/stat.cpp",
         "bionic/stdlib_l.cpp",
@@ -1700,6 +1701,7 @@
         symbol_file: "libc.map.txt",
         versions: [
             "29",
+            "R",
             "10000",
         ],
     },
@@ -1749,6 +1751,7 @@
 // Headers that only other parts of the platform can include.
 cc_library_headers {
     name: "bionic_libc_platform_headers",
+    defaults: ["linux_bionic_supported"],
     visibility: [
         "//art:__subpackages__",
         "//bionic:__subpackages__",
@@ -1760,7 +1763,6 @@
         "//system/core/libunwindstack:__subpackages__",
         "//system/memory/libmemunreachable:__subpackages__",
     ],
-    host_supported: true,
     vendor_available: true,
     ramdisk_available: true,
     recovery_available: true,
diff --git a/libc/SYSCALLS.TXT b/libc/SYSCALLS.TXT
index ab309e6..6142baf 100644
--- a/libc/SYSCALLS.TXT
+++ b/libc/SYSCALLS.TXT
@@ -126,7 +126,7 @@
 int         __fcntl:fcntl(int, int, void*)  lp64
 int         flock(int, int)   all
 int         __fchmod:fchmod(int, mode_t)  all
-int         pipe2(int*, int) all
+int         __pipe2:pipe2(int*, int) all
 int         __dup:dup(int)  all
 int         __dup3:dup3(int, int, int)   all
 int         fsync(int)  all
@@ -243,7 +243,7 @@
 
 # sockets
 int           __socket:socket(int, int, int)              arm,lp64
-int           socketpair(int, int, int, int*)    arm,lp64
+int           __socketpair:socketpair(int, int, int, int*)    arm,lp64
 int           bind(int, struct sockaddr*, socklen_t)  arm,lp64
 int           __connect:connect(int, struct sockaddr*, socklen_t)   arm,lp64
 int           listen(int, int)                   arm,lp64
@@ -267,7 +267,7 @@
 int           listen:socketcall:4(int, int)                   x86
 int           getsockname:socketcall:6(int, struct sockaddr*, socklen_t*)  x86
 int           getpeername:socketcall:7(int, struct sockaddr*, socklen_t*)  x86
-int           socketpair:socketcall:8(int, int, int, int*)    x86
+int           __socketpair:socketcall:8(int, int, int, int*)    x86
 ssize_t       __sendto:socketcall:11(int, const void*, size_t, int, const struct sockaddr*, socklen_t)  x86
 ssize_t       recvfrom:socketcall:12(int, void*, size_t, unsigned int, struct sockaddr*, socklen_t*)  x86
 int           shutdown:socketcall:13(int, int)  x86
@@ -308,11 +308,11 @@
 ssize_t splice(int, off64_t*, int, off64_t*, size_t, unsigned int)  all
 ssize_t vmsplice(int, const struct iovec*, size_t, unsigned int)  all
 
-int epoll_create1(int)  all
+int __epoll_create1:epoll_create1(int)  all
 int epoll_ctl(int, int op, int, struct epoll_event*)  all
 int __epoll_pwait:epoll_pwait(int, struct epoll_event*, int, int, const sigset64_t*, size_t)  all
 
-int eventfd:eventfd2(unsigned int, int)  all
+int __eventfd:eventfd2(unsigned int, int)  all
 
 void _exit|_Exit:exit_group(int)  all
 void __exit:exit(int)  all
diff --git a/libc/arch-x86/atom/string/sse2-memset-atom.S b/libc/arch-x86/atom/string/sse2-memset-atom.S
index 016c49e..e4cd038 100644
--- a/libc/arch-x86/atom/string/sse2-memset-atom.S
+++ b/libc/arch-x86/atom/string/sse2-memset-atom.S
@@ -77,15 +77,6 @@
     /* We loaded the jump table and adjusted EDX. Go.  */	\
     jmp		*%ebx
 
-	.section	.gnu.linkonce.t.__x86.get_pc_thunk.bx,"ax",@progbits
-	.globl	__x86.get_pc_thunk.bx
-	.hidden	__x86.get_pc_thunk.bx
-	ALIGN(4)
-	.type	__x86.get_pc_thunk.bx,@function
-__x86.get_pc_thunk.bx:
-	movl	(%esp), %ebx
-	ret
-
 ENTRY(__memset_chk_atom)
   ENTRANCE
 
diff --git a/libc/arch-x86/bionic/__x86.get_pc_thunk.S b/libc/arch-x86/bionic/__x86.get_pc_thunk.S
new file mode 100644
index 0000000..58dbb08
--- /dev/null
+++ b/libc/arch-x86/bionic/__x86.get_pc_thunk.S
@@ -0,0 +1,53 @@
+/*
+ * Copyright (C) 2020 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.
+ */
+
+// Typically GCC outputs functions like these into any object file that needs a PIC base register,
+// and one of the copies for each register is used. Clang doesn't use these functions, but some of
+// the quasi-hand-written assembly in the optimized string routines does.
+
+    .section .text.__x86.get_pc_thunk.bx,"axG",@progbits,__x86.get_pc_thunk.bx,comdat
+    .globl __x86.get_pc_thunk.bx
+    .hidden __x86.get_pc_thunk.bx
+    .p2align 4
+    .type __x86.get_pc_thunk.bx,@function
+__x86.get_pc_thunk.bx:
+    .cfi_startproc
+    movl (%esp), %ebx
+    ret
+    .cfi_endproc
+
+    .section .text.__x86.get_pc_thunk.cx,"axG",@progbits,__x86.get_pc_thunk.cx,comdat
+    .globl __x86.get_pc_thunk.cx
+    .hidden __x86.get_pc_thunk.cx
+    .p2align 4
+    .type __x86.get_pc_thunk.cx,@function
+__x86.get_pc_thunk.cx:
+    .cfi_startproc
+    movl (%esp), %ecx
+    ret
+    .cfi_endproc
diff --git a/libc/arch-x86/silvermont/string/sse2-memset-slm.S b/libc/arch-x86/silvermont/string/sse2-memset-slm.S
index adaccae..b7633f5 100644
--- a/libc/arch-x86/silvermont/string/sse2-memset-slm.S
+++ b/libc/arch-x86/silvermont/string/sse2-memset-slm.S
@@ -77,15 +77,6 @@
     /* We loaded the jump table and adjusted EDX. Go.  */	\
     jmp		*%ebx
 
-	.section	.gnu.linkonce.t.__x86.get_pc_thunk.bx,"ax",@progbits
-	.globl	__x86.get_pc_thunk.bx
-	.hidden	__x86.get_pc_thunk.bx
-	ALIGN(4)
-	.type	__x86.get_pc_thunk.bx,@function
-__x86.get_pc_thunk.bx:
-	movl	(%esp), %ebx
-	ret
-
 ENTRY(__memset_chk_generic)
   ENTRANCE
 
diff --git a/libc/bionic/eventfd_read.cpp b/libc/bionic/eventfd.cpp
similarity index 82%
rename from libc/bionic/eventfd_read.cpp
rename to libc/bionic/eventfd.cpp
index 50e73fd..f13c6a3 100644
--- a/libc/bionic/eventfd_read.cpp
+++ b/libc/bionic/eventfd.cpp
@@ -29,6 +29,18 @@
 #include <sys/eventfd.h>
 #include <unistd.h>
 
+#include "private/bionic_fdtrack.h"
+
+extern "C" int __eventfd(unsigned int initval, int flags);
+
+int eventfd(unsigned int initval, int flags) {
+  return FDTRACK_CREATE(__eventfd(initval, flags));
+}
+
 int eventfd_read(int fd, eventfd_t* value) {
   return (read(fd, value, sizeof(*value)) == sizeof(*value)) ? 0 : -1;
 }
+
+int eventfd_write(int fd, eventfd_t value) {
+  return (write(fd, &value, sizeof(value)) == sizeof(value)) ? 0 : -1;
+}
diff --git a/libc/bionic/eventfd_write.cpp b/libc/bionic/eventfd_write.cpp
index 3c3d3f1..6d86d8c 100644
--- a/libc/bionic/eventfd_write.cpp
+++ b/libc/bionic/eventfd_write.cpp
@@ -29,6 +29,3 @@
 #include <sys/eventfd.h>
 #include <unistd.h>
 
-int eventfd_write(int fd, eventfd_t value) {
-  return (write(fd, &value, sizeof(value)) == sizeof(value)) ? 0 : -1;
-}
diff --git a/libc/bionic/malloc_tagged_pointers.h b/libc/bionic/malloc_tagged_pointers.h
index 212459b..de3cc2e 100644
--- a/libc/bionic/malloc_tagged_pointers.h
+++ b/libc/bionic/malloc_tagged_pointers.h
@@ -43,10 +43,11 @@
 // is also deliberately different from the standard pattern-init tag (0xAA), as
 // to be distinguishable from an uninitialized-pointer access. The first and
 // second nibbles are also deliberately designed to be the bitset-mirror of each
-// other (0b1100, 0b0011) in order to reduce incidental matches. Users must not
-// rely on the implementation-defined value of this pointer tag, as it may
-// change.
-static constexpr uintptr_t POINTER_TAG = 0x3C;
+// other (0b1011, 0b0100) in order to reduce incidental matches. We also ensure
+// that the top bit is set, as this catches incorrect code that assumes that a
+// "negative" pointer indicates error. Users must not rely on the
+// implementation-defined value of this pointer tag, as it may change.
+static constexpr uintptr_t POINTER_TAG = 0xB4;
 static constexpr unsigned UNTAG_SHIFT = 40;
 static constexpr unsigned CHECK_SHIFT = 48;
 static constexpr unsigned TAG_SHIFT = 56;
diff --git a/libc/bionic/pipe.cpp b/libc/bionic/pipe.cpp
index a81afe8..f197656 100644
--- a/libc/bionic/pipe.cpp
+++ b/libc/bionic/pipe.cpp
@@ -28,6 +28,24 @@
 
 #include <unistd.h>
 
+#include "private/bionic_fdtrack.h"
+
+extern "C" int __pipe2(int pipefd[2], int flags);
+
 int pipe(int pipefd[2]) {
-  return pipe2(pipefd, 0);
+  int rc = __pipe2(pipefd, 0);
+  if (rc == 0) {
+    FDTRACK_CREATE(pipefd[0]);
+    FDTRACK_CREATE(pipefd[1]);
+  }
+  return rc;
+}
+
+int pipe2(int pipefd[2], int flags) {
+  int rc = __pipe2(pipefd, flags);
+  if (rc == 0) {
+    FDTRACK_CREATE(pipefd[0]);
+    FDTRACK_CREATE(pipefd[1]);
+  }
+  return rc;
 }
diff --git a/libc/bionic/eventfd_read.cpp b/libc/bionic/socketpair.cpp
similarity index 77%
copy from libc/bionic/eventfd_read.cpp
copy to libc/bionic/socketpair.cpp
index 50e73fd..d2b4c19 100644
--- a/libc/bionic/eventfd_read.cpp
+++ b/libc/bionic/socketpair.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2010 The Android Open Source Project
+ * Copyright (C) 2020 The Android Open Source Project
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -26,9 +26,18 @@
  * SUCH DAMAGE.
  */
 
-#include <sys/eventfd.h>
+#include <sys/socket.h>
 #include <unistd.h>
 
-int eventfd_read(int fd, eventfd_t* value) {
-  return (read(fd, value, sizeof(*value)) == sizeof(*value)) ? 0 : -1;
+#include "private/bionic_fdtrack.h"
+
+extern "C" int __socketpair(int domain, int type, int protocol, int sv[2]);
+
+int socketpair(int domain, int type, int protocol, int sv[2]) {
+  int rc = __socketpair(domain, type, protocol, sv);
+  if (rc == 0) {
+    FDTRACK_CREATE(sv[0]);
+    FDTRACK_CREATE(sv[1]);
+  }
+  return rc;
 }
diff --git a/libc/bionic/sys_epoll.cpp b/libc/bionic/sys_epoll.cpp
index 9f82912..22d0a98 100644
--- a/libc/bionic/sys_epoll.cpp
+++ b/libc/bionic/sys_epoll.cpp
@@ -30,7 +30,9 @@
 #include <sys/epoll.h>
 
 #include "private/SigSetConverter.h"
+#include "private/bionic_fdtrack.h"
 
+extern "C" int __epoll_create1(int flags);
 extern "C" int __epoll_pwait(int, epoll_event*, int, int, const sigset64_t*, size_t);
 
 int epoll_create(int size) {
@@ -38,7 +40,11 @@
     errno = EINVAL;
     return -1;
   }
-  return epoll_create1(0);
+  return FDTRACK_CREATE(__epoll_create1(0));
+}
+
+int epoll_create1(int flags) {
+  return FDTRACK_CREATE(__epoll_create1(flags));
 }
 
 int epoll_pwait(int fd, epoll_event* events, int max_events, int timeout, const sigset_t* ss) {
diff --git a/libc/bionic/tmpfile.cpp b/libc/bionic/tmpfile.cpp
index 4378e84..d7ce897 100644
--- a/libc/bionic/tmpfile.cpp
+++ b/libc/bionic/tmpfile.cpp
@@ -31,6 +31,7 @@
  */
 
 #include <errno.h>
+#include <fcntl.h>
 #include <signal.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -39,48 +40,48 @@
 #include <unistd.h>
 
 #include "private/ErrnoRestorer.h"
-#include "private/ScopedSignalBlocker.h"
 
-static FILE* __tmpfile_dir(const char* tmp_dir) {
+static FILE* __fd_to_fp(int fd) {
+  FILE* fp = fdopen(fd, "w+");
+  if (fp != nullptr) return fp;
+
+  ErrnoRestorer errno_restorer;
+  close(fd);
+  return nullptr;
+}
+
+static FILE* __tmpfile_dir_legacy(const char* tmp_dir) {
   char* path = nullptr;
   if (asprintf(&path, "%s/tmp.XXXXXXXXXX", tmp_dir) == -1) {
     return nullptr;
   }
 
-  int fd;
-  {
-    ScopedSignalBlocker ssb;
-    fd = mkstemp(path);
-    if (fd == -1) {
-      free(path);
-      return nullptr;
-    }
-
-    // Unlink the file now so that it's removed when closed.
-    unlink(path);
+  int fd = mkstemp(path);
+  if (fd == -1) {
     free(path);
-
-    // Can we still use the file now it's unlinked?
-    // File systems without hard link support won't have the usual Unix semantics.
-    struct stat sb;
-    int rc = fstat(fd, &sb);
-    if (rc == -1) {
-      ErrnoRestorer errno_restorer;
-      close(fd);
-      return nullptr;
-    }
+    return nullptr;
   }
 
-  // Turn the file descriptor into a FILE*.
-  FILE* fp = fdopen(fd, "w+");
-  if (fp != nullptr) {
-    return fp;
+  // Unlink the file now so that it's removed when closed.
+  unlink(path);
+  free(path);
+
+  // Can we still use the file now it's unlinked?
+  // File systems without hard link support won't have the usual Unix semantics.
+  struct stat sb;
+  if (fstat(fd, &sb) == -1) {
+    ErrnoRestorer errno_restorer;
+    close(fd);
+    return nullptr;
   }
 
-  // Failure. Clean up. We already unlinked, so we just need to close.
-  ErrnoRestorer errno_restorer;
-  close(fd);
-  return nullptr;
+  return __fd_to_fp(fd);
+}
+
+static FILE* __tmpfile_dir(const char* tmp_dir) {
+  int fd = open(tmp_dir, O_TMPFILE | O_RDWR, S_IRUSR | S_IWUSR);
+  if (fd == -1) return __tmpfile_dir_legacy(tmp_dir);
+  return __fd_to_fp(fd);
 }
 
 FILE* tmpfile() {
diff --git a/libc/malloc_debug/tests/malloc_debug_system_tests.cpp b/libc/malloc_debug/tests/malloc_debug_system_tests.cpp
index dd569fd..ac19cf9 100644
--- a/libc/malloc_debug/tests/malloc_debug_system_tests.cpp
+++ b/libc/malloc_debug/tests/malloc_debug_system_tests.cpp
@@ -40,7 +40,7 @@
 #include <android-base/file.h>
 #include <android-base/stringprintf.h>
 #include <gtest/gtest.h>
-#include <log/log.h>
+#include <log/log_read.h>
 
 #include <string>
 #include <thread>
diff --git a/libc/malloc_hooks/README.md b/libc/malloc_hooks/README.md
index b418e1e..1747e8d 100644
--- a/libc/malloc_hooks/README.md
+++ b/libc/malloc_hooks/README.md
@@ -74,11 +74,11 @@
 ======================
 Below is a simple implementation intercepting only malloc/calloc calls.
 
-    void* new_malloc_hook(size_t bytes, const char* arg) {
+    void* new_malloc_hook(size_t bytes, const void* arg) {
       return orig_malloc_hook(bytes, arg);
     }
 
-    void orig_malloc_hook = __malloc_hook;
+    auto orig_malloc_hook = __malloc_hook;
     __malloc_hook = new_malloc_hook;
 
 Enabling Examples
diff --git a/libc/platform/bionic/mte_kernel.h b/libc/platform/bionic/mte_kernel.h
index d22d65e..e8ef2a5 100644
--- a/libc/platform/bionic/mte_kernel.h
+++ b/libc/platform/bionic/mte_kernel.h
@@ -51,6 +51,7 @@
 #define SEGV_MTEAERR 8
 #define SEGV_MTESERR 9
 
-#define PTRACE_PEEKTAG 33
+#define PTRACE_PEEKMTETAGS 33
+#define PTRACE_POKEMTETAGS 34
 
 #endif
diff --git a/libc/private/ScopedSignalBlocker.h b/libc/private/ScopedSignalBlocker.h
index ce0ae64..f6ba9ed 100644
--- a/libc/private/ScopedSignalBlocker.h
+++ b/libc/private/ScopedSignalBlocker.h
@@ -20,20 +20,26 @@
 
 #include "platform/bionic/macros.h"
 
+// This code needs to really block all the signals, not just the user-visible
+// ones. We call __rt_sigprocmask(2) directly so we don't mask out our own
+// signals (https://issuetracker.google.com/153624226 was a pthread_exit(3)
+// crash because a request to dump the thread's stack came in as it was exiting).
+extern "C" int __rt_sigprocmask(int, const sigset64_t*, sigset64_t*, size_t);
+
 class ScopedSignalBlocker {
  public:
   // Block all signals.
   explicit ScopedSignalBlocker() {
     sigset64_t set;
     sigfillset64(&set);
-    sigprocmask64(SIG_BLOCK, &set, &old_set_);
+    __rt_sigprocmask(SIG_BLOCK, &set, &old_set_, sizeof(sigset64_t));
   }
 
   // Block just the specified signal.
   explicit ScopedSignalBlocker(int signal) {
     sigset64_t set = {};
     sigaddset64(&set, signal);
-    sigprocmask64(SIG_BLOCK, &set, &old_set_);
+    __rt_sigprocmask(SIG_BLOCK, &set, &old_set_, sizeof(sigset64_t));
   }
 
   ~ScopedSignalBlocker() {
@@ -41,7 +47,7 @@
   }
 
   void reset() {
-    sigprocmask64(SIG_SETMASK, &old_set_, nullptr);
+    __rt_sigprocmask(SIG_SETMASK, &old_set_, nullptr, sizeof(sigset64_t));
   }
 
   sigset64_t old_set_;
diff --git a/libc/private/bionic_globals.h b/libc/private/bionic_globals.h
index 6e7eb76..54605db 100644
--- a/libc/private/bionic_globals.h
+++ b/libc/private/bionic_globals.h
@@ -101,8 +101,8 @@
   const char* init_progname = nullptr;
   char** init_environ = nullptr;
 
-  const gwp_asan::AllocatorState *gwp_asan_state = nullptr;
-  const gwp_asan::AllocationMetadata *gwp_asan_metadata = nullptr;
+  const gwp_asan::AllocatorState* gwp_asan_state = nullptr;
+  const gwp_asan::AllocationMetadata* gwp_asan_metadata = nullptr;
 };
 
 __LIBC_HIDDEN__ libc_shared_globals* __libc_shared_globals();
diff --git a/libfdtrack/fdtrack.cpp b/libfdtrack/fdtrack.cpp
index 8d9d61d..d371577 100644
--- a/libfdtrack/fdtrack.cpp
+++ b/libfdtrack/fdtrack.cpp
@@ -60,7 +60,7 @@
 static constexpr size_t kStackDepth = 10;
 
 static bool installed = false;
-static std::array<FdEntry, kFdTableSize> stack_traces;
+static std::array<FdEntry, kFdTableSize> stack_traces [[clang::no_destroy]];
 static unwindstack::LocalUnwinder& Unwinder() {
   static android::base::NoDestructor<unwindstack::LocalUnwinder> unwinder;
   return *unwinder.get();
diff --git a/linker/dlfcn.cpp b/linker/dlfcn.cpp
index 228e30a..ec6850a 100644
--- a/linker/dlfcn.cpp
+++ b/linker/dlfcn.cpp
@@ -279,10 +279,12 @@
 }
 
 android_namespace_t* __loader_android_get_exported_namespace(const char* name) {
+  ScopedPthreadMutexLocker locker(&g_dl_mutex);
   return get_exported_namespace(name);
 }
 
 void __loader_cfi_fail(uint64_t CallSiteTypeId, void* Ptr, void *DiagData, void *CallerPc) {
+  ScopedPthreadMutexLocker locker(&g_dl_mutex);
   CFIShadowWriter::CfiFail(CallSiteTypeId, Ptr, DiagData, CallerPc);
 }
 
diff --git a/tests/Android.bp b/tests/Android.bp
index c751084..75fdd90 100644
--- a/tests/Android.bp
+++ b/tests/Android.bp
@@ -22,6 +22,12 @@
         darwin: {
             enabled: false,
         },
+        android: {
+            header_libs: ["bionic_libc_platform_headers"],
+        },
+        linux_bionic: {
+            header_libs: ["bionic_libc_platform_headers"],
+        },
     },
     cflags: [
         "-fstack-protector-all",
@@ -38,7 +44,6 @@
         // For glibc.
         "-D__STDC_LIMIT_MACROS",
     ],
-    header_libs: ["bionic_libc_platform_headers"],
     // Ensure that the tests exercise shadow call stack support and
     // the hint space PAC/BTI instructions.
     arch: {
diff --git a/tests/fdtrack_test.cpp b/tests/fdtrack_test.cpp
index fca92ce..0613f45 100644
--- a/tests/fdtrack_test.cpp
+++ b/tests/fdtrack_test.cpp
@@ -20,19 +20,41 @@
 #include <err.h>
 #include <errno.h>
 #include <fcntl.h>
+#include <netinet/in.h>
 #include <stdlib.h>
+#include <sys/epoll.h>
+#include <sys/eventfd.h>
 #include <sys/types.h>
 #include <unistd.h>
 
 #if defined(__BIONIC__)
 #include "platform/bionic/fdtrack.h"
+#include "platform/bionic/reserved_signals.h"
 #endif
 
 #include <vector>
 
+#include <android-base/cmsg.h>
+#include <android-base/logging.h>
 #include <android-base/unique_fd.h>
 
+using android::base::ReceiveFileDescriptors;
+using android::base::SendFileDescriptors;
+using android::base::unique_fd;
+
 #if defined(__BIONIC__)
+void DumpEvent(std::vector<android_fdtrack_event>* events, size_t index) {
+  auto& event = (*events)[index];
+  if (event.type == ANDROID_FDTRACK_EVENT_TYPE_CREATE) {
+    fprintf(stderr, "  event %zu: fd %d created by %s\n", index, event.fd,
+            event.data.create.function_name);
+  } else if (event.type == ANDROID_FDTRACK_EVENT_TYPE_CLOSE) {
+    fprintf(stderr, "  event %zu: fd %d closed\n", index, event.fd);
+  } else {
+    errx(1, "unexpected fdtrack event type: %d", event.type);
+  }
+}
+
 std::vector<android_fdtrack_event> FdtrackRun(void (*func)()) {
   // Each bionic test is run in separate process, so we can safely use a static here.
   static std::vector<android_fdtrack_event> events;
@@ -40,6 +62,7 @@
 
   android_fdtrack_hook_t previous = nullptr;
   android_fdtrack_hook_t hook = [](android_fdtrack_event* event) {
+    raise(BIONIC_SIGNAL_DEBUGGER);
     events.push_back(*event);
   };
 
@@ -58,21 +81,42 @@
     errx(1, "failed to reset hook");
   }
 
+  // Filter out temporary fds created and closed as a result of the call.
+  // (e.g. accept creating a socket to tell netd about the newly accepted socket)
+  size_t i = 0;
+  while (i + 1 < events.size()) {
+    auto& event = events[i];
+    if (event.type == ANDROID_FDTRACK_EVENT_TYPE_CREATE) {
+      for (size_t j = i + 1; j < events.size(); ++j) {
+        if (event.fd == events[j].fd) {
+          if (events[j].type == ANDROID_FDTRACK_EVENT_TYPE_CREATE) {
+            fprintf(stderr, "error: multiple create events for the same fd:\n");
+            DumpEvent(&events, i);
+            DumpEvent(&events, j);
+            exit(1);
+          }
+
+          events.erase(events.begin() + j);
+          events.erase(events.begin() + i);
+          continue;
+        }
+      }
+    }
+    ++i;
+  }
+
   return std::move(events);
 }
-#endif
 
-TEST(fdtrack, open) {
-#if defined(__BIONIC__)
-  static int fd = -1;
-  auto events = FdtrackRun([]() { fd = open("/dev/null", O_WRONLY | O_CLOEXEC); });
-  ASSERT_NE(-1, fd);
-  ASSERT_EQ(1U, events.size());
-  ASSERT_EQ(fd, events[0].fd);
-  ASSERT_EQ(ANDROID_FDTRACK_EVENT_TYPE_CREATE, events[0].type);
-  ASSERT_STREQ("open", events[0].data.create.function_name);
-#endif
+const char* FdtrackEventTypeToName(android_fdtrack_event_type event_type) {
+  switch (event_type) {
+    case ANDROID_FDTRACK_EVENT_TYPE_CREATE:
+      return "created";
+    case ANDROID_FDTRACK_EVENT_TYPE_CLOSE:
+      return "closed";
+  }
 }
+#endif
 
 TEST(fdtrack, close) {
 #if defined(__BIONIC__)
@@ -118,3 +162,131 @@
   ASSERT_STREQ("open", events[1].data.create.function_name);
 #endif
 }
+
+struct require_semicolon;
+
+#if defined(__BIONIC__)
+void SetFdResult(std::vector<int>* output, int fd) {
+  output->push_back(fd);
+}
+
+void SetFdResult(std::vector<int>* output, std::vector<int> fds) {
+  *output = fds;
+}
+
+#define FDTRACK_TEST_NAME(test_name, fdtrack_name, expression)                                   \
+  TEST(fdtrack, test_name) {                                                                     \
+    static std::vector<int> expected_fds;                                                        \
+    auto events = FdtrackRun([]() { SetFdResult(&expected_fds, expression); });                  \
+    for (auto& fd : expected_fds) {                                                              \
+      ASSERT_NE(-1, fd);                                                                         \
+    }                                                                                            \
+    if (events.size() != expected_fds.size()) {                                                  \
+      fprintf(stderr, "too many events received: expected %zu, got %zu:\n", expected_fds.size(), \
+              events.size());                                                                    \
+      for (size_t i = 0; i < events.size(); ++i) {                                               \
+        DumpEvent(&events, i);                                                                   \
+      }                                                                                          \
+      FAIL();                                                                                    \
+      return;                                                                                    \
+    }                                                                                            \
+    for (auto& event : events) {                                                                 \
+      ASSERT_NE(expected_fds.end(),                                                              \
+                std::find(expected_fds.begin(), expected_fds.end(), events[0].fd));              \
+      ASSERT_EQ(ANDROID_FDTRACK_EVENT_TYPE_CREATE, event.type);                                  \
+      ASSERT_STREQ(fdtrack_name, event.data.create.function_name);                               \
+    }                                                                                            \
+  }                                                                                              \
+  struct require_semicolon
+#else
+#define FDTRACK_TEST_NAME(name, fdtrack_name, expression) \
+  TEST(fdtrack, name) {}                                  \
+  struct require_semicolon
+#endif
+
+#define FDTRACK_TEST(name, expression) FDTRACK_TEST_NAME(name, #name, expression)
+
+// clang-format misformats statement expressions pretty badly here:
+// clang-format off
+FDTRACK_TEST(open, open("/dev/null", O_WRONLY | O_CLOEXEC));
+FDTRACK_TEST(openat, openat(AT_EMPTY_PATH, "/dev/null", O_WRONLY | O_CLOEXEC));
+FDTRACK_TEST(socket, socket(AF_UNIX, SOCK_STREAM, 0));
+
+FDTRACK_TEST(dup, dup(STDOUT_FILENO));
+FDTRACK_TEST(dup2, dup2(STDOUT_FILENO, STDERR_FILENO));
+FDTRACK_TEST(dup3, dup3(STDOUT_FILENO, STDERR_FILENO, 0));
+FDTRACK_TEST_NAME(fcntl_F_DUPFD, "F_DUPFD", fcntl(STDOUT_FILENO, F_DUPFD, 0));
+FDTRACK_TEST_NAME(fcntl_F_DUPFD_CLOEXEC, "F_DUPFD_CLOEXEC", fcntl(STDOUT_FILENO, F_DUPFD_CLOEXEC, 0));
+
+FDTRACK_TEST(pipe, ({
+  std::vector<int> fds = { -1, -1};
+  if (pipe(fds.data()) != 0) {
+    err(1, "pipe failed");
+  }
+  fds;
+}));
+
+FDTRACK_TEST(pipe2, ({
+  std::vector<int> fds = { -1, -1};
+  if (pipe2(fds.data(), O_CLOEXEC) != 0) {
+    err(1, "pipe failed");
+  }
+  fds;
+}));
+
+FDTRACK_TEST(socketpair, ({
+  std::vector<int> fds = { -1, -1};
+  if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds.data()) != 0) {
+    err(1, "socketpair failed");
+  }
+  fds;
+}));
+
+FDTRACK_TEST(epoll_create, epoll_create(1));
+FDTRACK_TEST(epoll_create1, epoll_create1(0));
+
+FDTRACK_TEST(eventfd, eventfd(0, 0));
+
+#if defined(__BIONIC__)
+static int CreateListener() {
+  android_fdtrack_set_enabled(false);
+  int listener = socket(AF_INET, SOCK_STREAM, 0);
+  CHECK_NE(-1, listener);
+
+  sockaddr_in addr = {
+      .sin_family = AF_INET,
+      .sin_port = 0,
+      .sin_addr = {htonl(INADDR_LOOPBACK)},
+  };
+  socklen_t addrlen = sizeof(addr);
+
+  CHECK_NE(-1, bind(listener, reinterpret_cast<sockaddr*>(&addr), addrlen)) << strerror(errno);
+  CHECK_NE(-1, getsockname(listener, reinterpret_cast<sockaddr*>(&addr), &addrlen));
+  CHECK_EQ(static_cast<size_t>(addrlen), sizeof(addr));
+  CHECK_NE(-1, listen(listener, 1));
+
+  int connector = socket(AF_INET, SOCK_STREAM, 0);
+  CHECK_NE(-1, connector);
+  CHECK_NE(-1, connect(connector, reinterpret_cast<sockaddr*>(&addr), addrlen));
+  android_fdtrack_set_enabled(true);
+
+  return listener;
+}
+#endif
+
+FDTRACK_TEST_NAME(accept, "accept4", accept(CreateListener(), nullptr, nullptr));
+FDTRACK_TEST(accept4, accept4(CreateListener(), nullptr, nullptr, 0));
+
+FDTRACK_TEST(recvmsg, ({
+  android_fdtrack_set_enabled(false);
+  int sockets[2];
+  ASSERT_NE(-1, socketpair(AF_UNIX, SOCK_SEQPACKET, 0, sockets));
+  ASSERT_EQ(3, SendFileDescriptors(sockets[0], "foo", 3, STDIN_FILENO));
+  android_fdtrack_set_enabled(true);
+
+  char buf[4];
+  unique_fd received_fd;
+  ASSERT_EQ(3, ReceiveFileDescriptors(sockets[1], buf, sizeof(buf), &received_fd));
+  received_fd.release();
+}));
+// clang-format on
diff --git a/tests/mte_test.cpp b/tests/mte_test.cpp
index 8928805..ff8cde6 100644
--- a/tests/mte_test.cpp
+++ b/tests/mte_test.cpp
@@ -14,6 +14,10 @@
  * limitations under the License.
  */
 
+#include <sys/cdefs.h>
+
+#if defined(__BIONIC__)
+
 #include <gtest/gtest.h>
 
 #include <android-base/macros.h>
@@ -46,3 +50,5 @@
 TEST(mte_test, ScopedDisableMTE) {
   test_tag_mismatch();
 }
+
+#endif  // __BIONIC__
diff --git a/tests/utils.h b/tests/utils.h
index 5085a7a..a9b8513 100644
--- a/tests/utils.h
+++ b/tests/utils.h
@@ -25,7 +25,11 @@
 #include <sys/wait.h>
 #include <unistd.h>
 
+#if defined(__BIONIC__)
 #include <bionic/macros.h>
+#else
+#define untag_address(p) p
+#endif
 
 #include <atomic>
 #include <string>