UndefResultChecker.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. //=== UndefResultChecker.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 defines UndefResultChecker, a builtin check in ExprEngine that
  11. // performs checks for undefined results of non-assignment binary operators.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "ClangSACheckers.h"
  15. #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
  16. #include "clang/StaticAnalyzer/Core/Checker.h"
  17. #include "clang/StaticAnalyzer/Core/CheckerManager.h"
  18. #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
  19. #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
  20. #include "llvm/ADT/SmallString.h"
  21. #include "llvm/Support/raw_ostream.h"
  22. using namespace clang;
  23. using namespace ento;
  24. namespace {
  25. class UndefResultChecker
  26. : public Checker< check::PostStmt<BinaryOperator> > {
  27. mutable std::unique_ptr<BugType> BT;
  28. public:
  29. void checkPostStmt(const BinaryOperator *B, CheckerContext &C) const;
  30. };
  31. } // end anonymous namespace
  32. static bool isArrayIndexOutOfBounds(CheckerContext &C, const Expr *Ex) {
  33. ProgramStateRef state = C.getState();
  34. if (!isa<ArraySubscriptExpr>(Ex))
  35. return false;
  36. SVal Loc = C.getSVal(Ex);
  37. if (!Loc.isValid())
  38. return false;
  39. const MemRegion *MR = Loc.castAs<loc::MemRegionVal>().getRegion();
  40. const ElementRegion *ER = dyn_cast<ElementRegion>(MR);
  41. if (!ER)
  42. return false;
  43. DefinedOrUnknownSVal Idx = ER->getIndex().castAs<DefinedOrUnknownSVal>();
  44. DefinedOrUnknownSVal NumElements = C.getStoreManager().getSizeInElements(
  45. state, ER->getSuperRegion(), ER->getValueType());
  46. ProgramStateRef StInBound = state->assumeInBound(Idx, NumElements, true);
  47. ProgramStateRef StOutBound = state->assumeInBound(Idx, NumElements, false);
  48. return StOutBound && !StInBound;
  49. }
  50. static bool isShiftOverflow(const BinaryOperator *B, CheckerContext &C) {
  51. return C.isGreaterOrEqual(
  52. B->getRHS(), C.getASTContext().getIntWidth(B->getLHS()->getType()));
  53. }
  54. void UndefResultChecker::checkPostStmt(const BinaryOperator *B,
  55. CheckerContext &C) const {
  56. if (C.getSVal(B).isUndef()) {
  57. // Do not report assignments of uninitialized values inside swap functions.
  58. // This should allow to swap partially uninitialized structs
  59. // (radar://14129997)
  60. if (const FunctionDecl *EnclosingFunctionDecl =
  61. dyn_cast<FunctionDecl>(C.getStackFrame()->getDecl()))
  62. if (C.getCalleeName(EnclosingFunctionDecl) == "swap")
  63. return;
  64. // Generate an error node.
  65. ExplodedNode *N = C.generateErrorNode();
  66. if (!N)
  67. return;
  68. if (!BT)
  69. BT.reset(
  70. new BuiltinBug(this, "Result of operation is garbage or undefined"));
  71. SmallString<256> sbuf;
  72. llvm::raw_svector_ostream OS(sbuf);
  73. const Expr *Ex = nullptr;
  74. bool isLeft = true;
  75. if (C.getSVal(B->getLHS()).isUndef()) {
  76. Ex = B->getLHS()->IgnoreParenCasts();
  77. isLeft = true;
  78. }
  79. else if (C.getSVal(B->getRHS()).isUndef()) {
  80. Ex = B->getRHS()->IgnoreParenCasts();
  81. isLeft = false;
  82. }
  83. if (Ex) {
  84. OS << "The " << (isLeft ? "left" : "right") << " operand of '"
  85. << BinaryOperator::getOpcodeStr(B->getOpcode())
  86. << "' is a garbage value";
  87. if (isArrayIndexOutOfBounds(C, Ex))
  88. OS << " due to array index out of bounds";
  89. } else {
  90. // Neither operand was undefined, but the result is undefined.
  91. if ((B->getOpcode() == BinaryOperatorKind::BO_Shl ||
  92. B->getOpcode() == BinaryOperatorKind::BO_Shr) &&
  93. C.isNegative(B->getRHS())) {
  94. OS << "The result of the "
  95. << ((B->getOpcode() == BinaryOperatorKind::BO_Shl) ? "left"
  96. : "right")
  97. << " shift is undefined because the right operand is negative";
  98. } else if ((B->getOpcode() == BinaryOperatorKind::BO_Shl ||
  99. B->getOpcode() == BinaryOperatorKind::BO_Shr) &&
  100. isShiftOverflow(B, C)) {
  101. OS << "The result of the "
  102. << ((B->getOpcode() == BinaryOperatorKind::BO_Shl) ? "left"
  103. : "right")
  104. << " shift is undefined due to shifting by ";
  105. SValBuilder &SB = C.getSValBuilder();
  106. const llvm::APSInt *I =
  107. SB.getKnownValue(C.getState(), C.getSVal(B->getRHS()));
  108. if (!I)
  109. OS << "a value that is";
  110. else if (I->isUnsigned())
  111. OS << '\'' << I->getZExtValue() << "\', which is";
  112. else
  113. OS << '\'' << I->getSExtValue() << "\', which is";
  114. OS << " greater or equal to the width of type '"
  115. << B->getLHS()->getType().getAsString() << "'.";
  116. } else if (B->getOpcode() == BinaryOperatorKind::BO_Shl &&
  117. C.isNegative(B->getLHS())) {
  118. OS << "The result of the left shift is undefined because the left "
  119. "operand is negative";
  120. } else {
  121. OS << "The result of the '"
  122. << BinaryOperator::getOpcodeStr(B->getOpcode())
  123. << "' expression is undefined";
  124. }
  125. }
  126. auto report = llvm::make_unique<BugReport>(*BT, OS.str(), N);
  127. if (Ex) {
  128. report->addRange(Ex->getSourceRange());
  129. bugreporter::trackNullOrUndefValue(N, Ex, *report);
  130. }
  131. else
  132. bugreporter::trackNullOrUndefValue(N, B, *report);
  133. C.emitReport(std::move(report));
  134. }
  135. }
  136. void ento::registerUndefResultChecker(CheckerManager &mgr) {
  137. mgr.registerChecker<UndefResultChecker>();
  138. }