Skip to content

Commit baf2f35

Browse files
committed
sanitizers: Introduce ThreadType enum
Replace bool workerthread flag with ThreadType enum. This change is preparation for fiber support. [dvyukov: fixed build of sanitizer_thread_registry_test.cc] Author: yuri (Yuri Per) Reviewed in: https://reviews.llvm.org/D57839 Context: https://reviews.llvm.org/D54889 llvm-svn: 353390
1 parent a822b11 commit baf2f35

18 files changed

+44
-35
lines changed

compiler-rt/lib/asan/asan_fuchsia.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ static void ThreadStartHook(void *hook, uptr os_id) {
178178
SetCurrentThread(thread);
179179

180180
// In lieu of AsanThread::ThreadStart.
181-
asanThreadRegistry().StartThread(thread->tid(), os_id, /*workerthread*/ false,
181+
asanThreadRegistry().StartThread(thread->tid(), os_id, ThreadType::Regular,
182182
nullptr);
183183
}
184184

compiler-rt/lib/asan/asan_mac.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,8 @@ void asan_register_worker_thread(int parent_tid, StackTrace *stack) {
181181
t = AsanThread::Create(/* start_routine */ nullptr, /* arg */ nullptr,
182182
parent_tid, stack, /* detached */ true);
183183
t->Init();
184-
asanThreadRegistry().StartThread(t->tid(), GetTid(),
185-
/* workerthread */ true, 0);
184+
asanThreadRegistry().StartThread(t->tid(), GetTid(), ThreadType::Worker,
185+
nullptr);
186186
SetCurrentThread(t);
187187
}
188188
}

compiler-rt/lib/asan/asan_rtems.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,8 @@ static void ThreadStartHook(void *hook, uptr os_id) {
183183
// Determine whether we are starting or restarting the thread.
184184
if (status == ThreadStatusCreated)
185185
// In lieu of AsanThread::ThreadStart.
186-
asanThreadRegistry().StartThread(thread->tid(), os_id,
187-
/*workerthread*/ false, nullptr);
186+
asanThreadRegistry().StartThread(thread->tid(), os_id, ThreadType::Regular,
187+
nullptr);
188188
else {
189189
// In a thread restart, a thread may resume execution at an
190190
// arbitrary function entry point, with its stack and TLS state

compiler-rt/lib/asan/asan_thread.cc

+1-2
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,7 @@ void AsanThread::Init(const InitOptions *options) {
245245
thread_return_t AsanThread::ThreadStart(
246246
tid_t os_id, atomic_uintptr_t *signal_thread_is_registered) {
247247
Init();
248-
asanThreadRegistry().StartThread(tid(), os_id, /*workerthread*/ false,
249-
nullptr);
248+
asanThreadRegistry().StartThread(tid(), os_id, ThreadType::Regular, nullptr);
250249
if (signal_thread_is_registered)
251250
atomic_store(signal_thread_is_registered, 1, memory_order_release);
252251

compiler-rt/lib/lsan/lsan_thread.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ u32 ThreadCreate(u32 parent_tid, uptr user_id, bool detached) {
7676
/* arg */ nullptr);
7777
}
7878

79-
void ThreadStart(u32 tid, tid_t os_id, bool workerthread) {
79+
void ThreadStart(u32 tid, tid_t os_id, ThreadType thread_type) {
8080
OnStartedArgs args;
8181
uptr stack_size = 0;
8282
uptr tls_size = 0;
@@ -86,7 +86,7 @@ void ThreadStart(u32 tid, tid_t os_id, bool workerthread) {
8686
args.tls_end = args.tls_begin + tls_size;
8787
GetAllocatorCacheRange(&args.cache_begin, &args.cache_end);
8888
args.dtls = DTLS_Get();
89-
thread_registry->StartThread(tid, os_id, workerthread, &args);
89+
thread_registry->StartThread(tid, os_id, thread_type, &args);
9090
}
9191

9292
void ThreadFinish() {

compiler-rt/lib/lsan/lsan_thread.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ class ThreadContext : public ThreadContextBase {
4444

4545
void InitializeThreadRegistry();
4646

47-
void ThreadStart(u32 tid, tid_t os_id, bool workerthread = false);
47+
void ThreadStart(u32 tid, tid_t os_id,
48+
ThreadType thread_type = ThreadType::Regular);
4849
void ThreadFinish();
4950
u32 ThreadCreate(u32 tid, uptr uid, bool detached);
5051
void ThreadJoin(u32 tid);

compiler-rt/lib/sanitizer_common/sanitizer_thread_registry.cc

+6-6
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ namespace __sanitizer {
1717

1818
ThreadContextBase::ThreadContextBase(u32 tid)
1919
: tid(tid), unique_id(0), reuse_count(), os_id(0), user_id(0),
20-
status(ThreadStatusInvalid),
21-
detached(false), workerthread(false), parent_tid(0), next(0) {
20+
status(ThreadStatusInvalid), detached(false),
21+
thread_type(ThreadType::Regular), parent_tid(0), next(0) {
2222
name[0] = '\0';
2323
atomic_store(&thread_destroyed, 0, memory_order_release);
2424
}
@@ -70,11 +70,11 @@ void ThreadContextBase::SetFinished() {
7070
OnFinished();
7171
}
7272

73-
void ThreadContextBase::SetStarted(tid_t _os_id, bool _workerthread,
73+
void ThreadContextBase::SetStarted(tid_t _os_id, ThreadType _thread_type,
7474
void *arg) {
7575
status = ThreadStatusRunning;
7676
os_id = _os_id;
77-
workerthread = _workerthread;
77+
thread_type = _thread_type;
7878
OnStarted(arg);
7979
}
8080

@@ -302,15 +302,15 @@ void ThreadRegistry::FinishThread(u32 tid) {
302302
tctx->SetDestroyed();
303303
}
304304

305-
void ThreadRegistry::StartThread(u32 tid, tid_t os_id, bool workerthread,
305+
void ThreadRegistry::StartThread(u32 tid, tid_t os_id, ThreadType thread_type,
306306
void *arg) {
307307
BlockingMutexLock l(&mtx_);
308308
running_threads_++;
309309
CHECK_LT(tid, n_contexts_);
310310
ThreadContextBase *tctx = threads_[tid];
311311
CHECK_NE(tctx, 0);
312312
CHECK_EQ(ThreadStatusCreated, tctx->status);
313-
tctx->SetStarted(os_id, workerthread, arg);
313+
tctx->SetStarted(os_id, thread_type, arg);
314314
}
315315

316316
void ThreadRegistry::QuarantinePush(ThreadContextBase *tctx) {

compiler-rt/lib/sanitizer_common/sanitizer_thread_registry.h

+8-3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ enum ThreadStatus {
2828
ThreadStatusDead // Joined, but some info is still available.
2929
};
3030

31+
enum class ThreadType {
32+
Regular, // Normal thread
33+
Worker, // macOS Grand Central Dispatch (GCD) worker thread
34+
};
35+
3136
// Generic thread context. Specific sanitizer tools may inherit from it.
3237
// If thread is dead, context may optionally be reused for a new thread.
3338
class ThreadContextBase {
@@ -44,7 +49,7 @@ class ThreadContextBase {
4449

4550
ThreadStatus status;
4651
bool detached;
47-
bool workerthread;
52+
ThreadType thread_type;
4853

4954
u32 parent_tid;
5055
ThreadContextBase *next; // For storing thread contexts in a list.
@@ -56,7 +61,7 @@ class ThreadContextBase {
5661
void SetDead();
5762
void SetJoined(void *arg);
5863
void SetFinished();
59-
void SetStarted(tid_t _os_id, bool _workerthread, void *arg);
64+
void SetStarted(tid_t _os_id, ThreadType _thread_type, void *arg);
6065
void SetCreated(uptr _user_id, u64 _unique_id, bool _detached,
6166
u32 _parent_tid, void *arg);
6267
void Reset();
@@ -120,7 +125,7 @@ class ThreadRegistry {
120125
void DetachThread(u32 tid, void *arg);
121126
void JoinThread(u32 tid, void *arg);
122127
void FinishThread(u32 tid);
123-
void StartThread(u32 tid, tid_t os_id, bool workerthread, void *arg);
128+
void StartThread(u32 tid, tid_t os_id, ThreadType thread_type, void *arg);
124129
void SetThreadUserId(u32 tid, uptr user_id);
125130

126131
private:

compiler-rt/lib/sanitizer_common/tests/sanitizer_thread_registry_test.cc

+7-6
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,15 @@ static void MarkUidAsPresent(ThreadContextBase *tctx, void *arg) {
6666
static void TestRegistry(ThreadRegistry *registry, bool has_quarantine) {
6767
// Create and start a main thread.
6868
EXPECT_EQ(0U, registry->CreateThread(get_uid(0), true, -1, 0));
69-
registry->StartThread(0, 0, false, 0);
69+
registry->StartThread(0, 0, ThreadType::Regular, 0);
7070
// Create a bunch of threads.
7171
for (u32 i = 1; i <= 10; i++) {
7272
EXPECT_EQ(i, registry->CreateThread(get_uid(i), is_detached(i), 0, 0));
7373
}
7474
CheckThreadQuantity(registry, 11, 1, 11);
7575
// Start some of them.
7676
for (u32 i = 1; i <= 5; i++) {
77-
registry->StartThread(i, 0, false, 0);
77+
registry->StartThread(i, 0, ThreadType::Regular, 0);
7878
}
7979
CheckThreadQuantity(registry, 11, 6, 11);
8080
// Finish, create and start more threads.
@@ -84,7 +84,7 @@ static void TestRegistry(ThreadRegistry *registry, bool has_quarantine) {
8484
registry->JoinThread(i, 0);
8585
}
8686
for (u32 i = 6; i <= 10; i++) {
87-
registry->StartThread(i, 0, false, 0);
87+
registry->StartThread(i, 0, ThreadType::Regular, 0);
8888
}
8989
std::vector<u32> new_tids;
9090
for (u32 i = 11; i <= 15; i++) {
@@ -111,7 +111,7 @@ static void TestRegistry(ThreadRegistry *registry, bool has_quarantine) {
111111
}
112112
for (u32 i = 0; i < new_tids.size(); i++) {
113113
u32 tid = new_tids[i];
114-
registry->StartThread(tid, 0, false, 0);
114+
registry->StartThread(tid, 0, ThreadType::Regular, 0);
115115
registry->DetachThread(tid, 0);
116116
registry->FinishThread(tid);
117117
}
@@ -188,7 +188,8 @@ void *RunThread(void *arg) {
188188
tids.push_back(
189189
args->registry->CreateThread(0, false, 0, (void*)args->shard));
190190
for (int i = 0; i < kThreadsPerShard; i++)
191-
args->registry->StartThread(tids[i], 0, false, (void*)args->shard);
191+
args->registry->StartThread(tids[i], 0, ThreadType::Regular,
192+
(void*)args->shard);
192193
for (int i = 0; i < kThreadsPerShard; i++)
193194
args->registry->FinishThread(tids[i]);
194195
for (int i = 0; i < kThreadsPerShard; i++)
@@ -199,7 +200,7 @@ void *RunThread(void *arg) {
199200
static void ThreadedTestRegistry(ThreadRegistry *registry) {
200201
// Create and start a main thread.
201202
EXPECT_EQ(0U, registry->CreateThread(0, true, -1, 0));
202-
registry->StartThread(0, 0, false, 0);
203+
registry->StartThread(0, 0, ThreadType::Regular, 0);
203204
pthread_t threads[kNumShards];
204205
RunThreadArgs args[kNumShards];
205206
for (int i = 0; i < kNumShards; i++) {

compiler-rt/lib/tsan/go/tsan_go.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ void __tsan_go_start(ThreadState *parent, ThreadState **pthr, void *pc) {
213213
ThreadState *thr = AllocGoroutine();
214214
*pthr = thr;
215215
int goid = ThreadCreate(parent, (uptr)pc, 0, true);
216-
ThreadStart(thr, goid, 0, /*workerthread*/ false);
216+
ThreadStart(thr, goid, 0, ThreadType::Regular);
217217
}
218218

219219
void __tsan_go_end(ThreadState *thr) {

compiler-rt/lib/tsan/rtl/tsan_interceptors.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -958,7 +958,7 @@ extern "C" void *__tsan_thread_start_func(void *arg) {
958958
internal_sched_yield();
959959
Processor *proc = ProcCreate();
960960
ProcWire(proc, thr);
961-
ThreadStart(thr, tid, GetTid(), /*workerthread*/ false);
961+
ThreadStart(thr, tid, GetTid(), ThreadType::Regular);
962962
atomic_store(&p->tid, 0, memory_order_release);
963963
}
964964
void *res = callback(param);

compiler-rt/lib/tsan/rtl/tsan_platform_mac.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ static void my_pthread_introspection_hook(unsigned int event, pthread_t thread,
212212
ThreadState *parent_thread_state = nullptr; // No parent.
213213
int tid = ThreadCreate(parent_thread_state, 0, (uptr)thread, true);
214214
CHECK_NE(tid, 0);
215-
ThreadStart(thr, tid, GetTid(), /*workerthread*/ true);
215+
ThreadStart(thr, tid, GetTid(), ThreadType::Worker);
216216
}
217217
} else if (event == PTHREAD_INTROSPECTION_THREAD_TERMINATE) {
218218
if (thread == pthread_self()) {

compiler-rt/lib/tsan/rtl/tsan_report.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ static void PrintThread(const ReportThread *rt) {
257257
Printf(" '%s'", rt->name);
258258
char thrbuf[kThreadBufSize];
259259
const char *thread_status = rt->running ? "running" : "finished";
260-
if (rt->workerthread) {
260+
if (rt->thread_type == ThreadType::Worker) {
261261
Printf(" (tid=%zu, %s) is a GCD worker thread\n", rt->os_id, thread_status);
262262
Printf("\n");
263263
Printf("%s", d.Default());

compiler-rt/lib/tsan/rtl/tsan_report.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#define TSAN_REPORT_H
1414

1515
#include "sanitizer_common/sanitizer_symbolizer.h"
16+
#include "sanitizer_common/sanitizer_thread_registry.h"
1617
#include "sanitizer_common/sanitizer_vector.h"
1718
#include "tsan_defs.h"
1819

@@ -91,7 +92,7 @@ struct ReportThread {
9192
int id;
9293
tid_t os_id;
9394
bool running;
94-
bool workerthread;
95+
ThreadType thread_type;
9596
char *name;
9697
u32 parent_tid;
9798
ReportStack *stack;

compiler-rt/lib/tsan/rtl/tsan_rtl.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ void Initialize(ThreadState *thr) {
396396
// Initialize thread 0.
397397
int tid = ThreadCreate(thr, 0, 0, true);
398398
CHECK_EQ(tid, 0);
399-
ThreadStart(thr, tid, GetTid(), /*workerthread*/ false);
399+
ThreadStart(thr, tid, GetTid(), ThreadType::Regular);
400400
#if TSAN_CONTAINS_UBSAN
401401
__ubsan::InitAsPlugin();
402402
#endif

compiler-rt/lib/tsan/rtl/tsan_rtl.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -764,7 +764,8 @@ void FuncEntry(ThreadState *thr, uptr pc);
764764
void FuncExit(ThreadState *thr);
765765

766766
int ThreadCreate(ThreadState *thr, uptr pc, uptr uid, bool detached);
767-
void ThreadStart(ThreadState *thr, int tid, tid_t os_id, bool workerthread);
767+
void ThreadStart(ThreadState *thr, int tid, tid_t os_id,
768+
ThreadType thread_type);
768769
void ThreadFinish(ThreadState *thr);
769770
int ThreadTid(ThreadState *thr, uptr pc, uptr uid);
770771
void ThreadJoin(ThreadState *thr, uptr pc, int tid);

compiler-rt/lib/tsan/rtl/tsan_rtl_report.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ void ScopedReportBase::AddThread(const ThreadContext *tctx, bool suppressable) {
201201
rt->running = (tctx->status == ThreadStatusRunning);
202202
rt->name = internal_strdup(tctx->name);
203203
rt->parent_tid = tctx->parent_tid;
204-
rt->workerthread = tctx->workerthread;
204+
rt->thread_type = tctx->thread_type;
205205
rt->stack = 0;
206206
rt->stack = SymbolizeStackId(tctx->creation_stack_id);
207207
if (rt->stack)

compiler-rt/lib/tsan/rtl/tsan_rtl_thread.cc

+3-2
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,8 @@ int ThreadCreate(ThreadState *thr, uptr pc, uptr uid, bool detached) {
239239
return tid;
240240
}
241241

242-
void ThreadStart(ThreadState *thr, int tid, tid_t os_id, bool workerthread) {
242+
void ThreadStart(ThreadState *thr, int tid, tid_t os_id,
243+
ThreadType thread_type) {
243244
uptr stk_addr = 0;
244245
uptr stk_size = 0;
245246
uptr tls_addr = 0;
@@ -257,7 +258,7 @@ void ThreadStart(ThreadState *thr, int tid, tid_t os_id, bool workerthread) {
257258

258259
ThreadRegistry *tr = ctx->thread_registry;
259260
OnStartedArgs args = { thr, stk_addr, stk_size, tls_addr, tls_size };
260-
tr->StartThread(tid, os_id, workerthread, &args);
261+
tr->StartThread(tid, os_id, thread_type, &args);
261262

262263
tr->Lock();
263264
thr->tctx = (ThreadContext*)tr->GetThreadLocked(tid);

0 commit comments

Comments
 (0)