blob: a02e544506e1191eeeb155234ef1bca77c81e4b3 [file] [log] [blame]
Nick Lewycky5d796aa2008-08-16 17:46:53 +00001//==- UninitializedValues.cpp - Find Uninitialized Values -------*- C++ --*-==//
Ted Kremenek13ed7fe2007-09-14 22:49:21 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Ted Kremenek13ed7fe2007-09-14 22:49:21 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements Uninitialized Values analysis for source-level CFGs.
11//
12//===----------------------------------------------------------------------===//
13
Ted Kremenekcf6e41b2007-12-21 21:42:19 +000014#include "clang/Analysis/Analyses/UninitializedValues.h"
Ted Kremenek11de5cb2007-09-20 21:42:55 +000015#include "clang/Analysis/Visitors/CFGRecStmtDeclVisitor.h"
Ted Kremenekcd5860c2007-09-17 19:59:27 +000016#include "clang/Analysis/LocalCheckers.h"
17#include "clang/Basic/Diagnostic.h"
18#include "clang/AST/ASTContext.h"
Ted Kremenek1de632b2007-09-25 21:00:24 +000019#include "clang/Analysis/FlowSensitive/DataflowSolver.h"
Ted Kremenekc2b51d82008-01-08 18:19:08 +000020#include "llvm/Support/Compiler.h"
Ted Kremenek13ed7fe2007-09-14 22:49:21 +000021
Ted Kremenekcd5860c2007-09-17 19:59:27 +000022#include "llvm/ADT/SmallPtrSet.h"
23
Ted Kremenek13ed7fe2007-09-14 22:49:21 +000024using namespace clang;
25
Ted Kremenekcd5860c2007-09-17 19:59:27 +000026//===----------------------------------------------------------------------===//
Ted Kremenek13ed7fe2007-09-14 22:49:21 +000027// Dataflow initialization logic.
Ted Kremenekcd5860c2007-09-17 19:59:27 +000028//===----------------------------------------------------------------------===//
Ted Kremenek13ed7fe2007-09-14 22:49:21 +000029
30namespace {
31
Ted Kremenekc2b51d82008-01-08 18:19:08 +000032class VISIBILITY_HIDDEN RegisterDecls
33 : public CFGRecStmtDeclVisitor<RegisterDecls> {
34
Ted Kremenek56d516d2007-09-17 17:14:52 +000035 UninitializedValues::AnalysisDataTy& AD;
Ted Kremenek13ed7fe2007-09-14 22:49:21 +000036public:
Ted Kremenek11e72182007-10-01 20:33:52 +000037 RegisterDecls(UninitializedValues::AnalysisDataTy& ad) : AD(ad) {}
Ted Kremenek13ed7fe2007-09-14 22:49:21 +000038
Ted Kremenek606ceee2008-04-15 23:02:18 +000039 void VisitVarDecl(VarDecl* VD) { AD.Register(VD); }
Ted Kremenek9f9141c2007-11-20 03:01:58 +000040 CFG& getCFG() { return AD.getCFG(); }
Ted Kremenek13ed7fe2007-09-14 22:49:21 +000041};
42
43} // end anonymous namespace
44
45void UninitializedValues::InitializeValues(const CFG& cfg) {
Ted Kremenek11e72182007-10-01 20:33:52 +000046 RegisterDecls R(getAnalysisData());
Ted Kremeneka90b0d12007-09-18 20:59:00 +000047 cfg.VisitBlockStmts(R);
Ted Kremenek13ed7fe2007-09-14 22:49:21 +000048}
49
Ted Kremenekcd5860c2007-09-17 19:59:27 +000050//===----------------------------------------------------------------------===//
Ted Kremenek13ed7fe2007-09-14 22:49:21 +000051// Transfer functions.
Ted Kremenekcd5860c2007-09-17 19:59:27 +000052//===----------------------------------------------------------------------===//
Ted Kremenek13ed7fe2007-09-14 22:49:21 +000053
54namespace {
Ted Kremenekc2b51d82008-01-08 18:19:08 +000055class VISIBILITY_HIDDEN TransferFuncs
56 : public CFGStmtVisitor<TransferFuncs,bool> {
57
Ted Kremenek13ed7fe2007-09-14 22:49:21 +000058 UninitializedValues::ValTy V;
Ted Kremenek56d516d2007-09-17 17:14:52 +000059 UninitializedValues::AnalysisDataTy& AD;
Ted Kremenek13ed7fe2007-09-14 22:49:21 +000060public:
Ted Kremenek7deed0c2008-04-15 18:35:30 +000061 TransferFuncs(UninitializedValues::AnalysisDataTy& ad) : AD(ad) {}
Ted Kremenek13ed7fe2007-09-14 22:49:21 +000062
63 UninitializedValues::ValTy& getVal() { return V; }
Ted Kremenek9f9141c2007-11-20 03:01:58 +000064 CFG& getCFG() { return AD.getCFG(); }
Ted Kremenek56d516d2007-09-17 17:14:52 +000065
Ted Kremenek7deed0c2008-04-15 18:35:30 +000066 void SetTopValue(UninitializedValues::ValTy& X) {
67 X.resetValues(AD);
68 }
69
Ted Kremenek20ee4fb2007-09-17 18:31:23 +000070 bool VisitDeclRefExpr(DeclRefExpr* DR);
71 bool VisitBinaryOperator(BinaryOperator* B);
72 bool VisitUnaryOperator(UnaryOperator* U);
73 bool VisitStmt(Stmt* S);
74 bool VisitCallExpr(CallExpr* C);
Ted Kremenekcd5860c2007-09-17 19:59:27 +000075 bool VisitDeclStmt(DeclStmt* D);
Ted Kremeneka1de8c72007-09-28 00:09:38 +000076 bool VisitConditionalOperator(ConditionalOperator* C);
77
78 bool Visit(Stmt *S);
79 bool BlockStmt_VisitExpr(Expr* E);
Ted Kremenek37622082008-04-15 04:39:08 +000080
Ted Kremenek411cdee2008-04-16 21:10:48 +000081 void VisitTerminator(CFGBlock* B) { }
Ted Kremenek13ed7fe2007-09-14 22:49:21 +000082};
Ted Kremenekaead1532007-09-27 18:20:22 +000083
84static const bool Initialized = true;
85static const bool Uninitialized = false;
Ted Kremenek20ee4fb2007-09-17 18:31:23 +000086
Ted Kremenek20ee4fb2007-09-17 18:31:23 +000087bool TransferFuncs::VisitDeclRefExpr(DeclRefExpr* DR) {
Ted Kremenek2f868c02008-04-16 02:59:55 +000088
89 if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl()))
90 if (VD->isBlockVarDecl()) {
91
92 if (AD.Observer)
93 AD.Observer->ObserveDeclRefExpr(V, AD, DR, VD);
Ted Kremeneka1de8c72007-09-28 00:09:38 +000094
Ted Kremenek2f868c02008-04-16 02:59:55 +000095 // Pseudo-hack to prevent cascade of warnings. If an accessed variable
96 // is uninitialized, then we are already going to flag a warning for
97 // this variable, which a "source" of uninitialized values.
98 // We can otherwise do a full "taint" of uninitialized values. The
99 // client has both options by toggling AD.FullUninitTaint.
Ted Kremeneka1de8c72007-09-28 00:09:38 +0000100
Ted Kremenek2f868c02008-04-16 02:59:55 +0000101 if (AD.FullUninitTaint)
102 return V(VD,AD);
103 }
104
105 return Initialized;
Ted Kremenek43a16982007-09-18 21:43:18 +0000106}
107
Ted Kremenek2f868c02008-04-16 02:59:55 +0000108static VarDecl* FindBlockVarDecl(Expr* E) {
109
110 // Blast through casts and parentheses to find any DeclRefExprs that
111 // refer to a block VarDecl.
112
113 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(E->IgnoreParenCasts()))
114 if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl()))
115 if (VD->isBlockVarDecl()) return VD;
116
117 return NULL;
Ted Kremenek20ee4fb2007-09-17 18:31:23 +0000118}
119
120bool TransferFuncs::VisitBinaryOperator(BinaryOperator* B) {
Ted Kremenek2f868c02008-04-16 02:59:55 +0000121
122 if (VarDecl* VD = FindBlockVarDecl(B->getLHS()))
Ted Kremenek6ce2b632007-09-28 21:08:51 +0000123 if (B->isAssignmentOp()) {
Ted Kremenekff7c5382007-11-24 20:07:36 +0000124 if (B->getOpcode() == BinaryOperator::Assign)
125 return V(VD,AD) = Visit(B->getRHS());
126 else // Handle +=, -=, *=, etc. We do want '&', not '&&'.
127 return V(VD,AD) = Visit(B->getLHS()) & Visit(B->getRHS());
Ted Kremenek6ce2b632007-09-28 21:08:51 +0000128 }
129
Ted Kremenek20ee4fb2007-09-17 18:31:23 +0000130 return VisitStmt(B);
131}
132
Ted Kremenekcd5860c2007-09-17 19:59:27 +0000133bool TransferFuncs::VisitDeclStmt(DeclStmt* S) {
Ted Kremenek14f8b4f2008-08-05 20:46:55 +0000134 for (DeclStmt::decl_iterator I=S->decl_begin(), E=S->decl_end(); I!=E; ++I) {
135 VarDecl *VD = dyn_cast<VarDecl>(*I);
Steve Naroff248a7532008-04-15 22:42:06 +0000136 if (VD && VD->isBlockVarDecl()) {
Ted Kremeneka1de8c72007-09-28 00:09:38 +0000137 if (Stmt* I = VD->getInit())
138 V(VD,AD) = AD.FullUninitTaint ? V(cast<Expr>(I),AD) : Initialized;
Ted Kremenek81a56ec2007-12-13 05:14:22 +0000139 else {
140 // Special case for declarations of array types. For things like:
141 //
142 // char x[10];
143 //
144 // we should treat "x" as being initialized, because the variable
145 // "x" really refers to the memory block. Clearly x[1] is
146 // uninitialized, but expressions like "(char *) x" really do refer to
147 // an initialized value. This simple dataflow analysis does not reason
148 // about the contents of arrays, although it could be potentially
149 // extended to do so if the array were of constant size.
150 if (VD->getType()->isArrayType())
151 V(VD,AD) = Initialized;
152 else
153 V(VD,AD) = Uninitialized;
154 }
Ted Kremenekaead1532007-09-27 18:20:22 +0000155 }
Steve Naroff248a7532008-04-15 22:42:06 +0000156 }
Ted Kremeneka1de8c72007-09-28 00:09:38 +0000157 return Uninitialized; // Value is never consumed.
Ted Kremenekcd5860c2007-09-17 19:59:27 +0000158}
Steve Naroff248a7532008-04-15 22:42:06 +0000159
Ted Kremenek20ee4fb2007-09-17 18:31:23 +0000160bool TransferFuncs::VisitCallExpr(CallExpr* C) {
Ted Kremenek59d18272007-09-18 21:47:41 +0000161 VisitChildren(C);
Ted Kremenekaead1532007-09-27 18:20:22 +0000162 return Initialized;
Ted Kremenek20ee4fb2007-09-17 18:31:23 +0000163}
164
165bool TransferFuncs::VisitUnaryOperator(UnaryOperator* U) {
Ted Kremenek8d9ebae2007-12-13 04:47:15 +0000166 switch (U->getOpcode()) {
Argyrios Kyrtzidis5da6b252008-04-17 13:52:22 +0000167 case UnaryOperator::AddrOf: {
Steve Naroff248a7532008-04-15 22:42:06 +0000168 VarDecl* VD = FindBlockVarDecl(U->getSubExpr());
169 if (VD && VD->isBlockVarDecl())
Ted Kremenek8d9ebae2007-12-13 04:47:15 +0000170 return V(VD,AD) = Initialized;
Ted Kremenek8d9ebae2007-12-13 04:47:15 +0000171 break;
Argyrios Kyrtzidis5da6b252008-04-17 13:52:22 +0000172 }
Ted Kremenek8d9ebae2007-12-13 04:47:15 +0000173
174 case UnaryOperator::SizeOf:
175 return Initialized;
176
177 default:
178 break;
179 }
Ted Kremenek20ee4fb2007-09-17 18:31:23 +0000180
Ted Kremeneka1de8c72007-09-28 00:09:38 +0000181 return Visit(U->getSubExpr());
182}
183
184bool TransferFuncs::VisitConditionalOperator(ConditionalOperator* C) {
185 Visit(C->getCond());
Anders Carlsson39073232007-11-30 19:04:31 +0000186
187 bool rhsResult = Visit(C->getRHS());
188 // Handle the GNU extension for missing LHS.
189 if (Expr *lhs = C->getLHS())
190 return Visit(lhs) & rhsResult; // Yes: we want &, not &&.
191 else
192 return rhsResult;
Ted Kremenek20ee4fb2007-09-17 18:31:23 +0000193}
194
195bool TransferFuncs::VisitStmt(Stmt* S) {
Ted Kremenekaead1532007-09-27 18:20:22 +0000196 bool x = Initialized;
Ted Kremenek20ee4fb2007-09-17 18:31:23 +0000197
198 // We don't stop at the first subexpression that is Uninitialized because
199 // evaluating some subexpressions may result in propogating "Uninitialized"
200 // or "Initialized" to variables referenced in the other subexpressions.
201 for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I!=E; ++I)
Ted Kremeneka1de8c72007-09-28 00:09:38 +0000202 if (*I && Visit(*I) == Uninitialized) x = Uninitialized;
Ted Kremenek20ee4fb2007-09-17 18:31:23 +0000203
204 return x;
205}
Ted Kremeneka1de8c72007-09-28 00:09:38 +0000206
207bool TransferFuncs::Visit(Stmt *S) {
208 if (AD.isTracked(static_cast<Expr*>(S))) return V(static_cast<Expr*>(S),AD);
209 else return static_cast<CFGStmtVisitor<TransferFuncs,bool>*>(this)->Visit(S);
210}
Ted Kremenek20ee4fb2007-09-17 18:31:23 +0000211
212bool TransferFuncs::BlockStmt_VisitExpr(Expr* E) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +0000213 bool x = static_cast<CFGStmtVisitor<TransferFuncs,bool>*>(this)->Visit(E);
214 if (AD.isTracked(E)) V(E,AD) = x;
215 return x;
Ted Kremenek20ee4fb2007-09-17 18:31:23 +0000216}
217
Ted Kremenek13ed7fe2007-09-14 22:49:21 +0000218} // end anonymous namespace
219
Ted Kremenekcd5860c2007-09-17 19:59:27 +0000220//===----------------------------------------------------------------------===//
Ted Kremenek13ed7fe2007-09-14 22:49:21 +0000221// Merge operator.
Ted Kremenek20ee4fb2007-09-17 18:31:23 +0000222//
223// In our transfer functions we take the approach that any
Nick Lewycky5d796aa2008-08-16 17:46:53 +0000224// combination of uninitialized values, e.g.
225// Uninitialized + ___ = Uninitialized.
Ted Kremenek20ee4fb2007-09-17 18:31:23 +0000226//
Ted Kremenek7deed0c2008-04-15 18:35:30 +0000227// Merges take the same approach, preferring soundness. At a confluence point,
228// if any predecessor has a variable marked uninitialized, the value is
229// uninitialized at the confluence point.
Ted Kremenekcd5860c2007-09-17 19:59:27 +0000230//===----------------------------------------------------------------------===//
Ted Kremenek13ed7fe2007-09-14 22:49:21 +0000231
232namespace {
Ted Kremenek5fb5c6a2008-03-22 20:11:00 +0000233 typedef ExprDeclBitVector_Types::Intersect Merge;
Ted Kremenekaead1532007-09-27 18:20:22 +0000234 typedef DataflowSolver<UninitializedValues,TransferFuncs,Merge> Solver;
235}
Ted Kremenek13ed7fe2007-09-14 22:49:21 +0000236
Ted Kremenekcd5860c2007-09-17 19:59:27 +0000237//===----------------------------------------------------------------------===//
Nick Lewycky5d796aa2008-08-16 17:46:53 +0000238// Uninitialized values checker. Scan an AST and flag variable uses
Ted Kremenekcd5860c2007-09-17 19:59:27 +0000239//===----------------------------------------------------------------------===//
Ted Kremenek13ed7fe2007-09-14 22:49:21 +0000240
Ted Kremenekcd5860c2007-09-17 19:59:27 +0000241UninitializedValues_ValueTypes::ObserverTy::~ObserverTy() {}
242
243namespace {
Ted Kremenekc2b51d82008-01-08 18:19:08 +0000244class VISIBILITY_HIDDEN UninitializedValuesChecker
245 : public UninitializedValues::ObserverTy {
246
Ted Kremenekcd5860c2007-09-17 19:59:27 +0000247 ASTContext &Ctx;
248 Diagnostic &Diags;
Steve Naroff248a7532008-04-15 22:42:06 +0000249 llvm::SmallPtrSet<VarDecl*,10> AlreadyWarned;
Ted Kremenekcd5860c2007-09-17 19:59:27 +0000250
251public:
252 UninitializedValuesChecker(ASTContext &ctx, Diagnostic &diags)
253 : Ctx(ctx), Diags(diags) {}
254
255 virtual void ObserveDeclRefExpr(UninitializedValues::ValTy& V,
256 UninitializedValues::AnalysisDataTy& AD,
Steve Naroff248a7532008-04-15 22:42:06 +0000257 DeclRefExpr* DR, VarDecl* VD) {
Ted Kremenekcd5860c2007-09-17 19:59:27 +0000258
Ted Kremenek43a16982007-09-18 21:43:18 +0000259 assert ( AD.isTracked(VD) && "Unknown VarDecl.");
260
Ted Kremenekaead1532007-09-27 18:20:22 +0000261 if (V(VD,AD) == Uninitialized)
Ted Kremenekcd5860c2007-09-17 19:59:27 +0000262 if (AlreadyWarned.insert(VD))
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000263 Diags.Report(Ctx.getFullLoc(DR->getSourceRange().getBegin()),
264 diag::warn_uninit_val);
Ted Kremenekcd5860c2007-09-17 19:59:27 +0000265 }
266};
Ted Kremenekcd5860c2007-09-17 19:59:27 +0000267} // end anonymous namespace
268
Ted Kremenek2bf55142007-09-17 20:49:30 +0000269namespace clang {
Ted Kremeneka1de8c72007-09-28 00:09:38 +0000270void CheckUninitializedValues(CFG& cfg, ASTContext &Ctx, Diagnostic &Diags,
271 bool FullUninitTaint) {
Ted Kremenek13ed7fe2007-09-14 22:49:21 +0000272
Nick Lewycky5d796aa2008-08-16 17:46:53 +0000273 // Compute the uninitialized values information.
Ted Kremenek11e72182007-10-01 20:33:52 +0000274 UninitializedValues U(cfg);
Ted Kremeneka1de8c72007-09-28 00:09:38 +0000275 U.getAnalysisData().FullUninitTaint = FullUninitTaint;
Ted Kremenek13ed7fe2007-09-14 22:49:21 +0000276 Solver S(U);
Ted Kremenekcd5860c2007-09-17 19:59:27 +0000277 S.runOnCFG(cfg);
278
279 // Scan for DeclRefExprs that use uninitialized values.
280 UninitializedValuesChecker Observer(Ctx,Diags);
281 U.getAnalysisData().Observer = &Observer;
Ted Kremenek294a7c92007-09-18 21:08:21 +0000282 S.runOnAllBlocks(cfg);
Ted Kremenek13ed7fe2007-09-14 22:49:21 +0000283}
Ted Kremenek294a7c92007-09-18 21:08:21 +0000284} // end namespace clang