blob: 908cf7de9d7a237741e506979d82e4d3160a6e05 [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
Kristof Umann76a21502018-12-15 16:23:51 +000013#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000014#include "clang/Basic/Builtins.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"
17#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Zhongxing Xufe2f9012009-12-08 09:07:59 +000018
19using namespace clang;
Ted Kremenek98857c92010-12-23 07:20:52 +000020using namespace ento;
Zhongxing Xufe2f9012009-12-08 09:07:59 +000021
22namespace {
23
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +000024class BuiltinFunctionChecker : public Checker<eval::Call> {
Zhongxing Xufe2f9012009-12-08 09:07:59 +000025public:
Argyrios Kyrtzidisf3ed8b62011-02-28 01:27:07 +000026 bool evalCall(const CallExpr *CE, CheckerContext &C) const;
Zhongxing Xufe2f9012009-12-08 09:07:59 +000027};
28
29}
30
Argyrios Kyrtzidisf3ed8b62011-02-28 01:27:07 +000031bool BuiltinFunctionChecker::evalCall(const CallExpr *CE,
Anna Zaksc6aa5312011-12-01 05:57:37 +000032 CheckerContext &C) const {
Ted Kremenek49b1e382012-01-26 21:29:00 +000033 ProgramStateRef state = C.getState();
Anna Zaksc6aa5312011-12-01 05:57:37 +000034 const FunctionDecl *FD = C.getCalleeDecl(CE);
Ted Kremenek632e3b72012-01-06 22:09:28 +000035 const LocationContext *LCtx = C.getLocationContext();
Zhongxing Xufe2f9012009-12-08 09:07:59 +000036 if (!FD)
37 return false;
38
Jordan Rose5374c072013-08-19 16:27:28 +000039 switch (FD->getBuiltinID()) {
40 default:
Zhongxing Xufe2f9012009-12-08 09:07:59 +000041 return false;
42
Gabor Horvath3a00b412017-05-12 07:02:54 +000043 case Builtin::BI__builtin_assume: {
44 assert (CE->arg_begin() != CE->arg_end());
George Karpenkovd703ec92018-01-17 20:27:29 +000045 SVal ArgSVal = C.getSVal(CE->getArg(0));
Gabor Horvath3a00b412017-05-12 07:02:54 +000046 if (ArgSVal.isUndef())
47 return true; // Return true to model purity.
48
49 state = state->assume(ArgSVal.castAs<DefinedOrUnknownSVal>(), true);
50 // FIXME: do we want to warn here? Not right now. The most reports might
51 // come from infeasible paths, thus being false positives.
Gabor Horvathb3bcddf2017-06-22 10:09:40 +000052 if (!state) {
53 C.generateSink(C.getState(), C.getPredecessor());
Gabor Horvath3a00b412017-05-12 07:02:54 +000054 return true;
Gabor Horvathb3bcddf2017-06-22 10:09:40 +000055 }
Gabor Horvath3a00b412017-05-12 07:02:54 +000056
57 C.addTransition(state);
58 return true;
59 }
60
Sanjay Patela24296b2015-09-02 20:01:30 +000061 case Builtin::BI__builtin_unpredictable:
Jordan Rose78cd51b2013-07-12 00:26:14 +000062 case Builtin::BI__builtin_expect:
Jordan Rose21933cc2014-09-09 21:42:16 +000063 case Builtin::BI__builtin_assume_aligned:
Jordan Rose78cd51b2013-07-12 00:26:14 +000064 case Builtin::BI__builtin_addressof: {
Sanjay Patela24296b2015-09-02 20:01:30 +000065 // For __builtin_unpredictable, __builtin_expect, and
66 // __builtin_assume_aligned, just return the value of the subexpression.
Jordan Rose78cd51b2013-07-12 00:26:14 +000067 // __builtin_addressof is going from a reference to a pointer, but those
68 // are represented the same way in the analyzer.
Zhongxing Xufe2f9012009-12-08 09:07:59 +000069 assert (CE->arg_begin() != CE->arg_end());
George Karpenkovd703ec92018-01-17 20:27:29 +000070 SVal X = C.getSVal(*(CE->arg_begin()));
Ted Kremenek632e3b72012-01-06 22:09:28 +000071 C.addTransition(state->BindExpr(CE, LCtx, X));
Zhongxing Xufe2f9012009-12-08 09:07:59 +000072 return true;
73 }
74
David Majnemer51169932016-10-31 05:37:48 +000075 case Builtin::BI__builtin_alloca_with_align:
Zhongxing Xufe2f9012009-12-08 09:07:59 +000076 case Builtin::BI__builtin_alloca: {
77 // FIXME: Refactor into StoreManager itself?
78 MemRegionManager& RM = C.getStoreManager().getRegionManager();
Jordy Rose674bd552010-07-04 00:00:41 +000079 const AllocaRegion* R =
Ted Kremenekd94854a2012-08-22 06:26:15 +000080 RM.getAllocaRegion(CE, C.blockCount(), C.getLocationContext());
Zhongxing Xufe2f9012009-12-08 09:07:59 +000081
82 // Set the extent of the region in bytes. This enables us to use the
83 // SVal of the argument directly. If we save the extent in bits, we
84 // cannot represent values like symbol*8.
George Karpenkovd703ec92018-01-17 20:27:29 +000085 auto Size = C.getSVal(*(CE->arg_begin())).castAs<DefinedOrUnknownSVal>();
Jordy Rose674bd552010-07-04 00:00:41 +000086
Ted Kremenek90af9092010-12-02 07:49:45 +000087 SValBuilder& svalBuilder = C.getSValBuilder();
88 DefinedOrUnknownSVal Extent = R->getExtent(svalBuilder);
89 DefinedOrUnknownSVal extentMatchesSizeArg =
Ted Kremenekdc891422010-12-01 21:57:22 +000090 svalBuilder.evalEQ(state, Extent, Size);
Ted Kremenek90af9092010-12-02 07:49:45 +000091 state = state->assume(extentMatchesSizeArg, true);
Anna Zakse3beeaa2012-11-26 19:11:46 +000092 assert(state && "The region should not have any previous constraints");
Jordy Rose674bd552010-07-04 00:00:41 +000093
Ted Kremenek632e3b72012-01-06 22:09:28 +000094 C.addTransition(state->BindExpr(CE, LCtx, loc::MemRegionVal(R)));
Zhongxing Xufe2f9012009-12-08 09:07:59 +000095 return true;
96 }
Zhongxing Xufe2f9012009-12-08 09:07:59 +000097
Artem Dergachevf3e09bd2018-02-10 00:51:47 +000098 case Builtin::BI__builtin_object_size:
99 case Builtin::BI__builtin_constant_p: {
Jordan Rose5374c072013-08-19 16:27:28 +0000100 // This must be resolvable at compile time, so we defer to the constant
101 // evaluator for a value.
102 SVal V = UnknownVal();
Fangrui Song407659a2018-11-30 23:41:18 +0000103 Expr::EvalResult EVResult;
104 if (CE->EvaluateAsInt(EVResult, C.getASTContext(), Expr::SE_NoSideEffects)) {
Jordan Rose5374c072013-08-19 16:27:28 +0000105 // Make sure the result has the correct type.
Fangrui Song407659a2018-11-30 23:41:18 +0000106 llvm::APSInt Result = EVResult.Val.getInt();
Jordan Rose5374c072013-08-19 16:27:28 +0000107 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}