1*e5dd7070Spatrick //===----- UninitializedObject.h ---------------------------------*- C++ -*-==//
2*e5dd7070Spatrick //
3*e5dd7070Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*e5dd7070Spatrick // See https://llvm.org/LICENSE.txt for license information.
5*e5dd7070Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*e5dd7070Spatrick //
7*e5dd7070Spatrick //===----------------------------------------------------------------------===//
8*e5dd7070Spatrick //
9*e5dd7070Spatrick // This file defines helper classes for UninitializedObjectChecker and
10*e5dd7070Spatrick // documentation about the logic of it.
11*e5dd7070Spatrick //
12*e5dd7070Spatrick // The checker reports uninitialized fields in objects created after a
13*e5dd7070Spatrick // constructor call.
14*e5dd7070Spatrick //
15*e5dd7070Spatrick // This checker has several options:
16*e5dd7070Spatrick // - "Pedantic" (boolean). If its not set or is set to false, the checker
17*e5dd7070Spatrick // won't emit warnings for objects that don't have at least one initialized
18*e5dd7070Spatrick // field. This may be set with
19*e5dd7070Spatrick //
20*e5dd7070Spatrick // `-analyzer-config optin.cplusplus.UninitializedObject:Pedantic=true`.
21*e5dd7070Spatrick //
22*e5dd7070Spatrick // - "NotesAsWarnings" (boolean). If set to true, the checker will emit a
23*e5dd7070Spatrick // warning for each uninitialized field, as opposed to emitting one warning
24*e5dd7070Spatrick // per constructor call, and listing the uninitialized fields that belongs
25*e5dd7070Spatrick // to it in notes. Defaults to false.
26*e5dd7070Spatrick //
27*e5dd7070Spatrick // `-analyzer-config \
28*e5dd7070Spatrick // optin.cplusplus.UninitializedObject:NotesAsWarnings=true`.
29*e5dd7070Spatrick //
30*e5dd7070Spatrick // - "CheckPointeeInitialization" (boolean). If set to false, the checker will
31*e5dd7070Spatrick // not analyze the pointee of pointer/reference fields, and will only check
32*e5dd7070Spatrick // whether the object itself is initialized. Defaults to false.
33*e5dd7070Spatrick //
34*e5dd7070Spatrick // `-analyzer-config \
35*e5dd7070Spatrick // optin.cplusplus.UninitializedObject:CheckPointeeInitialization=true`.
36*e5dd7070Spatrick //
37*e5dd7070Spatrick // TODO: With some clever heuristics, some pointers should be dereferenced
38*e5dd7070Spatrick // by default. For example, if the pointee is constructed within the
39*e5dd7070Spatrick // constructor call, it's reasonable to say that no external object
40*e5dd7070Spatrick // references it, and we wouldn't generate multiple report on the same
41*e5dd7070Spatrick // pointee.
42*e5dd7070Spatrick //
43*e5dd7070Spatrick // - "IgnoreRecordsWithField" (string). If supplied, the checker will not
44*e5dd7070Spatrick // analyze structures that have a field with a name or type name that
45*e5dd7070Spatrick // matches the given pattern. Defaults to "".
46*e5dd7070Spatrick //
47*e5dd7070Spatrick // `-analyzer-config \
48*e5dd7070Spatrick // optin.cplusplus.UninitializedObject:IgnoreRecordsWithField="[Tt]ag|[Kk]ind"`.
49*e5dd7070Spatrick //
50*e5dd7070Spatrick // - "IgnoreGuardedFields" (boolean). If set to true, the checker will analyze
51*e5dd7070Spatrick // _syntactically_ whether the found uninitialized object is used without a
52*e5dd7070Spatrick // preceding assert call. Defaults to false.
53*e5dd7070Spatrick //
54*e5dd7070Spatrick // `-analyzer-config \
55*e5dd7070Spatrick // optin.cplusplus.UninitializedObject:IgnoreGuardedFields=true`.
56*e5dd7070Spatrick //
57*e5dd7070Spatrick // Most of the following methods as well as the checker itself is defined in
58*e5dd7070Spatrick // UninitializedObjectChecker.cpp.
59*e5dd7070Spatrick //
60*e5dd7070Spatrick // Some methods are implemented in UninitializedPointee.cpp, to reduce the
61*e5dd7070Spatrick // complexity of the main checker file.
62*e5dd7070Spatrick //
63*e5dd7070Spatrick //===----------------------------------------------------------------------===//
64*e5dd7070Spatrick
65*e5dd7070Spatrick #ifndef LLVM_CLANG_STATICANALYZER_UNINITIALIZEDOBJECT_H
66*e5dd7070Spatrick #define LLVM_CLANG_STATICANALYZER_UNINITIALIZEDOBJECT_H
67*e5dd7070Spatrick
68*e5dd7070Spatrick #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
69*e5dd7070Spatrick
70*e5dd7070Spatrick namespace clang {
71*e5dd7070Spatrick namespace ento {
72*e5dd7070Spatrick
73*e5dd7070Spatrick struct UninitObjCheckerOptions {
74*e5dd7070Spatrick bool IsPedantic = false;
75*e5dd7070Spatrick bool ShouldConvertNotesToWarnings = false;
76*e5dd7070Spatrick bool CheckPointeeInitialization = false;
77*e5dd7070Spatrick std::string IgnoredRecordsWithFieldPattern;
78*e5dd7070Spatrick bool IgnoreGuardedFields = false;
79*e5dd7070Spatrick };
80*e5dd7070Spatrick
81*e5dd7070Spatrick /// A lightweight polymorphic wrapper around FieldRegion *. We'll use this
82*e5dd7070Spatrick /// interface to store addinitional information about fields. As described
83*e5dd7070Spatrick /// later, a list of these objects (i.e. "fieldchain") will be constructed and
84*e5dd7070Spatrick /// used for printing note messages should an uninitialized value be found.
85*e5dd7070Spatrick class FieldNode {
86*e5dd7070Spatrick protected:
87*e5dd7070Spatrick const FieldRegion *FR;
88*e5dd7070Spatrick
89*e5dd7070Spatrick /// FieldNodes are never meant to be created on the heap, see
90*e5dd7070Spatrick /// FindUninitializedFields::addFieldToUninits().
91*e5dd7070Spatrick /* non-virtual */ ~FieldNode() = default;
92*e5dd7070Spatrick
93*e5dd7070Spatrick public:
FieldNode(const FieldRegion * FR)94*e5dd7070Spatrick FieldNode(const FieldRegion *FR) : FR(FR) {}
95*e5dd7070Spatrick
96*e5dd7070Spatrick // We'll delete all of these special member functions to force the users of
97*e5dd7070Spatrick // this interface to only store references to FieldNode objects in containers.
98*e5dd7070Spatrick FieldNode() = delete;
99*e5dd7070Spatrick FieldNode(const FieldNode &) = delete;
100*e5dd7070Spatrick FieldNode(FieldNode &&) = delete;
101*e5dd7070Spatrick FieldNode &operator=(const FieldNode &) = delete;
102*e5dd7070Spatrick FieldNode &operator=(const FieldNode &&) = delete;
103*e5dd7070Spatrick
Profile(llvm::FoldingSetNodeID & ID)104*e5dd7070Spatrick void Profile(llvm::FoldingSetNodeID &ID) const { ID.AddPointer(this); }
105*e5dd7070Spatrick
106*e5dd7070Spatrick /// Helper method for uniqueing.
isSameRegion(const FieldRegion * OtherFR)107*e5dd7070Spatrick bool isSameRegion(const FieldRegion *OtherFR) const {
108*e5dd7070Spatrick // Special FieldNode descendants may wrap nullpointers (for example if they
109*e5dd7070Spatrick // describe a special relationship between two elements of the fieldchain)
110*e5dd7070Spatrick // -- we wouldn't like to unique these objects.
111*e5dd7070Spatrick if (FR == nullptr)
112*e5dd7070Spatrick return false;
113*e5dd7070Spatrick
114*e5dd7070Spatrick return FR == OtherFR;
115*e5dd7070Spatrick }
116*e5dd7070Spatrick
getRegion()117*e5dd7070Spatrick const FieldRegion *getRegion() const { return FR; }
getDecl()118*e5dd7070Spatrick const FieldDecl *getDecl() const {
119*e5dd7070Spatrick assert(FR);
120*e5dd7070Spatrick return FR->getDecl();
121*e5dd7070Spatrick }
122*e5dd7070Spatrick
123*e5dd7070Spatrick // When a fieldchain is printed, it will have the following format (without
124*e5dd7070Spatrick // newline, indices are in order of insertion, from 1 to n):
125*e5dd7070Spatrick //
126*e5dd7070Spatrick // <note_message_n>'<prefix_n><prefix_n-1>...<prefix_1>
127*e5dd7070Spatrick // this-><node_1><separator_1><node_2><separator_2>...<node_n>'
128*e5dd7070Spatrick
129*e5dd7070Spatrick /// If this is the last element of the fieldchain, this method will print the
130*e5dd7070Spatrick /// note message associated with it.
131*e5dd7070Spatrick /// The note message should state something like "uninitialized field" or
132*e5dd7070Spatrick /// "uninitialized pointee" etc.
133*e5dd7070Spatrick virtual void printNoteMsg(llvm::raw_ostream &Out) const = 0;
134*e5dd7070Spatrick
135*e5dd7070Spatrick /// Print any prefixes before the fieldchain. Could contain casts, etc.
136*e5dd7070Spatrick virtual void printPrefix(llvm::raw_ostream &Out) const = 0;
137*e5dd7070Spatrick
138*e5dd7070Spatrick /// Print the node. Should contain the name of the field stored in FR.
139*e5dd7070Spatrick virtual void printNode(llvm::raw_ostream &Out) const = 0;
140*e5dd7070Spatrick
141*e5dd7070Spatrick /// Print the separator. For example, fields may be separated with '.' or
142*e5dd7070Spatrick /// "->".
143*e5dd7070Spatrick virtual void printSeparator(llvm::raw_ostream &Out) const = 0;
144*e5dd7070Spatrick
isBase()145*e5dd7070Spatrick virtual bool isBase() const { return false; }
146*e5dd7070Spatrick };
147*e5dd7070Spatrick
148*e5dd7070Spatrick /// Returns with Field's name. This is a helper function to get the correct name
149*e5dd7070Spatrick /// even if Field is a captured lambda variable.
150*e5dd7070Spatrick std::string getVariableName(const FieldDecl *Field);
151*e5dd7070Spatrick
152*e5dd7070Spatrick /// Represents a field chain. A field chain is a list of fields where the first
153*e5dd7070Spatrick /// element of the chain is the object under checking (not stored), and every
154*e5dd7070Spatrick /// other element is a field, and the element that precedes it is the object
155*e5dd7070Spatrick /// that contains it.
156*e5dd7070Spatrick ///
157*e5dd7070Spatrick /// Note that this class is immutable (essentially a wrapper around an
158*e5dd7070Spatrick /// ImmutableList), new FieldChainInfo objects may be created by member
159*e5dd7070Spatrick /// functions such as add() and replaceHead().
160*e5dd7070Spatrick class FieldChainInfo {
161*e5dd7070Spatrick public:
162*e5dd7070Spatrick using FieldChain = llvm::ImmutableList<const FieldNode &>;
163*e5dd7070Spatrick
164*e5dd7070Spatrick private:
165*e5dd7070Spatrick FieldChain::Factory &ChainFactory;
166*e5dd7070Spatrick FieldChain Chain;
167*e5dd7070Spatrick
FieldChainInfo(FieldChain::Factory & F,FieldChain NewChain)168*e5dd7070Spatrick FieldChainInfo(FieldChain::Factory &F, FieldChain NewChain)
169*e5dd7070Spatrick : FieldChainInfo(F) {
170*e5dd7070Spatrick Chain = NewChain;
171*e5dd7070Spatrick }
172*e5dd7070Spatrick
173*e5dd7070Spatrick public:
174*e5dd7070Spatrick FieldChainInfo() = delete;
FieldChainInfo(FieldChain::Factory & F)175*e5dd7070Spatrick FieldChainInfo(FieldChain::Factory &F) : ChainFactory(F) {}
176*e5dd7070Spatrick FieldChainInfo(const FieldChainInfo &Other) = default;
177*e5dd7070Spatrick
178*e5dd7070Spatrick /// Constructs a new FieldChainInfo object with \p FN appended.
179*e5dd7070Spatrick template <class FieldNodeT> FieldChainInfo add(const FieldNodeT &FN);
180*e5dd7070Spatrick
181*e5dd7070Spatrick /// Constructs a new FieldChainInfo object with \p FN as the new head of the
182*e5dd7070Spatrick /// list.
183*e5dd7070Spatrick template <class FieldNodeT> FieldChainInfo replaceHead(const FieldNodeT &FN);
184*e5dd7070Spatrick
185*e5dd7070Spatrick bool contains(const FieldRegion *FR) const;
isEmpty()186*e5dd7070Spatrick bool isEmpty() const { return Chain.isEmpty(); }
187*e5dd7070Spatrick
getHead()188*e5dd7070Spatrick const FieldNode &getHead() const { return Chain.getHead(); }
getUninitRegion()189*e5dd7070Spatrick const FieldRegion *getUninitRegion() const { return getHead().getRegion(); }
190*e5dd7070Spatrick
191*e5dd7070Spatrick void printNoteMsg(llvm::raw_ostream &Out) const;
192*e5dd7070Spatrick };
193*e5dd7070Spatrick
194*e5dd7070Spatrick using UninitFieldMap = std::map<const FieldRegion *, llvm::SmallString<50>>;
195*e5dd7070Spatrick
196*e5dd7070Spatrick /// Searches for and stores uninitialized fields in a non-union object.
197*e5dd7070Spatrick class FindUninitializedFields {
198*e5dd7070Spatrick ProgramStateRef State;
199*e5dd7070Spatrick const TypedValueRegion *const ObjectR;
200*e5dd7070Spatrick
201*e5dd7070Spatrick const UninitObjCheckerOptions Opts;
202*e5dd7070Spatrick bool IsAnyFieldInitialized = false;
203*e5dd7070Spatrick
204*e5dd7070Spatrick FieldChainInfo::FieldChain::Factory ChainFactory;
205*e5dd7070Spatrick
206*e5dd7070Spatrick /// A map for assigning uninitialized regions to note messages. For example,
207*e5dd7070Spatrick ///
208*e5dd7070Spatrick /// struct A {
209*e5dd7070Spatrick /// int x;
210*e5dd7070Spatrick /// };
211*e5dd7070Spatrick ///
212*e5dd7070Spatrick /// A a;
213*e5dd7070Spatrick ///
214*e5dd7070Spatrick /// After analyzing `a`, the map will contain a pair for `a.x`'s region and
215*e5dd7070Spatrick /// the note message "uninitialized field 'this->x'.
216*e5dd7070Spatrick UninitFieldMap UninitFields;
217*e5dd7070Spatrick
218*e5dd7070Spatrick public:
219*e5dd7070Spatrick /// Constructs the FindUninitializedField object, searches for and stores
220*e5dd7070Spatrick /// uninitialized fields in R.
221*e5dd7070Spatrick FindUninitializedFields(ProgramStateRef State,
222*e5dd7070Spatrick const TypedValueRegion *const R,
223*e5dd7070Spatrick const UninitObjCheckerOptions &Opts);
224*e5dd7070Spatrick
225*e5dd7070Spatrick /// Returns with the modified state and a map of (uninitialized region,
226*e5dd7070Spatrick /// note message) pairs.
getResults()227*e5dd7070Spatrick std::pair<ProgramStateRef, const UninitFieldMap &> getResults() {
228*e5dd7070Spatrick return {State, UninitFields};
229*e5dd7070Spatrick }
230*e5dd7070Spatrick
231*e5dd7070Spatrick /// Returns whether the analyzed region contains at least one initialized
232*e5dd7070Spatrick /// field. Note that this includes subfields as well, not just direct ones,
233*e5dd7070Spatrick /// and will return false if an uninitialized pointee is found with
234*e5dd7070Spatrick /// CheckPointeeInitialization enabled.
isAnyFieldInitialized()235*e5dd7070Spatrick bool isAnyFieldInitialized() { return IsAnyFieldInitialized; }
236*e5dd7070Spatrick
237*e5dd7070Spatrick private:
238*e5dd7070Spatrick // For the purposes of this checker, we'll regard the analyzed region as a
239*e5dd7070Spatrick // directed tree, where
240*e5dd7070Spatrick // * the root is the object under checking
241*e5dd7070Spatrick // * every node is an object that is
242*e5dd7070Spatrick // - a union
243*e5dd7070Spatrick // - a non-union record
244*e5dd7070Spatrick // - dereferenceable (see isDereferencableType())
245*e5dd7070Spatrick // - an array
246*e5dd7070Spatrick // - of a primitive type (see isPrimitiveType())
247*e5dd7070Spatrick // * the parent of each node is the object that contains it
248*e5dd7070Spatrick // * every leaf is an array, a primitive object, a nullptr or an undefined
249*e5dd7070Spatrick // pointer.
250*e5dd7070Spatrick //
251*e5dd7070Spatrick // Example:
252*e5dd7070Spatrick //
253*e5dd7070Spatrick // struct A {
254*e5dd7070Spatrick // struct B {
255*e5dd7070Spatrick // int x, y = 0;
256*e5dd7070Spatrick // };
257*e5dd7070Spatrick // B b;
258*e5dd7070Spatrick // int *iptr = new int;
259*e5dd7070Spatrick // B* bptr;
260*e5dd7070Spatrick //
261*e5dd7070Spatrick // A() {}
262*e5dd7070Spatrick // };
263*e5dd7070Spatrick //
264*e5dd7070Spatrick // The directed tree:
265*e5dd7070Spatrick //
266*e5dd7070Spatrick // ->x
267*e5dd7070Spatrick // /
268*e5dd7070Spatrick // ->b--->y
269*e5dd7070Spatrick // /
270*e5dd7070Spatrick // A-->iptr->(int value)
271*e5dd7070Spatrick // \
272*e5dd7070Spatrick // ->bptr
273*e5dd7070Spatrick //
274*e5dd7070Spatrick // From this we'll construct a vector of fieldchains, where each fieldchain
275*e5dd7070Spatrick // represents an uninitialized field. An uninitialized field may be a
276*e5dd7070Spatrick // primitive object, a pointer, a pointee or a union without a single
277*e5dd7070Spatrick // initialized field.
278*e5dd7070Spatrick // In the above example, for the default constructor call we'll end up with
279*e5dd7070Spatrick // these fieldchains:
280*e5dd7070Spatrick //
281*e5dd7070Spatrick // this->b.x
282*e5dd7070Spatrick // this->iptr (pointee uninit)
283*e5dd7070Spatrick // this->bptr (pointer uninit)
284*e5dd7070Spatrick //
285*e5dd7070Spatrick // We'll traverse each node of the above graph with the appropriate one of
286*e5dd7070Spatrick // these methods:
287*e5dd7070Spatrick
288*e5dd7070Spatrick /// Checks the region of a union object, and returns true if no field is
289*e5dd7070Spatrick /// initialized within the region.
290*e5dd7070Spatrick bool isUnionUninit(const TypedValueRegion *R);
291*e5dd7070Spatrick
292*e5dd7070Spatrick /// Checks a region of a non-union object, and returns true if an
293*e5dd7070Spatrick /// uninitialized field is found within the region.
294*e5dd7070Spatrick bool isNonUnionUninit(const TypedValueRegion *R, FieldChainInfo LocalChain);
295*e5dd7070Spatrick
296*e5dd7070Spatrick /// Checks a region of a pointer or reference object, and returns true if the
297*e5dd7070Spatrick /// ptr/ref object itself or any field within the pointee's region is
298*e5dd7070Spatrick /// uninitialized.
299*e5dd7070Spatrick bool isDereferencableUninit(const FieldRegion *FR, FieldChainInfo LocalChain);
300*e5dd7070Spatrick
301*e5dd7070Spatrick /// Returns true if the value of a primitive object is uninitialized.
302*e5dd7070Spatrick bool isPrimitiveUninit(const SVal &V);
303*e5dd7070Spatrick
304*e5dd7070Spatrick // Note that we don't have a method for arrays -- the elements of an array are
305*e5dd7070Spatrick // often left uninitialized intentionally even when it is of a C++ record
306*e5dd7070Spatrick // type, so we'll assume that an array is always initialized.
307*e5dd7070Spatrick // TODO: Add a support for nonloc::LocAsInteger.
308*e5dd7070Spatrick
309*e5dd7070Spatrick /// Processes LocalChain and attempts to insert it into UninitFields. Returns
310*e5dd7070Spatrick /// true on success. Also adds the head of the list and \p PointeeR (if
311*e5dd7070Spatrick /// supplied) to the GDM as already analyzed objects.
312*e5dd7070Spatrick ///
313*e5dd7070Spatrick /// Since this class analyzes regions with recursion, we'll only store
314*e5dd7070Spatrick /// references to temporary FieldNode objects created on the stack. This means
315*e5dd7070Spatrick /// that after analyzing a leaf of the directed tree described above, the
316*e5dd7070Spatrick /// elements LocalChain references will be destructed, so we can't store it
317*e5dd7070Spatrick /// directly.
318*e5dd7070Spatrick bool addFieldToUninits(FieldChainInfo LocalChain,
319*e5dd7070Spatrick const MemRegion *PointeeR = nullptr);
320*e5dd7070Spatrick };
321*e5dd7070Spatrick
322*e5dd7070Spatrick /// Returns true if T is a primitive type. An object of a primitive type only
323*e5dd7070Spatrick /// needs to be analyzed as much as checking whether their value is undefined.
isPrimitiveType(const QualType & T)324*e5dd7070Spatrick inline bool isPrimitiveType(const QualType &T) {
325*e5dd7070Spatrick return T->isBuiltinType() || T->isEnumeralType() ||
326*e5dd7070Spatrick T->isFunctionType() || T->isAtomicType() ||
327*e5dd7070Spatrick T->isVectorType() || T->isScalarType();
328*e5dd7070Spatrick }
329*e5dd7070Spatrick
isDereferencableType(const QualType & T)330*e5dd7070Spatrick inline bool isDereferencableType(const QualType &T) {
331*e5dd7070Spatrick return T->isAnyPointerType() || T->isReferenceType();
332*e5dd7070Spatrick }
333*e5dd7070Spatrick
334*e5dd7070Spatrick // Template method definitions.
335*e5dd7070Spatrick
336*e5dd7070Spatrick template <class FieldNodeT>
add(const FieldNodeT & FN)337*e5dd7070Spatrick inline FieldChainInfo FieldChainInfo::add(const FieldNodeT &FN) {
338*e5dd7070Spatrick assert(!contains(FN.getRegion()) &&
339*e5dd7070Spatrick "Can't add a field that is already a part of the "
340*e5dd7070Spatrick "fieldchain! Is this a cyclic reference?");
341*e5dd7070Spatrick
342*e5dd7070Spatrick FieldChainInfo NewChain = *this;
343*e5dd7070Spatrick NewChain.Chain = ChainFactory.add(FN, Chain);
344*e5dd7070Spatrick return NewChain;
345*e5dd7070Spatrick }
346*e5dd7070Spatrick
347*e5dd7070Spatrick template <class FieldNodeT>
replaceHead(const FieldNodeT & FN)348*e5dd7070Spatrick inline FieldChainInfo FieldChainInfo::replaceHead(const FieldNodeT &FN) {
349*e5dd7070Spatrick FieldChainInfo NewChain(ChainFactory, Chain.getTail());
350*e5dd7070Spatrick return NewChain.add(FN);
351*e5dd7070Spatrick }
352*e5dd7070Spatrick
353*e5dd7070Spatrick } // end of namespace ento
354*e5dd7070Spatrick } // end of namespace clang
355*e5dd7070Spatrick
356*e5dd7070Spatrick #endif // LLVM_CLANG_STATICANALYZER_UNINITIALIZEDOBJECT_H
357