Don't assume PTHREAD_CREATE_JOINABLE is 0 on all systems

Summary:
Lsan was using PTHREAD_CREATE_JOINABLE/PTHREAD_CREATE_DETACHED
as truthy values, which works on Linux, where the values are 0 and 1,
but this fails on OS X, where the values are 1 and 2.

Set PTHREAD_CREATE_DETACHED to the correct value for a given system.

Reviewers: kcc, glider, kubamracek, alekseyshl

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D31883

llvm-svn: 300221
This commit is contained in:
Francis Ricci 2017-04-13 17:28:52 +00:00
parent 4a9a89241b
commit bdb8b58d16
4 changed files with 12 additions and 13 deletions

View File

@ -21,6 +21,7 @@
#include "sanitizer_common/sanitizer_linux.h"
#include "sanitizer_common/sanitizer_platform_interceptors.h"
#include "sanitizer_common/sanitizer_platform_limits_posix.h"
#include "sanitizer_common/sanitizer_posix.h"
#include "sanitizer_common/sanitizer_tls_get_addr.h"
#include "lsan.h"
#include "lsan_allocator.h"
@ -281,7 +282,8 @@ INTERCEPTOR(int, pthread_create, void *th, void *attr,
res = REAL(pthread_create)(th, attr, __lsan_thread_start_func, &p);
}
if (res == 0) {
int tid = ThreadCreate(GetCurrentThread(), *(uptr *)th, detached);
int tid = ThreadCreate(GetCurrentThread(), *(uptr *)th,
IsStateDetached(detached));
CHECK_NE(tid, 0);
atomic_store(&p.tid, tid, memory_order_release);
while (atomic_load(&p.tid, memory_order_acquire) != 0)

View File

@ -87,6 +87,9 @@ bool internal_sigismember(__sanitizer_sigset_t *set, int signum);
uptr internal_execve(const char *filename, char *const argv[],
char *const envp[]);
bool IsStateDetached(int state);
} // namespace __sanitizer
#endif // SANITIZER_POSIX_H

View File

@ -418,6 +418,10 @@ int WaitForProcess(pid_t pid) {
return process_status;
}
bool IsStateDetached(int state) {
return state == PTHREAD_CREATE_DETACHED;
}
} // namespace __sanitizer
#endif // SANITIZER_POSIX

View File

@ -18,6 +18,7 @@
#include "sanitizer_common/sanitizer_linux.h"
#include "sanitizer_common/sanitizer_platform_limits_posix.h"
#include "sanitizer_common/sanitizer_placement_new.h"
#include "sanitizer_common/sanitizer_posix.h"
#include "sanitizer_common/sanitizer_stacktrace.h"
#include "sanitizer_common/sanitizer_tls_get_addr.h"
#include "interception/interception.h"
@ -29,9 +30,6 @@
#include "tsan_mman.h"
#include "tsan_fd.h"
#if SANITIZER_POSIX
#include "sanitizer_common/sanitizer_posix.h"
#endif
using namespace __tsan; // NOLINT
@ -46,13 +44,6 @@ using namespace __tsan; // NOLINT
#define mallopt(a, b)
#endif
#if SANITIZER_LINUX || SANITIZER_FREEBSD
#define PTHREAD_CREATE_DETACHED 1
#elif SANITIZER_MAC
#define PTHREAD_CREATE_DETACHED 2
#endif
#ifdef __mips__
const int kSigCount = 129;
#else
@ -928,8 +919,7 @@ TSAN_INTERCEPTOR(int, pthread_create,
ThreadIgnoreEnd(thr, pc);
}
if (res == 0) {
int tid = ThreadCreate(thr, pc, *(uptr*)th,
detached == PTHREAD_CREATE_DETACHED);
int tid = ThreadCreate(thr, pc, *(uptr*)th, IsStateDetached(detached));
CHECK_NE(tid, 0);
// Synchronization on p.tid serves two purposes:
// 1. ThreadCreate must finish before the new thread starts.