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