1 //===----- 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 26 namespace clang { 27 namespace ento { 28 29 /// Represent a single field. This is only an interface to abstract away special 30 /// cases like pointers/references. 31 class FieldNode { 32 protected: 33 const FieldRegion *FR; 34 35 /* non-virtual */ ~FieldNode() = default; 36 37 public: 38 FieldNode(const FieldRegion *FR) : FR(FR) {} 39 40 FieldNode() = delete; 41 FieldNode(const FieldNode &) = delete; 42 FieldNode(FieldNode &&) = delete; 43 FieldNode &operator=(const FieldNode &) = delete; 44 FieldNode &operator=(const FieldNode &&) = delete; 45 46 /// Profile - Used to profile the contents of this object for inclusion in a 47 /// FoldingSet. 48 void Profile(llvm::FoldingSetNodeID &ID) const { ID.AddPointer(this); } 49 50 // Helper method for uniqueing. 51 bool isSameRegion(const FieldRegion *OtherFR) const { 52 // Special FieldNode descendants may wrap nullpointers -- we wouldn't like 53 // to unique these objects. 54 if (FR == nullptr) 55 return false; 56 57 return FR == OtherFR; 58 } 59 60 const FieldRegion *getRegion() const { return FR; } 61 const FieldDecl *getDecl() const { 62 assert(FR); 63 return FR->getDecl(); 64 } 65 66 // When a fieldchain is printed (a list of FieldNode objects), it will have 67 // the following format: 68 // <note message>'<prefix>this-><node><separator><node><separator>...<node>' 69 70 /// If this is the last element of the fieldchain, this method will be called. 71 /// The note message should state something like "uninitialized field" or 72 /// "uninitialized pointee" etc. 73 virtual void printNoteMsg(llvm::raw_ostream &Out) const = 0; 74 75 /// Print any prefixes before the fieldchain. 76 virtual void printPrefix(llvm::raw_ostream &Out) const = 0; 77 78 /// Print the node. Should contain the name of the field stored in getRegion. 79 virtual void printNode(llvm::raw_ostream &Out) const = 0; 80 81 /// Print the separator. For example, fields may be separated with '.' or 82 /// "->". 83 virtual void printSeparator(llvm::raw_ostream &Out) const = 0; 84 85 virtual bool isBase() const { return false; } 86 }; 87 88 /// Returns with Field's name. This is a helper function to get the correct name 89 /// even if Field is a captured lambda variable. 90 StringRef getVariableName(const FieldDecl *Field); 91 92 /// Represents a field chain. A field chain is a vector of fields where the 93 /// first element of the chain is the object under checking (not stored), and 94 /// every other element is a field, and the element that precedes it is the 95 /// object that contains it. 96 /// 97 /// Note that this class is immutable (essentially a wrapper around an 98 /// ImmutableList), and new elements can only be added by creating new 99 /// FieldChainInfo objects through add(). 100 class FieldChainInfo { 101 public: 102 using FieldChainImpl = llvm::ImmutableListImpl<const FieldNode &>; 103 using FieldChain = llvm::ImmutableList<const FieldNode &>; 104 105 private: 106 FieldChain::Factory &ChainFactory; 107 FieldChain Chain; 108 109 FieldChainInfo(FieldChain::Factory &F, FieldChain NewChain) 110 : FieldChainInfo(F) { 111 Chain = NewChain; 112 } 113 114 public: 115 FieldChainInfo() = delete; 116 FieldChainInfo(FieldChain::Factory &F) : ChainFactory(F) {} 117 FieldChainInfo(const FieldChainInfo &Other) = default; 118 119 template <class FieldNodeT> FieldChainInfo add(const FieldNodeT &FN); 120 template <class FieldNodeT> FieldChainInfo replaceHead(const FieldNodeT &FN); 121 122 bool contains(const FieldRegion *FR) const; 123 bool isEmpty() const { return Chain.isEmpty(); } 124 125 const FieldRegion *getUninitRegion() const; 126 const FieldNode &getHead() { return Chain.getHead(); } 127 void printNoteMsg(llvm::raw_ostream &Out) const; 128 }; 129 130 using UninitFieldMap = std::map<const FieldRegion *, llvm::SmallString<50>>; 131 132 /// Searches for and stores uninitialized fields in a non-union object. 133 class FindUninitializedFields { 134 ProgramStateRef State; 135 const TypedValueRegion *const ObjectR; 136 137 const bool CheckPointeeInitialization; 138 bool IsAnyFieldInitialized = false; 139 140 FieldChainInfo::FieldChain::Factory ChainFactory; 141 142 /// A map for assigning uninitialized regions to note messages. For example, 143 /// 144 /// struct A { 145 /// int x; 146 /// }; 147 /// 148 /// A a; 149 /// 150 /// After analyzing `a`, the map will contain a pair for `a.x`'s region and 151 /// the note message "uninitialized field 'this->x'. 152 UninitFieldMap UninitFields; 153 154 public: 155 /// Constructs the FindUninitializedField object, searches for and stores 156 /// uninitialized fields in R. 157 FindUninitializedFields(ProgramStateRef State, 158 const TypedValueRegion *const R, 159 bool CheckPointeeInitialization); 160 161 const UninitFieldMap &getUninitFields() { return UninitFields; } 162 163 /// Returns whether the analyzed region contains at least one initialized 164 /// field. 165 bool isAnyFieldInitialized() { return IsAnyFieldInitialized; } 166 167 private: 168 // For the purposes of this checker, we'll regard the object under checking as 169 // a directed tree, where 170 // * the root is the object under checking 171 // * every node is an object that is 172 // - a union 173 // - a non-union record 174 // - a pointer/reference 175 // - an array 176 // - of a primitive type, which we'll define later in a helper function. 177 // * the parent of each node is the object that contains it 178 // * every leaf is an array, a primitive object, a nullptr or an undefined 179 // pointer. 180 // 181 // Example: 182 // 183 // struct A { 184 // struct B { 185 // int x, y = 0; 186 // }; 187 // B b; 188 // int *iptr = new int; 189 // B* bptr; 190 // 191 // A() {} 192 // }; 193 // 194 // The directed tree: 195 // 196 // ->x 197 // / 198 // ->b--->y 199 // / 200 // A-->iptr->(int value) 201 // \ 202 // ->bptr 203 // 204 // From this we'll construct a vector of fieldchains, where each fieldchain 205 // represents an uninitialized field. An uninitialized field may be a 206 // primitive object, a pointer, a pointee or a union without a single 207 // initialized field. 208 // In the above example, for the default constructor call we'll end up with 209 // these fieldchains: 210 // 211 // this->b.x 212 // this->iptr (pointee uninit) 213 // this->bptr (pointer uninit) 214 // 215 // We'll traverse each node of the above graph with the appropiate one of 216 // these methods: 217 218 /// This method checks a region of a union object, and returns true if no 219 /// field is initialized within the region. 220 bool isUnionUninit(const TypedValueRegion *R); 221 222 /// This method checks a region of a non-union object, and returns true if 223 /// an uninitialized field is found within the region. 224 bool isNonUnionUninit(const TypedValueRegion *R, FieldChainInfo LocalChain); 225 226 /// This method checks a region of a pointer or reference object, and returns 227 /// true if the ptr/ref object itself or any field within the pointee's region 228 /// is uninitialized. 229 bool isPointerOrReferenceUninit(const FieldRegion *FR, 230 FieldChainInfo LocalChain); 231 232 /// This method returns true if the value of a primitive object is 233 /// uninitialized. 234 bool isPrimitiveUninit(const SVal &V); 235 236 // Note that we don't have a method for arrays -- the elements of an array are 237 // often left uninitialized intentionally even when it is of a C++ record 238 // type, so we'll assume that an array is always initialized. 239 // TODO: Add a support for nonloc::LocAsInteger. 240 241 /// Processes LocalChain and attempts to insert it into UninitFields. Returns 242 /// true on success. 243 /// 244 /// Since this class analyzes regions with recursion, we'll only store 245 /// references to temporary FieldNode objects created on the stack. This means 246 /// that after analyzing a leaf of the directed tree described above, the 247 /// elements LocalChain references will be destructed, so we can't store it 248 /// directly. 249 bool addFieldToUninits(FieldChainInfo LocalChain); 250 }; 251 252 /// Returns true if T is a primitive type. We defined this type so that for 253 /// objects that we'd only like analyze as much as checking whether their 254 /// value is undefined or not, such as ints and doubles, can be analyzed with 255 /// ease. This also helps ensuring that every special field type is handled 256 /// correctly. 257 inline bool isPrimitiveType(const QualType &T) { 258 return T->isBuiltinType() || T->isEnumeralType() || T->isMemberPointerType(); 259 } 260 261 // Template method definitions. 262 263 template <class FieldNodeT> 264 inline FieldChainInfo FieldChainInfo::add(const FieldNodeT &FN) { 265 assert(!contains(FN.getRegion()) && 266 "Can't add a field that is already a part of the " 267 "fieldchain! Is this a cyclic reference?"); 268 269 FieldChainInfo NewChain = *this; 270 NewChain.Chain = ChainFactory.add(FN, Chain); 271 return NewChain; 272 } 273 274 template <class FieldNodeT> 275 inline FieldChainInfo FieldChainInfo::replaceHead(const FieldNodeT &FN) { 276 FieldChainInfo NewChain(ChainFactory, Chain.getTail()); 277 return NewChain.add(FN); 278 } 279 280 } // end of namespace ento 281 } // end of namespace clang 282 283 #endif // LLVM_CLANG_STATICANALYZER_UNINITIALIZEDOBJECT_H 284