| 1 |
// RUN: %clang_cc1 -fsyntax-only -verify -Wthread-safety -Wthread-safety-beta %s |
| 2 |
// RUN: %clang_cc1 -fsyntax-only -verify -Wthread-safety -Wthread-safety-beta -fexperimental-late-parse-attributes -DLATE_PARSING %s |
| 3 |
|
| 4 |
#define LOCKABLE __attribute__ ((lockable)) |
| 5 |
#define SCOPED_LOCKABLE __attribute__ ((scoped_lockable)) |
| 6 |
#define GUARDED_BY(...) __attribute__ ((guarded_by(__VA_ARGS__))) |
| 7 |
#define GUARDED_VAR __attribute__ ((guarded_var)) |
| 8 |
#define PT_GUARDED_BY(...) __attribute__ ((pt_guarded_by(__VA_ARGS__))) |
| 9 |
#define PT_GUARDED_VAR __attribute__ ((pt_guarded_var)) |
| 10 |
#define ACQUIRED_AFTER(...) __attribute__ ((acquired_after(__VA_ARGS__))) |
| 11 |
#define ACQUIRED_BEFORE(...) __attribute__ ((acquired_before(__VA_ARGS__))) |
| 12 |
#define EXCLUSIVE_LOCK_FUNCTION(...) __attribute__ ((exclusive_lock_function(__VA_ARGS__))) |
| 13 |
#define SHARED_LOCK_FUNCTION(...) __attribute__ ((shared_lock_function(__VA_ARGS__))) |
| 14 |
#define ASSERT_EXCLUSIVE_LOCK(...) __attribute__ ((assert_exclusive_lock(__VA_ARGS__))) |
| 15 |
#define ASSERT_SHARED_LOCK(...) __attribute__ ((assert_shared_lock(__VA_ARGS__))) |
| 16 |
#define EXCLUSIVE_TRYLOCK_FUNCTION(...) __attribute__ ((exclusive_trylock_function(__VA_ARGS__))) |
| 17 |
#define SHARED_TRYLOCK_FUNCTION(...) __attribute__ ((shared_trylock_function(__VA_ARGS__))) |
| 18 |
#define UNLOCK_FUNCTION(...) __attribute__ ((unlock_function(__VA_ARGS__))) |
| 19 |
#define LOCK_RETURNED(x) __attribute__ ((lock_returned(x))) |
| 20 |
#define LOCKS_EXCLUDED(...) __attribute__ ((locks_excluded(__VA_ARGS__))) |
| 21 |
#define EXCLUSIVE_LOCKS_REQUIRED(...) \ |
| 22 |
__attribute__ ((exclusive_locks_required(__VA_ARGS__))) |
| 23 |
#define SHARED_LOCKS_REQUIRED(...) \ |
| 24 |
__attribute__ ((shared_locks_required(__VA_ARGS__))) |
| 25 |
#define NO_THREAD_SAFETY_ANALYSIS __attribute__ ((no_thread_safety_analysis)) |
| 26 |
|
| 27 |
// Define the mutex struct. |
| 28 |
// Simplified only for test purpose. |
| 29 |
struct LOCKABLE Mutex {}; |
| 30 |
|
| 31 |
struct Foo { |
| 32 |
struct Mutex *mu_; |
| 33 |
int a_value GUARDED_BY(mu_); |
| 34 |
|
| 35 |
struct Bar { |
| 36 |
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. |
| 37 |
struct Mutex *third_mu ACQUIRED_BEFORE(other_mu); |
| 38 |
} bar; |
| 39 |
|
| 40 |
int* a_ptr PT_GUARDED_BY(bar.other_mu); |
| 41 |
}; |
| 42 |
|
| 43 |
struct LOCKABLE Lock {}; |
| 44 |
struct A { |
| 45 |
struct Lock lock; |
| 46 |
union { |
| 47 |
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. |
| 48 |
}; |
| 49 |
}; |
| 50 |
|
| 51 |
// Declare mutex lock/unlock functions. |
| 52 |
void mutex_exclusive_lock(struct Mutex *mu) EXCLUSIVE_LOCK_FUNCTION(mu); |
| 53 |
void mutex_shared_lock(struct Mutex *mu) SHARED_LOCK_FUNCTION(mu); |
| 54 |
void mutex_unlock(struct Mutex *mu) UNLOCK_FUNCTION(mu); |
| 55 |
void mutex_shared_unlock(struct Mutex *mu) __attribute__((release_shared_capability(mu))); |
| 56 |
void mutex_exclusive_unlock(struct Mutex *mu) __attribute__((release_capability(mu))); |
| 57 |
|
| 58 |
// Define global variables. |
| 59 |
struct Mutex mu1; |
| 60 |
struct Mutex mu2 ACQUIRED_AFTER(mu1); |
| 61 |
struct Foo foo_ = {&mu1}; |
| 62 |
int a_ GUARDED_BY(foo_.mu_); |
| 63 |
int *b_ PT_GUARDED_BY(foo_.mu_) = &a_; |
| 64 |
int c_ GUARDED_VAR; |
| 65 |
int *d_ PT_GUARDED_VAR = &c_; |
| 66 |
|
| 67 |
// Define test functions. |
| 68 |
int Foo_fun1(int i) SHARED_LOCKS_REQUIRED(mu2) EXCLUSIVE_LOCKS_REQUIRED(mu1) { |
| 69 |
return i; |
| 70 |
} |
| 71 |
|
| 72 |
int Foo_fun2(int i) EXCLUSIVE_LOCKS_REQUIRED(mu2) SHARED_LOCKS_REQUIRED(mu1) { |
| 73 |
return i; |
| 74 |
} |
| 75 |
|
| 76 |
int Foo_func3(int i) LOCKS_EXCLUDED(mu1, mu2) { |
| 77 |
return i; |
| 78 |
} |
| 79 |
|
| 80 |
static int Bar_fun1(int i) EXCLUSIVE_LOCKS_REQUIRED(mu1) { |
| 81 |
return i; |
| 82 |
} |
| 83 |
|
| 84 |
void set_value(int *a, int value) EXCLUSIVE_LOCKS_REQUIRED(foo_.mu_) { |
| 85 |
*a = value; |
| 86 |
} |
| 87 |
|
| 88 |
int get_value(int *p) SHARED_LOCKS_REQUIRED(foo_.mu_){ |
| 89 |
return *p; |
| 90 |
} |
| 91 |
|
| 92 |
void unlock_scope(struct Mutex *const *mu) __attribute__((release_capability(**mu))); |
| 93 |
|
| 94 |
// Verify late parsing: |
| 95 |
#ifdef LATE_PARSING |
| 96 |
struct LateParsing { |
| 97 |
int a_value_defined_before GUARDED_BY(a_mutex_defined_late); |
| 98 |
int *a_ptr_defined_before PT_GUARDED_BY(a_mutex_defined_late); |
| 99 |
struct Mutex *a_mutex_defined_early |
| 100 |
ACQUIRED_BEFORE(a_mutex_defined_late); |
| 101 |
struct Mutex *a_mutex_defined_late |
| 102 |
ACQUIRED_AFTER(a_mutex_defined_very_late); |
| 103 |
struct Mutex *a_mutex_defined_very_late; |
| 104 |
} late_parsing; |
| 105 |
#endif |
| 106 |
|
| 107 |
int main(void) { |
| 108 |
|
| 109 |
Foo_fun1(1); // expected-warning{{calling function 'Foo_fun1' requires holding mutex 'mu2'}} \ |
| 110 |
expected-warning{{calling function 'Foo_fun1' requires holding mutex 'mu1' exclusively}} |
| 111 |
|
| 112 |
mutex_exclusive_lock(&mu1); // expected-note{{mutex acquired here}} |
| 113 |
mutex_shared_lock(&mu2); |
| 114 |
Foo_fun1(1); |
| 115 |
|
| 116 |
mutex_shared_lock(&mu1); // expected-warning{{acquiring mutex 'mu1' that is already held}} \ |
| 117 |
expected-warning{{mutex 'mu1' must be acquired before 'mu2'}} |
| 118 |
mutex_unlock(&mu1); |
| 119 |
mutex_unlock(&mu2); |
| 120 |
mutex_shared_lock(&mu1); |
| 121 |
mutex_exclusive_lock(&mu2); |
| 122 |
Foo_fun2(2); |
| 123 |
|
| 124 |
mutex_unlock(&mu2); |
| 125 |
mutex_unlock(&mu1); |
| 126 |
mutex_exclusive_lock(&mu1); |
| 127 |
Bar_fun1(3); |
| 128 |
mutex_unlock(&mu1); |
| 129 |
|
| 130 |
mutex_exclusive_lock(&mu1); |
| 131 |
Foo_func3(4); // expected-warning{{cannot call function 'Foo_func3' while mutex 'mu1' is held}} |
| 132 |
mutex_unlock(&mu1); |
| 133 |
|
| 134 |
Foo_func3(5); |
| 135 |
|
| 136 |
set_value(&a_, 0); // expected-warning{{calling function 'set_value' requires holding mutex 'foo_.mu_' exclusively}} |
| 137 |
get_value(b_); // expected-warning{{calling function 'get_value' requires holding mutex 'foo_.mu_'}} |
| 138 |
mutex_exclusive_lock(foo_.mu_); |
| 139 |
set_value(&a_, 1); |
| 140 |
mutex_unlock(foo_.mu_); |
| 141 |
mutex_shared_lock(foo_.mu_); |
| 142 |
(void)(get_value(b_) == 1); |
| 143 |
mutex_unlock(foo_.mu_); |
| 144 |
|
| 145 |
c_ = 0; // expected-warning{{writing variable 'c_' requires holding any mutex exclusively}} |
| 146 |
(void)(*d_ == 0); // expected-warning{{reading the value pointed to by 'd_' requires holding any mutex}} |
| 147 |
mutex_exclusive_lock(foo_.mu_); |
| 148 |
c_ = 1; |
| 149 |
(void)(*d_ == 1); |
| 150 |
mutex_unlock(foo_.mu_); |
| 151 |
|
| 152 |
mutex_exclusive_lock(&mu1); // expected-note {{mutex acquired here}} |
| 153 |
mutex_shared_unlock(&mu1); // expected-warning {{releasing mutex 'mu1' using shared access, expected exclusive access}} |
| 154 |
// expected-note@-1{{mutex released here}} |
| 155 |
mutex_exclusive_unlock(&mu1); // expected-warning {{releasing mutex 'mu1' that was not held}} |
| 156 |
|
| 157 |
mutex_shared_lock(&mu1); // expected-note {{mutex acquired here}} |
| 158 |
mutex_exclusive_unlock(&mu1); // expected-warning {{releasing mutex 'mu1' using exclusive access, expected shared access}} |
| 159 |
// expected-note@-1{{mutex released here}} |
| 160 |
mutex_shared_unlock(&mu1); // expected-warning {{releasing mutex 'mu1' that was not held}} |
| 161 |
|
| 162 |
/// Cleanup functions |
| 163 |
{ |
| 164 |
struct Mutex* const __attribute__((cleanup(unlock_scope))) scope = &mu1; |
| 165 |
mutex_exclusive_lock(scope); // Note that we have to lock through scope, because no alias analysis! |
| 166 |
// Cleanup happens automatically -> no warning. |
| 167 |
} |
| 168 |
|
| 169 |
foo_.a_value = 0; // expected-warning {{writing variable 'a_value' requires holding mutex 'mu_' exclusively}} |
| 170 |
*foo_.a_ptr = 1; // expected-warning {{writing the value pointed to by 'a_ptr' requires holding mutex 'bar.other_mu' exclusively}} |
| 171 |
|
| 172 |
|
| 173 |
mutex_exclusive_lock(foo_.bar.other_mu); |
| 174 |
mutex_exclusive_lock(foo_.bar.third_mu); // expected-warning{{mutex 'third_mu' must be acquired before 'other_mu'}} |
| 175 |
mutex_exclusive_lock(foo_.mu_); // expected-warning{{mutex 'mu_' must be acquired before 'other_mu'}} |
| 176 |
mutex_exclusive_unlock(foo_.mu_); |
| 177 |
mutex_exclusive_unlock(foo_.bar.other_mu); |
| 178 |
mutex_exclusive_unlock(foo_.bar.third_mu); |
| 179 |
|
| 180 |
#ifdef LATE_PARSING |
| 181 |
late_parsing.a_value_defined_before = 1; // expected-warning{{writing variable 'a_value_defined_before' requires holding mutex 'a_mutex_defined_late' exclusively}} |
| 182 |
late_parsing.a_ptr_defined_before = 0; |
| 183 |
mutex_exclusive_lock(late_parsing.a_mutex_defined_late); |
| 184 |
mutex_exclusive_lock(late_parsing.a_mutex_defined_early); // expected-warning{{mutex 'a_mutex_defined_early' must be acquired before 'a_mutex_defined_late'}} |
| 185 |
mutex_exclusive_unlock(late_parsing.a_mutex_defined_early); |
| 186 |
mutex_exclusive_unlock(late_parsing.a_mutex_defined_late); |
| 187 |
mutex_exclusive_lock(late_parsing.a_mutex_defined_late); |
| 188 |
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'}} |
| 189 |
mutex_exclusive_unlock(late_parsing.a_mutex_defined_very_late); |
| 190 |
mutex_exclusive_unlock(late_parsing.a_mutex_defined_late); |
| 191 |
#endif |
| 192 |
|
| 193 |
return 0; |
| 194 |
} |
| 195 |
|
| 196 |
// We had a problem where we'd skip all attributes that follow a late-parsed |
| 197 |
// attribute in a single __attribute__. |
| 198 |
void run(void) __attribute__((guarded_by(mu1), guarded_by(mu1))); // expected-warning 2{{only applies to non-static data members and global variables}} |
| 199 |
|
| 200 |
int value_with_wrong_number_of_args GUARDED_BY(mu1, mu2); // expected-error{{'guarded_by' attribute takes one argument}} |
| 201 |
|
| 202 |
int *ptr_with_wrong_number_of_args PT_GUARDED_BY(mu1, mu2); // expected-error{{'pt_guarded_by' attribute takes one argument}} |
| 203 |
|
| 204 |
int value_with_no_open_brace __attribute__((guarded_by)); // expected-error{{'guarded_by' attribute takes one argument}} |
| 205 |
int *ptr_with_no_open_brace __attribute__((pt_guarded_by)); // expected-error{{'pt_guarded_by' attribute takes one argument}} |
| 206 |
|
| 207 |
int value_with_no_open_brace_on_acquire_after __attribute__((acquired_after)); // expected-error{{'acquired_after' attribute takes at least 1 argument}} |
| 208 |
int value_with_no_open_brace_on_acquire_before __attribute__((acquired_before)); // expected-error{{'acquired_before' attribute takes at least 1 argument}} |
| 209 |
|
| 210 |
int value_with_bad_expr GUARDED_BY(bad_expr); // expected-error{{use of undeclared identifier 'bad_expr'}} |
| 211 |
int *ptr_with_bad_expr PT_GUARDED_BY(bad_expr); // expected-error{{use of undeclared identifier 'bad_expr'}} |
| 212 |
|
| 213 |
int value_with_bad_expr_on_acquire_after __attribute__((acquired_after(other_bad_expr))); // expected-error{{use of undeclared identifier 'other_bad_expr'}} |
| 214 |
int value_with_bad_expr_on_acquire_before __attribute__((acquired_before(other_bad_expr))); // expected-error{{use of undeclared identifier 'other_bad_expr'}} |
| 215 |
|
| 216 |
int a_final_expression = 0; |