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#include "Inputs/system-header-simulator-for-pthread-lock.h" 6 7pthread_mutex_t mtx1, mtx2; 8pthread_mutex_t *pmtx; 9lck_mtx_t lck1, lck2; 10lck_grp_t grp1; 11 12#define NULL 0 13 14void 15ok1(void) 16{ 17 pthread_mutex_lock(&mtx1); // no-warning 18} 19 20void 21ok2(void) 22{ 23 pthread_mutex_unlock(&mtx1); // no-warning 24} 25 26void 27ok3(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 35void 36ok4(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 44void 45ok5(void) 46{ 47 if (pthread_mutex_trylock(&mtx1) == 0) // no-warning 48 pthread_mutex_unlock(&mtx1); // no-warning 49} 50 51void 52ok6(void) 53{ 54 lck_mtx_lock(&lck1); // no-warning 55} 56 57void 58ok7(void) 59{ 60 if (lck_mtx_try_lock(&lck1) != 0) // no-warning 61 lck_mtx_unlock(&lck1); // no-warning 62} 63 64void 65ok8(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 73void 74ok9(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 81void 82ok10(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 89void 90ok11(void) 91{ 92 pthread_mutex_destroy(&mtx1); // no-warning 93} 94 95void 96ok12(void) 97{ 98 pthread_mutex_destroy(&mtx1); // no-warning 99 pthread_mutex_destroy(&mtx2); // no-warning 100} 101 102void 103ok13(void) 104{ 105 pthread_mutex_unlock(&mtx1); // no-warning 106 pthread_mutex_destroy(&mtx1); // no-warning 107} 108 109void 110ok14(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 118void 119ok15(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 126void 127ok16(void) 128{ 129 pthread_mutex_init(&mtx1, NULL); // no-warning 130} 131 132void 133ok17(void) 134{ 135 pthread_mutex_init(&mtx1, NULL); // no-warning 136 pthread_mutex_init(&mtx2, NULL); // no-warning 137} 138 139void 140ok18(void) 141{ 142 pthread_mutex_destroy(&mtx1); // no-warning 143 pthread_mutex_init(&mtx1, NULL); // no-warning 144} 145 146void 147ok19(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 155void 156ok20(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 165void 166ok21(void) { 167 pthread_mutex_lock(pmtx); // no-warning 168 pthread_mutex_unlock(pmtx); // no-warning 169} 170 171void 172ok22(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 180void 181bad1(void) 182{ 183 pthread_mutex_lock(&mtx1); // no-warning 184 pthread_mutex_lock(&mtx1); // expected-warning{{This lock has already been acquired}} 185} 186 187void 188bad2(void) 189{ 190 pthread_mutex_lock(&mtx1); // no-warning 191 pthread_mutex_unlock(&mtx1); // no-warning 192 pthread_mutex_lock(&mtx1); // no-warning 193 pthread_mutex_lock(&mtx1); // expected-warning{{This lock has already been acquired}} 194} 195 196void 197bad3(void) 198{ 199 pthread_mutex_lock(&mtx1); // no-warning 200 pthread_mutex_lock(&mtx2); // no-warning 201 pthread_mutex_unlock(&mtx1); // expected-warning{{This was not the most recently acquired lock}} 202 pthread_mutex_unlock(&mtx2); 203} 204 205void 206bad4(void) 207{ 208 if (pthread_mutex_trylock(&mtx1)) // no-warning 209 return; 210 pthread_mutex_lock(&mtx2); // no-warning 211 pthread_mutex_unlock(&mtx1); // expected-warning{{This was not the most recently acquired lock}} 212} 213 214void 215bad5(void) 216{ 217 lck_mtx_lock(&lck1); // no-warning 218 lck_mtx_lock(&lck1); // expected-warning{{This lock has already been acquired}} 219} 220 221void 222bad6(void) 223{ 224 lck_mtx_lock(&lck1); // no-warning 225 lck_mtx_unlock(&lck1); // no-warning 226 lck_mtx_lock(&lck1); // no-warning 227 lck_mtx_lock(&lck1); // expected-warning{{This lock has already been acquired}} 228} 229 230void 231bad7(void) 232{ 233 lck_mtx_lock(&lck1); // no-warning 234 lck_mtx_lock(&lck2); // no-warning 235 lck_mtx_unlock(&lck1); // expected-warning{{This was not the most recently acquired lock}} 236 lck_mtx_unlock(&lck2); 237} 238 239void 240bad8(void) 241{ 242 if (lck_mtx_try_lock(&lck1) == 0) // no-warning 243 return; 244 lck_mtx_lock(&lck2); // no-warning 245 lck_mtx_unlock(&lck1); // expected-warning{{This was not the most recently acquired lock}} 246} 247 248void 249bad9(void) 250{ 251 lck_mtx_unlock(&lck1); // no-warning 252 lck_mtx_unlock(&lck1); // expected-warning{{This lock has already been unlocked}} 253} 254 255void 256bad10(void) 257{ 258 lck_mtx_lock(&lck1); // no-warning 259 lck_mtx_unlock(&lck1); // no-warning 260 lck_mtx_unlock(&lck1); // expected-warning{{This lock has already been unlocked}} 261} 262 263static void 264bad11_sub(pthread_mutex_t *lock) 265{ 266 lck_mtx_unlock(lock); // expected-warning{{This lock has already been unlocked}} 267} 268 269void 270bad11(int i) 271{ 272 lck_mtx_lock(&lck1); // no-warning 273 lck_mtx_unlock(&lck1); // no-warning 274 if (i < 5) 275 bad11_sub(&lck1); 276} 277 278void 279bad12(void) 280{ 281 pthread_mutex_lock(&mtx1); // no-warning 282 pthread_mutex_unlock(&mtx1); // no-warning 283 pthread_mutex_lock(&mtx1); // no-warning 284 pthread_mutex_unlock(&mtx1); // no-warning 285 pthread_mutex_unlock(&mtx1); // expected-warning{{This lock has already been unlocked}} 286} 287 288void 289bad13(void) 290{ 291 pthread_mutex_lock(&mtx1); // no-warning 292 pthread_mutex_unlock(&mtx1); // no-warning 293 pthread_mutex_lock(&mtx2); // no-warning 294 pthread_mutex_unlock(&mtx2); // no-warning 295 pthread_mutex_unlock(&mtx1); // expected-warning{{This lock has already been unlocked}} 296} 297 298void 299bad14(void) 300{ 301 pthread_mutex_lock(&mtx1); // no-warning 302 pthread_mutex_lock(&mtx2); // no-warning 303 pthread_mutex_unlock(&mtx2); // no-warning 304 pthread_mutex_unlock(&mtx1); // no-warning 305 pthread_mutex_unlock(&mtx2); // expected-warning{{This lock has already been unlocked}} 306} 307 308void 309bad15(void) 310{ 311 pthread_mutex_lock(&mtx1); // no-warning 312 pthread_mutex_lock(&mtx2); // no-warning 313 pthread_mutex_unlock(&mtx2); // no-warning 314 pthread_mutex_unlock(&mtx1); // no-warning 315 pthread_mutex_lock(&mtx1); // no-warning 316 pthread_mutex_unlock(&mtx2); // expected-warning{{This lock has already been unlocked}} 317} 318 319void 320bad16(void) 321{ 322 pthread_mutex_destroy(&mtx1); // no-warning 323 pthread_mutex_lock(&mtx1); // expected-warning{{This lock has already been destroyed}} 324} 325 326void 327bad17(void) 328{ 329 pthread_mutex_destroy(&mtx1); // no-warning 330 pthread_mutex_unlock(&mtx1); // expected-warning{{This lock has already been destroyed}} 331} 332 333void 334bad18(void) 335{ 336 pthread_mutex_destroy(&mtx1); // no-warning 337 pthread_mutex_destroy(&mtx1); // expected-warning{{This lock has already been destroyed}} 338} 339 340void 341bad19(void) 342{ 343 pthread_mutex_lock(&mtx1); // no-warning 344 pthread_mutex_destroy(&mtx1); // expected-warning{{This lock is still locked}} 345} 346 347void 348bad20(void) 349{ 350 lck_mtx_destroy(&mtx1, &grp1); // no-warning 351 lck_mtx_lock(&mtx1); // expected-warning{{This lock has already been destroyed}} 352} 353 354void 355bad21(void) 356{ 357 lck_mtx_destroy(&mtx1, &grp1); // no-warning 358 lck_mtx_unlock(&mtx1); // expected-warning{{This lock has already been destroyed}} 359} 360 361void 362bad22(void) 363{ 364 lck_mtx_destroy(&mtx1, &grp1); // no-warning 365 lck_mtx_destroy(&mtx1, &grp1); // expected-warning{{This lock has already been destroyed}} 366} 367 368void 369bad23(void) 370{ 371 lck_mtx_lock(&mtx1); // no-warning 372 lck_mtx_destroy(&mtx1, &grp1); // expected-warning{{This lock is still locked}} 373} 374 375void 376bad24(void) 377{ 378 pthread_mutex_init(&mtx1, NULL); // no-warning 379 pthread_mutex_init(&mtx1, NULL); // expected-warning{{This lock has already been initialized}} 380} 381 382void 383bad25(void) 384{ 385 pthread_mutex_lock(&mtx1); // no-warning 386 pthread_mutex_init(&mtx1, NULL); // expected-warning{{This lock is still being held}} 387} 388 389void 390bad26(void) 391{ 392 pthread_mutex_unlock(&mtx1); // no-warning 393 pthread_mutex_init(&mtx1, NULL); // expected-warning{{This lock has already been initialized}} 394} 395