[compiler-rt] 6fb2634 - [LSAN] Move ThreadCreate into child thread
Vitaly Buka via llvm-commits
llvm-commits at lists.llvm.org
Mon May 29 23:44:01 PDT 2023
Author: Vitaly Buka
Date: 2023-05-29T23:43:45-07:00
New Revision: 6fb26348e91e04b39aed38bf36c5603c48aa427d
URL: https://github.com/llvm/llvm-project/commit/6fb26348e91e04b39aed38bf36c5603c48aa427d
DIFF: https://github.com/llvm/llvm-project/commit/6fb26348e91e04b39aed38bf36c5603c48aa427d.diff
LOG: [LSAN] Move ThreadCreate into child thread
Speeds up thread creation. Similar approach is already used
by other sanitizers.
Added:
Modified:
compiler-rt/lib/lsan/lsan_interceptors.cpp
Removed:
################################################################################
diff --git a/compiler-rt/lib/lsan/lsan_interceptors.cpp b/compiler-rt/lib/lsan/lsan_interceptors.cpp
index fe852b9fcfa8d..ade9dfdab40c4 100644
--- a/compiler-rt/lib/lsan/lsan_interceptors.cpp
+++ b/compiler-rt/lib/lsan/lsan_interceptors.cpp
@@ -415,8 +415,10 @@ INTERCEPTOR(char *, strerror, int errnum) {
#if SANITIZER_POSIX
-extern "C" void *__lsan_thread_start_func(void *arg) {
- atomic_uintptr_t *atomic_tid = (atomic_uintptr_t *)arg;
+template <bool Detached>
+static void *ThreadStartFunc(void *arg) {
+ u32 parent_tid = (uptr)arg;
+ uptr tid = ThreadCreate(parent_tid, Detached);
// Wait until the last iteration to maximize the chance that we are the last
// destructor to run.
#if !SANITIZER_NETBSD && !SANITIZER_FREEBSD
@@ -425,12 +427,8 @@ extern "C" void *__lsan_thread_start_func(void *arg) {
Report("LeakSanitizer: failed to set thread key.\n");
Die();
}
-#endif
- int tid = 0;
- while ((tid = atomic_load(atomic_tid, memory_order_acquire)) == 0)
- internal_sched_yield();
+# endif
ThreadStart(tid, GetTid());
- atomic_store(atomic_tid, 0, memory_order_release);
auto self = GetThreadSelf();
auto args = GetThreadArgRetval().GetArgs(self);
void *retval = (*args.routine)(args.arg_retval);
@@ -442,17 +440,19 @@ INTERCEPTOR(int, pthread_create, void *th, void *attr,
void *(*callback)(void *), void *param) {
ENSURE_LSAN_INITED;
EnsureMainThreadIDIsCorrect();
+
bool detached = [attr]() {
int d = 0;
return attr && !pthread_attr_getdetachstate(attr, &d) && IsStateDetached(d);
}();
+
__sanitizer_pthread_attr_t myattr;
if (!attr) {
pthread_attr_init(&myattr);
attr = &myattr;
}
AdjustStackSize(attr);
- atomic_uintptr_t atomic_tid = {};
+ uptr this_tid = GetCurrentThreadId();
int result;
{
// Ignore all allocations made by pthread_create: thread stack/TLS may be
@@ -461,18 +461,12 @@ INTERCEPTOR(int, pthread_create, void *th, void *attr,
// objects, the latter are calculated by obscure pointer arithmetic.
ScopedInterceptorDisabler disabler;
GetThreadArgRetval().Create(detached, {callback, param}, [&]() -> uptr {
- result =
- REAL(pthread_create)(th, attr, __lsan_thread_start_func, &atomic_tid);
+ result = REAL(pthread_create)(
+ th, attr, detached ? ThreadStartFunc<true> : ThreadStartFunc<false>,
+ (void *)this_tid);
return result ? 0 : *(uptr *)(th);
});
}
- if (result == 0) {
- int tid = ThreadCreate(GetCurrentThreadId(), detached);
- CHECK_NE(tid, kMainTid);
- atomic_store(&atomic_tid, tid, memory_order_release);
- while (atomic_load(&atomic_tid, memory_order_acquire) != 0)
- internal_sched_yield();
- }
if (attr == &myattr)
pthread_attr_destroy(&myattr);
return result;
More information about the llvm-commits
mailing list