diff options
author | nkozlovskiy <nmk@ydb.tech> | 2023-10-11 19:11:46 +0300 |
---|---|---|
committer | nkozlovskiy <nmk@ydb.tech> | 2023-10-11 19:33:28 +0300 |
commit | 61b3971447e473726d6cdb23fc298e457b4d973c (patch) | |
tree | e2a2a864bb7717f7ae6138f6a3194a254dd2c7bb /contrib/libs/clang14-rt/lib/tsan/dd | |
parent | a674dc57d88d43c2e8e90a6084d5d2c988e0402c (diff) | |
download | ydb-61b3971447e473726d6cdb23fc298e457b4d973c.tar.gz |
add sanitizers dependencies
Diffstat (limited to 'contrib/libs/clang14-rt/lib/tsan/dd')
-rw-r--r-- | contrib/libs/clang14-rt/lib/tsan/dd/dd_interceptors.cpp | 330 | ||||
-rw-r--r-- | contrib/libs/clang14-rt/lib/tsan/dd/dd_rtl.cpp | 158 | ||||
-rw-r--r-- | contrib/libs/clang14-rt/lib/tsan/dd/dd_rtl.h | 66 |
3 files changed, 554 insertions, 0 deletions
diff --git a/contrib/libs/clang14-rt/lib/tsan/dd/dd_interceptors.cpp b/contrib/libs/clang14-rt/lib/tsan/dd/dd_interceptors.cpp new file mode 100644 index 0000000000..2c36f691ec --- /dev/null +++ b/contrib/libs/clang14-rt/lib/tsan/dd/dd_interceptors.cpp @@ -0,0 +1,330 @@ +//===-- dd_interceptors.cpp -----------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include <pthread.h> + +#include "dd_rtl.h" +#include "interception/interception.h" +#include "sanitizer_common/sanitizer_allocator_internal.h" +#include "sanitizer_common/sanitizer_procmaps.h" + +using namespace __dsan; + +__attribute__((tls_model("initial-exec"))) +static __thread Thread *thr; +__attribute__((tls_model("initial-exec"))) +static __thread volatile int initing; +static bool inited; +static uptr g_data_start; +static uptr g_data_end; + +static bool InitThread() { + if (initing) + return false; + if (thr != 0) + return true; + initing = true; + if (!inited) { + inited = true; + Initialize(); + } + thr = (Thread*)InternalAlloc(sizeof(*thr)); + internal_memset(thr, 0, sizeof(*thr)); + ThreadInit(thr); + initing = false; + return true; +} + +INTERCEPTOR(int, pthread_mutex_destroy, pthread_mutex_t *m) { + InitThread(); + MutexDestroy(thr, (uptr)m); + return REAL(pthread_mutex_destroy)(m); +} + +INTERCEPTOR(int, pthread_mutex_lock, pthread_mutex_t *m) { + InitThread(); + MutexBeforeLock(thr, (uptr)m, true); + int res = REAL(pthread_mutex_lock)(m); + MutexAfterLock(thr, (uptr)m, true, false); + return res; +} + +INTERCEPTOR(int, pthread_mutex_trylock, pthread_mutex_t *m) { + InitThread(); + int res = REAL(pthread_mutex_trylock)(m); + if (res == 0) + MutexAfterLock(thr, (uptr)m, true, true); + return res; +} + +INTERCEPTOR(int, pthread_mutex_unlock, pthread_mutex_t *m) { + InitThread(); + MutexBeforeUnlock(thr, (uptr)m, true); + return REAL(pthread_mutex_unlock)(m); +} + +INTERCEPTOR(int, pthread_spin_destroy, pthread_spinlock_t *m) { + InitThread(); + int res = REAL(pthread_spin_destroy)(m); + MutexDestroy(thr, (uptr)m); + return res; +} + +INTERCEPTOR(int, pthread_spin_lock, pthread_spinlock_t *m) { + InitThread(); + MutexBeforeLock(thr, (uptr)m, true); + int res = REAL(pthread_spin_lock)(m); + MutexAfterLock(thr, (uptr)m, true, false); + return res; +} + +INTERCEPTOR(int, pthread_spin_trylock, pthread_spinlock_t *m) { + InitThread(); + int res = REAL(pthread_spin_trylock)(m); + if (res == 0) + MutexAfterLock(thr, (uptr)m, true, true); + return res; +} + +INTERCEPTOR(int, pthread_spin_unlock, pthread_spinlock_t *m) { + InitThread(); + MutexBeforeUnlock(thr, (uptr)m, true); + return REAL(pthread_spin_unlock)(m); +} + +INTERCEPTOR(int, pthread_rwlock_destroy, pthread_rwlock_t *m) { + InitThread(); + MutexDestroy(thr, (uptr)m); + return REAL(pthread_rwlock_destroy)(m); +} + +INTERCEPTOR(int, pthread_rwlock_rdlock, pthread_rwlock_t *m) { + InitThread(); + MutexBeforeLock(thr, (uptr)m, false); + int res = REAL(pthread_rwlock_rdlock)(m); + MutexAfterLock(thr, (uptr)m, false, false); + return res; +} + +INTERCEPTOR(int, pthread_rwlock_tryrdlock, pthread_rwlock_t *m) { + InitThread(); + int res = REAL(pthread_rwlock_tryrdlock)(m); + if (res == 0) + MutexAfterLock(thr, (uptr)m, false, true); + return res; +} + +INTERCEPTOR(int, pthread_rwlock_timedrdlock, pthread_rwlock_t *m, + const timespec *abstime) { + InitThread(); + int res = REAL(pthread_rwlock_timedrdlock)(m, abstime); + if (res == 0) + MutexAfterLock(thr, (uptr)m, false, true); + return res; +} + +INTERCEPTOR(int, pthread_rwlock_wrlock, pthread_rwlock_t *m) { + InitThread(); + MutexBeforeLock(thr, (uptr)m, true); + int res = REAL(pthread_rwlock_wrlock)(m); + MutexAfterLock(thr, (uptr)m, true, false); + return res; +} + +INTERCEPTOR(int, pthread_rwlock_trywrlock, pthread_rwlock_t *m) { + InitThread(); + int res = REAL(pthread_rwlock_trywrlock)(m); + if (res == 0) + MutexAfterLock(thr, (uptr)m, true, true); + return res; +} + +INTERCEPTOR(int, pthread_rwlock_timedwrlock, pthread_rwlock_t *m, + const timespec *abstime) { + InitThread(); + int res = REAL(pthread_rwlock_timedwrlock)(m, abstime); + if (res == 0) + MutexAfterLock(thr, (uptr)m, true, true); + return res; +} + +INTERCEPTOR(int, pthread_rwlock_unlock, pthread_rwlock_t *m) { + InitThread(); + MutexBeforeUnlock(thr, (uptr)m, true); // note: not necessary write unlock + return REAL(pthread_rwlock_unlock)(m); +} + +static pthread_cond_t *init_cond(pthread_cond_t *c, bool force = false) { + atomic_uintptr_t *p = (atomic_uintptr_t*)c; + uptr cond = atomic_load(p, memory_order_acquire); + if (!force && cond != 0) + return (pthread_cond_t*)cond; + void *newcond = InternalAlloc(sizeof(pthread_cond_t)); + internal_memset(newcond, 0, sizeof(pthread_cond_t)); + if (atomic_compare_exchange_strong(p, &cond, (uptr)newcond, + memory_order_acq_rel)) + return (pthread_cond_t*)newcond; + InternalFree(newcond); + return (pthread_cond_t*)cond; +} + +INTERCEPTOR(int, pthread_cond_init, pthread_cond_t *c, + const pthread_condattr_t *a) { + InitThread(); + pthread_cond_t *cond = init_cond(c, true); + return REAL(pthread_cond_init)(cond, a); +} + +INTERCEPTOR(int, pthread_cond_wait, pthread_cond_t *c, pthread_mutex_t *m) { + InitThread(); + pthread_cond_t *cond = init_cond(c); + MutexBeforeUnlock(thr, (uptr)m, true); + MutexBeforeLock(thr, (uptr)m, true); + int res = REAL(pthread_cond_wait)(cond, m); + MutexAfterLock(thr, (uptr)m, true, false); + return res; +} + +INTERCEPTOR(int, pthread_cond_timedwait, pthread_cond_t *c, pthread_mutex_t *m, + const timespec *abstime) { + InitThread(); + pthread_cond_t *cond = init_cond(c); + MutexBeforeUnlock(thr, (uptr)m, true); + MutexBeforeLock(thr, (uptr)m, true); + int res = REAL(pthread_cond_timedwait)(cond, m, abstime); + MutexAfterLock(thr, (uptr)m, true, false); + return res; +} + +INTERCEPTOR(int, pthread_cond_signal, pthread_cond_t *c) { + InitThread(); + pthread_cond_t *cond = init_cond(c); + return REAL(pthread_cond_signal)(cond); +} + +INTERCEPTOR(int, pthread_cond_broadcast, pthread_cond_t *c) { + InitThread(); + pthread_cond_t *cond = init_cond(c); + return REAL(pthread_cond_broadcast)(cond); +} + +INTERCEPTOR(int, pthread_cond_destroy, pthread_cond_t *c) { + InitThread(); + pthread_cond_t *cond = init_cond(c); + int res = REAL(pthread_cond_destroy)(cond); + InternalFree(cond); + atomic_store((atomic_uintptr_t*)c, 0, memory_order_relaxed); + return res; +} + +// for symbolizer +INTERCEPTOR(char*, realpath, const char *path, char *resolved_path) { + InitThread(); + return REAL(realpath)(path, resolved_path); +} + +INTERCEPTOR(SSIZE_T, read, int fd, void *ptr, SIZE_T count) { + InitThread(); + return REAL(read)(fd, ptr, count); +} + +INTERCEPTOR(SSIZE_T, pread, int fd, void *ptr, SIZE_T count, OFF_T offset) { + InitThread(); + return REAL(pread)(fd, ptr, count, offset); +} + +extern "C" { +void __dsan_before_mutex_lock(uptr m, int writelock) { + if (!InitThread()) + return; + MutexBeforeLock(thr, m, writelock); +} + +void __dsan_after_mutex_lock(uptr m, int writelock, int trylock) { + if (!InitThread()) + return; + MutexAfterLock(thr, m, writelock, trylock); +} + +void __dsan_before_mutex_unlock(uptr m, int writelock) { + if (!InitThread()) + return; + MutexBeforeUnlock(thr, m, writelock); +} + +void __dsan_mutex_destroy(uptr m) { + if (!InitThread()) + return; + // if (m >= g_data_start && m < g_data_end) + // return; + MutexDestroy(thr, m); +} +} // extern "C" + +namespace __dsan { + +static void InitDataSeg() { + MemoryMappingLayout proc_maps(true); + char name[128]; + MemoryMappedSegment segment(name, ARRAY_SIZE(name)); + bool prev_is_data = false; + while (proc_maps.Next(&segment)) { + bool is_data = segment.offset != 0 && segment.filename[0] != 0; + // BSS may get merged with [heap] in /proc/self/maps. This is not very + // reliable. + bool is_bss = segment.offset == 0 && + (segment.filename[0] == 0 || + internal_strcmp(segment.filename, "[heap]") == 0) && + prev_is_data; + if (g_data_start == 0 && is_data) g_data_start = segment.start; + if (is_bss) g_data_end = segment.end; + prev_is_data = is_data; + } + VPrintf(1, "guessed data_start=0x%zx data_end=0x%zx\n", g_data_start, + g_data_end); + CHECK_LT(g_data_start, g_data_end); + CHECK_GE((uptr)&g_data_start, g_data_start); + CHECK_LT((uptr)&g_data_start, g_data_end); +} + +void InitializeInterceptors() { + INTERCEPT_FUNCTION(pthread_mutex_destroy); + INTERCEPT_FUNCTION(pthread_mutex_lock); + INTERCEPT_FUNCTION(pthread_mutex_trylock); + INTERCEPT_FUNCTION(pthread_mutex_unlock); + + INTERCEPT_FUNCTION(pthread_spin_destroy); + INTERCEPT_FUNCTION(pthread_spin_lock); + INTERCEPT_FUNCTION(pthread_spin_trylock); + INTERCEPT_FUNCTION(pthread_spin_unlock); + + INTERCEPT_FUNCTION(pthread_rwlock_destroy); + INTERCEPT_FUNCTION(pthread_rwlock_rdlock); + INTERCEPT_FUNCTION(pthread_rwlock_tryrdlock); + INTERCEPT_FUNCTION(pthread_rwlock_timedrdlock); + INTERCEPT_FUNCTION(pthread_rwlock_wrlock); + INTERCEPT_FUNCTION(pthread_rwlock_trywrlock); + INTERCEPT_FUNCTION(pthread_rwlock_timedwrlock); + INTERCEPT_FUNCTION(pthread_rwlock_unlock); + + INTERCEPT_FUNCTION_VER(pthread_cond_init, "GLIBC_2.3.2"); + INTERCEPT_FUNCTION_VER(pthread_cond_signal, "GLIBC_2.3.2"); + INTERCEPT_FUNCTION_VER(pthread_cond_broadcast, "GLIBC_2.3.2"); + INTERCEPT_FUNCTION_VER(pthread_cond_wait, "GLIBC_2.3.2"); + INTERCEPT_FUNCTION_VER(pthread_cond_timedwait, "GLIBC_2.3.2"); + INTERCEPT_FUNCTION_VER(pthread_cond_destroy, "GLIBC_2.3.2"); + + // for symbolizer + INTERCEPT_FUNCTION(realpath); + INTERCEPT_FUNCTION(read); + INTERCEPT_FUNCTION(pread); + + InitDataSeg(); +} + +} // namespace __dsan diff --git a/contrib/libs/clang14-rt/lib/tsan/dd/dd_rtl.cpp b/contrib/libs/clang14-rt/lib/tsan/dd/dd_rtl.cpp new file mode 100644 index 0000000000..35b367c0ce --- /dev/null +++ b/contrib/libs/clang14-rt/lib/tsan/dd/dd_rtl.cpp @@ -0,0 +1,158 @@ +//===-- dd_rtl.cpp --------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "dd_rtl.h" +#include "sanitizer_common/sanitizer_common.h" +#include "sanitizer_common/sanitizer_placement_new.h" +#include "sanitizer_common/sanitizer_flags.h" +#include "sanitizer_common/sanitizer_flag_parser.h" +#include "sanitizer_common/sanitizer_stacktrace.h" +#include "sanitizer_common/sanitizer_stackdepot.h" + +namespace __dsan { + +static Context *ctx; + +static u32 CurrentStackTrace(Thread *thr, uptr skip) { + BufferedStackTrace stack; + thr->ignore_interceptors = true; + stack.Unwind(1000, 0, 0, 0, 0, 0, false); + thr->ignore_interceptors = false; + if (stack.size <= skip) + return 0; + return StackDepotPut(StackTrace(stack.trace + skip, stack.size - skip)); +} + +static void PrintStackTrace(Thread *thr, u32 stk) { + StackTrace stack = StackDepotGet(stk); + thr->ignore_interceptors = true; + stack.Print(); + thr->ignore_interceptors = false; +} + +static void ReportDeadlock(Thread *thr, DDReport *rep) { + if (rep == 0) + return; + Lock lock(&ctx->report_mutex); + Printf("==============================\n"); + Printf("WARNING: lock-order-inversion (potential deadlock)\n"); + for (int i = 0; i < rep->n; i++) { + Printf("Thread %lld locks mutex %llu while holding mutex %llu:\n", + rep->loop[i].thr_ctx, rep->loop[i].mtx_ctx1, rep->loop[i].mtx_ctx0); + PrintStackTrace(thr, rep->loop[i].stk[1]); + if (rep->loop[i].stk[0]) { + Printf("Mutex %llu was acquired here:\n", + rep->loop[i].mtx_ctx0); + PrintStackTrace(thr, rep->loop[i].stk[0]); + } + } + Printf("==============================\n"); +} + +Callback::Callback(Thread *thr) + : thr(thr) { + lt = thr->dd_lt; + pt = thr->dd_pt; +} + +u32 Callback::Unwind() { + return CurrentStackTrace(thr, 3); +} + +static void InitializeFlags() { + Flags *f = flags(); + + // Default values. + f->second_deadlock_stack = false; + + SetCommonFlagsDefaults(); + { + // Override some common flags defaults. + CommonFlags cf; + cf.CopyFrom(*common_flags()); + cf.allow_addr2line = true; + OverrideCommonFlags(cf); + } + + // Override from command line. + FlagParser parser; + RegisterFlag(&parser, "second_deadlock_stack", "", &f->second_deadlock_stack); + RegisterCommonFlags(&parser); + parser.ParseStringFromEnv("DSAN_OPTIONS"); + SetVerbosity(common_flags()->verbosity); +} + +void Initialize() { + static u64 ctx_mem[sizeof(Context) / sizeof(u64) + 1]; + ctx = new(ctx_mem) Context(); + + InitializeInterceptors(); + InitializeFlags(); + ctx->dd = DDetector::Create(flags()); +} + +void ThreadInit(Thread *thr) { + static atomic_uintptr_t id_gen; + uptr id = atomic_fetch_add(&id_gen, 1, memory_order_relaxed); + thr->dd_pt = ctx->dd->CreatePhysicalThread(); + thr->dd_lt = ctx->dd->CreateLogicalThread(id); +} + +void ThreadDestroy(Thread *thr) { + ctx->dd->DestroyPhysicalThread(thr->dd_pt); + ctx->dd->DestroyLogicalThread(thr->dd_lt); +} + +void MutexBeforeLock(Thread *thr, uptr m, bool writelock) { + if (thr->ignore_interceptors) + return; + Callback cb(thr); + { + MutexHashMap::Handle h(&ctx->mutex_map, m); + if (h.created()) + ctx->dd->MutexInit(&cb, &h->dd); + ctx->dd->MutexBeforeLock(&cb, &h->dd, writelock); + } + ReportDeadlock(thr, ctx->dd->GetReport(&cb)); +} + +void MutexAfterLock(Thread *thr, uptr m, bool writelock, bool trylock) { + if (thr->ignore_interceptors) + return; + Callback cb(thr); + { + MutexHashMap::Handle h(&ctx->mutex_map, m); + if (h.created()) + ctx->dd->MutexInit(&cb, &h->dd); + ctx->dd->MutexAfterLock(&cb, &h->dd, writelock, trylock); + } + ReportDeadlock(thr, ctx->dd->GetReport(&cb)); +} + +void MutexBeforeUnlock(Thread *thr, uptr m, bool writelock) { + if (thr->ignore_interceptors) + return; + Callback cb(thr); + { + MutexHashMap::Handle h(&ctx->mutex_map, m); + ctx->dd->MutexBeforeUnlock(&cb, &h->dd, writelock); + } + ReportDeadlock(thr, ctx->dd->GetReport(&cb)); +} + +void MutexDestroy(Thread *thr, uptr m) { + if (thr->ignore_interceptors) + return; + Callback cb(thr); + MutexHashMap::Handle h(&ctx->mutex_map, m, true); + if (!h.exists()) + return; + ctx->dd->MutexDestroy(&cb, &h->dd); +} + +} // namespace __dsan diff --git a/contrib/libs/clang14-rt/lib/tsan/dd/dd_rtl.h b/contrib/libs/clang14-rt/lib/tsan/dd/dd_rtl.h new file mode 100644 index 0000000000..c812ffbd13 --- /dev/null +++ b/contrib/libs/clang14-rt/lib/tsan/dd/dd_rtl.h @@ -0,0 +1,66 @@ +//===-- dd_rtl.h ----------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +#ifndef DD_RTL_H +#define DD_RTL_H + +#include "sanitizer_common/sanitizer_internal_defs.h" +#include "sanitizer_common/sanitizer_deadlock_detector_interface.h" +#include "sanitizer_common/sanitizer_flags.h" +#include "sanitizer_common/sanitizer_allocator_internal.h" +#include "sanitizer_common/sanitizer_addrhashmap.h" +#include "sanitizer_common/sanitizer_mutex.h" + +namespace __dsan { + +typedef DDFlags Flags; + +struct UserMutex { + DDMutex dd; +}; + +struct Thread { + DDPhysicalThread *dd_pt; + DDLogicalThread *dd_lt; + + bool ignore_interceptors; +}; + +struct Callback final : public DDCallback { + Thread *thr; + + Callback(Thread *thr); + u32 Unwind() override; +}; + +typedef AddrHashMap<UserMutex, 31051> MutexHashMap; + +struct Context { + DDetector *dd; + + Mutex report_mutex; + MutexHashMap mutex_map; +}; + +inline Flags* flags() { + static Flags flags; + return &flags; +} + +void Initialize(); +void InitializeInterceptors(); + +void ThreadInit(Thread *thr); +void ThreadDestroy(Thread *thr); + +void MutexBeforeLock(Thread *thr, uptr m, bool writelock); +void MutexAfterLock(Thread *thr, uptr m, bool writelock, bool trylock); +void MutexBeforeUnlock(Thread *thr, uptr m, bool writelock); +void MutexDestroy(Thread *thr, uptr m); + +} // namespace __dsan +#endif // DD_RTL_H |