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