2011-07-20 04:21:41 +08:00
|
|
|
//===--- PthreadLockChecker.cpp - Check for locking problems ---*- C++ -*--===//
|
2009-11-12 14:17:47 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2011-07-20 04:31:42 +08:00
|
|
|
// This defines PthreadLockChecker, a simple lock -> unlock checker.
|
|
|
|
// Also handles XNU locks, which behave similarly enough to share code.
|
2009-11-12 14:17:47 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-02-16 05:25:03 +08:00
|
|
|
#include "ClangSACheckers.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
|
2011-03-01 09:16:21 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/Checker.h"
|
2011-02-18 05:39:17 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
|
2011-02-23 09:05:36 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
|
2011-08-16 06:09:50 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
|
2011-07-20 04:21:41 +08:00
|
|
|
#include "llvm/ADT/ImmutableList.h"
|
2009-11-12 14:17:47 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
2010-12-23 15:20:52 +08:00
|
|
|
using namespace ento;
|
2009-11-12 14:17:47 +08:00
|
|
|
|
|
|
|
namespace {
|
2011-07-20 04:21:41 +08:00
|
|
|
class PthreadLockChecker : public Checker< check::PostStmt<CallExpr> > {
|
2012-02-05 10:12:40 +08:00
|
|
|
mutable OwningPtr<BugType> BT_doublelock;
|
|
|
|
mutable OwningPtr<BugType> BT_lor;
|
2011-07-20 04:21:41 +08:00
|
|
|
enum LockingSemantics {
|
|
|
|
NotApplicable = 0,
|
|
|
|
PthreadSemantics,
|
|
|
|
XNUSemantics
|
|
|
|
};
|
2009-11-12 14:17:47 +08:00
|
|
|
public:
|
2011-02-23 09:05:36 +08:00
|
|
|
void checkPostStmt(const CallExpr *CE, CheckerContext &C) const;
|
2009-11-12 14:17:47 +08:00
|
|
|
|
2011-07-20 04:21:41 +08:00
|
|
|
void AcquireLock(CheckerContext &C, const CallExpr *CE, SVal lock,
|
|
|
|
bool isTryLock, enum LockingSemantics semantics) const;
|
2009-11-12 14:17:47 +08:00
|
|
|
|
2011-07-20 04:21:41 +08:00
|
|
|
void ReleaseLock(CheckerContext &C, const CallExpr *CE, SVal lock) const;
|
2009-11-12 14:17:47 +08:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
// GDM Entry for tracking lock state.
|
2012-11-02 09:54:06 +08:00
|
|
|
REGISTER_LIST_WITH_PROGRAMSTATE(LockSet, const MemRegion *)
|
2009-11-12 14:17:47 +08:00
|
|
|
|
2011-02-18 05:39:17 +08:00
|
|
|
|
2011-02-23 09:05:36 +08:00
|
|
|
void PthreadLockChecker::checkPostStmt(const CallExpr *CE,
|
|
|
|
CheckerContext &C) const {
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef state = C.getState();
|
2012-01-07 06:09:28 +08:00
|
|
|
const LocationContext *LCtx = C.getLocationContext();
|
2011-12-01 13:57:37 +08:00
|
|
|
StringRef FName = C.getCalleeName(CE);
|
|
|
|
if (FName.empty())
|
2010-11-02 07:16:05 +08:00
|
|
|
return;
|
2011-07-20 04:21:41 +08:00
|
|
|
|
|
|
|
if (CE->getNumArgs() != 1)
|
|
|
|
return;
|
2011-07-20 04:31:42 +08:00
|
|
|
|
2011-07-20 04:21:41 +08:00
|
|
|
if (FName == "pthread_mutex_lock" ||
|
|
|
|
FName == "pthread_rwlock_rdlock" ||
|
|
|
|
FName == "pthread_rwlock_wrlock")
|
2012-01-07 06:09:28 +08:00
|
|
|
AcquireLock(C, CE, state->getSVal(CE->getArg(0), LCtx),
|
|
|
|
false, PthreadSemantics);
|
2011-07-20 04:21:41 +08:00
|
|
|
else if (FName == "lck_mtx_lock" ||
|
|
|
|
FName == "lck_rw_lock_exclusive" ||
|
|
|
|
FName == "lck_rw_lock_shared")
|
2012-01-07 06:09:28 +08:00
|
|
|
AcquireLock(C, CE, state->getSVal(CE->getArg(0), LCtx),
|
|
|
|
false, XNUSemantics);
|
2011-07-20 04:21:41 +08:00
|
|
|
else if (FName == "pthread_mutex_trylock" ||
|
|
|
|
FName == "pthread_rwlock_tryrdlock" ||
|
|
|
|
FName == "pthread_rwlock_tryrwlock")
|
2012-01-07 06:09:28 +08:00
|
|
|
AcquireLock(C, CE, state->getSVal(CE->getArg(0), LCtx),
|
|
|
|
true, PthreadSemantics);
|
2011-07-20 04:21:41 +08:00
|
|
|
else if (FName == "lck_mtx_try_lock" ||
|
|
|
|
FName == "lck_rw_try_lock_exclusive" ||
|
|
|
|
FName == "lck_rw_try_lock_shared")
|
2012-01-07 06:09:28 +08:00
|
|
|
AcquireLock(C, CE, state->getSVal(CE->getArg(0), LCtx),
|
|
|
|
true, XNUSemantics);
|
2011-07-20 04:21:41 +08:00
|
|
|
else if (FName == "pthread_mutex_unlock" ||
|
|
|
|
FName == "pthread_rwlock_unlock" ||
|
|
|
|
FName == "lck_mtx_unlock" ||
|
|
|
|
FName == "lck_rw_done")
|
2012-01-07 06:09:28 +08:00
|
|
|
ReleaseLock(C, CE, state->getSVal(CE->getArg(0), LCtx));
|
2009-11-12 14:17:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PthreadLockChecker::AcquireLock(CheckerContext &C, const CallExpr *CE,
|
2011-07-20 04:21:41 +08:00
|
|
|
SVal lock, bool isTryLock,
|
|
|
|
enum LockingSemantics semantics) const {
|
2009-11-12 14:17:47 +08:00
|
|
|
|
|
|
|
const MemRegion *lockR = lock.getAsRegion();
|
|
|
|
if (!lockR)
|
|
|
|
return;
|
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef state = C.getState();
|
2009-11-12 14:17:47 +08:00
|
|
|
|
2012-01-07 06:09:28 +08:00
|
|
|
SVal X = state->getSVal(CE, C.getLocationContext());
|
2009-11-12 14:17:47 +08:00
|
|
|
if (X.isUnknownOrUndef())
|
|
|
|
return;
|
|
|
|
|
|
|
|
DefinedSVal retVal = cast<DefinedSVal>(X);
|
2011-07-20 04:21:41 +08:00
|
|
|
|
|
|
|
if (state->contains<LockSet>(lockR)) {
|
|
|
|
if (!BT_doublelock)
|
|
|
|
BT_doublelock.reset(new BugType("Double locking", "Lock checker"));
|
|
|
|
ExplodedNode *N = C.generateSink();
|
|
|
|
if (!N)
|
|
|
|
return;
|
2011-08-18 07:00:25 +08:00
|
|
|
BugReport *report = new BugReport(*BT_doublelock,
|
2011-07-20 04:21:41 +08:00
|
|
|
"This lock has already "
|
|
|
|
"been acquired", N);
|
|
|
|
report->addRange(CE->getArg(0)->getSourceRange());
|
2012-11-02 09:53:40 +08:00
|
|
|
C.emitReport(report);
|
2011-07-20 04:21:41 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef lockSucc = state;
|
2009-11-12 14:17:47 +08:00
|
|
|
if (isTryLock) {
|
2011-07-20 04:21:41 +08:00
|
|
|
// Bifurcate the state, and allow a mode where the lock acquisition fails.
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef lockFail;
|
2011-07-20 04:21:41 +08:00
|
|
|
switch (semantics) {
|
|
|
|
case PthreadSemantics:
|
|
|
|
llvm::tie(lockFail, lockSucc) = state->assume(retVal);
|
|
|
|
break;
|
|
|
|
case XNUSemantics:
|
|
|
|
llvm::tie(lockSucc, lockFail) = state->assume(retVal);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Unknown tryLock locking semantics");
|
|
|
|
}
|
2009-11-12 14:17:47 +08:00
|
|
|
assert(lockFail && lockSucc);
|
2011-10-27 05:06:34 +08:00
|
|
|
C.addTransition(lockFail);
|
2011-07-20 04:21:41 +08:00
|
|
|
|
|
|
|
} else if (semantics == PthreadSemantics) {
|
|
|
|
// Assume that the return value was 0.
|
2010-12-02 06:16:56 +08:00
|
|
|
lockSucc = state->assume(retVal, false);
|
2009-11-12 14:17:47 +08:00
|
|
|
assert(lockSucc);
|
2011-07-20 04:21:41 +08:00
|
|
|
|
|
|
|
} else {
|
|
|
|
// XNU locking semantics return void on non-try locks
|
|
|
|
assert((semantics == XNUSemantics) && "Unknown locking semantics");
|
|
|
|
lockSucc = state;
|
2009-11-12 14:17:47 +08:00
|
|
|
}
|
|
|
|
|
2011-07-20 04:21:41 +08:00
|
|
|
// Record that the lock was acquired.
|
2009-11-12 14:17:47 +08:00
|
|
|
lockSucc = lockSucc->add<LockSet>(lockR);
|
2011-10-27 05:06:34 +08:00
|
|
|
C.addTransition(lockSucc);
|
2009-11-12 14:17:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PthreadLockChecker::ReleaseLock(CheckerContext &C, const CallExpr *CE,
|
2011-02-23 09:05:36 +08:00
|
|
|
SVal lock) const {
|
2009-11-12 14:17:47 +08:00
|
|
|
|
|
|
|
const MemRegion *lockR = lock.getAsRegion();
|
|
|
|
if (!lockR)
|
|
|
|
return;
|
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef state = C.getState();
|
2012-11-02 09:54:06 +08:00
|
|
|
LockSetTy LS = state->get<LockSet>();
|
2009-11-12 14:17:47 +08:00
|
|
|
|
2011-07-20 04:21:41 +08:00
|
|
|
// FIXME: Better analysis requires IPA for wrappers.
|
|
|
|
// FIXME: check for double unlocks
|
|
|
|
if (LS.isEmpty())
|
2009-11-12 14:17:47 +08:00
|
|
|
return;
|
|
|
|
|
2011-07-20 04:21:41 +08:00
|
|
|
const MemRegion *firstLockR = LS.getHead();
|
|
|
|
if (firstLockR != lockR) {
|
|
|
|
if (!BT_lor)
|
|
|
|
BT_lor.reset(new BugType("Lock order reversal", "Lock checker"));
|
|
|
|
ExplodedNode *N = C.generateSink();
|
|
|
|
if (!N)
|
|
|
|
return;
|
2011-08-18 07:00:25 +08:00
|
|
|
BugReport *report = new BugReport(*BT_lor,
|
2011-07-20 04:21:41 +08:00
|
|
|
"This was not the most "
|
|
|
|
"recently acquired lock. "
|
|
|
|
"Possible lock order "
|
|
|
|
"reversal", N);
|
|
|
|
report->addRange(CE->getArg(0)->getSourceRange());
|
2012-11-02 09:53:40 +08:00
|
|
|
C.emitReport(report);
|
2011-07-20 04:21:41 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Record that the lock was released.
|
|
|
|
state = state->set<LockSet>(LS.getTail());
|
2011-10-27 05:06:34 +08:00
|
|
|
C.addTransition(state);
|
2009-11-12 14:17:47 +08:00
|
|
|
}
|
2011-02-23 09:05:36 +08:00
|
|
|
|
2011-07-20 04:21:41 +08:00
|
|
|
|
2011-02-23 09:05:36 +08:00
|
|
|
void ento::registerPthreadLockChecker(CheckerManager &mgr) {
|
|
|
|
mgr.registerChecker<PthreadLockChecker>();
|
|
|
|
}
|