blob: 1ede3a2a512637c2052f364b35db797fd512cfa4 [file] [log] [blame]
Jordy Rosedcb1d5d2011-07-19 20:21:41 +00001//===--- PthreadLockChecker.cpp - Check for locking problems ---*- C++ -*--===//
Ted Kremenekac9bea82009-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 Rose4cc11872011-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 Kremenekac9bea82009-11-12 06:17:47 +000012//
13//===----------------------------------------------------------------------===//
14
Argyrios Kyrtzidisa0decc92011-02-15 21:25:03 +000015#include "ClangSACheckers.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000016#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +000017#include "clang/StaticAnalyzer/Core/Checker.h"
Argyrios Kyrtzidis695fb502011-02-17 21:39:17 +000018#include "clang/StaticAnalyzer/Core/CheckerManager.h"
Argyrios Kyrtzidis983326f2011-02-23 01:05:36 +000019#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Ted Kremenek18c66fd2011-08-15 22:09:50 +000020#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
Jordy Rosedcb1d5d2011-07-19 20:21:41 +000021#include "llvm/ADT/ImmutableList.h"
Ted Kremenekac9bea82009-11-12 06:17:47 +000022
23using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000024using namespace ento;
Ted Kremenekac9bea82009-11-12 06:17:47 +000025
26namespace {
Stephen Hines651f13c2014-04-23 16:59:28 -070027
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 Rosedcb1d5d2011-07-19 20:21:41 +000052class PthreadLockChecker : public Checker< check::PostStmt<CallExpr> > {
Stephen Hines651f13c2014-04-23 16:59:28 -070053 mutable std::unique_ptr<BugType> BT_doublelock;
54 mutable std::unique_ptr<BugType> BT_doubleunlock;
55 mutable std::unique_ptr<BugType> BT_destroylock;
56 mutable std::unique_ptr<BugType> BT_initlock;
57 mutable std::unique_ptr<BugType> BT_lor;
Jordy Rosedcb1d5d2011-07-19 20:21:41 +000058 enum LockingSemantics {
59 NotApplicable = 0,
60 PthreadSemantics,
61 XNUSemantics
62 };
Ted Kremenekac9bea82009-11-12 06:17:47 +000063public:
Argyrios Kyrtzidis983326f2011-02-23 01:05:36 +000064 void checkPostStmt(const CallExpr *CE, CheckerContext &C) const;
Ted Kremenekac9bea82009-11-12 06:17:47 +000065
Jordy Rosedcb1d5d2011-07-19 20:21:41 +000066 void AcquireLock(CheckerContext &C, const CallExpr *CE, SVal lock,
67 bool isTryLock, enum LockingSemantics semantics) const;
Ted Kremenekac9bea82009-11-12 06:17:47 +000068
Jordy Rosedcb1d5d2011-07-19 20:21:41 +000069 void ReleaseLock(CheckerContext &C, const CallExpr *CE, SVal lock) const;
Stephen Hines651f13c2014-04-23 16:59:28 -070070 void DestroyLock(CheckerContext &C, const CallExpr *CE, SVal Lock) const;
71 void InitLock(CheckerContext &C, const CallExpr *CE, SVal Lock) const;
72 void reportUseDestroyedBug(CheckerContext &C, const CallExpr *CE) const;
Ted Kremenekac9bea82009-11-12 06:17:47 +000073};
74} // end anonymous namespace
75
76// GDM Entry for tracking lock state.
Jordan Rose166d5022012-11-02 01:54:06 +000077REGISTER_LIST_WITH_PROGRAMSTATE(LockSet, const MemRegion *)
Ted Kremenekac9bea82009-11-12 06:17:47 +000078
Stephen Hines651f13c2014-04-23 16:59:28 -070079REGISTER_MAP_WITH_PROGRAMSTATE(LockMap, const MemRegion *, LockState)
Ted Kremenekac9bea82009-11-12 06:17:47 +000080
Argyrios Kyrtzidis983326f2011-02-23 01:05:36 +000081void PthreadLockChecker::checkPostStmt(const CallExpr *CE,
82 CheckerContext &C) const {
Ted Kremenek8bef8232012-01-26 21:29:00 +000083 ProgramStateRef state = C.getState();
Ted Kremenek5eca4822012-01-06 22:09:28 +000084 const LocationContext *LCtx = C.getLocationContext();
Anna Zaksb805c8f2011-12-01 05:57:37 +000085 StringRef FName = C.getCalleeName(CE);
86 if (FName.empty())
Ted Kremenekac9bea82009-11-12 06:17:47 +000087 return;
Jordy Rose4cc11872011-07-19 20:31:42 +000088
Stephen Hines651f13c2014-04-23 16:59:28 -070089 if (CE->getNumArgs() != 1 && CE->getNumArgs() != 2)
Jordy Rosedcb1d5d2011-07-19 20:21:41 +000090 return;
Jordy Rose4cc11872011-07-19 20:31:42 +000091
Jordy Rosedcb1d5d2011-07-19 20:21:41 +000092 if (FName == "pthread_mutex_lock" ||
93 FName == "pthread_rwlock_rdlock" ||
94 FName == "pthread_rwlock_wrlock")
Ted Kremenek5eca4822012-01-06 22:09:28 +000095 AcquireLock(C, CE, state->getSVal(CE->getArg(0), LCtx),
96 false, PthreadSemantics);
Jordy Rosedcb1d5d2011-07-19 20:21:41 +000097 else if (FName == "lck_mtx_lock" ||
98 FName == "lck_rw_lock_exclusive" ||
99 FName == "lck_rw_lock_shared")
Ted Kremenek5eca4822012-01-06 22:09:28 +0000100 AcquireLock(C, CE, state->getSVal(CE->getArg(0), LCtx),
101 false, XNUSemantics);
Jordy Rosedcb1d5d2011-07-19 20:21:41 +0000102 else if (FName == "pthread_mutex_trylock" ||
103 FName == "pthread_rwlock_tryrdlock" ||
Stephen Hines651f13c2014-04-23 16:59:28 -0700104 FName == "pthread_rwlock_trywrlock")
Ted Kremenek5eca4822012-01-06 22:09:28 +0000105 AcquireLock(C, CE, state->getSVal(CE->getArg(0), LCtx),
106 true, PthreadSemantics);
Jordy Rosedcb1d5d2011-07-19 20:21:41 +0000107 else if (FName == "lck_mtx_try_lock" ||
108 FName == "lck_rw_try_lock_exclusive" ||
109 FName == "lck_rw_try_lock_shared")
Ted Kremenek5eca4822012-01-06 22:09:28 +0000110 AcquireLock(C, CE, state->getSVal(CE->getArg(0), LCtx),
111 true, XNUSemantics);
Jordy Rosedcb1d5d2011-07-19 20:21:41 +0000112 else if (FName == "pthread_mutex_unlock" ||
113 FName == "pthread_rwlock_unlock" ||
114 FName == "lck_mtx_unlock" ||
115 FName == "lck_rw_done")
Ted Kremenek5eca4822012-01-06 22:09:28 +0000116 ReleaseLock(C, CE, state->getSVal(CE->getArg(0), LCtx));
Stephen Hines651f13c2014-04-23 16:59:28 -0700117 else if (FName == "pthread_mutex_destroy" ||
118 FName == "lck_mtx_destroy")
119 DestroyLock(C, CE, state->getSVal(CE->getArg(0), LCtx));
120 else if (FName == "pthread_mutex_init")
121 InitLock(C, CE, state->getSVal(CE->getArg(0), LCtx));
Ted Kremenekac9bea82009-11-12 06:17:47 +0000122}
123
124void PthreadLockChecker::AcquireLock(CheckerContext &C, const CallExpr *CE,
Jordy Rosedcb1d5d2011-07-19 20:21:41 +0000125 SVal lock, bool isTryLock,
126 enum LockingSemantics semantics) const {
Ted Kremenekac9bea82009-11-12 06:17:47 +0000127
128 const MemRegion *lockR = lock.getAsRegion();
129 if (!lockR)
130 return;
131
Ted Kremenek8bef8232012-01-26 21:29:00 +0000132 ProgramStateRef state = C.getState();
Ted Kremenekac9bea82009-11-12 06:17:47 +0000133
Ted Kremenek5eca4822012-01-06 22:09:28 +0000134 SVal X = state->getSVal(CE, C.getLocationContext());
Ted Kremenekac9bea82009-11-12 06:17:47 +0000135 if (X.isUnknownOrUndef())
136 return;
137
David Blaikie5251abe2013-02-20 05:52:05 +0000138 DefinedSVal retVal = X.castAs<DefinedSVal>();
Jordy Rosedcb1d5d2011-07-19 20:21:41 +0000139
Stephen Hines651f13c2014-04-23 16:59:28 -0700140 if (const LockState *LState = state->get<LockMap>(lockR)) {
141 if (LState->isLocked()) {
142 if (!BT_doublelock)
143 BT_doublelock.reset(new BugType(this, "Double locking",
144 "Lock checker"));
145 ExplodedNode *N = C.generateSink();
146 if (!N)
147 return;
148 BugReport *report = new BugReport(*BT_doublelock,
149 "This lock has already been acquired",
150 N);
151 report->addRange(CE->getArg(0)->getSourceRange());
152 C.emitReport(report);
Jordy Rosedcb1d5d2011-07-19 20:21:41 +0000153 return;
Stephen Hines651f13c2014-04-23 16:59:28 -0700154 } else if (LState->isDestroyed()) {
155 reportUseDestroyedBug(C, CE);
156 return;
157 }
Ted Kremenekac9bea82009-11-12 06:17:47 +0000158 }
Jordy Rosedcb1d5d2011-07-19 20:21:41 +0000159
Ted Kremenek8bef8232012-01-26 21:29:00 +0000160 ProgramStateRef lockSucc = state;
Jordy Rosedcb1d5d2011-07-19 20:21:41 +0000161 if (isTryLock) {
162 // Bifurcate the state, and allow a mode where the lock acquisition fails.
Ted Kremenek8bef8232012-01-26 21:29:00 +0000163 ProgramStateRef lockFail;
Jordy Rosedcb1d5d2011-07-19 20:21:41 +0000164 switch (semantics) {
165 case PthreadSemantics:
Stephen Hines651f13c2014-04-23 16:59:28 -0700166 std::tie(lockFail, lockSucc) = state->assume(retVal);
Jordy Rosedcb1d5d2011-07-19 20:21:41 +0000167 break;
168 case XNUSemantics:
Stephen Hines651f13c2014-04-23 16:59:28 -0700169 std::tie(lockSucc, lockFail) = state->assume(retVal);
Jordy Rosedcb1d5d2011-07-19 20:21:41 +0000170 break;
171 default:
172 llvm_unreachable("Unknown tryLock locking semantics");
Jordy Rosedcb1d5d2011-07-19 20:21:41 +0000173 }
174 assert(lockFail && lockSucc);
Anna Zaks0bd6b112011-10-26 21:06:34 +0000175 C.addTransition(lockFail);
Jordy Rosedcb1d5d2011-07-19 20:21:41 +0000176
177 } else if (semantics == PthreadSemantics) {
178 // Assume that the return value was 0.
Ted Kremenek28f47b92010-12-01 22:16:56 +0000179 lockSucc = state->assume(retVal, false);
Ted Kremenekac9bea82009-11-12 06:17:47 +0000180 assert(lockSucc);
Jordy Rosedcb1d5d2011-07-19 20:21:41 +0000181
182 } else {
183 // XNU locking semantics return void on non-try locks
184 assert((semantics == XNUSemantics) && "Unknown locking semantics");
185 lockSucc = state;
Ted Kremenekac9bea82009-11-12 06:17:47 +0000186 }
187
Jordy Rosedcb1d5d2011-07-19 20:21:41 +0000188 // Record that the lock was acquired.
Ted Kremenekac9bea82009-11-12 06:17:47 +0000189 lockSucc = lockSucc->add<LockSet>(lockR);
Stephen Hines651f13c2014-04-23 16:59:28 -0700190 lockSucc = lockSucc->set<LockMap>(lockR, LockState::getLocked());
Anna Zaks0bd6b112011-10-26 21:06:34 +0000191 C.addTransition(lockSucc);
Ted Kremenekac9bea82009-11-12 06:17:47 +0000192}
193
194void PthreadLockChecker::ReleaseLock(CheckerContext &C, const CallExpr *CE,
Argyrios Kyrtzidis983326f2011-02-23 01:05:36 +0000195 SVal lock) const {
Ted Kremenekac9bea82009-11-12 06:17:47 +0000196
197 const MemRegion *lockR = lock.getAsRegion();
198 if (!lockR)
199 return;
200
Ted Kremenek8bef8232012-01-26 21:29:00 +0000201 ProgramStateRef state = C.getState();
Stephen Hines651f13c2014-04-23 16:59:28 -0700202
203 if (const LockState *LState = state->get<LockMap>(lockR)) {
204 if (LState->isUnlocked()) {
205 if (!BT_doubleunlock)
206 BT_doubleunlock.reset(new BugType(this, "Double unlocking",
207 "Lock checker"));
208 ExplodedNode *N = C.generateSink();
209 if (!N)
210 return;
211 BugReport *Report = new BugReport(*BT_doubleunlock,
212 "This lock has already been unlocked",
213 N);
214 Report->addRange(CE->getArg(0)->getSourceRange());
215 C.emitReport(Report);
216 return;
217 } else if (LState->isDestroyed()) {
218 reportUseDestroyedBug(C, CE);
219 return;
220 }
221 }
222
Jordan Rose166d5022012-11-02 01:54:06 +0000223 LockSetTy LS = state->get<LockSet>();
Ted Kremenekac9bea82009-11-12 06:17:47 +0000224
Jordy Rosedcb1d5d2011-07-19 20:21:41 +0000225 // FIXME: Better analysis requires IPA for wrappers.
Stephen Hines651f13c2014-04-23 16:59:28 -0700226
227 if (!LS.isEmpty()) {
228 const MemRegion *firstLockR = LS.getHead();
229 if (firstLockR != lockR) {
230 if (!BT_lor)
231 BT_lor.reset(new BugType(this, "Lock order reversal", "Lock checker"));
232 ExplodedNode *N = C.generateSink();
233 if (!N)
234 return;
235 BugReport *report = new BugReport(*BT_lor,
236 "This was not the most recently "
237 "acquired lock. Possible lock order "
238 "reversal",
239 N);
240 report->addRange(CE->getArg(0)->getSourceRange());
241 C.emitReport(report);
Jordy Rosedcb1d5d2011-07-19 20:21:41 +0000242 return;
Stephen Hines651f13c2014-04-23 16:59:28 -0700243 }
244 // Record that the lock was released.
245 state = state->set<LockSet>(LS.getTail());
Jordy Rosedcb1d5d2011-07-19 20:21:41 +0000246 }
247
Stephen Hines651f13c2014-04-23 16:59:28 -0700248 state = state->set<LockMap>(lockR, LockState::getUnlocked());
Anna Zaks0bd6b112011-10-26 21:06:34 +0000249 C.addTransition(state);
Ted Kremenekac9bea82009-11-12 06:17:47 +0000250}
Argyrios Kyrtzidis983326f2011-02-23 01:05:36 +0000251
Stephen Hines651f13c2014-04-23 16:59:28 -0700252void PthreadLockChecker::DestroyLock(CheckerContext &C, const CallExpr *CE,
253 SVal Lock) const {
254
255 const MemRegion *LockR = Lock.getAsRegion();
256 if (!LockR)
257 return;
258
259 ProgramStateRef State = C.getState();
260
261 const LockState *LState = State->get<LockMap>(LockR);
262 if (!LState || LState->isUnlocked()) {
263 State = State->set<LockMap>(LockR, LockState::getDestroyed());
264 C.addTransition(State);
265 return;
266 }
267
268 StringRef Message;
269
270 if (LState->isLocked()) {
271 Message = "This lock is still locked";
272 } else {
273 Message = "This lock has already been destroyed";
274 }
275
276 if (!BT_destroylock)
277 BT_destroylock.reset(new BugType(this, "Destroy invalid lock",
278 "Lock checker"));
279 ExplodedNode *N = C.generateSink();
280 if (!N)
281 return;
282 BugReport *Report = new BugReport(*BT_destroylock, Message, N);
283 Report->addRange(CE->getArg(0)->getSourceRange());
284 C.emitReport(Report);
285}
286
287void PthreadLockChecker::InitLock(CheckerContext &C, const CallExpr *CE,
288 SVal Lock) const {
289
290 const MemRegion *LockR = Lock.getAsRegion();
291 if (!LockR)
292 return;
293
294 ProgramStateRef State = C.getState();
295
296 const struct LockState *LState = State->get<LockMap>(LockR);
297 if (!LState || LState->isDestroyed()) {
298 State = State->set<LockMap>(LockR, LockState::getUnlocked());
299 C.addTransition(State);
300 return;
301 }
302
303 StringRef Message;
304
305 if (LState->isLocked()) {
306 Message = "This lock is still being held";
307 } else {
308 Message = "This lock has already been initialized";
309 }
310
311 if (!BT_initlock)
312 BT_initlock.reset(new BugType(this, "Init invalid lock",
313 "Lock checker"));
314 ExplodedNode *N = C.generateSink();
315 if (!N)
316 return;
317 BugReport *Report = new BugReport(*BT_initlock, Message, N);
318 Report->addRange(CE->getArg(0)->getSourceRange());
319 C.emitReport(Report);
320}
321
322void PthreadLockChecker::reportUseDestroyedBug(CheckerContext &C,
323 const CallExpr *CE) const {
324 if (!BT_destroylock)
325 BT_destroylock.reset(new BugType(this, "Use destroyed lock",
326 "Lock checker"));
327 ExplodedNode *N = C.generateSink();
328 if (!N)
329 return;
330 BugReport *Report = new BugReport(*BT_destroylock,
331 "This lock has already been destroyed",
332 N);
333 Report->addRange(CE->getArg(0)->getSourceRange());
334 C.emitReport(Report);
335}
Jordy Rosedcb1d5d2011-07-19 20:21:41 +0000336
Argyrios Kyrtzidis983326f2011-02-23 01:05:36 +0000337void ento::registerPthreadLockChecker(CheckerManager &mgr) {
338 mgr.registerChecker<PthreadLockChecker>();
339}