BuiltinFunctionChecker.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. #include "ClangSACheckers.h"
  14. #include "clang/Basic/Builtins.h"
  15. #include "clang/StaticAnalyzer/Core/Checker.h"
  16. #include "clang/StaticAnalyzer/Core/CheckerManager.h"
  17. #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
  18. using namespace clang;
  19. using namespace ento;
  20. namespace {
  21. class BuiltinFunctionChecker : public Checker<eval::Call> {
  22. public:
  23. bool evalCall(const CallExpr *CE, CheckerContext &C) const;
  24. };
  25. }
  26. bool BuiltinFunctionChecker::evalCall(const CallExpr *CE,
  27. CheckerContext &C) const {
  28. ProgramStateRef state = C.getState();
  29. const FunctionDecl *FD = C.getCalleeDecl(CE);
  30. const LocationContext *LCtx = C.getLocationContext();
  31. if (!FD)
  32. return false;
  33. switch (FD->getBuiltinID()) {
  34. default:
  35. return false;
  36. case Builtin::BI__builtin_expect:
  37. case Builtin::BI__builtin_assume_aligned:
  38. case Builtin::BI__builtin_addressof: {
  39. // For __builtin_expect and __builtin_assume_aligned, just return the value
  40. // of the subexpression.
  41. // __builtin_addressof is going from a reference to a pointer, but those
  42. // are represented the same way in the analyzer.
  43. assert (CE->arg_begin() != CE->arg_end());
  44. SVal X = state->getSVal(*(CE->arg_begin()), LCtx);
  45. C.addTransition(state->BindExpr(CE, LCtx, X));
  46. return true;
  47. }
  48. case Builtin::BI__builtin_alloca: {
  49. // FIXME: Refactor into StoreManager itself?
  50. MemRegionManager& RM = C.getStoreManager().getRegionManager();
  51. const AllocaRegion* R =
  52. RM.getAllocaRegion(CE, C.blockCount(), C.getLocationContext());
  53. // Set the extent of the region in bytes. This enables us to use the
  54. // SVal of the argument directly. If we save the extent in bits, we
  55. // cannot represent values like symbol*8.
  56. DefinedOrUnknownSVal Size =
  57. state->getSVal(*(CE->arg_begin()), LCtx).castAs<DefinedOrUnknownSVal>();
  58. SValBuilder& svalBuilder = C.getSValBuilder();
  59. DefinedOrUnknownSVal Extent = R->getExtent(svalBuilder);
  60. DefinedOrUnknownSVal extentMatchesSizeArg =
  61. svalBuilder.evalEQ(state, Extent, Size);
  62. state = state->assume(extentMatchesSizeArg, true);
  63. assert(state && "The region should not have any previous constraints");
  64. C.addTransition(state->BindExpr(CE, LCtx, loc::MemRegionVal(R)));
  65. return true;
  66. }
  67. case Builtin::BI__builtin_object_size: {
  68. // This must be resolvable at compile time, so we defer to the constant
  69. // evaluator for a value.
  70. SVal V = UnknownVal();
  71. llvm::APSInt Result;
  72. if (CE->EvaluateAsInt(Result, C.getASTContext(), Expr::SE_NoSideEffects)) {
  73. // Make sure the result has the correct type.
  74. SValBuilder &SVB = C.getSValBuilder();
  75. BasicValueFactory &BVF = SVB.getBasicValueFactory();
  76. BVF.getAPSIntType(CE->getType()).apply(Result);
  77. V = SVB.makeIntVal(Result);
  78. }
  79. C.addTransition(state->BindExpr(CE, LCtx, V));
  80. return true;
  81. }
  82. }
  83. }
  84. void ento::registerBuiltinFunctionChecker(CheckerManager &mgr) {
  85. mgr.registerChecker<BuiltinFunctionChecker>();
  86. }