Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 1 | //=-- GRExprEngineInternalChecks.cpp - Builtin GRExprEngine Checks---*- 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 | // |
| 10 | // This file defines the BugType classes used by GRExprEngine to report |
| 11 | // bugs derived from builtin checks in the path-sensitive engine. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "clang/Analysis/PathSensitive/BugReporter.h" |
| 16 | #include "clang/Analysis/PathSensitive/GRExprEngine.h" |
Ted Kremenek | c26a8b0 | 2009-07-22 21:46:56 +0000 | [diff] [blame] | 17 | #include "clang/Analysis/PathSensitive/CheckerVisitor.h" |
Ted Kremenek | dd986cc | 2009-05-07 00:45:33 +0000 | [diff] [blame] | 18 | #include "clang/Analysis/PathDiagnostic.h" |
Ted Kremenek | 8aed806 | 2008-10-31 00:13:20 +0000 | [diff] [blame] | 19 | #include "clang/Basic/SourceManager.h" |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Compiler.h" |
Ted Kremenek | ad51a60 | 2008-10-31 00:18:30 +0000 | [diff] [blame] | 21 | #include "llvm/Support/raw_ostream.h" |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 22 | |
| 23 | using namespace clang; |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 24 | using namespace clang::bugreporter; |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 25 | |
| 26 | //===----------------------------------------------------------------------===// |
| 27 | // Utility functions. |
| 28 | //===----------------------------------------------------------------------===// |
| 29 | |
| 30 | template <typename ITERATOR> inline |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 31 | ExplodedNode* GetNode(ITERATOR I) { |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 32 | return *I; |
| 33 | } |
| 34 | |
| 35 | template <> inline |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 36 | ExplodedNode* GetNode(GRExprEngine::undef_arg_iterator I) { |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 37 | return I->first; |
| 38 | } |
| 39 | |
| 40 | //===----------------------------------------------------------------------===// |
| 41 | // Bug Descriptions. |
| 42 | //===----------------------------------------------------------------------===// |
| 43 | |
| 44 | namespace { |
Ted Kremenek | dd986cc | 2009-05-07 00:45:33 +0000 | [diff] [blame] | 45 | |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame] | 46 | class VISIBILITY_HIDDEN BuiltinBugReport : public RangedBugReport { |
Ted Kremenek | dd986cc | 2009-05-07 00:45:33 +0000 | [diff] [blame] | 47 | public: |
| 48 | BuiltinBugReport(BugType& bt, const char* desc, |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 49 | ExplodedNode *n) |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame] | 50 | : RangedBugReport(bt, desc, n) {} |
Ted Kremenek | dd986cc | 2009-05-07 00:45:33 +0000 | [diff] [blame] | 51 | |
Ted Kremenek | 85ac934 | 2009-05-15 05:25:09 +0000 | [diff] [blame] | 52 | BuiltinBugReport(BugType& bt, const char *shortDesc, const char *desc, |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 53 | ExplodedNode *n) |
Ted Kremenek | 85ac934 | 2009-05-15 05:25:09 +0000 | [diff] [blame] | 54 | : RangedBugReport(bt, shortDesc, desc, n) {} |
| 55 | |
Ted Kremenek | dd986cc | 2009-05-07 00:45:33 +0000 | [diff] [blame] | 56 | void registerInitialVisitors(BugReporterContext& BRC, |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 57 | const ExplodedNode* N); |
Ted Kremenek | dd986cc | 2009-05-07 00:45:33 +0000 | [diff] [blame] | 58 | }; |
| 59 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 60 | class VISIBILITY_HIDDEN BuiltinBug : public BugType { |
| 61 | GRExprEngine &Eng; |
Ted Kremenek | 159d248 | 2008-12-09 00:44:16 +0000 | [diff] [blame] | 62 | protected: |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 63 | const std::string desc; |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 64 | public: |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 65 | BuiltinBug(GRExprEngine *eng, const char* n, const char* d) |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame] | 66 | : BugType(n, "Logic errors"), Eng(*eng), desc(d) {} |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 67 | |
| 68 | BuiltinBug(GRExprEngine *eng, const char* n) |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame] | 69 | : BugType(n, "Logic errors"), Eng(*eng), desc(n) {} |
Ted Kremenek | 22bda88 | 2008-07-31 20:31:27 +0000 | [diff] [blame] | 70 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 71 | virtual void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) = 0; |
| 72 | |
| 73 | void FlushReports(BugReporter& BR) { FlushReportsImpl(BR, Eng); } |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 74 | |
Ted Kremenek | dd986cc | 2009-05-07 00:45:33 +0000 | [diff] [blame] | 75 | virtual void registerInitialVisitors(BugReporterContext& BRC, |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 76 | const ExplodedNode* N, |
Ted Kremenek | dd986cc | 2009-05-07 00:45:33 +0000 | [diff] [blame] | 77 | BuiltinBugReport *R) {} |
| 78 | |
| 79 | template <typename ITER> void Emit(BugReporter& BR, ITER I, ITER E); |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 80 | }; |
| 81 | |
Ted Kremenek | dd986cc | 2009-05-07 00:45:33 +0000 | [diff] [blame] | 82 | |
| 83 | template <typename ITER> |
| 84 | void BuiltinBug::Emit(BugReporter& BR, ITER I, ITER E) { |
| 85 | for (; I != E; ++I) BR.EmitReport(new BuiltinBugReport(*this, desc.c_str(), |
| 86 | GetNode(I))); |
| 87 | } |
| 88 | |
| 89 | void BuiltinBugReport::registerInitialVisitors(BugReporterContext& BRC, |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 90 | const ExplodedNode* N) { |
Ted Kremenek | dd986cc | 2009-05-07 00:45:33 +0000 | [diff] [blame] | 91 | static_cast<BuiltinBug&>(getBugType()).registerInitialVisitors(BRC, N, this); |
| 92 | } |
| 93 | |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 94 | class VISIBILITY_HIDDEN NullDeref : public BuiltinBug { |
| 95 | public: |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 96 | NullDeref(GRExprEngine* eng) |
Ted Kremenek | 0fa9654 | 2009-04-07 04:54:31 +0000 | [diff] [blame] | 97 | : BuiltinBug(eng,"Null dereference", "Dereference of null pointer") {} |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 98 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 99 | void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) { |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 100 | Emit(BR, Eng.null_derefs_begin(), Eng.null_derefs_end()); |
| 101 | } |
Ted Kremenek | dd986cc | 2009-05-07 00:45:33 +0000 | [diff] [blame] | 102 | |
| 103 | void registerInitialVisitors(BugReporterContext& BRC, |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 104 | const ExplodedNode* N, |
Ted Kremenek | dd986cc | 2009-05-07 00:45:33 +0000 | [diff] [blame] | 105 | BuiltinBugReport *R) { |
Ted Kremenek | 85ac934 | 2009-05-15 05:25:09 +0000 | [diff] [blame] | 106 | registerTrackNullOrUndefValue(BRC, GetDerefExpr(N), N); |
Ted Kremenek | dd986cc | 2009-05-07 00:45:33 +0000 | [diff] [blame] | 107 | } |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 108 | }; |
| 109 | |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame] | 110 | class VISIBILITY_HIDDEN NilReceiverStructRet : public BuiltinBug { |
Ted Kremenek | 21fe837 | 2009-02-19 04:06:22 +0000 | [diff] [blame] | 111 | public: |
| 112 | NilReceiverStructRet(GRExprEngine* eng) : |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame] | 113 | BuiltinBug(eng, "'nil' receiver with struct return type") {} |
Ted Kremenek | 21fe837 | 2009-02-19 04:06:22 +0000 | [diff] [blame] | 114 | |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame] | 115 | void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) { |
Ted Kremenek | 21fe837 | 2009-02-19 04:06:22 +0000 | [diff] [blame] | 116 | for (GRExprEngine::nil_receiver_struct_ret_iterator |
| 117 | I=Eng.nil_receiver_struct_ret_begin(), |
| 118 | E=Eng.nil_receiver_struct_ret_end(); I!=E; ++I) { |
| 119 | |
| 120 | std::string sbuf; |
| 121 | llvm::raw_string_ostream os(sbuf); |
| 122 | PostStmt P = cast<PostStmt>((*I)->getLocation()); |
Ted Kremenek | 5f85e17 | 2009-07-22 22:35:28 +0000 | [diff] [blame] | 123 | const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(P.getStmt()); |
Ted Kremenek | 21fe837 | 2009-02-19 04:06:22 +0000 | [diff] [blame] | 124 | os << "The receiver in the message expression is 'nil' and results in the" |
| 125 | " returned value (of type '" |
| 126 | << ME->getType().getAsString() |
| 127 | << "') to be garbage or otherwise undefined."; |
| 128 | |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame] | 129 | BuiltinBugReport *R = new BuiltinBugReport(*this, os.str().c_str(), *I); |
Ted Kremenek | 21fe837 | 2009-02-19 04:06:22 +0000 | [diff] [blame] | 130 | R->addRange(ME->getReceiver()->getSourceRange()); |
| 131 | BR.EmitReport(R); |
| 132 | } |
| 133 | } |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame] | 134 | |
| 135 | void registerInitialVisitors(BugReporterContext& BRC, |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 136 | const ExplodedNode* N, |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame] | 137 | BuiltinBugReport *R) { |
Ted Kremenek | 85ac934 | 2009-05-15 05:25:09 +0000 | [diff] [blame] | 138 | registerTrackNullOrUndefValue(BRC, GetReceiverExpr(N), N); |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame] | 139 | } |
Ted Kremenek | 21fe837 | 2009-02-19 04:06:22 +0000 | [diff] [blame] | 140 | }; |
Ted Kremenek | 899b3de | 2009-04-08 03:07:17 +0000 | [diff] [blame] | 141 | |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame] | 142 | class VISIBILITY_HIDDEN NilReceiverLargerThanVoidPtrRet : public BuiltinBug { |
Ted Kremenek | 899b3de | 2009-04-08 03:07:17 +0000 | [diff] [blame] | 143 | public: |
| 144 | NilReceiverLargerThanVoidPtrRet(GRExprEngine* eng) : |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame] | 145 | BuiltinBug(eng, |
| 146 | "'nil' receiver with return type larger than sizeof(void *)") {} |
Ted Kremenek | 899b3de | 2009-04-08 03:07:17 +0000 | [diff] [blame] | 147 | |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame] | 148 | void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) { |
Ted Kremenek | 899b3de | 2009-04-08 03:07:17 +0000 | [diff] [blame] | 149 | for (GRExprEngine::nil_receiver_larger_than_voidptr_ret_iterator |
| 150 | I=Eng.nil_receiver_larger_than_voidptr_ret_begin(), |
| 151 | E=Eng.nil_receiver_larger_than_voidptr_ret_end(); I!=E; ++I) { |
| 152 | |
| 153 | std::string sbuf; |
| 154 | llvm::raw_string_ostream os(sbuf); |
| 155 | PostStmt P = cast<PostStmt>((*I)->getLocation()); |
Ted Kremenek | 5f85e17 | 2009-07-22 22:35:28 +0000 | [diff] [blame] | 156 | const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(P.getStmt()); |
Ted Kremenek | 899b3de | 2009-04-08 03:07:17 +0000 | [diff] [blame] | 157 | os << "The receiver in the message expression is 'nil' and results in the" |
| 158 | " returned value (of type '" |
| 159 | << ME->getType().getAsString() |
| 160 | << "' and of size " |
| 161 | << Eng.getContext().getTypeSize(ME->getType()) / 8 |
| 162 | << " bytes) to be garbage or otherwise undefined."; |
| 163 | |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame] | 164 | BuiltinBugReport *R = new BuiltinBugReport(*this, os.str().c_str(), *I); |
Ted Kremenek | 899b3de | 2009-04-08 03:07:17 +0000 | [diff] [blame] | 165 | R->addRange(ME->getReceiver()->getSourceRange()); |
| 166 | BR.EmitReport(R); |
| 167 | } |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame] | 168 | } |
| 169 | void registerInitialVisitors(BugReporterContext& BRC, |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 170 | const ExplodedNode* N, |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame] | 171 | BuiltinBugReport *R) { |
Ted Kremenek | 85ac934 | 2009-05-15 05:25:09 +0000 | [diff] [blame] | 172 | registerTrackNullOrUndefValue(BRC, GetReceiverExpr(N), N); |
Ted Kremenek | 899b3de | 2009-04-08 03:07:17 +0000 | [diff] [blame] | 173 | } |
| 174 | }; |
Ted Kremenek | 21fe837 | 2009-02-19 04:06:22 +0000 | [diff] [blame] | 175 | |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 176 | class VISIBILITY_HIDDEN UndefinedDeref : public BuiltinBug { |
| 177 | public: |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 178 | UndefinedDeref(GRExprEngine* eng) |
Ted Kremenek | 17a8e07 | 2009-03-01 05:43:22 +0000 | [diff] [blame] | 179 | : BuiltinBug(eng,"Dereference of undefined pointer value") {} |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 180 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 181 | void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) { |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 182 | Emit(BR, Eng.undef_derefs_begin(), Eng.undef_derefs_end()); |
| 183 | } |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame] | 184 | |
| 185 | void registerInitialVisitors(BugReporterContext& BRC, |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 186 | const ExplodedNode* N, |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame] | 187 | BuiltinBugReport *R) { |
Ted Kremenek | 85ac934 | 2009-05-15 05:25:09 +0000 | [diff] [blame] | 188 | registerTrackNullOrUndefValue(BRC, GetDerefExpr(N), N); |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame] | 189 | } |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 190 | }; |
| 191 | |
| 192 | class VISIBILITY_HIDDEN DivZero : public BuiltinBug { |
| 193 | public: |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 194 | DivZero(GRExprEngine* eng) |
Ted Kremenek | 5d88ff8 | 2009-04-02 02:40:26 +0000 | [diff] [blame] | 195 | : BuiltinBug(eng,"Division-by-zero", |
| 196 | "Division by zero or undefined value.") {} |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 197 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 198 | void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) { |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 199 | Emit(BR, Eng.explicit_bad_divides_begin(), Eng.explicit_bad_divides_end()); |
| 200 | } |
Ted Kremenek | 85ac934 | 2009-05-15 05:25:09 +0000 | [diff] [blame] | 201 | |
| 202 | void registerInitialVisitors(BugReporterContext& BRC, |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 203 | const ExplodedNode* N, |
Ted Kremenek | 85ac934 | 2009-05-15 05:25:09 +0000 | [diff] [blame] | 204 | BuiltinBugReport *R) { |
| 205 | registerTrackNullOrUndefValue(BRC, GetDenomExpr(N), N); |
| 206 | } |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 207 | }; |
| 208 | |
| 209 | class VISIBILITY_HIDDEN UndefResult : public BuiltinBug { |
| 210 | public: |
Ted Kremenek | 5d88ff8 | 2009-04-02 02:40:26 +0000 | [diff] [blame] | 211 | UndefResult(GRExprEngine* eng) : BuiltinBug(eng,"Undefined result", |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 212 | "Result of operation is undefined.") {} |
| 213 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 214 | void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) { |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 215 | Emit(BR, Eng.undef_results_begin(), Eng.undef_results_end()); |
| 216 | } |
| 217 | }; |
| 218 | |
| 219 | class VISIBILITY_HIDDEN BadCall : public BuiltinBug { |
| 220 | public: |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 221 | BadCall(GRExprEngine *eng) |
Ted Kremenek | 5d88ff8 | 2009-04-02 02:40:26 +0000 | [diff] [blame] | 222 | : BuiltinBug(eng, "Invalid function call", |
Ted Kremenek | 17a8e07 | 2009-03-01 05:43:22 +0000 | [diff] [blame] | 223 | "Called function pointer is a null or undefined pointer value") {} |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 224 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 225 | void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) { |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 226 | Emit(BR, Eng.bad_calls_begin(), Eng.bad_calls_end()); |
| 227 | } |
Ted Kremenek | 85ac934 | 2009-05-15 05:25:09 +0000 | [diff] [blame] | 228 | |
| 229 | void registerInitialVisitors(BugReporterContext& BRC, |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 230 | const ExplodedNode* N, |
Ted Kremenek | 85ac934 | 2009-05-15 05:25:09 +0000 | [diff] [blame] | 231 | BuiltinBugReport *R) { |
| 232 | registerTrackNullOrUndefValue(BRC, GetCalleeExpr(N), N); |
| 233 | } |
| 234 | }; |
| 235 | |
| 236 | |
| 237 | class VISIBILITY_HIDDEN ArgReport : public BuiltinBugReport { |
| 238 | const Stmt *Arg; |
| 239 | public: |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 240 | ArgReport(BugType& bt, const char* desc, ExplodedNode *n, |
Ted Kremenek | 85ac934 | 2009-05-15 05:25:09 +0000 | [diff] [blame] | 241 | const Stmt *arg) |
| 242 | : BuiltinBugReport(bt, desc, n), Arg(arg) {} |
| 243 | |
| 244 | ArgReport(BugType& bt, const char *shortDesc, const char *desc, |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 245 | ExplodedNode *n, const Stmt *arg) |
Ted Kremenek | 85ac934 | 2009-05-15 05:25:09 +0000 | [diff] [blame] | 246 | : BuiltinBugReport(bt, shortDesc, desc, n), Arg(arg) {} |
| 247 | |
Zhongxing Xu | 9a5bca3 | 2009-08-29 02:11:01 +0000 | [diff] [blame^] | 248 | const Stmt *getArg() const { return Arg; } |
| 249 | |
| 250 | void registerInitialVisitors(BugReporterContext& BRC, |
| 251 | const ExplodedNode* N) { |
| 252 | registerTrackNullOrUndefValue(BRC, getArg(), N); |
| 253 | } |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 254 | }; |
| 255 | |
| 256 | class VISIBILITY_HIDDEN BadArg : public BuiltinBug { |
Ted Kremenek | 85ac934 | 2009-05-15 05:25:09 +0000 | [diff] [blame] | 257 | public: |
Ted Kremenek | 5d88ff8 | 2009-04-02 02:40:26 +0000 | [diff] [blame] | 258 | BadArg(GRExprEngine* eng) : BuiltinBug(eng,"Uninitialized argument", |
Ted Kremenek | 17a8e07 | 2009-03-01 05:43:22 +0000 | [diff] [blame] | 259 | "Pass-by-value argument in function call is undefined.") {} |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 260 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 261 | BadArg(GRExprEngine* eng, const char* d) |
Ted Kremenek | 5d88ff8 | 2009-04-02 02:40:26 +0000 | [diff] [blame] | 262 | : BuiltinBug(eng,"Uninitialized argument", d) {} |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 263 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 264 | void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) { |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 265 | for (GRExprEngine::UndefArgsTy::iterator I = Eng.undef_arg_begin(), |
| 266 | E = Eng.undef_arg_end(); I!=E; ++I) { |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 267 | // Generate a report for this bug. |
Ted Kremenek | 85ac934 | 2009-05-15 05:25:09 +0000 | [diff] [blame] | 268 | ArgReport *report = new ArgReport(*this, desc.c_str(), I->first, |
| 269 | I->second); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 270 | report->addRange(I->second->getSourceRange()); |
| 271 | BR.EmitReport(report); |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 272 | } |
| 273 | } |
Ted Kremenek | 85ac934 | 2009-05-15 05:25:09 +0000 | [diff] [blame] | 274 | |
| 275 | void registerInitialVisitors(BugReporterContext& BRC, |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 276 | const ExplodedNode* N, |
Ted Kremenek | 85ac934 | 2009-05-15 05:25:09 +0000 | [diff] [blame] | 277 | BuiltinBugReport *R) { |
| 278 | registerTrackNullOrUndefValue(BRC, static_cast<ArgReport*>(R)->getArg(), |
| 279 | N); |
| 280 | } |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 281 | }; |
| 282 | |
| 283 | class VISIBILITY_HIDDEN BadMsgExprArg : public BadArg { |
| 284 | public: |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 285 | BadMsgExprArg(GRExprEngine* eng) |
Ted Kremenek | 5d88ff8 | 2009-04-02 02:40:26 +0000 | [diff] [blame] | 286 | : BadArg(eng,"Pass-by-value argument in message expression is undefined"){} |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 287 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 288 | void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) { |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 289 | for (GRExprEngine::UndefArgsTy::iterator I=Eng.msg_expr_undef_arg_begin(), |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 290 | E = Eng.msg_expr_undef_arg_end(); I!=E; ++I) { |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 291 | // Generate a report for this bug. |
Ted Kremenek | 85ac934 | 2009-05-15 05:25:09 +0000 | [diff] [blame] | 292 | ArgReport *report = new ArgReport(*this, desc.c_str(), I->first, |
| 293 | I->second); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 294 | report->addRange(I->second->getSourceRange()); |
| 295 | BR.EmitReport(report); |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 296 | } |
Ted Kremenek | 85ac934 | 2009-05-15 05:25:09 +0000 | [diff] [blame] | 297 | } |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 298 | }; |
| 299 | |
| 300 | class VISIBILITY_HIDDEN BadReceiver : public BuiltinBug { |
| 301 | public: |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 302 | BadReceiver(GRExprEngine* eng) |
Ted Kremenek | 5d88ff8 | 2009-04-02 02:40:26 +0000 | [diff] [blame] | 303 | : BuiltinBug(eng,"Uninitialized receiver", |
| 304 | "Receiver in message expression is an uninitialized value") {} |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 305 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 306 | void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) { |
Ted Kremenek | efd5994 | 2008-12-08 22:47:34 +0000 | [diff] [blame] | 307 | for (GRExprEngine::ErrorNodes::iterator I=Eng.undef_receivers_begin(), |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 308 | End = Eng.undef_receivers_end(); I!=End; ++I) { |
| 309 | |
| 310 | // Generate a report for this bug. |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame] | 311 | BuiltinBugReport *report = new BuiltinBugReport(*this, desc.c_str(), *I); |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 312 | ExplodedNode* N = *I; |
Ted Kremenek | 5f85e17 | 2009-07-22 22:35:28 +0000 | [diff] [blame] | 313 | const Stmt *S = cast<PostStmt>(N->getLocation()).getStmt(); |
| 314 | const Expr* E = cast<ObjCMessageExpr>(S)->getReceiver(); |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 315 | assert (E && "Receiver cannot be NULL"); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 316 | report->addRange(E->getSourceRange()); |
| 317 | BR.EmitReport(report); |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame] | 318 | } |
| 319 | } |
Ted Kremenek | 85ac934 | 2009-05-15 05:25:09 +0000 | [diff] [blame] | 320 | |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame] | 321 | void registerInitialVisitors(BugReporterContext& BRC, |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 322 | const ExplodedNode* N, |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame] | 323 | BuiltinBugReport *R) { |
Ted Kremenek | 85ac934 | 2009-05-15 05:25:09 +0000 | [diff] [blame] | 324 | registerTrackNullOrUndefValue(BRC, GetReceiverExpr(N), N); |
| 325 | } |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 326 | }; |
Ted Kremenek | 5917d78 | 2008-11-21 00:27:44 +0000 | [diff] [blame] | 327 | |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 328 | class VISIBILITY_HIDDEN RetStack : public BuiltinBug { |
| 329 | public: |
Ted Kremenek | 17a8e07 | 2009-03-01 05:43:22 +0000 | [diff] [blame] | 330 | RetStack(GRExprEngine* eng) |
Ted Kremenek | 5d88ff8 | 2009-04-02 02:40:26 +0000 | [diff] [blame] | 331 | : BuiltinBug(eng, "Return of address to stack-allocated memory") {} |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 332 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 333 | void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) { |
Ted Kremenek | b7714b2 | 2008-07-30 17:49:12 +0000 | [diff] [blame] | 334 | for (GRExprEngine::ret_stackaddr_iterator I=Eng.ret_stackaddr_begin(), |
| 335 | End = Eng.ret_stackaddr_end(); I!=End; ++I) { |
Ted Kremenek | 22bda88 | 2008-07-31 20:31:27 +0000 | [diff] [blame] | 336 | |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 337 | ExplodedNode* N = *I; |
Ted Kremenek | 5f85e17 | 2009-07-22 22:35:28 +0000 | [diff] [blame] | 338 | const Stmt *S = cast<PostStmt>(N->getLocation()).getStmt(); |
| 339 | const Expr* E = cast<ReturnStmt>(S)->getRetValue(); |
| 340 | assert(E && "Return expression cannot be NULL"); |
Ted Kremenek | 22bda88 | 2008-07-31 20:31:27 +0000 | [diff] [blame] | 341 | |
| 342 | // Get the value associated with E. |
Ted Kremenek | 23ec48c | 2009-06-18 23:58:37 +0000 | [diff] [blame] | 343 | loc::MemRegionVal V = cast<loc::MemRegionVal>(N->getState()->getSVal(E)); |
Ted Kremenek | 22bda88 | 2008-07-31 20:31:27 +0000 | [diff] [blame] | 344 | |
| 345 | // Generate a report for this bug. |
Ted Kremenek | ad51a60 | 2008-10-31 00:18:30 +0000 | [diff] [blame] | 346 | std::string buf; |
| 347 | llvm::raw_string_ostream os(buf); |
Ted Kremenek | 8aed806 | 2008-10-31 00:13:20 +0000 | [diff] [blame] | 348 | SourceRange R; |
Ted Kremenek | 22bda88 | 2008-07-31 20:31:27 +0000 | [diff] [blame] | 349 | |
Ted Kremenek | 8aed806 | 2008-10-31 00:13:20 +0000 | [diff] [blame] | 350 | // Check if the region is a compound literal. |
| 351 | if (const CompoundLiteralRegion* CR = |
| 352 | dyn_cast<CompoundLiteralRegion>(V.getRegion())) { |
| 353 | |
| 354 | const CompoundLiteralExpr* CL = CR->getLiteralExpr(); |
| 355 | os << "Address of stack memory associated with a compound literal " |
| 356 | "declared on line " |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 357 | << BR.getSourceManager() |
| 358 | .getInstantiationLineNumber(CL->getLocStart()) |
Ted Kremenek | 8aed806 | 2008-10-31 00:13:20 +0000 | [diff] [blame] | 359 | << " returned."; |
| 360 | |
| 361 | R = CL->getSourceRange(); |
| 362 | } |
Ted Kremenek | de8cd19 | 2008-11-02 00:35:25 +0000 | [diff] [blame] | 363 | else if (const AllocaRegion* AR = dyn_cast<AllocaRegion>(V.getRegion())) { |
| 364 | const Expr* ARE = AR->getExpr(); |
| 365 | SourceLocation L = ARE->getLocStart(); |
| 366 | R = ARE->getSourceRange(); |
| 367 | |
| 368 | os << "Address of stack memory allocated by call to alloca() on line " |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 369 | << BR.getSourceManager().getInstantiationLineNumber(L) |
Ted Kremenek | de8cd19 | 2008-11-02 00:35:25 +0000 | [diff] [blame] | 370 | << " returned."; |
| 371 | } |
Ted Kremenek | 8aed806 | 2008-10-31 00:13:20 +0000 | [diff] [blame] | 372 | else { |
| 373 | os << "Address of stack memory associated with local variable '" |
| 374 | << V.getRegion()->getString() << "' returned."; |
| 375 | } |
Ted Kremenek | 22bda88 | 2008-07-31 20:31:27 +0000 | [diff] [blame] | 376 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 377 | RangedBugReport *report = new RangedBugReport(*this, os.str().c_str(), N); |
| 378 | report->addRange(E->getSourceRange()); |
| 379 | if (R.isValid()) report->addRange(R); |
| 380 | BR.EmitReport(report); |
Ted Kremenek | b7714b2 | 2008-07-30 17:49:12 +0000 | [diff] [blame] | 381 | } |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 382 | } |
| 383 | }; |
Ted Kremenek | 5917d78 | 2008-11-21 00:27:44 +0000 | [diff] [blame] | 384 | |
| 385 | class VISIBILITY_HIDDEN RetUndef : public BuiltinBug { |
| 386 | public: |
Ted Kremenek | 5d88ff8 | 2009-04-02 02:40:26 +0000 | [diff] [blame] | 387 | RetUndef(GRExprEngine* eng) : BuiltinBug(eng, "Uninitialized return value", |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame] | 388 | "Uninitialized or undefined value returned to caller.") {} |
Ted Kremenek | 5917d78 | 2008-11-21 00:27:44 +0000 | [diff] [blame] | 389 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 390 | void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) { |
Ted Kremenek | 5917d78 | 2008-11-21 00:27:44 +0000 | [diff] [blame] | 391 | Emit(BR, Eng.ret_undef_begin(), Eng.ret_undef_end()); |
| 392 | } |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame] | 393 | |
| 394 | void registerInitialVisitors(BugReporterContext& BRC, |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 395 | const ExplodedNode* N, |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame] | 396 | BuiltinBugReport *R) { |
Ted Kremenek | 85ac934 | 2009-05-15 05:25:09 +0000 | [diff] [blame] | 397 | registerTrackNullOrUndefValue(BRC, GetRetValExpr(N), N); |
| 398 | } |
Ted Kremenek | 5917d78 | 2008-11-21 00:27:44 +0000 | [diff] [blame] | 399 | }; |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 400 | |
| 401 | class VISIBILITY_HIDDEN UndefBranch : public BuiltinBug { |
| 402 | struct VISIBILITY_HIDDEN FindUndefExpr { |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 403 | GRStateManager& VM; |
| 404 | const GRState* St; |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 405 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 406 | FindUndefExpr(GRStateManager& V, const GRState* S) : VM(V), St(S) {} |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 407 | |
Ted Kremenek | b7714b2 | 2008-07-30 17:49:12 +0000 | [diff] [blame] | 408 | Expr* FindExpr(Expr* Ex) { |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 409 | if (!MatchesCriteria(Ex)) |
Ted Kremenek | b7714b2 | 2008-07-30 17:49:12 +0000 | [diff] [blame] | 410 | return 0; |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 411 | |
Ted Kremenek | b7714b2 | 2008-07-30 17:49:12 +0000 | [diff] [blame] | 412 | for (Stmt::child_iterator I=Ex->child_begin(), E=Ex->child_end();I!=E;++I) |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 413 | if (Expr* ExI = dyn_cast_or_null<Expr>(*I)) { |
| 414 | Expr* E2 = FindExpr(ExI); |
| 415 | if (E2) return E2; |
| 416 | } |
| 417 | |
| 418 | return Ex; |
| 419 | } |
| 420 | |
Ted Kremenek | 23ec48c | 2009-06-18 23:58:37 +0000 | [diff] [blame] | 421 | bool MatchesCriteria(Expr* Ex) { return St->getSVal(Ex).isUndef(); } |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 422 | }; |
| 423 | |
| 424 | public: |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 425 | UndefBranch(GRExprEngine *eng) |
Ted Kremenek | 5d88ff8 | 2009-04-02 02:40:26 +0000 | [diff] [blame] | 426 | : BuiltinBug(eng,"Use of uninitialized value", |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 427 | "Branch condition evaluates to an uninitialized value.") {} |
| 428 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 429 | void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) { |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 430 | for (GRExprEngine::undef_branch_iterator I=Eng.undef_branches_begin(), |
| 431 | E=Eng.undef_branches_end(); I!=E; ++I) { |
| 432 | |
| 433 | // What's going on here: we want to highlight the subexpression of the |
| 434 | // condition that is the most likely source of the "uninitialized |
| 435 | // branch condition." We do a recursive walk of the condition's |
| 436 | // subexpressions and roughly look for the most nested subexpression |
| 437 | // that binds to Undefined. We then highlight that expression's range. |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 438 | BlockEdge B = cast<BlockEdge>((*I)->getLocation()); |
| 439 | Expr* Ex = cast<Expr>(B.getSrc()->getTerminatorCondition()); |
| 440 | assert (Ex && "Block must have a terminator."); |
| 441 | |
| 442 | // Get the predecessor node and check if is a PostStmt with the Stmt |
| 443 | // being the terminator condition. We want to inspect the state |
| 444 | // of that node instead because it will contain main information about |
| 445 | // the subexpressions. |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 446 | assert (!(*I)->pred_empty()); |
| 447 | |
| 448 | // Note: any predecessor will do. They should have identical state, |
| 449 | // since all the BlockEdge did was act as an error sink since the value |
| 450 | // had to already be undefined. |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 451 | ExplodedNode *N = *(*I)->pred_begin(); |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 452 | ProgramPoint P = N->getLocation(); |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 453 | const GRState* St = (*I)->getState(); |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 454 | |
| 455 | if (PostStmt* PS = dyn_cast<PostStmt>(&P)) |
| 456 | if (PS->getStmt() == Ex) |
| 457 | St = N->getState(); |
| 458 | |
| 459 | FindUndefExpr FindIt(Eng.getStateManager(), St); |
| 460 | Ex = FindIt.FindExpr(Ex); |
| 461 | |
Ted Kremenek | 85ac934 | 2009-05-15 05:25:09 +0000 | [diff] [blame] | 462 | ArgReport *R = new ArgReport(*this, desc.c_str(), *I, Ex); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 463 | R->addRange(Ex->getSourceRange()); |
| 464 | BR.EmitReport(R); |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 465 | } |
| 466 | } |
Ted Kremenek | 85ac934 | 2009-05-15 05:25:09 +0000 | [diff] [blame] | 467 | |
| 468 | void registerInitialVisitors(BugReporterContext& BRC, |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 469 | const ExplodedNode* N, |
Ted Kremenek | 85ac934 | 2009-05-15 05:25:09 +0000 | [diff] [blame] | 470 | BuiltinBugReport *R) { |
| 471 | registerTrackNullOrUndefValue(BRC, static_cast<ArgReport*>(R)->getArg(), |
| 472 | N); |
| 473 | } |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 474 | }; |
| 475 | |
Zhongxing Xu | 1c0c233 | 2008-11-23 05:52:28 +0000 | [diff] [blame] | 476 | class VISIBILITY_HIDDEN OutOfBoundMemoryAccess : public BuiltinBug { |
| 477 | public: |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 478 | OutOfBoundMemoryAccess(GRExprEngine* eng) |
Ted Kremenek | 5d88ff8 | 2009-04-02 02:40:26 +0000 | [diff] [blame] | 479 | : BuiltinBug(eng,"Out-of-bounds memory access", |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 480 | "Load or store into an out-of-bound memory position.") {} |
Zhongxing Xu | 1c0c233 | 2008-11-23 05:52:28 +0000 | [diff] [blame] | 481 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 482 | void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) { |
Zhongxing Xu | 1c0c233 | 2008-11-23 05:52:28 +0000 | [diff] [blame] | 483 | Emit(BR, Eng.explicit_oob_memacc_begin(), Eng.explicit_oob_memacc_end()); |
| 484 | } |
| 485 | }; |
Ted Kremenek | efd5994 | 2008-12-08 22:47:34 +0000 | [diff] [blame] | 486 | |
Ted Kremenek | 159d248 | 2008-12-09 00:44:16 +0000 | [diff] [blame] | 487 | class VISIBILITY_HIDDEN BadSizeVLA : public BuiltinBug { |
Ted Kremenek | efd5994 | 2008-12-08 22:47:34 +0000 | [diff] [blame] | 488 | public: |
Ted Kremenek | 5d88ff8 | 2009-04-02 02:40:26 +0000 | [diff] [blame] | 489 | BadSizeVLA(GRExprEngine* eng) : |
| 490 | BuiltinBug(eng, "Bad variable-length array (VLA) size") {} |
Ted Kremenek | efd5994 | 2008-12-08 22:47:34 +0000 | [diff] [blame] | 491 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 492 | void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) { |
Ted Kremenek | efd5994 | 2008-12-08 22:47:34 +0000 | [diff] [blame] | 493 | for (GRExprEngine::ErrorNodes::iterator |
Ted Kremenek | 159d248 | 2008-12-09 00:44:16 +0000 | [diff] [blame] | 494 | I = Eng.ExplicitBadSizedVLA.begin(), |
| 495 | E = Eng.ExplicitBadSizedVLA.end(); I!=E; ++I) { |
| 496 | |
| 497 | // Determine whether this was a 'zero-sized' VLA or a VLA with an |
| 498 | // undefined size. |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 499 | ExplodedNode* N = *I; |
Ted Kremenek | 159d248 | 2008-12-09 00:44:16 +0000 | [diff] [blame] | 500 | PostStmt PS = cast<PostStmt>(N->getLocation()); |
Ted Kremenek | 5f85e17 | 2009-07-22 22:35:28 +0000 | [diff] [blame] | 501 | const DeclStmt *DS = cast<DeclStmt>(PS.getStmt()); |
Ted Kremenek | efd5994 | 2008-12-08 22:47:34 +0000 | [diff] [blame] | 502 | VarDecl* VD = cast<VarDecl>(*DS->decl_begin()); |
| 503 | QualType T = Eng.getContext().getCanonicalType(VD->getType()); |
| 504 | VariableArrayType* VT = cast<VariableArrayType>(T); |
Ted Kremenek | 159d248 | 2008-12-09 00:44:16 +0000 | [diff] [blame] | 505 | Expr* SizeExpr = VT->getSizeExpr(); |
Ted Kremenek | efd5994 | 2008-12-08 22:47:34 +0000 | [diff] [blame] | 506 | |
Ted Kremenek | 159d248 | 2008-12-09 00:44:16 +0000 | [diff] [blame] | 507 | std::string buf; |
| 508 | llvm::raw_string_ostream os(buf); |
Ted Kremenek | 5d88ff8 | 2009-04-02 02:40:26 +0000 | [diff] [blame] | 509 | os << "The expression used to specify the number of elements in the " |
| 510 | "variable-length array (VLA) '" |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 511 | << VD->getNameAsString() << "' evaluates to "; |
Ted Kremenek | 159d248 | 2008-12-09 00:44:16 +0000 | [diff] [blame] | 512 | |
Ted Kremenek | 23ec48c | 2009-06-18 23:58:37 +0000 | [diff] [blame] | 513 | bool isUndefined = N->getState()->getSVal(SizeExpr).isUndef(); |
Ted Kremenek | d49967f | 2009-04-29 21:58:13 +0000 | [diff] [blame] | 514 | |
| 515 | if (isUndefined) |
Ted Kremenek | 159d248 | 2008-12-09 00:44:16 +0000 | [diff] [blame] | 516 | os << "an undefined or garbage value."; |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 517 | else |
| 518 | os << "0. VLAs with no elements have undefined behavior."; |
Ted Kremenek | d49967f | 2009-04-29 21:58:13 +0000 | [diff] [blame] | 519 | |
| 520 | std::string shortBuf; |
| 521 | llvm::raw_string_ostream os_short(shortBuf); |
| 522 | os_short << "Variable-length array '" << VD->getNameAsString() << "' " |
Ted Kremenek | eaedfea | 2009-05-10 05:11:21 +0000 | [diff] [blame] | 523 | << (isUndefined ? "garbage value for array size" |
| 524 | : "has zero elements (undefined behavior)"); |
Ted Kremenek | 159d248 | 2008-12-09 00:44:16 +0000 | [diff] [blame] | 525 | |
Ted Kremenek | 85ac934 | 2009-05-15 05:25:09 +0000 | [diff] [blame] | 526 | ArgReport *report = new ArgReport(*this, os_short.str().c_str(), |
| 527 | os.str().c_str(), N, SizeExpr); |
| 528 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 529 | report->addRange(SizeExpr->getSourceRange()); |
| 530 | BR.EmitReport(report); |
Ted Kremenek | efd5994 | 2008-12-08 22:47:34 +0000 | [diff] [blame] | 531 | } |
| 532 | } |
Ted Kremenek | 85ac934 | 2009-05-15 05:25:09 +0000 | [diff] [blame] | 533 | |
| 534 | void registerInitialVisitors(BugReporterContext& BRC, |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 535 | const ExplodedNode* N, |
Ted Kremenek | 85ac934 | 2009-05-15 05:25:09 +0000 | [diff] [blame] | 536 | BuiltinBugReport *R) { |
| 537 | registerTrackNullOrUndefValue(BRC, static_cast<ArgReport*>(R)->getArg(), |
| 538 | N); |
| 539 | } |
Ted Kremenek | efd5994 | 2008-12-08 22:47:34 +0000 | [diff] [blame] | 540 | }; |
Zhongxing Xu | 1c0c233 | 2008-11-23 05:52:28 +0000 | [diff] [blame] | 541 | |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 542 | //===----------------------------------------------------------------------===// |
| 543 | // __attribute__(nonnull) checking |
| 544 | |
Ted Kremenek | c26a8b0 | 2009-07-22 21:46:56 +0000 | [diff] [blame] | 545 | class VISIBILITY_HIDDEN CheckAttrNonNull : |
| 546 | public CheckerVisitor<CheckAttrNonNull> { |
| 547 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 548 | BugType *BT; |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 549 | |
| 550 | public: |
Ted Kremenek | c26a8b0 | 2009-07-22 21:46:56 +0000 | [diff] [blame] | 551 | CheckAttrNonNull() : BT(0) {} |
Ted Kremenek | 3111218 | 2009-07-24 00:40:31 +0000 | [diff] [blame] | 552 | ~CheckAttrNonNull() {} |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 553 | |
Ted Kremenek | c26a8b0 | 2009-07-22 21:46:56 +0000 | [diff] [blame] | 554 | const void *getTag() { |
| 555 | static int x = 0; |
| 556 | return &x; |
| 557 | } |
| 558 | |
| 559 | void PreVisitCallExpr(CheckerContext &C, const CallExpr *CE) { |
| 560 | const GRState *state = C.getState(); |
| 561 | const GRState *originalState = state; |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 562 | |
Ted Kremenek | c26a8b0 | 2009-07-22 21:46:56 +0000 | [diff] [blame] | 563 | // Check if the callee has a 'nonnull' attribute. |
Ted Kremenek | 23ec48c | 2009-06-18 23:58:37 +0000 | [diff] [blame] | 564 | SVal X = state->getSVal(CE->getCallee()); |
Ted Kremenek | c26a8b0 | 2009-07-22 21:46:56 +0000 | [diff] [blame] | 565 | |
Zhongxing Xu | 369f447 | 2009-04-20 05:24:46 +0000 | [diff] [blame] | 566 | const FunctionDecl* FD = X.getAsFunctionDecl(); |
| 567 | if (!FD) |
Ted Kremenek | c26a8b0 | 2009-07-22 21:46:56 +0000 | [diff] [blame] | 568 | return; |
Zhongxing Xu | 369f447 | 2009-04-20 05:24:46 +0000 | [diff] [blame] | 569 | |
Ted Kremenek | c26a8b0 | 2009-07-22 21:46:56 +0000 | [diff] [blame] | 570 | const NonNullAttr* Att = FD->getAttr<NonNullAttr>(); |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 571 | if (!Att) |
Ted Kremenek | c26a8b0 | 2009-07-22 21:46:56 +0000 | [diff] [blame] | 572 | return; |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 573 | |
| 574 | // Iterate through the arguments of CE and check them for null. |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 575 | unsigned idx = 0; |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 576 | |
Ted Kremenek | c26a8b0 | 2009-07-22 21:46:56 +0000 | [diff] [blame] | 577 | for (CallExpr::const_arg_iterator I=CE->arg_begin(), E=CE->arg_end(); I!=E; |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 578 | ++I, ++idx) { |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 579 | |
Ted Kremenek | c26a8b0 | 2009-07-22 21:46:56 +0000 | [diff] [blame] | 580 | if (!Att->isNonNull(idx)) |
| 581 | continue; |
| 582 | |
Ted Kremenek | 0878007 | 2009-08-24 22:47:34 +0000 | [diff] [blame] | 583 | const SVal &V = state->getSVal(*I); |
| 584 | const DefinedSVal *DV = dyn_cast<DefinedSVal>(&V); |
| 585 | |
| 586 | if (!DV) |
| 587 | continue; |
| 588 | |
Ted Kremenek | c26a8b0 | 2009-07-22 21:46:56 +0000 | [diff] [blame] | 589 | ConstraintManager &CM = C.getConstraintManager(); |
| 590 | const GRState *stateNotNull, *stateNull; |
Ted Kremenek | 0878007 | 2009-08-24 22:47:34 +0000 | [diff] [blame] | 591 | llvm::tie(stateNotNull, stateNull) = CM.AssumeDual(state, *DV); |
Ted Kremenek | c26a8b0 | 2009-07-22 21:46:56 +0000 | [diff] [blame] | 592 | |
| 593 | if (stateNull && !stateNotNull) { |
| 594 | // Generate an error node. Check for a null node in case |
| 595 | // we cache out. |
Ted Kremenek | 592362b | 2009-08-18 01:05:30 +0000 | [diff] [blame] | 596 | if (ExplodedNode *errorNode = C.generateNode(CE, stateNull, true)) { |
Ted Kremenek | c26a8b0 | 2009-07-22 21:46:56 +0000 | [diff] [blame] | 597 | |
| 598 | // Lazily allocate the BugType object if it hasn't already been |
| 599 | // created. Ownership is transferred to the BugReporter object once |
| 600 | // the BugReport is passed to 'EmitWarning'. |
| 601 | if (!BT) |
| 602 | BT = new BugType("Argument with 'nonnull' attribute passed null", |
| 603 | "API"); |
| 604 | |
Ted Kremenek | 592362b | 2009-08-18 01:05:30 +0000 | [diff] [blame] | 605 | EnhancedBugReport *R = |
| 606 | new EnhancedBugReport(*BT, |
| 607 | "Null pointer passed as an argument to a " |
| 608 | "'nonnull' parameter", errorNode); |
Ted Kremenek | c26a8b0 | 2009-07-22 21:46:56 +0000 | [diff] [blame] | 609 | |
| 610 | // Highlight the range of the argument that was null. |
Ted Kremenek | 592362b | 2009-08-18 01:05:30 +0000 | [diff] [blame] | 611 | const Expr *arg = *I; |
| 612 | R->addRange(arg->getSourceRange()); |
| 613 | R->addVisitorCreator(registerTrackNullOrUndefValue, arg); |
Ted Kremenek | c26a8b0 | 2009-07-22 21:46:56 +0000 | [diff] [blame] | 614 | |
| 615 | // Emit the bug report. |
| 616 | C.EmitReport(R); |
| 617 | } |
| 618 | |
| 619 | // Always return. Either we cached out or we just emitted an error. |
| 620 | return; |
| 621 | } |
| 622 | |
| 623 | // If a pointer value passed the check we should assume that it is |
| 624 | // indeed not null from this point forward. |
| 625 | assert(stateNotNull); |
| 626 | state = stateNotNull; |
| 627 | } |
| 628 | |
| 629 | // If we reach here all of the arguments passed the nonnull check. |
| 630 | // If 'state' has been updated generated a new node. |
| 631 | if (state != originalState) |
Ted Kremenek | bb97722 | 2009-07-28 19:24:31 +0000 | [diff] [blame] | 632 | C.addTransition(C.generateNode(CE, state)); |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 633 | } |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 634 | }; |
| 635 | } // end anonymous namespace |
| 636 | |
Zhongxing Xu | 9a5bca3 | 2009-08-29 02:11:01 +0000 | [diff] [blame^] | 637 | // Undefined arguments checking. |
| 638 | namespace { |
| 639 | class VISIBILITY_HIDDEN CheckUndefinedArg |
| 640 | : public CheckerVisitor<CheckUndefinedArg> { |
| 641 | |
| 642 | BugType *BT; |
| 643 | |
| 644 | public: |
| 645 | CheckUndefinedArg() : BT(0) {} |
| 646 | ~CheckUndefinedArg() {} |
| 647 | |
| 648 | const void *getTag() { |
| 649 | static int x = 0; |
| 650 | return &x; |
| 651 | } |
| 652 | |
| 653 | void PreVisitCallExpr(CheckerContext &C, const CallExpr *CE); |
| 654 | }; |
| 655 | |
| 656 | void CheckUndefinedArg::PreVisitCallExpr(CheckerContext &C, const CallExpr *CE){ |
| 657 | for (CallExpr::const_arg_iterator I = CE->arg_begin(), E = CE->arg_end(); |
| 658 | I != E; ++I) { |
| 659 | if (C.getState()->getSVal(*I).isUndef()) { |
| 660 | if (ExplodedNode *ErrorNode = C.generateNode(CE, C.getState(), true)) { |
| 661 | if (!BT) |
| 662 | BT = new BugType("Uninitialized argument.", "Logic Errors."); |
| 663 | // Generate a report for this bug. |
| 664 | ArgReport *Report = new ArgReport(*BT, |
| 665 | "Pass-by-value argument in function call is undefined.", |
| 666 | ErrorNode, *I); |
| 667 | Report->addRange((*I)->getSourceRange()); |
| 668 | C.EmitReport(Report); |
| 669 | } |
| 670 | } |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | } |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 675 | //===----------------------------------------------------------------------===// |
| 676 | // Check registration. |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 677 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 678 | |
| 679 | void GRExprEngine::RegisterInternalChecks() { |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 680 | // Register internal "built-in" BugTypes with the BugReporter. These BugTypes |
| 681 | // are different than what probably many checks will do since they don't |
| 682 | // create BugReports on-the-fly but instead wait until GRExprEngine finishes |
| 683 | // analyzing a function. Generation of BugReport objects is done via a call |
| 684 | // to 'FlushReports' from BugReporter. |
| 685 | BR.Register(new NullDeref(this)); |
| 686 | BR.Register(new UndefinedDeref(this)); |
| 687 | BR.Register(new UndefBranch(this)); |
| 688 | BR.Register(new DivZero(this)); |
| 689 | BR.Register(new UndefResult(this)); |
| 690 | BR.Register(new BadCall(this)); |
| 691 | BR.Register(new RetStack(this)); |
| 692 | BR.Register(new RetUndef(this)); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 693 | BR.Register(new BadMsgExprArg(this)); |
| 694 | BR.Register(new BadReceiver(this)); |
| 695 | BR.Register(new OutOfBoundMemoryAccess(this)); |
| 696 | BR.Register(new BadSizeVLA(this)); |
Ted Kremenek | 21fe837 | 2009-02-19 04:06:22 +0000 | [diff] [blame] | 697 | BR.Register(new NilReceiverStructRet(this)); |
Ted Kremenek | 899b3de | 2009-04-08 03:07:17 +0000 | [diff] [blame] | 698 | BR.Register(new NilReceiverLargerThanVoidPtrRet(this)); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 699 | |
| 700 | // The following checks do not need to have their associated BugTypes |
| 701 | // explicitly registered with the BugReporter. If they issue any BugReports, |
| 702 | // their associated BugType will get registered with the BugReporter |
| 703 | // automatically. Note that the check itself is owned by the GRExprEngine |
| 704 | // object. |
Ted Kremenek | c26a8b0 | 2009-07-22 21:46:56 +0000 | [diff] [blame] | 705 | registerCheck(new CheckAttrNonNull()); |
Zhongxing Xu | 9a5bca3 | 2009-08-29 02:11:01 +0000 | [diff] [blame^] | 706 | registerCheck(new CheckUndefinedArg()); |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 707 | } |