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