Home | History | Annotate | Download | only in Analysis
      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 
      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 
    180 void
    181 bad1(void)
    182 {
    183 	pthread_mutex_lock(&mtx1);	// no-warning
    184 	pthread_mutex_lock(&mtx1);	// expected-warning{{This lock has already been acquired}}
    185 }
    186 
    187 void
    188 bad2(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 
    196 void
    197 bad3(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 
    205 void
    206 bad4(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 
    214 void
    215 bad5(void)
    216 {
    217 	lck_mtx_lock(&lck1);	// no-warning
    218 	lck_mtx_lock(&lck1);	// expected-warning{{This lock has already been acquired}}
    219 }
    220 
    221 void
    222 bad6(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 
    230 void
    231 bad7(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 
    239 void
    240 bad8(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 
    248 void
    249 bad9(void)
    250 {
    251 	lck_mtx_unlock(&lck1);		// no-warning
    252 	lck_mtx_unlock(&lck1);		// expected-warning{{This lock has already been unlocked}}
    253 }
    254 
    255 void
    256 bad10(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 
    263 static void
    264 bad11_sub(pthread_mutex_t *lock)
    265 {
    266 	lck_mtx_unlock(lock);		// expected-warning{{This lock has already been unlocked}}
    267 }
    268 
    269 void
    270 bad11(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 
    278 void
    279 bad12(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 
    288 void
    289 bad13(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 
    298 void
    299 bad14(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 
    308 void
    309 bad15(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 
    319 void
    320 bad16(void)
    321 {
    322 	pthread_mutex_destroy(&mtx1);	// no-warning
    323 	pthread_mutex_lock(&mtx1);	// expected-warning{{This lock has already been destroyed}}
    324 }
    325 
    326 void
    327 bad17(void)
    328 {
    329 	pthread_mutex_destroy(&mtx1);	// no-warning
    330 	pthread_mutex_unlock(&mtx1);	// expected-warning{{This lock has already been destroyed}}
    331 }
    332 
    333 void
    334 bad18(void)
    335 {
    336 	pthread_mutex_destroy(&mtx1);	// no-warning
    337 	pthread_mutex_destroy(&mtx1);	// expected-warning{{This lock has already been destroyed}}
    338 }
    339 
    340 void
    341 bad19(void)
    342 {
    343 	pthread_mutex_lock(&mtx1);	// no-warning
    344 	pthread_mutex_destroy(&mtx1);	// expected-warning{{This lock is still locked}}
    345 }
    346 
    347 void
    348 bad20(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 
    354 void
    355 bad21(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 
    361 void
    362 bad22(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 
    368 void
    369 bad23(void)
    370 {
    371 	lck_mtx_lock(&mtx1);		// no-warning
    372 	lck_mtx_destroy(&mtx1, &grp1);	// expected-warning{{This lock is still locked}}
    373 }
    374 
    375 void
    376 bad24(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 
    382 void
    383 bad25(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 
    389 void
    390 bad26(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