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