diff --git a/compiler-rt/lib/gwp_asan/CMakeLists.txt b/compiler-rt/lib/gwp_asan/CMakeLists.txt --- a/compiler-rt/lib/gwp_asan/CMakeLists.txt +++ b/compiler-rt/lib/gwp_asan/CMakeLists.txt @@ -23,6 +23,8 @@ options.h options.inc platform_specific/guarded_pool_allocator_tls.h + platform_specific/mutex_fuchsia.h + platform_specific/mutex_posix.h stack_trace_compressor.h utilities.h ) diff --git a/compiler-rt/lib/gwp_asan/mutex.h b/compiler-rt/lib/gwp_asan/mutex.h --- a/compiler-rt/lib/gwp_asan/mutex.h +++ b/compiler-rt/lib/gwp_asan/mutex.h @@ -9,14 +9,11 @@ #ifndef GWP_ASAN_MUTEX_H_ #define GWP_ASAN_MUTEX_H_ -#ifdef __unix__ -#include -#else -#error "GWP-ASan is not supported on this platform." -#endif +#include "gwp_asan/platform_specific/mutex_fuchsia.h" +#include "gwp_asan/platform_specific/mutex_posix.h" namespace gwp_asan { -class Mutex { +class Mutex final : PlatformMutex { public: constexpr Mutex() = default; ~Mutex() = default; @@ -28,11 +25,6 @@ bool tryLock(); // Unlock the mutex. void unlock(); - -private: -#ifdef __unix__ - pthread_mutex_t Mu = PTHREAD_MUTEX_INITIALIZER; -#endif // defined(__unix__) }; class ScopedLock { diff --git a/compiler-rt/lib/gwp_asan/platform_specific/mutex_fuchsia.h b/compiler-rt/lib/gwp_asan/platform_specific/mutex_fuchsia.h new file mode 100644 --- /dev/null +++ b/compiler-rt/lib/gwp_asan/platform_specific/mutex_fuchsia.h @@ -0,0 +1,23 @@ +//===-- mutex_fuchsia.h -----------------------------------------*- C++ -*-===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#if defined(__Fuchsia__) +#ifndef GWP_ASAN_MUTEX_FUCHSIA_H_ +#define GWP_ASAN_MUTEX_FUCHSIA_H_ + +#include + +namespace gwp_asan { +class PlatformMutex { +protected: + sync_mutex_t Mu = {}; +}; +} // namespace gwp_asan + +#endif // GWP_ASAN_MUTEX_FUCHSIA_H_ +#endif // defined(__Fuchsia__) diff --git a/compiler-rt/lib/gwp_asan/platform_specific/mutex_fuchsia.cpp b/compiler-rt/lib/gwp_asan/platform_specific/mutex_fuchsia.cpp new file mode 100644 --- /dev/null +++ b/compiler-rt/lib/gwp_asan/platform_specific/mutex_fuchsia.cpp @@ -0,0 +1,21 @@ +//===-- mutex_fuchsia.cpp ---------------------------------------*- C++ -*-===// +// +// 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 "gwp_asan/mutex.h" + +#include + +namespace gwp_asan { +void Mutex::lock() __TA_NO_THREAD_SAFETY_ANALYSIS { sync_mutex_lock(&Mu); } + +bool Mutex::tryLock() __TA_NO_THREAD_SAFETY_ANALYSIS { + return sync_mutex_trylock(&Mu) == ZX_OK; +} + +void Mutex::unlock() __TA_NO_THREAD_SAFETY_ANALYSIS { sync_mutex_unlock(&Mu); } +} // namespace gwp_asan diff --git a/compiler-rt/lib/gwp_asan/platform_specific/mutex_posix.h b/compiler-rt/lib/gwp_asan/platform_specific/mutex_posix.h new file mode 100644 --- /dev/null +++ b/compiler-rt/lib/gwp_asan/platform_specific/mutex_posix.h @@ -0,0 +1,23 @@ +//===-- mutex_posix.h -------------------------------------------*- C++ -*-===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#if defined(__unix__) +#ifndef GWP_ASAN_MUTEX_POSIX_H_ +#define GWP_ASAN_MUTEX_POSIX_H_ + +#include + +namespace gwp_asan { +class PlatformMutex { +protected: + pthread_mutex_t Mu = PTHREAD_MUTEX_INITIALIZER; +}; +} // namespace gwp_asan + +#endif // GWP_ASAN_MUTEX_POSIX_H_ +#endif // defined(__unix__)