Kristof Umann | ceb5f65 | 2018-09-14 09:07:40 +0000 | [diff] [blame] | 1 | //===----- UninitializedPointee.cpp ------------------------------*- C++ -*-==// |
Kristof Umann | 56963ae | 2018-08-13 18:17:05 +0000 | [diff] [blame] | 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 | // |
Kristof Umann | 6cec6c4 | 2018-09-14 09:39:26 +0000 | [diff] [blame] | 13 | // To read about command line options and documentation about how the checker |
| 14 | // works, refer to UninitializedObjectChecker.h. |
Kristof Umann | 56963ae | 2018-08-13 18:17:05 +0000 | [diff] [blame] | 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
Kristof Umann | a37bba4 | 2018-08-13 18:22:22 +0000 | [diff] [blame] | 18 | #include "UninitializedObject.h" |
Kristof Umann | 56963ae | 2018-08-13 18:17:05 +0000 | [diff] [blame] | 19 | #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" |
| 20 | #include "clang/StaticAnalyzer/Core/Checker.h" |
| 21 | #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" |
| 22 | #include "clang/StaticAnalyzer/Core/PathSensitive/DynamicTypeMap.h" |
| 23 | |
| 24 | using namespace clang; |
| 25 | using namespace clang::ento; |
| 26 | |
Kristof Umann | 015b059 | 2018-08-13 18:43:08 +0000 | [diff] [blame] | 27 | namespace { |
| 28 | |
| 29 | /// Represents a pointer or a reference field. |
Richard Smith | 651d683 | 2018-08-13 22:07:11 +0000 | [diff] [blame] | 30 | class LocField final : public FieldNode { |
Kristof Umann | 015b059 | 2018-08-13 18:43:08 +0000 | [diff] [blame] | 31 | /// We'll store whether the pointee or the pointer itself is uninitialited. |
| 32 | const bool IsDereferenced; |
| 33 | |
| 34 | public: |
| 35 | LocField(const FieldRegion *FR, const bool IsDereferenced = true) |
| 36 | : FieldNode(FR), IsDereferenced(IsDereferenced) {} |
| 37 | |
| 38 | virtual void printNoteMsg(llvm::raw_ostream &Out) const override { |
| 39 | if (IsDereferenced) |
| 40 | Out << "uninitialized pointee "; |
| 41 | else |
| 42 | Out << "uninitialized pointer "; |
| 43 | } |
| 44 | |
| 45 | virtual void printPrefix(llvm::raw_ostream &Out) const override {} |
| 46 | |
| 47 | virtual void printNode(llvm::raw_ostream &Out) const override { |
| 48 | Out << getVariableName(getDecl()); |
| 49 | } |
| 50 | |
| 51 | virtual void printSeparator(llvm::raw_ostream &Out) const override { |
| 52 | if (getDecl()->getType()->isPointerType()) |
| 53 | Out << "->"; |
| 54 | else |
| 55 | Out << '.'; |
| 56 | } |
| 57 | }; |
| 58 | |
Kristof Umann | f051379 | 2018-09-14 10:18:26 +0000 | [diff] [blame] | 59 | /// Represents a nonloc::LocAsInteger or void* field, that point to objects, but |
| 60 | /// needs to be casted back to its dynamic type for a correct note message. |
Ilya Biryukov | 88ad9cf | 2018-09-14 11:28:48 +0000 | [diff] [blame] | 61 | class NeedsCastLocField final : public FieldNode { |
Kristof Umann | 5a42441 | 2018-08-14 08:20:51 +0000 | [diff] [blame] | 62 | QualType CastBackType; |
| 63 | |
| 64 | public: |
| 65 | NeedsCastLocField(const FieldRegion *FR, const QualType &T) |
| 66 | : FieldNode(FR), CastBackType(T) {} |
| 67 | |
| 68 | virtual void printNoteMsg(llvm::raw_ostream &Out) const override { |
| 69 | Out << "uninitialized pointee "; |
| 70 | } |
| 71 | |
| 72 | virtual void printPrefix(llvm::raw_ostream &Out) const override { |
Kristof Umann | f051379 | 2018-09-14 10:18:26 +0000 | [diff] [blame] | 73 | // If this object is a nonloc::LocAsInteger. |
| 74 | if (getDecl()->getType()->isIntegerType()) |
| 75 | Out << "reinterpret_cast"; |
| 76 | // If this pointer's dynamic type is different then it's static type. |
| 77 | else |
| 78 | Out << "static_cast"; |
| 79 | Out << '<' << CastBackType.getAsString() << ">("; |
Kristof Umann | 5a42441 | 2018-08-14 08:20:51 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | virtual void printNode(llvm::raw_ostream &Out) const override { |
| 83 | Out << getVariableName(getDecl()) << ')'; |
| 84 | } |
| 85 | |
| 86 | virtual void printSeparator(llvm::raw_ostream &Out) const override { |
| 87 | Out << "->"; |
| 88 | } |
| 89 | }; |
| 90 | |
Kristof Umann | 8e5328b | 2018-10-11 11:58:53 +0000 | [diff] [blame] | 91 | /// Represents a Loc field that points to itself. |
| 92 | class CyclicLocField final : public FieldNode { |
| 93 | |
| 94 | public: |
| 95 | CyclicLocField(const FieldRegion *FR) : FieldNode(FR) {} |
| 96 | |
| 97 | virtual void printNoteMsg(llvm::raw_ostream &Out) const override { |
| 98 | Out << "object references itself "; |
| 99 | } |
| 100 | |
| 101 | virtual void printPrefix(llvm::raw_ostream &Out) const override {} |
| 102 | |
| 103 | virtual void printNode(llvm::raw_ostream &Out) const override { |
| 104 | Out << getVariableName(getDecl()); |
| 105 | } |
| 106 | |
| 107 | virtual void printSeparator(llvm::raw_ostream &Out) const override { |
| 108 | llvm_unreachable("CyclicLocField objects must be the last node of the " |
| 109 | "fieldchain!"); |
| 110 | } |
| 111 | }; |
| 112 | |
Kristof Umann | 015b059 | 2018-08-13 18:43:08 +0000 | [diff] [blame] | 113 | } // end of anonymous namespace |
| 114 | |
Kristof Umann | 56963ae | 2018-08-13 18:17:05 +0000 | [diff] [blame] | 115 | // Utility function declarations. |
| 116 | |
Kristof Umann | 8e5328b | 2018-10-11 11:58:53 +0000 | [diff] [blame] | 117 | struct DereferenceInfo { |
| 118 | const TypedValueRegion *R; |
| 119 | const bool NeedsCastBack; |
| 120 | const bool IsCyclic; |
| 121 | DereferenceInfo(const TypedValueRegion *R, bool NCB, bool IC) |
| 122 | : R(R), NeedsCastBack(NCB), IsCyclic(IC) {} |
| 123 | }; |
Kristof Umann | f0dd101 | 2018-09-14 08:58:21 +0000 | [diff] [blame] | 124 | |
| 125 | /// Dereferences \p FR and returns with the pointee's region, and whether it |
| 126 | /// needs to be casted back to it's location type. If for whatever reason |
| 127 | /// dereferencing fails, returns with None. |
| 128 | static llvm::Optional<DereferenceInfo> dereference(ProgramStateRef State, |
| 129 | const FieldRegion *FR); |
Kristof Umann | 6460196 | 2018-08-21 10:45:21 +0000 | [diff] [blame] | 130 | |
Kristof Umann | 8e5328b | 2018-10-11 11:58:53 +0000 | [diff] [blame] | 131 | /// Returns whether \p T can be (transitively) dereferenced to a void pointer |
| 132 | /// type (void*, void**, ...). |
| 133 | static bool isVoidPointer(QualType T); |
| 134 | |
Kristof Umann | 56963ae | 2018-08-13 18:17:05 +0000 | [diff] [blame] | 135 | //===----------------------------------------------------------------------===// |
| 136 | // Methods for FindUninitializedFields. |
| 137 | //===----------------------------------------------------------------------===// |
| 138 | |
Kristof Umann | ceb5f65 | 2018-09-14 09:07:40 +0000 | [diff] [blame] | 139 | bool FindUninitializedFields::isDereferencableUninit( |
Kristof Umann | 56963ae | 2018-08-13 18:17:05 +0000 | [diff] [blame] | 140 | const FieldRegion *FR, FieldChainInfo LocalChain) { |
| 141 | |
Kristof Umann | 56963ae | 2018-08-13 18:17:05 +0000 | [diff] [blame] | 142 | SVal V = State->getSVal(FR); |
| 143 | |
Kristof Umann | f051379 | 2018-09-14 10:18:26 +0000 | [diff] [blame] | 144 | assert((isDereferencableType(FR->getDecl()->getType()) || |
| 145 | V.getAs<nonloc::LocAsInteger>()) && |
Raphael Isemann | b23ccec | 2018-12-10 12:37:46 +0000 | [diff] [blame] | 146 | "This method only checks dereferenceable objects!"); |
Kristof Umann | f051379 | 2018-09-14 10:18:26 +0000 | [diff] [blame] | 147 | |
Kristof Umann | 56963ae | 2018-08-13 18:17:05 +0000 | [diff] [blame] | 148 | if (V.isUnknown() || V.getAs<loc::ConcreteInt>()) { |
| 149 | IsAnyFieldInitialized = true; |
| 150 | return false; |
| 151 | } |
| 152 | |
| 153 | if (V.isUndef()) { |
Kristof Umann | 015b059 | 2018-08-13 18:43:08 +0000 | [diff] [blame] | 154 | return addFieldToUninits( |
Kristof Umann | 4ff7769 | 2018-11-18 11:34:10 +0000 | [diff] [blame] | 155 | LocalChain.add(LocField(FR, /*IsDereferenced*/ false)), FR); |
Kristof Umann | 56963ae | 2018-08-13 18:17:05 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Kristof Umann | 6cec6c4 | 2018-09-14 09:39:26 +0000 | [diff] [blame] | 158 | if (!Opts.CheckPointeeInitialization) { |
Kristof Umann | 56963ae | 2018-08-13 18:17:05 +0000 | [diff] [blame] | 159 | IsAnyFieldInitialized = true; |
| 160 | return false; |
| 161 | } |
| 162 | |
Kristof Umann | 56963ae | 2018-08-13 18:17:05 +0000 | [diff] [blame] | 163 | // At this point the pointer itself is initialized and points to a valid |
| 164 | // location, we'll now check the pointee. |
Kristof Umann | f0dd101 | 2018-09-14 08:58:21 +0000 | [diff] [blame] | 165 | llvm::Optional<DereferenceInfo> DerefInfo = dereference(State, FR); |
Kristof Umann | 6460196 | 2018-08-21 10:45:21 +0000 | [diff] [blame] | 166 | if (!DerefInfo) { |
| 167 | IsAnyFieldInitialized = true; |
| 168 | return false; |
Kristof Umann | 56963ae | 2018-08-13 18:17:05 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Kristof Umann | 8e5328b | 2018-10-11 11:58:53 +0000 | [diff] [blame] | 171 | if (DerefInfo->IsCyclic) |
Kristof Umann | 4ff7769 | 2018-11-18 11:34:10 +0000 | [diff] [blame] | 172 | return addFieldToUninits(LocalChain.add(CyclicLocField(FR)), FR); |
Kristof Umann | 8e5328b | 2018-10-11 11:58:53 +0000 | [diff] [blame] | 173 | |
| 174 | const TypedValueRegion *R = DerefInfo->R; |
| 175 | const bool NeedsCastBack = DerefInfo->NeedsCastBack; |
Kristof Umann | 6460196 | 2018-08-21 10:45:21 +0000 | [diff] [blame] | 176 | |
Kristof Umann | f0dd101 | 2018-09-14 08:58:21 +0000 | [diff] [blame] | 177 | QualType DynT = R->getLocationType(); |
| 178 | QualType PointeeT = DynT->getPointeeType(); |
Kristof Umann | 56963ae | 2018-08-13 18:17:05 +0000 | [diff] [blame] | 179 | |
Kristof Umann | f0dd101 | 2018-09-14 08:58:21 +0000 | [diff] [blame] | 180 | if (PointeeT->isStructureOrClassType()) { |
| 181 | if (NeedsCastBack) |
| 182 | return isNonUnionUninit(R, LocalChain.add(NeedsCastLocField(FR, DynT))); |
| 183 | return isNonUnionUninit(R, LocalChain.add(LocField(FR))); |
| 184 | } |
Kristof Umann | 56963ae | 2018-08-13 18:17:05 +0000 | [diff] [blame] | 185 | |
Kristof Umann | f0dd101 | 2018-09-14 08:58:21 +0000 | [diff] [blame] | 186 | if (PointeeT->isUnionType()) { |
| 187 | if (isUnionUninit(R)) { |
Kristof Umann | 5a42441 | 2018-08-14 08:20:51 +0000 | [diff] [blame] | 188 | if (NeedsCastBack) |
Kristof Umann | 4ff7769 | 2018-11-18 11:34:10 +0000 | [diff] [blame] | 189 | return addFieldToUninits(LocalChain.add(NeedsCastLocField(FR, DynT)), |
| 190 | R); |
| 191 | return addFieldToUninits(LocalChain.add(LocField(FR)), R); |
Kristof Umann | f0dd101 | 2018-09-14 08:58:21 +0000 | [diff] [blame] | 192 | } else { |
Kristof Umann | 56963ae | 2018-08-13 18:17:05 +0000 | [diff] [blame] | 193 | IsAnyFieldInitialized = true; |
| 194 | return false; |
| 195 | } |
Kristof Umann | 56963ae | 2018-08-13 18:17:05 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Kristof Umann | f0dd101 | 2018-09-14 08:58:21 +0000 | [diff] [blame] | 198 | if (PointeeT->isArrayType()) { |
| 199 | IsAnyFieldInitialized = true; |
| 200 | return false; |
| 201 | } |
| 202 | |
| 203 | assert((isPrimitiveType(PointeeT) || isDereferencableType(PointeeT)) && |
Kristof Umann | 56963ae | 2018-08-13 18:17:05 +0000 | [diff] [blame] | 204 | "At this point FR must either have a primitive dynamic type, or it " |
| 205 | "must be a null, undefined, unknown or concrete pointer!"); |
| 206 | |
Kristof Umann | f0dd101 | 2018-09-14 08:58:21 +0000 | [diff] [blame] | 207 | SVal PointeeV = State->getSVal(R); |
| 208 | |
| 209 | if (isPrimitiveUninit(PointeeV)) { |
Kristof Umann | 5a42441 | 2018-08-14 08:20:51 +0000 | [diff] [blame] | 210 | if (NeedsCastBack) |
Kristof Umann | 4ff7769 | 2018-11-18 11:34:10 +0000 | [diff] [blame] | 211 | return addFieldToUninits(LocalChain.add(NeedsCastLocField(FR, DynT)), R); |
| 212 | return addFieldToUninits(LocalChain.add(LocField(FR)), R); |
Kristof Umann | 5a42441 | 2018-08-14 08:20:51 +0000 | [diff] [blame] | 213 | } |
Kristof Umann | 56963ae | 2018-08-13 18:17:05 +0000 | [diff] [blame] | 214 | |
| 215 | IsAnyFieldInitialized = true; |
| 216 | return false; |
| 217 | } |
| 218 | |
| 219 | //===----------------------------------------------------------------------===// |
| 220 | // Utility functions. |
| 221 | //===----------------------------------------------------------------------===// |
| 222 | |
Kristof Umann | f0dd101 | 2018-09-14 08:58:21 +0000 | [diff] [blame] | 223 | static llvm::Optional<DereferenceInfo> dereference(ProgramStateRef State, |
| 224 | const FieldRegion *FR) { |
Kristof Umann | 6460196 | 2018-08-21 10:45:21 +0000 | [diff] [blame] | 225 | |
Kristof Umann | f0dd101 | 2018-09-14 08:58:21 +0000 | [diff] [blame] | 226 | llvm::SmallSet<const TypedValueRegion *, 5> VisitedRegions; |
Kristof Umann | 6460196 | 2018-08-21 10:45:21 +0000 | [diff] [blame] | 227 | |
Kristof Umann | 6460196 | 2018-08-21 10:45:21 +0000 | [diff] [blame] | 228 | SVal V = State->getSVal(FR); |
Kristof Umann | f0dd101 | 2018-09-14 08:58:21 +0000 | [diff] [blame] | 229 | assert(V.getAsRegion() && "V must have an underlying region!"); |
Kristof Umann | 6460196 | 2018-08-21 10:45:21 +0000 | [diff] [blame] | 230 | |
Kristof Umann | f051379 | 2018-09-14 10:18:26 +0000 | [diff] [blame] | 231 | // If the static type of the field is a void pointer, or it is a |
| 232 | // nonloc::LocAsInteger, we need to cast it back to the dynamic type before |
| 233 | // dereferencing. |
| 234 | bool NeedsCastBack = isVoidPointer(FR->getDecl()->getType()) || |
| 235 | V.getAs<nonloc::LocAsInteger>(); |
| 236 | |
Kristof Umann | f0dd101 | 2018-09-14 08:58:21 +0000 | [diff] [blame] | 237 | // The region we'd like to acquire. |
| 238 | const auto *R = V.getAsRegion()->getAs<TypedValueRegion>(); |
| 239 | if (!R) |
| 240 | return None; |
| 241 | |
| 242 | VisitedRegions.insert(R); |
| 243 | |
| 244 | // We acquire the dynamic type of R, |
| 245 | QualType DynT = R->getLocationType(); |
| 246 | |
| 247 | while (const MemRegion *Tmp = State->getSVal(R, DynT).getAsRegion()) { |
| 248 | |
| 249 | R = Tmp->getAs<TypedValueRegion>(); |
Kristof Umann | f0dd101 | 2018-09-14 08:58:21 +0000 | [diff] [blame] | 250 | if (!R) |
Kristof Umann | 6460196 | 2018-08-21 10:45:21 +0000 | [diff] [blame] | 251 | return None; |
Kristof Umann | 6460196 | 2018-08-21 10:45:21 +0000 | [diff] [blame] | 252 | |
Kristof Umann | f0dd101 | 2018-09-14 08:58:21 +0000 | [diff] [blame] | 253 | // We found a cyclic pointer, like int *ptr = (int *)&ptr. |
Kristof Umann | f0dd101 | 2018-09-14 08:58:21 +0000 | [diff] [blame] | 254 | if (!VisitedRegions.insert(R).second) |
Kristof Umann | 8e5328b | 2018-10-11 11:58:53 +0000 | [diff] [blame] | 255 | return DereferenceInfo{R, NeedsCastBack, /*IsCyclic*/ true}; |
Kristof Umann | 6460196 | 2018-08-21 10:45:21 +0000 | [diff] [blame] | 256 | |
Kristof Umann | f0dd101 | 2018-09-14 08:58:21 +0000 | [diff] [blame] | 257 | DynT = R->getLocationType(); |
| 258 | // In order to ensure that this loop terminates, we're also checking the |
| 259 | // dynamic type of R, since type hierarchy is finite. |
| 260 | if (isDereferencableType(DynT->getPointeeType())) |
| 261 | break; |
Kristof Umann | 6460196 | 2018-08-21 10:45:21 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Kristof Umann | 3ef3dd7 | 2018-09-14 09:13:36 +0000 | [diff] [blame] | 264 | while (R->getAs<CXXBaseObjectRegion>()) { |
| 265 | NeedsCastBack = true; |
| 266 | |
| 267 | if (!isa<TypedValueRegion>(R->getSuperRegion())) |
| 268 | break; |
| 269 | R = R->getSuperRegion()->getAs<TypedValueRegion>(); |
| 270 | } |
| 271 | |
Kristof Umann | 8e5328b | 2018-10-11 11:58:53 +0000 | [diff] [blame] | 272 | return DereferenceInfo{R, NeedsCastBack, /*IsCyclic*/ false}; |
| 273 | } |
| 274 | |
| 275 | static bool isVoidPointer(QualType T) { |
| 276 | while (!T.isNull()) { |
| 277 | if (T->isVoidPointerType()) |
| 278 | return true; |
| 279 | T = T->getPointeeType(); |
| 280 | } |
| 281 | return false; |
Kristof Umann | 6460196 | 2018-08-21 10:45:21 +0000 | [diff] [blame] | 282 | } |