Imported Upstream version 6.10.0.49

Former-commit-id: 1d6753294b2993e1fbf92de9366bb9544db4189b
This commit is contained in:
Xamarin Public Jenkins (auto-signing)
2020-01-16 16:38:04 +00:00
parent d94e79959b
commit 468663ddbb
48518 changed files with 2789335 additions and 61176 deletions

View File

@@ -0,0 +1,49 @@
# Build for the experimental deadlock detector runtime library.
include_directories(../..)
set(DD_CFLAGS ${SANITIZER_COMMON_CFLAGS})
append_rtti_flag(OFF DD_CFLAGS)
set(DD_SOURCES
dd_rtl.cc
dd_interceptors.cc
)
set(DD_LINKLIBS ${SANITIZER_CXX_ABI_LIBRARY} ${SANITIZER_COMMON_LINK_LIBS})
append_list_if(COMPILER_RT_HAS_LIBDL dl DD_LINKLIBS)
append_list_if(COMPILER_RT_HAS_LIBRT rt DD_LINKLIBS)
append_list_if(COMPILER_RT_HAS_LIBPTHREAD pthread DD_LINKLIBS)
add_custom_target(dd)
# Deadlock detector is currently supported on 64-bit Linux only.
if(CAN_TARGET_x86_64 AND UNIX AND NOT APPLE AND NOT ANDROID)
set(arch "x86_64")
add_compiler_rt_runtime(clang_rt.dd
STATIC
ARCHS ${arch}
SOURCES ${DD_SOURCES}
$<TARGET_OBJECTS:RTInterception.${arch}>
$<TARGET_OBJECTS:RTSanitizerCommon.${arch}>
$<TARGET_OBJECTS:RTSanitizerCommonLibc.${arch}>
CFLAGS ${DD_CFLAGS}
PARENT_TARGET dd)
add_compiler_rt_object_libraries(RTDD
ARCHS ${arch}
SOURCES ${DD_SOURCES} CFLAGS ${DD_CFLAGS})
add_compiler_rt_runtime(clang_rt.dyndd
SHARED
ARCHS ${arch}
SOURCES $<TARGET_OBJECTS:RTDD.${arch}>
$<TARGET_OBJECTS:RTInterception.${arch}>
$<TARGET_OBJECTS:RTSanitizerCommon.${arch}>
$<TARGET_OBJECTS:RTSanitizerCommonLibc.${arch}>
LINK_FLAGS ${SANITIZER_COMMON_LINK_FLAGS}
LINK_LIBS ${DD_LINKLIBS}
PARENT_TARGET dd)
endif()
add_dependencies(compiler-rt dd)

View File

@@ -0,0 +1,329 @@
//===-- dd_interceptors.cc ------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "dd_rtl.h"
#include "interception/interception.h"
#include "sanitizer_common/sanitizer_procmaps.h"
#include <pthread.h>
#include <stdlib.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 = malloc(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;
free(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);
free(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=%p data_end=%p\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

View File

@@ -0,0 +1,159 @@
//===-- dd_rtl.cc ---------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#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;
BlockingMutexLock lock(&ctx->report_mutex);
Printf("==============================\n");
Printf("WARNING: lock-order-inversion (potential deadlock)\n");
for (int i = 0; i < rep->n; i++) {
Printf("Thread %d 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.ParseString(GetEnv("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

View File

@@ -0,0 +1,67 @@
//===-- dd_rtl.h ----------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#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 Mutex {
DDMutex dd;
};
struct Thread {
DDPhysicalThread *dd_pt;
DDLogicalThread *dd_lt;
bool ignore_interceptors;
};
struct Callback : DDCallback {
Thread *thr;
Callback(Thread *thr);
u32 Unwind() override;
};
typedef AddrHashMap<Mutex, 31051> MutexHashMap;
struct Context {
DDetector *dd;
BlockingMutex 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