blob: 89ff1c20681bf8af920cf27754f7225d94fdb6ac [file] [log] [blame]
Ted Kremenek13ed7fe2007-09-14 22:49:21 +00001//==- UninitializedValues.cpp - Find Unintialized Values --------*- C++ --*-==//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Ted Kremenek and is distributed under
6// the University of Illinois Open Source 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/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 Kremenek13ed7fe2007-09-14 22:49:21 +000020
Ted Kremenekcd5860c2007-09-17 19:59:27 +000021#include "llvm/ADT/SmallPtrSet.h"
22
Ted Kremenek13ed7fe2007-09-14 22:49:21 +000023using namespace clang;
24
Ted Kremenekcd5860c2007-09-17 19:59:27 +000025//===----------------------------------------------------------------------===//
Ted Kremenek13ed7fe2007-09-14 22:49:21 +000026// Dataflow initialization logic.
Ted Kremenekcd5860c2007-09-17 19:59:27 +000027//===----------------------------------------------------------------------===//
Ted Kremenek13ed7fe2007-09-14 22:49:21 +000028
29namespace {
30
Ted Kremenek11e72182007-10-01 20:33:52 +000031class RegisterDecls : public CFGRecStmtDeclVisitor<RegisterDecls> {
Ted Kremenek56d516d2007-09-17 17:14:52 +000032 UninitializedValues::AnalysisDataTy& AD;
Ted Kremenek13ed7fe2007-09-14 22:49:21 +000033public:
Ted Kremenek11e72182007-10-01 20:33:52 +000034 RegisterDecls(UninitializedValues::AnalysisDataTy& ad) : AD(ad) {}
Ted Kremenek13ed7fe2007-09-14 22:49:21 +000035
Ted Kremenekaead1532007-09-27 18:20:22 +000036 void VisitBlockVarDecl(BlockVarDecl* VD) { AD.Register(VD); }
Ted Kremenek9f9141c2007-11-20 03:01:58 +000037 CFG& getCFG() { return AD.getCFG(); }
Ted Kremenek13ed7fe2007-09-14 22:49:21 +000038};
39
40} // end anonymous namespace
41
42void UninitializedValues::InitializeValues(const CFG& cfg) {
Ted Kremenek11e72182007-10-01 20:33:52 +000043 RegisterDecls R(getAnalysisData());
Ted Kremeneka90b0d12007-09-18 20:59:00 +000044 cfg.VisitBlockStmts(R);
Ted Kremenek13ed7fe2007-09-14 22:49:21 +000045}
46
Ted Kremenekcd5860c2007-09-17 19:59:27 +000047//===----------------------------------------------------------------------===//
Ted Kremenek13ed7fe2007-09-14 22:49:21 +000048// Transfer functions.
Ted Kremenekcd5860c2007-09-17 19:59:27 +000049//===----------------------------------------------------------------------===//
Ted Kremenek13ed7fe2007-09-14 22:49:21 +000050
51namespace {
Ted Kremenek20ee4fb2007-09-17 18:31:23 +000052
Ted Kremenek13ed7fe2007-09-14 22:49:21 +000053class TransferFuncs : public CFGStmtVisitor<TransferFuncs,bool> {
54 UninitializedValues::ValTy V;
Ted Kremenek56d516d2007-09-17 17:14:52 +000055 UninitializedValues::AnalysisDataTy& AD;
Ted Kremenek13ed7fe2007-09-14 22:49:21 +000056public:
Ted Kremeneka1de8c72007-09-28 00:09:38 +000057 TransferFuncs(UninitializedValues::AnalysisDataTy& ad) : AD(ad) {
Ted Kremenek2bf55142007-09-17 20:49:30 +000058 V.resetValues(AD);
Ted Kremenek13ed7fe2007-09-14 22:49:21 +000059 }
60
61 UninitializedValues::ValTy& getVal() { return V; }
Ted Kremenek9f9141c2007-11-20 03:01:58 +000062 CFG& getCFG() { return AD.getCFG(); }
Ted Kremenek56d516d2007-09-17 17:14:52 +000063
Ted Kremenek20ee4fb2007-09-17 18:31:23 +000064 bool VisitDeclRefExpr(DeclRefExpr* DR);
65 bool VisitBinaryOperator(BinaryOperator* B);
66 bool VisitUnaryOperator(UnaryOperator* U);
67 bool VisitStmt(Stmt* S);
68 bool VisitCallExpr(CallExpr* C);
Ted Kremenekcd5860c2007-09-17 19:59:27 +000069 bool VisitDeclStmt(DeclStmt* D);
Ted Kremeneka1de8c72007-09-28 00:09:38 +000070 bool VisitConditionalOperator(ConditionalOperator* C);
71
72 bool Visit(Stmt *S);
73 bool BlockStmt_VisitExpr(Expr* E);
Ted Kremenek20ee4fb2007-09-17 18:31:23 +000074
Ted Kremenek43a16982007-09-18 21:43:18 +000075 BlockVarDecl* FindBlockVarDecl(Stmt* S);
Ted Kremenek13ed7fe2007-09-14 22:49:21 +000076};
Ted Kremenekaead1532007-09-27 18:20:22 +000077
78static const bool Initialized = true;
79static const bool Uninitialized = false;
Ted Kremenek20ee4fb2007-09-17 18:31:23 +000080
Ted Kremenek20ee4fb2007-09-17 18:31:23 +000081bool TransferFuncs::VisitDeclRefExpr(DeclRefExpr* DR) {
Ted Kremenek2bf55142007-09-17 20:49:30 +000082 if (BlockVarDecl* VD = dyn_cast<BlockVarDecl>(DR->getDecl())) {
Ted Kremenek43a16982007-09-18 21:43:18 +000083 if (AD.Observer) AD.Observer->ObserveDeclRefExpr(V,AD,DR,VD);
Ted Kremeneka1de8c72007-09-28 00:09:38 +000084
85 // Pseudo-hack to prevent cascade of warnings. If an accessed variable
86 // is uninitialized, then we are already going to flag a warning for
87 // this variable, which a "source" of uninitialized values.
88 // We can otherwise do a full "taint" of uninitialized values. The
89 // client has both options by toggling AD.FullUninitTaint.
90
91 return AD.FullUninitTaint ? V(VD,AD) : Initialized;
Ted Kremenek20ee4fb2007-09-17 18:31:23 +000092 }
Ted Kremenekaead1532007-09-27 18:20:22 +000093 else return Initialized;
Ted Kremenek43a16982007-09-18 21:43:18 +000094}
95
96BlockVarDecl* TransferFuncs::FindBlockVarDecl(Stmt *S) {
Ted Kremeneka1de8c72007-09-28 00:09:38 +000097 for (;;)
Ted Kremenek43a16982007-09-18 21:43:18 +000098 if (ParenExpr* P = dyn_cast<ParenExpr>(S)) {
Ted Kremeneka1de8c72007-09-28 00:09:38 +000099 S = P->getSubExpr(); continue;
Ted Kremenek43a16982007-09-18 21:43:18 +0000100 }
Ted Kremeneka1de8c72007-09-28 00:09:38 +0000101 else if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(S)) {
Ted Kremenek43a16982007-09-18 21:43:18 +0000102 if (BlockVarDecl* VD = dyn_cast<BlockVarDecl>(DR->getDecl()))
103 return VD;
Ted Kremenekff7c5382007-11-24 20:07:36 +0000104 else
105 return NULL;
Ted Kremeneka1de8c72007-09-28 00:09:38 +0000106 }
107 else return NULL;
Ted Kremenek20ee4fb2007-09-17 18:31:23 +0000108}
109
110bool TransferFuncs::VisitBinaryOperator(BinaryOperator* B) {
Ted Kremenek6ce2b632007-09-28 21:08:51 +0000111 if (BlockVarDecl* VD = FindBlockVarDecl(B->getLHS()))
112 if (B->isAssignmentOp()) {
Ted Kremenekff7c5382007-11-24 20:07:36 +0000113 if (B->getOpcode() == BinaryOperator::Assign)
114 return V(VD,AD) = Visit(B->getRHS());
115 else // Handle +=, -=, *=, etc. We do want '&', not '&&'.
116 return V(VD,AD) = Visit(B->getLHS()) & Visit(B->getRHS());
Ted Kremenek6ce2b632007-09-28 21:08:51 +0000117 }
118
Ted Kremenek20ee4fb2007-09-17 18:31:23 +0000119 return VisitStmt(B);
120}
121
Ted Kremenekcd5860c2007-09-17 19:59:27 +0000122bool TransferFuncs::VisitDeclStmt(DeclStmt* S) {
Ted Kremenekcd5860c2007-09-17 19:59:27 +0000123 for (ScopedDecl* D = S->getDecl(); D != NULL; D = D->getNextDeclarator())
Ted Kremenekaead1532007-09-27 18:20:22 +0000124 if (BlockVarDecl* VD = dyn_cast<BlockVarDecl>(D)) {
Ted Kremeneka1de8c72007-09-28 00:09:38 +0000125 if (Stmt* I = VD->getInit())
126 V(VD,AD) = AD.FullUninitTaint ? V(cast<Expr>(I),AD) : Initialized;
Ted Kremenek81a56ec2007-12-13 05:14:22 +0000127 else {
128 // Special case for declarations of array types. For things like:
129 //
130 // char x[10];
131 //
132 // we should treat "x" as being initialized, because the variable
133 // "x" really refers to the memory block. Clearly x[1] is
134 // uninitialized, but expressions like "(char *) x" really do refer to
135 // an initialized value. This simple dataflow analysis does not reason
136 // about the contents of arrays, although it could be potentially
137 // extended to do so if the array were of constant size.
138 if (VD->getType()->isArrayType())
139 V(VD,AD) = Initialized;
140 else
141 V(VD,AD) = Uninitialized;
142 }
Ted Kremenekaead1532007-09-27 18:20:22 +0000143 }
Ted Kremenekcd5860c2007-09-17 19:59:27 +0000144
Ted Kremeneka1de8c72007-09-28 00:09:38 +0000145 return Uninitialized; // Value is never consumed.
Ted Kremenekcd5860c2007-09-17 19:59:27 +0000146}
147
Ted Kremenek20ee4fb2007-09-17 18:31:23 +0000148bool TransferFuncs::VisitCallExpr(CallExpr* C) {
Ted Kremenek59d18272007-09-18 21:47:41 +0000149 VisitChildren(C);
Ted Kremenekaead1532007-09-27 18:20:22 +0000150 return Initialized;
Ted Kremenek20ee4fb2007-09-17 18:31:23 +0000151}
152
153bool TransferFuncs::VisitUnaryOperator(UnaryOperator* U) {
Ted Kremenek8d9ebae2007-12-13 04:47:15 +0000154 switch (U->getOpcode()) {
155 case UnaryOperator::AddrOf:
156 if (BlockVarDecl* VD = FindBlockVarDecl(U->getSubExpr()))
157 return V(VD,AD) = Initialized;
158
159 break;
160
161 case UnaryOperator::SizeOf:
162 return Initialized;
163
164 default:
165 break;
166 }
Ted Kremenek20ee4fb2007-09-17 18:31:23 +0000167
Ted Kremeneka1de8c72007-09-28 00:09:38 +0000168 return Visit(U->getSubExpr());
169}
170
171bool TransferFuncs::VisitConditionalOperator(ConditionalOperator* C) {
172 Visit(C->getCond());
Anders Carlsson39073232007-11-30 19:04:31 +0000173
174 bool rhsResult = Visit(C->getRHS());
175 // Handle the GNU extension for missing LHS.
176 if (Expr *lhs = C->getLHS())
177 return Visit(lhs) & rhsResult; // Yes: we want &, not &&.
178 else
179 return rhsResult;
Ted Kremenek20ee4fb2007-09-17 18:31:23 +0000180}
181
182bool TransferFuncs::VisitStmt(Stmt* S) {
Ted Kremenekaead1532007-09-27 18:20:22 +0000183 bool x = Initialized;
Ted Kremenek20ee4fb2007-09-17 18:31:23 +0000184
185 // We don't stop at the first subexpression that is Uninitialized because
186 // evaluating some subexpressions may result in propogating "Uninitialized"
187 // or "Initialized" to variables referenced in the other subexpressions.
188 for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I!=E; ++I)
Ted Kremeneka1de8c72007-09-28 00:09:38 +0000189 if (*I && Visit(*I) == Uninitialized) x = Uninitialized;
Ted Kremenek20ee4fb2007-09-17 18:31:23 +0000190
191 return x;
192}
Ted Kremeneka1de8c72007-09-28 00:09:38 +0000193
194bool TransferFuncs::Visit(Stmt *S) {
195 if (AD.isTracked(static_cast<Expr*>(S))) return V(static_cast<Expr*>(S),AD);
196 else return static_cast<CFGStmtVisitor<TransferFuncs,bool>*>(this)->Visit(S);
197}
Ted Kremenek20ee4fb2007-09-17 18:31:23 +0000198
199bool TransferFuncs::BlockStmt_VisitExpr(Expr* E) {
Ted Kremenek43a16982007-09-18 21:43:18 +0000200 assert (AD.isTracked(E));
Ted Kremeneka1de8c72007-09-28 00:09:38 +0000201 return V(E,AD) =
202 static_cast<CFGStmtVisitor<TransferFuncs,bool>*>(this)->Visit(E);
Ted Kremenek20ee4fb2007-09-17 18:31:23 +0000203}
204
Ted Kremenek13ed7fe2007-09-14 22:49:21 +0000205} // end anonymous namespace
206
Ted Kremenekcd5860c2007-09-17 19:59:27 +0000207//===----------------------------------------------------------------------===//
Ted Kremenek13ed7fe2007-09-14 22:49:21 +0000208// Merge operator.
Ted Kremenek20ee4fb2007-09-17 18:31:23 +0000209//
210// In our transfer functions we take the approach that any
211// combination of unintialized values, e.g. Unitialized + ___ = Unitialized.
212//
213// Merges take the opposite approach.
214//
Ted Kremeneka90b0d12007-09-18 20:59:00 +0000215// In the merge of dataflow values we prefer unsoundness, and
Ted Kremenek20ee4fb2007-09-17 18:31:23 +0000216// prefer false negatives to false positives. At merges, if a value for a
217// tracked Decl is EVER initialized in any of the predecessors we treat it as
218// initialized at the confluence point.
Ted Kremenekcd5860c2007-09-17 19:59:27 +0000219//===----------------------------------------------------------------------===//
Ted Kremenek13ed7fe2007-09-14 22:49:21 +0000220
221namespace {
Ted Kremenekaead1532007-09-27 18:20:22 +0000222 typedef ExprDeclBitVector_Types::Union Merge;
223 typedef DataflowSolver<UninitializedValues,TransferFuncs,Merge> Solver;
224}
Ted Kremenek13ed7fe2007-09-14 22:49:21 +0000225
Ted Kremenekcd5860c2007-09-17 19:59:27 +0000226//===----------------------------------------------------------------------===//
227// Unitialized values checker. Scan an AST and flag variable uses
228//===----------------------------------------------------------------------===//
Ted Kremenek13ed7fe2007-09-14 22:49:21 +0000229
Ted Kremenekcd5860c2007-09-17 19:59:27 +0000230UninitializedValues_ValueTypes::ObserverTy::~ObserverTy() {}
231
232namespace {
Ted Kremenekcd5860c2007-09-17 19:59:27 +0000233class UninitializedValuesChecker : public UninitializedValues::ObserverTy {
234 ASTContext &Ctx;
235 Diagnostic &Diags;
Ted Kremenek2bf55142007-09-17 20:49:30 +0000236 llvm::SmallPtrSet<BlockVarDecl*,10> AlreadyWarned;
Ted Kremenekcd5860c2007-09-17 19:59:27 +0000237
238public:
239 UninitializedValuesChecker(ASTContext &ctx, Diagnostic &diags)
240 : Ctx(ctx), Diags(diags) {}
241
242 virtual void ObserveDeclRefExpr(UninitializedValues::ValTy& V,
243 UninitializedValues::AnalysisDataTy& AD,
Ted Kremenek2bf55142007-09-17 20:49:30 +0000244 DeclRefExpr* DR, BlockVarDecl* VD) {
Ted Kremenekcd5860c2007-09-17 19:59:27 +0000245
Ted Kremenek43a16982007-09-18 21:43:18 +0000246 assert ( AD.isTracked(VD) && "Unknown VarDecl.");
247
Ted Kremenekaead1532007-09-27 18:20:22 +0000248 if (V(VD,AD) == Uninitialized)
Ted Kremenekcd5860c2007-09-17 19:59:27 +0000249 if (AlreadyWarned.insert(VD))
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000250 Diags.Report(Ctx.getFullLoc(DR->getSourceRange().getBegin()),
251 diag::warn_uninit_val);
Ted Kremenekcd5860c2007-09-17 19:59:27 +0000252 }
253};
Ted Kremenekcd5860c2007-09-17 19:59:27 +0000254} // end anonymous namespace
255
Ted Kremenek2bf55142007-09-17 20:49:30 +0000256namespace clang {
Ted Kremeneka1de8c72007-09-28 00:09:38 +0000257void CheckUninitializedValues(CFG& cfg, ASTContext &Ctx, Diagnostic &Diags,
258 bool FullUninitTaint) {
Ted Kremenek13ed7fe2007-09-14 22:49:21 +0000259
Ted Kremenekcd5860c2007-09-17 19:59:27 +0000260 // Compute the unitialized values information.
Ted Kremenek11e72182007-10-01 20:33:52 +0000261 UninitializedValues U(cfg);
Ted Kremeneka1de8c72007-09-28 00:09:38 +0000262 U.getAnalysisData().FullUninitTaint = FullUninitTaint;
Ted Kremenek13ed7fe2007-09-14 22:49:21 +0000263 Solver S(U);
Ted Kremenekcd5860c2007-09-17 19:59:27 +0000264 S.runOnCFG(cfg);
265
266 // Scan for DeclRefExprs that use uninitialized values.
267 UninitializedValuesChecker Observer(Ctx,Diags);
268 U.getAnalysisData().Observer = &Observer;
Ted Kremenek294a7c92007-09-18 21:08:21 +0000269 S.runOnAllBlocks(cfg);
Ted Kremenek13ed7fe2007-09-14 22:49:21 +0000270}
Ted Kremenek294a7c92007-09-18 21:08:21 +0000271} // end namespace clang