blob: 5300fae7756dd8e28e2c0303536f8ab0ba309c10 [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
40 unsigned id = FD->getBuiltinID();
41
42 if (!id)
43 return false;
44
45 switch (id) {
Jordan Rose78cd51b2013-07-12 00:26:14 +000046 case Builtin::BI__builtin_expect:
47 case Builtin::BI__builtin_addressof: {
Zhongxing Xufe2f9012009-12-08 09:07:59 +000048 // For __builtin_expect, just return the value of the subexpression.
Jordan Rose78cd51b2013-07-12 00:26:14 +000049 // __builtin_addressof is going from a reference to a pointer, but those
50 // are represented the same way in the analyzer.
Zhongxing Xufe2f9012009-12-08 09:07:59 +000051 assert (CE->arg_begin() != CE->arg_end());
Ted Kremenek632e3b72012-01-06 22:09:28 +000052 SVal X = state->getSVal(*(CE->arg_begin()), LCtx);
53 C.addTransition(state->BindExpr(CE, LCtx, X));
Zhongxing Xufe2f9012009-12-08 09:07:59 +000054 return true;
55 }
56
57 case Builtin::BI__builtin_alloca: {
58 // FIXME: Refactor into StoreManager itself?
59 MemRegionManager& RM = C.getStoreManager().getRegionManager();
Jordy Rose674bd552010-07-04 00:00:41 +000060 const AllocaRegion* R =
Ted Kremenekd94854a2012-08-22 06:26:15 +000061 RM.getAllocaRegion(CE, C.blockCount(), C.getLocationContext());
Zhongxing Xufe2f9012009-12-08 09:07:59 +000062
63 // Set the extent of the region in bytes. This enables us to use the
64 // SVal of the argument directly. If we save the extent in bits, we
65 // cannot represent values like symbol*8.
Jordy Rose674bd552010-07-04 00:00:41 +000066 DefinedOrUnknownSVal Size =
David Blaikie2fdacbc2013-02-20 05:52:05 +000067 state->getSVal(*(CE->arg_begin()), LCtx).castAs<DefinedOrUnknownSVal>();
Jordy Rose674bd552010-07-04 00:00:41 +000068
Ted Kremenek90af9092010-12-02 07:49:45 +000069 SValBuilder& svalBuilder = C.getSValBuilder();
70 DefinedOrUnknownSVal Extent = R->getExtent(svalBuilder);
71 DefinedOrUnknownSVal extentMatchesSizeArg =
Ted Kremenekdc891422010-12-01 21:57:22 +000072 svalBuilder.evalEQ(state, Extent, Size);
Ted Kremenek90af9092010-12-02 07:49:45 +000073 state = state->assume(extentMatchesSizeArg, true);
Anna Zakse3beeaa2012-11-26 19:11:46 +000074 assert(state && "The region should not have any previous constraints");
Jordy Rose674bd552010-07-04 00:00:41 +000075
Ted Kremenek632e3b72012-01-06 22:09:28 +000076 C.addTransition(state->BindExpr(CE, LCtx, loc::MemRegionVal(R)));
Zhongxing Xufe2f9012009-12-08 09:07:59 +000077 return true;
78 }
79 }
80
81 return false;
82}
Argyrios Kyrtzidisf3ed8b62011-02-28 01:27:07 +000083
84void ento::registerBuiltinFunctionChecker(CheckerManager &mgr) {
85 mgr.registerChecker<BuiltinFunctionChecker>();
86}