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 | |
Ted Kremenek | 1053d24 | 2009-11-06 02:24:13 +0000 | [diff] [blame] | 15 | #include "GRExprEngineInternalChecks.h" |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 16 | #include "clang/Analysis/PathSensitive/BugReporter.h" |
| 17 | #include "clang/Analysis/PathSensitive/GRExprEngine.h" |
Ted Kremenek | c26a8b0 | 2009-07-22 21:46:56 +0000 | [diff] [blame] | 18 | #include "clang/Analysis/PathSensitive/CheckerVisitor.h" |
Ted Kremenek | dc998c1 | 2009-11-03 18:41:06 +0000 | [diff] [blame] | 19 | #include "clang/Analysis/PathSensitive/Checkers/DereferenceChecker.h" |
Zhongxing Xu | 4f64e5f | 2009-11-03 05:48:04 +0000 | [diff] [blame] | 20 | #include "clang/Analysis/PathSensitive/Checkers/BadCallChecker.h" |
Zhongxing Xu | 8958fff | 2009-11-03 06:46:03 +0000 | [diff] [blame] | 21 | #include "clang/Analysis/PathSensitive/Checkers/UndefinedArgChecker.h" |
Ted Kremenek | b107c4b | 2009-11-04 04:24:16 +0000 | [diff] [blame] | 22 | #include "clang/Analysis/PathSensitive/Checkers/UndefinedAssignmentChecker.h" |
Zhongxing Xu | 94943b6 | 2009-11-03 07:35:33 +0000 | [diff] [blame] | 23 | #include "clang/Analysis/PathSensitive/Checkers/AttrNonNullChecker.h" |
Ted Kremenek | dd986cc | 2009-05-07 00:45:33 +0000 | [diff] [blame] | 24 | #include "clang/Analysis/PathDiagnostic.h" |
Ted Kremenek | 8aed806 | 2008-10-31 00:13:20 +0000 | [diff] [blame] | 25 | #include "clang/Basic/SourceManager.h" |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Compiler.h" |
Ted Kremenek | ad51a60 | 2008-10-31 00:18:30 +0000 | [diff] [blame] | 27 | #include "llvm/Support/raw_ostream.h" |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 28 | |
| 29 | using namespace clang; |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 30 | using namespace clang::bugreporter; |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 31 | |
| 32 | //===----------------------------------------------------------------------===// |
| 33 | // Utility functions. |
| 34 | //===----------------------------------------------------------------------===// |
| 35 | |
| 36 | template <typename ITERATOR> inline |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 37 | ExplodedNode* GetNode(ITERATOR I) { |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 38 | return *I; |
| 39 | } |
| 40 | |
| 41 | template <> inline |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 42 | ExplodedNode* GetNode(GRExprEngine::undef_arg_iterator I) { |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 43 | return I->first; |
| 44 | } |
| 45 | |
| 46 | //===----------------------------------------------------------------------===// |
| 47 | // Bug Descriptions. |
| 48 | //===----------------------------------------------------------------------===// |
Zhongxing Xu | ec9227f | 2009-10-29 02:09:30 +0000 | [diff] [blame] | 49 | namespace clang { |
| 50 | class BuiltinBugReport : public RangedBugReport { |
Ted Kremenek | dd986cc | 2009-05-07 00:45:33 +0000 | [diff] [blame] | 51 | public: |
| 52 | BuiltinBugReport(BugType& bt, const char* desc, |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 53 | ExplodedNode *n) |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame] | 54 | : RangedBugReport(bt, desc, n) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 55 | |
Ted Kremenek | 85ac934 | 2009-05-15 05:25:09 +0000 | [diff] [blame] | 56 | BuiltinBugReport(BugType& bt, const char *shortDesc, const char *desc, |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 57 | ExplodedNode *n) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 58 | : RangedBugReport(bt, shortDesc, desc, n) {} |
| 59 | |
Ted Kremenek | dd986cc | 2009-05-07 00:45:33 +0000 | [diff] [blame] | 60 | void registerInitialVisitors(BugReporterContext& BRC, |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 61 | const ExplodedNode* N); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 62 | }; |
| 63 | |
Zhongxing Xu | ec9227f | 2009-10-29 02:09:30 +0000 | [diff] [blame] | 64 | void BuiltinBugReport::registerInitialVisitors(BugReporterContext& BRC, |
| 65 | const ExplodedNode* N) { |
| 66 | static_cast<BuiltinBug&>(getBugType()).registerInitialVisitors(BRC, N, this); |
| 67 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 68 | |
Ted Kremenek | dd986cc | 2009-05-07 00:45:33 +0000 | [diff] [blame] | 69 | template <typename ITER> |
| 70 | void BuiltinBug::Emit(BugReporter& BR, ITER I, ITER E) { |
| 71 | for (; I != E; ++I) BR.EmitReport(new BuiltinBugReport(*this, desc.c_str(), |
| 72 | GetNode(I))); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 73 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 74 | |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame] | 75 | class VISIBILITY_HIDDEN NilReceiverStructRet : public BuiltinBug { |
Ted Kremenek | 21fe837 | 2009-02-19 04:06:22 +0000 | [diff] [blame] | 76 | public: |
| 77 | NilReceiverStructRet(GRExprEngine* eng) : |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame] | 78 | BuiltinBug(eng, "'nil' receiver with struct return type") {} |
Ted Kremenek | 21fe837 | 2009-02-19 04:06:22 +0000 | [diff] [blame] | 79 | |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame] | 80 | void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) { |
Ted Kremenek | 21fe837 | 2009-02-19 04:06:22 +0000 | [diff] [blame] | 81 | for (GRExprEngine::nil_receiver_struct_ret_iterator |
| 82 | I=Eng.nil_receiver_struct_ret_begin(), |
| 83 | E=Eng.nil_receiver_struct_ret_end(); I!=E; ++I) { |
| 84 | |
| 85 | std::string sbuf; |
| 86 | llvm::raw_string_ostream os(sbuf); |
| 87 | PostStmt P = cast<PostStmt>((*I)->getLocation()); |
Ted Kremenek | 5f85e17 | 2009-07-22 22:35:28 +0000 | [diff] [blame] | 88 | const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(P.getStmt()); |
Ted Kremenek | 21fe837 | 2009-02-19 04:06:22 +0000 | [diff] [blame] | 89 | os << "The receiver in the message expression is 'nil' and results in the" |
| 90 | " returned value (of type '" |
| 91 | << ME->getType().getAsString() |
Ted Kremenek | 5b9bd21 | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 92 | << "') to be garbage or otherwise undefined"; |
Ted Kremenek | 21fe837 | 2009-02-19 04:06:22 +0000 | [diff] [blame] | 93 | |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame] | 94 | BuiltinBugReport *R = new BuiltinBugReport(*this, os.str().c_str(), *I); |
Ted Kremenek | 21fe837 | 2009-02-19 04:06:22 +0000 | [diff] [blame] | 95 | R->addRange(ME->getReceiver()->getSourceRange()); |
| 96 | BR.EmitReport(R); |
| 97 | } |
| 98 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 99 | |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame] | 100 | void registerInitialVisitors(BugReporterContext& BRC, |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 101 | const ExplodedNode* N, |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame] | 102 | BuiltinBugReport *R) { |
Ted Kremenek | 85ac934 | 2009-05-15 05:25:09 +0000 | [diff] [blame] | 103 | registerTrackNullOrUndefValue(BRC, GetReceiverExpr(N), N); |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame] | 104 | } |
Ted Kremenek | 21fe837 | 2009-02-19 04:06:22 +0000 | [diff] [blame] | 105 | }; |
Ted Kremenek | 899b3de | 2009-04-08 03:07:17 +0000 | [diff] [blame] | 106 | |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame] | 107 | class VISIBILITY_HIDDEN NilReceiverLargerThanVoidPtrRet : public BuiltinBug { |
Ted Kremenek | 899b3de | 2009-04-08 03:07:17 +0000 | [diff] [blame] | 108 | public: |
| 109 | NilReceiverLargerThanVoidPtrRet(GRExprEngine* eng) : |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame] | 110 | BuiltinBug(eng, |
| 111 | "'nil' receiver with return type larger than sizeof(void *)") {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 112 | |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame] | 113 | void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) { |
Ted Kremenek | 899b3de | 2009-04-08 03:07:17 +0000 | [diff] [blame] | 114 | for (GRExprEngine::nil_receiver_larger_than_voidptr_ret_iterator |
| 115 | I=Eng.nil_receiver_larger_than_voidptr_ret_begin(), |
| 116 | E=Eng.nil_receiver_larger_than_voidptr_ret_end(); I!=E; ++I) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 117 | |
Ted Kremenek | 899b3de | 2009-04-08 03:07:17 +0000 | [diff] [blame] | 118 | std::string sbuf; |
| 119 | llvm::raw_string_ostream os(sbuf); |
| 120 | PostStmt P = cast<PostStmt>((*I)->getLocation()); |
Ted Kremenek | 5f85e17 | 2009-07-22 22:35:28 +0000 | [diff] [blame] | 121 | const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(P.getStmt()); |
Ted Kremenek | 899b3de | 2009-04-08 03:07:17 +0000 | [diff] [blame] | 122 | os << "The receiver in the message expression is 'nil' and results in the" |
| 123 | " returned value (of type '" |
| 124 | << ME->getType().getAsString() |
| 125 | << "' and of size " |
| 126 | << Eng.getContext().getTypeSize(ME->getType()) / 8 |
Ted Kremenek | 5b9bd21 | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 127 | << " bytes) to be garbage or otherwise undefined"; |
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 | BuiltinBugReport *R = new BuiltinBugReport(*this, os.str().c_str(), *I); |
Ted Kremenek | 899b3de | 2009-04-08 03:07:17 +0000 | [diff] [blame] | 130 | R->addRange(ME->getReceiver()->getSourceRange()); |
| 131 | BR.EmitReport(R); |
| 132 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 133 | } |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame] | 134 | void registerInitialVisitors(BugReporterContext& BRC, |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 135 | const ExplodedNode* N, |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame] | 136 | BuiltinBugReport *R) { |
Ted Kremenek | 85ac934 | 2009-05-15 05:25:09 +0000 | [diff] [blame] | 137 | registerTrackNullOrUndefValue(BRC, GetReceiverExpr(N), N); |
Ted Kremenek | 899b3de | 2009-04-08 03:07:17 +0000 | [diff] [blame] | 138 | } |
| 139 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 140 | |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 141 | class VISIBILITY_HIDDEN UndefResult : public BuiltinBug { |
| 142 | public: |
Ted Kremenek | 5b9bd21 | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 143 | UndefResult(GRExprEngine* eng) |
| 144 | : BuiltinBug(eng,"Undefined or garbage result", |
| 145 | "Result of operation is garbage or undefined") {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 146 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 147 | void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) { |
Ted Kremenek | 5b9bd21 | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 148 | for (GRExprEngine::undef_result_iterator I=Eng.undef_results_begin(), |
| 149 | E = Eng.undef_results_end(); I!=E; ++I) { |
| 150 | |
| 151 | ExplodedNode *N = *I; |
| 152 | const Stmt *S = N->getLocationAs<PostStmt>()->getStmt(); |
| 153 | BuiltinBugReport *report = NULL; |
| 154 | |
| 155 | if (const BinaryOperator *B = dyn_cast<BinaryOperator>(S)) { |
| 156 | llvm::SmallString<256> sbuf; |
| 157 | llvm::raw_svector_ostream OS(sbuf); |
| 158 | const GRState *ST = N->getState(); |
Ted Kremenek | 7c039bf | 2009-09-16 06:04:26 +0000 | [diff] [blame] | 159 | const Expr *Ex = NULL; |
| 160 | bool isLeft = true; |
Ted Kremenek | 5b9bd21 | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 161 | |
| 162 | if (ST->getSVal(B->getLHS()).isUndef()) { |
| 163 | Ex = B->getLHS()->IgnoreParenCasts(); |
Ted Kremenek | 7c039bf | 2009-09-16 06:04:26 +0000 | [diff] [blame] | 164 | isLeft = true; |
Ted Kremenek | 5b9bd21 | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 165 | } |
Ted Kremenek | 24c411b | 2009-09-15 17:43:54 +0000 | [diff] [blame] | 166 | else if (ST->getSVal(B->getRHS()).isUndef()) { |
Ted Kremenek | 5b9bd21 | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 167 | Ex = B->getRHS()->IgnoreParenCasts(); |
Ted Kremenek | 7c039bf | 2009-09-16 06:04:26 +0000 | [diff] [blame] | 168 | isLeft = false; |
Ted Kremenek | 5b9bd21 | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 169 | } |
Ted Kremenek | 7c039bf | 2009-09-16 06:04:26 +0000 | [diff] [blame] | 170 | |
| 171 | if (Ex) { |
| 172 | OS << "The " << (isLeft ? "left" : "right") |
Ted Kremenek | 112ba7e | 2009-09-24 00:44:26 +0000 | [diff] [blame] | 173 | << " operand of '" |
Ted Kremenek | 7c039bf | 2009-09-16 06:04:26 +0000 | [diff] [blame] | 174 | << BinaryOperator::getOpcodeStr(B->getOpcode()) |
Ted Kremenek | 112ba7e | 2009-09-24 00:44:26 +0000 | [diff] [blame] | 175 | << "' is a garbage value"; |
Ted Kremenek | 24c411b | 2009-09-15 17:43:54 +0000 | [diff] [blame] | 176 | } |
| 177 | else { |
Ted Kremenek | 7c039bf | 2009-09-16 06:04:26 +0000 | [diff] [blame] | 178 | // Neither operand was undefined, but the result is undefined. |
Ted Kremenek | 24c411b | 2009-09-15 17:43:54 +0000 | [diff] [blame] | 179 | OS << "The result of the '" |
| 180 | << BinaryOperator::getOpcodeStr(B->getOpcode()) |
| 181 | << "' expression is undefined"; |
| 182 | } |
| 183 | |
Ted Kremenek | 5b9bd21 | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 184 | // FIXME: Use StringRefs to pass string information. |
| 185 | report = new BuiltinBugReport(*this, OS.str().str().c_str(), N); |
Ted Kremenek | 24c411b | 2009-09-15 17:43:54 +0000 | [diff] [blame] | 186 | if (Ex) report->addRange(Ex->getSourceRange()); |
Ted Kremenek | 5b9bd21 | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 187 | } |
| 188 | else { |
| 189 | report = new BuiltinBugReport(*this, |
| 190 | "Expression evaluates to an uninitialized" |
| 191 | " or undefined value", N); |
| 192 | } |
| 193 | |
| 194 | BR.EmitReport(report); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | void registerInitialVisitors(BugReporterContext& BRC, |
| 199 | const ExplodedNode* N, |
| 200 | BuiltinBugReport *R) { |
| 201 | |
| 202 | const Stmt *S = N->getLocationAs<StmtPoint>()->getStmt(); |
| 203 | const Stmt *X = S; |
| 204 | |
| 205 | if (const BinaryOperator *B = dyn_cast<BinaryOperator>(S)) { |
| 206 | const GRState *ST = N->getState(); |
Ted Kremenek | 7c039bf | 2009-09-16 06:04:26 +0000 | [diff] [blame] | 207 | if (ST->getSVal(B->getLHS()).isUndef()) |
| 208 | X = B->getLHS(); |
| 209 | else if (ST->getSVal(B->getRHS()).isUndef()) |
| 210 | X = B->getRHS(); |
Ted Kremenek | 5b9bd21 | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | registerTrackNullOrUndefValue(BRC, X, N); |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 214 | } |
| 215 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 216 | |
Ted Kremenek | 85ac934 | 2009-05-15 05:25:09 +0000 | [diff] [blame] | 217 | class VISIBILITY_HIDDEN ArgReport : public BuiltinBugReport { |
| 218 | const Stmt *Arg; |
| 219 | public: |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 220 | ArgReport(BugType& bt, const char* desc, ExplodedNode *n, |
Ted Kremenek | 85ac934 | 2009-05-15 05:25:09 +0000 | [diff] [blame] | 221 | const Stmt *arg) |
| 222 | : BuiltinBugReport(bt, desc, n), Arg(arg) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 223 | |
Ted Kremenek | 85ac934 | 2009-05-15 05:25:09 +0000 | [diff] [blame] | 224 | ArgReport(BugType& bt, const char *shortDesc, const char *desc, |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 225 | ExplodedNode *n, const Stmt *arg) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 226 | : BuiltinBugReport(bt, shortDesc, desc, n), Arg(arg) {} |
| 227 | |
| 228 | const Stmt *getArg() const { return Arg; } |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 229 | }; |
| 230 | |
| 231 | class VISIBILITY_HIDDEN BadArg : public BuiltinBug { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 232 | public: |
| 233 | BadArg(GRExprEngine* eng=0) : BuiltinBug(eng,"Uninitialized argument", |
Ted Kremenek | 5b9bd21 | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 234 | "Pass-by-value argument in function call is undefined") {} |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 235 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 236 | BadArg(GRExprEngine* eng, const char* d) |
Ted Kremenek | 5d88ff8 | 2009-04-02 02:40:26 +0000 | [diff] [blame] | 237 | : BuiltinBug(eng,"Uninitialized argument", d) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 238 | |
Ted Kremenek | 85ac934 | 2009-05-15 05:25:09 +0000 | [diff] [blame] | 239 | void registerInitialVisitors(BugReporterContext& BRC, |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 240 | const ExplodedNode* N, |
Ted Kremenek | 85ac934 | 2009-05-15 05:25:09 +0000 | [diff] [blame] | 241 | BuiltinBugReport *R) { |
| 242 | registerTrackNullOrUndefValue(BRC, static_cast<ArgReport*>(R)->getArg(), |
| 243 | N); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 244 | } |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 245 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 246 | |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 247 | class VISIBILITY_HIDDEN BadMsgExprArg : public BadArg { |
| 248 | public: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 249 | BadMsgExprArg(GRExprEngine* eng) |
Ted Kremenek | 5d88ff8 | 2009-04-02 02:40:26 +0000 | [diff] [blame] | 250 | : BadArg(eng,"Pass-by-value argument in message expression is undefined"){} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 251 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 252 | void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) { |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 253 | for (GRExprEngine::UndefArgsTy::iterator I=Eng.msg_expr_undef_arg_begin(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 254 | E = Eng.msg_expr_undef_arg_end(); I!=E; ++I) { |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 255 | // Generate a report for this bug. |
Ted Kremenek | 85ac934 | 2009-05-15 05:25:09 +0000 | [diff] [blame] | 256 | ArgReport *report = new ArgReport(*this, desc.c_str(), I->first, |
| 257 | I->second); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 258 | report->addRange(I->second->getSourceRange()); |
| 259 | BR.EmitReport(report); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 260 | } |
| 261 | } |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 262 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 263 | |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 264 | class VISIBILITY_HIDDEN BadReceiver : public BuiltinBug { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 265 | public: |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 266 | BadReceiver(GRExprEngine* eng) |
Ted Kremenek | 5d88ff8 | 2009-04-02 02:40:26 +0000 | [diff] [blame] | 267 | : BuiltinBug(eng,"Uninitialized receiver", |
| 268 | "Receiver in message expression is an uninitialized value") {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 269 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 270 | void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) { |
Ted Kremenek | efd5994 | 2008-12-08 22:47:34 +0000 | [diff] [blame] | 271 | for (GRExprEngine::ErrorNodes::iterator I=Eng.undef_receivers_begin(), |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 272 | End = Eng.undef_receivers_end(); I!=End; ++I) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 273 | |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 274 | // Generate a report for this bug. |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame] | 275 | BuiltinBugReport *report = new BuiltinBugReport(*this, desc.c_str(), *I); |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 276 | ExplodedNode* N = *I; |
Ted Kremenek | 5f85e17 | 2009-07-22 22:35:28 +0000 | [diff] [blame] | 277 | const Stmt *S = cast<PostStmt>(N->getLocation()).getStmt(); |
| 278 | const Expr* E = cast<ObjCMessageExpr>(S)->getReceiver(); |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 279 | assert (E && "Receiver cannot be NULL"); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 280 | report->addRange(E->getSourceRange()); |
| 281 | BR.EmitReport(report); |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame] | 282 | } |
| 283 | } |
Ted Kremenek | 85ac934 | 2009-05-15 05:25:09 +0000 | [diff] [blame] | 284 | |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame] | 285 | void registerInitialVisitors(BugReporterContext& BRC, |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 286 | const ExplodedNode* N, |
Ted Kremenek | 0c31317 | 2009-05-13 19:16:35 +0000 | [diff] [blame] | 287 | BuiltinBugReport *R) { |
Ted Kremenek | 85ac934 | 2009-05-15 05:25:09 +0000 | [diff] [blame] | 288 | registerTrackNullOrUndefValue(BRC, GetReceiverExpr(N), N); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 289 | } |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 290 | }; |
Ted Kremenek | 5917d78 | 2008-11-21 00:27:44 +0000 | [diff] [blame] | 291 | |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 292 | class VISIBILITY_HIDDEN UndefBranch : public BuiltinBug { |
| 293 | struct VISIBILITY_HIDDEN FindUndefExpr { |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 294 | GRStateManager& VM; |
| 295 | const GRState* St; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 296 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 297 | FindUndefExpr(GRStateManager& V, const GRState* S) : VM(V), St(S) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 298 | |
| 299 | Expr* FindExpr(Expr* Ex) { |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 300 | if (!MatchesCriteria(Ex)) |
Ted Kremenek | b7714b2 | 2008-07-30 17:49:12 +0000 | [diff] [blame] | 301 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 302 | |
Ted Kremenek | b7714b2 | 2008-07-30 17:49:12 +0000 | [diff] [blame] | 303 | 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] | 304 | if (Expr* ExI = dyn_cast_or_null<Expr>(*I)) { |
| 305 | Expr* E2 = FindExpr(ExI); |
| 306 | if (E2) return E2; |
| 307 | } |
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 | return Ex; |
| 310 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 311 | |
Ted Kremenek | 23ec48c | 2009-06-18 23:58:37 +0000 | [diff] [blame] | 312 | bool MatchesCriteria(Expr* Ex) { return St->getSVal(Ex).isUndef(); } |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 313 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 314 | |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 315 | public: |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 316 | UndefBranch(GRExprEngine *eng) |
Ted Kremenek | 5b9bd21 | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 317 | : BuiltinBug(eng,"Use of garbage value", |
| 318 | "Branch condition evaluates to an undefined or garbage value") |
| 319 | {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 320 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 321 | void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) { |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 322 | for (GRExprEngine::undef_branch_iterator I=Eng.undef_branches_begin(), |
| 323 | E=Eng.undef_branches_end(); I!=E; ++I) { |
| 324 | |
| 325 | // What's going on here: we want to highlight the subexpression of the |
| 326 | // condition that is the most likely source of the "uninitialized |
| 327 | // branch condition." We do a recursive walk of the condition's |
| 328 | // subexpressions and roughly look for the most nested subexpression |
| 329 | // that binds to Undefined. We then highlight that expression's range. |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 330 | BlockEdge B = cast<BlockEdge>((*I)->getLocation()); |
| 331 | Expr* Ex = cast<Expr>(B.getSrc()->getTerminatorCondition()); |
| 332 | assert (Ex && "Block must have a terminator."); |
| 333 | |
| 334 | // Get the predecessor node and check if is a PostStmt with the Stmt |
| 335 | // being the terminator condition. We want to inspect the state |
| 336 | // of that node instead because it will contain main information about |
| 337 | // the subexpressions. |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 338 | assert (!(*I)->pred_empty()); |
| 339 | |
| 340 | // Note: any predecessor will do. They should have identical state, |
| 341 | // since all the BlockEdge did was act as an error sink since the value |
| 342 | // had to already be undefined. |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 343 | ExplodedNode *N = *(*I)->pred_begin(); |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 344 | ProgramPoint P = N->getLocation(); |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 345 | const GRState* St = (*I)->getState(); |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 346 | |
| 347 | if (PostStmt* PS = dyn_cast<PostStmt>(&P)) |
| 348 | if (PS->getStmt() == Ex) |
| 349 | St = N->getState(); |
| 350 | |
| 351 | FindUndefExpr FindIt(Eng.getStateManager(), St); |
| 352 | Ex = FindIt.FindExpr(Ex); |
| 353 | |
Ted Kremenek | 85ac934 | 2009-05-15 05:25:09 +0000 | [diff] [blame] | 354 | ArgReport *R = new ArgReport(*this, desc.c_str(), *I, Ex); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 355 | R->addRange(Ex->getSourceRange()); |
| 356 | BR.EmitReport(R); |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 357 | } |
| 358 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 359 | |
Ted Kremenek | 85ac934 | 2009-05-15 05:25:09 +0000 | [diff] [blame] | 360 | void registerInitialVisitors(BugReporterContext& BRC, |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 361 | const ExplodedNode* N, |
Ted Kremenek | 85ac934 | 2009-05-15 05:25:09 +0000 | [diff] [blame] | 362 | BuiltinBugReport *R) { |
| 363 | registerTrackNullOrUndefValue(BRC, static_cast<ArgReport*>(R)->getArg(), |
| 364 | N); |
| 365 | } |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 366 | }; |
| 367 | |
Zhongxing Xu | 1c0c233 | 2008-11-23 05:52:28 +0000 | [diff] [blame] | 368 | class VISIBILITY_HIDDEN OutOfBoundMemoryAccess : public BuiltinBug { |
| 369 | public: |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 370 | OutOfBoundMemoryAccess(GRExprEngine* eng) |
Ted Kremenek | 5d88ff8 | 2009-04-02 02:40:26 +0000 | [diff] [blame] | 371 | : BuiltinBug(eng,"Out-of-bounds memory access", |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 372 | "Load or store into an out-of-bound memory position.") {} |
Zhongxing Xu | 1c0c233 | 2008-11-23 05:52:28 +0000 | [diff] [blame] | 373 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 374 | void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) { |
Zhongxing Xu | 1c0c233 | 2008-11-23 05:52:28 +0000 | [diff] [blame] | 375 | Emit(BR, Eng.explicit_oob_memacc_begin(), Eng.explicit_oob_memacc_end()); |
| 376 | } |
| 377 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 378 | |
Zhongxing Xu | ec9227f | 2009-10-29 02:09:30 +0000 | [diff] [blame] | 379 | } // end clang namespace |
Ted Kremenek | bc3a021 | 2009-10-30 17:24:47 +0000 | [diff] [blame] | 380 | |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 381 | //===----------------------------------------------------------------------===// |
| 382 | // Check registration. |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 383 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 384 | |
| 385 | void GRExprEngine::RegisterInternalChecks() { |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 386 | // Register internal "built-in" BugTypes with the BugReporter. These BugTypes |
| 387 | // are different than what probably many checks will do since they don't |
| 388 | // create BugReports on-the-fly but instead wait until GRExprEngine finishes |
| 389 | // analyzing a function. Generation of BugReport objects is done via a call |
| 390 | // to 'FlushReports' from BugReporter. |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 391 | BR.Register(new UndefBranch(this)); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 392 | BR.Register(new UndefResult(this)); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 393 | BR.Register(new BadMsgExprArg(this)); |
| 394 | BR.Register(new BadReceiver(this)); |
| 395 | BR.Register(new OutOfBoundMemoryAccess(this)); |
Ted Kremenek | 21fe837 | 2009-02-19 04:06:22 +0000 | [diff] [blame] | 396 | BR.Register(new NilReceiverStructRet(this)); |
Ted Kremenek | 899b3de | 2009-04-08 03:07:17 +0000 | [diff] [blame] | 397 | BR.Register(new NilReceiverLargerThanVoidPtrRet(this)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 398 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 399 | // The following checks do not need to have their associated BugTypes |
| 400 | // explicitly registered with the BugReporter. If they issue any BugReports, |
| 401 | // their associated BugType will get registered with the BugReporter |
| 402 | // automatically. Note that the check itself is owned by the GRExprEngine |
Ted Kremenek | 36df58a | 2009-11-06 20:47:51 +0000 | [diff] [blame] | 403 | // object. |
Ted Kremenek | b107c4b | 2009-11-04 04:24:16 +0000 | [diff] [blame] | 404 | registerCheck(new AttrNonNullChecker()); |
| 405 | registerCheck(new UndefinedArgChecker()); |
| 406 | registerCheck(new UndefinedAssignmentChecker()); |
| 407 | registerCheck(new BadCallChecker()); |
Ted Kremenek | b107c4b | 2009-11-04 04:24:16 +0000 | [diff] [blame] | 408 | registerCheck(new UndefDerefChecker()); |
| 409 | registerCheck(new NullDerefChecker()); |
Ted Kremenek | 84b3595 | 2009-11-06 21:51:50 +0000 | [diff] [blame] | 410 | |
| 411 | RegisterVLASizeChecker(*this); |
| 412 | RegisterDivZeroChecker(*this); |
| 413 | RegisterReturnStackAddressChecker(*this); |
| 414 | RegisterReturnUndefChecker(*this); |
Zhongxing Xu | 3ce2dc3 | 2009-11-09 05:34:10 +0000 | [diff] [blame^] | 415 | RegisterPointerSubChecker(*this); |
Ted Kremenek | 84b3595 | 2009-11-06 21:51:50 +0000 | [diff] [blame] | 416 | |
| 417 | // Note that this must be registered after ReturnStackAddressChecker. |
| 418 | RegisterReturnPointerRangeChecker(*this); |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 419 | } |