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