21 using namespace clang;
31 UntouchedAndPossiblyDestroyed,
32 UnlockedAndPossiblyDestroyed
36 LockState(
Kind K) : K(K) {}
39 static LockState getLocked() {
return LockState(Locked); }
40 static LockState getUnlocked() {
return LockState(Unlocked); }
41 static LockState getDestroyed() {
return LockState(Destroyed); }
42 static LockState getUntouchedAndPossiblyDestroyed() {
43 return LockState(UntouchedAndPossiblyDestroyed);
45 static LockState getUnlockedAndPossiblyDestroyed() {
46 return LockState(UnlockedAndPossiblyDestroyed);
53 bool isLocked()
const {
return K == Locked; }
54 bool isUnlocked()
const {
return K == Unlocked; }
55 bool isDestroyed()
const {
return K == Destroyed; }
56 bool isUntouchedAndPossiblyDestroyed()
const {
57 return K == UntouchedAndPossiblyDestroyed;
59 bool isUnlockedAndPossiblyDestroyed()
const {
60 return K == UnlockedAndPossiblyDestroyed;
63 void Profile(llvm::FoldingSetNodeID &
ID)
const {
68 class PthreadLockChecker
69 :
public Checker<check::PostStmt<CallExpr>, check::DeadSymbols> {
70 mutable std::unique_ptr<BugType> BT_doublelock;
71 mutable std::unique_ptr<BugType> BT_doubleunlock;
72 mutable std::unique_ptr<BugType> BT_destroylock;
73 mutable std::unique_ptr<BugType> BT_initlock;
74 mutable std::unique_ptr<BugType> BT_lor;
75 enum LockingSemantics {
81 void checkPostStmt(
const CallExpr *CE, CheckerContext &C)
const;
82 void checkDeadSymbols(SymbolReaper &SymReaper, CheckerContext &C)
const;
84 const char *NL,
const char *Sep)
const override;
86 void AcquireLock(CheckerContext &C,
const CallExpr *CE, SVal lock,
87 bool isTryLock,
enum LockingSemantics semantics)
const;
89 void ReleaseLock(CheckerContext &C,
const CallExpr *CE, SVal lock)
const;
90 void DestroyLock(CheckerContext &C,
const CallExpr *CE, SVal Lock,
91 enum LockingSemantics semantics)
const;
92 void InitLock(CheckerContext &C,
const CallExpr *CE, SVal Lock)
const;
93 void reportUseDestroyedBug(CheckerContext &C,
const CallExpr *CE)
const;
95 const MemRegion *lockR,
109 void PthreadLockChecker::checkPostStmt(
const CallExpr *CE,
110 CheckerContext &C)
const {
111 StringRef FName = C.getCalleeName(CE);
118 if (FName ==
"pthread_mutex_lock" ||
119 FName ==
"pthread_rwlock_rdlock" ||
120 FName ==
"pthread_rwlock_wrlock")
121 AcquireLock(C, CE, C.getSVal(CE->
getArg(0)),
false, PthreadSemantics);
122 else if (FName ==
"lck_mtx_lock" ||
123 FName ==
"lck_rw_lock_exclusive" ||
124 FName ==
"lck_rw_lock_shared")
125 AcquireLock(C, CE, C.getSVal(CE->
getArg(0)),
false, XNUSemantics);
126 else if (FName ==
"pthread_mutex_trylock" ||
127 FName ==
"pthread_rwlock_tryrdlock" ||
128 FName ==
"pthread_rwlock_trywrlock")
129 AcquireLock(C, CE, C.getSVal(CE->
getArg(0)),
130 true, PthreadSemantics);
131 else if (FName ==
"lck_mtx_try_lock" ||
132 FName ==
"lck_rw_try_lock_exclusive" ||
133 FName ==
"lck_rw_try_lock_shared")
134 AcquireLock(C, CE, C.getSVal(CE->
getArg(0)),
true, XNUSemantics);
135 else if (FName ==
"pthread_mutex_unlock" ||
136 FName ==
"pthread_rwlock_unlock" ||
137 FName ==
"lck_mtx_unlock" ||
138 FName ==
"lck_rw_done")
139 ReleaseLock(C, CE, C.getSVal(CE->
getArg(0)));
140 else if (FName ==
"pthread_mutex_destroy")
141 DestroyLock(C, CE, C.getSVal(CE->
getArg(0)), PthreadSemantics);
142 else if (FName ==
"lck_mtx_destroy")
143 DestroyLock(C, CE, C.getSVal(CE->
getArg(0)), XNUSemantics);
144 else if (FName ==
"pthread_mutex_init")
145 InitLock(C, CE, C.getSVal(CE->
getArg(0)));
160 const LockState *lstate = state->get<LockMap>(lockR);
164 assert(lstate->isUntouchedAndPossiblyDestroyed() ||
165 lstate->isUnlockedAndPossiblyDestroyed());
167 ConstraintManager &CMgr = state->getConstraintManager();
168 ConditionTruthVal retZero = CMgr.isNull(state, *sym);
169 if (retZero.isConstrainedFalse()) {
170 if (lstate->isUntouchedAndPossiblyDestroyed())
171 state = state->remove<LockMap>(lockR);
172 else if (lstate->isUnlockedAndPossiblyDestroyed())
173 state = state->set<LockMap>(lockR, LockState::getUnlocked());
175 state = state->set<LockMap>(lockR, LockState::getDestroyed());
179 state = state->remove<DestroyRetVal>(lockR);
184 const char *NL,
const char *Sep)
const {
185 LockMapTy LM = State->get<LockMap>();
187 Out << Sep <<
"Mutex states:" << NL;
189 I.first->dumpToStream(Out);
190 if (I.second.isLocked())
192 else if (I.second.isUnlocked())
194 else if (I.second.isDestroyed())
195 Out <<
": destroyed";
196 else if (I.second.isUntouchedAndPossiblyDestroyed())
197 Out <<
": not tracked, possibly destroyed";
198 else if (I.second.isUnlockedAndPossiblyDestroyed())
199 Out <<
": unlocked, possibly destroyed";
204 LockSetTy LS = State->get<LockSet>();
206 Out << Sep <<
"Mutex lock order:" << NL;
208 I->dumpToStream(Out);
216 void PthreadLockChecker::AcquireLock(CheckerContext &C,
const CallExpr *CE,
217 SVal lock,
bool isTryLock,
218 enum LockingSemantics semantics)
const {
220 const MemRegion *lockR = lock.getAsRegion();
225 const SymbolRef *sym = state->get<DestroyRetVal>(lockR);
227 state = resolvePossiblyDestroyedMutex(state, lockR, sym);
229 SVal
X = C.getSVal(CE);
230 if (X.isUnknownOrUndef())
233 DefinedSVal retVal = X.castAs<DefinedSVal>();
235 if (
const LockState *LState = state->get<LockMap>(lockR)) {
236 if (LState->isLocked()) {
238 BT_doublelock.reset(
new BugType(
this,
"Double locking",
240 ExplodedNode *N = C.generateErrorNode();
243 auto report = std::make_unique<PathSensitiveBugReport>(
244 *BT_doublelock,
"This lock has already been acquired", N);
246 C.emitReport(std::move(report));
248 }
else if (LState->isDestroyed()) {
249 reportUseDestroyedBug(C, CE);
259 case PthreadSemantics:
260 std::tie(lockFail, lockSucc) = state->assume(retVal);
263 std::tie(lockSucc, lockFail) = state->assume(retVal);
266 llvm_unreachable(
"Unknown tryLock locking semantics");
268 assert(lockFail && lockSucc);
269 C.addTransition(lockFail);
271 }
else if (semantics == PthreadSemantics) {
273 lockSucc = state->assume(retVal,
false);
278 assert((semantics == XNUSemantics) &&
"Unknown locking semantics");
283 lockSucc = lockSucc->add<LockSet>(lockR);
284 lockSucc = lockSucc->set<LockMap>(lockR, LockState::getLocked());
285 C.addTransition(lockSucc);
288 void PthreadLockChecker::ReleaseLock(CheckerContext &C,
const CallExpr *CE,
291 const MemRegion *lockR = lock.getAsRegion();
296 const SymbolRef *sym = state->get<DestroyRetVal>(lockR);
298 state = resolvePossiblyDestroyedMutex(state, lockR, sym);
300 if (
const LockState *LState = state->get<LockMap>(lockR)) {
301 if (LState->isUnlocked()) {
302 if (!BT_doubleunlock)
303 BT_doubleunlock.reset(
new BugType(
this,
"Double unlocking",
305 ExplodedNode *N = C.generateErrorNode();
308 auto Report = std::make_unique<PathSensitiveBugReport>(
309 *BT_doubleunlock,
"This lock has already been unlocked", N);
311 C.emitReport(std::move(Report));
313 }
else if (LState->isDestroyed()) {
314 reportUseDestroyedBug(C, CE);
319 LockSetTy LS = state->get<LockSet>();
324 const MemRegion *firstLockR = LS.getHead();
325 if (firstLockR != lockR) {
327 BT_lor.reset(
new BugType(
this,
"Lock order reversal",
"Lock checker"));
328 ExplodedNode *N = C.generateErrorNode();
331 auto report = std::make_unique<PathSensitiveBugReport>(
332 *BT_lor,
"This was not the most recently acquired lock. Possible " 333 "lock order reversal", N);
335 C.emitReport(std::move(report));
339 state = state->set<LockSet>(LS.getTail());
342 state = state->set<LockMap>(lockR, LockState::getUnlocked());
343 C.addTransition(state);
346 void PthreadLockChecker::DestroyLock(CheckerContext &C,
const CallExpr *CE,
348 enum LockingSemantics semantics)
const {
350 const MemRegion *LockR = Lock.getAsRegion();
356 const SymbolRef *sym = State->get<DestroyRetVal>(LockR);
358 State = resolvePossiblyDestroyedMutex(State, LockR, sym);
360 const LockState *LState = State->get<LockMap>(LockR);
363 if (semantics == PthreadSemantics) {
364 if (!LState || LState->isUnlocked()) {
365 SymbolRef sym = C.getSVal(CE).getAsSymbol();
367 State = State->remove<LockMap>(LockR);
368 C.addTransition(State);
371 State = State->set<DestroyRetVal>(LockR, sym);
372 if (LState && LState->isUnlocked())
373 State = State->set<LockMap>(
374 LockR, LockState::getUnlockedAndPossiblyDestroyed());
376 State = State->set<LockMap>(
377 LockR, LockState::getUntouchedAndPossiblyDestroyed());
378 C.addTransition(State);
382 if (!LState || LState->isUnlocked()) {
383 State = State->set<LockMap>(LockR, LockState::getDestroyed());
384 C.addTransition(State);
390 if (LState->isLocked()) {
391 Message =
"This lock is still locked";
393 Message =
"This lock has already been destroyed";
397 BT_destroylock.reset(
new BugType(
this,
"Destroy invalid lock",
399 ExplodedNode *N = C.generateErrorNode();
403 std::make_unique<PathSensitiveBugReport>(*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();
443 std::make_unique<PathSensitiveBugReport>(*BT_initlock, Message, N);
445 C.emitReport(std::move(Report));
448 void PthreadLockChecker::reportUseDestroyedBug(CheckerContext &C,
451 BT_destroylock.reset(
new BugType(
this,
"Use destroyed lock",
453 ExplodedNode *N = C.generateErrorNode();
456 auto Report = std::make_unique<PathSensitiveBugReport>(
457 *BT_destroylock,
"This lock has already been destroyed", N);
459 C.emitReport(std::move(Report));
462 void PthreadLockChecker::checkDeadSymbols(SymbolReaper &SymReaper,
463 CheckerContext &C)
const {
468 DestroyRetValTy TrackedSymbols = State->get<DestroyRetVal>();
469 for (DestroyRetValTy::iterator I = TrackedSymbols.begin(),
470 E = TrackedSymbols.end();
473 const MemRegion *lockR = I->first;
474 bool IsSymDead = SymReaper.isDead(Sym);
477 State = resolvePossiblyDestroyedMutex(State, lockR, &Sym);
479 C.addTransition(State);
482 void ento::registerPthreadLockChecker(CheckerManager &mgr) {
483 mgr.registerChecker<PthreadLockChecker>();
486 bool ento::shouldRegisterPthreadLockChecker(
const LangOptions &LO) {
Expr * getArg(unsigned Arg)
getArg - Return the specified argument.
bool operator==(CanQual< T > x, CanQual< U > y)
unsigned getNumArgs() const
getNumArgs - Return the number of actual arguments to this call.
IntrusiveRefCntPtr< const ProgramState > ProgramStateRef
const SymExpr * SymbolRef
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
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]).