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