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
}

0 commit comments

Comments
 (0)