2009-12-08 17:07:59 +08:00
|
|
|
//=== BuiltinFunctionChecker.cpp --------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This checker evaluates clang builtin functions.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-02-28 09:27:07 +08:00
|
|
|
#include "ClangSACheckers.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;
|
|
|
|
|
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: {
|
2014-09-10 05:42:16 +08:00
|
|
|
// For __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());
|
2012-01-07 06:09:28 +08:00
|
|
|
SVal X = state->getSVal(*(CE->arg_begin()), LCtx);
|
|
|
|
C.addTransition(state->BindExpr(CE, LCtx, X));
|
2009-12-08 17:07:59 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2010-07-04 08:00:41 +08:00
|
|
|
DefinedOrUnknownSVal Size =
|
2013-02-20 13:52:05 +08:00
|
|
|
state->getSVal(*(CE->arg_begin()), LCtx).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;
|
|
|
|
}
|
|
|
|
|
2013-08-20 00:27:28 +08:00
|
|
|
case Builtin::BI__builtin_object_size: {
|
|
|
|
// This must be resolvable at compile time, so we defer to the constant
|
|
|
|
// evaluator for a value.
|
|
|
|
SVal V = UnknownVal();
|
|
|
|
llvm::APSInt Result;
|
|
|
|
if (CE->EvaluateAsInt(Result, C.getASTContext(), Expr::SE_NoSideEffects)) {
|
|
|
|
// Make sure the result has the correct type.
|
|
|
|
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>();
|
|
|
|
}
|