blob: 76ae02731a3d3c6dbbf8fd10f5fe9f46120b96da [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"
Jordy Rosed9c52212011-07-19 20:21:41 +000021#include "llvm/ADT/ImmutableList.h"
Ted Kremenekd48568f2009-11-12 06:17:47 +000022
23using namespace clang;
Ted Kremenek98857c92010-12-23 07:20:52 +000024using namespace ento;
Ted Kremenekd48568f2009-11-12 06:17:47 +000025
26namespace {
Jordan Rose7fcaa142014-04-01 03:40:47 +000027
28struct LockState {
29 enum Kind { Destroyed, Locked, Unlocked } K;
30
31private:
32 LockState(Kind K) : K(K) {}
33
34public:
35 static LockState getLocked(void) { return LockState(Locked); }
36 static LockState getUnlocked(void) { return LockState(Unlocked); }
37 static LockState getDestroyed(void) { return LockState(Destroyed); }
38
39 bool operator==(const LockState &X) const {
40 return K == X.K;
41 }
42
43 bool isLocked() const { return K == Locked; }
44 bool isUnlocked() const { return K == Unlocked; }
45 bool isDestroyed() const { return K == Destroyed; }
46
47 void Profile(llvm::FoldingSetNodeID &ID) const {
48 ID.AddInteger(K);
49 }
50};
51
Jordy Rosed9c52212011-07-19 20:21:41 +000052class PthreadLockChecker : public Checker< check::PostStmt<CallExpr> > {
Ahmed Charlesb8984322014-03-07 20:03:18 +000053 mutable std::unique_ptr<BugType> BT_doublelock;
Jordan Rose0696bb42014-04-01 03:40:38 +000054 mutable std::unique_ptr<BugType> BT_doubleunlock;
Jordan Rose7fcaa142014-04-01 03:40:47 +000055 mutable std::unique_ptr<BugType> BT_destroylock;
Ahmed Charlesb8984322014-03-07 20:03:18 +000056 mutable std::unique_ptr<BugType> BT_lor;
Jordy Rosed9c52212011-07-19 20:21:41 +000057 enum LockingSemantics {
58 NotApplicable = 0,
59 PthreadSemantics,
60 XNUSemantics
61 };
Ted Kremenekd48568f2009-11-12 06:17:47 +000062public:
Argyrios Kyrtzidisdff865d2011-02-23 01:05:36 +000063 void checkPostStmt(const CallExpr *CE, CheckerContext &C) const;
Ted Kremenekd48568f2009-11-12 06:17:47 +000064
Jordy Rosed9c52212011-07-19 20:21:41 +000065 void AcquireLock(CheckerContext &C, const CallExpr *CE, SVal lock,
66 bool isTryLock, enum LockingSemantics semantics) const;
Ted Kremenekd48568f2009-11-12 06:17:47 +000067
Jordy Rosed9c52212011-07-19 20:21:41 +000068 void ReleaseLock(CheckerContext &C, const CallExpr *CE, SVal lock) const;
Jordan Rose7fcaa142014-04-01 03:40:47 +000069 void DestroyLock(CheckerContext &C, const CallExpr *CE, SVal Lock) const;
70 void reportUseDestroyedBug(CheckerContext &C, const CallExpr *CE) const;
Ted Kremenekd48568f2009-11-12 06:17:47 +000071};
72} // end anonymous namespace
73
74// GDM Entry for tracking lock state.
Jordan Rose0c153cb2012-11-02 01:54:06 +000075REGISTER_LIST_WITH_PROGRAMSTATE(LockSet, const MemRegion *)
Ted Kremenekd48568f2009-11-12 06:17:47 +000076
Jordan Rose7fcaa142014-04-01 03:40:47 +000077REGISTER_MAP_WITH_PROGRAMSTATE(LockMap, const MemRegion *, LockState)
Ted Kremenekd48568f2009-11-12 06:17:47 +000078
Argyrios Kyrtzidisdff865d2011-02-23 01:05:36 +000079void PthreadLockChecker::checkPostStmt(const CallExpr *CE,
80 CheckerContext &C) const {
Ted Kremenek49b1e382012-01-26 21:29:00 +000081 ProgramStateRef state = C.getState();
Ted Kremenek632e3b72012-01-06 22:09:28 +000082 const LocationContext *LCtx = C.getLocationContext();
Anna Zaksc6aa5312011-12-01 05:57:37 +000083 StringRef FName = C.getCalleeName(CE);
84 if (FName.empty())
Ted Kremenekd48568f2009-11-12 06:17:47 +000085 return;
Jordy Rosea39e10f2011-07-19 20:31:42 +000086
Jordan Rose7fcaa142014-04-01 03:40:47 +000087 if (CE->getNumArgs() != 1 && CE->getNumArgs() != 2)
Jordy Rosed9c52212011-07-19 20:21:41 +000088 return;
Jordy Rosea39e10f2011-07-19 20:31:42 +000089
Jordy Rosed9c52212011-07-19 20:21:41 +000090 if (FName == "pthread_mutex_lock" ||
91 FName == "pthread_rwlock_rdlock" ||
92 FName == "pthread_rwlock_wrlock")
Ted Kremenek632e3b72012-01-06 22:09:28 +000093 AcquireLock(C, CE, state->getSVal(CE->getArg(0), LCtx),
94 false, PthreadSemantics);
Jordy Rosed9c52212011-07-19 20:21:41 +000095 else if (FName == "lck_mtx_lock" ||
96 FName == "lck_rw_lock_exclusive" ||
97 FName == "lck_rw_lock_shared")
Ted Kremenek632e3b72012-01-06 22:09:28 +000098 AcquireLock(C, CE, state->getSVal(CE->getArg(0), LCtx),
99 false, XNUSemantics);
Jordy Rosed9c52212011-07-19 20:21:41 +0000100 else if (FName == "pthread_mutex_trylock" ||
101 FName == "pthread_rwlock_tryrdlock" ||
Ted Kremenek115c3f7a2014-01-17 16:06:43 +0000102 FName == "pthread_rwlock_trywrlock")
Ted Kremenek632e3b72012-01-06 22:09:28 +0000103 AcquireLock(C, CE, state->getSVal(CE->getArg(0), LCtx),
104 true, PthreadSemantics);
Jordy Rosed9c52212011-07-19 20:21:41 +0000105 else if (FName == "lck_mtx_try_lock" ||
106 FName == "lck_rw_try_lock_exclusive" ||
107 FName == "lck_rw_try_lock_shared")
Ted Kremenek632e3b72012-01-06 22:09:28 +0000108 AcquireLock(C, CE, state->getSVal(CE->getArg(0), LCtx),
109 true, XNUSemantics);
Jordy Rosed9c52212011-07-19 20:21:41 +0000110 else if (FName == "pthread_mutex_unlock" ||
111 FName == "pthread_rwlock_unlock" ||
112 FName == "lck_mtx_unlock" ||
113 FName == "lck_rw_done")
Ted Kremenek632e3b72012-01-06 22:09:28 +0000114 ReleaseLock(C, CE, state->getSVal(CE->getArg(0), LCtx));
Jordan Rose7fcaa142014-04-01 03:40:47 +0000115 else if (FName == "pthread_mutex_destroy" ||
116 FName == "lck_mtx_destroy")
117 DestroyLock(C, CE, state->getSVal(CE->getArg(0), LCtx));
Ted Kremenekd48568f2009-11-12 06:17:47 +0000118}
119
120void PthreadLockChecker::AcquireLock(CheckerContext &C, const CallExpr *CE,
Jordy Rosed9c52212011-07-19 20:21:41 +0000121 SVal lock, bool isTryLock,
122 enum LockingSemantics semantics) const {
Ted Kremenekd48568f2009-11-12 06:17:47 +0000123
124 const MemRegion *lockR = lock.getAsRegion();
125 if (!lockR)
126 return;
127
Ted Kremenek49b1e382012-01-26 21:29:00 +0000128 ProgramStateRef state = C.getState();
Ted Kremenekd48568f2009-11-12 06:17:47 +0000129
Ted Kremenek632e3b72012-01-06 22:09:28 +0000130 SVal X = state->getSVal(CE, C.getLocationContext());
Ted Kremenekd48568f2009-11-12 06:17:47 +0000131 if (X.isUnknownOrUndef())
132 return;
133
David Blaikie2fdacbc2013-02-20 05:52:05 +0000134 DefinedSVal retVal = X.castAs<DefinedSVal>();
Jordy Rosed9c52212011-07-19 20:21:41 +0000135
Jordan Rose7fcaa142014-04-01 03:40:47 +0000136 if (const LockState *LState = state->get<LockMap>(lockR)) {
137 if (LState->isLocked()) {
138 if (!BT_doublelock)
139 BT_doublelock.reset(new BugType(this, "Double locking",
140 "Lock checker"));
141 ExplodedNode *N = C.generateSink();
142 if (!N)
143 return;
144 BugReport *report = new BugReport(*BT_doublelock,
145 "This lock has already been acquired",
146 N);
147 report->addRange(CE->getArg(0)->getSourceRange());
148 C.emitReport(report);
Jordy Rosed9c52212011-07-19 20:21:41 +0000149 return;
Jordan Rose7fcaa142014-04-01 03:40:47 +0000150 } else if (LState->isDestroyed()) {
151 reportUseDestroyedBug(C, CE);
152 return;
153 }
Ted Kremenekd48568f2009-11-12 06:17:47 +0000154 }
Jordy Rosed9c52212011-07-19 20:21:41 +0000155
Ted Kremenek49b1e382012-01-26 21:29:00 +0000156 ProgramStateRef lockSucc = state;
Jordy Rosed9c52212011-07-19 20:21:41 +0000157 if (isTryLock) {
158 // Bifurcate the state, and allow a mode where the lock acquisition fails.
Ted Kremenek49b1e382012-01-26 21:29:00 +0000159 ProgramStateRef lockFail;
Jordy Rosed9c52212011-07-19 20:21:41 +0000160 switch (semantics) {
161 case PthreadSemantics:
Benjamin Kramer867ea1d2014-03-02 13:01:17 +0000162 std::tie(lockFail, lockSucc) = state->assume(retVal);
Jordy Rosed9c52212011-07-19 20:21:41 +0000163 break;
164 case XNUSemantics:
Benjamin Kramer867ea1d2014-03-02 13:01:17 +0000165 std::tie(lockSucc, lockFail) = state->assume(retVal);
Jordy Rosed9c52212011-07-19 20:21:41 +0000166 break;
167 default:
168 llvm_unreachable("Unknown tryLock locking semantics");
Jordy Rosed9c52212011-07-19 20:21:41 +0000169 }
170 assert(lockFail && lockSucc);
Anna Zaksda4c8d62011-10-26 21:06:34 +0000171 C.addTransition(lockFail);
Jordy Rosed9c52212011-07-19 20:21:41 +0000172
173 } else if (semantics == PthreadSemantics) {
174 // Assume that the return value was 0.
Ted Kremenekc5bea1e2010-12-01 22:16:56 +0000175 lockSucc = state->assume(retVal, false);
Ted Kremenekd48568f2009-11-12 06:17:47 +0000176 assert(lockSucc);
Jordy Rosed9c52212011-07-19 20:21:41 +0000177
178 } else {
179 // XNU locking semantics return void on non-try locks
180 assert((semantics == XNUSemantics) && "Unknown locking semantics");
181 lockSucc = state;
Ted Kremenekd48568f2009-11-12 06:17:47 +0000182 }
183
Jordy Rosed9c52212011-07-19 20:21:41 +0000184 // Record that the lock was acquired.
Ted Kremenekd48568f2009-11-12 06:17:47 +0000185 lockSucc = lockSucc->add<LockSet>(lockR);
Jordan Rose7fcaa142014-04-01 03:40:47 +0000186 lockSucc = lockSucc->set<LockMap>(lockR, LockState::getLocked());
Anna Zaksda4c8d62011-10-26 21:06:34 +0000187 C.addTransition(lockSucc);
Ted Kremenekd48568f2009-11-12 06:17:47 +0000188}
189
190void PthreadLockChecker::ReleaseLock(CheckerContext &C, const CallExpr *CE,
Argyrios Kyrtzidisdff865d2011-02-23 01:05:36 +0000191 SVal lock) const {
Ted Kremenekd48568f2009-11-12 06:17:47 +0000192
193 const MemRegion *lockR = lock.getAsRegion();
194 if (!lockR)
195 return;
196
Ted Kremenek49b1e382012-01-26 21:29:00 +0000197 ProgramStateRef state = C.getState();
Ted Kremenekd48568f2009-11-12 06:17:47 +0000198
Jordan Rose7fcaa142014-04-01 03:40:47 +0000199 if (const LockState *LState = state->get<LockMap>(lockR)) {
200 if (LState->isUnlocked()) {
201 if (!BT_doubleunlock)
202 BT_doubleunlock.reset(new BugType(this, "Double unlocking",
203 "Lock checker"));
204 ExplodedNode *N = C.generateSink();
205 if (!N)
206 return;
207 BugReport *Report = new BugReport(*BT_doubleunlock,
208 "This lock has already been unlocked",
209 N);
210 Report->addRange(CE->getArg(0)->getSourceRange());
211 C.emitReport(Report);
Jordy Rosed9c52212011-07-19 20:21:41 +0000212 return;
Jordan Rose7fcaa142014-04-01 03:40:47 +0000213 } else if (LState->isDestroyed()) {
214 reportUseDestroyedBug(C, CE);
215 return;
216 }
Jordy Rosed9c52212011-07-19 20:21:41 +0000217 }
218
Jordan Rose0696bb42014-04-01 03:40:38 +0000219 LockSetTy LS = state->get<LockSet>();
220
221 // FIXME: Better analysis requires IPA for wrappers.
222
223 if (!LS.isEmpty()) {
224 const MemRegion *firstLockR = LS.getHead();
225 if (firstLockR != lockR) {
226 if (!BT_lor)
227 BT_lor.reset(new BugType(this, "Lock order reversal", "Lock checker"));
228 ExplodedNode *N = C.generateSink();
229 if (!N)
230 return;
231 BugReport *report = new BugReport(*BT_lor,
232 "This was not the most recently "
233 "acquired lock. Possible lock order "
234 "reversal",
235 N);
236 report->addRange(CE->getArg(0)->getSourceRange());
237 C.emitReport(report);
238 return;
239 }
Jordan Rose7fcaa142014-04-01 03:40:47 +0000240 // Record that the lock was released.
241 state = state->set<LockSet>(LS.getTail());
Jordan Rose0696bb42014-04-01 03:40:38 +0000242 }
243
Jordan Rose7fcaa142014-04-01 03:40:47 +0000244 state = state->set<LockMap>(lockR, LockState::getUnlocked());
Anna Zaksda4c8d62011-10-26 21:06:34 +0000245 C.addTransition(state);
Ted Kremenekd48568f2009-11-12 06:17:47 +0000246}
Argyrios Kyrtzidisdff865d2011-02-23 01:05:36 +0000247
Jordan Rose7fcaa142014-04-01 03:40:47 +0000248void PthreadLockChecker::DestroyLock(CheckerContext &C, const CallExpr *CE,
249 SVal Lock) const {
250
251 const MemRegion *LockR = Lock.getAsRegion();
252 if (!LockR)
253 return;
254
255 ProgramStateRef State = C.getState();
256
257 const LockState *LState = State->get<LockMap>(LockR);
258 if (!LState || LState->isUnlocked()) {
259 State = State->set<LockMap>(LockR, LockState::getDestroyed());
260 C.addTransition(State);
261 return;
262 }
263
264 StringRef Message;
265
266 if (LState->isLocked()) {
267 Message = "This lock is still locked";
268 } else {
269 Message = "This lock has already been destroyed";
270 }
271
272 if (!BT_destroylock)
273 BT_destroylock.reset(new BugType(this, "Destroy invalid lock",
274 "Lock checker"));
275 ExplodedNode *N = C.generateSink();
276 if (!N)
277 return;
278 BugReport *Report = new BugReport(*BT_destroylock, Message, N);
279 Report->addRange(CE->getArg(0)->getSourceRange());
280 C.emitReport(Report);
281}
282
283void PthreadLockChecker::reportUseDestroyedBug(CheckerContext &C,
284 const CallExpr *CE) const {
285 if (!BT_destroylock)
286 BT_destroylock.reset(new BugType(this, "Use destroyed lock",
287 "Lock checker"));
288 ExplodedNode *N = C.generateSink();
289 if (!N)
290 return;
291 BugReport *Report = new BugReport(*BT_destroylock,
292 "This lock has already been destroyed",
293 N);
294 Report->addRange(CE->getArg(0)->getSourceRange());
295 C.emitReport(Report);
296}
297
Argyrios Kyrtzidisdff865d2011-02-23 01:05:36 +0000298void ento::registerPthreadLockChecker(CheckerManager &mgr) {
299 mgr.registerChecker<PthreadLockChecker>();
300}