Ted Kremenek | bc3a021 | 2009-10-30 17:24:47 +0000 | [diff] [blame] | 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 GRExprEngine that performs |
| 11 | // checks for null pointers at loads and stores. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Ted Kremenek | dc998c1 | 2009-11-03 18:41:06 +0000 | [diff] [blame] | 15 | #include "clang/Analysis/PathSensitive/Checkers/DereferenceChecker.h" |
Ted Kremenek | b4b817d | 2009-11-11 03:26:34 +0000 | [diff] [blame] | 16 | #include "clang/Analysis/PathSensitive/Checker.h" |
Ted Kremenek | bc3a021 | 2009-10-30 17:24:47 +0000 | [diff] [blame] | 17 | #include "clang/Analysis/PathSensitive/GRExprEngine.h" |
| 18 | #include "clang/Analysis/PathSensitive/BugReporter.h" |
Ted Kremenek | b4b817d | 2009-11-11 03:26:34 +0000 | [diff] [blame] | 19 | #include "GRExprEngineInternalChecks.h" |
Ted Kremenek | bc3a021 | 2009-10-30 17:24:47 +0000 | [diff] [blame] | 20 | |
| 21 | using namespace clang; |
| 22 | |
Ted Kremenek | b4b817d | 2009-11-11 03:26:34 +0000 | [diff] [blame] | 23 | namespace { |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 24 | class DereferenceChecker : public Checker { |
Ted Kremenek | b4b817d | 2009-11-11 03:26:34 +0000 | [diff] [blame] | 25 | BuiltinBug *BT_null; |
| 26 | BuiltinBug *BT_undef; |
| 27 | llvm::SmallVector<ExplodedNode*, 2> ImplicitNullDerefNodes; |
| 28 | public: |
| 29 | DereferenceChecker() : BT_null(0), BT_undef(0) {} |
| 30 | static void *getTag() { static int tag = 0; return &tag; } |
| 31 | void VisitLocation(CheckerContext &C, const Stmt *S, SVal location); |
| 32 | |
| 33 | std::pair<ExplodedNode * const*, ExplodedNode * const*> |
| 34 | getImplicitNodes() const { |
| 35 | return std::make_pair(ImplicitNullDerefNodes.data(), |
| 36 | ImplicitNullDerefNodes.data() + |
| 37 | ImplicitNullDerefNodes.size()); |
| 38 | } |
| 39 | }; |
| 40 | } // end anonymous namespace |
| 41 | |
| 42 | void clang::RegisterDereferenceChecker(GRExprEngine &Eng) { |
| 43 | Eng.registerCheck(new DereferenceChecker()); |
Ted Kremenek | bc3a021 | 2009-10-30 17:24:47 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Ted Kremenek | b4b817d | 2009-11-11 03:26:34 +0000 | [diff] [blame] | 46 | std::pair<ExplodedNode * const *, ExplodedNode * const *> |
| 47 | clang::GetImplicitNullDereferences(GRExprEngine &Eng) { |
| 48 | DereferenceChecker *checker = Eng.getChecker<DereferenceChecker>(); |
| 49 | if (!checker) |
| 50 | return std::make_pair((ExplodedNode * const *) 0, |
| 51 | (ExplodedNode * const *) 0); |
| 52 | return checker->getImplicitNodes(); |
| 53 | } |
| 54 | |
| 55 | void DereferenceChecker::VisitLocation(CheckerContext &C, const Stmt *S, |
| 56 | SVal l) { |
| 57 | // Check for dereference of an undefined value. |
| 58 | if (l.isUndef()) { |
Ted Kremenek | 19d67b5 | 2009-11-23 22:22:01 +0000 | [diff] [blame] | 59 | if (ExplodedNode *N = C.GenerateSink()) { |
Ted Kremenek | b4b817d | 2009-11-11 03:26:34 +0000 | [diff] [blame] | 60 | if (!BT_undef) |
| 61 | BT_undef = new BuiltinBug("Dereference of undefined pointer value"); |
Ted Kremenek | bc3a021 | 2009-10-30 17:24:47 +0000 | [diff] [blame] | 62 | |
Ted Kremenek | b4b817d | 2009-11-11 03:26:34 +0000 | [diff] [blame] | 63 | EnhancedBugReport *report = |
Benjamin Kramer | d02e232 | 2009-11-14 12:08:24 +0000 | [diff] [blame] | 64 | new EnhancedBugReport(*BT_undef, BT_undef->getDescription(), N); |
Ted Kremenek | b4b817d | 2009-11-11 03:26:34 +0000 | [diff] [blame] | 65 | report->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, |
| 66 | bugreporter::GetDerefExpr(N)); |
| 67 | C.EmitReport(report); |
| 68 | } |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | DefinedOrUnknownSVal location = cast<DefinedOrUnknownSVal>(l); |
| 73 | |
| 74 | // Check for null dereferences. |
| 75 | if (!isa<Loc>(location)) |
| 76 | return; |
| 77 | |
| 78 | const GRState *state = C.getState(); |
| 79 | const GRState *notNullState, *nullState; |
| 80 | llvm::tie(notNullState, nullState) = state->Assume(location); |
| 81 | |
| 82 | // The explicit NULL case. |
| 83 | if (nullState) { |
Ted Kremenek | 78d722f | 2009-11-21 01:50:48 +0000 | [diff] [blame] | 84 | if (!notNullState) { |
| 85 | // Generate an error node. |
Ted Kremenek | 19d67b5 | 2009-11-23 22:22:01 +0000 | [diff] [blame] | 86 | ExplodedNode *N = C.GenerateSink(nullState); |
Ted Kremenek | 78d722f | 2009-11-21 01:50:48 +0000 | [diff] [blame] | 87 | if (!N) |
Ted Kremenek | b4b817d | 2009-11-11 03:26:34 +0000 | [diff] [blame] | 88 | return; |
Ted Kremenek | 78d722f | 2009-11-21 01:50:48 +0000 | [diff] [blame] | 89 | |
| 90 | // We know that 'location' cannot be non-null. This is what |
| 91 | // we call an "explicit" null dereference. |
| 92 | if (!BT_null) |
Ted Kremenek | e576af2 | 2009-11-24 01:33:10 +0000 | [diff] [blame] | 93 | BT_null = new BuiltinBug("Dereference of null pointer"); |
| 94 | |
| 95 | llvm::SmallString<100> buf; |
| 96 | |
| 97 | switch (S->getStmtClass()) { |
| 98 | case Stmt::UnaryOperatorClass: { |
| 99 | const UnaryOperator *U = cast<UnaryOperator>(S); |
| 100 | const Expr *SU = U->getSubExpr()->IgnoreParens(); |
| 101 | if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(SU)) { |
| 102 | if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) { |
| 103 | llvm::raw_svector_ostream os(buf); |
| 104 | os << "Dereference of null pointer loaded from variable '" |
| 105 | << VD->getName() << '\''; |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | default: |
| 110 | break; |
| 111 | } |
Ted Kremenek | 2c791bd | 2009-11-06 00:44:32 +0000 | [diff] [blame] | 112 | |
Ted Kremenek | 78d722f | 2009-11-21 01:50:48 +0000 | [diff] [blame] | 113 | EnhancedBugReport *report = |
Ted Kremenek | e576af2 | 2009-11-24 01:33:10 +0000 | [diff] [blame] | 114 | new EnhancedBugReport(*BT_null, |
| 115 | buf.empty() ? BT_null->getDescription():buf.str(), |
| 116 | N); |
| 117 | |
Ted Kremenek | 78d722f | 2009-11-21 01:50:48 +0000 | [diff] [blame] | 118 | report->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, |
| 119 | bugreporter::GetDerefExpr(N)); |
| 120 | |
| 121 | C.EmitReport(report); |
| 122 | return; |
| 123 | } |
| 124 | else { |
Ted Kremenek | b4b817d | 2009-11-11 03:26:34 +0000 | [diff] [blame] | 125 | // Otherwise, we have the case where the location could either be |
| 126 | // null or not-null. Record the error node as an "implicit" null |
Ted Kremenek | 78d722f | 2009-11-21 01:50:48 +0000 | [diff] [blame] | 127 | // dereference. |
Ted Kremenek | 19d67b5 | 2009-11-23 22:22:01 +0000 | [diff] [blame] | 128 | if (ExplodedNode *N = C.GenerateSink(nullState)) |
Ted Kremenek | 78d722f | 2009-11-21 01:50:48 +0000 | [diff] [blame] | 129 | ImplicitNullDerefNodes.push_back(N); |
Ted Kremenek | bc3a021 | 2009-10-30 17:24:47 +0000 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
Ted Kremenek | b4b817d | 2009-11-11 03:26:34 +0000 | [diff] [blame] | 133 | // From this point forward, we know that the location is not null. |
Ted Kremenek | 19d67b5 | 2009-11-23 22:22:01 +0000 | [diff] [blame] | 134 | C.addTransition(notNullState); |
Ted Kremenek | bc3a021 | 2009-10-30 17:24:47 +0000 | [diff] [blame] | 135 | } |