blob: 9ddb2ff76481d02f33fb53ddcd4fb92a1d99ff89 [file] [log] [blame]
Josh Gao7d15dc32017-03-13 17:10:46 -07001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <err.h>
18#include <inttypes.h>
19#include <pthread.h>
20#include <stdio.h>
21#include <string.h>
22#include <sys/mman.h>
23#include <sys/user.h>
24#include <unistd.h>
25
26#include <gtest/gtest.h>
27
28#include <chrono>
29#include <thread>
30#include <vector>
31
32#include <android-base/macros.h>
33
34#include "utils.h"
35
36using namespace std::chrono_literals;
37
38static size_t GetMappingSize() {
39 std::vector<map_record> maps;
40 if (!Maps::parse_maps(&maps)) {
41 err(1, "failed to parse maps");
42 }
43
44 size_t result = 0;
45 for (const map_record& map : maps) {
46 result += map.addr_end - map.addr_start;
47 }
48
49 return result;
50}
51
52#define LEAK_TEST(test_case_name, test_name) \
53 static void __leak_test__##test_case_name##__##test_name(); \
54 TEST(test_case_name, test_name) { \
55 auto previous_size = GetMappingSize(); \
56 __leak_test__##test_case_name##__##test_name(); \
57 auto current_size = GetMappingSize(); \
58 if (current_size > previous_size) { \
59 FAIL() << "increase in process map size: " << previous_size << " -> " << current_size; \
60 } \
61 } \
62 static void __leak_test__##test_case_name##__##test_name()
63
64LEAK_TEST(leak, smoke) {
65 // Do nothing.
66}
67
68LEAK_TEST(leak, xfail) {
69 UNUSED(mmap(nullptr, PAGE_SIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0));
70}
71
72// http://b/36045112
73LEAK_TEST(pthread_leak, join) {
74 for (int i = 0; i < 100; ++i) {
75 pthread_t thread;
76 ASSERT_EQ(0, pthread_create(&thread, nullptr, [](void*) -> void* { return nullptr; }, nullptr));
77 ASSERT_EQ(0, pthread_join(thread, nullptr));
78 }
79}
80
81// http://b/36045112
82LEAK_TEST(pthread_leak, detach) {
83 pthread_barrier_t barrier;
84 constexpr int thread_count = 100;
85 ASSERT_EQ(0, pthread_barrier_init(&barrier, nullptr, thread_count + 1));
86 for (int i = 0; i < thread_count; ++i) {
87 pthread_t thread;
88 const auto thread_function = +[](void* barrier) -> void* {
89 pthread_barrier_wait(static_cast<pthread_barrier_t*>(barrier));
90 return nullptr;
91 };
92 ASSERT_EQ(0, pthread_create(&thread, nullptr, thread_function, &barrier));
93 ASSERT_EQ(0, pthread_detach(thread));
94 }
95
96 pthread_barrier_wait(&barrier);
97
98 // Give the threads some time to exit.
99 std::this_thread::sleep_for(100ms);
100}