blob: dab2f61229a0a847194d566b38f0faf99e927a35 [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
Sanjay Patela24296b2015-09-02 20:01:30 +000044 case Builtin::BI__builtin_unpredictable:
Jordan Rose78cd51b2013-07-12 00:26:14 +000045 case Builtin::BI__builtin_expect:
Jordan Rose21933cc2014-09-09 21:42:16 +000046 case Builtin::BI__builtin_assume_aligned:
Jordan Rose78cd51b2013-07-12 00:26:14 +000047 case Builtin::BI__builtin_addressof: {
Sanjay Patela24296b2015-09-02 20:01:30 +000048 // For __builtin_unpredictable, __builtin_expect, and
49 // __builtin_assume_aligned, just return the value of the subexpression.
Jordan Rose78cd51b2013-07-12 00:26:14 +000050 // __builtin_addressof is going from a reference to a pointer, but those
51 // are represented the same way in the analyzer.
Zhongxing Xufe2f9012009-12-08 09:07:59 +000052 assert (CE->arg_begin() != CE->arg_end());
Ted Kremenek632e3b72012-01-06 22:09:28 +000053 SVal X = state->getSVal(*(CE->arg_begin()), LCtx);
54 C.addTransition(state->BindExpr(CE, LCtx, X));
Zhongxing Xufe2f9012009-12-08 09:07:59 +000055 return true;
56 }
57
58 case Builtin::BI__builtin_alloca: {
59 // FIXME: Refactor into StoreManager itself?
60 MemRegionManager& RM = C.getStoreManager().getRegionManager();
Jordy Rose674bd552010-07-04 00:00:41 +000061 const AllocaRegion* R =
Ted Kremenekd94854a2012-08-22 06:26:15 +000062 RM.getAllocaRegion(CE, C.blockCount(), C.getLocationContext());
Zhongxing Xufe2f9012009-12-08 09:07:59 +000063
64 // Set the extent of the region in bytes. This enables us to use the
65 // SVal of the argument directly. If we save the extent in bits, we
66 // cannot represent values like symbol*8.
Jordy Rose674bd552010-07-04 00:00:41 +000067 DefinedOrUnknownSVal Size =
David Blaikie2fdacbc2013-02-20 05:52:05 +000068 state->getSVal(*(CE->arg_begin()), LCtx).castAs<DefinedOrUnknownSVal>();
Jordy Rose674bd552010-07-04 00:00:41 +000069
Ted Kremenek90af9092010-12-02 07:49:45 +000070 SValBuilder& svalBuilder = C.getSValBuilder();
71 DefinedOrUnknownSVal Extent = R->getExtent(svalBuilder);
72 DefinedOrUnknownSVal extentMatchesSizeArg =
Ted Kremenekdc891422010-12-01 21:57:22 +000073 svalBuilder.evalEQ(state, Extent, Size);
Ted Kremenek90af9092010-12-02 07:49:45 +000074 state = state->assume(extentMatchesSizeArg, true);
Anna Zakse3beeaa2012-11-26 19:11:46 +000075 assert(state && "The region should not have any previous constraints");
Jordy Rose674bd552010-07-04 00:00:41 +000076
Ted Kremenek632e3b72012-01-06 22:09:28 +000077 C.addTransition(state->BindExpr(CE, LCtx, loc::MemRegionVal(R)));
Zhongxing Xufe2f9012009-12-08 09:07:59 +000078 return true;
79 }
Zhongxing Xufe2f9012009-12-08 09:07:59 +000080
Jordan Rose5374c072013-08-19 16:27:28 +000081 case Builtin::BI__builtin_object_size: {
82 // This must be resolvable at compile time, so we defer to the constant
83 // evaluator for a value.
84 SVal V = UnknownVal();
85 llvm::APSInt Result;
86 if (CE->EvaluateAsInt(Result, C.getASTContext(), Expr::SE_NoSideEffects)) {
87 // Make sure the result has the correct type.
88 SValBuilder &SVB = C.getSValBuilder();
89 BasicValueFactory &BVF = SVB.getBasicValueFactory();
90 BVF.getAPSIntType(CE->getType()).apply(Result);
91 V = SVB.makeIntVal(Result);
92 }
93
94 C.addTransition(state->BindExpr(CE, LCtx, V));
95 return true;
96 }
97 }
Zhongxing Xufe2f9012009-12-08 09:07:59 +000098}
Argyrios Kyrtzidisf3ed8b62011-02-28 01:27:07 +000099
100void ento::registerBuiltinFunctionChecker(CheckerManager &mgr) {
101 mgr.registerChecker<BuiltinFunctionChecker>();
102}