2010-09-10 11:45:29 +08:00
|
|
|
//== ObjCAtSyncChecker.cpp - nil mutex checker for @synchronized -*- C++ -*--=//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This defines ObjCAtSyncChecker, a builtin check that checks for null pointers
|
|
|
|
// used as mutexes for @synchronized.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-02-15 15:42:33 +08:00
|
|
|
#include "ClangSACheckers.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 15:19:18 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
|
2011-02-10 09:03:03 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
|
2010-12-24 03:38:26 +08:00
|
|
|
#include "clang/StaticAnalyzer/Checkers/DereferenceChecker.h"
|
2011-02-10 09:03:03 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
|
2010-09-10 11:45:29 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
2010-12-23 15:20:52 +08:00
|
|
|
using namespace ento;
|
2010-09-10 11:45:29 +08:00
|
|
|
|
|
|
|
namespace {
|
2011-02-23 15:19:18 +08:00
|
|
|
class ObjCAtSyncChecker
|
2011-03-01 09:16:21 +08:00
|
|
|
: public Checker< check::PreStmt<ObjCAtSynchronizedStmt> > {
|
2011-02-23 15:19:18 +08:00
|
|
|
mutable llvm::OwningPtr<BuiltinBug> BT_null;
|
|
|
|
mutable llvm::OwningPtr<BuiltinBug> BT_undef;
|
|
|
|
|
2010-09-10 11:45:29 +08:00
|
|
|
public:
|
2011-02-23 15:19:18 +08:00
|
|
|
void checkPreStmt(const ObjCAtSynchronizedStmt *S, CheckerContext &C) const;
|
2010-09-10 11:45:29 +08:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2011-02-23 15:19:18 +08:00
|
|
|
void ObjCAtSyncChecker::checkPreStmt(const ObjCAtSynchronizedStmt *S,
|
|
|
|
CheckerContext &C) const {
|
2010-09-10 11:45:29 +08:00
|
|
|
|
|
|
|
const Expr *Ex = S->getSynchExpr();
|
|
|
|
const GRState *state = C.getState();
|
|
|
|
SVal V = state->getSVal(Ex);
|
|
|
|
|
|
|
|
// Uninitialized value used for the mutex?
|
|
|
|
if (isa<UndefinedVal>(V)) {
|
2010-12-21 05:19:09 +08:00
|
|
|
if (ExplodedNode *N = C.generateSink()) {
|
2010-09-10 11:45:29 +08:00
|
|
|
if (!BT_undef)
|
2011-02-23 15:19:18 +08:00
|
|
|
BT_undef.reset(new BuiltinBug("Uninitialized value used as mutex "
|
|
|
|
"for @synchronized"));
|
2010-09-10 11:45:29 +08:00
|
|
|
EnhancedBugReport *report =
|
|
|
|
new EnhancedBugReport(*BT_undef, BT_undef->getDescription(), N);
|
|
|
|
report->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, Ex);
|
|
|
|
C.EmitReport(report);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-10-26 04:20:56 +08:00
|
|
|
if (V.isUnknown())
|
|
|
|
return;
|
|
|
|
|
2010-09-10 11:45:29 +08:00
|
|
|
// Check for null mutexes.
|
|
|
|
const GRState *notNullState, *nullState;
|
2010-12-02 06:16:56 +08:00
|
|
|
llvm::tie(notNullState, nullState) = state->assume(cast<DefinedSVal>(V));
|
2010-09-10 11:45:29 +08:00
|
|
|
|
|
|
|
if (nullState) {
|
|
|
|
if (!notNullState) {
|
|
|
|
// Generate an error node. This isn't a sink since
|
|
|
|
// a null mutex just means no synchronization occurs.
|
2010-12-21 05:19:09 +08:00
|
|
|
if (ExplodedNode *N = C.generateNode(nullState)) {
|
2010-09-10 11:45:29 +08:00
|
|
|
if (!BT_null)
|
2011-02-23 15:19:18 +08:00
|
|
|
BT_null.reset(new BuiltinBug("Nil value used as mutex for @synchronized() "
|
|
|
|
"(no synchronization will occur)"));
|
2010-09-10 11:45:29 +08:00
|
|
|
EnhancedBugReport *report =
|
|
|
|
new EnhancedBugReport(*BT_null, BT_null->getDescription(), N);
|
|
|
|
report->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue,
|
|
|
|
Ex);
|
|
|
|
|
|
|
|
C.EmitReport(report);
|
2010-10-21 23:38:55 +08:00
|
|
|
return;
|
2010-09-10 11:45:29 +08:00
|
|
|
}
|
|
|
|
}
|
2010-10-21 23:38:55 +08:00
|
|
|
// Don't add a transition for 'nullState'. If the value is
|
|
|
|
// under-constrained to be null or non-null, assume it is non-null
|
|
|
|
// afterwards.
|
2010-09-10 11:45:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (notNullState)
|
|
|
|
C.addTransition(notNullState);
|
|
|
|
}
|
2010-10-21 23:38:55 +08:00
|
|
|
|
2011-02-23 15:19:18 +08:00
|
|
|
void ento::registerObjCAtSyncChecker(CheckerManager &mgr) {
|
|
|
|
if (mgr.getLangOptions().ObjC2)
|
|
|
|
mgr.registerChecker<ObjCAtSyncChecker>();
|
|
|
|
}
|