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 {
85 const char *NL,
const char *Sep)
const override;
88 bool isTryLock,
enum LockingSemantics semantics)
const;
92 enum LockingSemantics semantics)
const;
110 void PthreadLockChecker::checkPostStmt(
const CallExpr *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")
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")
141 else if (FName ==
"pthread_mutex_destroy")
142 DestroyLock(C, CE, C.
getSVal(CE->
getArg(0)), PthreadSemantics);
143 else if (FName ==
"lck_mtx_destroy")
145 else if (FName ==
"pthread_mutex_init")
161 const LockState *lstate = state->get<LockMap>(lockR);
165 assert(lstate->isUntouchedAndPossiblyDestroyed() ||
166 lstate->isUnlockedAndPossiblyDestroyed());
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);
218 SVal lock,
bool isTryLock,
219 enum LockingSemantics semantics)
const {
226 const SymbolRef *sym = state->get<DestroyRetVal>(lockR);
228 state = resolvePossiblyDestroyedMutex(state, lockR, sym);
236 if (
const LockState *LState = state->get<LockMap>(lockR)) {
237 if (LState->isLocked()) {
239 BT_doublelock.reset(
new BugType(
this,
"Double locking",
244 auto report = llvm::make_unique<BugReport>(
245 *BT_doublelock,
"This lock has already been acquired", N);
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);
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());
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",
309 auto Report = llvm::make_unique<BugReport>(
310 *BT_doubleunlock,
"This lock has already been unlocked", N);
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"));
332 auto report = llvm::make_unique<BugReport>(
333 *BT_lor,
"This was not the most recently acquired lock. Possible " 334 "lock order reversal", N);
340 state = state->set<LockSet>(LS.getTail());
343 state = state->set<LockMap>(lockR, LockState::getUnlocked());
349 enum LockingSemantics semantics)
const {
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()) {
368 State = State->remove<LockMap>(LockR);
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());
383 if (!LState || LState->isUnlocked()) {
384 State = State->set<LockMap>(LockR, LockState::getDestroyed());
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",
403 auto Report = llvm::make_unique<BugReport>(*BT_destroylock, Message, N);
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());
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",
442 auto Report = llvm::make_unique<BugReport>(*BT_initlock, Message, N);
450 BT_destroylock.reset(
new BugType(
this,
"Use destroyed lock",
455 auto Report = llvm::make_unique<BugReport>(
456 *BT_destroylock,
"This lock has already been destroyed", N);
461 void PthreadLockChecker::checkDeadSymbols(
SymbolReaper &SymReaper,
467 DestroyRetValTy TrackedSymbols = State->get<DestroyRetVal>();
468 for (DestroyRetValTy::iterator I = TrackedSymbols.begin(),
469 E = TrackedSymbols.end();
473 bool IsSymDead = SymReaper.
isDead(Sym);
476 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)
unsigned getNumArgs() const
getNumArgs - Return the number of actual arguments to this call.
ExplodedNode * addTransition(ProgramStateRef State=nullptr, const ProgramPointTag *Tag=nullptr)
Generates a new transition in the program state graph (ExplodedGraph).
bool isDead(SymbolRef sym) const
Returns whether or not a symbol has been confirmed dead.
SVal getSVal(const Stmt *S) const
Get the value of arbitrary expressions at this point in the path.
StringRef getCalleeName(const FunctionDecl *FunDecl) const
Get the name of the called function (path-sensitive).
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
SymbolRef getAsSymbol(bool IncludeBaseRegions=false) const
If this SVal wraps a symbol return that SymbolRef.
#define REGISTER_MAP_WITH_PROGRAMSTATE(Name, Key, Value)
Declares an immutable map of type NameTy, suitable for placement into the ProgramState.
CHECKER * registerChecker(AT... Args)
Used to register checkers.
void emitReport(std::unique_ptr< BugReport > R)
Emit the diagnostics report.
const MemRegion * getAsRegion() const
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.
Dataflow Directional Tag Classes.
T castAs() const
Convert to the specified SVal type, asserting that this SVal is of the desired type.
const ProgramStateRef & getState() 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.
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]).
bool isUnknownOrUndef() const