blob: 28a4a083ea3cd452e8139d7b0424568feb4af757 [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:
Eugene Zelenkod4304d22015-11-04 21:37:17 +000035 static LockState getLocked() { return LockState(Locked); }
36 static LockState getUnlocked() { return LockState(Unlocked); }
37 static LockState getDestroyed() { return LockState(Destroyed); }
Jordan Rose7fcaa142014-04-01 03:40:47 +000038
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;
Jordan Rose3a176ed2014-04-01 03:40:53 +000056 mutable std::unique_ptr<BugType> BT_initlock;
Ahmed Charlesb8984322014-03-07 20:03:18 +000057 mutable std::unique_ptr<BugType> BT_lor;
Jordy Rosed9c52212011-07-19 20:21:41 +000058 enum LockingSemantics {
59 NotApplicable = 0,
60 PthreadSemantics,
61 XNUSemantics
62 };
Ted Kremenekd48568f2009-11-12 06:17:47 +000063public:
Argyrios Kyrtzidisdff865d2011-02-23 01:05:36 +000064 void checkPostStmt(const CallExpr *CE, CheckerContext &C) const;
Ted Kremenek3a0678e2015-09-08 03:50:52 +000065
Jordy Rosed9c52212011-07-19 20:21:41 +000066 void AcquireLock(CheckerContext &C, const CallExpr *CE, SVal lock,
67 bool isTryLock, enum LockingSemantics semantics) const;
Ted Kremenek3a0678e2015-09-08 03:50:52 +000068
Jordy Rosed9c52212011-07-19 20:21:41 +000069 void ReleaseLock(CheckerContext &C, const CallExpr *CE, SVal lock) const;
Jordan Rose7fcaa142014-04-01 03:40:47 +000070 void DestroyLock(CheckerContext &C, const CallExpr *CE, SVal Lock) const;
Jordan Rose3a176ed2014-04-01 03:40:53 +000071 void InitLock(CheckerContext &C, const CallExpr *CE, SVal Lock) const;
Jordan Rose7fcaa142014-04-01 03:40:47 +000072 void reportUseDestroyedBug(CheckerContext &C, const CallExpr *CE) const;
Ted Kremenekd48568f2009-11-12 06:17:47 +000073};
74} // end anonymous namespace
75
76// GDM Entry for tracking lock state.
Jordan Rose0c153cb2012-11-02 01:54:06 +000077REGISTER_LIST_WITH_PROGRAMSTATE(LockSet, const MemRegion *)
Ted Kremenekd48568f2009-11-12 06:17:47 +000078
Jordan Rose7fcaa142014-04-01 03:40:47 +000079REGISTER_MAP_WITH_PROGRAMSTATE(LockMap, const MemRegion *, LockState)
Ted Kremenekd48568f2009-11-12 06:17:47 +000080
Argyrios Kyrtzidisdff865d2011-02-23 01:05:36 +000081void PthreadLockChecker::checkPostStmt(const CallExpr *CE,
82 CheckerContext &C) const {
Ted Kremenek49b1e382012-01-26 21:29:00 +000083 ProgramStateRef state = C.getState();
Ted Kremenek632e3b72012-01-06 22:09:28 +000084 const LocationContext *LCtx = C.getLocationContext();
Anna Zaksc6aa5312011-12-01 05:57:37 +000085 StringRef FName = C.getCalleeName(CE);
86 if (FName.empty())
Ted Kremenekd48568f2009-11-12 06:17:47 +000087 return;
Jordy Rosea39e10f2011-07-19 20:31:42 +000088
Jordan Rose7fcaa142014-04-01 03:40:47 +000089 if (CE->getNumArgs() != 1 && CE->getNumArgs() != 2)
Jordy Rosed9c52212011-07-19 20:21:41 +000090 return;
Jordy Rosea39e10f2011-07-19 20:31:42 +000091
Jordy Rosed9c52212011-07-19 20:21:41 +000092 if (FName == "pthread_mutex_lock" ||
93 FName == "pthread_rwlock_rdlock" ||
94 FName == "pthread_rwlock_wrlock")
Ted Kremenek632e3b72012-01-06 22:09:28 +000095 AcquireLock(C, CE, state->getSVal(CE->getArg(0), LCtx),
96 false, PthreadSemantics);
Jordy Rosed9c52212011-07-19 20:21:41 +000097 else if (FName == "lck_mtx_lock" ||
98 FName == "lck_rw_lock_exclusive" ||
Ted Kremenek3a0678e2015-09-08 03:50:52 +000099 FName == "lck_rw_lock_shared")
Ted Kremenek632e3b72012-01-06 22:09:28 +0000100 AcquireLock(C, CE, state->getSVal(CE->getArg(0), LCtx),
101 false, XNUSemantics);
Jordy Rosed9c52212011-07-19 20:21:41 +0000102 else if (FName == "pthread_mutex_trylock" ||
103 FName == "pthread_rwlock_tryrdlock" ||
Ted Kremenek115c3f7a2014-01-17 16:06:43 +0000104 FName == "pthread_rwlock_trywrlock")
Ted Kremenek632e3b72012-01-06 22:09:28 +0000105 AcquireLock(C, CE, state->getSVal(CE->getArg(0), LCtx),
106 true, PthreadSemantics);
Jordy Rosed9c52212011-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 Kremenek632e3b72012-01-06 22:09:28 +0000110 AcquireLock(C, CE, state->getSVal(CE->getArg(0), LCtx),
111 true, XNUSemantics);
Jordy Rosed9c52212011-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 Kremenek632e3b72012-01-06 22:09:28 +0000116 ReleaseLock(C, CE, state->getSVal(CE->getArg(0), LCtx));
Jordan Rose7fcaa142014-04-01 03:40:47 +0000117 else if (FName == "pthread_mutex_destroy" ||
118 FName == "lck_mtx_destroy")
119 DestroyLock(C, CE, state->getSVal(CE->getArg(0), LCtx));
Jordan Rose3a176ed2014-04-01 03:40:53 +0000120 else if (FName == "pthread_mutex_init")
121 InitLock(C, CE, state->getSVal(CE->getArg(0), LCtx));
Ted Kremenekd48568f2009-11-12 06:17:47 +0000122}
123
124void PthreadLockChecker::AcquireLock(CheckerContext &C, const CallExpr *CE,
Jordy Rosed9c52212011-07-19 20:21:41 +0000125 SVal lock, bool isTryLock,
126 enum LockingSemantics semantics) const {
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000127
Ted Kremenekd48568f2009-11-12 06:17:47 +0000128 const MemRegion *lockR = lock.getAsRegion();
129 if (!lockR)
130 return;
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000131
Ted Kremenek49b1e382012-01-26 21:29:00 +0000132 ProgramStateRef state = C.getState();
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000133
Ted Kremenek632e3b72012-01-06 22:09:28 +0000134 SVal X = state->getSVal(CE, C.getLocationContext());
Ted Kremenekd48568f2009-11-12 06:17:47 +0000135 if (X.isUnknownOrUndef())
136 return;
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000137
David Blaikie2fdacbc2013-02-20 05:52:05 +0000138 DefinedSVal retVal = X.castAs<DefinedSVal>();
Jordy Rosed9c52212011-07-19 20:21:41 +0000139
Jordan Rose7fcaa142014-04-01 03:40:47 +0000140 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"));
Devin Coughline39bd402015-09-16 22:03:05 +0000145 ExplodedNode *N = C.generateErrorNode();
Jordan Rose7fcaa142014-04-01 03:40:47 +0000146 if (!N)
147 return;
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000148 auto report = llvm::make_unique<BugReport>(
149 *BT_doublelock, "This lock has already been acquired", N);
Jordan Rose7fcaa142014-04-01 03:40:47 +0000150 report->addRange(CE->getArg(0)->getSourceRange());
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000151 C.emitReport(std::move(report));
Jordy Rosed9c52212011-07-19 20:21:41 +0000152 return;
Jordan Rose7fcaa142014-04-01 03:40:47 +0000153 } else if (LState->isDestroyed()) {
154 reportUseDestroyedBug(C, CE);
155 return;
156 }
Ted Kremenekd48568f2009-11-12 06:17:47 +0000157 }
Jordy Rosed9c52212011-07-19 20:21:41 +0000158
Ted Kremenek49b1e382012-01-26 21:29:00 +0000159 ProgramStateRef lockSucc = state;
Jordy Rosed9c52212011-07-19 20:21:41 +0000160 if (isTryLock) {
161 // Bifurcate the state, and allow a mode where the lock acquisition fails.
Ted Kremenek49b1e382012-01-26 21:29:00 +0000162 ProgramStateRef lockFail;
Jordy Rosed9c52212011-07-19 20:21:41 +0000163 switch (semantics) {
164 case PthreadSemantics:
Benjamin Kramer867ea1d2014-03-02 13:01:17 +0000165 std::tie(lockFail, lockSucc) = state->assume(retVal);
Jordy Rosed9c52212011-07-19 20:21:41 +0000166 break;
167 case XNUSemantics:
Benjamin Kramer867ea1d2014-03-02 13:01:17 +0000168 std::tie(lockSucc, lockFail) = state->assume(retVal);
Jordy Rosed9c52212011-07-19 20:21:41 +0000169 break;
170 default:
171 llvm_unreachable("Unknown tryLock locking semantics");
Jordy Rosed9c52212011-07-19 20:21:41 +0000172 }
173 assert(lockFail && lockSucc);
Anna Zaksda4c8d62011-10-26 21:06:34 +0000174 C.addTransition(lockFail);
Jordy Rosed9c52212011-07-19 20:21:41 +0000175
176 } else if (semantics == PthreadSemantics) {
177 // Assume that the return value was 0.
Ted Kremenekc5bea1e2010-12-01 22:16:56 +0000178 lockSucc = state->assume(retVal, false);
Ted Kremenekd48568f2009-11-12 06:17:47 +0000179 assert(lockSucc);
Jordy Rosed9c52212011-07-19 20:21:41 +0000180
181 } else {
182 // XNU locking semantics return void on non-try locks
183 assert((semantics == XNUSemantics) && "Unknown locking semantics");
184 lockSucc = state;
Ted Kremenekd48568f2009-11-12 06:17:47 +0000185 }
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000186
187 // Record that the lock was acquired.
Ted Kremenekd48568f2009-11-12 06:17:47 +0000188 lockSucc = lockSucc->add<LockSet>(lockR);
Jordan Rose7fcaa142014-04-01 03:40:47 +0000189 lockSucc = lockSucc->set<LockMap>(lockR, LockState::getLocked());
Anna Zaksda4c8d62011-10-26 21:06:34 +0000190 C.addTransition(lockSucc);
Ted Kremenekd48568f2009-11-12 06:17:47 +0000191}
192
193void PthreadLockChecker::ReleaseLock(CheckerContext &C, const CallExpr *CE,
Argyrios Kyrtzidisdff865d2011-02-23 01:05:36 +0000194 SVal lock) const {
Ted Kremenekd48568f2009-11-12 06:17:47 +0000195
196 const MemRegion *lockR = lock.getAsRegion();
197 if (!lockR)
198 return;
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000199
Ted Kremenek49b1e382012-01-26 21:29:00 +0000200 ProgramStateRef state = C.getState();
Ted Kremenekd48568f2009-11-12 06:17:47 +0000201
Jordan Rose7fcaa142014-04-01 03:40:47 +0000202 if (const LockState *LState = state->get<LockMap>(lockR)) {
203 if (LState->isUnlocked()) {
204 if (!BT_doubleunlock)
205 BT_doubleunlock.reset(new BugType(this, "Double unlocking",
206 "Lock checker"));
Devin Coughline39bd402015-09-16 22:03:05 +0000207 ExplodedNode *N = C.generateErrorNode();
Jordan Rose7fcaa142014-04-01 03:40:47 +0000208 if (!N)
209 return;
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000210 auto Report = llvm::make_unique<BugReport>(
211 *BT_doubleunlock, "This lock has already been unlocked", N);
Jordan Rose7fcaa142014-04-01 03:40:47 +0000212 Report->addRange(CE->getArg(0)->getSourceRange());
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000213 C.emitReport(std::move(Report));
Jordy Rosed9c52212011-07-19 20:21:41 +0000214 return;
Jordan Rose7fcaa142014-04-01 03:40:47 +0000215 } else if (LState->isDestroyed()) {
216 reportUseDestroyedBug(C, CE);
217 return;
218 }
Jordy Rosed9c52212011-07-19 20:21:41 +0000219 }
220
Jordan Rose0696bb42014-04-01 03:40:38 +0000221 LockSetTy LS = state->get<LockSet>();
222
223 // FIXME: Better analysis requires IPA for wrappers.
224
225 if (!LS.isEmpty()) {
226 const MemRegion *firstLockR = LS.getHead();
227 if (firstLockR != lockR) {
228 if (!BT_lor)
229 BT_lor.reset(new BugType(this, "Lock order reversal", "Lock checker"));
Devin Coughline39bd402015-09-16 22:03:05 +0000230 ExplodedNode *N = C.generateErrorNode();
Jordan Rose0696bb42014-04-01 03:40:38 +0000231 if (!N)
232 return;
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000233 auto report = llvm::make_unique<BugReport>(
234 *BT_lor, "This was not the most recently acquired lock. Possible "
235 "lock order reversal", N);
Jordan Rose0696bb42014-04-01 03:40:38 +0000236 report->addRange(CE->getArg(0)->getSourceRange());
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000237 C.emitReport(std::move(report));
Jordan Rose0696bb42014-04-01 03:40:38 +0000238 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"));
Devin Coughline39bd402015-09-16 22:03:05 +0000275 ExplodedNode *N = C.generateErrorNode();
Jordan Rose7fcaa142014-04-01 03:40:47 +0000276 if (!N)
277 return;
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000278 auto Report = llvm::make_unique<BugReport>(*BT_destroylock, Message, N);
Jordan Rose7fcaa142014-04-01 03:40:47 +0000279 Report->addRange(CE->getArg(0)->getSourceRange());
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000280 C.emitReport(std::move(Report));
Jordan Rose7fcaa142014-04-01 03:40:47 +0000281}
282
Jordan Rose3a176ed2014-04-01 03:40:53 +0000283void PthreadLockChecker::InitLock(CheckerContext &C, const CallExpr *CE,
284 SVal Lock) const {
285
286 const MemRegion *LockR = Lock.getAsRegion();
287 if (!LockR)
288 return;
289
290 ProgramStateRef State = C.getState();
291
292 const struct LockState *LState = State->get<LockMap>(LockR);
293 if (!LState || LState->isDestroyed()) {
294 State = State->set<LockMap>(LockR, LockState::getUnlocked());
295 C.addTransition(State);
296 return;
297 }
298
299 StringRef Message;
300
301 if (LState->isLocked()) {
302 Message = "This lock is still being held";
303 } else {
304 Message = "This lock has already been initialized";
305 }
306
307 if (!BT_initlock)
308 BT_initlock.reset(new BugType(this, "Init invalid lock",
309 "Lock checker"));
Devin Coughline39bd402015-09-16 22:03:05 +0000310 ExplodedNode *N = C.generateErrorNode();
Jordan Rose3a176ed2014-04-01 03:40:53 +0000311 if (!N)
312 return;
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000313 auto Report = llvm::make_unique<BugReport>(*BT_initlock, Message, N);
Jordan Rose3a176ed2014-04-01 03:40:53 +0000314 Report->addRange(CE->getArg(0)->getSourceRange());
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000315 C.emitReport(std::move(Report));
Jordan Rose3a176ed2014-04-01 03:40:53 +0000316}
317
Jordan Rose7fcaa142014-04-01 03:40:47 +0000318void PthreadLockChecker::reportUseDestroyedBug(CheckerContext &C,
319 const CallExpr *CE) const {
320 if (!BT_destroylock)
321 BT_destroylock.reset(new BugType(this, "Use destroyed lock",
322 "Lock checker"));
Devin Coughline39bd402015-09-16 22:03:05 +0000323 ExplodedNode *N = C.generateErrorNode();
Jordan Rose7fcaa142014-04-01 03:40:47 +0000324 if (!N)
325 return;
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000326 auto Report = llvm::make_unique<BugReport>(
327 *BT_destroylock, "This lock has already been destroyed", N);
Jordan Rose7fcaa142014-04-01 03:40:47 +0000328 Report->addRange(CE->getArg(0)->getSourceRange());
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000329 C.emitReport(std::move(Report));
Jordan Rose7fcaa142014-04-01 03:40:47 +0000330}
331
Argyrios Kyrtzidisdff865d2011-02-23 01:05:36 +0000332void ento::registerPthreadLockChecker(CheckerManager &mgr) {
333 mgr.registerChecker<PthreadLockChecker>();
334}