1 //== NullDerefChecker.cpp - Null dereference checker ------------*- C++ -*--==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This defines NullDerefChecker, a builtin check in ExprEngine that performs
11 // checks for null pointers at loads and stores.
13 //===----------------------------------------------------------------------===//
15 #include "ClangSACheckers.h"
16 #include "clang/AST/ExprObjC.h"
17 #include "clang/StaticAnalyzer/Core/Checker.h"
18 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
19 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
20 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
21 #include "llvm/ADT/SmallString.h"
23 using namespace clang;
27 class DereferenceChecker
28 : public Checker< check::Location,
30 EventDispatcher<ImplicitNullDerefEvent> > {
31 mutable OwningPtr<BuiltinBug> BT_null;
32 mutable OwningPtr<BuiltinBug> BT_undef;
34 void reportBug(ProgramStateRef State, const Stmt *S, CheckerContext &C,
35 bool IsBind = false) const;
38 void checkLocation(SVal location, bool isLoad, const Stmt* S,
39 CheckerContext &C) const;
40 void checkBind(SVal L, SVal V, const Stmt *S, CheckerContext &C) const;
42 static void AddDerefSource(raw_ostream &os,
43 SmallVectorImpl<SourceRange> &Ranges,
44 const Expr *Ex, const ProgramState *state,
45 const LocationContext *LCtx,
46 bool loadedFrom = false);
48 } // end anonymous namespace
51 DereferenceChecker::AddDerefSource(raw_ostream &os,
52 SmallVectorImpl<SourceRange> &Ranges,
54 const ProgramState *state,
55 const LocationContext *LCtx,
57 Ex = Ex->IgnoreParenLValueCasts();
58 switch (Ex->getStmtClass()) {
61 case Stmt::DeclRefExprClass: {
62 const DeclRefExpr *DR = cast<DeclRefExpr>(Ex);
63 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
64 os << " (" << (loadedFrom ? "loaded from" : "from")
65 << " variable '" << VD->getName() << "')";
66 Ranges.push_back(DR->getSourceRange());
70 case Stmt::MemberExprClass: {
71 const MemberExpr *ME = cast<MemberExpr>(Ex);
72 os << " (" << (loadedFrom ? "loaded from" : "via")
73 << " field '" << ME->getMemberNameInfo() << "')";
74 SourceLocation L = ME->getMemberLoc();
75 Ranges.push_back(SourceRange(L, L));
81 void DereferenceChecker::reportBug(ProgramStateRef State, const Stmt *S,
82 CheckerContext &C, bool IsBind) const {
83 // Generate an error node.
84 ExplodedNode *N = C.generateSink(State);
88 // We know that 'location' cannot be non-null. This is what
89 // we call an "explicit" null dereference.
91 BT_null.reset(new BuiltinBug("Dereference of null pointer"));
94 llvm::raw_svector_ostream os(buf);
96 SmallVector<SourceRange, 2> Ranges;
98 // Walk through lvalue casts to get the original expression
99 // that syntactically caused the load.
100 if (const Expr *expr = dyn_cast<Expr>(S))
101 S = expr->IgnoreParenLValueCasts();
104 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(S)) {
105 if (BO->isAssignmentOp())
107 } else if (const DeclStmt *DS = dyn_cast<DeclStmt>(S)) {
108 assert(DS->isSingleDecl() && "We process decls one by one");
109 if (const VarDecl *VD = dyn_cast<VarDecl>(DS->getSingleDecl()))
110 if (const Expr *Init = VD->getAnyInitializer())
115 switch (S->getStmtClass()) {
116 case Stmt::ArraySubscriptExprClass: {
117 os << "Array access";
118 const ArraySubscriptExpr *AE = cast<ArraySubscriptExpr>(S);
119 AddDerefSource(os, Ranges, AE->getBase()->IgnoreParenCasts(),
120 State.getPtr(), N->getLocationContext());
121 os << " results in a null pointer dereference";
124 case Stmt::UnaryOperatorClass: {
125 os << "Dereference of null pointer";
126 const UnaryOperator *U = cast<UnaryOperator>(S);
127 AddDerefSource(os, Ranges, U->getSubExpr()->IgnoreParens(),
128 State.getPtr(), N->getLocationContext(), true);
131 case Stmt::MemberExprClass: {
132 const MemberExpr *M = cast<MemberExpr>(S);
133 if (M->isArrow() || bugreporter::isDeclRefExprToReference(M->getBase())) {
134 os << "Access to field '" << M->getMemberNameInfo()
135 << "' results in a dereference of a null pointer";
136 AddDerefSource(os, Ranges, M->getBase()->IgnoreParenCasts(),
137 State.getPtr(), N->getLocationContext(), true);
141 case Stmt::ObjCIvarRefExprClass: {
142 const ObjCIvarRefExpr *IV = cast<ObjCIvarRefExpr>(S);
143 os << "Access to instance variable '" << *IV->getDecl()
144 << "' results in a dereference of a null pointer";
145 AddDerefSource(os, Ranges, IV->getBase()->IgnoreParenCasts(),
146 State.getPtr(), N->getLocationContext(), true);
155 new BugReport(*BT_null,
156 buf.empty() ? BT_null->getDescription() : buf.str(),
159 bugreporter::trackNullOrUndefValue(N, bugreporter::GetDerefExpr(N), *report);
161 for (SmallVectorImpl<SourceRange>::iterator
162 I = Ranges.begin(), E = Ranges.end(); I!=E; ++I)
163 report->addRange(*I);
165 C.emitReport(report);
168 void DereferenceChecker::checkLocation(SVal l, bool isLoad, const Stmt* S,
169 CheckerContext &C) const {
170 // Check for dereference of an undefined value.
172 if (ExplodedNode *N = C.generateSink()) {
174 BT_undef.reset(new BuiltinBug("Dereference of undefined pointer value"));
177 new BugReport(*BT_undef, BT_undef->getDescription(), N);
178 bugreporter::trackNullOrUndefValue(N, bugreporter::GetDerefExpr(N),
180 C.emitReport(report);
185 DefinedOrUnknownSVal location = cast<DefinedOrUnknownSVal>(l);
187 // Check for null dereferences.
188 if (!isa<Loc>(location))
191 ProgramStateRef state = C.getState();
193 ProgramStateRef notNullState, nullState;
194 llvm::tie(notNullState, nullState) = state->assume(location);
196 // The explicit NULL case.
199 reportBug(nullState, S, C);
203 // Otherwise, we have the case where the location could either be
204 // null or not-null. Record the error node as an "implicit" null
206 if (ExplodedNode *N = C.generateSink(nullState)) {
207 ImplicitNullDerefEvent event = { l, isLoad, N, &C.getBugReporter() };
208 dispatchEvent(event);
212 // From this point forward, we know that the location is not null.
213 C.addTransition(notNullState);
216 void DereferenceChecker::checkBind(SVal L, SVal V, const Stmt *S,
217 CheckerContext &C) const {
218 // If we're binding to a reference, check if the value is known to be null.
222 const MemRegion *MR = L.getAsRegion();
223 const TypedValueRegion *TVR = dyn_cast_or_null<TypedValueRegion>(MR);
227 if (!TVR->getValueType()->isReferenceType())
230 ProgramStateRef State = C.getState();
232 ProgramStateRef StNonNull, StNull;
233 llvm::tie(StNonNull, StNull) = State->assume(cast<DefinedOrUnknownSVal>(V));
237 reportBug(StNull, S, C, /*isBind=*/true);
241 // At this point the value could be either null or non-null.
242 // Record this as an "implicit" null dereference.
243 if (ExplodedNode *N = C.generateSink(StNull)) {
244 ImplicitNullDerefEvent event = { V, /*isLoad=*/true, N,
245 &C.getBugReporter() };
246 dispatchEvent(event);
250 // Unlike a regular null dereference, initializing a reference with a
251 // dereferenced null pointer does not actually cause a runtime exception in
252 // Clang's implementation of references.
254 // int &r = *p; // safe??
255 // if (p != NULL) return; // uh-oh
256 // r = 5; // trap here
258 // The standard says this is invalid as soon as we try to create a "null
259 // reference" (there is no such thing), but turning this into an assumption
260 // that 'p' is never null will not match our actual runtime behavior.
261 // So we do not record this assumption, allowing us to warn on the last line
264 // We do need to add a transition because we may have generated a sink for
265 // the "implicit" null dereference.
266 C.addTransition(State, this);
269 void ento::registerDereferenceChecker(CheckerManager &mgr) {
270 mgr.registerChecker<DereferenceChecker>();