Merge "Load /dev/linker/ld.config.txt by default"
diff --git a/docs/native_allocator.md b/docs/native_allocator.md
index 7695470..adfa6ef 100644
--- a/docs/native_allocator.md
+++ b/docs/native_allocator.md
@@ -100,6 +100,19 @@
 The allocation tests are not meant to be complete, so it is expected
 that a native allocator will have its own set of tests that can be run.
 
+### Libmemunreachable Tests
+The libmemunreachable tests verify that the iterator functions are working
+properly.
+
+To run all of the tests:
+
+    adb shell /data/nativetest64/memunreachable_binder_test/memunreachable_binder_test
+    adb shell /data/nativetest/memunreachable_binder_test/memunreachable_binder_test
+    adb shell /data/nativetest64/memunreachable_test/memunreachable_test
+    adb shell /data/nativetest/memunreachable_test/memunreachable_test
+    adb shell /data/nativetest64/memunreachable_unit_test/memunreachable_unit_test
+    adb shell /data/nativetest/memunreachable_unit_test/memunreachable_unit_test
+
 ### CTS Entropy Test
 In addition to the bionic tests, there is also a CTS test that is designed
 to verify that the addresses returned by malloc are sufficiently randomized
diff --git a/libm/upstream-freebsd/lib/msun/src/e_exp.c b/libm/upstream-freebsd/lib/msun/src/e_exp.c
index 94c9769..dd88d3e 100644
--- a/libm/upstream-freebsd/lib/msun/src/e_exp.c
+++ b/libm/upstream-freebsd/lib/msun/src/e_exp.c
@@ -11,7 +11,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD$");
+__FBSDID("$FreeBSD: head/lib/msun/src/e_exp.c 352710 2019-09-25 18:50:57Z dim $");
 
 /* __ieee754_exp(x)
  * Returns the exponential of x.
@@ -145,9 +145,9 @@
     /* x is now in primary range */
 	t  = x*x;
 	if(k >= -1021)
-	    INSERT_WORDS(twopk,0x3ff00000+(k<<20), 0);
+	    INSERT_WORDS(twopk,((u_int32_t)(0x3ff+k))<<20, 0);
 	else
-	    INSERT_WORDS(twopk,0x3ff00000+((k+1000)<<20), 0);
+	    INSERT_WORDS(twopk,((u_int32_t)(0x3ff+(k+1000)))<<20, 0);
 	c  = x - t*(P1+t*(P2+t*(P3+t*(P4+t*P5))));
 	if(k==0) 	return one-((x*c)/(c-2.0)-x); 
 	else 		y = one-((lo-(x*c)/(2.0-c))-hi);
diff --git a/libm/upstream-freebsd/lib/msun/src/e_expf.c b/libm/upstream-freebsd/lib/msun/src/e_expf.c
index b1fe2c5..aaf6d5f 100644
--- a/libm/upstream-freebsd/lib/msun/src/e_expf.c
+++ b/libm/upstream-freebsd/lib/msun/src/e_expf.c
@@ -14,7 +14,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD$");
+__FBSDID("$FreeBSD: head/lib/msun/src/e_expf.c 352710 2019-09-25 18:50:57Z dim $");
 
 #include <float.h>
 
@@ -83,9 +83,9 @@
     /* x is now in primary range */
 	t  = x*x;
 	if(k >= -125)
-	    SET_FLOAT_WORD(twopk,0x3f800000+(k<<23));
+	    SET_FLOAT_WORD(twopk,((u_int32_t)(0x7f+k))<<23);
 	else
-	    SET_FLOAT_WORD(twopk,0x3f800000+((k+100)<<23));
+	    SET_FLOAT_WORD(twopk,((u_int32_t)(0x7f+(k+100)))<<23);
 	c  = x - t*(P1+t*P2);
 	if(k==0) 	return one-((x*c)/(c-(float)2.0)-x);
 	else 		y = one-((lo-(x*c)/((float)2.0-c))-hi);
diff --git a/libm/upstream-freebsd/lib/msun/src/e_j0.c b/libm/upstream-freebsd/lib/msun/src/e_j0.c
index 6bca542..a008b42 100644
--- a/libm/upstream-freebsd/lib/msun/src/e_j0.c
+++ b/libm/upstream-freebsd/lib/msun/src/e_j0.c
@@ -11,7 +11,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/lib/msun/src/e_j0.c 343023 2019-01-14 15:48:35Z pfg $");
+__FBSDID("$FreeBSD: head/lib/msun/src/e_j0.c 343953 2019-02-10 08:46:07Z peterj $");
 
 /* __ieee754_j0(x), __ieee754_y0(x)
  * Bessel function of the first and second kinds of order zero.
@@ -93,8 +93,7 @@
 	if(ix>=0x7ff00000) return one/(x*x);
 	x = fabs(x);
 	if(ix >= 0x40000000) {	/* |x| >= 2.0 */
-		s = sin(x);
-		c = cos(x);
+		sincos(x, &s, &c);
 		ss = s-c;
 		cc = s+c;
 		if(ix<0x7fe00000) {  /* Make sure x+x does not overflow. */
@@ -173,8 +172,7 @@
          *              sin(x) +- cos(x) = -cos(2x)/(sin(x) -+ cos(x))
          * to compute the worse one.
          */
-                s = sin(x);
-                c = cos(x);
+                sincos(x, &s, &c);
                 ss = s-c;
                 cc = s+c;
 	/*
diff --git a/libm/upstream-freebsd/lib/msun/src/e_j0f.c b/libm/upstream-freebsd/lib/msun/src/e_j0f.c
index 714caac..495a5e5 100644
--- a/libm/upstream-freebsd/lib/msun/src/e_j0f.c
+++ b/libm/upstream-freebsd/lib/msun/src/e_j0f.c
@@ -14,7 +14,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/lib/msun/src/e_j0f.c 343023 2019-01-14 15:48:35Z pfg $");
+__FBSDID("$FreeBSD: head/lib/msun/src/e_j0f.c 343953 2019-02-10 08:46:07Z peterj $");
 
 /*
  * See e_j0.c for complete comments.
@@ -55,8 +55,7 @@
 	if(ix>=0x7f800000) return one/(x*x);
 	x = fabsf(x);
 	if(ix >= 0x40000000) {	/* |x| >= 2.0 */
-		s = sinf(x);
-		c = cosf(x);
+		sincosf(x, &s, &c);
 		ss = s-c;
 		cc = s+c;
 		if(ix<0x7f000000) {  /* Make sure x+x does not overflow. */
@@ -128,8 +127,7 @@
          *              sin(x) +- cos(x) = -cos(2x)/(sin(x) -+ cos(x))
          * to compute the worse one.
          */
-                s = sinf(x);
-                c = cosf(x);
+                sincosf(x, &s, &c);
                 ss = s-c;
                 cc = s+c;
 	/*
diff --git a/libm/upstream-freebsd/lib/msun/src/e_j1.c b/libm/upstream-freebsd/lib/msun/src/e_j1.c
index 78bb329..6d1c41c 100644
--- a/libm/upstream-freebsd/lib/msun/src/e_j1.c
+++ b/libm/upstream-freebsd/lib/msun/src/e_j1.c
@@ -11,7 +11,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/lib/msun/src/e_j1.c 336089 2018-07-08 16:26:13Z markj $");
+__FBSDID("$FreeBSD: head/lib/msun/src/e_j1.c 343953 2019-02-10 08:46:07Z peterj $");
 
 /* __ieee754_j1(x), __ieee754_y1(x)
  * Bessel function of the first and second kinds of order zero.
@@ -94,8 +94,7 @@
 	if(ix>=0x7ff00000) return one/x;
 	y = fabs(x);
 	if(ix >= 0x40000000) {	/* |x| >= 2.0 */
-		s = sin(y);
-		c = cos(y);
+		sincos(y, &s, &c);
 		ss = -s-c;
 		cc = s-c;
 		if(ix<0x7fe00000) {  /* make sure y+y not overflow */
@@ -159,8 +158,7 @@
 	/* y1(x<0) = NaN and raise invalid exception. */
         if(hx<0) return vzero/vzero;
         if(ix >= 0x40000000) {  /* |x| >= 2.0 */
-                s = sin(x);
-                c = cos(x);
+                sincos(x, &s, &c);
                 ss = -s-c;
                 cc = s-c;
                 if(ix<0x7fe00000) {  /* make sure x+x not overflow */
diff --git a/libm/upstream-freebsd/lib/msun/src/e_j1f.c b/libm/upstream-freebsd/lib/msun/src/e_j1f.c
index 3abe201..31fc28a 100644
--- a/libm/upstream-freebsd/lib/msun/src/e_j1f.c
+++ b/libm/upstream-freebsd/lib/msun/src/e_j1f.c
@@ -14,7 +14,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/lib/msun/src/e_j1f.c 336089 2018-07-08 16:26:13Z markj $");
+__FBSDID("$FreeBSD: head/lib/msun/src/e_j1f.c 343953 2019-02-10 08:46:07Z peterj $");
 
 /*
  * See e_j1.c for complete comments.
@@ -56,8 +56,7 @@
 	if(ix>=0x7f800000) return one/x;
 	y = fabsf(x);
 	if(ix >= 0x40000000) {	/* |x| >= 2.0 */
-		s = sinf(y);
-		c = cosf(y);
+		sincosf(y, &s, &c);
 		ss = -s-c;
 		cc = s-c;
 		if(ix<0x7f000000) {  /* make sure y+y not overflow */
@@ -114,8 +113,7 @@
 	if(ix==0) return -one/vzero;
 	if(hx<0) return vzero/vzero;
         if(ix >= 0x40000000) {  /* |x| >= 2.0 */
-                s = sinf(x);
-                c = cosf(x);
+                sincosf(x, &s, &c);
                 ss = -s-c;
                 cc = s-c;
                 if(ix<0x7f000000) {  /* make sure x+x not overflow */
diff --git a/libm/upstream-freebsd/lib/msun/src/e_jn.c b/libm/upstream-freebsd/lib/msun/src/e_jn.c
index 58ec905..c039cf2 100644
--- a/libm/upstream-freebsd/lib/msun/src/e_jn.c
+++ b/libm/upstream-freebsd/lib/msun/src/e_jn.c
@@ -11,7 +11,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/lib/msun/src/e_jn.c 336089 2018-07-08 16:26:13Z markj $");
+__FBSDID("$FreeBSD: head/lib/msun/src/e_jn.c 343953 2019-02-10 08:46:07Z peterj $");
 
 /*
  * __ieee754_jn(n, x), __ieee754_yn(n, x)
@@ -54,7 +54,7 @@
 __ieee754_jn(int n, double x)
 {
 	int32_t i,hx,ix,lx, sgn;
-	double a, b, temp, di;
+	double a, b, c, s, temp, di;
 	double z, w;
 
     /* J(-n,x) = (-1)^n * J(n, x), J(n, -x) = (-1)^n * J(n, x)
@@ -91,11 +91,12 @@
      *		   2	-s+c		-c-s
      *		   3	 s+c		 c-s
      */
+		sincos(x, &s, &c);
 		switch(n&3) {
-		    case 0: temp =  cos(x)+sin(x); break;
-		    case 1: temp = -cos(x)+sin(x); break;
-		    case 2: temp = -cos(x)-sin(x); break;
-		    case 3: temp =  cos(x)-sin(x); break;
+		    case 0: temp =  c+s; break;
+		    case 1: temp = -c+s; break;
+		    case 2: temp = -c-s; break;
+		    case 3: temp =  c-s; break;
 		}
 		b = invsqrtpi*temp/sqrt(x);
 	    } else {
@@ -216,7 +217,7 @@
 {
 	int32_t i,hx,ix,lx;
 	int32_t sign;
-	double a, b, temp;
+	double a, b, c, s, temp;
 
 	EXTRACT_WORDS(hx,lx,x);
 	ix = 0x7fffffff&hx;
@@ -248,11 +249,12 @@
      *		   2	-s+c		-c-s
      *		   3	 s+c		 c-s
      */
+		sincos(x, &s, &c);
 		switch(n&3) {
-		    case 0: temp =  sin(x)-cos(x); break;
-		    case 1: temp = -sin(x)-cos(x); break;
-		    case 2: temp = -sin(x)+cos(x); break;
-		    case 3: temp =  sin(x)+cos(x); break;
+		    case 0: temp =  s-c; break;
+		    case 1: temp = -s-c; break;
+		    case 2: temp = -s+c; break;
+		    case 3: temp =  s+c; break;
 		}
 		b = invsqrtpi*temp/sqrt(x);
 	} else {
diff --git a/libm/upstream-freebsd/lib/msun/src/s_expm1.c b/libm/upstream-freebsd/lib/msun/src/s_expm1.c
index 37998a3..21256ce 100644
--- a/libm/upstream-freebsd/lib/msun/src/s_expm1.c
+++ b/libm/upstream-freebsd/lib/msun/src/s_expm1.c
@@ -11,7 +11,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD$");
+__FBSDID("$FreeBSD: head/lib/msun/src/s_expm1.c 352710 2019-09-25 18:50:57Z dim $");
 
 /* expm1(x)
  * Returns exp(x)-1, the exponential of x minus 1.
@@ -188,7 +188,7 @@
 	e  = hxs*((r1-t)/(6.0 - x*t));
 	if(k==0) return x - (x*e-hxs);		/* c is 0 */
 	else {
-	    INSERT_WORDS(twopk,0x3ff00000+(k<<20),0);	/* 2^k */
+	    INSERT_WORDS(twopk,((u_int32_t)(0x3ff+k))<<20,0);	/* 2^k */
 	    e  = (x*(e-c)-c);
 	    e -= hxs;
 	    if(k== -1) return 0.5*(x-e)-0.5;
diff --git a/libm/upstream-freebsd/lib/msun/src/s_expm1f.c b/libm/upstream-freebsd/lib/msun/src/s_expm1f.c
index c0a3934..4d9ab36 100644
--- a/libm/upstream-freebsd/lib/msun/src/s_expm1f.c
+++ b/libm/upstream-freebsd/lib/msun/src/s_expm1f.c
@@ -14,7 +14,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD$");
+__FBSDID("$FreeBSD: head/lib/msun/src/s_expm1f.c 352710 2019-09-25 18:50:57Z dim $");
 
 #include <float.h>
 
@@ -94,7 +94,7 @@
 	e  = hxs*((r1-t)/((float)6.0 - x*t));
 	if(k==0) return x - (x*e-hxs);		/* c is 0 */
 	else {
-	    SET_FLOAT_WORD(twopk,0x3f800000+(k<<23));	/* 2^k */
+	    SET_FLOAT_WORD(twopk,((u_int32_t)(0x7f+k))<<23);	/* 2^k */
 	    e  = (x*(e-c)-c);
 	    e -= hxs;
 	    if(k== -1) return (float)0.5*(x-e)-(float)0.5;
diff --git a/libm/upstream-freebsd/lib/msun/src/s_lround.c b/libm/upstream-freebsd/lib/msun/src/s_lround.c
index b6a25b7..ce2aee7 100644
--- a/libm/upstream-freebsd/lib/msun/src/s_lround.c
+++ b/libm/upstream-freebsd/lib/msun/src/s_lround.c
@@ -32,7 +32,7 @@
 #include <math.h>
 
 #ifndef type
-__FBSDID("$FreeBSD: head/lib/msun/src/s_lround.c 326219 2017-11-26 02:00:33Z pfg $");
+__FBSDID("$FreeBSD: head/lib/msun/src/s_lround.c 353329 2019-10-08 21:39:51Z brooks $");
 #define type		double
 #define	roundit		round
 #define dtype		long
@@ -49,9 +49,9 @@
  * that everything is in range.  At compile time, INRANGE(x) should reduce to
  * two floating-point comparisons in the former case, or TRUE otherwise.
  */
-static const type dtype_min = DTYPE_MIN - 0.5;
-static const type dtype_max = DTYPE_MAX + 0.5;
-#define	INRANGE(x)	(dtype_max - DTYPE_MAX != 0.5 || \
+static const type dtype_min = (type)DTYPE_MIN - 0.5;
+static const type dtype_max = (type)DTYPE_MAX + 0.5;
+#define	INRANGE(x)	(dtype_max - (type)DTYPE_MAX != 0.5 || \
 			 ((x) > dtype_min && (x) < dtype_max))
 
 dtype
diff --git a/tests/Android.bp b/tests/Android.bp
index f0fe5e6..1755053 100644
--- a/tests/Android.bp
+++ b/tests/Android.bp
@@ -751,6 +751,33 @@
     ],
 }
 
+cc_test {
+    name: "bionic-stress-tests",
+    defaults: [
+        "bionic_tests_defaults",
+    ],
+
+    // For now, these tests run forever, so do not use the isolation framework.
+    isolated: false,
+
+    srcs: [
+        "malloc_stress_test.cpp",
+    ],
+
+    shared_libs: [
+        "libbase",
+    ],
+
+    target: {
+        android: {
+            static_libs: [
+                "libmeminfo",
+                "libprocinfo",
+            ],
+        },
+    },
+}
+
 // -----------------------------------------------------------------------------
 // Tests for the device linked against bionic's static library. Run with:
 //   adb shell /data/nativetest/bionic-unit-tests-static/bionic-unit-tests-static
diff --git a/tests/malloc_stress_test.cpp b/tests/malloc_stress_test.cpp
new file mode 100644
index 0000000..6c5cc06
--- /dev/null
+++ b/tests/malloc_stress_test.cpp
@@ -0,0 +1,115 @@
+/*
+ * Copyright (C) 2019 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 <gtest/gtest.h>
+
+#include <inttypes.h>
+#include <malloc.h>
+#include <limits.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <thread>
+#include <vector>
+
+#include <android-base/strings.h>
+#if defined(__BIONIC__)
+#include <meminfo/procmeminfo.h>
+#include <procinfo/process_map.h>
+#endif
+
+TEST(malloc_stress, multiple_threads_forever) {
+  constexpr size_t kMaxThreads = 256;
+  constexpr size_t kAllocSize = 4096;
+#if defined(__BIONIC__)
+  uint64_t rss_min = UINT64_MAX;
+  uint64_t rss_max = 0;
+  uint64_t vss_min = UINT64_MAX;
+  uint64_t vss_max = 0;
+  ASSERT_EQ(1, mallopt(M_DECAY_TIME, 1));
+#endif
+  uint64_t mallinfo_min = UINT64_MAX;
+  uint64_t mallinfo_max = 0;
+  for (size_t i = 0; ; i++) {
+    printf("Pass %zu\n", i);
+
+    std::vector<std::thread*> threads;
+    for (size_t i = 0; i < kMaxThreads; i++) {
+      threads.push_back(new std::thread([]() {
+        void* buf = malloc(4096);
+        if (buf == nullptr) {
+          printf("Failed to allocate memory\n");
+          _exit(1);
+        }
+        memset(buf, 0, kAllocSize);
+        sleep(1);
+        free(buf);
+      }));
+    }
+
+    for (auto thread : threads) {
+      thread->join();
+      delete thread;
+    }
+    threads.clear();
+
+#if defined(__BIONIC__)
+    android::meminfo::ProcMemInfo proc_mem(getpid());
+    const std::vector<android::meminfo::Vma>& maps = proc_mem.MapsWithoutUsageStats();
+    uint64_t rss_bytes = 0;
+    uint64_t vss_bytes = 0;
+    for (auto& vma : maps) {
+      if (vma.name == "[anon:libc_malloc]" || android::base::StartsWith(vma.name, "[anon:scudo:")) {
+        android::meminfo::Vma update_vma(vma);
+        ASSERT_TRUE(proc_mem.FillInVmaStats(update_vma));
+        rss_bytes += update_vma.usage.rss;
+        vss_bytes += update_vma.usage.vss;
+      }
+    }
+    if (rss_bytes < rss_min) {
+      rss_min = rss_bytes;
+    }
+    if (rss_bytes > rss_max) {
+      rss_max = rss_bytes;
+    }
+    if (vss_bytes < vss_min) {
+      vss_min = vss_bytes;
+    }
+    if (vss_bytes > vss_max) {
+      vss_max = vss_bytes;
+    }
+    printf("RSS %" PRIu64 " %0.2fMB\n", rss_bytes, rss_bytes / (1024.0 * 1024.0));
+    printf("  Min %" PRIu64 " %0.2fMB\n", rss_min, rss_min / (1024.0 * 1024.0));
+    printf("  Max %" PRIu64 " %0.2fMB\n", rss_max, rss_max / (1024.0 * 1024.0));
+    printf("VSS %" PRIu64 " %0.2f MB\n", vss_bytes, vss_bytes / (1024.0 * 1024.0));
+    printf("  Min %" PRIu64 " %0.2fMB\n", vss_min, vss_min / (1024.0 * 1024.0));
+    printf("  Max %" PRIu64 " %0.2fMB\n", vss_max, vss_max / (1024.0 * 1024.0));
+#endif
+
+    size_t mallinfo_bytes = mallinfo().uordblks;
+    if (mallinfo_bytes < mallinfo_min) {
+      mallinfo_min = mallinfo_bytes;
+    }
+    if (mallinfo_bytes > mallinfo_max) {
+      mallinfo_max = mallinfo_bytes;
+    }
+    printf("Allocated memory %zu %0.2fMB\n", mallinfo_bytes, mallinfo_bytes / (1024.0 * 1024.0));
+    printf("  Min %" PRIu64 " %0.2fMB\n", mallinfo_min, mallinfo_min / (1024.0 * 1024.0));
+    printf("  Max %" PRIu64 " %0.2fMB\n", mallinfo_max, mallinfo_max / (1024.0 * 1024.0));
+  }
+}