blob: de088691786dc3040a87fe982ac1d0be867f984f [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
Zhenhua WANGcf17b482017-05-12 13:53:51 +080038static void WaitUntilAllExited(pid_t* pids, size_t pid_count) {
39 // Wait until all children have exited.
40 bool alive = true;
41 while (alive) {
42 alive = false;
43 for (size_t i = 0; i < pid_count; ++i) {
44 if (pids[i] != 0) {
45 if (kill(pids[i], 0) == 0) {
46 alive = true;
47 } else {
48 EXPECT_EQ(errno, ESRCH);
49 pids[i] = 0; // Skip in next loop.
50 }
51 }
52 }
53 }
Josh Gao7d15dc32017-03-13 17:10:46 -070054}
55
Zhenhua WANGcf17b482017-05-12 13:53:51 +080056class LeakChecker {
57 public:
58 LeakChecker() {
Elliott Hughesa8384902017-10-03 10:18:58 -070059 // Avoid resizing and using memory later.
60 // 64Ki is the default limit on VMAs per process.
61 maps_.reserve(64*1024);
Zhenhua WANGcf17b482017-05-12 13:53:51 +080062 Reset();
63 }
64
65 ~LeakChecker() {
66 Check();
67 }
68
69 void Reset() {
70 previous_size_ = GetMappingSize();
71 }
72
73 void DumpTo(std::ostream& os) const {
74 os << previous_size_;
75 }
76
77 private:
78 size_t previous_size_;
Elliott Hughesa8384902017-10-03 10:18:58 -070079 std::vector<map_record> maps_;
Zhenhua WANGcf17b482017-05-12 13:53:51 +080080
81 void Check() {
82 auto current_size = GetMappingSize();
83 if (current_size > previous_size_) {
84 FAIL() << "increase in process map size: " << previous_size_ << " -> " << current_size;
85 }
86 }
Elliott Hughesa8384902017-10-03 10:18:58 -070087
88 size_t GetMappingSize() {
89 if (!Maps::parse_maps(&maps_)) {
90 err(1, "failed to parse maps");
91 }
92
93 size_t result = 0;
94 for (const map_record& map : maps_) {
95 result += map.addr_end - map.addr_start;
96 }
97
98 return result;
99 }
Zhenhua WANGcf17b482017-05-12 13:53:51 +0800100};
101
102std::ostream& operator<<(std::ostream& os, const LeakChecker& lc) {
103 lc.DumpTo(os);
104 return os;
Josh Gao7d15dc32017-03-13 17:10:46 -0700105}
106
107// http://b/36045112
Zhenhua WANGcf17b482017-05-12 13:53:51 +0800108TEST(pthread_leak, join) {
109 LeakChecker lc;
Josh Gao7d15dc32017-03-13 17:10:46 -0700110 for (int i = 0; i < 100; ++i) {
111 pthread_t thread;
112 ASSERT_EQ(0, pthread_create(&thread, nullptr, [](void*) -> void* { return nullptr; }, nullptr));
113 ASSERT_EQ(0, pthread_join(thread, nullptr));
114 }
115}
116
117// http://b/36045112
Zhenhua WANGcf17b482017-05-12 13:53:51 +0800118TEST(pthread_leak, detach) {
119 LeakChecker lc;
120
121 for (size_t pass = 0; pass < 2; ++pass) {
Elliott Hughesa8384902017-10-03 10:18:58 -0700122 constexpr int kThreadCount = 100;
123 struct thread_data { pthread_barrier_t* barrier; pid_t* tid; } threads[kThreadCount] = {};
124
Zhenhua WANGcf17b482017-05-12 13:53:51 +0800125 pthread_barrier_t barrier;
Elliott Hughesa8384902017-10-03 10:18:58 -0700126 ASSERT_EQ(pthread_barrier_init(&barrier, nullptr, kThreadCount + 1), 0);
Zhenhua WANGcf17b482017-05-12 13:53:51 +0800127
128 // Start child threads.
Elliott Hughesa8384902017-10-03 10:18:58 -0700129 pid_t tids[kThreadCount];
130 for (int i = 0; i < kThreadCount; ++i) {
131 threads[i] = {&barrier, &tids[i]};
Zhenhua WANGcf17b482017-05-12 13:53:51 +0800132 const auto thread_function = +[](void* ptr) -> void* {
133 thread_data* data = static_cast<thread_data*>(ptr);
134 *data->tid = gettid();
135 pthread_barrier_wait(data->barrier);
Zhenhua WANGcf17b482017-05-12 13:53:51 +0800136 return nullptr;
137 };
138 pthread_t thread;
Elliott Hughesa8384902017-10-03 10:18:58 -0700139 ASSERT_EQ(0, pthread_create(&thread, nullptr, thread_function, &threads[i]));
Zhenhua WANGcf17b482017-05-12 13:53:51 +0800140 ASSERT_EQ(0, pthread_detach(thread));
141 }
142
143 pthread_barrier_wait(&barrier);
144 ASSERT_EQ(pthread_barrier_destroy(&barrier), 0);
145
146 WaitUntilAllExited(tids, arraysize(tids));
147
148 // houdini keeps a thread pool, so we ignore the first pass while the
149 // pool fills, but then on the second pass require that the "pool" isn't
150 // actually an unbounded leak. https://issuetracker.google.com/37920774.
151 if (pass == 0) lc.Reset();
Josh Gao7d15dc32017-03-13 17:10:46 -0700152 }
Josh Gao7d15dc32017-03-13 17:10:46 -0700153}