1 //===----- UninitializedObjectChecker.cpp ------------------------*- C++ -*-==//
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
7 //===----------------------------------------------------------------------===//
9 // This file defines a checker that reports uninitialized fields in objects
10 // created after a constructor call.
12 // To read about command line options and how the checker works, refer to the
13 // top of the file and inline comments in UninitializedObject.h.
15 // Some of the logic is implemented in UninitializedPointee.cpp, to reduce the
16 // complexity of this file.
18 //===----------------------------------------------------------------------===//
20 #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
21 #include "UninitializedObject.h"
22 #include "clang/ASTMatchers/ASTMatchFinder.h"
23 #include "clang/Driver/DriverDiagnostic.h"
24 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
25 #include "clang/StaticAnalyzer/Core/Checker.h"
26 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
27 #include "clang/StaticAnalyzer/Core/PathSensitive/DynamicTypeMap.h"
29 using namespace clang;
30 using namespace clang::ento;
31 using namespace clang::ast_matchers;
33 /// We'll mark fields (and pointee of fields) that are confirmed to be
34 /// uninitialized as already analyzed.
35 REGISTER_SET_WITH_PROGRAMSTATE(AnalyzedRegions, const MemRegion *)
39 class UninitializedObjectChecker
40 : public Checker<check::EndFunction, check::DeadSymbols> {
41 std::unique_ptr<BuiltinBug> BT_uninitField;
44 // The fields of this struct will be initialized when registering the checker.
45 UninitObjCheckerOptions Opts;
47 UninitializedObjectChecker()
48 : BT_uninitField(new BuiltinBug(this, "Uninitialized fields")) {}
50 void checkEndFunction(const ReturnStmt *RS, CheckerContext &C) const;
51 void checkDeadSymbols(SymbolReaper &SR, CheckerContext &C) const;
54 /// A basic field type, that is not a pointer or a reference, it's dynamic and
55 /// static type is the same.
56 class RegularField final : public FieldNode {
58 RegularField(const FieldRegion *FR) : FieldNode(FR) {}
60 virtual void printNoteMsg(llvm::raw_ostream &Out) const override {
61 Out << "uninitialized field ";
64 virtual void printPrefix(llvm::raw_ostream &Out) const override {}
66 virtual void printNode(llvm::raw_ostream &Out) const override {
67 Out << getVariableName(getDecl());
70 virtual void printSeparator(llvm::raw_ostream &Out) const override {
75 /// Represents that the FieldNode that comes after this is declared in a base
76 /// of the previous FieldNode. As such, this descendant doesn't wrap a
77 /// FieldRegion, and is purely a tool to describe a relation between two other
78 /// FieldRegion wrapping descendants.
79 class BaseClass final : public FieldNode {
80 const QualType BaseClassT;
83 BaseClass(const QualType &T) : FieldNode(nullptr), BaseClassT(T) {
85 assert(T->getAsCXXRecordDecl());
88 virtual void printNoteMsg(llvm::raw_ostream &Out) const override {
89 llvm_unreachable("This node can never be the final node in the "
93 virtual void printPrefix(llvm::raw_ostream &Out) const override {}
95 virtual void printNode(llvm::raw_ostream &Out) const override {
96 Out << BaseClassT->getAsCXXRecordDecl()->getName() << "::";
99 virtual void printSeparator(llvm::raw_ostream &Out) const override {}
101 virtual bool isBase() const override { return true; }
104 } // end of anonymous namespace
106 // Utility function declarations.
108 /// Returns the region that was constructed by CtorDecl, or nullptr if that
110 static const TypedValueRegion *
111 getConstructedRegion(const CXXConstructorDecl *CtorDecl,
112 CheckerContext &Context);
114 /// Checks whether the object constructed by \p Ctor will be analyzed later
115 /// (e.g. if the object is a field of another object, in which case we'd check
116 /// it multiple times).
117 static bool willObjectBeAnalyzedLater(const CXXConstructorDecl *Ctor,
118 CheckerContext &Context);
120 /// Checks whether RD contains a field with a name or type name that matches
122 static bool shouldIgnoreRecord(const RecordDecl *RD, StringRef Pattern);
124 /// Checks _syntactically_ whether it is possible to access FD from the record
125 /// that contains it without a preceding assert (even if that access happens
126 /// inside a method). This is mainly used for records that act like unions, like
127 /// having multiple bit fields, with only a fraction being properly initialized.
128 /// If these fields are properly guarded with asserts, this method returns
131 /// Since this check is done syntactically, this method could be inaccurate.
132 static bool hasUnguardedAccess(const FieldDecl *FD, ProgramStateRef State);
134 //===----------------------------------------------------------------------===//
135 // Methods for UninitializedObjectChecker.
136 //===----------------------------------------------------------------------===//
138 void UninitializedObjectChecker::checkEndFunction(
139 const ReturnStmt *RS, CheckerContext &Context) const {
141 const auto *CtorDecl = dyn_cast_or_null<CXXConstructorDecl>(
142 Context.getLocationContext()->getDecl());
146 if (!CtorDecl->isUserProvided())
149 if (CtorDecl->getParent()->isUnion())
152 // This avoids essentially the same error being reported multiple times.
153 if (willObjectBeAnalyzedLater(CtorDecl, Context))
156 const TypedValueRegion *R = getConstructedRegion(CtorDecl, Context);
160 FindUninitializedFields F(Context.getState(), R, Opts);
162 std::pair<ProgramStateRef, const UninitFieldMap &> UninitInfo =
165 ProgramStateRef UpdatedState = UninitInfo.first;
166 const UninitFieldMap &UninitFields = UninitInfo.second;
168 if (UninitFields.empty()) {
169 Context.addTransition(UpdatedState);
173 // There are uninitialized fields in the record.
175 ExplodedNode *Node = Context.generateNonFatalErrorNode(UpdatedState);
179 PathDiagnosticLocation LocUsedForUniqueing;
180 const Stmt *CallSite = Context.getStackFrame()->getCallSite();
182 LocUsedForUniqueing = PathDiagnosticLocation::createBegin(
183 CallSite, Context.getSourceManager(), Node->getLocationContext());
185 // For Plist consumers that don't support notes just yet, we'll convert notes
187 if (Opts.ShouldConvertNotesToWarnings) {
188 for (const auto &Pair : UninitFields) {
190 auto Report = llvm::make_unique<BugReport>(
191 *BT_uninitField, Pair.second, Node, LocUsedForUniqueing,
192 Node->getLocationContext()->getDecl());
193 Context.emitReport(std::move(Report));
198 SmallString<100> WarningBuf;
199 llvm::raw_svector_ostream WarningOS(WarningBuf);
200 WarningOS << UninitFields.size() << " uninitialized field"
201 << (UninitFields.size() == 1 ? "" : "s")
202 << " at the end of the constructor call";
204 auto Report = llvm::make_unique<BugReport>(
205 *BT_uninitField, WarningOS.str(), Node, LocUsedForUniqueing,
206 Node->getLocationContext()->getDecl());
208 for (const auto &Pair : UninitFields) {
209 Report->addNote(Pair.second,
210 PathDiagnosticLocation::create(Pair.first->getDecl(),
211 Context.getSourceManager()));
213 Context.emitReport(std::move(Report));
216 void UninitializedObjectChecker::checkDeadSymbols(SymbolReaper &SR,
217 CheckerContext &C) const {
218 ProgramStateRef State = C.getState();
219 for (const MemRegion *R : State->get<AnalyzedRegions>()) {
220 if (!SR.isLiveRegion(R))
221 State = State->remove<AnalyzedRegions>(R);
225 //===----------------------------------------------------------------------===//
226 // Methods for FindUninitializedFields.
227 //===----------------------------------------------------------------------===//
229 FindUninitializedFields::FindUninitializedFields(
230 ProgramStateRef State, const TypedValueRegion *const R,
231 const UninitObjCheckerOptions &Opts)
232 : State(State), ObjectR(R), Opts(Opts) {
234 isNonUnionUninit(ObjectR, FieldChainInfo(ChainFactory));
236 // In non-pedantic mode, if ObjectR doesn't contain a single initialized
237 // field, we'll assume that Object was intentionally left uninitialized.
238 if (!Opts.IsPedantic && !isAnyFieldInitialized())
239 UninitFields.clear();
242 bool FindUninitializedFields::addFieldToUninits(FieldChainInfo Chain,
243 const MemRegion *PointeeR) {
244 const FieldRegion *FR = Chain.getUninitRegion();
246 assert((PointeeR || !isDereferencableType(FR->getDecl()->getType())) &&
247 "One must also pass the pointee region as a parameter for "
248 "dereferenceable fields!");
250 if (State->getStateManager().getContext().getSourceManager().isInSystemHeader(
251 FR->getDecl()->getLocation()))
254 if (Opts.IgnoreGuardedFields && !hasUnguardedAccess(FR->getDecl(), State))
257 if (State->contains<AnalyzedRegions>(FR))
261 if (State->contains<AnalyzedRegions>(PointeeR)) {
264 State = State->add<AnalyzedRegions>(PointeeR);
267 State = State->add<AnalyzedRegions>(FR);
269 UninitFieldMap::mapped_type NoteMsgBuf;
270 llvm::raw_svector_ostream OS(NoteMsgBuf);
271 Chain.printNoteMsg(OS);
273 return UninitFields.insert({FR, std::move(NoteMsgBuf)}).second;
276 bool FindUninitializedFields::isNonUnionUninit(const TypedValueRegion *R,
277 FieldChainInfo LocalChain) {
278 assert(R->getValueType()->isRecordType() &&
279 !R->getValueType()->isUnionType() &&
280 "This method only checks non-union record objects!");
282 const RecordDecl *RD = R->getValueType()->getAsRecordDecl()->getDefinition();
285 IsAnyFieldInitialized = true;
289 if (!Opts.IgnoredRecordsWithFieldPattern.empty() &&
290 shouldIgnoreRecord(RD, Opts.IgnoredRecordsWithFieldPattern)) {
291 IsAnyFieldInitialized = true;
295 bool ContainsUninitField = false;
297 // Are all of this non-union's fields initialized?
298 for (const FieldDecl *I : RD->fields()) {
300 const auto FieldVal =
301 State->getLValue(I, loc::MemRegionVal(R)).castAs<loc::MemRegionVal>();
302 const auto *FR = FieldVal.getRegionAs<FieldRegion>();
303 QualType T = I->getType();
305 // If LocalChain already contains FR, then we encountered a cyclic
306 // reference. In this case, region FR is already under checking at an
307 // earlier node in the directed tree.
308 if (LocalChain.contains(FR))
311 if (T->isStructureOrClassType()) {
312 if (isNonUnionUninit(FR, LocalChain.add(RegularField(FR))))
313 ContainsUninitField = true;
317 if (T->isUnionType()) {
318 if (isUnionUninit(FR)) {
319 if (addFieldToUninits(LocalChain.add(RegularField(FR))))
320 ContainsUninitField = true;
322 IsAnyFieldInitialized = true;
326 if (T->isArrayType()) {
327 IsAnyFieldInitialized = true;
331 SVal V = State->getSVal(FieldVal);
333 if (isDereferencableType(T) || V.getAs<nonloc::LocAsInteger>()) {
334 if (isDereferencableUninit(FR, LocalChain))
335 ContainsUninitField = true;
339 if (isPrimitiveType(T)) {
340 if (isPrimitiveUninit(V)) {
341 if (addFieldToUninits(LocalChain.add(RegularField(FR))))
342 ContainsUninitField = true;
347 llvm_unreachable("All cases are handled!");
350 // Checking bases. The checker will regard inherited data members as direct
352 const auto *CXXRD = dyn_cast<CXXRecordDecl>(RD);
354 return ContainsUninitField;
356 for (const CXXBaseSpecifier &BaseSpec : CXXRD->bases()) {
357 const auto *BaseRegion = State->getLValue(BaseSpec, R)
358 .castAs<loc::MemRegionVal>()
359 .getRegionAs<TypedValueRegion>();
361 // If the head of the list is also a BaseClass, we'll overwrite it to avoid
362 // note messages like 'this->A::B::x'.
363 if (!LocalChain.isEmpty() && LocalChain.getHead().isBase()) {
364 if (isNonUnionUninit(BaseRegion, LocalChain.replaceHead(
365 BaseClass(BaseSpec.getType()))))
366 ContainsUninitField = true;
368 if (isNonUnionUninit(BaseRegion,
369 LocalChain.add(BaseClass(BaseSpec.getType()))))
370 ContainsUninitField = true;
374 return ContainsUninitField;
377 bool FindUninitializedFields::isUnionUninit(const TypedValueRegion *R) {
378 assert(R->getValueType()->isUnionType() &&
379 "This method only checks union objects!");
380 // TODO: Implement support for union fields.
384 bool FindUninitializedFields::isPrimitiveUninit(const SVal &V) {
388 IsAnyFieldInitialized = true;
392 //===----------------------------------------------------------------------===//
393 // Methods for FieldChainInfo.
394 //===----------------------------------------------------------------------===//
396 bool FieldChainInfo::contains(const FieldRegion *FR) const {
397 for (const FieldNode &Node : Chain) {
398 if (Node.isSameRegion(FR))
404 /// Prints every element except the last to `Out`. Since ImmutableLists store
405 /// elements in reverse order, and have no reverse iterators, we use a
406 /// recursive function to print the fieldchain correctly. The last element in
407 /// the chain is to be printed by `FieldChainInfo::print`.
408 static void printTail(llvm::raw_ostream &Out,
409 const FieldChainInfo::FieldChain L);
411 // FIXME: This function constructs an incorrect string in the following case:
413 // struct Base { int x; };
414 // struct D1 : Base {}; struct D2 : Base {};
416 // struct MostDerived : D1, D2 {
420 // A call to MostDerived::MostDerived() will cause two notes that say
421 // "uninitialized field 'this->x'", but we can't refer to 'x' directly,
422 // we need an explicit namespace resolution whether the uninit field was
423 // 'D1::x' or 'D2::x'.
424 void FieldChainInfo::printNoteMsg(llvm::raw_ostream &Out) const {
428 const FieldNode &LastField = getHead();
430 LastField.printNoteMsg(Out);
433 for (const FieldNode &Node : Chain)
434 Node.printPrefix(Out);
437 printTail(Out, Chain.getTail());
438 LastField.printNode(Out);
442 static void printTail(llvm::raw_ostream &Out,
443 const FieldChainInfo::FieldChain L) {
447 printTail(Out, L.getTail());
449 L.getHead().printNode(Out);
450 L.getHead().printSeparator(Out);
453 //===----------------------------------------------------------------------===//
454 // Utility functions.
455 //===----------------------------------------------------------------------===//
457 static const TypedValueRegion *
458 getConstructedRegion(const CXXConstructorDecl *CtorDecl,
459 CheckerContext &Context) {
462 Context.getSValBuilder().getCXXThis(CtorDecl, Context.getStackFrame());
464 SVal ObjectV = Context.getState()->getSVal(ThisLoc);
466 auto *R = ObjectV.getAsRegion()->getAs<TypedValueRegion>();
467 if (R && !R->getValueType()->getAsCXXRecordDecl())
473 static bool willObjectBeAnalyzedLater(const CXXConstructorDecl *Ctor,
474 CheckerContext &Context) {
476 const TypedValueRegion *CurrRegion = getConstructedRegion(Ctor, Context);
480 const LocationContext *LC = Context.getLocationContext();
481 while ((LC = LC->getParent())) {
483 // If \p Ctor was called by another constructor.
484 const auto *OtherCtor = dyn_cast<CXXConstructorDecl>(LC->getDecl());
488 const TypedValueRegion *OtherRegion =
489 getConstructedRegion(OtherCtor, Context);
493 // If the CurrRegion is a subregion of OtherRegion, it will be analyzed
494 // during the analysis of OtherRegion.
495 if (CurrRegion->isSubRegionOf(OtherRegion))
502 static bool shouldIgnoreRecord(const RecordDecl *RD, StringRef Pattern) {
503 llvm::Regex R(Pattern);
505 for (const FieldDecl *FD : RD->fields()) {
506 if (R.match(FD->getType().getAsString()))
508 if (R.match(FD->getName()))
515 static const Stmt *getMethodBody(const CXXMethodDecl *M) {
516 if (isa<CXXConstructorDecl>(M))
522 return M->getDefinition()->getBody();
525 static bool hasUnguardedAccess(const FieldDecl *FD, ProgramStateRef State) {
527 if (FD->getAccess() == AccessSpecifier::AS_public)
530 const auto *Parent = dyn_cast<CXXRecordDecl>(FD->getParent());
535 Parent = Parent->getDefinition();
536 assert(Parent && "The record's definition must be avaible if an uninitialized"
537 " field of it was found!");
539 ASTContext &AC = State->getStateManager().getContext();
541 auto FieldAccessM = memberExpr(hasDeclaration(equalsNode(FD))).bind("access");
543 auto AssertLikeM = callExpr(callee(functionDecl(
544 anyOf(hasName("exit"), hasName("panic"), hasName("error"),
545 hasName("Assert"), hasName("assert"), hasName("ziperr"),
546 hasName("assfail"), hasName("db_error"), hasName("__assert"),
547 hasName("__assert2"), hasName("_wassert"), hasName("__assert_rtn"),
548 hasName("__assert_fail"), hasName("dtrace_assfail"),
549 hasName("yy_fatal_error"), hasName("_XCAssertionFailureHandler"),
550 hasName("_DTAssertionFailureHandler"),
551 hasName("_TSAssertionFailureHandler")))));
553 auto NoReturnFuncM = callExpr(callee(functionDecl(isNoReturn())));
556 stmt(anyOf(ifStmt(), switchStmt(), conditionalOperator(), AssertLikeM,
560 for (const CXXMethodDecl *M : Parent->methods()) {
561 const Stmt *MethodBody = getMethodBody(M);
565 auto Accesses = match(stmt(hasDescendant(FieldAccessM)), *MethodBody, AC);
566 if (Accesses.empty())
568 const auto *FirstAccess = Accesses[0].getNodeAs<MemberExpr>("access");
571 auto Guards = match(stmt(hasDescendant(GuardM)), *MethodBody, AC);
574 const auto *FirstGuard = Guards[0].getNodeAs<Stmt>("guard");
577 if (FirstAccess->getBeginLoc() < FirstGuard->getBeginLoc())
584 std::string clang::ento::getVariableName(const FieldDecl *Field) {
585 // If Field is a captured lambda variable, Field->getName() will return with
586 // an empty string. We can however acquire it's name from the lambda's
588 const auto *CXXParent = dyn_cast<CXXRecordDecl>(Field->getParent());
590 if (CXXParent && CXXParent->isLambda()) {
591 assert(CXXParent->captures_begin());
592 auto It = CXXParent->captures_begin() + Field->getFieldIndex();
594 if (It->capturesVariable())
595 return llvm::Twine("/*captured variable*/" +
596 It->getCapturedVar()->getName())
599 if (It->capturesThis())
600 return "/*'this' capture*/";
602 llvm_unreachable("No other capture type is expected!");
605 return Field->getName();
608 void ento::registerUninitializedObjectChecker(CheckerManager &Mgr) {
609 auto Chk = Mgr.registerChecker<UninitializedObjectChecker>();
611 AnalyzerOptions &AnOpts = Mgr.getAnalyzerOptions();
612 UninitObjCheckerOptions &ChOpts = Chk->Opts;
614 ChOpts.IsPedantic = AnOpts.getCheckerBooleanOption(Chk, "Pedantic");
615 ChOpts.ShouldConvertNotesToWarnings = AnOpts.getCheckerBooleanOption(
616 Chk, "NotesAsWarnings");
617 ChOpts.CheckPointeeInitialization = AnOpts.getCheckerBooleanOption(
618 Chk, "CheckPointeeInitialization");
619 ChOpts.IgnoredRecordsWithFieldPattern =
620 AnOpts.getCheckerStringOption(Chk, "IgnoreRecordsWithField");
621 ChOpts.IgnoreGuardedFields =
622 AnOpts.getCheckerBooleanOption(Chk, "IgnoreGuardedFields");
624 std::string ErrorMsg;
625 if (!llvm::Regex(ChOpts.IgnoredRecordsWithFieldPattern).isValid(ErrorMsg))
626 Mgr.reportInvalidCheckerOptionValue(Chk, "IgnoreRecordsWithField",
627 "a valid regex, building failed with error message "
628 "\"" + ErrorMsg + "\"");
631 bool ento::shouldRegisterUninitializedObjectChecker(const LangOptions &LO) {