blob: 51236437329c6255279293ffc74f3635dd3980a1 [file] [log] [blame]
Zhongxing Xufe2f9012009-12-08 09:07:59 +00001//=== BuiltinFunctionChecker.cpp --------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Zhongxing Xufe2f9012009-12-08 09:07:59 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This checker evaluates clang builtin functions.
10//
11//===----------------------------------------------------------------------===//
12
Chandler Carruth3a022472012-12-04 09:13:33 +000013#include "clang/Basic/Builtins.h"
Charusso601687b2020-01-30 16:04:37 +010014#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +000015#include "clang/StaticAnalyzer/Core/Checker.h"
Argyrios Kyrtzidisf3ed8b62011-02-28 01:27:07 +000016#include "clang/StaticAnalyzer/Core/CheckerManager.h"
Artem Dergachev44820632019-06-19 23:33:42 +000017#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
Argyrios Kyrtzidisf3ed8b62011-02-28 01:27:07 +000018#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Charusso601687b2020-01-30 16:04:37 +010019#include "clang/StaticAnalyzer/Core/PathSensitive/DynamicSize.h"
Zhongxing Xufe2f9012009-12-08 09:07:59 +000020
21using namespace clang;
Ted Kremenek98857c92010-12-23 07:20:52 +000022using namespace ento;
Zhongxing Xufe2f9012009-12-08 09:07:59 +000023
24namespace {
25
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +000026class BuiltinFunctionChecker : public Checker<eval::Call> {
Zhongxing Xufe2f9012009-12-08 09:07:59 +000027public:
Artem Dergachev44820632019-06-19 23:33:42 +000028 bool evalCall(const CallEvent &Call, CheckerContext &C) const;
Zhongxing Xufe2f9012009-12-08 09:07:59 +000029};
30
31}
32
Artem Dergachev44820632019-06-19 23:33:42 +000033bool BuiltinFunctionChecker::evalCall(const CallEvent &Call,
Anna Zaksc6aa5312011-12-01 05:57:37 +000034 CheckerContext &C) const {
Ted Kremenek49b1e382012-01-26 21:29:00 +000035 ProgramStateRef state = C.getState();
Artem Dergachev44820632019-06-19 23:33:42 +000036 const auto *FD = dyn_cast_or_null<FunctionDecl>(Call.getDecl());
Zhongxing Xufe2f9012009-12-08 09:07:59 +000037 if (!FD)
38 return false;
39
Artem Dergachev44820632019-06-19 23:33:42 +000040 const LocationContext *LCtx = C.getLocationContext();
41 const Expr *CE = Call.getOriginExpr();
42
Jordan Rose5374c072013-08-19 16:27:28 +000043 switch (FD->getBuiltinID()) {
44 default:
Zhongxing Xufe2f9012009-12-08 09:07:59 +000045 return false;
46
Gabor Horvath3a00b412017-05-12 07:02:54 +000047 case Builtin::BI__builtin_assume: {
Artem Dergachev44820632019-06-19 23:33:42 +000048 assert (Call.getNumArgs() > 0);
49 SVal Arg = Call.getArgSVal(0);
50 if (Arg.isUndef())
Gabor Horvath3a00b412017-05-12 07:02:54 +000051 return true; // Return true to model purity.
52
Artem Dergachev44820632019-06-19 23:33:42 +000053 state = state->assume(Arg.castAs<DefinedOrUnknownSVal>(), true);
Gabor Horvath3a00b412017-05-12 07:02:54 +000054 // FIXME: do we want to warn here? Not right now. The most reports might
55 // come from infeasible paths, thus being false positives.
Gabor Horvathb3bcddf2017-06-22 10:09:40 +000056 if (!state) {
57 C.generateSink(C.getState(), C.getPredecessor());
Gabor Horvath3a00b412017-05-12 07:02:54 +000058 return true;
Gabor Horvathb3bcddf2017-06-22 10:09:40 +000059 }
Gabor Horvath3a00b412017-05-12 07:02:54 +000060
61 C.addTransition(state);
62 return true;
63 }
64
Sanjay Patela24296b2015-09-02 20:01:30 +000065 case Builtin::BI__builtin_unpredictable:
Jordan Rose78cd51b2013-07-12 00:26:14 +000066 case Builtin::BI__builtin_expect:
Jordan Rose21933cc2014-09-09 21:42:16 +000067 case Builtin::BI__builtin_assume_aligned:
Jordan Rose78cd51b2013-07-12 00:26:14 +000068 case Builtin::BI__builtin_addressof: {
Sanjay Patela24296b2015-09-02 20:01:30 +000069 // For __builtin_unpredictable, __builtin_expect, and
70 // __builtin_assume_aligned, just return the value of the subexpression.
Jordan Rose78cd51b2013-07-12 00:26:14 +000071 // __builtin_addressof is going from a reference to a pointer, but those
72 // are represented the same way in the analyzer.
Artem Dergachev44820632019-06-19 23:33:42 +000073 assert (Call.getNumArgs() > 0);
74 SVal Arg = Call.getArgSVal(0);
75 C.addTransition(state->BindExpr(CE, LCtx, Arg));
Zhongxing Xufe2f9012009-12-08 09:07:59 +000076 return true;
77 }
78
David Majnemer51169932016-10-31 05:37:48 +000079 case Builtin::BI__builtin_alloca_with_align:
Zhongxing Xufe2f9012009-12-08 09:07:59 +000080 case Builtin::BI__builtin_alloca: {
81 // FIXME: Refactor into StoreManager itself?
82 MemRegionManager& RM = C.getStoreManager().getRegionManager();
Jordy Rose674bd552010-07-04 00:00:41 +000083 const AllocaRegion* R =
Ted Kremenekd94854a2012-08-22 06:26:15 +000084 RM.getAllocaRegion(CE, C.blockCount(), C.getLocationContext());
Zhongxing Xufe2f9012009-12-08 09:07:59 +000085
86 // Set the extent of the region in bytes. This enables us to use the
87 // SVal of the argument directly. If we save the extent in bits, we
88 // cannot represent values like symbol*8.
Artem Dergachev44820632019-06-19 23:33:42 +000089 auto Size = Call.getArgSVal(0);
90 if (Size.isUndef())
91 return true; // Return true to model purity.
Jordy Rose674bd552010-07-04 00:00:41 +000092
Ted Kremenek90af9092010-12-02 07:49:45 +000093 SValBuilder& svalBuilder = C.getSValBuilder();
Charusso601687b2020-01-30 16:04:37 +010094 DefinedOrUnknownSVal DynSize = getDynamicSize(state, R, svalBuilder);
95 DefinedOrUnknownSVal DynSizeMatchesSizeArg =
96 svalBuilder.evalEQ(state, DynSize, Size.castAs<DefinedOrUnknownSVal>());
97 state = state->assume(DynSizeMatchesSizeArg, true);
Anna Zakse3beeaa2012-11-26 19:11:46 +000098 assert(state && "The region should not have any previous constraints");
Jordy Rose674bd552010-07-04 00:00:41 +000099
Ted Kremenek632e3b72012-01-06 22:09:28 +0000100 C.addTransition(state->BindExpr(CE, LCtx, loc::MemRegionVal(R)));
Zhongxing Xufe2f9012009-12-08 09:07:59 +0000101 return true;
102 }
Zhongxing Xufe2f9012009-12-08 09:07:59 +0000103
Erik Pilkington9c3b5882019-01-30 20:34:53 +0000104 case Builtin::BI__builtin_dynamic_object_size:
Artem Dergachevf3e09bd2018-02-10 00:51:47 +0000105 case Builtin::BI__builtin_object_size:
106 case Builtin::BI__builtin_constant_p: {
Jordan Rose5374c072013-08-19 16:27:28 +0000107 // This must be resolvable at compile time, so we defer to the constant
108 // evaluator for a value.
Artem Dergachevf7887d42019-04-03 01:53:40 +0000109 SValBuilder &SVB = C.getSValBuilder();
Jordan Rose5374c072013-08-19 16:27:28 +0000110 SVal V = UnknownVal();
Fangrui Song407659a2018-11-30 23:41:18 +0000111 Expr::EvalResult EVResult;
112 if (CE->EvaluateAsInt(EVResult, C.getASTContext(), Expr::SE_NoSideEffects)) {
Jordan Rose5374c072013-08-19 16:27:28 +0000113 // Make sure the result has the correct type.
Fangrui Song407659a2018-11-30 23:41:18 +0000114 llvm::APSInt Result = EVResult.Val.getInt();
Jordan Rose5374c072013-08-19 16:27:28 +0000115 BasicValueFactory &BVF = SVB.getBasicValueFactory();
116 BVF.getAPSIntType(CE->getType()).apply(Result);
117 V = SVB.makeIntVal(Result);
118 }
119
Artem Dergachevf7887d42019-04-03 01:53:40 +0000120 if (FD->getBuiltinID() == Builtin::BI__builtin_constant_p) {
121 // If we didn't manage to figure out if the value is constant or not,
122 // it is safe to assume that it's not constant and unsafe to assume
123 // that it's constant.
124 if (V.isUnknown())
125 V = SVB.makeIntVal(0, CE->getType());
126 }
127
Jordan Rose5374c072013-08-19 16:27:28 +0000128 C.addTransition(state->BindExpr(CE, LCtx, V));
129 return true;
130 }
131 }
Zhongxing Xufe2f9012009-12-08 09:07:59 +0000132}
Argyrios Kyrtzidisf3ed8b62011-02-28 01:27:07 +0000133
134void ento::registerBuiltinFunctionChecker(CheckerManager &mgr) {
135 mgr.registerChecker<BuiltinFunctionChecker>();
136}
Kristof Umann058a7a42019-01-26 14:23:08 +0000137
138bool ento::shouldRegisterBuiltinFunctionChecker(const LangOptions &LO) {
139 return true;
140}