1 //=== BuiltinFunctionChecker.cpp --------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This checker evaluates clang builtin functions.
12 //===----------------------------------------------------------------------===//
14 #include "ClangSACheckers.h"
15 #include "clang/Basic/Builtins.h"
16 #include "clang/StaticAnalyzer/Core/Checker.h"
17 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
18 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
20 using namespace clang;
25 class BuiltinFunctionChecker : public Checker<eval::Call> {
27 bool evalCall(const CallExpr *CE, CheckerContext &C) const;
32 bool BuiltinFunctionChecker::evalCall(const CallExpr *CE,
33 CheckerContext &C) const {
34 ProgramStateRef state = C.getState();
35 const FunctionDecl *FD = C.getCalleeDecl(CE);
36 const LocationContext *LCtx = C.getLocationContext();
40 switch (FD->getBuiltinID()) {
44 case Builtin::BI__builtin_unpredictable:
45 case Builtin::BI__builtin_expect:
46 case Builtin::BI__builtin_assume_aligned:
47 case Builtin::BI__builtin_addressof: {
48 // For __builtin_unpredictable, __builtin_expect, and
49 // __builtin_assume_aligned, just return the value of the subexpression.
50 // __builtin_addressof is going from a reference to a pointer, but those
51 // are represented the same way in the analyzer.
52 assert (CE->arg_begin() != CE->arg_end());
53 SVal X = state->getSVal(*(CE->arg_begin()), LCtx);
54 C.addTransition(state->BindExpr(CE, LCtx, X));
58 case Builtin::BI__builtin_alloca_with_align:
59 case Builtin::BI__builtin_alloca: {
60 // FIXME: Refactor into StoreManager itself?
61 MemRegionManager& RM = C.getStoreManager().getRegionManager();
62 const AllocaRegion* R =
63 RM.getAllocaRegion(CE, C.blockCount(), C.getLocationContext());
65 // Set the extent of the region in bytes. This enables us to use the
66 // SVal of the argument directly. If we save the extent in bits, we
67 // cannot represent values like symbol*8.
68 DefinedOrUnknownSVal Size =
69 state->getSVal(*(CE->arg_begin()), LCtx).castAs<DefinedOrUnknownSVal>();
71 SValBuilder& svalBuilder = C.getSValBuilder();
72 DefinedOrUnknownSVal Extent = R->getExtent(svalBuilder);
73 DefinedOrUnknownSVal extentMatchesSizeArg =
74 svalBuilder.evalEQ(state, Extent, Size);
75 state = state->assume(extentMatchesSizeArg, true);
76 assert(state && "The region should not have any previous constraints");
78 C.addTransition(state->BindExpr(CE, LCtx, loc::MemRegionVal(R)));
82 case Builtin::BI__builtin_object_size: {
83 // This must be resolvable at compile time, so we defer to the constant
84 // evaluator for a value.
85 SVal V = UnknownVal();
87 if (CE->EvaluateAsInt(Result, C.getASTContext(), Expr::SE_NoSideEffects)) {
88 // Make sure the result has the correct type.
89 SValBuilder &SVB = C.getSValBuilder();
90 BasicValueFactory &BVF = SVB.getBasicValueFactory();
91 BVF.getAPSIntType(CE->getType()).apply(Result);
92 V = SVB.makeIntVal(Result);
95 C.addTransition(state->BindExpr(CE, LCtx, V));
101 void ento::registerBuiltinFunctionChecker(CheckerManager &mgr) {
102 mgr.registerChecker<BuiltinFunctionChecker>();