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/Basic/Builtins.h" 16 #include "clang/StaticAnalyzer/Core/Checker.h" 17 #include "clang/StaticAnalyzer/Core/CheckerManager.h" 18 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.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 ProgramStateRef state = C.getState(); 35 const FunctionDecl *FD = C.getCalleeDecl(CE); 36 const LocationContext *LCtx = C.getLocationContext(); 37 if (!FD) 38 return false; 39 40 switch (FD->getBuiltinID()) { 41 default: 42 return false; 43 44 case Builtin::BI__builtin_expect: 45 case Builtin::BI__builtin_addressof: { 46 // For __builtin_expect, just return the value of the subexpression. 47 // __builtin_addressof is going from a reference to a pointer, but those 48 // are represented the same way in the analyzer. 49 assert (CE->arg_begin() != CE->arg_end()); 50 SVal X = state->getSVal(*(CE->arg_begin()), LCtx); 51 C.addTransition(state->BindExpr(CE, LCtx, X)); 52 return true; 53 } 54 55 case Builtin::BI__builtin_alloca: { 56 // FIXME: Refactor into StoreManager itself? 57 MemRegionManager& RM = C.getStoreManager().getRegionManager(); 58 const AllocaRegion* R = 59 RM.getAllocaRegion(CE, C.blockCount(), C.getLocationContext()); 60 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. 64 DefinedOrUnknownSVal Size = 65 state->getSVal(*(CE->arg_begin()), LCtx).castAs<DefinedOrUnknownSVal>(); 66 67 SValBuilder& svalBuilder = C.getSValBuilder(); 68 DefinedOrUnknownSVal Extent = R->getExtent(svalBuilder); 69 DefinedOrUnknownSVal extentMatchesSizeArg = 70 svalBuilder.evalEQ(state, Extent, Size); 71 state = state->assume(extentMatchesSizeArg, true); 72 assert(state && "The region should not have any previous constraints"); 73 74 C.addTransition(state->BindExpr(CE, LCtx, loc::MemRegionVal(R))); 75 return true; 76 } 77 78 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 } 95 } 96 97 void ento::registerBuiltinFunctionChecker(CheckerManager &mgr) { 98 mgr.registerChecker<BuiltinFunctionChecker>(); 99 } 100