Merge "Revert "linker: disable ld.config.txt in ASAN mode""
diff --git a/tests/Android.bp b/tests/Android.bp
index da56367..25af05e 100644
--- a/tests/Android.bp
+++ b/tests/Android.bp
@@ -328,12 +328,6 @@
             ],
         }
     },
-
-    product_variables: {
-        debuggable: {
-            cppflags: ["-DUSE_LD_CONFIG_FILE"],
-        },
-    },
 }
 
 // -----------------------------------------------------------------------------
@@ -624,12 +618,6 @@
     sanitize: {
         never: false,
     },
-
-    product_variables: {
-        debuggable: {
-            cppflags: ["-DUSE_LD_CONFIG_FILE"],
-        },
-    },
 }
 
 subdirs = ["libs"]
diff --git a/tests/dl_test.cpp b/tests/dl_test.cpp
index ceb0b4d..2133a2c 100644
--- a/tests/dl_test.cpp
+++ b/tests/dl_test.cpp
@@ -180,12 +180,24 @@
 }
 #endif
 
-#ifdef USE_LD_CONFIG_FILE
+#if defined(__BIONIC__)
+static bool is_user_build() {
+  std::string build_type = android::base::GetProperty("ro.build.type", "user");
+  if (build_type == "userdebug" || build_type == "eng") {
+    return false;
+  }
+  return true;
+}
+#endif
 
 // _lib1.so and _lib2.so are now searchable by having another namespace 'ns2'
 // whose search paths include the 'ns2/' subdir.
 TEST(dl, exec_with_ld_config_file) {
 #if defined(__BIONIC__)
+  if (is_user_build()) {
+    // LD_CONFIG_FILE is not supported on user build
+    return;
+  }
   std::string helper = get_testlib_root() +
       "/ld_config_test_helper/ld_config_test_helper";
   std::string config_file = get_testlib_root() + "/ld.config.txt";
@@ -204,6 +216,10 @@
 // additional namespaces other than the default namespace.
 TEST(dl, exec_with_ld_config_file_with_ld_preload) {
 #if defined(__BIONIC__)
+  if (is_user_build()) {
+    // LD_CONFIG_FILE is not supported on user build
+    return;
+  }
   std::string helper = get_testlib_root() +
       "/ld_config_test_helper/ld_config_test_helper";
   std::string config_file = get_testlib_root() + "/ld.config.txt";
@@ -218,8 +234,6 @@
 #endif
 }
 
-#endif // USE_LD_CONFIG_FILE
-
 // ensures that LD_CONFIG_FILE env var does not work for production builds.
 // The test input is the same as exec_with_ld_config_file, but it must fail in
 // this case.
@@ -230,8 +244,7 @@
     // This test is only for CTS.
     return;
   }
-  std::string build_type = android::base::GetProperty("ro.build.type", "user");
-  if (build_type == "userdebug" || build_type == "eng") {
+  if (!is_user_build()) {
     // Skip the test for non production devices
     return;
   }
diff --git a/tests/leak_test.cpp b/tests/leak_test.cpp
index a356640..de08869 100644
--- a/tests/leak_test.cpp
+++ b/tests/leak_test.cpp
@@ -35,20 +35,6 @@
 
 using namespace std::chrono_literals;
 
-static size_t GetMappingSize() {
-  std::vector<map_record> maps;
-  if (!Maps::parse_maps(&maps)) {
-    err(1, "failed to parse maps");
-  }
-
-  size_t result = 0;
-  for (const map_record& map : maps) {
-    result += map.addr_end - map.addr_start;
-  }
-
-  return result;
-}
-
 static void WaitUntilAllExited(pid_t* pids, size_t pid_count) {
   // Wait until all children have exited.
   bool alive = true;
@@ -70,6 +56,9 @@
 class LeakChecker {
  public:
   LeakChecker() {
+    // Avoid resizing and using memory later.
+    // 64Ki is the default limit on VMAs per process.
+    maps_.reserve(64*1024);
     Reset();
   }
 
@@ -87,6 +76,7 @@
 
  private:
   size_t previous_size_;
+  std::vector<map_record> maps_;
 
   void Check() {
     auto current_size = GetMappingSize();
@@ -94,6 +84,19 @@
       FAIL() << "increase in process map size: " << previous_size_ << " -> " << current_size;
     }
   }
+
+  size_t GetMappingSize() {
+    if (!Maps::parse_maps(&maps_)) {
+      err(1, "failed to parse maps");
+    }
+
+    size_t result = 0;
+    for (const map_record& map : maps_) {
+      result += map.addr_end - map.addr_start;
+    }
+
+    return result;
+  }
 };
 
 std::ostream& operator<<(std::ostream& os, const LeakChecker& lc) {
@@ -116,27 +119,24 @@
   LeakChecker lc;
 
   for (size_t pass = 0; pass < 2; ++pass) {
+    constexpr int kThreadCount = 100;
+    struct thread_data { pthread_barrier_t* barrier; pid_t* tid; } threads[kThreadCount] = {};
+
     pthread_barrier_t barrier;
-    constexpr int thread_count = 100;
-    ASSERT_EQ(pthread_barrier_init(&barrier, nullptr, thread_count + 1), 0);
+    ASSERT_EQ(pthread_barrier_init(&barrier, nullptr, kThreadCount + 1), 0);
 
     // Start child threads.
-    struct thread_data { pthread_barrier_t* barrier; pid_t* tid; };
-    pid_t tids[thread_count];
-    for (int i = 0; i < thread_count; ++i) {
-      thread_data* td = new thread_data{&barrier, &tids[i]};
+    pid_t tids[kThreadCount];
+    for (int i = 0; i < kThreadCount; ++i) {
+      threads[i] = {&barrier, &tids[i]};
       const auto thread_function = +[](void* ptr) -> void* {
         thread_data* data = static_cast<thread_data*>(ptr);
         *data->tid = gettid();
         pthread_barrier_wait(data->barrier);
-        // Doing this delete allocates new VMAs for jemalloc bookkeeping,
-        // but the two-pass nature of this test means we can check that
-        // it's a pool rather than an unbounded leak.
-        delete data;
         return nullptr;
       };
       pthread_t thread;
-      ASSERT_EQ(0, pthread_create(&thread, nullptr, thread_function, td));
+      ASSERT_EQ(0, pthread_create(&thread, nullptr, thread_function, &threads[i]));
       ASSERT_EQ(0, pthread_detach(thread));
     }
 
diff --git a/tests/unistd_test.cpp b/tests/unistd_test.cpp
index 8af2263..cd51e1b 100644
--- a/tests/unistd_test.cpp
+++ b/tests/unistd_test.cpp
@@ -784,12 +784,6 @@
   EXPECT_GT(_XOPEN_IOV_MAX, 0);
   EXPECT_GT(_XOPEN_UNIX, 0);
 
-  // In O, the headers still have -1 (even though all the functionality has
-  // been there for a long time). This was fixed in O-DR, but there isn't a
-  // separate CTS for O-DR, so we'll accept both.
-  EXPECT_TRUE(_POSIX_THREAD_PROCESS_SHARED == -1 ||
-              _POSIX_THREAD_PROCESS_SHARED == _POSIX_VERSION);
-
 #if defined(__BIONIC__)
   // These tests only pass on bionic, as bionic and glibc has different support on these macros.
   // Macros like _POSIX_ASYNCHRONOUS_IO are not supported on bionic yet.
diff --git a/tests/utils.h b/tests/utils.h
index 8638850..daf382e 100644
--- a/tests/utils.h
+++ b/tests/utils.h
@@ -71,15 +71,13 @@
 class Maps {
  public:
   static bool parse_maps(std::vector<map_record>* maps) {
-    FILE* fp = fopen("/proc/self/maps", "re");
-    if (fp == nullptr) {
-      return false;
-    }
+    maps->clear();
 
-    auto fp_guard = android::base::make_scope_guard([&]() { fclose(fp); });
+    std::unique_ptr<FILE, decltype(&fclose)> fp(fopen("/proc/self/maps", "re"), fclose);
+    if (!fp) return false;
 
     char line[BUFSIZ];
-    while (fgets(line, sizeof(line), fp) != nullptr) {
+    while (fgets(line, sizeof(line), fp.get()) != nullptr) {
       map_record record;
       uint32_t dev_major, dev_minor;
       int path_offset;