blob: 80618e50390c749d58dcc9e70255f15d49405ef3 [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>
Josh Gao253ef122019-08-13 15:00:54 -070020#include <sched.h>
Josh Gao7d15dc32017-03-13 17:10:46 -070021#include <stdio.h>
22#include <string.h>
23#include <sys/mman.h>
Colin Cross7da20342021-07-28 11:18:11 -070024#include <sys/syscall.h>
Josh Gao7d15dc32017-03-13 17:10:46 -070025#include <sys/user.h>
26#include <unistd.h>
27
28#include <gtest/gtest.h>
29
30#include <chrono>
31#include <thread>
32#include <vector>
33
34#include <android-base/macros.h>
Josh Gao253ef122019-08-13 15:00:54 -070035#include <android-base/threads.h>
Josh Gao7d15dc32017-03-13 17:10:46 -070036
37#include "utils.h"
38
39using namespace std::chrono_literals;
40
Josh Gao253ef122019-08-13 15:00:54 -070041static void WaitUntilAllThreadsExited(pid_t* tids, size_t tid_count) {
Zhenhua WANGcf17b482017-05-12 13:53:51 +080042 // Wait until all children have exited.
43 bool alive = true;
44 while (alive) {
45 alive = false;
Josh Gao253ef122019-08-13 15:00:54 -070046 for (size_t i = 0; i < tid_count; ++i) {
47 if (tids[i] != 0) {
Colin Cross7da20342021-07-28 11:18:11 -070048 if (syscall(__NR_tgkill, getpid(), tids[i], 0) == 0) {
Zhenhua WANGcf17b482017-05-12 13:53:51 +080049 alive = true;
50 } else {
51 EXPECT_EQ(errno, ESRCH);
Josh Gao253ef122019-08-13 15:00:54 -070052 tids[i] = 0; // Skip in next loop.
Zhenhua WANGcf17b482017-05-12 13:53:51 +080053 }
54 }
55 }
Josh Gao253ef122019-08-13 15:00:54 -070056 sched_yield();
Zhenhua WANGcf17b482017-05-12 13:53:51 +080057 }
Josh Gao7d15dc32017-03-13 17:10:46 -070058}
59
Zhenhua WANGcf17b482017-05-12 13:53:51 +080060class LeakChecker {
61 public:
62 LeakChecker() {
Elliott Hughesa8384902017-10-03 10:18:58 -070063 // Avoid resizing and using memory later.
64 // 64Ki is the default limit on VMAs per process.
65 maps_.reserve(64*1024);
Zhenhua WANGcf17b482017-05-12 13:53:51 +080066 Reset();
67 }
68
69 ~LeakChecker() {
70 Check();
71 }
72
73 void Reset() {
74 previous_size_ = GetMappingSize();
75 }
76
77 void DumpTo(std::ostream& os) const {
78 os << previous_size_;
79 }
80
81 private:
82 size_t previous_size_;
Elliott Hughesa8384902017-10-03 10:18:58 -070083 std::vector<map_record> maps_;
Zhenhua WANGcf17b482017-05-12 13:53:51 +080084
85 void Check() {
86 auto current_size = GetMappingSize();
87 if (current_size > previous_size_) {
88 FAIL() << "increase in process map size: " << previous_size_ << " -> " << current_size;
89 }
90 }
Elliott Hughesa8384902017-10-03 10:18:58 -070091
92 size_t GetMappingSize() {
93 if (!Maps::parse_maps(&maps_)) {
94 err(1, "failed to parse maps");
95 }
96
97 size_t result = 0;
98 for (const map_record& map : maps_) {
99 result += map.addr_end - map.addr_start;
100 }
101
102 return result;
103 }
Zhenhua WANGcf17b482017-05-12 13:53:51 +0800104};
105
106std::ostream& operator<<(std::ostream& os, const LeakChecker& lc) {
107 lc.DumpTo(os);
108 return os;
Josh Gao7d15dc32017-03-13 17:10:46 -0700109}
110
111// http://b/36045112
Zhenhua WANGcf17b482017-05-12 13:53:51 +0800112TEST(pthread_leak, join) {
Evgeny Eltsin45b36c22020-06-08 21:19:12 +0200113 SKIP_WITH_NATIVE_BRIDGE; // http://b/37920774
114
Zhenhua WANGcf17b482017-05-12 13:53:51 +0800115 LeakChecker lc;
Evgeny Eltsin775fb092018-05-04 20:08:37 +0200116
Evgeny Eltsin45b36c22020-06-08 21:19:12 +0200117 for (int i = 0; i < 100; ++i) {
118 pthread_t thread;
119 ASSERT_EQ(0, pthread_create(&thread, nullptr, [](void*) -> void* { return nullptr; }, nullptr));
120 ASSERT_EQ(0, pthread_join(thread, nullptr));
Josh Gao7d15dc32017-03-13 17:10:46 -0700121 }
122}
123
124// http://b/36045112
Zhenhua WANGcf17b482017-05-12 13:53:51 +0800125TEST(pthread_leak, detach) {
Evgeny Eltsin45b36c22020-06-08 21:19:12 +0200126 SKIP_WITH_NATIVE_BRIDGE; // http://b/37920774
127
Zhenhua WANGcf17b482017-05-12 13:53:51 +0800128 LeakChecker lc;
129
Dmytro Chystiakov6e36dd22019-10-14 12:24:33 -0700130 // Ancient devices with only 2 cores need a lower limit.
131 // http://b/129924384 and https://issuetracker.google.com/142210680.
132 const int thread_count = (sysconf(_SC_NPROCESSORS_CONF) > 2) ? 100 : 50;
Dmytro Chystiakova3392332019-05-10 09:30:04 -0700133
134 for (size_t pass = 0; pass < 1; ++pass) {
Dmytro Chystiakov6e36dd22019-10-14 12:24:33 -0700135 struct thread_data { pthread_barrier_t* barrier; pid_t* tid; } threads[thread_count];
Elliott Hughesa8384902017-10-03 10:18:58 -0700136
Zhenhua WANGcf17b482017-05-12 13:53:51 +0800137 pthread_barrier_t barrier;
Dmytro Chystiakov6e36dd22019-10-14 12:24:33 -0700138 ASSERT_EQ(pthread_barrier_init(&barrier, nullptr, thread_count + 1), 0);
Zhenhua WANGcf17b482017-05-12 13:53:51 +0800139
140 // Start child threads.
Dmytro Chystiakov6e36dd22019-10-14 12:24:33 -0700141 pid_t tids[thread_count];
142 for (int i = 0; i < thread_count; ++i) {
Elliott Hughesa8384902017-10-03 10:18:58 -0700143 threads[i] = {&barrier, &tids[i]};
Zhenhua WANGcf17b482017-05-12 13:53:51 +0800144 const auto thread_function = +[](void* ptr) -> void* {
145 thread_data* data = static_cast<thread_data*>(ptr);
146 *data->tid = gettid();
147 pthread_barrier_wait(data->barrier);
Zhenhua WANGcf17b482017-05-12 13:53:51 +0800148 return nullptr;
149 };
150 pthread_t thread;
Elliott Hughesa8384902017-10-03 10:18:58 -0700151 ASSERT_EQ(0, pthread_create(&thread, nullptr, thread_function, &threads[i]));
Zhenhua WANGcf17b482017-05-12 13:53:51 +0800152 ASSERT_EQ(0, pthread_detach(thread));
153 }
154
155 pthread_barrier_wait(&barrier);
156 ASSERT_EQ(pthread_barrier_destroy(&barrier), 0);
157
Dmytro Chystiakov6e36dd22019-10-14 12:24:33 -0700158 WaitUntilAllThreadsExited(tids, thread_count);
Zhenhua WANGcf17b482017-05-12 13:53:51 +0800159
Evgeny Eltsin45b36c22020-06-08 21:19:12 +0200160 // TODO(b/158573595): the test is flaky without the warmup pass.
Zhenhua WANGcf17b482017-05-12 13:53:51 +0800161 if (pass == 0) lc.Reset();
Josh Gao7d15dc32017-03-13 17:10:46 -0700162 }
Josh Gao7d15dc32017-03-13 17:10:46 -0700163}