1 // RUN: %clang_cc1 -analyze -analyzer-checker=alpha.unix.PthreadLock -verify %s 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 struct { 10 void *foo; 11 } pthread_mutexattr_t; 12 13 typedef struct { 14 void *foo; 15 } lck_grp_t; 16 17 typedef pthread_mutex_t lck_mtx_t; 18 19 extern int pthread_mutex_lock(pthread_mutex_t *); 20 extern int pthread_mutex_unlock(pthread_mutex_t *); 21 extern int pthread_mutex_trylock(pthread_mutex_t *); 22 extern int pthread_mutex_destroy(pthread_mutex_t *); 23 extern int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *mutexattr); 24 extern int lck_mtx_lock(lck_mtx_t *); 25 extern int lck_mtx_unlock(lck_mtx_t *); 26 extern int lck_mtx_try_lock(lck_mtx_t *); 27 extern void lck_mtx_destroy(lck_mtx_t *lck, lck_grp_t *grp); 28 29 pthread_mutex_t mtx1, mtx2; 30 lck_mtx_t lck1, lck2; 31 lck_grp_t grp1; 32 33 #define NULL 0 34 35 void 36 ok1(void) 37 { 38 pthread_mutex_lock(&mtx1); // no-warning 39 } 40 41 void 42 ok2(void) 43 { 44 pthread_mutex_unlock(&mtx1); // no-warning 45 } 46 47 void 48 ok3(void) 49 { 50 pthread_mutex_lock(&mtx1); // no-warning 51 pthread_mutex_unlock(&mtx1); // no-warning 52 pthread_mutex_lock(&mtx1); // no-warning 53 pthread_mutex_unlock(&mtx1); // no-warning 54 } 55 56 void 57 ok4(void) 58 { 59 pthread_mutex_lock(&mtx1); // no-warning 60 pthread_mutex_unlock(&mtx1); // no-warning 61 pthread_mutex_lock(&mtx2); // no-warning 62 pthread_mutex_unlock(&mtx2); // no-warning 63 } 64 65 void 66 ok5(void) 67 { 68 if (pthread_mutex_trylock(&mtx1) == 0) // no-warning 69 pthread_mutex_unlock(&mtx1); // no-warning 70 } 71 72 void 73 ok6(void) 74 { 75 lck_mtx_lock(&lck1); // no-warning 76 } 77 78 void 79 ok7(void) 80 { 81 if (lck_mtx_try_lock(&lck1) != 0) // no-warning 82 lck_mtx_unlock(&lck1); // no-warning 83 } 84 85 void 86 ok8(void) 87 { 88 pthread_mutex_lock(&mtx1); // no-warning 89 pthread_mutex_lock(&mtx2); // no-warning 90 pthread_mutex_unlock(&mtx2); // no-warning 91 pthread_mutex_unlock(&mtx1); // no-warning 92 } 93 94 void 95 ok9(void) 96 { 97 pthread_mutex_unlock(&mtx1); // no-warning 98 if (pthread_mutex_trylock(&mtx1) == 0) // no-warning 99 pthread_mutex_unlock(&mtx1); // no-warning 100 } 101 102 void 103 ok10(void) 104 { 105 if (pthread_mutex_trylock(&mtx1) != 0) // no-warning 106 pthread_mutex_lock(&mtx1); // no-warning 107 pthread_mutex_unlock(&mtx1); // no-warning 108 } 109 110 void 111 ok11(void) 112 { 113 pthread_mutex_destroy(&mtx1); // no-warning 114 } 115 116 void 117 ok12(void) 118 { 119 pthread_mutex_destroy(&mtx1); // no-warning 120 pthread_mutex_destroy(&mtx2); // no-warning 121 } 122 123 void 124 ok13(void) 125 { 126 pthread_mutex_unlock(&mtx1); // no-warning 127 pthread_mutex_destroy(&mtx1); // no-warning 128 } 129 130 void 131 ok14(void) 132 { 133 pthread_mutex_unlock(&mtx1); // no-warning 134 pthread_mutex_destroy(&mtx1); // no-warning 135 pthread_mutex_unlock(&mtx2); // no-warning 136 pthread_mutex_destroy(&mtx2); // no-warning 137 } 138 139 void 140 ok15(void) 141 { 142 pthread_mutex_lock(&mtx1); // no-warning 143 pthread_mutex_unlock(&mtx1); // no-warning 144 pthread_mutex_destroy(&mtx1); // no-warning 145 } 146 147 void 148 ok16(void) 149 { 150 pthread_mutex_init(&mtx1, NULL); // no-warning 151 } 152 153 void 154 ok17(void) 155 { 156 pthread_mutex_init(&mtx1, NULL); // no-warning 157 pthread_mutex_init(&mtx2, NULL); // no-warning 158 } 159 160 void 161 ok18(void) 162 { 163 pthread_mutex_destroy(&mtx1); // no-warning 164 pthread_mutex_init(&mtx1, NULL); // no-warning 165 } 166 167 void 168 ok19(void) 169 { 170 pthread_mutex_destroy(&mtx1); // no-warning 171 pthread_mutex_init(&mtx1, NULL); // no-warning 172 pthread_mutex_destroy(&mtx2); // no-warning 173 pthread_mutex_init(&mtx2, NULL); // no-warning 174 } 175 176 void 177 ok20(void) 178 { 179 pthread_mutex_unlock(&mtx1); // no-warning 180 pthread_mutex_destroy(&mtx1); // no-warning 181 pthread_mutex_init(&mtx1, NULL); // no-warning 182 pthread_mutex_destroy(&mtx1); // no-warning 183 pthread_mutex_init(&mtx1, NULL); // no-warning 184 } 185 186 void 187 bad1(void) 188 { 189 pthread_mutex_lock(&mtx1); // no-warning 190 pthread_mutex_lock(&mtx1); // expected-warning{{This lock has already been acquired}} 191 } 192 193 void 194 bad2(void) 195 { 196 pthread_mutex_lock(&mtx1); // no-warning 197 pthread_mutex_unlock(&mtx1); // no-warning 198 pthread_mutex_lock(&mtx1); // no-warning 199 pthread_mutex_lock(&mtx1); // expected-warning{{This lock has already been acquired}} 200 } 201 202 void 203 bad3(void) 204 { 205 pthread_mutex_lock(&mtx1); // no-warning 206 pthread_mutex_lock(&mtx2); // no-warning 207 pthread_mutex_unlock(&mtx1); // expected-warning{{This was not the most recently acquired lock}} 208 pthread_mutex_unlock(&mtx2); 209 } 210 211 void 212 bad4(void) 213 { 214 if (pthread_mutex_trylock(&mtx1)) // no-warning 215 return; 216 pthread_mutex_lock(&mtx2); // no-warning 217 pthread_mutex_unlock(&mtx1); // expected-warning{{This was not the most recently acquired lock}} 218 } 219 220 void 221 bad5(void) 222 { 223 lck_mtx_lock(&lck1); // no-warning 224 lck_mtx_lock(&lck1); // expected-warning{{This lock has already been acquired}} 225 } 226 227 void 228 bad6(void) 229 { 230 lck_mtx_lock(&lck1); // no-warning 231 lck_mtx_unlock(&lck1); // no-warning 232 lck_mtx_lock(&lck1); // no-warning 233 lck_mtx_lock(&lck1); // expected-warning{{This lock has already been acquired}} 234 } 235 236 void 237 bad7(void) 238 { 239 lck_mtx_lock(&lck1); // no-warning 240 lck_mtx_lock(&lck2); // no-warning 241 lck_mtx_unlock(&lck1); // expected-warning{{This was not the most recently acquired lock}} 242 lck_mtx_unlock(&lck2); 243 } 244 245 void 246 bad8(void) 247 { 248 if (lck_mtx_try_lock(&lck1) == 0) // no-warning 249 return; 250 lck_mtx_lock(&lck2); // no-warning 251 lck_mtx_unlock(&lck1); // expected-warning{{This was not the most recently acquired lock}} 252 } 253 254 void 255 bad9(void) 256 { 257 lck_mtx_unlock(&lck1); // no-warning 258 lck_mtx_unlock(&lck1); // expected-warning{{This lock has already been unlocked}} 259 } 260 261 void 262 bad10(void) 263 { 264 lck_mtx_lock(&lck1); // no-warning 265 lck_mtx_unlock(&lck1); // no-warning 266 lck_mtx_unlock(&lck1); // expected-warning{{This lock has already been unlocked}} 267 } 268 269 static void 270 bad11_sub(pthread_mutex_t *lock) 271 { 272 lck_mtx_unlock(lock); // expected-warning{{This lock has already been unlocked}} 273 } 274 275 void 276 bad11(int i) 277 { 278 lck_mtx_lock(&lck1); // no-warning 279 lck_mtx_unlock(&lck1); // no-warning 280 if (i < 5) 281 bad11_sub(&lck1); 282 } 283 284 void 285 bad12(void) 286 { 287 pthread_mutex_lock(&mtx1); // no-warning 288 pthread_mutex_unlock(&mtx1); // no-warning 289 pthread_mutex_lock(&mtx1); // no-warning 290 pthread_mutex_unlock(&mtx1); // no-warning 291 pthread_mutex_unlock(&mtx1); // expected-warning{{This lock has already been unlocked}} 292 } 293 294 void 295 bad13(void) 296 { 297 pthread_mutex_lock(&mtx1); // no-warning 298 pthread_mutex_unlock(&mtx1); // no-warning 299 pthread_mutex_lock(&mtx2); // no-warning 300 pthread_mutex_unlock(&mtx2); // no-warning 301 pthread_mutex_unlock(&mtx1); // expected-warning{{This lock has already been unlocked}} 302 } 303 304 void 305 bad14(void) 306 { 307 pthread_mutex_lock(&mtx1); // no-warning 308 pthread_mutex_lock(&mtx2); // no-warning 309 pthread_mutex_unlock(&mtx2); // no-warning 310 pthread_mutex_unlock(&mtx1); // no-warning 311 pthread_mutex_unlock(&mtx2); // expected-warning{{This lock has already been unlocked}} 312 } 313 314 void 315 bad15(void) 316 { 317 pthread_mutex_lock(&mtx1); // no-warning 318 pthread_mutex_lock(&mtx2); // no-warning 319 pthread_mutex_unlock(&mtx2); // no-warning 320 pthread_mutex_unlock(&mtx1); // no-warning 321 pthread_mutex_lock(&mtx1); // no-warning 322 pthread_mutex_unlock(&mtx2); // expected-warning{{This lock has already been unlocked}} 323 } 324 325 void 326 bad16(void) 327 { 328 pthread_mutex_destroy(&mtx1); // no-warning 329 pthread_mutex_lock(&mtx1); // expected-warning{{This lock has already been destroyed}} 330 } 331 332 void 333 bad17(void) 334 { 335 pthread_mutex_destroy(&mtx1); // no-warning 336 pthread_mutex_unlock(&mtx1); // expected-warning{{This lock has already been destroyed}} 337 } 338 339 void 340 bad18(void) 341 { 342 pthread_mutex_destroy(&mtx1); // no-warning 343 pthread_mutex_destroy(&mtx1); // expected-warning{{This lock has already been destroyed}} 344 } 345 346 void 347 bad19(void) 348 { 349 pthread_mutex_lock(&mtx1); // no-warning 350 pthread_mutex_destroy(&mtx1); // expected-warning{{This lock is still locked}} 351 } 352 353 void 354 bad20(void) 355 { 356 lck_mtx_destroy(&mtx1, &grp1); // no-warning 357 lck_mtx_lock(&mtx1); // expected-warning{{This lock has already been destroyed}} 358 } 359 360 void 361 bad21(void) 362 { 363 lck_mtx_destroy(&mtx1, &grp1); // no-warning 364 lck_mtx_unlock(&mtx1); // expected-warning{{This lock has already been destroyed}} 365 } 366 367 void 368 bad22(void) 369 { 370 lck_mtx_destroy(&mtx1, &grp1); // no-warning 371 lck_mtx_destroy(&mtx1, &grp1); // expected-warning{{This lock has already been destroyed}} 372 } 373 374 void 375 bad23(void) 376 { 377 lck_mtx_lock(&mtx1); // no-warning 378 lck_mtx_destroy(&mtx1, &grp1); // expected-warning{{This lock is still locked}} 379 } 380 381 void 382 bad24(void) 383 { 384 pthread_mutex_init(&mtx1, NULL); // no-warning 385 pthread_mutex_init(&mtx1, NULL); // expected-warning{{This lock has already been initialized}} 386 } 387 388 void 389 bad25(void) 390 { 391 pthread_mutex_lock(&mtx1); // no-warning 392 pthread_mutex_init(&mtx1, NULL); // expected-warning{{This lock is still being held}} 393 } 394 395 void 396 bad26(void) 397 { 398 pthread_mutex_unlock(&mtx1); // no-warning 399 pthread_mutex_init(&mtx1, NULL); // expected-warning{{This lock has already been initialized}} 400 } 401