Ted Kremenek | f613e89 | 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 | 1696f50 | 2010-12-22 18:53:44 +0000 | [diff] [blame] | 10 | // This defines NullDerefChecker, a builtin check in ExprEngine that performs |
Ted Kremenek | f613e89 | 2009-10-30 17:24:47 +0000 | [diff] [blame] | 11 | // checks for null pointers at loads and stores. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Argyrios Kyrtzidis | 2c49ec7 | 2011-02-28 17:36:18 +0000 | [diff] [blame] | 15 | #include "ClangSACheckers.h" |
Benjamin Kramer | 11764ab | 2012-01-28 12:06:22 +0000 | [diff] [blame] | 16 | #include "clang/AST/ExprObjC.h" |
Alexey Bataev | 1a3320e | 2015-08-25 14:24:04 +0000 | [diff] [blame] | 17 | #include "clang/AST/ExprOpenMP.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 18 | #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" |
Argyrios Kyrtzidis | 6a5674f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 19 | #include "clang/StaticAnalyzer/Core/Checker.h" |
Argyrios Kyrtzidis | 2c49ec7 | 2011-02-28 17:36:18 +0000 | [diff] [blame] | 20 | #include "clang/StaticAnalyzer/Core/CheckerManager.h" |
| 21 | #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" |
Yury Gribov | 22b4164 | 2015-11-06 11:16:31 +0000 | [diff] [blame] | 22 | #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerHelpers.h" |
Benjamin Kramer | 4903802 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/SmallString.h" |
Benjamin Kramer | 444a130 | 2012-12-01 17:12:56 +0000 | [diff] [blame] | 24 | #include "llvm/Support/raw_ostream.h" |
Ted Kremenek | f613e89 | 2009-10-30 17:24:47 +0000 | [diff] [blame] | 25 | |
| 26 | using namespace clang; |
Ted Kremenek | 98857c9 | 2010-12-23 07:20:52 +0000 | [diff] [blame] | 27 | using namespace ento; |
Ted Kremenek | f613e89 | 2009-10-30 17:24:47 +0000 | [diff] [blame] | 28 | |
Ted Kremenek | 5e1f78a | 2009-11-11 03:26:34 +0000 | [diff] [blame] | 29 | namespace { |
Argyrios Kyrtzidis | 2c49ec7 | 2011-02-28 17:36:18 +0000 | [diff] [blame] | 30 | class DereferenceChecker |
Argyrios Kyrtzidis | 6a5674f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 31 | : public Checker< check::Location, |
Jordan Rose | 9a2eec3 | 2012-08-02 21:33:42 +0000 | [diff] [blame] | 32 | check::Bind, |
| 33 | EventDispatcher<ImplicitNullDerefEvent> > { |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 34 | mutable std::unique_ptr<BuiltinBug> BT_null; |
| 35 | mutable std::unique_ptr<BuiltinBug> BT_undef; |
Ted Kremenek | bb6f5af | 2010-03-23 01:11:38 +0000 | [diff] [blame] | 36 | |
Anna Zaks | c9f16fe4 | 2016-01-06 00:32:49 +0000 | [diff] [blame] | 37 | void reportBug(ProgramStateRef State, const Stmt *S, CheckerContext &C) const; |
Jordan Rose | 9a2eec3 | 2012-08-02 21:33:42 +0000 | [diff] [blame] | 38 | |
Argyrios Kyrtzidis | 2c49ec7 | 2011-02-28 17:36:18 +0000 | [diff] [blame] | 39 | public: |
Anna Zaks | 3e0f415 | 2011-10-06 00:43:15 +0000 | [diff] [blame] | 40 | void checkLocation(SVal location, bool isLoad, const Stmt* S, |
| 41 | CheckerContext &C) const; |
Jordan Rose | 9a2eec3 | 2012-08-02 21:33:42 +0000 | [diff] [blame] | 42 | void checkBind(SVal L, SVal V, const Stmt *S, CheckerContext &C) const; |
Argyrios Kyrtzidis | 2c49ec7 | 2011-02-28 17:36:18 +0000 | [diff] [blame] | 43 | |
Anna Zaks | 1e2a0dc | 2012-09-05 22:31:49 +0000 | [diff] [blame] | 44 | static void AddDerefSource(raw_ostream &os, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 45 | SmallVectorImpl<SourceRange> &Ranges, |
Ted Kremenek | 1e809b4 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 46 | const Expr *Ex, const ProgramState *state, |
| 47 | const LocationContext *LCtx, |
| 48 | bool loadedFrom = false); |
Ted Kremenek | 5e1f78a | 2009-11-11 03:26:34 +0000 | [diff] [blame] | 49 | }; |
| 50 | } // end anonymous namespace |
| 51 | |
Anna Zaks | 1e2a0dc | 2012-09-05 22:31:49 +0000 | [diff] [blame] | 52 | void |
Ted Kremenek | 1e809b4 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 53 | DereferenceChecker::AddDerefSource(raw_ostream &os, |
| 54 | SmallVectorImpl<SourceRange> &Ranges, |
| 55 | const Expr *Ex, |
| 56 | const ProgramState *state, |
| 57 | const LocationContext *LCtx, |
| 58 | bool loadedFrom) { |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 59 | Ex = Ex->IgnoreParenLValueCasts(); |
Ted Kremenek | 731310e | 2010-10-26 00:06:13 +0000 | [diff] [blame] | 60 | switch (Ex->getStmtClass()) { |
| 61 | default: |
Ted Kremenek | 1e809b4 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 62 | break; |
Ted Kremenek | 731310e | 2010-10-26 00:06:13 +0000 | [diff] [blame] | 63 | case Stmt::DeclRefExprClass: { |
| 64 | const DeclRefExpr *DR = cast<DeclRefExpr>(Ex); |
| 65 | if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) { |
| 66 | os << " (" << (loadedFrom ? "loaded from" : "from") |
| 67 | << " variable '" << VD->getName() << "')"; |
| 68 | Ranges.push_back(DR->getSourceRange()); |
| 69 | } |
Ted Kremenek | 1e809b4 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 70 | break; |
Ted Kremenek | 731310e | 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 | } |
Ted Kremenek | 9625048 | 2013-02-24 07:21:01 +0000 | [diff] [blame] | 80 | case Stmt::ObjCIvarRefExprClass: { |
| 81 | const ObjCIvarRefExpr *IV = cast<ObjCIvarRefExpr>(Ex); |
| 82 | os << " (" << (loadedFrom ? "loaded from" : "via") |
| 83 | << " ivar '" << IV->getDecl()->getName() << "')"; |
| 84 | SourceLocation L = IV->getLocation(); |
| 85 | Ranges.push_back(SourceRange(L, L)); |
| 86 | break; |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 87 | } |
Ted Kremenek | 731310e | 2010-10-26 00:06:13 +0000 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | |
Anna Zaks | c9f16fe4 | 2016-01-06 00:32:49 +0000 | [diff] [blame] | 91 | static const Expr *getDereferenceExpr(const Stmt *S, bool IsBind=false){ |
| 92 | const Expr *E = nullptr; |
| 93 | |
| 94 | // Walk through lvalue casts to get the original expression |
| 95 | // that syntactically caused the load. |
| 96 | if (const Expr *expr = dyn_cast<Expr>(S)) |
| 97 | E = expr->IgnoreParenLValueCasts(); |
| 98 | |
| 99 | if (IsBind) { |
| 100 | const VarDecl *VD; |
| 101 | const Expr *Init; |
| 102 | std::tie(VD, Init) = parseAssignment(S); |
| 103 | if (VD && Init) |
| 104 | E = Init; |
| 105 | } |
| 106 | return E; |
| 107 | } |
| 108 | |
| 109 | static bool suppressReport(const Expr *E) { |
| 110 | // Do not report dereferences on memory in non-default address spaces. |
| 111 | return E->getType().getQualifiers().hasAddressSpace(); |
| 112 | } |
| 113 | |
Jordan Rose | 9a2eec3 | 2012-08-02 21:33:42 +0000 | [diff] [blame] | 114 | void DereferenceChecker::reportBug(ProgramStateRef State, const Stmt *S, |
Anna Zaks | c9f16fe4 | 2016-01-06 00:32:49 +0000 | [diff] [blame] | 115 | CheckerContext &C) const { |
Jordan Rose | 9a2eec3 | 2012-08-02 21:33:42 +0000 | [diff] [blame] | 116 | // Generate an error node. |
Devin Coughlin | e39bd40 | 2015-09-16 22:03:05 +0000 | [diff] [blame] | 117 | ExplodedNode *N = C.generateErrorNode(State); |
Jordan Rose | 9a2eec3 | 2012-08-02 21:33:42 +0000 | [diff] [blame] | 118 | if (!N) |
| 119 | return; |
| 120 | |
| 121 | // We know that 'location' cannot be non-null. This is what |
| 122 | // we call an "explicit" null dereference. |
| 123 | if (!BT_null) |
Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 124 | BT_null.reset(new BuiltinBug(this, "Dereference of null pointer")); |
Jordan Rose | 9a2eec3 | 2012-08-02 21:33:42 +0000 | [diff] [blame] | 125 | |
| 126 | SmallString<100> buf; |
Jordan Rose | 1d64a49 | 2012-09-22 01:24:38 +0000 | [diff] [blame] | 127 | llvm::raw_svector_ostream os(buf); |
| 128 | |
Jordan Rose | 9a2eec3 | 2012-08-02 21:33:42 +0000 | [diff] [blame] | 129 | SmallVector<SourceRange, 2> Ranges; |
| 130 | |
Jordan Rose | 9a2eec3 | 2012-08-02 21:33:42 +0000 | [diff] [blame] | 131 | switch (S->getStmtClass()) { |
| 132 | case Stmt::ArraySubscriptExprClass: { |
Jordan Rose | 9a2eec3 | 2012-08-02 21:33:42 +0000 | [diff] [blame] | 133 | os << "Array access"; |
| 134 | const ArraySubscriptExpr *AE = cast<ArraySubscriptExpr>(S); |
Anna Zaks | 1e2a0dc | 2012-09-05 22:31:49 +0000 | [diff] [blame] | 135 | AddDerefSource(os, Ranges, AE->getBase()->IgnoreParenCasts(), |
Alp Toker | f994cef | 2014-07-05 03:08:06 +0000 | [diff] [blame] | 136 | State.get(), N->getLocationContext()); |
Jordan Rose | 9a2eec3 | 2012-08-02 21:33:42 +0000 | [diff] [blame] | 137 | os << " results in a null pointer dereference"; |
| 138 | break; |
| 139 | } |
Alexey Bataev | 1a3320e | 2015-08-25 14:24:04 +0000 | [diff] [blame] | 140 | case Stmt::OMPArraySectionExprClass: { |
| 141 | os << "Array access"; |
| 142 | const OMPArraySectionExpr *AE = cast<OMPArraySectionExpr>(S); |
| 143 | AddDerefSource(os, Ranges, AE->getBase()->IgnoreParenCasts(), |
| 144 | State.get(), N->getLocationContext()); |
| 145 | os << " results in a null pointer dereference"; |
| 146 | break; |
| 147 | } |
Jordan Rose | 9a2eec3 | 2012-08-02 21:33:42 +0000 | [diff] [blame] | 148 | case Stmt::UnaryOperatorClass: { |
Jordan Rose | 9a2eec3 | 2012-08-02 21:33:42 +0000 | [diff] [blame] | 149 | os << "Dereference of null pointer"; |
| 150 | const UnaryOperator *U = cast<UnaryOperator>(S); |
Anna Zaks | 1e2a0dc | 2012-09-05 22:31:49 +0000 | [diff] [blame] | 151 | AddDerefSource(os, Ranges, U->getSubExpr()->IgnoreParens(), |
Alp Toker | f994cef | 2014-07-05 03:08:06 +0000 | [diff] [blame] | 152 | State.get(), N->getLocationContext(), true); |
Jordan Rose | 9a2eec3 | 2012-08-02 21:33:42 +0000 | [diff] [blame] | 153 | break; |
| 154 | } |
| 155 | case Stmt::MemberExprClass: { |
| 156 | const MemberExpr *M = cast<MemberExpr>(S); |
Anna Zaks | 3245e58 | 2012-09-05 23:41:54 +0000 | [diff] [blame] | 157 | if (M->isArrow() || bugreporter::isDeclRefExprToReference(M->getBase())) { |
Jordan Rose | 9a2eec3 | 2012-08-02 21:33:42 +0000 | [diff] [blame] | 158 | os << "Access to field '" << M->getMemberNameInfo() |
| 159 | << "' results in a dereference of a null pointer"; |
Anna Zaks | 1e2a0dc | 2012-09-05 22:31:49 +0000 | [diff] [blame] | 160 | AddDerefSource(os, Ranges, M->getBase()->IgnoreParenCasts(), |
Alp Toker | f994cef | 2014-07-05 03:08:06 +0000 | [diff] [blame] | 161 | State.get(), N->getLocationContext(), true); |
Jordan Rose | 9a2eec3 | 2012-08-02 21:33:42 +0000 | [diff] [blame] | 162 | } |
| 163 | break; |
| 164 | } |
| 165 | case Stmt::ObjCIvarRefExprClass: { |
| 166 | const ObjCIvarRefExpr *IV = cast<ObjCIvarRefExpr>(S); |
Jordan Rose | 1d64a49 | 2012-09-22 01:24:38 +0000 | [diff] [blame] | 167 | os << "Access to instance variable '" << *IV->getDecl() |
| 168 | << "' results in a dereference of a null pointer"; |
| 169 | AddDerefSource(os, Ranges, IV->getBase()->IgnoreParenCasts(), |
Alp Toker | f994cef | 2014-07-05 03:08:06 +0000 | [diff] [blame] | 170 | State.get(), N->getLocationContext(), true); |
Jordan Rose | 9a2eec3 | 2012-08-02 21:33:42 +0000 | [diff] [blame] | 171 | break; |
| 172 | } |
| 173 | default: |
| 174 | break; |
| 175 | } |
| 176 | |
Aaron Ballman | 8d3a7a5 | 2015-06-23 13:15:32 +0000 | [diff] [blame] | 177 | auto report = llvm::make_unique<BugReport>( |
| 178 | *BT_null, buf.empty() ? BT_null->getDescription() : StringRef(buf), N); |
Jordan Rose | 9a2eec3 | 2012-08-02 21:33:42 +0000 | [diff] [blame] | 179 | |
Jordan Rose | c362eda | 2013-01-26 01:28:19 +0000 | [diff] [blame] | 180 | bugreporter::trackNullOrUndefValue(N, bugreporter::getDerefExpr(S), *report); |
Jordan Rose | 9a2eec3 | 2012-08-02 21:33:42 +0000 | [diff] [blame] | 181 | |
| 182 | for (SmallVectorImpl<SourceRange>::iterator |
| 183 | I = Ranges.begin(), E = Ranges.end(); I!=E; ++I) |
| 184 | report->addRange(*I); |
| 185 | |
Aaron Ballman | 8d3a7a5 | 2015-06-23 13:15:32 +0000 | [diff] [blame] | 186 | C.emitReport(std::move(report)); |
Jordan Rose | 9a2eec3 | 2012-08-02 21:33:42 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Anna Zaks | 3e0f415 | 2011-10-06 00:43:15 +0000 | [diff] [blame] | 189 | void DereferenceChecker::checkLocation(SVal l, bool isLoad, const Stmt* S, |
Argyrios Kyrtzidis | 2c49ec7 | 2011-02-28 17:36:18 +0000 | [diff] [blame] | 190 | CheckerContext &C) const { |
Ted Kremenek | 5e1f78a | 2009-11-11 03:26:34 +0000 | [diff] [blame] | 191 | // Check for dereference of an undefined value. |
| 192 | if (l.isUndef()) { |
Devin Coughlin | e39bd40 | 2015-09-16 22:03:05 +0000 | [diff] [blame] | 193 | if (ExplodedNode *N = C.generateErrorNode()) { |
Ted Kremenek | 5e1f78a | 2009-11-11 03:26:34 +0000 | [diff] [blame] | 194 | if (!BT_undef) |
Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 195 | BT_undef.reset( |
| 196 | new BuiltinBug(this, "Dereference of undefined pointer value")); |
Ted Kremenek | bb6f5af | 2010-03-23 01:11:38 +0000 | [diff] [blame] | 197 | |
Aaron Ballman | 8d3a7a5 | 2015-06-23 13:15:32 +0000 | [diff] [blame] | 198 | auto report = |
| 199 | llvm::make_unique<BugReport>(*BT_undef, BT_undef->getDescription(), N); |
Jordan Rose | c362eda | 2013-01-26 01:28:19 +0000 | [diff] [blame] | 200 | bugreporter::trackNullOrUndefValue(N, bugreporter::getDerefExpr(S), |
Jordan Rose | a0f7d35 | 2012-08-28 00:50:51 +0000 | [diff] [blame] | 201 | *report); |
Aaron Ballman | 8d3a7a5 | 2015-06-23 13:15:32 +0000 | [diff] [blame] | 202 | C.emitReport(std::move(report)); |
Ted Kremenek | 5e1f78a | 2009-11-11 03:26:34 +0000 | [diff] [blame] | 203 | } |
| 204 | return; |
| 205 | } |
Ted Kremenek | bb6f5af | 2010-03-23 01:11:38 +0000 | [diff] [blame] | 206 | |
David Blaikie | 2fdacbc | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 207 | DefinedOrUnknownSVal location = l.castAs<DefinedOrUnknownSVal>(); |
Ted Kremenek | bb6f5af | 2010-03-23 01:11:38 +0000 | [diff] [blame] | 208 | |
| 209 | // Check for null dereferences. |
David Blaikie | 2fdacbc | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 210 | if (!location.getAs<Loc>()) |
Ted Kremenek | 5e1f78a | 2009-11-11 03:26:34 +0000 | [diff] [blame] | 211 | return; |
Ted Kremenek | bb6f5af | 2010-03-23 01:11:38 +0000 | [diff] [blame] | 212 | |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 213 | ProgramStateRef state = C.getState(); |
Jordan Rose | 9a2eec3 | 2012-08-02 21:33:42 +0000 | [diff] [blame] | 214 | |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 215 | ProgramStateRef notNullState, nullState; |
Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 216 | std::tie(notNullState, nullState) = state->assume(location); |
Ted Kremenek | bb6f5af | 2010-03-23 01:11:38 +0000 | [diff] [blame] | 217 | |
Ted Kremenek | 5e1f78a | 2009-11-11 03:26:34 +0000 | [diff] [blame] | 218 | // The explicit NULL case. |
| 219 | if (nullState) { |
Ted Kremenek | bb6f5af | 2010-03-23 01:11:38 +0000 | [diff] [blame] | 220 | if (!notNullState) { |
Anna Zaks | c9f16fe4 | 2016-01-06 00:32:49 +0000 | [diff] [blame] | 221 | const Expr *expr = getDereferenceExpr(S); |
| 222 | if (!suppressReport(expr)) { |
| 223 | reportBug(nullState, expr, C); |
| 224 | return; |
| 225 | } |
Ted Kremenek | 9d6daf2 | 2009-11-21 01:50:48 +0000 | [diff] [blame] | 226 | } |
Jordan Rose | 9a2eec3 | 2012-08-02 21:33:42 +0000 | [diff] [blame] | 227 | |
| 228 | // Otherwise, we have the case where the location could either be |
| 229 | // null or not-null. Record the error node as an "implicit" null |
| 230 | // dereference. |
Devin Coughlin | e39bd40 | 2015-09-16 22:03:05 +0000 | [diff] [blame] | 231 | if (ExplodedNode *N = C.generateSink(nullState, C.getPredecessor())) { |
Gabor Horvath | 8d3ad6b | 2015-08-27 18:49:07 +0000 | [diff] [blame] | 232 | ImplicitNullDerefEvent event = {l, isLoad, N, &C.getBugReporter(), |
Anna Zaks | ad9e7ea | 2016-01-29 18:43:15 +0000 | [diff] [blame] | 233 | /*IsDirectDereference=*/true}; |
Jordan Rose | 9a2eec3 | 2012-08-02 21:33:42 +0000 | [diff] [blame] | 234 | dispatchEvent(event); |
Ted Kremenek | f613e89 | 2009-10-30 17:24:47 +0000 | [diff] [blame] | 235 | } |
| 236 | } |
Ted Kremenek | bb6f5af | 2010-03-23 01:11:38 +0000 | [diff] [blame] | 237 | |
Ted Kremenek | 5e1f78a | 2009-11-11 03:26:34 +0000 | [diff] [blame] | 238 | // From this point forward, we know that the location is not null. |
Anna Zaks | da4c8d6 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 239 | C.addTransition(notNullState); |
Ted Kremenek | f613e89 | 2009-10-30 17:24:47 +0000 | [diff] [blame] | 240 | } |
Argyrios Kyrtzidis | 2c49ec7 | 2011-02-28 17:36:18 +0000 | [diff] [blame] | 241 | |
Jordan Rose | 9a2eec3 | 2012-08-02 21:33:42 +0000 | [diff] [blame] | 242 | void DereferenceChecker::checkBind(SVal L, SVal V, const Stmt *S, |
| 243 | CheckerContext &C) const { |
Jordan Rose | 4aa80e1 | 2012-08-04 00:25:30 +0000 | [diff] [blame] | 244 | // If we're binding to a reference, check if the value is known to be null. |
Jordan Rose | 9a2eec3 | 2012-08-02 21:33:42 +0000 | [diff] [blame] | 245 | if (V.isUndef()) |
| 246 | return; |
| 247 | |
| 248 | const MemRegion *MR = L.getAsRegion(); |
| 249 | const TypedValueRegion *TVR = dyn_cast_or_null<TypedValueRegion>(MR); |
| 250 | if (!TVR) |
| 251 | return; |
| 252 | |
| 253 | if (!TVR->getValueType()->isReferenceType()) |
| 254 | return; |
| 255 | |
| 256 | ProgramStateRef State = C.getState(); |
| 257 | |
| 258 | ProgramStateRef StNonNull, StNull; |
Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 259 | std::tie(StNonNull, StNull) = State->assume(V.castAs<DefinedOrUnknownSVal>()); |
Jordan Rose | 9a2eec3 | 2012-08-02 21:33:42 +0000 | [diff] [blame] | 260 | |
| 261 | if (StNull) { |
| 262 | if (!StNonNull) { |
Anna Zaks | c9f16fe4 | 2016-01-06 00:32:49 +0000 | [diff] [blame] | 263 | const Expr *expr = getDereferenceExpr(S, /*IsBind=*/true); |
| 264 | if (!suppressReport(expr)) { |
| 265 | reportBug(StNull, expr, C); |
| 266 | return; |
| 267 | } |
Jordan Rose | 9a2eec3 | 2012-08-02 21:33:42 +0000 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | // At this point the value could be either null or non-null. |
| 271 | // Record this as an "implicit" null dereference. |
Devin Coughlin | e39bd40 | 2015-09-16 22:03:05 +0000 | [diff] [blame] | 272 | if (ExplodedNode *N = C.generateSink(StNull, C.getPredecessor())) { |
Gabor Horvath | 8d3ad6b | 2015-08-27 18:49:07 +0000 | [diff] [blame] | 273 | ImplicitNullDerefEvent event = {V, /*isLoad=*/true, N, |
| 274 | &C.getBugReporter(), |
Anna Zaks | ad9e7ea | 2016-01-29 18:43:15 +0000 | [diff] [blame] | 275 | /*IsDirectDereference=*/true}; |
Jordan Rose | 9a2eec3 | 2012-08-02 21:33:42 +0000 | [diff] [blame] | 276 | dispatchEvent(event); |
| 277 | } |
| 278 | } |
| 279 | |
Jordan Rose | 4aa80e1 | 2012-08-04 00:25:30 +0000 | [diff] [blame] | 280 | // Unlike a regular null dereference, initializing a reference with a |
| 281 | // dereferenced null pointer does not actually cause a runtime exception in |
| 282 | // Clang's implementation of references. |
| 283 | // |
| 284 | // int &r = *p; // safe?? |
| 285 | // if (p != NULL) return; // uh-oh |
| 286 | // r = 5; // trap here |
| 287 | // |
| 288 | // The standard says this is invalid as soon as we try to create a "null |
| 289 | // reference" (there is no such thing), but turning this into an assumption |
| 290 | // that 'p' is never null will not match our actual runtime behavior. |
| 291 | // So we do not record this assumption, allowing us to warn on the last line |
| 292 | // of this example. |
| 293 | // |
| 294 | // We do need to add a transition because we may have generated a sink for |
| 295 | // the "implicit" null dereference. |
| 296 | C.addTransition(State, this); |
Jordan Rose | 9a2eec3 | 2012-08-02 21:33:42 +0000 | [diff] [blame] | 297 | } |
| 298 | |
Argyrios Kyrtzidis | 2c49ec7 | 2011-02-28 17:36:18 +0000 | [diff] [blame] | 299 | void ento::registerDereferenceChecker(CheckerManager &mgr) { |
| 300 | mgr.registerChecker<DereferenceChecker>(); |
| 301 | } |