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