5d3805fca3
* ubsan.c (ubsan_expand_null_ifn): Use _v1 suffixed type mismatch builtins, store max (log2 (align), 0) into uchar field instead of align into uptr field. (ubsan_expand_objsize_ifn): Use _v1 suffixed type mismatch builtins, store uchar 0 field instead of uptr 0 field. (instrument_nonnull_return): Use _v1 suffixed nonnull return builtin, instead of passing one address of struct with 2 locations pass two addresses of structs with 1 location each. * sanitizer.def (BUILT_IN_UBSAN_HANDLE_TYPE_MISMATCH, BUILT_IN_UBSAN_HANDLE_TYPE_MISMATCH_ABORT, BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN, BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN_ABORT): Removed. (BUILT_IN_UBSAN_HANDLE_TYPE_MISMATCH_V1, BUILT_IN_UBSAN_HANDLE_TYPE_MISMATCH_V1_ABORT, BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN_V1, BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN_V1_ABORT): New builtins. * c-c++-common/ubsan/float-cast-overflow-1.c: Drop value keyword from expected output regexps. * c-c++-common/ubsan/float-cast-overflow-2.c: Likewise. * c-c++-common/ubsan/float-cast-overflow-3.c: Likewise. * c-c++-common/ubsan/float-cast-overflow-4.c: Likewise. * c-c++-common/ubsan/float-cast-overflow-5.c: Likewise. * c-c++-common/ubsan/float-cast-overflow-6.c: Likewise. * c-c++-common/ubsan/float-cast-overflow-8.c: Likewise. * c-c++-common/ubsan/float-cast-overflow-9.c: Likewise. * c-c++-common/ubsan/float-cast-overflow-10.c: Likewise. * g++.dg/ubsan/float-cast-overflow-bf.C: Likewise. * gcc.dg/ubsan/float-cast-overflow-bf.c: Likewise. * g++.dg/asan/default-options-1.C (__asan_default_options): Add used attribute. * g++.dg/asan/asan_test.C: Run with ASAN_OPTIONS=handle_segv=2 in the environment. * All source files: Merge from upstream 315899. * asan/Makefile.am (nodist_saninclude_HEADERS): Add include/sanitizer/tsan_interface.h. * asan/libtool-version: Bump the libasan SONAME. * lsan/Makefile.am (sanitizer_lsan_files): Add lsan_common_mac.cc. (lsan_files): Add lsan_linux.cc, lsan_mac.cc and lsan_malloc_mac.cc. * sanitizer_common/Makefile.am (sanitizer_common_files): Add sancov_flags.cc, sanitizer_allocator_checks.cc, sanitizer_coverage_libcdep_new.cc, sanitizer_errno.cc, sanitizer_file.cc, sanitizer_mac_libcdep.cc and sanitizer_stoptheworld_mac.cc. Remove sanitizer_coverage_libcdep.cc and sanitizer_coverage_mapping_libcdep.cc. * tsan/Makefile.am (tsan_files): Add tsan_external.cc. * ubsan/Makefile.am (DEFS): Add -DUBSAN_CAN_USE_CXXABI=1. (ubsan_files): Add ubsan_init_standalone.cc and ubsan_signals_standalone.cc. * ubsan/libtool-version: Bump the libubsan SONAME. * asan/Makefile.in: Regenerate. * lsan/Makefile.in: Regenerate. * sanitizer_common/Makefile.in: Regenerate. * tsan/Makefile.in: Regenerate. * ubsan/Makefile.in: Regenerate. From-SVN: r253887
226 lines
5.1 KiB
C++
226 lines
5.1 KiB
C++
//===-- sanitizer_mutex.h ---------------------------------------*- C++ -*-===//
|
|
//
|
|
// This file is distributed under the University of Illinois Open Source
|
|
// License. See LICENSE.TXT for details.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This file is a part of ThreadSanitizer/AddressSanitizer runtime.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#ifndef SANITIZER_MUTEX_H
|
|
#define SANITIZER_MUTEX_H
|
|
|
|
#include "sanitizer_atomic.h"
|
|
#include "sanitizer_internal_defs.h"
|
|
#include "sanitizer_libc.h"
|
|
|
|
namespace __sanitizer {
|
|
|
|
class StaticSpinMutex {
|
|
public:
|
|
void Init() {
|
|
atomic_store(&state_, 0, memory_order_relaxed);
|
|
}
|
|
|
|
void Lock() {
|
|
if (TryLock())
|
|
return;
|
|
LockSlow();
|
|
}
|
|
|
|
bool TryLock() {
|
|
return atomic_exchange(&state_, 1, memory_order_acquire) == 0;
|
|
}
|
|
|
|
void Unlock() {
|
|
atomic_store(&state_, 0, memory_order_release);
|
|
}
|
|
|
|
void CheckLocked() {
|
|
CHECK_EQ(atomic_load(&state_, memory_order_relaxed), 1);
|
|
}
|
|
|
|
private:
|
|
atomic_uint8_t state_;
|
|
|
|
void NOINLINE LockSlow() {
|
|
for (int i = 0;; i++) {
|
|
if (i < 10)
|
|
proc_yield(10);
|
|
else
|
|
internal_sched_yield();
|
|
if (atomic_load(&state_, memory_order_relaxed) == 0
|
|
&& atomic_exchange(&state_, 1, memory_order_acquire) == 0)
|
|
return;
|
|
}
|
|
}
|
|
};
|
|
|
|
class SpinMutex : public StaticSpinMutex {
|
|
public:
|
|
SpinMutex() {
|
|
Init();
|
|
}
|
|
|
|
private:
|
|
SpinMutex(const SpinMutex&);
|
|
void operator=(const SpinMutex&);
|
|
};
|
|
|
|
class BlockingMutex {
|
|
public:
|
|
#if SANITIZER_WINDOWS
|
|
// Windows does not currently support LinkerInitialized
|
|
explicit BlockingMutex(LinkerInitialized);
|
|
#else
|
|
explicit constexpr BlockingMutex(LinkerInitialized)
|
|
: opaque_storage_ {0, }, owner_(0) {}
|
|
#endif
|
|
BlockingMutex();
|
|
void Lock();
|
|
void Unlock();
|
|
|
|
// This function does not guarantee an explicit check that the calling thread
|
|
// is the thread which owns the mutex. This behavior, while more strictly
|
|
// correct, causes problems in cases like StopTheWorld, where a parent thread
|
|
// owns the mutex but a child checks that it is locked. Rather than
|
|
// maintaining complex state to work around those situations, the check only
|
|
// checks that the mutex is owned, and assumes callers to be generally
|
|
// well-behaved.
|
|
void CheckLocked();
|
|
private:
|
|
uptr opaque_storage_[10];
|
|
uptr owner_; // for debugging
|
|
};
|
|
|
|
// Reader-writer spin mutex.
|
|
class RWMutex {
|
|
public:
|
|
RWMutex() {
|
|
atomic_store(&state_, kUnlocked, memory_order_relaxed);
|
|
}
|
|
|
|
~RWMutex() {
|
|
CHECK_EQ(atomic_load(&state_, memory_order_relaxed), kUnlocked);
|
|
}
|
|
|
|
void Lock() {
|
|
u32 cmp = kUnlocked;
|
|
if (atomic_compare_exchange_strong(&state_, &cmp, kWriteLock,
|
|
memory_order_acquire))
|
|
return;
|
|
LockSlow();
|
|
}
|
|
|
|
void Unlock() {
|
|
u32 prev = atomic_fetch_sub(&state_, kWriteLock, memory_order_release);
|
|
DCHECK_NE(prev & kWriteLock, 0);
|
|
(void)prev;
|
|
}
|
|
|
|
void ReadLock() {
|
|
u32 prev = atomic_fetch_add(&state_, kReadLock, memory_order_acquire);
|
|
if ((prev & kWriteLock) == 0)
|
|
return;
|
|
ReadLockSlow();
|
|
}
|
|
|
|
void ReadUnlock() {
|
|
u32 prev = atomic_fetch_sub(&state_, kReadLock, memory_order_release);
|
|
DCHECK_EQ(prev & kWriteLock, 0);
|
|
DCHECK_GT(prev & ~kWriteLock, 0);
|
|
(void)prev;
|
|
}
|
|
|
|
void CheckLocked() {
|
|
CHECK_NE(atomic_load(&state_, memory_order_relaxed), kUnlocked);
|
|
}
|
|
|
|
private:
|
|
atomic_uint32_t state_;
|
|
|
|
enum {
|
|
kUnlocked = 0,
|
|
kWriteLock = 1,
|
|
kReadLock = 2
|
|
};
|
|
|
|
void NOINLINE LockSlow() {
|
|
for (int i = 0;; i++) {
|
|
if (i < 10)
|
|
proc_yield(10);
|
|
else
|
|
internal_sched_yield();
|
|
u32 cmp = atomic_load(&state_, memory_order_relaxed);
|
|
if (cmp == kUnlocked &&
|
|
atomic_compare_exchange_weak(&state_, &cmp, kWriteLock,
|
|
memory_order_acquire))
|
|
return;
|
|
}
|
|
}
|
|
|
|
void NOINLINE ReadLockSlow() {
|
|
for (int i = 0;; i++) {
|
|
if (i < 10)
|
|
proc_yield(10);
|
|
else
|
|
internal_sched_yield();
|
|
u32 prev = atomic_load(&state_, memory_order_acquire);
|
|
if ((prev & kWriteLock) == 0)
|
|
return;
|
|
}
|
|
}
|
|
|
|
RWMutex(const RWMutex&);
|
|
void operator = (const RWMutex&);
|
|
};
|
|
|
|
template<typename MutexType>
|
|
class GenericScopedLock {
|
|
public:
|
|
explicit GenericScopedLock(MutexType *mu)
|
|
: mu_(mu) {
|
|
mu_->Lock();
|
|
}
|
|
|
|
~GenericScopedLock() {
|
|
mu_->Unlock();
|
|
}
|
|
|
|
private:
|
|
MutexType *mu_;
|
|
|
|
GenericScopedLock(const GenericScopedLock&);
|
|
void operator=(const GenericScopedLock&);
|
|
};
|
|
|
|
template<typename MutexType>
|
|
class GenericScopedReadLock {
|
|
public:
|
|
explicit GenericScopedReadLock(MutexType *mu)
|
|
: mu_(mu) {
|
|
mu_->ReadLock();
|
|
}
|
|
|
|
~GenericScopedReadLock() {
|
|
mu_->ReadUnlock();
|
|
}
|
|
|
|
private:
|
|
MutexType *mu_;
|
|
|
|
GenericScopedReadLock(const GenericScopedReadLock&);
|
|
void operator=(const GenericScopedReadLock&);
|
|
};
|
|
|
|
typedef GenericScopedLock<StaticSpinMutex> SpinMutexLock;
|
|
typedef GenericScopedLock<BlockingMutex> BlockingMutexLock;
|
|
typedef GenericScopedLock<RWMutex> RWMutexLock;
|
|
typedef GenericScopedReadLock<RWMutex> RWMutexReadLock;
|
|
|
|
} // namespace __sanitizer
|
|
|
|
#endif // SANITIZER_MUTEX_H
|