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