2017-03-14 01:10:46 +01:00
|
|
|
/*
|
|
|
|
* 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>
|
2019-08-14 00:00:54 +02:00
|
|
|
#include <sched.h>
|
2017-03-14 01:10:46 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/mman.h>
|
2021-07-28 20:18:11 +02:00
|
|
|
#include <sys/syscall.h>
|
2017-03-14 01:10:46 +01:00
|
|
|
#include <sys/user.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
|
|
#include <chrono>
|
|
|
|
#include <thread>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include <android-base/macros.h>
|
2019-08-14 00:00:54 +02:00
|
|
|
#include <android-base/threads.h>
|
2017-03-14 01:10:46 +01:00
|
|
|
|
|
|
|
#include "utils.h"
|
|
|
|
|
|
|
|
using namespace std::chrono_literals;
|
|
|
|
|
2019-08-14 00:00:54 +02:00
|
|
|
static void WaitUntilAllThreadsExited(pid_t* tids, size_t tid_count) {
|
2017-05-12 07:53:51 +02:00
|
|
|
// Wait until all children have exited.
|
|
|
|
bool alive = true;
|
|
|
|
while (alive) {
|
|
|
|
alive = false;
|
2019-08-14 00:00:54 +02:00
|
|
|
for (size_t i = 0; i < tid_count; ++i) {
|
|
|
|
if (tids[i] != 0) {
|
2021-07-28 20:18:11 +02:00
|
|
|
if (syscall(__NR_tgkill, getpid(), tids[i], 0) == 0) {
|
2017-05-12 07:53:51 +02:00
|
|
|
alive = true;
|
|
|
|
} else {
|
2023-09-21 23:11:19 +02:00
|
|
|
EXPECT_ERRNO(ESRCH);
|
2019-08-14 00:00:54 +02:00
|
|
|
tids[i] = 0; // Skip in next loop.
|
2017-05-12 07:53:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-14 00:00:54 +02:00
|
|
|
sched_yield();
|
2017-05-12 07:53:51 +02:00
|
|
|
}
|
2017-03-14 01:10:46 +01:00
|
|
|
}
|
|
|
|
|
2017-05-12 07:53:51 +02:00
|
|
|
class LeakChecker {
|
|
|
|
public:
|
|
|
|
LeakChecker() {
|
2017-10-03 19:18:58 +02:00
|
|
|
// Avoid resizing and using memory later.
|
|
|
|
// 64Ki is the default limit on VMAs per process.
|
|
|
|
maps_.reserve(64*1024);
|
2017-05-12 07:53:51 +02:00
|
|
|
Reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
~LeakChecker() {
|
|
|
|
Check();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Reset() {
|
|
|
|
previous_size_ = GetMappingSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DumpTo(std::ostream& os) const {
|
|
|
|
os << previous_size_;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
size_t previous_size_;
|
2017-10-03 19:18:58 +02:00
|
|
|
std::vector<map_record> maps_;
|
2017-05-12 07:53:51 +02:00
|
|
|
|
|
|
|
void Check() {
|
|
|
|
auto current_size = GetMappingSize();
|
|
|
|
if (current_size > previous_size_) {
|
|
|
|
FAIL() << "increase in process map size: " << previous_size_ << " -> " << current_size;
|
|
|
|
}
|
|
|
|
}
|
2017-10-03 19:18:58 +02:00
|
|
|
|
|
|
|
size_t GetMappingSize() {
|
|
|
|
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;
|
|
|
|
}
|
2017-05-12 07:53:51 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
std::ostream& operator<<(std::ostream& os, const LeakChecker& lc) {
|
|
|
|
lc.DumpTo(os);
|
|
|
|
return os;
|
2017-03-14 01:10:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// http://b/36045112
|
2017-05-12 07:53:51 +02:00
|
|
|
TEST(pthread_leak, join) {
|
2020-06-08 21:19:12 +02:00
|
|
|
SKIP_WITH_NATIVE_BRIDGE; // http://b/37920774
|
2018-05-04 20:08:37 +02:00
|
|
|
|
2021-09-17 23:59:15 +02:00
|
|
|
// Warm up. HWASan allocates an extra page on the first iteration, but never after.
|
|
|
|
pthread_t thread;
|
|
|
|
ASSERT_EQ(0, pthread_create(
|
|
|
|
&thread, nullptr, [](void*) -> void* { return nullptr; }, nullptr));
|
|
|
|
ASSERT_EQ(0, pthread_join(thread, nullptr));
|
|
|
|
|
2020-06-08 21:19:12 +02:00
|
|
|
LeakChecker lc;
|
2018-05-04 20:08:37 +02:00
|
|
|
|
2020-06-08 21:19:12 +02:00
|
|
|
for (int i = 0; i < 100; ++i) {
|
2021-09-17 23:59:15 +02:00
|
|
|
ASSERT_EQ(0, pthread_create(
|
|
|
|
&thread, nullptr, [](void*) -> void* { return nullptr; }, nullptr));
|
2020-06-08 21:19:12 +02:00
|
|
|
ASSERT_EQ(0, pthread_join(thread, nullptr));
|
2017-03-14 01:10:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// http://b/36045112
|
2017-05-12 07:53:51 +02:00
|
|
|
TEST(pthread_leak, detach) {
|
2020-06-08 21:19:12 +02:00
|
|
|
SKIP_WITH_NATIVE_BRIDGE; // http://b/37920774
|
|
|
|
|
2017-05-12 07:53:51 +02:00
|
|
|
LeakChecker lc;
|
|
|
|
|
2019-10-14 21:24:33 +02:00
|
|
|
// Ancient devices with only 2 cores need a lower limit.
|
|
|
|
// http://b/129924384 and https://issuetracker.google.com/142210680.
|
|
|
|
const int thread_count = (sysconf(_SC_NPROCESSORS_CONF) > 2) ? 100 : 50;
|
2019-05-10 18:30:04 +02:00
|
|
|
|
|
|
|
for (size_t pass = 0; pass < 1; ++pass) {
|
2019-10-14 21:24:33 +02:00
|
|
|
struct thread_data { pthread_barrier_t* barrier; pid_t* tid; } threads[thread_count];
|
2017-10-03 19:18:58 +02:00
|
|
|
|
2017-05-12 07:53:51 +02:00
|
|
|
pthread_barrier_t barrier;
|
2019-10-14 21:24:33 +02:00
|
|
|
ASSERT_EQ(pthread_barrier_init(&barrier, nullptr, thread_count + 1), 0);
|
2017-05-12 07:53:51 +02:00
|
|
|
|
|
|
|
// Start child threads.
|
2019-10-14 21:24:33 +02:00
|
|
|
pid_t tids[thread_count];
|
|
|
|
for (int i = 0; i < thread_count; ++i) {
|
2017-10-03 19:18:58 +02:00
|
|
|
threads[i] = {&barrier, &tids[i]};
|
2017-05-12 07:53:51 +02:00
|
|
|
const auto thread_function = +[](void* ptr) -> void* {
|
|
|
|
thread_data* data = static_cast<thread_data*>(ptr);
|
|
|
|
*data->tid = gettid();
|
|
|
|
pthread_barrier_wait(data->barrier);
|
|
|
|
return nullptr;
|
|
|
|
};
|
|
|
|
pthread_t thread;
|
2017-10-03 19:18:58 +02:00
|
|
|
ASSERT_EQ(0, pthread_create(&thread, nullptr, thread_function, &threads[i]));
|
2017-05-12 07:53:51 +02:00
|
|
|
ASSERT_EQ(0, pthread_detach(thread));
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_barrier_wait(&barrier);
|
|
|
|
ASSERT_EQ(pthread_barrier_destroy(&barrier), 0);
|
|
|
|
|
2019-10-14 21:24:33 +02:00
|
|
|
WaitUntilAllThreadsExited(tids, thread_count);
|
2017-05-12 07:53:51 +02:00
|
|
|
|
2020-06-08 21:19:12 +02:00
|
|
|
// TODO(b/158573595): the test is flaky without the warmup pass.
|
2017-05-12 07:53:51 +02:00
|
|
|
if (pass == 0) lc.Reset();
|
2017-03-14 01:10:46 +01:00
|
|
|
}
|
|
|
|
}
|