Chris Lattner | be1a7a0 | 2008-03-15 23:59:48 +0000 | [diff] [blame] | 1 | // CFRefCount.cpp - Transfer functions for tracking simple values -*- C++ -*--// |
Ted Kremenek | 827f93b | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Gabor Greif | 2224fcb | 2008-03-06 10:40:09 +0000 | [diff] [blame] | 10 | // This file defines the methods for CFRefCount, which implements |
Ted Kremenek | 827f93b | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 11 | // a reference count checker for Core Foundation (Mac OS X). |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 15 | #include "GRSimpleVals.h" |
Ted Kremenek | 827f93b | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 16 | #include "clang/Analysis/PathSensitive/ValueState.h" |
Ted Kremenek | dd0126b | 2008-03-31 18:26:32 +0000 | [diff] [blame] | 17 | #include "clang/Analysis/PathDiagnostic.h" |
Ted Kremenek | 827f93b | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 18 | #include "clang/Analysis/LocalCheckers.h" |
Ted Kremenek | 10fe66d | 2008-04-09 01:10:13 +0000 | [diff] [blame] | 19 | #include "clang/Analysis/PathDiagnostic.h" |
| 20 | #include "clang/Analysis/PathSensitive/BugReporter.h" |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/DenseMap.h" |
| 22 | #include "llvm/ADT/FoldingSet.h" |
| 23 | #include "llvm/ADT/ImmutableMap.h" |
Ted Kremenek | 10fe66d | 2008-04-09 01:10:13 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Compiler.h" |
Ted Kremenek | 3b11f7a | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 25 | #include <ostream> |
Ted Kremenek | 827f93b | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 26 | |
| 27 | using namespace clang; |
| 28 | |
Ted Kremenek | 7d421f3 | 2008-04-09 23:49:11 +0000 | [diff] [blame] | 29 | //===----------------------------------------------------------------------===// |
| 30 | // Symbolic Evaluation of Reference Counting Logic |
| 31 | //===----------------------------------------------------------------------===// |
| 32 | |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 33 | namespace { |
| 34 | enum ArgEffect { IncRef, DecRef, DoNothing }; |
| 35 | typedef std::vector<ArgEffect> ArgEffects; |
| 36 | } |
Ted Kremenek | 827f93b | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 37 | |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 38 | namespace llvm { |
| 39 | template <> struct FoldingSetTrait<ArgEffects> { |
Ted Kremenek | a4c7429 | 2008-04-10 22:58:08 +0000 | [diff] [blame^] | 40 | static void Profile(const ArgEffects& X, FoldingSetNodeID& ID) { |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 41 | for (ArgEffects::const_iterator I = X.begin(), E = X.end(); I!= E; ++I) |
| 42 | ID.AddInteger((unsigned) *I); |
Ted Kremenek | a4c7429 | 2008-04-10 22:58:08 +0000 | [diff] [blame^] | 43 | } |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 44 | }; |
| 45 | } // end llvm namespace |
| 46 | |
| 47 | namespace { |
Ted Kremenek | 827f93b | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 48 | |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 49 | class RetEffect { |
| 50 | public: |
| 51 | enum Kind { Alias = 0x0, OwnedSymbol = 0x1, NotOwnedSymbol = 0x2 }; |
| 52 | |
| 53 | private: |
| 54 | unsigned Data; |
Ted Kremenek | a4c7429 | 2008-04-10 22:58:08 +0000 | [diff] [blame^] | 55 | RetEffect(Kind k, unsigned D) { Data = (D << 2) | (unsigned) k; } |
Ted Kremenek | 827f93b | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 56 | |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 57 | public: |
| 58 | |
| 59 | Kind getKind() const { return (Kind) (Data & 0x3); } |
| 60 | |
| 61 | unsigned getValue() const { |
| 62 | assert(getKind() == Alias); |
Ted Kremenek | a4c7429 | 2008-04-10 22:58:08 +0000 | [diff] [blame^] | 63 | return Data >> 2; |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 64 | } |
Ted Kremenek | 827f93b | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 65 | |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 66 | static RetEffect MakeAlias(unsigned Idx) { return RetEffect(Alias, Idx); } |
Ted Kremenek | 827f93b | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 67 | |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 68 | static RetEffect MakeOwned() { return RetEffect(OwnedSymbol, 0); } |
Ted Kremenek | 827f93b | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 69 | |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 70 | static RetEffect MakeNotOwned() { return RetEffect(NotOwnedSymbol, 0); } |
| 71 | |
| 72 | operator Kind() const { return getKind(); } |
| 73 | |
| 74 | void Profile(llvm::FoldingSetNodeID& ID) const { ID.AddInteger(Data); } |
| 75 | }; |
| 76 | |
| 77 | |
| 78 | class CFRefSummary : public llvm::FoldingSetNode { |
| 79 | ArgEffects* Args; |
| 80 | RetEffect Ret; |
| 81 | public: |
| 82 | |
| 83 | CFRefSummary(ArgEffects* A, RetEffect R) : Args(A), Ret(R) {} |
| 84 | |
| 85 | unsigned getNumArgs() const { return Args->size(); } |
| 86 | |
Ted Kremenek | 0d72157 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 87 | ArgEffect getArg(unsigned idx) const { |
| 88 | assert (idx < getNumArgs()); |
| 89 | return (*Args)[idx]; |
| 90 | } |
| 91 | |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 92 | RetEffect getRet() const { |
| 93 | return Ret; |
| 94 | } |
| 95 | |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 96 | typedef ArgEffects::const_iterator arg_iterator; |
| 97 | |
| 98 | arg_iterator begin_args() const { return Args->begin(); } |
| 99 | arg_iterator end_args() const { return Args->end(); } |
| 100 | |
| 101 | static void Profile(llvm::FoldingSetNodeID& ID, ArgEffects* A, RetEffect R) { |
| 102 | ID.AddPointer(A); |
| 103 | ID.Add(R); |
| 104 | } |
| 105 | |
| 106 | void Profile(llvm::FoldingSetNodeID& ID) const { |
| 107 | Profile(ID, Args, Ret); |
| 108 | } |
| 109 | }; |
| 110 | |
| 111 | |
| 112 | class CFRefSummaryManager { |
| 113 | typedef llvm::FoldingSet<llvm::FoldingSetNodeWrapper<ArgEffects> > AESetTy; |
| 114 | typedef llvm::FoldingSet<CFRefSummary> SummarySetTy; |
| 115 | typedef llvm::DenseMap<FunctionDecl*, CFRefSummary*> SummaryMapTy; |
| 116 | |
Ted Kremenek | a4c7429 | 2008-04-10 22:58:08 +0000 | [diff] [blame^] | 117 | ASTContext& Ctx; |
| 118 | SummarySetTy SummarySet; |
| 119 | SummaryMapTy SummaryMap; |
| 120 | AESetTy AESet; |
| 121 | llvm::BumpPtrAllocator BPAlloc; |
| 122 | ArgEffects ScratchArgs; |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 123 | |
| 124 | |
| 125 | ArgEffects* getArgEffects(); |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 126 | |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 127 | CFRefSummary* getCannedCFSummary(FunctionTypeProto* FT, bool isRetain); |
| 128 | |
| 129 | CFRefSummary* getCFSummary(FunctionDecl* FD, const char* FName); |
| 130 | |
| 131 | CFRefSummary* getCFSummaryCreateRule(FunctionTypeProto* FT); |
| 132 | CFRefSummary* getCFSummaryGetRule(FunctionTypeProto* FT); |
| 133 | |
| 134 | CFRefSummary* getPersistentSummary(ArgEffects* AE, RetEffect RE); |
| 135 | |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 136 | public: |
Ted Kremenek | a4c7429 | 2008-04-10 22:58:08 +0000 | [diff] [blame^] | 137 | CFRefSummaryManager(ASTContext& ctx) : Ctx(ctx) {} |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 138 | ~CFRefSummaryManager(); |
| 139 | |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 140 | CFRefSummary* getSummary(FunctionDecl* FD, ASTContext& Ctx); |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 141 | }; |
| 142 | |
| 143 | } // end anonymous namespace |
| 144 | |
| 145 | //===----------------------------------------------------------------------===// |
| 146 | // Implementation of checker data structures. |
| 147 | //===----------------------------------------------------------------------===// |
| 148 | |
| 149 | CFRefSummaryManager::~CFRefSummaryManager() { |
| 150 | |
| 151 | // FIXME: The ArgEffects could eventually be allocated from BPAlloc, |
| 152 | // mitigating the need to do explicit cleanup of the |
| 153 | // Argument-Effect summaries. |
| 154 | |
| 155 | for (AESetTy::iterator I = AESet.begin(), E = AESet.end(); I!=E; ++I) |
| 156 | I->getValue().~ArgEffects(); |
Ted Kremenek | 827f93b | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 157 | } |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 158 | |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 159 | ArgEffects* CFRefSummaryManager::getArgEffects() { |
| 160 | |
| 161 | assert (!ScratchArgs.empty()); |
| 162 | |
| 163 | llvm::FoldingSetNodeID profile; |
| 164 | profile.Add(ScratchArgs); |
| 165 | void* InsertPos; |
| 166 | |
| 167 | llvm::FoldingSetNodeWrapper<ArgEffects>* E = |
| 168 | AESet.FindNodeOrInsertPos(profile, InsertPos); |
| 169 | |
| 170 | if (E) { |
| 171 | ScratchArgs.clear(); |
| 172 | return &E->getValue(); |
| 173 | } |
| 174 | |
| 175 | E = (llvm::FoldingSetNodeWrapper<ArgEffects>*) |
| 176 | BPAlloc.Allocate<llvm::FoldingSetNodeWrapper<ArgEffects> >(); |
| 177 | |
| 178 | new (E) llvm::FoldingSetNodeWrapper<ArgEffects>(ScratchArgs); |
| 179 | AESet.InsertNode(E, InsertPos); |
| 180 | |
| 181 | ScratchArgs.clear(); |
| 182 | return &E->getValue(); |
| 183 | } |
| 184 | |
| 185 | CFRefSummary* CFRefSummaryManager::getPersistentSummary(ArgEffects* AE, |
| 186 | RetEffect RE) { |
| 187 | |
| 188 | llvm::FoldingSetNodeID profile; |
| 189 | CFRefSummary::Profile(profile, AE, RE); |
| 190 | void* InsertPos; |
| 191 | |
| 192 | CFRefSummary* Summ = SummarySet.FindNodeOrInsertPos(profile, InsertPos); |
| 193 | |
| 194 | if (Summ) |
| 195 | return Summ; |
| 196 | |
| 197 | Summ = (CFRefSummary*) BPAlloc.Allocate<CFRefSummary>(); |
| 198 | new (Summ) CFRefSummary(AE, RE); |
| 199 | SummarySet.InsertNode(Summ, InsertPos); |
| 200 | |
| 201 | return Summ; |
| 202 | } |
| 203 | |
| 204 | |
| 205 | CFRefSummary* CFRefSummaryManager::getSummary(FunctionDecl* FD, |
| 206 | ASTContext& Ctx) { |
| 207 | |
| 208 | SourceLocation Loc = FD->getLocation(); |
| 209 | |
| 210 | if (!Loc.isFileID()) |
| 211 | return NULL; |
Ted Kremenek | 827f93b | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 212 | |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 213 | { // Look into our cache of summaries to see if we have already computed |
| 214 | // a summary for this FunctionDecl. |
| 215 | |
| 216 | SummaryMapTy::iterator I = SummaryMap.find(FD); |
| 217 | |
| 218 | if (I != SummaryMap.end()) |
| 219 | return I->second; |
| 220 | } |
| 221 | |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 222 | #if 0 |
| 223 | SourceManager& SrcMgr = Ctx.getSourceManager(); |
| 224 | unsigned fid = Loc.getFileID(); |
| 225 | const FileEntry* FE = SrcMgr.getFileEntryForID(fid); |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 226 | |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 227 | if (!FE) |
| 228 | return NULL; |
| 229 | |
| 230 | const char* DirName = FE->getDir()->getName(); |
| 231 | assert (DirName); |
| 232 | assert (strlen(DirName) > 0); |
| 233 | |
| 234 | if (!strstr(DirName, "CoreFoundation")) { |
| 235 | SummaryMap[FD] = NULL; |
| 236 | return NULL; |
| 237 | } |
| 238 | #endif |
| 239 | |
| 240 | const char* FName = FD->getIdentifier()->getName(); |
| 241 | |
| 242 | if (FName[0] == 'C' && FName[1] == 'F') { |
| 243 | CFRefSummary* S = getCFSummary(FD, FName); |
| 244 | SummaryMap[FD] = S; |
| 245 | return S; |
| 246 | } |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 247 | |
| 248 | return NULL; |
Ted Kremenek | 827f93b | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 251 | CFRefSummary* CFRefSummaryManager::getCFSummary(FunctionDecl* FD, |
| 252 | const char* FName) { |
| 253 | |
| 254 | // For now, only generate summaries for functions that have a prototype. |
| 255 | |
| 256 | FunctionTypeProto* FT = |
| 257 | dyn_cast<FunctionTypeProto>(FD->getType().getTypePtr()); |
| 258 | |
| 259 | if (!FT) |
| 260 | return NULL; |
| 261 | |
| 262 | FName += 2; |
| 263 | |
| 264 | if (strcmp(FName, "Retain") == 0) |
| 265 | return getCannedCFSummary(FT, true); |
| 266 | |
| 267 | if (strcmp(FName, "Release") == 0) |
| 268 | return getCannedCFSummary(FT, false); |
| 269 | |
| 270 | assert (ScratchArgs.empty()); |
| 271 | bool usesCreateRule = false; |
| 272 | |
| 273 | if (strstr(FName, "Create")) |
| 274 | usesCreateRule = true; |
| 275 | |
| 276 | if (!usesCreateRule && strstr(FName, "Copy")) |
| 277 | usesCreateRule = true; |
| 278 | |
| 279 | if (usesCreateRule) |
| 280 | return getCFSummaryCreateRule(FT); |
| 281 | |
| 282 | if (strstr(FName, "Get")) |
| 283 | return getCFSummaryGetRule(FT); |
| 284 | |
| 285 | return NULL; |
| 286 | } |
| 287 | |
| 288 | CFRefSummary* CFRefSummaryManager::getCannedCFSummary(FunctionTypeProto* FT, |
| 289 | bool isRetain) { |
| 290 | |
| 291 | if (FT->getNumArgs() != 1) |
| 292 | return NULL; |
| 293 | |
| 294 | TypedefType* ArgT = dyn_cast<TypedefType>(FT->getArgType(0).getTypePtr()); |
| 295 | |
| 296 | if (!ArgT) |
| 297 | return NULL; |
| 298 | |
| 299 | // For CFRetain/CFRelease, the first (and only) argument is of type |
| 300 | // "CFTypeRef". |
| 301 | |
| 302 | const char* TDName = ArgT->getDecl()->getIdentifier()->getName(); |
| 303 | assert (TDName); |
| 304 | |
Ted Kremenek | a4c7429 | 2008-04-10 22:58:08 +0000 | [diff] [blame^] | 305 | if (strcmp("CFTypeRef", TDName) != 0) |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 306 | return NULL; |
| 307 | |
| 308 | if (!ArgT->isPointerType()) |
| 309 | return NULL; |
Ted Kremenek | a4c7429 | 2008-04-10 22:58:08 +0000 | [diff] [blame^] | 310 | |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 311 | QualType RetTy = FT->getResultType(); |
| 312 | |
Ted Kremenek | a4c7429 | 2008-04-10 22:58:08 +0000 | [diff] [blame^] | 313 | if (isRetain) { |
| 314 | // CFRetain: the return type should also be "CFTypeRef". |
| 315 | if (RetTy.getTypePtr() != ArgT) |
| 316 | return NULL; |
| 317 | } |
| 318 | else { |
| 319 | // CFRelease: the return type should be void. |
| 320 | |
| 321 | if (RetTy != Ctx.VoidTy) |
| 322 | return NULL; |
| 323 | } |
| 324 | |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 325 | // The function's interface checks out. Generate a canned summary. |
| 326 | |
| 327 | assert (ScratchArgs.empty()); |
| 328 | ScratchArgs.push_back(isRetain ? IncRef : DecRef); |
| 329 | |
| 330 | return getPersistentSummary(getArgEffects(), RetEffect::MakeAlias(0)); |
| 331 | } |
| 332 | |
| 333 | static bool isCFRefType(QualType T) { |
| 334 | |
| 335 | if (!T->isPointerType()) |
| 336 | return false; |
| 337 | |
| 338 | // Check the typedef for the name "CF" and the substring "Ref". |
| 339 | |
| 340 | TypedefType* TD = dyn_cast<TypedefType>(T.getTypePtr()); |
| 341 | |
| 342 | if (!TD) |
| 343 | return false; |
| 344 | |
| 345 | const char* TDName = TD->getDecl()->getIdentifier()->getName(); |
| 346 | assert (TDName); |
| 347 | |
| 348 | if (TDName[0] != 'C' || TDName[1] != 'F') |
| 349 | return false; |
| 350 | |
| 351 | if (strstr(TDName, "Ref") == 0) |
| 352 | return false; |
| 353 | |
| 354 | return true; |
| 355 | } |
| 356 | |
| 357 | |
| 358 | CFRefSummary* |
| 359 | CFRefSummaryManager::getCFSummaryCreateRule(FunctionTypeProto* FT) { |
| 360 | |
| 361 | if (!isCFRefType(FT->getResultType())) |
| 362 | return NULL; |
| 363 | |
| 364 | assert (ScratchArgs.empty()); |
| 365 | |
| 366 | // FIXME: Add special-cases for functions that retain/release. For now |
| 367 | // just handle the default case. |
| 368 | |
| 369 | for (unsigned i = 0, n = FT->getNumArgs(); i != n; ++i) |
| 370 | ScratchArgs.push_back(DoNothing); |
| 371 | |
| 372 | return getPersistentSummary(getArgEffects(), RetEffect::MakeOwned()); |
| 373 | } |
| 374 | |
| 375 | CFRefSummary* |
| 376 | CFRefSummaryManager::getCFSummaryGetRule(FunctionTypeProto* FT) { |
| 377 | |
| 378 | if (!isCFRefType(FT->getResultType())) |
| 379 | return NULL; |
| 380 | |
| 381 | assert (ScratchArgs.empty()); |
| 382 | |
| 383 | // FIXME: Add special-cases for functions that retain/release. For now |
| 384 | // just handle the default case. |
| 385 | |
| 386 | for (unsigned i = 0, n = FT->getNumArgs(); i != n; ++i) |
| 387 | ScratchArgs.push_back(DoNothing); |
| 388 | |
| 389 | return getPersistentSummary(getArgEffects(), RetEffect::MakeNotOwned()); |
| 390 | } |
| 391 | |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 392 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 7d421f3 | 2008-04-09 23:49:11 +0000 | [diff] [blame] | 393 | // Bug Descriptions. |
| 394 | //===----------------------------------------------------------------------===// |
| 395 | |
| 396 | namespace { |
| 397 | |
| 398 | class CFRefCount; |
| 399 | |
| 400 | class VISIBILITY_HIDDEN CFRefBug : public BugType { |
| 401 | protected: |
| 402 | CFRefCount& TF; |
| 403 | |
| 404 | public: |
| 405 | CFRefBug(CFRefCount& tf) : TF(tf) {} |
| 406 | }; |
| 407 | |
| 408 | class VISIBILITY_HIDDEN UseAfterRelease : public CFRefBug { |
| 409 | public: |
| 410 | UseAfterRelease(CFRefCount& tf) : CFRefBug(tf) {} |
| 411 | |
| 412 | virtual const char* getName() const { |
| 413 | return "(CoreFoundation) use-after-release"; |
| 414 | } |
| 415 | virtual const char* getDescription() const { |
| 416 | return "(CoreFoundation) Reference-counted object is used" |
| 417 | " after it is released."; |
| 418 | } |
| 419 | |
| 420 | virtual void EmitWarnings(BugReporter& BR); |
| 421 | |
| 422 | }; |
| 423 | |
| 424 | class VISIBILITY_HIDDEN BadRelease : public CFRefBug { |
| 425 | public: |
| 426 | BadRelease(CFRefCount& tf) : CFRefBug(tf) {} |
| 427 | |
| 428 | virtual const char* getName() const { |
| 429 | return "(CoreFoundation) release of non-owned object"; |
| 430 | } |
| 431 | virtual const char* getDescription() const { |
| 432 | return "Incorrect decrement of the reference count of a " |
| 433 | "CoreFoundation object:\n" |
| 434 | "The object is not owned at this point by the caller."; |
| 435 | } |
| 436 | |
| 437 | virtual void EmitWarnings(BugReporter& BR); |
| 438 | }; |
| 439 | |
| 440 | } // end anonymous namespace |
| 441 | |
| 442 | //===----------------------------------------------------------------------===// |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 443 | // Transfer functions. |
| 444 | //===----------------------------------------------------------------------===// |
| 445 | |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 446 | namespace { |
| 447 | |
Ted Kremenek | 7d421f3 | 2008-04-09 23:49:11 +0000 | [diff] [blame] | 448 | class VISIBILITY_HIDDEN RefVal { |
Ted Kremenek | 0d72157 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 449 | unsigned Data; |
| 450 | |
| 451 | RefVal(unsigned K, unsigned D) : Data((D << 3) | K) { |
Ted Kremenek | a4c7429 | 2008-04-10 22:58:08 +0000 | [diff] [blame^] | 452 | assert ((K & ~0x7) == 0x0); |
Ted Kremenek | 0d72157 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | RefVal(unsigned K) : Data(K) { |
Ted Kremenek | a4c7429 | 2008-04-10 22:58:08 +0000 | [diff] [blame^] | 456 | assert ((K & ~0x7) == 0x0); |
Ted Kremenek | 0d72157 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | public: |
| 460 | enum Kind { Owned = 0, AcqOwned = 1, NotOwned = 2, Released = 3, |
| 461 | ErrorUseAfterRelease = 4, ErrorReleaseNotOwned = 5 }; |
| 462 | |
| 463 | |
| 464 | Kind getKind() const { return (Kind) (Data & 0x5); } |
| 465 | |
| 466 | unsigned getCount() const { |
| 467 | assert (getKind() == Owned || getKind() == AcqOwned); |
| 468 | return Data >> 3; |
| 469 | } |
| 470 | |
Ted Kremenek | 1daa16c | 2008-03-11 18:14:09 +0000 | [diff] [blame] | 471 | static bool isError(Kind k) { return k >= ErrorUseAfterRelease; } |
| 472 | |
Ted Kremenek | 0d72157 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 473 | static RefVal makeOwned(unsigned Count) { return RefVal(Owned, Count); } |
| 474 | static RefVal makeAcqOwned(unsigned Count) { return RefVal(AcqOwned, Count); } |
| 475 | static RefVal makeNotOwned() { return RefVal(NotOwned); } |
| 476 | static RefVal makeReleased() { return RefVal(Released); } |
| 477 | static RefVal makeUseAfterRelease() { return RefVal(ErrorUseAfterRelease); } |
| 478 | static RefVal makeReleaseNotOwned() { return RefVal(ErrorReleaseNotOwned); } |
| 479 | |
| 480 | bool operator==(const RefVal& X) const { return Data == X.Data; } |
| 481 | void Profile(llvm::FoldingSetNodeID& ID) const { ID.AddInteger(Data); } |
Ted Kremenek | 3b11f7a | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 482 | |
| 483 | void print(std::ostream& Out) const; |
Ted Kremenek | 0d72157 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 484 | }; |
Ted Kremenek | 3b11f7a | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 485 | |
| 486 | void RefVal::print(std::ostream& Out) const { |
| 487 | switch (getKind()) { |
| 488 | default: assert(false); |
| 489 | case Owned: |
| 490 | Out << "Owned(" << getCount() << ")"; |
| 491 | break; |
| 492 | |
| 493 | case AcqOwned: |
| 494 | Out << "Acquired-Owned(" << getCount() << ")"; |
| 495 | break; |
| 496 | |
| 497 | case NotOwned: |
| 498 | Out << "Not-Owned"; |
| 499 | break; |
| 500 | |
| 501 | case Released: |
| 502 | Out << "Released"; |
| 503 | break; |
| 504 | |
| 505 | case ErrorUseAfterRelease: |
| 506 | Out << "Use-After-Release [ERROR]"; |
| 507 | break; |
| 508 | |
| 509 | case ErrorReleaseNotOwned: |
| 510 | Out << "Release of Not-Owned [ERROR]"; |
| 511 | break; |
| 512 | } |
| 513 | } |
Ted Kremenek | 0d72157 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 514 | |
Ted Kremenek | 7d421f3 | 2008-04-09 23:49:11 +0000 | [diff] [blame] | 515 | class VISIBILITY_HIDDEN CFRefCount : public GRSimpleVals { |
Ted Kremenek | 3b11f7a | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 516 | |
| 517 | // Type definitions. |
| 518 | |
Ted Kremenek | 0d72157 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 519 | typedef llvm::ImmutableMap<SymbolID, RefVal> RefBindings; |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 520 | typedef RefBindings::Factory RefBFactoryTy; |
Ted Kremenek | 1daa16c | 2008-03-11 18:14:09 +0000 | [diff] [blame] | 521 | |
| 522 | typedef llvm::SmallPtrSet<GRExprEngine::NodeTy*,2> UseAfterReleasesTy; |
| 523 | typedef llvm::SmallPtrSet<GRExprEngine::NodeTy*,2> ReleasesNotOwnedTy; |
| 524 | |
Ted Kremenek | 3b11f7a | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 525 | class BindingsPrinter : public ValueState::CheckerStatePrinter { |
| 526 | public: |
| 527 | virtual void PrintCheckerState(std::ostream& Out, void* State, |
| 528 | const char* nl, const char* sep); |
| 529 | }; |
| 530 | |
| 531 | // Instance variables. |
| 532 | |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 533 | CFRefSummaryManager Summaries; |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 534 | RefBFactoryTy RefBFactory; |
| 535 | |
Ted Kremenek | 1daa16c | 2008-03-11 18:14:09 +0000 | [diff] [blame] | 536 | UseAfterReleasesTy UseAfterReleases; |
| 537 | ReleasesNotOwnedTy ReleasesNotOwned; |
| 538 | |
Ted Kremenek | 3b11f7a | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 539 | BindingsPrinter Printer; |
| 540 | |
| 541 | // Private methods. |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 542 | |
| 543 | static RefBindings GetRefBindings(ValueState& StImpl) { |
| 544 | return RefBindings((RefBindings::TreeTy*) StImpl.CheckerState); |
| 545 | } |
| 546 | |
| 547 | static void SetRefBindings(ValueState& StImpl, RefBindings B) { |
| 548 | StImpl.CheckerState = B.getRoot(); |
| 549 | } |
| 550 | |
| 551 | RefBindings Remove(RefBindings B, SymbolID sym) { |
| 552 | return RefBFactory.Remove(B, sym); |
| 553 | } |
| 554 | |
Ted Kremenek | 0d72157 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 555 | RefBindings Update(RefBindings B, SymbolID sym, RefVal V, ArgEffect E, |
Ted Kremenek | 1daa16c | 2008-03-11 18:14:09 +0000 | [diff] [blame] | 556 | RefVal::Kind& hasError); |
Ted Kremenek | 3b11f7a | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 557 | |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 558 | |
| 559 | public: |
Ted Kremenek | a4c7429 | 2008-04-10 22:58:08 +0000 | [diff] [blame^] | 560 | CFRefCount(ASTContext& Ctx) : Summaries(Ctx) {} |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 561 | virtual ~CFRefCount() {} |
Ted Kremenek | 7d421f3 | 2008-04-09 23:49:11 +0000 | [diff] [blame] | 562 | |
| 563 | virtual void RegisterChecks(GRExprEngine& Eng); |
Ted Kremenek | 3b11f7a | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 564 | |
| 565 | virtual ValueState::CheckerStatePrinter* getCheckerStatePrinter() { |
| 566 | return &Printer; |
| 567 | } |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 568 | |
| 569 | // Calls. |
| 570 | |
| 571 | virtual void EvalCall(ExplodedNodeSet<ValueState>& Dst, |
Ted Kremenek | ce0767f | 2008-03-12 21:06:49 +0000 | [diff] [blame] | 572 | GRExprEngine& Eng, |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 573 | GRStmtNodeBuilder<ValueState>& Builder, |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 574 | CallExpr* CE, LVal L, |
| 575 | ExplodedNode<ValueState>* Pred); |
Ted Kremenek | 10fe66d | 2008-04-09 01:10:13 +0000 | [diff] [blame] | 576 | |
| 577 | // Error iterators. |
| 578 | |
| 579 | typedef UseAfterReleasesTy::iterator use_after_iterator; |
| 580 | typedef ReleasesNotOwnedTy::iterator bad_release_iterator; |
| 581 | |
Ted Kremenek | 7d421f3 | 2008-04-09 23:49:11 +0000 | [diff] [blame] | 582 | use_after_iterator use_after_begin() { return UseAfterReleases.begin(); } |
| 583 | use_after_iterator use_after_end() { return UseAfterReleases.end(); } |
Ted Kremenek | 10fe66d | 2008-04-09 01:10:13 +0000 | [diff] [blame] | 584 | |
Ted Kremenek | 7d421f3 | 2008-04-09 23:49:11 +0000 | [diff] [blame] | 585 | bad_release_iterator bad_release_begin() { return ReleasesNotOwned.begin(); } |
| 586 | bad_release_iterator bad_release_end() { return ReleasesNotOwned.end(); } |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 587 | }; |
| 588 | |
| 589 | } // end anonymous namespace |
| 590 | |
Ted Kremenek | 7d421f3 | 2008-04-09 23:49:11 +0000 | [diff] [blame] | 591 | void CFRefCount::RegisterChecks(GRExprEngine& Eng) { |
| 592 | GRSimpleVals::RegisterChecks(Eng); |
| 593 | Eng.Register(new UseAfterRelease(*this)); |
| 594 | Eng.Register(new BadRelease(*this)); |
| 595 | } |
| 596 | |
| 597 | |
Ted Kremenek | 3b11f7a | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 598 | void CFRefCount::BindingsPrinter::PrintCheckerState(std::ostream& Out, |
| 599 | void* State, const char* nl, |
| 600 | const char* sep) { |
| 601 | RefBindings B((RefBindings::TreeTy*) State); |
| 602 | |
| 603 | if (State) |
| 604 | Out << sep << nl; |
| 605 | |
| 606 | for (RefBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I) { |
| 607 | Out << (*I).first << " : "; |
| 608 | (*I).second.print(Out); |
| 609 | Out << nl; |
| 610 | } |
| 611 | } |
| 612 | |
Ted Kremenek | 827f93b | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 613 | void CFRefCount::EvalCall(ExplodedNodeSet<ValueState>& Dst, |
Ted Kremenek | ce0767f | 2008-03-12 21:06:49 +0000 | [diff] [blame] | 614 | GRExprEngine& Eng, |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 615 | GRStmtNodeBuilder<ValueState>& Builder, |
| 616 | CallExpr* CE, LVal L, |
| 617 | ExplodedNode<ValueState>* Pred) { |
| 618 | |
Ted Kremenek | ce0767f | 2008-03-12 21:06:49 +0000 | [diff] [blame] | 619 | ValueStateManager& StateMgr = Eng.getStateManager(); |
Ted Kremenek | 827f93b | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 620 | |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 621 | // FIXME: Support calls to things other than lval::FuncVal. At the very |
| 622 | // least we should stop tracking ref-state for ref-counted objects passed |
| 623 | // to these functions. |
Ted Kremenek | 827f93b | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 624 | |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 625 | assert (isa<lval::FuncVal>(L) && "Not yet implemented."); |
| 626 | |
| 627 | // Get the summary. |
Ted Kremenek | 827f93b | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 628 | |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 629 | lval::FuncVal FV = cast<lval::FuncVal>(L); |
| 630 | FunctionDecl* FD = FV.getDecl(); |
Ted Kremenek | ce0767f | 2008-03-12 21:06:49 +0000 | [diff] [blame] | 631 | CFRefSummary* Summ = Summaries.getSummary(FD, Eng.getContext()); |
Ted Kremenek | 827f93b | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 632 | |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 633 | // Get the state. |
| 634 | |
| 635 | ValueState* St = Builder.GetState(Pred); |
| 636 | |
| 637 | // Evaluate the effects of the call. |
| 638 | |
| 639 | ValueState StVals = *St; |
Ted Kremenek | 1daa16c | 2008-03-11 18:14:09 +0000 | [diff] [blame] | 640 | RefVal::Kind hasError = (RefVal::Kind) 0; |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 641 | |
| 642 | if (!Summ) { |
Ted Kremenek | 827f93b | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 643 | |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 644 | // This function has no summary. Invalidate all reference-count state |
| 645 | // for arguments passed to this function, and also nuke the values of |
| 646 | // arguments passed-by-reference. |
| 647 | |
| 648 | ValueState StVals = *St; |
| 649 | |
| 650 | for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end(); |
| 651 | I != E; ++I) { |
| 652 | |
| 653 | RVal V = StateMgr.GetRVal(St, *I); |
| 654 | |
| 655 | if (isa<lval::SymbolVal>(V)) { |
| 656 | SymbolID Sym = cast<lval::SymbolVal>(V).getSymbol(); |
| 657 | RefBindings B = GetRefBindings(StVals); |
| 658 | SetRefBindings(StVals, Remove(B, Sym)); |
| 659 | } |
| 660 | |
| 661 | if (isa<LVal>(V)) |
| 662 | StateMgr.Unbind(StVals, cast<LVal>(V)); |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | St = StateMgr.getPersistentState(StVals); |
Ted Kremenek | ce0767f | 2008-03-12 21:06:49 +0000 | [diff] [blame] | 666 | |
| 667 | // Make up a symbol for the return value of this function. |
| 668 | |
| 669 | if (CE->getType() != Eng.getContext().VoidTy) { |
| 670 | unsigned Count = Builder.getCurrentBlockCount(); |
Ted Kremenek | d467651 | 2008-03-12 21:45:47 +0000 | [diff] [blame] | 671 | SymbolID Sym = Eng.getSymbolManager().getConjuredSymbol(CE, Count); |
Ted Kremenek | ce0767f | 2008-03-12 21:06:49 +0000 | [diff] [blame] | 672 | |
| 673 | RVal X = CE->getType()->isPointerType() |
| 674 | ? cast<RVal>(lval::SymbolVal(Sym)) |
| 675 | : cast<RVal>(nonlval::SymbolVal(Sym)); |
| 676 | |
| 677 | St = StateMgr.SetRVal(St, CE, X, Eng.getCFG().isBlkExpr(CE), false); |
| 678 | } |
| 679 | |
Ted Kremenek | f10f288 | 2008-03-21 21:30:14 +0000 | [diff] [blame] | 680 | Builder.MakeNode(Dst, CE, Pred, St); |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 681 | return; |
Ted Kremenek | 827f93b | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 682 | } |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 683 | |
| 684 | // This function has a summary. Evaluate the effect of the arguments. |
| 685 | |
| 686 | unsigned idx = 0; |
| 687 | |
| 688 | for (CallExpr::arg_iterator I=CE->arg_begin(), E=CE->arg_end(); |
| 689 | I!=E; ++I, ++idx) { |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 690 | |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 691 | RVal V = StateMgr.GetRVal(St, *I); |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 692 | |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 693 | if (isa<lval::SymbolVal>(V)) { |
| 694 | SymbolID Sym = cast<lval::SymbolVal>(V).getSymbol(); |
| 695 | RefBindings B = GetRefBindings(StVals); |
Ted Kremenek | 0d72157 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 696 | |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 697 | if (RefBindings::TreeTy* T = B.SlimFind(Sym)) { |
| 698 | B = Update(B, Sym, T->getValue().second, Summ->getArg(idx), hasError); |
| 699 | SetRefBindings(StVals, B); |
| 700 | if (hasError) break; |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 701 | } |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 702 | } |
| 703 | } |
| 704 | |
Ted Kremenek | 0d72157 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 705 | if (hasError) { |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 706 | St = StateMgr.getPersistentState(StVals); |
Ted Kremenek | 1daa16c | 2008-03-11 18:14:09 +0000 | [diff] [blame] | 707 | GRExprEngine::NodeTy* N = Builder.generateNode(CE, St, Pred); |
| 708 | |
| 709 | if (N) { |
| 710 | N->markAsSink(); |
| 711 | |
| 712 | switch (hasError) { |
| 713 | default: assert(false); |
| 714 | case RefVal::ErrorUseAfterRelease: |
| 715 | UseAfterReleases.insert(N); |
| 716 | break; |
| 717 | |
| 718 | case RefVal::ErrorReleaseNotOwned: |
| 719 | ReleasesNotOwned.insert(N); |
| 720 | break; |
| 721 | } |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | return; |
Ted Kremenek | 0d72157 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 725 | } |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 726 | |
| 727 | // Finally, consult the summary for the return value. |
| 728 | |
| 729 | RetEffect RE = Summ->getRet(); |
| 730 | St = StateMgr.getPersistentState(StVals); |
| 731 | |
| 732 | |
| 733 | switch (RE.getKind()) { |
| 734 | default: |
| 735 | assert (false && "Unhandled RetEffect."); break; |
| 736 | |
| 737 | case RetEffect::Alias: { |
| 738 | unsigned idx = RE.getValue(); |
| 739 | assert (idx < CE->getNumArgs()); |
| 740 | RVal V = StateMgr.GetRVal(St, CE->getArg(idx)); |
Ted Kremenek | ce0767f | 2008-03-12 21:06:49 +0000 | [diff] [blame] | 741 | St = StateMgr.SetRVal(St, CE, V, Eng.getCFG().isBlkExpr(CE), false); |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 742 | break; |
| 743 | } |
| 744 | |
| 745 | case RetEffect::OwnedSymbol: { |
| 746 | unsigned Count = Builder.getCurrentBlockCount(); |
Ted Kremenek | d467651 | 2008-03-12 21:45:47 +0000 | [diff] [blame] | 747 | SymbolID Sym = Eng.getSymbolManager().getConjuredSymbol(CE, Count); |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 748 | |
| 749 | ValueState StImpl = *St; |
| 750 | RefBindings B = GetRefBindings(StImpl); |
| 751 | SetRefBindings(StImpl, RefBFactory.Add(B, Sym, RefVal::makeOwned(1))); |
| 752 | |
| 753 | St = StateMgr.SetRVal(StateMgr.getPersistentState(StImpl), |
| 754 | CE, lval::SymbolVal(Sym), |
Ted Kremenek | ce0767f | 2008-03-12 21:06:49 +0000 | [diff] [blame] | 755 | Eng.getCFG().isBlkExpr(CE), false); |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 756 | |
| 757 | break; |
| 758 | } |
| 759 | |
| 760 | case RetEffect::NotOwnedSymbol: { |
| 761 | unsigned Count = Builder.getCurrentBlockCount(); |
Ted Kremenek | d467651 | 2008-03-12 21:45:47 +0000 | [diff] [blame] | 762 | SymbolID Sym = Eng.getSymbolManager().getConjuredSymbol(CE, Count); |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 763 | |
| 764 | ValueState StImpl = *St; |
| 765 | RefBindings B = GetRefBindings(StImpl); |
| 766 | SetRefBindings(StImpl, RefBFactory.Add(B, Sym, RefVal::makeNotOwned())); |
| 767 | |
| 768 | St = StateMgr.SetRVal(StateMgr.getPersistentState(StImpl), |
| 769 | CE, lval::SymbolVal(Sym), |
Ted Kremenek | ce0767f | 2008-03-12 21:06:49 +0000 | [diff] [blame] | 770 | Eng.getCFG().isBlkExpr(CE), false); |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 771 | |
| 772 | break; |
| 773 | } |
| 774 | } |
| 775 | |
Ted Kremenek | f10f288 | 2008-03-21 21:30:14 +0000 | [diff] [blame] | 776 | Builder.MakeNode(Dst, CE, Pred, St); |
Ted Kremenek | 827f93b | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 777 | } |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 778 | |
| 779 | |
| 780 | CFRefCount::RefBindings CFRefCount::Update(RefBindings B, SymbolID sym, |
Ted Kremenek | 0d72157 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 781 | RefVal V, ArgEffect E, |
Ted Kremenek | 1daa16c | 2008-03-11 18:14:09 +0000 | [diff] [blame] | 782 | RefVal::Kind& hasError) { |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 783 | |
Ted Kremenek | 0d72157 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 784 | // FIXME: This dispatch can potentially be sped up by unifiying it into |
| 785 | // a single switch statement. Opt for simplicity for now. |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 786 | |
Ted Kremenek | 0d72157 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 787 | switch (E) { |
| 788 | default: |
| 789 | assert (false && "Unhandled CFRef transition."); |
| 790 | |
| 791 | case DoNothing: |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 792 | if (V.getKind() == RefVal::Released) { |
| 793 | V = RefVal::makeUseAfterRelease(); |
| 794 | hasError = V.getKind(); |
| 795 | break; |
| 796 | } |
| 797 | |
Ted Kremenek | 0d72157 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 798 | return B; |
| 799 | |
| 800 | case IncRef: |
| 801 | switch (V.getKind()) { |
| 802 | default: |
| 803 | assert(false); |
| 804 | |
| 805 | case RefVal::Owned: |
| 806 | V = RefVal::makeOwned(V.getCount()+1); break; |
| 807 | |
| 808 | case RefVal::AcqOwned: |
| 809 | V = RefVal::makeAcqOwned(V.getCount()+1); |
| 810 | break; |
| 811 | |
| 812 | case RefVal::NotOwned: |
| 813 | V = RefVal::makeAcqOwned(1); |
| 814 | break; |
| 815 | |
| 816 | case RefVal::Released: |
Ted Kremenek | 0d72157 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 817 | V = RefVal::makeUseAfterRelease(); |
Ted Kremenek | 1daa16c | 2008-03-11 18:14:09 +0000 | [diff] [blame] | 818 | hasError = V.getKind(); |
Ted Kremenek | 0d72157 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 819 | break; |
| 820 | } |
| 821 | |
| 822 | case DecRef: |
| 823 | switch (V.getKind()) { |
| 824 | default: |
| 825 | assert (false); |
| 826 | |
| 827 | case RefVal::Owned: { |
| 828 | unsigned Count = V.getCount() - 1; |
| 829 | V = Count ? RefVal::makeOwned(Count) : RefVal::makeReleased(); |
| 830 | break; |
| 831 | } |
| 832 | |
| 833 | case RefVal::AcqOwned: { |
| 834 | unsigned Count = V.getCount() - 1; |
| 835 | V = Count ? RefVal::makeAcqOwned(Count) : RefVal::makeNotOwned(); |
| 836 | break; |
| 837 | } |
| 838 | |
| 839 | case RefVal::NotOwned: |
Ted Kremenek | 0d72157 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 840 | V = RefVal::makeReleaseNotOwned(); |
Ted Kremenek | 1daa16c | 2008-03-11 18:14:09 +0000 | [diff] [blame] | 841 | hasError = V.getKind(); |
Ted Kremenek | 0d72157 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 842 | break; |
| 843 | |
| 844 | case RefVal::Released: |
Ted Kremenek | 0d72157 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 845 | V = RefVal::makeUseAfterRelease(); |
Ted Kremenek | 1daa16c | 2008-03-11 18:14:09 +0000 | [diff] [blame] | 846 | hasError = V.getKind(); |
Ted Kremenek | 0d72157 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 847 | break; |
| 848 | } |
| 849 | } |
| 850 | |
| 851 | return RefBFactory.Add(B, sym, V); |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 852 | } |
| 853 | |
Ted Kremenek | 10fe66d | 2008-04-09 01:10:13 +0000 | [diff] [blame] | 854 | |
| 855 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 7d421f3 | 2008-04-09 23:49:11 +0000 | [diff] [blame] | 856 | // Error reporting. |
Ted Kremenek | 10fe66d | 2008-04-09 01:10:13 +0000 | [diff] [blame] | 857 | //===----------------------------------------------------------------------===// |
| 858 | |
Ted Kremenek | 7d421f3 | 2008-04-09 23:49:11 +0000 | [diff] [blame] | 859 | void UseAfterRelease::EmitWarnings(BugReporter& BR) { |
Ted Kremenek | 10fe66d | 2008-04-09 01:10:13 +0000 | [diff] [blame] | 860 | |
Ted Kremenek | 7d421f3 | 2008-04-09 23:49:11 +0000 | [diff] [blame] | 861 | for (CFRefCount::use_after_iterator I = TF.use_after_begin(), |
| 862 | E = TF.use_after_end(); I != E; ++I) { |
| 863 | |
| 864 | BugReport report(*this); |
| 865 | BR.EmitPathWarning(report, *I); |
Ted Kremenek | 10fe66d | 2008-04-09 01:10:13 +0000 | [diff] [blame] | 866 | } |
Ted Kremenek | 7d421f3 | 2008-04-09 23:49:11 +0000 | [diff] [blame] | 867 | } |
| 868 | |
| 869 | void BadRelease::EmitWarnings(BugReporter& BR) { |
Ted Kremenek | 10fe66d | 2008-04-09 01:10:13 +0000 | [diff] [blame] | 870 | |
Ted Kremenek | 7d421f3 | 2008-04-09 23:49:11 +0000 | [diff] [blame] | 871 | for (CFRefCount::bad_release_iterator I = TF.bad_release_begin(), |
| 872 | E = TF.bad_release_end(); I != E; ++I) { |
| 873 | |
| 874 | BugReport report(*this); |
| 875 | BR.EmitPathWarning(report, *I); |
| 876 | } |
| 877 | } |
Ted Kremenek | 10fe66d | 2008-04-09 01:10:13 +0000 | [diff] [blame] | 878 | |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 879 | //===----------------------------------------------------------------------===// |
Ted Kremenek | b1983ba | 2008-04-10 22:16:52 +0000 | [diff] [blame] | 880 | // Transfer function creation for external clients. |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 881 | //===----------------------------------------------------------------------===// |
| 882 | |
Ted Kremenek | a4c7429 | 2008-04-10 22:58:08 +0000 | [diff] [blame^] | 883 | GRTransferFuncs* clang::MakeCFRefCountTF(ASTContext& Ctx) { |
| 884 | return new CFRefCount(Ctx); |
| 885 | } |