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" |
Argyrios Kyrtzidis | ec8605f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 17 | #include "clang/StaticAnalyzer/Core/Checker.h" |
Argyrios Kyrtzidis | bd90076 | 2011-02-28 01:28:01 +0000 | [diff] [blame] | 18 | #include "clang/StaticAnalyzer/Core/CheckerManager.h" |
Jordan Rose | f540c54 | 2012-07-26 21:39:41 +0000 | [diff] [blame] | 19 | #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h" |
Argyrios Kyrtzidis | bd90076 | 2011-02-28 01:28:01 +0000 | [diff] [blame] | 20 | #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" |
Ted Kremenek | 9b66371 | 2011-02-10 01:03:03 +0000 | [diff] [blame] | 21 | #include "clang/StaticAnalyzer/Core/BugReporter/BugType.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 > { |
Dylan Noblesmith | 6f42b62 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 29 | mutable OwningPtr<BugType> BT; |
Ted Kremenek | f493f49 | 2009-11-11 05:50:44 +0000 | [diff] [blame] | 30 | public: |
Argyrios Kyrtzidis | bd90076 | 2011-02-28 01:28:01 +0000 | [diff] [blame] | 31 | |
Jordan Rose | fe6a011 | 2012-07-02 19:28:21 +0000 | [diff] [blame] | 32 | void checkPreCall(const CallEvent &Call, CheckerContext &C) const; |
Ted Kremenek | f493f49 | 2009-11-11 05:50:44 +0000 | [diff] [blame] | 33 | }; |
| 34 | } // end anonymous namespace |
| 35 | |
Jordan Rose | fe6a011 | 2012-07-02 19:28:21 +0000 | [diff] [blame] | 36 | void AttrNonNullChecker::checkPreCall(const CallEvent &Call, |
Argyrios Kyrtzidis | bd90076 | 2011-02-28 01:28:01 +0000 | [diff] [blame] | 37 | CheckerContext &C) const { |
Jordan Rose | fe6a011 | 2012-07-02 19:28:21 +0000 | [diff] [blame] | 38 | const Decl *FD = Call.getDecl(); |
Zhongxing Xu | 94943b6 | 2009-11-03 07:35:33 +0000 | [diff] [blame] | 39 | if (!FD) |
| 40 | return; |
| 41 | |
Jordan Rose | fe6a011 | 2012-07-02 19:28:21 +0000 | [diff] [blame] | 42 | const NonNullAttr *Att = FD->getAttr<NonNullAttr>(); |
Zhongxing Xu | 94943b6 | 2009-11-03 07:35:33 +0000 | [diff] [blame] | 43 | if (!Att) |
| 44 | return; |
| 45 | |
Jordan Rose | fe6a011 | 2012-07-02 19:28:21 +0000 | [diff] [blame] | 46 | ProgramStateRef state = C.getState(); |
| 47 | |
Zhongxing Xu | 94943b6 | 2009-11-03 07:35:33 +0000 | [diff] [blame] | 48 | // Iterate through the arguments of CE and check them for null. |
Jordan Rose | fe6a011 | 2012-07-02 19:28:21 +0000 | [diff] [blame] | 49 | for (unsigned idx = 0, count = Call.getNumArgs(); idx != count; ++idx) { |
Zhongxing Xu | 94943b6 | 2009-11-03 07:35:33 +0000 | [diff] [blame] | 50 | if (!Att->isNonNull(idx)) |
| 51 | continue; |
| 52 | |
Jordan Rose | fe6a011 | 2012-07-02 19:28:21 +0000 | [diff] [blame] | 53 | SVal V = Call.getArgSVal(idx); |
Jordy Rose | 9a12685 | 2010-06-21 20:08:28 +0000 | [diff] [blame] | 54 | DefinedSVal *DV = dyn_cast<DefinedSVal>(&V); |
| 55 | |
| 56 | // If the value is unknown or undefined, we can't perform this check. |
| 57 | if (!DV) |
| 58 | continue; |
Zhongxing Xu | 94943b6 | 2009-11-03 07:35:33 +0000 | [diff] [blame] | 59 | |
Ted Kremenek | bb0ba0b | 2010-11-09 02:11:43 +0000 | [diff] [blame] | 60 | if (!isa<Loc>(*DV)) { |
| 61 | // 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] | 62 | // transparent_union GCC extension. |
| 63 | const Expr *ArgE = Call.getArgExpr(idx); |
| 64 | if (!ArgE) |
| 65 | continue; |
| 66 | |
| 67 | QualType T = ArgE->getType(); |
Ted Kremenek | bb0ba0b | 2010-11-09 02:11:43 +0000 | [diff] [blame] | 68 | const RecordType *UT = T->getAsUnionType(); |
| 69 | if (!UT || !UT->getDecl()->hasAttr<TransparentUnionAttr>()) |
| 70 | continue; |
Jordan Rose | fe6a011 | 2012-07-02 19:28:21 +0000 | [diff] [blame] | 71 | |
Ted Kremenek | bb0ba0b | 2010-11-09 02:11:43 +0000 | [diff] [blame] | 72 | if (nonloc::CompoundVal *CSV = dyn_cast<nonloc::CompoundVal>(DV)) { |
| 73 | nonloc::CompoundVal::iterator CSV_I = CSV->begin(); |
| 74 | assert(CSV_I != CSV->end()); |
| 75 | V = *CSV_I; |
| 76 | DV = dyn_cast<DefinedSVal>(&V); |
| 77 | assert(++CSV_I == CSV->end()); |
| 78 | if (!DV) |
| 79 | continue; |
Jordan Rose | fe6a011 | 2012-07-02 19:28:21 +0000 | [diff] [blame] | 80 | } else { |
Ted Kremenek | bb0ba0b | 2010-11-09 02:11:43 +0000 | [diff] [blame] | 81 | // FIXME: Handle LazyCompoundVals? |
| 82 | continue; |
| 83 | } |
| 84 | } |
| 85 | |
Zhongxing Xu | 94943b6 | 2009-11-03 07:35:33 +0000 | [diff] [blame] | 86 | ConstraintManager &CM = C.getConstraintManager(); |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 87 | ProgramStateRef stateNotNull, stateNull; |
Ted Kremenek | 28f47b9 | 2010-12-01 22:16:56 +0000 | [diff] [blame] | 88 | llvm::tie(stateNotNull, stateNull) = CM.assumeDual(state, *DV); |
Zhongxing Xu | 94943b6 | 2009-11-03 07:35:33 +0000 | [diff] [blame] | 89 | |
| 90 | if (stateNull && !stateNotNull) { |
| 91 | // Generate an error node. Check for a null node in case |
| 92 | // we cache out. |
Ted Kremenek | d048c6e | 2010-12-20 21:19:09 +0000 | [diff] [blame] | 93 | if (ExplodedNode *errorNode = C.generateSink(stateNull)) { |
Zhongxing Xu | 94943b6 | 2009-11-03 07:35:33 +0000 | [diff] [blame] | 94 | |
| 95 | // Lazily allocate the BugType object if it hasn't already been |
| 96 | // created. Ownership is transferred to the BugReporter object once |
| 97 | // the BugReport is passed to 'EmitWarning'. |
| 98 | if (!BT) |
Argyrios Kyrtzidis | bd90076 | 2011-02-28 01:28:01 +0000 | [diff] [blame] | 99 | BT.reset(new BugType("Argument with 'nonnull' attribute passed null", |
| 100 | "API")); |
Zhongxing Xu | 94943b6 | 2009-11-03 07:35:33 +0000 | [diff] [blame] | 101 | |
Anna Zaks | e172e8b | 2011-08-17 23:00:25 +0000 | [diff] [blame] | 102 | BugReport *R = |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 103 | new BugReport(*BT, "Null pointer passed as an argument to a " |
| 104 | "'nonnull' parameter", errorNode); |
Zhongxing Xu | 94943b6 | 2009-11-03 07:35:33 +0000 | [diff] [blame] | 105 | |
| 106 | // Highlight the range of the argument that was null. |
Jordan Rose | fe6a011 | 2012-07-02 19:28:21 +0000 | [diff] [blame] | 107 | R->addRange(Call.getArgSourceRange(idx)); |
| 108 | if (const Expr *ArgE = Call.getArgExpr(idx)) |
Jordan Rose | a1f81bb | 2012-08-28 00:50:51 +0000 | [diff] [blame] | 109 | bugreporter::trackNullOrUndefValue(errorNode, ArgE, *R); |
Zhongxing Xu | 94943b6 | 2009-11-03 07:35:33 +0000 | [diff] [blame] | 110 | // Emit the bug report. |
Jordan Rose | 785950e | 2012-11-02 01:53:40 +0000 | [diff] [blame] | 111 | C.emitReport(R); |
Zhongxing Xu | 94943b6 | 2009-11-03 07:35:33 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | // Always return. Either we cached out or we just emitted an error. |
| 115 | return; |
| 116 | } |
| 117 | |
| 118 | // If a pointer value passed the check we should assume that it is |
| 119 | // indeed not null from this point forward. |
| 120 | assert(stateNotNull); |
| 121 | state = stateNotNull; |
| 122 | } |
| 123 | |
| 124 | // If we reach here all of the arguments passed the nonnull check. |
| 125 | // If 'state' has been updated generated a new node. |
Anna Zaks | 0bd6b11 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 126 | C.addTransition(state); |
Zhongxing Xu | 94943b6 | 2009-11-03 07:35:33 +0000 | [diff] [blame] | 127 | } |
Argyrios Kyrtzidis | bd90076 | 2011-02-28 01:28:01 +0000 | [diff] [blame] | 128 | |
| 129 | void ento::registerAttrNonNullChecker(CheckerManager &mgr) { |
| 130 | mgr.registerChecker<AttrNonNullChecker>(); |
| 131 | } |