22 using namespace clang;
32 UntouchedAndPossiblyDestroyed,
33 UnlockedAndPossiblyDestroyed
37 LockState(
Kind K) : K(K) {}
40 static LockState getLocked() {
return LockState(Locked); }
41 static LockState getUnlocked() {
return LockState(Unlocked); }
42 static LockState getDestroyed() {
return LockState(Destroyed); }
43 static LockState getUntouchedAndPossiblyDestroyed() {
44 return LockState(UntouchedAndPossiblyDestroyed);
46 static LockState getUnlockedAndPossiblyDestroyed() {
47 return LockState(UnlockedAndPossiblyDestroyed);
54 bool isLocked()
const {
return K == Locked; }
55 bool isUnlocked()
const {
return K == Unlocked; }
56 bool isDestroyed()
const {
return K == Destroyed; }
57 bool isUntouchedAndPossiblyDestroyed()
const {
58 return K == UntouchedAndPossiblyDestroyed;
60 bool isUnlockedAndPossiblyDestroyed()
const {
61 return K == UnlockedAndPossiblyDestroyed;
64 void Profile(llvm::FoldingSetNodeID &
ID)
const {
69 class PthreadLockChecker
70 :
public Checker<check::PostStmt<CallExpr>, check::DeadSymbols> {
71 mutable std::unique_ptr<BugType> BT_doublelock;
72 mutable std::unique_ptr<BugType> BT_doubleunlock;
73 mutable std::unique_ptr<BugType> BT_destroylock;
74 mutable std::unique_ptr<BugType> BT_initlock;
75 mutable std::unique_ptr<BugType> BT_lor;
76 enum LockingSemantics {
82 void checkPostStmt(
const CallExpr *CE, CheckerContext &C)
const;
83 void checkDeadSymbols(SymbolReaper &SymReaper, CheckerContext &C)
const;
85 const char *NL,
const char *Sep)
const override;
87 void AcquireLock(CheckerContext &C,
const CallExpr *CE, SVal lock,
88 bool isTryLock,
enum LockingSemantics semantics)
const;
90 void ReleaseLock(CheckerContext &C,
const CallExpr *CE, SVal lock)
const;
91 void DestroyLock(CheckerContext &C,
const CallExpr *CE, SVal Lock,
92 enum LockingSemantics semantics)
const;
93 void InitLock(CheckerContext &C,
const CallExpr *CE, SVal Lock)
const;
94 void reportUseDestroyedBug(CheckerContext &C,
const CallExpr *CE)
const;
96 const MemRegion *lockR,
110 void PthreadLockChecker::checkPostStmt(
const CallExpr *CE,
111 CheckerContext &C)
const {
112 StringRef FName = C.getCalleeName(CE);
119 if (FName ==
"pthread_mutex_lock" ||
120 FName ==
"pthread_rwlock_rdlock" ||
121 FName ==
"pthread_rwlock_wrlock")
122 AcquireLock(C, CE, C.getSVal(CE->
getArg(0)),
false, PthreadSemantics);
123 else if (FName ==
"lck_mtx_lock" ||
124 FName ==
"lck_rw_lock_exclusive" ||
125 FName ==
"lck_rw_lock_shared")
126 AcquireLock(C, CE, C.getSVal(CE->
getArg(0)),
false, XNUSemantics);
127 else if (FName ==
"pthread_mutex_trylock" ||
128 FName ==
"pthread_rwlock_tryrdlock" ||
129 FName ==
"pthread_rwlock_trywrlock")
130 AcquireLock(C, CE, C.getSVal(CE->
getArg(0)),
131 true, PthreadSemantics);
132 else if (FName ==
"lck_mtx_try_lock" ||
133 FName ==
"lck_rw_try_lock_exclusive" ||
134 FName ==
"lck_rw_try_lock_shared")
135 AcquireLock(C, CE, C.getSVal(CE->
getArg(0)),
true, XNUSemantics);
136 else if (FName ==
"pthread_mutex_unlock" ||
137 FName ==
"pthread_rwlock_unlock" ||
138 FName ==
"lck_mtx_unlock" ||
139 FName ==
"lck_rw_done")
140 ReleaseLock(C, CE, C.getSVal(CE->
getArg(0)));
141 else if (FName ==
"pthread_mutex_destroy")
142 DestroyLock(C, CE, C.getSVal(CE->
getArg(0)), PthreadSemantics);
143 else if (FName ==
"lck_mtx_destroy")
144 DestroyLock(C, CE, C.getSVal(CE->
getArg(0)), XNUSemantics);
145 else if (FName ==
"pthread_mutex_init")
146 InitLock(C, CE, C.getSVal(CE->
getArg(0)));
161 const LockState *lstate = state->get<LockMap>(lockR);
165 assert(lstate->isUntouchedAndPossiblyDestroyed() ||
166 lstate->isUnlockedAndPossiblyDestroyed());
168 ConstraintManager &CMgr = state->getConstraintManager();
169 ConditionTruthVal retZero = CMgr.isNull(state, *sym);
170 if (retZero.isConstrainedFalse()) {
171 if (lstate->isUntouchedAndPossiblyDestroyed())
172 state = state->remove<LockMap>(lockR);
173 else if (lstate->isUnlockedAndPossiblyDestroyed())
174 state = state->set<LockMap>(lockR, LockState::getUnlocked());
176 state = state->set<LockMap>(lockR, LockState::getDestroyed());
180 state = state->remove<DestroyRetVal>(lockR);
185 const char *NL,
const char *Sep)
const {
186 LockMapTy LM = State->get<LockMap>();
188 Out << Sep <<
"Mutex states:" << NL;
190 I.first->dumpToStream(Out);
191 if (I.second.isLocked())
193 else if (I.second.isUnlocked())
195 else if (I.second.isDestroyed())
196 Out <<
": destroyed";
197 else if (I.second.isUntouchedAndPossiblyDestroyed())
198 Out <<
": not tracked, possibly destroyed";
199 else if (I.second.isUnlockedAndPossiblyDestroyed())
200 Out <<
": unlocked, possibly destroyed";
205 LockSetTy LS = State->get<LockSet>();
207 Out << Sep <<
"Mutex lock order:" << NL;
209 I->dumpToStream(Out);
217 void PthreadLockChecker::AcquireLock(CheckerContext &C,
const CallExpr *CE,
218 SVal lock,
bool isTryLock,
219 enum LockingSemantics semantics)
const {
221 const MemRegion *lockR = lock.getAsRegion();
226 const SymbolRef *sym = state->get<DestroyRetVal>(lockR);
228 state = resolvePossiblyDestroyedMutex(state, lockR, sym);
230 SVal
X = C.getSVal(CE);
231 if (X.isUnknownOrUndef())
234 DefinedSVal retVal = X.castAs<DefinedSVal>();
236 if (
const LockState *LState = state->get<LockMap>(lockR)) {
237 if (LState->isLocked()) {
239 BT_doublelock.reset(
new BugType(
this,
"Double locking",
241 ExplodedNode *N = C.generateErrorNode();
244 auto report = llvm::make_unique<BugReport>(
245 *BT_doublelock,
"This lock has already been acquired", N);
247 C.emitReport(std::move(report));
249 }
else if (LState->isDestroyed()) {
250 reportUseDestroyedBug(C, CE);
260 case PthreadSemantics:
261 std::tie(lockFail, lockSucc) = state->assume(retVal);
264 std::tie(lockSucc, lockFail) = state->assume(retVal);
267 llvm_unreachable(
"Unknown tryLock locking semantics");
269 assert(lockFail && lockSucc);
270 C.addTransition(lockFail);
272 }
else if (semantics == PthreadSemantics) {
274 lockSucc = state->assume(retVal,
false);
279 assert((semantics == XNUSemantics) &&
"Unknown locking semantics");
284 lockSucc = lockSucc->add<LockSet>(lockR);
285 lockSucc = lockSucc->set<LockMap>(lockR, LockState::getLocked());
286 C.addTransition(lockSucc);
289 void PthreadLockChecker::ReleaseLock(CheckerContext &C,
const CallExpr *CE,
292 const MemRegion *lockR = lock.getAsRegion();
297 const SymbolRef *sym = state->get<DestroyRetVal>(lockR);
299 state = resolvePossiblyDestroyedMutex(state, lockR, sym);
301 if (
const LockState *LState = state->get<LockMap>(lockR)) {
302 if (LState->isUnlocked()) {
303 if (!BT_doubleunlock)
304 BT_doubleunlock.reset(
new BugType(
this,
"Double unlocking",
306 ExplodedNode *N = C.generateErrorNode();
309 auto Report = llvm::make_unique<BugReport>(
310 *BT_doubleunlock,
"This lock has already been unlocked", N);
312 C.emitReport(std::move(Report));
314 }
else if (LState->isDestroyed()) {
315 reportUseDestroyedBug(C, CE);
320 LockSetTy LS = state->get<LockSet>();
325 const MemRegion *firstLockR = LS.getHead();
326 if (firstLockR != lockR) {
328 BT_lor.reset(
new BugType(
this,
"Lock order reversal",
"Lock checker"));
329 ExplodedNode *N = C.generateErrorNode();
332 auto report = llvm::make_unique<BugReport>(
333 *BT_lor,
"This was not the most recently acquired lock. Possible " 334 "lock order reversal", N);
336 C.emitReport(std::move(report));
340 state = state->set<LockSet>(LS.getTail());
343 state = state->set<LockMap>(lockR, LockState::getUnlocked());
344 C.addTransition(state);
347 void PthreadLockChecker::DestroyLock(CheckerContext &C,
const CallExpr *CE,
349 enum LockingSemantics semantics)
const {
351 const MemRegion *LockR = Lock.getAsRegion();
357 const SymbolRef *sym = State->get<DestroyRetVal>(LockR);
359 State = resolvePossiblyDestroyedMutex(State, LockR, sym);
361 const LockState *LState = State->get<LockMap>(LockR);
364 if (semantics == PthreadSemantics) {
365 if (!LState || LState->isUnlocked()) {
366 SymbolRef sym = C.getSVal(CE).getAsSymbol();
368 State = State->remove<LockMap>(LockR);
369 C.addTransition(State);
372 State = State->set<DestroyRetVal>(LockR, sym);
373 if (LState && LState->isUnlocked())
374 State = State->set<LockMap>(
375 LockR, LockState::getUnlockedAndPossiblyDestroyed());
377 State = State->set<LockMap>(
378 LockR, LockState::getUntouchedAndPossiblyDestroyed());
379 C.addTransition(State);
383 if (!LState || LState->isUnlocked()) {
384 State = State->set<LockMap>(LockR, LockState::getDestroyed());
385 C.addTransition(State);
391 if (LState->isLocked()) {
392 Message =
"This lock is still locked";
394 Message =
"This lock has already been destroyed";
398 BT_destroylock.reset(
new BugType(
this,
"Destroy invalid lock",
400 ExplodedNode *N = C.generateErrorNode();
403 auto Report = llvm::make_unique<BugReport>(*BT_destroylock, Message, N);
405 C.emitReport(std::move(Report));
408 void PthreadLockChecker::InitLock(CheckerContext &C,
const CallExpr *CE,
411 const MemRegion *LockR = Lock.getAsRegion();
417 const SymbolRef *sym = State->get<DestroyRetVal>(LockR);
419 State = resolvePossiblyDestroyedMutex(State, LockR, sym);
421 const struct LockState *LState = State->get<LockMap>(LockR);
422 if (!LState || LState->isDestroyed()) {
423 State = State->set<LockMap>(LockR, LockState::getUnlocked());
424 C.addTransition(State);
430 if (LState->isLocked()) {
431 Message =
"This lock is still being held";
433 Message =
"This lock has already been initialized";
437 BT_initlock.reset(
new BugType(
this,
"Init invalid lock",
439 ExplodedNode *N = C.generateErrorNode();
442 auto Report = llvm::make_unique<BugReport>(*BT_initlock, Message, N);
444 C.emitReport(std::move(Report));
447 void PthreadLockChecker::reportUseDestroyedBug(CheckerContext &C,
450 BT_destroylock.reset(
new BugType(
this,
"Use destroyed lock",
452 ExplodedNode *N = C.generateErrorNode();
455 auto Report = llvm::make_unique<BugReport>(
456 *BT_destroylock,
"This lock has already been destroyed", N);
458 C.emitReport(std::move(Report));
461 void PthreadLockChecker::checkDeadSymbols(SymbolReaper &SymReaper,
462 CheckerContext &C)
const {
467 DestroyRetValTy TrackedSymbols = State->get<DestroyRetVal>();
468 for (DestroyRetValTy::iterator I = TrackedSymbols.begin(),
469 E = TrackedSymbols.end();
472 const MemRegion *lockR = I->first;
473 bool IsSymDead = SymReaper.isDead(Sym);
476 State = resolvePossiblyDestroyedMutex(State, lockR, &Sym);
478 C.addTransition(State);
481 void ento::registerPthreadLockChecker(CheckerManager &mgr) {
482 mgr.registerChecker<PthreadLockChecker>();
Expr * getArg(unsigned Arg)
getArg - Return the specified argument.
bool operator==(CanQual< T > x, CanQual< U > y)
const SymExpr * SymbolRef
unsigned getNumArgs() const
getNumArgs - Return the number of actual arguments to this call.
IntrusiveRefCntPtr< const ProgramState > ProgramStateRef
i32 captured_struct **param SharedsTy A type which contains references the shared variables *param Shareds Context with the list of shared variables from the p *TaskFunction *param Data Additional data for task generation like final * state
#define REGISTER_LIST_WITH_PROGRAMSTATE(Name, Elem)
Declares an immutable list type NameTy, suitable for placement into the ProgramState.
#define REGISTER_MAP_WITH_PROGRAMSTATE(Name, Key, Value)
Declares an immutable map of type NameTy, suitable for placement into the ProgramState.
Dataflow Directional Tag Classes.
X
Add a minimal nested name specifier fixit hint to allow lookup of a tag name from an outer enclosing ...
SourceRange getSourceRange() const LLVM_READONLY
SourceLocation tokens are not useful in isolation - they are low level value objects created/interpre...
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).