DereferenceChecker.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. //== NullDerefChecker.cpp - Null dereference checker ------------*- 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 NullDerefChecker, a builtin check in ExprEngine that performs
  11. // checks for null pointers at loads and stores.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "InternalChecks.h"
  15. #include "clang/StaticAnalyzer/BugReporter/BugType.h"
  16. #include "clang/StaticAnalyzer/Checkers/DereferenceChecker.h"
  17. #include "clang/StaticAnalyzer/PathSensitive/Checker.h"
  18. #include "clang/StaticAnalyzer/PathSensitive/ExprEngine.h"
  19. using namespace clang;
  20. using namespace ento;
  21. namespace {
  22. class DereferenceChecker : public Checker {
  23. BuiltinBug *BT_null;
  24. BuiltinBug *BT_undef;
  25. llvm::SmallVector<ExplodedNode*, 2> ImplicitNullDerefNodes;
  26. public:
  27. DereferenceChecker() : BT_null(0), BT_undef(0) {}
  28. static void *getTag() { static int tag = 0; return &tag; }
  29. void visitLocation(CheckerContext &C, const Stmt *S, SVal location,
  30. bool isLoad);
  31. std::pair<ExplodedNode * const*, ExplodedNode * const*>
  32. getImplicitNodes() const {
  33. return std::make_pair(ImplicitNullDerefNodes.data(),
  34. ImplicitNullDerefNodes.data() +
  35. ImplicitNullDerefNodes.size());
  36. }
  37. void AddDerefSource(llvm::raw_ostream &os,
  38. llvm::SmallVectorImpl<SourceRange> &Ranges,
  39. const Expr *Ex, bool loadedFrom = false);
  40. };
  41. } // end anonymous namespace
  42. void ento::RegisterDereferenceChecker(ExprEngine &Eng) {
  43. Eng.registerCheck(new DereferenceChecker());
  44. }
  45. std::pair<ExplodedNode * const *, ExplodedNode * const *>
  46. ento::GetImplicitNullDereferences(ExprEngine &Eng) {
  47. DereferenceChecker *checker = Eng.getChecker<DereferenceChecker>();
  48. if (!checker)
  49. return std::make_pair((ExplodedNode * const *) 0,
  50. (ExplodedNode * const *) 0);
  51. return checker->getImplicitNodes();
  52. }
  53. void DereferenceChecker::AddDerefSource(llvm::raw_ostream &os,
  54. llvm::SmallVectorImpl<SourceRange> &Ranges,
  55. const Expr *Ex,
  56. bool loadedFrom) {
  57. Ex = Ex->IgnoreParenLValueCasts();
  58. switch (Ex->getStmtClass()) {
  59. default:
  60. return;
  61. case Stmt::DeclRefExprClass: {
  62. const DeclRefExpr *DR = cast<DeclRefExpr>(Ex);
  63. if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
  64. os << " (" << (loadedFrom ? "loaded from" : "from")
  65. << " variable '" << VD->getName() << "')";
  66. Ranges.push_back(DR->getSourceRange());
  67. }
  68. return;
  69. }
  70. case Stmt::MemberExprClass: {
  71. const MemberExpr *ME = cast<MemberExpr>(Ex);
  72. os << " (" << (loadedFrom ? "loaded from" : "via")
  73. << " field '" << ME->getMemberNameInfo() << "')";
  74. SourceLocation L = ME->getMemberLoc();
  75. Ranges.push_back(SourceRange(L, L));
  76. break;
  77. }
  78. }
  79. }
  80. void DereferenceChecker::visitLocation(CheckerContext &C, const Stmt *S,
  81. SVal l, bool isLoad) {
  82. // Check for dereference of an undefined value.
  83. if (l.isUndef()) {
  84. if (ExplodedNode *N = C.generateSink()) {
  85. if (!BT_undef)
  86. BT_undef = new BuiltinBug("Dereference of undefined pointer value");
  87. EnhancedBugReport *report =
  88. new EnhancedBugReport(*BT_undef, BT_undef->getDescription(), N);
  89. report->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue,
  90. bugreporter::GetDerefExpr(N));
  91. C.EmitReport(report);
  92. }
  93. return;
  94. }
  95. DefinedOrUnknownSVal location = cast<DefinedOrUnknownSVal>(l);
  96. // Check for null dereferences.
  97. if (!isa<Loc>(location))
  98. return;
  99. const GRState *state = C.getState();
  100. const GRState *notNullState, *nullState;
  101. llvm::tie(notNullState, nullState) = state->assume(location);
  102. // The explicit NULL case.
  103. if (nullState) {
  104. if (!notNullState) {
  105. // Generate an error node.
  106. ExplodedNode *N = C.generateSink(nullState);
  107. if (!N)
  108. return;
  109. // We know that 'location' cannot be non-null. This is what
  110. // we call an "explicit" null dereference.
  111. if (!BT_null)
  112. BT_null = new BuiltinBug("Dereference of null pointer");
  113. llvm::SmallString<100> buf;
  114. llvm::SmallVector<SourceRange, 2> Ranges;
  115. // Walk through lvalue casts to get the original expression
  116. // that syntactically caused the load.
  117. if (const Expr *expr = dyn_cast<Expr>(S))
  118. S = expr->IgnoreParenLValueCasts();
  119. switch (S->getStmtClass()) {
  120. case Stmt::ArraySubscriptExprClass: {
  121. llvm::raw_svector_ostream os(buf);
  122. os << "Array access";
  123. const ArraySubscriptExpr *AE = cast<ArraySubscriptExpr>(S);
  124. AddDerefSource(os, Ranges, AE->getBase()->IgnoreParenCasts());
  125. os << " results in a null pointer dereference";
  126. break;
  127. }
  128. case Stmt::UnaryOperatorClass: {
  129. llvm::raw_svector_ostream os(buf);
  130. os << "Dereference of null pointer";
  131. const UnaryOperator *U = cast<UnaryOperator>(S);
  132. AddDerefSource(os, Ranges, U->getSubExpr()->IgnoreParens(), true);
  133. break;
  134. }
  135. case Stmt::MemberExprClass: {
  136. const MemberExpr *M = cast<MemberExpr>(S);
  137. if (M->isArrow()) {
  138. llvm::raw_svector_ostream os(buf);
  139. os << "Access to field '" << M->getMemberNameInfo()
  140. << "' results in a dereference of a null pointer";
  141. AddDerefSource(os, Ranges, M->getBase()->IgnoreParenCasts(), true);
  142. }
  143. break;
  144. }
  145. case Stmt::ObjCIvarRefExprClass: {
  146. const ObjCIvarRefExpr *IV = cast<ObjCIvarRefExpr>(S);
  147. if (const DeclRefExpr *DR =
  148. dyn_cast<DeclRefExpr>(IV->getBase()->IgnoreParenCasts())) {
  149. if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
  150. llvm::raw_svector_ostream os(buf);
  151. os << "Instance variable access (via '" << VD->getName()
  152. << "') results in a null pointer dereference";
  153. }
  154. }
  155. Ranges.push_back(IV->getSourceRange());
  156. break;
  157. }
  158. default:
  159. break;
  160. }
  161. EnhancedBugReport *report =
  162. new EnhancedBugReport(*BT_null,
  163. buf.empty() ? BT_null->getDescription():buf.str(),
  164. N);
  165. report->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue,
  166. bugreporter::GetDerefExpr(N));
  167. for (llvm::SmallVectorImpl<SourceRange>::iterator
  168. I = Ranges.begin(), E = Ranges.end(); I!=E; ++I)
  169. report->addRange(*I);
  170. C.EmitReport(report);
  171. return;
  172. }
  173. else {
  174. // Otherwise, we have the case where the location could either be
  175. // null or not-null. Record the error node as an "implicit" null
  176. // dereference.
  177. if (ExplodedNode *N = C.generateSink(nullState))
  178. ImplicitNullDerefNodes.push_back(N);
  179. }
  180. }
  181. // From this point forward, we know that the location is not null.
  182. C.addTransition(notNullState);
  183. }