]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/lib/StaticAnalyzer/Checkers/BuiltinFunctionChecker.cpp
Merge ^/head r319251 through r319479.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / lib / StaticAnalyzer / Checkers / BuiltinFunctionChecker.cpp
1 //=== BuiltinFunctionChecker.cpp --------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This checker evaluates clang builtin functions.
11 //
12 //===----------------------------------------------------------------------===//
13
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"
19
20 using namespace clang;
21 using namespace ento;
22
23 namespace {
24
25 class BuiltinFunctionChecker : public Checker<eval::Call> {
26 public:
27   bool evalCall(const CallExpr *CE, CheckerContext &C) const;
28 };
29
30 }
31
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();
37   if (!FD)
38     return false;
39
40   switch (FD->getBuiltinID()) {
41   default:
42     return false;
43
44   case Builtin::BI__builtin_assume: {
45     assert (CE->arg_begin() != CE->arg_end());
46     SVal ArgSVal = state->getSVal(CE->getArg(0), LCtx);
47     if (ArgSVal.isUndef())
48       return true; // Return true to model purity.
49
50     state = state->assume(ArgSVal.castAs<DefinedOrUnknownSVal>(), true);
51     // FIXME: do we want to warn here? Not right now. The most reports might
52     // come from infeasible paths, thus being false positives.
53     if (!state)
54       return true;
55
56     C.addTransition(state);
57     return true;
58   }
59
60   case Builtin::BI__builtin_unpredictable:
61   case Builtin::BI__builtin_expect:
62   case Builtin::BI__builtin_assume_aligned:
63   case Builtin::BI__builtin_addressof: {
64     // For __builtin_unpredictable, __builtin_expect, and
65     // __builtin_assume_aligned, just return the value of the subexpression.
66     // __builtin_addressof is going from a reference to a pointer, but those
67     // are represented the same way in the analyzer.
68     assert (CE->arg_begin() != CE->arg_end());
69     SVal X = state->getSVal(*(CE->arg_begin()), LCtx);
70     C.addTransition(state->BindExpr(CE, LCtx, X));
71     return true;
72   }
73
74   case Builtin::BI__builtin_alloca_with_align:
75   case Builtin::BI__builtin_alloca: {
76     // FIXME: Refactor into StoreManager itself?
77     MemRegionManager& RM = C.getStoreManager().getRegionManager();
78     const AllocaRegion* R =
79       RM.getAllocaRegion(CE, C.blockCount(), C.getLocationContext());
80
81     // Set the extent of the region in bytes. This enables us to use the
82     // SVal of the argument directly. If we save the extent in bits, we
83     // cannot represent values like symbol*8.
84     DefinedOrUnknownSVal Size =
85         state->getSVal(*(CE->arg_begin()), LCtx).castAs<DefinedOrUnknownSVal>();
86
87     SValBuilder& svalBuilder = C.getSValBuilder();
88     DefinedOrUnknownSVal Extent = R->getExtent(svalBuilder);
89     DefinedOrUnknownSVal extentMatchesSizeArg =
90       svalBuilder.evalEQ(state, Extent, Size);
91     state = state->assume(extentMatchesSizeArg, true);
92     assert(state && "The region should not have any previous constraints");
93
94     C.addTransition(state->BindExpr(CE, LCtx, loc::MemRegionVal(R)));
95     return true;
96   }
97
98   case Builtin::BI__builtin_object_size: {
99     // This must be resolvable at compile time, so we defer to the constant
100     // evaluator for a value.
101     SVal V = UnknownVal();
102     llvm::APSInt Result;
103     if (CE->EvaluateAsInt(Result, C.getASTContext(), Expr::SE_NoSideEffects)) {
104       // Make sure the result has the correct type.
105       SValBuilder &SVB = C.getSValBuilder();
106       BasicValueFactory &BVF = SVB.getBasicValueFactory();
107       BVF.getAPSIntType(CE->getType()).apply(Result);
108       V = SVB.makeIntVal(Result);
109     }
110
111     C.addTransition(state->BindExpr(CE, LCtx, V));
112     return true;
113   }
114   }
115 }
116
117 void ento::registerBuiltinFunctionChecker(CheckerManager &mgr) {
118   mgr.registerChecker<BuiltinFunctionChecker>();
119 }