UndefinedAssignmentChecker.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //===--- UndefinedAssignmentChecker.h ---------------------------*- C++ -*--==//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This defines UndefinedAssignmentChecker, a builtin check in ExprEngine that
  10. // checks for assigning undefined values.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
  14. #include "clang/StaticAnalyzer/Core/BugReporter/BugType.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 UndefinedAssignmentChecker
  22. : public Checker<check::Bind> {
  23. mutable std::unique_ptr<BugType> BT;
  24. public:
  25. void checkBind(SVal location, SVal val, const Stmt *S,
  26. CheckerContext &C) const;
  27. };
  28. }
  29. void UndefinedAssignmentChecker::checkBind(SVal location, SVal val,
  30. const Stmt *StoreE,
  31. CheckerContext &C) const {
  32. if (!val.isUndef())
  33. return;
  34. // Do not report assignments of uninitialized values inside swap functions.
  35. // This should allow to swap partially uninitialized structs
  36. // (radar://14129997)
  37. if (const FunctionDecl *EnclosingFunctionDecl =
  38. dyn_cast<FunctionDecl>(C.getStackFrame()->getDecl()))
  39. if (C.getCalleeName(EnclosingFunctionDecl) == "swap")
  40. return;
  41. ExplodedNode *N = C.generateErrorNode();
  42. if (!N)
  43. return;
  44. static const char *const DefaultMsg =
  45. "Assigned value is garbage or undefined";
  46. if (!BT)
  47. BT.reset(new BuiltinBug(this, DefaultMsg));
  48. // Generate a report for this bug.
  49. llvm::SmallString<128> Str;
  50. llvm::raw_svector_ostream OS(Str);
  51. const Expr *ex = nullptr;
  52. while (StoreE) {
  53. if (const UnaryOperator *U = dyn_cast<UnaryOperator>(StoreE)) {
  54. OS << "The expression is an uninitialized value. "
  55. "The computed value will also be garbage";
  56. ex = U->getSubExpr();
  57. break;
  58. }
  59. if (const BinaryOperator *B = dyn_cast<BinaryOperator>(StoreE)) {
  60. if (B->isCompoundAssignmentOp()) {
  61. if (C.getSVal(B->getLHS()).isUndef()) {
  62. OS << "The left expression of the compound assignment is an "
  63. "uninitialized value. The computed value will also be garbage";
  64. ex = B->getLHS();
  65. break;
  66. }
  67. }
  68. ex = B->getRHS();
  69. break;
  70. }
  71. if (const DeclStmt *DS = dyn_cast<DeclStmt>(StoreE)) {
  72. const VarDecl *VD = cast<VarDecl>(DS->getSingleDecl());
  73. ex = VD->getInit();
  74. }
  75. if (const auto *CD =
  76. dyn_cast<CXXConstructorDecl>(C.getStackFrame()->getDecl())) {
  77. if (CD->isImplicit()) {
  78. for (auto I : CD->inits()) {
  79. if (I->getInit()->IgnoreImpCasts() == StoreE) {
  80. OS << "Value assigned to field '" << I->getMember()->getName()
  81. << "' in implicit constructor is garbage or undefined";
  82. break;
  83. }
  84. }
  85. }
  86. }
  87. break;
  88. }
  89. if (OS.str().empty())
  90. OS << DefaultMsg;
  91. auto R = std::make_unique<PathSensitiveBugReport>(*BT, OS.str(), N);
  92. if (ex) {
  93. R->addRange(ex->getSourceRange());
  94. bugreporter::trackExpressionValue(N, ex, *R);
  95. }
  96. C.emitReport(std::move(R));
  97. }
  98. void ento::registerUndefinedAssignmentChecker(CheckerManager &mgr) {
  99. mgr.registerChecker<UndefinedAssignmentChecker>();
  100. }
  101. bool ento::shouldRegisterUndefinedAssignmentChecker(const CheckerManager &mgr) {
  102. return true;
  103. }