blob: 097d4198800d4a2ce81ea5bd5f1f8d7abc099559 [file] [log] [blame]
Zhongxing Xufe2f9012009-12-08 09:07:59 +00001//=== 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
Argyrios Kyrtzidisf3ed8b62011-02-28 01:27:07 +000014#include "ClangSACheckers.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000015#include "clang/Basic/Builtins.h"
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +000016#include "clang/StaticAnalyzer/Core/Checker.h"
Argyrios Kyrtzidisf3ed8b62011-02-28 01:27:07 +000017#include "clang/StaticAnalyzer/Core/CheckerManager.h"
18#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Zhongxing Xufe2f9012009-12-08 09:07:59 +000019
20using namespace clang;
Ted Kremenek98857c92010-12-23 07:20:52 +000021using namespace ento;
Zhongxing Xufe2f9012009-12-08 09:07:59 +000022
23namespace {
24
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +000025class BuiltinFunctionChecker : public Checker<eval::Call> {
Zhongxing Xufe2f9012009-12-08 09:07:59 +000026public:
Argyrios Kyrtzidisf3ed8b62011-02-28 01:27:07 +000027 bool evalCall(const CallExpr *CE, CheckerContext &C) const;
Zhongxing Xufe2f9012009-12-08 09:07:59 +000028};
29
30}
31
Argyrios Kyrtzidisf3ed8b62011-02-28 01:27:07 +000032bool BuiltinFunctionChecker::evalCall(const CallExpr *CE,
Anna Zaksc6aa5312011-12-01 05:57:37 +000033 CheckerContext &C) const {
Ted Kremenek49b1e382012-01-26 21:29:00 +000034 ProgramStateRef state = C.getState();
Anna Zaksc6aa5312011-12-01 05:57:37 +000035 const FunctionDecl *FD = C.getCalleeDecl(CE);
Ted Kremenek632e3b72012-01-06 22:09:28 +000036 const LocationContext *LCtx = C.getLocationContext();
Zhongxing Xufe2f9012009-12-08 09:07:59 +000037 if (!FD)
38 return false;
39
Jordan Rose5374c072013-08-19 16:27:28 +000040 switch (FD->getBuiltinID()) {
41 default:
Zhongxing Xufe2f9012009-12-08 09:07:59 +000042 return false;
43
Gabor Horvath3a00b412017-05-12 07:02:54 +000044 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.
Gabor Horvathb3bcddf2017-06-22 10:09:40 +000053 if (!state) {
54 C.generateSink(C.getState(), C.getPredecessor());
Gabor Horvath3a00b412017-05-12 07:02:54 +000055 return true;
Gabor Horvathb3bcddf2017-06-22 10:09:40 +000056 }
Gabor Horvath3a00b412017-05-12 07:02:54 +000057
58 C.addTransition(state);
59 return true;
60 }
61
Sanjay Patela24296b2015-09-02 20:01:30 +000062 case Builtin::BI__builtin_unpredictable:
Jordan Rose78cd51b2013-07-12 00:26:14 +000063 case Builtin::BI__builtin_expect:
Jordan Rose21933cc2014-09-09 21:42:16 +000064 case Builtin::BI__builtin_assume_aligned:
Jordan Rose78cd51b2013-07-12 00:26:14 +000065 case Builtin::BI__builtin_addressof: {
Sanjay Patela24296b2015-09-02 20:01:30 +000066 // For __builtin_unpredictable, __builtin_expect, and
67 // __builtin_assume_aligned, just return the value of the subexpression.
Jordan Rose78cd51b2013-07-12 00:26:14 +000068 // __builtin_addressof is going from a reference to a pointer, but those
69 // are represented the same way in the analyzer.
Zhongxing Xufe2f9012009-12-08 09:07:59 +000070 assert (CE->arg_begin() != CE->arg_end());
Ted Kremenek632e3b72012-01-06 22:09:28 +000071 SVal X = state->getSVal(*(CE->arg_begin()), LCtx);
72 C.addTransition(state->BindExpr(CE, LCtx, X));
Zhongxing Xufe2f9012009-12-08 09:07:59 +000073 return true;
74 }
75
David Majnemer51169932016-10-31 05:37:48 +000076 case Builtin::BI__builtin_alloca_with_align:
Zhongxing Xufe2f9012009-12-08 09:07:59 +000077 case Builtin::BI__builtin_alloca: {
78 // FIXME: Refactor into StoreManager itself?
79 MemRegionManager& RM = C.getStoreManager().getRegionManager();
Jordy Rose674bd552010-07-04 00:00:41 +000080 const AllocaRegion* R =
Ted Kremenekd94854a2012-08-22 06:26:15 +000081 RM.getAllocaRegion(CE, C.blockCount(), C.getLocationContext());
Zhongxing Xufe2f9012009-12-08 09:07:59 +000082
83 // Set the extent of the region in bytes. This enables us to use the
84 // SVal of the argument directly. If we save the extent in bits, we
85 // cannot represent values like symbol*8.
Jordy Rose674bd552010-07-04 00:00:41 +000086 DefinedOrUnknownSVal Size =
David Blaikie2fdacbc2013-02-20 05:52:05 +000087 state->getSVal(*(CE->arg_begin()), LCtx).castAs<DefinedOrUnknownSVal>();
Jordy Rose674bd552010-07-04 00:00:41 +000088
Ted Kremenek90af9092010-12-02 07:49:45 +000089 SValBuilder& svalBuilder = C.getSValBuilder();
90 DefinedOrUnknownSVal Extent = R->getExtent(svalBuilder);
91 DefinedOrUnknownSVal extentMatchesSizeArg =
Ted Kremenekdc891422010-12-01 21:57:22 +000092 svalBuilder.evalEQ(state, Extent, Size);
Ted Kremenek90af9092010-12-02 07:49:45 +000093 state = state->assume(extentMatchesSizeArg, true);
Anna Zakse3beeaa2012-11-26 19:11:46 +000094 assert(state && "The region should not have any previous constraints");
Jordy Rose674bd552010-07-04 00:00:41 +000095
Ted Kremenek632e3b72012-01-06 22:09:28 +000096 C.addTransition(state->BindExpr(CE, LCtx, loc::MemRegionVal(R)));
Zhongxing Xufe2f9012009-12-08 09:07:59 +000097 return true;
98 }
Zhongxing Xufe2f9012009-12-08 09:07:59 +000099
Jordan Rose5374c072013-08-19 16:27:28 +0000100 case Builtin::BI__builtin_object_size: {
101 // This must be resolvable at compile time, so we defer to the constant
102 // evaluator for a value.
103 SVal V = UnknownVal();
104 llvm::APSInt Result;
105 if (CE->EvaluateAsInt(Result, C.getASTContext(), Expr::SE_NoSideEffects)) {
106 // Make sure the result has the correct type.
107 SValBuilder &SVB = C.getSValBuilder();
108 BasicValueFactory &BVF = SVB.getBasicValueFactory();
109 BVF.getAPSIntType(CE->getType()).apply(Result);
110 V = SVB.makeIntVal(Result);
111 }
112
113 C.addTransition(state->BindExpr(CE, LCtx, V));
114 return true;
115 }
116 }
Zhongxing Xufe2f9012009-12-08 09:07:59 +0000117}
Argyrios Kyrtzidisf3ed8b62011-02-28 01:27:07 +0000118
119void ento::registerBuiltinFunctionChecker(CheckerManager &mgr) {
120 mgr.registerChecker<BuiltinFunctionChecker>();
121}