blob: 98243874d7d97e6dc804cc7633b461ee2c6884e0 [file] [log] [blame]
Ted Kremenekbc3a0212009-10-30 17:24:47 +00001//== 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 Kremenekdc998c12009-11-03 18:41:06 +000015#include "clang/Analysis/PathSensitive/Checkers/DereferenceChecker.h"
Ted Kremenekb4b817d2009-11-11 03:26:34 +000016#include "clang/Analysis/PathSensitive/Checker.h"
Ted Kremenekbc3a0212009-10-30 17:24:47 +000017#include "clang/Analysis/PathSensitive/GRExprEngine.h"
18#include "clang/Analysis/PathSensitive/BugReporter.h"
Ted Kremenekb4b817d2009-11-11 03:26:34 +000019#include "GRExprEngineInternalChecks.h"
Ted Kremenekbc3a0212009-10-30 17:24:47 +000020
21using namespace clang;
22
Ted Kremenekb4b817d2009-11-11 03:26:34 +000023namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000024class DereferenceChecker : public Checker {
Ted Kremenekb4b817d2009-11-11 03:26:34 +000025 BuiltinBug *BT_null;
26 BuiltinBug *BT_undef;
27 llvm::SmallVector<ExplodedNode*, 2> ImplicitNullDerefNodes;
28public:
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
42void clang::RegisterDereferenceChecker(GRExprEngine &Eng) {
43 Eng.registerCheck(new DereferenceChecker());
Ted Kremenekbc3a0212009-10-30 17:24:47 +000044}
45
Ted Kremenekb4b817d2009-11-11 03:26:34 +000046std::pair<ExplodedNode * const *, ExplodedNode * const *>
47clang::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
55void DereferenceChecker::VisitLocation(CheckerContext &C, const Stmt *S,
56 SVal l) {
57 // Check for dereference of an undefined value.
58 if (l.isUndef()) {
Ted Kremenek19d67b52009-11-23 22:22:01 +000059 if (ExplodedNode *N = C.GenerateSink()) {
Ted Kremenekb4b817d2009-11-11 03:26:34 +000060 if (!BT_undef)
61 BT_undef = new BuiltinBug("Dereference of undefined pointer value");
Ted Kremenekbc3a0212009-10-30 17:24:47 +000062
Ted Kremenekb4b817d2009-11-11 03:26:34 +000063 EnhancedBugReport *report =
Benjamin Kramerd02e2322009-11-14 12:08:24 +000064 new EnhancedBugReport(*BT_undef, BT_undef->getDescription(), N);
Ted Kremenekb4b817d2009-11-11 03:26:34 +000065 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 Kremenek78d722f2009-11-21 01:50:48 +000084 if (!notNullState) {
85 // Generate an error node.
Ted Kremenek19d67b52009-11-23 22:22:01 +000086 ExplodedNode *N = C.GenerateSink(nullState);
Ted Kremenek78d722f2009-11-21 01:50:48 +000087 if (!N)
Ted Kremenekb4b817d2009-11-11 03:26:34 +000088 return;
Ted Kremenek78d722f2009-11-21 01:50:48 +000089
90 // We know that 'location' cannot be non-null. This is what
91 // we call an "explicit" null dereference.
92 if (!BT_null)
Ted Kremeneke576af22009-11-24 01:33:10 +000093 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 Kremenek2c791bd2009-11-06 00:44:32 +0000112
Ted Kremenek78d722f2009-11-21 01:50:48 +0000113 EnhancedBugReport *report =
Ted Kremeneke576af22009-11-24 01:33:10 +0000114 new EnhancedBugReport(*BT_null,
115 buf.empty() ? BT_null->getDescription():buf.str(),
116 N);
117
Ted Kremenek78d722f2009-11-21 01:50:48 +0000118 report->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue,
119 bugreporter::GetDerefExpr(N));
120
121 C.EmitReport(report);
122 return;
123 }
124 else {
Ted Kremenekb4b817d2009-11-11 03:26:34 +0000125 // 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 Kremenek78d722f2009-11-21 01:50:48 +0000127 // dereference.
Ted Kremenek19d67b52009-11-23 22:22:01 +0000128 if (ExplodedNode *N = C.GenerateSink(nullState))
Ted Kremenek78d722f2009-11-21 01:50:48 +0000129 ImplicitNullDerefNodes.push_back(N);
Ted Kremenekbc3a0212009-10-30 17:24:47 +0000130 }
131 }
132
Ted Kremenekb4b817d2009-11-11 03:26:34 +0000133 // From this point forward, we know that the location is not null.
Ted Kremenek19d67b52009-11-23 22:22:01 +0000134 C.addTransition(notNullState);
Ted Kremenekbc3a0212009-10-30 17:24:47 +0000135}