2009-12-08 17:07:59 +08:00
|
|
|
//=== BuiltinFunctionChecker.cpp --------------------------------*- 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
|
2009-12-08 17:07:59 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This checker evaluates clang builtin functions.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
[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-12-04 17:13:33 +08:00
|
|
|
#include "clang/Basic/Builtins.h"
|
2011-03-01 09:16:21 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/Checker.h"
|
2011-02-28 09:27:07 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
|
2009-12-08 17:07:59 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
2010-12-23 15:20:52 +08:00
|
|
|
using namespace ento;
|
2009-12-08 17:07:59 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2011-03-01 09:16:21 +08:00
|
|
|
class BuiltinFunctionChecker : public Checker<eval::Call> {
|
2009-12-08 17:07:59 +08:00
|
|
|
public:
|
2011-02-28 09:27:07 +08:00
|
|
|
bool evalCall(const CallExpr *CE, CheckerContext &C) const;
|
2009-12-08 17:07:59 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-02-28 09:27:07 +08:00
|
|
|
bool BuiltinFunctionChecker::evalCall(const CallExpr *CE,
|
2011-12-01 13:57:37 +08:00
|
|
|
CheckerContext &C) const {
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef state = C.getState();
|
2011-12-01 13:57:37 +08:00
|
|
|
const FunctionDecl *FD = C.getCalleeDecl(CE);
|
2012-01-07 06:09:28 +08:00
|
|
|
const LocationContext *LCtx = C.getLocationContext();
|
2009-12-08 17:07:59 +08:00
|
|
|
if (!FD)
|
|
|
|
return false;
|
|
|
|
|
2013-08-20 00:27:28 +08:00
|
|
|
switch (FD->getBuiltinID()) {
|
|
|
|
default:
|
2009-12-08 17:07:59 +08:00
|
|
|
return false;
|
|
|
|
|
2017-05-12 15:02:54 +08:00
|
|
|
case Builtin::BI__builtin_assume: {
|
|
|
|
assert (CE->arg_begin() != CE->arg_end());
|
2018-01-18 04:27:29 +08:00
|
|
|
SVal ArgSVal = C.getSVal(CE->getArg(0));
|
2017-05-12 15:02:54 +08:00
|
|
|
if (ArgSVal.isUndef())
|
|
|
|
return true; // Return true to model purity.
|
|
|
|
|
|
|
|
state = state->assume(ArgSVal.castAs<DefinedOrUnknownSVal>(), true);
|
|
|
|
// FIXME: do we want to warn here? Not right now. The most reports might
|
|
|
|
// come from infeasible paths, thus being false positives.
|
2017-06-22 18:09:40 +08:00
|
|
|
if (!state) {
|
|
|
|
C.generateSink(C.getState(), C.getPredecessor());
|
2017-05-12 15:02:54 +08:00
|
|
|
return true;
|
2017-06-22 18:09:40 +08:00
|
|
|
}
|
2017-05-12 15:02:54 +08:00
|
|
|
|
|
|
|
C.addTransition(state);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-09-03 04:01:30 +08:00
|
|
|
case Builtin::BI__builtin_unpredictable:
|
2013-07-12 08:26:14 +08:00
|
|
|
case Builtin::BI__builtin_expect:
|
2014-09-10 05:42:16 +08:00
|
|
|
case Builtin::BI__builtin_assume_aligned:
|
2013-07-12 08:26:14 +08:00
|
|
|
case Builtin::BI__builtin_addressof: {
|
2015-09-03 04:01:30 +08:00
|
|
|
// For __builtin_unpredictable, __builtin_expect, and
|
|
|
|
// __builtin_assume_aligned, just return the value of the subexpression.
|
2013-07-12 08:26:14 +08:00
|
|
|
// __builtin_addressof is going from a reference to a pointer, but those
|
|
|
|
// are represented the same way in the analyzer.
|
2009-12-08 17:07:59 +08:00
|
|
|
assert (CE->arg_begin() != CE->arg_end());
|
2018-01-18 04:27:29 +08:00
|
|
|
SVal X = C.getSVal(*(CE->arg_begin()));
|
2012-01-07 06:09:28 +08:00
|
|
|
C.addTransition(state->BindExpr(CE, LCtx, X));
|
2009-12-08 17:07:59 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-10-31 13:37:48 +08:00
|
|
|
case Builtin::BI__builtin_alloca_with_align:
|
2009-12-08 17:07:59 +08:00
|
|
|
case Builtin::BI__builtin_alloca: {
|
|
|
|
// FIXME: Refactor into StoreManager itself?
|
|
|
|
MemRegionManager& RM = C.getStoreManager().getRegionManager();
|
2010-07-04 08:00:41 +08:00
|
|
|
const AllocaRegion* R =
|
2012-08-22 14:26:15 +08:00
|
|
|
RM.getAllocaRegion(CE, C.blockCount(), C.getLocationContext());
|
2009-12-08 17:07:59 +08:00
|
|
|
|
|
|
|
// Set the extent of the region in bytes. This enables us to use the
|
|
|
|
// SVal of the argument directly. If we save the extent in bits, we
|
|
|
|
// cannot represent values like symbol*8.
|
2018-01-18 04:27:29 +08:00
|
|
|
auto Size = C.getSVal(*(CE->arg_begin())).castAs<DefinedOrUnknownSVal>();
|
2010-07-04 08:00:41 +08:00
|
|
|
|
2010-12-02 15:49:45 +08:00
|
|
|
SValBuilder& svalBuilder = C.getSValBuilder();
|
|
|
|
DefinedOrUnknownSVal Extent = R->getExtent(svalBuilder);
|
|
|
|
DefinedOrUnknownSVal extentMatchesSizeArg =
|
2010-12-02 05:57:22 +08:00
|
|
|
svalBuilder.evalEQ(state, Extent, Size);
|
2010-12-02 15:49:45 +08:00
|
|
|
state = state->assume(extentMatchesSizeArg, true);
|
2012-11-27 03:11:46 +08:00
|
|
|
assert(state && "The region should not have any previous constraints");
|
2010-07-04 08:00:41 +08:00
|
|
|
|
2012-01-07 06:09:28 +08:00
|
|
|
C.addTransition(state->BindExpr(CE, LCtx, loc::MemRegionVal(R)));
|
2009-12-08 17:07:59 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-02-10 08:51:47 +08:00
|
|
|
case Builtin::BI__builtin_object_size:
|
|
|
|
case Builtin::BI__builtin_constant_p: {
|
2013-08-20 00:27:28 +08:00
|
|
|
// This must be resolvable at compile time, so we defer to the constant
|
|
|
|
// evaluator for a value.
|
|
|
|
SVal V = UnknownVal();
|
2018-12-01 07:41:18 +08:00
|
|
|
Expr::EvalResult EVResult;
|
|
|
|
if (CE->EvaluateAsInt(EVResult, C.getASTContext(), Expr::SE_NoSideEffects)) {
|
2013-08-20 00:27:28 +08:00
|
|
|
// Make sure the result has the correct type.
|
2018-12-01 07:41:18 +08:00
|
|
|
llvm::APSInt Result = EVResult.Val.getInt();
|
2013-08-20 00:27:28 +08:00
|
|
|
SValBuilder &SVB = C.getSValBuilder();
|
|
|
|
BasicValueFactory &BVF = SVB.getBasicValueFactory();
|
|
|
|
BVF.getAPSIntType(CE->getType()).apply(Result);
|
|
|
|
V = SVB.makeIntVal(Result);
|
|
|
|
}
|
|
|
|
|
|
|
|
C.addTransition(state->BindExpr(CE, LCtx, V));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2009-12-08 17:07:59 +08:00
|
|
|
}
|
2011-02-28 09:27:07 +08:00
|
|
|
|
|
|
|
void ento::registerBuiltinFunctionChecker(CheckerManager &mgr) {
|
|
|
|
mgr.registerChecker<BuiltinFunctionChecker>();
|
|
|
|
}
|
2019-01-26 22:23:08 +08:00
|
|
|
|
|
|
|
bool ento::shouldRegisterBuiltinFunctionChecker(const LangOptions &LO) {
|
|
|
|
return true;
|
|
|
|
}
|