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