1 //===- UninitializedValues.cpp - Find Uninitialized Values ----------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements uninitialized values analysis for source-level CFGs. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "clang/Analysis/Analyses/UninitializedValues.h" 14 #include "clang/AST/Attr.h" 15 #include "clang/AST/Decl.h" 16 #include "clang/AST/DeclBase.h" 17 #include "clang/AST/Expr.h" 18 #include "clang/AST/OperationKinds.h" 19 #include "clang/AST/Stmt.h" 20 #include "clang/AST/StmtObjC.h" 21 #include "clang/AST/StmtVisitor.h" 22 #include "clang/AST/Type.h" 23 #include "clang/Analysis/Analyses/PostOrderCFGView.h" 24 #include "clang/Analysis/AnalysisDeclContext.h" 25 #include "clang/Analysis/CFG.h" 26 #include "clang/Analysis/DomainSpecific/ObjCNoReturn.h" 27 #include "clang/Analysis/FlowSensitive/DataflowWorklist.h" 28 #include "clang/Basic/LLVM.h" 29 #include "llvm/ADT/BitVector.h" 30 #include "llvm/ADT/DenseMap.h" 31 #include "llvm/ADT/None.h" 32 #include "llvm/ADT/Optional.h" 33 #include "llvm/ADT/PackedVector.h" 34 #include "llvm/ADT/SmallBitVector.h" 35 #include "llvm/ADT/SmallVector.h" 36 #include "llvm/Support/Casting.h" 37 #include <algorithm> 38 #include <cassert> 39 40 using namespace clang; 41 42 #define DEBUG_LOGGING 0 43 44 static bool isTrackedVar(const VarDecl *vd, const DeclContext *dc) { 45 if (vd->isLocalVarDecl() && !vd->hasGlobalStorage() && 46 !vd->isExceptionVariable() && !vd->isInitCapture() && 47 !vd->isImplicit() && vd->getDeclContext() == dc) { 48 QualType ty = vd->getType(); 49 return ty->isScalarType() || ty->isVectorType() || ty->isRecordType(); 50 } 51 return false; 52 } 53 54 //------------------------------------------------------------------------====// 55 // DeclToIndex: a mapping from Decls we track to value indices. 56 //====------------------------------------------------------------------------// 57 58 namespace { 59 60 class DeclToIndex { 61 llvm::DenseMap<const VarDecl *, unsigned> map; 62 63 public: 64 DeclToIndex() = default; 65 66 /// Compute the actual mapping from declarations to bits. 67 void computeMap(const DeclContext &dc); 68 69 /// Return the number of declarations in the map. 70 unsigned size() const { return map.size(); } 71 72 /// Returns the bit vector index for a given declaration. 73 Optional<unsigned> getValueIndex(const VarDecl *d) const; 74 }; 75 76 } // namespace 77 78 void DeclToIndex::computeMap(const DeclContext &dc) { 79 unsigned count = 0; 80 DeclContext::specific_decl_iterator<VarDecl> I(dc.decls_begin()), 81 E(dc.decls_end()); 82 for ( ; I != E; ++I) { 83 const VarDecl *vd = *I; 84 if (isTrackedVar(vd, &dc)) 85 map[vd] = count++; 86 } 87 } 88 89 Optional<unsigned> DeclToIndex::getValueIndex(const VarDecl *d) const { 90 llvm::DenseMap<const VarDecl *, unsigned>::const_iterator I = map.find(d); 91 if (I == map.end()) 92 return None; 93 return I->second; 94 } 95 96 //------------------------------------------------------------------------====// 97 // CFGBlockValues: dataflow values for CFG blocks. 98 //====------------------------------------------------------------------------// 99 100 // These values are defined in such a way that a merge can be done using 101 // a bitwise OR. 102 enum Value { Unknown = 0x0, /* 00 */ 103 Initialized = 0x1, /* 01 */ 104 Uninitialized = 0x2, /* 10 */ 105 MayUninitialized = 0x3 /* 11 */ }; 106 107 static bool isUninitialized(const Value v) { 108 return v >= Uninitialized; 109 } 110 111 static bool isAlwaysUninit(const Value v) { 112 return v == Uninitialized; 113 } 114 115 namespace { 116 117 using ValueVector = llvm::PackedVector<Value, 2, llvm::SmallBitVector>; 118 119 class CFGBlockValues { 120 const CFG &cfg; 121 SmallVector<ValueVector, 8> vals; 122 ValueVector scratch; 123 DeclToIndex declToIndex; 124 125 public: 126 CFGBlockValues(const CFG &cfg); 127 128 unsigned getNumEntries() const { return declToIndex.size(); } 129 130 void computeSetOfDeclarations(const DeclContext &dc); 131 132 ValueVector &getValueVector(const CFGBlock *block) { 133 return vals[block->getBlockID()]; 134 } 135 136 void setAllScratchValues(Value V); 137 void mergeIntoScratch(ValueVector const &source, bool isFirst); 138 bool updateValueVectorWithScratch(const CFGBlock *block); 139 140 bool hasNoDeclarations() const { 141 return declToIndex.size() == 0; 142 } 143 144 void resetScratch(); 145 146 ValueVector::reference operator[](const VarDecl *vd); 147 148 Value getValue(const CFGBlock *block, const CFGBlock *dstBlock, 149 const VarDecl *vd) { 150 const Optional<unsigned> &idx = declToIndex.getValueIndex(vd); 151 assert(idx.hasValue()); 152 return getValueVector(block)[idx.getValue()]; 153 } 154 }; 155 156 } // namespace 157 158 CFGBlockValues::CFGBlockValues(const CFG &c) : cfg(c), vals(0) {} 159 160 void CFGBlockValues::computeSetOfDeclarations(const DeclContext &dc) { 161 declToIndex.computeMap(dc); 162 unsigned decls = declToIndex.size(); 163 scratch.resize(decls); 164 unsigned n = cfg.getNumBlockIDs(); 165 if (!n) 166 return; 167 vals.resize(n); 168 for (auto &val : vals) 169 val.resize(decls); 170 } 171 172 #if DEBUG_LOGGING 173 static void printVector(const CFGBlock *block, ValueVector &bv, 174 unsigned num) { 175 llvm::errs() << block->getBlockID() << " :"; 176 for (const auto &i : bv) 177 llvm::errs() << ' ' << i; 178 llvm::errs() << " : " << num << '\n'; 179 } 180 #endif 181 182 void CFGBlockValues::setAllScratchValues(Value V) { 183 for (unsigned I = 0, E = scratch.size(); I != E; ++I) 184 scratch[I] = V; 185 } 186 187 void CFGBlockValues::mergeIntoScratch(ValueVector const &source, 188 bool isFirst) { 189 if (isFirst) 190 scratch = source; 191 else 192 scratch |= source; 193 } 194 195 bool CFGBlockValues::updateValueVectorWithScratch(const CFGBlock *block) { 196 ValueVector &dst = getValueVector(block); 197 bool changed = (dst != scratch); 198 if (changed) 199 dst = scratch; 200 #if DEBUG_LOGGING 201 printVector(block, scratch, 0); 202 #endif 203 return changed; 204 } 205 206 void CFGBlockValues::resetScratch() { 207 scratch.reset(); 208 } 209 210 ValueVector::reference CFGBlockValues::operator[](const VarDecl *vd) { 211 const Optional<unsigned> &idx = declToIndex.getValueIndex(vd); 212 assert(idx.hasValue()); 213 return scratch[idx.getValue()]; 214 } 215 216 //------------------------------------------------------------------------====// 217 // Classification of DeclRefExprs as use or initialization. 218 //====------------------------------------------------------------------------// 219 220 namespace { 221 222 class FindVarResult { 223 const VarDecl *vd; 224 const DeclRefExpr *dr; 225 226 public: 227 FindVarResult(const VarDecl *vd, const DeclRefExpr *dr) : vd(vd), dr(dr) {} 228 229 const DeclRefExpr *getDeclRefExpr() const { return dr; } 230 const VarDecl *getDecl() const { return vd; } 231 }; 232 233 } // namespace 234 235 static const Expr *stripCasts(ASTContext &C, const Expr *Ex) { 236 while (Ex) { 237 Ex = Ex->IgnoreParenNoopCasts(C); 238 if (const auto *CE = dyn_cast<CastExpr>(Ex)) { 239 if (CE->getCastKind() == CK_LValueBitCast) { 240 Ex = CE->getSubExpr(); 241 continue; 242 } 243 } 244 break; 245 } 246 return Ex; 247 } 248 249 /// If E is an expression comprising a reference to a single variable, find that 250 /// variable. 251 static FindVarResult findVar(const Expr *E, const DeclContext *DC) { 252 if (const auto *DRE = 253 dyn_cast<DeclRefExpr>(stripCasts(DC->getParentASTContext(), E))) 254 if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) 255 if (isTrackedVar(VD, DC)) 256 return FindVarResult(VD, DRE); 257 return FindVarResult(nullptr, nullptr); 258 } 259 260 namespace { 261 262 /// Classify each DeclRefExpr as an initialization or a use. Any 263 /// DeclRefExpr which isn't explicitly classified will be assumed to have 264 /// escaped the analysis and will be treated as an initialization. 265 class ClassifyRefs : public StmtVisitor<ClassifyRefs> { 266 public: 267 enum Class { 268 Init, 269 Use, 270 SelfInit, 271 ConstRefUse, 272 Ignore 273 }; 274 275 private: 276 const DeclContext *DC; 277 llvm::DenseMap<const DeclRefExpr *, Class> Classification; 278 279 bool isTrackedVar(const VarDecl *VD) const { 280 return ::isTrackedVar(VD, DC); 281 } 282 283 void classify(const Expr *E, Class C); 284 285 public: 286 ClassifyRefs(AnalysisDeclContext &AC) : DC(cast<DeclContext>(AC.getDecl())) {} 287 288 void VisitDeclStmt(DeclStmt *DS); 289 void VisitUnaryOperator(UnaryOperator *UO); 290 void VisitBinaryOperator(BinaryOperator *BO); 291 void VisitCallExpr(CallExpr *CE); 292 void VisitCastExpr(CastExpr *CE); 293 void VisitOMPExecutableDirective(OMPExecutableDirective *ED); 294 295 void operator()(Stmt *S) { Visit(S); } 296 297 Class get(const DeclRefExpr *DRE) const { 298 llvm::DenseMap<const DeclRefExpr*, Class>::const_iterator I 299 = Classification.find(DRE); 300 if (I != Classification.end()) 301 return I->second; 302 303 const auto *VD = dyn_cast<VarDecl>(DRE->getDecl()); 304 if (!VD || !isTrackedVar(VD)) 305 return Ignore; 306 307 return Init; 308 } 309 }; 310 311 } // namespace 312 313 static const DeclRefExpr *getSelfInitExpr(VarDecl *VD) { 314 if (VD->getType()->isRecordType()) 315 return nullptr; 316 if (Expr *Init = VD->getInit()) { 317 const auto *DRE = 318 dyn_cast<DeclRefExpr>(stripCasts(VD->getASTContext(), Init)); 319 if (DRE && DRE->getDecl() == VD) 320 return DRE; 321 } 322 return nullptr; 323 } 324 325 void ClassifyRefs::classify(const Expr *E, Class C) { 326 // The result of a ?: could also be an lvalue. 327 E = E->IgnoreParens(); 328 if (const auto *CO = dyn_cast<ConditionalOperator>(E)) { 329 classify(CO->getTrueExpr(), C); 330 classify(CO->getFalseExpr(), C); 331 return; 332 } 333 334 if (const auto *BCO = dyn_cast<BinaryConditionalOperator>(E)) { 335 classify(BCO->getFalseExpr(), C); 336 return; 337 } 338 339 if (const auto *OVE = dyn_cast<OpaqueValueExpr>(E)) { 340 classify(OVE->getSourceExpr(), C); 341 return; 342 } 343 344 if (const auto *ME = dyn_cast<MemberExpr>(E)) { 345 if (const auto *VD = dyn_cast<VarDecl>(ME->getMemberDecl())) { 346 if (!VD->isStaticDataMember()) 347 classify(ME->getBase(), C); 348 } 349 return; 350 } 351 352 if (const auto *BO = dyn_cast<BinaryOperator>(E)) { 353 switch (BO->getOpcode()) { 354 case BO_PtrMemD: 355 case BO_PtrMemI: 356 classify(BO->getLHS(), C); 357 return; 358 case BO_Comma: 359 classify(BO->getRHS(), C); 360 return; 361 default: 362 return; 363 } 364 } 365 366 FindVarResult Var = findVar(E, DC); 367 if (const DeclRefExpr *DRE = Var.getDeclRefExpr()) 368 Classification[DRE] = std::max(Classification[DRE], C); 369 } 370 371 void ClassifyRefs::VisitDeclStmt(DeclStmt *DS) { 372 for (auto *DI : DS->decls()) { 373 auto *VD = dyn_cast<VarDecl>(DI); 374 if (VD && isTrackedVar(VD)) 375 if (const DeclRefExpr *DRE = getSelfInitExpr(VD)) 376 Classification[DRE] = SelfInit; 377 } 378 } 379 380 void ClassifyRefs::VisitBinaryOperator(BinaryOperator *BO) { 381 // Ignore the evaluation of a DeclRefExpr on the LHS of an assignment. If this 382 // is not a compound-assignment, we will treat it as initializing the variable 383 // when TransferFunctions visits it. A compound-assignment does not affect 384 // whether a variable is uninitialized, and there's no point counting it as a 385 // use. 386 if (BO->isCompoundAssignmentOp()) 387 classify(BO->getLHS(), Use); 388 else if (BO->getOpcode() == BO_Assign || BO->getOpcode() == BO_Comma) 389 classify(BO->getLHS(), Ignore); 390 } 391 392 void ClassifyRefs::VisitUnaryOperator(UnaryOperator *UO) { 393 // Increment and decrement are uses despite there being no lvalue-to-rvalue 394 // conversion. 395 if (UO->isIncrementDecrementOp()) 396 classify(UO->getSubExpr(), Use); 397 } 398 399 void ClassifyRefs::VisitOMPExecutableDirective(OMPExecutableDirective *ED) { 400 for (Stmt *S : OMPExecutableDirective::used_clauses_children(ED->clauses())) 401 classify(cast<Expr>(S), Use); 402 } 403 404 static bool isPointerToConst(const QualType &QT) { 405 return QT->isAnyPointerType() && QT->getPointeeType().isConstQualified(); 406 } 407 408 void ClassifyRefs::VisitCallExpr(CallExpr *CE) { 409 // Classify arguments to std::move as used. 410 if (CE->isCallToStdMove()) { 411 // RecordTypes are handled in SemaDeclCXX.cpp. 412 if (!CE->getArg(0)->getType()->isRecordType()) 413 classify(CE->getArg(0), Use); 414 return; 415 } 416 417 // If a value is passed by const pointer to a function, 418 // we should not assume that it is initialized by the call, and we 419 // conservatively do not assume that it is used. 420 // If a value is passed by const reference to a function, 421 // it should already be initialized. 422 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end(); 423 I != E; ++I) { 424 if ((*I)->isGLValue()) { 425 if ((*I)->getType().isConstQualified()) 426 classify((*I), ConstRefUse); 427 } else if (isPointerToConst((*I)->getType())) { 428 const Expr *Ex = stripCasts(DC->getParentASTContext(), *I); 429 const auto *UO = dyn_cast<UnaryOperator>(Ex); 430 if (UO && UO->getOpcode() == UO_AddrOf) 431 Ex = UO->getSubExpr(); 432 classify(Ex, Ignore); 433 } 434 } 435 } 436 437 void ClassifyRefs::VisitCastExpr(CastExpr *CE) { 438 if (CE->getCastKind() == CK_LValueToRValue) 439 classify(CE->getSubExpr(), Use); 440 else if (const auto *CSE = dyn_cast<CStyleCastExpr>(CE)) { 441 if (CSE->getType()->isVoidType()) { 442 // Squelch any detected load of an uninitialized value if 443 // we cast it to void. 444 // e.g. (void) x; 445 classify(CSE->getSubExpr(), Ignore); 446 } 447 } 448 } 449 450 //------------------------------------------------------------------------====// 451 // Transfer function for uninitialized values analysis. 452 //====------------------------------------------------------------------------// 453 454 namespace { 455 456 class TransferFunctions : public StmtVisitor<TransferFunctions> { 457 CFGBlockValues &vals; 458 const CFG &cfg; 459 const CFGBlock *block; 460 AnalysisDeclContext ∾ 461 const ClassifyRefs &classification; 462 ObjCNoReturn objCNoRet; 463 UninitVariablesHandler &handler; 464 465 public: 466 TransferFunctions(CFGBlockValues &vals, const CFG &cfg, 467 const CFGBlock *block, AnalysisDeclContext &ac, 468 const ClassifyRefs &classification, 469 UninitVariablesHandler &handler) 470 : vals(vals), cfg(cfg), block(block), ac(ac), 471 classification(classification), objCNoRet(ac.getASTContext()), 472 handler(handler) {} 473 474 void reportUse(const Expr *ex, const VarDecl *vd); 475 void reportConstRefUse(const Expr *ex, const VarDecl *vd); 476 477 void VisitBinaryOperator(BinaryOperator *bo); 478 void VisitBlockExpr(BlockExpr *be); 479 void VisitCallExpr(CallExpr *ce); 480 void VisitDeclRefExpr(DeclRefExpr *dr); 481 void VisitDeclStmt(DeclStmt *ds); 482 void VisitGCCAsmStmt(GCCAsmStmt *as); 483 void VisitObjCForCollectionStmt(ObjCForCollectionStmt *FS); 484 void VisitObjCMessageExpr(ObjCMessageExpr *ME); 485 void VisitOMPExecutableDirective(OMPExecutableDirective *ED); 486 487 bool isTrackedVar(const VarDecl *vd) { 488 return ::isTrackedVar(vd, cast<DeclContext>(ac.getDecl())); 489 } 490 491 FindVarResult findVar(const Expr *ex) { 492 return ::findVar(ex, cast<DeclContext>(ac.getDecl())); 493 } 494 495 UninitUse getUninitUse(const Expr *ex, const VarDecl *vd, Value v) { 496 UninitUse Use(ex, isAlwaysUninit(v)); 497 498 assert(isUninitialized(v)); 499 if (Use.getKind() == UninitUse::Always) 500 return Use; 501 502 // If an edge which leads unconditionally to this use did not initialize 503 // the variable, we can say something stronger than 'may be uninitialized': 504 // we can say 'either it's used uninitialized or you have dead code'. 505 // 506 // We track the number of successors of a node which have been visited, and 507 // visit a node once we have visited all of its successors. Only edges where 508 // the variable might still be uninitialized are followed. Since a variable 509 // can't transfer from being initialized to being uninitialized, this will 510 // trace out the subgraph which inevitably leads to the use and does not 511 // initialize the variable. We do not want to skip past loops, since their 512 // non-termination might be correlated with the initialization condition. 513 // 514 // For example: 515 // 516 // void f(bool a, bool b) { 517 // block1: int n; 518 // if (a) { 519 // block2: if (b) 520 // block3: n = 1; 521 // block4: } else if (b) { 522 // block5: while (!a) { 523 // block6: do_work(&a); 524 // n = 2; 525 // } 526 // } 527 // block7: if (a) 528 // block8: g(); 529 // block9: return n; 530 // } 531 // 532 // Starting from the maybe-uninitialized use in block 9: 533 // * Block 7 is not visited because we have only visited one of its two 534 // successors. 535 // * Block 8 is visited because we've visited its only successor. 536 // From block 8: 537 // * Block 7 is visited because we've now visited both of its successors. 538 // From block 7: 539 // * Blocks 1, 2, 4, 5, and 6 are not visited because we didn't visit all 540 // of their successors (we didn't visit 4, 3, 5, 6, and 5, respectively). 541 // * Block 3 is not visited because it initializes 'n'. 542 // Now the algorithm terminates, having visited blocks 7 and 8, and having 543 // found the frontier is blocks 2, 4, and 5. 544 // 545 // 'n' is definitely uninitialized for two edges into block 7 (from blocks 2 546 // and 4), so we report that any time either of those edges is taken (in 547 // each case when 'b == false'), 'n' is used uninitialized. 548 SmallVector<const CFGBlock*, 32> Queue; 549 SmallVector<unsigned, 32> SuccsVisited(cfg.getNumBlockIDs(), 0); 550 Queue.push_back(block); 551 // Specify that we've already visited all successors of the starting block. 552 // This has the dual purpose of ensuring we never add it to the queue, and 553 // of marking it as not being a candidate element of the frontier. 554 SuccsVisited[block->getBlockID()] = block->succ_size(); 555 while (!Queue.empty()) { 556 const CFGBlock *B = Queue.pop_back_val(); 557 558 // If the use is always reached from the entry block, make a note of that. 559 if (B == &cfg.getEntry()) 560 Use.setUninitAfterCall(); 561 562 for (CFGBlock::const_pred_iterator I = B->pred_begin(), E = B->pred_end(); 563 I != E; ++I) { 564 const CFGBlock *Pred = *I; 565 if (!Pred) 566 continue; 567 568 Value AtPredExit = vals.getValue(Pred, B, vd); 569 if (AtPredExit == Initialized) 570 // This block initializes the variable. 571 continue; 572 if (AtPredExit == MayUninitialized && 573 vals.getValue(B, nullptr, vd) == Uninitialized) { 574 // This block declares the variable (uninitialized), and is reachable 575 // from a block that initializes the variable. We can't guarantee to 576 // give an earlier location for the diagnostic (and it appears that 577 // this code is intended to be reachable) so give a diagnostic here 578 // and go no further down this path. 579 Use.setUninitAfterDecl(); 580 continue; 581 } 582 583 if (AtPredExit == MayUninitialized) { 584 // If the predecessor's terminator is an "asm goto" that initializes 585 // the variable, then it won't be counted as "initialized" on the 586 // non-fallthrough paths. 587 CFGTerminator term = Pred->getTerminator(); 588 if (const auto *as = dyn_cast_or_null<GCCAsmStmt>(term.getStmt())) { 589 const CFGBlock *fallthrough = *Pred->succ_begin(); 590 if (as->isAsmGoto() && 591 llvm::any_of(as->outputs(), [&](const Expr *output) { 592 return vd == findVar(output).getDecl() && 593 llvm::any_of(as->labels(), 594 [&](const AddrLabelExpr *label) { 595 return label->getLabel()->getStmt() == B->Label && 596 B != fallthrough; 597 }); 598 })) { 599 Use.setUninitAfterDecl(); 600 continue; 601 } 602 } 603 } 604 605 unsigned &SV = SuccsVisited[Pred->getBlockID()]; 606 if (!SV) { 607 // When visiting the first successor of a block, mark all NULL 608 // successors as having been visited. 609 for (CFGBlock::const_succ_iterator SI = Pred->succ_begin(), 610 SE = Pred->succ_end(); 611 SI != SE; ++SI) 612 if (!*SI) 613 ++SV; 614 } 615 616 if (++SV == Pred->succ_size()) 617 // All paths from this block lead to the use and don't initialize the 618 // variable. 619 Queue.push_back(Pred); 620 } 621 } 622 623 // Scan the frontier, looking for blocks where the variable was 624 // uninitialized. 625 for (const auto *Block : cfg) { 626 unsigned BlockID = Block->getBlockID(); 627 const Stmt *Term = Block->getTerminatorStmt(); 628 if (SuccsVisited[BlockID] && SuccsVisited[BlockID] < Block->succ_size() && 629 Term) { 630 // This block inevitably leads to the use. If we have an edge from here 631 // to a post-dominator block, and the variable is uninitialized on that 632 // edge, we have found a bug. 633 for (CFGBlock::const_succ_iterator I = Block->succ_begin(), 634 E = Block->succ_end(); I != E; ++I) { 635 const CFGBlock *Succ = *I; 636 if (Succ && SuccsVisited[Succ->getBlockID()] >= Succ->succ_size() && 637 vals.getValue(Block, Succ, vd) == Uninitialized) { 638 // Switch cases are a special case: report the label to the caller 639 // as the 'terminator', not the switch statement itself. Suppress 640 // situations where no label matched: we can't be sure that's 641 // possible. 642 if (isa<SwitchStmt>(Term)) { 643 const Stmt *Label = Succ->getLabel(); 644 if (!Label || !isa<SwitchCase>(Label)) 645 // Might not be possible. 646 continue; 647 UninitUse::Branch Branch; 648 Branch.Terminator = Label; 649 Branch.Output = 0; // Ignored. 650 Use.addUninitBranch(Branch); 651 } else { 652 UninitUse::Branch Branch; 653 Branch.Terminator = Term; 654 Branch.Output = I - Block->succ_begin(); 655 Use.addUninitBranch(Branch); 656 } 657 } 658 } 659 } 660 } 661 662 return Use; 663 } 664 }; 665 666 } // namespace 667 668 void TransferFunctions::reportUse(const Expr *ex, const VarDecl *vd) { 669 Value v = vals[vd]; 670 if (isUninitialized(v)) 671 handler.handleUseOfUninitVariable(vd, getUninitUse(ex, vd, v)); 672 } 673 674 void TransferFunctions::reportConstRefUse(const Expr *ex, const VarDecl *vd) { 675 Value v = vals[vd]; 676 if (isAlwaysUninit(v)) 677 handler.handleConstRefUseOfUninitVariable(vd, getUninitUse(ex, vd, v)); 678 } 679 680 void TransferFunctions::VisitObjCForCollectionStmt(ObjCForCollectionStmt *FS) { 681 // This represents an initialization of the 'element' value. 682 if (const auto *DS = dyn_cast<DeclStmt>(FS->getElement())) { 683 const auto *VD = cast<VarDecl>(DS->getSingleDecl()); 684 if (isTrackedVar(VD)) 685 vals[VD] = Initialized; 686 } 687 } 688 689 void TransferFunctions::VisitOMPExecutableDirective( 690 OMPExecutableDirective *ED) { 691 for (Stmt *S : OMPExecutableDirective::used_clauses_children(ED->clauses())) { 692 assert(S && "Expected non-null used-in-clause child."); 693 Visit(S); 694 } 695 if (!ED->isStandaloneDirective()) 696 Visit(ED->getStructuredBlock()); 697 } 698 699 void TransferFunctions::VisitBlockExpr(BlockExpr *be) { 700 const BlockDecl *bd = be->getBlockDecl(); 701 for (const auto &I : bd->captures()) { 702 const VarDecl *vd = I.getVariable(); 703 if (!isTrackedVar(vd)) 704 continue; 705 if (I.isByRef()) { 706 vals[vd] = Initialized; 707 continue; 708 } 709 reportUse(be, vd); 710 } 711 } 712 713 void TransferFunctions::VisitCallExpr(CallExpr *ce) { 714 if (Decl *Callee = ce->getCalleeDecl()) { 715 if (Callee->hasAttr<ReturnsTwiceAttr>()) { 716 // After a call to a function like setjmp or vfork, any variable which is 717 // initialized anywhere within this function may now be initialized. For 718 // now, just assume such a call initializes all variables. FIXME: Only 719 // mark variables as initialized if they have an initializer which is 720 // reachable from here. 721 vals.setAllScratchValues(Initialized); 722 } 723 else if (Callee->hasAttr<AnalyzerNoReturnAttr>()) { 724 // Functions labeled like "analyzer_noreturn" are often used to denote 725 // "panic" functions that in special debug situations can still return, 726 // but for the most part should not be treated as returning. This is a 727 // useful annotation borrowed from the static analyzer that is useful for 728 // suppressing branch-specific false positives when we call one of these 729 // functions but keep pretending the path continues (when in reality the 730 // user doesn't care). 731 vals.setAllScratchValues(Unknown); 732 } 733 } 734 } 735 736 void TransferFunctions::VisitDeclRefExpr(DeclRefExpr *dr) { 737 switch (classification.get(dr)) { 738 case ClassifyRefs::Ignore: 739 break; 740 case ClassifyRefs::Use: 741 reportUse(dr, cast<VarDecl>(dr->getDecl())); 742 break; 743 case ClassifyRefs::Init: 744 vals[cast<VarDecl>(dr->getDecl())] = Initialized; 745 break; 746 case ClassifyRefs::SelfInit: 747 handler.handleSelfInit(cast<VarDecl>(dr->getDecl())); 748 break; 749 case ClassifyRefs::ConstRefUse: 750 reportConstRefUse(dr, cast<VarDecl>(dr->getDecl())); 751 break; 752 } 753 } 754 755 void TransferFunctions::VisitBinaryOperator(BinaryOperator *BO) { 756 if (BO->getOpcode() == BO_Assign) { 757 FindVarResult Var = findVar(BO->getLHS()); 758 if (const VarDecl *VD = Var.getDecl()) 759 vals[VD] = Initialized; 760 } 761 } 762 763 void TransferFunctions::VisitDeclStmt(DeclStmt *DS) { 764 for (auto *DI : DS->decls()) { 765 auto *VD = dyn_cast<VarDecl>(DI); 766 if (VD && isTrackedVar(VD)) { 767 if (getSelfInitExpr(VD)) { 768 // If the initializer consists solely of a reference to itself, we 769 // explicitly mark the variable as uninitialized. This allows code 770 // like the following: 771 // 772 // int x = x; 773 // 774 // to deliberately leave a variable uninitialized. Different analysis 775 // clients can detect this pattern and adjust their reporting 776 // appropriately, but we need to continue to analyze subsequent uses 777 // of the variable. 778 vals[VD] = Uninitialized; 779 } else if (VD->getInit()) { 780 // Treat the new variable as initialized. 781 vals[VD] = Initialized; 782 } else { 783 // No initializer: the variable is now uninitialized. This matters 784 // for cases like: 785 // while (...) { 786 // int n; 787 // use(n); 788 // n = 0; 789 // } 790 // FIXME: Mark the variable as uninitialized whenever its scope is 791 // left, since its scope could be re-entered by a jump over the 792 // declaration. 793 vals[VD] = Uninitialized; 794 } 795 } 796 } 797 } 798 799 void TransferFunctions::VisitGCCAsmStmt(GCCAsmStmt *as) { 800 // An "asm goto" statement is a terminator that may initialize some variables. 801 if (!as->isAsmGoto()) 802 return; 803 804 for (const Expr *o : as->outputs()) 805 if (const VarDecl *VD = findVar(o).getDecl()) 806 if (vals[VD] != Initialized) 807 // If the variable isn't initialized by the time we get here, then we 808 // mark it as potentially uninitialized for those cases where it's used 809 // on an indirect path, where it's not guaranteed to be defined. 810 vals[VD] = MayUninitialized; 811 } 812 813 void TransferFunctions::VisitObjCMessageExpr(ObjCMessageExpr *ME) { 814 // If the Objective-C message expression is an implicit no-return that 815 // is not modeled in the CFG, set the tracked dataflow values to Unknown. 816 if (objCNoRet.isImplicitNoReturn(ME)) { 817 vals.setAllScratchValues(Unknown); 818 } 819 } 820 821 //------------------------------------------------------------------------====// 822 // High-level "driver" logic for uninitialized values analysis. 823 //====------------------------------------------------------------------------// 824 825 static bool runOnBlock(const CFGBlock *block, const CFG &cfg, 826 AnalysisDeclContext &ac, CFGBlockValues &vals, 827 const ClassifyRefs &classification, 828 llvm::BitVector &wasAnalyzed, 829 UninitVariablesHandler &handler) { 830 wasAnalyzed[block->getBlockID()] = true; 831 vals.resetScratch(); 832 // Merge in values of predecessor blocks. 833 bool isFirst = true; 834 for (CFGBlock::const_pred_iterator I = block->pred_begin(), 835 E = block->pred_end(); I != E; ++I) { 836 const CFGBlock *pred = *I; 837 if (!pred) 838 continue; 839 if (wasAnalyzed[pred->getBlockID()]) { 840 vals.mergeIntoScratch(vals.getValueVector(pred), isFirst); 841 isFirst = false; 842 } 843 } 844 // Apply the transfer function. 845 TransferFunctions tf(vals, cfg, block, ac, classification, handler); 846 for (const auto &I : *block) { 847 if (Optional<CFGStmt> cs = I.getAs<CFGStmt>()) 848 tf.Visit(const_cast<Stmt *>(cs->getStmt())); 849 } 850 CFGTerminator terminator = block->getTerminator(); 851 if (auto *as = dyn_cast_or_null<GCCAsmStmt>(terminator.getStmt())) 852 if (as->isAsmGoto()) 853 tf.Visit(as); 854 return vals.updateValueVectorWithScratch(block); 855 } 856 857 namespace { 858 859 /// PruneBlocksHandler is a special UninitVariablesHandler that is used 860 /// to detect when a CFGBlock has any *potential* use of an uninitialized 861 /// variable. It is mainly used to prune out work during the final 862 /// reporting pass. 863 struct PruneBlocksHandler : public UninitVariablesHandler { 864 /// Records if a CFGBlock had a potential use of an uninitialized variable. 865 llvm::BitVector hadUse; 866 867 /// Records if any CFGBlock had a potential use of an uninitialized variable. 868 bool hadAnyUse = false; 869 870 /// The current block to scribble use information. 871 unsigned currentBlock = 0; 872 873 PruneBlocksHandler(unsigned numBlocks) : hadUse(numBlocks, false) {} 874 875 ~PruneBlocksHandler() override = default; 876 877 void handleUseOfUninitVariable(const VarDecl *vd, 878 const UninitUse &use) override { 879 hadUse[currentBlock] = true; 880 hadAnyUse = true; 881 } 882 883 void handleConstRefUseOfUninitVariable(const VarDecl *vd, 884 const UninitUse &use) override { 885 hadUse[currentBlock] = true; 886 hadAnyUse = true; 887 } 888 889 /// Called when the uninitialized variable analysis detects the 890 /// idiom 'int x = x'. All other uses of 'x' within the initializer 891 /// are handled by handleUseOfUninitVariable. 892 void handleSelfInit(const VarDecl *vd) override { 893 hadUse[currentBlock] = true; 894 hadAnyUse = true; 895 } 896 }; 897 898 } // namespace 899 900 void clang::runUninitializedVariablesAnalysis( 901 const DeclContext &dc, 902 const CFG &cfg, 903 AnalysisDeclContext &ac, 904 UninitVariablesHandler &handler, 905 UninitVariablesAnalysisStats &stats) { 906 CFGBlockValues vals(cfg); 907 vals.computeSetOfDeclarations(dc); 908 if (vals.hasNoDeclarations()) 909 return; 910 911 stats.NumVariablesAnalyzed = vals.getNumEntries(); 912 913 // Precompute which expressions are uses and which are initializations. 914 ClassifyRefs classification(ac); 915 cfg.VisitBlockStmts(classification); 916 917 // Mark all variables uninitialized at the entry. 918 const CFGBlock &entry = cfg.getEntry(); 919 ValueVector &vec = vals.getValueVector(&entry); 920 const unsigned n = vals.getNumEntries(); 921 for (unsigned j = 0; j < n; ++j) { 922 vec[j] = Uninitialized; 923 } 924 925 // Proceed with the workist. 926 ForwardDataflowWorklist worklist(cfg, ac); 927 llvm::BitVector previouslyVisited(cfg.getNumBlockIDs()); 928 worklist.enqueueSuccessors(&cfg.getEntry()); 929 llvm::BitVector wasAnalyzed(cfg.getNumBlockIDs(), false); 930 wasAnalyzed[cfg.getEntry().getBlockID()] = true; 931 PruneBlocksHandler PBH(cfg.getNumBlockIDs()); 932 933 while (const CFGBlock *block = worklist.dequeue()) { 934 PBH.currentBlock = block->getBlockID(); 935 936 // Did the block change? 937 bool changed = runOnBlock(block, cfg, ac, vals, 938 classification, wasAnalyzed, PBH); 939 ++stats.NumBlockVisits; 940 if (changed || !previouslyVisited[block->getBlockID()]) 941 worklist.enqueueSuccessors(block); 942 previouslyVisited[block->getBlockID()] = true; 943 } 944 945 if (!PBH.hadAnyUse) 946 return; 947 948 // Run through the blocks one more time, and report uninitialized variables. 949 for (const auto *block : cfg) 950 if (PBH.hadUse[block->getBlockID()]) { 951 runOnBlock(block, cfg, ac, vals, classification, wasAnalyzed, handler); 952 ++stats.NumBlockVisits; 953 } 954 } 955 956 UninitVariablesHandler::~UninitVariablesHandler() = default; 957