Merge "Replace cxa_guard fences with acquire loads"
diff --git a/libc/SECCOMP_WHITELIST.TXT b/libc/SECCOMP_WHITELIST.TXT
new file mode 100644
index 0000000..22e8987
--- /dev/null
+++ b/libc/SECCOMP_WHITELIST.TXT
@@ -0,0 +1,93 @@
+# This file is used to populate seccomp's whitelist policy in comination with SYSCALLS.txt.
+#
+# Each non-blank, non-comment line has the following format:
+#
+# return_type func_name[|alias_list][:syscall_name[:socketcall_id]]([parameter_list]) arch_list
+#
+# where:
+#       arch_list ::= "all" | arch+
+#       arch      ::= "arm" | "arm64" | "mips" | "mips64" | "x86" | "x86_64"
+#
+# Note:
+#      - syscall_name corresponds to the name of the syscall, which may differ from
+#        the exported function name (example: the exit syscall is implemented by the _exit()
+#        function, which is not the same as the standard C exit() function which calls it)
+
+#      - alias_list is optional comma separated list of function aliases
+#
+#      - The call_id parameter, given that func_name and syscall_name have
+#        been provided, allows the user to specify dispatch style syscalls.
+#        For example, socket() syscall on i386 actually becomes:
+#          socketcall(__NR_socket, 1, *(rest of args on stack)).
+#
+#      - Each parameter type is assumed to be stored in 32 bits.
+#
+# This file is processed by a python script named gensyscalls.py.
+
+# syscalls needed to boot android
+int	pivot_root:pivot_root(const char *new_root, const char *put_old)	arm64
+int	ioprio_get:ioprio_get(int which, int who)	arm64
+int	ioprio_set:ioprio_set(int which, int who, int ioprio)	arm64
+pid_t	gettid:gettid()	arm64,arm
+int	futex:futex(int *uaddr, int futex_op, int val, const struct timespec *timeout, int *uaddr2, int val3)	arm64,arm
+int	clone:clone(int (*fn)(void *), void *child_stack, int flags, void *arg, ..) arm64,arm
+int	rt_sigreturn:rt_sigreturn(unsigned long __unused)	arm64,arm
+int	rt_tgsigqueueinfo:int rt_tgsigqueueinfo(pid_t tgid, pid_t tid, int sig, siginfo_t *uinfo)	arm64,arm
+int	restart_syscall:int restart_syscall()	arm64,arm
+int	getrandom:int getrandom(void *buf, size_t buflen, unsigned int flags) arm64,arm
+
+# Needed for performance tools
+int	perf_event_open:perf_event_open(struct perf_event_attr *attr, pid_t pid, int cpu, int group_fd, unsigned long flags)	arm64,arm
+
+# Needed for strace
+int	tkill:tkill(int tid, int sig)	arm64,arm
+
+# b/35034743
+int	syncfs:syncfs(int fd)	arm64
+
+# b/34763393
+int	seccomp:seccomp(unsigned int operation, unsigned int flags, void *args)	arm64,arm
+
+# syscalls needed to boot android
+int	sigreturn:sigreturn(unsigned long __unused)	arm
+
+# Syscalls needed to run GFXBenchmark
+pid_t	vfork:vfork()	arm
+
+# Needed for debugging 32-bit Chrome
+int	pipe:pipe(int pipefd[2])	arm
+
+# b/34651972
+int	access:access(const char *pathname, int mode)	arm
+int	stat64:stat64(const char *restrict path, struct stat64 *restrict buf)	arm
+
+# b/34813887
+int	open:open(const char *path, int oflag, ... ) arm
+int	getdents:getdents(unsigned int fd, struct linux_dirent *dirp, unsigned int count) arm
+int	getdents64:getdents64(unsigned int fd, struct linux_dirent64 *dirp, unsigned int count) arm
+
+# b/34719286
+int	eventfd:eventfd(unsigned int initval, int flags)	arm
+
+# b/34817266
+int	epoll_wait:epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout)	arm
+
+# Needed by sanitizers (b/34606909)
+# 5 (__NR_open) and 195 (__NR_stat64) are also required, but they are
+# already allowed.
+ssize_t	readlink:readlink(const char *path, char *buf, size_t bufsiz)	arm
+
+# b/34908783
+int	epoll_create:epoll_create(int size)	arm
+
+# b/34979910
+int	creat:creat(const char *pathname, mode_t mode)	arm
+int	unlink:unlink(const char *pathname)	arm
+
+# b/35059702
+int	lstat64:lstat64(const char *restrict path, struct stat64 *restrict buf)	arm
+
+# b/35217603
+int	fcntl:fcntl(int fd, int cmd, ... /* arg */ )	arm
+pid_t	fork:fork()	arm
+int	poll:poll(struct pollfd *fds, nfds_t nfds, int timeout)	arm
diff --git a/libc/bionic/grp_pwd.cpp b/libc/bionic/grp_pwd.cpp
index e99eaca..5d565c4 100644
--- a/libc/bionic/grp_pwd.cpp
+++ b/libc/bionic/grp_pwd.cpp
@@ -39,9 +39,9 @@
 
 #include "private/android_filesystem_config.h"
 #include "private/bionic_macros.h"
+#include "private/grp_pwd.h"
 #include "private/ErrnoRestorer.h"
 #include "private/libc_logging.h"
-#include "private/ThreadLocalBuffer.h"
 
 // Generated android_ids array
 #include "generated_android_ids.h"
@@ -52,25 +52,14 @@
 // okay for all the <grp.h> functions to share state, and all the <passwd.h>
 // functions to share state, but <grp.h> functions can't clobber <passwd.h>
 // functions' state and vice versa.
+#include "bionic/pthread_internal.h"
+static group_state_t* get_group_tls_buffer() {
+  return &__get_bionic_tls().group;
+}
 
-struct group_state_t {
-  group group_;
-  char* group_members_[2];
-  char group_name_buffer_[32];
-  // Must be last so init_group_state can run a simple memset for the above
-  ssize_t getgrent_idx;
-};
-
-struct passwd_state_t {
-  passwd passwd_;
-  char name_buffer_[32];
-  char dir_buffer_[32];
-  char sh_buffer_[32];
-  ssize_t getpwent_idx;
-};
-
-static ThreadLocalBuffer<group_state_t> g_group_tls_buffer;
-static ThreadLocalBuffer<passwd_state_t> g_passwd_tls_buffer;
+static passwd_state_t* get_passwd_tls_buffer() {
+  return &__get_bionic_tls().passwd;
+}
 
 static void init_group_state(group_state_t* state) {
   memset(state, 0, sizeof(group_state_t) - sizeof(state->getgrent_idx));
@@ -78,7 +67,7 @@
 }
 
 static group_state_t* __group_state() {
-  group_state_t* result = g_group_tls_buffer.get();
+  group_state_t* result = get_group_tls_buffer();
   if (result != nullptr) {
     init_group_state(result);
   }
@@ -432,7 +421,7 @@
 }
 
 passwd* getpwuid(uid_t uid) { // NOLINT: implementing bad function.
-  passwd_state_t* state = g_passwd_tls_buffer.get();
+  passwd_state_t* state = get_passwd_tls_buffer();
   if (state == NULL) {
     return NULL;
   }
@@ -450,7 +439,7 @@
 }
 
 passwd* getpwnam(const char* login) { // NOLINT: implementing bad function.
-  passwd_state_t* state = g_passwd_tls_buffer.get();
+  passwd_state_t* state = get_passwd_tls_buffer();
   if (state == NULL) {
     return NULL;
   }
@@ -483,7 +472,7 @@
 }
 
 void setpwent() {
-  passwd_state_t* state = g_passwd_tls_buffer.get();
+  passwd_state_t* state = get_passwd_tls_buffer();
   if (state) {
     state->getpwent_idx = 0;
   }
@@ -494,7 +483,7 @@
 }
 
 passwd* getpwent() {
-  passwd_state_t* state = g_passwd_tls_buffer.get();
+  passwd_state_t* state = get_passwd_tls_buffer();
   if (state == NULL) {
     return NULL;
   }
@@ -608,7 +597,7 @@
 }
 
 void setgrent() {
-  group_state_t* state = g_group_tls_buffer.get();
+  group_state_t* state = get_group_tls_buffer();
   if (state) {
     state->getgrent_idx = 0;
   }
@@ -619,7 +608,7 @@
 }
 
 group* getgrent() {
-  group_state_t* state = g_group_tls_buffer.get();
+  group_state_t* state = get_group_tls_buffer();
   if (state == NULL) {
     return NULL;
   }
diff --git a/libc/bionic/libgen.cpp b/libc/bionic/libgen.cpp
index c415c0f..33b46a1 100644
--- a/libc/bionic/libgen.cpp
+++ b/libc/bionic/libgen.cpp
@@ -34,10 +34,7 @@
 #include <sys/cdefs.h>
 #include <sys/param.h>
 
-#include "private/ThreadLocalBuffer.h"
-
-static ThreadLocalBuffer<char, MAXPATHLEN> g_basename_tls_buffer;
-static ThreadLocalBuffer<char, MAXPATHLEN> g_dirname_tls_buffer;
+#include "bionic/pthread_internal.h"
 
 static int __basename_r(const char* path, char* buffer, size_t buffer_size) {
   const char* startp = NULL;
@@ -161,13 +158,13 @@
 }
 
 char* basename(const char* path) {
-  char* buf = g_basename_tls_buffer.get();
-  int rc = __basename_r(path, buf, g_basename_tls_buffer.size());
+  char* buf = __get_bionic_tls().basename_buf;
+  int rc = __basename_r(path, buf, sizeof(__get_bionic_tls().basename_buf));
   return (rc < 0) ? NULL : buf;
 }
 
 char* dirname(const char* path) {
-  char* buf = g_dirname_tls_buffer.get();
-  int rc = __dirname_r(path, buf, g_dirname_tls_buffer.size());
+  char* buf = __get_bionic_tls().dirname_buf;
+  int rc = __dirname_r(path, buf, sizeof(__get_bionic_tls().dirname_buf));
   return (rc < 0) ? NULL : buf;
 }
diff --git a/libc/bionic/locale.cpp b/libc/bionic/locale.cpp
index 113118d..38e15b7 100644
--- a/libc/bionic/locale.cpp
+++ b/libc/bionic/locale.cpp
@@ -37,6 +37,8 @@
 
 #include "private/bionic_macros.h"
 
+#include "bionic/pthread_internal.h"
+
 // We only support two locales, the "C" locale (also known as "POSIX"),
 // and the "C.UTF-8" locale (also known as "en_US.UTF-8").
 
@@ -161,17 +163,9 @@
   return const_cast<char*>(__bionic_current_locale_is_utf8 ? "C.UTF-8" : "C");
 }
 
-// We can't use a constructor to create g_uselocal_key, because it may be used in constructors.
-static pthread_once_t g_uselocale_once = PTHREAD_ONCE_INIT;
-static pthread_key_t g_uselocale_key;
-
-static void g_uselocale_key_init() {
-  pthread_key_create(&g_uselocale_key, NULL);
-}
-
 locale_t uselocale(locale_t new_locale) {
-  pthread_once(&g_uselocale_once, g_uselocale_key_init);
-  locale_t old_locale = static_cast<locale_t>(pthread_getspecific(g_uselocale_key));
+  locale_t* locale_storage = &__get_bionic_tls().locale;
+  locale_t old_locale = *locale_storage;
 
   // If this is the first call to uselocale(3) on this thread, we return LC_GLOBAL_LOCALE.
   if (old_locale == NULL) {
@@ -179,7 +173,7 @@
   }
 
   if (new_locale != NULL) {
-    pthread_setspecific(g_uselocale_key, new_locale);
+    *locale_storage = new_locale;
   }
 
   return old_locale;
diff --git a/libc/bionic/mntent.cpp b/libc/bionic/mntent.cpp
index 994b84d..92284ce 100644
--- a/libc/bionic/mntent.cpp
+++ b/libc/bionic/mntent.cpp
@@ -29,15 +29,11 @@
 #include <mntent.h>
 #include <string.h>
 
-#include "private/ThreadLocalBuffer.h"
-
-static ThreadLocalBuffer<mntent> g_getmntent_mntent_tls_buffer;
-static ThreadLocalBuffer<char, BUFSIZ> g_getmntent_strings_tls_buffer;
+#include "bionic/pthread_internal.h"
 
 mntent* getmntent(FILE* fp) {
-  return getmntent_r(fp, g_getmntent_mntent_tls_buffer.get(),
-                     g_getmntent_strings_tls_buffer.get(),
-                     g_getmntent_strings_tls_buffer.size());
+  auto& tls = __get_bionic_tls();
+  return getmntent_r(fp, &tls.mntent_buf, tls.mntent_strings, sizeof(tls.mntent_strings));
 }
 
 mntent* getmntent_r(FILE* fp, struct mntent* e, char* buf, int buf_len) {
diff --git a/libc/bionic/pthread_create.cpp b/libc/bionic/pthread_create.cpp
index ab92853..f591c86 100644
--- a/libc/bionic/pthread_create.cpp
+++ b/libc/bionic/pthread_create.cpp
@@ -55,6 +55,18 @@
   // Slot 0 must point to itself. The x86 Linux kernel reads the TLS from %fs:0.
   thread->tls[TLS_SLOT_SELF] = thread->tls;
   thread->tls[TLS_SLOT_THREAD_ID] = thread;
+
+  // Add a guard page before and after.
+  size_t allocation_size = BIONIC_TLS_SIZE + 2 * PAGE_SIZE;
+  void* allocation = mmap(nullptr, allocation_size, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+  if (allocation == MAP_FAILED) {
+    __libc_fatal("failed to allocate TLS");
+  }
+
+  thread->bionic_tls = reinterpret_cast<bionic_tls*>(static_cast<char*>(allocation) + PAGE_SIZE);
+  if (mprotect(thread->bionic_tls, BIONIC_TLS_SIZE, PROT_READ | PROT_WRITE) != 0) {
+    __libc_fatal("failed to mprotect TLS");
+  }
 }
 
 void __init_thread_stack_guard(pthread_internal_t* thread) {
diff --git a/libc/bionic/pthread_internal.cpp b/libc/bionic/pthread_internal.cpp
index 5819bc1..2bc2bfb 100644
--- a/libc/bionic/pthread_internal.cpp
+++ b/libc/bionic/pthread_internal.cpp
@@ -86,6 +86,10 @@
 }
 
 static void __pthread_internal_free(pthread_internal_t* thread) {
+  // Unmap the TLS, including guard pages.
+  void* allocation = reinterpret_cast<char*>(thread->bionic_tls) - PAGE_SIZE;
+  munmap(allocation, BIONIC_TLS_SIZE + 2 * PAGE_SIZE);
+
   if (thread->mmap_size != 0) {
     // Free mapped space, including thread stack and pthread_internal_t.
     munmap(thread->attr.stack_base, thread->mmap_size);
diff --git a/libc/bionic/pthread_internal.h b/libc/bionic/pthread_internal.h
index d2abea0..b170299 100644
--- a/libc/bionic/pthread_internal.h
+++ b/libc/bionic/pthread_internal.h
@@ -110,6 +110,8 @@
    */
 #define __BIONIC_DLERROR_BUFFER_SIZE 512
   char dlerror_buffer[__BIONIC_DLERROR_BUFFER_SIZE];
+
+  bionic_tls* bionic_tls;
 };
 
 __LIBC_HIDDEN__ int __init_thread(pthread_internal_t* thread);
@@ -133,6 +135,10 @@
   return nullptr;
 }
 
+static inline __always_inline bionic_tls& __get_bionic_tls() {
+  return *__get_thread()->bionic_tls;
+}
+
 __LIBC_HIDDEN__ void pthread_key_clean_all(void);
 
 #if defined(__LP64__)
diff --git a/libc/bionic/pty.cpp b/libc/bionic/pty.cpp
index d699ff5..bdabf36 100644
--- a/libc/bionic/pty.cpp
+++ b/libc/bionic/pty.cpp
@@ -36,10 +36,7 @@
 #include <unistd.h>
 #include <utmp.h>
 
-#include "private/ThreadLocalBuffer.h"
-
-static ThreadLocalBuffer<char, 32> g_ptsname_tls_buffer;
-static ThreadLocalBuffer<char, 64> g_ttyname_tls_buffer;
+#include "bionic/pthread_internal.h"
 
 int getpt() {
   return posix_openpt(O_RDWR|O_NOCTTY);
@@ -54,8 +51,9 @@
 }
 
 char* ptsname(int fd) {
-  char* buf = g_ptsname_tls_buffer.get();
-  int error = ptsname_r(fd, buf, g_ptsname_tls_buffer.size());
+  bionic_tls& tls = __get_bionic_tls();
+  char* buf = tls.ptsname_buf;
+  int error = ptsname_r(fd, buf, sizeof(tls.ptsname_buf));
   return (error == 0) ? buf : NULL;
 }
 
@@ -80,8 +78,9 @@
 }
 
 char* ttyname(int fd) {
-  char* buf = g_ttyname_tls_buffer.get();
-  int error = ttyname_r(fd, buf, g_ttyname_tls_buffer.size());
+  bionic_tls& tls = __get_bionic_tls();
+  char* buf = tls.ttyname_buf;
+  int error = ttyname_r(fd, buf, sizeof(tls.ttyname_buf));
   return (error == 0) ? buf : NULL;
 }
 
diff --git a/libc/bionic/strerror.cpp b/libc/bionic/strerror.cpp
index f74194f..99692ca 100644
--- a/libc/bionic/strerror.cpp
+++ b/libc/bionic/strerror.cpp
@@ -27,12 +27,11 @@
  */
 
 #include <string.h>
-#include "private/ThreadLocalBuffer.h"
+
+#include "bionic/pthread_internal.h"
 
 extern "C" const char* __strerror_lookup(int);
 
-static ThreadLocalBuffer<char, NL_TEXTMAX> g_strerror_tls_buffer;
-
 char* strerror(int error_number) {
   // Just return the original constant in the easy cases.
   char* result = const_cast<char*>(__strerror_lookup(error_number));
@@ -40,7 +39,8 @@
     return result;
   }
 
-  result = g_strerror_tls_buffer.get();
-  strerror_r(error_number, result, g_strerror_tls_buffer.size());
+  bionic_tls& tls = __get_bionic_tls();
+  result = tls.strerror_buf;
+  strerror_r(error_number, result, sizeof(tls.strerror_buf));
   return result;
 }
diff --git a/libc/bionic/strsignal.cpp b/libc/bionic/strsignal.cpp
index c389ddd..81a8f95 100644
--- a/libc/bionic/strsignal.cpp
+++ b/libc/bionic/strsignal.cpp
@@ -27,13 +27,12 @@
  */
 
 #include <string.h>
-#include "private/ThreadLocalBuffer.h"
+
+#include "bionic/pthread_internal.h"
 
 extern "C" const char* __strsignal_lookup(int);
 extern "C" const char* __strsignal(int, char*, size_t);
 
-static ThreadLocalBuffer<char, NL_TEXTMAX> g_strsignal_tls_buffer;
-
 char* strsignal(int signal_number) {
   // Just return the original constant in the easy cases.
   char* result = const_cast<char*>(__strsignal_lookup(signal_number));
@@ -41,6 +40,6 @@
     return result;
   }
 
-  return const_cast<char*>(__strsignal(signal_number, g_strsignal_tls_buffer.get(),
-                                       g_strsignal_tls_buffer.size()));
+  bionic_tls& tls = __get_bionic_tls();
+  return const_cast<char*>(__strsignal(signal_number, tls.strsignal_buf, sizeof(tls.strsignal_buf)));
 }
diff --git a/libc/include/android/legacy_termios_inlines.h b/libc/include/android/legacy_termios_inlines.h
index 4424bdb..02e9429 100644
--- a/libc/include/android/legacy_termios_inlines.h
+++ b/libc/include/android/legacy_termios_inlines.h
@@ -91,6 +91,18 @@
   s->c_cflag |= CS8;
 }
 
+static __inline int cfsetspeed(struct termios* s, speed_t speed) {
+  // TODO: check 'speed' is valid.
+  s->c_cflag = (s->c_cflag & ~CBAUD) | (speed & CBAUD);
+  return 0;
+}
+
+static __inline int tcdrain(int fd) {
+  // A non-zero argument to TCSBRK means "don't send a break".
+  // The drain is a side-effect of the ioctl!
+  return ioctl(fd, TCSBRK, __BIONIC_CAST(static_cast, unsigned long, 1));
+}
+
 __END_DECLS
 
 #endif
diff --git a/libc/include/sys/epoll.h b/libc/include/sys/epoll.h
index 4ec8969..b7fdd4d 100644
--- a/libc/include/sys/epoll.h
+++ b/libc/include/sys/epoll.h
@@ -31,11 +31,17 @@
 
 #include <sys/cdefs.h>
 #include <sys/types.h>
-#include <fcntl.h> /* For O_CLOEXEC. */
 #include <signal.h> /* For sigset_t. */
 
+#include <linux/eventpoll.h>
+/* TODO: https://lkml.org/lkml/2017/2/23/416 has a better fix. */
+#undef EPOLLWAKEUP
+#undef EPOLLONESHOT
+#undef EPOLLET
+
 __BEGIN_DECLS
 
+/* TODO: remove once https://lkml.org/lkml/2017/2/23/417 is upstream. */
 #define EPOLLIN          0x00000001
 #define EPOLLPRI         0x00000002
 #define EPOLLOUT         0x00000004
@@ -51,12 +57,6 @@
 #define EPOLLONESHOT     0x40000000
 #define EPOLLET          0x80000000
 
-#define EPOLL_CTL_ADD    1
-#define EPOLL_CTL_DEL    2
-#define EPOLL_CTL_MOD    3
-
-#define EPOLL_CLOEXEC O_CLOEXEC
-
 typedef union epoll_data {
   void* ptr;
   int fd;
diff --git a/libc/include/termios.h b/libc/include/termios.h
index 49ffcd2..66ae71c 100644
--- a/libc/include/termios.h
+++ b/libc/include/termios.h
@@ -40,8 +40,10 @@
 speed_t cfgetispeed(const struct termios*) __INTRODUCED_IN(21);
 speed_t cfgetospeed(const struct termios*) __INTRODUCED_IN(21);
 void cfmakeraw(struct termios*) __INTRODUCED_IN(21);
+int cfsetspeed(struct termios*, speed_t) __INTRODUCED_IN(21);
 int cfsetispeed(struct termios*, speed_t) __INTRODUCED_IN(21);
 int cfsetospeed(struct termios*, speed_t) __INTRODUCED_IN(21);
+int tcdrain(int) __INTRODUCED_IN(21);
 int tcflow(int, int) __INTRODUCED_IN(21);
 int tcflush(int, int) __INTRODUCED_IN(21);
 int tcgetattr(int, struct termios*) __INTRODUCED_IN(21);
@@ -50,9 +52,6 @@
 int tcsetattr(int, int, const struct termios*) __INTRODUCED_IN(21);
 #endif
 
-int cfsetspeed(struct termios*, speed_t) __INTRODUCED_IN(21);
-int tcdrain(int) __INTRODUCED_IN(21);
-
 __END_DECLS
 
 #include <android/legacy_termios_inlines.h>
diff --git a/libc/kernel/tools/defaults.py b/libc/kernel/tools/defaults.py
index d5577da..620bb31 100644
--- a/libc/kernel/tools/defaults.py
+++ b/libc/kernel/tools/defaults.py
@@ -75,6 +75,8 @@
     "SIGRTMAX": "__SIGRTMAX",
     # We want to support both BSD and Linux member names in struct udphdr.
     "udphdr": "__kernel_udphdr",
+    # The kernel's struct epoll_event just has __u64 for the data.
+    "epoll_event": "__kernel_uapi_epoll_event",
     }
 
 # this is the set of known static inline functions that we want to keep
diff --git a/libc/kernel/uapi/linux/eventpoll.h b/libc/kernel/uapi/linux/eventpoll.h
index ec51b9f..6c7e355 100644
--- a/libc/kernel/uapi/linux/eventpoll.h
+++ b/libc/kernel/uapi/linux/eventpoll.h
@@ -37,7 +37,7 @@
 #define EPOLL_PACKED
 /* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
 #endif
-struct epoll_event {
+struct __kernel_uapi_epoll_event {
   __u32 events;
   __u64 data;
 /* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
diff --git a/libc/private/bionic_tls.h b/libc/private/bionic_tls.h
index c61e2ff..852b9ae 100644
--- a/libc/private/bionic_tls.h
+++ b/libc/private/bionic_tls.h
@@ -29,10 +29,15 @@
 #ifndef __BIONIC_PRIVATE_BIONIC_TLS_H_
 #define __BIONIC_PRIVATE_BIONIC_TLS_H_
 
+#include <locale.h>
+#include <mntent.h>
+#include <stdio.h>
 #include <sys/cdefs.h>
+#include <sys/param.h>
 
 #include "bionic_macros.h"
 #include "__get_tls.h"
+#include "grp_pwd.h"
 
 __BEGIN_DECLS
 
@@ -77,6 +82,28 @@
   BIONIC_TLS_SLOTS // Must come last!
 };
 
+// ~3 pages.
+struct bionic_tls {
+  locale_t locale;
+
+  char basename_buf[MAXPATHLEN];
+  char dirname_buf[MAXPATHLEN];
+
+  mntent mntent_buf;
+  char mntent_strings[BUFSIZ];
+
+  char ptsname_buf[32];
+  char ttyname_buf[64];
+
+  char strerror_buf[NL_TEXTMAX];
+  char strsignal_buf[NL_TEXTMAX];
+
+  group_state_t group;
+  passwd_state_t passwd;
+};
+
+#define BIONIC_TLS_SIZE (BIONIC_ALIGN(sizeof(bionic_tls), PAGE_SIZE))
+
 /*
  * Bionic uses some pthread keys internally. All pthread keys used internally
  * should be created in constructors, except for keys that may be used in or
@@ -86,22 +113,10 @@
  * pthread_test should fail if we forget.
  *
  * These are the pthread keys currently used internally by libc:
- *
- *  basename               libc (ThreadLocalBuffer)
- *  dirname                libc (ThreadLocalBuffer)
- *  uselocale              libc (can be used in constructors)
- *  getmntent_mntent       libc (ThreadLocalBuffer)
- *  getmntent_strings      libc (ThreadLocalBuffer)
- *  ptsname                libc (ThreadLocalBuffer)
- *  ttyname                libc (ThreadLocalBuffer)
- *  strerror               libc (ThreadLocalBuffer)
- *  strsignal              libc (ThreadLocalBuffer)
- *  passwd                 libc (ThreadLocalBuffer)
- *  group                  libc (ThreadLocalBuffer)
  *  _res_key               libc (constructor in BSD code)
  */
 
-#define LIBC_PTHREAD_KEY_RESERVED_COUNT 12
+#define LIBC_PTHREAD_KEY_RESERVED_COUNT 1
 
 /* Internally, jemalloc uses a single key for per thread data. */
 #define JEMALLOC_PTHREAD_KEY_RESERVED_COUNT 1
diff --git a/libc/private/ThreadLocalBuffer.h b/libc/private/grp_pwd.h
similarity index 60%
rename from libc/private/ThreadLocalBuffer.h
rename to libc/private/grp_pwd.h
index 5e43665..e1aff4f 100644
--- a/libc/private/ThreadLocalBuffer.h
+++ b/libc/private/grp_pwd.h
@@ -1,5 +1,7 @@
+#pragma once
+
 /*
- * Copyright (C) 2012 The Android Open Source Project
+ * Copyright (C) 2017 The Android Open Source Project
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -26,36 +28,21 @@
  * SUCH DAMAGE.
  */
 
-#ifndef _BIONIC_THREAD_LOCAL_BUFFER_H_included
-#define _BIONIC_THREAD_LOCAL_BUFFER_H_included
+#include <grp.h>
+#include <pwd.h>
 
-#include <malloc.h>
-#include <pthread.h>
-
-// TODO: use __thread instead?
-
-template <typename T, size_t Size = sizeof(T)>
-class ThreadLocalBuffer {
- public:
-  ThreadLocalBuffer() {
-    // We used to use pthread_once to initialize the keys, but life is more predictable
-    // if we allocate them all up front when the C library starts up, via __constructor__.
-    pthread_key_create(&key_, free);
-  }
-
-  T* get() {
-    T* result = reinterpret_cast<T*>(pthread_getspecific(key_));
-    if (result == nullptr) {
-      result = reinterpret_cast<T*>(calloc(1, Size));
-      pthread_setspecific(key_, result);
-    }
-    return result;
-  }
-
-  size_t size() { return Size; }
-
- private:
-  pthread_key_t key_;
+struct group_state_t {
+  group group_;
+  char* group_members_[2];
+  char group_name_buffer_[32];
+  // Must be last so init_group_state can run a simple memset for the above
+  ssize_t getgrent_idx;
 };
 
-#endif // _BIONIC_THREAD_LOCAL_BUFFER_H_included
+struct passwd_state_t {
+  passwd passwd_;
+  char name_buffer_[32];
+  char dir_buffer_[32];
+  char sh_buffer_[32];
+  ssize_t getpwent_idx;
+};
diff --git a/libc/seccomp/arm64_policy.c b/libc/seccomp/arm64_policy.c
index b05a88c..fc7127f 100644
--- a/libc/seccomp/arm64_policy.c
+++ b/libc/seccomp/arm64_policy.c
@@ -5,42 +5,32 @@
 
 #include "seccomp_policy.h"
 const struct sock_filter arm64_filter[] = {
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 5, 0, 35),
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 140, 17, 0),
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 101, 9, 0),
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 43, 5, 0),
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 32, 3, 0),
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 5, 0, 25),
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 203, 13, 0),
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 101, 7, 0),
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 43, 3, 0),
 BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 19, 1, 0),
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 18, 30, 29), //setxattr|lsetxattr|fsetxattr|getxattr|lgetxattr|fgetxattr|listxattr|llistxattr|flistxattr|removexattr|lremovexattr|fremovexattr|getcwd
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 30, 29, 28), //eventfd2|epoll_create1|epoll_ctl|epoll_pwait|dup|dup3|fcntl|inotify_init1|inotify_add_watch|inotify_rm_watch|ioctl
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 41, 28, 27), //flock|mknodat|mkdirat|unlinkat|symlinkat|linkat|renameat|umount2|mount
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 18, 21, 20), //setxattr|lsetxattr|fsetxattr|getxattr|lgetxattr|fgetxattr|listxattr|llistxattr|flistxattr|removexattr|lremovexattr|fremovexattr|getcwd
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 42, 20, 19), //eventfd2|epoll_create1|epoll_ctl|epoll_pwait|dup|dup3|fcntl|inotify_init1|inotify_add_watch|inotify_rm_watch|ioctl|ioprio_set|ioprio_get|flock|mknodat|mkdirat|unlinkat|symlinkat|linkat|renameat|umount2|mount|pivot_root
 BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 59, 1, 0),
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 58, 26, 25), //statfs|fstatfs|truncate|ftruncate|fallocate|faccessat|chdir|fchdir|chroot|fchmod|fchmodat|fchownat|fchown|openat|close
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 98, 25, 24), //pipe2|quotactl|getdents64|lseek|read|write|readv|writev|pread64|pwrite64|preadv|pwritev|sendfile|pselect6|ppoll|signalfd4|vmsplice|splice|tee|readlinkat|newfstatat|fstat|sync|fsync|fdatasync|sync_file_range|timerfd_create|timerfd_settime|timerfd_gettime|utimensat|acct|capget|capset|personality|exit|exit_group|waitid|set_tid_address|unshare
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 129, 3, 0),
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 105, 1, 0),
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 104, 22, 21), //nanosleep|getitimer|setitimer
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 128, 21, 20), //init_module|delete_module|timer_create|timer_gettime|timer_getoverrun|timer_settime|timer_delete|clock_settime|clock_gettime|clock_getres|clock_nanosleep|syslog|ptrace|sched_setparam|sched_setscheduler|sched_getscheduler|sched_getparam|sched_setaffinity|sched_getaffinity|sched_yield|sched_get_priority_max|sched_get_priority_min|sched_rr_get_interval
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 131, 1, 0),
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 130, 19, 18), //kill
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 139, 18, 17), //tgkill|sigaltstack|rt_sigsuspend|rt_sigaction|rt_sigprocmask|rt_sigpending|rt_sigtimedwait|rt_sigqueueinfo
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 242, 9, 0),
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 203, 5, 0),
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 58, 18, 17), //statfs|fstatfs|truncate|ftruncate|fallocate|faccessat|chdir|fchdir|chroot|fchmod|fchmodat|fchownat|fchown|openat|close
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 99, 17, 16), //pipe2|quotactl|getdents64|lseek|read|write|readv|writev|pread64|pwrite64|preadv|pwritev|sendfile|pselect6|ppoll|signalfd4|vmsplice|splice|tee|readlinkat|newfstatat|fstat|sync|fsync|fdatasync|sync_file_range|timerfd_create|timerfd_settime|timerfd_gettime|utimensat|acct|capget|capset|personality|exit|exit_group|waitid|set_tid_address|unshare|futex
 BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 198, 3, 0),
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 179, 1, 0),
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 178, 13, 12), //setpriority|getpriority|reboot|setregid|setgid|setreuid|setuid|setresuid|getresuid|setresgid|getresgid|setfsuid|setfsgid|times|setpgid|getpgid|getsid|setsid|getgroups|setgroups|uname|sethostname|setdomainname|getrlimit|setrlimit|getrusage|umask|prctl|getcpu|gettimeofday|settimeofday|adjtimex|getpid|getppid|getuid|geteuid|getgid|getegid
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 180, 12, 11), //sysinfo
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 202, 11, 10), //socket|socketpair|bind|listen
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 221, 1, 0),
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 217, 9, 8), //connect|getsockname|getpeername|sendto|recvfrom|setsockopt|getsockopt|shutdown|sendmsg|recvmsg|readahead|brk|munmap|mremap
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 234, 8, 7), //execve|mmap|fadvise64|swapon|swapoff|mprotect|msync|mlock|munlock|mlockall|munlockall|mincore|madvise
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 266, 3, 0),
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 260, 1, 0),
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 244, 5, 4), //accept4|recvmmsg
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 262, 4, 3), //wait4|prlimit64
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 268, 1, 0),
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 267, 2, 1), //clock_adjtime
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 272, 1, 0), //setns|sendmmsg|process_vm_readv|process_vm_writev
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 105, 1, 0),
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 104, 14, 13), //nanosleep|getitimer|setitimer
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 180, 13, 12), //init_module|delete_module|timer_create|timer_gettime|timer_getoverrun|timer_settime|timer_delete|clock_settime|clock_gettime|clock_getres|clock_nanosleep|syslog|ptrace|sched_setparam|sched_setscheduler|sched_getscheduler|sched_getparam|sched_setaffinity|sched_getaffinity|sched_yield|sched_get_priority_max|sched_get_priority_min|sched_rr_get_interval|restart_syscall|kill|tkill|tgkill|sigaltstack|rt_sigsuspend|rt_sigaction|rt_sigprocmask|rt_sigpending|rt_sigtimedwait|rt_sigqueueinfo|rt_sigreturn|setpriority|getpriority|reboot|setregid|setgid|setreuid|setuid|setresuid|getresuid|setresgid|getresgid|setfsuid|setfsgid|times|setpgid|getpgid|getsid|setsid|getgroups|setgroups|uname|sethostname|setdomainname|getrlimit|setrlimit|getrusage|umask|prctl|getcpu|gettimeofday|settimeofday|adjtimex|getpid|getppid|getuid|geteuid|getgid|getegid|gettid|sysinfo
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 202, 12, 11), //socket|socketpair|bind|listen
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 260, 5, 0),
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 240, 3, 0),
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 220, 1, 0),
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 217, 8, 7), //connect|getsockname|getpeername|sendto|recvfrom|setsockopt|getsockopt|shutdown|sendmsg|recvmsg|readahead|brk|munmap|mremap
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 234, 7, 6), //clone|execve|mmap|fadvise64|swapon|swapoff|mprotect|msync|mlock|munlock|mlockall|munlockall|mincore|madvise
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 244, 6, 5), //rt_tgsigqueueinfo|perf_event_open|accept4|recvmmsg
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 277, 3, 0),
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 266, 1, 0),
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 262, 3, 2), //wait4|prlimit64
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 272, 2, 1), //clock_adjtime|syncfs|setns|sendmmsg|process_vm_readv|process_vm_writev
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 279, 1, 0), //seccomp|getrandom
 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
 };
 
diff --git a/libc/seccomp/arm_policy.c b/libc/seccomp/arm_policy.c
index 1b9ba2f..f175d6a 100644
--- a/libc/seccomp/arm_policy.c
+++ b/libc/seccomp/arm_policy.c
@@ -5,138 +5,130 @@
 
 #include "seccomp_policy.h"
 const struct sock_filter arm_filter[] = {
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 1, 0, 133),
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 174, 67, 0),
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 77, 33, 0),
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 45, 17, 0),
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 26, 9, 0),
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 11, 5, 0),
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 6, 3, 0),
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 3, 1, 0),
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 2, 126, 125), //exit
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 5, 125, 124), //read|write
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 7, 124, 123), //close
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 0, 0, 125),
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 168, 63, 0),
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 77, 31, 0),
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 45, 15, 0),
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 26, 7, 0),
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 10, 3, 0),
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 8, 1, 0),
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 7, 119, 118), //restart_syscall|exit|fork|read|write|open|close
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 9, 118, 117), //creat
 BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 19, 1, 0),
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 13, 122, 121), //execve|chdir
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 22, 121, 120), //lseek|getpid|mount
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 41, 3, 0),
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 36, 1, 0),
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 27, 118, 117), //ptrace
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 38, 117, 116), //sync|kill
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 43, 1, 0),
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 42, 115, 114), //dup
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 44, 114, 113), //times
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 13, 116, 115), //unlink|execve|chdir
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 22, 115, 114), //lseek|getpid|mount
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 36, 3, 0),
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 33, 1, 0),
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 27, 112, 111), //ptrace
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 34, 111, 110), //access
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 41, 1, 0),
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 38, 109, 108), //sync|kill
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 44, 108, 107), //dup|pipe|times
 BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 60, 7, 0),
 BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 54, 3, 0),
 BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 51, 1, 0),
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 46, 110, 109), //brk
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 53, 109, 108), //acct|umount2
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 46, 104, 103), //brk
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 53, 103, 102), //acct|umount2
 BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 57, 1, 0),
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 55, 107, 106), //ioctl
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 58, 106, 105), //setpgid
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 56, 101, 100), //ioctl|fcntl
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 58, 100, 99), //setpgid
 BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 66, 3, 0),
 BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 64, 1, 0),
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 62, 103, 102), //umask|chroot
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 65, 102, 101), //getppid
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 62, 97, 96), //umask|chroot
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 65, 96, 95), //getppid
 BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 74, 1, 0),
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 68, 100, 99), //setsid|sigaction
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 76, 99, 98), //sethostname|setrlimit
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 124, 17, 0),
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 103, 9, 0),
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 94, 5, 0),
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 91, 3, 0),
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 87, 1, 0),
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 80, 93, 92), //getrusage|gettimeofday|settimeofday
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 89, 92, 91), //swapon|reboot
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 93, 91, 90), //munmap|truncate
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 68, 94, 93), //setsid|sigaction
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 76, 93, 92), //sethostname|setrlimit
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 118, 15, 0),
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 94, 7, 0),
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 87, 3, 0),
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 85, 1, 0),
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 80, 88, 87), //getrusage|gettimeofday|settimeofday
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 86, 87, 86), //readlink
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 91, 1, 0),
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 89, 85, 84), //swapon|reboot
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 93, 84, 83), //munmap|truncate
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 103, 3, 0),
 BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 96, 1, 0),
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 95, 89, 88), //fchmod
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 98, 88, 87), //getpriority|setpriority
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 118, 3, 0),
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 95, 81, 80), //fchmod
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 98, 80, 79), //getpriority|setpriority
 BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 114, 1, 0),
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 106, 85, 84), //syslog|setitimer|getitimer
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 117, 84, 83), //wait4|swapoff|sysinfo
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 121, 1, 0),
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 119, 82, 81), //fsync
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 123, 81, 80), //setdomainname|uname
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 138, 7, 0),
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 131, 3, 0),
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 128, 1, 0),
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 126, 77, 76), //adjtimex|mprotect
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 130, 76, 75), //init_module|delete_module
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 136, 1, 0),
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 134, 74, 73), //quotactl|getpgid|fchdir
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 137, 73, 72), //personality
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 150, 3, 0),
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 143, 1, 0),
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 141, 70, 69), //setfsuid|setfsgid|_llseek
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 149, 69, 68), //flock|msync|readv|writev|getsid|fdatasync
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 106, 78, 77), //syslog|setitimer|getitimer
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 117, 77, 76), //wait4|swapoff|sysinfo
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 136, 7, 0),
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 128, 3, 0),
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 124, 1, 0),
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 123, 73, 72), //fsync|sigreturn|clone|setdomainname|uname
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 126, 72, 71), //adjtimex|mprotect
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 131, 1, 0),
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 130, 70, 69), //init_module|delete_module
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 134, 69, 68), //quotactl|getpgid|fchdir
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 143, 3, 0),
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 138, 1, 0),
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 137, 66, 65), //personality
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 142, 65, 64), //setfsuid|setfsgid|_llseek|getdents
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 150, 1, 0),
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 149, 63, 62), //flock|msync|readv|writev|getsid|fdatasync
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 164, 62, 61), //mlock|munlock|mlockall|munlockall|sched_setparam|sched_getparam|sched_setscheduler|sched_getscheduler|sched_yield|sched_get_priority_max|sched_get_priority_min|sched_rr_get_interval|nanosleep|mremap
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 290, 31, 0),
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 219, 15, 0),
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 199, 7, 0),
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 183, 3, 0),
 BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 172, 1, 0),
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 164, 67, 66), //mlock|munlock|mlockall|munlockall|sched_setparam|sched_getparam|sched_setscheduler|sched_getscheduler|sched_yield|sched_get_priority_max|sched_get_priority_min|sched_rr_get_interval|nanosleep|mremap
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 173, 66, 65), //prctl
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 290, 33, 0),
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 239, 17, 0),
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 213, 9, 0),
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 197, 5, 0),
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 191, 3, 0),
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 183, 1, 0),
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 182, 59, 58), //rt_sigaction|rt_sigprocmask|rt_sigpending|rt_sigtimedwait|rt_sigqueueinfo|rt_sigsuspend|pread64|pwrite64
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 188, 58, 57), //getcwd|capget|capset|sigaltstack|sendfile
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 195, 57, 56), //ugetrlimit|mmap2|truncate64|ftruncate64
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 199, 1, 0),
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 198, 55, 54), //fstat64
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 212, 54, 53), //getuid32|getgid32|geteuid32|getegid32|setreuid32|setregid32|getgroups32|setgroups32|fchown32|setresuid32|getresuid32|setresgid32|getresgid32
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 219, 3, 0),
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 169, 56, 55), //poll
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 182, 55, 54), //prctl|rt_sigreturn|rt_sigaction|rt_sigprocmask|rt_sigpending|rt_sigtimedwait|rt_sigqueueinfo|rt_sigsuspend|pread64|pwrite64
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 190, 1, 0),
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 188, 53, 52), //getcwd|capget|capset|sigaltstack|sendfile
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 198, 52, 51), //vfork|ugetrlimit|mmap2|truncate64|ftruncate64|stat64|lstat64|fstat64
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 217, 3, 0),
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 213, 1, 0),
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 212, 49, 48), //getuid32|getgid32|geteuid32|getegid32|setreuid32|setregid32|getgroups32|setgroups32|fchown32|setresuid32|getresuid32|setresgid32|getresgid32
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 215, 48, 47), //setuid32|setgid32
 BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 217, 1, 0),
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 215, 51, 50), //setuid32|setgid32
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 218, 50, 49), //getdents64
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 225, 1, 0),
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 222, 48, 47), //mincore|madvise|fcntl64
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 238, 47, 46), //readahead|setxattr|lsetxattr|fsetxattr|getxattr|lgetxattr|fgetxattr|listxattr|llistxattr|flistxattr|removexattr|lremovexattr|fremovexattr
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 218, 46, 45), //getdents64
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 218, 45, 44), //getdents64
 BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 256, 7, 0),
 BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 248, 3, 0),
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 241, 1, 0),
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 240, 43, 42), //sendfile64
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 243, 42, 41), //sched_setaffinity|sched_getaffinity
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 251, 1, 0),
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 249, 40, 39), //exit_group
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 252, 39, 38), //epoll_ctl
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 224, 1, 0),
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 222, 41, 40), //mincore|madvise|fcntl64
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 243, 40, 39), //gettid|readahead|setxattr|lsetxattr|fsetxattr|getxattr|lgetxattr|fgetxattr|listxattr|llistxattr|flistxattr|removexattr|lremovexattr|fremovexattr|tkill|sendfile64|futex|sched_setaffinity|sched_getaffinity
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 250, 1, 0),
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 249, 38, 37), //exit_group
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 253, 37, 36), //epoll_create|epoll_ctl|epoll_wait
 BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 280, 3, 0),
 BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 270, 1, 0),
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 269, 36, 35), //set_tid_address|timer_create|timer_settime|timer_gettime|timer_getoverrun|timer_delete|clock_settime|clock_gettime|clock_getres|clock_nanosleep|statfs64|fstatfs64|tgkill
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 271, 35, 34), //arm_fadvise64_64
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 269, 34, 33), //set_tid_address|timer_create|timer_settime|timer_gettime|timer_getoverrun|timer_delete|clock_settime|clock_gettime|clock_getres|clock_nanosleep|statfs64|fstatfs64|tgkill
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 271, 33, 32), //arm_fadvise64_64
 BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 286, 1, 0),
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 285, 33, 32), //waitid|socket|bind|connect|listen
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 289, 32, 31), //getsockname|getpeername|socketpair
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 285, 31, 30), //waitid|socket|bind|connect|listen
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 289, 30, 29), //getsockname|getpeername|socketpair
 BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 350, 15, 0),
 BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 327, 7, 0),
 BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 317, 3, 0),
 BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 292, 1, 0),
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 291, 27, 26), //sendto
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 298, 26, 25), //recvfrom|shutdown|setsockopt|getsockopt|sendmsg|recvmsg
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 291, 25, 24), //sendto
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 298, 24, 23), //recvfrom|shutdown|setsockopt|getsockopt|sendmsg|recvmsg
 BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 322, 1, 0),
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 319, 24, 23), //inotify_add_watch|inotify_rm_watch
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 326, 23, 22), //openat|mkdirat|mknodat|fchownat
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 319, 22, 21), //inotify_add_watch|inotify_rm_watch
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 326, 21, 20), //openat|mkdirat|mknodat|fchownat
 BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 345, 3, 0),
 BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 340, 1, 0),
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 338, 20, 19), //fstatat64|unlinkat|renameat|linkat|symlinkat|readlinkat|fchmodat|faccessat|pselect6|ppoll|unshare
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 344, 19, 18), //splice|sync_file_range2|tee|vmsplice
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 338, 18, 17), //fstatat64|unlinkat|renameat|linkat|symlinkat|readlinkat|fchmodat|faccessat|pselect6|ppoll|unshare
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 344, 17, 16), //splice|sync_file_range2|tee|vmsplice
 BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 348, 1, 0),
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 347, 17, 16), //getcpu|epoll_pwait
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 349, 16, 15), //utimensat
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 372, 7, 0),
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 365, 3, 0),
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 352, 1, 0),
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 351, 12, 11), //timerfd_create
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 363, 11, 10), //fallocate|timerfd_settime|timerfd_gettime|signalfd4|eventfd2|epoll_create1|dup3|pipe2|inotify_init1|preadv|pwritev
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 347, 15, 14), //getcpu|epoll_pwait
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 349, 14, 13), //utimensat
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 383, 7, 0),
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 372, 3, 0),
 BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 369, 1, 0),
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 367, 9, 8), //recvmmsg|accept4
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 370, 8, 7), //prlimit64
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 983042, 3, 0),
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 367, 10, 9), //timerfd_create|eventfd|fallocate|timerfd_settime|timerfd_gettime|signalfd4|eventfd2|epoll_create1|dup3|pipe2|inotify_init1|preadv|pwritev|rt_tgsigqueueinfo|perf_event_open|recvmmsg|accept4
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 370, 9, 8), //prlimit64
 BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 374, 1, 0),
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 373, 5, 4), //clock_adjtime
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 378, 4, 3), //sendmmsg|setns|process_vm_readv|process_vm_writev
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 983045, 1, 0),
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 373, 7, 6), //clock_adjtime
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 378, 6, 5), //sendmmsg|setns|process_vm_readv|process_vm_writev
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 983045, 3, 0),
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 983042, 1, 0),
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 385, 3, 2), //seccomp|getrandom
 BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 983043, 2, 1), //__ARM_NR_cacheflush
 BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 983046, 1, 0), //__ARM_NR_set_tls
 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
diff --git a/libc/tools/genseccomp.py b/libc/tools/genseccomp.py
index fa6e7e3..7d2b1da 100755
--- a/libc/tools/genseccomp.py
+++ b/libc/tools/genseccomp.py
@@ -5,7 +5,8 @@
 from gensyscalls import SysCallsTxtParser
 
 
-syscall_file = "SYSCALLS.TXT"
+BPF_JGE = "BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, {0}, {1}, {2})"
+BPF_ALLOW = "BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW)"
 
 
 class SyscallRange(object):
@@ -14,6 +15,9 @@
     self.begin = value
     self.end = self.begin + 1
 
+  def __str__(self):
+    return "(%s, %s, %s)" % (self.begin, self.end, self.names)
+
   def add(self, name, value):
     if value != self.end:
       raise ValueError
@@ -21,39 +25,21 @@
     self.names.append(name)
 
 
-def generate_bpf_jge(value, ge_target, less_target):
-  return "BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, {0}, {1}, {2})".format(value, ge_target, less_target)
-
-
-# Converts the sorted ranges of allowed syscalls to a binary tree bpf
-# For a single range, output a simple jump to {fail} or {allow}. We can't set
-# the jump ranges yet, since we don't know the size of the filter, so use a
-# placeholder
-# For multiple ranges, split into two, convert the two halves and output a jump
-# to the correct half
-def convert_to_bpf(ranges):
-  if len(ranges) == 1:
-    # We will replace {fail} and {allow} with appropriate range jumps later
-    return [generate_bpf_jge(ranges[0].end, "{fail}", "{allow}") +
-            ", //" + "|".join(ranges[0].names)]
-  else:
-    half = (len(ranges) + 1) / 2
-    first = convert_to_bpf(ranges[:half])
-    second = convert_to_bpf(ranges[half:])
-    return [generate_bpf_jge(ranges[half].begin, len(first), 0) + ","] + first + second
-
-
-def construct_bpf(architecture, header_dir, output_path):
-  parser = SysCallsTxtParser()
-  parser.parse_file(syscall_file)
-  syscalls = parser.syscalls
+def get_names(syscall_files, architecture):
+  syscalls = []
+  for syscall_file in syscall_files:
+    parser = SysCallsTxtParser()
+    parser.parse_open_file(syscall_file)
+    syscalls += parser.syscalls
 
   # Select only elements matching required architecture
   syscalls = [x for x in syscalls if architecture in x and x[architecture]]
 
   # We only want the name
-  names = [x["name"] for x in syscalls]
+  return [x["name"] for x in syscalls]
 
+
+def convert_names_to_NRs(names, header_dir):
   # Run preprocessor over the __NR_syscall symbols, including unistd.h,
   # to get the actual numbers
   prefix = "__SECCOMP_"  # prefix to ensure no name collisions
@@ -89,6 +75,10 @@
     value = eval(value)
     syscalls.append((name, value))
 
+  return syscalls
+
+
+def convert_NRs_to_ranges(syscalls):
   # Sort the values so we convert to ranges and binary chop
   syscalls = sorted(syscalls, lambda x, y: cmp(x[1], y[1]))
 
@@ -104,8 +94,30 @@
       last_range.add(name, value)
     else:
       ranges.append(SyscallRange(name, value))
+  return ranges
 
-  bpf = convert_to_bpf(ranges)
+
+# Converts the sorted ranges of allowed syscalls to a binary tree bpf
+# For a single range, output a simple jump to {fail} or {allow}. We can't set
+# the jump ranges yet, since we don't know the size of the filter, so use a
+# placeholder
+# For multiple ranges, split into two, convert the two halves and output a jump
+# to the correct half
+def convert_to_intermediate_bpf(ranges):
+  if len(ranges) == 1:
+    # We will replace {fail} and {allow} with appropriate range jumps later
+    return [BPF_JGE.format(ranges[0].end, "{fail}", "{allow}") +
+            ", //" + "|".join(ranges[0].names)]
+  else:
+    half = (len(ranges) + 1) / 2
+    first = convert_to_intermediate_bpf(ranges[:half])
+    second = convert_to_intermediate_bpf(ranges[half:])
+    jump = [BPF_JGE.format(ranges[half].begin, len(first), 0) + ","]
+    return jump + first + second
+
+
+def convert_ranges_to_bpf(ranges):
+  bpf = convert_to_intermediate_bpf(ranges)
 
   # Now we know the size of the tree, we can substitute the {fail} and {allow}
   # placeholders
@@ -121,16 +133,16 @@
                                 allow=str(len(bpf) - i - 1))
 
   # Add check that we aren't off the bottom of the syscalls
-  bpf.insert(0,
-             "BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, " + str(ranges[0].begin) +
-             ", 0, " + str(len(bpf)) + "),")
+  bpf.insert(0, BPF_JGE.format(ranges[0].begin, 0, str(len(bpf))) + ',')
 
   # Add the allow calls at the end. If the syscall is not matched, we will
   # continue. This allows the user to choose to match further syscalls, and
   # also to choose the action when we want to block
-  bpf.append("BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),")
+  bpf.append(BPF_ALLOW + ",")
+  return bpf
 
-  # And output policy
+
+def convert_bpf_to_output(bpf, architecture):
   header = textwrap.dedent("""\
     // Autogenerated file - edit at your peril!!
 
@@ -147,25 +159,52 @@
 
     const size_t {architecture}_filter_size = sizeof({architecture}_filter) / sizeof(struct sock_filter);
     """).format(architecture=architecture)
-  output = header + "\n".join(bpf) + footer
+  return header + "\n".join(bpf) + footer
 
-  existing = ""
-  if os.path.isfile(output_path):
-    existing = open(output_path).read()
-  if output == existing:
-    print "File " + output_path + " not changed."
-  else:
-    with open(output_path, "w") as output_file:
-      output_file.write(output)
 
-    print "Generated file " + output_path
+def construct_bpf(syscall_files, architecture, header_dir):
+  names = get_names(syscall_files, architecture)
+  syscalls = convert_names_to_NRs(names, header_dir)
+  ranges = convert_NRs_to_ranges(syscalls)
+  bpf = convert_ranges_to_bpf(ranges)
+  return convert_bpf_to_output(bpf, architecture)
+
+
+android_syscall_files = ["SYSCALLS.TXT", "SECCOMP_WHITELIST.TXT"]
+arm_headers = "kernel/uapi/asm-arm"
+arm64_headers = "kernel/uapi/asm-arm64"
+arm_architecture = "arm"
+arm64_architecture = "arm64"
+
+
+ANDROID_SYSCALL_FILES = ["SYSCALLS.TXT", "SECCOMP_WHITELIST.TXT"]
+
+POLICY_CONFIGS = [("arm", "kernel/uapi/asm-arm"),
+                  ("arm64", "kernel/uapi/asm-arm64")]
+
+
+def set_dir():
+  # Set working directory for predictable results
+  os.chdir(os.path.join(os.environ["ANDROID_BUILD_TOP"], "bionic/libc"))
 
 
 def main():
-  # Set working directory for predictable results
-  os.chdir(os.path.join(os.environ["ANDROID_BUILD_TOP"], "bionic/libc"))
-  construct_bpf("arm", "kernel/uapi/asm-arm", "seccomp/arm_policy.c")
-  construct_bpf("arm64", "kernel/uapi/asm-arm64", "seccomp/arm64_policy.c")
+  set_dir()
+  for arch, header_path in POLICY_CONFIGS:
+    files = [open(filename) for filename in ANDROID_SYSCALL_FILES]
+    output = construct_bpf(files, arch, header_path)
+
+    # And output policy
+    existing = ""
+    output_path = "seccomp/{}_policy.c".format(arch)
+    if os.path.isfile(output_path):
+      existing = open(output_path).read()
+    if output == existing:
+      print "File " + output_path + " not changed."
+    else:
+      with open(output_path, "w") as output_file:
+        output_file.write(output)
+      print "Generated file " + output_path
 
 
 if __name__ == "__main__":
diff --git a/libc/tools/gensyscalls.py b/libc/tools/gensyscalls.py
index 329184f..f50445c 100755
--- a/libc/tools/gensyscalls.py
+++ b/libc/tools/gensyscalls.py
@@ -500,18 +500,18 @@
 
         logging.debug(t)
 
-
-    def parse_file(self, file_path):
-        logging.debug("parse_file: %s" % file_path)
-        fp = open(file_path)
-        for line in fp.xreadlines():
+    def parse_open_file(self, fp):
+        for line in fp:
             self.lineno += 1
             line = line.strip()
             if not line: continue
             if line[0] == '#': continue
             self.parse_line(line)
 
-        fp.close()
+    def parse_file(self, file_path):
+        logging.debug("parse_file: %s" % file_path)
+        with open(file_path) as fp:
+            parse_open_file(fp)
 
 
 class State:
diff --git a/libc/tools/test_genseccomp.py b/libc/tools/test_genseccomp.py
new file mode 100755
index 0000000..de1e5fe
--- /dev/null
+++ b/libc/tools/test_genseccomp.py
@@ -0,0 +1,154 @@
+#!/usr/bin/env python
+# Unit tests for genseccomp.py
+
+import cStringIO
+import textwrap
+import unittest
+
+import genseccomp
+
+class TestGenseccomp(unittest.TestCase):
+  def setUp(self):
+    genseccomp.set_dir()
+
+  def get_config(self, arch):
+    for i in genseccomp.POLICY_CONFIGS:
+      if i[0] == arch:
+        return i
+    self.fail("No such architecture")
+
+  def get_headers(self, arch):
+    return self.get_config(arch)[1]
+
+  def test_get_names(self):
+    syscalls = cStringIO.StringIO(textwrap.dedent("""\
+int __llseek:_llseek(int, unsigned long, unsigned long, off64_t*, int) arm,mips,x86
+int         fchown:fchown(int, uid_t, gid_t)    arm64,mips,mips64,x86_64
+    """))
+
+    whitelist = cStringIO.StringIO(textwrap.dedent("""\
+ssize_t     read(int, void*, size_t)        all
+    """))
+
+    syscall_files = [syscalls, whitelist]
+    names = genseccomp.get_names(syscall_files, "arm")
+    for f in syscall_files:
+      f.seek(0)
+    names64 = genseccomp.get_names(syscall_files, "arm64")
+
+    self.assertIn("fchown", names64)
+    self.assertNotIn("fchown", names)
+    self.assertIn("_llseek", names)
+    self.assertNotIn("_llseek", names64)
+    self.assertIn("read", names)
+    self.assertIn("read", names64)
+
+  def test_convert_names_to_NRs(self):
+    self.assertEquals(genseccomp.convert_names_to_NRs(["open"],
+                                                      self.get_headers("arm")),
+                      [("open", 5)])
+
+    self.assertEquals(genseccomp.convert_names_to_NRs(["__ARM_NR_set_tls"],
+                                                      self.get_headers("arm")),
+                      [('__ARM_NR_set_tls', 983045)])
+
+    self.assertEquals(genseccomp.convert_names_to_NRs(["openat"],
+                                                      self.get_headers("arm64")),
+                      [("openat", 56)])
+
+
+  def test_convert_NRs_to_ranges(self):
+    ranges = genseccomp.convert_NRs_to_ranges([("b", 2), ("a", 1)])
+    self.assertEquals(len(ranges), 1)
+    self.assertEquals(ranges[0].begin, 1)
+    self.assertEquals(ranges[0].end, 3)
+    self.assertItemsEqual(ranges[0].names, ["a", "b"])
+
+    ranges = genseccomp.convert_NRs_to_ranges([("b", 3), ("a", 1)])
+    self.assertEquals(len(ranges), 2)
+    self.assertEquals(ranges[0].begin, 1)
+    self.assertEquals(ranges[0].end, 2)
+    self.assertItemsEqual(ranges[0].names, ["a"])
+    self.assertEquals(ranges[1].begin, 3)
+    self.assertEquals(ranges[1].end, 4)
+    self.assertItemsEqual(ranges[1].names, ["b"])
+
+  def test_convert_to_intermediate_bpf(self):
+    ranges = genseccomp.convert_NRs_to_ranges([("b", 2), ("a", 1)])
+    bpf = genseccomp.convert_to_intermediate_bpf(ranges)
+    self.assertEquals(bpf, ['BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 3, {fail}, {allow}), //a|b'])
+
+    ranges = genseccomp.convert_NRs_to_ranges([("b", 3), ("a", 1)])
+    bpf = genseccomp.convert_to_intermediate_bpf(ranges)
+    self.assertEquals(bpf, ['BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 3, 1, 0),',
+                            'BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 2, {fail}, {allow}), //a',
+                            'BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 4, {fail}, {allow}), //b'])
+
+  def test_convert_ranges_to_bpf(self):
+    ranges = genseccomp.convert_NRs_to_ranges([("b", 2), ("a", 1)])
+    bpf = genseccomp.convert_ranges_to_bpf(ranges)
+    self.assertEquals(bpf, ['BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 1, 0, 1),',
+                            'BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 3, 1, 0), //a|b',
+                            'BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),'])
+
+    ranges = genseccomp.convert_NRs_to_ranges([("b", 3), ("a", 1)])
+    bpf = genseccomp.convert_ranges_to_bpf(ranges)
+    self.assertEquals(bpf, ['BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 1, 0, 3),',
+                            'BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 3, 1, 0),',
+                            'BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 2, 2, 1), //a',
+                            'BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 4, 1, 0), //b',
+                            'BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),'])
+
+  def test_convert_bpf_to_output(self):
+    output = genseccomp.convert_bpf_to_output(["line1", "line2"], "arm")
+    expected_output = textwrap.dedent("""\
+    // Autogenerated file - edit at your peril!!
+
+    #include <linux/filter.h>
+    #include <errno.h>
+
+    #include "seccomp_policy.h"
+    const struct sock_filter arm_filter[] = {
+    line1
+    line2
+    };
+
+    const size_t arm_filter_size = sizeof(arm_filter) / sizeof(struct sock_filter);
+    """)
+    self.assertEquals(output, expected_output)
+
+  def test_construct_bpf(self):
+    syscalls = cStringIO.StringIO(textwrap.dedent("""\
+    int __llseek:_llseek(int, unsigned long, unsigned long, off64_t*, int) arm,mips,x86
+    int         fchown:fchown(int, uid_t, gid_t)    arm64,mips,mips64,x86_64
+    """))
+
+    whitelist = cStringIO.StringIO(textwrap.dedent("""\
+    ssize_t     read(int, void*, size_t)        all
+    """))
+
+    syscall_files = [syscalls, whitelist]
+    output = genseccomp.construct_bpf(syscall_files, "arm", self.get_headers("arm"))
+
+    expected_output = textwrap.dedent("""\
+    // Autogenerated file - edit at your peril!!
+
+    #include <linux/filter.h>
+    #include <errno.h>
+
+    #include "seccomp_policy.h"
+    const struct sock_filter arm_filter[] = {
+    BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 3, 0, 3),
+    BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 140, 1, 0),
+    BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 4, 2, 1), //read
+    BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 141, 1, 0), //_llseek
+    BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
+    };
+
+    const size_t arm_filter_size = sizeof(arm_filter) / sizeof(struct sock_filter);
+    """)
+    self.assertEquals(output, expected_output)
+
+
+if __name__ == '__main__':
+  unittest.main()
diff --git a/linker/dlfcn.cpp b/linker/dlfcn.cpp
index 15e1922..5ccd656 100644
--- a/linker/dlfcn.cpp
+++ b/linker/dlfcn.cpp
@@ -40,7 +40,6 @@
 #include <bionic/pthread_internal.h>
 #include "private/bionic_tls.h"
 #include "private/ScopedPthreadMutexLocker.h"
-#include "private/ThreadLocalBuffer.h"
 
 static pthread_mutex_t g_dl_mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
 
diff --git a/linker/linker_logger.cpp b/linker/linker_logger.cpp
index 08728af..b2ea320 100644
--- a/linker/linker_logger.cpp
+++ b/linker/linker_logger.cpp
@@ -85,13 +85,20 @@
   }
 
   flags_ = 0;
-  // check flag applied to all processes first
+
+  // Check flag applied to all processes first.
   std::string value = property_get(kSystemLdDebugProperty);
   flags_ |= ParseProperty(value);
 
-  // get process basename
+  // Ignore processes started without argv (http://b/33276926).
+  if (g_argv[0] == nullptr) {
+    return;
+  }
+
+  // Get process basename.
   const char* process_name_start = basename(g_argv[0]);
-  // remove ':' and everything after it. This is naming convention for
+
+  // Remove ':' and everything after it. This is the naming convention for
   // services: https://developer.android.com/guide/components/services.html
   const char* process_name_end = strchr(process_name_start, ':');
 
diff --git a/tests/dlext_test.cpp b/tests/dlext_test.cpp
index dcabae8..17e1a48 100644
--- a/tests/dlext_test.cpp
+++ b/tests/dlext_test.cpp
@@ -898,6 +898,37 @@
             "\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
 }
 
+TEST(dlext, ns_greylist) {
+  ASSERT_TRUE(android_init_anonymous_namespace(g_core_shared_libs.c_str(), nullptr));
+
+  const std::string ns_search_path = get_testlib_root() + "/private_namespace_libs";
+
+  android_namespace_t* ns =
+          android_create_namespace("namespace",
+                                   nullptr,
+                                   ns_search_path.c_str(),
+                                   ANDROID_NAMESPACE_TYPE_ISOLATED,
+                                   nullptr,
+                                   nullptr);
+
+  ASSERT_TRUE(android_link_namespaces(ns, nullptr, g_core_shared_libs.c_str())) << dlerror();
+
+  android_dlextinfo extinfo;
+  extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
+  extinfo.library_namespace = ns;
+
+  android_set_application_target_sdk_version(__ANDROID_API_M__);
+  void* handle = android_dlopen_ext("libnativehelper.so", RTLD_NOW, &extinfo);
+  ASSERT_TRUE(handle != nullptr) << dlerror();
+
+  dlclose(handle);
+
+  android_set_application_target_sdk_version(__ANDROID_API_N__);
+  handle = android_dlopen_ext("libnativehelper.so", RTLD_NOW, &extinfo);
+  ASSERT_TRUE(handle == nullptr);
+  ASSERT_STREQ("dlopen failed: library \"libnativehelper.so\" not found", dlerror());
+}
+
 TEST(dlext, ns_cyclic_namespaces) {
   // Test that ns1->ns2->ns1 link does not break the loader
   ASSERT_TRUE(android_init_anonymous_namespace(g_core_shared_libs.c_str(), nullptr));
diff --git a/tests/sys_ptrace_test.cpp b/tests/sys_ptrace_test.cpp
index 8fe7a29..bce5898 100644
--- a/tests/sys_ptrace_test.cpp
+++ b/tests/sys_ptrace_test.cpp
@@ -28,6 +28,7 @@
 
 #include <gtest/gtest.h>
 
+#include <android-base/macros.h>
 #include <android-base/unique_fd.h>
 
 using android::base::unique_fd;
@@ -37,33 +38,6 @@
 #define TRAP_HWBKPT 4
 #endif
 
-template <typename T>
-static void __attribute__((noreturn)) watchpoint_fork_child(unsigned cpu, T& data) {
-  // Extra precaution: make sure we go away if anything happens to our parent.
-  if (prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0) == -1) {
-    perror("prctl(PR_SET_PDEATHSIG)");
-    _exit(1);
-  }
-
-  cpu_set_t cpus;
-  CPU_ZERO(&cpus);
-  CPU_SET(cpu, &cpus);
-  if (sched_setaffinity(0, sizeof cpus, &cpus) == -1) {
-    perror("sched_setaffinity");
-    _exit(2);
-  }
-  if (ptrace(PTRACE_TRACEME, 0, nullptr, nullptr) == -1) {
-    perror("ptrace(PTRACE_TRACEME)");
-    _exit(3);
-  }
-
-  raise(SIGSTOP); // Synchronize with the tracer, let it set the watchpoint.
-
-  data = 1; // Now trigger the watchpoint.
-
-  _exit(0);
-}
-
 class ChildGuard {
  public:
   explicit ChildGuard(pid_t pid) : pid(pid) {}
@@ -109,18 +83,19 @@
   return (dreg_state.dbg_info & 0xff) > 0;
 #elif defined(__i386__) || defined(__x86_64__)
   // We assume watchpoints and breakpoints are always supported on x86.
-  (void) child;
-  (void)feature;
+  UNUSED(child);
+  UNUSED(feature);
   return true;
 #else
   // TODO: mips support.
-  (void) child;
-  (void)feature;
+  UNUSED(child);
+  UNUSED(feature);
   return false;
 #endif
 }
 
-static void set_watchpoint(pid_t child, const void *address, size_t size) {
+static void set_watchpoint(pid_t child, uintptr_t address, size_t size) {
+  ASSERT_EQ(0u, address & 0x7) << "address: " << address;
 #if defined(__arm__) || defined(__aarch64__)
   const unsigned byte_mask = (1 << size) - 1;
   const unsigned type = 2; // Write.
@@ -133,7 +108,7 @@
 #else // aarch64
   user_hwdebug_state dreg_state;
   memset(&dreg_state, 0, sizeof dreg_state);
-  dreg_state.dbg_regs[0].addr = reinterpret_cast<uintptr_t>(address);
+  dreg_state.dbg_regs[0].addr = address;
   dreg_state.dbg_regs[0].ctrl = control;
 
   iovec iov;
@@ -158,19 +133,33 @@
   data |= value;
   ASSERT_EQ(0, ptrace(PTRACE_POKEUSER, child, offsetof(user, u_debugreg[7]), data)) << strerror(errno);
 #else
-  (void) child;
-  (void) address;
-  (void) size;
+  UNUSED(child);
+  UNUSED(address);
+  UNUSED(size);
 #endif
 }
 
-template<typename T>
-static void run_watchpoint_test_impl(unsigned cpu) {
-  alignas(8) T data = 0;
+template <typename T>
+static void run_watchpoint_test(std::function<void(T&)> child_func, size_t offset, size_t size) {
+  alignas(16) T data{};
 
   pid_t child = fork();
   ASSERT_NE(-1, child) << strerror(errno);
-  if (child == 0) watchpoint_fork_child(cpu, data);
+  if (child == 0) {
+    // Extra precaution: make sure we go away if anything happens to our parent.
+    if (prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0) == -1) {
+      perror("prctl(PR_SET_PDEATHSIG)");
+      _exit(1);
+    }
+
+    if (ptrace(PTRACE_TRACEME, 0, nullptr, nullptr) == -1) {
+      perror("ptrace(PTRACE_TRACEME)");
+      _exit(2);
+    }
+
+    child_func(data);
+    _exit(0);
+  }
 
   ChildGuard guard(child);
 
@@ -184,7 +173,7 @@
     return;
   }
 
-  set_watchpoint(child, &data, sizeof data);
+  set_watchpoint(child, uintptr_t(&data) + offset, size);
 
   ASSERT_EQ(0, ptrace(PTRACE_CONT, child, nullptr, nullptr)) << strerror(errno);
   ASSERT_EQ(child, waitpid(child, &status, __WALL)) << strerror(errno);
@@ -195,17 +184,29 @@
   ASSERT_EQ(0, ptrace(PTRACE_GETSIGINFO, child, nullptr, &siginfo)) << strerror(errno);
   ASSERT_EQ(TRAP_HWBKPT, siginfo.si_code);
 #if defined(__arm__) || defined(__aarch64__)
-  ASSERT_EQ(&data, siginfo.si_addr);
+  ASSERT_LE(&data, siginfo.si_addr);
+  ASSERT_GT((&data) + 1, siginfo.si_addr);
 #endif
 }
 
-static void run_watchpoint_test(unsigned cpu) {
-  run_watchpoint_test_impl<uint8_t>(cpu);
-  run_watchpoint_test_impl<uint16_t>(cpu);
-  run_watchpoint_test_impl<uint32_t>(cpu);
-#if defined(__LP64__)
-  run_watchpoint_test_impl<uint64_t>(cpu);
-#endif
+template <typename T>
+static void watchpoint_stress_child(unsigned cpu, T& data) {
+  cpu_set_t cpus;
+  CPU_ZERO(&cpus);
+  CPU_SET(cpu, &cpus);
+  if (sched_setaffinity(0, sizeof cpus, &cpus) == -1) {
+    perror("sched_setaffinity");
+    _exit(3);
+  }
+  raise(SIGSTOP);  // Synchronize with the tracer, let it set the watchpoint.
+
+  data = 1;  // Now trigger the watchpoint.
+}
+
+template <typename T>
+static void run_watchpoint_stress(size_t cpu) {
+  run_watchpoint_test<T>(std::bind(watchpoint_stress_child<T>, cpu, std::placeholders::_1), 0,
+                         sizeof(T));
 }
 
 // Test watchpoint API. The test is considered successful if our watchpoints get hit OR the
@@ -217,10 +218,53 @@
 
   for (size_t cpu = 0; cpu < CPU_SETSIZE; ++cpu) {
     if (!CPU_ISSET(cpu, &available_cpus)) continue;
-    run_watchpoint_test(cpu);
+
+    run_watchpoint_stress<uint8_t>(cpu);
+    run_watchpoint_stress<uint16_t>(cpu);
+    run_watchpoint_stress<uint32_t>(cpu);
+#if defined(__LP64__)
+    run_watchpoint_stress<uint64_t>(cpu);
+#endif
   }
 }
 
+struct Uint128_t {
+  uint64_t data[2];
+};
+static void watchpoint_imprecise_child(Uint128_t& data) {
+  raise(SIGSTOP);  // Synchronize with the tracer, let it set the watchpoint.
+
+#if defined(__i386__) || defined(__x86_64__)
+  asm volatile("movdqa %%xmm0, %0" : : "m"(data));
+#elif defined(__arm__)
+  asm volatile("stm %0, { r0, r1, r2, r3 }" : : "r"(&data));
+#elif defined(__aarch64__)
+  asm volatile("stp x0, x1, %0" : : "m"(data));
+#elif defined(__mips__)
+// TODO
+  UNUSED(data);
+#endif
+}
+
+// Test that the kernel is able to handle the case when the instruction writes
+// to a larger block of memory than the one we are watching. If you see this
+// test fail on arm64, you will likely need to cherry-pick fdfeff0f into your
+// kernel.
+TEST(sys_ptrace, watchpoint_imprecise) {
+  // Make sure we get interrupted in case a buggy kernel does not report the
+  // watchpoint hit correctly.
+  struct sigaction action, oldaction;
+  action.sa_handler = [](int) {};
+  sigemptyset(&action.sa_mask);
+  action.sa_flags = 0;
+  ASSERT_EQ(0, sigaction(SIGALRM, &action, &oldaction)) << strerror(errno);
+  alarm(5);
+
+  run_watchpoint_test<Uint128_t>(watchpoint_imprecise_child, 8, 8);
+
+  ASSERT_EQ(0, sigaction(SIGALRM, &oldaction, nullptr)) << strerror(errno);
+}
+
 static void __attribute__((noinline)) breakpoint_func() {
   asm volatile("");
 }
@@ -285,8 +329,8 @@
   ASSERT_EQ(0, ptrace(PTRACE_POKEUSER, child, offsetof(user, u_debugreg[7]), data))
       << strerror(errno);
 #else
-  (void)child;
-  (void)address;
+  UNUSED(child);
+  UNUSED(address);
 #endif
 }
 
diff --git a/tests/unistd_test.cpp b/tests/unistd_test.cpp
index ddf0df5..d90b01e 100644
--- a/tests/unistd_test.cpp
+++ b/tests/unistd_test.cpp
@@ -1364,3 +1364,11 @@
   ASSERT_EQ(-1, execvp("/system/bin/does-not-exist", eth.GetArgs()));
   ASSERT_EQ(ENOENT, errno);
 }
+
+TEST(UNISTD_TEST, exec_argv0_null) {
+  // http://b/33276926
+  char* args[] = {nullptr};
+  char* envs[] = {nullptr};
+  ASSERT_EXIT(execve("/system/bin/run-as", args, envs), testing::ExitedWithCode(1),
+              "<unknown>: usage: run-as");
+}
diff --git a/tools/versioner/src/VFS.cpp b/tools/versioner/src/VFS.cpp
index 1aa7229..cdf232f 100644
--- a/tools/versioner/src/VFS.cpp
+++ b/tools/versioner/src/VFS.cpp
@@ -59,7 +59,7 @@
       errx(1, "failed to map header '%s'", file_path);
     }
 
-    if (!vfs->addFile(file_path, ent->fts_statp->st_mtim.tv_sec, std::move(buffer_opt.get()))) {
+    if (!vfs->addFile(file_path, ent->fts_statp->st_mtime, std::move(buffer_opt.get()))) {
       errx(1, "failed to add file '%s'", file_path);
     }
   }