1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
|
//===-- sanitizer_thread_registry_test.cpp --------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file is a part of shared sanitizer runtime.
//
//===----------------------------------------------------------------------===//
#include "sanitizer_common/sanitizer_thread_registry.h"
#include <iostream>
#include <vector>
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "sanitizer_common/sanitizer_common.h"
#include "sanitizer_common/sanitizer_stackdepot.h"
#include "sanitizer_common/sanitizer_stacktrace.h"
#include "sanitizer_common/sanitizer_thread_history.h"
#include "sanitizer_pthread_wrappers.h"
using testing::HasSubstr;
namespace __sanitizer {
static Mutex tctx_allocator_lock;
static LowLevelAllocator tctx_allocator;
template<typename TCTX>
static ThreadContextBase *GetThreadContext(u32 tid) {
Lock l(&tctx_allocator_lock);
return new(tctx_allocator) TCTX(tid);
}
static const u32 kMaxRegistryThreads = 1000;
static const u32 kRegistryQuarantine = 2;
static void CheckThreadQuantity(ThreadRegistry *registry, uptr exp_total,
uptr exp_running, uptr exp_alive) {
uptr total, running, alive;
registry->GetNumberOfThreads(&total, &running, &alive);
EXPECT_EQ(exp_total, total);
EXPECT_EQ(exp_running, running);
EXPECT_EQ(exp_alive, alive);
}
static bool is_detached(u32 tid) {
return (tid % 2 == 0);
}
static uptr get_uid(u32 tid) {
return tid * 2;
}
static bool HasName(ThreadContextBase *tctx, void *arg) {
char *name = (char*)arg;
return (0 == internal_strcmp(tctx->name, name));
}
static bool HasUid(ThreadContextBase *tctx, void *arg) {
uptr uid = (uptr)arg;
return (tctx->user_id == uid);
}
static void MarkUidAsPresent(ThreadContextBase *tctx, void *arg) {
bool *arr = (bool*)arg;
arr[tctx->tid] = true;
}
static void TestRegistry(ThreadRegistry *registry, bool has_quarantine) {
// Create and start a main thread.
EXPECT_EQ(0U, registry->CreateThread(get_uid(0), true, -1, 0, nullptr));
registry->StartThread(0, 0, ThreadType::Regular, 0);
// Create a bunch of threads.
for (u32 i = 1; i <= 10; i++) {
EXPECT_EQ(i, registry->CreateThread(get_uid(i), is_detached(i), 100 + i,
200 + i, nullptr));
}
CheckThreadQuantity(registry, 11, 1, 11);
// Start some of them.
for (u32 i = 1; i <= 5; i++) {
registry->StartThread(i, 0, ThreadType::Regular, 0);
}
CheckThreadQuantity(registry, 11, 6, 11);
// Finish, create and start more threads.
for (u32 i = 1; i <= 5; i++) {
registry->FinishThread(i);
if (!is_detached(i))
registry->JoinThread(i, 0);
}
for (u32 i = 6; i <= 10; i++) {
registry->StartThread(i, 0, ThreadType::Regular, 0);
}
std::vector<u32> new_tids;
for (u32 i = 11; i <= 15; i++) {
new_tids.push_back(
registry->CreateThread(get_uid(i), is_detached(i), 0, 0, nullptr));
}
ASSERT_LE(kRegistryQuarantine, 5U);
u32 exp_total = 16 - (has_quarantine ? 5 - kRegistryQuarantine : 0);
CheckThreadQuantity(registry, exp_total, 6, 11);
// Test SetThreadName and FindThread.
registry->SetThreadName(6, "six");
registry->SetThreadName(7, "seven");
EXPECT_EQ(7U, registry->FindThread(HasName, (void *)"seven"));
EXPECT_EQ(kInvalidTid, registry->FindThread(HasName, (void *)"none"));
EXPECT_EQ(0U, registry->FindThread(HasUid, (void *)get_uid(0)));
EXPECT_EQ(10U, registry->FindThread(HasUid, (void *)get_uid(10)));
EXPECT_EQ(kInvalidTid, registry->FindThread(HasUid, (void *)0x1234));
EXPECT_EQ(7U,
registry->FindThread([](ThreadContextBase *tctx,
void *) { return tctx->parent_tid == 107; },
nullptr));
EXPECT_EQ(8U,
registry->FindThread([](ThreadContextBase *tctx,
void *) { return tctx->stack_id == 208; },
nullptr));
// Detach and finish and join remaining threads.
for (u32 i = 6; i <= 10; i++) {
registry->DetachThread(i, 0);
registry->FinishThread(i);
}
for (u32 i = 0; i < new_tids.size(); i++) {
u32 tid = new_tids[i];
registry->StartThread(tid, 0, ThreadType::Regular, 0);
registry->DetachThread(tid, 0);
registry->FinishThread(tid);
}
CheckThreadQuantity(registry, exp_total, 1, 1);
// Test methods that require the caller to hold a ThreadRegistryLock.
bool has_tid[16];
internal_memset(&has_tid[0], 0, sizeof(has_tid));
{
ThreadRegistryLock l(registry);
registry->RunCallbackForEachThreadLocked(MarkUidAsPresent, &has_tid[0]);
}
for (u32 i = 0; i < exp_total; i++) {
EXPECT_TRUE(has_tid[i]);
}
{
ThreadRegistryLock l(registry);
registry->CheckLocked();
ThreadContextBase *main_thread = registry->GetThreadLocked(0);
EXPECT_EQ(main_thread, registry->FindThreadContextLocked(
HasUid, (void*)get_uid(0)));
}
EXPECT_EQ(11U, registry->GetMaxAliveThreads());
}
TEST(SanitizerCommon, ThreadRegistryTest) {
ThreadRegistry quarantine_registry(GetThreadContext<ThreadContextBase>,
kMaxRegistryThreads, kRegistryQuarantine,
0);
TestRegistry(&quarantine_registry, true);
ThreadRegistry no_quarantine_registry(GetThreadContext<ThreadContextBase>,
kMaxRegistryThreads,
kMaxRegistryThreads, 0);
TestRegistry(&no_quarantine_registry, false);
}
static const int kThreadsPerShard = 20;
static const int kNumShards = 25;
static int num_created[kNumShards + 1];
static int num_started[kNumShards + 1];
static int num_joined[kNumShards + 1];
namespace {
struct RunThreadArgs {
ThreadRegistry *registry;
uptr shard; // started from 1.
};
class TestThreadContext final : public ThreadContextBase {
public:
explicit TestThreadContext(int tid) : ThreadContextBase(tid) {}
void OnJoined(void *arg) {
uptr shard = (uptr)arg;
num_joined[shard]++;
}
void OnStarted(void *arg) {
uptr shard = (uptr)arg;
num_started[shard]++;
}
void OnCreated(void *arg) {
uptr shard = (uptr)arg;
num_created[shard]++;
}
};
} // namespace
void *RunThread(void *arg) {
RunThreadArgs *args = static_cast<RunThreadArgs*>(arg);
std::vector<int> tids;
for (int i = 0; i < kThreadsPerShard; i++)
tids.push_back(
args->registry->CreateThread(0, false, 0, (void*)args->shard));
for (int i = 0; i < kThreadsPerShard; i++)
args->registry->StartThread(tids[i], 0, ThreadType::Regular,
(void*)args->shard);
for (int i = 0; i < kThreadsPerShard; i++)
args->registry->FinishThread(tids[i]);
for (int i = 0; i < kThreadsPerShard; i++)
args->registry->JoinThread(tids[i], (void*)args->shard);
return 0;
}
static void ThreadedTestRegistry(ThreadRegistry *registry) {
// Create and start a main thread.
EXPECT_EQ(0U, registry->CreateThread(0, true, -1, 0));
registry->StartThread(0, 0, ThreadType::Regular, 0);
pthread_t threads[kNumShards];
RunThreadArgs args[kNumShards];
for (int i = 0; i < kNumShards; i++) {
args[i].registry = registry;
args[i].shard = i + 1;
PTHREAD_CREATE(&threads[i], 0, RunThread, &args[i]);
}
for (int i = 0; i < kNumShards; i++) {
PTHREAD_JOIN(threads[i], 0);
}
// Check that each thread created/started/joined correct amount
// of "threads" in thread_registry.
EXPECT_EQ(1, num_created[0]);
EXPECT_EQ(1, num_started[0]);
EXPECT_EQ(0, num_joined[0]);
for (int i = 1; i <= kNumShards; i++) {
EXPECT_EQ(kThreadsPerShard, num_created[i]);
EXPECT_EQ(kThreadsPerShard, num_started[i]);
EXPECT_EQ(kThreadsPerShard, num_joined[i]);
}
}
TEST(SanitizerCommon, ThreadRegistryThreadedTest) {
memset(&num_created, 0, sizeof(num_created));
memset(&num_started, 0, sizeof(num_created));
memset(&num_joined, 0, sizeof(num_created));
ThreadRegistry registry(GetThreadContext<TestThreadContext>,
kThreadsPerShard * kNumShards + 1, 10, 0);
ThreadedTestRegistry(®istry);
}
TEST(SanitizerCommon, PrintThreadHistory) {
ThreadRegistry registry(GetThreadContext<TestThreadContext>,
kThreadsPerShard * kNumShards + 1, 10, 0);
UNINITIALIZED BufferedStackTrace stack1;
stack1.Unwind(StackTrace::GetCurrentPc(), GET_CURRENT_FRAME(), nullptr, false,
/*max_depth=*/1);
UNINITIALIZED BufferedStackTrace stack2;
stack2.Unwind(StackTrace::GetCurrentPc(), GET_CURRENT_FRAME(), nullptr, false,
/*max_depth=*/1);
EXPECT_EQ(0U, registry.CreateThread(0, true, -1, 0, nullptr));
for (int i = 0; i < 5; i++) {
registry.CreateThread(0, true, 0, StackDepotPut(stack1), nullptr);
registry.CreateThread(0, true, 0, StackDepotPut(stack2), nullptr);
}
InternalScopedString out;
PrintThreadHistory(registry, out);
std::string substrings[] = {
"Thread T0/0 was created by T-1",
"<empty stack>",
"",
"Thread T1/0 was created by T0/0",
"Thread T3/0 was created by T0/0",
"Thread T5/0 was created by T0/0",
"Thread T7/0 was created by T0/0",
"Thread T9/0 was created by T0/0",
"#0 0x",
"",
"Thread T2/0 was created by T0/0",
"Thread T4/0 was created by T0/0",
"Thread T6/0 was created by T0/0",
"Thread T8/0 was created by T0/0",
"Thread T10/0 was created by T0/0",
"#0 0x",
"",
};
std::stringstream ss(out.data());
std::string line;
for (auto substr : substrings) {
std::getline(ss, line);
EXPECT_THAT(line, HasSubstr(substr)) << line;
}
EXPECT_FALSE(std::getline(ss, line)) << "Unmatched line: " << line;
}
} // namespace __sanitizer
|