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 | // |
Argyrios Kyrtzidis | d2592a3 | 2010-12-22 18:53:44 +0000 | [diff] [blame] | 10 | // This defines NullDerefChecker, a builtin check in ExprEngine that performs |
Ted Kremenek | bc3a021 | 2009-10-30 17:24:47 +0000 | [diff] [blame] | 11 | // checks for null pointers at loads and stores. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Argyrios Kyrtzidis | b3d74da | 2011-02-28 17:36:18 +0000 | [diff] [blame] | 15 | #include "ClangSACheckers.h" |
Benjamin Kramer | c35fb7d | 2012-01-28 12:06:22 +0000 | [diff] [blame] | 16 | #include "clang/AST/ExprObjC.h" |
Argyrios Kyrtzidis | ec8605f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 17 | #include "clang/StaticAnalyzer/Core/Checker.h" |
Argyrios Kyrtzidis | b3d74da | 2011-02-28 17:36:18 +0000 | [diff] [blame] | 18 | #include "clang/StaticAnalyzer/Core/CheckerManager.h" |
| 19 | #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" |
Ted Kremenek | 9b66371 | 2011-02-10 01:03:03 +0000 | [diff] [blame] | 20 | #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" |
Benjamin Kramer | 8fe83e1 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SmallString.h" |
Ted Kremenek | bc3a021 | 2009-10-30 17:24:47 +0000 | [diff] [blame] | 22 | |
| 23 | using namespace clang; |
Ted Kremenek | 9ef6537 | 2010-12-23 07:20:52 +0000 | [diff] [blame] | 24 | using namespace ento; |
Ted Kremenek | bc3a021 | 2009-10-30 17:24:47 +0000 | [diff] [blame] | 25 | |
Ted Kremenek | b4b817d | 2009-11-11 03:26:34 +0000 | [diff] [blame] | 26 | namespace { |
Argyrios Kyrtzidis | b3d74da | 2011-02-28 17:36:18 +0000 | [diff] [blame] | 27 | class DereferenceChecker |
Argyrios Kyrtzidis | ec8605f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 28 | : public Checker< check::Location, |
Jordan Rose | 9f3b9d5 | 2012-08-02 21:33:42 +0000 | [diff] [blame] | 29 | check::Bind, |
| 30 | EventDispatcher<ImplicitNullDerefEvent> > { |
Dylan Noblesmith | 6f42b62 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 31 | mutable OwningPtr<BuiltinBug> BT_null; |
| 32 | mutable OwningPtr<BuiltinBug> BT_undef; |
Ted Kremenek | 452b84d | 2010-03-23 01:11:38 +0000 | [diff] [blame] | 33 | |
Jordan Rose | 9f3b9d5 | 2012-08-02 21:33:42 +0000 | [diff] [blame] | 34 | void reportBug(ProgramStateRef State, const Stmt *S, CheckerContext &C, |
| 35 | bool IsBind = false) const; |
| 36 | |
Argyrios Kyrtzidis | b3d74da | 2011-02-28 17:36:18 +0000 | [diff] [blame] | 37 | public: |
Anna Zaks | 390909c | 2011-10-06 00:43:15 +0000 | [diff] [blame] | 38 | void checkLocation(SVal location, bool isLoad, const Stmt* S, |
| 39 | CheckerContext &C) const; |
Jordan Rose | 9f3b9d5 | 2012-08-02 21:33:42 +0000 | [diff] [blame] | 40 | void checkBind(SVal L, SVal V, const Stmt *S, CheckerContext &C) const; |
Argyrios Kyrtzidis | b3d74da | 2011-02-28 17:36:18 +0000 | [diff] [blame] | 41 | |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 42 | static const MemRegion *AddDerefSource(raw_ostream &os, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 43 | SmallVectorImpl<SourceRange> &Ranges, |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 44 | const Expr *Ex, const ProgramState *state, |
| 45 | const LocationContext *LCtx, |
| 46 | bool loadedFrom = false); |
Ted Kremenek | b4b817d | 2009-11-11 03:26:34 +0000 | [diff] [blame] | 47 | }; |
| 48 | } // end anonymous namespace |
| 49 | |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 50 | const MemRegion * |
| 51 | DereferenceChecker::AddDerefSource(raw_ostream &os, |
| 52 | SmallVectorImpl<SourceRange> &Ranges, |
| 53 | const Expr *Ex, |
| 54 | const ProgramState *state, |
| 55 | const LocationContext *LCtx, |
| 56 | bool loadedFrom) { |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 57 | Ex = Ex->IgnoreParenLValueCasts(); |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 58 | const MemRegion *sourceR = 0; |
Ted Kremenek | 646c3c3 | 2010-10-26 00:06:13 +0000 | [diff] [blame] | 59 | switch (Ex->getStmtClass()) { |
| 60 | default: |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 61 | break; |
Ted Kremenek | 646c3c3 | 2010-10-26 00:06:13 +0000 | [diff] [blame] | 62 | case Stmt::DeclRefExprClass: { |
| 63 | const DeclRefExpr *DR = cast<DeclRefExpr>(Ex); |
| 64 | if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) { |
| 65 | os << " (" << (loadedFrom ? "loaded from" : "from") |
| 66 | << " variable '" << VD->getName() << "')"; |
| 67 | Ranges.push_back(DR->getSourceRange()); |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 68 | sourceR = state->getLValue(VD, LCtx).getAsRegion(); |
Ted Kremenek | 646c3c3 | 2010-10-26 00:06:13 +0000 | [diff] [blame] | 69 | } |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 70 | break; |
Ted Kremenek | 646c3c3 | 2010-10-26 00:06:13 +0000 | [diff] [blame] | 71 | } |
| 72 | case Stmt::MemberExprClass: { |
| 73 | const MemberExpr *ME = cast<MemberExpr>(Ex); |
| 74 | os << " (" << (loadedFrom ? "loaded from" : "via") |
| 75 | << " field '" << ME->getMemberNameInfo() << "')"; |
| 76 | SourceLocation L = ME->getMemberLoc(); |
| 77 | Ranges.push_back(SourceRange(L, L)); |
| 78 | break; |
| 79 | } |
| 80 | } |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 81 | return sourceR; |
Ted Kremenek | 646c3c3 | 2010-10-26 00:06:13 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Jordan Rose | 9f3b9d5 | 2012-08-02 21:33:42 +0000 | [diff] [blame] | 84 | void DereferenceChecker::reportBug(ProgramStateRef State, const Stmt *S, |
| 85 | CheckerContext &C, bool IsBind) const { |
| 86 | // Generate an error node. |
| 87 | ExplodedNode *N = C.generateSink(State); |
| 88 | if (!N) |
| 89 | return; |
| 90 | |
| 91 | // We know that 'location' cannot be non-null. This is what |
| 92 | // we call an "explicit" null dereference. |
| 93 | if (!BT_null) |
| 94 | BT_null.reset(new BuiltinBug("Dereference of null pointer")); |
| 95 | |
| 96 | SmallString<100> buf; |
| 97 | SmallVector<SourceRange, 2> Ranges; |
| 98 | |
| 99 | // Walk through lvalue casts to get the original expression |
| 100 | // that syntactically caused the load. |
| 101 | if (const Expr *expr = dyn_cast<Expr>(S)) |
| 102 | S = expr->IgnoreParenLValueCasts(); |
| 103 | |
| 104 | const MemRegion *sourceR = 0; |
| 105 | |
| 106 | if (IsBind) { |
| 107 | if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(S)) { |
| 108 | if (BO->isAssignmentOp()) |
| 109 | S = BO->getRHS(); |
| 110 | } else if (const DeclStmt *DS = dyn_cast<DeclStmt>(S)) { |
| 111 | assert(DS->isSingleDecl() && "We process decls one by one"); |
| 112 | if (const VarDecl *VD = dyn_cast<VarDecl>(DS->getSingleDecl())) |
| 113 | if (const Expr *Init = VD->getAnyInitializer()) |
| 114 | S = Init; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | switch (S->getStmtClass()) { |
| 119 | case Stmt::ArraySubscriptExprClass: { |
| 120 | llvm::raw_svector_ostream os(buf); |
| 121 | os << "Array access"; |
| 122 | const ArraySubscriptExpr *AE = cast<ArraySubscriptExpr>(S); |
| 123 | sourceR = AddDerefSource(os, Ranges, AE->getBase()->IgnoreParenCasts(), |
| 124 | State.getPtr(), N->getLocationContext()); |
| 125 | os << " results in a null pointer dereference"; |
| 126 | break; |
| 127 | } |
| 128 | case Stmt::UnaryOperatorClass: { |
| 129 | llvm::raw_svector_ostream os(buf); |
| 130 | os << "Dereference of null pointer"; |
| 131 | const UnaryOperator *U = cast<UnaryOperator>(S); |
| 132 | sourceR = AddDerefSource(os, Ranges, U->getSubExpr()->IgnoreParens(), |
| 133 | State.getPtr(), N->getLocationContext(), true); |
| 134 | break; |
| 135 | } |
| 136 | case Stmt::MemberExprClass: { |
| 137 | const MemberExpr *M = cast<MemberExpr>(S); |
| 138 | if (M->isArrow()) { |
| 139 | llvm::raw_svector_ostream os(buf); |
| 140 | os << "Access to field '" << M->getMemberNameInfo() |
| 141 | << "' results in a dereference of a null pointer"; |
| 142 | sourceR = AddDerefSource(os, Ranges, M->getBase()->IgnoreParenCasts(), |
| 143 | State.getPtr(), N->getLocationContext(), true); |
| 144 | } |
| 145 | break; |
| 146 | } |
| 147 | case Stmt::ObjCIvarRefExprClass: { |
| 148 | const ObjCIvarRefExpr *IV = cast<ObjCIvarRefExpr>(S); |
| 149 | if (const DeclRefExpr *DR = |
| 150 | dyn_cast<DeclRefExpr>(IV->getBase()->IgnoreParenCasts())) { |
| 151 | if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) { |
| 152 | llvm::raw_svector_ostream os(buf); |
| 153 | os << "Instance variable access (via '" << VD->getName() |
| 154 | << "') results in a null pointer dereference"; |
| 155 | } |
| 156 | } |
| 157 | Ranges.push_back(IV->getSourceRange()); |
| 158 | break; |
| 159 | } |
| 160 | default: |
| 161 | break; |
| 162 | } |
| 163 | |
| 164 | BugReport *report = |
| 165 | new BugReport(*BT_null, |
| 166 | buf.empty() ? BT_null->getDescription() : buf.str(), |
| 167 | N); |
| 168 | |
Jordan Rose | 6853799 | 2012-08-03 23:09:01 +0000 | [diff] [blame] | 169 | bugreporter::addTrackNullOrUndefValueVisitor(N, bugreporter::GetDerefExpr(N), |
| 170 | report); |
Jordan Rose | 9f3b9d5 | 2012-08-02 21:33:42 +0000 | [diff] [blame] | 171 | |
| 172 | for (SmallVectorImpl<SourceRange>::iterator |
| 173 | I = Ranges.begin(), E = Ranges.end(); I!=E; ++I) |
| 174 | report->addRange(*I); |
| 175 | |
| 176 | if (sourceR) { |
| 177 | report->markInteresting(sourceR); |
| 178 | report->markInteresting(State->getRawSVal(loc::MemRegionVal(sourceR))); |
| 179 | } |
| 180 | |
| 181 | C.EmitReport(report); |
| 182 | } |
| 183 | |
Anna Zaks | 390909c | 2011-10-06 00:43:15 +0000 | [diff] [blame] | 184 | void DereferenceChecker::checkLocation(SVal l, bool isLoad, const Stmt* S, |
Argyrios Kyrtzidis | b3d74da | 2011-02-28 17:36:18 +0000 | [diff] [blame] | 185 | CheckerContext &C) const { |
Ted Kremenek | b4b817d | 2009-11-11 03:26:34 +0000 | [diff] [blame] | 186 | // Check for dereference of an undefined value. |
| 187 | if (l.isUndef()) { |
Ted Kremenek | d048c6e | 2010-12-20 21:19:09 +0000 | [diff] [blame] | 188 | if (ExplodedNode *N = C.generateSink()) { |
Ted Kremenek | b4b817d | 2009-11-11 03:26:34 +0000 | [diff] [blame] | 189 | if (!BT_undef) |
Argyrios Kyrtzidis | b3d74da | 2011-02-28 17:36:18 +0000 | [diff] [blame] | 190 | BT_undef.reset(new BuiltinBug("Dereference of undefined pointer value")); |
Ted Kremenek | 452b84d | 2010-03-23 01:11:38 +0000 | [diff] [blame] | 191 | |
Anna Zaks | e172e8b | 2011-08-17 23:00:25 +0000 | [diff] [blame] | 192 | BugReport *report = |
| 193 | new BugReport(*BT_undef, BT_undef->getDescription(), N); |
Jordan Rose | 6853799 | 2012-08-03 23:09:01 +0000 | [diff] [blame] | 194 | bugreporter::addTrackNullOrUndefValueVisitor(N, |
| 195 | bugreporter::GetDerefExpr(N), |
| 196 | report); |
Ted Kremenek | ed7948b | 2012-05-31 06:03:17 +0000 | [diff] [blame] | 197 | report->disablePathPruning(); |
Ted Kremenek | b4b817d | 2009-11-11 03:26:34 +0000 | [diff] [blame] | 198 | C.EmitReport(report); |
| 199 | } |
| 200 | return; |
| 201 | } |
Ted Kremenek | 452b84d | 2010-03-23 01:11:38 +0000 | [diff] [blame] | 202 | |
Ted Kremenek | b4b817d | 2009-11-11 03:26:34 +0000 | [diff] [blame] | 203 | DefinedOrUnknownSVal location = cast<DefinedOrUnknownSVal>(l); |
Ted Kremenek | 452b84d | 2010-03-23 01:11:38 +0000 | [diff] [blame] | 204 | |
| 205 | // Check for null dereferences. |
Ted Kremenek | b4b817d | 2009-11-11 03:26:34 +0000 | [diff] [blame] | 206 | if (!isa<Loc>(location)) |
| 207 | return; |
Ted Kremenek | 452b84d | 2010-03-23 01:11:38 +0000 | [diff] [blame] | 208 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 209 | ProgramStateRef state = C.getState(); |
Jordan Rose | 9f3b9d5 | 2012-08-02 21:33:42 +0000 | [diff] [blame] | 210 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 211 | ProgramStateRef notNullState, nullState; |
Ted Kremenek | 28f47b9 | 2010-12-01 22:16:56 +0000 | [diff] [blame] | 212 | llvm::tie(notNullState, nullState) = state->assume(location); |
Ted Kremenek | 452b84d | 2010-03-23 01:11:38 +0000 | [diff] [blame] | 213 | |
Ted Kremenek | b4b817d | 2009-11-11 03:26:34 +0000 | [diff] [blame] | 214 | // The explicit NULL case. |
| 215 | if (nullState) { |
Ted Kremenek | 452b84d | 2010-03-23 01:11:38 +0000 | [diff] [blame] | 216 | if (!notNullState) { |
Jordan Rose | 9f3b9d5 | 2012-08-02 21:33:42 +0000 | [diff] [blame] | 217 | reportBug(nullState, S, C); |
Ted Kremenek | 78d722f | 2009-11-21 01:50:48 +0000 | [diff] [blame] | 218 | return; |
| 219 | } |
Jordan Rose | 9f3b9d5 | 2012-08-02 21:33:42 +0000 | [diff] [blame] | 220 | |
| 221 | // Otherwise, we have the case where the location could either be |
| 222 | // null or not-null. Record the error node as an "implicit" null |
| 223 | // dereference. |
| 224 | if (ExplodedNode *N = C.generateSink(nullState)) { |
| 225 | ImplicitNullDerefEvent event = { l, isLoad, N, &C.getBugReporter() }; |
| 226 | dispatchEvent(event); |
Ted Kremenek | bc3a021 | 2009-10-30 17:24:47 +0000 | [diff] [blame] | 227 | } |
| 228 | } |
Ted Kremenek | 452b84d | 2010-03-23 01:11:38 +0000 | [diff] [blame] | 229 | |
Ted Kremenek | b4b817d | 2009-11-11 03:26:34 +0000 | [diff] [blame] | 230 | // From this point forward, we know that the location is not null. |
Anna Zaks | 0bd6b11 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 231 | C.addTransition(notNullState); |
Ted Kremenek | bc3a021 | 2009-10-30 17:24:47 +0000 | [diff] [blame] | 232 | } |
Argyrios Kyrtzidis | b3d74da | 2011-02-28 17:36:18 +0000 | [diff] [blame] | 233 | |
Jordan Rose | 9f3b9d5 | 2012-08-02 21:33:42 +0000 | [diff] [blame] | 234 | void DereferenceChecker::checkBind(SVal L, SVal V, const Stmt *S, |
| 235 | CheckerContext &C) const { |
Jordan Rose | 522f46f | 2012-08-04 00:25:30 +0000 | [diff] [blame] | 236 | // If we're binding to a reference, check if the value is known to be null. |
Jordan Rose | 9f3b9d5 | 2012-08-02 21:33:42 +0000 | [diff] [blame] | 237 | if (V.isUndef()) |
| 238 | return; |
| 239 | |
| 240 | const MemRegion *MR = L.getAsRegion(); |
| 241 | const TypedValueRegion *TVR = dyn_cast_or_null<TypedValueRegion>(MR); |
| 242 | if (!TVR) |
| 243 | return; |
| 244 | |
| 245 | if (!TVR->getValueType()->isReferenceType()) |
| 246 | return; |
| 247 | |
| 248 | ProgramStateRef State = C.getState(); |
| 249 | |
| 250 | ProgramStateRef StNonNull, StNull; |
| 251 | llvm::tie(StNonNull, StNull) = State->assume(cast<DefinedOrUnknownSVal>(V)); |
| 252 | |
| 253 | if (StNull) { |
| 254 | if (!StNonNull) { |
| 255 | reportBug(StNull, S, C, /*isBind=*/true); |
| 256 | return; |
| 257 | } |
| 258 | |
| 259 | // At this point the value could be either null or non-null. |
| 260 | // Record this as an "implicit" null dereference. |
| 261 | if (ExplodedNode *N = C.generateSink(StNull)) { |
| 262 | ImplicitNullDerefEvent event = { V, /*isLoad=*/true, N, |
| 263 | &C.getBugReporter() }; |
| 264 | dispatchEvent(event); |
| 265 | } |
| 266 | } |
| 267 | |
Jordan Rose | 522f46f | 2012-08-04 00:25:30 +0000 | [diff] [blame] | 268 | // Unlike a regular null dereference, initializing a reference with a |
| 269 | // dereferenced null pointer does not actually cause a runtime exception in |
| 270 | // Clang's implementation of references. |
| 271 | // |
| 272 | // int &r = *p; // safe?? |
| 273 | // if (p != NULL) return; // uh-oh |
| 274 | // r = 5; // trap here |
| 275 | // |
| 276 | // The standard says this is invalid as soon as we try to create a "null |
| 277 | // reference" (there is no such thing), but turning this into an assumption |
| 278 | // that 'p' is never null will not match our actual runtime behavior. |
| 279 | // So we do not record this assumption, allowing us to warn on the last line |
| 280 | // of this example. |
| 281 | // |
| 282 | // We do need to add a transition because we may have generated a sink for |
| 283 | // the "implicit" null dereference. |
| 284 | C.addTransition(State, this); |
Jordan Rose | 9f3b9d5 | 2012-08-02 21:33:42 +0000 | [diff] [blame] | 285 | } |
| 286 | |
Argyrios Kyrtzidis | b3d74da | 2011-02-28 17:36:18 +0000 | [diff] [blame] | 287 | void ento::registerDereferenceChecker(CheckerManager &mgr) { |
| 288 | mgr.registerChecker<DereferenceChecker>(); |
| 289 | } |