[GWP-ASan] Mutex implementation [2].
Summary:
See D60593 for further information.
This patch pulls out the mutex implementation and the required definitions file.
We implement our own mutex for GWP-ASan currently, because:
1. We must be compatible with the sum of the most restrictive elements of the supporting allocator's build system. Current targets for GWP-ASan include Scudo (on Linux and Fuchsia), and bionic (on Android).
2. Scudo specifies `-nostdlib++ -nonodefaultlibs`, meaning we can't use `std::mutex` or `mtx_t`.
3. We can't use `sanitizer_common`'s mutex, as the supporting allocators cannot afford the extra maintenance (Android, Fuchsia) and code size (Fuchsia) overheads that this would incur.
In future, we would like to implement a shared base mutex for GWP-ASan, Scudo and sanitizer_common. This will likely happen when both GWP-ASan and Scudo standalone are not in the development phase, at which point they will have stable requirements.
Reviewers: vlad.tsyrklevich, morehouse, jfb
Reviewed By: morehouse
Subscribers: dexonsmith, srhines, cfe-commits, kubamracek, mgorny, cryptoad, jfb, #sanitizers, llvm-commits, vitalybuka, eugenis
Tags: #sanitizers, #llvm, #clang
Differential Revision: https://reviews.llvm.org/D61923
llvm-svn: 362138
2019-05-31 03:45:32 +08:00
|
|
|
//===-- mutex.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
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef GWP_ASAN_MUTEX_H_
|
|
|
|
#define GWP_ASAN_MUTEX_H_
|
|
|
|
|
2020-12-11 04:41:56 +08:00
|
|
|
#include "gwp_asan/platform_specific/mutex_fuchsia.h" // IWYU pragma: keep
|
|
|
|
#include "gwp_asan/platform_specific/mutex_posix.h" // IWYU pragma: keep
|
[GWP-ASan] Mutex implementation [2].
Summary:
See D60593 for further information.
This patch pulls out the mutex implementation and the required definitions file.
We implement our own mutex for GWP-ASan currently, because:
1. We must be compatible with the sum of the most restrictive elements of the supporting allocator's build system. Current targets for GWP-ASan include Scudo (on Linux and Fuchsia), and bionic (on Android).
2. Scudo specifies `-nostdlib++ -nonodefaultlibs`, meaning we can't use `std::mutex` or `mtx_t`.
3. We can't use `sanitizer_common`'s mutex, as the supporting allocators cannot afford the extra maintenance (Android, Fuchsia) and code size (Fuchsia) overheads that this would incur.
In future, we would like to implement a shared base mutex for GWP-ASan, Scudo and sanitizer_common. This will likely happen when both GWP-ASan and Scudo standalone are not in the development phase, at which point they will have stable requirements.
Reviewers: vlad.tsyrklevich, morehouse, jfb
Reviewed By: morehouse
Subscribers: dexonsmith, srhines, cfe-commits, kubamracek, mgorny, cryptoad, jfb, #sanitizers, llvm-commits, vitalybuka, eugenis
Tags: #sanitizers, #llvm, #clang
Differential Revision: https://reviews.llvm.org/D61923
llvm-svn: 362138
2019-05-31 03:45:32 +08:00
|
|
|
|
|
|
|
namespace gwp_asan {
|
2020-10-29 07:04:02 +08:00
|
|
|
class Mutex final : PlatformMutex {
|
[GWP-ASan] Mutex implementation [2].
Summary:
See D60593 for further information.
This patch pulls out the mutex implementation and the required definitions file.
We implement our own mutex for GWP-ASan currently, because:
1. We must be compatible with the sum of the most restrictive elements of the supporting allocator's build system. Current targets for GWP-ASan include Scudo (on Linux and Fuchsia), and bionic (on Android).
2. Scudo specifies `-nostdlib++ -nonodefaultlibs`, meaning we can't use `std::mutex` or `mtx_t`.
3. We can't use `sanitizer_common`'s mutex, as the supporting allocators cannot afford the extra maintenance (Android, Fuchsia) and code size (Fuchsia) overheads that this would incur.
In future, we would like to implement a shared base mutex for GWP-ASan, Scudo and sanitizer_common. This will likely happen when both GWP-ASan and Scudo standalone are not in the development phase, at which point they will have stable requirements.
Reviewers: vlad.tsyrklevich, morehouse, jfb
Reviewed By: morehouse
Subscribers: dexonsmith, srhines, cfe-commits, kubamracek, mgorny, cryptoad, jfb, #sanitizers, llvm-commits, vitalybuka, eugenis
Tags: #sanitizers, #llvm, #clang
Differential Revision: https://reviews.llvm.org/D61923
llvm-svn: 362138
2019-05-31 03:45:32 +08:00
|
|
|
public:
|
|
|
|
constexpr Mutex() = default;
|
|
|
|
~Mutex() = default;
|
|
|
|
Mutex(const Mutex &) = delete;
|
|
|
|
Mutex &operator=(const Mutex &) = delete;
|
|
|
|
// Lock the mutex.
|
|
|
|
void lock();
|
|
|
|
// Nonblocking trylock of the mutex. Returns true if the lock was acquired.
|
|
|
|
bool tryLock();
|
|
|
|
// Unlock the mutex.
|
|
|
|
void unlock();
|
|
|
|
};
|
|
|
|
|
|
|
|
class ScopedLock {
|
|
|
|
public:
|
|
|
|
explicit ScopedLock(Mutex &Mx) : Mu(Mx) { Mu.lock(); }
|
|
|
|
~ScopedLock() { Mu.unlock(); }
|
|
|
|
ScopedLock(const ScopedLock &) = delete;
|
|
|
|
ScopedLock &operator=(const ScopedLock &) = delete;
|
|
|
|
|
|
|
|
private:
|
|
|
|
Mutex Μ
|
|
|
|
};
|
|
|
|
} // namespace gwp_asan
|
|
|
|
|
|
|
|
#endif // GWP_ASAN_MUTEX_H_
|