UninitializedValues.cpp 32 KB

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