DereferenceChecker.cpp 6.8 KB

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