Merge "Remove the HASLB and FREEUB macros."
diff --git a/README.md b/README.md
index ee83b08..cb9c454 100644
--- a/README.md
+++ b/README.md
@@ -171,6 +171,10 @@
      contents for external/kernel-headers/.
   2. Run update_all.py to scrub those headers and import them into bionic.
 
+Note that if you're actually just trying to expose device-specific headers to
+build your device drivers, you shouldn't modify bionic. Instead use
+`TARGET_DEVICE_KERNEL_HEADERS` and friends described in [config.mk](https://android.googlesource.com/platform/build/+/master/core/config.mk#186).
+
 
 Updating tzdata
 ---------------
diff --git a/libc/Android.bp b/libc/Android.bp
index 7b0ac23..842dae4 100644
--- a/libc/Android.bp
+++ b/libc/Android.bp
@@ -13,6 +13,7 @@
     "bionic/sigblock.c",
     "bionic/siginterrupt.c",
     "bionic/sigsetmask.c",
+    "stdio/fmemopen.cpp",
     "stdio/fread.c",
     "stdio/parsefloat.c",
     "stdio/refill.c",
@@ -237,9 +238,11 @@
         "upstream-freebsd/lib/libc/string/wcspbrk.c",
         "upstream-freebsd/lib/libc/string/wcsrchr.c",
         "upstream-freebsd/lib/libc/string/wcsspn.c",
+        "upstream-freebsd/lib/libc/string/wcsstr.c",
         "upstream-freebsd/lib/libc/string/wcstok.c",
         "upstream-freebsd/lib/libc/string/wmemchr.c",
         "upstream-freebsd/lib/libc/string/wmemcmp.c",
+        "upstream-freebsd/lib/libc/string/wmemcpy.c",
         "upstream-freebsd/lib/libc/string/wmemmove.c",
         "upstream-freebsd/lib/libc/string/wmemset.c",
     ],
@@ -385,7 +388,6 @@
     name: "libc_openbsd_ndk",
     defaults: ["libc_defaults"],
     srcs: [
-        "upstream-openbsd/lib/libc/compat-43/killpg.c",
         "upstream-openbsd/lib/libc/gen/alarm.c",
         "upstream-openbsd/lib/libc/gen/ctype_.c",
         "upstream-openbsd/lib/libc/gen/daemon.c",
@@ -438,7 +440,6 @@
         "upstream-openbsd/lib/libc/stdio/fgetwc.c",
         "upstream-openbsd/lib/libc/stdio/fgetws.c",
         "upstream-openbsd/lib/libc/stdio/flags.c",
-        "upstream-openbsd/lib/libc/stdio/fmemopen.c",
         "upstream-openbsd/lib/libc/stdio/fpurge.c",
         "upstream-openbsd/lib/libc/stdio/fputs.c",
         "upstream-openbsd/lib/libc/stdio/fputwc.c",
@@ -501,9 +502,7 @@
         "upstream-openbsd/lib/libc/string/strspn.c",
         "upstream-openbsd/lib/libc/string/strstr.c",
         "upstream-openbsd/lib/libc/string/strtok.c",
-        "upstream-openbsd/lib/libc/string/wmemcpy.c",
         "upstream-openbsd/lib/libc/string/wcslcpy.c",
-        "upstream-openbsd/lib/libc/string/wcsstr.c",
         "upstream-openbsd/lib/libc/string/wcswidth.c",
     ],
 
@@ -1396,6 +1395,7 @@
         "bionic/ifaddrs.cpp",
         "bionic/inotify_init.cpp",
         "bionic/ioctl.cpp",
+        "bionic/killpg.cpp",
         "bionic/langinfo.cpp",
         "bionic/lchown.cpp",
         "bionic/lfs64_support.cpp",
diff --git a/libc/NOTICE b/libc/NOTICE
index ae98d1d..90de7ef 100644
--- a/libc/NOTICE
+++ b/libc/NOTICE
@@ -682,6 +682,31 @@
 
 -------------------------------------------------------------------
 
+Copyright (C) 2013 Pietro Cerutti <gahr@FreeBSD.org>
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+1. Redistributions of source code must retain the above copyright
+   notice, this list of conditions and the following disclaimer.
+2. 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 AUTHOR 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 AUTHOR 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.
+
+-------------------------------------------------------------------
+
 Copyright (C) 2013 The Android Open Source Project
 
 Licensed under the Apache License, Version 2.0 (the "License");
@@ -5071,23 +5096,6 @@
 
 -------------------------------------------------------------------
 
-Copyright (c) 2011 Martin Pieuchot <mpi@openbsd.org>
-Copyright (c) 2009 Ted Unangst
-
-Permission to use, copy, modify, and distribute this software for any
-purpose with or without fee is hereby granted, provided that the above
-copyright notice and this permission notice appear in all copies.
-
-THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
-WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
-MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
-ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
-WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
-ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
-OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-
--------------------------------------------------------------------
-
 Copyright (c) 2011 The Android Open Source Project
 Copyright (c) 2008 ARM Ltd
 All rights reserved.
diff --git a/libc/bionic/getcwd.cpp b/libc/bionic/getcwd.cpp
index c2a7e23..50487ec 100644
--- a/libc/bionic/getcwd.cpp
+++ b/libc/bionic/getcwd.cpp
@@ -26,7 +26,6 @@
  * SUCH DAMAGE.
  */
 
-#undef _FORTIFY_SOURCE
 #include <errno.h>
 #include <malloc.h>
 #include <string.h>
diff --git a/libc/bionic/killpg.cpp b/libc/bionic/killpg.cpp
new file mode 100644
index 0000000..01a58b3
--- /dev/null
+++ b/libc/bionic/killpg.cpp
@@ -0,0 +1,38 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *  * Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *  * Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <errno.h>
+#include <signal.h>
+
+int killpg(pid_t pgrp, int sig) {
+  if (pgrp < 0) {
+    errno = EINVAL;
+    return -1;
+  }
+  return kill(-pgrp, sig);
+}
diff --git a/libc/bionic/poll.cpp b/libc/bionic/poll.cpp
index eded56a..dbc9584 100644
--- a/libc/bionic/poll.cpp
+++ b/libc/bionic/poll.cpp
@@ -26,7 +26,6 @@
  * SUCH DAMAGE.
  */
 
-#undef _FORTIFY_SOURCE
 #include <errno.h>
 #include <sys/poll.h>
 #include <sys/select.h>
diff --git a/libc/bionic/readlink.cpp b/libc/bionic/readlink.cpp
index a53f933..3bb7bc1 100644
--- a/libc/bionic/readlink.cpp
+++ b/libc/bionic/readlink.cpp
@@ -26,8 +26,6 @@
  * SUCH DAMAGE.
  */
 
-#undef _FORTIFY_SOURCE
-
 #include <fcntl.h>
 #include <sys/stat.h>
 #include <sys/types.h>
diff --git a/libc/bionic/recv.cpp b/libc/bionic/recv.cpp
index 061cd46..60f264d 100644
--- a/libc/bionic/recv.cpp
+++ b/libc/bionic/recv.cpp
@@ -26,7 +26,6 @@
  * SUCH DAMAGE.
  */
 
-#undef _FORTIFY_SOURCE
 #include <sys/socket.h>
 
 ssize_t recv(int socket, void *buf, size_t len, int flags) {
diff --git a/libc/bionic/strchr.cpp b/libc/bionic/strchr.cpp
index 5bd3f19..fd8a924 100644
--- a/libc/bionic/strchr.cpp
+++ b/libc/bionic/strchr.cpp
@@ -27,7 +27,6 @@
  * SUCH DAMAGE.
  */
 
-#undef _FORTIFY_SOURCE
 #include <string.h>
 
 char* strchr(const char* p, int ch) {
diff --git a/libc/bionic/strrchr.cpp b/libc/bionic/strrchr.cpp
index 3fdb47c..b6c40f4 100644
--- a/libc/bionic/strrchr.cpp
+++ b/libc/bionic/strrchr.cpp
@@ -27,7 +27,6 @@
  * SUCH DAMAGE.
  */
 
-#undef _FORTIFY_SOURCE
 #include <string.h>
 
 char* strrchr(const char* p, int ch) {
diff --git a/libc/include/bits/lockf.h b/libc/include/bits/lockf.h
index c24f18b..a8e273e 100644
--- a/libc/include/bits/lockf.h
+++ b/libc/include/bits/lockf.h
@@ -39,11 +39,7 @@
 
 __BEGIN_DECLS
 
-#if defined(__USE_FILE_OFFSET64)
-int lockf(int, int, off_t) __RENAME(lockf64) __INTRODUCED_IN(24);
-#else
-int lockf(int, int, off_t) __INTRODUCED_IN(24);
-#endif
+int lockf(int, int, off_t) __RENAME_IF_FILE_OFFSET64(lockf64) __INTRODUCED_IN(24);
 int lockf64(int, int, off64_t) __INTRODUCED_IN(24);
 
 __END_DECLS
diff --git a/libc/include/fcntl.h b/libc/include/fcntl.h
index c98022c..f27efdf 100644
--- a/libc/include/fcntl.h
+++ b/libc/include/fcntl.h
@@ -71,8 +71,7 @@
 
 int creat(const char*, mode_t);
 int creat64(const char*, mode_t) __INTRODUCED_IN(21);
-int openat(int, const char*, int, ...) __overloadable
-        __RENAME_CLANG(openat);
+int openat(int, const char*, int, ...) __overloadable __RENAME_CLANG(openat);
 int openat64(int, const char*, int, ...) __INTRODUCED_IN(21);
 int open(const char*, int, ...) __overloadable __RENAME_CLANG(open);
 int open64(const char*, int, ...) __INTRODUCED_IN(21);
@@ -80,17 +79,11 @@
 ssize_t tee(int, int, size_t, unsigned int) __INTRODUCED_IN(21);
 ssize_t vmsplice(int, const struct iovec*, size_t, unsigned int) __INTRODUCED_IN(21);
 
-#if defined(__USE_FILE_OFFSET64)
-int fallocate(int, int, off_t, off_t) __RENAME(fallocate64) __INTRODUCED_IN(21);
-int posix_fadvise(int, off_t, off_t, int) __RENAME(posix_fadvise64) __INTRODUCED_IN(21);
-int posix_fallocate(int, off_t, off_t) __RENAME(posix_fallocate64) __INTRODUCED_IN(21);
-#else
-int fallocate(int, int, off_t, off_t) __INTRODUCED_IN(21);
-int posix_fadvise(int, off_t, off_t, int) __INTRODUCED_IN(21);
-int posix_fallocate(int, off_t, off_t) __INTRODUCED_IN(21);
-#endif
+int fallocate(int, int, off_t, off_t) __RENAME_IF_FILE_OFFSET64(fallocate64) __INTRODUCED_IN(21);
 int fallocate64(int, int, off64_t, off64_t) __INTRODUCED_IN(21);
+int posix_fadvise(int, off_t, off_t, int) __RENAME_IF_FILE_OFFSET64(posix_fadvise64) __INTRODUCED_IN(21);
 int posix_fadvise64(int, off64_t, off64_t, int) __INTRODUCED_IN(21);
+int posix_fallocate(int, off_t, off_t) __RENAME_IF_FILE_OFFSET64(posix_fallocate64) __INTRODUCED_IN(21);
 int posix_fallocate64(int, off64_t, off64_t) __INTRODUCED_IN(21);
 
 #if defined(__USE_GNU)
diff --git a/libc/include/sys/cdefs.h b/libc/include/sys/cdefs.h
index 307545c..dd5b345 100644
--- a/libc/include/sys/cdefs.h
+++ b/libc/include/sys/cdefs.h
@@ -237,6 +237,11 @@
 /* _FILE_OFFSET_BITS 64 support. */
 #if !defined(__LP64__) && defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64
 #define __USE_FILE_OFFSET64 1
+/*
+ * Note that __RENAME_IF_FILE_OFFSET64 is only valid if the off_t and off64_t
+ * functions were both added at the same API level because if you use this,
+ * you only have one declaration to attach __INTRODUCED_IN to.
+ */
 #define __RENAME_IF_FILE_OFFSET64(func) __RENAME(func)
 #else
 #define __RENAME_IF_FILE_OFFSET64(func)
diff --git a/libc/include/sys/uio.h b/libc/include/sys/uio.h
index 2611774..b166277 100644
--- a/libc/include/sys/uio.h
+++ b/libc/include/sys/uio.h
@@ -38,13 +38,8 @@
 ssize_t writev(int, const struct iovec*, int);
 
 #if defined(__USE_GNU)
-#if defined(__USE_FILE_OFFSET64)
-ssize_t preadv(int, const struct iovec*, int, off_t) __RENAME(preadv64) __INTRODUCED_IN(24);
-ssize_t pwritev(int, const struct iovec*, int, off_t) __RENAME(pwritev64) __INTRODUCED_IN(24);
-#else
-ssize_t preadv(int, const struct iovec*, int, off_t) __INTRODUCED_IN(24);
-ssize_t pwritev(int, const struct iovec*, int, off_t) __INTRODUCED_IN(24);
-#endif
+ssize_t preadv(int, const struct iovec*, int, off_t) __RENAME_IF_FILE_OFFSET64(preadv64) __INTRODUCED_IN(24);
+ssize_t pwritev(int, const struct iovec*, int, off_t) __RENAME_IF_FILE_OFFSET64(pwritev64) __INTRODUCED_IN(24);
 ssize_t preadv64(int, const struct iovec*, int, off64_t) __INTRODUCED_IN(24);
 ssize_t pwritev64(int, const struct iovec*, int, off64_t) __INTRODUCED_IN(24);
 #endif
diff --git a/libc/stdio/fmemopen.cpp b/libc/stdio/fmemopen.cpp
new file mode 100644
index 0000000..9d8c41f
--- /dev/null
+++ b/libc/stdio/fmemopen.cpp
@@ -0,0 +1,156 @@
+/*-
+ * Copyright (C) 2013 Pietro Cerutti <gahr@FreeBSD.org>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. 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 AUTHOR 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 AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "local.h"
+
+// See http://pubs.opengroup.org/onlinepubs/9699919799/functions/fmemopen.html
+// and http://man7.org/linux/man-pages/man3/fmemopen.3.html for documentation.
+
+struct fmemopen_cookie {
+  char* buf;
+  char* allocation;
+  size_t capacity;
+  size_t size;
+  size_t offset;
+  bool append;
+};
+
+static int fmemopen_read(void* cookie, char* buf, int n) {
+  fmemopen_cookie* ck = static_cast<fmemopen_cookie*>(cookie);
+
+  if (static_cast<size_t>(n) > ck->size - ck->offset) n = ck->size - ck->offset;
+
+  if (n > 0) {
+    memmove(buf, ck->buf + ck->offset, n);
+    ck->offset += n;
+  }
+  return n;
+}
+
+static int fmemopen_write(void* cookie, const char* buf, int n) {
+  fmemopen_cookie* ck = static_cast<fmemopen_cookie*>(cookie);
+
+  // We don't need to add the trailing NUL if there's already a trailing NUL
+  // in the data we're writing.
+  size_t space_for_null = (n > 0 && buf[n - 1] != '\0') ? 1 : 0;
+
+  // Undo any seeking/reading on an append-only stream.
+  if (ck->append) ck->offset = ck->size;
+
+  // How much can we actually fit?
+  if (static_cast<size_t>(n) + space_for_null > ck->capacity - ck->offset) {
+    n = ck->capacity - ck->offset - space_for_null;
+    // Give up if we don't even have room for one byte of userdata.
+    if (n <= 0) {
+      errno = ENOSPC;
+      return -1;
+    }
+  }
+
+  if (n > 0) {
+    memmove(ck->buf + ck->offset, buf, n);
+    ck->offset += n;
+    // Is this the furthest we've ever been?
+    if (ck->offset >= ck->size) {
+      if (buf[n - 1] != '\0') ck->buf[ck->offset] = '\0';
+      ck->size = ck->offset;
+    }
+  }
+  return n;
+}
+
+static fpos_t fmemopen_seek(void* cookie, fpos_t offset, int whence) {
+  fmemopen_cookie* ck = static_cast<fmemopen_cookie*>(cookie);
+
+  if (whence == SEEK_SET && (offset >= 0 && static_cast<size_t>(offset) <= ck->capacity)) {
+    return (ck->offset = offset);
+  } else if (whence == SEEK_CUR && (ck->offset + offset <= ck->capacity)) {
+    return (ck->offset += offset);
+  } else if (whence == SEEK_END && (offset <= 0 && static_cast<size_t>(-offset) <= ck->size)) {
+    return (ck->offset = ck->size + offset);
+  }
+  errno = EINVAL;
+  return -1;
+}
+
+static int fmemopen_close(void* cookie) {
+  fmemopen_cookie* ck = static_cast<fmemopen_cookie*>(cookie);
+  free(ck->allocation);
+  free(ck);
+  return 0;
+}
+
+FILE* fmemopen(void* buf, size_t capacity, const char* mode) {
+  int flags;
+  if (__sflags(mode, &flags) == 0) {
+    errno = EINVAL;
+    return nullptr;
+  }
+
+  fmemopen_cookie* ck = static_cast<fmemopen_cookie*>(calloc(sizeof(fmemopen_cookie), 1));
+  if (ck == nullptr) return nullptr;
+
+  ck->buf = static_cast<char*>(buf);
+  ck->capacity = capacity;
+
+  if (ck->buf == nullptr) ck->buf = ck->allocation = static_cast<char*>(calloc(capacity, 1));
+  if (ck->buf == nullptr) {
+    free(ck);
+    return nullptr;
+  }
+
+  FILE* fp = funopen(ck,
+                     (flags & O_WRONLY) ? nullptr : fmemopen_read,
+                     (flags & O_RDONLY) ? nullptr : fmemopen_write,
+                     fmemopen_seek,
+                     fmemopen_close);
+  if (fp == nullptr) {
+    fmemopen_close(ck);
+    return nullptr;
+  }
+
+  if (mode[0] == 'a') {
+    ck->size = strnlen(ck->buf, ck->capacity);
+    ck->offset = ck->size;
+    ck->append = true;
+  } else if (mode[0] == 'r') {
+    ck->size = capacity;
+    ck->offset = 0;
+  } else if (mode[0] == 'w') {
+    ck->size = 0;
+    ck->offset = 0;
+    ck->buf[0] = '\0';
+  }
+
+  return fp;
+}
diff --git a/libc/upstream-openbsd/lib/libc/compat-43/killpg.c b/libc/upstream-freebsd/lib/libc/string/wcsstr.c
similarity index 65%
rename from libc/upstream-openbsd/lib/libc/compat-43/killpg.c
rename to libc/upstream-freebsd/lib/libc/string/wcsstr.c
index 75b1ad9..ce598a6 100644
--- a/libc/upstream-openbsd/lib/libc/compat-43/killpg.c
+++ b/libc/upstream-freebsd/lib/libc/string/wcsstr.c
@@ -1,6 +1,9 @@
-/*
- * Copyright (c) 1989 The Regents of the University of California.
- * All rights reserved.
+/*-
+ * Copyright (c) 1990, 1993
+ *	The Regents of the University of California.  All rights reserved.
+ *
+ * This code is derived from software contributed to Berkeley by
+ * Chris Torek.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -27,19 +30,34 @@
  * SUCH DAMAGE.
  */
 
-#include <sys/types.h>
-#include <signal.h>
-#include <errno.h>
+#if 0
+#if defined(LIBC_SCCS) && !defined(lint)
+static char sccsid[] = "@(#)strstr.c	8.1 (Berkeley) 6/4/93";
+#endif /* LIBC_SCCS and not lint */
+#endif
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <wchar.h>
 
 /*
- * Backwards-compatible killpg().
+ * Find the first occurrence of find in s.
  */
-int
-killpg(pid_t pgid, int sig)
+wchar_t *
+wcsstr(const wchar_t * __restrict s, const wchar_t * __restrict find)
 {
-	if (pgid == 1) {
-		errno = ESRCH;
-		return (-1);
+	wchar_t c, sc;
+	size_t len;
+
+	if ((c = *find++) != L'\0') {
+		len = wcslen(find);
+		do {
+			do {
+				if ((sc = *s++) == L'\0')
+					return (NULL);
+			} while (sc != c);
+		} while (wcsncmp(s, find, len) != 0);
+		s--;
 	}
-	return (kill(-pgid, sig));
+	return ((wchar_t *)s);
 }
diff --git a/libc/upstream-openbsd/lib/libc/string/wmemcpy.c b/libc/upstream-freebsd/lib/libc/string/wmemcpy.c
similarity index 84%
rename from libc/upstream-openbsd/lib/libc/string/wmemcpy.c
rename to libc/upstream-freebsd/lib/libc/string/wmemcpy.c
index cf02ab9..c10770c 100644
--- a/libc/upstream-openbsd/lib/libc/string/wmemcpy.c
+++ b/libc/upstream-freebsd/lib/libc/string/wmemcpy.c
@@ -1,6 +1,3 @@
-/*	$OpenBSD: wmemcpy.c,v 1.4 2015/09/12 16:23:14 guenther Exp $	*/
-/*	$NetBSD: wmemcpy.c,v 1.2 2001/01/03 14:29:37 lukem Exp $	*/
-
 /*-
  * Copyright (c)1999 Citrus Project,
  * All rights reserved.
@@ -29,13 +26,19 @@
  *	citrus Id: wmemcpy.c,v 1.2 2000/12/20 14:08:31 itojun Exp
  */
 
+#include <sys/cdefs.h>
+#if 0
+#if defined(LIBC_SCCS) && !defined(lint)
+__RCSID("$NetBSD: wmemcpy.c,v 1.1 2000/12/23 23:14:37 itojun Exp $");
+#endif /* LIBC_SCCS and not lint */
+#endif
+__FBSDID("$FreeBSD$");
+
 #include <string.h>
 #include <wchar.h>
 
 wchar_t *
-wmemcpy(wchar_t *d, const wchar_t *s, size_t n)
+wmemcpy(wchar_t * __restrict d, const wchar_t * __restrict s, size_t n)
 {
-
 	return (wchar_t *)memcpy(d, s, n * sizeof(wchar_t));
 }
-DEF_STRONG(wmemcpy);
diff --git a/libc/upstream-openbsd/android/include/openbsd-compat.h b/libc/upstream-openbsd/android/include/openbsd-compat.h
index 9b16da7..baf5057 100644
--- a/libc/upstream-openbsd/android/include/openbsd-compat.h
+++ b/libc/upstream-openbsd/android/include/openbsd-compat.h
@@ -40,6 +40,7 @@
 /* Ignore all DEF_STRONG/DEF_WEAK in OpenBSD. */
 #define DEF_STRONG(sym)
 #define DEF_WEAK(sym)
+#define __weak_alias __strong_alias
 
 /* Ignore all __warn_references in OpenBSD. */
 #define __warn_references(sym,msg)
diff --git a/libc/upstream-openbsd/lib/libc/stdio/fmemopen.c b/libc/upstream-openbsd/lib/libc/stdio/fmemopen.c
deleted file mode 100644
index 00c2764..0000000
--- a/libc/upstream-openbsd/lib/libc/stdio/fmemopen.c
+++ /dev/null
@@ -1,184 +0,0 @@
-/*	$OpenBSD: fmemopen.c,v 1.3 2015/08/31 02:53:57 guenther Exp $	*/
-
-/*
- * Copyright (c) 2011 Martin Pieuchot <mpi@openbsd.org>
- * Copyright (c) 2009 Ted Unangst
- *
- * Permission to use, copy, modify, and distribute this software for any
- * purpose with or without fee is hereby granted, provided that the above
- * copyright notice and this permission notice appear in all copies.
- *
- * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
- * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
- * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
- * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
- * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
- * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
- * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
- */
-
-#include <errno.h>
-#include <fcntl.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include "local.h"
-
-struct state {
-	char		*string;	/* actual stream */
-	size_t		 pos;		/* current position */
-	size_t		 size;		/* allocated size */
-	size_t		 len;		/* length of the data */
-	int		 update;	/* open for update */
-};
-
-static int
-fmemopen_read(void *v, char *b, int l)
-{
-	struct state	*st = v;
-	int		 i;
-
-	for (i = 0; i < l && i + st->pos < st->len; i++)
-		b[i] = st->string[st->pos + i];
-	st->pos += i;
-
-	return (i);
-}
-
-static int
-fmemopen_write(void *v, const char *b, int l)
-{
-	struct state	*st = v;
-	int		i;
-
-	for (i = 0; i < l && i + st->pos < st->size; i++)
-		st->string[st->pos + i] = b[i];
-	st->pos += i;
-
-	if (st->pos >= st->len) {
-		st->len = st->pos;
-
-		if (st->len < st->size)
-			st->string[st->len] = '\0';
-		else if (!st->update)
-			st->string[st->size - 1] = '\0';
-	}
-
-	return (i);
-}
-
-static fpos_t
-fmemopen_seek(void *v, fpos_t off, int whence)
-{
-	struct state	*st = v;
-	ssize_t		 base = 0;
-
-	switch (whence) {
-	case SEEK_SET:
-		break;
-	case SEEK_CUR:
-		base = st->pos;
-		break;
-	case SEEK_END:
-		base = st->len;
-		break;
-	}
-
-	if (off > st->size - base || off < -base) {
-		errno = EOVERFLOW;
-		return (-1);
-	}
-
-	st->pos = base + off;
-
-	return (st->pos);
-}
-
-static int
-fmemopen_close(void *v)
-{
-	free(v);
-
-	return (0);
-}
-
-static int
-fmemopen_close_free(void *v)
-{
-	struct state	*st = v;
-
-	free(st->string);
-	free(st);
-
-	return (0);
-}
-
-FILE *
-fmemopen(void *buf, size_t size, const char *mode)
-{
-	struct state	*st;
-	FILE		*fp;
-	int		 flags, oflags;
-
-	if (size == 0) {
-		errno = EINVAL;
-		return (NULL);
-	}
-
-	if ((flags = __sflags(mode, &oflags)) == 0) {
-		errno = EINVAL;
-		return (NULL);
-	}
-
-	if (buf == NULL && ((oflags & O_RDWR) == 0)) {
-		errno = EINVAL;
-		return (NULL);
-	}
-
-	if ((st = malloc(sizeof(*st))) == NULL)
-		return (NULL);
-
-	if ((fp = __sfp()) == NULL) {
-		free(st);
-		return (NULL);
-	}
-
-	st->pos = 0;
-	st->len = (oflags & O_WRONLY) ? 0 : size;
-	st->size = size;
-	st->update = oflags & O_RDWR;
-
-	if (buf == NULL) {
-		if ((st->string = malloc(size)) == NULL) {
-			free(st);
-			fp->_flags = 0;
-			return (NULL);
-		}
-		*st->string = '\0';
-	} else {
-		st->string = (char *)buf;
-
-		if (oflags & O_TRUNC)
-			*st->string = '\0';
-
-		if (oflags & O_APPEND) {
-			char	*p;
-
-			if ((p = memchr(st->string, '\0', size)) != NULL)
-				st->pos = st->len = (p - st->string);
-			else
-				st->pos = st->len = size;
-		}
-	}
-
-	fp->_flags = (short)flags;
-	fp->_file = -1;
-	fp->_cookie = (void *)st;
-	fp->_read = (flags & __SWR) ? NULL : fmemopen_read;
-	fp->_write = (flags & __SRD) ? NULL : fmemopen_write;
-	fp->_seek = fmemopen_seek;
-	fp->_close = (buf == NULL) ? fmemopen_close_free : fmemopen_close;
-
-	return (fp);
-}
-DEF_WEAK(fmemopen);
diff --git a/libc/upstream-openbsd/lib/libc/stdlib/strtoimax.c b/libc/upstream-openbsd/lib/libc/stdlib/strtoimax.c
index 2fc04e4..74e3556 100644
--- a/libc/upstream-openbsd/lib/libc/stdlib/strtoimax.c
+++ b/libc/upstream-openbsd/lib/libc/stdlib/strtoimax.c
@@ -1,4 +1,4 @@
-/*	$OpenBSD: strtoimax.c,v 1.2 2014/09/13 20:10:12 schwarze Exp $	*/
+/*	$OpenBSD: strtoimax.c,v 1.4 2017/07/06 16:23:11 millert Exp $	*/
 /*
  * Copyright (c) 1992 The Regents of the University of California.
  * All rights reserved.
@@ -74,8 +74,8 @@
 		if (c == '+')
 			c = *s++;
 	}
-	if ((base == 0 || base == 16) &&
-	    c == '0' && (*s == 'x' || *s == 'X')) {
+	if ((base == 0 || base == 16) && c == '0' &&
+	    (*s == 'x' || *s == 'X') && isxdigit((unsigned char)s[1])) {
 		c = s[1];
 		s += 2;
 		base = 16;
@@ -148,3 +148,4 @@
 		*endptr = (char *) (any ? s - 1 : nptr);
 	return (acc);
 }
+DEF_STRONG(strtoimax);
diff --git a/libc/upstream-openbsd/lib/libc/stdlib/strtol.c b/libc/upstream-openbsd/lib/libc/stdlib/strtol.c
index 86cec35..599d235 100644
--- a/libc/upstream-openbsd/lib/libc/stdlib/strtol.c
+++ b/libc/upstream-openbsd/lib/libc/stdlib/strtol.c
@@ -1,4 +1,4 @@
-/*	$OpenBSD: strtol.c,v 1.10 2014/09/13 20:10:12 schwarze Exp $ */
+/*	$OpenBSD: strtol.c,v 1.12 2017/07/06 16:23:11 millert Exp $ */
 /*
  * Copyright (c) 1990 The Regents of the University of California.
  * All rights reserved.
@@ -75,8 +75,8 @@
 		if (c == '+')
 			c = *s++;
 	}
-	if ((base == 0 || base == 16) &&
-	    c == '0' && (*s == 'x' || *s == 'X')) {
+	if ((base == 0 || base == 16) && c == '0' &&
+	    (*s == 'x' || *s == 'X') && isxdigit((unsigned char)s[1])) {
 		c = s[1];
 		s += 2;
 		base = 16;
@@ -148,3 +148,4 @@
 		*endptr = (char *) (any ? s - 1 : nptr);
 	return (acc);
 }
+DEF_STRONG(strtol);
diff --git a/libc/upstream-openbsd/lib/libc/stdlib/strtoll.c b/libc/upstream-openbsd/lib/libc/stdlib/strtoll.c
index cf82c8e..d21a249 100644
--- a/libc/upstream-openbsd/lib/libc/stdlib/strtoll.c
+++ b/libc/upstream-openbsd/lib/libc/stdlib/strtoll.c
@@ -1,4 +1,4 @@
-/*	$OpenBSD: strtoll.c,v 1.8 2014/09/13 20:10:12 schwarze Exp $ */
+/*	$OpenBSD: strtoll.c,v 1.10 2017/07/06 16:23:11 millert Exp $ */
 /*
  * Copyright (c) 1992 The Regents of the University of California.
  * All rights reserved.
@@ -77,8 +77,8 @@
 		if (c == '+')
 			c = *s++;
 	}
-	if ((base == 0 || base == 16) &&
-	    c == '0' && (*s == 'x' || *s == 'X')) {
+	if ((base == 0 || base == 16) && c == '0' &&
+	    (*s == 'x' || *s == 'X') && isxdigit((unsigned char)s[1])) {
 		c = s[1];
 		s += 2;
 		base = 16;
@@ -151,5 +151,6 @@
 		*endptr = (char *) (any ? s - 1 : nptr);
 	return (acc);
 }
+DEF_STRONG(strtoll);
 
-__strong_alias(strtoq, strtoll);
+__weak_alias(strtoq, strtoll);
diff --git a/libc/upstream-openbsd/lib/libc/stdlib/strtoul.c b/libc/upstream-openbsd/lib/libc/stdlib/strtoul.c
index 2aa41b7..6667bea 100644
--- a/libc/upstream-openbsd/lib/libc/stdlib/strtoul.c
+++ b/libc/upstream-openbsd/lib/libc/stdlib/strtoul.c
@@ -1,4 +1,4 @@
-/*	$OpenBSD: strtoul.c,v 1.9 2014/09/13 20:10:12 schwarze Exp $ */
+/*	$OpenBSD: strtoul.c,v 1.11 2017/07/06 16:23:11 millert Exp $ */
 /*
  * Copyright (c) 1990 The Regents of the University of California.
  * All rights reserved.
@@ -69,8 +69,8 @@
 		if (c == '+')
 			c = *s++;
 	}
-	if ((base == 0 || base == 16) &&
-	    c == '0' && (*s == 'x' || *s == 'X')) {
+	if ((base == 0 || base == 16) && c == '0' &&
+	    (*s == 'x' || *s == 'X') && isxdigit((unsigned char)s[1])) {
 		c = s[1];
 		s += 2;
 		base = 16;
@@ -107,3 +107,4 @@
 		*endptr = (char *) (any ? s - 1 : nptr);
 	return (acc);
 }
+DEF_STRONG(strtoul);
diff --git a/libc/upstream-openbsd/lib/libc/stdlib/strtoull.c b/libc/upstream-openbsd/lib/libc/stdlib/strtoull.c
index 8464176..d7733e40 100644
--- a/libc/upstream-openbsd/lib/libc/stdlib/strtoull.c
+++ b/libc/upstream-openbsd/lib/libc/stdlib/strtoull.c
@@ -1,4 +1,4 @@
-/*	$OpenBSD: strtoull.c,v 1.7 2014/09/13 20:10:12 schwarze Exp $ */
+/*	$OpenBSD: strtoull.c,v 1.9 2017/07/06 16:23:11 millert Exp $ */
 /*
  * Copyright (c) 1992 The Regents of the University of California.
  * All rights reserved.
@@ -71,8 +71,8 @@
 		if (c == '+')
 			c = *s++;
 	}
-	if ((base == 0 || base == 16) &&
-	    c == '0' && (*s == 'x' || *s == 'X')) {
+	if ((base == 0 || base == 16) && c == '0' &&
+	    (*s == 'x' || *s == 'X') && isxdigit((unsigned char)s[1])) {
 		c = s[1];
 		s += 2;
 		base = 16;
@@ -109,5 +109,6 @@
 		*endptr = (char *) (any ? s - 1 : nptr);
 	return (acc);
 }
+DEF_STRONG(strtoull);
 
-__strong_alias(strtouq, strtoull);
+__weak_alias(strtouq, strtoull);
diff --git a/libc/upstream-openbsd/lib/libc/stdlib/strtoumax.c b/libc/upstream-openbsd/lib/libc/stdlib/strtoumax.c
index c73f7e5..348184c 100644
--- a/libc/upstream-openbsd/lib/libc/stdlib/strtoumax.c
+++ b/libc/upstream-openbsd/lib/libc/stdlib/strtoumax.c
@@ -1,4 +1,4 @@
-/*	$OpenBSD: strtoumax.c,v 1.2 2014/09/13 20:10:12 schwarze Exp $	*/
+/*	$OpenBSD: strtoumax.c,v 1.4 2017/07/06 16:23:11 millert Exp $	*/
 /*
  * Copyright (c) 1992 The Regents of the University of California.
  * All rights reserved.
@@ -68,8 +68,8 @@
 		if (c == '+')
 			c = *s++;
 	}
-	if ((base == 0 || base == 16) &&
-	    c == '0' && (*s == 'x' || *s == 'X')) {
+	if ((base == 0 || base == 16) && c == '0' &&
+	    (*s == 'x' || *s == 'X') && isxdigit((unsigned char)s[1])) {
 		c = s[1];
 		s += 2;
 		base = 16;
@@ -106,3 +106,4 @@
 		*endptr = (char *) (any ? s - 1 : nptr);
 	return (acc);
 }
+DEF_STRONG(strtoumax);
diff --git a/libc/upstream-openbsd/lib/libc/string/wcsstr.c b/libc/upstream-openbsd/lib/libc/string/wcsstr.c
deleted file mode 100644
index 6a7b0da..0000000
--- a/libc/upstream-openbsd/lib/libc/string/wcsstr.c
+++ /dev/null
@@ -1,71 +0,0 @@
-/*	$OpenBSD: wcsstr.c,v 1.5 2015/10/01 02:32:07 guenther Exp $	*/
-/*	$NetBSD: wcsstr.c,v 1.3 2003/03/05 20:18:17 tshiozak Exp $	*/
-
-/*-
- * Copyright (c)1999 Citrus 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:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. 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 AUTHOR 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 AUTHOR 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.
- *
- *	citrus Id: wcsstr.c,v 1.2 2000/12/21 05:07:25 itojun Exp
- */
-
-#include <wchar.h>
-
-wchar_t *
-#ifdef WCSWCS
-wcswcs(const wchar_t *big, const wchar_t *little)
-#else
-wcsstr(const wchar_t *big, const wchar_t *little)
-#endif
-{
-	const wchar_t *p;
-	const wchar_t *q;
-	const wchar_t *r;
-
-	if (!*little) {
-		return (wchar_t *)big;
-	}
-	if (wcslen(big) < wcslen(little))
-		return NULL;
-
-	p = big;
-	q = little;
-	while (*p) {
-		q = little;
-		r = p;
-		while (*q) {
-			if (*r != *q)
-				break;
-			q++;
-			r++;
-		}
-		if (!*q) {
-			return (wchar_t *)p;
-		}
-		p++;
-	}
-	return NULL;
-}
-#ifndef WCSWCS
-DEF_STRONG(wcsstr);
-#endif
diff --git a/tests/gtest_main.cpp b/tests/gtest_main.cpp
index 227b6d1..9dcc000 100644
--- a/tests/gtest_main.cpp
+++ b/tests/gtest_main.cpp
@@ -148,12 +148,16 @@
 
   const std::string& GetName() const { return name_; }
 
-  void SetResult(TestResult result) { result_ = result; }
+  void SetResult(TestResult result) {
+    // Native xfails are inherently likely to actually be relying on undefined
+    // behavior/uninitialized memory, and thus likely to pass from time to time
+    // on CTS. Avoid that unpleasantness by just rewriting all xfail failures
+    // as successes. You'll still see the actual failure details.
+    if (GetName().find("xfail") == 0) result = TEST_SUCCESS;
+    result_ = result;
+  }
 
   TestResult GetResult() const { return result_; }
-  TestResult GetExpectedResult() const {
-    return GetName().find("xfail") == 0 ? TEST_FAILED : TEST_SUCCESS;
-  }
 
   void SetTestTime(int64_t elapsed_time_ns) { elapsed_time_ns_ = elapsed_time_ns; }
 
@@ -208,13 +212,8 @@
     return test_list_[test_id].GetResult();
   }
 
-  TestResult GetExpectedTestResult(size_t test_id) const {
-    VerifyTestId(test_id);
-    return test_list_[test_id].GetExpectedResult();
-  }
-
   bool GetTestSuccess(size_t test_id) const {
-    return GetTestResult(test_id) == GetExpectedTestResult(test_id);
+    return GetTestResult(test_id) == TEST_SUCCESS;
   }
 
   void SetTestTime(size_t test_id, int64_t elapsed_time_ns) {
@@ -349,7 +348,7 @@
   printf("%s", test_output.c_str());
 
   TestResult result = testcase.GetTestResult(test_id);
-  if (result == testcase.GetExpectedTestResult(test_id)) {
+  if (result == TEST_SUCCESS) {
     ColoredPrintf(COLOR_GREEN, "[       OK ] ");
   } else {
     ColoredPrintf(COLOR_RED, "[  FAILED  ] ");
@@ -366,19 +365,10 @@
 
 static void OnTestEndPrint(const TestCase& testcase, size_t test_id) {
   TestResult result = testcase.GetTestResult(test_id);
-  TestResult expected = testcase.GetExpectedTestResult(test_id);
   if (result == TEST_SUCCESS) {
-    if (expected == TEST_SUCCESS) {
-      ColoredPrintf(COLOR_GREEN, "[    OK    ] ");
-    } else if (expected == TEST_FAILED) {
-      ColoredPrintf(COLOR_RED, "[  XPASS   ] ");
-    }
+    ColoredPrintf(COLOR_GREEN, "[    OK    ] ");
   } else if (result == TEST_FAILED) {
-    if (expected == TEST_SUCCESS) {
-      ColoredPrintf(COLOR_RED, "[  FAILED  ] ");
-    } else if (expected == TEST_FAILED) {
-      ColoredPrintf(COLOR_YELLOW, "[  XFAIL   ] ");
-    }
+    ColoredPrintf(COLOR_RED, "[  FAILED  ] ");
   } else if (result == TEST_TIMEOUT) {
     ColoredPrintf(COLOR_RED, "[ TIMEOUT  ] ");
   }
@@ -400,7 +390,6 @@
                                     int64_t elapsed_time_ns) {
 
   std::vector<std::string> fail_test_name_list;
-  std::vector<std::string> xpass_test_name_list;
   std::vector<std::pair<std::string, int64_t>> timeout_test_list;
 
   // For tests that were slow but didn't time out.
@@ -414,22 +403,14 @@
     test_count += testcase.TestCount();
     for (size_t i = 0; i < testcase.TestCount(); ++i) {
       TestResult result = testcase.GetTestResult(i);
-      TestResult expected = testcase.GetExpectedTestResult(i);
       if (result == TEST_TIMEOUT) {
         timeout_test_list.push_back(
             std::make_pair(testcase.GetTestName(i), testcase.GetTestTime(i)));
-      } else if (result == expected) {
-        if (result == TEST_SUCCESS) {
-          ++success_test_count;
-        } else {
-          ++expected_failure_count;
-        }
-      } else {
-        if (result == TEST_FAILED) {
+      } else if (result == TEST_SUCCESS) {
+        ++success_test_count;
+        if (testcase.GetTestName(i).find(".xfail_") != std::string::npos) ++expected_failure_count;
+      } else if (result == TEST_FAILED) {
           fail_test_name_list.push_back(testcase.GetTestName(i));
-        } else {
-          xpass_test_name_list.push_back(testcase.GetTestName(i));
-        }
       }
       if (result != TEST_TIMEOUT &&
           testcase.GetTestTime(i) / 1000000 >= GetSlowThresholdMs(testcase.GetTestName(i))) {
@@ -450,7 +431,7 @@
   ColoredPrintf(COLOR_GREEN,  "[   PASS   ] ");
   printf("%zu %s.", success_test_count, (success_test_count == 1) ? "test" : "tests");
   if (expected_failure_count > 0) {
-    printf(" (%zu expected failure%s)", expected_failure_count,
+    printf(" (%zu expected failure%s.)", expected_failure_count,
            (expected_failure_count == 1) ? "" : "s");
   }
   printf("\n");
@@ -490,18 +471,7 @@
     }
   }
 
-  // Print tests that should have failed.
-  size_t xpass_test_count = xpass_test_name_list.size();
-  if (xpass_test_count > 0) {
-    ColoredPrintf(COLOR_RED,  "[  XPASS   ] ");
-    printf("%zu %s, listed below:\n", xpass_test_count, (xpass_test_count == 1) ? "test" : "tests");
-    for (const auto& name : xpass_test_name_list) {
-      ColoredPrintf(COLOR_RED, "[  XPASS   ] ");
-      printf("%s\n", name.c_str());
-    }
-  }
-
-  if (timeout_test_count > 0 || slow_test_count > 0 || fail_test_count > 0 || xpass_test_count > 0) {
+  if (timeout_test_count > 0 || slow_test_count > 0 || fail_test_count > 0) {
     printf("\n");
   }
 
@@ -514,9 +484,6 @@
   if (fail_test_count > 0) {
     printf("%2zu FAILED %s\n", fail_test_count, (fail_test_count == 1) ? "TEST" : "TESTS");
   }
-  if (xpass_test_count > 0) {
-    printf("%2zu SHOULD HAVE FAILED %s\n", xpass_test_count, (xpass_test_count == 1) ? "TEST" : "TESTS");
-  }
 
   fflush(stdout);
 }
diff --git a/tests/signal_test.cpp b/tests/signal_test.cpp
index 36ac690..207c156 100644
--- a/tests/signal_test.cpp
+++ b/tests/signal_test.cpp
@@ -527,3 +527,11 @@
   ASSERT_EQ(0, sigprocmask(SIG_BLOCK, nullptr, &set));
   EXPECT_TRUE(sigismember(&set, SIGALRM));
 }
+
+TEST(signal, killpg_EINVAL) {
+  // POSIX leaves pgrp <= 1 undefined, but glibc fails with EINVAL for < 0
+  // and passes 0 through to kill(2).
+  errno = 0;
+  ASSERT_EQ(-1, killpg(-1, SIGKILL));
+  ASSERT_EQ(EINVAL, errno);
+}
diff --git a/tests/stdio_test.cpp b/tests/stdio_test.cpp
index da70d21..1f27c83 100644
--- a/tests/stdio_test.cpp
+++ b/tests/stdio_test.cpp
@@ -27,6 +27,7 @@
 #include <wchar.h>
 #include <locale.h>
 
+#include <string>
 #include <vector>
 
 #include "BionicDeathTest.h"
@@ -41,6 +42,8 @@
 #define STDIO_DEATHTEST stdio_DeathTest
 #endif
 
+using namespace std::string_literals;
+
 class stdio_DeathTest : public BionicDeathTest {};
 class stdio_nofortify_DeathTest : public BionicDeathTest {};
 
@@ -923,7 +926,7 @@
   ASSERT_EQ(WEOF, fgetwc(fp));
   ASSERT_EQ(EILSEQ, errno);
 
-  fclose(fp);
+  ASSERT_EQ(0, fclose(fp));
 }
 
 TEST(STDIO_TEST, fmemopen) {
@@ -934,32 +937,410 @@
   ASSERT_NE(EOF, fputs("abc>\n", fp));
   fflush(fp);
 
+  // We wrote to the buffer...
   ASSERT_STREQ("<abc>\n", buf);
 
+  // And can read back from the file.
   AssertFileIs(fp, "<abc>\n", true);
-  fclose(fp);
+  ASSERT_EQ(0, fclose(fp));
 }
 
-TEST(STDIO_TEST, KNOWN_FAILURE_ON_BIONIC(fmemopen_NULL)) {
+TEST(STDIO_TEST, fmemopen_nullptr) {
   FILE* fp = fmemopen(nullptr, 128, "r+");
   ASSERT_NE(EOF, fputs("xyz\n", fp));
 
   AssertFileIs(fp, "xyz\n", true);
-  fclose(fp);
+  ASSERT_EQ(0, fclose(fp));
 }
 
-TEST(STDIO_TEST, fmemopen_EINVAL) {
+TEST(STDIO_TEST, fmemopen_trailing_NUL_byte) {
+  FILE* fp;
+  char buf[8];
+
+  // POSIX: "When a stream open for writing is flushed or closed, a null byte
+  // shall be written at the current position or at the end of the buffer,
+  // depending on the size of the contents."
+  memset(buf, 'x', sizeof(buf));
+  ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w"));
+  // Even with nothing written (and not in truncate mode), we'll flush a NUL...
+  ASSERT_EQ(0, fflush(fp));
+  EXPECT_EQ("\0xxxxxxx"s, std::string(buf, buf + sizeof(buf)));
+  // Now write and check that the NUL moves along with our writes...
+  ASSERT_NE(EOF, fputs("hello", fp));
+  ASSERT_EQ(0, fflush(fp));
+  EXPECT_EQ("hello\0xx"s, std::string(buf, buf + sizeof(buf)));
+  ASSERT_NE(EOF, fputs("wo", fp));
+  ASSERT_EQ(0, fflush(fp));
+  EXPECT_EQ("hellowo\0"s, std::string(buf, buf + sizeof(buf)));
+  ASSERT_EQ(0, fclose(fp));
+
+  // "If a stream open for update is flushed or closed and the last write has
+  // advanced the current buffer size, a null byte shall be written at the end
+  // of the buffer if it fits."
+  memset(buf, 'x', sizeof(buf));
+  ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "r+"));
+  // Nothing written yet, so no advance...
+  ASSERT_EQ(0, fflush(fp));
+  EXPECT_EQ("xxxxxxxx"s, std::string(buf, buf + sizeof(buf)));
+  ASSERT_NE(EOF, fputs("hello", fp));
+  ASSERT_EQ(0, fclose(fp));
+}
+
+TEST(STDIO_TEST, fmemopen_size) {
+  FILE* fp;
   char buf[16];
+  memset(buf, 'x', sizeof(buf));
 
-  // Invalid size.
-  errno = 0;
-  ASSERT_EQ(nullptr, fmemopen(buf, 0, "r+"));
-  ASSERT_EQ(EINVAL, errno);
+  // POSIX: "The stream shall also maintain the size of the current buffer
+  // contents; use of fseek() or fseeko() on the stream with SEEK_END shall
+  // seek relative to this size."
 
-  // No '+' with NULL buffer.
+  // "For modes r and r+ the size shall be set to the value given by the size
+  // argument."
+  ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "r"));
+  ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
+  EXPECT_EQ(16, ftell(fp));
+  EXPECT_EQ(16, ftello(fp));
+  ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
+  EXPECT_EQ(16, ftell(fp));
+  EXPECT_EQ(16, ftello(fp));
+  ASSERT_EQ(0, fclose(fp));
+  ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "r+"));
+  ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
+  EXPECT_EQ(16, ftell(fp));
+  EXPECT_EQ(16, ftello(fp));
+  ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
+  EXPECT_EQ(16, ftell(fp));
+  EXPECT_EQ(16, ftello(fp));
+  ASSERT_EQ(0, fclose(fp));
+
+  // "For modes w and w+ the initial size shall be zero..."
+  ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "w"));
+  ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
+  EXPECT_EQ(0, ftell(fp));
+  EXPECT_EQ(0, ftello(fp));
+  ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
+  EXPECT_EQ(0, ftell(fp));
+  EXPECT_EQ(0, ftello(fp));
+  ASSERT_EQ(0, fclose(fp));
+  ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "w+"));
+  ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
+  EXPECT_EQ(0, ftell(fp));
+  EXPECT_EQ(0, ftello(fp));
+  ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
+  EXPECT_EQ(0, ftell(fp));
+  EXPECT_EQ(0, ftello(fp));
+  ASSERT_EQ(0, fclose(fp));
+
+  // "...and for modes a and a+ the initial size shall be:
+  // 1. Zero, if buf is a null pointer
+  ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "a"));
+  ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
+  EXPECT_EQ(0, ftell(fp));
+  EXPECT_EQ(0, ftello(fp));
+  ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
+  EXPECT_EQ(0, ftell(fp));
+  EXPECT_EQ(0, ftello(fp));
+  ASSERT_EQ(0, fclose(fp));
+  ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "a+"));
+  ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
+  EXPECT_EQ(0, ftell(fp));
+  EXPECT_EQ(0, ftello(fp));
+  ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
+  EXPECT_EQ(0, ftell(fp));
+  EXPECT_EQ(0, ftello(fp));
+  ASSERT_EQ(0, fclose(fp));
+
+  // 2. The position of the first null byte in the buffer, if one is found
+  memset(buf, 'x', sizeof(buf));
+  buf[3] = '\0';
+  ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "a"));
+  ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
+  EXPECT_EQ(3, ftell(fp));
+  EXPECT_EQ(3, ftello(fp));
+  ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
+  EXPECT_EQ(3, ftell(fp));
+  EXPECT_EQ(3, ftello(fp));
+  ASSERT_EQ(0, fclose(fp));
+  memset(buf, 'x', sizeof(buf));
+  buf[3] = '\0';
+  ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "a+"));
+  ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
+  EXPECT_EQ(3, ftell(fp));
+  EXPECT_EQ(3, ftello(fp));
+  ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
+  EXPECT_EQ(3, ftell(fp));
+  EXPECT_EQ(3, ftello(fp));
+  ASSERT_EQ(0, fclose(fp));
+
+  // 3. The value of the size argument, if buf is not a null pointer and no
+  // null byte is found.
+  memset(buf, 'x', sizeof(buf));
+  ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "a"));
+  ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
+  EXPECT_EQ(16, ftell(fp));
+  EXPECT_EQ(16, ftello(fp));
+  ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
+  EXPECT_EQ(16, ftell(fp));
+  EXPECT_EQ(16, ftello(fp));
+  ASSERT_EQ(0, fclose(fp));
+  memset(buf, 'x', sizeof(buf));
+  ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "a+"));
+  ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
+  EXPECT_EQ(16, ftell(fp));
+  EXPECT_EQ(16, ftello(fp));
+  ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
+  EXPECT_EQ(16, ftell(fp));
+  EXPECT_EQ(16, ftello(fp));
+  ASSERT_EQ(0, fclose(fp));
+}
+
+TEST(STDIO_TEST, fmemopen_SEEK_END) {
+  // fseek SEEK_END is relative to the current string length, not the buffer size.
+  FILE* fp;
+  char buf[8];
+  memset(buf, 'x', sizeof(buf));
+  strcpy(buf, "str");
+  ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w+"));
+  ASSERT_NE(EOF, fputs("string", fp));
+  EXPECT_EQ(0, fseek(fp, 0, SEEK_END));
+  EXPECT_EQ(static_cast<long>(strlen("string")), ftell(fp));
+  EXPECT_EQ(static_cast<off_t>(strlen("string")), ftello(fp));
+  EXPECT_EQ(0, fclose(fp));
+
+  // glibc < 2.22 interpreted SEEK_END the wrong way round (subtracting rather
+  // than adding).
+  ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w+"));
+  ASSERT_NE(EOF, fputs("54321", fp));
+  EXPECT_EQ(0, fseek(fp, -2, SEEK_END));
+  EXPECT_EQ('2', fgetc(fp));
+  EXPECT_EQ(0, fclose(fp));
+}
+
+TEST(STDIO_TEST, fmemopen_seek_invalid) {
+  char buf[8];
+  memset(buf, 'x', sizeof(buf));
+  FILE* fp = fmemopen(buf, sizeof(buf), "w");
+  ASSERT_TRUE(fp != nullptr);
+
+  // POSIX: "An attempt to seek ... to a negative position or to a position
+  // larger than the buffer size given in the size argument shall fail."
+  // (There's no mention of what errno should be set to, and glibc doesn't
+  // set errno in any of these cases.)
+  EXPECT_EQ(-1, fseek(fp, -2, SEEK_SET));
+  EXPECT_EQ(-1, fseeko(fp, -2, SEEK_SET));
+  EXPECT_EQ(-1, fseek(fp, sizeof(buf) + 1, SEEK_SET));
+  EXPECT_EQ(-1, fseeko(fp, sizeof(buf) + 1, SEEK_SET));
+}
+
+TEST(STDIO_TEST, fmemopen_read_EOF) {
+  // POSIX: "A read operation on the stream shall not advance the current
+  // buffer position beyond the current buffer size."
+  char buf[8];
+  memset(buf, 'x', sizeof(buf));
+  FILE* fp = fmemopen(buf, sizeof(buf), "r");
+  ASSERT_TRUE(fp != nullptr);
+  char buf2[BUFSIZ];
+  ASSERT_EQ(8U, fread(buf2, 1, sizeof(buf2), fp));
+  // POSIX: "Reaching the buffer size in a read operation shall count as
+  // end-of-file.
+  ASSERT_TRUE(feof(fp));
+  ASSERT_EQ(EOF, fgetc(fp));
+  ASSERT_EQ(0, fclose(fp));
+}
+
+TEST(STDIO_TEST, fmemopen_read_null_bytes) {
+  // POSIX: "Null bytes in the buffer shall have no special meaning for reads."
+  char buf[] = "h\0e\0l\0l\0o";
+  FILE* fp = fmemopen(buf, sizeof(buf), "r");
+  ASSERT_TRUE(fp != nullptr);
+  ASSERT_EQ('h', fgetc(fp));
+  ASSERT_EQ(0, fgetc(fp));
+  ASSERT_EQ('e', fgetc(fp));
+  ASSERT_EQ(0, fgetc(fp));
+  ASSERT_EQ('l', fgetc(fp));
+  ASSERT_EQ(0, fgetc(fp));
+  // POSIX: "The read operation shall start at the current buffer position of
+  // the stream."
+  char buf2[8];
+  memset(buf2, 'x', sizeof(buf2));
+  ASSERT_EQ(4U, fread(buf2, 1, sizeof(buf2), fp));
+  ASSERT_EQ('l', buf2[0]);
+  ASSERT_EQ(0, buf2[1]);
+  ASSERT_EQ('o', buf2[2]);
+  ASSERT_EQ(0, buf2[3]);
+  for (size_t i = 4; i < sizeof(buf2); ++i) ASSERT_EQ('x', buf2[i]) << i;
+  ASSERT_TRUE(feof(fp));
+  ASSERT_EQ(0, fclose(fp));
+}
+
+TEST(STDIO_TEST, fmemopen_write) {
+  FILE* fp;
+  char buf[8];
+
+  // POSIX: "A write operation shall start either at the current position of
+  // the stream (if mode has not specified 'a' as the first character)..."
+  memset(buf, 'x', sizeof(buf));
+  ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "r+"));
+  setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
+  ASSERT_EQ(0, fseek(fp, 2, SEEK_SET));
+  ASSERT_EQ(' ', fputc(' ', fp));
+  EXPECT_EQ("xx xxxxx", std::string(buf, buf + sizeof(buf)));
+  ASSERT_EQ(0, fclose(fp));
+
+  // "...or at the current size of the stream (if mode had 'a' as the first
+  // character)." (See the fmemopen_size test for what "size" means, but for
+  // mode "a", it's the first NUL byte.)
+  memset(buf, 'x', sizeof(buf));
+  buf[3] = '\0';
+  ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a+"));
+  setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
+  ASSERT_EQ(' ', fputc(' ', fp));
+  EXPECT_EQ("xxx \0xxx"s, std::string(buf, buf + sizeof(buf)));
+  ASSERT_EQ(0, fclose(fp));
+
+  // "If the current position at the end of the write is larger than the
+  // current buffer size, the current buffer size shall be set to the current
+  // position." (See the fmemopen_size test for what "size" means, but to
+  // query it we SEEK_END with offset 0, and then ftell.)
+  memset(buf, 'x', sizeof(buf));
+  ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w+"));
+  setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
+  ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
+  EXPECT_EQ(0, ftell(fp));
+  ASSERT_EQ(' ', fputc(' ', fp));
+  ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
+  EXPECT_EQ(1, ftell(fp));
+  ASSERT_NE(EOF, fputs("123", fp));
+  ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
+  EXPECT_EQ(4, ftell(fp));
+  EXPECT_EQ(" 123\0xxx"s, std::string(buf, buf + sizeof(buf)));
+  ASSERT_EQ(0, fclose(fp));
+}
+
+TEST(STDIO_TEST, fmemopen_write_EOF) {
+  // POSIX: "A write operation on the stream shall not advance the current
+  // buffer size beyond the size given in the size argument."
+  FILE* fp;
+
+  // Scalar writes...
+  ASSERT_NE(nullptr, fp = fmemopen(nullptr, 4, "w"));
+  setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
+  ASSERT_EQ('x', fputc('x', fp));
+  ASSERT_EQ('x', fputc('x', fp));
+  ASSERT_EQ('x', fputc('x', fp));
+  ASSERT_EQ(EOF, fputc('x', fp)); // Only 3 fit because of the implicit NUL.
+  ASSERT_EQ(0, fclose(fp));
+
+  // Vector writes...
+  ASSERT_NE(nullptr, fp = fmemopen(nullptr, 4, "w"));
+  setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
+  ASSERT_EQ(3U, fwrite("xxxx", 1, 4, fp));
+  ASSERT_EQ(0, fclose(fp));
+}
+
+TEST(STDIO_TEST, fmemopen_initial_position) {
+  // POSIX: "The ... current position in the buffer ... shall be initially
+  // set to either the beginning of the buffer (for r and w modes) ..."
+  char buf[] = "hello\0world";
+  FILE* fp;
+  ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "r"));
+  EXPECT_EQ(0L, ftell(fp));
+  EXPECT_EQ(0, fclose(fp));
+  ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w"));
+  EXPECT_EQ(0L, ftell(fp));
+  EXPECT_EQ(0, fclose(fp));
+  buf[0] = 'h'; // (Undo the effects of the above.)
+
+  // POSIX: "...or to the first null byte in the buffer (for a modes)."
+  ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a"));
+  EXPECT_EQ(5L, ftell(fp));
+  EXPECT_EQ(0, fclose(fp));
+
+  // POSIX: "If no null byte is found in append mode, the initial position
+  // shall be set to one byte after the end of the buffer."
+  memset(buf, 'x', sizeof(buf));
+  ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a"));
+  EXPECT_EQ(static_cast<long>(sizeof(buf)), ftell(fp));
+  EXPECT_EQ(0, fclose(fp));
+}
+
+TEST(STDIO_TEST, fmemopen_initial_position_allocated) {
+  // POSIX: "If buf is a null pointer, the initial position shall always be
+  // set to the beginning of the buffer."
+  FILE* fp = fmemopen(nullptr, 128, "a+");
+  ASSERT_TRUE(fp != nullptr);
+  EXPECT_EQ(0L, ftell(fp));
+  EXPECT_EQ(0L, fseek(fp, 0, SEEK_SET));
+  EXPECT_EQ(0, fclose(fp));
+}
+
+TEST(STDIO_TEST, fmemopen_zero_length) {
+  // POSIX says it's up to the implementation whether or not you can have a
+  // zero-length buffer (but "A future version of this standard may require
+  // support of zero-length buffer streams explicitly"). BSD and glibc < 2.22
+  // agreed that you couldn't, but glibc >= 2.22 allows it for consistency.
+  FILE* fp;
+  char buf[16];
+  ASSERT_NE(nullptr, fp = fmemopen(buf, 0, "r+"));
+  ASSERT_EQ(EOF, fgetc(fp));
+  ASSERT_TRUE(feof(fp));
+  ASSERT_EQ(0, fclose(fp));
+  ASSERT_NE(nullptr, fp = fmemopen(nullptr, 0, "r+"));
+  ASSERT_EQ(EOF, fgetc(fp));
+  ASSERT_TRUE(feof(fp));
+  ASSERT_EQ(0, fclose(fp));
+
+  ASSERT_NE(nullptr, fp = fmemopen(buf, 0, "w+"));
+  setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
+  ASSERT_EQ(EOF, fputc('x', fp));
+  ASSERT_EQ(0, fclose(fp));
+  ASSERT_NE(nullptr, fp = fmemopen(nullptr, 0, "w+"));
+  setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
+  ASSERT_EQ(EOF, fputc('x', fp));
+  ASSERT_EQ(0, fclose(fp));
+}
+
+TEST(STDIO_TEST, fmemopen_write_only_allocated) {
+  // POSIX says fmemopen "may fail if the mode argument does not include a '+'".
+  // BSD fails, glibc doesn't. We side with the more lenient.
+  FILE* fp;
+  ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "r"));
+  ASSERT_EQ(0, fclose(fp));
+  ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "w"));
+  ASSERT_EQ(0, fclose(fp));
+}
+
+TEST(STDIO_TEST, fmemopen_fileno) {
+  // There's no fd backing an fmemopen FILE*.
+  FILE* fp = fmemopen(nullptr, 16, "r");
+  ASSERT_TRUE(fp != nullptr);
   errno = 0;
-  ASSERT_EQ(nullptr, fmemopen(nullptr, 0, "r"));
-  ASSERT_EQ(EINVAL, errno);
+  ASSERT_EQ(-1, fileno(fp));
+  ASSERT_EQ(EBADF, errno);
+  ASSERT_EQ(0, fclose(fp));
+}
+
+TEST(STDIO_TEST, fmemopen_append_after_seek) {
+  // In BSD and glibc < 2.22, append mode didn't force writes to append if
+  // there had been an intervening seek.
+
+  FILE* fp;
+  char buf[] = "hello\0world";
+  ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a"));
+  setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
+  ASSERT_EQ(0, fseek(fp, 0, SEEK_SET));
+  ASSERT_NE(EOF, fputc('!', fp));
+  EXPECT_EQ("hello!\0orld\0"s, std::string(buf, buf + sizeof(buf)));
+  ASSERT_EQ(0, fclose(fp));
+
+  memcpy(buf, "hello\0world", sizeof(buf));
+  ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a+"));
+  setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
+  ASSERT_EQ(0, fseek(fp, 0, SEEK_SET));
+  ASSERT_NE(EOF, fputc('!', fp));
+  EXPECT_EQ("hello!\0orld\0"s, std::string(buf, buf + sizeof(buf)));
+  ASSERT_EQ(0, fclose(fp));
 }
 
 TEST(STDIO_TEST, open_memstream) {
diff --git a/tests/stdlib_test.cpp b/tests/stdlib_test.cpp
index 4c4c102..c724f74 100644
--- a/tests/stdlib_test.cpp
+++ b/tests/stdlib_test.cpp
@@ -555,54 +555,6 @@
   close(fd);
 }
 
-TEST(stdlib, strtol_EINVAL) {
-  errno = 0;
-  strtol("123", NULL, -1);
-  ASSERT_EQ(EINVAL, errno);
-  errno = 0;
-  strtol("123", NULL, 1);
-  ASSERT_EQ(EINVAL, errno);
-  errno = 0;
-  strtol("123", NULL, 37);
-  ASSERT_EQ(EINVAL, errno);
-}
-
-TEST(stdlib, strtoll_EINVAL) {
-  errno = 0;
-  strtoll("123", NULL, -1);
-  ASSERT_EQ(EINVAL, errno);
-  errno = 0;
-  strtoll("123", NULL, 1);
-  ASSERT_EQ(EINVAL, errno);
-  errno = 0;
-  strtoll("123", NULL, 37);
-  ASSERT_EQ(EINVAL, errno);
-}
-
-TEST(stdlib, strtoul_EINVAL) {
-  errno = 0;
-  strtoul("123", NULL, -1);
-  ASSERT_EQ(EINVAL, errno);
-  errno = 0;
-  strtoul("123", NULL, 1);
-  ASSERT_EQ(EINVAL, errno);
-  errno = 0;
-  strtoul("123", NULL, 37);
-  ASSERT_EQ(EINVAL, errno);
-}
-
-TEST(stdlib, strtoull_EINVAL) {
-  errno = 0;
-  strtoull("123", NULL, -1);
-  ASSERT_EQ(EINVAL, errno);
-  errno = 0;
-  strtoull("123", NULL, 1);
-  ASSERT_EQ(EINVAL, errno);
-  errno = 0;
-  strtoull("123", NULL, 37);
-  ASSERT_EQ(EINVAL, errno);
-}
-
 TEST(stdlib, getsubopt) {
   char* const tokens[] = {
     const_cast<char*>("a"),
@@ -648,3 +600,51 @@
   // "mblen() shall ... return 0 (if s points to the null byte)".
   EXPECT_EQ(0, mblen("", 1));
 }
+
+template <typename T>
+static void CheckStrToInt(T fn(const char* s, char** end, int base)) {
+  char* end_p;
+
+  // Negative base => invalid.
+  errno = 0;
+  ASSERT_EQ(T(0), fn("123", &end_p, -1));
+  ASSERT_EQ(EINVAL, errno);
+
+  // Base 1 => invalid (base 0 means "please guess").
+  errno = 0;
+  ASSERT_EQ(T(0), fn("123", &end_p, 1));
+  ASSERT_EQ(EINVAL, errno);
+
+  // Base > 36 => invalid.
+  errno = 0;
+  ASSERT_EQ(T(0), fn("123", &end_p, 37));
+  ASSERT_EQ(EINVAL, errno);
+
+  // If we see "0x" *not* followed by a hex digit, we shouldn't swallow the 'x'.
+  ASSERT_EQ(T(0), fn("0xy", &end_p, 16));
+  ASSERT_EQ('x', *end_p);
+}
+
+TEST(stdlib, strtol_smoke) {
+  CheckStrToInt(strtol);
+}
+
+TEST(stdlib, strtoll_smoke) {
+  CheckStrToInt(strtoll);
+}
+
+TEST(stdlib, strtoul_smoke) {
+  CheckStrToInt(strtoul);
+}
+
+TEST(stdlib, strtoull_smoke) {
+  CheckStrToInt(strtoull);
+}
+
+TEST(stdlib, strtoimax_smoke) {
+  CheckStrToInt(strtoimax);
+}
+
+TEST(stdlib, strtoumax_smoke) {
+  CheckStrToInt(strtoumax);
+}