blob: 93ade9d208e6f19d9f0d59d83608131fc319fa7a [file] [log] [blame]
Ted Kremenek78d46242008-07-22 16:21:24 +00001//=-- 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 Kremenek1053d242009-11-06 02:24:13 +000015#include "GRExprEngineInternalChecks.h"
Ted Kremenek78d46242008-07-22 16:21:24 +000016#include "clang/Analysis/PathSensitive/BugReporter.h"
17#include "clang/Analysis/PathSensitive/GRExprEngine.h"
Ted Kremenekc26a8b02009-07-22 21:46:56 +000018#include "clang/Analysis/PathSensitive/CheckerVisitor.h"
Ted Kremenekdd986cc2009-05-07 00:45:33 +000019#include "clang/Analysis/PathDiagnostic.h"
Ted Kremenek8aed8062008-10-31 00:13:20 +000020#include "clang/Basic/SourceManager.h"
Ted Kremenek78d46242008-07-22 16:21:24 +000021#include "llvm/Support/Compiler.h"
Ted Kremenekad51a602008-10-31 00:18:30 +000022#include "llvm/Support/raw_ostream.h"
Ted Kremenek78d46242008-07-22 16:21:24 +000023
24using namespace clang;
Ted Kremenek53500662009-07-22 17:55:28 +000025using namespace clang::bugreporter;
Ted Kremenek78d46242008-07-22 16:21:24 +000026
27//===----------------------------------------------------------------------===//
28// Utility functions.
29//===----------------------------------------------------------------------===//
30
31template <typename ITERATOR> inline
Zhongxing Xuc5619d92009-08-06 01:32:16 +000032ExplodedNode* GetNode(ITERATOR I) {
Ted Kremenek78d46242008-07-22 16:21:24 +000033 return *I;
34}
35
Ted Kremenek78d46242008-07-22 16:21:24 +000036//===----------------------------------------------------------------------===//
37// Bug Descriptions.
38//===----------------------------------------------------------------------===//
Zhongxing Xuec9227f2009-10-29 02:09:30 +000039namespace clang {
40class BuiltinBugReport : public RangedBugReport {
Ted Kremenekdd986cc2009-05-07 00:45:33 +000041public:
42 BuiltinBugReport(BugType& bt, const char* desc,
Zhongxing Xuc5619d92009-08-06 01:32:16 +000043 ExplodedNode *n)
Ted Kremenek0c313172009-05-13 19:16:35 +000044 : RangedBugReport(bt, desc, n) {}
Mike Stump1eb44332009-09-09 15:08:12 +000045
Ted Kremenek85ac9342009-05-15 05:25:09 +000046 BuiltinBugReport(BugType& bt, const char *shortDesc, const char *desc,
Zhongxing Xuc5619d92009-08-06 01:32:16 +000047 ExplodedNode *n)
Mike Stump1eb44332009-09-09 15:08:12 +000048 : RangedBugReport(bt, shortDesc, desc, n) {}
49
Ted Kremenekdd986cc2009-05-07 00:45:33 +000050 void registerInitialVisitors(BugReporterContext& BRC,
Zhongxing Xuc5619d92009-08-06 01:32:16 +000051 const ExplodedNode* N);
Mike Stump1eb44332009-09-09 15:08:12 +000052};
53
Zhongxing Xuec9227f2009-10-29 02:09:30 +000054void BuiltinBugReport::registerInitialVisitors(BugReporterContext& BRC,
55 const ExplodedNode* N) {
56 static_cast<BuiltinBug&>(getBugType()).registerInitialVisitors(BRC, N, this);
57}
Mike Stump1eb44332009-09-09 15:08:12 +000058
Ted Kremenekdd986cc2009-05-07 00:45:33 +000059template <typename ITER>
60void 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 Stump1eb44332009-09-09 15:08:12 +000063}
Mike Stump1eb44332009-09-09 15:08:12 +000064
Ted Kremenek78d46242008-07-22 16:21:24 +000065class VISIBILITY_HIDDEN UndefResult : public BuiltinBug {
66public:
Ted Kremenek5b9bd212009-09-11 22:07:28 +000067 UndefResult(GRExprEngine* eng)
68 : BuiltinBug(eng,"Undefined or garbage result",
69 "Result of operation is garbage or undefined") {}
Mike Stump1eb44332009-09-09 15:08:12 +000070
Ted Kremenekcf118d42009-02-04 23:49:09 +000071 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenek5b9bd212009-09-11 22:07:28 +000072 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 Kremenek7c039bf2009-09-16 06:04:26 +000083 const Expr *Ex = NULL;
84 bool isLeft = true;
Ted Kremenek5b9bd212009-09-11 22:07:28 +000085
86 if (ST->getSVal(B->getLHS()).isUndef()) {
87 Ex = B->getLHS()->IgnoreParenCasts();
Ted Kremenek7c039bf2009-09-16 06:04:26 +000088 isLeft = true;
Ted Kremenek5b9bd212009-09-11 22:07:28 +000089 }
Ted Kremenek24c411b2009-09-15 17:43:54 +000090 else if (ST->getSVal(B->getRHS()).isUndef()) {
Ted Kremenek5b9bd212009-09-11 22:07:28 +000091 Ex = B->getRHS()->IgnoreParenCasts();
Ted Kremenek7c039bf2009-09-16 06:04:26 +000092 isLeft = false;
Ted Kremenek5b9bd212009-09-11 22:07:28 +000093 }
Ted Kremenek7c039bf2009-09-16 06:04:26 +000094
95 if (Ex) {
96 OS << "The " << (isLeft ? "left" : "right")
Ted Kremenek112ba7e2009-09-24 00:44:26 +000097 << " operand of '"
Ted Kremenek7c039bf2009-09-16 06:04:26 +000098 << BinaryOperator::getOpcodeStr(B->getOpcode())
Ted Kremenek112ba7e2009-09-24 00:44:26 +000099 << "' is a garbage value";
Ted Kremenek24c411b2009-09-15 17:43:54 +0000100 }
101 else {
Ted Kremenek7c039bf2009-09-16 06:04:26 +0000102 // Neither operand was undefined, but the result is undefined.
Ted Kremenek24c411b2009-09-15 17:43:54 +0000103 OS << "The result of the '"
104 << BinaryOperator::getOpcodeStr(B->getOpcode())
105 << "' expression is undefined";
106 }
107
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000108 // FIXME: Use StringRefs to pass string information.
109 report = new BuiltinBugReport(*this, OS.str().str().c_str(), N);
Ted Kremenek24c411b2009-09-15 17:43:54 +0000110 if (Ex) report->addRange(Ex->getSourceRange());
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000111 }
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 Kremenek7c039bf2009-09-16 06:04:26 +0000131 if (ST->getSVal(B->getLHS()).isUndef())
132 X = B->getLHS();
133 else if (ST->getSVal(B->getRHS()).isUndef())
134 X = B->getRHS();
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000135 }
136
137 registerTrackNullOrUndefValue(BRC, X, N);
Ted Kremenek78d46242008-07-22 16:21:24 +0000138 }
139};
Mike Stump1eb44332009-09-09 15:08:12 +0000140
Zhongxing Xuec9227f2009-10-29 02:09:30 +0000141} // end clang namespace
Ted Kremenekbc3a0212009-10-30 17:24:47 +0000142
Ted Kremenek78d46242008-07-22 16:21:24 +0000143//===----------------------------------------------------------------------===//
144// Check registration.
Ted Kremenekcf118d42009-02-04 23:49:09 +0000145//===----------------------------------------------------------------------===//
Ted Kremenek78d46242008-07-22 16:21:24 +0000146
147void GRExprEngine::RegisterInternalChecks() {
Ted Kremenekcf118d42009-02-04 23:49:09 +0000148 // 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 Kremenekcf118d42009-02-04 23:49:09 +0000153 BR.Register(new UndefResult(this));
Mike Stump1eb44332009-09-09 15:08:12 +0000154
Ted Kremenekcf118d42009-02-04 23:49:09 +0000155 // 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 Kremenek36df58a2009-11-06 20:47:51 +0000159 // object.
Ted Kremenekf493f492009-11-11 05:50:44 +0000160 RegisterAttrNonNullChecker(*this);
Zhongxing Xud02174c2009-11-24 04:45:44 +0000161 RegisterCallAndMessageChecker(*this);
Ted Kremenekb4b817d2009-11-11 03:26:34 +0000162 RegisterDereferenceChecker(*this);
Ted Kremenek84b35952009-11-06 21:51:50 +0000163 RegisterVLASizeChecker(*this);
164 RegisterDivZeroChecker(*this);
165 RegisterReturnStackAddressChecker(*this);
166 RegisterReturnUndefChecker(*this);
Zhongxing Xud6944852009-11-11 13:42:54 +0000167 RegisterUndefinedArraySubscriptChecker(*this);
Zhongxing Xuc3372e02009-11-22 12:29:52 +0000168 RegisterUndefinedAssignmentChecker(*this);
Zhongxing Xu0835e4c2009-11-23 03:20:54 +0000169 RegisterUndefBranchChecker(*this);
Ted Kremenek78d46242008-07-22 16:21:24 +0000170}