[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
This commit is contained in:
Kostya Kortchinsky 2020-10-28 16:04:02 -07:00
parent eaee608448
commit 3591721ada
5 changed files with 72 additions and 11 deletions

View File

@ -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
)

View File

@ -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 @@ public:
bool tryLock();
// Unlock the mutex.
void unlock();
private:
#ifdef __unix__
pthread_mutex_t Mu = PTHREAD_MUTEX_INITIALIZER;
#endif // defined(__unix__)
};
class ScopedLock {

View File

@ -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

View File

@ -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__)

View File

@ -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__)