blob: 10ab952e069bb428b7687a1cf39622ccf2564588 [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 {
Anna Zaksc6aa5312011-12-01 05:57:37 +0000112 StringRef FName = C.getCalleeName(CE);
113 if (FName.empty())
Ted Kremenekd48568f2009-11-12 06:17:47 +0000114 return;
Jordy Rosea39e10f2011-07-19 20:31:42 +0000115
Jordan Rose7fcaa142014-04-01 03:40:47 +0000116 if (CE->getNumArgs() != 1 && CE->getNumArgs() != 2)
Jordy Rosed9c52212011-07-19 20:21:41 +0000117 return;
Jordy Rosea39e10f2011-07-19 20:31:42 +0000118
Jordy Rosed9c52212011-07-19 20:21:41 +0000119 if (FName == "pthread_mutex_lock" ||
120 FName == "pthread_rwlock_rdlock" ||
121 FName == "pthread_rwlock_wrlock")
George Karpenkovd703ec92018-01-17 20:27:29 +0000122 AcquireLock(C, CE, C.getSVal(CE->getArg(0)), false, PthreadSemantics);
Jordy Rosed9c52212011-07-19 20:21:41 +0000123 else if (FName == "lck_mtx_lock" ||
124 FName == "lck_rw_lock_exclusive" ||
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000125 FName == "lck_rw_lock_shared")
George Karpenkovd703ec92018-01-17 20:27:29 +0000126 AcquireLock(C, CE, C.getSVal(CE->getArg(0)), false, XNUSemantics);
Jordy Rosed9c52212011-07-19 20:21:41 +0000127 else if (FName == "pthread_mutex_trylock" ||
128 FName == "pthread_rwlock_tryrdlock" ||
Ted Kremenek115c3f7a2014-01-17 16:06:43 +0000129 FName == "pthread_rwlock_trywrlock")
George Karpenkovd703ec92018-01-17 20:27:29 +0000130 AcquireLock(C, CE, C.getSVal(CE->getArg(0)),
Ted Kremenek632e3b72012-01-06 22:09:28 +0000131 true, PthreadSemantics);
Jordy Rosed9c52212011-07-19 20:21:41 +0000132 else if (FName == "lck_mtx_try_lock" ||
133 FName == "lck_rw_try_lock_exclusive" ||
134 FName == "lck_rw_try_lock_shared")
George Karpenkovd703ec92018-01-17 20:27:29 +0000135 AcquireLock(C, CE, C.getSVal(CE->getArg(0)), true, XNUSemantics);
Jordy Rosed9c52212011-07-19 20:21:41 +0000136 else if (FName == "pthread_mutex_unlock" ||
137 FName == "pthread_rwlock_unlock" ||
138 FName == "lck_mtx_unlock" ||
139 FName == "lck_rw_done")
George Karpenkovd703ec92018-01-17 20:27:29 +0000140 ReleaseLock(C, CE, C.getSVal(CE->getArg(0)));
Artem Dergachev77915932017-05-29 14:51:39 +0000141 else if (FName == "pthread_mutex_destroy")
George Karpenkovd703ec92018-01-17 20:27:29 +0000142 DestroyLock(C, CE, C.getSVal(CE->getArg(0)), PthreadSemantics);
Artem Dergachev77915932017-05-29 14:51:39 +0000143 else if (FName == "lck_mtx_destroy")
George Karpenkovd703ec92018-01-17 20:27:29 +0000144 DestroyLock(C, CE, C.getSVal(CE->getArg(0)), XNUSemantics);
Jordan Rose3a176ed2014-04-01 03:40:53 +0000145 else if (FName == "pthread_mutex_init")
George Karpenkovd703ec92018-01-17 20:27:29 +0000146 InitLock(C, CE, C.getSVal(CE->getArg(0)));
Ted Kremenekd48568f2009-11-12 06:17:47 +0000147}
148
Artem Dergachev77915932017-05-29 14:51:39 +0000149// When a lock is destroyed, in some semantics(like PthreadSemantics) we are not
150// sure if the destroy call has succeeded or failed, and the lock enters one of
151// the 'possibly destroyed' state. There is a short time frame for the
152// programmer to check the return value to see if the lock was successfully
153// destroyed. Before we model the next operation over that lock, we call this
154// function to see if the return value was checked by now and set the lock state
155// - either to destroyed state or back to its previous state.
156
157// In PthreadSemantics, pthread_mutex_destroy() returns zero if the lock is
158// successfully destroyed and it returns a non-zero value otherwise.
159ProgramStateRef PthreadLockChecker::resolvePossiblyDestroyedMutex(
160 ProgramStateRef state, const MemRegion *lockR, const SymbolRef *sym) const {
161 const LockState *lstate = state->get<LockMap>(lockR);
162 // Existence in DestroyRetVal ensures existence in LockMap.
163 // Existence in Destroyed also ensures that the lock state for lockR is either
164 // UntouchedAndPossiblyDestroyed or UnlockedAndPossiblyDestroyed.
165 assert(lstate->isUntouchedAndPossiblyDestroyed() ||
166 lstate->isUnlockedAndPossiblyDestroyed());
167
168 ConstraintManager &CMgr = state->getConstraintManager();
169 ConditionTruthVal retZero = CMgr.isNull(state, *sym);
170 if (retZero.isConstrainedFalse()) {
171 if (lstate->isUntouchedAndPossiblyDestroyed())
172 state = state->remove<LockMap>(lockR);
173 else if (lstate->isUnlockedAndPossiblyDestroyed())
174 state = state->set<LockMap>(lockR, LockState::getUnlocked());
175 } else
176 state = state->set<LockMap>(lockR, LockState::getDestroyed());
177
178 // Removing the map entry (lockR, sym) from DestroyRetVal as the lock state is
179 // now resolved.
180 state = state->remove<DestroyRetVal>(lockR);
181 return state;
182}
183
Artem Dergachev8e320082017-10-10 11:49:09 +0000184void PthreadLockChecker::printState(raw_ostream &Out, ProgramStateRef State,
185 const char *NL, const char *Sep) const {
186 LockMapTy LM = State->get<LockMap>();
187 if (!LM.isEmpty()) {
188 Out << Sep << "Mutex states:" << NL;
189 for (auto I : LM) {
190 I.first->dumpToStream(Out);
191 if (I.second.isLocked())
192 Out << ": locked";
193 else if (I.second.isUnlocked())
194 Out << ": unlocked";
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";
201 Out << NL;
202 }
203 }
204
205 LockSetTy LS = State->get<LockSet>();
206 if (!LS.isEmpty()) {
207 Out << Sep << "Mutex lock order:" << NL;
208 for (auto I: LS) {
209 I->dumpToStream(Out);
210 Out << NL;
211 }
212 }
213
214 // TODO: Dump destroyed mutex symbols?
215}
216
Ted Kremenekd48568f2009-11-12 06:17:47 +0000217void PthreadLockChecker::AcquireLock(CheckerContext &C, const CallExpr *CE,
Jordy Rosed9c52212011-07-19 20:21:41 +0000218 SVal lock, bool isTryLock,
219 enum LockingSemantics semantics) const {
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000220
Ted Kremenekd48568f2009-11-12 06:17:47 +0000221 const MemRegion *lockR = lock.getAsRegion();
222 if (!lockR)
223 return;
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000224
Ted Kremenek49b1e382012-01-26 21:29:00 +0000225 ProgramStateRef state = C.getState();
Artem Dergachev77915932017-05-29 14:51:39 +0000226 const SymbolRef *sym = state->get<DestroyRetVal>(lockR);
227 if (sym)
228 state = resolvePossiblyDestroyedMutex(state, lockR, sym);
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000229
George Karpenkovd703ec92018-01-17 20:27:29 +0000230 SVal X = C.getSVal(CE);
Ted Kremenekd48568f2009-11-12 06:17:47 +0000231 if (X.isUnknownOrUndef())
232 return;
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000233
David Blaikie2fdacbc2013-02-20 05:52:05 +0000234 DefinedSVal retVal = X.castAs<DefinedSVal>();
Jordy Rosed9c52212011-07-19 20:21:41 +0000235
Jordan Rose7fcaa142014-04-01 03:40:47 +0000236 if (const LockState *LState = state->get<LockMap>(lockR)) {
237 if (LState->isLocked()) {
238 if (!BT_doublelock)
239 BT_doublelock.reset(new BugType(this, "Double locking",
240 "Lock checker"));
Devin Coughline39bd402015-09-16 22:03:05 +0000241 ExplodedNode *N = C.generateErrorNode();
Jordan Rose7fcaa142014-04-01 03:40:47 +0000242 if (!N)
243 return;
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000244 auto report = llvm::make_unique<BugReport>(
245 *BT_doublelock, "This lock has already been acquired", N);
Jordan Rose7fcaa142014-04-01 03:40:47 +0000246 report->addRange(CE->getArg(0)->getSourceRange());
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000247 C.emitReport(std::move(report));
Jordy Rosed9c52212011-07-19 20:21:41 +0000248 return;
Jordan Rose7fcaa142014-04-01 03:40:47 +0000249 } else if (LState->isDestroyed()) {
250 reportUseDestroyedBug(C, CE);
251 return;
252 }
Ted Kremenekd48568f2009-11-12 06:17:47 +0000253 }
Jordy Rosed9c52212011-07-19 20:21:41 +0000254
Ted Kremenek49b1e382012-01-26 21:29:00 +0000255 ProgramStateRef lockSucc = state;
Jordy Rosed9c52212011-07-19 20:21:41 +0000256 if (isTryLock) {
257 // Bifurcate the state, and allow a mode where the lock acquisition fails.
Ted Kremenek49b1e382012-01-26 21:29:00 +0000258 ProgramStateRef lockFail;
Jordy Rosed9c52212011-07-19 20:21:41 +0000259 switch (semantics) {
260 case PthreadSemantics:
Benjamin Kramer867ea1d2014-03-02 13:01:17 +0000261 std::tie(lockFail, lockSucc) = state->assume(retVal);
Jordy Rosed9c52212011-07-19 20:21:41 +0000262 break;
263 case XNUSemantics:
Benjamin Kramer867ea1d2014-03-02 13:01:17 +0000264 std::tie(lockSucc, lockFail) = state->assume(retVal);
Jordy Rosed9c52212011-07-19 20:21:41 +0000265 break;
266 default:
267 llvm_unreachable("Unknown tryLock locking semantics");
Jordy Rosed9c52212011-07-19 20:21:41 +0000268 }
269 assert(lockFail && lockSucc);
Anna Zaksda4c8d62011-10-26 21:06:34 +0000270 C.addTransition(lockFail);
Jordy Rosed9c52212011-07-19 20:21:41 +0000271
272 } else if (semantics == PthreadSemantics) {
273 // Assume that the return value was 0.
Ted Kremenekc5bea1e2010-12-01 22:16:56 +0000274 lockSucc = state->assume(retVal, false);
Ted Kremenekd48568f2009-11-12 06:17:47 +0000275 assert(lockSucc);
Jordy Rosed9c52212011-07-19 20:21:41 +0000276
277 } else {
278 // XNU locking semantics return void on non-try locks
279 assert((semantics == XNUSemantics) && "Unknown locking semantics");
280 lockSucc = state;
Ted Kremenekd48568f2009-11-12 06:17:47 +0000281 }
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000282
283 // Record that the lock was acquired.
Ted Kremenekd48568f2009-11-12 06:17:47 +0000284 lockSucc = lockSucc->add<LockSet>(lockR);
Jordan Rose7fcaa142014-04-01 03:40:47 +0000285 lockSucc = lockSucc->set<LockMap>(lockR, LockState::getLocked());
Anna Zaksda4c8d62011-10-26 21:06:34 +0000286 C.addTransition(lockSucc);
Ted Kremenekd48568f2009-11-12 06:17:47 +0000287}
288
289void PthreadLockChecker::ReleaseLock(CheckerContext &C, const CallExpr *CE,
Argyrios Kyrtzidisdff865d2011-02-23 01:05:36 +0000290 SVal lock) const {
Ted Kremenekd48568f2009-11-12 06:17:47 +0000291
292 const MemRegion *lockR = lock.getAsRegion();
293 if (!lockR)
294 return;
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000295
Ted Kremenek49b1e382012-01-26 21:29:00 +0000296 ProgramStateRef state = C.getState();
Artem Dergachev77915932017-05-29 14:51:39 +0000297 const SymbolRef *sym = state->get<DestroyRetVal>(lockR);
298 if (sym)
299 state = resolvePossiblyDestroyedMutex(state, lockR, sym);
Ted Kremenekd48568f2009-11-12 06:17:47 +0000300
Jordan Rose7fcaa142014-04-01 03:40:47 +0000301 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",
305 "Lock checker"));
Devin Coughline39bd402015-09-16 22:03:05 +0000306 ExplodedNode *N = C.generateErrorNode();
Jordan Rose7fcaa142014-04-01 03:40:47 +0000307 if (!N)
308 return;
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000309 auto Report = llvm::make_unique<BugReport>(
310 *BT_doubleunlock, "This lock has already been unlocked", N);
Jordan Rose7fcaa142014-04-01 03:40:47 +0000311 Report->addRange(CE->getArg(0)->getSourceRange());
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000312 C.emitReport(std::move(Report));
Jordy Rosed9c52212011-07-19 20:21:41 +0000313 return;
Jordan Rose7fcaa142014-04-01 03:40:47 +0000314 } else if (LState->isDestroyed()) {
315 reportUseDestroyedBug(C, CE);
316 return;
317 }
Jordy Rosed9c52212011-07-19 20:21:41 +0000318 }
319
Jordan Rose0696bb42014-04-01 03:40:38 +0000320 LockSetTy LS = state->get<LockSet>();
321
322 // FIXME: Better analysis requires IPA for wrappers.
323
324 if (!LS.isEmpty()) {
325 const MemRegion *firstLockR = LS.getHead();
326 if (firstLockR != lockR) {
327 if (!BT_lor)
328 BT_lor.reset(new BugType(this, "Lock order reversal", "Lock checker"));
Devin Coughline39bd402015-09-16 22:03:05 +0000329 ExplodedNode *N = C.generateErrorNode();
Jordan Rose0696bb42014-04-01 03:40:38 +0000330 if (!N)
331 return;
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000332 auto report = llvm::make_unique<BugReport>(
333 *BT_lor, "This was not the most recently acquired lock. Possible "
334 "lock order reversal", N);
Jordan Rose0696bb42014-04-01 03:40:38 +0000335 report->addRange(CE->getArg(0)->getSourceRange());
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000336 C.emitReport(std::move(report));
Jordan Rose0696bb42014-04-01 03:40:38 +0000337 return;
338 }
Jordan Rose7fcaa142014-04-01 03:40:47 +0000339 // Record that the lock was released.
340 state = state->set<LockSet>(LS.getTail());
Jordan Rose0696bb42014-04-01 03:40:38 +0000341 }
342
Jordan Rose7fcaa142014-04-01 03:40:47 +0000343 state = state->set<LockMap>(lockR, LockState::getUnlocked());
Anna Zaksda4c8d62011-10-26 21:06:34 +0000344 C.addTransition(state);
Ted Kremenekd48568f2009-11-12 06:17:47 +0000345}
Argyrios Kyrtzidisdff865d2011-02-23 01:05:36 +0000346
Jordan Rose7fcaa142014-04-01 03:40:47 +0000347void PthreadLockChecker::DestroyLock(CheckerContext &C, const CallExpr *CE,
Artem Dergachev77915932017-05-29 14:51:39 +0000348 SVal Lock,
349 enum LockingSemantics semantics) const {
Jordan Rose7fcaa142014-04-01 03:40:47 +0000350
351 const MemRegion *LockR = Lock.getAsRegion();
352 if (!LockR)
353 return;
354
355 ProgramStateRef State = C.getState();
356
Artem Dergachev77915932017-05-29 14:51:39 +0000357 const SymbolRef *sym = State->get<DestroyRetVal>(LockR);
358 if (sym)
359 State = resolvePossiblyDestroyedMutex(State, LockR, sym);
Jordan Rose7fcaa142014-04-01 03:40:47 +0000360
Artem Dergachev77915932017-05-29 14:51:39 +0000361 const LockState *LState = State->get<LockMap>(LockR);
362 // Checking the return value of the destroy method only in the case of
363 // PthreadSemantics
364 if (semantics == PthreadSemantics) {
365 if (!LState || LState->isUnlocked()) {
366 SymbolRef sym = C.getSVal(CE).getAsSymbol();
367 if (!sym) {
368 State = State->remove<LockMap>(LockR);
369 C.addTransition(State);
370 return;
371 }
372 State = State->set<DestroyRetVal>(LockR, sym);
373 if (LState && LState->isUnlocked())
374 State = State->set<LockMap>(
375 LockR, LockState::getUnlockedAndPossiblyDestroyed());
376 else
377 State = State->set<LockMap>(
378 LockR, LockState::getUntouchedAndPossiblyDestroyed());
379 C.addTransition(State);
380 return;
381 }
382 } else {
383 if (!LState || LState->isUnlocked()) {
384 State = State->set<LockMap>(LockR, LockState::getDestroyed());
385 C.addTransition(State);
386 return;
387 }
388 }
Jordan Rose7fcaa142014-04-01 03:40:47 +0000389 StringRef Message;
390
391 if (LState->isLocked()) {
392 Message = "This lock is still locked";
393 } else {
394 Message = "This lock has already been destroyed";
395 }
396
397 if (!BT_destroylock)
398 BT_destroylock.reset(new BugType(this, "Destroy invalid lock",
399 "Lock checker"));
Devin Coughline39bd402015-09-16 22:03:05 +0000400 ExplodedNode *N = C.generateErrorNode();
Jordan Rose7fcaa142014-04-01 03:40:47 +0000401 if (!N)
402 return;
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000403 auto Report = llvm::make_unique<BugReport>(*BT_destroylock, Message, N);
Jordan Rose7fcaa142014-04-01 03:40:47 +0000404 Report->addRange(CE->getArg(0)->getSourceRange());
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000405 C.emitReport(std::move(Report));
Jordan Rose7fcaa142014-04-01 03:40:47 +0000406}
407
Jordan Rose3a176ed2014-04-01 03:40:53 +0000408void PthreadLockChecker::InitLock(CheckerContext &C, const CallExpr *CE,
409 SVal Lock) const {
410
411 const MemRegion *LockR = Lock.getAsRegion();
412 if (!LockR)
413 return;
414
415 ProgramStateRef State = C.getState();
416
Artem Dergachev77915932017-05-29 14:51:39 +0000417 const SymbolRef *sym = State->get<DestroyRetVal>(LockR);
418 if (sym)
419 State = resolvePossiblyDestroyedMutex(State, LockR, sym);
420
Jordan Rose3a176ed2014-04-01 03:40:53 +0000421 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);
425 return;
426 }
427
428 StringRef Message;
429
430 if (LState->isLocked()) {
431 Message = "This lock is still being held";
432 } else {
433 Message = "This lock has already been initialized";
434 }
435
436 if (!BT_initlock)
437 BT_initlock.reset(new BugType(this, "Init invalid lock",
438 "Lock checker"));
Devin Coughline39bd402015-09-16 22:03:05 +0000439 ExplodedNode *N = C.generateErrorNode();
Jordan Rose3a176ed2014-04-01 03:40:53 +0000440 if (!N)
441 return;
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000442 auto Report = llvm::make_unique<BugReport>(*BT_initlock, Message, N);
Jordan Rose3a176ed2014-04-01 03:40:53 +0000443 Report->addRange(CE->getArg(0)->getSourceRange());
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000444 C.emitReport(std::move(Report));
Jordan Rose3a176ed2014-04-01 03:40:53 +0000445}
446
Jordan Rose7fcaa142014-04-01 03:40:47 +0000447void PthreadLockChecker::reportUseDestroyedBug(CheckerContext &C,
448 const CallExpr *CE) const {
449 if (!BT_destroylock)
450 BT_destroylock.reset(new BugType(this, "Use destroyed lock",
451 "Lock checker"));
Devin Coughline39bd402015-09-16 22:03:05 +0000452 ExplodedNode *N = C.generateErrorNode();
Jordan Rose7fcaa142014-04-01 03:40:47 +0000453 if (!N)
454 return;
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000455 auto Report = llvm::make_unique<BugReport>(
456 *BT_destroylock, "This lock has already been destroyed", N);
Jordan Rose7fcaa142014-04-01 03:40:47 +0000457 Report->addRange(CE->getArg(0)->getSourceRange());
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000458 C.emitReport(std::move(Report));
Jordan Rose7fcaa142014-04-01 03:40:47 +0000459}
460
Artem Dergachev77915932017-05-29 14:51:39 +0000461void PthreadLockChecker::checkDeadSymbols(SymbolReaper &SymReaper,
462 CheckerContext &C) const {
463 ProgramStateRef State = C.getState();
464
465 // TODO: Clean LockMap when a mutex region dies.
466
467 DestroyRetValTy TrackedSymbols = State->get<DestroyRetVal>();
468 for (DestroyRetValTy::iterator I = TrackedSymbols.begin(),
469 E = TrackedSymbols.end();
470 I != E; ++I) {
471 const SymbolRef Sym = I->second;
472 const MemRegion *lockR = I->first;
473 bool IsSymDead = SymReaper.isDead(Sym);
474 // Remove the dead symbol from the return value symbols map.
475 if (IsSymDead)
476 State = resolvePossiblyDestroyedMutex(State, lockR, &Sym);
477 }
478 C.addTransition(State);
479}
480
Argyrios Kyrtzidisdff865d2011-02-23 01:05:36 +0000481void ento::registerPthreadLockChecker(CheckerManager &mgr) {
482 mgr.registerChecker<PthreadLockChecker>();
483}