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 | |
| 42 | static void registerTrackNullValue(BugReporterContext& BRC, |
| 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 | |
| 51 | class VISIBILITY_HIDDEN BuiltinBugReport : public BugReport { |
| 52 | public: |
| 53 | BuiltinBugReport(BugType& bt, const char* desc, |
| 54 | const ExplodedNode<GRState> *n) |
| 55 | : BugReport(bt, desc, n) {} |
| 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) |
| 67 | : BugType(n, "Logic Errors"), Eng(*eng), desc(d) {} |
| 68 | |
| 69 | BuiltinBug(GRExprEngine *eng, const char* n) |
| 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) { |
| 107 | registerTrackNullValue(BRC, N); |
| 108 | } |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 109 | }; |
| 110 | |
Ted Kremenek | 21fe837 | 2009-02-19 04:06:22 +0000 | [diff] [blame] | 111 | class VISIBILITY_HIDDEN NilReceiverStructRet : public BugType { |
| 112 | GRExprEngine &Eng; |
| 113 | public: |
| 114 | NilReceiverStructRet(GRExprEngine* eng) : |
Ted Kremenek | 5d88ff8 | 2009-04-02 02:40:26 +0000 | [diff] [blame] | 115 | BugType("'nil' receiver with struct return type", "Logic Errors"), |
Ted Kremenek | 21fe837 | 2009-02-19 04:06:22 +0000 | [diff] [blame] | 116 | Eng(*eng) {} |
| 117 | |
| 118 | void FlushReports(BugReporter& BR) { |
| 119 | for (GRExprEngine::nil_receiver_struct_ret_iterator |
| 120 | I=Eng.nil_receiver_struct_ret_begin(), |
| 121 | E=Eng.nil_receiver_struct_ret_end(); I!=E; ++I) { |
| 122 | |
| 123 | std::string sbuf; |
| 124 | llvm::raw_string_ostream os(sbuf); |
| 125 | PostStmt P = cast<PostStmt>((*I)->getLocation()); |
| 126 | ObjCMessageExpr *ME = cast<ObjCMessageExpr>(P.getStmt()); |
| 127 | os << "The receiver in the message expression is 'nil' and results in the" |
| 128 | " returned value (of type '" |
| 129 | << ME->getType().getAsString() |
| 130 | << "') to be garbage or otherwise undefined."; |
| 131 | |
| 132 | RangedBugReport *R = new RangedBugReport(*this, os.str().c_str(), *I); |
| 133 | R->addRange(ME->getReceiver()->getSourceRange()); |
| 134 | BR.EmitReport(R); |
| 135 | } |
| 136 | } |
| 137 | }; |
Ted Kremenek | 899b3de | 2009-04-08 03:07:17 +0000 | [diff] [blame] | 138 | |
| 139 | class VISIBILITY_HIDDEN NilReceiverLargerThanVoidPtrRet : public BugType { |
| 140 | GRExprEngine &Eng; |
| 141 | public: |
| 142 | NilReceiverLargerThanVoidPtrRet(GRExprEngine* eng) : |
| 143 | BugType("'nil' receiver with return type larger than sizeof(void *)", |
| 144 | "Logic Errors"), |
| 145 | Eng(*eng) {} |
| 146 | |
| 147 | void FlushReports(BugReporter& BR) { |
| 148 | for (GRExprEngine::nil_receiver_larger_than_voidptr_ret_iterator |
| 149 | I=Eng.nil_receiver_larger_than_voidptr_ret_begin(), |
| 150 | E=Eng.nil_receiver_larger_than_voidptr_ret_end(); I!=E; ++I) { |
| 151 | |
| 152 | std::string sbuf; |
| 153 | llvm::raw_string_ostream os(sbuf); |
| 154 | PostStmt P = cast<PostStmt>((*I)->getLocation()); |
| 155 | ObjCMessageExpr *ME = cast<ObjCMessageExpr>(P.getStmt()); |
| 156 | os << "The receiver in the message expression is 'nil' and results in the" |
| 157 | " returned value (of type '" |
| 158 | << ME->getType().getAsString() |
| 159 | << "' and of size " |
| 160 | << Eng.getContext().getTypeSize(ME->getType()) / 8 |
| 161 | << " bytes) to be garbage or otherwise undefined."; |
| 162 | |
| 163 | RangedBugReport *R = new RangedBugReport(*this, os.str().c_str(), *I); |
| 164 | R->addRange(ME->getReceiver()->getSourceRange()); |
| 165 | BR.EmitReport(R); |
| 166 | } |
| 167 | } |
| 168 | }; |
Ted Kremenek | 21fe837 | 2009-02-19 04:06:22 +0000 | [diff] [blame] | 169 | |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 170 | class VISIBILITY_HIDDEN UndefinedDeref : public BuiltinBug { |
| 171 | public: |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 172 | UndefinedDeref(GRExprEngine* eng) |
Ted Kremenek | 17a8e07 | 2009-03-01 05:43:22 +0000 | [diff] [blame] | 173 | : BuiltinBug(eng,"Dereference of undefined pointer value") {} |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 174 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 175 | void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) { |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 176 | Emit(BR, Eng.undef_derefs_begin(), Eng.undef_derefs_end()); |
| 177 | } |
| 178 | }; |
| 179 | |
| 180 | class VISIBILITY_HIDDEN DivZero : public BuiltinBug { |
| 181 | public: |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 182 | DivZero(GRExprEngine* eng) |
Ted Kremenek | 5d88ff8 | 2009-04-02 02:40:26 +0000 | [diff] [blame] | 183 | : BuiltinBug(eng,"Division-by-zero", |
| 184 | "Division by zero or undefined value.") {} |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 185 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 186 | void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) { |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 187 | Emit(BR, Eng.explicit_bad_divides_begin(), Eng.explicit_bad_divides_end()); |
| 188 | } |
| 189 | }; |
| 190 | |
| 191 | class VISIBILITY_HIDDEN UndefResult : public BuiltinBug { |
| 192 | public: |
Ted Kremenek | 5d88ff8 | 2009-04-02 02:40:26 +0000 | [diff] [blame] | 193 | UndefResult(GRExprEngine* eng) : BuiltinBug(eng,"Undefined result", |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 194 | "Result of operation is undefined.") {} |
| 195 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 196 | void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) { |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 197 | Emit(BR, Eng.undef_results_begin(), Eng.undef_results_end()); |
| 198 | } |
| 199 | }; |
| 200 | |
| 201 | class VISIBILITY_HIDDEN BadCall : public BuiltinBug { |
| 202 | public: |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 203 | BadCall(GRExprEngine *eng) |
Ted Kremenek | 5d88ff8 | 2009-04-02 02:40:26 +0000 | [diff] [blame] | 204 | : BuiltinBug(eng, "Invalid function call", |
Ted Kremenek | 17a8e07 | 2009-03-01 05:43:22 +0000 | [diff] [blame] | 205 | "Called function pointer is a null or undefined pointer value") {} |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 206 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 207 | void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) { |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 208 | Emit(BR, Eng.bad_calls_begin(), Eng.bad_calls_end()); |
| 209 | } |
| 210 | }; |
| 211 | |
| 212 | class VISIBILITY_HIDDEN BadArg : public BuiltinBug { |
| 213 | public: |
Ted Kremenek | 5d88ff8 | 2009-04-02 02:40:26 +0000 | [diff] [blame] | 214 | BadArg(GRExprEngine* eng) : BuiltinBug(eng,"Uninitialized argument", |
Ted Kremenek | 17a8e07 | 2009-03-01 05:43:22 +0000 | [diff] [blame] | 215 | "Pass-by-value argument in function call is undefined.") {} |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 216 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 217 | BadArg(GRExprEngine* eng, const char* d) |
Ted Kremenek | 5d88ff8 | 2009-04-02 02:40:26 +0000 | [diff] [blame] | 218 | : BuiltinBug(eng,"Uninitialized argument", d) {} |
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 | for (GRExprEngine::UndefArgsTy::iterator I = Eng.undef_arg_begin(), |
| 222 | E = Eng.undef_arg_end(); I!=E; ++I) { |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 223 | // Generate a report for this bug. |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 224 | RangedBugReport *report = new RangedBugReport(*this, desc.c_str(), |
| 225 | I->first); |
| 226 | report->addRange(I->second->getSourceRange()); |
| 227 | BR.EmitReport(report); |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | }; |
| 231 | |
| 232 | class VISIBILITY_HIDDEN BadMsgExprArg : public BadArg { |
| 233 | public: |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 234 | BadMsgExprArg(GRExprEngine* eng) |
Ted Kremenek | 5d88ff8 | 2009-04-02 02:40:26 +0000 | [diff] [blame] | 235 | : BadArg(eng,"Pass-by-value argument in message expression is undefined"){} |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 236 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 237 | void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) { |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 238 | for (GRExprEngine::UndefArgsTy::iterator I=Eng.msg_expr_undef_arg_begin(), |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 239 | E = Eng.msg_expr_undef_arg_end(); I!=E; ++I) { |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 240 | // Generate a report for this bug. |
Ted Kremenek | 21fe837 | 2009-02-19 04:06:22 +0000 | [diff] [blame] | 241 | RangedBugReport *report = new RangedBugReport(*this, desc.c_str(), |
| 242 | I->first); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 243 | report->addRange(I->second->getSourceRange()); |
| 244 | BR.EmitReport(report); |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 245 | } |
| 246 | } |
| 247 | }; |
| 248 | |
| 249 | class VISIBILITY_HIDDEN BadReceiver : public BuiltinBug { |
| 250 | public: |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 251 | BadReceiver(GRExprEngine* eng) |
Ted Kremenek | 5d88ff8 | 2009-04-02 02:40:26 +0000 | [diff] [blame] | 252 | : BuiltinBug(eng,"Uninitialized receiver", |
| 253 | "Receiver in message expression is an uninitialized value") {} |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 254 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 255 | void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) { |
Ted Kremenek | efd5994 | 2008-12-08 22:47:34 +0000 | [diff] [blame] | 256 | for (GRExprEngine::ErrorNodes::iterator I=Eng.undef_receivers_begin(), |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 257 | End = Eng.undef_receivers_end(); I!=End; ++I) { |
| 258 | |
| 259 | // Generate a report for this bug. |
Ted Kremenek | 5d88ff8 | 2009-04-02 02:40:26 +0000 | [diff] [blame] | 260 | RangedBugReport *report = new RangedBugReport(*this, desc.c_str(), *I); |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 261 | ExplodedNode<GRState>* N = *I; |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 262 | Stmt *S = cast<PostStmt>(N->getLocation()).getStmt(); |
| 263 | Expr* E = cast<ObjCMessageExpr>(S)->getReceiver(); |
| 264 | assert (E && "Receiver cannot be NULL"); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 265 | report->addRange(E->getSourceRange()); |
| 266 | BR.EmitReport(report); |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 267 | } |
| 268 | } |
| 269 | }; |
Ted Kremenek | 5917d78 | 2008-11-21 00:27:44 +0000 | [diff] [blame] | 270 | |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 271 | class VISIBILITY_HIDDEN RetStack : public BuiltinBug { |
| 272 | public: |
Ted Kremenek | 17a8e07 | 2009-03-01 05:43:22 +0000 | [diff] [blame] | 273 | RetStack(GRExprEngine* eng) |
Ted Kremenek | 5d88ff8 | 2009-04-02 02:40:26 +0000 | [diff] [blame] | 274 | : BuiltinBug(eng, "Return of address to stack-allocated memory") {} |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 275 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 276 | void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) { |
Ted Kremenek | b7714b2 | 2008-07-30 17:49:12 +0000 | [diff] [blame] | 277 | for (GRExprEngine::ret_stackaddr_iterator I=Eng.ret_stackaddr_begin(), |
| 278 | End = Eng.ret_stackaddr_end(); I!=End; ++I) { |
Ted Kremenek | 22bda88 | 2008-07-31 20:31:27 +0000 | [diff] [blame] | 279 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 280 | ExplodedNode<GRState>* N = *I; |
Ted Kremenek | b7714b2 | 2008-07-30 17:49:12 +0000 | [diff] [blame] | 281 | Stmt *S = cast<PostStmt>(N->getLocation()).getStmt(); |
| 282 | Expr* E = cast<ReturnStmt>(S)->getRetValue(); |
| 283 | assert (E && "Return expression cannot be NULL"); |
Ted Kremenek | 22bda88 | 2008-07-31 20:31:27 +0000 | [diff] [blame] | 284 | |
| 285 | // Get the value associated with E. |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 286 | loc::MemRegionVal V = |
| 287 | cast<loc::MemRegionVal>(Eng.getStateManager().GetSVal(N->getState(), |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 288 | E)); |
Ted Kremenek | 22bda88 | 2008-07-31 20:31:27 +0000 | [diff] [blame] | 289 | |
| 290 | // Generate a report for this bug. |
Ted Kremenek | ad51a60 | 2008-10-31 00:18:30 +0000 | [diff] [blame] | 291 | std::string buf; |
| 292 | llvm::raw_string_ostream os(buf); |
Ted Kremenek | 8aed806 | 2008-10-31 00:13:20 +0000 | [diff] [blame] | 293 | SourceRange R; |
Ted Kremenek | 22bda88 | 2008-07-31 20:31:27 +0000 | [diff] [blame] | 294 | |
Ted Kremenek | 8aed806 | 2008-10-31 00:13:20 +0000 | [diff] [blame] | 295 | // Check if the region is a compound literal. |
| 296 | if (const CompoundLiteralRegion* CR = |
| 297 | dyn_cast<CompoundLiteralRegion>(V.getRegion())) { |
| 298 | |
| 299 | const CompoundLiteralExpr* CL = CR->getLiteralExpr(); |
| 300 | os << "Address of stack memory associated with a compound literal " |
| 301 | "declared on line " |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 302 | << BR.getSourceManager() |
| 303 | .getInstantiationLineNumber(CL->getLocStart()) |
Ted Kremenek | 8aed806 | 2008-10-31 00:13:20 +0000 | [diff] [blame] | 304 | << " returned."; |
| 305 | |
| 306 | R = CL->getSourceRange(); |
| 307 | } |
Ted Kremenek | de8cd19 | 2008-11-02 00:35:25 +0000 | [diff] [blame] | 308 | else if (const AllocaRegion* AR = dyn_cast<AllocaRegion>(V.getRegion())) { |
| 309 | const Expr* ARE = AR->getExpr(); |
| 310 | SourceLocation L = ARE->getLocStart(); |
| 311 | R = ARE->getSourceRange(); |
| 312 | |
| 313 | os << "Address of stack memory allocated by call to alloca() on line " |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 314 | << BR.getSourceManager().getInstantiationLineNumber(L) |
Ted Kremenek | de8cd19 | 2008-11-02 00:35:25 +0000 | [diff] [blame] | 315 | << " returned."; |
| 316 | } |
Ted Kremenek | 8aed806 | 2008-10-31 00:13:20 +0000 | [diff] [blame] | 317 | else { |
| 318 | os << "Address of stack memory associated with local variable '" |
| 319 | << V.getRegion()->getString() << "' returned."; |
| 320 | } |
Ted Kremenek | 22bda88 | 2008-07-31 20:31:27 +0000 | [diff] [blame] | 321 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 322 | RangedBugReport *report = new RangedBugReport(*this, os.str().c_str(), N); |
| 323 | report->addRange(E->getSourceRange()); |
| 324 | if (R.isValid()) report->addRange(R); |
| 325 | BR.EmitReport(report); |
Ted Kremenek | b7714b2 | 2008-07-30 17:49:12 +0000 | [diff] [blame] | 326 | } |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 327 | } |
| 328 | }; |
Ted Kremenek | 5917d78 | 2008-11-21 00:27:44 +0000 | [diff] [blame] | 329 | |
| 330 | class VISIBILITY_HIDDEN RetUndef : public BuiltinBug { |
| 331 | public: |
Ted Kremenek | 5d88ff8 | 2009-04-02 02:40:26 +0000 | [diff] [blame] | 332 | RetUndef(GRExprEngine* eng) : BuiltinBug(eng, "Uninitialized return value", |
Ted Kremenek | 5917d78 | 2008-11-21 00:27:44 +0000 | [diff] [blame] | 333 | "Uninitialized or undefined return value returned to caller.") {} |
| 334 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 335 | void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) { |
Ted Kremenek | 5917d78 | 2008-11-21 00:27:44 +0000 | [diff] [blame] | 336 | Emit(BR, Eng.ret_undef_begin(), Eng.ret_undef_end()); |
| 337 | } |
| 338 | }; |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 339 | |
| 340 | class VISIBILITY_HIDDEN UndefBranch : public BuiltinBug { |
| 341 | struct VISIBILITY_HIDDEN FindUndefExpr { |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 342 | GRStateManager& VM; |
| 343 | const GRState* St; |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 344 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 345 | FindUndefExpr(GRStateManager& V, const GRState* S) : VM(V), St(S) {} |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 346 | |
Ted Kremenek | b7714b2 | 2008-07-30 17:49:12 +0000 | [diff] [blame] | 347 | Expr* FindExpr(Expr* Ex) { |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 348 | if (!MatchesCriteria(Ex)) |
Ted Kremenek | b7714b2 | 2008-07-30 17:49:12 +0000 | [diff] [blame] | 349 | return 0; |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 350 | |
Ted Kremenek | b7714b2 | 2008-07-30 17:49:12 +0000 | [diff] [blame] | 351 | 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] | 352 | if (Expr* ExI = dyn_cast_or_null<Expr>(*I)) { |
| 353 | Expr* E2 = FindExpr(ExI); |
| 354 | if (E2) return E2; |
| 355 | } |
| 356 | |
| 357 | return Ex; |
| 358 | } |
| 359 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 360 | bool MatchesCriteria(Expr* Ex) { return VM.GetSVal(St, Ex).isUndef(); } |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 361 | }; |
| 362 | |
| 363 | public: |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 364 | UndefBranch(GRExprEngine *eng) |
Ted Kremenek | 5d88ff8 | 2009-04-02 02:40:26 +0000 | [diff] [blame] | 365 | : BuiltinBug(eng,"Use of uninitialized value", |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 366 | "Branch condition evaluates to an uninitialized value.") {} |
| 367 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 368 | void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) { |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 369 | for (GRExprEngine::undef_branch_iterator I=Eng.undef_branches_begin(), |
| 370 | E=Eng.undef_branches_end(); I!=E; ++I) { |
| 371 | |
| 372 | // What's going on here: we want to highlight the subexpression of the |
| 373 | // condition that is the most likely source of the "uninitialized |
| 374 | // branch condition." We do a recursive walk of the condition's |
| 375 | // subexpressions and roughly look for the most nested subexpression |
| 376 | // that binds to Undefined. We then highlight that expression's range. |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 377 | BlockEdge B = cast<BlockEdge>((*I)->getLocation()); |
| 378 | Expr* Ex = cast<Expr>(B.getSrc()->getTerminatorCondition()); |
| 379 | assert (Ex && "Block must have a terminator."); |
| 380 | |
| 381 | // Get the predecessor node and check if is a PostStmt with the Stmt |
| 382 | // being the terminator condition. We want to inspect the state |
| 383 | // of that node instead because it will contain main information about |
| 384 | // the subexpressions. |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 385 | assert (!(*I)->pred_empty()); |
| 386 | |
| 387 | // Note: any predecessor will do. They should have identical state, |
| 388 | // since all the BlockEdge did was act as an error sink since the value |
| 389 | // had to already be undefined. |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 390 | ExplodedNode<GRState> *N = *(*I)->pred_begin(); |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 391 | ProgramPoint P = N->getLocation(); |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 392 | const GRState* St = (*I)->getState(); |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 393 | |
| 394 | if (PostStmt* PS = dyn_cast<PostStmt>(&P)) |
| 395 | if (PS->getStmt() == Ex) |
| 396 | St = N->getState(); |
| 397 | |
| 398 | FindUndefExpr FindIt(Eng.getStateManager(), St); |
| 399 | Ex = FindIt.FindExpr(Ex); |
| 400 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 401 | RangedBugReport *R = new RangedBugReport(*this, desc.c_str(), *I); |
| 402 | R->addRange(Ex->getSourceRange()); |
| 403 | BR.EmitReport(R); |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 404 | } |
| 405 | } |
| 406 | }; |
| 407 | |
Zhongxing Xu | 1c0c233 | 2008-11-23 05:52:28 +0000 | [diff] [blame] | 408 | class VISIBILITY_HIDDEN OutOfBoundMemoryAccess : public BuiltinBug { |
| 409 | public: |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 410 | OutOfBoundMemoryAccess(GRExprEngine* eng) |
Ted Kremenek | 5d88ff8 | 2009-04-02 02:40:26 +0000 | [diff] [blame] | 411 | : BuiltinBug(eng,"Out-of-bounds memory access", |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 412 | "Load or store into an out-of-bound memory position.") {} |
Zhongxing Xu | 1c0c233 | 2008-11-23 05:52:28 +0000 | [diff] [blame] | 413 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 414 | void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) { |
Zhongxing Xu | 1c0c233 | 2008-11-23 05:52:28 +0000 | [diff] [blame] | 415 | Emit(BR, Eng.explicit_oob_memacc_begin(), Eng.explicit_oob_memacc_end()); |
| 416 | } |
| 417 | }; |
Ted Kremenek | efd5994 | 2008-12-08 22:47:34 +0000 | [diff] [blame] | 418 | |
Ted Kremenek | 159d248 | 2008-12-09 00:44:16 +0000 | [diff] [blame] | 419 | class VISIBILITY_HIDDEN BadSizeVLA : public BuiltinBug { |
Ted Kremenek | efd5994 | 2008-12-08 22:47:34 +0000 | [diff] [blame] | 420 | public: |
Ted Kremenek | 5d88ff8 | 2009-04-02 02:40:26 +0000 | [diff] [blame] | 421 | BadSizeVLA(GRExprEngine* eng) : |
| 422 | BuiltinBug(eng, "Bad variable-length array (VLA) size") {} |
Ted Kremenek | efd5994 | 2008-12-08 22:47:34 +0000 | [diff] [blame] | 423 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 424 | void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) { |
Ted Kremenek | efd5994 | 2008-12-08 22:47:34 +0000 | [diff] [blame] | 425 | for (GRExprEngine::ErrorNodes::iterator |
Ted Kremenek | 159d248 | 2008-12-09 00:44:16 +0000 | [diff] [blame] | 426 | I = Eng.ExplicitBadSizedVLA.begin(), |
| 427 | E = Eng.ExplicitBadSizedVLA.end(); I!=E; ++I) { |
| 428 | |
| 429 | // Determine whether this was a 'zero-sized' VLA or a VLA with an |
| 430 | // undefined size. |
| 431 | GRExprEngine::NodeTy* N = *I; |
| 432 | PostStmt PS = cast<PostStmt>(N->getLocation()); |
Ted Kremenek | efd5994 | 2008-12-08 22:47:34 +0000 | [diff] [blame] | 433 | DeclStmt *DS = cast<DeclStmt>(PS.getStmt()); |
| 434 | VarDecl* VD = cast<VarDecl>(*DS->decl_begin()); |
| 435 | QualType T = Eng.getContext().getCanonicalType(VD->getType()); |
| 436 | VariableArrayType* VT = cast<VariableArrayType>(T); |
Ted Kremenek | 159d248 | 2008-12-09 00:44:16 +0000 | [diff] [blame] | 437 | Expr* SizeExpr = VT->getSizeExpr(); |
Ted Kremenek | efd5994 | 2008-12-08 22:47:34 +0000 | [diff] [blame] | 438 | |
Ted Kremenek | 159d248 | 2008-12-09 00:44:16 +0000 | [diff] [blame] | 439 | std::string buf; |
| 440 | llvm::raw_string_ostream os(buf); |
Ted Kremenek | 5d88ff8 | 2009-04-02 02:40:26 +0000 | [diff] [blame] | 441 | os << "The expression used to specify the number of elements in the " |
| 442 | "variable-length array (VLA) '" |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 443 | << VD->getNameAsString() << "' evaluates to "; |
Ted Kremenek | 159d248 | 2008-12-09 00:44:16 +0000 | [diff] [blame] | 444 | |
Ted Kremenek | d49967f | 2009-04-29 21:58:13 +0000 | [diff] [blame] | 445 | bool isUndefined = Eng.getStateManager().GetSVal(N->getState(), |
| 446 | SizeExpr).isUndef(); |
| 447 | |
| 448 | if (isUndefined) |
Ted Kremenek | 159d248 | 2008-12-09 00:44:16 +0000 | [diff] [blame] | 449 | os << "an undefined or garbage value."; |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 450 | else |
| 451 | os << "0. VLAs with no elements have undefined behavior."; |
Ted Kremenek | d49967f | 2009-04-29 21:58:13 +0000 | [diff] [blame] | 452 | |
| 453 | std::string shortBuf; |
| 454 | llvm::raw_string_ostream os_short(shortBuf); |
| 455 | os_short << "Variable-length array '" << VD->getNameAsString() << "' " |
| 456 | << (isUndefined ? " garbage value for array size" |
| 457 | : " has zero elements (undefined behavior)"); |
Ted Kremenek | 159d248 | 2008-12-09 00:44:16 +0000 | [diff] [blame] | 458 | |
Ted Kremenek | d49967f | 2009-04-29 21:58:13 +0000 | [diff] [blame] | 459 | RangedBugReport *report = new RangedBugReport(*this, |
| 460 | os_short.str().c_str(), |
| 461 | os.str().c_str(), N); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 462 | report->addRange(SizeExpr->getSourceRange()); |
| 463 | BR.EmitReport(report); |
Ted Kremenek | efd5994 | 2008-12-08 22:47:34 +0000 | [diff] [blame] | 464 | } |
| 465 | } |
| 466 | }; |
Zhongxing Xu | 1c0c233 | 2008-11-23 05:52:28 +0000 | [diff] [blame] | 467 | |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 468 | //===----------------------------------------------------------------------===// |
| 469 | // __attribute__(nonnull) checking |
| 470 | |
| 471 | class VISIBILITY_HIDDEN CheckAttrNonNull : public GRSimpleAPICheck { |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 472 | BugType *BT; |
| 473 | BugReporter &BR; |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 474 | |
| 475 | public: |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 476 | CheckAttrNonNull(BugReporter &br) : BT(0), BR(br) {} |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 477 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 478 | virtual bool Audit(ExplodedNode<GRState>* N, GRStateManager& VMgr) { |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 479 | CallExpr* CE = cast<CallExpr>(cast<PostStmt>(N->getLocation()).getStmt()); |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 480 | const GRState* state = N->getState(); |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 481 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 482 | SVal X = VMgr.GetSVal(state, CE->getCallee()); |
Zhongxing Xu | 369f447 | 2009-04-20 05:24:46 +0000 | [diff] [blame] | 483 | |
| 484 | const FunctionDecl* FD = X.getAsFunctionDecl(); |
| 485 | if (!FD) |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 486 | return false; |
Zhongxing Xu | 369f447 | 2009-04-20 05:24:46 +0000 | [diff] [blame] | 487 | |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 488 | const NonNullAttr* Att = FD->getAttr<NonNullAttr>(); |
| 489 | |
| 490 | if (!Att) |
| 491 | return false; |
| 492 | |
| 493 | // Iterate through the arguments of CE and check them for null. |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 494 | unsigned idx = 0; |
| 495 | bool hasError = false; |
| 496 | |
| 497 | for (CallExpr::arg_iterator I=CE->arg_begin(), E=CE->arg_end(); I!=E; |
| 498 | ++I, ++idx) { |
| 499 | |
| 500 | if (!VMgr.isEqual(state, *I, 0) || !Att->isNonNull(idx)) |
| 501 | continue; |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 502 | |
| 503 | // Lazily allocate the BugType object if it hasn't already been created. |
| 504 | // Ownership is transferred to the BugReporter object once the BugReport |
| 505 | // is passed to 'EmitWarning'. |
Ted Kremenek | 5d88ff8 | 2009-04-02 02:40:26 +0000 | [diff] [blame] | 506 | if (!BT) BT = |
| 507 | new BugType("Argument with 'nonnull' attribute passed null", "API"); |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 508 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 509 | RangedBugReport *R = new RangedBugReport(*BT, |
| 510 | "Null pointer passed as an argument to a " |
| 511 | "'nonnull' parameter", N); |
| 512 | |
| 513 | R->addRange((*I)->getSourceRange()); |
| 514 | BR.EmitReport(R); |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 515 | hasError = true; |
| 516 | } |
| 517 | |
| 518 | return hasError; |
| 519 | } |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 520 | }; |
| 521 | } // end anonymous namespace |
| 522 | |
| 523 | //===----------------------------------------------------------------------===// |
Ted Kremenek | dd986cc | 2009-05-07 00:45:33 +0000 | [diff] [blame^] | 524 | // Definitions for bug reporter visitors. |
| 525 | //===----------------------------------------------------------------------===// |
| 526 | |
| 527 | namespace { |
| 528 | class VISIBILITY_HIDDEN TrackConstraintBRVisitor : public BugReporterVisitor { |
| 529 | SVal Constraint; |
| 530 | const bool Assumption; |
| 531 | bool isSatisfied; |
| 532 | public: |
| 533 | TrackConstraintBRVisitor(SVal constraint, bool assumption) |
| 534 | : Constraint(constraint), Assumption(assumption), isSatisfied(false) {} |
| 535 | |
| 536 | PathDiagnosticPiece* VisitNode(const ExplodedNode<GRState>* N, |
| 537 | const ExplodedNode<GRState>* PrevN, |
| 538 | BugReporterContext& BRC) { |
| 539 | if (isSatisfied) |
| 540 | return NULL; |
| 541 | |
| 542 | // Check if in the previous state it was feasible for this constraint |
| 543 | // to *not* be true. |
| 544 | |
| 545 | GRStateManager &StateMgr = BRC.getStateManager(); |
| 546 | bool isFeasible = false; |
| 547 | if (StateMgr.Assume(PrevN->getState(), Constraint, !Assumption, |
| 548 | isFeasible)) { |
| 549 | assert(isFeasible); // Eventually we don't need 'isFeasible'. |
| 550 | |
| 551 | isSatisfied = true; |
| 552 | |
| 553 | // As a sanity check, make sure that the negation of the constraint |
| 554 | // was infeasible in the current state. If it is feasible, we somehow |
| 555 | // missed the transition point. |
| 556 | isFeasible = false; |
| 557 | if (StateMgr.Assume(N->getState(), Constraint, !Assumption, |
| 558 | isFeasible)) { |
| 559 | assert(isFeasible); |
| 560 | return NULL; |
| 561 | } |
| 562 | |
| 563 | // We found the transition point for the constraint. We now need to |
| 564 | // pretty-print the constraint. (work-in-progress) |
| 565 | std::string sbuf; |
| 566 | llvm::raw_string_ostream os(sbuf); |
| 567 | |
| 568 | if (isa<Loc>(Constraint)) { |
| 569 | os << "Assuming pointer value is "; |
| 570 | os << (Assumption ? "non-NULL" : "NULL"); |
| 571 | } |
| 572 | |
| 573 | if (os.str().empty()) |
| 574 | return NULL; |
| 575 | |
| 576 | // FIXME: Refactor this into BugReporterContext. |
| 577 | Stmt *S = 0; |
| 578 | ProgramPoint P = N->getLocation(); |
| 579 | |
| 580 | if (BlockEdge *BE = dyn_cast<BlockEdge>(&P)) { |
| 581 | CFGBlock *BSrc = BE->getSrc(); |
| 582 | S = BSrc->getTerminatorCondition(); |
| 583 | } |
| 584 | else if (PostStmt *PS = dyn_cast<PostStmt>(&P)) { |
| 585 | S = PS->getStmt(); |
| 586 | } |
| 587 | |
| 588 | if (!S) |
| 589 | return NULL; |
| 590 | |
| 591 | // Construct a new PathDiagnosticPiece. |
| 592 | PathDiagnosticLocation L(S, BRC.getSourceManager()); |
| 593 | return new PathDiagnosticEventPiece(L, os.str()); |
| 594 | } |
| 595 | |
| 596 | return NULL; |
| 597 | } |
| 598 | }; |
| 599 | } // end anonymous namespace |
| 600 | |
| 601 | static void registerTrackConstraint(BugReporterContext& BRC, SVal Constraint, |
| 602 | bool Assumption) { |
| 603 | BRC.addVisitor(new TrackConstraintBRVisitor(Constraint, Assumption)); |
| 604 | } |
| 605 | |
| 606 | static void registerTrackNullValue(BugReporterContext& BRC, |
| 607 | const ExplodedNode<GRState>* N) { |
| 608 | |
| 609 | ProgramPoint P = N->getLocation(); |
| 610 | PostStmt *PS = dyn_cast<PostStmt>(&P); |
| 611 | |
| 612 | if (!PS) |
| 613 | return; |
| 614 | |
| 615 | Stmt *S = PS->getStmt(); |
| 616 | |
| 617 | if (ArraySubscriptExpr *AE = dyn_cast<ArraySubscriptExpr>(S)) { |
| 618 | S = AE->getBase(); |
| 619 | } |
| 620 | |
| 621 | SVal V = BRC.getStateManager().GetSValAsScalarOrLoc(N->getState(), S); |
| 622 | |
| 623 | // Uncomment this to find cases where we aren't properly getting the |
| 624 | // base value that was dereferenced. |
| 625 | // assert(!V.isUnknownOrUndef()); |
| 626 | |
| 627 | // For now just track when a symbolic value became null. |
| 628 | if (loc::MemRegionVal *L = dyn_cast<loc::MemRegionVal>(&V)) { |
| 629 | const SubRegion *R = cast<SubRegion>(L->getRegion()); |
| 630 | while (R && !isa<SymbolicRegion>(R)) { |
| 631 | R = dyn_cast<SubRegion>(R->getSuperRegion()); |
| 632 | } |
| 633 | |
| 634 | if (R) { |
| 635 | assert(isa<SymbolicRegion>(R)); |
| 636 | registerTrackConstraint(BRC, loc::MemRegionVal(R), false); |
| 637 | } |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 642 | // Check registration. |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 643 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 644 | |
| 645 | void GRExprEngine::RegisterInternalChecks() { |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 646 | // Register internal "built-in" BugTypes with the BugReporter. These BugTypes |
| 647 | // are different than what probably many checks will do since they don't |
| 648 | // create BugReports on-the-fly but instead wait until GRExprEngine finishes |
| 649 | // analyzing a function. Generation of BugReport objects is done via a call |
| 650 | // to 'FlushReports' from BugReporter. |
| 651 | BR.Register(new NullDeref(this)); |
| 652 | BR.Register(new UndefinedDeref(this)); |
| 653 | BR.Register(new UndefBranch(this)); |
| 654 | BR.Register(new DivZero(this)); |
| 655 | BR.Register(new UndefResult(this)); |
| 656 | BR.Register(new BadCall(this)); |
| 657 | BR.Register(new RetStack(this)); |
| 658 | BR.Register(new RetUndef(this)); |
| 659 | BR.Register(new BadArg(this)); |
| 660 | BR.Register(new BadMsgExprArg(this)); |
| 661 | BR.Register(new BadReceiver(this)); |
| 662 | BR.Register(new OutOfBoundMemoryAccess(this)); |
| 663 | BR.Register(new BadSizeVLA(this)); |
Ted Kremenek | 21fe837 | 2009-02-19 04:06:22 +0000 | [diff] [blame] | 664 | BR.Register(new NilReceiverStructRet(this)); |
Ted Kremenek | 899b3de | 2009-04-08 03:07:17 +0000 | [diff] [blame] | 665 | BR.Register(new NilReceiverLargerThanVoidPtrRet(this)); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 666 | |
| 667 | // The following checks do not need to have their associated BugTypes |
| 668 | // explicitly registered with the BugReporter. If they issue any BugReports, |
| 669 | // their associated BugType will get registered with the BugReporter |
| 670 | // automatically. Note that the check itself is owned by the GRExprEngine |
| 671 | // object. |
| 672 | AddCheck(new CheckAttrNonNull(BR), Stmt::CallExprClass); |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 673 | } |