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