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,
121 if (FName ==
"pthread_mutex_lock" ||
122 FName ==
"pthread_rwlock_rdlock" ||
123 FName ==
"pthread_rwlock_wrlock")
124 AcquireLock(C, CE, state->getSVal(CE->
getArg(0), LCtx),
125 false, PthreadSemantics);
126 else if (FName ==
"lck_mtx_lock" ||
127 FName ==
"lck_rw_lock_exclusive" ||
128 FName ==
"lck_rw_lock_shared")
129 AcquireLock(C, CE, state->getSVal(CE->
getArg(0), LCtx),
130 false, XNUSemantics);
131 else if (FName ==
"pthread_mutex_trylock" ||
132 FName ==
"pthread_rwlock_tryrdlock" ||
133 FName ==
"pthread_rwlock_trywrlock")
134 AcquireLock(C, CE, state->getSVal(CE->
getArg(0), LCtx),
135 true, PthreadSemantics);
136 else if (FName ==
"lck_mtx_try_lock" ||
137 FName ==
"lck_rw_try_lock_exclusive" ||
138 FName ==
"lck_rw_try_lock_shared")
139 AcquireLock(C, CE, state->getSVal(CE->
getArg(0), LCtx),
141 else if (FName ==
"pthread_mutex_unlock" ||
142 FName ==
"pthread_rwlock_unlock" ||
143 FName ==
"lck_mtx_unlock" ||
144 FName ==
"lck_rw_done")
145 ReleaseLock(C, CE, state->getSVal(CE->
getArg(0), LCtx));
146 else if (FName ==
"pthread_mutex_destroy")
147 DestroyLock(C, CE, state->getSVal(CE->
getArg(0), LCtx), PthreadSemantics);
148 else if (FName ==
"lck_mtx_destroy")
149 DestroyLock(C, CE, state->getSVal(CE->
getArg(0), LCtx), XNUSemantics);
150 else if (FName ==
"pthread_mutex_init")
151 InitLock(C, CE, state->getSVal(CE->
getArg(0), LCtx));
166 const LockState *lstate = state->get<LockMap>(lockR);
170 assert(lstate->isUntouchedAndPossiblyDestroyed() ||
171 lstate->isUnlockedAndPossiblyDestroyed());
176 if (lstate->isUntouchedAndPossiblyDestroyed())
177 state = state->remove<LockMap>(lockR);
178 else if (lstate->isUnlockedAndPossiblyDestroyed())
179 state = state->set<LockMap>(lockR, LockState::getUnlocked());
181 state = state->set<LockMap>(lockR, LockState::getDestroyed());
185 state = state->remove<DestroyRetVal>(lockR);
190 const char *NL,
const char *Sep)
const {
191 LockMapTy LM = State->get<LockMap>();
193 Out << Sep <<
"Mutex states:" << NL;
195 I.first->dumpToStream(Out);
196 if (I.second.isLocked())
198 else if (I.second.isUnlocked())
200 else if (I.second.isDestroyed())
201 Out <<
": destroyed";
202 else if (I.second.isUntouchedAndPossiblyDestroyed())
203 Out <<
": not tracked, possibly destroyed";
204 else if (I.second.isUnlockedAndPossiblyDestroyed())
205 Out <<
": unlocked, possibly destroyed";
210 LockSetTy LS = State->get<LockSet>();
212 Out << Sep <<
"Mutex lock order:" << NL;
214 I->dumpToStream(Out);
223 SVal lock,
bool isTryLock,
224 enum LockingSemantics semantics)
const {
231 const SymbolRef *sym = state->get<DestroyRetVal>(lockR);
233 state = resolvePossiblyDestroyedMutex(state, lockR, sym);
241 if (
const LockState *LState = state->get<LockMap>(lockR)) {
242 if (LState->isLocked()) {
244 BT_doublelock.reset(
new BugType(
this,
"Double locking",
249 auto report = llvm::make_unique<BugReport>(
250 *BT_doublelock,
"This lock has already been acquired", N);
254 }
else if (LState->isDestroyed()) {
255 reportUseDestroyedBug(C, CE);
265 case PthreadSemantics:
266 std::tie(lockFail, lockSucc) = state->assume(retVal);
269 std::tie(lockSucc, lockFail) = state->assume(retVal);
272 llvm_unreachable(
"Unknown tryLock locking semantics");
274 assert(lockFail && lockSucc);
277 }
else if (semantics == PthreadSemantics) {
279 lockSucc = state->assume(retVal,
false);
284 assert((semantics == XNUSemantics) &&
"Unknown locking semantics");
289 lockSucc = lockSucc->add<LockSet>(lockR);
290 lockSucc = lockSucc->set<LockMap>(lockR, LockState::getLocked());
302 const SymbolRef *sym = state->get<DestroyRetVal>(lockR);
304 state = resolvePossiblyDestroyedMutex(state, lockR, sym);
306 if (
const LockState *LState = state->get<LockMap>(lockR)) {
307 if (LState->isUnlocked()) {
308 if (!BT_doubleunlock)
309 BT_doubleunlock.reset(
new BugType(
this,
"Double unlocking",
314 auto Report = llvm::make_unique<BugReport>(
315 *BT_doubleunlock,
"This lock has already been unlocked", N);
319 }
else if (LState->isDestroyed()) {
320 reportUseDestroyedBug(C, CE);
325 LockSetTy LS = state->get<LockSet>();
330 const MemRegion *firstLockR = LS.getHead();
331 if (firstLockR != lockR) {
333 BT_lor.reset(
new BugType(
this,
"Lock order reversal",
"Lock checker"));
337 auto report = llvm::make_unique<BugReport>(
338 *BT_lor,
"This was not the most recently acquired lock. Possible " 339 "lock order reversal", N);
345 state = state->set<LockSet>(LS.getTail());
348 state = state->set<LockMap>(lockR, LockState::getUnlocked());
354 enum LockingSemantics semantics)
const {
362 const SymbolRef *sym = State->get<DestroyRetVal>(LockR);
364 State = resolvePossiblyDestroyedMutex(State, LockR, sym);
366 const LockState *LState = State->get<LockMap>(LockR);
369 if (semantics == PthreadSemantics) {
370 if (!LState || LState->isUnlocked()) {
373 State = State->remove<LockMap>(LockR);
377 State = State->set<DestroyRetVal>(LockR, sym);
378 if (LState && LState->isUnlocked())
379 State = State->set<LockMap>(
380 LockR, LockState::getUnlockedAndPossiblyDestroyed());
382 State = State->set<LockMap>(
383 LockR, LockState::getUntouchedAndPossiblyDestroyed());
388 if (!LState || LState->isUnlocked()) {
389 State = State->set<LockMap>(LockR, LockState::getDestroyed());
396 if (LState->isLocked()) {
397 Message =
"This lock is still locked";
399 Message =
"This lock has already been destroyed";
403 BT_destroylock.reset(
new BugType(
this,
"Destroy invalid lock",
408 auto Report = llvm::make_unique<BugReport>(*BT_destroylock, Message, N);
422 const SymbolRef *sym = State->get<DestroyRetVal>(LockR);
424 State = resolvePossiblyDestroyedMutex(State, LockR, sym);
426 const struct LockState *LState = State->get<LockMap>(LockR);
427 if (!LState || LState->isDestroyed()) {
428 State = State->set<LockMap>(LockR, LockState::getUnlocked());
435 if (LState->isLocked()) {
436 Message =
"This lock is still being held";
438 Message =
"This lock has already been initialized";
442 BT_initlock.reset(
new BugType(
this,
"Init invalid lock",
447 auto Report = llvm::make_unique<BugReport>(*BT_initlock, Message, N);
455 BT_destroylock.reset(
new BugType(
this,
"Use destroyed lock",
460 auto Report = llvm::make_unique<BugReport>(
461 *BT_destroylock,
"This lock has already been destroyed", N);
466 void PthreadLockChecker::checkDeadSymbols(
SymbolReaper &SymReaper,
472 DestroyRetValTy TrackedSymbols = State->get<DestroyRetVal>();
473 for (DestroyRetValTy::iterator I = TrackedSymbols.begin(),
474 E = TrackedSymbols.end();
478 bool IsSymDead = SymReaper.
isDead(Sym);
481 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.
void emitReport(std::unique_ptr< BugReport > R)
Emit the diagnostics report.
CHECKER * registerChecker()
Used to register checkers.
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]).
const LocationContext * getLocationContext() const
bool isUnknownOrUndef() const