blob: 2aaf11af9c2f9149fc2c1e69d14ca44817d88abf [file] [log] [blame]
Kristof Umann56963ae2018-08-13 18:17:05 +00001//===----- UninitializedObject.h ---------------------------------*- 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 helper classes for UninitializedObjectChecker and
11// documentation about the logic of it.
12//
13// To read about command line options and a description what this checker does,
14// refer to UninitializedObjectChecker.cpp.
15//
16// Some methods are implemented in UninitializedPointee.cpp, to reduce the
17// complexity of the main checker file.
18//
19//===----------------------------------------------------------------------===//
20
21#ifndef LLVM_CLANG_STATICANALYZER_UNINITIALIZEDOBJECT_H
22#define LLVM_CLANG_STATICANALYZER_UNINITIALIZEDOBJECT_H
23
24#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
25
26namespace clang {
27namespace ento {
28
29/// Represents a field chain. A field chain is a vector of fields where the
30/// first element of the chain is the object under checking (not stored), and
31/// every other element is a field, and the element that precedes it is the
32/// object that contains it.
33///
34/// Note that this class is immutable, and new fields may only be added through
35/// constructor calls.
36class FieldChainInfo {
37public:
38 using FieldChain = llvm::ImmutableList<const FieldRegion *>;
39
40private:
41 FieldChain::Factory &Factory;
42 FieldChain Chain;
43
44 const bool IsDereferenced = false;
45
46public:
47 FieldChainInfo() = delete;
48 FieldChainInfo(FieldChain::Factory &F) : Factory(F) {}
49
50 FieldChainInfo(const FieldChainInfo &Other, const bool IsDereferenced)
51 : Factory(Other.Factory), Chain(Other.Chain), IsDereferenced(IsDereferenced) {}
52
53 FieldChainInfo(const FieldChainInfo &Other, const FieldRegion *FR,
54 const bool IsDereferenced = false);
55
56 bool contains(const FieldRegion *FR) const { return Chain.contains(FR); }
57 bool isPointer() const;
58
59 /// If this is a fieldchain whose last element is an uninitialized region of a
60 /// pointer type, `IsDereferenced` will store whether the pointer itself or
61 /// the pointee is uninitialized.
62 bool isDereferenced() const;
63 const FieldDecl *getEndOfChain() const;
64 void print(llvm::raw_ostream &Out) const;
65
66private:
67 /// Prints every element except the last to `Out`. Since ImmutableLists store
68 /// elements in reverse order, and have no reverse iterators, we use a
69 /// recursive function to print the fieldchain correctly. The last element in
70 /// the chain is to be printed by `print`.
71 static void printTail(llvm::raw_ostream &Out,
72 const llvm::ImmutableListImpl<const FieldRegion *> *L);
73 friend struct FieldChainInfoComparator;
74};
75
76struct FieldChainInfoComparator {
77 bool operator()(const FieldChainInfo &lhs, const FieldChainInfo &rhs) const {
78 assert(!lhs.Chain.isEmpty() && !rhs.Chain.isEmpty() &&
79 "Attempted to store an empty fieldchain!");
80 return *lhs.Chain.begin() < *rhs.Chain.begin();
81 }
82};
83
84using UninitFieldSet = std::set<FieldChainInfo, FieldChainInfoComparator>;
85
86/// Searches for and stores uninitialized fields in a non-union object.
87class FindUninitializedFields {
88 ProgramStateRef State;
89 const TypedValueRegion *const ObjectR;
90
91 const bool IsPedantic;
92 const bool CheckPointeeInitialization;
93
94 bool IsAnyFieldInitialized = false;
95
96 FieldChainInfo::FieldChain::Factory Factory;
97 UninitFieldSet UninitFields;
98
99public:
100 FindUninitializedFields(ProgramStateRef State,
101 const TypedValueRegion *const R, bool IsPedantic,
102 bool CheckPointeeInitialization);
103 const UninitFieldSet &getUninitFields();
104
105private:
106 /// Adds a FieldChainInfo object to UninitFields. Return true if an insertion
107 /// took place.
108 bool addFieldToUninits(FieldChainInfo LocalChain);
109
110 // For the purposes of this checker, we'll regard the object under checking as
111 // a directed tree, where
112 // * the root is the object under checking
113 // * every node is an object that is
114 // - a union
115 // - a non-union record
116 // - a pointer/reference
117 // - an array
118 // - of a primitive type, which we'll define later in a helper function.
119 // * the parent of each node is the object that contains it
120 // * every leaf is an array, a primitive object, a nullptr or an undefined
121 // pointer.
122 //
123 // Example:
124 //
125 // struct A {
126 // struct B {
127 // int x, y = 0;
128 // };
129 // B b;
130 // int *iptr = new int;
131 // B* bptr;
132 //
133 // A() {}
134 // };
135 //
136 // The directed tree:
137 //
138 // ->x
139 // /
140 // ->b--->y
141 // /
142 // A-->iptr->(int value)
143 // \
144 // ->bptr
145 //
146 // From this we'll construct a vector of fieldchains, where each fieldchain
147 // represents an uninitialized field. An uninitialized field may be a
148 // primitive object, a pointer, a pointee or a union without a single
149 // initialized field.
150 // In the above example, for the default constructor call we'll end up with
151 // these fieldchains:
152 //
153 // this->b.x
154 // this->iptr (pointee uninit)
155 // this->bptr (pointer uninit)
156 //
157 // We'll traverse each node of the above graph with the appropiate one of
158 // these methods:
159
160 /// This method checks a region of a union object, and returns true if no
161 /// field is initialized within the region.
162 bool isUnionUninit(const TypedValueRegion *R);
163
164 /// This method checks a region of a non-union object, and returns true if
165 /// an uninitialized field is found within the region.
166 bool isNonUnionUninit(const TypedValueRegion *R, FieldChainInfo LocalChain);
167
168 /// This method checks a region of a pointer or reference object, and returns
169 /// true if the ptr/ref object itself or any field within the pointee's region
170 /// is uninitialized.
171 bool isPointerOrReferenceUninit(const FieldRegion *FR,
172 FieldChainInfo LocalChain);
173
174 /// This method returns true if the value of a primitive object is
175 /// uninitialized.
176 bool isPrimitiveUninit(const SVal &V);
177
178 // Note that we don't have a method for arrays -- the elements of an array are
179 // often left uninitialized intentionally even when it is of a C++ record
180 // type, so we'll assume that an array is always initialized.
181 // TODO: Add a support for nonloc::LocAsInteger.
182};
183
184/// Returns true if T is a primitive type. We defined this type so that for
185/// objects that we'd only like analyze as much as checking whether their
186/// value is undefined or not, such as ints and doubles, can be analyzed with
187/// ease. This also helps ensuring that every special field type is handled
188/// correctly.
189static bool isPrimitiveType(const QualType &T) {
190 return T->isBuiltinType() || T->isEnumeralType() || T->isMemberPointerType();
191}
192
193} // end of namespace ento
194} // end of namespace clang
195
196#endif // LLVM_CLANG_STATICANALYZER_UNINITIALIZEDOBJECT_H