blob: a871049848835915cafc549f055adce99d44ce1b [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
Jordan Rose78cd51b2013-07-12 00:26:14 +000044 case Builtin::BI__builtin_expect:
45 case Builtin::BI__builtin_addressof: {
Zhongxing Xufe2f9012009-12-08 09:07:59 +000046 // For __builtin_expect, just return the value of the subexpression.
Jordan Rose78cd51b2013-07-12 00:26:14 +000047 // __builtin_addressof is going from a reference to a pointer, but those
48 // are represented the same way in the analyzer.
Zhongxing Xufe2f9012009-12-08 09:07:59 +000049 assert (CE->arg_begin() != CE->arg_end());
Ted Kremenek632e3b72012-01-06 22:09:28 +000050 SVal X = state->getSVal(*(CE->arg_begin()), LCtx);
51 C.addTransition(state->BindExpr(CE, LCtx, X));
Zhongxing Xufe2f9012009-12-08 09:07:59 +000052 return true;
53 }
54
55 case Builtin::BI__builtin_alloca: {
56 // FIXME: Refactor into StoreManager itself?
57 MemRegionManager& RM = C.getStoreManager().getRegionManager();
Jordy Rose674bd552010-07-04 00:00:41 +000058 const AllocaRegion* R =
Ted Kremenekd94854a2012-08-22 06:26:15 +000059 RM.getAllocaRegion(CE, C.blockCount(), C.getLocationContext());
Zhongxing Xufe2f9012009-12-08 09:07:59 +000060
61 // Set the extent of the region in bytes. This enables us to use the
62 // SVal of the argument directly. If we save the extent in bits, we
63 // cannot represent values like symbol*8.
Jordy Rose674bd552010-07-04 00:00:41 +000064 DefinedOrUnknownSVal Size =
David Blaikie2fdacbc2013-02-20 05:52:05 +000065 state->getSVal(*(CE->arg_begin()), LCtx).castAs<DefinedOrUnknownSVal>();
Jordy Rose674bd552010-07-04 00:00:41 +000066
Ted Kremenek90af9092010-12-02 07:49:45 +000067 SValBuilder& svalBuilder = C.getSValBuilder();
68 DefinedOrUnknownSVal Extent = R->getExtent(svalBuilder);
69 DefinedOrUnknownSVal extentMatchesSizeArg =
Ted Kremenekdc891422010-12-01 21:57:22 +000070 svalBuilder.evalEQ(state, Extent, Size);
Ted Kremenek90af9092010-12-02 07:49:45 +000071 state = state->assume(extentMatchesSizeArg, true);
Anna Zakse3beeaa2012-11-26 19:11:46 +000072 assert(state && "The region should not have any previous constraints");
Jordy Rose674bd552010-07-04 00:00:41 +000073
Ted Kremenek632e3b72012-01-06 22:09:28 +000074 C.addTransition(state->BindExpr(CE, LCtx, loc::MemRegionVal(R)));
Zhongxing Xufe2f9012009-12-08 09:07:59 +000075 return true;
76 }
Zhongxing Xufe2f9012009-12-08 09:07:59 +000077
Jordan Rose5374c072013-08-19 16:27:28 +000078 case Builtin::BI__builtin_object_size: {
79 // This must be resolvable at compile time, so we defer to the constant
80 // evaluator for a value.
81 SVal V = UnknownVal();
82 llvm::APSInt Result;
83 if (CE->EvaluateAsInt(Result, C.getASTContext(), Expr::SE_NoSideEffects)) {
84 // Make sure the result has the correct type.
85 SValBuilder &SVB = C.getSValBuilder();
86 BasicValueFactory &BVF = SVB.getBasicValueFactory();
87 BVF.getAPSIntType(CE->getType()).apply(Result);
88 V = SVB.makeIntVal(Result);
89 }
90
91 C.addTransition(state->BindExpr(CE, LCtx, V));
92 return true;
93 }
94 }
Zhongxing Xufe2f9012009-12-08 09:07:59 +000095}
Argyrios Kyrtzidisf3ed8b62011-02-28 01:27:07 +000096
97void ento::registerBuiltinFunctionChecker(CheckerManager &mgr) {
98 mgr.registerChecker<BuiltinFunctionChecker>();
99}