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 | // |
Kristof Umann | 56963ae | 2018-08-13 18:17:05 +0000 | [diff] [blame] | 41 | // To read about how the checker works, refer to the comments in |
| 42 | // UninitializedObject.h. |
| 43 | // |
| 44 | // Some of the logic is implemented in UninitializedPointee.cpp, to reduce the |
| 45 | // complexity of this file. |
| 46 | // |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 47 | //===----------------------------------------------------------------------===// |
| 48 | |
Richard Smith | 651d683 | 2018-08-13 22:07:11 +0000 | [diff] [blame] | 49 | #include "../ClangSACheckers.h" |
Kristof Umann | a37bba4 | 2018-08-13 18:22:22 +0000 | [diff] [blame] | 50 | #include "UninitializedObject.h" |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 51 | #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" |
| 52 | #include "clang/StaticAnalyzer/Core/Checker.h" |
| 53 | #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" |
Kristof Umann | ef9af05 | 2018-08-08 13:18:53 +0000 | [diff] [blame] | 54 | #include "clang/StaticAnalyzer/Core/PathSensitive/DynamicTypeMap.h" |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 55 | |
| 56 | using namespace clang; |
| 57 | using namespace clang::ento; |
| 58 | |
| 59 | namespace { |
| 60 | |
| 61 | class UninitializedObjectChecker : public Checker<check::EndFunction> { |
| 62 | std::unique_ptr<BuiltinBug> BT_uninitField; |
| 63 | |
| 64 | public: |
Kristof Umann | 9bd4439 | 2018-06-29 11:25:24 +0000 | [diff] [blame] | 65 | // These fields will be initialized when registering the checker. |
| 66 | bool IsPedantic; |
| 67 | bool ShouldConvertNotesToWarnings; |
Kristof Umann | a3f7b58 | 2018-08-07 12:55:26 +0000 | [diff] [blame] | 68 | bool CheckPointeeInitialization; |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 69 | |
| 70 | UninitializedObjectChecker() |
| 71 | : BT_uninitField(new BuiltinBug(this, "Uninitialized fields")) {} |
Reka Kovacs | ed8c05c | 2018-07-16 20:47:45 +0000 | [diff] [blame] | 72 | void checkEndFunction(const ReturnStmt *RS, CheckerContext &C) const; |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 73 | }; |
| 74 | |
Kristof Umann | 015b059 | 2018-08-13 18:43:08 +0000 | [diff] [blame] | 75 | /// A basic field type, that is not a pointer or a reference, it's dynamic and |
| 76 | /// static type is the same. |
Richard Smith | 651d683 | 2018-08-13 22:07:11 +0000 | [diff] [blame] | 77 | class RegularField final : public FieldNode { |
Kristof Umann | 015b059 | 2018-08-13 18:43:08 +0000 | [diff] [blame] | 78 | public: |
| 79 | RegularField(const FieldRegion *FR) : FieldNode(FR) {} |
| 80 | |
| 81 | virtual void printNoteMsg(llvm::raw_ostream &Out) const override { |
| 82 | Out << "uninitialized field "; |
| 83 | } |
| 84 | |
| 85 | virtual void printPrefix(llvm::raw_ostream &Out) const override {} |
| 86 | |
Richard Smith | 651d683 | 2018-08-13 22:07:11 +0000 | [diff] [blame] | 87 | virtual void printNode(llvm::raw_ostream &Out) const override { |
Kristof Umann | 015b059 | 2018-08-13 18:43:08 +0000 | [diff] [blame] | 88 | Out << getVariableName(getDecl()); |
| 89 | } |
| 90 | |
| 91 | virtual void printSeparator(llvm::raw_ostream &Out) const override { |
| 92 | Out << '.'; |
| 93 | } |
| 94 | }; |
| 95 | |
Kristof Umann | b59b45e | 2018-08-21 12:16:59 +0000 | [diff] [blame^] | 96 | /// Represents that the FieldNode that comes after this is declared in a base |
| 97 | /// of the previous FieldNode. |
| 98 | class BaseClass final : public FieldNode { |
| 99 | const QualType BaseClassT; |
| 100 | |
| 101 | public: |
| 102 | BaseClass(const QualType &T) : FieldNode(nullptr), BaseClassT(T) { |
| 103 | assert(!T.isNull()); |
| 104 | assert(T->getAsCXXRecordDecl()); |
| 105 | } |
| 106 | |
| 107 | virtual void printNoteMsg(llvm::raw_ostream &Out) const override { |
| 108 | llvm_unreachable("This node can never be the final node in the " |
| 109 | "fieldchain!"); |
| 110 | } |
| 111 | |
| 112 | virtual void printPrefix(llvm::raw_ostream &Out) const override {} |
| 113 | |
| 114 | virtual void printNode(llvm::raw_ostream &Out) const override { |
| 115 | Out << BaseClassT->getAsCXXRecordDecl()->getName() << "::"; |
| 116 | } |
| 117 | |
| 118 | virtual void printSeparator(llvm::raw_ostream &Out) const override {} |
| 119 | |
| 120 | virtual bool isBase() const { return true; } |
| 121 | }; |
| 122 | |
Kristof Umann | cc85244 | 2018-07-12 13:13:46 +0000 | [diff] [blame] | 123 | } // end of anonymous namespace |
| 124 | |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 125 | // Utility function declarations. |
| 126 | |
| 127 | /// Returns the object that was constructed by CtorDecl, or None if that isn't |
| 128 | /// possible. |
Kristof Umann | 0735cfb | 2018-08-08 12:23:02 +0000 | [diff] [blame] | 129 | // TODO: Refactor this function so that it returns the constructed object's |
| 130 | // region. |
Kristof Umann | cc85244 | 2018-07-12 13:13:46 +0000 | [diff] [blame] | 131 | static Optional<nonloc::LazyCompoundVal> |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 132 | getObjectVal(const CXXConstructorDecl *CtorDecl, CheckerContext &Context); |
| 133 | |
Kristof Umann | 0735cfb | 2018-08-08 12:23:02 +0000 | [diff] [blame] | 134 | /// Checks whether the object constructed by \p Ctor will be analyzed later |
| 135 | /// (e.g. if the object is a field of another object, in which case we'd check |
| 136 | /// it multiple times). |
| 137 | static bool willObjectBeAnalyzedLater(const CXXConstructorDecl *Ctor, |
Kristof Umann | a37bba4 | 2018-08-13 18:22:22 +0000 | [diff] [blame] | 138 | CheckerContext &Context); |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 139 | |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 140 | //===----------------------------------------------------------------------===// |
| 141 | // Methods for UninitializedObjectChecker. |
| 142 | //===----------------------------------------------------------------------===// |
| 143 | |
| 144 | void UninitializedObjectChecker::checkEndFunction( |
Reka Kovacs | ed8c05c | 2018-07-16 20:47:45 +0000 | [diff] [blame] | 145 | const ReturnStmt *RS, CheckerContext &Context) const { |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 146 | |
| 147 | const auto *CtorDecl = dyn_cast_or_null<CXXConstructorDecl>( |
| 148 | Context.getLocationContext()->getDecl()); |
| 149 | if (!CtorDecl) |
| 150 | return; |
| 151 | |
| 152 | if (!CtorDecl->isUserProvided()) |
| 153 | return; |
| 154 | |
| 155 | if (CtorDecl->getParent()->isUnion()) |
| 156 | return; |
| 157 | |
| 158 | // This avoids essentially the same error being reported multiple times. |
Kristof Umann | 0735cfb | 2018-08-08 12:23:02 +0000 | [diff] [blame] | 159 | if (willObjectBeAnalyzedLater(CtorDecl, Context)) |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 160 | return; |
| 161 | |
| 162 | Optional<nonloc::LazyCompoundVal> Object = getObjectVal(CtorDecl, Context); |
| 163 | if (!Object) |
| 164 | return; |
| 165 | |
Kristof Umann | 23ca966 | 2018-08-13 18:48:34 +0000 | [diff] [blame] | 166 | FindUninitializedFields F(Context.getState(), Object->getRegion(), |
Kristof Umann | a3f7b58 | 2018-08-07 12:55:26 +0000 | [diff] [blame] | 167 | CheckPointeeInitialization); |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 168 | |
Kristof Umann | 015b059 | 2018-08-13 18:43:08 +0000 | [diff] [blame] | 169 | const UninitFieldMap &UninitFields = F.getUninitFields(); |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 170 | |
| 171 | if (UninitFields.empty()) |
| 172 | return; |
| 173 | |
Kristof Umann | 23ca966 | 2018-08-13 18:48:34 +0000 | [diff] [blame] | 174 | // In non-pedantic mode, if Object's region doesn't contain a single |
| 175 | // initialized field, we'll assume that Object was intentionally left |
| 176 | // uninitialized. |
| 177 | if (!IsPedantic && !F.isAnyFieldInitialized()) |
| 178 | return; |
| 179 | |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 180 | // There are uninitialized fields in the record. |
| 181 | |
| 182 | ExplodedNode *Node = Context.generateNonFatalErrorNode(Context.getState()); |
| 183 | if (!Node) |
| 184 | return; |
| 185 | |
| 186 | PathDiagnosticLocation LocUsedForUniqueing; |
| 187 | const Stmt *CallSite = Context.getStackFrame()->getCallSite(); |
| 188 | if (CallSite) |
| 189 | LocUsedForUniqueing = PathDiagnosticLocation::createBegin( |
| 190 | CallSite, Context.getSourceManager(), Node->getLocationContext()); |
| 191 | |
Kristof Umann | 9bd4439 | 2018-06-29 11:25:24 +0000 | [diff] [blame] | 192 | // For Plist consumers that don't support notes just yet, we'll convert notes |
| 193 | // to warnings. |
| 194 | if (ShouldConvertNotesToWarnings) { |
Kristof Umann | 015b059 | 2018-08-13 18:43:08 +0000 | [diff] [blame] | 195 | for (const auto &Pair : UninitFields) { |
Kristof Umann | 9bd4439 | 2018-06-29 11:25:24 +0000 | [diff] [blame] | 196 | |
| 197 | auto Report = llvm::make_unique<BugReport>( |
Kristof Umann | 015b059 | 2018-08-13 18:43:08 +0000 | [diff] [blame] | 198 | *BT_uninitField, Pair.second, Node, LocUsedForUniqueing, |
Kristof Umann | 9bd4439 | 2018-06-29 11:25:24 +0000 | [diff] [blame] | 199 | Node->getLocationContext()->getDecl()); |
| 200 | Context.emitReport(std::move(Report)); |
| 201 | } |
| 202 | return; |
| 203 | } |
| 204 | |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 205 | SmallString<100> WarningBuf; |
| 206 | llvm::raw_svector_ostream WarningOS(WarningBuf); |
| 207 | WarningOS << UninitFields.size() << " uninitialized field" |
| 208 | << (UninitFields.size() == 1 ? "" : "s") |
| 209 | << " at the end of the constructor call"; |
| 210 | |
| 211 | auto Report = llvm::make_unique<BugReport>( |
| 212 | *BT_uninitField, WarningOS.str(), Node, LocUsedForUniqueing, |
| 213 | Node->getLocationContext()->getDecl()); |
| 214 | |
Kristof Umann | 015b059 | 2018-08-13 18:43:08 +0000 | [diff] [blame] | 215 | for (const auto &Pair : UninitFields) { |
| 216 | Report->addNote(Pair.second, |
| 217 | PathDiagnosticLocation::create(Pair.first->getDecl(), |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 218 | Context.getSourceManager())); |
| 219 | } |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 220 | Context.emitReport(std::move(Report)); |
| 221 | } |
| 222 | |
| 223 | //===----------------------------------------------------------------------===// |
| 224 | // Methods for FindUninitializedFields. |
| 225 | //===----------------------------------------------------------------------===// |
| 226 | |
| 227 | FindUninitializedFields::FindUninitializedFields( |
Kristof Umann | 23ca966 | 2018-08-13 18:48:34 +0000 | [diff] [blame] | 228 | ProgramStateRef State, const TypedValueRegion *const R, |
Kristof Umann | a3f7b58 | 2018-08-07 12:55:26 +0000 | [diff] [blame] | 229 | bool CheckPointeeInitialization) |
Kristof Umann | 23ca966 | 2018-08-13 18:48:34 +0000 | [diff] [blame] | 230 | : State(State), ObjectR(R), |
| 231 | CheckPointeeInitialization(CheckPointeeInitialization) { |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 232 | |
Kristof Umann | 015b059 | 2018-08-13 18:43:08 +0000 | [diff] [blame] | 233 | isNonUnionUninit(ObjectR, FieldChainInfo(ChainFactory)); |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | bool FindUninitializedFields::addFieldToUninits(FieldChainInfo Chain) { |
| 237 | if (State->getStateManager().getContext().getSourceManager().isInSystemHeader( |
Kristof Umann | 015b059 | 2018-08-13 18:43:08 +0000 | [diff] [blame] | 238 | Chain.getUninitRegion()->getDecl()->getLocation())) |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 239 | return false; |
| 240 | |
Kristof Umann | 015b059 | 2018-08-13 18:43:08 +0000 | [diff] [blame] | 241 | UninitFieldMap::mapped_type NoteMsgBuf; |
| 242 | llvm::raw_svector_ostream OS(NoteMsgBuf); |
| 243 | Chain.printNoteMsg(OS); |
| 244 | return UninitFields |
| 245 | .insert(std::make_pair(Chain.getUninitRegion(), std::move(NoteMsgBuf))) |
| 246 | .second; |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | bool FindUninitializedFields::isNonUnionUninit(const TypedValueRegion *R, |
| 250 | FieldChainInfo LocalChain) { |
| 251 | assert(R->getValueType()->isRecordType() && |
| 252 | !R->getValueType()->isUnionType() && |
| 253 | "This method only checks non-union record objects!"); |
| 254 | |
| 255 | const RecordDecl *RD = |
| 256 | R->getValueType()->getAs<RecordType>()->getDecl()->getDefinition(); |
| 257 | assert(RD && "Referred record has no definition"); |
| 258 | |
| 259 | bool ContainsUninitField = false; |
| 260 | |
| 261 | // Are all of this non-union's fields initialized? |
| 262 | for (const FieldDecl *I : RD->fields()) { |
| 263 | |
| 264 | const auto FieldVal = |
| 265 | State->getLValue(I, loc::MemRegionVal(R)).castAs<loc::MemRegionVal>(); |
| 266 | const auto *FR = FieldVal.getRegionAs<FieldRegion>(); |
| 267 | QualType T = I->getType(); |
| 268 | |
| 269 | // If LocalChain already contains FR, then we encountered a cyclic |
| 270 | // reference. In this case, region FR is already under checking at an |
| 271 | // earlier node in the directed tree. |
| 272 | if (LocalChain.contains(FR)) |
| 273 | return false; |
| 274 | |
| 275 | if (T->isStructureOrClassType()) { |
Kristof Umann | 015b059 | 2018-08-13 18:43:08 +0000 | [diff] [blame] | 276 | if (isNonUnionUninit(FR, LocalChain.add(RegularField(FR)))) |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 277 | ContainsUninitField = true; |
| 278 | continue; |
| 279 | } |
| 280 | |
| 281 | if (T->isUnionType()) { |
| 282 | if (isUnionUninit(FR)) { |
Kristof Umann | 015b059 | 2018-08-13 18:43:08 +0000 | [diff] [blame] | 283 | if (addFieldToUninits(LocalChain.add(RegularField(FR)))) |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 284 | ContainsUninitField = true; |
| 285 | } else |
| 286 | IsAnyFieldInitialized = true; |
| 287 | continue; |
| 288 | } |
| 289 | |
| 290 | if (T->isArrayType()) { |
| 291 | IsAnyFieldInitialized = true; |
| 292 | continue; |
| 293 | } |
| 294 | |
Kristof Umann | 6460196 | 2018-08-21 10:45:21 +0000 | [diff] [blame] | 295 | if (T->isAnyPointerType() || T->isReferenceType() || |
| 296 | T->isBlockPointerType()) { |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 297 | if (isPointerOrReferenceUninit(FR, LocalChain)) |
| 298 | ContainsUninitField = true; |
| 299 | continue; |
| 300 | } |
| 301 | |
Kristof Umann | 20e85ba | 2018-06-19 08:35:02 +0000 | [diff] [blame] | 302 | if (isPrimitiveType(T)) { |
| 303 | SVal V = State->getSVal(FieldVal); |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 304 | |
Kristof Umann | 20e85ba | 2018-06-19 08:35:02 +0000 | [diff] [blame] | 305 | if (isPrimitiveUninit(V)) { |
Kristof Umann | 015b059 | 2018-08-13 18:43:08 +0000 | [diff] [blame] | 306 | if (addFieldToUninits(LocalChain.add(RegularField(FR)))) |
Kristof Umann | 20e85ba | 2018-06-19 08:35:02 +0000 | [diff] [blame] | 307 | ContainsUninitField = true; |
| 308 | } |
| 309 | continue; |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 310 | } |
Kristof Umann | 20e85ba | 2018-06-19 08:35:02 +0000 | [diff] [blame] | 311 | |
| 312 | llvm_unreachable("All cases are handled!"); |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | // Checking bases. |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 316 | const auto *CXXRD = dyn_cast<CXXRecordDecl>(RD); |
| 317 | if (!CXXRD) |
| 318 | return ContainsUninitField; |
| 319 | |
| 320 | for (const CXXBaseSpecifier &BaseSpec : CXXRD->bases()) { |
| 321 | const auto *BaseRegion = State->getLValue(BaseSpec, R) |
| 322 | .castAs<loc::MemRegionVal>() |
| 323 | .getRegionAs<TypedValueRegion>(); |
| 324 | |
Kristof Umann | b59b45e | 2018-08-21 12:16:59 +0000 | [diff] [blame^] | 325 | // If the head of the list is also a BaseClass, we'll overwrite it to avoid |
| 326 | // note messages like 'this->A::B::x'. |
| 327 | if (!LocalChain.isEmpty() && LocalChain.getHead().isBase()) { |
| 328 | if (isNonUnionUninit(BaseRegion, LocalChain.replaceHead( |
| 329 | BaseClass(BaseSpec.getType())))) |
| 330 | ContainsUninitField = true; |
| 331 | } else { |
| 332 | if (isNonUnionUninit(BaseRegion, |
| 333 | LocalChain.add(BaseClass(BaseSpec.getType())))) |
| 334 | ContainsUninitField = true; |
| 335 | } |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | return ContainsUninitField; |
| 339 | } |
| 340 | |
| 341 | bool FindUninitializedFields::isUnionUninit(const TypedValueRegion *R) { |
| 342 | assert(R->getValueType()->isUnionType() && |
| 343 | "This method only checks union objects!"); |
| 344 | // TODO: Implement support for union fields. |
| 345 | return false; |
| 346 | } |
| 347 | |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 348 | bool FindUninitializedFields::isPrimitiveUninit(const SVal &V) { |
| 349 | if (V.isUndef()) |
| 350 | return true; |
| 351 | |
| 352 | IsAnyFieldInitialized = true; |
| 353 | return false; |
| 354 | } |
| 355 | |
| 356 | //===----------------------------------------------------------------------===// |
| 357 | // Methods for FieldChainInfo. |
| 358 | //===----------------------------------------------------------------------===// |
| 359 | |
Kristof Umann | 015b059 | 2018-08-13 18:43:08 +0000 | [diff] [blame] | 360 | const FieldRegion *FieldChainInfo::getUninitRegion() const { |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 361 | assert(!Chain.isEmpty() && "Empty fieldchain!"); |
Kristof Umann | 015b059 | 2018-08-13 18:43:08 +0000 | [diff] [blame] | 362 | return (*Chain.begin()).getRegion(); |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 363 | } |
| 364 | |
Kristof Umann | 015b059 | 2018-08-13 18:43:08 +0000 | [diff] [blame] | 365 | bool FieldChainInfo::contains(const FieldRegion *FR) const { |
| 366 | for (const FieldNode &Node : Chain) { |
| 367 | if (Node.isSameRegion(FR)) |
| 368 | return true; |
| 369 | } |
| 370 | return false; |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 371 | } |
| 372 | |
Kristof Umann | a37bba4 | 2018-08-13 18:22:22 +0000 | [diff] [blame] | 373 | /// Prints every element except the last to `Out`. Since ImmutableLists store |
| 374 | /// elements in reverse order, and have no reverse iterators, we use a |
| 375 | /// recursive function to print the fieldchain correctly. The last element in |
| 376 | /// the chain is to be printed by `print`. |
| 377 | static void printTail(llvm::raw_ostream &Out, |
| 378 | const FieldChainInfo::FieldChainImpl *L); |
| 379 | |
Kristof Umann | ef9af05 | 2018-08-08 13:18:53 +0000 | [diff] [blame] | 380 | // TODO: This function constructs an incorrect string if a void pointer is a |
| 381 | // part of the chain: |
| 382 | // |
| 383 | // struct B { int x; } |
| 384 | // |
| 385 | // struct A { |
| 386 | // void *vptr; |
| 387 | // A(void* vptr) : vptr(vptr) {} |
| 388 | // }; |
| 389 | // |
| 390 | // void f() { |
| 391 | // B b; |
| 392 | // A a(&b); |
| 393 | // } |
| 394 | // |
| 395 | // The note message will be "uninitialized field 'this->vptr->x'", even though |
| 396 | // void pointers can't be dereferenced. This should be changed to "uninitialized |
| 397 | // field 'static_cast<B*>(this->vptr)->x'". |
| 398 | // |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 399 | // TODO: This function constructs an incorrect fieldchain string in the |
| 400 | // following case: |
| 401 | // |
| 402 | // struct Base { int x; }; |
| 403 | // struct D1 : Base {}; struct D2 : Base {}; |
| 404 | // |
| 405 | // struct MostDerived : D1, D2 { |
| 406 | // MostDerived() {} |
| 407 | // } |
| 408 | // |
| 409 | // A call to MostDerived::MostDerived() will cause two notes that say |
| 410 | // "uninitialized field 'this->x'", but we can't refer to 'x' directly, |
| 411 | // we need an explicit namespace resolution whether the uninit field was |
| 412 | // 'D1::x' or 'D2::x'. |
Kristof Umann | 015b059 | 2018-08-13 18:43:08 +0000 | [diff] [blame] | 413 | void FieldChainInfo::printNoteMsg(llvm::raw_ostream &Out) const { |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 414 | if (Chain.isEmpty()) |
| 415 | return; |
| 416 | |
Kristof Umann | a37bba4 | 2018-08-13 18:22:22 +0000 | [diff] [blame] | 417 | const FieldChainImpl *L = Chain.getInternalPointer(); |
Kristof Umann | 015b059 | 2018-08-13 18:43:08 +0000 | [diff] [blame] | 418 | const FieldNode &LastField = L->getHead(); |
| 419 | |
| 420 | LastField.printNoteMsg(Out); |
| 421 | Out << '\''; |
| 422 | |
| 423 | for (const FieldNode &Node : Chain) |
| 424 | Node.printPrefix(Out); |
| 425 | |
| 426 | Out << "this->"; |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 427 | printTail(Out, L->getTail()); |
Kristof Umann | 015b059 | 2018-08-13 18:43:08 +0000 | [diff] [blame] | 428 | LastField.printNode(Out); |
| 429 | Out << '\''; |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 430 | } |
| 431 | |
Kristof Umann | a37bba4 | 2018-08-13 18:22:22 +0000 | [diff] [blame] | 432 | static void printTail(llvm::raw_ostream &Out, |
| 433 | const FieldChainInfo::FieldChainImpl *L) { |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 434 | if (!L) |
| 435 | return; |
| 436 | |
| 437 | printTail(Out, L->getTail()); |
Kristof Umann | 015b059 | 2018-08-13 18:43:08 +0000 | [diff] [blame] | 438 | |
| 439 | L->getHead().printNode(Out); |
| 440 | L->getHead().printSeparator(Out); |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | //===----------------------------------------------------------------------===// |
| 444 | // Utility functions. |
| 445 | //===----------------------------------------------------------------------===// |
| 446 | |
Kristof Umann | cc85244 | 2018-07-12 13:13:46 +0000 | [diff] [blame] | 447 | static Optional<nonloc::LazyCompoundVal> |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 448 | getObjectVal(const CXXConstructorDecl *CtorDecl, CheckerContext &Context) { |
| 449 | |
| 450 | Loc ThisLoc = Context.getSValBuilder().getCXXThis(CtorDecl->getParent(), |
| 451 | Context.getStackFrame()); |
| 452 | // Getting the value for 'this'. |
| 453 | SVal This = Context.getState()->getSVal(ThisLoc); |
| 454 | |
| 455 | // Getting the value for '*this'. |
| 456 | SVal Object = Context.getState()->getSVal(This.castAs<Loc>()); |
| 457 | |
| 458 | return Object.getAs<nonloc::LazyCompoundVal>(); |
| 459 | } |
| 460 | |
Kristof Umann | 0735cfb | 2018-08-08 12:23:02 +0000 | [diff] [blame] | 461 | static bool willObjectBeAnalyzedLater(const CXXConstructorDecl *Ctor, |
Kristof Umann | a37bba4 | 2018-08-13 18:22:22 +0000 | [diff] [blame] | 462 | CheckerContext &Context) { |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 463 | |
Kristof Umann | 0735cfb | 2018-08-08 12:23:02 +0000 | [diff] [blame] | 464 | Optional<nonloc::LazyCompoundVal> CurrentObject = getObjectVal(Ctor, Context); |
| 465 | if (!CurrentObject) |
| 466 | return false; |
| 467 | |
| 468 | const LocationContext *LC = Context.getLocationContext(); |
| 469 | while ((LC = LC->getParent())) { |
| 470 | |
| 471 | // If \p Ctor was called by another constructor. |
| 472 | const auto *OtherCtor = dyn_cast<CXXConstructorDecl>(LC->getDecl()); |
| 473 | if (!OtherCtor) |
| 474 | continue; |
| 475 | |
| 476 | Optional<nonloc::LazyCompoundVal> OtherObject = |
| 477 | getObjectVal(OtherCtor, Context); |
| 478 | if (!OtherObject) |
| 479 | continue; |
| 480 | |
| 481 | // If the CurrentObject is a subregion of OtherObject, it will be analyzed |
| 482 | // during the analysis of OtherObject. |
| 483 | if (CurrentObject->getRegion()->isSubRegionOf(OtherObject->getRegion())) |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 484 | return true; |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 485 | } |
Kristof Umann | 0735cfb | 2018-08-08 12:23:02 +0000 | [diff] [blame] | 486 | |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 487 | return false; |
| 488 | } |
| 489 | |
Kristof Umann | 015b059 | 2018-08-13 18:43:08 +0000 | [diff] [blame] | 490 | StringRef clang::ento::getVariableName(const FieldDecl *Field) { |
Kristof Umann | 8c11909 | 2018-07-13 12:54:47 +0000 | [diff] [blame] | 491 | // If Field is a captured lambda variable, Field->getName() will return with |
| 492 | // an empty string. We can however acquire it's name from the lambda's |
| 493 | // captures. |
| 494 | const auto *CXXParent = dyn_cast<CXXRecordDecl>(Field->getParent()); |
| 495 | |
| 496 | if (CXXParent && CXXParent->isLambda()) { |
| 497 | assert(CXXParent->captures_begin()); |
| 498 | auto It = CXXParent->captures_begin() + Field->getFieldIndex(); |
| 499 | return It->getCapturedVar()->getName(); |
| 500 | } |
| 501 | |
| 502 | return Field->getName(); |
| 503 | } |
| 504 | |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 505 | void ento::registerUninitializedObjectChecker(CheckerManager &Mgr) { |
| 506 | auto Chk = Mgr.registerChecker<UninitializedObjectChecker>(); |
| 507 | Chk->IsPedantic = Mgr.getAnalyzerOptions().getBooleanOption( |
| 508 | "Pedantic", /*DefaultVal*/ false, Chk); |
Kristof Umann | 9bd4439 | 2018-06-29 11:25:24 +0000 | [diff] [blame] | 509 | Chk->ShouldConvertNotesToWarnings = Mgr.getAnalyzerOptions().getBooleanOption( |
| 510 | "NotesAsWarnings", /*DefaultVal*/ false, Chk); |
Kristof Umann | a3f7b58 | 2018-08-07 12:55:26 +0000 | [diff] [blame] | 511 | Chk->CheckPointeeInitialization = Mgr.getAnalyzerOptions().getBooleanOption( |
| 512 | "CheckPointeeInitialization", /*DefaultVal*/ false, Chk); |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 513 | } |