91 lines · c
1// RUN: %clang_analyze_cc1 -analyzer-checker=alpha.core.C11Lock -verify %s2 3typedef int mtx_t;4struct timespec;5 6enum {7 // FIXME: The value if this enum is implementation defined. While all the8 // implementations I am aware of using 0, the right solution would be to9 // look this value up in the AST (and disable the check if it is not found).10 thrd_success = 0,11 thrd_error = 212};13 14int mtx_init(mtx_t *mutex, int type);15int mtx_lock(mtx_t *mutex);16int mtx_timedlock(mtx_t *mutex,17 const struct timespec *time_point);18int mtx_trylock(mtx_t *mutex);19int mtx_unlock(mtx_t *mutex);20int mtx_destroy(mtx_t *mutex);21 22mtx_t mtx1;23mtx_t mtx2;24 25void bad1(void)26{27 mtx_lock(&mtx1); // no-warning28 mtx_lock(&mtx1); // expected-warning{{This lock has already been acquired}}29}30 31void bad2(void) {32 mtx_t mtx;33 mtx_init(&mtx, 0);34 mtx_lock(&mtx);35} // TODO: Warn for missing unlock?36 37void bad3(void) {38 mtx_t mtx;39 mtx_init(&mtx, 0);40} // TODO: Warn for missing destroy?41 42void bad4(void) {43 mtx_t mtx;44 mtx_init(&mtx, 0);45 mtx_lock(&mtx);46 mtx_unlock(&mtx);47} // TODO: warn for missing destroy?48 49void bad5(void) {50 mtx_lock(&mtx1);51 mtx_unlock(&mtx1);52 mtx_unlock(&mtx1); // expected-warning {{This lock has already been unlocked}}53}54 55void bad6(void) {56 mtx_init(&mtx1, 0);57 if (mtx_trylock(&mtx1) != thrd_success)58 mtx_unlock(&mtx1); // expected-warning {{This lock has already been unlocked}}59}60 61void bad7(void) {62 mtx_lock(&mtx1);63 mtx_lock(&mtx2);64 mtx_unlock(&mtx1); // expected-warning {{This was not the most recently acquired lock. Possible lock order reversal}}65 mtx_unlock(&mtx2);66}67 68void good(void) {69 mtx_t mtx;70 mtx_init(&mtx, 0);71 mtx_lock(&mtx);72 mtx_unlock(&mtx);73 mtx_destroy(&mtx);74}75 76void good2(void) {77 mtx_t mtx;78 mtx_init(&mtx, 0);79 if (mtx_trylock(&mtx) == thrd_success)80 mtx_unlock(&mtx);81 mtx_destroy(&mtx);82}83 84void good3(void) {85 mtx_t mtx;86 mtx_init(&mtx, 0);87 if (mtx_timedlock(&mtx, 0) == thrd_success)88 mtx_unlock(&mtx);89 mtx_destroy(&mtx);90}91