UninitializedObjectChecker.cpp 17 KB

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