1 //=== 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 14 #include "ClangSACheckers.h" 15 #include "clang/StaticAnalyzer/Core/Checker.h" 16 #include "clang/StaticAnalyzer/Core/CheckerManager.h" 17 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" 18 #include "clang/Basic/Builtins.h" 19 20 using namespace clang; 21 using namespace ento; 22 23 namespace { 24 25 class BuiltinFunctionChecker : public Checker<eval::Call> { 26 public: 27 bool evalCall(const CallExpr *CE, CheckerContext &C) const; 28 }; 29 30 } 31 32 bool BuiltinFunctionChecker::evalCall(const CallExpr *CE, 33 CheckerContext &C) const { 34 const ProgramState *state = C.getState(); 35 const FunctionDecl *FD = C.getCalleeDecl(CE); 36 if (!FD) 37 return false; 38 39 unsigned id = FD->getBuiltinID(); 40 41 if (!id) 42 return false; 43 44 switch (id) { 45 case Builtin::BI__builtin_expect: { 46 // For __builtin_expect, just return the value of the subexpression. 47 assert (CE->arg_begin() != CE->arg_end()); 48 SVal X = state->getSVal(*(CE->arg_begin())); 49 C.addTransition(state->BindExpr(CE, X)); 50 return true; 51 } 52 53 case Builtin::BI__builtin_alloca: { 54 // FIXME: Refactor into StoreManager itself? 55 MemRegionManager& RM = C.getStoreManager().getRegionManager(); 56 const AllocaRegion* R = 57 RM.getAllocaRegion(CE, C.getCurrentBlockCount(), C.getLocationContext()); 58 59 // Set the extent of the region in bytes. This enables us to use the 60 // SVal of the argument directly. If we save the extent in bits, we 61 // cannot represent values like symbol*8. 62 DefinedOrUnknownSVal Size = 63 cast<DefinedOrUnknownSVal>(state->getSVal(*(CE->arg_begin()))); 64 65 SValBuilder& svalBuilder = C.getSValBuilder(); 66 DefinedOrUnknownSVal Extent = R->getExtent(svalBuilder); 67 DefinedOrUnknownSVal extentMatchesSizeArg = 68 svalBuilder.evalEQ(state, Extent, Size); 69 state = state->assume(extentMatchesSizeArg, true); 70 71 C.addTransition(state->BindExpr(CE, loc::MemRegionVal(R))); 72 return true; 73 } 74 } 75 76 return false; 77 } 78 79 void ento::registerBuiltinFunctionChecker(CheckerManager &mgr) { 80 mgr.registerChecker<BuiltinFunctionChecker>(); 81 } 82