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