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