Kristof Umann | 56963ae | 2018-08-13 18:17:05 +0000 | [diff] [blame] | 1 | //===----- UninitializedPointer.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 functions and methods for handling pointers and references |
| 11 | // to reduce the size and complexity of UninitializedObjectChecker.cpp. |
| 12 | // |
| 13 | // To read about command line options and a description what this checker does, |
| 14 | // refer to UninitializedObjectChecker.cpp. |
| 15 | // |
| 16 | // To read about how the checker works, refer to the comments in |
| 17 | // UninitializedObject.h. |
| 18 | // |
| 19 | //===----------------------------------------------------------------------===// |
| 20 | |
Richard Smith | 651d683 | 2018-08-13 22:07:11 +0000 | [diff] [blame] | 21 | #include "../ClangSACheckers.h" |
Kristof Umann | a37bba4 | 2018-08-13 18:22:22 +0000 | [diff] [blame] | 22 | #include "UninitializedObject.h" |
Kristof Umann | 56963ae | 2018-08-13 18:17:05 +0000 | [diff] [blame] | 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 | |
Kristof Umann | 015b059 | 2018-08-13 18:43:08 +0000 | [diff] [blame] | 31 | namespace { |
| 32 | |
| 33 | /// Represents a pointer or a reference field. |
Richard Smith | 651d683 | 2018-08-13 22:07:11 +0000 | [diff] [blame] | 34 | class LocField final : public FieldNode { |
Kristof Umann | 015b059 | 2018-08-13 18:43:08 +0000 | [diff] [blame] | 35 | /// We'll store whether the pointee or the pointer itself is uninitialited. |
| 36 | const bool IsDereferenced; |
| 37 | |
| 38 | public: |
| 39 | LocField(const FieldRegion *FR, const bool IsDereferenced = true) |
| 40 | : FieldNode(FR), IsDereferenced(IsDereferenced) {} |
| 41 | |
| 42 | virtual void printNoteMsg(llvm::raw_ostream &Out) const override { |
| 43 | if (IsDereferenced) |
| 44 | Out << "uninitialized pointee "; |
| 45 | else |
| 46 | Out << "uninitialized pointer "; |
| 47 | } |
| 48 | |
| 49 | virtual void printPrefix(llvm::raw_ostream &Out) const override {} |
| 50 | |
| 51 | virtual void printNode(llvm::raw_ostream &Out) const override { |
| 52 | Out << getVariableName(getDecl()); |
| 53 | } |
| 54 | |
| 55 | virtual void printSeparator(llvm::raw_ostream &Out) const override { |
| 56 | if (getDecl()->getType()->isPointerType()) |
| 57 | Out << "->"; |
| 58 | else |
| 59 | Out << '.'; |
| 60 | } |
| 61 | }; |
| 62 | |
Kristof Umann | 5a42441 | 2018-08-14 08:20:51 +0000 | [diff] [blame] | 63 | /// Represents a void* field that needs to be casted back to its dynamic type |
| 64 | /// for a correct note message. |
| 65 | class NeedsCastLocField final : public FieldNode { |
| 66 | QualType CastBackType; |
| 67 | |
| 68 | public: |
| 69 | NeedsCastLocField(const FieldRegion *FR, const QualType &T) |
| 70 | : FieldNode(FR), CastBackType(T) {} |
| 71 | |
| 72 | virtual void printNoteMsg(llvm::raw_ostream &Out) const override { |
| 73 | Out << "uninitialized pointee "; |
| 74 | } |
| 75 | |
| 76 | virtual void printPrefix(llvm::raw_ostream &Out) const override { |
| 77 | Out << "static_cast" << '<' << CastBackType.getAsString() << ">("; |
| 78 | } |
| 79 | |
| 80 | virtual void printNode(llvm::raw_ostream &Out) const override { |
| 81 | Out << getVariableName(getDecl()) << ')'; |
| 82 | } |
| 83 | |
| 84 | virtual void printSeparator(llvm::raw_ostream &Out) const override { |
| 85 | Out << "->"; |
| 86 | } |
| 87 | }; |
| 88 | |
Kristof Umann | 015b059 | 2018-08-13 18:43:08 +0000 | [diff] [blame] | 89 | } // end of anonymous namespace |
| 90 | |
Kristof Umann | 56963ae | 2018-08-13 18:17:05 +0000 | [diff] [blame] | 91 | // Utility function declarations. |
| 92 | |
| 93 | /// Returns whether T can be (transitively) dereferenced to a void pointer type |
| 94 | /// (void*, void**, ...). The type of the region behind a void pointer isn't |
| 95 | /// known, and thus FD can not be analyzed. |
| 96 | static bool isVoidPointer(QualType T); |
| 97 | |
Kristof Umann | f0dd101 | 2018-09-14 08:58:21 +0000 | [diff] [blame^] | 98 | using DereferenceInfo = std::pair<const TypedValueRegion *, bool>; |
| 99 | |
| 100 | /// Dereferences \p FR and returns with the pointee's region, and whether it |
| 101 | /// needs to be casted back to it's location type. If for whatever reason |
| 102 | /// dereferencing fails, returns with None. |
| 103 | static llvm::Optional<DereferenceInfo> dereference(ProgramStateRef State, |
| 104 | const FieldRegion *FR); |
Kristof Umann | 6460196 | 2018-08-21 10:45:21 +0000 | [diff] [blame] | 105 | |
Kristof Umann | 56963ae | 2018-08-13 18:17:05 +0000 | [diff] [blame] | 106 | //===----------------------------------------------------------------------===// |
| 107 | // Methods for FindUninitializedFields. |
| 108 | //===----------------------------------------------------------------------===// |
| 109 | |
| 110 | // Note that pointers/references don't contain fields themselves, so in this |
| 111 | // function we won't add anything to LocalChain. |
| 112 | bool FindUninitializedFields::isPointerOrReferenceUninit( |
| 113 | const FieldRegion *FR, FieldChainInfo LocalChain) { |
| 114 | |
Kristof Umann | f0dd101 | 2018-09-14 08:58:21 +0000 | [diff] [blame^] | 115 | assert(isDereferencableType(FR->getDecl()->getType()) && |
| 116 | "This method only checks dereferencable objects!"); |
Kristof Umann | 56963ae | 2018-08-13 18:17:05 +0000 | [diff] [blame] | 117 | |
| 118 | SVal V = State->getSVal(FR); |
| 119 | |
| 120 | if (V.isUnknown() || V.getAs<loc::ConcreteInt>()) { |
| 121 | IsAnyFieldInitialized = true; |
| 122 | return false; |
| 123 | } |
| 124 | |
| 125 | if (V.isUndef()) { |
Kristof Umann | 015b059 | 2018-08-13 18:43:08 +0000 | [diff] [blame] | 126 | return addFieldToUninits( |
| 127 | LocalChain.add(LocField(FR, /*IsDereferenced*/ false))); |
Kristof Umann | 56963ae | 2018-08-13 18:17:05 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | if (!CheckPointeeInitialization) { |
| 131 | IsAnyFieldInitialized = true; |
| 132 | return false; |
| 133 | } |
| 134 | |
Kristof Umann | 56963ae | 2018-08-13 18:17:05 +0000 | [diff] [blame] | 135 | // At this point the pointer itself is initialized and points to a valid |
| 136 | // location, we'll now check the pointee. |
Kristof Umann | f0dd101 | 2018-09-14 08:58:21 +0000 | [diff] [blame^] | 137 | llvm::Optional<DereferenceInfo> DerefInfo = dereference(State, FR); |
Kristof Umann | 6460196 | 2018-08-21 10:45:21 +0000 | [diff] [blame] | 138 | if (!DerefInfo) { |
| 139 | IsAnyFieldInitialized = true; |
| 140 | return false; |
Kristof Umann | 56963ae | 2018-08-13 18:17:05 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Kristof Umann | f0dd101 | 2018-09-14 08:58:21 +0000 | [diff] [blame^] | 143 | const TypedValueRegion *R = DerefInfo->first; |
| 144 | const bool NeedsCastBack = DerefInfo->second; |
Kristof Umann | 6460196 | 2018-08-21 10:45:21 +0000 | [diff] [blame] | 145 | |
Kristof Umann | f0dd101 | 2018-09-14 08:58:21 +0000 | [diff] [blame^] | 146 | QualType DynT = R->getLocationType(); |
| 147 | QualType PointeeT = DynT->getPointeeType(); |
Kristof Umann | 56963ae | 2018-08-13 18:17:05 +0000 | [diff] [blame] | 148 | |
Kristof Umann | f0dd101 | 2018-09-14 08:58:21 +0000 | [diff] [blame^] | 149 | if (PointeeT->isStructureOrClassType()) { |
| 150 | if (NeedsCastBack) |
| 151 | return isNonUnionUninit(R, LocalChain.add(NeedsCastLocField(FR, DynT))); |
| 152 | return isNonUnionUninit(R, LocalChain.add(LocField(FR))); |
| 153 | } |
Kristof Umann | 56963ae | 2018-08-13 18:17:05 +0000 | [diff] [blame] | 154 | |
Kristof Umann | f0dd101 | 2018-09-14 08:58:21 +0000 | [diff] [blame^] | 155 | if (PointeeT->isUnionType()) { |
| 156 | if (isUnionUninit(R)) { |
Kristof Umann | 5a42441 | 2018-08-14 08:20:51 +0000 | [diff] [blame] | 157 | if (NeedsCastBack) |
Kristof Umann | f0dd101 | 2018-09-14 08:58:21 +0000 | [diff] [blame^] | 158 | return addFieldToUninits(LocalChain.add(NeedsCastLocField(FR, DynT))); |
| 159 | return addFieldToUninits(LocalChain.add(LocField(FR))); |
| 160 | } else { |
Kristof Umann | 56963ae | 2018-08-13 18:17:05 +0000 | [diff] [blame] | 161 | IsAnyFieldInitialized = true; |
| 162 | return false; |
| 163 | } |
Kristof Umann | 56963ae | 2018-08-13 18:17:05 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Kristof Umann | f0dd101 | 2018-09-14 08:58:21 +0000 | [diff] [blame^] | 166 | if (PointeeT->isArrayType()) { |
| 167 | IsAnyFieldInitialized = true; |
| 168 | return false; |
| 169 | } |
| 170 | |
| 171 | assert((isPrimitiveType(PointeeT) || isDereferencableType(PointeeT)) && |
Kristof Umann | 56963ae | 2018-08-13 18:17:05 +0000 | [diff] [blame] | 172 | "At this point FR must either have a primitive dynamic type, or it " |
| 173 | "must be a null, undefined, unknown or concrete pointer!"); |
| 174 | |
Kristof Umann | f0dd101 | 2018-09-14 08:58:21 +0000 | [diff] [blame^] | 175 | SVal PointeeV = State->getSVal(R); |
| 176 | |
| 177 | if (isPrimitiveUninit(PointeeV)) { |
Kristof Umann | 5a42441 | 2018-08-14 08:20:51 +0000 | [diff] [blame] | 178 | if (NeedsCastBack) |
| 179 | return addFieldToUninits(LocalChain.add(NeedsCastLocField(FR, DynT))); |
Kristof Umann | 015b059 | 2018-08-13 18:43:08 +0000 | [diff] [blame] | 180 | return addFieldToUninits(LocalChain.add(LocField(FR))); |
Kristof Umann | 5a42441 | 2018-08-14 08:20:51 +0000 | [diff] [blame] | 181 | } |
Kristof Umann | 56963ae | 2018-08-13 18:17:05 +0000 | [diff] [blame] | 182 | |
| 183 | IsAnyFieldInitialized = true; |
| 184 | return false; |
| 185 | } |
| 186 | |
| 187 | //===----------------------------------------------------------------------===// |
| 188 | // Utility functions. |
| 189 | //===----------------------------------------------------------------------===// |
| 190 | |
| 191 | static bool isVoidPointer(QualType T) { |
| 192 | while (!T.isNull()) { |
| 193 | if (T->isVoidPointerType()) |
| 194 | return true; |
| 195 | T = T->getPointeeType(); |
| 196 | } |
| 197 | return false; |
| 198 | } |
Kristof Umann | 6460196 | 2018-08-21 10:45:21 +0000 | [diff] [blame] | 199 | |
Kristof Umann | f0dd101 | 2018-09-14 08:58:21 +0000 | [diff] [blame^] | 200 | static llvm::Optional<DereferenceInfo> dereference(ProgramStateRef State, |
| 201 | const FieldRegion *FR) { |
Kristof Umann | 6460196 | 2018-08-21 10:45:21 +0000 | [diff] [blame] | 202 | |
Kristof Umann | f0dd101 | 2018-09-14 08:58:21 +0000 | [diff] [blame^] | 203 | llvm::SmallSet<const TypedValueRegion *, 5> VisitedRegions; |
Kristof Umann | 6460196 | 2018-08-21 10:45:21 +0000 | [diff] [blame] | 204 | |
| 205 | // If the static type of the field is a void pointer, we need to cast it back |
| 206 | // to the dynamic type before dereferencing. |
| 207 | bool NeedsCastBack = isVoidPointer(FR->getDecl()->getType()); |
| 208 | |
| 209 | SVal V = State->getSVal(FR); |
Kristof Umann | f0dd101 | 2018-09-14 08:58:21 +0000 | [diff] [blame^] | 210 | assert(V.getAsRegion() && "V must have an underlying region!"); |
Kristof Umann | 6460196 | 2018-08-21 10:45:21 +0000 | [diff] [blame] | 211 | |
Kristof Umann | f0dd101 | 2018-09-14 08:58:21 +0000 | [diff] [blame^] | 212 | // The region we'd like to acquire. |
| 213 | const auto *R = V.getAsRegion()->getAs<TypedValueRegion>(); |
| 214 | if (!R) |
| 215 | return None; |
| 216 | |
| 217 | VisitedRegions.insert(R); |
| 218 | |
| 219 | // We acquire the dynamic type of R, |
| 220 | QualType DynT = R->getLocationType(); |
| 221 | |
| 222 | while (const MemRegion *Tmp = State->getSVal(R, DynT).getAsRegion()) { |
| 223 | |
| 224 | R = Tmp->getAs<TypedValueRegion>(); |
| 225 | |
| 226 | if (!R) |
Kristof Umann | 6460196 | 2018-08-21 10:45:21 +0000 | [diff] [blame] | 227 | return None; |
Kristof Umann | 6460196 | 2018-08-21 10:45:21 +0000 | [diff] [blame] | 228 | |
Kristof Umann | f0dd101 | 2018-09-14 08:58:21 +0000 | [diff] [blame^] | 229 | // We found a cyclic pointer, like int *ptr = (int *)&ptr. |
| 230 | // TODO: Report these fields too. |
| 231 | if (!VisitedRegions.insert(R).second) |
Kristof Umann | 6460196 | 2018-08-21 10:45:21 +0000 | [diff] [blame] | 232 | return None; |
Kristof Umann | 6460196 | 2018-08-21 10:45:21 +0000 | [diff] [blame] | 233 | |
Kristof Umann | f0dd101 | 2018-09-14 08:58:21 +0000 | [diff] [blame^] | 234 | DynT = R->getLocationType(); |
| 235 | // In order to ensure that this loop terminates, we're also checking the |
| 236 | // dynamic type of R, since type hierarchy is finite. |
| 237 | if (isDereferencableType(DynT->getPointeeType())) |
| 238 | break; |
Kristof Umann | 6460196 | 2018-08-21 10:45:21 +0000 | [diff] [blame] | 239 | } |
| 240 | |
Kristof Umann | f0dd101 | 2018-09-14 08:58:21 +0000 | [diff] [blame^] | 241 | return std::make_pair(R, NeedsCastBack); |
Kristof Umann | 6460196 | 2018-08-21 10:45:21 +0000 | [diff] [blame] | 242 | } |