clang API Documentation

BuiltinFunctionChecker.cpp
Go to the documentation of this file.
00001 //=== BuiltinFunctionChecker.cpp --------------------------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This checker evaluates clang builtin functions.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "ClangSACheckers.h"
00015 #include "clang/Basic/Builtins.h"
00016 #include "clang/StaticAnalyzer/Core/Checker.h"
00017 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
00018 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
00019 
00020 using namespace clang;
00021 using namespace ento;
00022 
00023 namespace {
00024 
00025 class BuiltinFunctionChecker : public Checker<eval::Call> {
00026 public:
00027   bool evalCall(const CallExpr *CE, CheckerContext &C) const;
00028 };
00029 
00030 }
00031 
00032 bool BuiltinFunctionChecker::evalCall(const CallExpr *CE,
00033                                       CheckerContext &C) const {
00034   ProgramStateRef state = C.getState();
00035   const FunctionDecl *FD = C.getCalleeDecl(CE);
00036   const LocationContext *LCtx = C.getLocationContext();
00037   if (!FD)
00038     return false;
00039 
00040   switch (FD->getBuiltinID()) {
00041   default:
00042     return false;
00043 
00044   case Builtin::BI__builtin_expect:
00045   case Builtin::BI__builtin_assume_aligned:
00046   case Builtin::BI__builtin_addressof: {
00047     // For __builtin_expect and __builtin_assume_aligned, just return the value
00048     // of the subexpression.
00049     // __builtin_addressof is going from a reference to a pointer, but those
00050     // are represented the same way in the analyzer.
00051     assert (CE->arg_begin() != CE->arg_end());
00052     SVal X = state->getSVal(*(CE->arg_begin()), LCtx);
00053     C.addTransition(state->BindExpr(CE, LCtx, X));
00054     return true;
00055   }
00056 
00057   case Builtin::BI__builtin_alloca: {
00058     // FIXME: Refactor into StoreManager itself?
00059     MemRegionManager& RM = C.getStoreManager().getRegionManager();
00060     const AllocaRegion* R =
00061       RM.getAllocaRegion(CE, C.blockCount(), C.getLocationContext());
00062 
00063     // Set the extent of the region in bytes. This enables us to use the
00064     // SVal of the argument directly. If we save the extent in bits, we
00065     // cannot represent values like symbol*8.
00066     DefinedOrUnknownSVal Size =
00067         state->getSVal(*(CE->arg_begin()), LCtx).castAs<DefinedOrUnknownSVal>();
00068 
00069     SValBuilder& svalBuilder = C.getSValBuilder();
00070     DefinedOrUnknownSVal Extent = R->getExtent(svalBuilder);
00071     DefinedOrUnknownSVal extentMatchesSizeArg =
00072       svalBuilder.evalEQ(state, Extent, Size);
00073     state = state->assume(extentMatchesSizeArg, true);
00074     assert(state && "The region should not have any previous constraints");
00075 
00076     C.addTransition(state->BindExpr(CE, LCtx, loc::MemRegionVal(R)));
00077     return true;
00078   }
00079 
00080   case Builtin::BI__builtin_object_size: {
00081     // This must be resolvable at compile time, so we defer to the constant
00082     // evaluator for a value.
00083     SVal V = UnknownVal();
00084     llvm::APSInt Result;
00085     if (CE->EvaluateAsInt(Result, C.getASTContext(), Expr::SE_NoSideEffects)) {
00086       // Make sure the result has the correct type.
00087       SValBuilder &SVB = C.getSValBuilder();
00088       BasicValueFactory &BVF = SVB.getBasicValueFactory();
00089       BVF.getAPSIntType(CE->getType()).apply(Result);
00090       V = SVB.makeIntVal(Result);
00091     }
00092 
00093     C.addTransition(state->BindExpr(CE, LCtx, V));
00094     return true;
00095   }
00096   }
00097 }
00098 
00099 void ento::registerBuiltinFunctionChecker(CheckerManager &mgr) {
00100   mgr.registerChecker<BuiltinFunctionChecker>();
00101 }