UninitializedValues.cpp revision f8fd82ba49827db0f6a6ba00c55a7b56b12a19fa
1//==- UninitializedValues.cpp - Find Uninitialized Values -------*- 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 implements Uninitialized Values analysis for source-level CFGs.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Analysis/Analyses/UninitializedValues.h"
15#include "clang/Analysis/Visitors/CFGRecStmtDeclVisitor.h"
16#include "clang/Analysis/AnalysisDiagnostic.h"
17#include "clang/AST/ASTContext.h"
18#include "clang/Analysis/FlowSensitive/DataflowSolver.h"
19
20#include "llvm/ADT/SmallPtrSet.h"
21
22using namespace clang;
23
24//===----------------------------------------------------------------------===//
25// Dataflow initialization logic.
26//===----------------------------------------------------------------------===//
27
28namespace {
29
30class RegisterDecls
31  : public CFGRecStmtDeclVisitor<RegisterDecls> {
32
33  UninitializedValues::AnalysisDataTy& AD;
34public:
35  RegisterDecls(UninitializedValues::AnalysisDataTy& ad) :  AD(ad) {}
36
37  void VisitVarDecl(VarDecl* VD) { AD.Register(VD); }
38  CFG& getCFG() { return AD.getCFG(); }
39};
40
41} // end anonymous namespace
42
43void UninitializedValues::InitializeValues(const CFG& cfg) {
44  RegisterDecls R(getAnalysisData());
45  cfg.VisitBlockStmts(R);
46}
47
48//===----------------------------------------------------------------------===//
49// Transfer functions.
50//===----------------------------------------------------------------------===//
51
52namespace {
53class TransferFuncs
54  : public CFGStmtVisitor<TransferFuncs,bool> {
55
56  UninitializedValues::ValTy V;
57  UninitializedValues::AnalysisDataTy& AD;
58public:
59  TransferFuncs(UninitializedValues::AnalysisDataTy& ad) : AD(ad) {}
60
61  UninitializedValues::ValTy& getVal() { return V; }
62  CFG& getCFG() { return AD.getCFG(); }
63
64  void SetTopValue(UninitializedValues::ValTy& X) {
65    X.setDeclValues(AD);
66    X.resetBlkExprValues(AD);
67  }
68
69  bool VisitDeclRefExpr(DeclRefExpr* DR);
70  bool VisitBinaryOperator(BinaryOperator* B);
71  bool VisitUnaryOperator(UnaryOperator* U);
72  bool VisitStmt(Stmt* S);
73  bool VisitCallExpr(CallExpr* C);
74  bool VisitDeclStmt(DeclStmt* D);
75  bool VisitConditionalOperator(ConditionalOperator* C);
76  bool BlockStmt_VisitObjCForCollectionStmt(ObjCForCollectionStmt* S);
77
78  bool Visit(Stmt *S);
79  bool BlockStmt_VisitExpr(Expr* E);
80
81  void VisitTerminator(CFGBlock* B) { }
82};
83
84static const bool Initialized = false;
85static const bool Uninitialized = true;
86
87bool TransferFuncs::VisitDeclRefExpr(DeclRefExpr* DR) {
88
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);
94
95      // 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.
100
101      if (AD.FullUninitTaint)
102        return V(VD,AD);
103    }
104
105  return Initialized;
106}
107
108static 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;
118}
119
120bool TransferFuncs::VisitBinaryOperator(BinaryOperator* B) {
121
122  if (VarDecl* VD = FindBlockVarDecl(B->getLHS()))
123    if (B->isAssignmentOp()) {
124      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());
128    }
129
130  return VisitStmt(B);
131}
132
133bool TransferFuncs::VisitDeclStmt(DeclStmt* S) {
134  for (DeclStmt::decl_iterator I=S->decl_begin(), E=S->decl_end(); I!=E; ++I) {
135    VarDecl *VD = dyn_cast<VarDecl>(*I);
136    if (VD && VD->isBlockVarDecl()) {
137      if (Stmt* I = VD->getInit())
138        V(VD,AD) = AD.FullUninitTaint ? V(cast<Expr>(I),AD) : Initialized;
139      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      }
155    }
156  }
157  return Uninitialized; // Value is never consumed.
158}
159
160bool TransferFuncs::VisitCallExpr(CallExpr* C) {
161  VisitChildren(C);
162  return Initialized;
163}
164
165bool TransferFuncs::VisitUnaryOperator(UnaryOperator* U) {
166  switch (U->getOpcode()) {
167    case UnaryOperator::AddrOf: {
168      VarDecl* VD = FindBlockVarDecl(U->getSubExpr());
169      if (VD && VD->isBlockVarDecl())
170        return V(VD,AD) = Initialized;
171      break;
172    }
173
174    default:
175      break;
176  }
177
178  return Visit(U->getSubExpr());
179}
180
181bool
182TransferFuncs::BlockStmt_VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
183  // This represents a use of the 'collection'
184  bool x = Visit(S->getCollection());
185
186  if (x == Uninitialized)
187    return Uninitialized;
188
189  // This represents an initialization of the 'element' value.
190  Stmt* Element = S->getElement();
191  VarDecl* VD = 0;
192
193  if (DeclStmt* DS = dyn_cast<DeclStmt>(Element))
194    VD = cast<VarDecl>(DS->getSingleDecl());
195  else {
196    Expr* ElemExpr = cast<Expr>(Element)->IgnoreParens();
197
198    // Initialize the value of the reference variable.
199    if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(ElemExpr))
200      VD = cast<VarDecl>(DR->getDecl());
201    else
202      return Visit(ElemExpr);
203  }
204
205  V(VD,AD) = Initialized;
206  return Initialized;
207}
208
209
210bool TransferFuncs::VisitConditionalOperator(ConditionalOperator* C) {
211  Visit(C->getCond());
212
213  bool rhsResult = Visit(C->getRHS());
214  // Handle the GNU extension for missing LHS.
215  if (Expr *lhs = C->getLHS())
216    return Visit(lhs) & rhsResult; // Yes: we want &, not &&.
217  else
218    return rhsResult;
219}
220
221bool TransferFuncs::VisitStmt(Stmt* S) {
222  bool x = Initialized;
223
224  // We don't stop at the first subexpression that is Uninitialized because
225  // evaluating some subexpressions may result in propogating "Uninitialized"
226  // or "Initialized" to variables referenced in the other subexpressions.
227  for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I!=E; ++I)
228    if (*I && Visit(*I) == Uninitialized) x = Uninitialized;
229
230  return x;
231}
232
233bool TransferFuncs::Visit(Stmt *S) {
234  if (AD.isTracked(static_cast<Expr*>(S))) return V(static_cast<Expr*>(S),AD);
235  else return static_cast<CFGStmtVisitor<TransferFuncs,bool>*>(this)->Visit(S);
236}
237
238bool TransferFuncs::BlockStmt_VisitExpr(Expr* E) {
239  bool x = static_cast<CFGStmtVisitor<TransferFuncs,bool>*>(this)->Visit(E);
240  if (AD.isTracked(E)) V(E,AD) = x;
241  return x;
242}
243
244} // end anonymous namespace
245
246//===----------------------------------------------------------------------===//
247// Merge operator.
248//
249//  In our transfer functions we take the approach that any
250//  combination of uninitialized values, e.g.
251//      Uninitialized + ___ = Uninitialized.
252//
253//  Merges take the same approach, preferring soundness.  At a confluence point,
254//  if any predecessor has a variable marked uninitialized, the value is
255//  uninitialized at the confluence point.
256//===----------------------------------------------------------------------===//
257
258namespace {
259  typedef StmtDeclBitVector_Types::Union Merge;
260  typedef DataflowSolver<UninitializedValues,TransferFuncs,Merge> Solver;
261}
262
263//===----------------------------------------------------------------------===//
264// Uninitialized values checker.   Scan an AST and flag variable uses
265//===----------------------------------------------------------------------===//
266
267UninitializedValues_ValueTypes::ObserverTy::~ObserverTy() {}
268
269namespace {
270class UninitializedValuesChecker
271  : public UninitializedValues::ObserverTy {
272
273  ASTContext &Ctx;
274  Diagnostic &Diags;
275  llvm::SmallPtrSet<VarDecl*,10> AlreadyWarned;
276
277public:
278  UninitializedValuesChecker(ASTContext &ctx, Diagnostic &diags)
279    : Ctx(ctx), Diags(diags) {}
280
281  virtual void ObserveDeclRefExpr(UninitializedValues::ValTy& V,
282                                  UninitializedValues::AnalysisDataTy& AD,
283                                  DeclRefExpr* DR, VarDecl* VD) {
284
285    assert ( AD.isTracked(VD) && "Unknown VarDecl.");
286
287    if (V(VD,AD) == Uninitialized)
288      if (AlreadyWarned.insert(VD))
289        Diags.Report(Ctx.getFullLoc(DR->getSourceRange().getBegin()),
290                     diag::warn_uninit_val);
291  }
292};
293} // end anonymous namespace
294
295namespace clang {
296void CheckUninitializedValues(CFG& cfg, ASTContext &Ctx, Diagnostic &Diags,
297                              bool FullUninitTaint) {
298
299  // Compute the uninitialized values information.
300  UninitializedValues U(cfg);
301  U.getAnalysisData().FullUninitTaint = FullUninitTaint;
302  Solver S(U);
303  S.runOnCFG(cfg);
304
305  // Scan for DeclRefExprs that use uninitialized values.
306  UninitializedValuesChecker Observer(Ctx,Diags);
307  U.getAnalysisData().Observer = &Observer;
308  S.runOnAllBlocks(cfg);
309}
310} // end namespace clang
311