1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
|
// RUN: %clang_analyze_cc1 \
// RUN: -analyzer-checker=unix.BlockInCriticalSection \
// RUN: -std=c++11 \
// RUN: -analyzer-output text \
// RUN: -verify %s
unsigned int sleep(unsigned int seconds) {return 0;}
namespace std {
// There are some standard library implementations where some mutex methods
// come from an implementation detail base class. We need to ensure that these
// are matched correctly.
class __mutex_base {
public:
void lock();
};
class mutex : public __mutex_base{
public:
void unlock();
bool try_lock();
};
} // namespace std
void gh_99628() {
std::mutex m;
m.lock();
// expected-note@-1 {{Entering critical section here}}
sleep(10);
// expected-warning@-1 {{Call to blocking function 'sleep' inside of critical section}}
// expected-note@-2 {{Call to blocking function 'sleep' inside of critical section}}
m.unlock();
}
void no_false_positive_gh_104241() {
std::mutex m;
m.lock();
// If inheritance not handled properly, this unlock might not match the lock
// above because technically they act on different memory regions:
// __mutex_base and mutex.
m.unlock();
sleep(10); // no-warning
}
struct TwoMutexes {
std::mutex m1;
std::mutex m2;
};
void two_mutexes_no_false_negative(TwoMutexes &tm) {
tm.m1.lock();
// expected-note@-1 {{Entering critical section here}}
tm.m2.unlock();
sleep(10);
// expected-warning@-1 {{Call to blocking function 'sleep' inside of critical section}}
// expected-note@-2 {{Call to blocking function 'sleep' inside of critical section}}
tm.m1.unlock();
}
struct MyMutexBase1 : std::mutex {
void lock1() { lock(); }
// expected-note@-1 {{Entering critical section here}}
void unlock1() { unlock(); }
};
struct MyMutexBase2 : std::mutex {
void lock2() { lock(); }
void unlock2() { unlock(); }
};
struct MyMutex : MyMutexBase1, MyMutexBase2 {};
// MyMutex has two distinct std::mutex as base classes
void custom_mutex_tp(MyMutexBase1 &mb) {
mb.lock();
// expected-note@-1 {{Entering critical section here}}
sleep(10);
// expected-warning@-1 {{Call to blocking function 'sleep' inside of critical section}}
// expected-note@-2 {{Call to blocking function 'sleep' inside of critical section}}
mb.unlock();
}
void custom_mutex_tn(MyMutexBase1 &mb) {
mb.lock();
mb.unlock();
sleep(10);
}
void custom_mutex_cast_tp(MyMutexBase1 &mb) {
static_cast<std::mutex&>(mb).lock();
// expected-note@-1 {{Entering critical section here}}
sleep(10);
// expected-warning@-1 {{Call to blocking function 'sleep' inside of critical section}}
// expected-note@-2 {{Call to blocking function 'sleep' inside of critical section}}
static_cast<std::mutex&>(mb).unlock();
}
void custom_mutex_cast_tn(MyMutexBase1 &mb) {
static_cast<std::mutex&>(mb).lock();
static_cast<std::mutex&>(mb).unlock();
sleep(10);
}
void two_custom_mutex_bases_tp(MyMutex &m) {
m.lock1();
// expected-note@-1 {{Calling 'MyMutexBase1::lock1'}}
// expected-note@-2 {{Returning from 'MyMutexBase1::lock1'}}
m.unlock2();
sleep(10);
// expected-warning@-1 {{Call to blocking function 'sleep' inside of critical section}}
// expected-note@-2 {{Call to blocking function 'sleep' inside of critical section}}
m.unlock1();
}
void two_custom_mutex_bases_tn(MyMutex &m) {
m.lock1();
m.unlock1();
sleep(10);
}
void two_custom_mutex_bases_casts_tp(MyMutex &m) {
static_cast<MyMutexBase1&>(m).lock();
// expected-note@-1 {{Entering critical section here}}
static_cast<MyMutexBase2&>(m).unlock();
sleep(10);
// expected-warning@-1 {{Call to blocking function 'sleep' inside of critical section}}
// expected-note@-2 {{Call to blocking function 'sleep' inside of critical section}}
static_cast<MyMutexBase1&>(m).unlock();
}
void two_custom_mutex_bases_casts_tn(MyMutex &m) {
static_cast<MyMutexBase1&>(m).lock();
static_cast<MyMutexBase1&>(m).unlock();
sleep(10);
}
struct MutexVirtBase1 : virtual std::mutex {
void lock1() { lock(); }
// expected-note@-1 {{Entering critical section here}}
void unlock1() { unlock(); }
};
struct MutexVirtBase2 : virtual std::mutex {
void lock2() { lock(); }
void unlock2() { unlock(); }
};
struct CombinedVirtMutex : MutexVirtBase1, MutexVirtBase2 {};
void virt_inherited_mutexes_same_base_tn1(CombinedVirtMutex &cvt) {
cvt.lock1();
cvt.unlock1();
sleep(10);
}
void virt_inherited_mutexes_different_bases_tn(CombinedVirtMutex &cvt) {
cvt.lock1();
cvt.unlock2(); // Despite a different name, unlock2 acts on the same mutex as lock1
sleep(10);
}
void virt_inherited_mutexes_different_bases_tp(CombinedVirtMutex &cvt) {
cvt.lock1();
// expected-note@-1 {{Calling 'MutexVirtBase1::lock1'}}
// expected-note@-2 {{Returning from 'MutexVirtBase1::lock1'}}
sleep(10);
// expected-warning@-1 {{Call to blocking function 'sleep' inside of critical section}}
// expected-note@-2 {{Call to blocking function 'sleep' inside of critical section}}
cvt.unlock1();
}
namespace std {
template <class... MutexTypes> struct scoped_lock {
explicit scoped_lock(MutexTypes&... m);
~scoped_lock();
};
template <class MutexType> class scoped_lock<MutexType> {
public:
explicit scoped_lock(MutexType& m) : m(m) { m.lock(); }
~scoped_lock() { m.unlock(); }
private:
MutexType& m;
};
} // namespace std
namespace gh_104241 {
int magic_number;
std::mutex m;
void fixed() {
int current;
for (int items_processed = 0; items_processed < 100; ++items_processed) {
{
std::scoped_lock<std::mutex> guard(m);
current = magic_number;
}
sleep(current); // expected no warning
}
}
} // namespace gh_104241
|