OSDN Git Service

Add tests for pthread TLS leak.
authorJosh Gao <jmgao@google.com>
Tue, 14 Mar 2017 00:10:46 +0000 (17:10 -0700)
committerJosh Gao <jmgao@google.com>
Wed, 15 Mar 2017 20:30:05 +0000 (13:30 -0700)
Add tests that ensure that threads don't leak memory mappings after
they exit.

Bug: http://b/36045112
Test: /data/nativetest/bionic-unit-tests/bionic-unit-tests --gtest_filter=*leak*
Test: /data/nativetest64/bionic-unit-tests/bionic-unit-tests --gtest_filter=*leak*
Change-Id: Id0c1194b5d2bb7d89947b1ade16eb0d768d8c5b7

tests/Android.bp
tests/gtest_main.cpp
tests/leak_test.cpp [new file with mode: 0644]

index 1cca332..2bdbbbc 100644 (file)
@@ -70,6 +70,7 @@ cc_test_library {
         "ifaddrs_test.cpp",
         "inttypes_test.cpp",
         "langinfo_test.cpp",
+        "leak_test.cpp",
         "libc_logging_test.cpp",
         "libgen_basename_test.cpp",
         "libgen_test.cpp",
index b9ee585..aba93ba 100644 (file)
@@ -171,7 +171,7 @@ class Test {
 
   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 (file)
index 0000000..9ddb2ff
--- /dev/null
@@ -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);
+}