blob: 0cbc4086701a81a0e29278f1a062bc12d5d12868 [file] [log] [blame]
Shih-wei Liaof8fd82b2010-02-10 11:10:31 -08001//== 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
15#include "clang/Checker/Checkers/DereferenceChecker.h"
16#include "clang/Checker/PathSensitive/Checker.h"
17#include "clang/Checker/PathSensitive/GRExprEngine.h"
18#include "clang/Checker/BugReporter/BugReporter.h"
19#include "GRExprEngineInternalChecks.h"
20
21using namespace clang;
22
23namespace {
24class DereferenceChecker : public Checker {
25 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());
44}
45
46std::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()) {
59 if (ExplodedNode *N = C.GenerateSink()) {
60 if (!BT_undef)
61 BT_undef = new BuiltinBug("Dereference of undefined pointer value");
62
63 EnhancedBugReport *report =
64 new EnhancedBugReport(*BT_undef, BT_undef->getDescription(), N);
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) {
84 if (!notNullState) {
85 // Generate an error node.
86 ExplodedNode *N = C.GenerateSink(nullState);
87 if (!N)
88 return;
89
90 // We know that 'location' cannot be non-null. This is what
91 // we call an "explicit" null dereference.
92 if (!BT_null)
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 }
112
113 EnhancedBugReport *report =
114 new EnhancedBugReport(*BT_null,
115 buf.empty() ? BT_null->getDescription():buf.str(),
116 N);
117
118 report->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue,
119 bugreporter::GetDerefExpr(N));
120
121 C.EmitReport(report);
122 return;
123 }
124 else {
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
127 // dereference.
128 if (ExplodedNode *N = C.GenerateSink(nullState))
129 ImplicitNullDerefNodes.push_back(N);
130 }
131 }
132
133 // From this point forward, we know that the location is not null.
134 C.addTransition(notNullState);
135}