blob: 50938fa777b9a27340c92cabc857fb9a63404197 [file] [log] [blame]
Zhongxing Xu7c9624b2009-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 Kyrtzidis10348702011-02-28 01:27:07 +000014#include "ClangSACheckers.h"
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +000015#include "clang/StaticAnalyzer/Core/Checker.h"
Argyrios Kyrtzidis10348702011-02-28 01:27:07 +000016#include "clang/StaticAnalyzer/Core/CheckerManager.h"
17#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Zhongxing Xu7c9624b2009-12-08 09:07:59 +000018#include "clang/Basic/Builtins.h"
Zhongxing Xu7c9624b2009-12-08 09:07:59 +000019
20using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000021using namespace ento;
Zhongxing Xu7c9624b2009-12-08 09:07:59 +000022
23namespace {
24
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +000025class BuiltinFunctionChecker : public Checker<eval::Call> {
Zhongxing Xu7c9624b2009-12-08 09:07:59 +000026public:
Argyrios Kyrtzidis10348702011-02-28 01:27:07 +000027 bool evalCall(const CallExpr *CE, CheckerContext &C) const;
Zhongxing Xu7c9624b2009-12-08 09:07:59 +000028};
29
30}
31
Argyrios Kyrtzidis10348702011-02-28 01:27:07 +000032bool BuiltinFunctionChecker::evalCall(const CallExpr *CE,
Anna Zaksb805c8f2011-12-01 05:57:37 +000033 CheckerContext &C) const {
Ted Kremenek18c66fd2011-08-15 22:09:50 +000034 const ProgramState *state = C.getState();
Anna Zaksb805c8f2011-12-01 05:57:37 +000035 const FunctionDecl *FD = C.getCalleeDecl(CE);
Ted Kremenek5eca4822012-01-06 22:09:28 +000036 const LocationContext *LCtx = C.getLocationContext();
Zhongxing Xu7c9624b2009-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) {
46 case Builtin::BI__builtin_expect: {
47 // For __builtin_expect, just return the value of the subexpression.
48 assert (CE->arg_begin() != CE->arg_end());
Ted Kremenek5eca4822012-01-06 22:09:28 +000049 SVal X = state->getSVal(*(CE->arg_begin()), LCtx);
50 C.addTransition(state->BindExpr(CE, LCtx, X));
Zhongxing Xu7c9624b2009-12-08 09:07:59 +000051 return true;
52 }
53
54 case Builtin::BI__builtin_alloca: {
55 // FIXME: Refactor into StoreManager itself?
56 MemRegionManager& RM = C.getStoreManager().getRegionManager();
Jordy Rose32f26562010-07-04 00:00:41 +000057 const AllocaRegion* R =
Anna Zaks39ac1872011-10-26 21:06:44 +000058 RM.getAllocaRegion(CE, C.getCurrentBlockCount(), C.getLocationContext());
Zhongxing Xu7c9624b2009-12-08 09:07:59 +000059
60 // Set the extent of the region in bytes. This enables us to use the
61 // SVal of the argument directly. If we save the extent in bits, we
62 // cannot represent values like symbol*8.
Jordy Rose32f26562010-07-04 00:00:41 +000063 DefinedOrUnknownSVal Size =
Ted Kremenek5eca4822012-01-06 22:09:28 +000064 cast<DefinedOrUnknownSVal>(state->getSVal(*(CE->arg_begin()), LCtx));
Jordy Rose32f26562010-07-04 00:00:41 +000065
Ted Kremenekc8413fd2010-12-02 07:49:45 +000066 SValBuilder& svalBuilder = C.getSValBuilder();
67 DefinedOrUnknownSVal Extent = R->getExtent(svalBuilder);
68 DefinedOrUnknownSVal extentMatchesSizeArg =
Ted Kremenek9c149532010-12-01 21:57:22 +000069 svalBuilder.evalEQ(state, Extent, Size);
Ted Kremenekc8413fd2010-12-02 07:49:45 +000070 state = state->assume(extentMatchesSizeArg, true);
Jordy Rose32f26562010-07-04 00:00:41 +000071
Ted Kremenek5eca4822012-01-06 22:09:28 +000072 C.addTransition(state->BindExpr(CE, LCtx, loc::MemRegionVal(R)));
Zhongxing Xu7c9624b2009-12-08 09:07:59 +000073 return true;
74 }
75 }
76
77 return false;
78}
Argyrios Kyrtzidis10348702011-02-28 01:27:07 +000079
80void ento::registerBuiltinFunctionChecker(CheckerManager &mgr) {
81 mgr.registerChecker<BuiltinFunctionChecker>();
82}