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