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