Anna Zaks | ef89339 | 2013-03-09 03:23:14 +0000 | [diff] [blame] | 1 | //===--- NonNullParamChecker.cpp - Undefined arguments checker -*- C++ -*--===// |
Zhongxing Xu | 9b9d731 | 2009-11-03 07:35:33 +0000 | [diff] [blame] | 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 | // |
Anna Zaks | ef89339 | 2013-03-09 03:23:14 +0000 | [diff] [blame] | 10 | // This defines NonNullParamChecker, which checks for arguments expected not to |
| 11 | // be null due to: |
| 12 | // - the corresponding parameters being declared to have nonnull attribute |
| 13 | // - the corresponding parameters being references; since the call would form |
| 14 | // a reference to a null pointer |
Zhongxing Xu | 9b9d731 | 2009-11-03 07:35:33 +0000 | [diff] [blame] | 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
Argyrios Kyrtzidis | 6fff2e3 | 2011-02-28 01:28:01 +0000 | [diff] [blame] | 18 | #include "ClangSACheckers.h" |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 19 | #include "clang/AST/Attr.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 20 | #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" |
Argyrios Kyrtzidis | 6a5674f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 21 | #include "clang/StaticAnalyzer/Core/Checker.h" |
Argyrios Kyrtzidis | 6fff2e3 | 2011-02-28 01:28:01 +0000 | [diff] [blame] | 22 | #include "clang/StaticAnalyzer/Core/CheckerManager.h" |
Jordan Rose | 4f7df9b | 2012-07-26 21:39:41 +0000 | [diff] [blame] | 23 | #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h" |
Argyrios Kyrtzidis | 6fff2e3 | 2011-02-28 01:28:01 +0000 | [diff] [blame] | 24 | #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" |
Zhongxing Xu | 9b9d731 | 2009-11-03 07:35:33 +0000 | [diff] [blame] | 25 | |
| 26 | using namespace clang; |
Ted Kremenek | 98857c9 | 2010-12-23 07:20:52 +0000 | [diff] [blame] | 27 | using namespace ento; |
Zhongxing Xu | 9b9d731 | 2009-11-03 07:35:33 +0000 | [diff] [blame] | 28 | |
Ted Kremenek | 4325315 | 2009-11-11 05:50:44 +0000 | [diff] [blame] | 29 | namespace { |
Anna Zaks | ef89339 | 2013-03-09 03:23:14 +0000 | [diff] [blame] | 30 | class NonNullParamChecker |
Jordan Rose | 682b316 | 2012-07-02 19:28:21 +0000 | [diff] [blame] | 31 | : public Checker< check::PreCall > { |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 32 | mutable std::unique_ptr<BugType> BTAttrNonNull; |
| 33 | mutable std::unique_ptr<BugType> BTNullRefArg; |
| 34 | |
Ted Kremenek | 4325315 | 2009-11-11 05:50:44 +0000 | [diff] [blame] | 35 | public: |
Argyrios Kyrtzidis | 6fff2e3 | 2011-02-28 01:28:01 +0000 | [diff] [blame] | 36 | |
Jordan Rose | 682b316 | 2012-07-02 19:28:21 +0000 | [diff] [blame] | 37 | void checkPreCall(const CallEvent &Call, CheckerContext &C) const; |
Anna Zaks | 9e0da9e0 | 2013-03-07 03:02:36 +0000 | [diff] [blame] | 38 | |
Aaron Ballman | 8d3a7a5 | 2015-06-23 13:15:32 +0000 | [diff] [blame^] | 39 | std::unique_ptr<BugReport> |
| 40 | genReportNullAttrNonNull(const ExplodedNode *ErrorN, const Expr *ArgE) const; |
| 41 | std::unique_ptr<BugReport> |
| 42 | genReportReferenceToNullPointer(const ExplodedNode *ErrorN, |
| 43 | const Expr *ArgE) const; |
Ted Kremenek | 4325315 | 2009-11-11 05:50:44 +0000 | [diff] [blame] | 44 | }; |
| 45 | } // end anonymous namespace |
| 46 | |
Anna Zaks | ef89339 | 2013-03-09 03:23:14 +0000 | [diff] [blame] | 47 | void NonNullParamChecker::checkPreCall(const CallEvent &Call, |
Ted Kremenek | f0ae7d0 | 2014-01-17 07:15:35 +0000 | [diff] [blame] | 48 | CheckerContext &C) const { |
Jordan Rose | 682b316 | 2012-07-02 19:28:21 +0000 | [diff] [blame] | 49 | const Decl *FD = Call.getDecl(); |
Zhongxing Xu | 9b9d731 | 2009-11-03 07:35:33 +0000 | [diff] [blame] | 50 | if (!FD) |
| 51 | return; |
| 52 | |
Jordan Rose | 679659f | 2014-10-13 19:38:02 +0000 | [diff] [blame] | 53 | // Merge all non-null attributes |
| 54 | unsigned NumArgs = Call.getNumArgs(); |
| 55 | llvm::SmallBitVector AttrNonNull(NumArgs); |
| 56 | for (const auto *NonNull : FD->specific_attrs<NonNullAttr>()) { |
| 57 | if (!NonNull->args_size()) { |
| 58 | AttrNonNull.set(0, NumArgs); |
| 59 | break; |
| 60 | } |
| 61 | for (unsigned Val : NonNull->args()) { |
| 62 | if (Val >= NumArgs) |
| 63 | continue; |
| 64 | AttrNonNull.set(Val); |
| 65 | } |
| 66 | } |
Zhongxing Xu | 9b9d731 | 2009-11-03 07:35:33 +0000 | [diff] [blame] | 67 | |
Jordan Rose | 682b316 | 2012-07-02 19:28:21 +0000 | [diff] [blame] | 68 | ProgramStateRef state = C.getState(); |
| 69 | |
Anna Zaks | 9e0da9e0 | 2013-03-07 03:02:36 +0000 | [diff] [blame] | 70 | CallEvent::param_type_iterator TyI = Call.param_type_begin(), |
| 71 | TyE = Call.param_type_end(); |
| 72 | |
Jordan Rose | 679659f | 2014-10-13 19:38:02 +0000 | [diff] [blame] | 73 | for (unsigned idx = 0; idx < NumArgs; ++idx) { |
Anna Zaks | 9e0da9e0 | 2013-03-07 03:02:36 +0000 | [diff] [blame] | 74 | |
| 75 | // Check if the parameter is a reference. We want to report when reference |
| 76 | // to a null pointer is passed as a paramter. |
| 77 | bool haveRefTypeParam = false; |
| 78 | if (TyI != TyE) { |
| 79 | haveRefTypeParam = (*TyI)->isReferenceType(); |
| 80 | TyI++; |
| 81 | } |
| 82 | |
Jordan Rose | 679659f | 2014-10-13 19:38:02 +0000 | [diff] [blame] | 83 | bool haveAttrNonNull = AttrNonNull[idx]; |
Ted Kremenek | f0ae7d0 | 2014-01-17 07:15:35 +0000 | [diff] [blame] | 84 | if (!haveAttrNonNull) { |
| 85 | // Check if the parameter is also marked 'nonnull'. |
| 86 | ArrayRef<ParmVarDecl*> parms = Call.parameters(); |
| 87 | if (idx < parms.size()) |
| 88 | haveAttrNonNull = parms[idx]->hasAttr<NonNullAttr>(); |
| 89 | } |
Anna Zaks | 9e0da9e0 | 2013-03-07 03:02:36 +0000 | [diff] [blame] | 90 | |
| 91 | if (!haveRefTypeParam && !haveAttrNonNull) |
Zhongxing Xu | 9b9d731 | 2009-11-03 07:35:33 +0000 | [diff] [blame] | 92 | continue; |
| 93 | |
Anna Zaks | 9e0da9e0 | 2013-03-07 03:02:36 +0000 | [diff] [blame] | 94 | // If the value is unknown or undefined, we can't perform this check. |
| 95 | const Expr *ArgE = Call.getArgExpr(idx); |
Jordan Rose | 682b316 | 2012-07-02 19:28:21 +0000 | [diff] [blame] | 96 | SVal V = Call.getArgSVal(idx); |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 97 | Optional<DefinedSVal> DV = V.getAs<DefinedSVal>(); |
Jordy Rose | 3d85888 | 2010-06-21 20:08:28 +0000 | [diff] [blame] | 98 | if (!DV) |
| 99 | continue; |
Zhongxing Xu | 9b9d731 | 2009-11-03 07:35:33 +0000 | [diff] [blame] | 100 | |
Anna Zaks | 9e0da9e0 | 2013-03-07 03:02:36 +0000 | [diff] [blame] | 101 | // Process the case when the argument is not a location. |
| 102 | assert(!haveRefTypeParam || DV->getAs<Loc>()); |
| 103 | |
| 104 | if (haveAttrNonNull && !DV->getAs<Loc>()) { |
Ted Kremenek | dcf85a8 | 2010-11-09 02:11:43 +0000 | [diff] [blame] | 105 | // If the argument is a union type, we want to handle a potential |
Jordan Rose | 682b316 | 2012-07-02 19:28:21 +0000 | [diff] [blame] | 106 | // transparent_union GCC extension. |
Jordan Rose | 682b316 | 2012-07-02 19:28:21 +0000 | [diff] [blame] | 107 | if (!ArgE) |
| 108 | continue; |
| 109 | |
| 110 | QualType T = ArgE->getType(); |
Ted Kremenek | dcf85a8 | 2010-11-09 02:11:43 +0000 | [diff] [blame] | 111 | const RecordType *UT = T->getAsUnionType(); |
| 112 | if (!UT || !UT->getDecl()->hasAttr<TransparentUnionAttr>()) |
| 113 | continue; |
Jordan Rose | 682b316 | 2012-07-02 19:28:21 +0000 | [diff] [blame] | 114 | |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 115 | if (Optional<nonloc::CompoundVal> CSV = |
David Blaikie | 2fdacbc | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 116 | DV->getAs<nonloc::CompoundVal>()) { |
Ted Kremenek | dcf85a8 | 2010-11-09 02:11:43 +0000 | [diff] [blame] | 117 | nonloc::CompoundVal::iterator CSV_I = CSV->begin(); |
| 118 | assert(CSV_I != CSV->end()); |
| 119 | V = *CSV_I; |
David Blaikie | 2fdacbc | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 120 | DV = V.getAs<DefinedSVal>(); |
Ted Kremenek | dcf85a8 | 2010-11-09 02:11:43 +0000 | [diff] [blame] | 121 | assert(++CSV_I == CSV->end()); |
Jordan Rose | e359d01 | 2014-02-26 01:20:19 +0000 | [diff] [blame] | 122 | // FIXME: Handle (some_union){ some_other_union_val }, which turns into |
| 123 | // a LazyCompoundVal inside a CompoundVal. |
| 124 | if (!V.getAs<Loc>()) |
Anna Zaks | 23c85ed | 2013-03-06 20:26:02 +0000 | [diff] [blame] | 125 | continue; |
| 126 | // Retrieve the corresponding expression. |
| 127 | if (const CompoundLiteralExpr *CE = dyn_cast<CompoundLiteralExpr>(ArgE)) |
| 128 | if (const InitListExpr *IE = |
| 129 | dyn_cast<InitListExpr>(CE->getInitializer())) |
| 130 | ArgE = dyn_cast<Expr>(*(IE->begin())); |
| 131 | |
Jordan Rose | 682b316 | 2012-07-02 19:28:21 +0000 | [diff] [blame] | 132 | } else { |
Ted Kremenek | dcf85a8 | 2010-11-09 02:11:43 +0000 | [diff] [blame] | 133 | // FIXME: Handle LazyCompoundVals? |
| 134 | continue; |
| 135 | } |
| 136 | } |
| 137 | |
Zhongxing Xu | 9b9d731 | 2009-11-03 07:35:33 +0000 | [diff] [blame] | 138 | ConstraintManager &CM = C.getConstraintManager(); |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 139 | ProgramStateRef stateNotNull, stateNull; |
Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 140 | std::tie(stateNotNull, stateNull) = CM.assumeDual(state, *DV); |
Zhongxing Xu | 9b9d731 | 2009-11-03 07:35:33 +0000 | [diff] [blame] | 141 | |
| 142 | if (stateNull && !stateNotNull) { |
| 143 | // Generate an error node. Check for a null node in case |
| 144 | // we cache out. |
Ted Kremenek | 750b7ac | 2010-12-20 21:19:09 +0000 | [diff] [blame] | 145 | if (ExplodedNode *errorNode = C.generateSink(stateNull)) { |
Zhongxing Xu | 9b9d731 | 2009-11-03 07:35:33 +0000 | [diff] [blame] | 146 | |
Aaron Ballman | 8d3a7a5 | 2015-06-23 13:15:32 +0000 | [diff] [blame^] | 147 | std::unique_ptr<BugReport> R; |
Anna Zaks | 9e0da9e0 | 2013-03-07 03:02:36 +0000 | [diff] [blame] | 148 | if (haveAttrNonNull) |
| 149 | R = genReportNullAttrNonNull(errorNode, ArgE); |
| 150 | else if (haveRefTypeParam) |
| 151 | R = genReportReferenceToNullPointer(errorNode, ArgE); |
Zhongxing Xu | 9b9d731 | 2009-11-03 07:35:33 +0000 | [diff] [blame] | 152 | |
| 153 | // Highlight the range of the argument that was null. |
Jordan Rose | 682b316 | 2012-07-02 19:28:21 +0000 | [diff] [blame] | 154 | R->addRange(Call.getArgSourceRange(idx)); |
Anna Zaks | 9e0da9e0 | 2013-03-07 03:02:36 +0000 | [diff] [blame] | 155 | |
Zhongxing Xu | 9b9d731 | 2009-11-03 07:35:33 +0000 | [diff] [blame] | 156 | // Emit the bug report. |
Aaron Ballman | 8d3a7a5 | 2015-06-23 13:15:32 +0000 | [diff] [blame^] | 157 | C.emitReport(std::move(R)); |
Zhongxing Xu | 9b9d731 | 2009-11-03 07:35:33 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | // Always return. Either we cached out or we just emitted an error. |
| 161 | return; |
| 162 | } |
| 163 | |
| 164 | // If a pointer value passed the check we should assume that it is |
| 165 | // indeed not null from this point forward. |
| 166 | assert(stateNotNull); |
| 167 | state = stateNotNull; |
| 168 | } |
| 169 | |
| 170 | // If we reach here all of the arguments passed the nonnull check. |
| 171 | // If 'state' has been updated generated a new node. |
Anna Zaks | da4c8d6 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 172 | C.addTransition(state); |
Zhongxing Xu | 9b9d731 | 2009-11-03 07:35:33 +0000 | [diff] [blame] | 173 | } |
Argyrios Kyrtzidis | 6fff2e3 | 2011-02-28 01:28:01 +0000 | [diff] [blame] | 174 | |
Aaron Ballman | 8d3a7a5 | 2015-06-23 13:15:32 +0000 | [diff] [blame^] | 175 | std::unique_ptr<BugReport> |
| 176 | NonNullParamChecker::genReportNullAttrNonNull(const ExplodedNode *ErrorNode, |
| 177 | const Expr *ArgE) const { |
Anna Zaks | 9e0da9e0 | 2013-03-07 03:02:36 +0000 | [diff] [blame] | 178 | // Lazily allocate the BugType object if it hasn't already been |
| 179 | // created. Ownership is transferred to the BugReporter object once |
| 180 | // the BugReport is passed to 'EmitWarning'. |
| 181 | if (!BTAttrNonNull) |
| 182 | BTAttrNonNull.reset(new BugType( |
Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 183 | this, "Argument with 'nonnull' attribute passed null", "API")); |
Anna Zaks | 9e0da9e0 | 2013-03-07 03:02:36 +0000 | [diff] [blame] | 184 | |
Aaron Ballman | 8d3a7a5 | 2015-06-23 13:15:32 +0000 | [diff] [blame^] | 185 | auto R = llvm::make_unique<BugReport>( |
| 186 | *BTAttrNonNull, |
| 187 | "Null pointer passed as an argument to a 'nonnull' parameter", ErrorNode); |
Anna Zaks | 9e0da9e0 | 2013-03-07 03:02:36 +0000 | [diff] [blame] | 188 | if (ArgE) |
| 189 | bugreporter::trackNullOrUndefValue(ErrorNode, ArgE, *R); |
| 190 | |
| 191 | return R; |
| 192 | } |
| 193 | |
Aaron Ballman | 8d3a7a5 | 2015-06-23 13:15:32 +0000 | [diff] [blame^] | 194 | std::unique_ptr<BugReport> NonNullParamChecker::genReportReferenceToNullPointer( |
| 195 | const ExplodedNode *ErrorNode, const Expr *ArgE) const { |
Anna Zaks | 9e0da9e0 | 2013-03-07 03:02:36 +0000 | [diff] [blame] | 196 | if (!BTNullRefArg) |
Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 197 | BTNullRefArg.reset(new BuiltinBug(this, "Dereference of null pointer")); |
Anna Zaks | 9e0da9e0 | 2013-03-07 03:02:36 +0000 | [diff] [blame] | 198 | |
Aaron Ballman | 8d3a7a5 | 2015-06-23 13:15:32 +0000 | [diff] [blame^] | 199 | auto R = llvm::make_unique<BugReport>( |
| 200 | *BTNullRefArg, "Forming reference to null pointer", ErrorNode); |
Anna Zaks | 9e0da9e0 | 2013-03-07 03:02:36 +0000 | [diff] [blame] | 201 | if (ArgE) { |
| 202 | const Expr *ArgEDeref = bugreporter::getDerefExpr(ArgE); |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame] | 203 | if (!ArgEDeref) |
Anna Zaks | 9e0da9e0 | 2013-03-07 03:02:36 +0000 | [diff] [blame] | 204 | ArgEDeref = ArgE; |
| 205 | bugreporter::trackNullOrUndefValue(ErrorNode, |
| 206 | ArgEDeref, |
| 207 | *R); |
| 208 | } |
| 209 | return R; |
| 210 | |
| 211 | } |
| 212 | |
Anna Zaks | ef89339 | 2013-03-09 03:23:14 +0000 | [diff] [blame] | 213 | void ento::registerNonNullParamChecker(CheckerManager &mgr) { |
| 214 | mgr.registerChecker<NonNullParamChecker>(); |
Argyrios Kyrtzidis | 6fff2e3 | 2011-02-28 01:28:01 +0000 | [diff] [blame] | 215 | } |