[compiler-rt] 3591721 - [GWP-ASan] Add mutexes for Fuchsia

Kostya Kortchinsky via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 29 15:51:31 PDT 2020


Author: Kostya Kortchinsky
Date: 2020-10-29T15:51:13-07:00
New Revision: 3591721ada99810aeddfa3d4c83eea0dbeed7f18

URL: https://github.com/llvm/llvm-project/commit/3591721ada99810aeddfa3d4c83eea0dbeed7f18
DIFF: https://github.com/llvm/llvm-project/commit/3591721ada99810aeddfa3d4c83eea0dbeed7f18.diff

LOG: [GWP-ASan] Add mutexes for Fuchsia

Mitch expressed a preference to not have `#ifdef`s in platform agnostic
code, this change tries to accomodate this.

I am not attached to the method this CL proposes, so if anyone has a
suggestion, I am open.

We move the platform specific member of the mutex into its own platform
specific class that the main `Mutex` class inherits from. Functions are
implemented in their respective platform specific compilation units.

For Fuchsia, we use the sync APIs, as those are also the ones being
used in Scudo.

Differential Revision: https://reviews.llvm.org/D90351

Added: 
    compiler-rt/lib/gwp_asan/platform_specific/mutex_fuchsia.cpp
    compiler-rt/lib/gwp_asan/platform_specific/mutex_fuchsia.h
    compiler-rt/lib/gwp_asan/platform_specific/mutex_posix.h

Modified: 
    compiler-rt/lib/gwp_asan/CMakeLists.txt
    compiler-rt/lib/gwp_asan/mutex.h

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/gwp_asan/CMakeLists.txt b/compiler-rt/lib/gwp_asan/CMakeLists.txt
index 889a15ef4ab9..df95cd302297 100644
--- a/compiler-rt/lib/gwp_asan/CMakeLists.txt
+++ b/compiler-rt/lib/gwp_asan/CMakeLists.txt
@@ -23,6 +23,8 @@ set(GWP_ASAN_HEADERS
   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
index c29df4cde164..a7214f527b29 100644
--- 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 <pthread.h>
-#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 @@ class Mutex {
   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.cpp b/compiler-rt/lib/gwp_asan/platform_specific/mutex_fuchsia.cpp
new file mode 100644
index 000000000000..0431a82b4b53
--- /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 <lib/sync/mutex.h>
+
+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_fuchsia.h b/compiler-rt/lib/gwp_asan/platform_specific/mutex_fuchsia.h
new file mode 100644
index 000000000000..edfc1a6f50b4
--- /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 <lib/sync/mutex.h>
+
+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_posix.h b/compiler-rt/lib/gwp_asan/platform_specific/mutex_posix.h
new file mode 100644
index 000000000000..7f0239198f56
--- /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 <pthread.h>
+
+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__)


        


More information about the llvm-commits mailing list