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 | dd986cc | 2009-05-07 00:45:33 +0000 | [diff] [blame] | 17 | #include "clang/Analysis/PathDiagnostic.h" |
Ted Kremenek | 8aed806 | 2008-10-31 00:13:20 +0000 | [diff] [blame] | 18 | #include "clang/Basic/SourceManager.h" |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Compiler.h" |
Ted Kremenek | ad51a60 | 2008-10-31 00:18:30 +0000 | [diff] [blame] | 20 | #include "llvm/Support/raw_ostream.h" |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 21 | |
| 22 | using namespace clang; |
| 23 | |
| 24 | //===----------------------------------------------------------------------===// |
| 25 | // Utility functions. |
| 26 | //===----------------------------------------------------------------------===// |
| 27 | |
| 28 | template <typename ITERATOR> inline |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 29 | ExplodedNode<GRState>* GetNode(ITERATOR I) { |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 30 | return *I; |
| 31 | } |
| 32 | |
| 33 | template <> inline |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 34 | ExplodedNode<GRState>* GetNode(GRExprEngine::undef_arg_iterator I) { |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 35 | return I->first; |
| 36 | } |
| 37 | |
| 38 | //===----------------------------------------------------------------------===// |
Ted Kremenek | dd986cc | 2009-05-07 00:45:33 +0000 | [diff] [blame] | 39 | // Forward declarations for bug reporter visitors. |
| 40 | //===----------------------------------------------------------------------===// |
| 41 | |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame^] | 42 | static void registerTrackNullOrUndefValue(BugReporterContext& BRC, |
Ted Kremenek | dd986cc | 2009-05-07 00:45:33 +0000 | [diff] [blame] | 43 | const ExplodedNode<GRState>* N); |
| 44 | |
| 45 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 46 | // Bug Descriptions. |
| 47 | //===----------------------------------------------------------------------===// |
| 48 | |
| 49 | namespace { |
Ted Kremenek | dd986cc | 2009-05-07 00:45:33 +0000 | [diff] [blame] | 50 | |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame^] | 51 | class VISIBILITY_HIDDEN BuiltinBugReport : public RangedBugReport { |
Ted Kremenek | dd986cc | 2009-05-07 00:45:33 +0000 | [diff] [blame] | 52 | public: |
| 53 | BuiltinBugReport(BugType& bt, const char* desc, |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame^] | 54 | ExplodedNode<GRState> *n) |
| 55 | : RangedBugReport(bt, desc, n) {} |
Ted Kremenek | dd986cc | 2009-05-07 00:45:33 +0000 | [diff] [blame] | 56 | |
| 57 | void registerInitialVisitors(BugReporterContext& BRC, |
| 58 | const ExplodedNode<GRState>* N); |
| 59 | }; |
| 60 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 61 | class VISIBILITY_HIDDEN BuiltinBug : public BugType { |
| 62 | GRExprEngine &Eng; |
Ted Kremenek | 159d248 | 2008-12-09 00:44:16 +0000 | [diff] [blame] | 63 | protected: |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 64 | const std::string desc; |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 65 | public: |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 66 | BuiltinBug(GRExprEngine *eng, const char* n, const char* d) |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame^] | 67 | : BugType(n, "Logic errors"), Eng(*eng), desc(d) {} |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 68 | |
| 69 | BuiltinBug(GRExprEngine *eng, const char* n) |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame^] | 70 | : BugType(n, "Logic errors"), Eng(*eng), desc(n) {} |
Ted Kremenek | 22bda88 | 2008-07-31 20:31:27 +0000 | [diff] [blame] | 71 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 72 | virtual void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) = 0; |
| 73 | |
| 74 | void FlushReports(BugReporter& BR) { FlushReportsImpl(BR, Eng); } |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 75 | |
Ted Kremenek | dd986cc | 2009-05-07 00:45:33 +0000 | [diff] [blame] | 76 | virtual void registerInitialVisitors(BugReporterContext& BRC, |
| 77 | const ExplodedNode<GRState>* N, |
| 78 | BuiltinBugReport *R) {} |
| 79 | |
| 80 | template <typename ITER> void Emit(BugReporter& BR, ITER I, ITER E); |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 81 | }; |
| 82 | |
Ted Kremenek | dd986cc | 2009-05-07 00:45:33 +0000 | [diff] [blame] | 83 | |
| 84 | template <typename ITER> |
| 85 | void BuiltinBug::Emit(BugReporter& BR, ITER I, ITER E) { |
| 86 | for (; I != E; ++I) BR.EmitReport(new BuiltinBugReport(*this, desc.c_str(), |
| 87 | GetNode(I))); |
| 88 | } |
| 89 | |
| 90 | void BuiltinBugReport::registerInitialVisitors(BugReporterContext& BRC, |
| 91 | const ExplodedNode<GRState>* N) { |
| 92 | static_cast<BuiltinBug&>(getBugType()).registerInitialVisitors(BRC, N, this); |
| 93 | } |
| 94 | |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 95 | class VISIBILITY_HIDDEN NullDeref : public BuiltinBug { |
| 96 | public: |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 97 | NullDeref(GRExprEngine* eng) |
Ted Kremenek | 0fa9654 | 2009-04-07 04:54:31 +0000 | [diff] [blame] | 98 | : BuiltinBug(eng,"Null dereference", "Dereference of null pointer") {} |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 99 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 100 | void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) { |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 101 | Emit(BR, Eng.null_derefs_begin(), Eng.null_derefs_end()); |
| 102 | } |
Ted Kremenek | dd986cc | 2009-05-07 00:45:33 +0000 | [diff] [blame] | 103 | |
| 104 | void registerInitialVisitors(BugReporterContext& BRC, |
| 105 | const ExplodedNode<GRState>* N, |
| 106 | BuiltinBugReport *R) { |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame^] | 107 | registerTrackNullOrUndefValue(BRC, N); |
Ted Kremenek | dd986cc | 2009-05-07 00:45:33 +0000 | [diff] [blame] | 108 | } |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 109 | }; |
| 110 | |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame^] | 111 | class VISIBILITY_HIDDEN NilReceiverStructRet : public BuiltinBug { |
Ted Kremenek | 21fe837 | 2009-02-19 04:06:22 +0000 | [diff] [blame] | 112 | public: |
| 113 | NilReceiverStructRet(GRExprEngine* eng) : |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame^] | 114 | BuiltinBug(eng, "'nil' receiver with struct return type") {} |
Ted Kremenek | 21fe837 | 2009-02-19 04:06:22 +0000 | [diff] [blame] | 115 | |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame^] | 116 | void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) { |
Ted Kremenek | 21fe837 | 2009-02-19 04:06:22 +0000 | [diff] [blame] | 117 | for (GRExprEngine::nil_receiver_struct_ret_iterator |
| 118 | I=Eng.nil_receiver_struct_ret_begin(), |
| 119 | E=Eng.nil_receiver_struct_ret_end(); I!=E; ++I) { |
| 120 | |
| 121 | std::string sbuf; |
| 122 | llvm::raw_string_ostream os(sbuf); |
| 123 | PostStmt P = cast<PostStmt>((*I)->getLocation()); |
| 124 | ObjCMessageExpr *ME = cast<ObjCMessageExpr>(P.getStmt()); |
| 125 | os << "The receiver in the message expression is 'nil' and results in the" |
| 126 | " returned value (of type '" |
| 127 | << ME->getType().getAsString() |
| 128 | << "') to be garbage or otherwise undefined."; |
| 129 | |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame^] | 130 | BuiltinBugReport *R = new BuiltinBugReport(*this, os.str().c_str(), *I); |
Ted Kremenek | 21fe837 | 2009-02-19 04:06:22 +0000 | [diff] [blame] | 131 | R->addRange(ME->getReceiver()->getSourceRange()); |
| 132 | BR.EmitReport(R); |
| 133 | } |
| 134 | } |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame^] | 135 | |
| 136 | void registerInitialVisitors(BugReporterContext& BRC, |
| 137 | const ExplodedNode<GRState>* N, |
| 138 | BuiltinBugReport *R) { |
| 139 | registerTrackNullOrUndefValue(BRC, N); |
| 140 | } |
Ted Kremenek | 21fe837 | 2009-02-19 04:06:22 +0000 | [diff] [blame] | 141 | }; |
Ted Kremenek | 899b3de | 2009-04-08 03:07:17 +0000 | [diff] [blame] | 142 | |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame^] | 143 | class VISIBILITY_HIDDEN NilReceiverLargerThanVoidPtrRet : public BuiltinBug { |
Ted Kremenek | 899b3de | 2009-04-08 03:07:17 +0000 | [diff] [blame] | 144 | public: |
| 145 | NilReceiverLargerThanVoidPtrRet(GRExprEngine* eng) : |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame^] | 146 | BuiltinBug(eng, |
| 147 | "'nil' receiver with return type larger than sizeof(void *)") {} |
Ted Kremenek | 899b3de | 2009-04-08 03:07:17 +0000 | [diff] [blame] | 148 | |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame^] | 149 | void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) { |
Ted Kremenek | 899b3de | 2009-04-08 03:07:17 +0000 | [diff] [blame] | 150 | for (GRExprEngine::nil_receiver_larger_than_voidptr_ret_iterator |
| 151 | I=Eng.nil_receiver_larger_than_voidptr_ret_begin(), |
| 152 | E=Eng.nil_receiver_larger_than_voidptr_ret_end(); I!=E; ++I) { |
| 153 | |
| 154 | std::string sbuf; |
| 155 | llvm::raw_string_ostream os(sbuf); |
| 156 | PostStmt P = cast<PostStmt>((*I)->getLocation()); |
| 157 | ObjCMessageExpr *ME = cast<ObjCMessageExpr>(P.getStmt()); |
| 158 | os << "The receiver in the message expression is 'nil' and results in the" |
| 159 | " returned value (of type '" |
| 160 | << ME->getType().getAsString() |
| 161 | << "' and of size " |
| 162 | << Eng.getContext().getTypeSize(ME->getType()) / 8 |
| 163 | << " bytes) to be garbage or otherwise undefined."; |
| 164 | |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame^] | 165 | BuiltinBugReport *R = new BuiltinBugReport(*this, os.str().c_str(), *I); |
Ted Kremenek | 899b3de | 2009-04-08 03:07:17 +0000 | [diff] [blame] | 166 | R->addRange(ME->getReceiver()->getSourceRange()); |
| 167 | BR.EmitReport(R); |
| 168 | } |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame^] | 169 | } |
| 170 | void registerInitialVisitors(BugReporterContext& BRC, |
| 171 | const ExplodedNode<GRState>* N, |
| 172 | BuiltinBugReport *R) { |
| 173 | registerTrackNullOrUndefValue(BRC, N); |
Ted Kremenek | 899b3de | 2009-04-08 03:07:17 +0000 | [diff] [blame] | 174 | } |
| 175 | }; |
Ted Kremenek | 21fe837 | 2009-02-19 04:06:22 +0000 | [diff] [blame] | 176 | |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 177 | class VISIBILITY_HIDDEN UndefinedDeref : public BuiltinBug { |
| 178 | public: |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 179 | UndefinedDeref(GRExprEngine* eng) |
Ted Kremenek | 17a8e07 | 2009-03-01 05:43:22 +0000 | [diff] [blame] | 180 | : BuiltinBug(eng,"Dereference of undefined pointer value") {} |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 181 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 182 | void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) { |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 183 | Emit(BR, Eng.undef_derefs_begin(), Eng.undef_derefs_end()); |
| 184 | } |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame^] | 185 | |
| 186 | void registerInitialVisitors(BugReporterContext& BRC, |
| 187 | const ExplodedNode<GRState>* N, |
| 188 | BuiltinBugReport *R) { |
| 189 | registerTrackNullOrUndefValue(BRC, N); |
| 190 | } |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 191 | }; |
| 192 | |
| 193 | class VISIBILITY_HIDDEN DivZero : public BuiltinBug { |
| 194 | public: |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 195 | DivZero(GRExprEngine* eng) |
Ted Kremenek | 5d88ff8 | 2009-04-02 02:40:26 +0000 | [diff] [blame] | 196 | : BuiltinBug(eng,"Division-by-zero", |
| 197 | "Division by zero or undefined value.") {} |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 198 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 199 | void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) { |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 200 | Emit(BR, Eng.explicit_bad_divides_begin(), Eng.explicit_bad_divides_end()); |
| 201 | } |
| 202 | }; |
| 203 | |
| 204 | class VISIBILITY_HIDDEN UndefResult : public BuiltinBug { |
| 205 | public: |
Ted Kremenek | 5d88ff8 | 2009-04-02 02:40:26 +0000 | [diff] [blame] | 206 | UndefResult(GRExprEngine* eng) : BuiltinBug(eng,"Undefined result", |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 207 | "Result of operation is undefined.") {} |
| 208 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 209 | void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) { |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 210 | Emit(BR, Eng.undef_results_begin(), Eng.undef_results_end()); |
| 211 | } |
| 212 | }; |
| 213 | |
| 214 | class VISIBILITY_HIDDEN BadCall : public BuiltinBug { |
| 215 | public: |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 216 | BadCall(GRExprEngine *eng) |
Ted Kremenek | 5d88ff8 | 2009-04-02 02:40:26 +0000 | [diff] [blame] | 217 | : BuiltinBug(eng, "Invalid function call", |
Ted Kremenek | 17a8e07 | 2009-03-01 05:43:22 +0000 | [diff] [blame] | 218 | "Called function pointer is a null or undefined pointer value") {} |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 219 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 220 | void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) { |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 221 | Emit(BR, Eng.bad_calls_begin(), Eng.bad_calls_end()); |
| 222 | } |
| 223 | }; |
| 224 | |
| 225 | class VISIBILITY_HIDDEN BadArg : public BuiltinBug { |
| 226 | public: |
Ted Kremenek | 5d88ff8 | 2009-04-02 02:40:26 +0000 | [diff] [blame] | 227 | BadArg(GRExprEngine* eng) : BuiltinBug(eng,"Uninitialized argument", |
Ted Kremenek | 17a8e07 | 2009-03-01 05:43:22 +0000 | [diff] [blame] | 228 | "Pass-by-value argument in function call is undefined.") {} |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 229 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 230 | BadArg(GRExprEngine* eng, const char* d) |
Ted Kremenek | 5d88ff8 | 2009-04-02 02:40:26 +0000 | [diff] [blame] | 231 | : BuiltinBug(eng,"Uninitialized argument", d) {} |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 232 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 233 | void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) { |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 234 | for (GRExprEngine::UndefArgsTy::iterator I = Eng.undef_arg_begin(), |
| 235 | E = Eng.undef_arg_end(); I!=E; ++I) { |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 236 | // Generate a report for this bug. |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 237 | RangedBugReport *report = new RangedBugReport(*this, desc.c_str(), |
| 238 | I->first); |
| 239 | report->addRange(I->second->getSourceRange()); |
| 240 | BR.EmitReport(report); |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 241 | } |
| 242 | } |
| 243 | }; |
| 244 | |
| 245 | class VISIBILITY_HIDDEN BadMsgExprArg : public BadArg { |
| 246 | public: |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 247 | BadMsgExprArg(GRExprEngine* eng) |
Ted Kremenek | 5d88ff8 | 2009-04-02 02:40:26 +0000 | [diff] [blame] | 248 | : BadArg(eng,"Pass-by-value argument in message expression is undefined"){} |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 249 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 250 | void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) { |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 251 | for (GRExprEngine::UndefArgsTy::iterator I=Eng.msg_expr_undef_arg_begin(), |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 252 | E = Eng.msg_expr_undef_arg_end(); I!=E; ++I) { |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 253 | // Generate a report for this bug. |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame^] | 254 | BuiltinBugReport *report = new BuiltinBugReport(*this, desc.c_str(), |
| 255 | I->first); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 256 | report->addRange(I->second->getSourceRange()); |
| 257 | BR.EmitReport(report); |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 258 | } |
| 259 | } |
| 260 | }; |
| 261 | |
| 262 | class VISIBILITY_HIDDEN BadReceiver : public BuiltinBug { |
| 263 | public: |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 264 | BadReceiver(GRExprEngine* eng) |
Ted Kremenek | 5d88ff8 | 2009-04-02 02:40:26 +0000 | [diff] [blame] | 265 | : BuiltinBug(eng,"Uninitialized receiver", |
| 266 | "Receiver in message expression is an uninitialized value") {} |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 267 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 268 | void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) { |
Ted Kremenek | efd5994 | 2008-12-08 22:47:34 +0000 | [diff] [blame] | 269 | for (GRExprEngine::ErrorNodes::iterator I=Eng.undef_receivers_begin(), |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 270 | End = Eng.undef_receivers_end(); I!=End; ++I) { |
| 271 | |
| 272 | // Generate a report for this bug. |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame^] | 273 | BuiltinBugReport *report = new BuiltinBugReport(*this, desc.c_str(), *I); |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 274 | ExplodedNode<GRState>* N = *I; |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 275 | Stmt *S = cast<PostStmt>(N->getLocation()).getStmt(); |
| 276 | Expr* E = cast<ObjCMessageExpr>(S)->getReceiver(); |
| 277 | assert (E && "Receiver cannot be NULL"); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 278 | report->addRange(E->getSourceRange()); |
| 279 | BR.EmitReport(report); |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame^] | 280 | } |
| 281 | } |
| 282 | |
| 283 | void registerInitialVisitors(BugReporterContext& BRC, |
| 284 | const ExplodedNode<GRState>* N, |
| 285 | BuiltinBugReport *R) { |
| 286 | registerTrackNullOrUndefValue(BRC, N); |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 287 | } |
| 288 | }; |
Ted Kremenek | 5917d78 | 2008-11-21 00:27:44 +0000 | [diff] [blame] | 289 | |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 290 | class VISIBILITY_HIDDEN RetStack : public BuiltinBug { |
| 291 | public: |
Ted Kremenek | 17a8e07 | 2009-03-01 05:43:22 +0000 | [diff] [blame] | 292 | RetStack(GRExprEngine* eng) |
Ted Kremenek | 5d88ff8 | 2009-04-02 02:40:26 +0000 | [diff] [blame] | 293 | : BuiltinBug(eng, "Return of address to stack-allocated memory") {} |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 294 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 295 | void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) { |
Ted Kremenek | b7714b2 | 2008-07-30 17:49:12 +0000 | [diff] [blame] | 296 | for (GRExprEngine::ret_stackaddr_iterator I=Eng.ret_stackaddr_begin(), |
| 297 | End = Eng.ret_stackaddr_end(); I!=End; ++I) { |
Ted Kremenek | 22bda88 | 2008-07-31 20:31:27 +0000 | [diff] [blame] | 298 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 299 | ExplodedNode<GRState>* N = *I; |
Ted Kremenek | b7714b2 | 2008-07-30 17:49:12 +0000 | [diff] [blame] | 300 | Stmt *S = cast<PostStmt>(N->getLocation()).getStmt(); |
| 301 | Expr* E = cast<ReturnStmt>(S)->getRetValue(); |
| 302 | assert (E && "Return expression cannot be NULL"); |
Ted Kremenek | 22bda88 | 2008-07-31 20:31:27 +0000 | [diff] [blame] | 303 | |
| 304 | // Get the value associated with E. |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 305 | loc::MemRegionVal V = |
| 306 | cast<loc::MemRegionVal>(Eng.getStateManager().GetSVal(N->getState(), |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 307 | E)); |
Ted Kremenek | 22bda88 | 2008-07-31 20:31:27 +0000 | [diff] [blame] | 308 | |
| 309 | // Generate a report for this bug. |
Ted Kremenek | ad51a60 | 2008-10-31 00:18:30 +0000 | [diff] [blame] | 310 | std::string buf; |
| 311 | llvm::raw_string_ostream os(buf); |
Ted Kremenek | 8aed806 | 2008-10-31 00:13:20 +0000 | [diff] [blame] | 312 | SourceRange R; |
Ted Kremenek | 22bda88 | 2008-07-31 20:31:27 +0000 | [diff] [blame] | 313 | |
Ted Kremenek | 8aed806 | 2008-10-31 00:13:20 +0000 | [diff] [blame] | 314 | // Check if the region is a compound literal. |
| 315 | if (const CompoundLiteralRegion* CR = |
| 316 | dyn_cast<CompoundLiteralRegion>(V.getRegion())) { |
| 317 | |
| 318 | const CompoundLiteralExpr* CL = CR->getLiteralExpr(); |
| 319 | os << "Address of stack memory associated with a compound literal " |
| 320 | "declared on line " |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 321 | << BR.getSourceManager() |
| 322 | .getInstantiationLineNumber(CL->getLocStart()) |
Ted Kremenek | 8aed806 | 2008-10-31 00:13:20 +0000 | [diff] [blame] | 323 | << " returned."; |
| 324 | |
| 325 | R = CL->getSourceRange(); |
| 326 | } |
Ted Kremenek | de8cd19 | 2008-11-02 00:35:25 +0000 | [diff] [blame] | 327 | else if (const AllocaRegion* AR = dyn_cast<AllocaRegion>(V.getRegion())) { |
| 328 | const Expr* ARE = AR->getExpr(); |
| 329 | SourceLocation L = ARE->getLocStart(); |
| 330 | R = ARE->getSourceRange(); |
| 331 | |
| 332 | os << "Address of stack memory allocated by call to alloca() on line " |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 333 | << BR.getSourceManager().getInstantiationLineNumber(L) |
Ted Kremenek | de8cd19 | 2008-11-02 00:35:25 +0000 | [diff] [blame] | 334 | << " returned."; |
| 335 | } |
Ted Kremenek | 8aed806 | 2008-10-31 00:13:20 +0000 | [diff] [blame] | 336 | else { |
| 337 | os << "Address of stack memory associated with local variable '" |
| 338 | << V.getRegion()->getString() << "' returned."; |
| 339 | } |
Ted Kremenek | 22bda88 | 2008-07-31 20:31:27 +0000 | [diff] [blame] | 340 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 341 | RangedBugReport *report = new RangedBugReport(*this, os.str().c_str(), N); |
| 342 | report->addRange(E->getSourceRange()); |
| 343 | if (R.isValid()) report->addRange(R); |
| 344 | BR.EmitReport(report); |
Ted Kremenek | b7714b2 | 2008-07-30 17:49:12 +0000 | [diff] [blame] | 345 | } |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 346 | } |
| 347 | }; |
Ted Kremenek | 5917d78 | 2008-11-21 00:27:44 +0000 | [diff] [blame] | 348 | |
| 349 | class VISIBILITY_HIDDEN RetUndef : public BuiltinBug { |
| 350 | public: |
Ted Kremenek | 5d88ff8 | 2009-04-02 02:40:26 +0000 | [diff] [blame] | 351 | RetUndef(GRExprEngine* eng) : BuiltinBug(eng, "Uninitialized return value", |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame^] | 352 | "Uninitialized or undefined value returned to caller.") {} |
Ted Kremenek | 5917d78 | 2008-11-21 00:27:44 +0000 | [diff] [blame] | 353 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 354 | void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) { |
Ted Kremenek | 5917d78 | 2008-11-21 00:27:44 +0000 | [diff] [blame] | 355 | Emit(BR, Eng.ret_undef_begin(), Eng.ret_undef_end()); |
| 356 | } |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame^] | 357 | |
| 358 | void registerInitialVisitors(BugReporterContext& BRC, |
| 359 | const ExplodedNode<GRState>* N, |
| 360 | BuiltinBugReport *R) { |
| 361 | registerTrackNullOrUndefValue(BRC, N); |
| 362 | } |
Ted Kremenek | 5917d78 | 2008-11-21 00:27:44 +0000 | [diff] [blame] | 363 | }; |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 364 | |
| 365 | class VISIBILITY_HIDDEN UndefBranch : public BuiltinBug { |
| 366 | struct VISIBILITY_HIDDEN FindUndefExpr { |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 367 | GRStateManager& VM; |
| 368 | const GRState* St; |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 369 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 370 | FindUndefExpr(GRStateManager& V, const GRState* S) : VM(V), St(S) {} |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 371 | |
Ted Kremenek | b7714b2 | 2008-07-30 17:49:12 +0000 | [diff] [blame] | 372 | Expr* FindExpr(Expr* Ex) { |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 373 | if (!MatchesCriteria(Ex)) |
Ted Kremenek | b7714b2 | 2008-07-30 17:49:12 +0000 | [diff] [blame] | 374 | return 0; |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 375 | |
Ted Kremenek | b7714b2 | 2008-07-30 17:49:12 +0000 | [diff] [blame] | 376 | 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] | 377 | if (Expr* ExI = dyn_cast_or_null<Expr>(*I)) { |
| 378 | Expr* E2 = FindExpr(ExI); |
| 379 | if (E2) return E2; |
| 380 | } |
| 381 | |
| 382 | return Ex; |
| 383 | } |
| 384 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 385 | bool MatchesCriteria(Expr* Ex) { return VM.GetSVal(St, Ex).isUndef(); } |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 386 | }; |
| 387 | |
| 388 | public: |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 389 | UndefBranch(GRExprEngine *eng) |
Ted Kremenek | 5d88ff8 | 2009-04-02 02:40:26 +0000 | [diff] [blame] | 390 | : BuiltinBug(eng,"Use of uninitialized value", |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 391 | "Branch condition evaluates to an uninitialized value.") {} |
| 392 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 393 | void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) { |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 394 | for (GRExprEngine::undef_branch_iterator I=Eng.undef_branches_begin(), |
| 395 | E=Eng.undef_branches_end(); I!=E; ++I) { |
| 396 | |
| 397 | // What's going on here: we want to highlight the subexpression of the |
| 398 | // condition that is the most likely source of the "uninitialized |
| 399 | // branch condition." We do a recursive walk of the condition's |
| 400 | // subexpressions and roughly look for the most nested subexpression |
| 401 | // that binds to Undefined. We then highlight that expression's range. |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 402 | BlockEdge B = cast<BlockEdge>((*I)->getLocation()); |
| 403 | Expr* Ex = cast<Expr>(B.getSrc()->getTerminatorCondition()); |
| 404 | assert (Ex && "Block must have a terminator."); |
| 405 | |
| 406 | // Get the predecessor node and check if is a PostStmt with the Stmt |
| 407 | // being the terminator condition. We want to inspect the state |
| 408 | // of that node instead because it will contain main information about |
| 409 | // the subexpressions. |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 410 | assert (!(*I)->pred_empty()); |
| 411 | |
| 412 | // Note: any predecessor will do. They should have identical state, |
| 413 | // since all the BlockEdge did was act as an error sink since the value |
| 414 | // had to already be undefined. |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 415 | ExplodedNode<GRState> *N = *(*I)->pred_begin(); |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 416 | ProgramPoint P = N->getLocation(); |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 417 | const GRState* St = (*I)->getState(); |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 418 | |
| 419 | if (PostStmt* PS = dyn_cast<PostStmt>(&P)) |
| 420 | if (PS->getStmt() == Ex) |
| 421 | St = N->getState(); |
| 422 | |
| 423 | FindUndefExpr FindIt(Eng.getStateManager(), St); |
| 424 | Ex = FindIt.FindExpr(Ex); |
| 425 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 426 | RangedBugReport *R = new RangedBugReport(*this, desc.c_str(), *I); |
| 427 | R->addRange(Ex->getSourceRange()); |
| 428 | BR.EmitReport(R); |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 429 | } |
| 430 | } |
| 431 | }; |
| 432 | |
Zhongxing Xu | 1c0c233 | 2008-11-23 05:52:28 +0000 | [diff] [blame] | 433 | class VISIBILITY_HIDDEN OutOfBoundMemoryAccess : public BuiltinBug { |
| 434 | public: |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 435 | OutOfBoundMemoryAccess(GRExprEngine* eng) |
Ted Kremenek | 5d88ff8 | 2009-04-02 02:40:26 +0000 | [diff] [blame] | 436 | : BuiltinBug(eng,"Out-of-bounds memory access", |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 437 | "Load or store into an out-of-bound memory position.") {} |
Zhongxing Xu | 1c0c233 | 2008-11-23 05:52:28 +0000 | [diff] [blame] | 438 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 439 | void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) { |
Zhongxing Xu | 1c0c233 | 2008-11-23 05:52:28 +0000 | [diff] [blame] | 440 | Emit(BR, Eng.explicit_oob_memacc_begin(), Eng.explicit_oob_memacc_end()); |
| 441 | } |
| 442 | }; |
Ted Kremenek | efd5994 | 2008-12-08 22:47:34 +0000 | [diff] [blame] | 443 | |
Ted Kremenek | 159d248 | 2008-12-09 00:44:16 +0000 | [diff] [blame] | 444 | class VISIBILITY_HIDDEN BadSizeVLA : public BuiltinBug { |
Ted Kremenek | efd5994 | 2008-12-08 22:47:34 +0000 | [diff] [blame] | 445 | public: |
Ted Kremenek | 5d88ff8 | 2009-04-02 02:40:26 +0000 | [diff] [blame] | 446 | BadSizeVLA(GRExprEngine* eng) : |
| 447 | BuiltinBug(eng, "Bad variable-length array (VLA) size") {} |
Ted Kremenek | efd5994 | 2008-12-08 22:47:34 +0000 | [diff] [blame] | 448 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 449 | void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) { |
Ted Kremenek | efd5994 | 2008-12-08 22:47:34 +0000 | [diff] [blame] | 450 | for (GRExprEngine::ErrorNodes::iterator |
Ted Kremenek | 159d248 | 2008-12-09 00:44:16 +0000 | [diff] [blame] | 451 | I = Eng.ExplicitBadSizedVLA.begin(), |
| 452 | E = Eng.ExplicitBadSizedVLA.end(); I!=E; ++I) { |
| 453 | |
| 454 | // Determine whether this was a 'zero-sized' VLA or a VLA with an |
| 455 | // undefined size. |
| 456 | GRExprEngine::NodeTy* N = *I; |
| 457 | PostStmt PS = cast<PostStmt>(N->getLocation()); |
Ted Kremenek | efd5994 | 2008-12-08 22:47:34 +0000 | [diff] [blame] | 458 | DeclStmt *DS = cast<DeclStmt>(PS.getStmt()); |
| 459 | VarDecl* VD = cast<VarDecl>(*DS->decl_begin()); |
| 460 | QualType T = Eng.getContext().getCanonicalType(VD->getType()); |
| 461 | VariableArrayType* VT = cast<VariableArrayType>(T); |
Ted Kremenek | 159d248 | 2008-12-09 00:44:16 +0000 | [diff] [blame] | 462 | Expr* SizeExpr = VT->getSizeExpr(); |
Ted Kremenek | efd5994 | 2008-12-08 22:47:34 +0000 | [diff] [blame] | 463 | |
Ted Kremenek | 159d248 | 2008-12-09 00:44:16 +0000 | [diff] [blame] | 464 | std::string buf; |
| 465 | llvm::raw_string_ostream os(buf); |
Ted Kremenek | 5d88ff8 | 2009-04-02 02:40:26 +0000 | [diff] [blame] | 466 | os << "The expression used to specify the number of elements in the " |
| 467 | "variable-length array (VLA) '" |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 468 | << VD->getNameAsString() << "' evaluates to "; |
Ted Kremenek | 159d248 | 2008-12-09 00:44:16 +0000 | [diff] [blame] | 469 | |
Ted Kremenek | d49967f | 2009-04-29 21:58:13 +0000 | [diff] [blame] | 470 | bool isUndefined = Eng.getStateManager().GetSVal(N->getState(), |
| 471 | SizeExpr).isUndef(); |
| 472 | |
| 473 | if (isUndefined) |
Ted Kremenek | 159d248 | 2008-12-09 00:44:16 +0000 | [diff] [blame] | 474 | os << "an undefined or garbage value."; |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 475 | else |
| 476 | os << "0. VLAs with no elements have undefined behavior."; |
Ted Kremenek | d49967f | 2009-04-29 21:58:13 +0000 | [diff] [blame] | 477 | |
| 478 | std::string shortBuf; |
| 479 | llvm::raw_string_ostream os_short(shortBuf); |
| 480 | os_short << "Variable-length array '" << VD->getNameAsString() << "' " |
Ted Kremenek | eaedfea | 2009-05-10 05:11:21 +0000 | [diff] [blame] | 481 | << (isUndefined ? "garbage value for array size" |
| 482 | : "has zero elements (undefined behavior)"); |
Ted Kremenek | 159d248 | 2008-12-09 00:44:16 +0000 | [diff] [blame] | 483 | |
Ted Kremenek | d49967f | 2009-04-29 21:58:13 +0000 | [diff] [blame] | 484 | RangedBugReport *report = new RangedBugReport(*this, |
| 485 | os_short.str().c_str(), |
| 486 | os.str().c_str(), N); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 487 | report->addRange(SizeExpr->getSourceRange()); |
| 488 | BR.EmitReport(report); |
Ted Kremenek | efd5994 | 2008-12-08 22:47:34 +0000 | [diff] [blame] | 489 | } |
| 490 | } |
| 491 | }; |
Zhongxing Xu | 1c0c233 | 2008-11-23 05:52:28 +0000 | [diff] [blame] | 492 | |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 493 | //===----------------------------------------------------------------------===// |
| 494 | // __attribute__(nonnull) checking |
| 495 | |
| 496 | class VISIBILITY_HIDDEN CheckAttrNonNull : public GRSimpleAPICheck { |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 497 | BugType *BT; |
| 498 | BugReporter &BR; |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 499 | |
| 500 | public: |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 501 | CheckAttrNonNull(BugReporter &br) : BT(0), BR(br) {} |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 502 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 503 | virtual bool Audit(ExplodedNode<GRState>* N, GRStateManager& VMgr) { |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 504 | CallExpr* CE = cast<CallExpr>(cast<PostStmt>(N->getLocation()).getStmt()); |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 505 | const GRState* state = N->getState(); |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 506 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 507 | SVal X = VMgr.GetSVal(state, CE->getCallee()); |
Zhongxing Xu | 369f447 | 2009-04-20 05:24:46 +0000 | [diff] [blame] | 508 | |
| 509 | const FunctionDecl* FD = X.getAsFunctionDecl(); |
| 510 | if (!FD) |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 511 | return false; |
Zhongxing Xu | 369f447 | 2009-04-20 05:24:46 +0000 | [diff] [blame] | 512 | |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 513 | const NonNullAttr* Att = FD->getAttr<NonNullAttr>(); |
| 514 | |
| 515 | if (!Att) |
| 516 | return false; |
| 517 | |
| 518 | // Iterate through the arguments of CE and check them for null. |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 519 | unsigned idx = 0; |
| 520 | bool hasError = false; |
| 521 | |
| 522 | for (CallExpr::arg_iterator I=CE->arg_begin(), E=CE->arg_end(); I!=E; |
| 523 | ++I, ++idx) { |
| 524 | |
| 525 | if (!VMgr.isEqual(state, *I, 0) || !Att->isNonNull(idx)) |
| 526 | continue; |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 527 | |
| 528 | // Lazily allocate the BugType object if it hasn't already been created. |
| 529 | // Ownership is transferred to the BugReporter object once the BugReport |
| 530 | // is passed to 'EmitWarning'. |
Ted Kremenek | 5d88ff8 | 2009-04-02 02:40:26 +0000 | [diff] [blame] | 531 | if (!BT) BT = |
| 532 | new BugType("Argument with 'nonnull' attribute passed null", "API"); |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 533 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 534 | RangedBugReport *R = new RangedBugReport(*BT, |
| 535 | "Null pointer passed as an argument to a " |
| 536 | "'nonnull' parameter", N); |
| 537 | |
| 538 | R->addRange((*I)->getSourceRange()); |
| 539 | BR.EmitReport(R); |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 540 | hasError = true; |
| 541 | } |
| 542 | |
| 543 | return hasError; |
| 544 | } |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 545 | }; |
| 546 | } // end anonymous namespace |
| 547 | |
| 548 | //===----------------------------------------------------------------------===// |
Ted Kremenek | dd986cc | 2009-05-07 00:45:33 +0000 | [diff] [blame] | 549 | // Definitions for bug reporter visitors. |
| 550 | //===----------------------------------------------------------------------===// |
| 551 | |
| 552 | namespace { |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame^] | 553 | class VISIBILITY_HIDDEN FindLastStoreBRVisitor : public BugReporterVisitor { |
Ted Kremenek | 7704a33 | 2009-05-07 21:49:45 +0000 | [diff] [blame] | 554 | const MemRegion *R; |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame^] | 555 | SVal V; |
| 556 | bool satisfied; |
| 557 | const ExplodedNode<GRState> *StoreSite; |
Ted Kremenek | 7704a33 | 2009-05-07 21:49:45 +0000 | [diff] [blame] | 558 | public: |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame^] | 559 | FindLastStoreBRVisitor(SVal v, const MemRegion *r) |
| 560 | : R(r), V(v), satisfied(false), StoreSite(0) {} |
| 561 | |
Ted Kremenek | 7704a33 | 2009-05-07 21:49:45 +0000 | [diff] [blame] | 562 | PathDiagnosticPiece* VisitNode(const ExplodedNode<GRState> *N, |
| 563 | const ExplodedNode<GRState> *PrevN, |
| 564 | BugReporterContext& BRC) { |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame^] | 565 | |
| 566 | if (satisfied) |
Ted Kremenek | 7704a33 | 2009-05-07 21:49:45 +0000 | [diff] [blame] | 567 | return NULL; |
Ted Kremenek | 7704a33 | 2009-05-07 21:49:45 +0000 | [diff] [blame] | 568 | |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame^] | 569 | if (!StoreSite) { |
| 570 | GRStateManager &StateMgr = BRC.getStateManager(); |
| 571 | const ExplodedNode<GRState> *Node = N, *Last = NULL; |
| 572 | |
| 573 | for ( ; Node ; Last = Node, Node = Node->getFirstPred()) { |
| 574 | |
| 575 | if (const VarRegion *VR = dyn_cast<VarRegion>(R)) { |
| 576 | if (const PostStmt *P = Node->getLocationAs<PostStmt>()) |
| 577 | if (const DeclStmt *DS = P->getStmtAs<DeclStmt>()) |
| 578 | if (DS->getSingleDecl() == VR->getDecl()) { |
| 579 | Last = Node; |
| 580 | break; |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | if (StateMgr.GetSVal(Node->getState(), R) != V) |
| 585 | break; |
| 586 | } |
| 587 | |
| 588 | if (!Node || !Last) { |
| 589 | satisfied = true; |
| 590 | return NULL; |
| 591 | } |
| 592 | |
| 593 | StoreSite = Last; |
| 594 | } |
Ted Kremenek | 7704a33 | 2009-05-07 21:49:45 +0000 | [diff] [blame] | 595 | |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame^] | 596 | if (StoreSite != N) |
| 597 | return NULL; |
| 598 | |
| 599 | satisfied = true; |
| 600 | std::string sbuf; |
| 601 | llvm::raw_string_ostream os(sbuf); |
Ted Kremenek | 7704a33 | 2009-05-07 21:49:45 +0000 | [diff] [blame] | 602 | |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame^] | 603 | if (const PostStmt *PS = N->getLocationAs<PostStmt>()) { |
| 604 | if (const DeclStmt *DS = PS->getStmtAs<DeclStmt>()) { |
| 605 | |
| 606 | if (const VarRegion *VR = dyn_cast<VarRegion>(R)) { |
| 607 | os << "Variable '" << VR->getDecl()->getNameAsString() << "' "; |
| 608 | } |
| 609 | else |
| 610 | return NULL; |
| 611 | |
| 612 | if (isa<loc::ConcreteInt>(V)) { |
| 613 | bool b = false; |
| 614 | ASTContext &C = BRC.getASTContext(); |
| 615 | if (R->isBoundable(C)) { |
| 616 | if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) { |
| 617 | if (C.isObjCObjectPointerType(TR->getValueType(C))) { |
| 618 | os << "initialized to nil"; |
| 619 | b = true; |
| 620 | } |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | if (!b) |
| 625 | os << "initialized to a null pointer value"; |
| 626 | } |
| 627 | else if (V.isUndef()) { |
| 628 | if (isa<VarRegion>(R)) { |
| 629 | const VarDecl *VD = cast<VarDecl>(DS->getSingleDecl()); |
| 630 | if (VD->getInit()) |
| 631 | os << "initialized to a garbage value"; |
| 632 | else |
| 633 | os << "declared without an initial value"; |
| 634 | } |
| 635 | } |
| 636 | } |
| 637 | } |
| 638 | |
| 639 | if (os.str().empty()) { |
| 640 | if (isa<loc::ConcreteInt>(V)) { |
| 641 | bool b = false; |
| 642 | ASTContext &C = BRC.getASTContext(); |
| 643 | if (R->isBoundable(C)) { |
| 644 | if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) { |
| 645 | if (C.isObjCObjectPointerType(TR->getValueType(C))) { |
| 646 | os << "nil object reference stored to "; |
| 647 | b = true; |
| 648 | } |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | if (!b) |
| 653 | os << "Null pointer value stored to "; |
| 654 | } |
| 655 | else if (V.isUndef()) { |
| 656 | os << "Uninitialized value stored to "; |
| 657 | } |
| 658 | else |
| 659 | return NULL; |
Ted Kremenek | 7704a33 | 2009-05-07 21:49:45 +0000 | [diff] [blame] | 660 | |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame^] | 661 | if (const VarRegion *VR = dyn_cast<VarRegion>(R)) { |
| 662 | os << '\'' << VR->getDecl()->getNameAsString() << '\''; |
| 663 | } |
| 664 | else |
| 665 | return NULL; |
| 666 | } |
| 667 | |
| 668 | // FIXME: Refactor this into BugReporterContext. |
| 669 | Stmt *S = 0; |
| 670 | ProgramPoint P = N->getLocation(); |
| 671 | |
| 672 | if (BlockEdge *BE = dyn_cast<BlockEdge>(&P)) { |
| 673 | CFGBlock *BSrc = BE->getSrc(); |
| 674 | S = BSrc->getTerminatorCondition(); |
| 675 | } |
| 676 | else if (PostStmt *PS = dyn_cast<PostStmt>(&P)) { |
| 677 | S = PS->getStmt(); |
| 678 | } |
| 679 | |
| 680 | if (!S) |
| 681 | return NULL; |
| 682 | |
| 683 | // Construct a new PathDiagnosticPiece. |
| 684 | PathDiagnosticLocation L(S, BRC.getSourceManager()); |
| 685 | return new PathDiagnosticEventPiece(L, os.str()); |
| 686 | } |
Ted Kremenek | 7704a33 | 2009-05-07 21:49:45 +0000 | [diff] [blame] | 687 | }; |
Ted Kremenek | 7704a33 | 2009-05-07 21:49:45 +0000 | [diff] [blame] | 688 | |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame^] | 689 | |
| 690 | static void registerFindLastStore(BugReporterContext& BRC, const MemRegion *R, |
| 691 | SVal V) { |
| 692 | BRC.addVisitor(new FindLastStoreBRVisitor(V, R)); |
| 693 | } |
| 694 | |
Ted Kremenek | dd986cc | 2009-05-07 00:45:33 +0000 | [diff] [blame] | 695 | class VISIBILITY_HIDDEN TrackConstraintBRVisitor : public BugReporterVisitor { |
| 696 | SVal Constraint; |
| 697 | const bool Assumption; |
| 698 | bool isSatisfied; |
| 699 | public: |
| 700 | TrackConstraintBRVisitor(SVal constraint, bool assumption) |
| 701 | : Constraint(constraint), Assumption(assumption), isSatisfied(false) {} |
| 702 | |
Ted Kremenek | 7704a33 | 2009-05-07 21:49:45 +0000 | [diff] [blame] | 703 | PathDiagnosticPiece* VisitNode(const ExplodedNode<GRState> *N, |
| 704 | const ExplodedNode<GRState> *PrevN, |
Ted Kremenek | dd986cc | 2009-05-07 00:45:33 +0000 | [diff] [blame] | 705 | BugReporterContext& BRC) { |
| 706 | if (isSatisfied) |
| 707 | return NULL; |
| 708 | |
| 709 | // Check if in the previous state it was feasible for this constraint |
| 710 | // to *not* be true. |
| 711 | |
| 712 | GRStateManager &StateMgr = BRC.getStateManager(); |
| 713 | bool isFeasible = false; |
| 714 | if (StateMgr.Assume(PrevN->getState(), Constraint, !Assumption, |
| 715 | isFeasible)) { |
| 716 | assert(isFeasible); // Eventually we don't need 'isFeasible'. |
| 717 | |
| 718 | isSatisfied = true; |
| 719 | |
| 720 | // As a sanity check, make sure that the negation of the constraint |
| 721 | // was infeasible in the current state. If it is feasible, we somehow |
| 722 | // missed the transition point. |
| 723 | isFeasible = false; |
| 724 | if (StateMgr.Assume(N->getState(), Constraint, !Assumption, |
| 725 | isFeasible)) { |
| 726 | assert(isFeasible); |
| 727 | return NULL; |
| 728 | } |
| 729 | |
| 730 | // We found the transition point for the constraint. We now need to |
| 731 | // pretty-print the constraint. (work-in-progress) |
| 732 | std::string sbuf; |
| 733 | llvm::raw_string_ostream os(sbuf); |
| 734 | |
| 735 | if (isa<Loc>(Constraint)) { |
| 736 | os << "Assuming pointer value is "; |
| 737 | os << (Assumption ? "non-NULL" : "NULL"); |
| 738 | } |
| 739 | |
| 740 | if (os.str().empty()) |
| 741 | return NULL; |
| 742 | |
| 743 | // FIXME: Refactor this into BugReporterContext. |
| 744 | Stmt *S = 0; |
| 745 | ProgramPoint P = N->getLocation(); |
| 746 | |
| 747 | if (BlockEdge *BE = dyn_cast<BlockEdge>(&P)) { |
| 748 | CFGBlock *BSrc = BE->getSrc(); |
| 749 | S = BSrc->getTerminatorCondition(); |
| 750 | } |
| 751 | else if (PostStmt *PS = dyn_cast<PostStmt>(&P)) { |
| 752 | S = PS->getStmt(); |
| 753 | } |
| 754 | |
| 755 | if (!S) |
| 756 | return NULL; |
| 757 | |
| 758 | // Construct a new PathDiagnosticPiece. |
| 759 | PathDiagnosticLocation L(S, BRC.getSourceManager()); |
| 760 | return new PathDiagnosticEventPiece(L, os.str()); |
| 761 | } |
| 762 | |
| 763 | return NULL; |
| 764 | } |
| 765 | }; |
| 766 | } // end anonymous namespace |
| 767 | |
| 768 | static void registerTrackConstraint(BugReporterContext& BRC, SVal Constraint, |
| 769 | bool Assumption) { |
| 770 | BRC.addVisitor(new TrackConstraintBRVisitor(Constraint, Assumption)); |
| 771 | } |
| 772 | |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame^] | 773 | static void registerTrackNullOrUndefValue(BugReporterContext& BRC, |
Ted Kremenek | dd986cc | 2009-05-07 00:45:33 +0000 | [diff] [blame] | 774 | const ExplodedNode<GRState>* N) { |
| 775 | |
| 776 | ProgramPoint P = N->getLocation(); |
| 777 | PostStmt *PS = dyn_cast<PostStmt>(&P); |
| 778 | |
| 779 | if (!PS) |
| 780 | return; |
| 781 | |
| 782 | Stmt *S = PS->getStmt(); |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame^] | 783 | GRStateManager &StateMgr = BRC.getStateManager(); |
| 784 | const GRState *state = N->getState(); |
Ted Kremenek | dd986cc | 2009-05-07 00:45:33 +0000 | [diff] [blame] | 785 | |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame^] | 786 | // Pattern match for a few useful cases (do something smarter later): |
| 787 | // a[0], p->f, *p |
| 788 | const DeclRefExpr *DR = 0; |
| 789 | |
| 790 | if (const UnaryOperator *U = dyn_cast<UnaryOperator>(S)) { |
| 791 | if (U->getOpcode() == UnaryOperator::Deref) |
| 792 | DR = dyn_cast<DeclRefExpr>(U->getSubExpr()->IgnoreParenCasts()); |
| 793 | } |
| 794 | else if (const MemberExpr *ME = dyn_cast<MemberExpr>(S)) { |
| 795 | DR = dyn_cast<DeclRefExpr>(ME->getBase()->IgnoreParenCasts()); |
| 796 | } |
| 797 | else if (const ObjCMessageExpr *MSE = dyn_cast<ObjCMessageExpr>(S)) { |
| 798 | // FIXME: We should probably distinguish between cases where we had |
| 799 | // a nil receiver and null dereferences. |
| 800 | const Expr *Receiver = MSE->getReceiver(); |
| 801 | if (Receiver) { |
| 802 | DR = dyn_cast<DeclRefExpr>(Receiver->IgnoreParenCasts()); |
| 803 | } |
| 804 | } |
| 805 | else if (const ReturnStmt *RS = dyn_cast<ReturnStmt>(S)) { |
| 806 | if (const Expr *Ret = RS->getRetValue()) |
| 807 | DR = dyn_cast<DeclRefExpr>(Ret->IgnoreParenCasts()); |
| 808 | } |
| 809 | else if (const Expr *Ex = dyn_cast<Expr>(S)) { |
| 810 | // Keep this case last. |
| 811 | DR = dyn_cast<DeclRefExpr>(Ex->IgnoreParenCasts()); |
| 812 | } |
| 813 | |
| 814 | if (DR) { |
| 815 | if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) { |
| 816 | const VarRegion *R = |
| 817 | StateMgr.getRegionManager().getVarRegion(VD); |
| 818 | |
| 819 | // What did we load? |
| 820 | SVal V = StateMgr.GetSVal(state, R); |
| 821 | |
| 822 | if (isa<loc::ConcreteInt>(V) || V.isUndef()) { |
| 823 | registerFindLastStore(BRC, R, V); |
| 824 | } |
| 825 | } |
| 826 | } |
| 827 | |
| 828 | // Retrieve the base for arrays since BasicStoreManager doesn't know how |
| 829 | // to reason about them. |
Ted Kremenek | dd986cc | 2009-05-07 00:45:33 +0000 | [diff] [blame] | 830 | if (ArraySubscriptExpr *AE = dyn_cast<ArraySubscriptExpr>(S)) { |
| 831 | S = AE->getBase(); |
| 832 | } |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame^] | 833 | |
| 834 | SVal V = StateMgr.GetSValAsScalarOrLoc(state, S); |
Ted Kremenek | dd986cc | 2009-05-07 00:45:33 +0000 | [diff] [blame] | 835 | |
| 836 | // Uncomment this to find cases where we aren't properly getting the |
| 837 | // base value that was dereferenced. |
| 838 | // assert(!V.isUnknownOrUndef()); |
| 839 | |
Ted Kremenek | 7704a33 | 2009-05-07 21:49:45 +0000 | [diff] [blame] | 840 | // Is it a symbolic value? |
Ted Kremenek | dd986cc | 2009-05-07 00:45:33 +0000 | [diff] [blame] | 841 | if (loc::MemRegionVal *L = dyn_cast<loc::MemRegionVal>(&V)) { |
| 842 | const SubRegion *R = cast<SubRegion>(L->getRegion()); |
| 843 | while (R && !isa<SymbolicRegion>(R)) { |
| 844 | R = dyn_cast<SubRegion>(R->getSuperRegion()); |
| 845 | } |
| 846 | |
| 847 | if (R) { |
| 848 | assert(isa<SymbolicRegion>(R)); |
| 849 | registerTrackConstraint(BRC, loc::MemRegionVal(R), false); |
| 850 | } |
| 851 | } |
Ted Kremenek | 7704a33 | 2009-05-07 21:49:45 +0000 | [diff] [blame] | 852 | |
| 853 | // Was it a hard integer? |
| 854 | // if (isa<nonloc::ConcreteInt>(V)) |
| 855 | // registerTrackValue(BRC, S, V, N); |
Ted Kremenek | dd986cc | 2009-05-07 00:45:33 +0000 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 859 | // Check registration. |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 860 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 861 | |
| 862 | void GRExprEngine::RegisterInternalChecks() { |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 863 | // Register internal "built-in" BugTypes with the BugReporter. These BugTypes |
| 864 | // are different than what probably many checks will do since they don't |
| 865 | // create BugReports on-the-fly but instead wait until GRExprEngine finishes |
| 866 | // analyzing a function. Generation of BugReport objects is done via a call |
| 867 | // to 'FlushReports' from BugReporter. |
| 868 | BR.Register(new NullDeref(this)); |
| 869 | BR.Register(new UndefinedDeref(this)); |
| 870 | BR.Register(new UndefBranch(this)); |
| 871 | BR.Register(new DivZero(this)); |
| 872 | BR.Register(new UndefResult(this)); |
| 873 | BR.Register(new BadCall(this)); |
| 874 | BR.Register(new RetStack(this)); |
| 875 | BR.Register(new RetUndef(this)); |
| 876 | BR.Register(new BadArg(this)); |
| 877 | BR.Register(new BadMsgExprArg(this)); |
| 878 | BR.Register(new BadReceiver(this)); |
| 879 | BR.Register(new OutOfBoundMemoryAccess(this)); |
| 880 | BR.Register(new BadSizeVLA(this)); |
Ted Kremenek | 21fe837 | 2009-02-19 04:06:22 +0000 | [diff] [blame] | 881 | BR.Register(new NilReceiverStructRet(this)); |
Ted Kremenek | 899b3de | 2009-04-08 03:07:17 +0000 | [diff] [blame] | 882 | BR.Register(new NilReceiverLargerThanVoidPtrRet(this)); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 883 | |
| 884 | // The following checks do not need to have their associated BugTypes |
| 885 | // explicitly registered with the BugReporter. If they issue any BugReports, |
| 886 | // their associated BugType will get registered with the BugReporter |
| 887 | // automatically. Note that the check itself is owned by the GRExprEngine |
| 888 | // object. |
| 889 | AddCheck(new CheckAttrNonNull(BR), Stmt::CallExprClass); |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 890 | } |