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 { |
| 24 | class VISIBILITY_HIDDEN DereferenceChecker : public Checker { |
| 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()) { |
| 59 | ExplodedNode *N = C.GenerateNode(S, true); |
Ted Kremenek | bc3a021 | 2009-10-30 17:24:47 +0000 | [diff] [blame] | 60 | if (N) { |
Ted Kremenek | b4b817d | 2009-11-11 03:26:34 +0000 | [diff] [blame^] | 61 | if (!BT_undef) |
| 62 | BT_undef = new BuiltinBug("Dereference of undefined pointer value"); |
Ted Kremenek | bc3a021 | 2009-10-30 17:24:47 +0000 | [diff] [blame] | 63 | |
Ted Kremenek | b4b817d | 2009-11-11 03:26:34 +0000 | [diff] [blame^] | 64 | EnhancedBugReport *report = |
| 65 | new EnhancedBugReport(*BT_undef, BT_undef->getDescription().c_str(), N); |
| 66 | report->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, |
| 67 | bugreporter::GetDerefExpr(N)); |
| 68 | C.EmitReport(report); |
| 69 | } |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | DefinedOrUnknownSVal location = cast<DefinedOrUnknownSVal>(l); |
| 74 | |
| 75 | // Check for null dereferences. |
| 76 | if (!isa<Loc>(location)) |
| 77 | return; |
| 78 | |
| 79 | const GRState *state = C.getState(); |
| 80 | const GRState *notNullState, *nullState; |
| 81 | llvm::tie(notNullState, nullState) = state->Assume(location); |
| 82 | |
| 83 | // The explicit NULL case. |
| 84 | if (nullState) { |
| 85 | // Generate an error node. |
| 86 | ExplodedNode *N = C.GenerateNode(S, nullState, true); |
| 87 | if (N) { |
| 88 | if (!notNullState) { |
| 89 | // We know that 'location' cannot be non-null. This is what |
| 90 | // we call an "explicit" null dereference. |
| 91 | if (!BT_null) |
| 92 | BT_null = new BuiltinBug("Null pointer dereference", |
| 93 | "Dereference of null pointer"); |
Ted Kremenek | bc3a021 | 2009-10-30 17:24:47 +0000 | [diff] [blame] | 94 | |
Ted Kremenek | b4b817d | 2009-11-11 03:26:34 +0000 | [diff] [blame^] | 95 | EnhancedBugReport *report = |
| 96 | new EnhancedBugReport(*BT_null, BT_null->getDescription().c_str(), N); |
| 97 | report->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, |
| 98 | bugreporter::GetDerefExpr(N)); |
| 99 | |
| 100 | C.EmitReport(report); |
| 101 | return; |
| 102 | } |
Ted Kremenek | 2c791bd | 2009-11-06 00:44:32 +0000 | [diff] [blame] | 103 | |
Ted Kremenek | b4b817d | 2009-11-11 03:26:34 +0000 | [diff] [blame^] | 104 | // Otherwise, we have the case where the location could either be |
| 105 | // null or not-null. Record the error node as an "implicit" null |
| 106 | // dereference. |
| 107 | ImplicitNullDerefNodes.push_back(N); |
Ted Kremenek | bc3a021 | 2009-10-30 17:24:47 +0000 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | |
Ted Kremenek | b4b817d | 2009-11-11 03:26:34 +0000 | [diff] [blame^] | 111 | // From this point forward, we know that the location is not null. |
| 112 | assert(notNullState); |
| 113 | C.addTransition(state != nullState ? C.GenerateNode(S, notNullState) : |
| 114 | C.getPredecessor()); |
Ted Kremenek | bc3a021 | 2009-10-30 17:24:47 +0000 | [diff] [blame] | 115 | } |