Merge "Add libc optimizations to bionic for MIPS"
diff --git a/libdl/Android.bp b/libdl/Android.bp
index 013554a..5b67e38 100644
--- a/libdl/Android.bp
+++ b/libdl/Android.bp
@@ -104,6 +104,7 @@
     stl: "none",
 
     name: "ld-android",
+    defaults: ["linux_bionic_supported"],
 
     // NOTE: libdl needs __aeabi_unwind_cpp_pr0 from libgcc.a but libgcc.a needs a
     // few symbols from libc. Using --no-undefined here results in having to link
diff --git a/tests/Android.bp b/tests/Android.bp
index 1cca332..2bdbbbc 100644
--- a/tests/Android.bp
+++ b/tests/Android.bp
@@ -70,6 +70,7 @@
         "ifaddrs_test.cpp",
         "inttypes_test.cpp",
         "langinfo_test.cpp",
+        "leak_test.cpp",
         "libc_logging_test.cpp",
         "libgen_basename_test.cpp",
         "libgen_test.cpp",
diff --git a/tests/gtest_globals.cpp b/tests/gtest_globals.cpp
index 75c08b1..538a534 100644
--- a/tests/gtest_globals.cpp
+++ b/tests/gtest_globals.cpp
@@ -19,17 +19,19 @@
 #include <gtest/gtest.h>
 #include "utils.h"
 
+#include <android-base/file.h>
+
 #include <string>
 
 static std::string init_testlib_root() {
   // Calculate ANDROID_DATA assuming the binary is in "$ANDROID_DATA/somedir/binary-dir/binary"
   std::string path = get_executable_path();
 
-  path = get_dirname(path.c_str());
+  path = android::base::Dirname(path);
   path += "/..";
 
   std::string out_path;
-  if (!get_realpath(path.c_str(), &out_path)) {
+  if (!android::base::Realpath(path.c_str(), &out_path)) {
     printf("Failed to get realpath for \"%s\"", path.c_str());
     abort();
   }
@@ -37,7 +39,7 @@
   out_path += "/bionic-loader-test-libs";
 
   std::string real_path;
-  if (!get_realpath(out_path, &real_path)) {
+  if (!android::base::Realpath(out_path, &real_path)) {
     printf("\"%s\": does not exists", out_path.c_str());
     abort();
   }
diff --git a/tests/gtest_main.cpp b/tests/gtest_main.cpp
index b9ee585..5f28321 100644
--- a/tests/gtest_main.cpp
+++ b/tests/gtest_main.cpp
@@ -56,25 +56,6 @@
   return g_executable_path;
 }
 
-bool get_realpath(const std::string& path, std::string* real_path) {
-  char realpath_buf[PATH_MAX];
-  if (realpath(path.c_str(), realpath_buf) != realpath_buf) {
-    return false;
-  }
-
-  *real_path = realpath_buf;
-  return true;
-}
-
-std::string get_dirname(const char* path) {
-#if defined(__BIONIC__)
-  return dirname(path);
-#else
-  // GLIBC does not have const char* dirname
-  return dirname(const_cast<char*>(path));
-#endif
-}
-
 int get_argc() {
   return g_argc;
 }
@@ -171,7 +152,7 @@
 
   TestResult GetResult() const { return result_; }
   TestResult GetExpectedResult() const {
-    return GetName().find("xfail_") == 0 ? TEST_FAILED : TEST_SUCCESS;
+    return GetName().find("xfail") == 0 ? TEST_FAILED : TEST_SUCCESS;
   }
 
   void SetTestTime(int64_t elapsed_time_ns) { elapsed_time_ns_ = elapsed_time_ns; }
diff --git a/tests/leak_test.cpp b/tests/leak_test.cpp
new file mode 100644
index 0000000..9ddb2ff
--- /dev/null
+++ b/tests/leak_test.cpp
@@ -0,0 +1,100 @@
+/*
+ * Copyright (C) 2017 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 <err.h>
+#include <inttypes.h>
+#include <pthread.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/mman.h>
+#include <sys/user.h>
+#include <unistd.h>
+
+#include <gtest/gtest.h>
+
+#include <chrono>
+#include <thread>
+#include <vector>
+
+#include <android-base/macros.h>
+
+#include "utils.h"
+
+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;
+}
+
+#define LEAK_TEST(test_case_name, test_name)                                                 \
+  static void __leak_test__##test_case_name##__##test_name();                                \
+  TEST(test_case_name, test_name) {                                                          \
+    auto previous_size = GetMappingSize();                                                   \
+    __leak_test__##test_case_name##__##test_name();                                          \
+    auto current_size = GetMappingSize();                                                    \
+    if (current_size > previous_size) {                                                      \
+      FAIL() << "increase in process map size: " << previous_size << " -> " << current_size; \
+    }                                                                                        \
+  }                                                                                          \
+  static void __leak_test__##test_case_name##__##test_name()
+
+LEAK_TEST(leak, smoke) {
+  // Do nothing.
+}
+
+LEAK_TEST(leak, xfail) {
+  UNUSED(mmap(nullptr, PAGE_SIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0));
+}
+
+// http://b/36045112
+LEAK_TEST(pthread_leak, join) {
+  for (int i = 0; i < 100; ++i) {
+    pthread_t thread;
+    ASSERT_EQ(0, pthread_create(&thread, nullptr, [](void*) -> void* { return nullptr; }, nullptr));
+    ASSERT_EQ(0, pthread_join(thread, nullptr));
+  }
+}
+
+// http://b/36045112
+LEAK_TEST(pthread_leak, detach) {
+  pthread_barrier_t barrier;
+  constexpr int thread_count = 100;
+  ASSERT_EQ(0, pthread_barrier_init(&barrier, nullptr, thread_count + 1));
+  for (int i = 0; i < thread_count; ++i) {
+    pthread_t thread;
+    const auto thread_function = +[](void* barrier) -> void* {
+      pthread_barrier_wait(static_cast<pthread_barrier_t*>(barrier));
+      return nullptr;
+    };
+    ASSERT_EQ(0, pthread_create(&thread, nullptr, thread_function, &barrier));
+    ASSERT_EQ(0, pthread_detach(thread));
+  }
+
+  pthread_barrier_wait(&barrier);
+
+  // Give the threads some time to exit.
+  std::this_thread::sleep_for(100ms);
+}
diff --git a/tests/pthread_test.cpp b/tests/pthread_test.cpp
index 4fb15ad..60fe294 100755
--- a/tests/pthread_test.cpp
+++ b/tests/pthread_test.cpp
@@ -1886,25 +1886,34 @@
 
 static volatile bool signal_handler_on_altstack_done;
 
+__attribute__((__noinline__))
+static void signal_handler_backtrace() {
+  // Check if we have enough stack space for unwinding.
+  int count = 0;
+  _Unwind_Backtrace(FrameCounter, &count);
+  ASSERT_GT(count, 0);
+}
+
+__attribute__((__noinline__))
+static void signal_handler_logging() {
+  // Check if we have enough stack space for logging.
+  std::string s(2048, '*');
+  GTEST_LOG_(INFO) << s;
+  signal_handler_on_altstack_done = true;
+}
+
+__attribute__((__noinline__))
+static void signal_handler_snprintf() {
+  // Check if we have enough stack space for snprintf to a PATH_MAX buffer, plus some extra.
+  char buf[PATH_MAX + 2048];
+  ASSERT_GT(snprintf(buf, sizeof(buf), "/proc/%d/status", getpid()), 0);
+}
+
 static void SignalHandlerOnAltStack(int signo, siginfo_t*, void*) {
   ASSERT_EQ(SIGUSR1, signo);
-  {
-    // Check if we have enough stack space for unwinding.
-    int count = 0;
-    _Unwind_Backtrace(FrameCounter, &count);
-    ASSERT_GT(count, 0);
-  }
-  {
-    // Check if we have enough stack space for logging.
-    std::string s(2048, '*');
-    GTEST_LOG_(INFO) << s;
-    signal_handler_on_altstack_done = true;
-  }
-  {
-    // Check if we have enough stack space for snprintf to a PATH_MAX buffer, plus some extra.
-    char buf[PATH_MAX + 2048];
-    ASSERT_GT(snprintf(buf, sizeof(buf), "/proc/%d/status", getpid()), 0);
-  }
+  signal_handler_backtrace();
+  signal_handler_logging();
+  signal_handler_snprintf();
 }
 
 TEST(pthread, big_enough_signal_stack) {
diff --git a/tests/utils.h b/tests/utils.h
index 31974d0..fa85545 100644
--- a/tests/utils.h
+++ b/tests/utils.h
@@ -143,11 +143,6 @@
 // The absolute path to the executable
 const std::string& get_executable_path();
 
-// Get realpath
-bool get_realpath(const std::string& path, std::string* realpath);
-// Get dirname
-std::string get_dirname(const char* path);
-
 // Access to argc/argv/envp
 int get_argc();
 char** get_argv();