1 //===----- UninitializedObjectChecker.cpp ------------------------*- C++ -*-==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines a checker that reports uninitialized fields in objects
11 // created after a constructor call.
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.
16 // Some of the logic is implemented in UninitializedPointee.cpp, to reduce the
17 // complexity of this file.
19 //===----------------------------------------------------------------------===//
21 #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
22 #include "UninitializedObject.h"
23 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
24 #include "clang/StaticAnalyzer/Core/Checker.h"
25 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
26 #include "clang/StaticAnalyzer/Core/PathSensitive/DynamicTypeMap.h"
28 using namespace clang;
29 using namespace clang::ento;
31 /// We'll mark fields (and pointee of fields) that are confirmed to be
32 /// uninitialized as already analyzed.
33 REGISTER_SET_WITH_PROGRAMSTATE(AnalyzedRegions, const MemRegion *)
37 class UninitializedObjectChecker
38 : public Checker<check::EndFunction, check::DeadSymbols> {
39 std::unique_ptr<BuiltinBug> BT_uninitField;
42 // The fields of this struct will be initialized when registering the checker.
43 UninitObjCheckerOptions Opts;
45 UninitializedObjectChecker()
46 : BT_uninitField(new BuiltinBug(this, "Uninitialized fields")) {}
48 void checkEndFunction(const ReturnStmt *RS, CheckerContext &C) const;
49 void checkDeadSymbols(SymbolReaper &SR, CheckerContext &C) const;
52 /// A basic field type, that is not a pointer or a reference, it's dynamic and
53 /// static type is the same.
54 class RegularField final : public FieldNode {
56 RegularField(const FieldRegion *FR) : FieldNode(FR) {}
58 virtual void printNoteMsg(llvm::raw_ostream &Out) const override {
59 Out << "uninitialized field ";
62 virtual void printPrefix(llvm::raw_ostream &Out) const override {}
64 virtual void printNode(llvm::raw_ostream &Out) const override {
65 Out << getVariableName(getDecl());
68 virtual void printSeparator(llvm::raw_ostream &Out) const override {
73 /// Represents that the FieldNode that comes after this is declared in a base
74 /// of the previous FieldNode. As such, this descendant doesn't wrap a
75 /// FieldRegion, and is purely a tool to describe a relation between two other
76 /// FieldRegion wrapping descendants.
77 class BaseClass final : public FieldNode {
78 const QualType BaseClassT;
81 BaseClass(const QualType &T) : FieldNode(nullptr), BaseClassT(T) {
83 assert(T->getAsCXXRecordDecl());
86 virtual void printNoteMsg(llvm::raw_ostream &Out) const override {
87 llvm_unreachable("This node can never be the final node in the "
91 virtual void printPrefix(llvm::raw_ostream &Out) const override {}
93 virtual void printNode(llvm::raw_ostream &Out) const override {
94 Out << BaseClassT->getAsCXXRecordDecl()->getName() << "::";
97 virtual void printSeparator(llvm::raw_ostream &Out) const override {}
99 virtual bool isBase() const override { return true; }
102 } // end of anonymous namespace
104 // Utility function declarations.
106 /// Returns the region that was constructed by CtorDecl, or nullptr if that
108 static const TypedValueRegion *
109 getConstructedRegion(const CXXConstructorDecl *CtorDecl,
110 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);
118 /// Checks whether RD contains a field with a name or type name that matches
120 static bool shouldIgnoreRecord(const RecordDecl *RD, StringRef Pattern);
122 //===----------------------------------------------------------------------===//
123 // Methods for UninitializedObjectChecker.
124 //===----------------------------------------------------------------------===//
126 void UninitializedObjectChecker::checkEndFunction(
127 const ReturnStmt *RS, CheckerContext &Context) const {
129 const auto *CtorDecl = dyn_cast_or_null<CXXConstructorDecl>(
130 Context.getLocationContext()->getDecl());
134 if (!CtorDecl->isUserProvided())
137 if (CtorDecl->getParent()->isUnion())
140 // This avoids essentially the same error being reported multiple times.
141 if (willObjectBeAnalyzedLater(CtorDecl, Context))
144 const TypedValueRegion *R = getConstructedRegion(CtorDecl, Context);
148 FindUninitializedFields F(Context.getState(), R, Opts);
150 std::pair<ProgramStateRef, const UninitFieldMap &> UninitInfo =
153 ProgramStateRef UpdatedState = UninitInfo.first;
154 const UninitFieldMap &UninitFields = UninitInfo.second;
156 if (UninitFields.empty()) {
157 Context.addTransition(UpdatedState);
161 // There are uninitialized fields in the record.
163 ExplodedNode *Node = Context.generateNonFatalErrorNode(UpdatedState);
167 PathDiagnosticLocation LocUsedForUniqueing;
168 const Stmt *CallSite = Context.getStackFrame()->getCallSite();
170 LocUsedForUniqueing = PathDiagnosticLocation::createBegin(
171 CallSite, Context.getSourceManager(), Node->getLocationContext());
173 // For Plist consumers that don't support notes just yet, we'll convert notes
175 if (Opts.ShouldConvertNotesToWarnings) {
176 for (const auto &Pair : UninitFields) {
178 auto Report = llvm::make_unique<BugReport>(
179 *BT_uninitField, Pair.second, Node, LocUsedForUniqueing,
180 Node->getLocationContext()->getDecl());
181 Context.emitReport(std::move(Report));
186 SmallString<100> WarningBuf;
187 llvm::raw_svector_ostream WarningOS(WarningBuf);
188 WarningOS << UninitFields.size() << " uninitialized field"
189 << (UninitFields.size() == 1 ? "" : "s")
190 << " at the end of the constructor call";
192 auto Report = llvm::make_unique<BugReport>(
193 *BT_uninitField, WarningOS.str(), Node, LocUsedForUniqueing,
194 Node->getLocationContext()->getDecl());
196 for (const auto &Pair : UninitFields) {
197 Report->addNote(Pair.second,
198 PathDiagnosticLocation::create(Pair.first->getDecl(),
199 Context.getSourceManager()));
201 Context.emitReport(std::move(Report));
204 void UninitializedObjectChecker::checkDeadSymbols(SymbolReaper &SR,
205 CheckerContext &C) const {
206 ProgramStateRef State = C.getState();
207 for (const MemRegion *R : State->get<AnalyzedRegions>()) {
208 if (!SR.isLiveRegion(R))
209 State = State->remove<AnalyzedRegions>(R);
213 //===----------------------------------------------------------------------===//
214 // Methods for FindUninitializedFields.
215 //===----------------------------------------------------------------------===//
217 FindUninitializedFields::FindUninitializedFields(
218 ProgramStateRef State, const TypedValueRegion *const R,
219 const UninitObjCheckerOptions &Opts)
220 : State(State), ObjectR(R), Opts(Opts) {
222 isNonUnionUninit(ObjectR, FieldChainInfo(ChainFactory));
224 // In non-pedantic mode, if ObjectR doesn't contain a single initialized
225 // field, we'll assume that Object was intentionally left uninitialized.
226 if (!Opts.IsPedantic && !isAnyFieldInitialized())
227 UninitFields.clear();
230 bool FindUninitializedFields::addFieldToUninits(FieldChainInfo Chain,
231 const MemRegion *PointeeR) {
232 const FieldRegion *FR = Chain.getUninitRegion();
234 assert((PointeeR || !isDereferencableType(FR->getDecl()->getType())) &&
235 "One must also pass the pointee region as a parameter for "
236 "dereferenceable fields!");
238 if (State->contains<AnalyzedRegions>(FR))
242 if (State->contains<AnalyzedRegions>(PointeeR)) {
245 State = State->add<AnalyzedRegions>(PointeeR);
248 State = State->add<AnalyzedRegions>(FR);
250 if (State->getStateManager().getContext().getSourceManager().isInSystemHeader(
251 FR->getDecl()->getLocation()))
254 UninitFieldMap::mapped_type NoteMsgBuf;
255 llvm::raw_svector_ostream OS(NoteMsgBuf);
256 Chain.printNoteMsg(OS);
257 return UninitFields.insert({FR, std::move(NoteMsgBuf)}).second;
260 bool FindUninitializedFields::isNonUnionUninit(const TypedValueRegion *R,
261 FieldChainInfo LocalChain) {
262 assert(R->getValueType()->isRecordType() &&
263 !R->getValueType()->isUnionType() &&
264 "This method only checks non-union record objects!");
266 const RecordDecl *RD = R->getValueType()->getAsRecordDecl()->getDefinition();
269 IsAnyFieldInitialized = true;
273 if (!Opts.IgnoredRecordsWithFieldPattern.empty() &&
274 shouldIgnoreRecord(RD, Opts.IgnoredRecordsWithFieldPattern)) {
275 IsAnyFieldInitialized = true;
279 bool ContainsUninitField = false;
281 // Are all of this non-union's fields initialized?
282 for (const FieldDecl *I : RD->fields()) {
284 const auto FieldVal =
285 State->getLValue(I, loc::MemRegionVal(R)).castAs<loc::MemRegionVal>();
286 const auto *FR = FieldVal.getRegionAs<FieldRegion>();
287 QualType T = I->getType();
289 // If LocalChain already contains FR, then we encountered a cyclic
290 // reference. In this case, region FR is already under checking at an
291 // earlier node in the directed tree.
292 if (LocalChain.contains(FR))
295 if (T->isStructureOrClassType()) {
296 if (isNonUnionUninit(FR, LocalChain.add(RegularField(FR))))
297 ContainsUninitField = true;
301 if (T->isUnionType()) {
302 if (isUnionUninit(FR)) {
303 if (addFieldToUninits(LocalChain.add(RegularField(FR))))
304 ContainsUninitField = true;
306 IsAnyFieldInitialized = true;
310 if (T->isArrayType()) {
311 IsAnyFieldInitialized = true;
315 SVal V = State->getSVal(FieldVal);
317 if (isDereferencableType(T) || V.getAs<nonloc::LocAsInteger>()) {
318 if (isDereferencableUninit(FR, LocalChain))
319 ContainsUninitField = true;
323 if (isPrimitiveType(T)) {
324 if (isPrimitiveUninit(V)) {
325 if (addFieldToUninits(LocalChain.add(RegularField(FR))))
326 ContainsUninitField = true;
331 llvm_unreachable("All cases are handled!");
334 // Checking bases. The checker will regard inherited data members as direct
336 const auto *CXXRD = dyn_cast<CXXRecordDecl>(RD);
338 return ContainsUninitField;
340 for (const CXXBaseSpecifier &BaseSpec : CXXRD->bases()) {
341 const auto *BaseRegion = State->getLValue(BaseSpec, R)
342 .castAs<loc::MemRegionVal>()
343 .getRegionAs<TypedValueRegion>();
345 // If the head of the list is also a BaseClass, we'll overwrite it to avoid
346 // note messages like 'this->A::B::x'.
347 if (!LocalChain.isEmpty() && LocalChain.getHead().isBase()) {
348 if (isNonUnionUninit(BaseRegion, LocalChain.replaceHead(
349 BaseClass(BaseSpec.getType()))))
350 ContainsUninitField = true;
352 if (isNonUnionUninit(BaseRegion,
353 LocalChain.add(BaseClass(BaseSpec.getType()))))
354 ContainsUninitField = true;
358 return ContainsUninitField;
361 bool FindUninitializedFields::isUnionUninit(const TypedValueRegion *R) {
362 assert(R->getValueType()->isUnionType() &&
363 "This method only checks union objects!");
364 // TODO: Implement support for union fields.
368 bool FindUninitializedFields::isPrimitiveUninit(const SVal &V) {
372 IsAnyFieldInitialized = true;
376 //===----------------------------------------------------------------------===//
377 // Methods for FieldChainInfo.
378 //===----------------------------------------------------------------------===//
380 bool FieldChainInfo::contains(const FieldRegion *FR) const {
381 for (const FieldNode &Node : Chain) {
382 if (Node.isSameRegion(FR))
388 /// Prints every element except the last to `Out`. Since ImmutableLists store
389 /// elements in reverse order, and have no reverse iterators, we use a
390 /// recursive function to print the fieldchain correctly. The last element in
391 /// the chain is to be printed by `FieldChainInfo::print`.
392 static void printTail(llvm::raw_ostream &Out,
393 const FieldChainInfo::FieldChain L);
395 // FIXME: This function constructs an incorrect string in the following case:
397 // struct Base { int x; };
398 // struct D1 : Base {}; struct D2 : Base {};
400 // struct MostDerived : D1, D2 {
404 // A call to MostDerived::MostDerived() will cause two notes that say
405 // "uninitialized field 'this->x'", but we can't refer to 'x' directly,
406 // we need an explicit namespace resolution whether the uninit field was
407 // 'D1::x' or 'D2::x'.
408 void FieldChainInfo::printNoteMsg(llvm::raw_ostream &Out) const {
412 const FieldNode &LastField = getHead();
414 LastField.printNoteMsg(Out);
417 for (const FieldNode &Node : Chain)
418 Node.printPrefix(Out);
421 printTail(Out, Chain.getTail());
422 LastField.printNode(Out);
426 static void printTail(llvm::raw_ostream &Out,
427 const FieldChainInfo::FieldChain L) {
431 printTail(Out, L.getTail());
433 L.getHead().printNode(Out);
434 L.getHead().printSeparator(Out);
437 //===----------------------------------------------------------------------===//
438 // Utility functions.
439 //===----------------------------------------------------------------------===//
441 static const TypedValueRegion *
442 getConstructedRegion(const CXXConstructorDecl *CtorDecl,
443 CheckerContext &Context) {
445 Loc ThisLoc = Context.getSValBuilder().getCXXThis(CtorDecl,
446 Context.getStackFrame());
448 SVal ObjectV = Context.getState()->getSVal(ThisLoc);
450 auto *R = ObjectV.getAsRegion()->getAs<TypedValueRegion>();
451 if (R && !R->getValueType()->getAsCXXRecordDecl())
457 static bool willObjectBeAnalyzedLater(const CXXConstructorDecl *Ctor,
458 CheckerContext &Context) {
460 const TypedValueRegion *CurrRegion = getConstructedRegion(Ctor, Context);
464 const LocationContext *LC = Context.getLocationContext();
465 while ((LC = LC->getParent())) {
467 // If \p Ctor was called by another constructor.
468 const auto *OtherCtor = dyn_cast<CXXConstructorDecl>(LC->getDecl());
472 const TypedValueRegion *OtherRegion =
473 getConstructedRegion(OtherCtor, Context);
477 // If the CurrRegion is a subregion of OtherRegion, it will be analyzed
478 // during the analysis of OtherRegion.
479 if (CurrRegion->isSubRegionOf(OtherRegion))
486 static bool shouldIgnoreRecord(const RecordDecl *RD, StringRef Pattern) {
487 llvm::Regex R(Pattern);
489 for (const FieldDecl *FD : RD->fields()) {
490 if (R.match(FD->getType().getAsString()))
492 if (R.match(FD->getName()))
499 std::string clang::ento::getVariableName(const FieldDecl *Field) {
500 // If Field is a captured lambda variable, Field->getName() will return with
501 // an empty string. We can however acquire it's name from the lambda's
503 const auto *CXXParent = dyn_cast<CXXRecordDecl>(Field->getParent());
505 if (CXXParent && CXXParent->isLambda()) {
506 assert(CXXParent->captures_begin());
507 auto It = CXXParent->captures_begin() + Field->getFieldIndex();
509 if (It->capturesVariable())
510 return llvm::Twine("/*captured variable*/" +
511 It->getCapturedVar()->getName())
514 if (It->capturesThis())
515 return "/*'this' capture*/";
517 llvm_unreachable("No other capture type is expected!");
520 return Field->getName();
523 void ento::registerUninitializedObjectChecker(CheckerManager &Mgr) {
524 auto Chk = Mgr.registerChecker<UninitializedObjectChecker>();
526 AnalyzerOptions &AnOpts = Mgr.getAnalyzerOptions();
527 UninitObjCheckerOptions &ChOpts = Chk->Opts;
530 AnOpts.getCheckerBooleanOption("Pedantic", /*DefaultVal*/ false, Chk);
531 ChOpts.ShouldConvertNotesToWarnings =
532 AnOpts.getCheckerBooleanOption("NotesAsWarnings", /*DefaultVal*/ false, Chk);
533 ChOpts.CheckPointeeInitialization = AnOpts.getCheckerBooleanOption(
534 "CheckPointeeInitialization", /*DefaultVal*/ false, Chk);
535 ChOpts.IgnoredRecordsWithFieldPattern =
536 AnOpts.getCheckerStringOption("IgnoreRecordsWithField",
537 /*DefaultVal*/ "", Chk);