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