1 //===--- PthreadLockChecker.cpp - Check for locking problems ---*- C++ -*--===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This defines PthreadLockChecker, a simple lock -> unlock checker.
11 // Also handles XNU locks, which behave similarly enough to share code.
13 //===----------------------------------------------------------------------===//
15 #include "ClangSACheckers.h"
16 #include "clang/StaticAnalyzer/Core/Checker.h"
17 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
18 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
19 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
20 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
21 #include "llvm/ADT/ImmutableList.h"
23 using namespace clang;
27 class PthreadLockChecker : public Checker< check::PostStmt<CallExpr> > {
28 mutable llvm::OwningPtr<BugType> BT_doublelock;
29 mutable llvm::OwningPtr<BugType> BT_lor;
30 enum LockingSemantics {
36 void checkPostStmt(const CallExpr *CE, CheckerContext &C) const;
38 void AcquireLock(CheckerContext &C, const CallExpr *CE, SVal lock,
39 bool isTryLock, enum LockingSemantics semantics) const;
41 void ReleaseLock(CheckerContext &C, const CallExpr *CE, SVal lock) const;
43 } // end anonymous namespace
45 // GDM Entry for tracking lock state.
46 namespace { class LockSet {}; }
49 template <> struct ProgramStateTrait<LockSet> :
50 public ProgramStatePartialTrait<llvm::ImmutableList<const MemRegion*> > {
51 static void *GDMIndex() { static int x = 0; return &x; }
53 } // end of ento (ProgramState) namespace
54 } // end clang namespace
57 void PthreadLockChecker::checkPostStmt(const CallExpr *CE,
58 CheckerContext &C) const {
59 const ProgramState *state = C.getState();
60 const Expr *Callee = CE->getCallee();
61 const FunctionDecl *FD = state->getSVal(Callee).getAsFunctionDecl();
66 // Get the name of the callee.
67 IdentifierInfo *II = FD->getIdentifier();
68 if (!II) // if no identifier, not a simple C function
70 StringRef FName = II->getName();
72 if (CE->getNumArgs() != 1)
75 if (FName == "pthread_mutex_lock" ||
76 FName == "pthread_rwlock_rdlock" ||
77 FName == "pthread_rwlock_wrlock")
78 AcquireLock(C, CE, state->getSVal(CE->getArg(0)), false, PthreadSemantics);
79 else if (FName == "lck_mtx_lock" ||
80 FName == "lck_rw_lock_exclusive" ||
81 FName == "lck_rw_lock_shared")
82 AcquireLock(C, CE, state->getSVal(CE->getArg(0)), false, XNUSemantics);
83 else if (FName == "pthread_mutex_trylock" ||
84 FName == "pthread_rwlock_tryrdlock" ||
85 FName == "pthread_rwlock_tryrwlock")
86 AcquireLock(C, CE, state->getSVal(CE->getArg(0)), true, PthreadSemantics);
87 else if (FName == "lck_mtx_try_lock" ||
88 FName == "lck_rw_try_lock_exclusive" ||
89 FName == "lck_rw_try_lock_shared")
90 AcquireLock(C, CE, state->getSVal(CE->getArg(0)), true, XNUSemantics);
91 else if (FName == "pthread_mutex_unlock" ||
92 FName == "pthread_rwlock_unlock" ||
93 FName == "lck_mtx_unlock" ||
94 FName == "lck_rw_done")
95 ReleaseLock(C, CE, state->getSVal(CE->getArg(0)));
98 void PthreadLockChecker::AcquireLock(CheckerContext &C, const CallExpr *CE,
99 SVal lock, bool isTryLock,
100 enum LockingSemantics semantics) const {
102 const MemRegion *lockR = lock.getAsRegion();
106 const ProgramState *state = C.getState();
108 SVal X = state->getSVal(CE);
109 if (X.isUnknownOrUndef())
112 DefinedSVal retVal = cast<DefinedSVal>(X);
114 if (state->contains<LockSet>(lockR)) {
116 BT_doublelock.reset(new BugType("Double locking", "Lock checker"));
117 ExplodedNode *N = C.generateSink();
120 BugReport *report = new BugReport(*BT_doublelock,
121 "This lock has already "
123 report->addRange(CE->getArg(0)->getSourceRange());
124 C.EmitReport(report);
128 const ProgramState *lockSucc = state;
130 // Bifurcate the state, and allow a mode where the lock acquisition fails.
131 const ProgramState *lockFail;
133 case PthreadSemantics:
134 llvm::tie(lockFail, lockSucc) = state->assume(retVal);
137 llvm::tie(lockSucc, lockFail) = state->assume(retVal);
140 llvm_unreachable("Unknown tryLock locking semantics");
143 assert(lockFail && lockSucc);
144 C.addTransition(lockFail);
146 } else if (semantics == PthreadSemantics) {
147 // Assume that the return value was 0.
148 lockSucc = state->assume(retVal, false);
152 // XNU locking semantics return void on non-try locks
153 assert((semantics == XNUSemantics) && "Unknown locking semantics");
157 // Record that the lock was acquired.
158 lockSucc = lockSucc->add<LockSet>(lockR);
159 C.addTransition(lockSucc);
162 void PthreadLockChecker::ReleaseLock(CheckerContext &C, const CallExpr *CE,
165 const MemRegion *lockR = lock.getAsRegion();
169 const ProgramState *state = C.getState();
170 llvm::ImmutableList<const MemRegion*> LS = state->get<LockSet>();
172 // FIXME: Better analysis requires IPA for wrappers.
173 // FIXME: check for double unlocks
177 const MemRegion *firstLockR = LS.getHead();
178 if (firstLockR != lockR) {
180 BT_lor.reset(new BugType("Lock order reversal", "Lock checker"));
181 ExplodedNode *N = C.generateSink();
184 BugReport *report = new BugReport(*BT_lor,
185 "This was not the most "
186 "recently acquired lock. "
187 "Possible lock order "
189 report->addRange(CE->getArg(0)->getSourceRange());
190 C.EmitReport(report);
194 // Record that the lock was released.
195 state = state->set<LockSet>(LS.getTail());
196 C.addTransition(state);
200 void ento::registerPthreadLockChecker(CheckerManager &mgr) {
201 mgr.registerChecker<PthreadLockChecker>();