Ted Kremenek | 722398f | 2012-08-24 20:39:55 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -analyze -analyzer-checker=alpha.unix.PthreadLock -verify %s |
Jordy Rose | d9c5221 | 2011-07-19 20:21:41 +0000 | [diff] [blame] | 2 | |
| 3 | // Tests performing normal locking patterns and wrong locking orders |
| 4 | |
| 5 | typedef struct { |
| 6 | void *foo; |
| 7 | } pthread_mutex_t; |
| 8 | |
| 9 | typedef pthread_mutex_t lck_mtx_t; |
| 10 | |
| 11 | extern int pthread_mutex_lock(pthread_mutex_t *); |
| 12 | extern int pthread_mutex_unlock(pthread_mutex_t *); |
| 13 | extern int pthread_mutex_trylock(pthread_mutex_t *); |
| 14 | extern int lck_mtx_lock(lck_mtx_t *); |
| 15 | extern int lck_mtx_unlock(lck_mtx_t *); |
| 16 | extern int lck_mtx_try_lock(lck_mtx_t *); |
| 17 | |
| 18 | pthread_mutex_t mtx1, mtx2; |
| 19 | lck_mtx_t lck1, lck2; |
| 20 | |
| 21 | void |
| 22 | ok1(void) |
| 23 | { |
| 24 | pthread_mutex_lock(&mtx1); // no-warning |
| 25 | } |
| 26 | |
| 27 | void |
| 28 | ok2(void) |
| 29 | { |
| 30 | pthread_mutex_unlock(&mtx1); // no-warning |
| 31 | } |
| 32 | |
| 33 | void |
| 34 | ok3(void) |
| 35 | { |
| 36 | pthread_mutex_lock(&mtx1); // no-warning |
| 37 | pthread_mutex_unlock(&mtx1); // no-warning |
| 38 | pthread_mutex_lock(&mtx1); // no-warning |
| 39 | pthread_mutex_unlock(&mtx1); // no-warning |
| 40 | } |
| 41 | |
| 42 | void |
| 43 | ok4(void) |
| 44 | { |
| 45 | pthread_mutex_lock(&mtx1); // no-warning |
| 46 | pthread_mutex_unlock(&mtx1); // no-warning |
| 47 | pthread_mutex_lock(&mtx2); // no-warning |
| 48 | pthread_mutex_unlock(&mtx2); // no-warning |
| 49 | } |
| 50 | |
| 51 | void |
| 52 | ok5(void) |
| 53 | { |
| 54 | if (pthread_mutex_trylock(&mtx1) == 0) // no-warning |
| 55 | pthread_mutex_unlock(&mtx1); // no-warning |
| 56 | } |
| 57 | |
| 58 | void |
| 59 | ok6(void) |
| 60 | { |
| 61 | lck_mtx_lock(&lck1); // no-warning |
| 62 | } |
| 63 | |
| 64 | void |
| 65 | ok7(void) |
| 66 | { |
| 67 | if (lck_mtx_try_lock(&lck1) != 0) // no-warning |
| 68 | lck_mtx_unlock(&lck1); // no-warning |
| 69 | } |
| 70 | |
| 71 | void |
Jordan Rose | 0696bb4 | 2014-04-01 03:40:38 +0000 | [diff] [blame^] | 72 | ok8(void) |
| 73 | { |
| 74 | pthread_mutex_lock(&mtx1); // no-warning |
| 75 | pthread_mutex_lock(&mtx2); // no-warning |
| 76 | pthread_mutex_unlock(&mtx2); // no-warning |
| 77 | pthread_mutex_unlock(&mtx1); // no-warning |
| 78 | } |
| 79 | |
| 80 | void |
| 81 | ok9(void) |
| 82 | { |
| 83 | pthread_mutex_unlock(&mtx1); // no-warning |
| 84 | if (pthread_mutex_trylock(&mtx1) == 0) // no-warning |
| 85 | pthread_mutex_unlock(&mtx1); // no-warning |
| 86 | } |
| 87 | |
| 88 | void |
| 89 | ok10(void) |
| 90 | { |
| 91 | if (pthread_mutex_trylock(&mtx1) != 0) // no-warning |
| 92 | pthread_mutex_lock(&mtx1); // no-warning |
| 93 | pthread_mutex_unlock(&mtx1); // no-warning |
| 94 | } |
| 95 | |
| 96 | void |
Jordy Rose | d9c5221 | 2011-07-19 20:21:41 +0000 | [diff] [blame] | 97 | bad1(void) |
| 98 | { |
| 99 | pthread_mutex_lock(&mtx1); // no-warning |
| 100 | pthread_mutex_lock(&mtx1); // expected-warning{{This lock has already been acquired}} |
| 101 | } |
| 102 | |
| 103 | void |
| 104 | bad2(void) |
| 105 | { |
| 106 | pthread_mutex_lock(&mtx1); // no-warning |
| 107 | pthread_mutex_unlock(&mtx1); // no-warning |
| 108 | pthread_mutex_lock(&mtx1); // no-warning |
| 109 | pthread_mutex_lock(&mtx1); // expected-warning{{This lock has already been acquired}} |
| 110 | } |
| 111 | |
| 112 | void |
| 113 | bad3(void) |
| 114 | { |
| 115 | pthread_mutex_lock(&mtx1); // no-warning |
| 116 | pthread_mutex_lock(&mtx2); // no-warning |
| 117 | pthread_mutex_unlock(&mtx1); // expected-warning{{This was not the most recently acquired lock}} |
| 118 | pthread_mutex_unlock(&mtx2); |
| 119 | } |
| 120 | |
| 121 | void |
| 122 | bad4(void) |
| 123 | { |
| 124 | if (pthread_mutex_trylock(&mtx1)) // no-warning |
| 125 | return; |
| 126 | pthread_mutex_lock(&mtx2); // no-warning |
| 127 | pthread_mutex_unlock(&mtx1); // expected-warning{{This was not the most recently acquired lock}} |
| 128 | } |
| 129 | |
| 130 | void |
| 131 | bad5(void) |
| 132 | { |
| 133 | lck_mtx_lock(&lck1); // no-warning |
| 134 | lck_mtx_lock(&lck1); // expected-warning{{This lock has already been acquired}} |
| 135 | } |
| 136 | |
| 137 | void |
| 138 | bad6(void) |
| 139 | { |
| 140 | lck_mtx_lock(&lck1); // no-warning |
| 141 | lck_mtx_unlock(&lck1); // no-warning |
| 142 | lck_mtx_lock(&lck1); // no-warning |
| 143 | lck_mtx_lock(&lck1); // expected-warning{{This lock has already been acquired}} |
| 144 | } |
| 145 | |
| 146 | void |
| 147 | bad7(void) |
| 148 | { |
| 149 | lck_mtx_lock(&lck1); // no-warning |
| 150 | lck_mtx_lock(&lck2); // no-warning |
| 151 | lck_mtx_unlock(&lck1); // expected-warning{{This was not the most recently acquired lock}} |
| 152 | lck_mtx_unlock(&lck2); |
| 153 | } |
| 154 | |
| 155 | void |
| 156 | bad8(void) |
| 157 | { |
| 158 | if (lck_mtx_try_lock(&lck1) == 0) // no-warning |
| 159 | return; |
| 160 | lck_mtx_lock(&lck2); // no-warning |
| 161 | lck_mtx_unlock(&lck1); // expected-warning{{This was not the most recently acquired lock}} |
| 162 | } |
Jordan Rose | 0696bb4 | 2014-04-01 03:40:38 +0000 | [diff] [blame^] | 163 | |
| 164 | void |
| 165 | bad9(void) |
| 166 | { |
| 167 | lck_mtx_unlock(&lck1); // no-warning |
| 168 | lck_mtx_unlock(&lck1); // expected-warning{{This lock has already been unlocked}} |
| 169 | } |
| 170 | |
| 171 | void |
| 172 | bad10(void) |
| 173 | { |
| 174 | lck_mtx_lock(&lck1); // no-warning |
| 175 | lck_mtx_unlock(&lck1); // no-warning |
| 176 | lck_mtx_unlock(&lck1); // expected-warning{{This lock has already been unlocked}} |
| 177 | } |
| 178 | |
| 179 | static void |
| 180 | bad11_sub(pthread_mutex_t *lock) |
| 181 | { |
| 182 | lck_mtx_unlock(lock); // expected-warning{{This lock has already been unlocked}} |
| 183 | } |
| 184 | |
| 185 | void |
| 186 | bad11(int i) |
| 187 | { |
| 188 | lck_mtx_lock(&lck1); // no-warning |
| 189 | lck_mtx_unlock(&lck1); // no-warning |
| 190 | if (i < 5) |
| 191 | bad11_sub(&lck1); |
| 192 | } |
| 193 | |
| 194 | void |
| 195 | bad12(void) |
| 196 | { |
| 197 | pthread_mutex_lock(&mtx1); // no-warning |
| 198 | pthread_mutex_unlock(&mtx1); // no-warning |
| 199 | pthread_mutex_lock(&mtx1); // no-warning |
| 200 | pthread_mutex_unlock(&mtx1); // no-warning |
| 201 | pthread_mutex_unlock(&mtx1); // expected-warning{{This lock has already been unlocked}} |
| 202 | } |
| 203 | |
| 204 | void |
| 205 | bad13(void) |
| 206 | { |
| 207 | pthread_mutex_lock(&mtx1); // no-warning |
| 208 | pthread_mutex_unlock(&mtx1); // no-warning |
| 209 | pthread_mutex_lock(&mtx2); // no-warning |
| 210 | pthread_mutex_unlock(&mtx2); // no-warning |
| 211 | pthread_mutex_unlock(&mtx1); // expected-warning{{This lock has already been unlocked}} |
| 212 | } |
| 213 | |
| 214 | void |
| 215 | bad14(void) |
| 216 | { |
| 217 | pthread_mutex_lock(&mtx1); // no-warning |
| 218 | pthread_mutex_lock(&mtx2); // no-warning |
| 219 | pthread_mutex_unlock(&mtx2); // no-warning |
| 220 | pthread_mutex_unlock(&mtx1); // no-warning |
| 221 | pthread_mutex_unlock(&mtx2); // expected-warning{{This lock has already been unlocked}} |
| 222 | } |
| 223 | |
| 224 | void |
| 225 | bad15(void) |
| 226 | { |
| 227 | pthread_mutex_lock(&mtx1); // no-warning |
| 228 | pthread_mutex_lock(&mtx2); // no-warning |
| 229 | pthread_mutex_unlock(&mtx2); // no-warning |
| 230 | pthread_mutex_unlock(&mtx1); // no-warning |
| 231 | pthread_mutex_lock(&mtx1); // no-warning |
| 232 | pthread_mutex_unlock(&mtx2); // expected-warning{{This lock has already been unlocked}} |
| 233 | } |