2012-08-25 04:39:55 +08:00
|
|
|
// RUN: %clang_cc1 -analyze -analyzer-checker=alpha.unix.PthreadLock -verify %s
|
2011-07-20 04:21:41 +08:00
|
|
|
|
|
|
|
// Tests performing normal locking patterns and wrong locking orders
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
void *foo;
|
|
|
|
} pthread_mutex_t;
|
|
|
|
|
2014-04-01 11:40:53 +08:00
|
|
|
typedef struct {
|
|
|
|
void *foo;
|
|
|
|
} pthread_mutexattr_t;
|
|
|
|
|
2014-04-01 11:40:47 +08:00
|
|
|
typedef struct {
|
|
|
|
void *foo;
|
|
|
|
} lck_grp_t;
|
|
|
|
|
2011-07-20 04:21:41 +08:00
|
|
|
typedef pthread_mutex_t lck_mtx_t;
|
|
|
|
|
|
|
|
extern int pthread_mutex_lock(pthread_mutex_t *);
|
|
|
|
extern int pthread_mutex_unlock(pthread_mutex_t *);
|
|
|
|
extern int pthread_mutex_trylock(pthread_mutex_t *);
|
2014-04-01 11:40:47 +08:00
|
|
|
extern int pthread_mutex_destroy(pthread_mutex_t *);
|
2014-04-01 11:40:53 +08:00
|
|
|
extern int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *mutexattr);
|
2011-07-20 04:21:41 +08:00
|
|
|
extern int lck_mtx_lock(lck_mtx_t *);
|
|
|
|
extern int lck_mtx_unlock(lck_mtx_t *);
|
|
|
|
extern int lck_mtx_try_lock(lck_mtx_t *);
|
2014-04-01 11:40:47 +08:00
|
|
|
extern void lck_mtx_destroy(lck_mtx_t *lck, lck_grp_t *grp);
|
2011-07-20 04:21:41 +08:00
|
|
|
|
|
|
|
pthread_mutex_t mtx1, mtx2;
|
|
|
|
lck_mtx_t lck1, lck2;
|
2014-04-01 11:40:47 +08:00
|
|
|
lck_grp_t grp1;
|
2011-07-20 04:21:41 +08:00
|
|
|
|
2014-04-01 11:40:53 +08:00
|
|
|
#define NULL 0
|
|
|
|
|
2011-07-20 04:21:41 +08:00
|
|
|
void
|
|
|
|
ok1(void)
|
|
|
|
{
|
|
|
|
pthread_mutex_lock(&mtx1); // no-warning
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ok2(void)
|
|
|
|
{
|
|
|
|
pthread_mutex_unlock(&mtx1); // no-warning
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ok3(void)
|
|
|
|
{
|
|
|
|
pthread_mutex_lock(&mtx1); // no-warning
|
|
|
|
pthread_mutex_unlock(&mtx1); // no-warning
|
|
|
|
pthread_mutex_lock(&mtx1); // no-warning
|
|
|
|
pthread_mutex_unlock(&mtx1); // no-warning
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ok4(void)
|
|
|
|
{
|
|
|
|
pthread_mutex_lock(&mtx1); // no-warning
|
|
|
|
pthread_mutex_unlock(&mtx1); // no-warning
|
|
|
|
pthread_mutex_lock(&mtx2); // no-warning
|
|
|
|
pthread_mutex_unlock(&mtx2); // no-warning
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ok5(void)
|
|
|
|
{
|
|
|
|
if (pthread_mutex_trylock(&mtx1) == 0) // no-warning
|
|
|
|
pthread_mutex_unlock(&mtx1); // no-warning
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ok6(void)
|
|
|
|
{
|
|
|
|
lck_mtx_lock(&lck1); // no-warning
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ok7(void)
|
|
|
|
{
|
|
|
|
if (lck_mtx_try_lock(&lck1) != 0) // no-warning
|
|
|
|
lck_mtx_unlock(&lck1); // no-warning
|
|
|
|
}
|
|
|
|
|
2014-04-01 11:40:38 +08:00
|
|
|
void
|
|
|
|
ok8(void)
|
|
|
|
{
|
|
|
|
pthread_mutex_lock(&mtx1); // no-warning
|
|
|
|
pthread_mutex_lock(&mtx2); // no-warning
|
|
|
|
pthread_mutex_unlock(&mtx2); // no-warning
|
|
|
|
pthread_mutex_unlock(&mtx1); // no-warning
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ok9(void)
|
|
|
|
{
|
|
|
|
pthread_mutex_unlock(&mtx1); // no-warning
|
|
|
|
if (pthread_mutex_trylock(&mtx1) == 0) // no-warning
|
|
|
|
pthread_mutex_unlock(&mtx1); // no-warning
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ok10(void)
|
|
|
|
{
|
|
|
|
if (pthread_mutex_trylock(&mtx1) != 0) // no-warning
|
|
|
|
pthread_mutex_lock(&mtx1); // no-warning
|
|
|
|
pthread_mutex_unlock(&mtx1); // no-warning
|
|
|
|
}
|
|
|
|
|
2014-04-01 11:40:47 +08:00
|
|
|
void
|
|
|
|
ok11(void)
|
|
|
|
{
|
|
|
|
pthread_mutex_destroy(&mtx1); // no-warning
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ok12(void)
|
|
|
|
{
|
|
|
|
pthread_mutex_destroy(&mtx1); // no-warning
|
|
|
|
pthread_mutex_destroy(&mtx2); // no-warning
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ok13(void)
|
|
|
|
{
|
|
|
|
pthread_mutex_unlock(&mtx1); // no-warning
|
|
|
|
pthread_mutex_destroy(&mtx1); // no-warning
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ok14(void)
|
|
|
|
{
|
|
|
|
pthread_mutex_unlock(&mtx1); // no-warning
|
|
|
|
pthread_mutex_destroy(&mtx1); // no-warning
|
|
|
|
pthread_mutex_unlock(&mtx2); // no-warning
|
|
|
|
pthread_mutex_destroy(&mtx2); // no-warning
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ok15(void)
|
|
|
|
{
|
|
|
|
pthread_mutex_lock(&mtx1); // no-warning
|
|
|
|
pthread_mutex_unlock(&mtx1); // no-warning
|
|
|
|
pthread_mutex_destroy(&mtx1); // no-warning
|
|
|
|
}
|
|
|
|
|
2014-04-01 11:40:53 +08:00
|
|
|
void
|
|
|
|
ok16(void)
|
|
|
|
{
|
|
|
|
pthread_mutex_init(&mtx1, NULL); // no-warning
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ok17(void)
|
|
|
|
{
|
|
|
|
pthread_mutex_init(&mtx1, NULL); // no-warning
|
|
|
|
pthread_mutex_init(&mtx2, NULL); // no-warning
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ok18(void)
|
|
|
|
{
|
|
|
|
pthread_mutex_destroy(&mtx1); // no-warning
|
|
|
|
pthread_mutex_init(&mtx1, NULL); // no-warning
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ok19(void)
|
|
|
|
{
|
|
|
|
pthread_mutex_destroy(&mtx1); // no-warning
|
|
|
|
pthread_mutex_init(&mtx1, NULL); // no-warning
|
|
|
|
pthread_mutex_destroy(&mtx2); // no-warning
|
|
|
|
pthread_mutex_init(&mtx2, NULL); // no-warning
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ok20(void)
|
|
|
|
{
|
|
|
|
pthread_mutex_unlock(&mtx1); // no-warning
|
|
|
|
pthread_mutex_destroy(&mtx1); // no-warning
|
|
|
|
pthread_mutex_init(&mtx1, NULL); // no-warning
|
|
|
|
pthread_mutex_destroy(&mtx1); // no-warning
|
|
|
|
pthread_mutex_init(&mtx1, NULL); // no-warning
|
|
|
|
}
|
|
|
|
|
2011-07-20 04:21:41 +08:00
|
|
|
void
|
|
|
|
bad1(void)
|
|
|
|
{
|
|
|
|
pthread_mutex_lock(&mtx1); // no-warning
|
|
|
|
pthread_mutex_lock(&mtx1); // expected-warning{{This lock has already been acquired}}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
bad2(void)
|
|
|
|
{
|
|
|
|
pthread_mutex_lock(&mtx1); // no-warning
|
|
|
|
pthread_mutex_unlock(&mtx1); // no-warning
|
|
|
|
pthread_mutex_lock(&mtx1); // no-warning
|
|
|
|
pthread_mutex_lock(&mtx1); // expected-warning{{This lock has already been acquired}}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
bad3(void)
|
|
|
|
{
|
|
|
|
pthread_mutex_lock(&mtx1); // no-warning
|
|
|
|
pthread_mutex_lock(&mtx2); // no-warning
|
|
|
|
pthread_mutex_unlock(&mtx1); // expected-warning{{This was not the most recently acquired lock}}
|
|
|
|
pthread_mutex_unlock(&mtx2);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
bad4(void)
|
|
|
|
{
|
|
|
|
if (pthread_mutex_trylock(&mtx1)) // no-warning
|
|
|
|
return;
|
|
|
|
pthread_mutex_lock(&mtx2); // no-warning
|
|
|
|
pthread_mutex_unlock(&mtx1); // expected-warning{{This was not the most recently acquired lock}}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
bad5(void)
|
|
|
|
{
|
|
|
|
lck_mtx_lock(&lck1); // no-warning
|
|
|
|
lck_mtx_lock(&lck1); // expected-warning{{This lock has already been acquired}}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
bad6(void)
|
|
|
|
{
|
|
|
|
lck_mtx_lock(&lck1); // no-warning
|
|
|
|
lck_mtx_unlock(&lck1); // no-warning
|
|
|
|
lck_mtx_lock(&lck1); // no-warning
|
|
|
|
lck_mtx_lock(&lck1); // expected-warning{{This lock has already been acquired}}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
bad7(void)
|
|
|
|
{
|
|
|
|
lck_mtx_lock(&lck1); // no-warning
|
|
|
|
lck_mtx_lock(&lck2); // no-warning
|
|
|
|
lck_mtx_unlock(&lck1); // expected-warning{{This was not the most recently acquired lock}}
|
|
|
|
lck_mtx_unlock(&lck2);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
bad8(void)
|
|
|
|
{
|
|
|
|
if (lck_mtx_try_lock(&lck1) == 0) // no-warning
|
|
|
|
return;
|
|
|
|
lck_mtx_lock(&lck2); // no-warning
|
|
|
|
lck_mtx_unlock(&lck1); // expected-warning{{This was not the most recently acquired lock}}
|
|
|
|
}
|
2014-04-01 11:40:38 +08:00
|
|
|
|
|
|
|
void
|
|
|
|
bad9(void)
|
|
|
|
{
|
|
|
|
lck_mtx_unlock(&lck1); // no-warning
|
|
|
|
lck_mtx_unlock(&lck1); // expected-warning{{This lock has already been unlocked}}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
bad10(void)
|
|
|
|
{
|
|
|
|
lck_mtx_lock(&lck1); // no-warning
|
|
|
|
lck_mtx_unlock(&lck1); // no-warning
|
|
|
|
lck_mtx_unlock(&lck1); // expected-warning{{This lock has already been unlocked}}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
bad11_sub(pthread_mutex_t *lock)
|
|
|
|
{
|
|
|
|
lck_mtx_unlock(lock); // expected-warning{{This lock has already been unlocked}}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
bad11(int i)
|
|
|
|
{
|
|
|
|
lck_mtx_lock(&lck1); // no-warning
|
|
|
|
lck_mtx_unlock(&lck1); // no-warning
|
|
|
|
if (i < 5)
|
|
|
|
bad11_sub(&lck1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
bad12(void)
|
|
|
|
{
|
|
|
|
pthread_mutex_lock(&mtx1); // no-warning
|
|
|
|
pthread_mutex_unlock(&mtx1); // no-warning
|
|
|
|
pthread_mutex_lock(&mtx1); // no-warning
|
|
|
|
pthread_mutex_unlock(&mtx1); // no-warning
|
|
|
|
pthread_mutex_unlock(&mtx1); // expected-warning{{This lock has already been unlocked}}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
bad13(void)
|
|
|
|
{
|
|
|
|
pthread_mutex_lock(&mtx1); // no-warning
|
|
|
|
pthread_mutex_unlock(&mtx1); // no-warning
|
|
|
|
pthread_mutex_lock(&mtx2); // no-warning
|
|
|
|
pthread_mutex_unlock(&mtx2); // no-warning
|
|
|
|
pthread_mutex_unlock(&mtx1); // expected-warning{{This lock has already been unlocked}}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
bad14(void)
|
|
|
|
{
|
|
|
|
pthread_mutex_lock(&mtx1); // no-warning
|
|
|
|
pthread_mutex_lock(&mtx2); // no-warning
|
|
|
|
pthread_mutex_unlock(&mtx2); // no-warning
|
|
|
|
pthread_mutex_unlock(&mtx1); // no-warning
|
|
|
|
pthread_mutex_unlock(&mtx2); // expected-warning{{This lock has already been unlocked}}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
bad15(void)
|
|
|
|
{
|
|
|
|
pthread_mutex_lock(&mtx1); // no-warning
|
|
|
|
pthread_mutex_lock(&mtx2); // no-warning
|
|
|
|
pthread_mutex_unlock(&mtx2); // no-warning
|
|
|
|
pthread_mutex_unlock(&mtx1); // no-warning
|
|
|
|
pthread_mutex_lock(&mtx1); // no-warning
|
|
|
|
pthread_mutex_unlock(&mtx2); // expected-warning{{This lock has already been unlocked}}
|
|
|
|
}
|
2014-04-01 11:40:47 +08:00
|
|
|
|
|
|
|
void
|
|
|
|
bad16(void)
|
|
|
|
{
|
|
|
|
pthread_mutex_destroy(&mtx1); // no-warning
|
|
|
|
pthread_mutex_lock(&mtx1); // expected-warning{{This lock has already been destroyed}}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
bad17(void)
|
|
|
|
{
|
|
|
|
pthread_mutex_destroy(&mtx1); // no-warning
|
|
|
|
pthread_mutex_unlock(&mtx1); // expected-warning{{This lock has already been destroyed}}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
bad18(void)
|
|
|
|
{
|
|
|
|
pthread_mutex_destroy(&mtx1); // no-warning
|
|
|
|
pthread_mutex_destroy(&mtx1); // expected-warning{{This lock has already been destroyed}}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
bad19(void)
|
|
|
|
{
|
|
|
|
pthread_mutex_lock(&mtx1); // no-warning
|
|
|
|
pthread_mutex_destroy(&mtx1); // expected-warning{{This lock is still locked}}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
bad20(void)
|
|
|
|
{
|
|
|
|
lck_mtx_destroy(&mtx1, &grp1); // no-warning
|
|
|
|
lck_mtx_lock(&mtx1); // expected-warning{{This lock has already been destroyed}}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
bad21(void)
|
|
|
|
{
|
|
|
|
lck_mtx_destroy(&mtx1, &grp1); // no-warning
|
|
|
|
lck_mtx_unlock(&mtx1); // expected-warning{{This lock has already been destroyed}}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
bad22(void)
|
|
|
|
{
|
|
|
|
lck_mtx_destroy(&mtx1, &grp1); // no-warning
|
|
|
|
lck_mtx_destroy(&mtx1, &grp1); // expected-warning{{This lock has already been destroyed}}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
bad23(void)
|
|
|
|
{
|
|
|
|
lck_mtx_lock(&mtx1); // no-warning
|
|
|
|
lck_mtx_destroy(&mtx1, &grp1); // expected-warning{{This lock is still locked}}
|
|
|
|
}
|
2014-04-01 11:40:53 +08:00
|
|
|
|
|
|
|
void
|
|
|
|
bad24(void)
|
|
|
|
{
|
|
|
|
pthread_mutex_init(&mtx1, NULL); // no-warning
|
|
|
|
pthread_mutex_init(&mtx1, NULL); // expected-warning{{This lock has already been initialized}}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
bad25(void)
|
|
|
|
{
|
|
|
|
pthread_mutex_lock(&mtx1); // no-warning
|
|
|
|
pthread_mutex_init(&mtx1, NULL); // expected-warning{{This lock is still being held}}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
bad26(void)
|
|
|
|
{
|
|
|
|
pthread_mutex_unlock(&mtx1); // no-warning
|
|
|
|
pthread_mutex_init(&mtx1, NULL); // expected-warning{{This lock has already been initialized}}
|
|
|
|
}
|