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 | |
Jordan Rose | 7fcaa14 | 2014-04-01 03:40:47 +0000 | [diff] [blame^] | 9 | typedef struct { |
| 10 | void *foo; |
| 11 | } lck_grp_t; |
| 12 | |
Jordy Rose | d9c5221 | 2011-07-19 20:21:41 +0000 | [diff] [blame] | 13 | typedef pthread_mutex_t lck_mtx_t; |
| 14 | |
| 15 | extern int pthread_mutex_lock(pthread_mutex_t *); |
| 16 | extern int pthread_mutex_unlock(pthread_mutex_t *); |
| 17 | extern int pthread_mutex_trylock(pthread_mutex_t *); |
Jordan Rose | 7fcaa14 | 2014-04-01 03:40:47 +0000 | [diff] [blame^] | 18 | extern int pthread_mutex_destroy(pthread_mutex_t *); |
Jordy Rose | d9c5221 | 2011-07-19 20:21:41 +0000 | [diff] [blame] | 19 | extern int lck_mtx_lock(lck_mtx_t *); |
| 20 | extern int lck_mtx_unlock(lck_mtx_t *); |
| 21 | extern int lck_mtx_try_lock(lck_mtx_t *); |
Jordan Rose | 7fcaa14 | 2014-04-01 03:40:47 +0000 | [diff] [blame^] | 22 | extern void lck_mtx_destroy(lck_mtx_t *lck, lck_grp_t *grp); |
Jordy Rose | d9c5221 | 2011-07-19 20:21:41 +0000 | [diff] [blame] | 23 | |
| 24 | pthread_mutex_t mtx1, mtx2; |
| 25 | lck_mtx_t lck1, lck2; |
Jordan Rose | 7fcaa14 | 2014-04-01 03:40:47 +0000 | [diff] [blame^] | 26 | lck_grp_t grp1; |
Jordy Rose | d9c5221 | 2011-07-19 20:21:41 +0000 | [diff] [blame] | 27 | |
| 28 | void |
| 29 | ok1(void) |
| 30 | { |
| 31 | pthread_mutex_lock(&mtx1); // no-warning |
| 32 | } |
| 33 | |
| 34 | void |
| 35 | ok2(void) |
| 36 | { |
| 37 | pthread_mutex_unlock(&mtx1); // no-warning |
| 38 | } |
| 39 | |
| 40 | void |
| 41 | ok3(void) |
| 42 | { |
| 43 | pthread_mutex_lock(&mtx1); // no-warning |
| 44 | pthread_mutex_unlock(&mtx1); // no-warning |
| 45 | pthread_mutex_lock(&mtx1); // no-warning |
| 46 | pthread_mutex_unlock(&mtx1); // no-warning |
| 47 | } |
| 48 | |
| 49 | void |
| 50 | ok4(void) |
| 51 | { |
| 52 | pthread_mutex_lock(&mtx1); // no-warning |
| 53 | pthread_mutex_unlock(&mtx1); // no-warning |
| 54 | pthread_mutex_lock(&mtx2); // no-warning |
| 55 | pthread_mutex_unlock(&mtx2); // no-warning |
| 56 | } |
| 57 | |
| 58 | void |
| 59 | ok5(void) |
| 60 | { |
| 61 | if (pthread_mutex_trylock(&mtx1) == 0) // no-warning |
| 62 | pthread_mutex_unlock(&mtx1); // no-warning |
| 63 | } |
| 64 | |
| 65 | void |
| 66 | ok6(void) |
| 67 | { |
| 68 | lck_mtx_lock(&lck1); // no-warning |
| 69 | } |
| 70 | |
| 71 | void |
| 72 | ok7(void) |
| 73 | { |
| 74 | if (lck_mtx_try_lock(&lck1) != 0) // no-warning |
| 75 | lck_mtx_unlock(&lck1); // no-warning |
| 76 | } |
| 77 | |
| 78 | void |
Jordan Rose | 0696bb4 | 2014-04-01 03:40:38 +0000 | [diff] [blame] | 79 | ok8(void) |
| 80 | { |
| 81 | pthread_mutex_lock(&mtx1); // no-warning |
| 82 | pthread_mutex_lock(&mtx2); // no-warning |
| 83 | pthread_mutex_unlock(&mtx2); // no-warning |
| 84 | pthread_mutex_unlock(&mtx1); // no-warning |
| 85 | } |
| 86 | |
| 87 | void |
| 88 | ok9(void) |
| 89 | { |
| 90 | pthread_mutex_unlock(&mtx1); // no-warning |
| 91 | if (pthread_mutex_trylock(&mtx1) == 0) // no-warning |
| 92 | pthread_mutex_unlock(&mtx1); // no-warning |
| 93 | } |
| 94 | |
| 95 | void |
| 96 | ok10(void) |
| 97 | { |
| 98 | if (pthread_mutex_trylock(&mtx1) != 0) // no-warning |
| 99 | pthread_mutex_lock(&mtx1); // no-warning |
| 100 | pthread_mutex_unlock(&mtx1); // no-warning |
| 101 | } |
| 102 | |
| 103 | void |
Jordan Rose | 7fcaa14 | 2014-04-01 03:40:47 +0000 | [diff] [blame^] | 104 | ok11(void) |
| 105 | { |
| 106 | pthread_mutex_destroy(&mtx1); // no-warning |
| 107 | } |
| 108 | |
| 109 | void |
| 110 | ok12(void) |
| 111 | { |
| 112 | pthread_mutex_destroy(&mtx1); // no-warning |
| 113 | pthread_mutex_destroy(&mtx2); // no-warning |
| 114 | } |
| 115 | |
| 116 | void |
| 117 | ok13(void) |
| 118 | { |
| 119 | pthread_mutex_unlock(&mtx1); // no-warning |
| 120 | pthread_mutex_destroy(&mtx1); // no-warning |
| 121 | } |
| 122 | |
| 123 | void |
| 124 | ok14(void) |
| 125 | { |
| 126 | pthread_mutex_unlock(&mtx1); // no-warning |
| 127 | pthread_mutex_destroy(&mtx1); // no-warning |
| 128 | pthread_mutex_unlock(&mtx2); // no-warning |
| 129 | pthread_mutex_destroy(&mtx2); // no-warning |
| 130 | } |
| 131 | |
| 132 | void |
| 133 | ok15(void) |
| 134 | { |
| 135 | pthread_mutex_lock(&mtx1); // no-warning |
| 136 | pthread_mutex_unlock(&mtx1); // no-warning |
| 137 | pthread_mutex_destroy(&mtx1); // no-warning |
| 138 | } |
| 139 | |
| 140 | void |
Jordy Rose | d9c5221 | 2011-07-19 20:21:41 +0000 | [diff] [blame] | 141 | bad1(void) |
| 142 | { |
| 143 | pthread_mutex_lock(&mtx1); // no-warning |
| 144 | pthread_mutex_lock(&mtx1); // expected-warning{{This lock has already been acquired}} |
| 145 | } |
| 146 | |
| 147 | void |
| 148 | bad2(void) |
| 149 | { |
| 150 | pthread_mutex_lock(&mtx1); // no-warning |
| 151 | pthread_mutex_unlock(&mtx1); // no-warning |
| 152 | pthread_mutex_lock(&mtx1); // no-warning |
| 153 | pthread_mutex_lock(&mtx1); // expected-warning{{This lock has already been acquired}} |
| 154 | } |
| 155 | |
| 156 | void |
| 157 | bad3(void) |
| 158 | { |
| 159 | pthread_mutex_lock(&mtx1); // no-warning |
| 160 | pthread_mutex_lock(&mtx2); // no-warning |
| 161 | pthread_mutex_unlock(&mtx1); // expected-warning{{This was not the most recently acquired lock}} |
| 162 | pthread_mutex_unlock(&mtx2); |
| 163 | } |
| 164 | |
| 165 | void |
| 166 | bad4(void) |
| 167 | { |
| 168 | if (pthread_mutex_trylock(&mtx1)) // no-warning |
| 169 | return; |
| 170 | pthread_mutex_lock(&mtx2); // no-warning |
| 171 | pthread_mutex_unlock(&mtx1); // expected-warning{{This was not the most recently acquired lock}} |
| 172 | } |
| 173 | |
| 174 | void |
| 175 | bad5(void) |
| 176 | { |
| 177 | lck_mtx_lock(&lck1); // no-warning |
| 178 | lck_mtx_lock(&lck1); // expected-warning{{This lock has already been acquired}} |
| 179 | } |
| 180 | |
| 181 | void |
| 182 | bad6(void) |
| 183 | { |
| 184 | lck_mtx_lock(&lck1); // no-warning |
| 185 | lck_mtx_unlock(&lck1); // no-warning |
| 186 | lck_mtx_lock(&lck1); // no-warning |
| 187 | lck_mtx_lock(&lck1); // expected-warning{{This lock has already been acquired}} |
| 188 | } |
| 189 | |
| 190 | void |
| 191 | bad7(void) |
| 192 | { |
| 193 | lck_mtx_lock(&lck1); // no-warning |
| 194 | lck_mtx_lock(&lck2); // no-warning |
| 195 | lck_mtx_unlock(&lck1); // expected-warning{{This was not the most recently acquired lock}} |
| 196 | lck_mtx_unlock(&lck2); |
| 197 | } |
| 198 | |
| 199 | void |
| 200 | bad8(void) |
| 201 | { |
| 202 | if (lck_mtx_try_lock(&lck1) == 0) // no-warning |
| 203 | return; |
| 204 | lck_mtx_lock(&lck2); // no-warning |
| 205 | lck_mtx_unlock(&lck1); // expected-warning{{This was not the most recently acquired lock}} |
| 206 | } |
Jordan Rose | 0696bb4 | 2014-04-01 03:40:38 +0000 | [diff] [blame] | 207 | |
| 208 | void |
| 209 | bad9(void) |
| 210 | { |
| 211 | lck_mtx_unlock(&lck1); // no-warning |
| 212 | lck_mtx_unlock(&lck1); // expected-warning{{This lock has already been unlocked}} |
| 213 | } |
| 214 | |
| 215 | void |
| 216 | bad10(void) |
| 217 | { |
| 218 | lck_mtx_lock(&lck1); // no-warning |
| 219 | lck_mtx_unlock(&lck1); // no-warning |
| 220 | lck_mtx_unlock(&lck1); // expected-warning{{This lock has already been unlocked}} |
| 221 | } |
| 222 | |
| 223 | static void |
| 224 | bad11_sub(pthread_mutex_t *lock) |
| 225 | { |
| 226 | lck_mtx_unlock(lock); // expected-warning{{This lock has already been unlocked}} |
| 227 | } |
| 228 | |
| 229 | void |
| 230 | bad11(int i) |
| 231 | { |
| 232 | lck_mtx_lock(&lck1); // no-warning |
| 233 | lck_mtx_unlock(&lck1); // no-warning |
| 234 | if (i < 5) |
| 235 | bad11_sub(&lck1); |
| 236 | } |
| 237 | |
| 238 | void |
| 239 | bad12(void) |
| 240 | { |
| 241 | pthread_mutex_lock(&mtx1); // no-warning |
| 242 | pthread_mutex_unlock(&mtx1); // no-warning |
| 243 | pthread_mutex_lock(&mtx1); // no-warning |
| 244 | pthread_mutex_unlock(&mtx1); // no-warning |
| 245 | pthread_mutex_unlock(&mtx1); // expected-warning{{This lock has already been unlocked}} |
| 246 | } |
| 247 | |
| 248 | void |
| 249 | bad13(void) |
| 250 | { |
| 251 | pthread_mutex_lock(&mtx1); // no-warning |
| 252 | pthread_mutex_unlock(&mtx1); // no-warning |
| 253 | pthread_mutex_lock(&mtx2); // no-warning |
| 254 | pthread_mutex_unlock(&mtx2); // no-warning |
| 255 | pthread_mutex_unlock(&mtx1); // expected-warning{{This lock has already been unlocked}} |
| 256 | } |
| 257 | |
| 258 | void |
| 259 | bad14(void) |
| 260 | { |
| 261 | pthread_mutex_lock(&mtx1); // no-warning |
| 262 | pthread_mutex_lock(&mtx2); // no-warning |
| 263 | pthread_mutex_unlock(&mtx2); // no-warning |
| 264 | pthread_mutex_unlock(&mtx1); // no-warning |
| 265 | pthread_mutex_unlock(&mtx2); // expected-warning{{This lock has already been unlocked}} |
| 266 | } |
| 267 | |
| 268 | void |
| 269 | bad15(void) |
| 270 | { |
| 271 | pthread_mutex_lock(&mtx1); // no-warning |
| 272 | pthread_mutex_lock(&mtx2); // no-warning |
| 273 | pthread_mutex_unlock(&mtx2); // no-warning |
| 274 | pthread_mutex_unlock(&mtx1); // no-warning |
| 275 | pthread_mutex_lock(&mtx1); // no-warning |
| 276 | pthread_mutex_unlock(&mtx2); // expected-warning{{This lock has already been unlocked}} |
| 277 | } |
Jordan Rose | 7fcaa14 | 2014-04-01 03:40:47 +0000 | [diff] [blame^] | 278 | |
| 279 | void |
| 280 | bad16(void) |
| 281 | { |
| 282 | pthread_mutex_destroy(&mtx1); // no-warning |
| 283 | pthread_mutex_lock(&mtx1); // expected-warning{{This lock has already been destroyed}} |
| 284 | } |
| 285 | |
| 286 | void |
| 287 | bad17(void) |
| 288 | { |
| 289 | pthread_mutex_destroy(&mtx1); // no-warning |
| 290 | pthread_mutex_unlock(&mtx1); // expected-warning{{This lock has already been destroyed}} |
| 291 | } |
| 292 | |
| 293 | void |
| 294 | bad18(void) |
| 295 | { |
| 296 | pthread_mutex_destroy(&mtx1); // no-warning |
| 297 | pthread_mutex_destroy(&mtx1); // expected-warning{{This lock has already been destroyed}} |
| 298 | } |
| 299 | |
| 300 | void |
| 301 | bad19(void) |
| 302 | { |
| 303 | pthread_mutex_lock(&mtx1); // no-warning |
| 304 | pthread_mutex_destroy(&mtx1); // expected-warning{{This lock is still locked}} |
| 305 | } |
| 306 | |
| 307 | void |
| 308 | bad20(void) |
| 309 | { |
| 310 | lck_mtx_destroy(&mtx1, &grp1); // no-warning |
| 311 | lck_mtx_lock(&mtx1); // expected-warning{{This lock has already been destroyed}} |
| 312 | } |
| 313 | |
| 314 | void |
| 315 | bad21(void) |
| 316 | { |
| 317 | lck_mtx_destroy(&mtx1, &grp1); // no-warning |
| 318 | lck_mtx_unlock(&mtx1); // expected-warning{{This lock has already been destroyed}} |
| 319 | } |
| 320 | |
| 321 | void |
| 322 | bad22(void) |
| 323 | { |
| 324 | lck_mtx_destroy(&mtx1, &grp1); // no-warning |
| 325 | lck_mtx_destroy(&mtx1, &grp1); // expected-warning{{This lock has already been destroyed}} |
| 326 | } |
| 327 | |
| 328 | void |
| 329 | bad23(void) |
| 330 | { |
| 331 | lck_mtx_lock(&mtx1); // no-warning |
| 332 | lck_mtx_destroy(&mtx1, &grp1); // expected-warning{{This lock is still locked}} |
| 333 | } |