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 | 8aed806 | 2008-10-31 00:13:20 +0000 | [diff] [blame] | 17 | #include "clang/Basic/SourceManager.h" |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Compiler.h" |
Ted Kremenek | ad51a60 | 2008-10-31 00:18:30 +0000 | [diff] [blame] | 19 | #include "llvm/Support/raw_ostream.h" |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 20 | |
| 21 | using namespace clang; |
| 22 | |
| 23 | //===----------------------------------------------------------------------===// |
| 24 | // Utility functions. |
| 25 | //===----------------------------------------------------------------------===// |
| 26 | |
| 27 | template <typename ITERATOR> inline |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 28 | ExplodedNode<GRState>* GetNode(ITERATOR I) { |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 29 | return *I; |
| 30 | } |
| 31 | |
| 32 | template <> inline |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 33 | ExplodedNode<GRState>* GetNode(GRExprEngine::undef_arg_iterator I) { |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 34 | return I->first; |
| 35 | } |
| 36 | |
| 37 | //===----------------------------------------------------------------------===// |
| 38 | // Bug Descriptions. |
| 39 | //===----------------------------------------------------------------------===// |
| 40 | |
| 41 | namespace { |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 42 | class VISIBILITY_HIDDEN BuiltinBug : public BugType { |
| 43 | GRExprEngine &Eng; |
Ted Kremenek | 159d248 | 2008-12-09 00:44:16 +0000 | [diff] [blame] | 44 | protected: |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 45 | const std::string desc; |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 46 | public: |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 47 | BuiltinBug(GRExprEngine *eng, const char* n, const char* d) |
| 48 | : BugType(n, "Logic Errors"), Eng(*eng), desc(d) {} |
| 49 | |
| 50 | BuiltinBug(GRExprEngine *eng, const char* n) |
| 51 | : BugType(n, "Logic Errors"), Eng(*eng), desc(n) {} |
Ted Kremenek | 22bda88 | 2008-07-31 20:31:27 +0000 | [diff] [blame] | 52 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 53 | virtual void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) = 0; |
| 54 | |
| 55 | void FlushReports(BugReporter& BR) { FlushReportsImpl(BR, Eng); } |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 56 | |
| 57 | template <typename ITER> |
| 58 | void Emit(BugReporter& BR, ITER I, ITER E) { |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 59 | for (; I != E; ++I) BR.EmitReport(new BugReport(*this, desc.c_str(), |
| 60 | GetNode(I))); |
| 61 | } |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | class VISIBILITY_HIDDEN NullDeref : public BuiltinBug { |
| 65 | public: |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 66 | NullDeref(GRExprEngine* eng) |
Ted Kremenek | 0fa9654 | 2009-04-07 04:54:31 +0000 | [diff] [blame^] | 67 | : BuiltinBug(eng,"Null dereference", "Dereference of null pointer") {} |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 68 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 69 | void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) { |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 70 | Emit(BR, Eng.null_derefs_begin(), Eng.null_derefs_end()); |
| 71 | } |
| 72 | }; |
| 73 | |
Ted Kremenek | 21fe837 | 2009-02-19 04:06:22 +0000 | [diff] [blame] | 74 | class VISIBILITY_HIDDEN NilReceiverStructRet : public BugType { |
| 75 | GRExprEngine &Eng; |
| 76 | public: |
| 77 | NilReceiverStructRet(GRExprEngine* eng) : |
Ted Kremenek | 5d88ff8 | 2009-04-02 02:40:26 +0000 | [diff] [blame] | 78 | BugType("'nil' receiver with struct return type", "Logic Errors"), |
Ted Kremenek | 21fe837 | 2009-02-19 04:06:22 +0000 | [diff] [blame] | 79 | Eng(*eng) {} |
| 80 | |
| 81 | void FlushReports(BugReporter& BR) { |
| 82 | for (GRExprEngine::nil_receiver_struct_ret_iterator |
| 83 | I=Eng.nil_receiver_struct_ret_begin(), |
| 84 | E=Eng.nil_receiver_struct_ret_end(); I!=E; ++I) { |
| 85 | |
| 86 | std::string sbuf; |
| 87 | llvm::raw_string_ostream os(sbuf); |
| 88 | PostStmt P = cast<PostStmt>((*I)->getLocation()); |
| 89 | ObjCMessageExpr *ME = cast<ObjCMessageExpr>(P.getStmt()); |
| 90 | os << "The receiver in the message expression is 'nil' and results in the" |
| 91 | " returned value (of type '" |
| 92 | << ME->getType().getAsString() |
| 93 | << "') to be garbage or otherwise undefined."; |
| 94 | |
| 95 | RangedBugReport *R = new RangedBugReport(*this, os.str().c_str(), *I); |
| 96 | R->addRange(ME->getReceiver()->getSourceRange()); |
| 97 | BR.EmitReport(R); |
| 98 | } |
| 99 | } |
| 100 | }; |
| 101 | |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 102 | class VISIBILITY_HIDDEN UndefinedDeref : public BuiltinBug { |
| 103 | public: |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 104 | UndefinedDeref(GRExprEngine* eng) |
Ted Kremenek | 17a8e07 | 2009-03-01 05:43:22 +0000 | [diff] [blame] | 105 | : BuiltinBug(eng,"Dereference of undefined pointer value") {} |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 106 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 107 | void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) { |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 108 | Emit(BR, Eng.undef_derefs_begin(), Eng.undef_derefs_end()); |
| 109 | } |
| 110 | }; |
| 111 | |
| 112 | class VISIBILITY_HIDDEN DivZero : public BuiltinBug { |
| 113 | public: |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 114 | DivZero(GRExprEngine* eng) |
Ted Kremenek | 5d88ff8 | 2009-04-02 02:40:26 +0000 | [diff] [blame] | 115 | : BuiltinBug(eng,"Division-by-zero", |
| 116 | "Division by zero or undefined value.") {} |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 117 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 118 | void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) { |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 119 | Emit(BR, Eng.explicit_bad_divides_begin(), Eng.explicit_bad_divides_end()); |
| 120 | } |
| 121 | }; |
| 122 | |
| 123 | class VISIBILITY_HIDDEN UndefResult : public BuiltinBug { |
| 124 | public: |
Ted Kremenek | 5d88ff8 | 2009-04-02 02:40:26 +0000 | [diff] [blame] | 125 | UndefResult(GRExprEngine* eng) : BuiltinBug(eng,"Undefined result", |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 126 | "Result of operation is undefined.") {} |
| 127 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 128 | void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) { |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 129 | Emit(BR, Eng.undef_results_begin(), Eng.undef_results_end()); |
| 130 | } |
| 131 | }; |
| 132 | |
| 133 | class VISIBILITY_HIDDEN BadCall : public BuiltinBug { |
| 134 | public: |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 135 | BadCall(GRExprEngine *eng) |
Ted Kremenek | 5d88ff8 | 2009-04-02 02:40:26 +0000 | [diff] [blame] | 136 | : BuiltinBug(eng, "Invalid function call", |
Ted Kremenek | 17a8e07 | 2009-03-01 05:43:22 +0000 | [diff] [blame] | 137 | "Called function pointer is a null or undefined pointer value") {} |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 138 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 139 | void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) { |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 140 | Emit(BR, Eng.bad_calls_begin(), Eng.bad_calls_end()); |
| 141 | } |
| 142 | }; |
| 143 | |
| 144 | class VISIBILITY_HIDDEN BadArg : public BuiltinBug { |
| 145 | public: |
Ted Kremenek | 5d88ff8 | 2009-04-02 02:40:26 +0000 | [diff] [blame] | 146 | BadArg(GRExprEngine* eng) : BuiltinBug(eng,"Uninitialized argument", |
Ted Kremenek | 17a8e07 | 2009-03-01 05:43:22 +0000 | [diff] [blame] | 147 | "Pass-by-value argument in function call is undefined.") {} |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 148 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 149 | BadArg(GRExprEngine* eng, const char* d) |
Ted Kremenek | 5d88ff8 | 2009-04-02 02:40:26 +0000 | [diff] [blame] | 150 | : BuiltinBug(eng,"Uninitialized argument", d) {} |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 151 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 152 | void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) { |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 153 | for (GRExprEngine::UndefArgsTy::iterator I = Eng.undef_arg_begin(), |
| 154 | E = Eng.undef_arg_end(); I!=E; ++I) { |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 155 | // Generate a report for this bug. |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 156 | RangedBugReport *report = new RangedBugReport(*this, desc.c_str(), |
| 157 | I->first); |
| 158 | report->addRange(I->second->getSourceRange()); |
| 159 | BR.EmitReport(report); |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | }; |
| 163 | |
| 164 | class VISIBILITY_HIDDEN BadMsgExprArg : public BadArg { |
| 165 | public: |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 166 | BadMsgExprArg(GRExprEngine* eng) |
Ted Kremenek | 5d88ff8 | 2009-04-02 02:40:26 +0000 | [diff] [blame] | 167 | : BadArg(eng,"Pass-by-value argument in message expression is undefined"){} |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 168 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 169 | void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) { |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 170 | for (GRExprEngine::UndefArgsTy::iterator I=Eng.msg_expr_undef_arg_begin(), |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 171 | E = Eng.msg_expr_undef_arg_end(); I!=E; ++I) { |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 172 | // Generate a report for this bug. |
Ted Kremenek | 21fe837 | 2009-02-19 04:06:22 +0000 | [diff] [blame] | 173 | RangedBugReport *report = new RangedBugReport(*this, desc.c_str(), |
| 174 | I->first); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 175 | report->addRange(I->second->getSourceRange()); |
| 176 | BR.EmitReport(report); |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | }; |
| 180 | |
| 181 | class VISIBILITY_HIDDEN BadReceiver : public BuiltinBug { |
| 182 | public: |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 183 | BadReceiver(GRExprEngine* eng) |
Ted Kremenek | 5d88ff8 | 2009-04-02 02:40:26 +0000 | [diff] [blame] | 184 | : BuiltinBug(eng,"Uninitialized receiver", |
| 185 | "Receiver in message expression is an uninitialized value") {} |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 186 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 187 | void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) { |
Ted Kremenek | efd5994 | 2008-12-08 22:47:34 +0000 | [diff] [blame] | 188 | for (GRExprEngine::ErrorNodes::iterator I=Eng.undef_receivers_begin(), |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 189 | End = Eng.undef_receivers_end(); I!=End; ++I) { |
| 190 | |
| 191 | // Generate a report for this bug. |
Ted Kremenek | 5d88ff8 | 2009-04-02 02:40:26 +0000 | [diff] [blame] | 192 | RangedBugReport *report = new RangedBugReport(*this, desc.c_str(), *I); |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 193 | ExplodedNode<GRState>* N = *I; |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 194 | Stmt *S = cast<PostStmt>(N->getLocation()).getStmt(); |
| 195 | Expr* E = cast<ObjCMessageExpr>(S)->getReceiver(); |
| 196 | assert (E && "Receiver cannot be NULL"); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 197 | report->addRange(E->getSourceRange()); |
| 198 | BR.EmitReport(report); |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | }; |
Ted Kremenek | 5917d78 | 2008-11-21 00:27:44 +0000 | [diff] [blame] | 202 | |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 203 | class VISIBILITY_HIDDEN RetStack : public BuiltinBug { |
| 204 | public: |
Ted Kremenek | 17a8e07 | 2009-03-01 05:43:22 +0000 | [diff] [blame] | 205 | RetStack(GRExprEngine* eng) |
Ted Kremenek | 5d88ff8 | 2009-04-02 02:40:26 +0000 | [diff] [blame] | 206 | : BuiltinBug(eng, "Return of address to stack-allocated memory") {} |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 207 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 208 | void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) { |
Ted Kremenek | b7714b2 | 2008-07-30 17:49:12 +0000 | [diff] [blame] | 209 | for (GRExprEngine::ret_stackaddr_iterator I=Eng.ret_stackaddr_begin(), |
| 210 | End = Eng.ret_stackaddr_end(); I!=End; ++I) { |
Ted Kremenek | 22bda88 | 2008-07-31 20:31:27 +0000 | [diff] [blame] | 211 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 212 | ExplodedNode<GRState>* N = *I; |
Ted Kremenek | b7714b2 | 2008-07-30 17:49:12 +0000 | [diff] [blame] | 213 | Stmt *S = cast<PostStmt>(N->getLocation()).getStmt(); |
| 214 | Expr* E = cast<ReturnStmt>(S)->getRetValue(); |
| 215 | assert (E && "Return expression cannot be NULL"); |
Ted Kremenek | 22bda88 | 2008-07-31 20:31:27 +0000 | [diff] [blame] | 216 | |
| 217 | // Get the value associated with E. |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 218 | loc::MemRegionVal V = |
| 219 | cast<loc::MemRegionVal>(Eng.getStateManager().GetSVal(N->getState(), |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 220 | E)); |
Ted Kremenek | 22bda88 | 2008-07-31 20:31:27 +0000 | [diff] [blame] | 221 | |
| 222 | // Generate a report for this bug. |
Ted Kremenek | ad51a60 | 2008-10-31 00:18:30 +0000 | [diff] [blame] | 223 | std::string buf; |
| 224 | llvm::raw_string_ostream os(buf); |
Ted Kremenek | 8aed806 | 2008-10-31 00:13:20 +0000 | [diff] [blame] | 225 | SourceRange R; |
Ted Kremenek | 22bda88 | 2008-07-31 20:31:27 +0000 | [diff] [blame] | 226 | |
Ted Kremenek | 8aed806 | 2008-10-31 00:13:20 +0000 | [diff] [blame] | 227 | // Check if the region is a compound literal. |
| 228 | if (const CompoundLiteralRegion* CR = |
| 229 | dyn_cast<CompoundLiteralRegion>(V.getRegion())) { |
| 230 | |
| 231 | const CompoundLiteralExpr* CL = CR->getLiteralExpr(); |
| 232 | os << "Address of stack memory associated with a compound literal " |
| 233 | "declared on line " |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 234 | << BR.getSourceManager() |
| 235 | .getInstantiationLineNumber(CL->getLocStart()) |
Ted Kremenek | 8aed806 | 2008-10-31 00:13:20 +0000 | [diff] [blame] | 236 | << " returned."; |
| 237 | |
| 238 | R = CL->getSourceRange(); |
| 239 | } |
Ted Kremenek | de8cd19 | 2008-11-02 00:35:25 +0000 | [diff] [blame] | 240 | else if (const AllocaRegion* AR = dyn_cast<AllocaRegion>(V.getRegion())) { |
| 241 | const Expr* ARE = AR->getExpr(); |
| 242 | SourceLocation L = ARE->getLocStart(); |
| 243 | R = ARE->getSourceRange(); |
| 244 | |
| 245 | os << "Address of stack memory allocated by call to alloca() on line " |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 246 | << BR.getSourceManager().getInstantiationLineNumber(L) |
Ted Kremenek | de8cd19 | 2008-11-02 00:35:25 +0000 | [diff] [blame] | 247 | << " returned."; |
| 248 | } |
Ted Kremenek | 8aed806 | 2008-10-31 00:13:20 +0000 | [diff] [blame] | 249 | else { |
| 250 | os << "Address of stack memory associated with local variable '" |
| 251 | << V.getRegion()->getString() << "' returned."; |
| 252 | } |
Ted Kremenek | 22bda88 | 2008-07-31 20:31:27 +0000 | [diff] [blame] | 253 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 254 | RangedBugReport *report = new RangedBugReport(*this, os.str().c_str(), N); |
| 255 | report->addRange(E->getSourceRange()); |
| 256 | if (R.isValid()) report->addRange(R); |
| 257 | BR.EmitReport(report); |
Ted Kremenek | b7714b2 | 2008-07-30 17:49:12 +0000 | [diff] [blame] | 258 | } |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 259 | } |
| 260 | }; |
Ted Kremenek | 5917d78 | 2008-11-21 00:27:44 +0000 | [diff] [blame] | 261 | |
| 262 | class VISIBILITY_HIDDEN RetUndef : public BuiltinBug { |
| 263 | public: |
Ted Kremenek | 5d88ff8 | 2009-04-02 02:40:26 +0000 | [diff] [blame] | 264 | RetUndef(GRExprEngine* eng) : BuiltinBug(eng, "Uninitialized return value", |
Ted Kremenek | 5917d78 | 2008-11-21 00:27:44 +0000 | [diff] [blame] | 265 | "Uninitialized or undefined return value returned to caller.") {} |
| 266 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 267 | void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) { |
Ted Kremenek | 5917d78 | 2008-11-21 00:27:44 +0000 | [diff] [blame] | 268 | Emit(BR, Eng.ret_undef_begin(), Eng.ret_undef_end()); |
| 269 | } |
| 270 | }; |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 271 | |
| 272 | class VISIBILITY_HIDDEN UndefBranch : public BuiltinBug { |
| 273 | struct VISIBILITY_HIDDEN FindUndefExpr { |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 274 | GRStateManager& VM; |
| 275 | const GRState* St; |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 276 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 277 | FindUndefExpr(GRStateManager& V, const GRState* S) : VM(V), St(S) {} |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 278 | |
Ted Kremenek | b7714b2 | 2008-07-30 17:49:12 +0000 | [diff] [blame] | 279 | Expr* FindExpr(Expr* Ex) { |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 280 | if (!MatchesCriteria(Ex)) |
Ted Kremenek | b7714b2 | 2008-07-30 17:49:12 +0000 | [diff] [blame] | 281 | return 0; |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 282 | |
Ted Kremenek | b7714b2 | 2008-07-30 17:49:12 +0000 | [diff] [blame] | 283 | 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] | 284 | if (Expr* ExI = dyn_cast_or_null<Expr>(*I)) { |
| 285 | Expr* E2 = FindExpr(ExI); |
| 286 | if (E2) return E2; |
| 287 | } |
| 288 | |
| 289 | return Ex; |
| 290 | } |
| 291 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 292 | bool MatchesCriteria(Expr* Ex) { return VM.GetSVal(St, Ex).isUndef(); } |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 293 | }; |
| 294 | |
| 295 | public: |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 296 | UndefBranch(GRExprEngine *eng) |
Ted Kremenek | 5d88ff8 | 2009-04-02 02:40:26 +0000 | [diff] [blame] | 297 | : BuiltinBug(eng,"Use of uninitialized value", |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 298 | "Branch condition evaluates to an uninitialized value.") {} |
| 299 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 300 | void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) { |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 301 | for (GRExprEngine::undef_branch_iterator I=Eng.undef_branches_begin(), |
| 302 | E=Eng.undef_branches_end(); I!=E; ++I) { |
| 303 | |
| 304 | // What's going on here: we want to highlight the subexpression of the |
| 305 | // condition that is the most likely source of the "uninitialized |
| 306 | // branch condition." We do a recursive walk of the condition's |
| 307 | // subexpressions and roughly look for the most nested subexpression |
| 308 | // that binds to Undefined. We then highlight that expression's range. |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 309 | BlockEdge B = cast<BlockEdge>((*I)->getLocation()); |
| 310 | Expr* Ex = cast<Expr>(B.getSrc()->getTerminatorCondition()); |
| 311 | assert (Ex && "Block must have a terminator."); |
| 312 | |
| 313 | // Get the predecessor node and check if is a PostStmt with the Stmt |
| 314 | // being the terminator condition. We want to inspect the state |
| 315 | // of that node instead because it will contain main information about |
| 316 | // the subexpressions. |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 317 | assert (!(*I)->pred_empty()); |
| 318 | |
| 319 | // Note: any predecessor will do. They should have identical state, |
| 320 | // since all the BlockEdge did was act as an error sink since the value |
| 321 | // had to already be undefined. |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 322 | ExplodedNode<GRState> *N = *(*I)->pred_begin(); |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 323 | ProgramPoint P = N->getLocation(); |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 324 | const GRState* St = (*I)->getState(); |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 325 | |
| 326 | if (PostStmt* PS = dyn_cast<PostStmt>(&P)) |
| 327 | if (PS->getStmt() == Ex) |
| 328 | St = N->getState(); |
| 329 | |
| 330 | FindUndefExpr FindIt(Eng.getStateManager(), St); |
| 331 | Ex = FindIt.FindExpr(Ex); |
| 332 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 333 | RangedBugReport *R = new RangedBugReport(*this, desc.c_str(), *I); |
| 334 | R->addRange(Ex->getSourceRange()); |
| 335 | BR.EmitReport(R); |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 336 | } |
| 337 | } |
| 338 | }; |
| 339 | |
Zhongxing Xu | 1c0c233 | 2008-11-23 05:52:28 +0000 | [diff] [blame] | 340 | class VISIBILITY_HIDDEN OutOfBoundMemoryAccess : public BuiltinBug { |
| 341 | public: |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 342 | OutOfBoundMemoryAccess(GRExprEngine* eng) |
Ted Kremenek | 5d88ff8 | 2009-04-02 02:40:26 +0000 | [diff] [blame] | 343 | : BuiltinBug(eng,"Out-of-bounds memory access", |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 344 | "Load or store into an out-of-bound memory position.") {} |
Zhongxing Xu | 1c0c233 | 2008-11-23 05:52:28 +0000 | [diff] [blame] | 345 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 346 | void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) { |
Zhongxing Xu | 1c0c233 | 2008-11-23 05:52:28 +0000 | [diff] [blame] | 347 | Emit(BR, Eng.explicit_oob_memacc_begin(), Eng.explicit_oob_memacc_end()); |
| 348 | } |
| 349 | }; |
Ted Kremenek | efd5994 | 2008-12-08 22:47:34 +0000 | [diff] [blame] | 350 | |
Ted Kremenek | 159d248 | 2008-12-09 00:44:16 +0000 | [diff] [blame] | 351 | class VISIBILITY_HIDDEN BadSizeVLA : public BuiltinBug { |
Ted Kremenek | efd5994 | 2008-12-08 22:47:34 +0000 | [diff] [blame] | 352 | public: |
Ted Kremenek | 5d88ff8 | 2009-04-02 02:40:26 +0000 | [diff] [blame] | 353 | BadSizeVLA(GRExprEngine* eng) : |
| 354 | BuiltinBug(eng, "Bad variable-length array (VLA) size") {} |
Ted Kremenek | efd5994 | 2008-12-08 22:47:34 +0000 | [diff] [blame] | 355 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 356 | void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) { |
Ted Kremenek | efd5994 | 2008-12-08 22:47:34 +0000 | [diff] [blame] | 357 | for (GRExprEngine::ErrorNodes::iterator |
Ted Kremenek | 159d248 | 2008-12-09 00:44:16 +0000 | [diff] [blame] | 358 | I = Eng.ExplicitBadSizedVLA.begin(), |
| 359 | E = Eng.ExplicitBadSizedVLA.end(); I!=E; ++I) { |
| 360 | |
| 361 | // Determine whether this was a 'zero-sized' VLA or a VLA with an |
| 362 | // undefined size. |
| 363 | GRExprEngine::NodeTy* N = *I; |
| 364 | PostStmt PS = cast<PostStmt>(N->getLocation()); |
Ted Kremenek | efd5994 | 2008-12-08 22:47:34 +0000 | [diff] [blame] | 365 | DeclStmt *DS = cast<DeclStmt>(PS.getStmt()); |
| 366 | VarDecl* VD = cast<VarDecl>(*DS->decl_begin()); |
| 367 | QualType T = Eng.getContext().getCanonicalType(VD->getType()); |
| 368 | VariableArrayType* VT = cast<VariableArrayType>(T); |
Ted Kremenek | 159d248 | 2008-12-09 00:44:16 +0000 | [diff] [blame] | 369 | Expr* SizeExpr = VT->getSizeExpr(); |
Ted Kremenek | efd5994 | 2008-12-08 22:47:34 +0000 | [diff] [blame] | 370 | |
Ted Kremenek | 159d248 | 2008-12-09 00:44:16 +0000 | [diff] [blame] | 371 | std::string buf; |
| 372 | llvm::raw_string_ostream os(buf); |
Ted Kremenek | 5d88ff8 | 2009-04-02 02:40:26 +0000 | [diff] [blame] | 373 | os << "The expression used to specify the number of elements in the " |
| 374 | "variable-length array (VLA) '" |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 375 | << VD->getNameAsString() << "' evaluates to "; |
Ted Kremenek | 159d248 | 2008-12-09 00:44:16 +0000 | [diff] [blame] | 376 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 377 | if (Eng.getStateManager().GetSVal(N->getState(), SizeExpr).isUndef()) |
Ted Kremenek | 159d248 | 2008-12-09 00:44:16 +0000 | [diff] [blame] | 378 | os << "an undefined or garbage value."; |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 379 | else |
| 380 | os << "0. VLAs with no elements have undefined behavior."; |
Ted Kremenek | 159d248 | 2008-12-09 00:44:16 +0000 | [diff] [blame] | 381 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 382 | RangedBugReport *report = new RangedBugReport(*this, os.str().c_str(), N); |
| 383 | report->addRange(SizeExpr->getSourceRange()); |
| 384 | BR.EmitReport(report); |
Ted Kremenek | efd5994 | 2008-12-08 22:47:34 +0000 | [diff] [blame] | 385 | } |
| 386 | } |
| 387 | }; |
Zhongxing Xu | 1c0c233 | 2008-11-23 05:52:28 +0000 | [diff] [blame] | 388 | |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 389 | //===----------------------------------------------------------------------===// |
| 390 | // __attribute__(nonnull) checking |
| 391 | |
| 392 | class VISIBILITY_HIDDEN CheckAttrNonNull : public GRSimpleAPICheck { |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 393 | BugType *BT; |
| 394 | BugReporter &BR; |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 395 | |
| 396 | public: |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 397 | CheckAttrNonNull(BugReporter &br) : BT(0), BR(br) {} |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 398 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 399 | virtual bool Audit(ExplodedNode<GRState>* N, GRStateManager& VMgr) { |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 400 | CallExpr* CE = cast<CallExpr>(cast<PostStmt>(N->getLocation()).getStmt()); |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 401 | const GRState* state = N->getState(); |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 402 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 403 | SVal X = VMgr.GetSVal(state, CE->getCallee()); |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 404 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 405 | if (!isa<loc::FuncVal>(X)) |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 406 | return false; |
| 407 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 408 | FunctionDecl* FD = dyn_cast<FunctionDecl>(cast<loc::FuncVal>(X).getDecl()); |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 409 | const NonNullAttr* Att = FD->getAttr<NonNullAttr>(); |
| 410 | |
| 411 | if (!Att) |
| 412 | return false; |
| 413 | |
| 414 | // Iterate through the arguments of CE and check them for null. |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 415 | unsigned idx = 0; |
| 416 | bool hasError = false; |
| 417 | |
| 418 | for (CallExpr::arg_iterator I=CE->arg_begin(), E=CE->arg_end(); I!=E; |
| 419 | ++I, ++idx) { |
| 420 | |
| 421 | if (!VMgr.isEqual(state, *I, 0) || !Att->isNonNull(idx)) |
| 422 | continue; |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 423 | |
| 424 | // Lazily allocate the BugType object if it hasn't already been created. |
| 425 | // Ownership is transferred to the BugReporter object once the BugReport |
| 426 | // is passed to 'EmitWarning'. |
Ted Kremenek | 5d88ff8 | 2009-04-02 02:40:26 +0000 | [diff] [blame] | 427 | if (!BT) BT = |
| 428 | new BugType("Argument with 'nonnull' attribute passed null", "API"); |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 429 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 430 | RangedBugReport *R = new RangedBugReport(*BT, |
| 431 | "Null pointer passed as an argument to a " |
| 432 | "'nonnull' parameter", N); |
| 433 | |
| 434 | R->addRange((*I)->getSourceRange()); |
| 435 | BR.EmitReport(R); |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 436 | hasError = true; |
| 437 | } |
| 438 | |
| 439 | return hasError; |
| 440 | } |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 441 | }; |
| 442 | } // end anonymous namespace |
| 443 | |
| 444 | //===----------------------------------------------------------------------===// |
| 445 | // Check registration. |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 446 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 447 | |
| 448 | void GRExprEngine::RegisterInternalChecks() { |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 449 | // Register internal "built-in" BugTypes with the BugReporter. These BugTypes |
| 450 | // are different than what probably many checks will do since they don't |
| 451 | // create BugReports on-the-fly but instead wait until GRExprEngine finishes |
| 452 | // analyzing a function. Generation of BugReport objects is done via a call |
| 453 | // to 'FlushReports' from BugReporter. |
| 454 | BR.Register(new NullDeref(this)); |
| 455 | BR.Register(new UndefinedDeref(this)); |
| 456 | BR.Register(new UndefBranch(this)); |
| 457 | BR.Register(new DivZero(this)); |
| 458 | BR.Register(new UndefResult(this)); |
| 459 | BR.Register(new BadCall(this)); |
| 460 | BR.Register(new RetStack(this)); |
| 461 | BR.Register(new RetUndef(this)); |
| 462 | BR.Register(new BadArg(this)); |
| 463 | BR.Register(new BadMsgExprArg(this)); |
| 464 | BR.Register(new BadReceiver(this)); |
| 465 | BR.Register(new OutOfBoundMemoryAccess(this)); |
| 466 | BR.Register(new BadSizeVLA(this)); |
Ted Kremenek | 21fe837 | 2009-02-19 04:06:22 +0000 | [diff] [blame] | 467 | BR.Register(new NilReceiverStructRet(this)); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 468 | |
| 469 | // The following checks do not need to have their associated BugTypes |
| 470 | // explicitly registered with the BugReporter. If they issue any BugReports, |
| 471 | // their associated BugType will get registered with the BugReporter |
| 472 | // automatically. Note that the check itself is owned by the GRExprEngine |
| 473 | // object. |
| 474 | AddCheck(new CheckAttrNonNull(BR), Stmt::CallExprClass); |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 475 | } |