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