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 | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 15 | #include "clang/Analysis/CFGStmtVisitor.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 | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 19 | #include "DataflowSolver.h" |
| 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 | 0a03ce6 | 2007-09-17 20:49:30 +0000 | [diff] [blame] | 31 | class RegisterDeclsAndExprs : public CFGStmtVisitor<RegisterDeclsAndExprs> { |
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 | 0a03ce6 | 2007-09-17 20:49:30 +0000 | [diff] [blame] | 34 | RegisterDeclsAndExprs(UninitializedValues::AnalysisDataTy& ad) : AD(ad) {} |
Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 35 | |
Ted Kremenek | 0a03ce6 | 2007-09-17 20:49:30 +0000 | [diff] [blame] | 36 | void VisitBlockVarDecl(BlockVarDecl* VD) { |
| 37 | if (AD.VMap.find(VD) == AD.VMap.end()) |
| 38 | AD.VMap[VD] = AD.NumDecls++; |
| 39 | } |
| 40 | |
| 41 | void VisitDeclChain(ScopedDecl* D) { |
| 42 | for (; D != NULL; D = D->getNextDeclarator()) |
| 43 | if (BlockVarDecl* VD = dyn_cast<BlockVarDecl>(D)) |
| 44 | VisitBlockVarDecl(VD); |
Ted Kremenek | 3e03975 | 2007-09-17 17:14:52 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | void BlockStmt_VisitExpr(Expr* E) { |
| 48 | if (AD.EMap.find(E) == AD.EMap.end()) |
Ted Kremenek | 334b30a | 2007-09-17 18:31:23 +0000 | [diff] [blame] | 49 | AD.EMap[E] = AD.NumBlockExprs++; |
Ted Kremenek | 0a03ce6 | 2007-09-17 20:49:30 +0000 | [diff] [blame] | 50 | |
| 51 | Visit(E); |
| 52 | } |
| 53 | |
Ted Kremenek | 68447a6 | 2007-09-18 20:59:00 +0000 | [diff] [blame] | 54 | void VisitDeclRefExpr(DeclRefExpr* DR) { VisitDeclChain(DR->getDecl()); } |
| 55 | void VisitDeclStmt(DeclStmt* S) { VisitDeclChain(S->getDecl()); } |
| 56 | void VisitStmt(Stmt* S) { VisitChildren(S); } |
| 57 | void operator()(Stmt* S) { BlockStmt_Visit(S); } |
Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | } // end anonymous namespace |
| 61 | |
| 62 | void UninitializedValues::InitializeValues(const CFG& cfg) { |
Ted Kremenek | 0a03ce6 | 2007-09-17 20:49:30 +0000 | [diff] [blame] | 63 | RegisterDeclsAndExprs R(this->getAnalysisData()); |
Ted Kremenek | 68447a6 | 2007-09-18 20:59:00 +0000 | [diff] [blame] | 64 | cfg.VisitBlockStmts(R); |
Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Ted Kremenek | 3871d8e | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 67 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 68 | // Transfer functions. |
Ted Kremenek | 3871d8e | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 69 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 70 | |
| 71 | namespace { |
Ted Kremenek | 334b30a | 2007-09-17 18:31:23 +0000 | [diff] [blame] | 72 | |
Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 73 | class TransferFuncs : public CFGStmtVisitor<TransferFuncs,bool> { |
| 74 | UninitializedValues::ValTy V; |
Ted Kremenek | 3e03975 | 2007-09-17 17:14:52 +0000 | [diff] [blame] | 75 | UninitializedValues::AnalysisDataTy& AD; |
Ted Kremenek | e6148f3 | 2007-09-17 21:59:08 +0000 | [diff] [blame] | 76 | bool InitWithAssigns; |
Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 77 | public: |
Ted Kremenek | e6148f3 | 2007-09-17 21:59:08 +0000 | [diff] [blame] | 78 | TransferFuncs(UninitializedValues::AnalysisDataTy& ad, |
| 79 | bool init_with_assigns=true) : |
| 80 | AD(ad), InitWithAssigns(init_with_assigns) { |
Ted Kremenek | 0a03ce6 | 2007-09-17 20:49:30 +0000 | [diff] [blame] | 81 | V.resetValues(AD); |
Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | UninitializedValues::ValTy& getVal() { return V; } |
Ted Kremenek | 3e03975 | 2007-09-17 17:14:52 +0000 | [diff] [blame] | 85 | |
Ted Kremenek | 334b30a | 2007-09-17 18:31:23 +0000 | [diff] [blame] | 86 | bool VisitDeclRefExpr(DeclRefExpr* DR); |
| 87 | bool VisitBinaryOperator(BinaryOperator* B); |
| 88 | bool VisitUnaryOperator(UnaryOperator* U); |
| 89 | bool VisitStmt(Stmt* S); |
| 90 | bool VisitCallExpr(CallExpr* C); |
| 91 | bool BlockStmt_VisitExpr(Expr* E); |
Ted Kremenek | 3871d8e | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 92 | bool VisitDeclStmt(DeclStmt* D); |
Ted Kremenek | 334b30a | 2007-09-17 18:31:23 +0000 | [diff] [blame] | 93 | |
Ted Kremenek | f92ba51 | 2007-09-18 21:43:18 +0000 | [diff] [blame] | 94 | BlockVarDecl* FindBlockVarDecl(Stmt* S); |
| 95 | |
Ted Kremenek | 334b30a | 2007-09-17 18:31:23 +0000 | [diff] [blame] | 96 | static inline bool Initialized() { return true; } |
Ted Kremenek | 3871d8e | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 97 | static inline bool Uninitialized() { return false; } |
Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 98 | }; |
Ted Kremenek | 334b30a | 2007-09-17 18:31:23 +0000 | [diff] [blame] | 99 | |
| 100 | |
| 101 | bool TransferFuncs::VisitDeclRefExpr(DeclRefExpr* DR) { |
Ted Kremenek | 0a03ce6 | 2007-09-17 20:49:30 +0000 | [diff] [blame] | 102 | if (BlockVarDecl* VD = dyn_cast<BlockVarDecl>(DR->getDecl())) { |
Ted Kremenek | f92ba51 | 2007-09-18 21:43:18 +0000 | [diff] [blame] | 103 | if (AD.Observer) AD.Observer->ObserveDeclRefExpr(V,AD,DR,VD); |
Ted Kremenek | 3871d8e | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 104 | |
Ted Kremenek | f92ba51 | 2007-09-18 21:43:18 +0000 | [diff] [blame] | 105 | return V.getBitRef(VD,AD); |
Ted Kremenek | 334b30a | 2007-09-17 18:31:23 +0000 | [diff] [blame] | 106 | } |
Ted Kremenek | f92ba51 | 2007-09-18 21:43:18 +0000 | [diff] [blame] | 107 | else return Initialized(); |
| 108 | } |
| 109 | |
| 110 | BlockVarDecl* TransferFuncs::FindBlockVarDecl(Stmt *S) { |
| 111 | for (;;) { |
| 112 | if (ParenExpr* P = dyn_cast<ParenExpr>(S)) { |
| 113 | S = P->getSubExpr(); |
| 114 | continue; |
| 115 | } |
| 116 | else if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(S)) |
| 117 | if (BlockVarDecl* VD = dyn_cast<BlockVarDecl>(DR->getDecl())) |
| 118 | return VD; |
| 119 | |
| 120 | return NULL; |
| 121 | } |
Ted Kremenek | 334b30a | 2007-09-17 18:31:23 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | bool TransferFuncs::VisitBinaryOperator(BinaryOperator* B) { |
Ted Kremenek | e6148f3 | 2007-09-17 21:59:08 +0000 | [diff] [blame] | 125 | |
Ted Kremenek | f92ba51 | 2007-09-18 21:43:18 +0000 | [diff] [blame] | 126 | if (CFG::hasImplicitControlFlow(B)) |
| 127 | return V.getBitRef(B,AD); |
| 128 | |
| 129 | if (B->isAssignmentOp()) |
| 130 | // Get the Decl for the LHS (if any). |
| 131 | if (BlockVarDecl* VD = FindBlockVarDecl(B->getLHS())) |
| 132 | if(InitWithAssigns) { |
| 133 | // Pseudo-hack to prevent cascade of warnings. If the RHS uses |
| 134 | // an uninitialized value, then we are already going to flag a warning |
| 135 | // for the RHS, or for the root "source" of the unintialized values. |
| 136 | // Thus, propogating uninitialized doesn't make sense, since we are |
| 137 | // just adding extra messages that don't |
| 138 | // contribute to diagnosing the bug. In InitWithAssigns mode |
| 139 | // we unconditionally set the assigned variable to Initialized to |
| 140 | // prevent Uninitialized propogation. |
| 141 | return V.getBitRef(VD,AD) = Initialized(); |
| 142 | } |
| 143 | else return V.getBitRef(VD,AD) = Visit(B->getRHS()); |
Ted Kremenek | 3871d8e | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 144 | |
Ted Kremenek | 334b30a | 2007-09-17 18:31:23 +0000 | [diff] [blame] | 145 | return VisitStmt(B); |
| 146 | } |
| 147 | |
Ted Kremenek | 3871d8e | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 148 | bool TransferFuncs::VisitDeclStmt(DeclStmt* S) { |
| 149 | bool x = Initialized(); |
| 150 | |
| 151 | for (ScopedDecl* D = S->getDecl(); D != NULL; D = D->getNextDeclarator()) |
Ted Kremenek | 0a03ce6 | 2007-09-17 20:49:30 +0000 | [diff] [blame] | 152 | if (BlockVarDecl* VD = dyn_cast<BlockVarDecl>(D)) |
Ted Kremenek | 3871d8e | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 153 | if (Stmt* I = VD->getInit()) { |
Ted Kremenek | f92ba51 | 2007-09-18 21:43:18 +0000 | [diff] [blame] | 154 | x = V.getBitRef(cast<Expr>(I),AD); |
| 155 | V.getBitRef(VD,AD) = x; |
Ted Kremenek | 3871d8e | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | return x; |
| 159 | } |
| 160 | |
Ted Kremenek | 334b30a | 2007-09-17 18:31:23 +0000 | [diff] [blame] | 161 | bool TransferFuncs::VisitCallExpr(CallExpr* C) { |
Ted Kremenek | a1d3586 | 2007-09-18 21:47:41 +0000 | [diff] [blame] | 162 | VisitChildren(C); |
Ted Kremenek | 334b30a | 2007-09-17 18:31:23 +0000 | [diff] [blame] | 163 | return Initialized(); |
| 164 | } |
| 165 | |
| 166 | bool TransferFuncs::VisitUnaryOperator(UnaryOperator* U) { |
| 167 | switch (U->getOpcode()) { |
Ted Kremenek | f92ba51 | 2007-09-18 21:43:18 +0000 | [diff] [blame] | 168 | case UnaryOperator::AddrOf: |
| 169 | // For "&x", treat "x" as now being initialized. |
| 170 | if (BlockVarDecl* VD = FindBlockVarDecl(U->getSubExpr())) |
| 171 | V.getBitRef(VD,AD) = Initialized(); |
| 172 | else |
| 173 | return Visit(U->getSubExpr()); |
Ted Kremenek | 334b30a | 2007-09-17 18:31:23 +0000 | [diff] [blame] | 174 | |
| 175 | default: |
| 176 | return Visit(U->getSubExpr()); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | bool TransferFuncs::VisitStmt(Stmt* S) { |
| 181 | bool x = Initialized(); |
| 182 | |
| 183 | // We don't stop at the first subexpression that is Uninitialized because |
| 184 | // evaluating some subexpressions may result in propogating "Uninitialized" |
| 185 | // or "Initialized" to variables referenced in the other subexpressions. |
| 186 | for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I!=E; ++I) |
Ted Kremenek | f92ba51 | 2007-09-18 21:43:18 +0000 | [diff] [blame] | 187 | if (Visit(*I) == Uninitialized()) x = Uninitialized(); |
Ted Kremenek | 334b30a | 2007-09-17 18:31:23 +0000 | [diff] [blame] | 188 | |
| 189 | return x; |
| 190 | } |
| 191 | |
| 192 | bool TransferFuncs::BlockStmt_VisitExpr(Expr* E) { |
Ted Kremenek | f92ba51 | 2007-09-18 21:43:18 +0000 | [diff] [blame] | 193 | assert (AD.isTracked(E)); |
| 194 | return V.getBitRef(E,AD) = Visit(E); |
Ted Kremenek | 334b30a | 2007-09-17 18:31:23 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 197 | } // end anonymous namespace |
| 198 | |
Ted Kremenek | 3871d8e | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 199 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 200 | // Merge operator. |
Ted Kremenek | 334b30a | 2007-09-17 18:31:23 +0000 | [diff] [blame] | 201 | // |
| 202 | // In our transfer functions we take the approach that any |
| 203 | // combination of unintialized values, e.g. Unitialized + ___ = Unitialized. |
| 204 | // |
| 205 | // Merges take the opposite approach. |
| 206 | // |
Ted Kremenek | 68447a6 | 2007-09-18 20:59:00 +0000 | [diff] [blame] | 207 | // In the merge of dataflow values we prefer unsoundness, and |
Ted Kremenek | 334b30a | 2007-09-17 18:31:23 +0000 | [diff] [blame] | 208 | // prefer false negatives to false positives. At merges, if a value for a |
| 209 | // tracked Decl is EVER initialized in any of the predecessors we treat it as |
| 210 | // initialized at the confluence point. |
Ted Kremenek | 3871d8e | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 211 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 212 | |
| 213 | namespace { |
| 214 | struct Merge { |
| 215 | void operator()(UninitializedValues::ValTy& Dst, |
| 216 | UninitializedValues::ValTy& Src) { |
Ted Kremenek | f92ba51 | 2007-09-18 21:43:18 +0000 | [diff] [blame] | 217 | assert (Src.sizesEqual(Dst) && "BV sizes do not match."); |
Ted Kremenek | 68447a6 | 2007-09-18 20:59:00 +0000 | [diff] [blame] | 218 | Dst.DeclBV |= Src.DeclBV; |
| 219 | Dst.ExprBV |= Src.ExprBV; |
Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 220 | } |
| 221 | }; |
| 222 | } // end anonymous namespace |
| 223 | |
Ted Kremenek | 3871d8e | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 224 | //===----------------------------------------------------------------------===// |
| 225 | // Unitialized values checker. Scan an AST and flag variable uses |
| 226 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 227 | |
Ted Kremenek | 3871d8e | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 228 | UninitializedValues_ValueTypes::ObserverTy::~ObserverTy() {} |
| 229 | |
| 230 | namespace { |
Ted Kremenek | 3871d8e | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 231 | class UninitializedValuesChecker : public UninitializedValues::ObserverTy { |
| 232 | ASTContext &Ctx; |
| 233 | Diagnostic &Diags; |
Ted Kremenek | 0a03ce6 | 2007-09-17 20:49:30 +0000 | [diff] [blame] | 234 | llvm::SmallPtrSet<BlockVarDecl*,10> AlreadyWarned; |
Ted Kremenek | 3871d8e | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 235 | |
| 236 | public: |
| 237 | UninitializedValuesChecker(ASTContext &ctx, Diagnostic &diags) |
| 238 | : Ctx(ctx), Diags(diags) {} |
| 239 | |
| 240 | virtual void ObserveDeclRefExpr(UninitializedValues::ValTy& V, |
| 241 | UninitializedValues::AnalysisDataTy& AD, |
Ted Kremenek | 0a03ce6 | 2007-09-17 20:49:30 +0000 | [diff] [blame] | 242 | DeclRefExpr* DR, BlockVarDecl* VD) { |
Ted Kremenek | 3871d8e | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 243 | |
Ted Kremenek | f92ba51 | 2007-09-18 21:43:18 +0000 | [diff] [blame] | 244 | assert ( AD.isTracked(VD) && "Unknown VarDecl."); |
| 245 | |
| 246 | if (V.getBitRef(VD,AD) == TransferFuncs::Uninitialized()) |
Ted Kremenek | 3871d8e | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 247 | if (AlreadyWarned.insert(VD)) |
| 248 | Diags.Report(DR->getSourceRange().Begin(), diag::warn_uninit_val); |
| 249 | } |
| 250 | }; |
Ted Kremenek | 3871d8e | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 251 | } // end anonymous namespace |
| 252 | |
Ted Kremenek | 0a03ce6 | 2007-09-17 20:49:30 +0000 | [diff] [blame] | 253 | namespace clang { |
Ted Kremenek | 3871d8e | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 254 | void CheckUninitializedValues(CFG& cfg, ASTContext &Ctx, Diagnostic &Diags) { |
Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 255 | |
| 256 | typedef DataflowSolver<UninitializedValues,TransferFuncs,Merge> Solver; |
Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 257 | |
Ted Kremenek | 3871d8e | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 258 | // Compute the unitialized values information. |
| 259 | UninitializedValues U; |
Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 260 | Solver S(U); |
Ted Kremenek | 3871d8e | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 261 | S.runOnCFG(cfg); |
| 262 | |
| 263 | // Scan for DeclRefExprs that use uninitialized values. |
| 264 | UninitializedValuesChecker Observer(Ctx,Diags); |
| 265 | U.getAnalysisData().Observer = &Observer; |
Ted Kremenek | 3fa5e09 | 2007-09-18 21:08:21 +0000 | [diff] [blame] | 266 | S.runOnAllBlocks(cfg); |
Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 267 | } |
Ted Kremenek | 3fa5e09 | 2007-09-18 21:08:21 +0000 | [diff] [blame] | 268 | } // end namespace clang |