Skip to content

Commit 92a3a2d

Browse files
committed
sanitizer_common: introduce kInvalidTid/kMainTid
Currently we have a bit of a mess related to tids: - sanitizers re-declare kInvalidTid multiple times - some call it kUnknownTid - implicit assumptions that main tid is 0 - asan/memprof claim their tids need to fit into 24 bits, but this does not seem to be true anymore - inconsistent use of u32/int to store tids Introduce kInvalidTid/kMainTid in sanitizer_common and use them consistently. Reviewed By: vitalybuka Differential Revision: https://reviews.llvm.org/D101428
1 parent 4978bf6 commit 92a3a2d

23 files changed

+77
-83
lines changed

compiler-rt/lib/asan/asan_allocator.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ struct Allocator {
476476
return false;
477477
if (m->Beg() != addr) return false;
478478
AsanThread *t = GetCurrentThread();
479-
m->SetAllocContext(t ? t->tid() : 0, StackDepotPut(*stack));
479+
m->SetAllocContext(t ? t->tid() : kMainTid, StackDepotPut(*stack));
480480
return true;
481481
}
482482

@@ -570,7 +570,7 @@ struct Allocator {
570570
m->SetUsedSize(size);
571571
m->user_requested_alignment_log = user_requested_alignment_log;
572572

573-
m->SetAllocContext(t ? t->tid() : 0, StackDepotPut(*stack));
573+
m->SetAllocContext(t ? t->tid() : kMainTid, StackDepotPut(*stack));
574574

575575
uptr size_rounded_down_to_granularity =
576576
RoundDownTo(size, SHADOW_GRANULARITY);

compiler-rt/lib/asan/asan_descriptions.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ void DescribeThread(AsanThreadContext *context) {
4444
CHECK(context);
4545
asanThreadRegistry().CheckLocked();
4646
// No need to announce the main thread.
47-
if (context->tid == 0 || context->announced) {
47+
if (context->tid == kMainTid || context->announced) {
4848
return;
4949
}
5050
context->announced = true;

compiler-rt/lib/asan/asan_thread.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ FakeStack *AsanThread::AsyncSignalSafeLazyInitFakeStack() {
228228
}
229229

230230
void AsanThread::Init(const InitOptions *options) {
231-
DCHECK_NE(tid(), ThreadRegistry::kUnknownTid);
231+
DCHECK_NE(tid(), kInvalidTid);
232232
next_stack_top_ = next_stack_bottom_ = 0;
233233
atomic_store(&stack_switching_, false, memory_order_release);
234234
CHECK_EQ(this->stack_size(), 0U);
@@ -291,7 +291,7 @@ thread_return_t AsanThread::ThreadStart(tid_t os_id) {
291291

292292
AsanThread *CreateMainThread() {
293293
AsanThread *main_thread = AsanThread::Create(
294-
/* start_routine */ nullptr, /* arg */ nullptr, /* parent_tid */ 0,
294+
/* start_routine */ nullptr, /* arg */ nullptr, /* parent_tid */ kMainTid,
295295
/* stack */ nullptr, /* detached */ true);
296296
SetCurrentThread(main_thread);
297297
main_thread->ThreadStart(internal_getpid());
@@ -305,8 +305,8 @@ void AsanThread::SetThreadStackAndTls(const InitOptions *options) {
305305
DCHECK_EQ(options, nullptr);
306306
uptr tls_size = 0;
307307
uptr stack_size = 0;
308-
GetThreadStackAndTls(tid() == 0, &stack_bottom_, &stack_size, &tls_begin_,
309-
&tls_size);
308+
GetThreadStackAndTls(tid() == kMainTid, &stack_bottom_, &stack_size,
309+
&tls_begin_, &tls_size);
310310
stack_top_ = RoundDownTo(stack_bottom_ + stack_size, SHADOW_GRANULARITY);
311311
tls_end_ = tls_begin_ + tls_size;
312312
dtls_ = DTLS_Get();
@@ -431,7 +431,7 @@ AsanThread *GetCurrentThread() {
431431
// address. We are not entirely sure that we have correct main thread
432432
// limits, so only do this magic on Android, and only if the found thread
433433
// is the main thread.
434-
AsanThreadContext *tctx = GetThreadContextByTidLocked(0);
434+
AsanThreadContext *tctx = GetThreadContextByTidLocked(kMainTid);
435435
if (tctx && ThreadStackContainsAddress(tctx, &context)) {
436436
SetCurrentThread(tctx->thread);
437437
return tctx->thread;
@@ -468,7 +468,7 @@ AsanThread *FindThreadByStackAddress(uptr addr) {
468468
void EnsureMainThreadIDIsCorrect() {
469469
AsanThreadContext *context =
470470
reinterpret_cast<AsanThreadContext *>(AsanTSDGet());
471-
if (context && (context->tid == 0))
471+
if (context && (context->tid == kMainTid))
472472
context->os_id = GetTid();
473473
}
474474

compiler-rt/lib/asan/asan_thread.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ struct DTLS;
2828

2929
namespace __asan {
3030

31-
const u32 kInvalidTid = 0xffffff; // Must fit into 24 bits.
3231
const u32 kMaxNumberOfThreads = (1 << 22); // 4M
3332

3433
class AsanThread;

compiler-rt/lib/lsan/lsan_common.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,6 @@ enum ChunkTag {
6666
kIgnored = 3
6767
};
6868

69-
const u32 kInvalidTid = (u32) -1;
70-
7169
struct Flags {
7270
#define LSAN_FLAG(Type, Name, DefaultValue, Description) Type Name;
7371
#include "lsan_flags.inc"

compiler-rt/lib/lsan/lsan_interceptors.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ INTERCEPTOR(int, pthread_create, void *th, void *attr,
460460
if (res == 0) {
461461
int tid = ThreadCreate(GetCurrentThread(), *(uptr *)th,
462462
IsStateDetached(detached));
463-
CHECK_NE(tid, 0);
463+
CHECK_NE(tid, kMainTid);
464464
atomic_store(&p.tid, tid, memory_order_release);
465465
while (atomic_load(&p.tid, memory_order_acquire) != 0)
466466
internal_sched_yield();

compiler-rt/lib/lsan/lsan_posix.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ void ThreadStart(u32 tid, tid_t os_id, ThreadType thread_type) {
4848
OnStartedArgs args;
4949
uptr stack_size = 0;
5050
uptr tls_size = 0;
51-
GetThreadStackAndTls(tid == 0, &args.stack_begin, &stack_size,
51+
GetThreadStackAndTls(tid == kMainTid, &args.stack_begin, &stack_size,
5252
&args.tls_begin, &tls_size);
5353
args.stack_end = args.stack_begin + stack_size;
5454
args.tls_end = args.tls_begin + tls_size;
@@ -75,8 +75,8 @@ bool GetThreadRangesLocked(tid_t os_id, uptr *stack_begin, uptr *stack_end,
7575
}
7676

7777
void InitializeMainThread() {
78-
u32 tid = ThreadCreate(0, 0, true);
79-
CHECK_EQ(tid, 0);
78+
u32 tid = ThreadCreate(kMainTid, 0, true);
79+
CHECK_EQ(tid, kMainTid);
8080
ThreadStart(tid, GetTid());
8181
}
8282

compiler-rt/lib/lsan/lsan_thread.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ void ThreadJoin(u32 tid) {
9494
}
9595

9696
void EnsureMainThreadIDIsCorrect() {
97-
if (GetCurrentThread() == 0)
97+
if (GetCurrentThread() == kMainTid)
9898
CurrentThreadContext()->os_id = GetTid();
9999
}
100100

compiler-rt/lib/memprof/memprof_descriptions.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ void DescribeThread(MemprofThreadContext *context) {
4444
CHECK(context);
4545
memprofThreadRegistry().CheckLocked();
4646
// No need to announce the main thread.
47-
if (context->tid == 0 || context->announced) {
47+
if (context->tid == kMainTid || context->announced) {
4848
return;
4949
}
5050
context->announced = true;

compiler-rt/lib/memprof/memprof_thread.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ MemprofThread::ThreadStart(tid_t os_id,
156156

157157
MemprofThread *CreateMainThread() {
158158
MemprofThread *main_thread = MemprofThread::Create(
159-
/* start_routine */ nullptr, /* arg */ nullptr, /* parent_tid */ 0,
159+
/* start_routine */ nullptr, /* arg */ nullptr, /* parent_tid */ kMainTid,
160160
/* stack */ nullptr, /* detached */ true);
161161
SetCurrentThread(main_thread);
162162
main_thread->ThreadStart(internal_getpid(),
@@ -171,8 +171,8 @@ void MemprofThread::SetThreadStackAndTls(const InitOptions *options) {
171171
DCHECK_EQ(options, nullptr);
172172
uptr tls_size = 0;
173173
uptr stack_size = 0;
174-
GetThreadStackAndTls(tid() == 0, &stack_bottom_, &stack_size, &tls_begin_,
175-
&tls_size);
174+
GetThreadStackAndTls(tid() == kMainTid, &stack_bottom_, &stack_size,
175+
&tls_begin_, &tls_size);
176176
stack_top_ = stack_bottom_ + stack_size;
177177
tls_end_ = tls_begin_ + tls_size;
178178
dtls_ = DTLS_Get();
@@ -214,7 +214,7 @@ u32 GetCurrentTidOrInvalid() {
214214
void EnsureMainThreadIDIsCorrect() {
215215
MemprofThreadContext *context =
216216
reinterpret_cast<MemprofThreadContext *>(TSDGet());
217-
if (context && (context->tid == 0))
217+
if (context && (context->tid == kMainTid))
218218
context->os_id = GetTid();
219219
}
220220
} // namespace __memprof

compiler-rt/lib/memprof/memprof_thread.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ struct DTLS;
2727

2828
namespace __memprof {
2929

30-
const u32 kInvalidTid = 0xffffff; // Must fit into 24 bits.
3130
const u32 kMaxNumberOfThreads = (1 << 22); // 4M
3231

3332
class MemprofThread;

compiler-rt/lib/sanitizer_common/sanitizer_internal_defs.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,9 @@ inline void Trap() {
409409
(void)enable_fp; \
410410
} while (0)
411411

412+
constexpr u32 kInvalidTid = -1;
413+
constexpr u32 kMainTid = 0;
414+
412415
} // namespace __sanitizer
413416

414417
namespace __asan {

compiler-rt/lib/sanitizer_common/sanitizer_thread_registry.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ void ThreadContextBase::SetCreated(uptr _user_id, u64 _unique_id,
8585
unique_id = _unique_id;
8686
detached = _detached;
8787
// Parent tid makes no sense for the main thread.
88-
if (tid != 0)
88+
if (tid != kMainTid)
8989
parent_tid = _parent_tid;
9090
OnCreated(arg);
9191
}
@@ -99,8 +99,6 @@ void ThreadContextBase::Reset() {
9999

100100
// ThreadRegistry implementation.
101101

102-
const u32 ThreadRegistry::kUnknownTid = ~0U;
103-
104102
ThreadRegistry::ThreadRegistry(ThreadContextFactory factory, u32 max_threads,
105103
u32 thread_quarantine_size, u32 max_reuse)
106104
: context_factory_(factory),
@@ -135,7 +133,7 @@ uptr ThreadRegistry::GetMaxAliveThreads() {
135133
u32 ThreadRegistry::CreateThread(uptr user_id, bool detached, u32 parent_tid,
136134
void *arg) {
137135
BlockingMutexLock l(&mtx_);
138-
u32 tid = kUnknownTid;
136+
u32 tid = kInvalidTid;
139137
ThreadContextBase *tctx = QuarantinePop();
140138
if (tctx) {
141139
tid = tctx->tid;
@@ -155,7 +153,7 @@ u32 ThreadRegistry::CreateThread(uptr user_id, bool detached, u32 parent_tid,
155153
Die();
156154
}
157155
CHECK_NE(tctx, 0);
158-
CHECK_NE(tid, kUnknownTid);
156+
CHECK_NE(tid, kInvalidTid);
159157
CHECK_LT(tid, max_threads_);
160158
CHECK_EQ(tctx->status, ThreadStatusInvalid);
161159
alive_threads_++;
@@ -186,7 +184,7 @@ u32 ThreadRegistry::FindThread(FindThreadCallback cb, void *arg) {
186184
if (tctx != 0 && cb(tctx, arg))
187185
return tctx->tid;
188186
}
189-
return kUnknownTid;
187+
return kInvalidTid;
190188
}
191189

192190
ThreadContextBase *

compiler-rt/lib/sanitizer_common/sanitizer_thread_registry.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,6 @@ typedef ThreadContextBase* (*ThreadContextFactory)(u32 tid);
8787

8888
class ThreadRegistry {
8989
public:
90-
static const u32 kUnknownTid;
91-
9290
ThreadRegistry(ThreadContextFactory factory, u32 max_threads,
9391
u32 thread_quarantine_size, u32 max_reuse = 0);
9492
void GetNumberOfThreads(uptr *total = nullptr, uptr *running = nullptr,
@@ -113,7 +111,7 @@ class ThreadRegistry {
113111
void RunCallbackForEachThreadLocked(ThreadCallback cb, void *arg);
114112

115113
typedef bool (*FindThreadCallback)(ThreadContextBase *tctx, void *arg);
116-
// Finds a thread using the provided callback. Returns kUnknownTid if no
114+
// Finds a thread using the provided callback. Returns kInvalidTid if no
117115
// thread is found.
118116
u32 FindThread(FindThreadCallback cb, void *arg);
119117
// Should be guarded by ThreadRegistryLock. Return 0 if no thread

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,10 @@ static void TestRegistry(ThreadRegistry *registry, bool has_quarantine) {
9898
registry->SetThreadName(6, "six");
9999
registry->SetThreadName(7, "seven");
100100
EXPECT_EQ(7U, registry->FindThread(HasName, (void*)"seven"));
101-
EXPECT_EQ(ThreadRegistry::kUnknownTid,
102-
registry->FindThread(HasName, (void*)"none"));
101+
EXPECT_EQ(kInvalidTid, registry->FindThread(HasName, (void *)"none"));
103102
EXPECT_EQ(0U, registry->FindThread(HasUid, (void*)get_uid(0)));
104103
EXPECT_EQ(10U, registry->FindThread(HasUid, (void*)get_uid(10)));
105-
EXPECT_EQ(ThreadRegistry::kUnknownTid,
106-
registry->FindThread(HasUid, (void*)0x1234));
104+
EXPECT_EQ(kInvalidTid, registry->FindThread(HasUid, (void *)0x1234));
107105
// Detach and finish and join remaining threads.
108106
for (u32 i = 6; i <= 10; i++) {
109107
registry->DetachThread(i, 0);

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ class SyncClock {
6363
friend class ThreadClock;
6464
friend class Iter;
6565
static const uptr kDirtyTids = 2;
66+
// Full kInvalidTid won't fit into Dirty::tid.
67+
static const u64 kInvalidTid = (1ull << (64 - kClkBits)) - 1;
6668

6769
struct Dirty {
6870
u64 epoch : kClkBits;
@@ -146,6 +148,7 @@ class ThreadClock {
146148

147149
private:
148150
static const uptr kDirtyTids = SyncClock::kDirtyTids;
151+
static const u64 kInvalidTid = SyncClock::kInvalidTid;
149152
// Index of the thread associated with he clock ("current thread").
150153
const unsigned tid_;
151154
const unsigned reused_; // tid_ reuse count.

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,6 @@ const bool kCollectHistory = false;
9898
const bool kCollectHistory = true;
9999
#endif
100100

101-
const u16 kInvalidTid = kMaxTid + 1;
102-
103101
// The following "build consistency" machinery ensures that all source files
104102
// are built in the same configuration. Inconsistent builds lead to
105103
// hard to debug crashes.

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ ReportDesc::~ReportDesc() {
6969

7070
const int kThreadBufSize = 32;
7171
const char *thread_name(char *buf, int tid) {
72-
if (tid == 0)
72+
if (tid == kMainTid)
7373
return "main thread";
7474
internal_snprintf(buf, kThreadBufSize, "thread T%d", tid);
7575
return buf;
@@ -250,7 +250,7 @@ static void PrintMutex(const ReportMutex *rm) {
250250

251251
static void PrintThread(const ReportThread *rt) {
252252
Decorator d;
253-
if (rt->id == 0) // Little sense in describing the main thread.
253+
if (rt->id == kMainTid) // Little sense in describing the main thread.
254254
return;
255255
Printf("%s", d.ThreadDescription());
256256
Printf(" Thread T%d", rt->id);
@@ -394,7 +394,7 @@ void PrintReport(const ReportDesc *rep) {
394394

395395
#else // #if !SANITIZER_GO
396396

397-
const int kMainThreadId = 1;
397+
const u32 kMainGoroutineId = 1;
398398

399399
void PrintStack(const ReportStack *ent) {
400400
if (ent == 0 || ent->frames == 0) {
@@ -415,7 +415,7 @@ static void PrintMop(const ReportMop *mop, bool first) {
415415
Printf("%s at %p by ",
416416
(first ? (mop->write ? "Write" : "Read")
417417
: (mop->write ? "Previous write" : "Previous read")), mop->addr);
418-
if (mop->tid == kMainThreadId)
418+
if (mop->tid == kMainGoroutineId)
419419
Printf("main goroutine:\n");
420420
else
421421
Printf("goroutine %d:\n", mop->tid);
@@ -428,7 +428,7 @@ static void PrintLocation(const ReportLocation *loc) {
428428
Printf("\n");
429429
Printf("Heap block of size %zu at %p allocated by ",
430430
loc->heap_chunk_size, loc->heap_chunk_start);
431-
if (loc->tid == kMainThreadId)
431+
if (loc->tid == kMainGoroutineId)
432432
Printf("main goroutine:\n");
433433
else
434434
Printf("goroutine %d:\n", loc->tid);
@@ -448,7 +448,7 @@ static void PrintLocation(const ReportLocation *loc) {
448448
}
449449

450450
static void PrintThread(const ReportThread *rt) {
451-
if (rt->id == kMainThreadId)
451+
if (rt->id == kMainGoroutineId)
452452
return;
453453
Printf("\n");
454454
Printf("Goroutine %d (%s) created at:\n",

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

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -129,27 +129,30 @@ Context::Context()
129129
}
130130

131131
// The objects are allocated in TLS, so one may rely on zero-initialization.
132-
ThreadState::ThreadState(Context *ctx, int tid, int unique_id, u64 epoch,
133-
unsigned reuse_count,
134-
uptr stk_addr, uptr stk_size,
132+
ThreadState::ThreadState(Context *ctx, u32 tid, int unique_id, u64 epoch,
133+
unsigned reuse_count, uptr stk_addr, uptr stk_size,
135134
uptr tls_addr, uptr tls_size)
136-
: fast_state(tid, epoch)
137-
// Do not touch these, rely on zero initialization,
138-
// they may be accessed before the ctor.
139-
// , ignore_reads_and_writes()
140-
// , ignore_interceptors()
141-
, clock(tid, reuse_count)
135+
: fast_state(tid, epoch)
136+
// Do not touch these, rely on zero initialization,
137+
// they may be accessed before the ctor.
138+
// , ignore_reads_and_writes()
139+
// , ignore_interceptors()
140+
,
141+
clock(tid, reuse_count)
142142
#if !SANITIZER_GO
143-
, jmp_bufs()
143+
,
144+
jmp_bufs()
144145
#endif
145-
, tid(tid)
146-
, unique_id(unique_id)
147-
, stk_addr(stk_addr)
148-
, stk_size(stk_size)
149-
, tls_addr(tls_addr)
150-
, tls_size(tls_size)
146+
,
147+
tid(tid),
148+
unique_id(unique_id),
149+
stk_addr(stk_addr),
150+
stk_size(stk_size),
151+
tls_addr(tls_addr),
152+
tls_size(tls_size)
151153
#if !SANITIZER_GO
152-
, last_sleep_clock(tid)
154+
,
155+
last_sleep_clock(tid)
153156
#endif
154157
{
155158
}

0 commit comments

Comments
 (0)