Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 1 | //===----- UninitializedObjectChecker.cpp ------------------------*- C++ -*-==// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file defines a checker that reports uninitialized fields in objects |
| 10 | // created after a constructor call. |
| 11 | // |
Kristof Umann | 6cec6c4 | 2018-09-14 09:39:26 +0000 | [diff] [blame] | 12 | // To read about command line options and how the checker works, refer to the |
| 13 | // top of the file and inline comments in UninitializedObject.h. |
Kristof Umann | 56963ae | 2018-08-13 18:17:05 +0000 | [diff] [blame] | 14 | // |
| 15 | // Some of the logic is implemented in UninitializedPointee.cpp, to reduce the |
| 16 | // complexity of this file. |
| 17 | // |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 18 | //===----------------------------------------------------------------------===// |
| 19 | |
Kristof Umann | 76a2150 | 2018-12-15 16:23:51 +0000 | [diff] [blame] | 20 | #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h" |
Kristof Umann | a37bba4 | 2018-08-13 18:22:22 +0000 | [diff] [blame] | 21 | #include "UninitializedObject.h" |
Kristof Umann | ffe93a1 | 2019-02-02 14:50:04 +0000 | [diff] [blame] | 22 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +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" |
Kristof Umann | ef9af05 | 2018-08-08 13:18:53 +0000 | [diff] [blame] | 26 | #include "clang/StaticAnalyzer/Core/PathSensitive/DynamicTypeMap.h" |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 27 | |
| 28 | using namespace clang; |
| 29 | using namespace clang::ento; |
Kristof Umann | ffe93a1 | 2019-02-02 14:50:04 +0000 | [diff] [blame] | 30 | using namespace clang::ast_matchers; |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 31 | |
Kristof Umann | 4ff7769 | 2018-11-18 11:34:10 +0000 | [diff] [blame] | 32 | /// We'll mark fields (and pointee of fields) that are confirmed to be |
| 33 | /// uninitialized as already analyzed. |
| 34 | REGISTER_SET_WITH_PROGRAMSTATE(AnalyzedRegions, const MemRegion *) |
| 35 | |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 36 | namespace { |
| 37 | |
Kristof Umann | 4ff7769 | 2018-11-18 11:34:10 +0000 | [diff] [blame] | 38 | class UninitializedObjectChecker |
| 39 | : public Checker<check::EndFunction, check::DeadSymbols> { |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 40 | std::unique_ptr<BuiltinBug> BT_uninitField; |
| 41 | |
| 42 | public: |
Kristof Umann | 6cec6c4 | 2018-09-14 09:39:26 +0000 | [diff] [blame] | 43 | // The fields of this struct will be initialized when registering the checker. |
| 44 | UninitObjCheckerOptions Opts; |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 45 | |
| 46 | UninitializedObjectChecker() |
| 47 | : BT_uninitField(new BuiltinBug(this, "Uninitialized fields")) {} |
Kristof Umann | 4ff7769 | 2018-11-18 11:34:10 +0000 | [diff] [blame] | 48 | |
Reka Kovacs | ed8c05c | 2018-07-16 20:47:45 +0000 | [diff] [blame] | 49 | void checkEndFunction(const ReturnStmt *RS, CheckerContext &C) const; |
Kristof Umann | 4ff7769 | 2018-11-18 11:34:10 +0000 | [diff] [blame] | 50 | void checkDeadSymbols(SymbolReaper &SR, CheckerContext &C) const; |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 51 | }; |
| 52 | |
Kristof Umann | 015b059 | 2018-08-13 18:43:08 +0000 | [diff] [blame] | 53 | /// A basic field type, that is not a pointer or a reference, it's dynamic and |
| 54 | /// static type is the same. |
Richard Smith | 651d683 | 2018-08-13 22:07:11 +0000 | [diff] [blame] | 55 | class RegularField final : public FieldNode { |
Kristof Umann | 015b059 | 2018-08-13 18:43:08 +0000 | [diff] [blame] | 56 | public: |
| 57 | RegularField(const FieldRegion *FR) : FieldNode(FR) {} |
| 58 | |
| 59 | virtual void printNoteMsg(llvm::raw_ostream &Out) const override { |
| 60 | Out << "uninitialized field "; |
| 61 | } |
| 62 | |
| 63 | virtual void printPrefix(llvm::raw_ostream &Out) const override {} |
| 64 | |
Richard Smith | 651d683 | 2018-08-13 22:07:11 +0000 | [diff] [blame] | 65 | virtual void printNode(llvm::raw_ostream &Out) const override { |
Kristof Umann | 015b059 | 2018-08-13 18:43:08 +0000 | [diff] [blame] | 66 | Out << getVariableName(getDecl()); |
| 67 | } |
| 68 | |
| 69 | virtual void printSeparator(llvm::raw_ostream &Out) const override { |
| 70 | Out << '.'; |
| 71 | } |
| 72 | }; |
| 73 | |
Kristof Umann | b59b45e | 2018-08-21 12:16:59 +0000 | [diff] [blame] | 74 | /// Represents that the FieldNode that comes after this is declared in a base |
Kristof Umann | ceb5f65 | 2018-09-14 09:07:40 +0000 | [diff] [blame] | 75 | /// of the previous FieldNode. As such, this descendant doesn't wrap a |
| 76 | /// FieldRegion, and is purely a tool to describe a relation between two other |
| 77 | /// FieldRegion wrapping descendants. |
Kristof Umann | b59b45e | 2018-08-21 12:16:59 +0000 | [diff] [blame] | 78 | class BaseClass final : public FieldNode { |
| 79 | const QualType BaseClassT; |
| 80 | |
| 81 | public: |
| 82 | BaseClass(const QualType &T) : FieldNode(nullptr), BaseClassT(T) { |
| 83 | assert(!T.isNull()); |
| 84 | assert(T->getAsCXXRecordDecl()); |
| 85 | } |
| 86 | |
| 87 | virtual void printNoteMsg(llvm::raw_ostream &Out) const override { |
| 88 | llvm_unreachable("This node can never be the final node in the " |
| 89 | "fieldchain!"); |
| 90 | } |
| 91 | |
| 92 | virtual void printPrefix(llvm::raw_ostream &Out) const override {} |
| 93 | |
| 94 | virtual void printNode(llvm::raw_ostream &Out) const override { |
| 95 | Out << BaseClassT->getAsCXXRecordDecl()->getName() << "::"; |
| 96 | } |
| 97 | |
| 98 | virtual void printSeparator(llvm::raw_ostream &Out) const override {} |
| 99 | |
Kristof Umann | 06209cb | 2018-08-21 15:09:22 +0000 | [diff] [blame] | 100 | virtual bool isBase() const override { return true; } |
Kristof Umann | b59b45e | 2018-08-21 12:16:59 +0000 | [diff] [blame] | 101 | }; |
| 102 | |
Kristof Umann | cc85244 | 2018-07-12 13:13:46 +0000 | [diff] [blame] | 103 | } // end of anonymous namespace |
| 104 | |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 105 | // Utility function declarations. |
| 106 | |
Kristof Umann | dbabdfa | 2018-10-21 23:30:01 +0000 | [diff] [blame] | 107 | /// Returns the region that was constructed by CtorDecl, or nullptr if that |
| 108 | /// isn't possible. |
| 109 | static const TypedValueRegion * |
| 110 | getConstructedRegion(const CXXConstructorDecl *CtorDecl, |
| 111 | CheckerContext &Context); |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 112 | |
Kristof Umann | 0735cfb | 2018-08-08 12:23:02 +0000 | [diff] [blame] | 113 | /// Checks whether the object constructed by \p Ctor will be analyzed later |
| 114 | /// (e.g. if the object is a field of another object, in which case we'd check |
| 115 | /// it multiple times). |
| 116 | static bool willObjectBeAnalyzedLater(const CXXConstructorDecl *Ctor, |
Kristof Umann | a37bba4 | 2018-08-13 18:22:22 +0000 | [diff] [blame] | 117 | CheckerContext &Context); |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 118 | |
Kristof Umann | d6145d9 | 2018-09-14 10:10:09 +0000 | [diff] [blame] | 119 | /// Checks whether RD contains a field with a name or type name that matches |
| 120 | /// \p Pattern. |
| 121 | static bool shouldIgnoreRecord(const RecordDecl *RD, StringRef Pattern); |
| 122 | |
Kristof Umann | ffe93a1 | 2019-02-02 14:50:04 +0000 | [diff] [blame] | 123 | /// Checks _syntactically_ whether it is possible to access FD from the record |
| 124 | /// that contains it without a preceding assert (even if that access happens |
| 125 | /// inside a method). This is mainly used for records that act like unions, like |
| 126 | /// having multiple bit fields, with only a fraction being properly initialized. |
| 127 | /// If these fields are properly guarded with asserts, this method returns |
| 128 | /// false. |
| 129 | /// |
| 130 | /// Since this check is done syntactically, this method could be inaccurate. |
| 131 | static bool hasUnguardedAccess(const FieldDecl *FD, ProgramStateRef State); |
| 132 | |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 133 | //===----------------------------------------------------------------------===// |
| 134 | // Methods for UninitializedObjectChecker. |
| 135 | //===----------------------------------------------------------------------===// |
| 136 | |
| 137 | void UninitializedObjectChecker::checkEndFunction( |
Reka Kovacs | ed8c05c | 2018-07-16 20:47:45 +0000 | [diff] [blame] | 138 | const ReturnStmt *RS, CheckerContext &Context) const { |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 139 | |
| 140 | const auto *CtorDecl = dyn_cast_or_null<CXXConstructorDecl>( |
| 141 | Context.getLocationContext()->getDecl()); |
| 142 | if (!CtorDecl) |
| 143 | return; |
| 144 | |
| 145 | if (!CtorDecl->isUserProvided()) |
| 146 | return; |
| 147 | |
| 148 | if (CtorDecl->getParent()->isUnion()) |
| 149 | return; |
| 150 | |
| 151 | // This avoids essentially the same error being reported multiple times. |
Kristof Umann | 0735cfb | 2018-08-08 12:23:02 +0000 | [diff] [blame] | 152 | if (willObjectBeAnalyzedLater(CtorDecl, Context)) |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 153 | return; |
| 154 | |
Kristof Umann | dbabdfa | 2018-10-21 23:30:01 +0000 | [diff] [blame] | 155 | const TypedValueRegion *R = getConstructedRegion(CtorDecl, Context); |
| 156 | if (!R) |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 157 | return; |
| 158 | |
Kristof Umann | dbabdfa | 2018-10-21 23:30:01 +0000 | [diff] [blame] | 159 | FindUninitializedFields F(Context.getState(), R, Opts); |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 160 | |
Kristof Umann | 4ff7769 | 2018-11-18 11:34:10 +0000 | [diff] [blame] | 161 | std::pair<ProgramStateRef, const UninitFieldMap &> UninitInfo = |
| 162 | F.getResults(); |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 163 | |
Kristof Umann | 4ff7769 | 2018-11-18 11:34:10 +0000 | [diff] [blame] | 164 | ProgramStateRef UpdatedState = UninitInfo.first; |
| 165 | const UninitFieldMap &UninitFields = UninitInfo.second; |
| 166 | |
| 167 | if (UninitFields.empty()) { |
| 168 | Context.addTransition(UpdatedState); |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 169 | return; |
Kristof Umann | 4ff7769 | 2018-11-18 11:34:10 +0000 | [diff] [blame] | 170 | } |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 171 | |
| 172 | // There are uninitialized fields in the record. |
| 173 | |
Kristof Umann | 4ff7769 | 2018-11-18 11:34:10 +0000 | [diff] [blame] | 174 | ExplodedNode *Node = Context.generateNonFatalErrorNode(UpdatedState); |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 175 | if (!Node) |
| 176 | return; |
| 177 | |
| 178 | PathDiagnosticLocation LocUsedForUniqueing; |
| 179 | const Stmt *CallSite = Context.getStackFrame()->getCallSite(); |
| 180 | if (CallSite) |
| 181 | LocUsedForUniqueing = PathDiagnosticLocation::createBegin( |
| 182 | CallSite, Context.getSourceManager(), Node->getLocationContext()); |
| 183 | |
Kristof Umann | 9bd4439 | 2018-06-29 11:25:24 +0000 | [diff] [blame] | 184 | // For Plist consumers that don't support notes just yet, we'll convert notes |
| 185 | // to warnings. |
Kristof Umann | 6cec6c4 | 2018-09-14 09:39:26 +0000 | [diff] [blame] | 186 | if (Opts.ShouldConvertNotesToWarnings) { |
Kristof Umann | 015b059 | 2018-08-13 18:43:08 +0000 | [diff] [blame] | 187 | for (const auto &Pair : UninitFields) { |
Kristof Umann | 9bd4439 | 2018-06-29 11:25:24 +0000 | [diff] [blame] | 188 | |
| 189 | auto Report = llvm::make_unique<BugReport>( |
Kristof Umann | 015b059 | 2018-08-13 18:43:08 +0000 | [diff] [blame] | 190 | *BT_uninitField, Pair.second, Node, LocUsedForUniqueing, |
Kristof Umann | 9bd4439 | 2018-06-29 11:25:24 +0000 | [diff] [blame] | 191 | Node->getLocationContext()->getDecl()); |
| 192 | Context.emitReport(std::move(Report)); |
| 193 | } |
| 194 | return; |
| 195 | } |
| 196 | |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 197 | SmallString<100> WarningBuf; |
| 198 | llvm::raw_svector_ostream WarningOS(WarningBuf); |
| 199 | WarningOS << UninitFields.size() << " uninitialized field" |
| 200 | << (UninitFields.size() == 1 ? "" : "s") |
| 201 | << " at the end of the constructor call"; |
| 202 | |
| 203 | auto Report = llvm::make_unique<BugReport>( |
| 204 | *BT_uninitField, WarningOS.str(), Node, LocUsedForUniqueing, |
| 205 | Node->getLocationContext()->getDecl()); |
| 206 | |
Kristof Umann | 015b059 | 2018-08-13 18:43:08 +0000 | [diff] [blame] | 207 | for (const auto &Pair : UninitFields) { |
| 208 | Report->addNote(Pair.second, |
| 209 | PathDiagnosticLocation::create(Pair.first->getDecl(), |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 210 | Context.getSourceManager())); |
| 211 | } |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 212 | Context.emitReport(std::move(Report)); |
| 213 | } |
| 214 | |
Kristof Umann | 4ff7769 | 2018-11-18 11:34:10 +0000 | [diff] [blame] | 215 | void UninitializedObjectChecker::checkDeadSymbols(SymbolReaper &SR, |
| 216 | CheckerContext &C) const { |
| 217 | ProgramStateRef State = C.getState(); |
| 218 | for (const MemRegion *R : State->get<AnalyzedRegions>()) { |
| 219 | if (!SR.isLiveRegion(R)) |
| 220 | State = State->remove<AnalyzedRegions>(R); |
| 221 | } |
| 222 | } |
| 223 | |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 224 | //===----------------------------------------------------------------------===// |
| 225 | // Methods for FindUninitializedFields. |
| 226 | //===----------------------------------------------------------------------===// |
| 227 | |
| 228 | FindUninitializedFields::FindUninitializedFields( |
Kristof Umann | 23ca966 | 2018-08-13 18:48:34 +0000 | [diff] [blame] | 229 | ProgramStateRef State, const TypedValueRegion *const R, |
Kristof Umann | 6cec6c4 | 2018-09-14 09:39:26 +0000 | [diff] [blame] | 230 | const UninitObjCheckerOptions &Opts) |
| 231 | : State(State), ObjectR(R), Opts(Opts) { |
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 | 6cec6c4 | 2018-09-14 09:39:26 +0000 | [diff] [blame] | 234 | |
| 235 | // In non-pedantic mode, if ObjectR doesn't contain a single initialized |
| 236 | // field, we'll assume that Object was intentionally left uninitialized. |
| 237 | if (!Opts.IsPedantic && !isAnyFieldInitialized()) |
| 238 | UninitFields.clear(); |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 239 | } |
| 240 | |
Kristof Umann | 4ff7769 | 2018-11-18 11:34:10 +0000 | [diff] [blame] | 241 | bool FindUninitializedFields::addFieldToUninits(FieldChainInfo Chain, |
| 242 | const MemRegion *PointeeR) { |
| 243 | const FieldRegion *FR = Chain.getUninitRegion(); |
| 244 | |
| 245 | assert((PointeeR || !isDereferencableType(FR->getDecl()->getType())) && |
| 246 | "One must also pass the pointee region as a parameter for " |
Raphael Isemann | b23ccec | 2018-12-10 12:37:46 +0000 | [diff] [blame] | 247 | "dereferenceable fields!"); |
Kristof Umann | 4ff7769 | 2018-11-18 11:34:10 +0000 | [diff] [blame] | 248 | |
Kristof Umann | ffe93a1 | 2019-02-02 14:50:04 +0000 | [diff] [blame] | 249 | if (State->getStateManager().getContext().getSourceManager().isInSystemHeader( |
| 250 | FR->getDecl()->getLocation())) |
| 251 | return false; |
| 252 | |
| 253 | if (Opts.IgnoreGuardedFields && !hasUnguardedAccess(FR->getDecl(), State)) |
| 254 | return false; |
| 255 | |
Kristof Umann | 4ff7769 | 2018-11-18 11:34:10 +0000 | [diff] [blame] | 256 | if (State->contains<AnalyzedRegions>(FR)) |
| 257 | return false; |
| 258 | |
| 259 | if (PointeeR) { |
| 260 | if (State->contains<AnalyzedRegions>(PointeeR)) { |
| 261 | return false; |
| 262 | } |
| 263 | State = State->add<AnalyzedRegions>(PointeeR); |
| 264 | } |
| 265 | |
| 266 | State = State->add<AnalyzedRegions>(FR); |
| 267 | |
Kristof Umann | 015b059 | 2018-08-13 18:43:08 +0000 | [diff] [blame] | 268 | UninitFieldMap::mapped_type NoteMsgBuf; |
| 269 | llvm::raw_svector_ostream OS(NoteMsgBuf); |
| 270 | Chain.printNoteMsg(OS); |
Kristof Umann | ffe93a1 | 2019-02-02 14:50:04 +0000 | [diff] [blame] | 271 | |
Kristof Umann | 4ff7769 | 2018-11-18 11:34:10 +0000 | [diff] [blame] | 272 | return UninitFields.insert({FR, std::move(NoteMsgBuf)}).second; |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | bool FindUninitializedFields::isNonUnionUninit(const TypedValueRegion *R, |
| 276 | FieldChainInfo LocalChain) { |
| 277 | assert(R->getValueType()->isRecordType() && |
| 278 | !R->getValueType()->isUnionType() && |
| 279 | "This method only checks non-union record objects!"); |
| 280 | |
Kristof Umann | f0dd101 | 2018-09-14 08:58:21 +0000 | [diff] [blame] | 281 | const RecordDecl *RD = R->getValueType()->getAsRecordDecl()->getDefinition(); |
| 282 | |
| 283 | if (!RD) { |
| 284 | IsAnyFieldInitialized = true; |
| 285 | return true; |
| 286 | } |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 287 | |
Kristof Umann | d6145d9 | 2018-09-14 10:10:09 +0000 | [diff] [blame] | 288 | if (!Opts.IgnoredRecordsWithFieldPattern.empty() && |
| 289 | shouldIgnoreRecord(RD, Opts.IgnoredRecordsWithFieldPattern)) { |
| 290 | IsAnyFieldInitialized = true; |
| 291 | return false; |
| 292 | } |
| 293 | |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 294 | bool ContainsUninitField = false; |
| 295 | |
| 296 | // Are all of this non-union's fields initialized? |
| 297 | for (const FieldDecl *I : RD->fields()) { |
| 298 | |
| 299 | const auto FieldVal = |
| 300 | State->getLValue(I, loc::MemRegionVal(R)).castAs<loc::MemRegionVal>(); |
| 301 | const auto *FR = FieldVal.getRegionAs<FieldRegion>(); |
| 302 | QualType T = I->getType(); |
| 303 | |
| 304 | // If LocalChain already contains FR, then we encountered a cyclic |
| 305 | // reference. In this case, region FR is already under checking at an |
| 306 | // earlier node in the directed tree. |
| 307 | if (LocalChain.contains(FR)) |
| 308 | return false; |
| 309 | |
| 310 | if (T->isStructureOrClassType()) { |
Kristof Umann | 015b059 | 2018-08-13 18:43:08 +0000 | [diff] [blame] | 311 | if (isNonUnionUninit(FR, LocalChain.add(RegularField(FR)))) |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 312 | ContainsUninitField = true; |
| 313 | continue; |
| 314 | } |
| 315 | |
| 316 | if (T->isUnionType()) { |
| 317 | if (isUnionUninit(FR)) { |
Kristof Umann | 015b059 | 2018-08-13 18:43:08 +0000 | [diff] [blame] | 318 | if (addFieldToUninits(LocalChain.add(RegularField(FR)))) |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 319 | ContainsUninitField = true; |
| 320 | } else |
| 321 | IsAnyFieldInitialized = true; |
| 322 | continue; |
| 323 | } |
| 324 | |
| 325 | if (T->isArrayType()) { |
| 326 | IsAnyFieldInitialized = true; |
| 327 | continue; |
| 328 | } |
| 329 | |
Kristof Umann | f051379 | 2018-09-14 10:18:26 +0000 | [diff] [blame] | 330 | SVal V = State->getSVal(FieldVal); |
| 331 | |
| 332 | if (isDereferencableType(T) || V.getAs<nonloc::LocAsInteger>()) { |
Kristof Umann | ceb5f65 | 2018-09-14 09:07:40 +0000 | [diff] [blame] | 333 | if (isDereferencableUninit(FR, LocalChain)) |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 334 | ContainsUninitField = true; |
| 335 | continue; |
| 336 | } |
| 337 | |
Kristof Umann | 20e85ba | 2018-06-19 08:35:02 +0000 | [diff] [blame] | 338 | if (isPrimitiveType(T)) { |
Kristof Umann | 20e85ba | 2018-06-19 08:35:02 +0000 | [diff] [blame] | 339 | if (isPrimitiveUninit(V)) { |
Kristof Umann | 015b059 | 2018-08-13 18:43:08 +0000 | [diff] [blame] | 340 | if (addFieldToUninits(LocalChain.add(RegularField(FR)))) |
Kristof Umann | 20e85ba | 2018-06-19 08:35:02 +0000 | [diff] [blame] | 341 | ContainsUninitField = true; |
| 342 | } |
| 343 | continue; |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 344 | } |
Kristof Umann | 20e85ba | 2018-06-19 08:35:02 +0000 | [diff] [blame] | 345 | |
| 346 | llvm_unreachable("All cases are handled!"); |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 347 | } |
| 348 | |
Kristof Umann | ceb5f65 | 2018-09-14 09:07:40 +0000 | [diff] [blame] | 349 | // Checking bases. The checker will regard inherited data members as direct |
| 350 | // fields. |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 351 | const auto *CXXRD = dyn_cast<CXXRecordDecl>(RD); |
| 352 | if (!CXXRD) |
| 353 | return ContainsUninitField; |
| 354 | |
| 355 | for (const CXXBaseSpecifier &BaseSpec : CXXRD->bases()) { |
| 356 | const auto *BaseRegion = State->getLValue(BaseSpec, R) |
| 357 | .castAs<loc::MemRegionVal>() |
| 358 | .getRegionAs<TypedValueRegion>(); |
| 359 | |
Kristof Umann | b59b45e | 2018-08-21 12:16:59 +0000 | [diff] [blame] | 360 | // If the head of the list is also a BaseClass, we'll overwrite it to avoid |
| 361 | // note messages like 'this->A::B::x'. |
| 362 | if (!LocalChain.isEmpty() && LocalChain.getHead().isBase()) { |
| 363 | if (isNonUnionUninit(BaseRegion, LocalChain.replaceHead( |
| 364 | BaseClass(BaseSpec.getType())))) |
| 365 | ContainsUninitField = true; |
| 366 | } else { |
| 367 | if (isNonUnionUninit(BaseRegion, |
| 368 | LocalChain.add(BaseClass(BaseSpec.getType())))) |
| 369 | ContainsUninitField = true; |
| 370 | } |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | return ContainsUninitField; |
| 374 | } |
| 375 | |
| 376 | bool FindUninitializedFields::isUnionUninit(const TypedValueRegion *R) { |
| 377 | assert(R->getValueType()->isUnionType() && |
| 378 | "This method only checks union objects!"); |
| 379 | // TODO: Implement support for union fields. |
| 380 | return false; |
| 381 | } |
| 382 | |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 383 | bool FindUninitializedFields::isPrimitiveUninit(const SVal &V) { |
| 384 | if (V.isUndef()) |
| 385 | return true; |
| 386 | |
| 387 | IsAnyFieldInitialized = true; |
| 388 | return false; |
| 389 | } |
| 390 | |
| 391 | //===----------------------------------------------------------------------===// |
| 392 | // Methods for FieldChainInfo. |
| 393 | //===----------------------------------------------------------------------===// |
| 394 | |
Kristof Umann | 015b059 | 2018-08-13 18:43:08 +0000 | [diff] [blame] | 395 | bool FieldChainInfo::contains(const FieldRegion *FR) const { |
| 396 | for (const FieldNode &Node : Chain) { |
| 397 | if (Node.isSameRegion(FR)) |
| 398 | return true; |
| 399 | } |
| 400 | return false; |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 401 | } |
| 402 | |
Kristof Umann | a37bba4 | 2018-08-13 18:22:22 +0000 | [diff] [blame] | 403 | /// Prints every element except the last to `Out`. Since ImmutableLists store |
| 404 | /// elements in reverse order, and have no reverse iterators, we use a |
| 405 | /// recursive function to print the fieldchain correctly. The last element in |
Kristof Umann | ceb5f65 | 2018-09-14 09:07:40 +0000 | [diff] [blame] | 406 | /// the chain is to be printed by `FieldChainInfo::print`. |
Kristof Umann | a37bba4 | 2018-08-13 18:22:22 +0000 | [diff] [blame] | 407 | static void printTail(llvm::raw_ostream &Out, |
Kristof Umann | 82eeca3 | 2018-09-23 09:16:27 +0000 | [diff] [blame] | 408 | const FieldChainInfo::FieldChain L); |
Kristof Umann | a37bba4 | 2018-08-13 18:22:22 +0000 | [diff] [blame] | 409 | |
Kristof Umann | ceb5f65 | 2018-09-14 09:07:40 +0000 | [diff] [blame] | 410 | // FIXME: This function constructs an incorrect string in the following case: |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 411 | // |
| 412 | // struct Base { int x; }; |
| 413 | // struct D1 : Base {}; struct D2 : Base {}; |
| 414 | // |
| 415 | // struct MostDerived : D1, D2 { |
| 416 | // MostDerived() {} |
| 417 | // } |
| 418 | // |
| 419 | // A call to MostDerived::MostDerived() will cause two notes that say |
| 420 | // "uninitialized field 'this->x'", but we can't refer to 'x' directly, |
| 421 | // we need an explicit namespace resolution whether the uninit field was |
| 422 | // 'D1::x' or 'D2::x'. |
Kristof Umann | 015b059 | 2018-08-13 18:43:08 +0000 | [diff] [blame] | 423 | void FieldChainInfo::printNoteMsg(llvm::raw_ostream &Out) const { |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 424 | if (Chain.isEmpty()) |
| 425 | return; |
| 426 | |
Kristof Umann | 82eeca3 | 2018-09-23 09:16:27 +0000 | [diff] [blame] | 427 | const FieldNode &LastField = getHead(); |
Kristof Umann | 015b059 | 2018-08-13 18:43:08 +0000 | [diff] [blame] | 428 | |
| 429 | LastField.printNoteMsg(Out); |
| 430 | Out << '\''; |
| 431 | |
| 432 | for (const FieldNode &Node : Chain) |
| 433 | Node.printPrefix(Out); |
| 434 | |
| 435 | Out << "this->"; |
Kristof Umann | 82eeca3 | 2018-09-23 09:16:27 +0000 | [diff] [blame] | 436 | printTail(Out, Chain.getTail()); |
Kristof Umann | 015b059 | 2018-08-13 18:43:08 +0000 | [diff] [blame] | 437 | LastField.printNode(Out); |
| 438 | Out << '\''; |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 439 | } |
| 440 | |
Kristof Umann | a37bba4 | 2018-08-13 18:22:22 +0000 | [diff] [blame] | 441 | static void printTail(llvm::raw_ostream &Out, |
Kristof Umann | 82eeca3 | 2018-09-23 09:16:27 +0000 | [diff] [blame] | 442 | const FieldChainInfo::FieldChain L) { |
| 443 | if (L.isEmpty()) |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 444 | return; |
| 445 | |
Kristof Umann | 82eeca3 | 2018-09-23 09:16:27 +0000 | [diff] [blame] | 446 | printTail(Out, L.getTail()); |
Kristof Umann | 015b059 | 2018-08-13 18:43:08 +0000 | [diff] [blame] | 447 | |
Kristof Umann | 82eeca3 | 2018-09-23 09:16:27 +0000 | [diff] [blame] | 448 | L.getHead().printNode(Out); |
| 449 | L.getHead().printSeparator(Out); |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | //===----------------------------------------------------------------------===// |
| 453 | // Utility functions. |
| 454 | //===----------------------------------------------------------------------===// |
| 455 | |
Kristof Umann | dbabdfa | 2018-10-21 23:30:01 +0000 | [diff] [blame] | 456 | static const TypedValueRegion * |
| 457 | getConstructedRegion(const CXXConstructorDecl *CtorDecl, |
| 458 | CheckerContext &Context) { |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 459 | |
Kristof Umann | ffe93a1 | 2019-02-02 14:50:04 +0000 | [diff] [blame] | 460 | Loc ThisLoc = |
| 461 | Context.getSValBuilder().getCXXThis(CtorDecl, Context.getStackFrame()); |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 462 | |
Kristof Umann | dbabdfa | 2018-10-21 23:30:01 +0000 | [diff] [blame] | 463 | SVal ObjectV = Context.getState()->getSVal(ThisLoc); |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 464 | |
Kristof Umann | dbabdfa | 2018-10-21 23:30:01 +0000 | [diff] [blame] | 465 | auto *R = ObjectV.getAsRegion()->getAs<TypedValueRegion>(); |
| 466 | if (R && !R->getValueType()->getAsCXXRecordDecl()) |
| 467 | return nullptr; |
| 468 | |
| 469 | return R; |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 470 | } |
| 471 | |
Kristof Umann | 0735cfb | 2018-08-08 12:23:02 +0000 | [diff] [blame] | 472 | static bool willObjectBeAnalyzedLater(const CXXConstructorDecl *Ctor, |
Kristof Umann | a37bba4 | 2018-08-13 18:22:22 +0000 | [diff] [blame] | 473 | CheckerContext &Context) { |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 474 | |
Kristof Umann | dbabdfa | 2018-10-21 23:30:01 +0000 | [diff] [blame] | 475 | const TypedValueRegion *CurrRegion = getConstructedRegion(Ctor, Context); |
| 476 | if (!CurrRegion) |
Kristof Umann | 0735cfb | 2018-08-08 12:23:02 +0000 | [diff] [blame] | 477 | return false; |
| 478 | |
| 479 | const LocationContext *LC = Context.getLocationContext(); |
| 480 | while ((LC = LC->getParent())) { |
| 481 | |
| 482 | // If \p Ctor was called by another constructor. |
| 483 | const auto *OtherCtor = dyn_cast<CXXConstructorDecl>(LC->getDecl()); |
| 484 | if (!OtherCtor) |
| 485 | continue; |
| 486 | |
Kristof Umann | dbabdfa | 2018-10-21 23:30:01 +0000 | [diff] [blame] | 487 | const TypedValueRegion *OtherRegion = |
| 488 | getConstructedRegion(OtherCtor, Context); |
| 489 | if (!OtherRegion) |
Kristof Umann | 0735cfb | 2018-08-08 12:23:02 +0000 | [diff] [blame] | 490 | continue; |
| 491 | |
Kristof Umann | dbabdfa | 2018-10-21 23:30:01 +0000 | [diff] [blame] | 492 | // If the CurrRegion is a subregion of OtherRegion, it will be analyzed |
| 493 | // during the analysis of OtherRegion. |
| 494 | if (CurrRegion->isSubRegionOf(OtherRegion)) |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 495 | return true; |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 496 | } |
Kristof Umann | 0735cfb | 2018-08-08 12:23:02 +0000 | [diff] [blame] | 497 | |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 498 | return false; |
| 499 | } |
| 500 | |
Kristof Umann | d6145d9 | 2018-09-14 10:10:09 +0000 | [diff] [blame] | 501 | static bool shouldIgnoreRecord(const RecordDecl *RD, StringRef Pattern) { |
| 502 | llvm::Regex R(Pattern); |
| 503 | |
| 504 | for (const FieldDecl *FD : RD->fields()) { |
| 505 | if (R.match(FD->getType().getAsString())) |
| 506 | return true; |
| 507 | if (R.match(FD->getName())) |
| 508 | return true; |
| 509 | } |
| 510 | |
| 511 | return false; |
| 512 | } |
| 513 | |
Kristof Umann | ffe93a1 | 2019-02-02 14:50:04 +0000 | [diff] [blame] | 514 | static const Stmt *getMethodBody(const CXXMethodDecl *M) { |
| 515 | if (isa<CXXConstructorDecl>(M)) |
| 516 | return nullptr; |
| 517 | |
| 518 | if (!M->isDefined()) |
| 519 | return nullptr; |
| 520 | |
| 521 | return M->getDefinition()->getBody(); |
| 522 | } |
| 523 | |
| 524 | static bool hasUnguardedAccess(const FieldDecl *FD, ProgramStateRef State) { |
| 525 | |
| 526 | if (FD->getAccess() == AccessSpecifier::AS_public) |
| 527 | return true; |
| 528 | |
| 529 | const auto *Parent = dyn_cast<CXXRecordDecl>(FD->getParent()); |
| 530 | |
| 531 | if (!Parent) |
| 532 | return true; |
| 533 | |
| 534 | Parent = Parent->getDefinition(); |
| 535 | assert(Parent && "The record's definition must be avaible if an uninitialized" |
| 536 | " field of it was found!"); |
| 537 | |
| 538 | ASTContext &AC = State->getStateManager().getContext(); |
| 539 | |
| 540 | auto FieldAccessM = memberExpr(hasDeclaration(equalsNode(FD))).bind("access"); |
| 541 | |
| 542 | auto AssertLikeM = callExpr(callee(functionDecl( |
| 543 | anyOf(hasName("exit"), hasName("panic"), hasName("error"), |
| 544 | hasName("Assert"), hasName("assert"), hasName("ziperr"), |
| 545 | hasName("assfail"), hasName("db_error"), hasName("__assert"), |
| 546 | hasName("__assert2"), hasName("_wassert"), hasName("__assert_rtn"), |
| 547 | hasName("__assert_fail"), hasName("dtrace_assfail"), |
| 548 | hasName("yy_fatal_error"), hasName("_XCAssertionFailureHandler"), |
| 549 | hasName("_DTAssertionFailureHandler"), |
| 550 | hasName("_TSAssertionFailureHandler"))))); |
| 551 | |
| 552 | auto NoReturnFuncM = callExpr(callee(functionDecl(isNoReturn()))); |
| 553 | |
| 554 | auto GuardM = |
| 555 | stmt(anyOf(ifStmt(), switchStmt(), conditionalOperator(), AssertLikeM, |
| 556 | NoReturnFuncM)) |
| 557 | .bind("guard"); |
| 558 | |
| 559 | for (const CXXMethodDecl *M : Parent->methods()) { |
| 560 | const Stmt *MethodBody = getMethodBody(M); |
| 561 | if (!MethodBody) |
| 562 | continue; |
| 563 | |
| 564 | auto Accesses = match(stmt(hasDescendant(FieldAccessM)), *MethodBody, AC); |
| 565 | if (Accesses.empty()) |
| 566 | continue; |
| 567 | const auto *FirstAccess = Accesses[0].getNodeAs<MemberExpr>("access"); |
| 568 | assert(FirstAccess); |
| 569 | |
| 570 | auto Guards = match(stmt(hasDescendant(GuardM)), *MethodBody, AC); |
| 571 | if (Guards.empty()) |
| 572 | return true; |
| 573 | const auto *FirstGuard = Guards[0].getNodeAs<Stmt>("guard"); |
| 574 | assert(FirstGuard); |
| 575 | |
| 576 | if (FirstAccess->getBeginLoc() < FirstGuard->getBeginLoc()) |
| 577 | return true; |
| 578 | } |
| 579 | |
| 580 | return false; |
| 581 | } |
| 582 | |
Kristof Umann | f0dd101 | 2018-09-14 08:58:21 +0000 | [diff] [blame] | 583 | std::string clang::ento::getVariableName(const FieldDecl *Field) { |
Kristof Umann | 8c11909 | 2018-07-13 12:54:47 +0000 | [diff] [blame] | 584 | // If Field is a captured lambda variable, Field->getName() will return with |
| 585 | // an empty string. We can however acquire it's name from the lambda's |
| 586 | // captures. |
| 587 | const auto *CXXParent = dyn_cast<CXXRecordDecl>(Field->getParent()); |
| 588 | |
| 589 | if (CXXParent && CXXParent->isLambda()) { |
| 590 | assert(CXXParent->captures_begin()); |
| 591 | auto It = CXXParent->captures_begin() + Field->getFieldIndex(); |
Kristof Umann | f0dd101 | 2018-09-14 08:58:21 +0000 | [diff] [blame] | 592 | |
| 593 | if (It->capturesVariable()) |
| 594 | return llvm::Twine("/*captured variable*/" + |
| 595 | It->getCapturedVar()->getName()) |
| 596 | .str(); |
| 597 | |
| 598 | if (It->capturesThis()) |
| 599 | return "/*'this' capture*/"; |
| 600 | |
| 601 | llvm_unreachable("No other capture type is expected!"); |
Kristof Umann | 8c11909 | 2018-07-13 12:54:47 +0000 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | return Field->getName(); |
| 605 | } |
| 606 | |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 607 | void ento::registerUninitializedObjectChecker(CheckerManager &Mgr) { |
| 608 | auto Chk = Mgr.registerChecker<UninitializedObjectChecker>(); |
Kristof Umann | ceb5f65 | 2018-09-14 09:07:40 +0000 | [diff] [blame] | 609 | |
Kristof Umann | 6cec6c4 | 2018-09-14 09:39:26 +0000 | [diff] [blame] | 610 | AnalyzerOptions &AnOpts = Mgr.getAnalyzerOptions(); |
| 611 | UninitObjCheckerOptions &ChOpts = Chk->Opts; |
| 612 | |
Kristof Umann | d6145d9 | 2018-09-14 10:10:09 +0000 | [diff] [blame] | 613 | ChOpts.IsPedantic = |
Kristof Umann | 088b1c9 | 2019-03-04 00:28:16 +0000 | [diff] [blame] | 614 | AnOpts.getCheckerBooleanOption(Chk, "Pedantic", /*DefaultVal*/ false); |
| 615 | ChOpts.ShouldConvertNotesToWarnings = AnOpts.getCheckerBooleanOption( |
| 616 | Chk, "NotesAsWarnings", /*DefaultVal*/ false); |
Kristof Umann | 0a1f91c | 2018-11-05 03:50:37 +0000 | [diff] [blame] | 617 | ChOpts.CheckPointeeInitialization = AnOpts.getCheckerBooleanOption( |
Kristof Umann | 088b1c9 | 2019-03-04 00:28:16 +0000 | [diff] [blame] | 618 | Chk, "CheckPointeeInitialization", /*DefaultVal*/ false); |
Kristof Umann | d6145d9 | 2018-09-14 10:10:09 +0000 | [diff] [blame] | 619 | ChOpts.IgnoredRecordsWithFieldPattern = |
Kristof Umann | 088b1c9 | 2019-03-04 00:28:16 +0000 | [diff] [blame] | 620 | AnOpts.getCheckerStringOption(Chk, "IgnoreRecordsWithField", |
| 621 | /*DefaultVal*/ ""); |
Kristof Umann | ffe93a1 | 2019-02-02 14:50:04 +0000 | [diff] [blame] | 622 | ChOpts.IgnoreGuardedFields = |
Kristof Umann | 088b1c9 | 2019-03-04 00:28:16 +0000 | [diff] [blame] | 623 | AnOpts.getCheckerBooleanOption(Chk, "IgnoreGuardedFields", |
| 624 | /*DefaultVal*/ false); |
Kristof Umann | 30f0865 | 2018-06-18 11:50:17 +0000 | [diff] [blame] | 625 | } |
Kristof Umann | 058a7a4 | 2019-01-26 14:23:08 +0000 | [diff] [blame] | 626 | |
| 627 | bool ento::shouldRegisterUninitializedObjectChecker(const LangOptions &LO) { |
| 628 | return true; |
| 629 | } |