sanitizer_thread_registry_test.cc revision 53177247698bfba075f2d5b255a447fc3ced6976
1//===-- sanitizer_thread_registry_test.cc ---------------------------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file is a part of shared sanitizer runtime. 11// 12//===----------------------------------------------------------------------===// 13#include "sanitizer_common/sanitizer_thread_registry.h" 14#include "gtest/gtest.h" 15 16#include <vector> 17 18namespace __sanitizer { 19 20static BlockingMutex tctx_allocator_lock(LINKER_INITIALIZED); 21static LowLevelAllocator tctx_allocator; 22 23template<typename TCTX> 24static ThreadContextBase *GetThreadContext(u32 tid) { 25 BlockingMutexLock l(&tctx_allocator_lock); 26 return new(tctx_allocator) TCTX(tid); 27} 28 29static const u32 kMaxRegistryThreads = 1000; 30static const u32 kRegistryQuarantine = 2; 31 32static void CheckThreadQuantity(ThreadRegistry *registry, uptr exp_total, 33 uptr exp_running, uptr exp_alive) { 34 uptr total, running, alive; 35 registry->GetNumberOfThreads(&total, &running, &alive); 36 EXPECT_EQ(exp_total, total); 37 EXPECT_EQ(exp_running, running); 38 EXPECT_EQ(exp_alive, alive); 39} 40 41static bool is_detached(u32 tid) { 42 return (tid % 2 == 0); 43} 44 45static uptr get_uid(u32 tid) { 46 return tid * 2; 47} 48 49static bool HasName(ThreadContextBase *tctx, void *arg) { 50 char *name = (char*)arg; 51 return (tctx->name && 0 == internal_strcmp(tctx->name, name)); 52} 53 54static bool HasUid(ThreadContextBase *tctx, void *arg) { 55 uptr uid = (uptr)arg; 56 return (tctx->user_id == uid); 57} 58 59static void MarkUidAsPresent(ThreadContextBase *tctx, void *arg) { 60 bool *arr = (bool*)arg; 61 arr[tctx->tid] = true; 62} 63 64static void TestRegistry(ThreadRegistry *registry, bool has_quarantine) { 65 // Create and start a main thread. 66 EXPECT_EQ(0U, registry->CreateThread(get_uid(0), true, -1, 0)); 67 registry->StartThread(0, 0, 0); 68 // Create a bunch of threads. 69 for (u32 i = 1; i <= 10; i++) { 70 EXPECT_EQ(i, registry->CreateThread(get_uid(i), is_detached(i), 0, 0)); 71 } 72 CheckThreadQuantity(registry, 11, 1, 11); 73 // Start some of them. 74 for (u32 i = 1; i <= 5; i++) { 75 registry->StartThread(i, 0, 0); 76 } 77 CheckThreadQuantity(registry, 11, 6, 11); 78 // Finish, create and start more threads. 79 for (u32 i = 1; i <= 5; i++) { 80 registry->FinishThread(i); 81 if (!is_detached(i)) 82 registry->JoinThread(i, 0); 83 } 84 for (u32 i = 6; i <= 10; i++) { 85 registry->StartThread(i, 0, 0); 86 } 87 std::vector<u32> new_tids; 88 for (u32 i = 11; i <= 15; i++) { 89 new_tids.push_back( 90 registry->CreateThread(get_uid(i), is_detached(i), 0, 0)); 91 } 92 ASSERT_LE(kRegistryQuarantine, 5U); 93 u32 exp_total = 16 - (has_quarantine ? 5 - kRegistryQuarantine : 0); 94 CheckThreadQuantity(registry, exp_total, 6, 11); 95 // Test SetThreadName and FindThread. 96 registry->SetThreadName(6, "six"); 97 registry->SetThreadName(7, "seven"); 98 EXPECT_EQ(7U, registry->FindThread(HasName, (void*)"seven")); 99 EXPECT_EQ(ThreadRegistry::kUnknownTid, 100 registry->FindThread(HasName, (void*)"none")); 101 EXPECT_EQ(0U, registry->FindThread(HasUid, (void*)get_uid(0))); 102 EXPECT_EQ(10U, registry->FindThread(HasUid, (void*)get_uid(10))); 103 EXPECT_EQ(ThreadRegistry::kUnknownTid, 104 registry->FindThread(HasUid, (void*)0x1234)); 105 // Detach and finish and join remaining threads. 106 for (u32 i = 6; i <= 10; i++) { 107 registry->DetachThread(i); 108 registry->FinishThread(i); 109 } 110 for (u32 i = 0; i < new_tids.size(); i++) { 111 u32 tid = new_tids[i]; 112 registry->StartThread(tid, 0, 0); 113 registry->DetachThread(tid); 114 registry->FinishThread(tid); 115 } 116 CheckThreadQuantity(registry, exp_total, 1, 1); 117 // Test methods that require the caller to hold a ThreadRegistryLock. 118 bool has_tid[16]; 119 internal_memset(&has_tid[0], 0, sizeof(has_tid)); 120 { 121 ThreadRegistryLock l(registry); 122 registry->RunCallbackForEachThreadLocked(MarkUidAsPresent, &has_tid[0]); 123 } 124 for (u32 i = 0; i < exp_total; i++) { 125 EXPECT_TRUE(has_tid[i]); 126 } 127 { 128 ThreadRegistryLock l(registry); 129 registry->CheckLocked(); 130 ThreadContextBase *main_thread = registry->GetThreadLocked(0); 131 EXPECT_EQ(main_thread, registry->FindThreadContextLocked( 132 HasUid, (void*)get_uid(0))); 133 } 134 EXPECT_EQ(11U, registry->GetMaxAliveThreads()); 135} 136 137TEST(SanitizerCommon, ThreadRegistryTest) { 138 ThreadRegistry quarantine_registry(GetThreadContext<ThreadContextBase>, 139 kMaxRegistryThreads, 140 kRegistryQuarantine); 141 TestRegistry(&quarantine_registry, true); 142 143 ThreadRegistry no_quarantine_registry(GetThreadContext<ThreadContextBase>, 144 kMaxRegistryThreads, 145 kMaxRegistryThreads); 146 TestRegistry(&no_quarantine_registry, false); 147} 148 149static const int kThreadsPerShard = 20; 150static const int kNumShards = 25; 151 152static int num_created[kNumShards + 1]; 153static int num_started[kNumShards + 1]; 154static int num_joined[kNumShards + 1]; 155 156namespace { 157 158struct RunThreadArgs { 159 ThreadRegistry *registry; 160 uptr shard; // started from 1. 161}; 162 163class TestThreadContext : public ThreadContextBase { 164 public: 165 explicit TestThreadContext(int tid) : ThreadContextBase(tid) {} 166 void OnJoined(void *arg) { 167 uptr shard = (uptr)arg; 168 num_joined[shard]++; 169 } 170 void OnStarted(void *arg) { 171 uptr shard = (uptr)arg; 172 num_started[shard]++; 173 } 174 void OnCreated(void *arg) { 175 uptr shard = (uptr)arg; 176 num_created[shard]++; 177 } 178}; 179 180} // namespace 181 182void *RunThread(void *arg) { 183 RunThreadArgs *args = static_cast<RunThreadArgs*>(arg); 184 std::vector<int> tids; 185 for (int i = 0; i < kThreadsPerShard; i++) 186 tids.push_back( 187 args->registry->CreateThread(0, false, 0, (void*)args->shard)); 188 for (int i = 0; i < kThreadsPerShard; i++) 189 args->registry->StartThread(tids[i], 0, (void*)args->shard); 190 for (int i = 0; i < kThreadsPerShard; i++) 191 args->registry->FinishThread(tids[i]); 192 for (int i = 0; i < kThreadsPerShard; i++) 193 args->registry->JoinThread(tids[i], (void*)args->shard); 194 return 0; 195} 196 197static void ThreadedTestRegistry(ThreadRegistry *registry) { 198 // Create and start a main thread. 199 EXPECT_EQ(0U, registry->CreateThread(0, true, -1, 0)); 200 registry->StartThread(0, 0, 0); 201 pthread_t threads[kNumShards]; 202 RunThreadArgs args[kNumShards]; 203 for (int i = 0; i < kNumShards; i++) { 204 args[i].registry = registry; 205 args[i].shard = i + 1; 206 pthread_create(&threads[i], 0, RunThread, &args[i]); 207 } 208 for (int i = 0; i < kNumShards; i++) { 209 pthread_join(threads[i], 0); 210 } 211 // Check that each thread created/started/joined correct amount 212 // of "threads" in thread_registry. 213 EXPECT_EQ(1, num_created[0]); 214 EXPECT_EQ(1, num_started[0]); 215 EXPECT_EQ(0, num_joined[0]); 216 for (int i = 1; i <= kNumShards; i++) { 217 EXPECT_EQ(kThreadsPerShard, num_created[i]); 218 EXPECT_EQ(kThreadsPerShard, num_started[i]); 219 EXPECT_EQ(kThreadsPerShard, num_joined[i]); 220 } 221} 222 223TEST(SanitizerCommon, ThreadRegistryThreadedTest) { 224 ThreadRegistry registry(GetThreadContext<TestThreadContext>, 225 kThreadsPerShard * kNumShards + 1, 10); 226 ThreadedTestRegistry(®istry); 227} 228 229} // namespace __sanitizer 230