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 {
86 bool isTryLock,
enum LockingSemantics semantics)
const;
90 enum LockingSemantics semantics)
const;
108 void PthreadLockChecker::checkPostStmt(const
CallExpr *CE,
112 StringRef FName = C.getCalleeName(CE);
116 if (CE->getNumArgs() != 1 && CE->getNumArgs() != 2)
119 if (FName ==
"pthread_mutex_lock" ||
120 FName ==
"pthread_rwlock_rdlock" ||
121 FName ==
"pthread_rwlock_wrlock")
122 AcquireLock(C, CE, state->getSVal(CE->getArg(0), LCtx),
123 false, PthreadSemantics);
124 else if (FName ==
"lck_mtx_lock" ||
125 FName ==
"lck_rw_lock_exclusive" ||
126 FName ==
"lck_rw_lock_shared")
127 AcquireLock(C, CE, state->getSVal(CE->getArg(0), LCtx),
128 false, XNUSemantics);
129 else if (FName ==
"pthread_mutex_trylock" ||
130 FName ==
"pthread_rwlock_tryrdlock" ||
131 FName ==
"pthread_rwlock_trywrlock")
132 AcquireLock(C, CE, state->getSVal(CE->getArg(0), LCtx),
133 true, PthreadSemantics);
134 else if (FName ==
"lck_mtx_try_lock" ||
135 FName ==
"lck_rw_try_lock_exclusive" ||
136 FName ==
"lck_rw_try_lock_shared")
137 AcquireLock(C, CE, state->getSVal(CE->getArg(0), LCtx),
139 else if (FName ==
"pthread_mutex_unlock" ||
140 FName ==
"pthread_rwlock_unlock" ||
141 FName ==
"lck_mtx_unlock" ||
142 FName ==
"lck_rw_done")
143 ReleaseLock(C, CE, state->getSVal(CE->getArg(0), LCtx));
144 else if (FName ==
"pthread_mutex_destroy")
145 DestroyLock(C, CE, state->getSVal(CE->getArg(0), LCtx), PthreadSemantics);
146 else if (FName ==
"lck_mtx_destroy")
147 DestroyLock(C, CE, state->getSVal(CE->getArg(0), LCtx), XNUSemantics);
148 else if (FName ==
"pthread_mutex_init")
149 InitLock(C, CE, state->getSVal(CE->getArg(0), LCtx));
164 const LockState *lstate = state->get<LockMap>(lockR);
168 assert(lstate->isUntouchedAndPossiblyDestroyed() ||
169 lstate->isUnlockedAndPossiblyDestroyed());
174 if (lstate->isUntouchedAndPossiblyDestroyed())
175 state = state->remove<LockMap>(lockR);
176 else if (lstate->isUnlockedAndPossiblyDestroyed())
177 state = state->set<LockMap>(lockR, LockState::getUnlocked());
179 state = state->set<LockMap>(lockR, LockState::getDestroyed());
183 state = state->remove<DestroyRetVal>(lockR);
188 SVal lock,
bool isTryLock,
189 enum LockingSemantics semantics)
const {
196 const SymbolRef *sym = state->get<DestroyRetVal>(lockR);
198 state = resolvePossiblyDestroyedMutex(state, lockR, sym);
206 if (
const LockState *LState = state->get<LockMap>(lockR)) {
207 if (LState->isLocked()) {
209 BT_doublelock.reset(
new BugType(
this,
"Double locking",
214 auto report = llvm::make_unique<BugReport>(
215 *BT_doublelock,
"This lock has already been acquired", N);
219 }
else if (LState->isDestroyed()) {
220 reportUseDestroyedBug(C, CE);
230 case PthreadSemantics:
231 std::tie(lockFail, lockSucc) = state->assume(retVal);
234 std::tie(lockSucc, lockFail) = state->assume(retVal);
237 llvm_unreachable(
"Unknown tryLock locking semantics");
239 assert(lockFail && lockSucc);
242 }
else if (semantics == PthreadSemantics) {
244 lockSucc = state->assume(retVal,
false);
249 assert((semantics == XNUSemantics) &&
"Unknown locking semantics");
254 lockSucc = lockSucc->add<LockSet>(lockR);
255 lockSucc = lockSucc->set<LockMap>(lockR, LockState::getLocked());
267 const SymbolRef *sym = state->get<DestroyRetVal>(lockR);
269 state = resolvePossiblyDestroyedMutex(state, lockR, sym);
271 if (
const LockState *LState = state->get<LockMap>(lockR)) {
272 if (LState->isUnlocked()) {
273 if (!BT_doubleunlock)
274 BT_doubleunlock.reset(
new BugType(
this,
"Double unlocking",
279 auto Report = llvm::make_unique<BugReport>(
280 *BT_doubleunlock,
"This lock has already been unlocked", N);
284 }
else if (LState->isDestroyed()) {
285 reportUseDestroyedBug(C, CE);
290 LockSetTy LS = state->get<LockSet>();
295 const MemRegion *firstLockR = LS.getHead();
296 if (firstLockR != lockR) {
298 BT_lor.reset(
new BugType(
this,
"Lock order reversal",
"Lock checker"));
302 auto report = llvm::make_unique<BugReport>(
303 *BT_lor,
"This was not the most recently acquired lock. Possible "
304 "lock order reversal", N);
310 state = state->set<LockSet>(LS.getTail());
313 state = state->set<LockMap>(lockR, LockState::getUnlocked());
319 enum LockingSemantics semantics)
const {
327 const SymbolRef *sym = State->get<DestroyRetVal>(LockR);
329 State = resolvePossiblyDestroyedMutex(State, LockR, sym);
331 const LockState *LState = State->get<LockMap>(LockR);
334 if (semantics == PthreadSemantics) {
335 if (!LState || LState->isUnlocked()) {
338 State = State->remove<LockMap>(LockR);
342 State = State->set<DestroyRetVal>(LockR, sym);
343 if (LState && LState->isUnlocked())
344 State = State->set<LockMap>(
345 LockR, LockState::getUnlockedAndPossiblyDestroyed());
347 State = State->set<LockMap>(
348 LockR, LockState::getUntouchedAndPossiblyDestroyed());
353 if (!LState || LState->isUnlocked()) {
354 State = State->set<LockMap>(LockR, LockState::getDestroyed());
361 if (LState->isLocked()) {
362 Message =
"This lock is still locked";
364 Message =
"This lock has already been destroyed";
368 BT_destroylock.reset(
new BugType(
this,
"Destroy invalid lock",
373 auto Report = llvm::make_unique<BugReport>(*BT_destroylock, Message, N);
387 const SymbolRef *sym = State->get<DestroyRetVal>(LockR);
389 State = resolvePossiblyDestroyedMutex(State, LockR, sym);
391 const struct LockState *LState = State->get<LockMap>(LockR);
392 if (!LState || LState->isDestroyed()) {
393 State = State->set<LockMap>(LockR, LockState::getUnlocked());
400 if (LState->isLocked()) {
401 Message =
"This lock is still being held";
403 Message =
"This lock has already been initialized";
407 BT_initlock.reset(
new BugType(
this,
"Init invalid lock",
412 auto Report = llvm::make_unique<BugReport>(*BT_initlock, Message, N);
420 BT_destroylock.reset(
new BugType(
this,
"Use destroyed lock",
425 auto Report = llvm::make_unique<BugReport>(
426 *BT_destroylock,
"This lock has already been destroyed", N);
431 void PthreadLockChecker::checkDeadSymbols(
SymbolReaper &SymReaper,
437 DestroyRetValTy TrackedSymbols = State->get<DestroyRetVal>();
438 for (DestroyRetValTy::iterator
I = TrackedSymbols.begin(),
439 E = TrackedSymbols.end();
442 const MemRegion *lockR =
I->first;
443 bool IsSymDead = SymReaper.
isDead(Sym);
446 State = resolvePossiblyDestroyedMutex(State, lockR, &Sym);
bool isConstrainedFalse() const
Return true if the constraint is perfectly constrained to 'false'.
MemRegion - The root abstract class for all memory regions.
ExplodedNode * generateErrorNode(ProgramStateRef State=nullptr, const ProgramPointTag *Tag=nullptr)
Generate a transition to a node that will be used to report an error.
Expr * getArg(unsigned Arg)
getArg - Return the specified argument.
bool operator==(CanQual< T > x, CanQual< U > y)
ExplodedNode * addTransition(ProgramStateRef State=nullptr, const ProgramPointTag *Tag=nullptr)
Generates a new transition in the program state graph (ExplodedGraph).
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
bool isUnknownOrUndef() const
detail::InMemoryDirectory::const_iterator I
#define REGISTER_MAP_WITH_PROGRAMSTATE(Name, Key, Value)
Declares an immutable map of type NameTy, suitable for placement into the ProgramState.
bool isDead(SymbolRef sym) const
Returns whether or not a symbol has been confirmed dead.
const ProgramStateRef & getState() const
void emitReport(std::unique_ptr< BugReport > R)
Emit the diagnostics report.
CHECKER * registerChecker()
Used to register checkers.
SVal - This represents a symbolic expression, which can be either an L-value or an R-value...
A class responsible for cleaning up unused symbols.
detail::InMemoryDirectory::const_iterator E
const MemRegion * getAsRegion() const
X
Add a minimal nested name specifier fixit hint to allow lookup of a tag name from an outer enclosing ...
#define REGISTER_LIST_WITH_PROGRAMSTATE(Name, Elem)
Declares an immutable list of type NameTy, suitable for placement into the ProgramState.
SymbolRef getAsSymbol(bool IncludeBaseRegions=false) const
If this SVal wraps a symbol return that SymbolRef.
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]).
T castAs() const
Convert to the specified SVal type, asserting that this SVal is of the desired type.
const LocationContext * getLocationContext() const
SVal getSVal(const Stmt *S) const
Get the value of arbitrary expressions at this point in the path.