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 | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 65 | class VISIBILITY_HIDDEN UndefResult : public BuiltinBug { |
| 66 | public: |
Ted Kremenek | 5b9bd21 | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 67 | UndefResult(GRExprEngine* eng) |
| 68 | : BuiltinBug(eng,"Undefined or garbage result", |
| 69 | "Result of operation is garbage or undefined") {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 70 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 71 | void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) { |
Ted Kremenek | 5b9bd21 | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 72 | for (GRExprEngine::undef_result_iterator I=Eng.undef_results_begin(), |
| 73 | E = Eng.undef_results_end(); I!=E; ++I) { |
| 74 | |
| 75 | ExplodedNode *N = *I; |
| 76 | const Stmt *S = N->getLocationAs<PostStmt>()->getStmt(); |
| 77 | BuiltinBugReport *report = NULL; |
| 78 | |
| 79 | if (const BinaryOperator *B = dyn_cast<BinaryOperator>(S)) { |
| 80 | llvm::SmallString<256> sbuf; |
| 81 | llvm::raw_svector_ostream OS(sbuf); |
| 82 | const GRState *ST = N->getState(); |
Ted Kremenek | 7c039bf | 2009-09-16 06:04:26 +0000 | [diff] [blame] | 83 | const Expr *Ex = NULL; |
| 84 | bool isLeft = true; |
Ted Kremenek | 5b9bd21 | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 85 | |
| 86 | if (ST->getSVal(B->getLHS()).isUndef()) { |
| 87 | Ex = B->getLHS()->IgnoreParenCasts(); |
Ted Kremenek | 7c039bf | 2009-09-16 06:04:26 +0000 | [diff] [blame] | 88 | isLeft = true; |
Ted Kremenek | 5b9bd21 | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 89 | } |
Ted Kremenek | 24c411b | 2009-09-15 17:43:54 +0000 | [diff] [blame] | 90 | else if (ST->getSVal(B->getRHS()).isUndef()) { |
Ted Kremenek | 5b9bd21 | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 91 | Ex = B->getRHS()->IgnoreParenCasts(); |
Ted Kremenek | 7c039bf | 2009-09-16 06:04:26 +0000 | [diff] [blame] | 92 | isLeft = false; |
Ted Kremenek | 5b9bd21 | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 93 | } |
Ted Kremenek | 7c039bf | 2009-09-16 06:04:26 +0000 | [diff] [blame] | 94 | |
| 95 | if (Ex) { |
| 96 | OS << "The " << (isLeft ? "left" : "right") |
Ted Kremenek | 112ba7e | 2009-09-24 00:44:26 +0000 | [diff] [blame] | 97 | << " operand of '" |
Ted Kremenek | 7c039bf | 2009-09-16 06:04:26 +0000 | [diff] [blame] | 98 | << BinaryOperator::getOpcodeStr(B->getOpcode()) |
Ted Kremenek | 112ba7e | 2009-09-24 00:44:26 +0000 | [diff] [blame] | 99 | << "' is a garbage value"; |
Ted Kremenek | 24c411b | 2009-09-15 17:43:54 +0000 | [diff] [blame] | 100 | } |
| 101 | else { |
Ted Kremenek | 7c039bf | 2009-09-16 06:04:26 +0000 | [diff] [blame] | 102 | // Neither operand was undefined, but the result is undefined. |
Ted Kremenek | 24c411b | 2009-09-15 17:43:54 +0000 | [diff] [blame] | 103 | OS << "The result of the '" |
| 104 | << BinaryOperator::getOpcodeStr(B->getOpcode()) |
| 105 | << "' expression is undefined"; |
| 106 | } |
| 107 | |
Ted Kremenek | 5b9bd21 | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 108 | // FIXME: Use StringRefs to pass string information. |
| 109 | report = new BuiltinBugReport(*this, OS.str().str().c_str(), N); |
Ted Kremenek | 24c411b | 2009-09-15 17:43:54 +0000 | [diff] [blame] | 110 | if (Ex) report->addRange(Ex->getSourceRange()); |
Ted Kremenek | 5b9bd21 | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 111 | } |
| 112 | else { |
| 113 | report = new BuiltinBugReport(*this, |
| 114 | "Expression evaluates to an uninitialized" |
| 115 | " or undefined value", N); |
| 116 | } |
| 117 | |
| 118 | BR.EmitReport(report); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | void registerInitialVisitors(BugReporterContext& BRC, |
| 123 | const ExplodedNode* N, |
| 124 | BuiltinBugReport *R) { |
| 125 | |
| 126 | const Stmt *S = N->getLocationAs<StmtPoint>()->getStmt(); |
| 127 | const Stmt *X = S; |
| 128 | |
| 129 | if (const BinaryOperator *B = dyn_cast<BinaryOperator>(S)) { |
| 130 | const GRState *ST = N->getState(); |
Ted Kremenek | 7c039bf | 2009-09-16 06:04:26 +0000 | [diff] [blame] | 131 | if (ST->getSVal(B->getLHS()).isUndef()) |
| 132 | X = B->getLHS(); |
| 133 | else if (ST->getSVal(B->getRHS()).isUndef()) |
| 134 | X = B->getRHS(); |
Ted Kremenek | 5b9bd21 | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | registerTrackNullOrUndefValue(BRC, X, N); |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 138 | } |
| 139 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 140 | |
Zhongxing Xu | ec9227f | 2009-10-29 02:09:30 +0000 | [diff] [blame] | 141 | } // end clang namespace |
Ted Kremenek | bc3a021 | 2009-10-30 17:24:47 +0000 | [diff] [blame] | 142 | |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 143 | //===----------------------------------------------------------------------===// |
| 144 | // Check registration. |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 145 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 146 | |
| 147 | void GRExprEngine::RegisterInternalChecks() { |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 148 | // Register internal "built-in" BugTypes with the BugReporter. These BugTypes |
| 149 | // are different than what probably many checks will do since they don't |
| 150 | // create BugReports on-the-fly but instead wait until GRExprEngine finishes |
| 151 | // analyzing a function. Generation of BugReport objects is done via a call |
| 152 | // to 'FlushReports' from BugReporter. |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 153 | BR.Register(new UndefResult(this)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 154 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 155 | // The following checks do not need to have their associated BugTypes |
| 156 | // explicitly registered with the BugReporter. If they issue any BugReports, |
| 157 | // their associated BugType will get registered with the BugReporter |
| 158 | // automatically. Note that the check itself is owned by the GRExprEngine |
Ted Kremenek | 36df58a | 2009-11-06 20:47:51 +0000 | [diff] [blame] | 159 | // object. |
Ted Kremenek | f493f49 | 2009-11-11 05:50:44 +0000 | [diff] [blame] | 160 | RegisterAttrNonNullChecker(*this); |
Zhongxing Xu | d02174c | 2009-11-24 04:45:44 +0000 | [diff] [blame] | 161 | RegisterCallAndMessageChecker(*this); |
Ted Kremenek | b4b817d | 2009-11-11 03:26:34 +0000 | [diff] [blame] | 162 | RegisterDereferenceChecker(*this); |
Ted Kremenek | 84b3595 | 2009-11-06 21:51:50 +0000 | [diff] [blame] | 163 | RegisterVLASizeChecker(*this); |
| 164 | RegisterDivZeroChecker(*this); |
| 165 | RegisterReturnStackAddressChecker(*this); |
| 166 | RegisterReturnUndefChecker(*this); |
Zhongxing Xu | d694485 | 2009-11-11 13:42:54 +0000 | [diff] [blame] | 167 | RegisterUndefinedArraySubscriptChecker(*this); |
Zhongxing Xu | c3372e0 | 2009-11-22 12:29:52 +0000 | [diff] [blame] | 168 | RegisterUndefinedAssignmentChecker(*this); |
Zhongxing Xu | 0835e4c | 2009-11-23 03:20:54 +0000 | [diff] [blame] | 169 | RegisterUndefBranchChecker(*this); |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 170 | } |