Merge "Fix, generalize stdatomic.h; improve test."
diff --git a/libc/Android.mk b/libc/Android.mk
index c268a87..6beb7b3 100644
--- a/libc/Android.mk
+++ b/libc/Android.mk
@@ -135,6 +135,7 @@
     bionic/link.cpp \
     bionic/locale.cpp \
     bionic/lstat.cpp \
+    bionic/malloc_info.cpp \
     bionic/mbrtoc16.cpp \
     bionic/mbrtoc32.cpp \
     bionic/mbstate.cpp \
@@ -391,6 +392,7 @@
     upstream-openbsd/lib/libc/stdio/fgetws.c \
     upstream-openbsd/lib/libc/stdio/fileno.c \
     upstream-openbsd/lib/libc/stdio/findfp.c \
+    upstream-openbsd/lib/libc/stdio/fmemopen.c \
     upstream-openbsd/lib/libc/stdio/fprintf.c \
     upstream-openbsd/lib/libc/stdio/fpurge.c \
     upstream-openbsd/lib/libc/stdio/fputc.c \
@@ -419,6 +421,8 @@
     upstream-openbsd/lib/libc/stdio/getwchar.c \
     upstream-openbsd/lib/libc/stdio/makebuf.c \
     upstream-openbsd/lib/libc/stdio/mktemp.c \
+    upstream-openbsd/lib/libc/stdio/open_memstream.c \
+    upstream-openbsd/lib/libc/stdio/open_wmemstream.c \
     upstream-openbsd/lib/libc/stdio/perror.c \
     upstream-openbsd/lib/libc/stdio/printf.c \
     upstream-openbsd/lib/libc/stdio/putc.c \
diff --git a/libc/SYSCALLS.TXT b/libc/SYSCALLS.TXT
index 38ae831..bfa13b7 100644
--- a/libc/SYSCALLS.TXT
+++ b/libc/SYSCALLS.TXT
@@ -315,7 +315,7 @@
 int     cacheflush:__ARM_NR_cacheflush(long start, long end, long flags)  arm
 
 # MIPS-specific
-int     _flush_cache:cacheflush(char* addr, const int nbytes, const int op) mips,mips64
+int     _flush_cache:cacheflush(char* addr, const int nbytes, const int op) mips
 int     __set_tls:set_thread_area(void*) mips,mips64
 
 # x86-specific
diff --git a/libc/arch-mips64/syscalls/_flush_cache.S b/libc/arch-mips64/syscalls/_flush_cache.S
deleted file mode 100644
index 997ccec..0000000
--- a/libc/arch-mips64/syscalls/_flush_cache.S
+++ /dev/null
@@ -1,27 +0,0 @@
-/* Generated by gensyscalls.py. Do not edit. */
-
-#include <private/bionic_asm.h>
-
-    .hidden __set_errno
-
-ENTRY(_flush_cache)
-    .set push
-    .set noreorder
-    li v0, __NR_cacheflush
-    syscall
-    bnez a3, 1f
-    move a0, v0
-    j ra
-    nop
-1:
-    move t0, ra
-    bal     2f
-    nop
-2:
-    .cpsetup ra, t1, 2b
-    LA t9,__set_errno
-    .cpreturn
-    j t9
-    move ra, t0
-    .set pop
-END(_flush_cache)
diff --git a/libc/bionic/dlmalloc.c b/libc/bionic/dlmalloc.c
index e89c5d1..5853e7c 100644
--- a/libc/bionic/dlmalloc.c
+++ b/libc/bionic/dlmalloc.c
@@ -16,6 +16,7 @@
 
 #include "dlmalloc.h"
 
+#include "malloc.h"
 #include "private/bionic_prctl.h"
 #include "private/libc_logging.h"
 
@@ -54,3 +55,25 @@
   prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, map, length, "libc_malloc");
   return map;
 }
+
+// Since dlmalloc isn't the default, we'll leave this unimplemented for now. If
+// we decide we need it later, we can fill it in.
+size_t __mallinfo_narenas() {
+  return 0;
+}
+
+size_t __mallinfo_nbins() {
+  return 0;
+}
+
+struct mallinfo __mallinfo_arena_info(size_t aidx __unused) {
+  struct mallinfo mi;
+  memset(&mi, 0, sizeof(mi));
+  return mi;
+}
+
+struct mallinfo __mallinfo_bin_info(size_t aidx __unused, size_t bidx __unused) {
+  struct mallinfo mi;
+  memset(&mi, 0, sizeof(mi));
+  return mi;
+}
diff --git a/libc/bionic/malloc_info.cpp b/libc/bionic/malloc_info.cpp
new file mode 100644
index 0000000..99caedb
--- /dev/null
+++ b/libc/bionic/malloc_info.cpp
@@ -0,0 +1,94 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "malloc_info.h"
+
+#include <errno.h>
+#include "private/bionic_macros.h"
+
+class __LIBC_HIDDEN__ Elem {
+public:
+  // name must be valid throughout lifetime of the object.
+  explicit Elem(FILE* fp, const char* name,
+                const char* attr_fmt = nullptr, ...) {
+    this->fp = fp;
+    this->name = name;
+
+    fprintf(fp, "<%s", name);
+    if (attr_fmt != nullptr) {
+      va_list args;
+      va_start(args, attr_fmt);
+      fputc(' ', fp);
+      vfprintf(fp, attr_fmt, args);
+      va_end(args);
+    }
+    fputc('>', fp);
+  }
+
+  ~Elem() noexcept {
+    fprintf(fp, "</%s>", name);
+  }
+
+  void contents(const char* fmt, ...) {
+      va_list args;
+      va_start(args, fmt);
+      vfprintf(fp, fmt, args);
+      va_end(args);
+  }
+
+private:
+  FILE* fp;
+  const char* name;
+
+  DISALLOW_COPY_AND_ASSIGN(Elem);
+};
+
+int malloc_info(int options, FILE* fp) {
+  if (options != 0) {
+    errno = EINVAL;
+    return -1;
+  }
+
+  Elem root(fp, "malloc", "version=\"jemalloc-1\"");
+
+  // Dump all of the large allocations in the arenas.
+  for (size_t i = 0; i < __mallinfo_narenas(); i++) {
+    struct mallinfo mi = __mallinfo_arena_info(i);
+    if (mi.hblkhd != 0) {
+      Elem arena_elem(fp, "heap", "nr=\"%d\"", i);
+      {
+        Elem(fp, "allocated-large").contents("%zu", mi.ordblks);
+        Elem(fp, "allocated-huge").contents("%zu", mi.uordblks);
+        Elem(fp, "allocated-bins").contents("%zu", mi.fsmblks);
+
+        size_t total = 0;
+        for (size_t j = 0; j < __mallinfo_nbins(); j++) {
+          struct mallinfo mi = __mallinfo_bin_info(i, j);
+          if (mi.ordblks != 0) {
+            Elem bin_elem(fp, "bin", "nr=\"%d\"", j);
+            Elem(fp, "allocated").contents("%zu", mi.ordblks);
+            Elem(fp, "nmalloc").contents("%zu", mi.uordblks);
+            Elem(fp, "ndalloc").contents("%zu", mi.fordblks);
+            total += mi.ordblks;
+          }
+        }
+        Elem(fp, "bins-total").contents("%zu", total);
+      }
+    }
+  }
+
+  return 0;
+}
diff --git a/libc/bionic/malloc_info.h b/libc/bionic/malloc_info.h
new file mode 100644
index 0000000..5fffae9
--- /dev/null
+++ b/libc/bionic/malloc_info.h
@@ -0,0 +1,32 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef LIBC_BIONIC_MALLOC_INFO_H_
+#define LIBC_BIONIC_MALLOC_INFO_H_
+
+#include <malloc.h>
+#include <sys/cdefs.h>
+
+__BEGIN_DECLS
+
+__LIBC_HIDDEN__ size_t __mallinfo_narenas();
+__LIBC_HIDDEN__ size_t __mallinfo_nbins();
+__LIBC_HIDDEN__ struct mallinfo __mallinfo_arena_info(size_t);
+__LIBC_HIDDEN__ struct mallinfo __mallinfo_bin_info(size_t, size_t);
+
+__END_DECLS
+
+#endif // LIBC_BIONIC_MALLOC_INFO_H_
diff --git a/libc/include/elf.h b/libc/include/elf.h
index faae73e..7a9485a 100644
--- a/libc/include/elf.h
+++ b/libc/include/elf.h
@@ -80,6 +80,4 @@
 #define STT_LOPROC    13
 #define STT_HIPROC    15
 
-#define R_386_IRELATIVE  42
-
 #endif /* _ELF_H */
diff --git a/libc/include/malloc.h b/libc/include/malloc.h
index e6ea276..cb1dd3b 100644
--- a/libc/include/malloc.h
+++ b/libc/include/malloc.h
@@ -24,6 +24,7 @@
  */
 #include <sys/cdefs.h>
 #include <stddef.h>
+#include <stdio.h>
 
 __BEGIN_DECLS
 
@@ -53,6 +54,27 @@
 
 extern struct mallinfo mallinfo(void);
 
+/*
+ * XML structure for malloc_info(3) is in the following format:
+ *
+ * <malloc version="jemalloc-1">
+ *   <heap nr="INT">
+ *     <allocated-large>INT</allocated-large>
+ *     <allocated-huge>INT</allocated-huge>
+ *     <allocated-bins>INT</allocated-bins>
+ *     <bins-total>INT</bins-total>
+ *     <bin nr="INT">
+ *       <allocated>INT</allocated>
+ *       <nmalloc>INT</nmalloc>
+ *       <ndalloc>INT</ndalloc>
+ *     </bin>
+ *     <!-- more bins -->
+ *   </heap>
+ *   <!-- more heaps -->
+ * </malloc>
+ */
+extern int malloc_info(int, FILE *);
+
 __END_DECLS
 
 #endif  /* LIBC_INCLUDE_MALLOC_H_ */
diff --git a/libc/include/stdio.h b/libc/include/stdio.h
index 8727a9f..ce60fd7 100644
--- a/libc/include/stdio.h
+++ b/libc/include/stdio.h
@@ -325,6 +325,11 @@
 int	 putchar_unlocked(int);
 #endif /* __POSIX_VISIBLE >= 199506 */
 
+#if __POSIX_VISIBLE >= 200809
+FILE* fmemopen(void*, size_t, const char*);
+FILE* open_memstream(char**, size_t*);
+#endif /* __POSIX_VISIBLE >= 200809 */
+
 __END_DECLS
 
 #endif /* __BSD_VISIBLE || __POSIX_VISIBLE || __XPG_VISIBLE */
diff --git a/libc/include/sys/cachectl.h b/libc/include/sys/cachectl.h
index 57e6ae7..a302ff8 100644
--- a/libc/include/sys/cachectl.h
+++ b/libc/include/sys/cachectl.h
@@ -31,6 +31,5 @@
 #ifdef __mips__
 #include <asm/cachectl.h>
 extern int __cachectl (void *addr, __const int nbytes, __const int op);
-extern int _flush_cache (char *addr, __const int nbytes, __const int op);
 #endif
 #endif /* sys/cachectl.h */
diff --git a/libc/include/wchar.h b/libc/include/wchar.h
index 1898c7e..ae10d93 100644
--- a/libc/include/wchar.h
+++ b/libc/include/wchar.h
@@ -166,6 +166,7 @@
 extern wctrans_t wctrans(const char*);
 
 #if __POSIX_VISIBLE >= 200809
+FILE* open_wmemstream(wchar_t**, size_t*);
 wchar_t* wcsdup(const wchar_t*);
 size_t wcsnlen(const wchar_t*, size_t);
 #endif
diff --git a/libc/tools/check-symbols-glibc.py b/libc/tools/check-symbols-glibc.py
index 8326730..8bcf7fc 100755
--- a/libc/tools/check-symbols-glibc.py
+++ b/libc/tools/check-symbols-glibc.py
@@ -70,6 +70,7 @@
   '__res_mkquery': 'res_mkquery',
   '__res_query': 'res_query',
   '__res_search': 'res_search',
+  '__xpg_basename': '__gnu_basename',
 }
 
 glibc = GetSymbolsFromSystemSo('libc.so.*', 'librt.so.*', 'libpthread.so.*', 'libresolv.so.*', 'libm.so.*')
diff --git a/libc/upstream-openbsd/lib/libc/stdio/fmemopen.c b/libc/upstream-openbsd/lib/libc/stdio/fmemopen.c
new file mode 100644
index 0000000..8cda047
--- /dev/null
+++ b/libc/upstream-openbsd/lib/libc/stdio/fmemopen.c
@@ -0,0 +1,183 @@
+/*	$OpenBSD: fmemopen.c,v 1.2 2013/03/27 15:06:25 mpi 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);
+}
diff --git a/libc/upstream-openbsd/lib/libc/stdio/open_memstream.c b/libc/upstream-openbsd/lib/libc/stdio/open_memstream.c
new file mode 100644
index 0000000..4610535
--- /dev/null
+++ b/libc/upstream-openbsd/lib/libc/stdio/open_memstream.c
@@ -0,0 +1,158 @@
+/*	$OpenBSD: open_memstream.c,v 1.3 2013/04/03 03:11:53 guenther Exp $	*/
+
+/*
+ * Copyright (c) 2011 Martin Pieuchot <mpi@openbsd.org>
+ *
+ * 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 <sys/param.h>
+
+#include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "local.h"
+
+struct state {
+	char		 *string;	/* actual stream */
+	char		**pbuf;		/* point to the stream */
+	size_t		 *psize;	/* point to min(pos, len) */
+	size_t		  pos;		/* current position */
+	size_t		  size;		/* number of allocated char */
+	size_t		  len;		/* length of the data */
+};
+
+static int
+memstream_write(void *v, const char *b, int l)
+{
+	struct state	*st = v;
+	char		*p;
+	size_t		 i, end;
+
+	end = (st->pos + l);
+
+	if (end >= st->size) {
+		/* 1.6 is (very) close to the golden ratio. */
+		size_t	sz = st->size * 8 / 5;
+
+		if (sz < end + 1)
+			sz = end + 1;
+		p = realloc(st->string, sz);
+		if (!p)
+			return (-1);
+		bzero(p + st->size, sz - st->size);
+		*st->pbuf = st->string = p;
+		st->size = sz;
+	}
+
+	for (i = 0; i < l; i++)
+		st->string[st->pos + i] = b[i];
+	st->pos += l;
+
+	if (st->pos > st->len) {
+		st->len = st->pos;
+		st->string[st->len] = '\0';
+	}
+
+	*st->psize = st->pos;
+
+	return (i);
+}
+
+static fpos_t
+memstream_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 > SIZE_MAX - base || off < -base) {
+		errno = EOVERFLOW;
+		return (-1);
+	}
+
+	st->pos = base + off;
+	*st->psize = MIN(st->pos, st->len);
+
+	return (st->pos);
+}
+
+static int
+memstream_close(void *v)
+{
+	struct state	*st = v;
+
+	free(st);
+
+	return (0);
+}
+
+FILE *
+open_memstream(char **pbuf, size_t *psize)
+{
+	struct state	*st;
+	FILE		*fp;
+
+	if (pbuf == NULL || psize == NULL) {
+		errno = EINVAL;
+		return (NULL);
+	}
+
+	if ((st = malloc(sizeof(*st))) == NULL)
+		return (NULL);
+
+	if ((fp = __sfp()) == NULL) {
+		free(st);
+		return (NULL);
+	}
+
+	st->size = BUFSIZ;
+	if ((st->string = calloc(1, st->size)) == NULL) {
+		free(st);
+		fp->_flags = 0;
+		return (NULL);
+	}
+
+	*st->string = '\0';
+	st->pos = 0;
+	st->len = 0;
+	st->pbuf = pbuf;
+	st->psize = psize;
+
+	*pbuf = st->string;
+	*psize = st->len;
+
+	fp->_flags = __SWR;
+	fp->_file = -1;
+	fp->_cookie = st;
+	fp->_read = NULL;
+	fp->_write = memstream_write;
+	fp->_seek = memstream_seek;
+	fp->_close = memstream_close;
+	_SET_ORIENTATION(fp, -1);
+
+	return (fp);
+}
diff --git a/libc/upstream-openbsd/lib/libc/stdio/open_wmemstream.c b/libc/upstream-openbsd/lib/libc/stdio/open_wmemstream.c
new file mode 100644
index 0000000..9414187
--- /dev/null
+++ b/libc/upstream-openbsd/lib/libc/stdio/open_wmemstream.c
@@ -0,0 +1,169 @@
+/*	$OpenBSD: open_wmemstream.c,v 1.3 2014/03/06 07:28:21 gerhard Exp $	*/
+
+/*
+ * Copyright (c) 2011 Martin Pieuchot <mpi@openbsd.org>
+ *
+ * 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 <sys/param.h>
+
+#include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <wchar.h>
+#include "local.h"
+
+struct state {
+	wchar_t		 *string;	/* actual stream */
+	wchar_t		**pbuf;		/* point to the stream */
+	size_t		 *psize;	/* point to min(pos, len) */
+	size_t		  pos;		/* current position */
+	size_t		  size;		/* number of allocated wchar_t */
+	size_t		  len;		/* length of the data */
+	mbstate_t	  mbs;		/* conversion state of the stream */
+};
+
+static int
+wmemstream_write(void *v, const char *b, int l)
+{
+	struct state	*st = v;
+	wchar_t		*p;
+	size_t		 nmc, len, end;
+
+	end = (st->pos + l);
+
+	if (end >= st->size) {
+		/* 1.6 is (very) close to the golden ratio. */
+		size_t	sz = st->size * 8 / 5;
+
+		if (sz < end + 1)
+			sz = end + 1;
+		p = realloc(st->string, sz * sizeof(wchar_t));
+		if (!p)
+			return (-1);
+		bzero(p + st->size, (sz - st->size) * sizeof(wchar_t));
+		*st->pbuf = st->string = p;
+		st->size = sz;
+	}
+
+	nmc = (st->size - st->pos) * sizeof(wchar_t);
+	len = mbsnrtowcs(st->string + st->pos, &b, nmc, l, &st->mbs);
+	if (len == (size_t)-1)
+		return (-1);
+	st->pos += len;
+
+	if (st->pos > st->len) {
+		st->len = st->pos;
+		st->string[st->len] = L'\0';
+	}
+
+	*st->psize = st->pos;
+
+	return (len);
+}
+
+static fpos_t
+wmemstream_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 > (SIZE_MAX / sizeof(wchar_t)) - base || off < -base) {
+		errno = EOVERFLOW;
+		return (-1);
+	}
+
+	/*
+	 * XXX Clearing mbs here invalidates shift state for state-
+	 * dependent encodings, but they are not (yet) supported.
+	 */
+	bzero(&st->mbs, sizeof(st->mbs));
+
+	st->pos = base + off;
+	*st->psize = MIN(st->pos, st->len);
+
+	return (st->pos);
+}
+
+static int
+wmemstream_close(void *v)
+{
+	struct state	*st = v;
+
+	free(st);
+
+	return (0);
+}
+
+FILE *
+open_wmemstream(wchar_t **pbuf, size_t *psize)
+{
+	struct state	*st;
+	FILE		*fp;
+
+	if (pbuf == NULL || psize == NULL) {
+		errno = EINVAL;
+		return (NULL);
+	}
+
+	if ((st = malloc(sizeof(*st))) == NULL)
+		return (NULL);
+
+	if ((fp = __sfp()) == NULL) {
+		free(st);
+		return (NULL);
+	}
+
+	st->size = BUFSIZ * sizeof(wchar_t);
+	if ((st->string = calloc(1, st->size)) == NULL) {
+		free(st);
+		fp->_flags = 0;
+		return (NULL);
+	}
+
+	*st->string = L'\0';
+	st->pos = 0;
+	st->len = 0;
+	st->pbuf = pbuf;
+	st->psize = psize;
+	bzero(&st->mbs, sizeof(st->mbs));
+
+	*pbuf = st->string;
+	*psize = st->len;
+
+	fp->_flags = __SWR;
+	fp->_file = -1;
+	fp->_cookie = st;
+	fp->_read = NULL;
+	fp->_write = wmemstream_write;
+	fp->_seek = wmemstream_seek;
+	fp->_close = wmemstream_close;
+	_SET_ORIENTATION(fp, 1);
+
+	return (fp);
+}
diff --git a/linker/dlfcn.cpp b/linker/dlfcn.cpp
index 5d6db8e..e15f54d 100644
--- a/linker/dlfcn.cpp
+++ b/linker/dlfcn.cpp
@@ -232,17 +232,12 @@
 static unsigned g_libdl_chains[] = { 0, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
 #endif
 
-// Defined as global because we do not yet have access
-// to synchronization functions __cxa_guard_* needed
-// to define statics inside functions.
-static soinfo __libdl_info;
+static soinfo __libdl_info("libdl.so", nullptr);
 
 // This is used by the dynamic linker. Every process gets these symbols for free.
 soinfo* get_libdl_info() {
-  if (__libdl_info.name[0] == '\0') {
-    // initialize
-    strncpy(__libdl_info.name, "libdl.so", sizeof(__libdl_info.name));
-    __libdl_info.flags = FLAG_LINKED | FLAG_NEW_SOINFO;
+  if ((__libdl_info.flags & FLAG_LINKED) == 0) {
+    __libdl_info.flags |= FLAG_LINKED;
     __libdl_info.strtab = ANDROID_LIBDL_STRTAB;
     __libdl_info.symtab = g_libdl_symtab;
     __libdl_info.nbucket = sizeof(g_libdl_buckets)/sizeof(unsigned);
diff --git a/linker/linker.cpp b/linker/linker.cpp
index 8a7c073..6a55571 100644
--- a/linker/linker.cpp
+++ b/linker/linker.cpp
@@ -35,9 +35,10 @@
 #include <stdlib.h>
 #include <string.h>
 #include <sys/mman.h>
-#include <sys/stat.h>
 #include <unistd.h>
 
+#include <new>
+
 // Private C library headers.
 #include "private/bionic_tls.h"
 #include "private/KernelArgumentBlock.h"
@@ -290,17 +291,7 @@
     return NULL;
   }
 
-  soinfo* si = g_soinfo_allocator.alloc();
-
-  // Initialize the new element.
-  memset(si, 0, sizeof(soinfo));
-  strlcpy(si->name, name, sizeof(si->name));
-  si->flags = FLAG_NEW_SOINFO;
-
-  if (file_stat != NULL) {
-    si->set_st_dev(file_stat->st_dev);
-    si->set_st_ino(file_stat->st_ino);
-  }
+  soinfo* si = new (g_soinfo_allocator.alloc()) soinfo(name, file_stat);
 
   sonext->next = si;
   sonext = si;
@@ -466,14 +457,30 @@
   return NULL;
 }
 
-static void resolve_ifunc_symbols(soinfo* si) {
+soinfo::soinfo(const char* name, const struct stat* file_stat) {
+  memset(this, 0, sizeof(*this));
 
-  phdr_table_unprotect_segments(si->phdr, si->phnum, si->load_bias);
+  strlcpy(this->name, name, sizeof(this->name));
+  flags = FLAG_NEW_SOINFO;
+  version = SOINFO_VERSION;
+
+  if (file_stat != NULL) {
+    set_st_dev(file_stat->st_dev);
+    set_st_ino(file_stat->st_ino);
+  }
+}
+
+void soinfo::resolve_ifunc_symbols() {
+  if (!get_has_ifuncs()) {
+    return;
+  }
+
+  phdr_table_unprotect_segments(phdr, phnum, load_bias);
 
   TRACE_TYPE(IFUNC, "CHECKING FOR IFUNCS AND PERFORMING SYMBOL UPDATES");
 
-  for (size_t i = 0; i < si->nchain; ++i) {
-    ElfW(Sym)* s = &si->symtab[i];
+  for (size_t i = 0; i < nchain; ++i) {
+    ElfW(Sym)* s = &symtab[i];
     if (ELF_ST_TYPE(s->st_info) == STT_GNU_IFUNC) {
       // The address of the ifunc in the symbol table is the address of the
       // function that chooses the function to which the ifunc will refer.
@@ -481,12 +488,12 @@
       // in the linker and then return its result (minus the base offset).
       TRACE_TYPE(IFUNC, "FOUND IFUNC");
       ElfW(Addr) (*ifunc_ptr)();
-      ifunc_ptr = reinterpret_cast<ElfW(Addr)(*)()>(s->st_value + si->base);
-      s->st_value = (ifunc_ptr() - si->base);
+      ifunc_ptr = reinterpret_cast<ElfW(Addr)(*)()>(s->st_value + base);
+      s->st_value = (ifunc_ptr() - base);
       TRACE_TYPE(IFUNC, "NEW VALUE IS %p", (void*)s->st_value);
     }
   }
-  phdr_table_protect_segments(si->phdr, si->phnum, si->load_bias);
+  phdr_table_protect_segments(phdr, phnum, load_bias);
 }
 
 static unsigned elfhash(const char* _name) {
@@ -809,10 +816,6 @@
       return NULL;
     }
 
-    // if the library has any ifuncs, we will need to resolve them so that dlsym
-    // can handle them properly
-    resolve_ifunc_symbols(si);
-
     return si;
 }
 
@@ -861,7 +864,7 @@
     TRACE("unloading '%s'", si->name);
     si->CallDestructors();
 
-    if ((si->flags | FLAG_NEW_SOINFO) != 0) {
+    if (si->has_min_version(0)) {
       si->get_children().for_each([&] (soinfo* child) {
         TRACE("%s needs to unload %s", si->name, child->name);
         soinfo_unload(child);
@@ -1567,6 +1570,8 @@
   // DT_INIT should be called before DT_INIT_ARRAY if both are present.
   CallFunction("DT_INIT", init_func);
   CallArray("DT_INIT_ARRAY", init_array, init_array_count, false);
+
+  resolve_ifunc_symbols();
 }
 
 void soinfo::CallDestructors() {
@@ -1584,16 +1589,14 @@
 }
 
 void soinfo::add_child(soinfo* child) {
-  if ((this->flags & FLAG_NEW_SOINFO) == 0) {
-    return;
+  if (has_min_version(0)) {
+    this->children.push_front(child);
+    child->parents.push_front(this);
   }
-
-  this->children.push_front(child);
-  child->parents.push_front(this);
 }
 
 void soinfo::remove_all_links() {
-  if ((this->flags & FLAG_NEW_SOINFO) == 0) {
+  if (!has_min_version(0)) {
     return;
   }
 
@@ -1616,51 +1619,45 @@
 }
 
 void soinfo::set_st_dev(dev_t dev) {
-  if ((this->flags & FLAG_NEW_SOINFO) == 0) {
-    return;
+  if (has_min_version(0)) {
+    st_dev = dev;
   }
-
-  st_dev = dev;
 }
 
 void soinfo::set_st_ino(ino_t ino) {
-  if ((this->flags & FLAG_NEW_SOINFO) == 0) {
-    return;
+  if (has_min_version(0)) {
+    st_ino = ino;
   }
-
-  st_ino = ino;
 }
 
 void soinfo::set_has_ifuncs(bool ifuncs) {
-  if ((this->flags & FLAG_NEW_SOINFO) == 0) {
-    return;
+  if (has_min_version(1)) {
+    has_ifuncs = ifuncs;
   }
-
-  has_ifuncs = ifuncs;
 }
 
 dev_t soinfo::get_st_dev() {
-  if ((this->flags & FLAG_NEW_SOINFO) == 0) {
-    return 0;
+  if (has_min_version(0)) {
+    return st_dev;
   }
 
-  return st_dev;
+  return 0;
 };
 
 ino_t soinfo::get_st_ino() {
-  if ((this->flags & FLAG_NEW_SOINFO) == 0) {
-    return 0;
+  if (has_min_version(0)) {
+    return st_ino;
   }
 
-  return st_ino;
+  return 0;
 }
 
 bool soinfo::get_has_ifuncs() {
-  if ((this->flags & FLAG_NEW_SOINFO) == 0) {
-    return false;
+  if (has_min_version(1)) {
+    return has_ifuncs;
   }
 
-  return has_ifuncs;
+  return false;
 }
 
 // This is a return on get_children() in case
@@ -1668,11 +1665,11 @@
 static soinfo::soinfo_list_t g_empty_list;
 
 soinfo::soinfo_list_t& soinfo::get_children() {
-  if ((this->flags & FLAG_NEW_SOINFO) == 0) {
-    return g_empty_list;
+  if (has_min_version(0)) {
+    return this->children;
   }
 
-  return this->children;
+  return g_empty_list;
 }
 
 /* Force any of the closed stdin, stdout and stderr to be associated with
@@ -2134,7 +2131,12 @@
 /*
  * This is linker soinfo for GDB. See details below.
  */
-static soinfo linker_soinfo_for_gdb;
+#if defined(__LP64__)
+#define LINKER_PATH "/system/bin/linker64"
+#else
+#define LINKER_PATH "/system/bin/linker"
+#endif
+static soinfo linker_soinfo_for_gdb(LINKER_PATH, nullptr);
 
 /* gdb expects the linker to be in the debug shared object list.
  * Without this, gdb has trouble locating the linker's ".text"
@@ -2144,12 +2146,6 @@
  * be on the soinfo list.
  */
 static void init_linker_info_for_gdb(ElfW(Addr) linker_base) {
-#if defined(__LP64__)
-  strlcpy(linker_soinfo_for_gdb.name, "/system/bin/linker64", sizeof(linker_soinfo_for_gdb.name));
-#else
-  strlcpy(linker_soinfo_for_gdb.name, "/system/bin/linker", sizeof(linker_soinfo_for_gdb.name));
-#endif
-  linker_soinfo_for_gdb.flags = FLAG_NEW_SOINFO;
   linker_soinfo_for_gdb.base = linker_base;
 
   /*
@@ -2368,10 +2364,6 @@
  * function, or other GOT reference will generate a segfault.
  */
 extern "C" ElfW(Addr) __linker_init(void* raw_args) {
-  // Initialize static variables.
-  solist = get_libdl_info();
-  sonext = get_libdl_info();
-
   KernelArgumentBlock args(raw_args);
 
   ElfW(Addr) linker_addr = args.getauxval(AT_BASE);
@@ -2379,8 +2371,7 @@
   ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_addr);
   ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_addr + elf_hdr->e_phoff);
 
-  soinfo linker_so;
-  memset(&linker_so, 0, sizeof(soinfo));
+  soinfo linker_so("[dynamic linker]", nullptr);
 
   // If the linker is not acting as PT_INTERP entry_point is equal to
   // _start. Which means that the linker is running as an executable and
@@ -2392,7 +2383,6 @@
     __libc_fatal("This is %s, the helper program for shared library executables.\n", args.argv[0]);
   }
 
-  strcpy(linker_so.name, "[dynamic linker]");
   linker_so.base = linker_addr;
   linker_so.size = phdr_table_get_load_size(phdr, elf_hdr->e_phnum);
   linker_so.load_bias = get_elf_exec_load_bias(elf_hdr);
@@ -2416,6 +2406,13 @@
   // Initialize the linker's own global variables
   linker_so.CallConstructors();
 
+  // Initialize static variables. Note that in order to
+  // get correct libdl_info we need to call constructors
+  // before get_libdl_info().
+  solist = get_libdl_info();
+  sonext = get_libdl_info();
+
+
   // We have successfully fixed our own relocations. It's safe to run
   // the main part of the linker now.
   args.abort_message_ptr = &g_abort_message;
diff --git a/linker/linker.h b/linker/linker.h
index d7cf24b..5e21f70 100644
--- a/linker/linker.h
+++ b/linker/linker.h
@@ -30,6 +30,7 @@
 #define _LINKER_H_
 
 #include <elf.h>
+#include <inttypes.h>
 #include <link.h>
 #include <unistd.h>
 #include <android/dlext.h>
@@ -88,6 +89,8 @@
 #define FLAG_LINKER     0x00000010 // The linker itself
 #define FLAG_NEW_SOINFO 0x40000000 // new soinfo format
 
+#define SOINFO_VERSION 1
+
 #define SOINFO_NAME_LEN 128
 
 typedef void (*linker_function_t)();
@@ -195,6 +198,9 @@
   bool has_text_relocations;
 #endif
   bool has_DT_SYMBOLIC;
+
+  soinfo(const char* name, const struct stat* file_stat);
+
   void CallConstructors();
   void CallDestructors();
   void CallPreInitConstructors();
@@ -209,21 +215,22 @@
   dev_t get_st_dev();
   bool get_has_ifuncs();
 
-
-
   soinfo_list_t& get_children();
 
+  bool inline has_min_version(uint32_t min_version) {
+    return (flags & FLAG_NEW_SOINFO) != 0 && version >= min_version;
+  }
  private:
   void CallArray(const char* array_name, linker_function_t* functions, size_t count, bool reverse);
   void CallFunction(const char* function_name, linker_function_t function);
+  void resolve_ifunc_symbols();
 
  private:
   // This part of the structure is only available
   // when FLAG_NEW_SOINFO is set in this->flags.
-  unsigned int version;
+  uint32_t version;
 
-  bool has_ifuncs;
-
+  // version >= 0
   dev_t st_dev;
   ino_t st_ino;
 
@@ -231,6 +238,8 @@
   soinfo_list_t children;
   soinfo_list_t parents;
 
+  // version >= 1
+  bool has_ifuncs;
 };
 
 extern soinfo* get_libdl_info();
diff --git a/tests/Android.mk b/tests/Android.mk
index 0cf9f6d..13d7cbe 100644
--- a/tests/Android.mk
+++ b/tests/Android.mk
@@ -130,6 +130,7 @@
 
 libBionicStandardTests_c_includes := \
     bionic/libc \
+    external/tinyxml2 \
 
 libBionicStandardTests_ldlibs_host := \
     -lrt \
@@ -244,6 +245,10 @@
 bionic-unit-tests_whole_static_libraries := \
     libBionicTests \
 
+bionic-unit-tests_static_libraries := \
+    libtinyxml2 \
+    liblog \
+
 bionic-unit-tests_src_files := \
     atexit_test.cpp \
     dlext_test.cpp \
@@ -282,6 +287,8 @@
     libm \
     libc \
     libstdc++ \
+    libtinyxml2 \
+    liblog \
 
 bionic-unit-tests-static_force_static_executable := true
 
@@ -357,6 +364,22 @@
 		$(TARGET_OUT_DATA_NATIVE_TESTS)/bionic-unit-tests/bionic-unit-tests$(NATIVE_TEST_SUFFIX) $(BIONIC_TEST_FLAGS)
 endif
 
+ifeq ($(TARGET_ARCH),$(filter $(TARGET_ARCH),x86_64))
+# add target to run lp32 tests
+bionic-unit-tests-run-on-host32: bionic-unit-tests $(TARGET_OUT_EXECUTABLES)/$(LINKER) $(TARGET_OUT_EXECUTABLES)/sh
+	if [ ! -d /system -o ! -d /system/bin ]; then \
+	  echo "Attempting to create /system/bin"; \
+	  sudo mkdir -p -m 0777 /system/bin; \
+	fi
+	mkdir -p $(TARGET_OUT_DATA)/local/tmp
+	cp $(TARGET_OUT_EXECUTABLES)/linker /system/bin
+	cp $(TARGET_OUT_EXECUTABLES)/sh /system/bin
+	ANDROID_DATA=$(TARGET_OUT_DATA) \
+	ANDROID_ROOT=$(TARGET_OUT) \
+	LD_LIBRARY_PATH=$(2ND_TARGET_OUT_SHARED_LIBRARIES) \
+		$(2ND_TARGET_OUT_DATA_NATIVE_TESTS)/bionic-unit-tests/bionic-unit-tests32 $(BIONIC_TEST_FLAGS)
+endif
+
 endif # linux-x86
 
 include $(call first-makefiles-under,$(LOCAL_PATH))
diff --git a/tests/dlfcn_test.cpp b/tests/dlfcn_test.cpp
index fc788ac..a731748 100644
--- a/tests/dlfcn_test.cpp
+++ b/tests/dlfcn_test.cpp
@@ -90,32 +90,42 @@
 // ifuncs are only supported on intel for now
 #if defined(__i386__) || defined(__x86_64__)
 TEST(dlfcn, ifunc) {
-  const char* (*foo_ptr)();
-  const char* (*foo_library_ptr)();
+  typedef const char* (*fn_ptr)();
 
   // ifunc's choice depends on whether IFUNC_CHOICE has a value
   // first check the set case
   setenv("IFUNC_CHOICE", "set", 1);
   void* handle = dlopen("libtest_ifunc.so", RTLD_NOW);
   ASSERT_TRUE(handle != NULL);
-  *(void **)(&foo_ptr) = dlsym(handle, "foo");
-  *(void **)(&foo_library_ptr) = dlsym(handle, "foo_library");
+  fn_ptr foo_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo"));
+  fn_ptr foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
   ASSERT_TRUE(foo_ptr != NULL);
   ASSERT_TRUE(foo_library_ptr != NULL);
-  ASSERT_EQ(strncmp("set", (*foo_ptr)(), 3), 0);
-  ASSERT_EQ(strncmp("set", (*foo_library_ptr)(), 3), 0);
+  ASSERT_EQ(strncmp("set", foo_ptr(), 3), 0);
+  ASSERT_EQ(strncmp("set", foo_library_ptr(), 3), 0);
   dlclose(handle);
 
   // then check the unset case
   unsetenv("IFUNC_CHOICE");
   handle = dlopen("libtest_ifunc.so", RTLD_NOW);
   ASSERT_TRUE(handle != NULL);
-  *(void **)(&foo_ptr) = dlsym(handle, "foo");
-  *(void **)(&foo_library_ptr) = dlsym(handle, "foo_library");
+  foo_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo"));
+  foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
   ASSERT_TRUE(foo_ptr != NULL);
   ASSERT_TRUE(foo_library_ptr != NULL);
-  ASSERT_EQ(strncmp("unset", (*foo_ptr)(), 5), 0);
-  ASSERT_EQ(strncmp("unset", (*foo_library_ptr)(), 3), 0);
+  ASSERT_EQ(strncmp("unset", foo_ptr(), 5), 0);
+  ASSERT_EQ(strncmp("unset", foo_library_ptr(), 3), 0);
+  dlclose(handle);
+}
+
+TEST(dlfcn, ifunc_ctor_call) {
+  typedef const char* (*fn_ptr)();
+
+  void* handle = dlopen("libtest_ifunc.so", RTLD_NOW);
+  ASSERT_TRUE(handle != NULL) << dlerror();
+  fn_ptr is_ctor_called =  reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called"));
+  ASSERT_TRUE(is_ctor_called != NULL) << dlerror();
+  ASSERT_STREQ("true", is_ctor_called());
   dlclose(handle);
 }
 #endif
diff --git a/tests/libs/dlopen_testlib_ifunc.c b/tests/libs/dlopen_testlib_ifunc.c
index 1c4bafa..4874841 100644
--- a/tests/libs/dlopen_testlib_ifunc.c
+++ b/tests/libs/dlopen_testlib_ifunc.c
@@ -17,7 +17,22 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+static int g_flag = 0;
+
+static void __attribute__((constructor)) init_flag() {
+  g_flag = 1;
+}
+
 const char* foo() __attribute__ ((ifunc ("foo_ifunc")));
+const char* is_ctor_called() __attribute__ ((ifunc("is_ctor_called_ifun")));
+
+const char* return_true() {
+  return "true";
+}
+
+const char* return_false() {
+  return "false";
+}
 
 const char* f1() {
   return "unset";
@@ -27,6 +42,10 @@
   return "set";
 }
 
+void* is_ctor_called_ifun() {
+  return g_flag == 0 ? return_false : return_true;
+}
+
 void* foo_ifunc() {
    char* choice = getenv("IFUNC_CHOICE");
    return choice == NULL ? f1 : f2;
@@ -34,4 +53,4 @@
 
 const char* foo_library() {
    return foo();
-}
\ No newline at end of file
+}
diff --git a/tests/malloc_test.cpp b/tests/malloc_test.cpp
index 6b7a28b..b76625a 100644
--- a/tests/malloc_test.cpp
+++ b/tests/malloc_test.cpp
@@ -22,6 +22,8 @@
 #include <malloc.h>
 #include <unistd.h>
 
+#include <tinyxml2.h>
+
 #include "private/bionic_config.h"
 
 TEST(malloc, malloc_std) {
@@ -322,3 +324,51 @@
   ASSERT_EQ(NULL, valloc(SIZE_MAX));
 }
 #endif
+
+TEST(malloc, malloc_info) {
+#ifdef __BIONIC__
+  char* buf;
+  size_t bufsize;
+  FILE* memstream = open_memstream(&buf, &bufsize);
+  ASSERT_NE(nullptr, memstream);
+  ASSERT_EQ(0, malloc_info(0, memstream));
+  ASSERT_EQ(0, fclose(memstream));
+
+  tinyxml2::XMLDocument doc;
+  ASSERT_EQ(tinyxml2::XML_SUCCESS, doc.Parse(buf));
+
+  auto root = doc.FirstChildElement();
+  ASSERT_NE(nullptr, root);
+  ASSERT_STREQ("malloc", root->Name());
+  ASSERT_STREQ("jemalloc-1", root->Attribute("version"));
+
+  auto arena = root->FirstChildElement();
+  for (; arena != nullptr; arena = arena->NextSiblingElement()) {
+    int val;
+
+    ASSERT_STREQ("heap", arena->Name());
+    ASSERT_EQ(tinyxml2::XML_SUCCESS, arena->QueryIntAttribute("nr", &val));
+    ASSERT_EQ(tinyxml2::XML_SUCCESS,
+              arena->FirstChildElement("allocated-large")->QueryIntText(&val));
+    ASSERT_EQ(tinyxml2::XML_SUCCESS,
+              arena->FirstChildElement("allocated-huge")->QueryIntText(&val));
+    ASSERT_EQ(tinyxml2::XML_SUCCESS,
+              arena->FirstChildElement("allocated-bins")->QueryIntText(&val));
+    ASSERT_EQ(tinyxml2::XML_SUCCESS,
+              arena->FirstChildElement("bins-total")->QueryIntText(&val));
+
+    auto bin = arena->FirstChildElement("bin");
+    for (; bin != nullptr; bin = bin ->NextSiblingElement()) {
+      if (strcmp(bin->Name(), "bin") == 0) {
+        ASSERT_EQ(tinyxml2::XML_SUCCESS, bin->QueryIntAttribute("nr", &val));
+        ASSERT_EQ(tinyxml2::XML_SUCCESS,
+                  bin->FirstChildElement("allocated")->QueryIntText(&val));
+        ASSERT_EQ(tinyxml2::XML_SUCCESS,
+                  bin->FirstChildElement("nmalloc")->QueryIntText(&val));
+        ASSERT_EQ(tinyxml2::XML_SUCCESS,
+                  bin->FirstChildElement("ndalloc")->QueryIntText(&val));
+      }
+    }
+  }
+#endif
+}
diff --git a/tests/stdio_test.cpp b/tests/stdio_test.cpp
index bb86509..2cd0df2 100644
--- a/tests/stdio_test.cpp
+++ b/tests/stdio_test.cpp
@@ -676,3 +676,82 @@
 
   fclose(fp);
 }
+
+TEST(stdio, fmemopen) {
+  char buf[16];
+  memset(buf, 0, sizeof(buf));
+  FILE* fp = fmemopen(buf, sizeof(buf), "r+");
+  ASSERT_EQ('<', fputc('<', fp));
+  ASSERT_NE(EOF, fputs("abc>\n", fp));
+  fflush(fp);
+
+  ASSERT_STREQ("<abc>\n", buf);
+
+  rewind(fp);
+
+  char line[16];
+  char* s = fgets(line, sizeof(line), fp);
+  ASSERT_TRUE(s != NULL);
+  ASSERT_STREQ("<abc>\n", s);
+
+  fclose(fp);
+}
+
+TEST(stdio, fmemopen_NULL) {
+  FILE* fp = fmemopen(nullptr, 128, "r+");
+  ASSERT_NE(EOF, fputs("xyz\n", fp));
+
+  rewind(fp);
+
+  char line[16];
+  char* s = fgets(line, sizeof(line), fp);
+  ASSERT_TRUE(s != NULL);
+  ASSERT_STREQ("xyz\n", s);
+
+  fclose(fp);
+}
+
+TEST(stdio, fmemopen_EINVAL) {
+  char buf[16];
+
+  // Invalid size.
+  errno = 0;
+  ASSERT_EQ(nullptr, fmemopen(buf, 0, "r+"));
+  ASSERT_EQ(EINVAL, errno);
+
+  // No '+' with NULL buffer.
+  errno = 0;
+  ASSERT_EQ(nullptr, fmemopen(nullptr, 0, "r"));
+  ASSERT_EQ(EINVAL, errno);
+}
+
+TEST(stdio, open_memstream) {
+  char* p = nullptr;
+  size_t size = 0;
+  FILE* fp = open_memstream(&p, &size);
+  ASSERT_NE(EOF, fputs("hello, world!", fp));
+  fclose(fp);
+
+  ASSERT_STREQ("hello, world!", p);
+  ASSERT_EQ(strlen("hello, world!"), size);
+  free(p);
+}
+
+TEST(stdio, open_memstream_EINVAL) {
+#if defined(__BIONIC__)
+  char* p;
+  size_t size;
+
+  // Invalid buffer.
+  errno = 0;
+  ASSERT_EQ(nullptr, open_memstream(nullptr, &size));
+  ASSERT_EQ(EINVAL, errno);
+
+  // Invalid size.
+  errno = 0;
+  ASSERT_EQ(nullptr, open_memstream(&p, nullptr));
+  ASSERT_EQ(EINVAL, errno);
+#else
+  GTEST_LOG_(INFO) << "This test does nothing.\n";
+#endif
+}
diff --git a/tests/unistd_test.cpp b/tests/unistd_test.cpp
index 2a65657..8195ea8 100644
--- a/tests/unistd_test.cpp
+++ b/tests/unistd_test.cpp
@@ -431,6 +431,10 @@
 
   void* child_stack[1024];
   int clone_result = clone(GetPidCachingCloneStartRoutine, &child_stack[1024], CLONE_NEWNS | SIGCHLD, NULL);
+  if (clone_result == -1 && errno == EPERM && getuid() != 0) {
+    GTEST_LOG_(INFO) << "This test only works if you have permission to CLONE_NEWNS; try running as root.\n";
+    return;
+  }
   ASSERT_NE(clone_result, -1);
 
   ASSERT_EQ(parent_pid, getpid());
diff --git a/tests/wchar_test.cpp b/tests/wchar_test.cpp
index d02c4bf..760475f 100644
--- a/tests/wchar_test.cpp
+++ b/tests/wchar_test.cpp
@@ -489,3 +489,34 @@
   EXPECT_EQ(4U, n);
   EXPECT_EQ(L'𤭢', wc);
 }
+
+TEST(wchar, open_wmemstream) {
+  wchar_t* p = nullptr;
+  size_t size = 0;
+  FILE* fp = open_wmemstream(&p, &size);
+  ASSERT_NE(EOF, fputws(L"hello, world!", fp));
+  fclose(fp);
+
+  ASSERT_STREQ(L"hello, world!", p);
+  ASSERT_EQ(wcslen(L"hello, world!"), size);
+  free(p);
+}
+
+TEST(stdio, open_wmemstream_EINVAL) {
+#if defined(__BIONIC__)
+  wchar_t* p;
+  size_t size;
+
+  // Invalid buffer.
+  errno = 0;
+  ASSERT_EQ(nullptr, open_wmemstream(nullptr, &size));
+  ASSERT_EQ(EINVAL, errno);
+
+  // Invalid size.
+  errno = 0;
+  ASSERT_EQ(nullptr, open_wmemstream(&p, nullptr));
+  ASSERT_EQ(EINVAL, errno);
+#else
+  GTEST_LOG_(INFO) << "This test does nothing.\n";
+#endif
+}