Victor Costan | 70f81b0a7fe | 2018-01-31 23:09:50 | [diff] [blame] | 1 | // Copyright (c) 2018 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | // This header file contains macro definitions for thread safety annotations |
| 6 | // that allow developers to document the locking policies of multi-threaded |
| 7 | // code. The annotations can also help program analysis tools to identify |
| 8 | // potential thread safety issues. |
| 9 | // |
| 10 | // Note that the annotations we use are described as deprecated in the Clang |
| 11 | // documentation, linked below. E.g. we use EXCLUSIVE_LOCKS_REQUIRED where the |
| 12 | // Clang docs use REQUIRES. |
| 13 | // |
| 14 | // http://clang.llvm.org/docs/ThreadSafetyAnalysis.html |
| 15 | // |
| 16 | // We use the deprecated Clang annotations to match Abseil (relevant header |
| 17 | // linked below) and its ecosystem of libraries. We will follow Abseil with |
| 18 | // respect to upgrading to more modern annotations. |
| 19 | // |
| 20 | // https://github.com/abseil/abseil-cpp/blob/master/absl/base/thread_annotations.h |
| 21 | // |
| 22 | // These annotations are implemented using compiler attributes. Using the macros |
| 23 | // defined here instead of raw attributes allow for portability and future |
| 24 | // compatibility. |
| 25 | // |
| 26 | // When referring to mutexes in the arguments of the attributes, you should |
| 27 | // use variable names or more complex expressions (e.g. my_object->mutex_) |
| 28 | // that evaluate to a concrete mutex object whenever possible. If the mutex |
| 29 | // you want to refer to is not in scope, you may use a member pointer |
| 30 | // (e.g. &MyClass::mutex_) to refer to a mutex in some (unknown) object. |
| 31 | |
Lukasz Anforowicz | 5e71bd4 | 2018-09-17 19:28:57 | [diff] [blame] | 32 | #ifndef BASE_THREAD_ANNOTATIONS_H_ |
| 33 | #define BASE_THREAD_ANNOTATIONS_H_ |
Victor Costan | 70f81b0a7fe | 2018-01-31 23:09:50 | [diff] [blame] | 34 | |
Lukasz Anforowicz | 5e71bd4 | 2018-09-17 19:28:57 | [diff] [blame] | 35 | #include "build/build_config.h" |
| 36 | |
Lukasz Anforowicz | dca7cc8 | 2018-10-01 22:59:49 | [diff] [blame] | 37 | #if defined(__clang__) |
Victor Costan | 70f81b0a7fe | 2018-01-31 23:09:50 | [diff] [blame] | 38 | #define THREAD_ANNOTATION_ATTRIBUTE__(x) __attribute__((x)) |
| 39 | #else |
| 40 | #define THREAD_ANNOTATION_ATTRIBUTE__(x) // no-op |
| 41 | #endif |
| 42 | |
| 43 | // GUARDED_BY() |
| 44 | // |
| 45 | // Documents if a shared field or global variable needs to be protected by a |
| 46 | // mutex. GUARDED_BY() allows the user to specify a particular mutex that |
| 47 | // should be held when accessing the annotated variable. |
| 48 | // |
| 49 | // Example: |
| 50 | // |
| 51 | // Mutex mu; |
| 52 | // int p1 GUARDED_BY(mu); |
| 53 | #define GUARDED_BY(x) THREAD_ANNOTATION_ATTRIBUTE__(guarded_by(x)) |
| 54 | |
| 55 | // PT_GUARDED_BY() |
| 56 | // |
| 57 | // Documents if the memory location pointed to by a pointer should be guarded |
| 58 | // by a mutex when dereferencing the pointer. |
| 59 | // |
| 60 | // Example: |
| 61 | // Mutex mu; |
| 62 | // int *p1 PT_GUARDED_BY(mu); |
| 63 | // |
| 64 | // Note that a pointer variable to a shared memory location could itself be a |
| 65 | // shared variable. |
| 66 | // |
| 67 | // Example: |
| 68 | // |
| 69 | // // `q`, guarded by `mu1`, points to a shared memory location that is |
| 70 | // // guarded by `mu2`: |
| 71 | // int *q GUARDED_BY(mu1) PT_GUARDED_BY(mu2); |
| 72 | #define PT_GUARDED_BY(x) THREAD_ANNOTATION_ATTRIBUTE__(pt_guarded_by(x)) |
| 73 | |
| 74 | // ACQUIRED_AFTER() / ACQUIRED_BEFORE() |
| 75 | // |
| 76 | // Documents the acquisition order between locks that can be held |
| 77 | // simultaneously by a thread. For any two locks that need to be annotated |
| 78 | // to establish an acquisition order, only one of them needs the annotation. |
| 79 | // (i.e. You don't have to annotate both locks with both ACQUIRED_AFTER |
| 80 | // and ACQUIRED_BEFORE.) |
| 81 | // |
| 82 | // Example: |
| 83 | // |
| 84 | // Mutex m1; |
| 85 | // Mutex m2 ACQUIRED_AFTER(m1); |
| 86 | #define ACQUIRED_AFTER(...) \ |
| 87 | THREAD_ANNOTATION_ATTRIBUTE__(acquired_after(__VA_ARGS__)) |
| 88 | |
| 89 | #define ACQUIRED_BEFORE(...) \ |
| 90 | THREAD_ANNOTATION_ATTRIBUTE__(acquired_before(__VA_ARGS__)) |
| 91 | |
| 92 | // EXCLUSIVE_LOCKS_REQUIRED() / SHARED_LOCKS_REQUIRED() |
| 93 | // |
| 94 | // Documents a function that expects a mutex to be held prior to entry. |
| 95 | // The mutex is expected to be held both on entry to, and exit from, the |
| 96 | // function. |
| 97 | // |
| 98 | // Example: |
| 99 | // |
| 100 | // Mutex mu1, mu2; |
| 101 | // int a GUARDED_BY(mu1); |
| 102 | // int b GUARDED_BY(mu2); |
| 103 | // |
| 104 | // void foo() EXCLUSIVE_LOCKS_REQUIRED(mu1, mu2) { ... }; |
| 105 | #define EXCLUSIVE_LOCKS_REQUIRED(...) \ |
| 106 | THREAD_ANNOTATION_ATTRIBUTE__(exclusive_locks_required(__VA_ARGS__)) |
| 107 | |
| 108 | #define SHARED_LOCKS_REQUIRED(...) \ |
| 109 | THREAD_ANNOTATION_ATTRIBUTE__(shared_locks_required(__VA_ARGS__)) |
| 110 | |
| 111 | // LOCKS_EXCLUDED() |
| 112 | // |
| 113 | // Documents the locks acquired in the body of the function. These locks |
| 114 | // cannot be held when calling this function (as Abseil's `Mutex` locks are |
| 115 | // non-reentrant). |
| 116 | #define LOCKS_EXCLUDED(...) \ |
| 117 | THREAD_ANNOTATION_ATTRIBUTE__(locks_excluded(__VA_ARGS__)) |
| 118 | |
| 119 | // LOCK_RETURNED() |
| 120 | // |
| 121 | // Documents a function that returns a mutex without acquiring it. For example, |
| 122 | // a public getter method that returns a pointer to a private mutex should |
| 123 | // be annotated with LOCK_RETURNED. |
| 124 | #define LOCK_RETURNED(x) THREAD_ANNOTATION_ATTRIBUTE__(lock_returned(x)) |
| 125 | |
| 126 | // LOCKABLE |
| 127 | // |
| 128 | // Documents if a class/type is a lockable type (such as the `Mutex` class). |
| 129 | #define LOCKABLE THREAD_ANNOTATION_ATTRIBUTE__(lockable) |
| 130 | |
| 131 | // SCOPED_LOCKABLE |
| 132 | // |
| 133 | // Documents if a class does RAII locking (such as the `MutexLock` class). |
| 134 | // The constructor should use `LOCK_FUNCTION()` to specify the mutex that is |
| 135 | // acquired, and the destructor should use `UNLOCK_FUNCTION()` with no |
| 136 | // arguments; the analysis will assume that the destructor unlocks whatever the |
| 137 | // constructor locked. |
| 138 | #define SCOPED_LOCKABLE THREAD_ANNOTATION_ATTRIBUTE__(scoped_lockable) |
| 139 | |
| 140 | // EXCLUSIVE_LOCK_FUNCTION() |
| 141 | // |
| 142 | // Documents functions that acquire a lock in the body of a function, and do |
| 143 | // not release it. |
| 144 | #define EXCLUSIVE_LOCK_FUNCTION(...) \ |
| 145 | THREAD_ANNOTATION_ATTRIBUTE__(exclusive_lock_function(__VA_ARGS__)) |
| 146 | |
| 147 | // SHARED_LOCK_FUNCTION() |
| 148 | // |
| 149 | // Documents functions that acquire a shared (reader) lock in the body of a |
| 150 | // function, and do not release it. |
| 151 | #define SHARED_LOCK_FUNCTION(...) \ |
| 152 | THREAD_ANNOTATION_ATTRIBUTE__(shared_lock_function(__VA_ARGS__)) |
| 153 | |
| 154 | // UNLOCK_FUNCTION() |
| 155 | // |
| 156 | // Documents functions that expect a lock to be held on entry to the function, |
| 157 | // and release it in the body of the function. |
| 158 | #define UNLOCK_FUNCTION(...) \ |
| 159 | THREAD_ANNOTATION_ATTRIBUTE__(unlock_function(__VA_ARGS__)) |
| 160 | |
| 161 | // EXCLUSIVE_TRYLOCK_FUNCTION() / SHARED_TRYLOCK_FUNCTION() |
| 162 | // |
| 163 | // Documents functions that try to acquire a lock, and return success or failure |
| 164 | // (or a non-boolean value that can be interpreted as a boolean). |
| 165 | // The first argument should be `true` for functions that return `true` on |
| 166 | // success, or `false` for functions that return `false` on success. The second |
| 167 | // argument specifies the mutex that is locked on success. If unspecified, this |
| 168 | // mutex is assumed to be `this`. |
| 169 | #define EXCLUSIVE_TRYLOCK_FUNCTION(...) \ |
| 170 | THREAD_ANNOTATION_ATTRIBUTE__(exclusive_trylock_function(__VA_ARGS__)) |
| 171 | |
| 172 | #define SHARED_TRYLOCK_FUNCTION(...) \ |
| 173 | THREAD_ANNOTATION_ATTRIBUTE__(shared_trylock_function(__VA_ARGS__)) |
| 174 | |
| 175 | // ASSERT_EXCLUSIVE_LOCK() / ASSERT_SHARED_LOCK() |
| 176 | // |
| 177 | // Documents functions that dynamically check to see if a lock is held, and fail |
| 178 | // if it is not held. |
| 179 | #define ASSERT_EXCLUSIVE_LOCK(...) \ |
| 180 | THREAD_ANNOTATION_ATTRIBUTE__(assert_exclusive_lock(__VA_ARGS__)) |
| 181 | |
| 182 | #define ASSERT_SHARED_LOCK(...) \ |
| 183 | THREAD_ANNOTATION_ATTRIBUTE__(assert_shared_lock(__VA_ARGS__)) |
| 184 | |
| 185 | // NO_THREAD_SAFETY_ANALYSIS |
| 186 | // |
| 187 | // Turns off thread safety checking within the body of a particular function. |
| 188 | // This annotation is used to mark functions that are known to be correct, but |
| 189 | // the locking behavior is more complicated than the analyzer can handle. |
| 190 | #define NO_THREAD_SAFETY_ANALYSIS \ |
| 191 | THREAD_ANNOTATION_ATTRIBUTE__(no_thread_safety_analysis) |
| 192 | |
| 193 | //------------------------------------------------------------------------------ |
| 194 | // Tool-Supplied Annotations |
| 195 | //------------------------------------------------------------------------------ |
| 196 | |
| 197 | // TS_UNCHECKED should be placed around lock expressions that are not valid |
| 198 | // C++ syntax, but which are present for documentation purposes. These |
| 199 | // annotations will be ignored by the analysis. |
| 200 | #define TS_UNCHECKED(x) "" |
| 201 | |
| 202 | // TS_FIXME is used to mark lock expressions that are not valid C++ syntax. |
| 203 | // It is used by automated tools to mark and disable invalid expressions. |
| 204 | // The annotation should either be fixed, or changed to TS_UNCHECKED. |
| 205 | #define TS_FIXME(x) "" |
| 206 | |
| 207 | // Like NO_THREAD_SAFETY_ANALYSIS, this turns off checking within the body of |
| 208 | // a particular function. However, this attribute is used to mark functions |
| 209 | // that are incorrect and need to be fixed. It is used by automated tools to |
| 210 | // avoid breaking the build when the analysis is updated. |
| 211 | // Code owners are expected to eventually fix the routine. |
| 212 | #define NO_THREAD_SAFETY_ANALYSIS_FIXME NO_THREAD_SAFETY_ANALYSIS |
| 213 | |
| 214 | // Similar to NO_THREAD_SAFETY_ANALYSIS_FIXME, this macro marks a GUARDED_BY |
| 215 | // annotation that needs to be fixed, because it is producing thread safety |
| 216 | // warning. It disables the GUARDED_BY. |
| 217 | #define GUARDED_BY_FIXME(x) |
| 218 | |
| 219 | // Disables warnings for a single read operation. This can be used to avoid |
| 220 | // warnings when it is known that the read is not actually involved in a race, |
| 221 | // but the compiler cannot confirm that. |
| 222 | #define TS_UNCHECKED_READ(x) thread_safety_analysis::ts_unchecked_read(x) |
| 223 | |
| 224 | namespace thread_safety_analysis { |
| 225 | |
| 226 | // Takes a reference to a guarded data member, and returns an unguarded |
| 227 | // reference. |
| 228 | template <typename T> |
| 229 | inline const T& ts_unchecked_read(const T& v) NO_THREAD_SAFETY_ANALYSIS { |
| 230 | return v; |
| 231 | } |
| 232 | |
| 233 | template <typename T> |
| 234 | inline T& ts_unchecked_read(T& v) NO_THREAD_SAFETY_ANALYSIS { |
| 235 | return v; |
| 236 | } |
| 237 | |
| 238 | } // namespace thread_safety_analysis |
| 239 | |
Lukasz Anforowicz | 5e71bd4 | 2018-09-17 19:28:57 | [diff] [blame] | 240 | #endif // BASE_THREAD_ANNOTATIONS_H_ |