Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 1 | //===----- UninitializedObjectChecker.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 a checker that reports uninitialized fields in objects |
| 11 | // created after a constructor call. |
| 12 | // |
Kristof Umann | a3f7b58 | 2018-08-07 12:55:26 +0000 | [diff] [blame] | 13 | // This checker has several options: |
Kristof Umann | 9bd4439 | 2018-06-29 11:25:24 +0000 | [diff] [blame] | 14 | // - "Pedantic" (boolean). If its not set or is set to false, the checker |
| 15 | // won't emit warnings for objects that don't have at least one initialized |
| 16 | // field. This may be set with |
| 17 | // |
Kristof Umann | a3f7b58 | 2018-08-07 12:55:26 +0000 | [diff] [blame] | 18 | // `-analyzer-config alpha.cplusplus.UninitializedObject:Pedantic=true`. |
Kristof Umann | 9bd4439 | 2018-06-29 11:25:24 +0000 | [diff] [blame] | 19 | // |
| 20 | // - "NotesAsWarnings" (boolean). If set to true, the checker will emit a |
| 21 | // warning for each uninitalized field, as opposed to emitting one warning |
| 22 | // per constructor call, and listing the uninitialized fields that belongs |
| 23 | // to it in notes. Defaults to false. |
| 24 | // |
Kristof Umann | a3f7b58 | 2018-08-07 12:55:26 +0000 | [diff] [blame] | 25 | // `-analyzer-config \ |
| 26 | // alpha.cplusplus.UninitializedObject:NotesAsWarnings=true`. |
| 27 | // |
| 28 | // - "CheckPointeeInitialization" (boolean). If set to false, the checker will |
| 29 | // not analyze the pointee of pointer/reference fields, and will only check |
| 30 | // whether the object itself is initialized. Defaults to false. |
| 31 | // |
| 32 | // `-analyzer-config \ |
| 33 | // alpha.cplusplus.UninitializedObject:CheckPointeeInitialization=true`. |
| 34 | // |
| 35 | // TODO: With some clever heuristics, some pointers should be dereferenced |
| 36 | // by default. For example, if the pointee is constructed within the |
| 37 | // constructor call, it's reasonable to say that no external object |
| 38 | // references it, and we wouldn't generate multiple report on the same |
| 39 | // pointee. |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 40 | // |
| 41 | //===----------------------------------------------------------------------===// |
| 42 | |
| 43 | #include "ClangSACheckers.h" |
| 44 | #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" |
| 45 | #include "clang/StaticAnalyzer/Core/Checker.h" |
| 46 | #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" |
| 47 | #include <algorithm> |
| 48 | |
| 49 | using namespace clang; |
| 50 | using namespace clang::ento; |
| 51 | |
| 52 | namespace { |
| 53 | |
| 54 | class UninitializedObjectChecker : public Checker<check::EndFunction> { |
| 55 | std::unique_ptr<BuiltinBug> BT_uninitField; |
| 56 | |
| 57 | public: |
Kristof Umann | 9bd4439 | 2018-06-29 11:25:24 +0000 | [diff] [blame] | 58 | // These fields will be initialized when registering the checker. |
| 59 | bool IsPedantic; |
| 60 | bool ShouldConvertNotesToWarnings; |
Kristof Umann | a3f7b58 | 2018-08-07 12:55:26 +0000 | [diff] [blame] | 61 | bool CheckPointeeInitialization; |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 62 | |
| 63 | UninitializedObjectChecker() |
| 64 | : BT_uninitField(new BuiltinBug(this, "Uninitialized fields")) {} |
Reka Kovacs | ed8c05c | 2018-07-16 20:47:45 +0000 | [diff] [blame] | 65 | void checkEndFunction(const ReturnStmt *RS, CheckerContext &C) const; |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 66 | }; |
| 67 | |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 68 | /// Represents a field chain. A field chain is a vector of fields where the |
| 69 | /// first element of the chain is the object under checking (not stored), and |
| 70 | /// every other element is a field, and the element that precedes it is the |
| 71 | /// object that contains it. |
| 72 | /// |
| 73 | /// Note that this class is immutable, and new fields may only be added through |
| 74 | /// constructor calls. |
| 75 | class FieldChainInfo { |
| 76 | using FieldChain = llvm::ImmutableList<const FieldRegion *>; |
| 77 | |
| 78 | FieldChain Chain; |
| 79 | |
| 80 | const bool IsDereferenced = false; |
| 81 | |
| 82 | public: |
| 83 | FieldChainInfo() = default; |
| 84 | |
| 85 | FieldChainInfo(const FieldChainInfo &Other, const bool IsDereferenced) |
| 86 | : Chain(Other.Chain), IsDereferenced(IsDereferenced) {} |
| 87 | |
| 88 | FieldChainInfo(const FieldChainInfo &Other, const FieldRegion *FR, |
| 89 | const bool IsDereferenced = false); |
| 90 | |
| 91 | bool contains(const FieldRegion *FR) const { return Chain.contains(FR); } |
| 92 | bool isPointer() const; |
| 93 | |
| 94 | /// If this is a fieldchain whose last element is an uninitialized region of a |
| 95 | /// pointer type, `IsDereferenced` will store whether the pointer itself or |
| 96 | /// the pointee is uninitialized. |
| 97 | bool isDereferenced() const; |
| 98 | const FieldDecl *getEndOfChain() const; |
| 99 | void print(llvm::raw_ostream &Out) const; |
| 100 | |
| 101 | private: |
| 102 | /// Prints every element except the last to `Out`. Since ImmutableLists store |
| 103 | /// elements in reverse order, and have no reverse iterators, we use a |
| 104 | /// recursive function to print the fieldchain correctly. The last element in |
| 105 | /// the chain is to be printed by `print`. |
| 106 | static void printTail(llvm::raw_ostream &Out, |
| 107 | const llvm::ImmutableListImpl<const FieldRegion *> *L); |
| 108 | friend struct FieldChainInfoComparator; |
| 109 | }; |
| 110 | |
| 111 | struct FieldChainInfoComparator { |
Steven Wu | b3684db | 2018-06-22 16:51:17 +0000 | [diff] [blame] | 112 | bool operator()(const FieldChainInfo &lhs, const FieldChainInfo &rhs) const { |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 113 | assert(!lhs.Chain.isEmpty() && !rhs.Chain.isEmpty() && |
| 114 | "Attempted to store an empty fieldchain!"); |
| 115 | return *lhs.Chain.begin() < *rhs.Chain.begin(); |
| 116 | } |
| 117 | }; |
| 118 | |
| 119 | using UninitFieldSet = std::set<FieldChainInfo, FieldChainInfoComparator>; |
| 120 | |
| 121 | /// Searches for and stores uninitialized fields in a non-union object. |
| 122 | class FindUninitializedFields { |
| 123 | ProgramStateRef State; |
| 124 | const TypedValueRegion *const ObjectR; |
| 125 | |
| 126 | const bool IsPedantic; |
Kristof Umann | a3f7b58 | 2018-08-07 12:55:26 +0000 | [diff] [blame] | 127 | const bool CheckPointeeInitialization; |
| 128 | |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 129 | bool IsAnyFieldInitialized = false; |
| 130 | |
| 131 | UninitFieldSet UninitFields; |
| 132 | |
| 133 | public: |
| 134 | FindUninitializedFields(ProgramStateRef State, |
Kristof Umann | a3f7b58 | 2018-08-07 12:55:26 +0000 | [diff] [blame] | 135 | const TypedValueRegion *const R, bool IsPedantic, |
| 136 | bool CheckPointeeInitialization); |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 137 | const UninitFieldSet &getUninitFields(); |
| 138 | |
| 139 | private: |
| 140 | /// Adds a FieldChainInfo object to UninitFields. Return true if an insertion |
| 141 | /// took place. |
| 142 | bool addFieldToUninits(FieldChainInfo LocalChain); |
| 143 | |
| 144 | // For the purposes of this checker, we'll regard the object under checking as |
| 145 | // a directed tree, where |
| 146 | // * the root is the object under checking |
| 147 | // * every node is an object that is |
| 148 | // - a union |
| 149 | // - a non-union record |
| 150 | // - a pointer/reference |
| 151 | // - an array |
Kristof Umann | 7212cc0 | 2018-07-13 12:21:38 +0000 | [diff] [blame] | 152 | // - of a primitive type, which we'll define later in a helper function. |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 153 | // * the parent of each node is the object that contains it |
Kristof Umann | 7212cc0 | 2018-07-13 12:21:38 +0000 | [diff] [blame] | 154 | // * every leaf is an array, a primitive object, a nullptr or an undefined |
| 155 | // pointer. |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 156 | // |
| 157 | // Example: |
| 158 | // |
| 159 | // struct A { |
| 160 | // struct B { |
| 161 | // int x, y = 0; |
| 162 | // }; |
| 163 | // B b; |
| 164 | // int *iptr = new int; |
| 165 | // B* bptr; |
| 166 | // |
| 167 | // A() {} |
| 168 | // }; |
| 169 | // |
| 170 | // The directed tree: |
| 171 | // |
| 172 | // ->x |
| 173 | // / |
| 174 | // ->b--->y |
| 175 | // / |
| 176 | // A-->iptr->(int value) |
| 177 | // \ |
| 178 | // ->bptr |
| 179 | // |
| 180 | // From this we'll construct a vector of fieldchains, where each fieldchain |
| 181 | // represents an uninitialized field. An uninitialized field may be a |
Kristof Umann | 7212cc0 | 2018-07-13 12:21:38 +0000 | [diff] [blame] | 182 | // primitive object, a pointer, a pointee or a union without a single |
| 183 | // initialized field. |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 184 | // In the above example, for the default constructor call we'll end up with |
| 185 | // these fieldchains: |
| 186 | // |
| 187 | // this->b.x |
| 188 | // this->iptr (pointee uninit) |
| 189 | // this->bptr (pointer uninit) |
| 190 | // |
| 191 | // We'll traverse each node of the above graph with the appropiate one of |
| 192 | // these methods: |
| 193 | |
| 194 | /// This method checks a region of a union object, and returns true if no |
| 195 | /// field is initialized within the region. |
| 196 | bool isUnionUninit(const TypedValueRegion *R); |
| 197 | |
| 198 | /// This method checks a region of a non-union object, and returns true if |
| 199 | /// an uninitialized field is found within the region. |
| 200 | bool isNonUnionUninit(const TypedValueRegion *R, FieldChainInfo LocalChain); |
| 201 | |
| 202 | /// This method checks a region of a pointer or reference object, and returns |
| 203 | /// true if the ptr/ref object itself or any field within the pointee's region |
| 204 | /// is uninitialized. |
| 205 | bool isPointerOrReferenceUninit(const FieldRegion *FR, |
| 206 | FieldChainInfo LocalChain); |
| 207 | |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 208 | /// This method returns true if the value of a primitive object is |
| 209 | /// uninitialized. |
| 210 | bool isPrimitiveUninit(const SVal &V); |
| 211 | |
| 212 | // Note that we don't have a method for arrays -- the elements of an array are |
| 213 | // often left uninitialized intentionally even when it is of a C++ record |
| 214 | // type, so we'll assume that an array is always initialized. |
| 215 | // TODO: Add a support for nonloc::LocAsInteger. |
| 216 | }; |
| 217 | |
Kristof Umann | cc85244 | 2018-07-12 13:13:46 +0000 | [diff] [blame] | 218 | } // end of anonymous namespace |
| 219 | |
| 220 | // Static variable instantionations. |
| 221 | |
| 222 | static llvm::ImmutableListFactory<const FieldRegion *> Factory; |
| 223 | |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 224 | // Utility function declarations. |
| 225 | |
| 226 | /// Returns the object that was constructed by CtorDecl, or None if that isn't |
| 227 | /// possible. |
Kristof Umann | cc85244 | 2018-07-12 13:13:46 +0000 | [diff] [blame] | 228 | static Optional<nonloc::LazyCompoundVal> |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 229 | getObjectVal(const CXXConstructorDecl *CtorDecl, CheckerContext &Context); |
| 230 | |
| 231 | /// Checks whether the constructor under checking is called by another |
| 232 | /// constructor. |
Kristof Umann | cc85244 | 2018-07-12 13:13:46 +0000 | [diff] [blame] | 233 | static bool isCalledByConstructor(const CheckerContext &Context); |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 234 | |
| 235 | /// Returns whether FD can be (transitively) dereferenced to a void pointer type |
| 236 | /// (void*, void**, ...). The type of the region behind a void pointer isn't |
| 237 | /// known, and thus FD can not be analyzed. |
Kristof Umann | cc85244 | 2018-07-12 13:13:46 +0000 | [diff] [blame] | 238 | static bool isVoidPointer(const FieldDecl *FD); |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 239 | |
Kristof Umann | 7212cc0 | 2018-07-13 12:21:38 +0000 | [diff] [blame] | 240 | /// Returns true if T is a primitive type. We defined this type so that for |
| 241 | /// objects that we'd only like analyze as much as checking whether their |
| 242 | /// value is undefined or not, such as ints and doubles, can be analyzed with |
| 243 | /// ease. This also helps ensuring that every special field type is handled |
| 244 | /// correctly. |
Kristof Umann | cc85244 | 2018-07-12 13:13:46 +0000 | [diff] [blame] | 245 | static bool isPrimitiveType(const QualType &T) { |
Kristof Umann | 7212cc0 | 2018-07-13 12:21:38 +0000 | [diff] [blame] | 246 | return T->isBuiltinType() || T->isEnumeralType() || T->isMemberPointerType(); |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 247 | } |
| 248 | |
Kristof Umann | 9bd4439 | 2018-06-29 11:25:24 +0000 | [diff] [blame] | 249 | /// Constructs a note message for a given FieldChainInfo object. |
Kristof Umann | cc85244 | 2018-07-12 13:13:46 +0000 | [diff] [blame] | 250 | static void printNoteMessage(llvm::raw_ostream &Out, |
| 251 | const FieldChainInfo &Chain); |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 252 | |
Kristof Umann | 8c11909 | 2018-07-13 12:54:47 +0000 | [diff] [blame] | 253 | /// Returns with Field's name. This is a helper function to get the correct name |
| 254 | /// even if Field is a captured lambda variable. |
| 255 | static StringRef getVariableName(const FieldDecl *Field); |
| 256 | |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 257 | //===----------------------------------------------------------------------===// |
| 258 | // Methods for UninitializedObjectChecker. |
| 259 | //===----------------------------------------------------------------------===// |
| 260 | |
| 261 | void UninitializedObjectChecker::checkEndFunction( |
Reka Kovacs | ed8c05c | 2018-07-16 20:47:45 +0000 | [diff] [blame] | 262 | const ReturnStmt *RS, CheckerContext &Context) const { |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 263 | |
| 264 | const auto *CtorDecl = dyn_cast_or_null<CXXConstructorDecl>( |
| 265 | Context.getLocationContext()->getDecl()); |
| 266 | if (!CtorDecl) |
| 267 | return; |
| 268 | |
| 269 | if (!CtorDecl->isUserProvided()) |
| 270 | return; |
| 271 | |
| 272 | if (CtorDecl->getParent()->isUnion()) |
| 273 | return; |
| 274 | |
| 275 | // This avoids essentially the same error being reported multiple times. |
| 276 | if (isCalledByConstructor(Context)) |
| 277 | return; |
| 278 | |
| 279 | Optional<nonloc::LazyCompoundVal> Object = getObjectVal(CtorDecl, Context); |
| 280 | if (!Object) |
| 281 | return; |
| 282 | |
Kristof Umann | a3f7b58 | 2018-08-07 12:55:26 +0000 | [diff] [blame] | 283 | FindUninitializedFields F(Context.getState(), Object->getRegion(), IsPedantic, |
| 284 | CheckPointeeInitialization); |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 285 | |
| 286 | const UninitFieldSet &UninitFields = F.getUninitFields(); |
| 287 | |
| 288 | if (UninitFields.empty()) |
| 289 | return; |
| 290 | |
| 291 | // There are uninitialized fields in the record. |
| 292 | |
| 293 | ExplodedNode *Node = Context.generateNonFatalErrorNode(Context.getState()); |
| 294 | if (!Node) |
| 295 | return; |
| 296 | |
| 297 | PathDiagnosticLocation LocUsedForUniqueing; |
| 298 | const Stmt *CallSite = Context.getStackFrame()->getCallSite(); |
| 299 | if (CallSite) |
| 300 | LocUsedForUniqueing = PathDiagnosticLocation::createBegin( |
| 301 | CallSite, Context.getSourceManager(), Node->getLocationContext()); |
| 302 | |
Kristof Umann | 9bd4439 | 2018-06-29 11:25:24 +0000 | [diff] [blame] | 303 | // For Plist consumers that don't support notes just yet, we'll convert notes |
| 304 | // to warnings. |
| 305 | if (ShouldConvertNotesToWarnings) { |
| 306 | for (const auto &Chain : UninitFields) { |
| 307 | SmallString<100> WarningBuf; |
| 308 | llvm::raw_svector_ostream WarningOS(WarningBuf); |
| 309 | |
| 310 | printNoteMessage(WarningOS, Chain); |
| 311 | |
| 312 | auto Report = llvm::make_unique<BugReport>( |
| 313 | *BT_uninitField, WarningOS.str(), Node, LocUsedForUniqueing, |
| 314 | Node->getLocationContext()->getDecl()); |
| 315 | Context.emitReport(std::move(Report)); |
| 316 | } |
| 317 | return; |
| 318 | } |
| 319 | |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 320 | SmallString<100> WarningBuf; |
| 321 | llvm::raw_svector_ostream WarningOS(WarningBuf); |
| 322 | WarningOS << UninitFields.size() << " uninitialized field" |
| 323 | << (UninitFields.size() == 1 ? "" : "s") |
| 324 | << " at the end of the constructor call"; |
| 325 | |
| 326 | auto Report = llvm::make_unique<BugReport>( |
| 327 | *BT_uninitField, WarningOS.str(), Node, LocUsedForUniqueing, |
| 328 | Node->getLocationContext()->getDecl()); |
| 329 | |
Kristof Umann | 9bd4439 | 2018-06-29 11:25:24 +0000 | [diff] [blame] | 330 | for (const auto &Chain : UninitFields) { |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 331 | SmallString<200> NoteBuf; |
| 332 | llvm::raw_svector_ostream NoteOS(NoteBuf); |
| 333 | |
Kristof Umann | 9bd4439 | 2018-06-29 11:25:24 +0000 | [diff] [blame] | 334 | printNoteMessage(NoteOS, Chain); |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 335 | |
| 336 | Report->addNote(NoteOS.str(), |
Kristof Umann | 9bd4439 | 2018-06-29 11:25:24 +0000 | [diff] [blame] | 337 | PathDiagnosticLocation::create(Chain.getEndOfChain(), |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 338 | Context.getSourceManager())); |
| 339 | } |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 340 | Context.emitReport(std::move(Report)); |
| 341 | } |
| 342 | |
| 343 | //===----------------------------------------------------------------------===// |
| 344 | // Methods for FindUninitializedFields. |
| 345 | //===----------------------------------------------------------------------===// |
| 346 | |
| 347 | FindUninitializedFields::FindUninitializedFields( |
Kristof Umann | a3f7b58 | 2018-08-07 12:55:26 +0000 | [diff] [blame] | 348 | ProgramStateRef State, const TypedValueRegion *const R, bool IsPedantic, |
| 349 | bool CheckPointeeInitialization) |
| 350 | : State(State), ObjectR(R), IsPedantic(IsPedantic), |
| 351 | CheckPointeeInitialization(CheckPointeeInitialization) {} |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 352 | |
| 353 | const UninitFieldSet &FindUninitializedFields::getUninitFields() { |
| 354 | isNonUnionUninit(ObjectR, FieldChainInfo()); |
| 355 | |
| 356 | if (!IsPedantic && !IsAnyFieldInitialized) |
| 357 | UninitFields.clear(); |
| 358 | |
| 359 | return UninitFields; |
| 360 | } |
| 361 | |
| 362 | bool FindUninitializedFields::addFieldToUninits(FieldChainInfo Chain) { |
| 363 | if (State->getStateManager().getContext().getSourceManager().isInSystemHeader( |
| 364 | Chain.getEndOfChain()->getLocation())) |
| 365 | return false; |
| 366 | |
| 367 | return UninitFields.insert(Chain).second; |
| 368 | } |
| 369 | |
| 370 | bool FindUninitializedFields::isNonUnionUninit(const TypedValueRegion *R, |
| 371 | FieldChainInfo LocalChain) { |
| 372 | assert(R->getValueType()->isRecordType() && |
| 373 | !R->getValueType()->isUnionType() && |
| 374 | "This method only checks non-union record objects!"); |
| 375 | |
| 376 | const RecordDecl *RD = |
| 377 | R->getValueType()->getAs<RecordType>()->getDecl()->getDefinition(); |
| 378 | assert(RD && "Referred record has no definition"); |
| 379 | |
| 380 | bool ContainsUninitField = false; |
| 381 | |
| 382 | // Are all of this non-union's fields initialized? |
| 383 | for (const FieldDecl *I : RD->fields()) { |
| 384 | |
| 385 | const auto FieldVal = |
| 386 | State->getLValue(I, loc::MemRegionVal(R)).castAs<loc::MemRegionVal>(); |
| 387 | const auto *FR = FieldVal.getRegionAs<FieldRegion>(); |
| 388 | QualType T = I->getType(); |
| 389 | |
| 390 | // If LocalChain already contains FR, then we encountered a cyclic |
| 391 | // reference. In this case, region FR is already under checking at an |
| 392 | // earlier node in the directed tree. |
| 393 | if (LocalChain.contains(FR)) |
| 394 | return false; |
| 395 | |
| 396 | if (T->isStructureOrClassType()) { |
| 397 | if (isNonUnionUninit(FR, {LocalChain, FR})) |
| 398 | ContainsUninitField = true; |
| 399 | continue; |
| 400 | } |
| 401 | |
| 402 | if (T->isUnionType()) { |
| 403 | if (isUnionUninit(FR)) { |
| 404 | if (addFieldToUninits({LocalChain, FR})) |
| 405 | ContainsUninitField = true; |
| 406 | } else |
| 407 | IsAnyFieldInitialized = true; |
| 408 | continue; |
| 409 | } |
| 410 | |
| 411 | if (T->isArrayType()) { |
| 412 | IsAnyFieldInitialized = true; |
| 413 | continue; |
| 414 | } |
| 415 | |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 416 | if (T->isPointerType() || T->isReferenceType()) { |
| 417 | if (isPointerOrReferenceUninit(FR, LocalChain)) |
| 418 | ContainsUninitField = true; |
| 419 | continue; |
| 420 | } |
| 421 | |
Kristof Umann | 20e85ba | 2018-06-19 08:35:02 +0000 | [diff] [blame] | 422 | if (isPrimitiveType(T)) { |
| 423 | SVal V = State->getSVal(FieldVal); |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 424 | |
Kristof Umann | 20e85ba | 2018-06-19 08:35:02 +0000 | [diff] [blame] | 425 | if (isPrimitiveUninit(V)) { |
| 426 | if (addFieldToUninits({LocalChain, FR})) |
| 427 | ContainsUninitField = true; |
| 428 | } |
| 429 | continue; |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 430 | } |
Kristof Umann | 20e85ba | 2018-06-19 08:35:02 +0000 | [diff] [blame] | 431 | |
| 432 | llvm_unreachable("All cases are handled!"); |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | // Checking bases. |
| 436 | // FIXME: As of now, because of `isCalledByConstructor`, objects whose type |
| 437 | // is a descendant of another type will emit warnings for uninitalized |
| 438 | // inherited members. |
| 439 | // This is not the only way to analyze bases of an object -- if we didn't |
| 440 | // filter them out, and didn't analyze the bases, this checker would run for |
| 441 | // each base of the object in order of base initailization and in theory would |
| 442 | // find every uninitalized field. This approach could also make handling |
| 443 | // diamond inheritances more easily. |
| 444 | // |
| 445 | // This rule (that a descendant type's cunstructor is responsible for |
| 446 | // initializing inherited data members) is not obvious, and should it should |
| 447 | // be. |
| 448 | const auto *CXXRD = dyn_cast<CXXRecordDecl>(RD); |
| 449 | if (!CXXRD) |
| 450 | return ContainsUninitField; |
| 451 | |
| 452 | for (const CXXBaseSpecifier &BaseSpec : CXXRD->bases()) { |
| 453 | const auto *BaseRegion = State->getLValue(BaseSpec, R) |
| 454 | .castAs<loc::MemRegionVal>() |
| 455 | .getRegionAs<TypedValueRegion>(); |
| 456 | |
| 457 | if (isNonUnionUninit(BaseRegion, LocalChain)) |
| 458 | ContainsUninitField = true; |
| 459 | } |
| 460 | |
| 461 | return ContainsUninitField; |
| 462 | } |
| 463 | |
| 464 | bool FindUninitializedFields::isUnionUninit(const TypedValueRegion *R) { |
| 465 | assert(R->getValueType()->isUnionType() && |
| 466 | "This method only checks union objects!"); |
| 467 | // TODO: Implement support for union fields. |
| 468 | return false; |
| 469 | } |
| 470 | |
| 471 | // Note that pointers/references don't contain fields themselves, so in this |
| 472 | // function we won't add anything to LocalChain. |
| 473 | bool FindUninitializedFields::isPointerOrReferenceUninit( |
| 474 | const FieldRegion *FR, FieldChainInfo LocalChain) { |
| 475 | |
| 476 | assert((FR->getDecl()->getType()->isPointerType() || |
| 477 | FR->getDecl()->getType()->isReferenceType()) && |
| 478 | "This method only checks pointer/reference objects!"); |
| 479 | |
| 480 | SVal V = State->getSVal(FR); |
| 481 | |
| 482 | if (V.isUnknown() || V.isZeroConstant()) { |
| 483 | IsAnyFieldInitialized = true; |
| 484 | return false; |
| 485 | } |
| 486 | |
| 487 | if (V.isUndef()) { |
| 488 | return addFieldToUninits({LocalChain, FR}); |
| 489 | } |
| 490 | |
Kristof Umann | a3f7b58 | 2018-08-07 12:55:26 +0000 | [diff] [blame] | 491 | if (!CheckPointeeInitialization) { |
| 492 | IsAnyFieldInitialized = true; |
| 493 | return false; |
| 494 | } |
| 495 | |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 496 | const FieldDecl *FD = FR->getDecl(); |
| 497 | |
| 498 | // TODO: The dynamic type of a void pointer may be retrieved with |
| 499 | // `getDynamicTypeInfo`. |
| 500 | if (isVoidPointer(FD)) { |
| 501 | IsAnyFieldInitialized = true; |
| 502 | return false; |
| 503 | } |
| 504 | |
| 505 | assert(V.getAs<Loc>() && "V should be Loc at this point!"); |
| 506 | |
| 507 | // At this point the pointer itself is initialized and points to a valid |
| 508 | // location, we'll now check the pointee. |
| 509 | SVal DerefdV = State->getSVal(V.castAs<Loc>()); |
| 510 | |
| 511 | // TODO: Dereferencing should be done according to the dynamic type. |
| 512 | while (Optional<Loc> L = DerefdV.getAs<Loc>()) { |
| 513 | DerefdV = State->getSVal(*L); |
| 514 | } |
| 515 | |
| 516 | // If V is a pointer pointing to a record type. |
| 517 | if (Optional<nonloc::LazyCompoundVal> RecordV = |
| 518 | DerefdV.getAs<nonloc::LazyCompoundVal>()) { |
| 519 | |
| 520 | const TypedValueRegion *R = RecordV->getRegion(); |
| 521 | |
| 522 | // We can't reason about symbolic regions, assume its initialized. |
| 523 | // Note that this also avoids a potential infinite recursion, because |
| 524 | // constructors for list-like classes are checked without being called, and |
| 525 | // the Static Analyzer will construct a symbolic region for Node *next; or |
| 526 | // similar code snippets. |
| 527 | if (R->getSymbolicBase()) { |
| 528 | IsAnyFieldInitialized = true; |
| 529 | return false; |
| 530 | } |
| 531 | |
| 532 | const QualType T = R->getValueType(); |
| 533 | |
| 534 | if (T->isStructureOrClassType()) |
| 535 | return isNonUnionUninit(R, {LocalChain, FR}); |
| 536 | |
| 537 | if (T->isUnionType()) { |
| 538 | if (isUnionUninit(R)) { |
| 539 | return addFieldToUninits({LocalChain, FR, /*IsDereferenced*/ true}); |
| 540 | } else { |
| 541 | IsAnyFieldInitialized = true; |
| 542 | return false; |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | if (T->isArrayType()) { |
| 547 | IsAnyFieldInitialized = true; |
| 548 | return false; |
| 549 | } |
| 550 | |
| 551 | llvm_unreachable("All cases are handled!"); |
| 552 | } |
| 553 | |
| 554 | // TODO: If possible, it should be asserted that the DerefdV at this point is |
| 555 | // primitive. |
| 556 | |
| 557 | if (isPrimitiveUninit(DerefdV)) |
| 558 | return addFieldToUninits({LocalChain, FR, /*IsDereferenced*/ true}); |
| 559 | |
| 560 | IsAnyFieldInitialized = true; |
| 561 | return false; |
| 562 | } |
| 563 | |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 564 | bool FindUninitializedFields::isPrimitiveUninit(const SVal &V) { |
| 565 | if (V.isUndef()) |
| 566 | return true; |
| 567 | |
| 568 | IsAnyFieldInitialized = true; |
| 569 | return false; |
| 570 | } |
| 571 | |
| 572 | //===----------------------------------------------------------------------===// |
| 573 | // Methods for FieldChainInfo. |
| 574 | //===----------------------------------------------------------------------===// |
| 575 | |
| 576 | FieldChainInfo::FieldChainInfo(const FieldChainInfo &Other, |
| 577 | const FieldRegion *FR, const bool IsDereferenced) |
| 578 | : FieldChainInfo(Other, IsDereferenced) { |
| 579 | assert(!contains(FR) && "Can't add a field that is already a part of the " |
| 580 | "fieldchain! Is this a cyclic reference?"); |
| 581 | Chain = Factory.add(FR, Other.Chain); |
| 582 | } |
| 583 | |
| 584 | bool FieldChainInfo::isPointer() const { |
| 585 | assert(!Chain.isEmpty() && "Empty fieldchain!"); |
| 586 | return (*Chain.begin())->getDecl()->getType()->isPointerType(); |
| 587 | } |
| 588 | |
| 589 | bool FieldChainInfo::isDereferenced() const { |
| 590 | assert(isPointer() && "Only pointers may or may not be dereferenced!"); |
| 591 | return IsDereferenced; |
| 592 | } |
| 593 | |
| 594 | const FieldDecl *FieldChainInfo::getEndOfChain() const { |
| 595 | assert(!Chain.isEmpty() && "Empty fieldchain!"); |
| 596 | return (*Chain.begin())->getDecl(); |
| 597 | } |
| 598 | |
| 599 | // TODO: This function constructs an incorrect fieldchain string in the |
| 600 | // following case: |
| 601 | // |
| 602 | // struct Base { int x; }; |
| 603 | // struct D1 : Base {}; struct D2 : Base {}; |
| 604 | // |
| 605 | // struct MostDerived : D1, D2 { |
| 606 | // MostDerived() {} |
| 607 | // } |
| 608 | // |
| 609 | // A call to MostDerived::MostDerived() will cause two notes that say |
| 610 | // "uninitialized field 'this->x'", but we can't refer to 'x' directly, |
| 611 | // we need an explicit namespace resolution whether the uninit field was |
| 612 | // 'D1::x' or 'D2::x'. |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 613 | void FieldChainInfo::print(llvm::raw_ostream &Out) const { |
| 614 | if (Chain.isEmpty()) |
| 615 | return; |
| 616 | |
| 617 | const llvm::ImmutableListImpl<const FieldRegion *> *L = |
| 618 | Chain.getInternalPointer(); |
| 619 | printTail(Out, L->getTail()); |
Kristof Umann | 8c11909 | 2018-07-13 12:54:47 +0000 | [diff] [blame] | 620 | Out << getVariableName(L->getHead()->getDecl()); |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | void FieldChainInfo::printTail( |
| 624 | llvm::raw_ostream &Out, |
| 625 | const llvm::ImmutableListImpl<const FieldRegion *> *L) { |
| 626 | if (!L) |
| 627 | return; |
| 628 | |
| 629 | printTail(Out, L->getTail()); |
| 630 | const FieldDecl *Field = L->getHead()->getDecl(); |
Kristof Umann | 8c11909 | 2018-07-13 12:54:47 +0000 | [diff] [blame] | 631 | Out << getVariableName(Field); |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 632 | Out << (Field->getType()->isPointerType() ? "->" : "."); |
| 633 | } |
| 634 | |
| 635 | //===----------------------------------------------------------------------===// |
| 636 | // Utility functions. |
| 637 | //===----------------------------------------------------------------------===// |
| 638 | |
Kristof Umann | cc85244 | 2018-07-12 13:13:46 +0000 | [diff] [blame] | 639 | static bool isVoidPointer(const FieldDecl *FD) { |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 640 | QualType T = FD->getType(); |
| 641 | |
| 642 | while (!T.isNull()) { |
| 643 | if (T->isVoidPointerType()) |
| 644 | return true; |
| 645 | T = T->getPointeeType(); |
| 646 | } |
| 647 | return false; |
| 648 | } |
| 649 | |
Kristof Umann | cc85244 | 2018-07-12 13:13:46 +0000 | [diff] [blame] | 650 | static Optional<nonloc::LazyCompoundVal> |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 651 | getObjectVal(const CXXConstructorDecl *CtorDecl, CheckerContext &Context) { |
| 652 | |
| 653 | Loc ThisLoc = Context.getSValBuilder().getCXXThis(CtorDecl->getParent(), |
| 654 | Context.getStackFrame()); |
| 655 | // Getting the value for 'this'. |
| 656 | SVal This = Context.getState()->getSVal(ThisLoc); |
| 657 | |
| 658 | // Getting the value for '*this'. |
| 659 | SVal Object = Context.getState()->getSVal(This.castAs<Loc>()); |
| 660 | |
| 661 | return Object.getAs<nonloc::LazyCompoundVal>(); |
| 662 | } |
| 663 | |
| 664 | // TODO: We should also check that if the constructor was called by another |
| 665 | // constructor, whether those two are in any relation to one another. In it's |
| 666 | // current state, this introduces some false negatives. |
Kristof Umann | cc85244 | 2018-07-12 13:13:46 +0000 | [diff] [blame] | 667 | static bool isCalledByConstructor(const CheckerContext &Context) { |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 668 | const LocationContext *LC = Context.getLocationContext()->getParent(); |
| 669 | |
| 670 | while (LC) { |
| 671 | if (isa<CXXConstructorDecl>(LC->getDecl())) |
| 672 | return true; |
| 673 | |
| 674 | LC = LC->getParent(); |
| 675 | } |
| 676 | return false; |
| 677 | } |
| 678 | |
Kristof Umann | cc85244 | 2018-07-12 13:13:46 +0000 | [diff] [blame] | 679 | static void printNoteMessage(llvm::raw_ostream &Out, |
| 680 | const FieldChainInfo &Chain) { |
Kristof Umann | 9bd4439 | 2018-06-29 11:25:24 +0000 | [diff] [blame] | 681 | if (Chain.isPointer()) { |
| 682 | if (Chain.isDereferenced()) |
| 683 | Out << "uninitialized pointee 'this->"; |
| 684 | else |
| 685 | Out << "uninitialized pointer 'this->"; |
| 686 | } else |
| 687 | Out << "uninitialized field 'this->"; |
| 688 | Chain.print(Out); |
| 689 | Out << "'"; |
| 690 | } |
| 691 | |
Kristof Umann | 8c11909 | 2018-07-13 12:54:47 +0000 | [diff] [blame] | 692 | static StringRef getVariableName(const FieldDecl *Field) { |
| 693 | // If Field is a captured lambda variable, Field->getName() will return with |
| 694 | // an empty string. We can however acquire it's name from the lambda's |
| 695 | // captures. |
| 696 | const auto *CXXParent = dyn_cast<CXXRecordDecl>(Field->getParent()); |
| 697 | |
| 698 | if (CXXParent && CXXParent->isLambda()) { |
| 699 | assert(CXXParent->captures_begin()); |
| 700 | auto It = CXXParent->captures_begin() + Field->getFieldIndex(); |
| 701 | return It->getCapturedVar()->getName(); |
| 702 | } |
| 703 | |
| 704 | return Field->getName(); |
| 705 | } |
| 706 | |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 707 | void ento::registerUninitializedObjectChecker(CheckerManager &Mgr) { |
| 708 | auto Chk = Mgr.registerChecker<UninitializedObjectChecker>(); |
| 709 | Chk->IsPedantic = Mgr.getAnalyzerOptions().getBooleanOption( |
| 710 | "Pedantic", /*DefaultVal*/ false, Chk); |
Kristof Umann | 9bd4439 | 2018-06-29 11:25:24 +0000 | [diff] [blame] | 711 | Chk->ShouldConvertNotesToWarnings = Mgr.getAnalyzerOptions().getBooleanOption( |
| 712 | "NotesAsWarnings", /*DefaultVal*/ false, Chk); |
Kristof Umann | a3f7b58 | 2018-08-07 12:55:26 +0000 | [diff] [blame] | 713 | Chk->CheckPointeeInitialization = Mgr.getAnalyzerOptions().getBooleanOption( |
| 714 | "CheckPointeeInitialization", /*DefaultVal*/ false, Chk); |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 715 | } |