Skip to content

Commit ceb30b0

Browse files
committed
[sanitizer] Introduce tid_t as a typedef for OS-provided thread IDs
We seem to assume that OS-provided thread IDs are either uptr or int, neither of which is true on Darwin. This introduces a tid_t type, which holds a OS-provided thread ID (gettid on Linux, pthread_threadid_np on Darwin, pthread_self on FreeBSD). Differential Revision: https://reviews.llvm.org/D31774 llvm-svn: 300473
1 parent dfe2ffe commit ceb30b0

23 files changed

+52
-48
lines changed

compiler-rt/lib/asan/asan_thread.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ void AsanThread::Init() {
237237
}
238238

239239
thread_return_t AsanThread::ThreadStart(
240-
uptr os_id, atomic_uintptr_t *signal_thread_is_registered) {
240+
tid_t os_id, atomic_uintptr_t *signal_thread_is_registered) {
241241
Init();
242242
asanThreadRegistry().StartThread(tid(), os_id, /*workerthread*/ false,
243243
nullptr);
@@ -395,7 +395,7 @@ void EnsureMainThreadIDIsCorrect() {
395395
context->os_id = GetTid();
396396
}
397397

398-
__asan::AsanThread *GetAsanThreadByOsIDLocked(uptr os_id) {
398+
__asan::AsanThread *GetAsanThreadByOsIDLocked(tid_t os_id) {
399399
__asan::AsanThreadContext *context = static_cast<__asan::AsanThreadContext *>(
400400
__asan::asanThreadRegistry().FindThreadContextByOsIDLocked(os_id));
401401
if (!context) return nullptr;
@@ -405,7 +405,7 @@ __asan::AsanThread *GetAsanThreadByOsIDLocked(uptr os_id) {
405405

406406
// --- Implementation of LSan-specific functions --- {{{1
407407
namespace __lsan {
408-
bool GetThreadRangesLocked(uptr os_id, uptr *stack_begin, uptr *stack_end,
408+
bool GetThreadRangesLocked(tid_t os_id, uptr *stack_begin, uptr *stack_end,
409409
uptr *tls_begin, uptr *tls_end, uptr *cache_begin,
410410
uptr *cache_end, DTLS **dtls) {
411411
__asan::AsanThread *t = __asan::GetAsanThreadByOsIDLocked(os_id);
@@ -421,7 +421,7 @@ bool GetThreadRangesLocked(uptr os_id, uptr *stack_begin, uptr *stack_end,
421421
return true;
422422
}
423423

424-
void ForEachExtraStackRange(uptr os_id, RangeIteratorCallback callback,
424+
void ForEachExtraStackRange(tid_t os_id, RangeIteratorCallback callback,
425425
void *arg) {
426426
__asan::AsanThread *t = __asan::GetAsanThreadByOsIDLocked(os_id);
427427
if (t && t->has_fake_stack())

compiler-rt/lib/asan/asan_thread.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class AsanThread {
6363
void Destroy();
6464

6565
void Init(); // Should be called from the thread itself.
66-
thread_return_t ThreadStart(uptr os_id,
66+
thread_return_t ThreadStart(tid_t os_id,
6767
atomic_uintptr_t *signal_thread_is_registered);
6868

6969
uptr stack_top();

compiler-rt/lib/lsan/lsan_common.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ static void ProcessThreads(SuspendedThreadsList const &suspended_threads,
204204
uptr registers_begin = reinterpret_cast<uptr>(registers.data());
205205
uptr registers_end = registers_begin + registers.size();
206206
for (uptr i = 0; i < suspended_threads.thread_count(); i++) {
207-
uptr os_id = static_cast<uptr>(suspended_threads.GetThreadID(i));
207+
tid_t os_id = static_cast<tid_t>(suspended_threads.GetThreadID(i));
208208
LOG_THREADS("Processing thread %d.\n", os_id);
209209
uptr stack_begin, stack_end, tls_begin, tls_end, cache_begin, cache_end;
210210
DTLS *dtls;

compiler-rt/lib/lsan/lsan_common.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,10 @@ bool WordIsPoisoned(uptr addr);
193193
// Wrappers for ThreadRegistry access.
194194
void LockThreadRegistry();
195195
void UnlockThreadRegistry();
196-
bool GetThreadRangesLocked(uptr os_id, uptr *stack_begin, uptr *stack_end,
196+
bool GetThreadRangesLocked(tid_t os_id, uptr *stack_begin, uptr *stack_end,
197197
uptr *tls_begin, uptr *tls_end, uptr *cache_begin,
198198
uptr *cache_end, DTLS **dtls);
199-
void ForEachExtraStackRange(uptr os_id, RangeIteratorCallback callback,
199+
void ForEachExtraStackRange(tid_t os_id, RangeIteratorCallback callback,
200200
void *arg);
201201
// If called from the main thread, updates the main thread's TID in the thread
202202
// registry. We need this to handle processes that fork() without a subsequent

compiler-rt/lib/lsan/lsan_thread.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ u32 ThreadCreate(u32 parent_tid, uptr user_id, bool detached) {
7777
/* arg */ nullptr);
7878
}
7979

80-
void ThreadStart(u32 tid, uptr os_id) {
80+
void ThreadStart(u32 tid, tid_t os_id) {
8181
OnStartedArgs args;
8282
uptr stack_size = 0;
8383
uptr tls_size = 0;
@@ -127,7 +127,7 @@ void EnsureMainThreadIDIsCorrect() {
127127

128128
///// Interface to the common LSan module. /////
129129

130-
bool GetThreadRangesLocked(uptr os_id, uptr *stack_begin, uptr *stack_end,
130+
bool GetThreadRangesLocked(tid_t os_id, uptr *stack_begin, uptr *stack_end,
131131
uptr *tls_begin, uptr *tls_end, uptr *cache_begin,
132132
uptr *cache_end, DTLS **dtls) {
133133
ThreadContext *context = static_cast<ThreadContext *>(
@@ -143,7 +143,7 @@ bool GetThreadRangesLocked(uptr os_id, uptr *stack_begin, uptr *stack_end,
143143
return true;
144144
}
145145

146-
void ForEachExtraStackRange(uptr os_id, RangeIteratorCallback callback,
146+
void ForEachExtraStackRange(tid_t os_id, RangeIteratorCallback callback,
147147
void *arg) {
148148
}
149149

compiler-rt/lib/lsan/lsan_thread.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class ThreadContext : public ThreadContextBase {
4545

4646
void InitializeThreadRegistry();
4747

48-
void ThreadStart(u32 tid, uptr os_id);
48+
void ThreadStart(u32 tid, tid_t os_id);
4949
void ThreadFinish();
5050
u32 ThreadCreate(u32 tid, uptr uid, bool detached);
5151
void ThreadJoin(u32 tid);

compiler-rt/lib/sanitizer_common/sanitizer_common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ INLINE uptr GetPageSizeCached() {
7272
uptr GetMmapGranularity();
7373
uptr GetMaxVirtualAddress();
7474
// Threads
75-
uptr GetTid();
75+
tid_t GetTid();
7676
uptr GetThreadSelf();
7777
void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top,
7878
uptr *stack_bottom);

compiler-rt/lib/sanitizer_common/sanitizer_internal_defs.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,12 @@ typedef u32 operator_new_size_type;
152152
# endif
153153
#endif
154154

155+
#if SANITIZER_MAC
156+
// On Darwin, thread IDs are 64-bit even on 32-bit systems.
157+
typedef u64 tid_t;
158+
#else
159+
typedef uptr tid_t;
160+
#endif
155161

156162
// ----------- ATTENTION -------------
157163
// This header should NOT include any other headers to avoid portability issues.

compiler-rt/lib/sanitizer_common/sanitizer_linux.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ bool FileExists(const char *filename) {
384384
return S_ISREG(st.st_mode);
385385
}
386386

387-
uptr GetTid() {
387+
tid_t GetTid() {
388388
#if SANITIZER_FREEBSD
389389
return (uptr)pthread_self();
390390
#else

compiler-rt/lib/sanitizer_common/sanitizer_mac.cc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,9 +252,8 @@ bool FileExists(const char *filename) {
252252
return S_ISREG(st.st_mode);
253253
}
254254

255-
uptr GetTid() {
256-
// FIXME: This can potentially get truncated on 32-bit, where uptr is 4 bytes.
257-
uint64_t tid;
255+
tid_t GetTid() {
256+
tid_t tid;
258257
pthread_threadid_np(nullptr, &tid);
259258
return tid;
260259
}

compiler-rt/lib/sanitizer_common/sanitizer_stoptheworld.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
#include "sanitizer_common.h"
1919

2020
namespace __sanitizer {
21-
typedef int SuspendedThreadID;
2221

2322
enum PtraceRegistersStatus {
2423
REGISTERS_UNAVAILABLE_FATAL = -1,
@@ -32,7 +31,7 @@ class SuspendedThreadsList {
3231
public:
3332
SuspendedThreadsList()
3433
: thread_ids_(1024) {}
35-
SuspendedThreadID GetThreadID(uptr index) const {
34+
tid_t GetThreadID(uptr index) const {
3635
CHECK_LT(index, thread_ids_.size());
3736
return thread_ids_[index];
3837
}
@@ -41,19 +40,19 @@ class SuspendedThreadsList {
4140
// The buffer in GetRegistersAndSP should be at least this big.
4241
static uptr RegisterCount();
4342
uptr thread_count() const { return thread_ids_.size(); }
44-
bool Contains(SuspendedThreadID thread_id) const {
43+
bool Contains(tid_t thread_id) const {
4544
for (uptr i = 0; i < thread_ids_.size(); i++) {
4645
if (thread_ids_[i] == thread_id)
4746
return true;
4847
}
4948
return false;
5049
}
51-
void Append(SuspendedThreadID thread_id) {
50+
void Append(tid_t thread_id) {
5251
thread_ids_.push_back(thread_id);
5352
}
5453

5554
private:
56-
InternalMmapVector<SuspendedThreadID> thread_ids_;
55+
InternalMmapVector<tid_t> thread_ids_;
5756

5857
// Prohibit copy and assign.
5958
SuspendedThreadsList(const SuspendedThreadsList&);

compiler-rt/lib/sanitizer_common/sanitizer_stoptheworld_linux_libcdep.cc

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,6 @@
8282

8383
namespace __sanitizer {
8484

85-
COMPILER_CHECK(sizeof(SuspendedThreadID) == sizeof(pid_t));
86-
8785
// Structure for passing arguments into the tracer thread.
8886
struct TracerThreadArgument {
8987
StopTheWorldCallback callback;
@@ -114,10 +112,10 @@ class ThreadSuspender {
114112
private:
115113
SuspendedThreadsList suspended_threads_list_;
116114
pid_t pid_;
117-
bool SuspendThread(SuspendedThreadID thread_id);
115+
bool SuspendThread(tid_t thread_id);
118116
};
119117

120-
bool ThreadSuspender::SuspendThread(SuspendedThreadID tid) {
118+
bool ThreadSuspender::SuspendThread(tid_t tid) {
121119
// Are we already attached to this thread?
122120
// Currently this check takes linear time, however the number of threads is
123121
// usually small.

compiler-rt/lib/sanitizer_common/sanitizer_thread_registry.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ void ThreadContextBase::SetFinished() {
5959
OnFinished();
6060
}
6161

62-
void ThreadContextBase::SetStarted(uptr _os_id, bool _workerthread, void *arg) {
62+
void ThreadContextBase::SetStarted(tid_t _os_id, bool _workerthread,
63+
void *arg) {
6364
status = ThreadStatusRunning;
6465
os_id = _os_id;
6566
workerthread = _workerthread;
@@ -193,7 +194,7 @@ static bool FindThreadContextByOsIdCallback(ThreadContextBase *tctx,
193194
tctx->status != ThreadStatusDead);
194195
}
195196

196-
ThreadContextBase *ThreadRegistry::FindThreadContextByOsIDLocked(uptr os_id) {
197+
ThreadContextBase *ThreadRegistry::FindThreadContextByOsIDLocked(tid_t os_id) {
197198
return FindThreadContextLocked(FindThreadContextByOsIdCallback,
198199
(void *)os_id);
199200
}
@@ -267,7 +268,7 @@ void ThreadRegistry::FinishThread(u32 tid) {
267268
}
268269
}
269270

270-
void ThreadRegistry::StartThread(u32 tid, uptr os_id, bool workerthread,
271+
void ThreadRegistry::StartThread(u32 tid, tid_t os_id, bool workerthread,
271272
void *arg) {
272273
BlockingMutexLock l(&mtx_);
273274
running_threads_++;

compiler-rt/lib/sanitizer_common/sanitizer_thread_registry.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class ThreadContextBase {
3939
const u32 tid; // Thread ID. Main thread should have tid = 0.
4040
u64 unique_id; // Unique thread ID.
4141
u32 reuse_count; // Number of times this tid was reused.
42-
uptr os_id; // PID (used for reporting).
42+
tid_t os_id; // PID (used for reporting).
4343
uptr user_id; // Some opaque user thread id (e.g. pthread_t).
4444
char name[64]; // As annotated by user.
4545

@@ -55,7 +55,7 @@ class ThreadContextBase {
5555
void SetDead();
5656
void SetJoined(void *arg);
5757
void SetFinished();
58-
void SetStarted(uptr _os_id, bool _workerthread, void *arg);
58+
void SetStarted(tid_t _os_id, bool _workerthread, void *arg);
5959
void SetCreated(uptr _user_id, u64 _unique_id, bool _detached,
6060
u32 _parent_tid, void *arg);
6161
void Reset();
@@ -109,14 +109,14 @@ class ThreadRegistry {
109109
// is found.
110110
ThreadContextBase *FindThreadContextLocked(FindThreadCallback cb,
111111
void *arg);
112-
ThreadContextBase *FindThreadContextByOsIDLocked(uptr os_id);
112+
ThreadContextBase *FindThreadContextByOsIDLocked(tid_t os_id);
113113

114114
void SetThreadName(u32 tid, const char *name);
115115
void SetThreadNameByUserId(uptr user_id, const char *name);
116116
void DetachThread(u32 tid, void *arg);
117117
void JoinThread(u32 tid, void *arg);
118118
void FinishThread(u32 tid);
119-
void StartThread(u32 tid, uptr os_id, bool workerthread, void *arg);
119+
void StartThread(u32 tid, tid_t os_id, bool workerthread, void *arg);
120120

121121
private:
122122
const ThreadContextFactory context_factory_;

compiler-rt/lib/sanitizer_common/sanitizer_win.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ uptr internal_getpid() {
8080

8181
// In contrast to POSIX, on Windows GetCurrentThreadId()
8282
// returns a system-unique identifier.
83-
uptr GetTid() {
83+
tid_t GetTid() {
8484
return GetCurrentThreadId();
8585
}
8686

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ int __tsan_get_report_mutex(void *report, uptr idx, uptr *mutex_id, void **addr,
151151
}
152152

153153
SANITIZER_INTERFACE_ATTRIBUTE
154-
int __tsan_get_report_thread(void *report, uptr idx, int *tid, uptr *os_id,
154+
int __tsan_get_report_thread(void *report, uptr idx, int *tid, tid_t *os_id,
155155
int *running, const char **name, int *parent_tid,
156156
void **trace, uptr trace_size) {
157157
const ReportDesc *rep = (ReportDesc *)report;
@@ -228,7 +228,7 @@ const char *__tsan_locate_address(uptr addr, char *name, uptr name_size,
228228

229229
SANITIZER_INTERFACE_ATTRIBUTE
230230
int __tsan_get_alloc_stack(uptr addr, uptr *trace, uptr size, int *thread_id,
231-
uptr *os_id) {
231+
tid_t *os_id) {
232232
MBlock *b = 0;
233233
Allocator *a = allocator();
234234
if (a->PointerIsMine((void *)addr)) {

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include <sanitizer_common/sanitizer_internal_defs.h>
2020
using __sanitizer::uptr;
21+
using __sanitizer::tid_t;
2122

2223
// This header should NOT include any other headers.
2324
// All functions in this header are extern "C" and start with __tsan_.
@@ -143,7 +144,7 @@ int __tsan_get_report_mutex(void *report, uptr idx, uptr *mutex_id, void **addr,
143144

144145
// Returns information about threads included in the report.
145146
SANITIZER_INTERFACE_ATTRIBUTE
146-
int __tsan_get_report_thread(void *report, uptr idx, int *tid, uptr *os_id,
147+
int __tsan_get_report_thread(void *report, uptr idx, int *tid, tid_t *os_id,
147148
int *running, const char **name, int *parent_tid,
148149
void **trace, uptr trace_size);
149150

@@ -160,7 +161,7 @@ const char *__tsan_locate_address(uptr addr, char *name, uptr name_size,
160161
// Returns the allocation stack for a heap pointer.
161162
SANITIZER_INTERFACE_ATTRIBUTE
162163
int __tsan_get_alloc_stack(uptr addr, uptr *trace, uptr size, int *thread_id,
163-
uptr *os_id);
164+
tid_t *os_id);
164165

165166
#endif // SANITIZER_GO
166167

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ struct ReportLocation {
9090

9191
struct ReportThread {
9292
int id;
93-
uptr os_id;
93+
tid_t os_id;
9494
bool running;
9595
bool workerthread;
9696
char *name;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,7 @@ void FuncEntry(ThreadState *thr, uptr pc);
720720
void FuncExit(ThreadState *thr);
721721

722722
int ThreadCreate(ThreadState *thr, uptr pc, uptr uid, bool detached);
723-
void ThreadStart(ThreadState *thr, int tid, uptr os_id, bool workerthread);
723+
void ThreadStart(ThreadState *thr, int tid, tid_t os_id, bool workerthread);
724724
void ThreadFinish(ThreadState *thr);
725725
int ThreadTid(ThreadState *thr, uptr pc, uptr uid);
726726
void ThreadJoin(ThreadState *thr, uptr pc, int tid);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ int ThreadCreate(ThreadState *thr, uptr pc, uptr uid, bool detached) {
236236
return tid;
237237
}
238238

239-
void ThreadStart(ThreadState *thr, int tid, uptr os_id, bool workerthread) {
239+
void ThreadStart(ThreadState *thr, int tid, tid_t os_id, bool workerthread) {
240240
uptr stk_addr = 0;
241241
uptr stk_size = 0;
242242
uptr tls_addr = 0;

compiler-rt/test/tsan/Darwin/main_tid.mm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
extern "C" {
99
void __tsan_on_report(void *report);
1010
int __tsan_get_report_thread(void *report, unsigned long idx, int *tid,
11-
unsigned long *os_id, int *running,
11+
uint64_t *os_id, int *running,
1212
const char **name, int *parent_tid, void **trace,
1313
unsigned long trace_size);
1414
}
@@ -17,7 +17,7 @@ void __tsan_on_report(void *report) {
1717
fprintf(stderr, "__tsan_on_report(%p)\n", report);
1818

1919
int tid;
20-
unsigned long os_id;
20+
uint64_t os_id;
2121
int running;
2222
const char *name;
2323
int parent_tid;

compiler-rt/test/tsan/debug_alloc_stack.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#endif
1616

1717
extern "C" int __tsan_get_alloc_stack(void *addr, void **trace, size_t size,
18-
int *thread_id, void *os_id);
18+
int *thread_id, uint64_t *os_id);
1919

2020
char *mem;
2121
void alloc_func() { mem = (char *)malloc(10); }
@@ -49,7 +49,7 @@ int main() {
4949
void *trace[100];
5050
size_t num_frames = 100;
5151
int thread_id;
52-
void *thread_os_id;
52+
uint64_t *thread_os_id;
5353
num_frames =
5454
__tsan_get_alloc_stack(mem, trace, num_frames, &thread_id, &thread_os_id);
5555

@@ -58,7 +58,7 @@ int main() {
5858
// CHECK: alloc stack retval ok
5959
fprintf(stderr, "thread id = %d\n", thread_id);
6060
// CHECK: thread id = 1
61-
fprintf(stderr, "thread os id = 0x%llx\n", (uint64_t)thread_os_id);
61+
fprintf(stderr, "thread os id = 0x%llx\n", thread_os_id);
6262
// CHECK: thread os id = [[THREAD_OS_ID]]
6363
fprintf(stderr, "%p\n", trace[0]);
6464
// CHECK: [[ALLOC_FRAME_0:0x[0-9a-f]+]]

0 commit comments

Comments
 (0)