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"
|
2012-01-28 20:06:22 +08:00
|
|
|
#include "clang/AST/StmtObjC.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 15:19:18 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.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> > {
|
2014-03-08 04:03:18 +08:00
|
|
|
mutable std::unique_ptr<BuiltinBug> BT_null;
|
|
|
|
mutable std::unique_ptr<BuiltinBug> BT_undef;
|
2011-02-23 15:19:18 +08:00
|
|
|
|
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();
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef state = C.getState();
|
2012-01-07 06:09:28 +08:00
|
|
|
SVal V = state->getSVal(Ex, C.getLocationContext());
|
2010-09-10 11:45:29 +08:00
|
|
|
|
|
|
|
// Uninitialized value used for the mutex?
|
2013-02-20 13:52:05 +08:00
|
|
|
if (V.getAs<UndefinedVal>()) {
|
2010-12-21 05:19:09 +08:00
|
|
|
if (ExplodedNode *N = C.generateSink()) {
|
2010-09-10 11:45:29 +08:00
|
|
|
if (!BT_undef)
|
2014-02-12 05:49:21 +08:00
|
|
|
BT_undef.reset(new BuiltinBug(this, "Uninitialized value used as mutex "
|
|
|
|
"for @synchronized"));
|
2015-06-23 21:15:32 +08:00
|
|
|
auto report =
|
|
|
|
llvm::make_unique<BugReport>(*BT_undef, BT_undef->getDescription(), N);
|
2012-08-28 08:50:51 +08:00
|
|
|
bugreporter::trackNullOrUndefValue(N, Ex, *report);
|
2015-06-23 21:15:32 +08:00
|
|
|
C.emitReport(std::move(report));
|
2010-09-10 11:45:29 +08:00
|
|
|
}
|
|
|
|
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.
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef notNullState, nullState;
|
2014-03-02 21:01:17 +08:00
|
|
|
std::tie(notNullState, nullState) = state->assume(V.castAs<DefinedSVal>());
|
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.
|
2011-10-27 05:06:34 +08:00
|
|
|
if (ExplodedNode *N = C.addTransition(nullState)) {
|
2010-09-10 11:45:29 +08:00
|
|
|
if (!BT_null)
|
2014-02-12 05:49:21 +08:00
|
|
|
BT_null.reset(new BuiltinBug(
|
|
|
|
this, "Nil value used as mutex for @synchronized() "
|
|
|
|
"(no synchronization will occur)"));
|
2015-06-23 21:15:32 +08:00
|
|
|
auto report =
|
|
|
|
llvm::make_unique<BugReport>(*BT_null, BT_null->getDescription(), N);
|
2012-08-28 08:50:51 +08:00
|
|
|
bugreporter::trackNullOrUndefValue(N, Ex, *report);
|
2010-09-10 11:45:29 +08:00
|
|
|
|
2015-06-23 21:15:32 +08:00
|
|
|
C.emitReport(std::move(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)
|
2011-10-27 05:06:34 +08:00
|
|
|
C.addTransition(notNullState);
|
2010-09-10 11:45:29 +08:00
|
|
|
}
|
2010-10-21 23:38:55 +08:00
|
|
|
|
2011-02-23 15:19:18 +08:00
|
|
|
void ento::registerObjCAtSyncChecker(CheckerManager &mgr) {
|
2012-03-11 15:00:24 +08:00
|
|
|
if (mgr.getLangOpts().ObjC2)
|
2011-02-23 15:19:18 +08:00
|
|
|
mgr.registerChecker<ObjCAtSyncChecker>();
|
|
|
|
}
|