UninitializedObjectChecker.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. //===----- UninitializedObjectChecker.cpp ------------------------*- C++ -*-==//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file defines a checker that reports uninitialized fields in objects
  11. // created after a constructor call.
  12. //
  13. // To read about command line options and how the checker works, refer to the
  14. // top of the file and inline comments in UninitializedObject.h.
  15. //
  16. // Some of the logic is implemented in UninitializedPointee.cpp, to reduce the
  17. // complexity of this file.
  18. //
  19. //===----------------------------------------------------------------------===//
  20. #include "../ClangSACheckers.h"
  21. #include "UninitializedObject.h"
  22. #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
  23. #include "clang/StaticAnalyzer/Core/Checker.h"
  24. #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
  25. #include "clang/StaticAnalyzer/Core/PathSensitive/DynamicTypeMap.h"
  26. using namespace clang;
  27. using namespace clang::ento;
  28. namespace {
  29. class UninitializedObjectChecker : public Checker<check::EndFunction> {
  30. std::unique_ptr<BuiltinBug> BT_uninitField;
  31. public:
  32. // The fields of this struct will be initialized when registering the checker.
  33. UninitObjCheckerOptions Opts;
  34. UninitializedObjectChecker()
  35. : BT_uninitField(new BuiltinBug(this, "Uninitialized fields")) {}
  36. void checkEndFunction(const ReturnStmt *RS, CheckerContext &C) const;
  37. };
  38. /// A basic field type, that is not a pointer or a reference, it's dynamic and
  39. /// static type is the same.
  40. class RegularField final : public FieldNode {
  41. public:
  42. RegularField(const FieldRegion *FR) : FieldNode(FR) {}
  43. virtual void printNoteMsg(llvm::raw_ostream &Out) const override {
  44. Out << "uninitialized field ";
  45. }
  46. virtual void printPrefix(llvm::raw_ostream &Out) const override {}
  47. virtual void printNode(llvm::raw_ostream &Out) const override {
  48. Out << getVariableName(getDecl());
  49. }
  50. virtual void printSeparator(llvm::raw_ostream &Out) const override {
  51. Out << '.';
  52. }
  53. };
  54. /// Represents that the FieldNode that comes after this is declared in a base
  55. /// of the previous FieldNode. As such, this descendant doesn't wrap a
  56. /// FieldRegion, and is purely a tool to describe a relation between two other
  57. /// FieldRegion wrapping descendants.
  58. class BaseClass final : public FieldNode {
  59. const QualType BaseClassT;
  60. public:
  61. BaseClass(const QualType &T) : FieldNode(nullptr), BaseClassT(T) {
  62. assert(!T.isNull());
  63. assert(T->getAsCXXRecordDecl());
  64. }
  65. virtual void printNoteMsg(llvm::raw_ostream &Out) const override {
  66. llvm_unreachable("This node can never be the final node in the "
  67. "fieldchain!");
  68. }
  69. virtual void printPrefix(llvm::raw_ostream &Out) const override {}
  70. virtual void printNode(llvm::raw_ostream &Out) const override {
  71. Out << BaseClassT->getAsCXXRecordDecl()->getName() << "::";
  72. }
  73. virtual void printSeparator(llvm::raw_ostream &Out) const override {}
  74. virtual bool isBase() const override { return true; }
  75. };
  76. } // end of anonymous namespace
  77. // Utility function declarations.
  78. /// Returns the object that was constructed by CtorDecl, or None if that isn't
  79. /// possible.
  80. // TODO: Refactor this function so that it returns the constructed object's
  81. // region.
  82. static Optional<nonloc::LazyCompoundVal>
  83. getObjectVal(const CXXConstructorDecl *CtorDecl, CheckerContext &Context);
  84. /// Checks whether the object constructed by \p Ctor will be analyzed later
  85. /// (e.g. if the object is a field of another object, in which case we'd check
  86. /// it multiple times).
  87. static bool willObjectBeAnalyzedLater(const CXXConstructorDecl *Ctor,
  88. CheckerContext &Context);
  89. /// Checks whether RD contains a field with a name or type name that matches
  90. /// \p Pattern.
  91. static bool shouldIgnoreRecord(const RecordDecl *RD, StringRef Pattern);
  92. //===----------------------------------------------------------------------===//
  93. // Methods for UninitializedObjectChecker.
  94. //===----------------------------------------------------------------------===//
  95. void UninitializedObjectChecker::checkEndFunction(
  96. const ReturnStmt *RS, CheckerContext &Context) const {
  97. const auto *CtorDecl = dyn_cast_or_null<CXXConstructorDecl>(
  98. Context.getLocationContext()->getDecl());
  99. if (!CtorDecl)
  100. return;
  101. if (!CtorDecl->isUserProvided())
  102. return;
  103. if (CtorDecl->getParent()->isUnion())
  104. return;
  105. // This avoids essentially the same error being reported multiple times.
  106. if (willObjectBeAnalyzedLater(CtorDecl, Context))
  107. return;
  108. Optional<nonloc::LazyCompoundVal> Object = getObjectVal(CtorDecl, Context);
  109. if (!Object)
  110. return;
  111. FindUninitializedFields F(Context.getState(), Object->getRegion(), Opts);
  112. const UninitFieldMap &UninitFields = F.getUninitFields();
  113. if (UninitFields.empty())
  114. return;
  115. // There are uninitialized fields in the record.
  116. ExplodedNode *Node = Context.generateNonFatalErrorNode(Context.getState());
  117. if (!Node)
  118. return;
  119. PathDiagnosticLocation LocUsedForUniqueing;
  120. const Stmt *CallSite = Context.getStackFrame()->getCallSite();
  121. if (CallSite)
  122. LocUsedForUniqueing = PathDiagnosticLocation::createBegin(
  123. CallSite, Context.getSourceManager(), Node->getLocationContext());
  124. // For Plist consumers that don't support notes just yet, we'll convert notes
  125. // to warnings.
  126. if (Opts.ShouldConvertNotesToWarnings) {
  127. for (const auto &Pair : UninitFields) {
  128. auto Report = llvm::make_unique<BugReport>(
  129. *BT_uninitField, Pair.second, Node, LocUsedForUniqueing,
  130. Node->getLocationContext()->getDecl());
  131. Context.emitReport(std::move(Report));
  132. }
  133. return;
  134. }
  135. SmallString<100> WarningBuf;
  136. llvm::raw_svector_ostream WarningOS(WarningBuf);
  137. WarningOS << UninitFields.size() << " uninitialized field"
  138. << (UninitFields.size() == 1 ? "" : "s")
  139. << " at the end of the constructor call";
  140. auto Report = llvm::make_unique<BugReport>(
  141. *BT_uninitField, WarningOS.str(), Node, LocUsedForUniqueing,
  142. Node->getLocationContext()->getDecl());
  143. for (const auto &Pair : UninitFields) {
  144. Report->addNote(Pair.second,
  145. PathDiagnosticLocation::create(Pair.first->getDecl(),
  146. Context.getSourceManager()));
  147. }
  148. Context.emitReport(std::move(Report));
  149. }
  150. //===----------------------------------------------------------------------===//
  151. // Methods for FindUninitializedFields.
  152. //===----------------------------------------------------------------------===//
  153. FindUninitializedFields::FindUninitializedFields(
  154. ProgramStateRef State, const TypedValueRegion *const R,
  155. const UninitObjCheckerOptions &Opts)
  156. : State(State), ObjectR(R), Opts(Opts) {
  157. isNonUnionUninit(ObjectR, FieldChainInfo(ChainFactory));
  158. // In non-pedantic mode, if ObjectR doesn't contain a single initialized
  159. // field, we'll assume that Object was intentionally left uninitialized.
  160. if (!Opts.IsPedantic && !isAnyFieldInitialized())
  161. UninitFields.clear();
  162. }
  163. bool FindUninitializedFields::addFieldToUninits(FieldChainInfo Chain) {
  164. if (State->getStateManager().getContext().getSourceManager().isInSystemHeader(
  165. Chain.getUninitRegion()->getDecl()->getLocation()))
  166. return false;
  167. UninitFieldMap::mapped_type NoteMsgBuf;
  168. llvm::raw_svector_ostream OS(NoteMsgBuf);
  169. Chain.printNoteMsg(OS);
  170. return UninitFields
  171. .insert(std::make_pair(Chain.getUninitRegion(), std::move(NoteMsgBuf)))
  172. .second;
  173. }
  174. bool FindUninitializedFields::isNonUnionUninit(const TypedValueRegion *R,
  175. FieldChainInfo LocalChain) {
  176. assert(R->getValueType()->isRecordType() &&
  177. !R->getValueType()->isUnionType() &&
  178. "This method only checks non-union record objects!");
  179. const RecordDecl *RD = R->getValueType()->getAsRecordDecl()->getDefinition();
  180. if (!RD) {
  181. IsAnyFieldInitialized = true;
  182. return true;
  183. }
  184. if (!Opts.IgnoredRecordsWithFieldPattern.empty() &&
  185. shouldIgnoreRecord(RD, Opts.IgnoredRecordsWithFieldPattern)) {
  186. IsAnyFieldInitialized = true;
  187. return false;
  188. }
  189. bool ContainsUninitField = false;
  190. // Are all of this non-union's fields initialized?
  191. for (const FieldDecl *I : RD->fields()) {
  192. const auto FieldVal =
  193. State->getLValue(I, loc::MemRegionVal(R)).castAs<loc::MemRegionVal>();
  194. const auto *FR = FieldVal.getRegionAs<FieldRegion>();
  195. QualType T = I->getType();
  196. // If LocalChain already contains FR, then we encountered a cyclic
  197. // reference. In this case, region FR is already under checking at an
  198. // earlier node in the directed tree.
  199. if (LocalChain.contains(FR))
  200. return false;
  201. if (T->isStructureOrClassType()) {
  202. if (isNonUnionUninit(FR, LocalChain.add(RegularField(FR))))
  203. ContainsUninitField = true;
  204. continue;
  205. }
  206. if (T->isUnionType()) {
  207. if (isUnionUninit(FR)) {
  208. if (addFieldToUninits(LocalChain.add(RegularField(FR))))
  209. ContainsUninitField = true;
  210. } else
  211. IsAnyFieldInitialized = true;
  212. continue;
  213. }
  214. if (T->isArrayType()) {
  215. IsAnyFieldInitialized = true;
  216. continue;
  217. }
  218. SVal V = State->getSVal(FieldVal);
  219. if (isDereferencableType(T) || V.getAs<nonloc::LocAsInteger>()) {
  220. if (isDereferencableUninit(FR, LocalChain))
  221. ContainsUninitField = true;
  222. continue;
  223. }
  224. if (isPrimitiveType(T)) {
  225. if (isPrimitiveUninit(V)) {
  226. if (addFieldToUninits(LocalChain.add(RegularField(FR))))
  227. ContainsUninitField = true;
  228. }
  229. continue;
  230. }
  231. llvm_unreachable("All cases are handled!");
  232. }
  233. // Checking bases. The checker will regard inherited data members as direct
  234. // fields.
  235. const auto *CXXRD = dyn_cast<CXXRecordDecl>(RD);
  236. if (!CXXRD)
  237. return ContainsUninitField;
  238. for (const CXXBaseSpecifier &BaseSpec : CXXRD->bases()) {
  239. const auto *BaseRegion = State->getLValue(BaseSpec, R)
  240. .castAs<loc::MemRegionVal>()
  241. .getRegionAs<TypedValueRegion>();
  242. // If the head of the list is also a BaseClass, we'll overwrite it to avoid
  243. // note messages like 'this->A::B::x'.
  244. if (!LocalChain.isEmpty() && LocalChain.getHead().isBase()) {
  245. if (isNonUnionUninit(BaseRegion, LocalChain.replaceHead(
  246. BaseClass(BaseSpec.getType()))))
  247. ContainsUninitField = true;
  248. } else {
  249. if (isNonUnionUninit(BaseRegion,
  250. LocalChain.add(BaseClass(BaseSpec.getType()))))
  251. ContainsUninitField = true;
  252. }
  253. }
  254. return ContainsUninitField;
  255. }
  256. bool FindUninitializedFields::isUnionUninit(const TypedValueRegion *R) {
  257. assert(R->getValueType()->isUnionType() &&
  258. "This method only checks union objects!");
  259. // TODO: Implement support for union fields.
  260. return false;
  261. }
  262. bool FindUninitializedFields::isPrimitiveUninit(const SVal &V) {
  263. if (V.isUndef())
  264. return true;
  265. IsAnyFieldInitialized = true;
  266. return false;
  267. }
  268. //===----------------------------------------------------------------------===//
  269. // Methods for FieldChainInfo.
  270. //===----------------------------------------------------------------------===//
  271. const FieldRegion *FieldChainInfo::getUninitRegion() const {
  272. assert(!Chain.isEmpty() && "Empty fieldchain!");
  273. // ImmutableList::getHead() isn't a const method, hence the not too nice
  274. // implementation.
  275. return (*Chain.begin()).getRegion();
  276. }
  277. bool FieldChainInfo::contains(const FieldRegion *FR) const {
  278. for (const FieldNode &Node : Chain) {
  279. if (Node.isSameRegion(FR))
  280. return true;
  281. }
  282. return false;
  283. }
  284. /// Prints every element except the last to `Out`. Since ImmutableLists store
  285. /// elements in reverse order, and have no reverse iterators, we use a
  286. /// recursive function to print the fieldchain correctly. The last element in
  287. /// the chain is to be printed by `FieldChainInfo::print`.
  288. static void printTail(llvm::raw_ostream &Out,
  289. const FieldChainInfo::FieldChainImpl *L);
  290. // FIXME: This function constructs an incorrect string in the following case:
  291. //
  292. // struct Base { int x; };
  293. // struct D1 : Base {}; struct D2 : Base {};
  294. //
  295. // struct MostDerived : D1, D2 {
  296. // MostDerived() {}
  297. // }
  298. //
  299. // A call to MostDerived::MostDerived() will cause two notes that say
  300. // "uninitialized field 'this->x'", but we can't refer to 'x' directly,
  301. // we need an explicit namespace resolution whether the uninit field was
  302. // 'D1::x' or 'D2::x'.
  303. void FieldChainInfo::printNoteMsg(llvm::raw_ostream &Out) const {
  304. if (Chain.isEmpty())
  305. return;
  306. const FieldChainImpl *L = Chain.getInternalPointer();
  307. const FieldNode &LastField = L->getHead();
  308. LastField.printNoteMsg(Out);
  309. Out << '\'';
  310. for (const FieldNode &Node : Chain)
  311. Node.printPrefix(Out);
  312. Out << "this->";
  313. printTail(Out, L->getTail());
  314. LastField.printNode(Out);
  315. Out << '\'';
  316. }
  317. static void printTail(llvm::raw_ostream &Out,
  318. const FieldChainInfo::FieldChainImpl *L) {
  319. if (!L)
  320. return;
  321. printTail(Out, L->getTail());
  322. L->getHead().printNode(Out);
  323. L->getHead().printSeparator(Out);
  324. }
  325. //===----------------------------------------------------------------------===//
  326. // Utility functions.
  327. //===----------------------------------------------------------------------===//
  328. static Optional<nonloc::LazyCompoundVal>
  329. getObjectVal(const CXXConstructorDecl *CtorDecl, CheckerContext &Context) {
  330. Loc ThisLoc = Context.getSValBuilder().getCXXThis(CtorDecl->getParent(),
  331. Context.getStackFrame());
  332. // Getting the value for 'this'.
  333. SVal This = Context.getState()->getSVal(ThisLoc);
  334. // Getting the value for '*this'.
  335. SVal Object = Context.getState()->getSVal(This.castAs<Loc>());
  336. return Object.getAs<nonloc::LazyCompoundVal>();
  337. }
  338. static bool willObjectBeAnalyzedLater(const CXXConstructorDecl *Ctor,
  339. CheckerContext &Context) {
  340. Optional<nonloc::LazyCompoundVal> CurrentObject = getObjectVal(Ctor, Context);
  341. if (!CurrentObject)
  342. return false;
  343. const LocationContext *LC = Context.getLocationContext();
  344. while ((LC = LC->getParent())) {
  345. // If \p Ctor was called by another constructor.
  346. const auto *OtherCtor = dyn_cast<CXXConstructorDecl>(LC->getDecl());
  347. if (!OtherCtor)
  348. continue;
  349. Optional<nonloc::LazyCompoundVal> OtherObject =
  350. getObjectVal(OtherCtor, Context);
  351. if (!OtherObject)
  352. continue;
  353. // If the CurrentObject is a subregion of OtherObject, it will be analyzed
  354. // during the analysis of OtherObject.
  355. if (CurrentObject->getRegion()->isSubRegionOf(OtherObject->getRegion()))
  356. return true;
  357. }
  358. return false;
  359. }
  360. static bool shouldIgnoreRecord(const RecordDecl *RD, StringRef Pattern) {
  361. llvm::Regex R(Pattern);
  362. for (const FieldDecl *FD : RD->fields()) {
  363. if (R.match(FD->getType().getAsString()))
  364. return true;
  365. if (R.match(FD->getName()))
  366. return true;
  367. }
  368. return false;
  369. }
  370. std::string clang::ento::getVariableName(const FieldDecl *Field) {
  371. // If Field is a captured lambda variable, Field->getName() will return with
  372. // an empty string. We can however acquire it's name from the lambda's
  373. // captures.
  374. const auto *CXXParent = dyn_cast<CXXRecordDecl>(Field->getParent());
  375. if (CXXParent && CXXParent->isLambda()) {
  376. assert(CXXParent->captures_begin());
  377. auto It = CXXParent->captures_begin() + Field->getFieldIndex();
  378. if (It->capturesVariable())
  379. return llvm::Twine("/*captured variable*/" +
  380. It->getCapturedVar()->getName())
  381. .str();
  382. if (It->capturesThis())
  383. return "/*'this' capture*/";
  384. llvm_unreachable("No other capture type is expected!");
  385. }
  386. return Field->getName();
  387. }
  388. void ento::registerUninitializedObjectChecker(CheckerManager &Mgr) {
  389. auto Chk = Mgr.registerChecker<UninitializedObjectChecker>();
  390. AnalyzerOptions &AnOpts = Mgr.getAnalyzerOptions();
  391. UninitObjCheckerOptions &ChOpts = Chk->Opts;
  392. ChOpts.IsPedantic =
  393. AnOpts.getBooleanOption("Pedantic", /*DefaultVal*/ false, Chk);
  394. ChOpts.ShouldConvertNotesToWarnings =
  395. AnOpts.getBooleanOption("NotesAsWarnings", /*DefaultVal*/ false, Chk);
  396. ChOpts.CheckPointeeInitialization = AnOpts.getBooleanOption(
  397. "CheckPointeeInitialization", /*DefaultVal*/ false, Chk);
  398. ChOpts.IgnoredRecordsWithFieldPattern =
  399. AnOpts.getOptionAsString("IgnoreRecordsWithField",
  400. /*DefaultVal*/ "", Chk);
  401. }