1 // RUN: %clang_analyze_cc1 -analyzer-checker=alpha.unix.PthreadLock -verify %s 2 3 // Tests performing normal locking patterns and wrong locking orders 4 5 #include "Inputs/system-header-simulator-for-pthread-lock.h" 6 7 pthread_mutex_t mtx1, mtx2; 8 pthread_mutex_t *pmtx; 9 lck_mtx_t lck1, lck2; 10 lck_grp_t grp1; 11 12 #define NULL 0 13 14 void 15 ok1(void) 16 { 17 pthread_mutex_lock(&mtx1); // no-warning 18 } 19 20 void 21 ok2(void) 22 { 23 pthread_mutex_unlock(&mtx1); // no-warning 24 } 25 26 void 27 ok3(void) 28 { 29 pthread_mutex_lock(&mtx1); // no-warning 30 pthread_mutex_unlock(&mtx1); // no-warning 31 pthread_mutex_lock(&mtx1); // no-warning 32 pthread_mutex_unlock(&mtx1); // no-warning 33 } 34 35 void 36 ok4(void) 37 { 38 pthread_mutex_lock(&mtx1); // no-warning 39 pthread_mutex_unlock(&mtx1); // no-warning 40 pthread_mutex_lock(&mtx2); // no-warning 41 pthread_mutex_unlock(&mtx2); // no-warning 42 } 43 44 void 45 ok5(void) 46 { 47 if (pthread_mutex_trylock(&mtx1) == 0) // no-warning 48 pthread_mutex_unlock(&mtx1); // no-warning 49 } 50 51 void 52 ok6(void) 53 { 54 lck_mtx_lock(&lck1); // no-warning 55 } 56 57 void 58 ok7(void) 59 { 60 if (lck_mtx_try_lock(&lck1) != 0) // no-warning 61 lck_mtx_unlock(&lck1); // no-warning 62 } 63 64 void 65 ok8(void) 66 { 67 pthread_mutex_lock(&mtx1); // no-warning 68 pthread_mutex_lock(&mtx2); // no-warning 69 pthread_mutex_unlock(&mtx2); // no-warning 70 pthread_mutex_unlock(&mtx1); // no-warning 71 } 72 73 void 74 ok9(void) 75 { 76 pthread_mutex_unlock(&mtx1); // no-warning 77 if (pthread_mutex_trylock(&mtx1) == 0) // no-warning 78 pthread_mutex_unlock(&mtx1); // no-warning 79 } 80 81 void 82 ok10(void) 83 { 84 if (pthread_mutex_trylock(&mtx1) != 0) // no-warning 85 pthread_mutex_lock(&mtx1); // no-warning 86 pthread_mutex_unlock(&mtx1); // no-warning 87 } 88 89 void 90 ok11(void) 91 { 92 pthread_mutex_destroy(&mtx1); // no-warning 93 } 94 95 void 96 ok12(void) 97 { 98 pthread_mutex_destroy(&mtx1); // no-warning 99 pthread_mutex_destroy(&mtx2); // no-warning 100 } 101 102 void 103 ok13(void) 104 { 105 pthread_mutex_unlock(&mtx1); // no-warning 106 pthread_mutex_destroy(&mtx1); // no-warning 107 } 108 109 void 110 ok14(void) 111 { 112 pthread_mutex_unlock(&mtx1); // no-warning 113 pthread_mutex_destroy(&mtx1); // no-warning 114 pthread_mutex_unlock(&mtx2); // no-warning 115 pthread_mutex_destroy(&mtx2); // no-warning 116 } 117 118 void 119 ok15(void) 120 { 121 pthread_mutex_lock(&mtx1); // no-warning 122 pthread_mutex_unlock(&mtx1); // no-warning 123 pthread_mutex_destroy(&mtx1); // no-warning 124 } 125 126 void 127 ok16(void) 128 { 129 pthread_mutex_init(&mtx1, NULL); // no-warning 130 } 131 132 void 133 ok17(void) 134 { 135 pthread_mutex_init(&mtx1, NULL); // no-warning 136 pthread_mutex_init(&mtx2, NULL); // no-warning 137 } 138 139 void 140 ok18(void) 141 { 142 pthread_mutex_destroy(&mtx1); // no-warning 143 pthread_mutex_init(&mtx1, NULL); // no-warning 144 } 145 146 void 147 ok19(void) 148 { 149 pthread_mutex_destroy(&mtx1); // no-warning 150 pthread_mutex_init(&mtx1, NULL); // no-warning 151 pthread_mutex_destroy(&mtx2); // no-warning 152 pthread_mutex_init(&mtx2, NULL); // no-warning 153 } 154 155 void 156 ok20(void) 157 { 158 pthread_mutex_unlock(&mtx1); // no-warning 159 pthread_mutex_destroy(&mtx1); // no-warning 160 pthread_mutex_init(&mtx1, NULL); // no-warning 161 pthread_mutex_destroy(&mtx1); // no-warning 162 pthread_mutex_init(&mtx1, NULL); // no-warning 163 } 164 165 void 166 ok21(void) { 167 pthread_mutex_lock(pmtx); // no-warning 168 pthread_mutex_unlock(pmtx); // no-warning 169 } 170 171 void 172 ok22(void) { 173 pthread_mutex_lock(pmtx); // no-warning 174 pthread_mutex_unlock(pmtx); // no-warning 175 pthread_mutex_lock(pmtx); // no-warning 176 pthread_mutex_unlock(pmtx); // no-warning 177 } 178 179 void ok23(void) { 180 if (pthread_mutex_destroy(&mtx1) != 0) // no-warning 181 pthread_mutex_destroy(&mtx1); // no-warning 182 } 183 184 void ok24(void) { 185 if (pthread_mutex_destroy(&mtx1) != 0) // no-warning 186 pthread_mutex_lock(&mtx1); // no-warning 187 } 188 189 void ok25(void) { 190 if (pthread_mutex_destroy(&mtx1) != 0) // no-warning 191 pthread_mutex_unlock(&mtx1); // no-warning 192 } 193 194 void ok26(void) { 195 pthread_mutex_unlock(&mtx1); // no-warning 196 if (pthread_mutex_destroy(&mtx1) != 0) // no-warning 197 pthread_mutex_lock(&mtx1); // no-warning 198 } 199 200 void ok27(void) { 201 pthread_mutex_unlock(&mtx1); // no-warning 202 if (pthread_mutex_destroy(&mtx1) != 0) // no-warning 203 pthread_mutex_lock(&mtx1); // no-warning 204 else 205 pthread_mutex_init(&mtx1, NULL); // no-warning 206 } 207 208 void ok28(void) { 209 if (pthread_mutex_destroy(&mtx1) != 0) { // no-warning 210 pthread_mutex_lock(&mtx1); // no-warning 211 pthread_mutex_unlock(&mtx1); // no-warning 212 pthread_mutex_destroy(&mtx1); // no-warning 213 } 214 } 215 216 void 217 bad1(void) 218 { 219 pthread_mutex_lock(&mtx1); // no-warning 220 pthread_mutex_lock(&mtx1); // expected-warning{{This lock has already been acquired}} 221 } 222 223 void 224 bad2(void) 225 { 226 pthread_mutex_lock(&mtx1); // no-warning 227 pthread_mutex_unlock(&mtx1); // no-warning 228 pthread_mutex_lock(&mtx1); // no-warning 229 pthread_mutex_lock(&mtx1); // expected-warning{{This lock has already been acquired}} 230 } 231 232 void 233 bad3(void) 234 { 235 pthread_mutex_lock(&mtx1); // no-warning 236 pthread_mutex_lock(&mtx2); // no-warning 237 pthread_mutex_unlock(&mtx1); // expected-warning{{This was not the most recently acquired lock}} 238 pthread_mutex_unlock(&mtx2); 239 } 240 241 void 242 bad4(void) 243 { 244 if (pthread_mutex_trylock(&mtx1)) // no-warning 245 return; 246 pthread_mutex_lock(&mtx2); // no-warning 247 pthread_mutex_unlock(&mtx1); // expected-warning{{This was not the most recently acquired lock}} 248 } 249 250 void 251 bad5(void) 252 { 253 lck_mtx_lock(&lck1); // no-warning 254 lck_mtx_lock(&lck1); // expected-warning{{This lock has already been acquired}} 255 } 256 257 void 258 bad6(void) 259 { 260 lck_mtx_lock(&lck1); // no-warning 261 lck_mtx_unlock(&lck1); // no-warning 262 lck_mtx_lock(&lck1); // no-warning 263 lck_mtx_lock(&lck1); // expected-warning{{This lock has already been acquired}} 264 } 265 266 void 267 bad7(void) 268 { 269 lck_mtx_lock(&lck1); // no-warning 270 lck_mtx_lock(&lck2); // no-warning 271 lck_mtx_unlock(&lck1); // expected-warning{{This was not the most recently acquired lock}} 272 lck_mtx_unlock(&lck2); 273 } 274 275 void 276 bad8(void) 277 { 278 if (lck_mtx_try_lock(&lck1) == 0) // no-warning 279 return; 280 lck_mtx_lock(&lck2); // no-warning 281 lck_mtx_unlock(&lck1); // expected-warning{{This was not the most recently acquired lock}} 282 } 283 284 void 285 bad9(void) 286 { 287 lck_mtx_unlock(&lck1); // no-warning 288 lck_mtx_unlock(&lck1); // expected-warning{{This lock has already been unlocked}} 289 } 290 291 void 292 bad10(void) 293 { 294 lck_mtx_lock(&lck1); // no-warning 295 lck_mtx_unlock(&lck1); // no-warning 296 lck_mtx_unlock(&lck1); // expected-warning{{This lock has already been unlocked}} 297 } 298 299 static void 300 bad11_sub(pthread_mutex_t *lock) 301 { 302 lck_mtx_unlock(lock); // expected-warning{{This lock has already been unlocked}} 303 } 304 305 void 306 bad11(int i) 307 { 308 lck_mtx_lock(&lck1); // no-warning 309 lck_mtx_unlock(&lck1); // no-warning 310 if (i < 5) 311 bad11_sub(&lck1); 312 } 313 314 void 315 bad12(void) 316 { 317 pthread_mutex_lock(&mtx1); // no-warning 318 pthread_mutex_unlock(&mtx1); // no-warning 319 pthread_mutex_lock(&mtx1); // no-warning 320 pthread_mutex_unlock(&mtx1); // no-warning 321 pthread_mutex_unlock(&mtx1); // expected-warning{{This lock has already been unlocked}} 322 } 323 324 void 325 bad13(void) 326 { 327 pthread_mutex_lock(&mtx1); // no-warning 328 pthread_mutex_unlock(&mtx1); // no-warning 329 pthread_mutex_lock(&mtx2); // no-warning 330 pthread_mutex_unlock(&mtx2); // no-warning 331 pthread_mutex_unlock(&mtx1); // expected-warning{{This lock has already been unlocked}} 332 } 333 334 void 335 bad14(void) 336 { 337 pthread_mutex_lock(&mtx1); // no-warning 338 pthread_mutex_lock(&mtx2); // no-warning 339 pthread_mutex_unlock(&mtx2); // no-warning 340 pthread_mutex_unlock(&mtx1); // no-warning 341 pthread_mutex_unlock(&mtx2); // expected-warning{{This lock has already been unlocked}} 342 } 343 344 void 345 bad15(void) 346 { 347 pthread_mutex_lock(&mtx1); // no-warning 348 pthread_mutex_lock(&mtx2); // no-warning 349 pthread_mutex_unlock(&mtx2); // no-warning 350 pthread_mutex_unlock(&mtx1); // no-warning 351 pthread_mutex_lock(&mtx1); // no-warning 352 pthread_mutex_unlock(&mtx2); // expected-warning{{This lock has already been unlocked}} 353 } 354 355 void 356 bad16(void) 357 { 358 pthread_mutex_destroy(&mtx1); // no-warning 359 pthread_mutex_lock(&mtx1); // expected-warning{{This lock has already been destroyed}} 360 } 361 362 void 363 bad17(void) 364 { 365 pthread_mutex_destroy(&mtx1); // no-warning 366 pthread_mutex_unlock(&mtx1); // expected-warning{{This lock has already been destroyed}} 367 } 368 369 void 370 bad18(void) 371 { 372 pthread_mutex_destroy(&mtx1); // no-warning 373 pthread_mutex_destroy(&mtx1); // expected-warning{{This lock has already been destroyed}} 374 } 375 376 void 377 bad19(void) 378 { 379 pthread_mutex_lock(&mtx1); // no-warning 380 pthread_mutex_destroy(&mtx1); // expected-warning{{This lock is still locked}} 381 } 382 383 void 384 bad20(void) 385 { 386 lck_mtx_destroy(&mtx1, &grp1); // no-warning 387 lck_mtx_lock(&mtx1); // expected-warning{{This lock has already been destroyed}} 388 } 389 390 void 391 bad21(void) 392 { 393 lck_mtx_destroy(&mtx1, &grp1); // no-warning 394 lck_mtx_unlock(&mtx1); // expected-warning{{This lock has already been destroyed}} 395 } 396 397 void 398 bad22(void) 399 { 400 lck_mtx_destroy(&mtx1, &grp1); // no-warning 401 lck_mtx_destroy(&mtx1, &grp1); // expected-warning{{This lock has already been destroyed}} 402 } 403 404 void 405 bad23(void) 406 { 407 lck_mtx_lock(&mtx1); // no-warning 408 lck_mtx_destroy(&mtx1, &grp1); // expected-warning{{This lock is still locked}} 409 } 410 411 void 412 bad24(void) 413 { 414 pthread_mutex_init(&mtx1, NULL); // no-warning 415 pthread_mutex_init(&mtx1, NULL); // expected-warning{{This lock has already been initialized}} 416 } 417 418 void 419 bad25(void) 420 { 421 pthread_mutex_lock(&mtx1); // no-warning 422 pthread_mutex_init(&mtx1, NULL); // expected-warning{{This lock is still being held}} 423 } 424 425 void 426 bad26(void) 427 { 428 pthread_mutex_unlock(&mtx1); // no-warning 429 pthread_mutex_init(&mtx1, NULL); // expected-warning{{This lock has already been initialized}} 430 } 431 432 void bad27(void) { 433 pthread_mutex_unlock(&mtx1); // no-warning 434 int ret = pthread_mutex_destroy(&mtx1); // no-warning 435 if (ret != 0) // no-warning 436 pthread_mutex_lock(&mtx1); // no-warning 437 else 438 pthread_mutex_unlock(&mtx1); // expected-warning{{This lock has already been destroyed}} 439 } 440 441 void bad28(void) { 442 pthread_mutex_unlock(&mtx1); // no-warning 443 int ret = pthread_mutex_destroy(&mtx1); // no-warning 444 if (ret != 0) // no-warning 445 pthread_mutex_lock(&mtx1); // no-warning 446 else 447 pthread_mutex_lock(&mtx1); // expected-warning{{This lock has already been destroyed}} 448 } 449 450 void bad29(void) { 451 pthread_mutex_lock(&mtx1); // no-warning 452 pthread_mutex_unlock(&mtx1); // no-warning 453 if (pthread_mutex_destroy(&mtx1) != 0) // no-warning 454 pthread_mutex_init(&mtx1, NULL); // expected-warning{{This lock has already been initialized}} 455 else 456 pthread_mutex_init(&mtx1, NULL); // no-warning 457 } 458 459 void bad30(void) { 460 pthread_mutex_lock(&mtx1); // no-warning 461 pthread_mutex_unlock(&mtx1); // no-warning 462 if (pthread_mutex_destroy(&mtx1) != 0) // no-warning 463 pthread_mutex_init(&mtx1, NULL); // expected-warning{{This lock has already been initialized}} 464 else 465 pthread_mutex_destroy(&mtx1); // expected-warning{{This lock has already been destroyed}} 466 } 467 468 void bad31(void) { 469 int ret = pthread_mutex_destroy(&mtx1); // no-warning 470 pthread_mutex_lock(&mtx1); // expected-warning{{This lock has already been destroyed}} 471 if (ret != 0) 472 pthread_mutex_lock(&mtx1); 473 } 474