2010-09-10 11:45:29 +08:00
|
|
|
//== ObjCAtSyncChecker.cpp - nil mutex checker for @synchronized -*- C++ -*--=//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2010-09-10 11:45:29 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This defines ObjCAtSyncChecker, a builtin check that checks for null pointers
|
|
|
|
// used as mutexes for @synchronized.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
[analyzer][NFC] Move CheckerRegistry from the Core directory to Frontend
ClangCheckerRegistry is a very non-obvious, poorly documented, weird concept.
It derives from CheckerRegistry, and is placed in lib/StaticAnalyzer/Frontend,
whereas it's base is located in lib/StaticAnalyzer/Core. It was, from what I can
imagine, used to circumvent the problem that the registry functions of the
checkers are located in the clangStaticAnalyzerCheckers library, but that
library depends on clangStaticAnalyzerCore. However, clangStaticAnalyzerFrontend
depends on both of those libraries.
One can make the observation however, that CheckerRegistry has no place in Core,
it isn't used there at all! The only place where it is used is Frontend, which
is where it ultimately belongs.
This move implies that since
include/clang/StaticAnalyzer/Checkers/ClangCheckers.h only contained a single function:
class CheckerRegistry;
void registerBuiltinCheckers(CheckerRegistry ®istry);
it had to re purposed, as CheckerRegistry is no longer available to
clangStaticAnalyzerCheckers. It was renamed to BuiltinCheckerRegistration.h,
which actually describes it a lot better -- it does not contain the registration
functions for checkers, but only those generated by the tblgen files.
Differential Revision: https://reviews.llvm.org/D54436
llvm-svn: 349275
2018-12-16 00:23:51 +08:00
|
|
|
#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.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();
|
2018-01-18 04:27:29 +08:00
|
|
|
SVal V = C.getSVal(Ex);
|
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>()) {
|
2015-09-17 06:03:05 +08:00
|
|
|
if (ExplodedNode *N = C.generateErrorNode()) {
|
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"));
|
2019-09-10 04:34:40 +08:00
|
|
|
auto report = std::make_unique<PathSensitiveBugReport>(
|
|
|
|
*BT_undef, BT_undef->getDescription(), N);
|
2018-10-24 02:24:53 +08:00
|
|
|
bugreporter::trackExpressionValue(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.
|
2015-09-17 06:03:05 +08:00
|
|
|
if (ExplodedNode *N = C.generateNonFatalErrorNode(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)"));
|
2019-09-10 04:34:40 +08:00
|
|
|
auto report = std::make_unique<PathSensitiveBugReport>(
|
|
|
|
*BT_null, BT_null->getDescription(), N);
|
2018-10-24 02:24:53 +08:00
|
|
|
bugreporter::trackExpressionValue(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) {
|
2019-01-26 22:23:08 +08:00
|
|
|
mgr.registerChecker<ObjCAtSyncChecker>();
|
|
|
|
}
|
|
|
|
|
2020-03-27 21:29:31 +08:00
|
|
|
bool ento::shouldRegisterObjCAtSyncChecker(const CheckerManager &mgr) {
|
|
|
|
const LangOptions &LO = mgr.getLangOpts();
|
2019-01-26 22:23:08 +08:00
|
|
|
return LO.ObjC;
|
2011-02-23 15:19:18 +08:00
|
|
|
}
|