522 lines · c
1// RUN: %clang_analyze_cc1 -analyzer-checker=alpha.unix.PthreadLock -verify %s2 3// Tests performing normal locking patterns and wrong locking orders4 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;11lck_rw_t rw;12 13#define NULL 014 15void16ok1(void)17{18 pthread_mutex_lock(&mtx1); // no-warning19}20 21void22ok2(void)23{24 pthread_mutex_unlock(&mtx1); // no-warning25}26 27void28ok3(void)29{30 pthread_mutex_lock(&mtx1); // no-warning31 pthread_mutex_unlock(&mtx1); // no-warning32 pthread_mutex_lock(&mtx1); // no-warning33 pthread_mutex_unlock(&mtx1); // no-warning34}35 36void37ok4(void)38{39 pthread_mutex_lock(&mtx1); // no-warning40 pthread_mutex_unlock(&mtx1); // no-warning41 pthread_mutex_lock(&mtx2); // no-warning42 pthread_mutex_unlock(&mtx2); // no-warning43}44 45void46ok5(void)47{48 if (pthread_mutex_trylock(&mtx1) == 0) // no-warning49 pthread_mutex_unlock(&mtx1); // no-warning50}51 52void53ok6(void)54{55 lck_mtx_lock(&lck1); // no-warning56}57 58void59ok7(void)60{61 if (lck_mtx_try_lock(&lck1) != 0) // no-warning62 lck_mtx_unlock(&lck1); // no-warning63}64 65void66ok8(void)67{68 pthread_mutex_lock(&mtx1); // no-warning69 pthread_mutex_lock(&mtx2); // no-warning70 pthread_mutex_unlock(&mtx2); // no-warning71 pthread_mutex_unlock(&mtx1); // no-warning72}73 74void75ok9(void)76{77 pthread_mutex_unlock(&mtx1); // no-warning78 if (pthread_mutex_trylock(&mtx1) == 0) // no-warning79 pthread_mutex_unlock(&mtx1); // no-warning80}81 82void83ok10(void)84{85 if (pthread_mutex_trylock(&mtx1) != 0) // no-warning86 pthread_mutex_lock(&mtx1); // no-warning87 pthread_mutex_unlock(&mtx1); // no-warning88}89 90void91ok11(void)92{93 pthread_mutex_destroy(&mtx1); // no-warning94}95 96void97ok12(void)98{99 pthread_mutex_destroy(&mtx1); // no-warning100 pthread_mutex_destroy(&mtx2); // no-warning101}102 103void104ok13(void)105{106 pthread_mutex_unlock(&mtx1); // no-warning107 pthread_mutex_destroy(&mtx1); // no-warning108}109 110void111ok14(void)112{113 pthread_mutex_unlock(&mtx1); // no-warning114 pthread_mutex_destroy(&mtx1); // no-warning115 pthread_mutex_unlock(&mtx2); // no-warning116 pthread_mutex_destroy(&mtx2); // no-warning117}118 119void120ok15(void)121{122 pthread_mutex_lock(&mtx1); // no-warning123 pthread_mutex_unlock(&mtx1); // no-warning124 pthread_mutex_destroy(&mtx1); // no-warning125}126 127void128ok16(void)129{130 pthread_mutex_init(&mtx1, NULL); // no-warning131}132 133void134ok17(void)135{136 pthread_mutex_init(&mtx1, NULL); // no-warning137 pthread_mutex_init(&mtx2, NULL); // no-warning138}139 140void141ok18(void)142{143 pthread_mutex_destroy(&mtx1); // no-warning144 pthread_mutex_init(&mtx1, NULL); // no-warning145}146 147void148ok19(void)149{150 pthread_mutex_destroy(&mtx1); // no-warning151 pthread_mutex_init(&mtx1, NULL); // no-warning152 pthread_mutex_destroy(&mtx2); // no-warning153 pthread_mutex_init(&mtx2, NULL); // no-warning154}155 156void157ok20(void)158{159 pthread_mutex_unlock(&mtx1); // no-warning160 pthread_mutex_destroy(&mtx1); // no-warning161 pthread_mutex_init(&mtx1, NULL); // no-warning162 pthread_mutex_destroy(&mtx1); // no-warning163 pthread_mutex_init(&mtx1, NULL); // no-warning164}165 166void167ok21(void) {168 pthread_mutex_lock(pmtx); // no-warning169 pthread_mutex_unlock(pmtx); // no-warning170}171 172void173ok22(void) {174 pthread_mutex_lock(pmtx); // no-warning175 pthread_mutex_unlock(pmtx); // no-warning176 pthread_mutex_lock(pmtx); // no-warning177 pthread_mutex_unlock(pmtx); // no-warning178}179 180void ok23(void) {181 if (pthread_mutex_destroy(&mtx1) != 0) // no-warning182 pthread_mutex_destroy(&mtx1); // no-warning183}184 185void ok24(void) {186 if (pthread_mutex_destroy(&mtx1) != 0) // no-warning187 pthread_mutex_lock(&mtx1); // no-warning188}189 190void ok25(void) {191 if (pthread_mutex_destroy(&mtx1) != 0) // no-warning192 pthread_mutex_unlock(&mtx1); // no-warning193}194 195void ok26(void) {196 pthread_mutex_unlock(&mtx1); // no-warning197 if (pthread_mutex_destroy(&mtx1) != 0) // no-warning198 pthread_mutex_lock(&mtx1); // no-warning199}200 201void ok27(void) {202 pthread_mutex_unlock(&mtx1); // no-warning203 if (pthread_mutex_destroy(&mtx1) != 0) // no-warning204 pthread_mutex_lock(&mtx1); // no-warning205 else206 pthread_mutex_init(&mtx1, NULL); // no-warning207}208 209void ok28(void) {210 if (pthread_mutex_destroy(&mtx1) != 0) { // no-warning211 pthread_mutex_lock(&mtx1); // no-warning212 pthread_mutex_unlock(&mtx1); // no-warning213 pthread_mutex_destroy(&mtx1); // no-warning214 }215}216 217void ok29(void) {218 lck_rw_lock_shared(&rw);219 lck_rw_unlock_shared(&rw);220 lck_rw_lock_exclusive(&rw); // no-warning221 lck_rw_unlock_exclusive(&rw); // no-warning222}223 224void escape_mutex(pthread_mutex_t *m);225void ok30(void) {226 pthread_mutex_t local_mtx;227 pthread_mutex_init(&local_mtx, NULL);228 pthread_mutex_lock(&local_mtx);229 escape_mutex(&local_mtx);230 pthread_mutex_lock(&local_mtx); // no-warning231 pthread_mutex_unlock(&local_mtx);232 pthread_mutex_destroy(&local_mtx);233}234 235void ok31(void) {236 pthread_mutex_t local_mtx;237 pthread_mutex_init(&local_mtx, NULL);238 pthread_mutex_lock(&local_mtx);239 fake_system_function_that_takes_a_mutex(&local_mtx);240 pthread_mutex_lock(&local_mtx); // no-warning241 pthread_mutex_unlock(&local_mtx);242 pthread_mutex_destroy(&local_mtx);243}244 245void246bad1(void)247{248 pthread_mutex_lock(&mtx1); // no-warning249 pthread_mutex_lock(&mtx1); // expected-warning{{This lock has already been acquired}}250}251 252void253bad2(void)254{255 pthread_mutex_lock(&mtx1); // no-warning256 pthread_mutex_unlock(&mtx1); // no-warning257 pthread_mutex_lock(&mtx1); // no-warning258 pthread_mutex_lock(&mtx1); // expected-warning{{This lock has already been acquired}}259}260 261void262bad3(void)263{264 pthread_mutex_lock(&mtx1); // no-warning265 pthread_mutex_lock(&mtx2); // no-warning266 pthread_mutex_unlock(&mtx1); // expected-warning{{This was not the most recently acquired lock}}267 pthread_mutex_unlock(&mtx2);268}269 270void271bad4(void)272{273 if (pthread_mutex_trylock(&mtx1)) // no-warning274 return;275 pthread_mutex_lock(&mtx2); // no-warning276 pthread_mutex_unlock(&mtx1); // expected-warning{{This was not the most recently acquired lock}}277}278 279void280bad5(void)281{282 lck_mtx_lock(&lck1); // no-warning283 lck_mtx_lock(&lck1); // expected-warning{{This lock has already been acquired}}284}285 286void287bad6(void)288{289 lck_mtx_lock(&lck1); // no-warning290 lck_mtx_unlock(&lck1); // no-warning291 lck_mtx_lock(&lck1); // no-warning292 lck_mtx_lock(&lck1); // expected-warning{{This lock has already been acquired}}293}294 295void296bad7(void)297{298 lck_mtx_lock(&lck1); // no-warning299 lck_mtx_lock(&lck2); // no-warning300 lck_mtx_unlock(&lck1); // expected-warning{{This was not the most recently acquired lock}}301 lck_mtx_unlock(&lck2);302}303 304void305bad8(void)306{307 if (lck_mtx_try_lock(&lck1) == 0) // no-warning308 return;309 lck_mtx_lock(&lck2); // no-warning310 lck_mtx_unlock(&lck1); // expected-warning{{This was not the most recently acquired lock}}311}312 313void314bad9(void)315{316 lck_mtx_unlock(&lck1); // no-warning317 lck_mtx_unlock(&lck1); // expected-warning{{This lock has already been unlocked}}318}319 320void321bad10(void)322{323 lck_mtx_lock(&lck1); // no-warning324 lck_mtx_unlock(&lck1); // no-warning325 lck_mtx_unlock(&lck1); // expected-warning{{This lock has already been unlocked}}326}327 328static void329bad11_sub(pthread_mutex_t *lock)330{331 lck_mtx_unlock(lock); // expected-warning{{This lock has already been unlocked}}332}333 334void335bad11(int i)336{337 lck_mtx_lock(&lck1); // no-warning338 lck_mtx_unlock(&lck1); // no-warning339 if (i < 5)340 bad11_sub(&lck1);341}342 343void344bad12(void)345{346 pthread_mutex_lock(&mtx1); // no-warning347 pthread_mutex_unlock(&mtx1); // no-warning348 pthread_mutex_lock(&mtx1); // no-warning349 pthread_mutex_unlock(&mtx1); // no-warning350 pthread_mutex_unlock(&mtx1); // expected-warning{{This lock has already been unlocked}}351}352 353void354bad13(void)355{356 pthread_mutex_lock(&mtx1); // no-warning357 pthread_mutex_unlock(&mtx1); // no-warning358 pthread_mutex_lock(&mtx2); // no-warning359 pthread_mutex_unlock(&mtx2); // no-warning360 pthread_mutex_unlock(&mtx1); // expected-warning{{This lock has already been unlocked}}361}362 363void364bad14(void)365{366 pthread_mutex_lock(&mtx1); // no-warning367 pthread_mutex_lock(&mtx2); // no-warning368 pthread_mutex_unlock(&mtx2); // no-warning369 pthread_mutex_unlock(&mtx1); // no-warning370 pthread_mutex_unlock(&mtx2); // expected-warning{{This lock has already been unlocked}}371}372 373void374bad15(void)375{376 pthread_mutex_lock(&mtx1); // no-warning377 pthread_mutex_lock(&mtx2); // no-warning378 pthread_mutex_unlock(&mtx2); // no-warning379 pthread_mutex_unlock(&mtx1); // no-warning380 pthread_mutex_lock(&mtx1); // no-warning381 pthread_mutex_unlock(&mtx2); // expected-warning{{This lock has already been unlocked}}382}383 384void385bad16(void)386{387 pthread_mutex_destroy(&mtx1); // no-warning388 pthread_mutex_lock(&mtx1); // expected-warning{{This lock has already been destroyed}}389}390 391void392bad17(void)393{394 pthread_mutex_destroy(&mtx1); // no-warning395 pthread_mutex_unlock(&mtx1); // expected-warning{{This lock has already been destroyed}}396}397 398void399bad18(void)400{401 pthread_mutex_destroy(&mtx1); // no-warning402 pthread_mutex_destroy(&mtx1); // expected-warning{{This lock has already been destroyed}}403}404 405void406bad19(void)407{408 pthread_mutex_lock(&mtx1); // no-warning409 pthread_mutex_destroy(&mtx1); // expected-warning{{This lock is still locked}}410}411 412void413bad20(void)414{415 lck_mtx_destroy(&mtx1, &grp1); // no-warning416 lck_mtx_lock(&mtx1); // expected-warning{{This lock has already been destroyed}}417}418 419void420bad21(void)421{422 lck_mtx_destroy(&mtx1, &grp1); // no-warning423 lck_mtx_unlock(&mtx1); // expected-warning{{This lock has already been destroyed}}424}425 426void427bad22(void)428{429 lck_mtx_destroy(&mtx1, &grp1); // no-warning430 lck_mtx_destroy(&mtx1, &grp1); // expected-warning{{This lock has already been destroyed}}431}432 433void434bad23(void)435{436 lck_mtx_lock(&mtx1); // no-warning437 lck_mtx_destroy(&mtx1, &grp1); // expected-warning{{This lock is still locked}}438}439 440void441bad24(void)442{443 pthread_mutex_init(&mtx1, NULL); // no-warning444 pthread_mutex_init(&mtx1, NULL); // expected-warning{{This lock has already been initialized}}445}446 447void448bad25(void)449{450 pthread_mutex_lock(&mtx1); // no-warning451 pthread_mutex_init(&mtx1, NULL); // expected-warning{{This lock is still being held}}452}453 454void455bad26(void)456{457 pthread_mutex_unlock(&mtx1); // no-warning458 pthread_mutex_init(&mtx1, NULL); // expected-warning{{This lock has already been initialized}}459}460 461void bad27(void) {462 pthread_mutex_unlock(&mtx1); // no-warning463 int ret = pthread_mutex_destroy(&mtx1); // no-warning464 if (ret != 0) // no-warning465 pthread_mutex_lock(&mtx1); // no-warning466 else467 pthread_mutex_unlock(&mtx1); // expected-warning{{This lock has already been destroyed}}468}469 470void bad28(void) {471 pthread_mutex_unlock(&mtx1); // no-warning472 int ret = pthread_mutex_destroy(&mtx1); // no-warning473 if (ret != 0) // no-warning474 pthread_mutex_lock(&mtx1); // no-warning475 else476 pthread_mutex_lock(&mtx1); // expected-warning{{This lock has already been destroyed}}477}478 479void bad29(void) {480 pthread_mutex_lock(&mtx1); // no-warning481 pthread_mutex_unlock(&mtx1); // no-warning482 if (pthread_mutex_destroy(&mtx1) != 0) // no-warning483 pthread_mutex_init(&mtx1, NULL); // expected-warning{{This lock has already been initialized}}484 else485 pthread_mutex_init(&mtx1, NULL); // no-warning486}487 488void bad30(void) {489 pthread_mutex_lock(&mtx1); // no-warning490 pthread_mutex_unlock(&mtx1); // no-warning491 if (pthread_mutex_destroy(&mtx1) != 0) // no-warning492 pthread_mutex_init(&mtx1, NULL); // expected-warning{{This lock has already been initialized}}493 else494 pthread_mutex_destroy(&mtx1); // expected-warning{{This lock has already been destroyed}}495}496 497void bad31(void) {498 int ret = pthread_mutex_destroy(&mtx1); // no-warning499 pthread_mutex_lock(&mtx1); // expected-warning{{This lock has already been destroyed}}500 if (ret != 0)501 pthread_mutex_lock(&mtx1);502}503 504void bad32(void) {505 lck_rw_lock_shared(&rw);506 lck_rw_unlock_exclusive(&rw); // FIXME: warn - should be shared?507 lck_rw_lock_exclusive(&rw);508 lck_rw_unlock_shared(&rw); // FIXME: warn - should be exclusive?509}510 511void bad33(void) {512 pthread_mutex_lock(pmtx);513 fake_system_function();514 pthread_mutex_lock(pmtx); // expected-warning{{This lock has already been acquired}}515}516 517void nocrash1(pthread_mutex_t *mutex) {518 int ret = pthread_mutex_destroy(mutex);519 if (ret == 0) // no crash520 ;521}522