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