blob: dab29be1c8fb1d18139e891b15b6bf9a70542926 [file] [log] [blame]
Jordy Rosed9c52212011-07-19 20:21:41 +00001//===--- PthreadLockChecker.cpp - Check for locking problems ---*- C++ -*--===//
Ted Kremenekd48568f2009-11-12 06:17:47 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Jordy Rosea39e10f2011-07-19 20:31:42 +000010// This defines PthreadLockChecker, a simple lock -> unlock checker.
11// Also handles XNU locks, which behave similarly enough to share code.
Ted Kremenekd48568f2009-11-12 06:17:47 +000012//
13//===----------------------------------------------------------------------===//
14
Argyrios Kyrtzidis2d3905f2011-02-15 21:25:03 +000015#include "ClangSACheckers.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000016#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +000017#include "clang/StaticAnalyzer/Core/Checker.h"
Argyrios Kyrtzidis507ff532011-02-17 21:39:17 +000018#include "clang/StaticAnalyzer/Core/CheckerManager.h"
Argyrios Kyrtzidisdff865d2011-02-23 01:05:36 +000019#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Ted Kremenek001fd5b2011-08-15 22:09:50 +000020#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
Ted Kremenekd48568f2009-11-12 06:17:47 +000021
22using namespace clang;
Ted Kremenek98857c92010-12-23 07:20:52 +000023using namespace ento;
Ted Kremenekd48568f2009-11-12 06:17:47 +000024
25namespace {
Jordan Rose7fcaa142014-04-01 03:40:47 +000026
27struct LockState {
Artem Dergachev77915932017-05-29 14:51:39 +000028 enum Kind {
29 Destroyed,
30 Locked,
31 Unlocked,
32 UntouchedAndPossiblyDestroyed,
33 UnlockedAndPossiblyDestroyed
34 } K;
Jordan Rose7fcaa142014-04-01 03:40:47 +000035
36private:
37 LockState(Kind K) : K(K) {}
38
39public:
Eugene Zelenkod4304d22015-11-04 21:37:17 +000040 static LockState getLocked() { return LockState(Locked); }
41 static LockState getUnlocked() { return LockState(Unlocked); }
42 static LockState getDestroyed() { return LockState(Destroyed); }
Artem Dergachev77915932017-05-29 14:51:39 +000043 static LockState getUntouchedAndPossiblyDestroyed() {
44 return LockState(UntouchedAndPossiblyDestroyed);
45 }
46 static LockState getUnlockedAndPossiblyDestroyed() {
47 return LockState(UnlockedAndPossiblyDestroyed);
48 }
Jordan Rose7fcaa142014-04-01 03:40:47 +000049
50 bool operator==(const LockState &X) const {
51 return K == X.K;
52 }
53
54 bool isLocked() const { return K == Locked; }
55 bool isUnlocked() const { return K == Unlocked; }
56 bool isDestroyed() const { return K == Destroyed; }
Artem Dergachev77915932017-05-29 14:51:39 +000057 bool isUntouchedAndPossiblyDestroyed() const {
58 return K == UntouchedAndPossiblyDestroyed;
59 }
60 bool isUnlockedAndPossiblyDestroyed() const {
61 return K == UnlockedAndPossiblyDestroyed;
62 }
Jordan Rose7fcaa142014-04-01 03:40:47 +000063
64 void Profile(llvm::FoldingSetNodeID &ID) const {
65 ID.AddInteger(K);
66 }
67};
68
Artem Dergachev77915932017-05-29 14:51:39 +000069class PthreadLockChecker
70 : public Checker<check::PostStmt<CallExpr>, check::DeadSymbols> {
Ahmed Charlesb8984322014-03-07 20:03:18 +000071 mutable std::unique_ptr<BugType> BT_doublelock;
Jordan Rose0696bb42014-04-01 03:40:38 +000072 mutable std::unique_ptr<BugType> BT_doubleunlock;
Jordan Rose7fcaa142014-04-01 03:40:47 +000073 mutable std::unique_ptr<BugType> BT_destroylock;
Jordan Rose3a176ed2014-04-01 03:40:53 +000074 mutable std::unique_ptr<BugType> BT_initlock;
Ahmed Charlesb8984322014-03-07 20:03:18 +000075 mutable std::unique_ptr<BugType> BT_lor;
Jordy Rosed9c52212011-07-19 20:21:41 +000076 enum LockingSemantics {
77 NotApplicable = 0,
78 PthreadSemantics,
79 XNUSemantics
80 };
Ted Kremenekd48568f2009-11-12 06:17:47 +000081public:
Argyrios Kyrtzidisdff865d2011-02-23 01:05:36 +000082 void checkPostStmt(const CallExpr *CE, CheckerContext &C) const;
Artem Dergachev77915932017-05-29 14:51:39 +000083 void checkDeadSymbols(SymbolReaper &SymReaper, CheckerContext &C) const;
Artem Dergachev8e320082017-10-10 11:49:09 +000084 void printState(raw_ostream &Out, ProgramStateRef State,
85 const char *NL, const char *Sep) const override;
Ted Kremenek3a0678e2015-09-08 03:50:52 +000086
Jordy Rosed9c52212011-07-19 20:21:41 +000087 void AcquireLock(CheckerContext &C, const CallExpr *CE, SVal lock,
88 bool isTryLock, enum LockingSemantics semantics) const;
Ted Kremenek3a0678e2015-09-08 03:50:52 +000089
Jordy Rosed9c52212011-07-19 20:21:41 +000090 void ReleaseLock(CheckerContext &C, const CallExpr *CE, SVal lock) const;
Artem Dergachev77915932017-05-29 14:51:39 +000091 void DestroyLock(CheckerContext &C, const CallExpr *CE, SVal Lock,
92 enum LockingSemantics semantics) const;
Jordan Rose3a176ed2014-04-01 03:40:53 +000093 void InitLock(CheckerContext &C, const CallExpr *CE, SVal Lock) const;
Jordan Rose7fcaa142014-04-01 03:40:47 +000094 void reportUseDestroyedBug(CheckerContext &C, const CallExpr *CE) const;
Artem Dergachev77915932017-05-29 14:51:39 +000095 ProgramStateRef resolvePossiblyDestroyedMutex(ProgramStateRef state,
96 const MemRegion *lockR,
97 const SymbolRef *sym) const;
Ted Kremenekd48568f2009-11-12 06:17:47 +000098};
99} // end anonymous namespace
100
Artem Dergachev77915932017-05-29 14:51:39 +0000101// A stack of locks for tracking lock-unlock order.
Jordan Rose0c153cb2012-11-02 01:54:06 +0000102REGISTER_LIST_WITH_PROGRAMSTATE(LockSet, const MemRegion *)
Ted Kremenekd48568f2009-11-12 06:17:47 +0000103
Artem Dergachev77915932017-05-29 14:51:39 +0000104// An entry for tracking lock states.
Jordan Rose7fcaa142014-04-01 03:40:47 +0000105REGISTER_MAP_WITH_PROGRAMSTATE(LockMap, const MemRegion *, LockState)
Ted Kremenekd48568f2009-11-12 06:17:47 +0000106
Artem Dergachev77915932017-05-29 14:51:39 +0000107// Return values for unresolved calls to pthread_mutex_destroy().
108REGISTER_MAP_WITH_PROGRAMSTATE(DestroyRetVal, const MemRegion *, SymbolRef)
109
Argyrios Kyrtzidisdff865d2011-02-23 01:05:36 +0000110void PthreadLockChecker::checkPostStmt(const CallExpr *CE,
111 CheckerContext &C) const {
Ted Kremenek49b1e382012-01-26 21:29:00 +0000112 ProgramStateRef state = C.getState();
Ted Kremenek632e3b72012-01-06 22:09:28 +0000113 const LocationContext *LCtx = C.getLocationContext();
Anna Zaksc6aa5312011-12-01 05:57:37 +0000114 StringRef FName = C.getCalleeName(CE);
115 if (FName.empty())
Ted Kremenekd48568f2009-11-12 06:17:47 +0000116 return;
Jordy Rosea39e10f2011-07-19 20:31:42 +0000117
Jordan Rose7fcaa142014-04-01 03:40:47 +0000118 if (CE->getNumArgs() != 1 && CE->getNumArgs() != 2)
Jordy Rosed9c52212011-07-19 20:21:41 +0000119 return;
Jordy Rosea39e10f2011-07-19 20:31:42 +0000120
Jordy Rosed9c52212011-07-19 20:21:41 +0000121 if (FName == "pthread_mutex_lock" ||
122 FName == "pthread_rwlock_rdlock" ||
123 FName == "pthread_rwlock_wrlock")
Ted Kremenek632e3b72012-01-06 22:09:28 +0000124 AcquireLock(C, CE, state->getSVal(CE->getArg(0), LCtx),
125 false, PthreadSemantics);
Jordy Rosed9c52212011-07-19 20:21:41 +0000126 else if (FName == "lck_mtx_lock" ||
127 FName == "lck_rw_lock_exclusive" ||
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000128 FName == "lck_rw_lock_shared")
Ted Kremenek632e3b72012-01-06 22:09:28 +0000129 AcquireLock(C, CE, state->getSVal(CE->getArg(0), LCtx),
130 false, XNUSemantics);
Jordy Rosed9c52212011-07-19 20:21:41 +0000131 else if (FName == "pthread_mutex_trylock" ||
132 FName == "pthread_rwlock_tryrdlock" ||
Ted Kremenek115c3f7a2014-01-17 16:06:43 +0000133 FName == "pthread_rwlock_trywrlock")
Ted Kremenek632e3b72012-01-06 22:09:28 +0000134 AcquireLock(C, CE, state->getSVal(CE->getArg(0), LCtx),
135 true, PthreadSemantics);
Jordy Rosed9c52212011-07-19 20:21:41 +0000136 else if (FName == "lck_mtx_try_lock" ||
137 FName == "lck_rw_try_lock_exclusive" ||
138 FName == "lck_rw_try_lock_shared")
Ted Kremenek632e3b72012-01-06 22:09:28 +0000139 AcquireLock(C, CE, state->getSVal(CE->getArg(0), LCtx),
140 true, XNUSemantics);
Jordy Rosed9c52212011-07-19 20:21:41 +0000141 else if (FName == "pthread_mutex_unlock" ||
142 FName == "pthread_rwlock_unlock" ||
143 FName == "lck_mtx_unlock" ||
144 FName == "lck_rw_done")
Ted Kremenek632e3b72012-01-06 22:09:28 +0000145 ReleaseLock(C, CE, state->getSVal(CE->getArg(0), LCtx));
Artem Dergachev77915932017-05-29 14:51:39 +0000146 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);
Jordan Rose3a176ed2014-04-01 03:40:53 +0000150 else if (FName == "pthread_mutex_init")
151 InitLock(C, CE, state->getSVal(CE->getArg(0), LCtx));
Ted Kremenekd48568f2009-11-12 06:17:47 +0000152}
153
Artem Dergachev77915932017-05-29 14:51:39 +0000154// When a lock is destroyed, in some semantics(like PthreadSemantics) we are not
155// sure if the destroy call has succeeded or failed, and the lock enters one of
156// the 'possibly destroyed' state. There is a short time frame for the
157// programmer to check the return value to see if the lock was successfully
158// destroyed. Before we model the next operation over that lock, we call this
159// function to see if the return value was checked by now and set the lock state
160// - either to destroyed state or back to its previous state.
161
162// In PthreadSemantics, pthread_mutex_destroy() returns zero if the lock is
163// successfully destroyed and it returns a non-zero value otherwise.
164ProgramStateRef PthreadLockChecker::resolvePossiblyDestroyedMutex(
165 ProgramStateRef state, const MemRegion *lockR, const SymbolRef *sym) const {
166 const LockState *lstate = state->get<LockMap>(lockR);
167 // Existence in DestroyRetVal ensures existence in LockMap.
168 // Existence in Destroyed also ensures that the lock state for lockR is either
169 // UntouchedAndPossiblyDestroyed or UnlockedAndPossiblyDestroyed.
170 assert(lstate->isUntouchedAndPossiblyDestroyed() ||
171 lstate->isUnlockedAndPossiblyDestroyed());
172
173 ConstraintManager &CMgr = state->getConstraintManager();
174 ConditionTruthVal retZero = CMgr.isNull(state, *sym);
175 if (retZero.isConstrainedFalse()) {
176 if (lstate->isUntouchedAndPossiblyDestroyed())
177 state = state->remove<LockMap>(lockR);
178 else if (lstate->isUnlockedAndPossiblyDestroyed())
179 state = state->set<LockMap>(lockR, LockState::getUnlocked());
180 } else
181 state = state->set<LockMap>(lockR, LockState::getDestroyed());
182
183 // Removing the map entry (lockR, sym) from DestroyRetVal as the lock state is
184 // now resolved.
185 state = state->remove<DestroyRetVal>(lockR);
186 return state;
187}
188
Artem Dergachev8e320082017-10-10 11:49:09 +0000189void PthreadLockChecker::printState(raw_ostream &Out, ProgramStateRef State,
190 const char *NL, const char *Sep) const {
191 LockMapTy LM = State->get<LockMap>();
192 if (!LM.isEmpty()) {
193 Out << Sep << "Mutex states:" << NL;
194 for (auto I : LM) {
195 I.first->dumpToStream(Out);
196 if (I.second.isLocked())
197 Out << ": locked";
198 else if (I.second.isUnlocked())
199 Out << ": unlocked";
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";
206 Out << NL;
207 }
208 }
209
210 LockSetTy LS = State->get<LockSet>();
211 if (!LS.isEmpty()) {
212 Out << Sep << "Mutex lock order:" << NL;
213 for (auto I: LS) {
214 I->dumpToStream(Out);
215 Out << NL;
216 }
217 }
218
219 // TODO: Dump destroyed mutex symbols?
220}
221
Ted Kremenekd48568f2009-11-12 06:17:47 +0000222void PthreadLockChecker::AcquireLock(CheckerContext &C, const CallExpr *CE,
Jordy Rosed9c52212011-07-19 20:21:41 +0000223 SVal lock, bool isTryLock,
224 enum LockingSemantics semantics) const {
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000225
Ted Kremenekd48568f2009-11-12 06:17:47 +0000226 const MemRegion *lockR = lock.getAsRegion();
227 if (!lockR)
228 return;
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000229
Ted Kremenek49b1e382012-01-26 21:29:00 +0000230 ProgramStateRef state = C.getState();
Artem Dergachev77915932017-05-29 14:51:39 +0000231 const SymbolRef *sym = state->get<DestroyRetVal>(lockR);
232 if (sym)
233 state = resolvePossiblyDestroyedMutex(state, lockR, sym);
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000234
Ted Kremenek632e3b72012-01-06 22:09:28 +0000235 SVal X = state->getSVal(CE, C.getLocationContext());
Ted Kremenekd48568f2009-11-12 06:17:47 +0000236 if (X.isUnknownOrUndef())
237 return;
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000238
David Blaikie2fdacbc2013-02-20 05:52:05 +0000239 DefinedSVal retVal = X.castAs<DefinedSVal>();
Jordy Rosed9c52212011-07-19 20:21:41 +0000240
Jordan Rose7fcaa142014-04-01 03:40:47 +0000241 if (const LockState *LState = state->get<LockMap>(lockR)) {
242 if (LState->isLocked()) {
243 if (!BT_doublelock)
244 BT_doublelock.reset(new BugType(this, "Double locking",
245 "Lock checker"));
Devin Coughline39bd402015-09-16 22:03:05 +0000246 ExplodedNode *N = C.generateErrorNode();
Jordan Rose7fcaa142014-04-01 03:40:47 +0000247 if (!N)
248 return;
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000249 auto report = llvm::make_unique<BugReport>(
250 *BT_doublelock, "This lock has already been acquired", N);
Jordan Rose7fcaa142014-04-01 03:40:47 +0000251 report->addRange(CE->getArg(0)->getSourceRange());
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000252 C.emitReport(std::move(report));
Jordy Rosed9c52212011-07-19 20:21:41 +0000253 return;
Jordan Rose7fcaa142014-04-01 03:40:47 +0000254 } else if (LState->isDestroyed()) {
255 reportUseDestroyedBug(C, CE);
256 return;
257 }
Ted Kremenekd48568f2009-11-12 06:17:47 +0000258 }
Jordy Rosed9c52212011-07-19 20:21:41 +0000259
Ted Kremenek49b1e382012-01-26 21:29:00 +0000260 ProgramStateRef lockSucc = state;
Jordy Rosed9c52212011-07-19 20:21:41 +0000261 if (isTryLock) {
262 // Bifurcate the state, and allow a mode where the lock acquisition fails.
Ted Kremenek49b1e382012-01-26 21:29:00 +0000263 ProgramStateRef lockFail;
Jordy Rosed9c52212011-07-19 20:21:41 +0000264 switch (semantics) {
265 case PthreadSemantics:
Benjamin Kramer867ea1d2014-03-02 13:01:17 +0000266 std::tie(lockFail, lockSucc) = state->assume(retVal);
Jordy Rosed9c52212011-07-19 20:21:41 +0000267 break;
268 case XNUSemantics:
Benjamin Kramer867ea1d2014-03-02 13:01:17 +0000269 std::tie(lockSucc, lockFail) = state->assume(retVal);
Jordy Rosed9c52212011-07-19 20:21:41 +0000270 break;
271 default:
272 llvm_unreachable("Unknown tryLock locking semantics");
Jordy Rosed9c52212011-07-19 20:21:41 +0000273 }
274 assert(lockFail && lockSucc);
Anna Zaksda4c8d62011-10-26 21:06:34 +0000275 C.addTransition(lockFail);
Jordy Rosed9c52212011-07-19 20:21:41 +0000276
277 } else if (semantics == PthreadSemantics) {
278 // Assume that the return value was 0.
Ted Kremenekc5bea1e2010-12-01 22:16:56 +0000279 lockSucc = state->assume(retVal, false);
Ted Kremenekd48568f2009-11-12 06:17:47 +0000280 assert(lockSucc);
Jordy Rosed9c52212011-07-19 20:21:41 +0000281
282 } else {
283 // XNU locking semantics return void on non-try locks
284 assert((semantics == XNUSemantics) && "Unknown locking semantics");
285 lockSucc = state;
Ted Kremenekd48568f2009-11-12 06:17:47 +0000286 }
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000287
288 // Record that the lock was acquired.
Ted Kremenekd48568f2009-11-12 06:17:47 +0000289 lockSucc = lockSucc->add<LockSet>(lockR);
Jordan Rose7fcaa142014-04-01 03:40:47 +0000290 lockSucc = lockSucc->set<LockMap>(lockR, LockState::getLocked());
Anna Zaksda4c8d62011-10-26 21:06:34 +0000291 C.addTransition(lockSucc);
Ted Kremenekd48568f2009-11-12 06:17:47 +0000292}
293
294void PthreadLockChecker::ReleaseLock(CheckerContext &C, const CallExpr *CE,
Argyrios Kyrtzidisdff865d2011-02-23 01:05:36 +0000295 SVal lock) const {
Ted Kremenekd48568f2009-11-12 06:17:47 +0000296
297 const MemRegion *lockR = lock.getAsRegion();
298 if (!lockR)
299 return;
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000300
Ted Kremenek49b1e382012-01-26 21:29:00 +0000301 ProgramStateRef state = C.getState();
Artem Dergachev77915932017-05-29 14:51:39 +0000302 const SymbolRef *sym = state->get<DestroyRetVal>(lockR);
303 if (sym)
304 state = resolvePossiblyDestroyedMutex(state, lockR, sym);
Ted Kremenekd48568f2009-11-12 06:17:47 +0000305
Jordan Rose7fcaa142014-04-01 03:40:47 +0000306 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",
310 "Lock checker"));
Devin Coughline39bd402015-09-16 22:03:05 +0000311 ExplodedNode *N = C.generateErrorNode();
Jordan Rose7fcaa142014-04-01 03:40:47 +0000312 if (!N)
313 return;
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000314 auto Report = llvm::make_unique<BugReport>(
315 *BT_doubleunlock, "This lock has already been unlocked", N);
Jordan Rose7fcaa142014-04-01 03:40:47 +0000316 Report->addRange(CE->getArg(0)->getSourceRange());
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000317 C.emitReport(std::move(Report));
Jordy Rosed9c52212011-07-19 20:21:41 +0000318 return;
Jordan Rose7fcaa142014-04-01 03:40:47 +0000319 } else if (LState->isDestroyed()) {
320 reportUseDestroyedBug(C, CE);
321 return;
322 }
Jordy Rosed9c52212011-07-19 20:21:41 +0000323 }
324
Jordan Rose0696bb42014-04-01 03:40:38 +0000325 LockSetTy LS = state->get<LockSet>();
326
327 // FIXME: Better analysis requires IPA for wrappers.
328
329 if (!LS.isEmpty()) {
330 const MemRegion *firstLockR = LS.getHead();
331 if (firstLockR != lockR) {
332 if (!BT_lor)
333 BT_lor.reset(new BugType(this, "Lock order reversal", "Lock checker"));
Devin Coughline39bd402015-09-16 22:03:05 +0000334 ExplodedNode *N = C.generateErrorNode();
Jordan Rose0696bb42014-04-01 03:40:38 +0000335 if (!N)
336 return;
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000337 auto report = llvm::make_unique<BugReport>(
338 *BT_lor, "This was not the most recently acquired lock. Possible "
339 "lock order reversal", N);
Jordan Rose0696bb42014-04-01 03:40:38 +0000340 report->addRange(CE->getArg(0)->getSourceRange());
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000341 C.emitReport(std::move(report));
Jordan Rose0696bb42014-04-01 03:40:38 +0000342 return;
343 }
Jordan Rose7fcaa142014-04-01 03:40:47 +0000344 // Record that the lock was released.
345 state = state->set<LockSet>(LS.getTail());
Jordan Rose0696bb42014-04-01 03:40:38 +0000346 }
347
Jordan Rose7fcaa142014-04-01 03:40:47 +0000348 state = state->set<LockMap>(lockR, LockState::getUnlocked());
Anna Zaksda4c8d62011-10-26 21:06:34 +0000349 C.addTransition(state);
Ted Kremenekd48568f2009-11-12 06:17:47 +0000350}
Argyrios Kyrtzidisdff865d2011-02-23 01:05:36 +0000351
Jordan Rose7fcaa142014-04-01 03:40:47 +0000352void PthreadLockChecker::DestroyLock(CheckerContext &C, const CallExpr *CE,
Artem Dergachev77915932017-05-29 14:51:39 +0000353 SVal Lock,
354 enum LockingSemantics semantics) const {
Jordan Rose7fcaa142014-04-01 03:40:47 +0000355
356 const MemRegion *LockR = Lock.getAsRegion();
357 if (!LockR)
358 return;
359
360 ProgramStateRef State = C.getState();
361
Artem Dergachev77915932017-05-29 14:51:39 +0000362 const SymbolRef *sym = State->get<DestroyRetVal>(LockR);
363 if (sym)
364 State = resolvePossiblyDestroyedMutex(State, LockR, sym);
Jordan Rose7fcaa142014-04-01 03:40:47 +0000365
Artem Dergachev77915932017-05-29 14:51:39 +0000366 const LockState *LState = State->get<LockMap>(LockR);
367 // Checking the return value of the destroy method only in the case of
368 // PthreadSemantics
369 if (semantics == PthreadSemantics) {
370 if (!LState || LState->isUnlocked()) {
371 SymbolRef sym = C.getSVal(CE).getAsSymbol();
372 if (!sym) {
373 State = State->remove<LockMap>(LockR);
374 C.addTransition(State);
375 return;
376 }
377 State = State->set<DestroyRetVal>(LockR, sym);
378 if (LState && LState->isUnlocked())
379 State = State->set<LockMap>(
380 LockR, LockState::getUnlockedAndPossiblyDestroyed());
381 else
382 State = State->set<LockMap>(
383 LockR, LockState::getUntouchedAndPossiblyDestroyed());
384 C.addTransition(State);
385 return;
386 }
387 } else {
388 if (!LState || LState->isUnlocked()) {
389 State = State->set<LockMap>(LockR, LockState::getDestroyed());
390 C.addTransition(State);
391 return;
392 }
393 }
Jordan Rose7fcaa142014-04-01 03:40:47 +0000394 StringRef Message;
395
396 if (LState->isLocked()) {
397 Message = "This lock is still locked";
398 } else {
399 Message = "This lock has already been destroyed";
400 }
401
402 if (!BT_destroylock)
403 BT_destroylock.reset(new BugType(this, "Destroy invalid lock",
404 "Lock checker"));
Devin Coughline39bd402015-09-16 22:03:05 +0000405 ExplodedNode *N = C.generateErrorNode();
Jordan Rose7fcaa142014-04-01 03:40:47 +0000406 if (!N)
407 return;
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000408 auto Report = llvm::make_unique<BugReport>(*BT_destroylock, Message, N);
Jordan Rose7fcaa142014-04-01 03:40:47 +0000409 Report->addRange(CE->getArg(0)->getSourceRange());
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000410 C.emitReport(std::move(Report));
Jordan Rose7fcaa142014-04-01 03:40:47 +0000411}
412
Jordan Rose3a176ed2014-04-01 03:40:53 +0000413void PthreadLockChecker::InitLock(CheckerContext &C, const CallExpr *CE,
414 SVal Lock) const {
415
416 const MemRegion *LockR = Lock.getAsRegion();
417 if (!LockR)
418 return;
419
420 ProgramStateRef State = C.getState();
421
Artem Dergachev77915932017-05-29 14:51:39 +0000422 const SymbolRef *sym = State->get<DestroyRetVal>(LockR);
423 if (sym)
424 State = resolvePossiblyDestroyedMutex(State, LockR, sym);
425
Jordan Rose3a176ed2014-04-01 03:40:53 +0000426 const struct LockState *LState = State->get<LockMap>(LockR);
427 if (!LState || LState->isDestroyed()) {
428 State = State->set<LockMap>(LockR, LockState::getUnlocked());
429 C.addTransition(State);
430 return;
431 }
432
433 StringRef Message;
434
435 if (LState->isLocked()) {
436 Message = "This lock is still being held";
437 } else {
438 Message = "This lock has already been initialized";
439 }
440
441 if (!BT_initlock)
442 BT_initlock.reset(new BugType(this, "Init invalid lock",
443 "Lock checker"));
Devin Coughline39bd402015-09-16 22:03:05 +0000444 ExplodedNode *N = C.generateErrorNode();
Jordan Rose3a176ed2014-04-01 03:40:53 +0000445 if (!N)
446 return;
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000447 auto Report = llvm::make_unique<BugReport>(*BT_initlock, Message, N);
Jordan Rose3a176ed2014-04-01 03:40:53 +0000448 Report->addRange(CE->getArg(0)->getSourceRange());
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000449 C.emitReport(std::move(Report));
Jordan Rose3a176ed2014-04-01 03:40:53 +0000450}
451
Jordan Rose7fcaa142014-04-01 03:40:47 +0000452void PthreadLockChecker::reportUseDestroyedBug(CheckerContext &C,
453 const CallExpr *CE) const {
454 if (!BT_destroylock)
455 BT_destroylock.reset(new BugType(this, "Use destroyed lock",
456 "Lock checker"));
Devin Coughline39bd402015-09-16 22:03:05 +0000457 ExplodedNode *N = C.generateErrorNode();
Jordan Rose7fcaa142014-04-01 03:40:47 +0000458 if (!N)
459 return;
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000460 auto Report = llvm::make_unique<BugReport>(
461 *BT_destroylock, "This lock has already been destroyed", N);
Jordan Rose7fcaa142014-04-01 03:40:47 +0000462 Report->addRange(CE->getArg(0)->getSourceRange());
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000463 C.emitReport(std::move(Report));
Jordan Rose7fcaa142014-04-01 03:40:47 +0000464}
465
Artem Dergachev77915932017-05-29 14:51:39 +0000466void PthreadLockChecker::checkDeadSymbols(SymbolReaper &SymReaper,
467 CheckerContext &C) const {
468 ProgramStateRef State = C.getState();
469
470 // TODO: Clean LockMap when a mutex region dies.
471
472 DestroyRetValTy TrackedSymbols = State->get<DestroyRetVal>();
473 for (DestroyRetValTy::iterator I = TrackedSymbols.begin(),
474 E = TrackedSymbols.end();
475 I != E; ++I) {
476 const SymbolRef Sym = I->second;
477 const MemRegion *lockR = I->first;
478 bool IsSymDead = SymReaper.isDead(Sym);
479 // Remove the dead symbol from the return value symbols map.
480 if (IsSymDead)
481 State = resolvePossiblyDestroyedMutex(State, lockR, &Sym);
482 }
483 C.addTransition(State);
484}
485
Argyrios Kyrtzidisdff865d2011-02-23 01:05:36 +0000486void ento::registerPthreadLockChecker(CheckerManager &mgr) {
487 mgr.registerChecker<PthreadLockChecker>();
488}