Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 1 | //==- 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 Kremenek | 26e4746 | 2007-09-20 21:42:55 +0000 | [diff] [blame] | 15 | #include "clang/Analysis/Visitors/CFGRecStmtDeclVisitor.h" |
Ted Kremenek | 3871d8e | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 16 | #include "clang/Analysis/LocalCheckers.h" |
| 17 | #include "clang/Basic/Diagnostic.h" |
| 18 | #include "clang/AST/ASTContext.h" |
Ted Kremenek | 10d8046 | 2007-09-25 21:00:24 +0000 | [diff] [blame] | 19 | #include "clang/Analysis/FlowSensitive/DataflowSolver.h" |
Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 20 | |
Ted Kremenek | 3871d8e | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SmallPtrSet.h" |
| 22 | |
Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 23 | using namespace clang; |
| 24 | |
Ted Kremenek | 3871d8e | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 25 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 26 | // Dataflow initialization logic. |
Ted Kremenek | 3871d8e | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 27 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 28 | |
| 29 | namespace { |
| 30 | |
Ted Kremenek | 1ff4115 | 2007-09-19 18:00:03 +0000 | [diff] [blame] | 31 | class RegisterDeclsExprs : public CFGRecStmtDeclVisitor<RegisterDeclsExprs> { |
Ted Kremenek | 3e03975 | 2007-09-17 17:14:52 +0000 | [diff] [blame] | 32 | UninitializedValues::AnalysisDataTy& AD; |
Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 33 | public: |
Ted Kremenek | 1ff4115 | 2007-09-19 18:00:03 +0000 | [diff] [blame] | 34 | RegisterDeclsExprs(UninitializedValues::AnalysisDataTy& ad) : AD(ad) {} |
Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 35 | |
Ted Kremenek | bfbb7fb | 2007-09-27 18:20:22 +0000 | [diff] [blame] | 36 | void VisitBlockVarDecl(BlockVarDecl* VD) { AD.Register(VD); } |
| 37 | void BlockStmt_VisitExpr(Expr* E) { AD.Register(E); } |
Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | } // end anonymous namespace |
| 41 | |
| 42 | void UninitializedValues::InitializeValues(const CFG& cfg) { |
Ted Kremenek | 1ff4115 | 2007-09-19 18:00:03 +0000 | [diff] [blame] | 43 | RegisterDeclsExprs R(this->getAnalysisData()); |
Ted Kremenek | 68447a6 | 2007-09-18 20:59:00 +0000 | [diff] [blame] | 44 | cfg.VisitBlockStmts(R); |
Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Ted Kremenek | 3871d8e | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 47 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 48 | // Transfer functions. |
Ted Kremenek | 3871d8e | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 49 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 50 | |
| 51 | namespace { |
Ted Kremenek | 334b30a | 2007-09-17 18:31:23 +0000 | [diff] [blame] | 52 | |
Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 53 | class TransferFuncs : public CFGStmtVisitor<TransferFuncs,bool> { |
| 54 | UninitializedValues::ValTy V; |
Ted Kremenek | 3e03975 | 2007-09-17 17:14:52 +0000 | [diff] [blame] | 55 | UninitializedValues::AnalysisDataTy& AD; |
Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 56 | public: |
Ted Kremenek | 6b57649 | 2007-09-28 00:09:38 +0000 | [diff] [blame^] | 57 | TransferFuncs(UninitializedValues::AnalysisDataTy& ad) : AD(ad) { |
Ted Kremenek | 0a03ce6 | 2007-09-17 20:49:30 +0000 | [diff] [blame] | 58 | V.resetValues(AD); |
Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | UninitializedValues::ValTy& getVal() { return V; } |
Ted Kremenek | 3e03975 | 2007-09-17 17:14:52 +0000 | [diff] [blame] | 62 | |
Ted Kremenek | 334b30a | 2007-09-17 18:31:23 +0000 | [diff] [blame] | 63 | bool VisitDeclRefExpr(DeclRefExpr* DR); |
| 64 | bool VisitBinaryOperator(BinaryOperator* B); |
| 65 | bool VisitUnaryOperator(UnaryOperator* U); |
| 66 | bool VisitStmt(Stmt* S); |
| 67 | bool VisitCallExpr(CallExpr* C); |
Ted Kremenek | 3871d8e | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 68 | bool VisitDeclStmt(DeclStmt* D); |
Ted Kremenek | 6b57649 | 2007-09-28 00:09:38 +0000 | [diff] [blame^] | 69 | bool VisitConditionalOperator(ConditionalOperator* C); |
| 70 | |
| 71 | bool Visit(Stmt *S); |
| 72 | bool BlockStmt_VisitExpr(Expr* E); |
Ted Kremenek | 334b30a | 2007-09-17 18:31:23 +0000 | [diff] [blame] | 73 | |
Ted Kremenek | f92ba51 | 2007-09-18 21:43:18 +0000 | [diff] [blame] | 74 | BlockVarDecl* FindBlockVarDecl(Stmt* S); |
Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 75 | }; |
Ted Kremenek | bfbb7fb | 2007-09-27 18:20:22 +0000 | [diff] [blame] | 76 | |
| 77 | static const bool Initialized = true; |
| 78 | static const bool Uninitialized = false; |
Ted Kremenek | 334b30a | 2007-09-17 18:31:23 +0000 | [diff] [blame] | 79 | |
| 80 | |
| 81 | bool TransferFuncs::VisitDeclRefExpr(DeclRefExpr* DR) { |
Ted Kremenek | 0a03ce6 | 2007-09-17 20:49:30 +0000 | [diff] [blame] | 82 | if (BlockVarDecl* VD = dyn_cast<BlockVarDecl>(DR->getDecl())) { |
Ted Kremenek | f92ba51 | 2007-09-18 21:43:18 +0000 | [diff] [blame] | 83 | if (AD.Observer) AD.Observer->ObserveDeclRefExpr(V,AD,DR,VD); |
Ted Kremenek | 6b57649 | 2007-09-28 00:09:38 +0000 | [diff] [blame^] | 84 | |
| 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 Kremenek | 334b30a | 2007-09-17 18:31:23 +0000 | [diff] [blame] | 92 | } |
Ted Kremenek | bfbb7fb | 2007-09-27 18:20:22 +0000 | [diff] [blame] | 93 | else return Initialized; |
Ted Kremenek | f92ba51 | 2007-09-18 21:43:18 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | BlockVarDecl* TransferFuncs::FindBlockVarDecl(Stmt *S) { |
Ted Kremenek | 6b57649 | 2007-09-28 00:09:38 +0000 | [diff] [blame^] | 97 | for (;;) |
Ted Kremenek | f92ba51 | 2007-09-18 21:43:18 +0000 | [diff] [blame] | 98 | if (ParenExpr* P = dyn_cast<ParenExpr>(S)) { |
Ted Kremenek | 6b57649 | 2007-09-28 00:09:38 +0000 | [diff] [blame^] | 99 | S = P->getSubExpr(); continue; |
Ted Kremenek | f92ba51 | 2007-09-18 21:43:18 +0000 | [diff] [blame] | 100 | } |
Ted Kremenek | 6b57649 | 2007-09-28 00:09:38 +0000 | [diff] [blame^] | 101 | else if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(S)) { |
Ted Kremenek | f92ba51 | 2007-09-18 21:43:18 +0000 | [diff] [blame] | 102 | if (BlockVarDecl* VD = dyn_cast<BlockVarDecl>(DR->getDecl())) |
| 103 | return VD; |
Ted Kremenek | 6b57649 | 2007-09-28 00:09:38 +0000 | [diff] [blame^] | 104 | } |
| 105 | else return NULL; |
Ted Kremenek | 334b30a | 2007-09-17 18:31:23 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | bool TransferFuncs::VisitBinaryOperator(BinaryOperator* B) { |
Ted Kremenek | f92ba51 | 2007-09-18 21:43:18 +0000 | [diff] [blame] | 109 | if (B->isAssignmentOp()) |
Ted Kremenek | f92ba51 | 2007-09-18 21:43:18 +0000 | [diff] [blame] | 110 | if (BlockVarDecl* VD = FindBlockVarDecl(B->getLHS())) |
Ted Kremenek | 6b57649 | 2007-09-28 00:09:38 +0000 | [diff] [blame^] | 111 | return V(VD,AD) = AD.FullUninitTaint ? Visit(B->getRHS()) : Initialized; |
Ted Kremenek | 3871d8e | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 112 | |
Ted Kremenek | 334b30a | 2007-09-17 18:31:23 +0000 | [diff] [blame] | 113 | return VisitStmt(B); |
| 114 | } |
| 115 | |
Ted Kremenek | 3871d8e | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 116 | bool TransferFuncs::VisitDeclStmt(DeclStmt* S) { |
Ted Kremenek | 3871d8e | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 117 | for (ScopedDecl* D = S->getDecl(); D != NULL; D = D->getNextDeclarator()) |
Ted Kremenek | bfbb7fb | 2007-09-27 18:20:22 +0000 | [diff] [blame] | 118 | if (BlockVarDecl* VD = dyn_cast<BlockVarDecl>(D)) { |
Ted Kremenek | 6b57649 | 2007-09-28 00:09:38 +0000 | [diff] [blame^] | 119 | if (Stmt* I = VD->getInit()) |
| 120 | V(VD,AD) = AD.FullUninitTaint ? V(cast<Expr>(I),AD) : Initialized; |
| 121 | else V(VD,AD) = Uninitialized; |
Ted Kremenek | bfbb7fb | 2007-09-27 18:20:22 +0000 | [diff] [blame] | 122 | } |
Ted Kremenek | 3871d8e | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 123 | |
Ted Kremenek | 6b57649 | 2007-09-28 00:09:38 +0000 | [diff] [blame^] | 124 | return Uninitialized; // Value is never consumed. |
Ted Kremenek | 3871d8e | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Ted Kremenek | 334b30a | 2007-09-17 18:31:23 +0000 | [diff] [blame] | 127 | bool TransferFuncs::VisitCallExpr(CallExpr* C) { |
Ted Kremenek | a1d3586 | 2007-09-18 21:47:41 +0000 | [diff] [blame] | 128 | VisitChildren(C); |
Ted Kremenek | bfbb7fb | 2007-09-27 18:20:22 +0000 | [diff] [blame] | 129 | return Initialized; |
Ted Kremenek | 334b30a | 2007-09-17 18:31:23 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | bool TransferFuncs::VisitUnaryOperator(UnaryOperator* U) { |
Ted Kremenek | 6b57649 | 2007-09-28 00:09:38 +0000 | [diff] [blame^] | 133 | if (U->getOpcode() == UnaryOperator::AddrOf) |
| 134 | if (BlockVarDecl* VD = FindBlockVarDecl(U->getSubExpr())) |
| 135 | return V(VD,AD) = Initialized; |
Ted Kremenek | 334b30a | 2007-09-17 18:31:23 +0000 | [diff] [blame] | 136 | |
Ted Kremenek | 6b57649 | 2007-09-28 00:09:38 +0000 | [diff] [blame^] | 137 | return Visit(U->getSubExpr()); |
| 138 | } |
| 139 | |
| 140 | bool TransferFuncs::VisitConditionalOperator(ConditionalOperator* C) { |
| 141 | Visit(C->getCond()); |
| 142 | return Visit(C->getLHS()) & Visit(C->getRHS()); // Yes: we want &, not &&. |
Ted Kremenek | 334b30a | 2007-09-17 18:31:23 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | bool TransferFuncs::VisitStmt(Stmt* S) { |
Ted Kremenek | bfbb7fb | 2007-09-27 18:20:22 +0000 | [diff] [blame] | 146 | bool x = Initialized; |
Ted Kremenek | 334b30a | 2007-09-17 18:31:23 +0000 | [diff] [blame] | 147 | |
| 148 | // We don't stop at the first subexpression that is Uninitialized because |
| 149 | // evaluating some subexpressions may result in propogating "Uninitialized" |
| 150 | // or "Initialized" to variables referenced in the other subexpressions. |
| 151 | for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I!=E; ++I) |
Ted Kremenek | 6b57649 | 2007-09-28 00:09:38 +0000 | [diff] [blame^] | 152 | if (*I && Visit(*I) == Uninitialized) x = Uninitialized; |
Ted Kremenek | 334b30a | 2007-09-17 18:31:23 +0000 | [diff] [blame] | 153 | |
| 154 | return x; |
| 155 | } |
Ted Kremenek | 6b57649 | 2007-09-28 00:09:38 +0000 | [diff] [blame^] | 156 | |
| 157 | bool TransferFuncs::Visit(Stmt *S) { |
| 158 | if (AD.isTracked(static_cast<Expr*>(S))) return V(static_cast<Expr*>(S),AD); |
| 159 | else return static_cast<CFGStmtVisitor<TransferFuncs,bool>*>(this)->Visit(S); |
| 160 | } |
Ted Kremenek | 334b30a | 2007-09-17 18:31:23 +0000 | [diff] [blame] | 161 | |
| 162 | bool TransferFuncs::BlockStmt_VisitExpr(Expr* E) { |
Ted Kremenek | f92ba51 | 2007-09-18 21:43:18 +0000 | [diff] [blame] | 163 | assert (AD.isTracked(E)); |
Ted Kremenek | 6b57649 | 2007-09-28 00:09:38 +0000 | [diff] [blame^] | 164 | return V(E,AD) = |
| 165 | static_cast<CFGStmtVisitor<TransferFuncs,bool>*>(this)->Visit(E); |
Ted Kremenek | 334b30a | 2007-09-17 18:31:23 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 168 | } // end anonymous namespace |
| 169 | |
Ted Kremenek | 3871d8e | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 170 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 171 | // Merge operator. |
Ted Kremenek | 334b30a | 2007-09-17 18:31:23 +0000 | [diff] [blame] | 172 | // |
| 173 | // In our transfer functions we take the approach that any |
| 174 | // combination of unintialized values, e.g. Unitialized + ___ = Unitialized. |
| 175 | // |
| 176 | // Merges take the opposite approach. |
| 177 | // |
Ted Kremenek | 68447a6 | 2007-09-18 20:59:00 +0000 | [diff] [blame] | 178 | // In the merge of dataflow values we prefer unsoundness, and |
Ted Kremenek | 334b30a | 2007-09-17 18:31:23 +0000 | [diff] [blame] | 179 | // prefer false negatives to false positives. At merges, if a value for a |
| 180 | // tracked Decl is EVER initialized in any of the predecessors we treat it as |
| 181 | // initialized at the confluence point. |
Ted Kremenek | 3871d8e | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 182 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 183 | |
| 184 | namespace { |
Ted Kremenek | bfbb7fb | 2007-09-27 18:20:22 +0000 | [diff] [blame] | 185 | typedef ExprDeclBitVector_Types::Union Merge; |
| 186 | typedef DataflowSolver<UninitializedValues,TransferFuncs,Merge> Solver; |
| 187 | } |
Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 188 | |
Ted Kremenek | 3871d8e | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 189 | //===----------------------------------------------------------------------===// |
| 190 | // Unitialized values checker. Scan an AST and flag variable uses |
| 191 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 192 | |
Ted Kremenek | 3871d8e | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 193 | UninitializedValues_ValueTypes::ObserverTy::~ObserverTy() {} |
| 194 | |
| 195 | namespace { |
Ted Kremenek | 3871d8e | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 196 | class UninitializedValuesChecker : public UninitializedValues::ObserverTy { |
| 197 | ASTContext &Ctx; |
| 198 | Diagnostic &Diags; |
Ted Kremenek | 0a03ce6 | 2007-09-17 20:49:30 +0000 | [diff] [blame] | 199 | llvm::SmallPtrSet<BlockVarDecl*,10> AlreadyWarned; |
Ted Kremenek | 3871d8e | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 200 | |
| 201 | public: |
| 202 | UninitializedValuesChecker(ASTContext &ctx, Diagnostic &diags) |
| 203 | : Ctx(ctx), Diags(diags) {} |
| 204 | |
| 205 | virtual void ObserveDeclRefExpr(UninitializedValues::ValTy& V, |
| 206 | UninitializedValues::AnalysisDataTy& AD, |
Ted Kremenek | 0a03ce6 | 2007-09-17 20:49:30 +0000 | [diff] [blame] | 207 | DeclRefExpr* DR, BlockVarDecl* VD) { |
Ted Kremenek | 3871d8e | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 208 | |
Ted Kremenek | f92ba51 | 2007-09-18 21:43:18 +0000 | [diff] [blame] | 209 | assert ( AD.isTracked(VD) && "Unknown VarDecl."); |
| 210 | |
Ted Kremenek | bfbb7fb | 2007-09-27 18:20:22 +0000 | [diff] [blame] | 211 | if (V(VD,AD) == Uninitialized) |
Ted Kremenek | 3871d8e | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 212 | if (AlreadyWarned.insert(VD)) |
| 213 | Diags.Report(DR->getSourceRange().Begin(), diag::warn_uninit_val); |
| 214 | } |
| 215 | }; |
Ted Kremenek | 3871d8e | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 216 | } // end anonymous namespace |
| 217 | |
Ted Kremenek | 0a03ce6 | 2007-09-17 20:49:30 +0000 | [diff] [blame] | 218 | namespace clang { |
Ted Kremenek | 6b57649 | 2007-09-28 00:09:38 +0000 | [diff] [blame^] | 219 | void CheckUninitializedValues(CFG& cfg, ASTContext &Ctx, Diagnostic &Diags, |
| 220 | bool FullUninitTaint) { |
Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 221 | |
Ted Kremenek | 3871d8e | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 222 | // Compute the unitialized values information. |
| 223 | UninitializedValues U; |
Ted Kremenek | 6b57649 | 2007-09-28 00:09:38 +0000 | [diff] [blame^] | 224 | U.getAnalysisData().FullUninitTaint = FullUninitTaint; |
Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 225 | Solver S(U); |
Ted Kremenek | 3871d8e | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 226 | S.runOnCFG(cfg); |
| 227 | |
| 228 | // Scan for DeclRefExprs that use uninitialized values. |
| 229 | UninitializedValuesChecker Observer(Ctx,Diags); |
| 230 | U.getAnalysisData().Observer = &Observer; |
Ted Kremenek | 3fa5e09 | 2007-09-18 21:08:21 +0000 | [diff] [blame] | 231 | S.runOnAllBlocks(cfg); |
Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 232 | } |
Ted Kremenek | 3fa5e09 | 2007-09-18 21:08:21 +0000 | [diff] [blame] | 233 | } // end namespace clang |