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