| 1 |
// RUN: %clang_cc1 -fsyntax-only -verify -Wthread-safety -Wthread-safety-pointer -Wthread-safety-beta %s |
| 2 |
// RUN: %clang_cc1 -fsyntax-only -verify -Wthread-safety -Wthread-safety-pointer -Wthread-safety-beta -fexperimental-late-parse-attributes -DLATE_PARSING %s |
| 3 |
|
| 4 |
#define LOCKABLE __attribute__ ((lockable)) |
| 5 |
#define REENTRANT_CAPABILITY __attribute__ ((reentrant_capability)) |
| 6 |
#define SCOPED_LOCKABLE __attribute__ ((scoped_lockable)) |
| 7 |
#define GUARDED_BY(...) __attribute__ ((guarded_by(__VA_ARGS__))) |
| 8 |
#define GUARDED_VAR __attribute__ ((guarded_var)) |
| 9 |
#define PT_GUARDED_BY(...) __attribute__ ((pt_guarded_by(__VA_ARGS__))) |
| 10 |
#define PT_GUARDED_VAR __attribute__ ((pt_guarded_var)) |
| 11 |
#define ACQUIRED_AFTER(...) __attribute__ ((acquired_after(__VA_ARGS__))) |
| 12 |
#define ACQUIRED_BEFORE(...) __attribute__ ((acquired_before(__VA_ARGS__))) |
| 13 |
#define EXCLUSIVE_LOCK_FUNCTION(...) __attribute__ ((exclusive_lock_function(__VA_ARGS__))) |
| 14 |
#define SHARED_LOCK_FUNCTION(...) __attribute__ ((shared_lock_function(__VA_ARGS__))) |
| 15 |
#define ASSERT_EXCLUSIVE_LOCK(...) __attribute__ ((assert_exclusive_lock(__VA_ARGS__))) |
| 16 |
#define ASSERT_SHARED_LOCK(...) __attribute__ ((assert_shared_lock(__VA_ARGS__))) |
| 17 |
#define EXCLUSIVE_TRYLOCK_FUNCTION(...) __attribute__ ((exclusive_trylock_function(__VA_ARGS__))) |
| 18 |
#define SHARED_TRYLOCK_FUNCTION(...) __attribute__ ((shared_trylock_function(__VA_ARGS__))) |
| 19 |
#define UNLOCK_FUNCTION(...) __attribute__ ((unlock_function(__VA_ARGS__))) |
| 20 |
#define LOCK_RETURNED(x) __attribute__ ((lock_returned(x))) |
| 21 |
#define LOCKS_EXCLUDED(...) __attribute__ ((locks_excluded(__VA_ARGS__))) |
| 22 |
#define EXCLUSIVE_LOCKS_REQUIRED(...) \ |
| 23 |
__attribute__ ((exclusive_locks_required(__VA_ARGS__))) |
| 24 |
#define SHARED_LOCKS_REQUIRED(...) \ |
| 25 |
__attribute__ ((shared_locks_required(__VA_ARGS__))) |
| 26 |
#define NO_THREAD_SAFETY_ANALYSIS __attribute__ ((no_thread_safety_analysis)) |
| 27 |
|
| 28 |
#define __READ_ONCE(x) (*(const volatile __typeof__(x) *)&(x)) |
| 29 |
#define __WRITE_ONCE(x, val) do { *(volatile __typeof__(x) *)&(x) = (val); } while (0) |
| 30 |
|
| 31 |
// Define the mutex struct. |
| 32 |
// Simplified only for test purpose. |
| 33 |
struct LOCKABLE Mutex {}; |
| 34 |
|
| 35 |
struct Foo { |
| 36 |
struct Mutex *mu_; |
| 37 |
int a_value GUARDED_BY(mu_); |
| 38 |
|
| 39 |
struct Bar { |
| 40 |
struct Mutex *other_mu ACQUIRED_AFTER(mu_); // Note: referencing the parent structure is convenient here, but this should probably be disallowed if the child structure is re-used outside of the parent. |
| 41 |
struct Mutex *third_mu ACQUIRED_BEFORE(other_mu); |
| 42 |
} bar; |
| 43 |
|
| 44 |
int* a_ptr PT_GUARDED_BY(bar.other_mu); |
| 45 |
}; |
| 46 |
|
| 47 |
struct LOCKABLE Lock {}; |
| 48 |
struct A { |
| 49 |
struct Lock lock; |
| 50 |
union { |
| 51 |
int b __attribute__((guarded_by(lock))); // Note: referencing the parent structure is convenient here, but this should probably be disallowed if the child is re-used outside of the parent. |
| 52 |
}; |
| 53 |
}; |
| 54 |
|
| 55 |
// Declare mutex lock/unlock functions. |
| 56 |
void mutex_exclusive_lock(struct Mutex *mu) EXCLUSIVE_LOCK_FUNCTION(mu); |
| 57 |
void mutex_shared_lock(struct Mutex *mu) SHARED_LOCK_FUNCTION(mu); |
| 58 |
void mutex_unlock(struct Mutex *mu) UNLOCK_FUNCTION(mu); |
| 59 |
void mutex_shared_unlock(struct Mutex *mu) __attribute__((release_shared_capability(mu))); |
| 60 |
void mutex_exclusive_unlock(struct Mutex *mu) __attribute__((release_capability(mu))); |
| 61 |
|
| 62 |
// Define global variables. |
| 63 |
struct Mutex mu1; |
| 64 |
struct Mutex mu2 ACQUIRED_AFTER(mu1); |
| 65 |
struct Foo foo_ = {&mu1}; |
| 66 |
int a_ GUARDED_BY(foo_.mu_); |
| 67 |
int *b_ PT_GUARDED_BY(foo_.mu_) = &a_; |
| 68 |
int c_ GUARDED_VAR; |
| 69 |
int *d_ PT_GUARDED_VAR = &c_; |
| 70 |
|
| 71 |
// Define test functions. |
| 72 |
int Foo_fun1(int i) SHARED_LOCKS_REQUIRED(mu2) EXCLUSIVE_LOCKS_REQUIRED(mu1) { |
| 73 |
return i; |
| 74 |
} |
| 75 |
|
| 76 |
int Foo_fun2(int i) EXCLUSIVE_LOCKS_REQUIRED(mu2) SHARED_LOCKS_REQUIRED(mu1) { |
| 77 |
return i; |
| 78 |
} |
| 79 |
|
| 80 |
int Foo_func3(int i) LOCKS_EXCLUDED(mu1, mu2) { |
| 81 |
return i; |
| 82 |
} |
| 83 |
|
| 84 |
static int Bar_fun1(int i) EXCLUSIVE_LOCKS_REQUIRED(mu1) { |
| 85 |
return i; |
| 86 |
} |
| 87 |
|
| 88 |
void set_value(int *a, int value) EXCLUSIVE_LOCKS_REQUIRED(foo_.mu_) { |
| 89 |
*a = value; |
| 90 |
} |
| 91 |
|
| 92 |
int get_value(int *p) SHARED_LOCKS_REQUIRED(foo_.mu_){ |
| 93 |
return *p; |
| 94 |
} |
| 95 |
|
| 96 |
void unlock_scope(struct Mutex *const *mu) __attribute__((release_capability(**mu))); |
| 97 |
void unlock_scope_type_erased(int **priv) __attribute__((release_capability(*(struct Mutex **)priv))); |
| 98 |
|
| 99 |
// Verify late parsing: |
| 100 |
#ifdef LATE_PARSING |
| 101 |
struct LateParsing { |
| 102 |
int a_value_defined_before GUARDED_BY(a_mutex_defined_late); |
| 103 |
int *a_ptr_defined_before PT_GUARDED_BY(a_mutex_defined_late); |
| 104 |
struct Mutex *a_mutex_defined_early |
| 105 |
ACQUIRED_BEFORE(a_mutex_defined_late); |
| 106 |
struct Mutex *a_mutex_defined_late |
| 107 |
ACQUIRED_AFTER(a_mutex_defined_very_late); |
| 108 |
struct Mutex *a_mutex_defined_very_late; |
| 109 |
} late_parsing; |
| 110 |
#endif |
| 111 |
|
| 112 |
int main(void) { |
| 113 |
|
| 114 |
Foo_fun1(1); // expected-warning{{calling function 'Foo_fun1' requires holding mutex 'mu2'}} \ |
| 115 |
expected-warning{{calling function 'Foo_fun1' requires holding mutex 'mu1' exclusively}} |
| 116 |
|
| 117 |
mutex_exclusive_lock(&mu1); // expected-note{{mutex acquired here}} |
| 118 |
mutex_shared_lock(&mu2); |
| 119 |
Foo_fun1(1); |
| 120 |
|
| 121 |
mutex_shared_lock(&mu1); // expected-warning{{acquiring mutex 'mu1' that is already held}} \ |
| 122 |
expected-warning{{mutex 'mu1' must be acquired before 'mu2'}} |
| 123 |
mutex_unlock(&mu1); |
| 124 |
mutex_unlock(&mu2); |
| 125 |
mutex_shared_lock(&mu1); |
| 126 |
mutex_exclusive_lock(&mu2); |
| 127 |
Foo_fun2(2); |
| 128 |
|
| 129 |
mutex_unlock(&mu2); |
| 130 |
mutex_unlock(&mu1); |
| 131 |
mutex_exclusive_lock(&mu1); |
| 132 |
Bar_fun1(3); |
| 133 |
mutex_unlock(&mu1); |
| 134 |
|
| 135 |
mutex_exclusive_lock(&mu1); |
| 136 |
Foo_func3(4); // expected-warning{{cannot call function 'Foo_func3' while mutex 'mu1' is held}} |
| 137 |
mutex_unlock(&mu1); |
| 138 |
|
| 139 |
Foo_func3(5); |
| 140 |
|
| 141 |
set_value(&a_, 0); // expected-warning{{calling function 'set_value' requires holding mutex 'foo_.mu_' exclusively}} |
| 142 |
// expected-warning@-1{{passing pointer to variable 'a_' requires holding mutex 'foo_.mu_'}} |
| 143 |
get_value(b_); // expected-warning{{calling function 'get_value' requires holding mutex 'foo_.mu_'}} |
| 144 |
// expected-warning@-1{{passing pointer 'b_' requires holding mutex 'foo_.mu_'}} |
| 145 |
mutex_exclusive_lock(foo_.mu_); |
| 146 |
set_value(&a_, 1); |
| 147 |
mutex_unlock(foo_.mu_); |
| 148 |
mutex_shared_lock(foo_.mu_); |
| 149 |
(void)(get_value(b_) == 1); |
| 150 |
mutex_unlock(foo_.mu_); |
| 151 |
|
| 152 |
a_ = 0; // expected-warning{{writing variable 'a_' requires holding mutex 'foo_.mu_'}} |
| 153 |
__WRITE_ONCE(a_, 0); // expected-warning{{writing variable 'a_' requires holding mutex 'foo_.mu_'}} |
| 154 |
(void)(a_ == 0); // expected-warning{{reading variable 'a_' requires holding mutex 'foo_.mu_'}} |
| 155 |
(void)(__READ_ONCE(a_) == 0); // expected-warning{{reading variable 'a_' requires holding mutex 'foo_.mu_'}} |
| 156 |
*b_ = 0; // expected-warning{{writing the value pointed to by 'b_' requires holding mutex 'foo_.mu_' exclusively}} |
| 157 |
__WRITE_ONCE(*b_, 0); // expected-warning{{writing the value pointed to by 'b_' requires holding mutex 'foo_.mu_' exclusively}} |
| 158 |
(void)(*b_ == 0); // expected-warning{{reading the value pointed to by 'b_' requires holding mutex 'foo_.mu_'}} |
| 159 |
(void)(__READ_ONCE(*b_) == 0); // expected-warning{{reading the value pointed to by 'b_' requires holding mutex 'foo_.mu_'}} |
| 160 |
c_ = 0; // expected-warning{{writing variable 'c_' requires holding any mutex exclusively}} |
| 161 |
(void)(*d_ == 0); // expected-warning{{reading the value pointed to by 'd_' requires holding any mutex}} |
| 162 |
mutex_exclusive_lock(foo_.mu_); |
| 163 |
a_ = 0; |
| 164 |
__WRITE_ONCE(a_, 0); |
| 165 |
(void)(a_ == 0); |
| 166 |
(void)(__READ_ONCE(a_) == 0); |
| 167 |
*b_ = 0; |
| 168 |
__WRITE_ONCE(*b_, 0); |
| 169 |
(void)(*b_ == 0); |
| 170 |
(void)(__READ_ONCE(*b_) == 0); |
| 171 |
c_ = 1; |
| 172 |
(void)(*d_ == 1); |
| 173 |
mutex_unlock(foo_.mu_); |
| 174 |
|
| 175 |
mutex_exclusive_lock(&mu1); // expected-note {{mutex acquired here}} |
| 176 |
mutex_shared_unlock(&mu1); // expected-warning {{releasing mutex 'mu1' using shared access, expected exclusive access}} |
| 177 |
// expected-note@-1{{mutex released here}} |
| 178 |
mutex_exclusive_unlock(&mu1); // expected-warning {{releasing mutex 'mu1' that was not held}} |
| 179 |
|
| 180 |
mutex_shared_lock(&mu1); // expected-note {{mutex acquired here}} |
| 181 |
mutex_exclusive_unlock(&mu1); // expected-warning {{releasing mutex 'mu1' using exclusive access, expected shared access}} |
| 182 |
// expected-note@-1{{mutex released here}} |
| 183 |
mutex_shared_unlock(&mu1); // expected-warning {{releasing mutex 'mu1' that was not held}} |
| 184 |
|
| 185 |
/// Cleanup functions |
| 186 |
{ |
| 187 |
struct Mutex* const __attribute__((cleanup(unlock_scope))) scope = &mu1; |
| 188 |
mutex_exclusive_lock(scope); // Lock through scope works. |
| 189 |
// Cleanup happens automatically -> no warning. |
| 190 |
} |
| 191 |
{ |
| 192 |
struct Mutex* const __attribute__((unused, cleanup(unlock_scope))) scope = &mu1; |
| 193 |
mutex_exclusive_lock(&mu1); // With basic alias analysis lock through mu1 also works. |
| 194 |
} |
| 195 |
// Cleanup through cast alias pointer in a for-loop; a variant of this pattern |
| 196 |
// appears in the Linux kernel for generic scoped guard macros. |
| 197 |
for (int i = (mutex_exclusive_lock(foo_.mu_), 0), |
| 198 |
*priv __attribute__((cleanup(unlock_scope_type_erased))) = (int *)(__UINTPTR_TYPE__)(foo_.mu_); |
| 199 |
!i; i++) { |
| 200 |
a_ = 42; |
| 201 |
} |
| 202 |
|
| 203 |
foo_.a_value = 0; // expected-warning {{writing variable 'a_value' requires holding mutex 'mu_' exclusively}} |
| 204 |
*foo_.a_ptr = 1; // expected-warning {{writing the value pointed to by 'a_ptr' requires holding mutex 'bar.other_mu' exclusively}} |
| 205 |
|
| 206 |
|
| 207 |
mutex_exclusive_lock(foo_.bar.other_mu); |
| 208 |
mutex_exclusive_lock(foo_.bar.third_mu); // expected-warning{{mutex 'third_mu' must be acquired before 'other_mu'}} |
| 209 |
mutex_exclusive_lock(foo_.mu_); // expected-warning{{mutex 'mu_' must be acquired before 'other_mu'}} |
| 210 |
mutex_exclusive_unlock(foo_.mu_); |
| 211 |
mutex_exclusive_unlock(foo_.bar.other_mu); |
| 212 |
mutex_exclusive_unlock(foo_.bar.third_mu); |
| 213 |
|
| 214 |
#ifdef LATE_PARSING |
| 215 |
late_parsing.a_value_defined_before = 1; // expected-warning{{writing variable 'a_value_defined_before' requires holding mutex 'a_mutex_defined_late' exclusively}} |
| 216 |
late_parsing.a_ptr_defined_before = 0; |
| 217 |
set_value(&late_parsing.a_value_defined_before, 0); // expected-warning{{calling function 'set_value' requires holding mutex 'foo_.mu_' exclusively}} |
| 218 |
// expected-warning@-1{{passing pointer to variable 'a_value_defined_before' requires holding mutex 'a_mutex_defined_late'}} |
| 219 |
mutex_exclusive_lock(late_parsing.a_mutex_defined_late); |
| 220 |
mutex_exclusive_lock(late_parsing.a_mutex_defined_early); // expected-warning{{mutex 'a_mutex_defined_early' must be acquired before 'a_mutex_defined_late'}} |
| 221 |
mutex_exclusive_unlock(late_parsing.a_mutex_defined_early); |
| 222 |
mutex_exclusive_unlock(late_parsing.a_mutex_defined_late); |
| 223 |
mutex_exclusive_lock(late_parsing.a_mutex_defined_late); |
| 224 |
mutex_exclusive_lock(late_parsing.a_mutex_defined_very_late); // expected-warning{{mutex 'a_mutex_defined_very_late' must be acquired before 'a_mutex_defined_late'}} |
| 225 |
mutex_exclusive_unlock(late_parsing.a_mutex_defined_very_late); |
| 226 |
mutex_exclusive_unlock(late_parsing.a_mutex_defined_late); |
| 227 |
#endif |
| 228 |
|
| 229 |
return 0; |
| 230 |
} |
| 231 |
|
| 232 |
/*** Reentrancy test ***/ |
| 233 |
struct LOCKABLE REENTRANT_CAPABILITY ReentrantMutex {}; |
| 234 |
void reentrant_mutex_lock(struct ReentrantMutex *mu) EXCLUSIVE_LOCK_FUNCTION(mu); |
| 235 |
void reentrant_mutex_unlock(struct ReentrantMutex *mu) UNLOCK_FUNCTION(mu); |
| 236 |
|
| 237 |
struct ReentrantMutex rmu; |
| 238 |
int r_ GUARDED_BY(&rmu); |
| 239 |
|
| 240 |
void test_reentrant(void) { |
| 241 |
reentrant_mutex_lock(&rmu); |
| 242 |
r_ = 1; |
| 243 |
reentrant_mutex_lock(&rmu); |
| 244 |
r_ = 1; |
| 245 |
reentrant_mutex_unlock(&rmu); |
| 246 |
r_ = 1; |
| 247 |
reentrant_mutex_unlock(&rmu); |
| 248 |
r_ = 1; // expected-warning{{writing variable 'r_' requires holding mutex 'rmu' exclusively}} |
| 249 |
} |
| 250 |
|
| 251 |
// In C code, object construction may want to initialize guarded members |
| 252 |
// alongside a capability (initialization implies exclusive access); to do so, |
| 253 |
// we can assert a capability as part of its initialization. To permit immediate |
| 254 |
// use of the capability, it is necessarily to "promote" the type to a reentrant |
| 255 |
// capability for the rest of the scope. While this avoids the false positive |
| 256 |
// warning on member initialization and immediate use, it does pose the danger |
| 257 |
// of false negatives (no warning on actual double-lock in the same function). |
| 258 |
void mutex_init(struct Mutex *mu) ASSERT_EXCLUSIVE_LOCK((struct ReentrantMutex *)mu); |
| 259 |
|
| 260 |
struct TestInit { |
| 261 |
struct Mutex mu; |
| 262 |
int a GUARDED_BY(&mu); |
| 263 |
}; |
| 264 |
|
| 265 |
struct TestInit test_init(void) { |
| 266 |
struct TestInit foo; |
| 267 |
|
| 268 |
mutex_init(&foo.mu); // initialize capability before guarded members |
| 269 |
foo.a = 0; // initialize guarded members normally |
| 270 |
|
| 271 |
// It should be allowed to immediately use the capability in the same |
| 272 |
// function, such as after spawning a thread. |
| 273 |
mutex_exclusive_lock(&foo.mu); |
| 274 |
mutex_exclusive_unlock(&foo.mu); |
| 275 |
|
| 276 |
return foo; |
| 277 |
} |
| 278 |
|
| 279 |
// We had a problem where we'd skip all attributes that follow a late-parsed |
| 280 |
// attribute in a single __attribute__. |
| 281 |
void run(void) __attribute__((guarded_by(mu1), guarded_by(mu1))); // expected-warning 2{{only applies to non-static data members and global variables}} |
| 282 |
|
| 283 |
int value_with_wrong_number_of_args GUARDED_BY(mu1, mu2); // expected-error{{'guarded_by' attribute takes one argument}} |
| 284 |
|
| 285 |
int *ptr_with_wrong_number_of_args PT_GUARDED_BY(mu1, mu2); // expected-error{{'pt_guarded_by' attribute takes one argument}} |
| 286 |
|
| 287 |
int value_with_no_open_brace __attribute__((guarded_by)); // expected-error{{'guarded_by' attribute takes one argument}} |
| 288 |
int *ptr_with_no_open_brace __attribute__((pt_guarded_by)); // expected-error{{'pt_guarded_by' attribute takes one argument}} |
| 289 |
|
| 290 |
int value_with_no_open_brace_on_acquire_after __attribute__((acquired_after)); // expected-error{{'acquired_after' attribute takes at least 1 argument}} |
| 291 |
int value_with_no_open_brace_on_acquire_before __attribute__((acquired_before)); // expected-error{{'acquired_before' attribute takes at least 1 argument}} |
| 292 |
|
| 293 |
int value_with_bad_expr GUARDED_BY(bad_expr); // expected-error{{use of undeclared identifier 'bad_expr'}} |
| 294 |
int *ptr_with_bad_expr PT_GUARDED_BY(bad_expr); // expected-error{{use of undeclared identifier 'bad_expr'}} |
| 295 |
|
| 296 |
int value_with_bad_expr_on_acquire_after __attribute__((acquired_after(other_bad_expr))); // expected-error{{use of undeclared identifier 'other_bad_expr'}} |
| 297 |
int value_with_bad_expr_on_acquire_before __attribute__((acquired_before(other_bad_expr))); // expected-error{{use of undeclared identifier 'other_bad_expr'}} |
| 298 |
|
| 299 |
int a_final_expression = 0; |