blob: ff8b3e5e6973dc7e4c327a5a95a201beaed7e045 [file] [log] [blame]
Chris Lattnerbda0b622008-03-15 23:59:48 +00001// CFRefCount.cpp - Transfer functions for tracking simple values -*- C++ -*--//
Ted Kremenek2fff37e2008-03-06 00:08:09 +00002//
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 Greif843e9342008-03-06 10:40:09 +000010// This file defines the methods for CFRefCount, which implements
Ted Kremenek2fff37e2008-03-06 00:08:09 +000011// a reference count checker for Core Foundation (Mac OS X).
12//
13//===----------------------------------------------------------------------===//
14
Ted Kremenek6b3a0f72008-03-11 06:39:11 +000015#include "GRSimpleVals.h"
Ted Kremenek2fff37e2008-03-06 00:08:09 +000016#include "clang/Analysis/PathSensitive/ValueState.h"
Ted Kremenek4dc41cc2008-03-31 18:26:32 +000017#include "clang/Analysis/PathDiagnostic.h"
Ted Kremenek2fff37e2008-03-06 00:08:09 +000018#include "clang/Analysis/LocalCheckers.h"
Ted Kremenekfa34b332008-04-09 01:10:13 +000019#include "clang/Analysis/PathDiagnostic.h"
20#include "clang/Analysis/PathSensitive/BugReporter.h"
Ted Kremenek6b3a0f72008-03-11 06:39:11 +000021#include "llvm/ADT/DenseMap.h"
22#include "llvm/ADT/FoldingSet.h"
23#include "llvm/ADT/ImmutableMap.h"
Ted Kremenekfa34b332008-04-09 01:10:13 +000024#include "llvm/Support/Compiler.h"
Ted Kremenekf3948042008-03-11 19:44:10 +000025#include <ostream>
Ted Kremenek2fff37e2008-03-06 00:08:09 +000026
27using namespace clang;
28
Ted Kremenek05cbe1a2008-04-09 23:49:11 +000029//===----------------------------------------------------------------------===//
30// Symbolic Evaluation of Reference Counting Logic
31//===----------------------------------------------------------------------===//
32
Ted Kremenek6b3a0f72008-03-11 06:39:11 +000033namespace {
34 enum ArgEffect { IncRef, DecRef, DoNothing };
35 typedef std::vector<ArgEffect> ArgEffects;
36}
Ted Kremenek2fff37e2008-03-06 00:08:09 +000037
Ted Kremenek6b3a0f72008-03-11 06:39:11 +000038namespace llvm {
39 template <> struct FoldingSetTrait<ArgEffects> {
Ted Kremenek3ea0b6a2008-04-10 22:58:08 +000040 static void Profile(const ArgEffects& X, FoldingSetNodeID& ID) {
Ted Kremenek6b3a0f72008-03-11 06:39:11 +000041 for (ArgEffects::const_iterator I = X.begin(), E = X.end(); I!= E; ++I)
42 ID.AddInteger((unsigned) *I);
Ted Kremenek3ea0b6a2008-04-10 22:58:08 +000043 }
Ted Kremenek6b3a0f72008-03-11 06:39:11 +000044 };
45} // end llvm namespace
46
47namespace {
Ted Kremenek2fff37e2008-03-06 00:08:09 +000048
Ted Kremenek6b3a0f72008-03-11 06:39:11 +000049class RetEffect {
50public:
Ted Kremenek940b1d82008-04-10 23:44:06 +000051 enum Kind { NoRet = 0x0, Alias = 0x1, OwnedSymbol = 0x2,
52 NotOwnedSymbol = 0x3 };
Ted Kremenek6b3a0f72008-03-11 06:39:11 +000053
54private:
55 unsigned Data;
Ted Kremenek3ea0b6a2008-04-10 22:58:08 +000056 RetEffect(Kind k, unsigned D) { Data = (D << 2) | (unsigned) k; }
Ted Kremenek2fff37e2008-03-06 00:08:09 +000057
Ted Kremenek6b3a0f72008-03-11 06:39:11 +000058public:
59
60 Kind getKind() const { return (Kind) (Data & 0x3); }
61
62 unsigned getValue() const {
63 assert(getKind() == Alias);
Ted Kremenek3ea0b6a2008-04-10 22:58:08 +000064 return Data >> 2;
Ted Kremenek6b3a0f72008-03-11 06:39:11 +000065 }
Ted Kremeneke7bd9c22008-04-11 22:25:11 +000066
Ted Kremenek6b3a0f72008-03-11 06:39:11 +000067 static RetEffect MakeAlias(unsigned Idx) { return RetEffect(Alias, Idx); }
Ted Kremenek2fff37e2008-03-06 00:08:09 +000068
Ted Kremenek6b3a0f72008-03-11 06:39:11 +000069 static RetEffect MakeOwned() { return RetEffect(OwnedSymbol, 0); }
Ted Kremenek2fff37e2008-03-06 00:08:09 +000070
Ted Kremenek6b3a0f72008-03-11 06:39:11 +000071 static RetEffect MakeNotOwned() { return RetEffect(NotOwnedSymbol, 0); }
72
Ted Kremenek940b1d82008-04-10 23:44:06 +000073 static RetEffect MakeNoRet() { return RetEffect(NoRet, 0); }
74
Ted Kremenek6b3a0f72008-03-11 06:39:11 +000075 operator Kind() const { return getKind(); }
76
77 void Profile(llvm::FoldingSetNodeID& ID) const { ID.AddInteger(Data); }
78};
79
80
81class CFRefSummary : public llvm::FoldingSetNode {
82 ArgEffects* Args;
83 RetEffect Ret;
84public:
85
86 CFRefSummary(ArgEffects* A, RetEffect R) : Args(A), Ret(R) {}
87
88 unsigned getNumArgs() const { return Args->size(); }
89
Ted Kremenek1ac08d62008-03-11 17:48:22 +000090 ArgEffect getArg(unsigned idx) const {
91 assert (idx < getNumArgs());
92 return (*Args)[idx];
93 }
94
Ted Kremenek00a3a5f2008-03-12 01:21:45 +000095 RetEffect getRet() const {
96 return Ret;
97 }
98
Ted Kremenek6b3a0f72008-03-11 06:39:11 +000099 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
115class CFRefSummaryManager {
116 typedef llvm::FoldingSet<llvm::FoldingSetNodeWrapper<ArgEffects> > AESetTy;
117 typedef llvm::FoldingSet<CFRefSummary> SummarySetTy;
118 typedef llvm::DenseMap<FunctionDecl*, CFRefSummary*> SummaryMapTy;
119
Ted Kremenek3ea0b6a2008-04-10 22:58:08 +0000120 ASTContext& Ctx;
121 SummarySetTy SummarySet;
122 SummaryMapTy SummaryMap;
123 AESetTy AESet;
124 llvm::BumpPtrAllocator BPAlloc;
125 ArgEffects ScratchArgs;
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000126
127
128 ArgEffects* getArgEffects();
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000129
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000130 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 Kremenek940b1d82008-04-10 23:44:06 +0000139 void FillDoNothing(unsigned Args);
140
141
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000142public:
Ted Kremenek3ea0b6a2008-04-10 22:58:08 +0000143 CFRefSummaryManager(ASTContext& ctx) : Ctx(ctx) {}
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000144 ~CFRefSummaryManager();
145
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000146 CFRefSummary* getSummary(FunctionDecl* FD, ASTContext& Ctx);
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000147};
148
149} // end anonymous namespace
150
151//===----------------------------------------------------------------------===//
152// Implementation of checker data structures.
153//===----------------------------------------------------------------------===//
154
155CFRefSummaryManager::~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 Kremenek2fff37e2008-03-06 00:08:09 +0000163}
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000164
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000165ArgEffects* CFRefSummaryManager::getArgEffects() {
166
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000167 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
189CFRefSummary* 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
209CFRefSummary* CFRefSummaryManager::getSummary(FunctionDecl* FD,
210 ASTContext& Ctx) {
211
212 SourceLocation Loc = FD->getLocation();
213
214 if (!Loc.isFileID())
215 return NULL;
Ted Kremenek2fff37e2008-03-06 00:08:09 +0000216
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000217 { // 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 Kremenek00a3a5f2008-03-12 01:21:45 +0000226#if 0
227 SourceManager& SrcMgr = Ctx.getSourceManager();
228 unsigned fid = Loc.getFileID();
229 const FileEntry* FE = SrcMgr.getFileEntryForID(fid);
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000230
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000231 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 Kremenek6b3a0f72008-03-11 06:39:11 +0000251
252 return NULL;
Ted Kremenek2fff37e2008-03-06 00:08:09 +0000253}
254
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000255CFRefSummary* 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
292CFRefSummary* 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 Kremenek3ea0b6a2008-04-10 22:58:08 +0000309 if (strcmp("CFTypeRef", TDName) != 0)
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000310 return NULL;
311
312 if (!ArgT->isPointerType())
313 return NULL;
Ted Kremenek3ea0b6a2008-04-10 22:58:08 +0000314
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000315 QualType RetTy = FT->getResultType();
316
Ted Kremenek3ea0b6a2008-04-10 22:58:08 +0000317 if (isRetain) {
318 // CFRetain: the return type should also be "CFTypeRef".
319 if (RetTy.getTypePtr() != ArgT)
320 return NULL;
Ted Kremenek940b1d82008-04-10 23:44:06 +0000321
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 Kremenek3ea0b6a2008-04-10 22:58:08 +0000326 }
327 else {
328 // CFRelease: the return type should be void.
329
330 if (RetTy != Ctx.VoidTy)
331 return NULL;
Ted Kremenek3ea0b6a2008-04-10 22:58:08 +0000332
Ted Kremenek940b1d82008-04-10 23:44:06 +0000333 assert (ScratchArgs.empty());
334 ScratchArgs.push_back(DecRef);
335 return getPersistentSummary(getArgEffects(), RetEffect::MakeNoRet());
336 }
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000337}
338
339static 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 Kremenek940b1d82008-04-10 23:44:06 +0000363void CFRefSummaryManager::FillDoNothing(unsigned Args) {
364 for (unsigned i = 0; i != Args; ++i)
365 ScratchArgs.push_back(DoNothing);
366}
367
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000368
369CFRefSummary*
370CFRefSummaryManager::getCFSummaryCreateRule(FunctionTypeProto* FT) {
371
372 if (!isCFRefType(FT->getResultType()))
Ted Kremeneka0df99f2008-04-11 20:11:19 +0000373 return NULL;
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000374
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 Kremenek940b1d82008-04-10 23:44:06 +0000380 FillDoNothing(FT->getNumArgs());
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000381 return getPersistentSummary(getArgEffects(), RetEffect::MakeOwned());
382}
383
384CFRefSummary*
385CFRefSummaryManager::getCFSummaryGetRule(FunctionTypeProto* FT) {
386
Ted Kremeneka0df99f2008-04-11 20:11:19 +0000387 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 Kremenek00a3a5f2008-03-12 01:21:45 +0000397
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 Kremenek940b1d82008-04-10 23:44:06 +0000403 FillDoNothing(FT->getNumArgs());
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000404 return getPersistentSummary(getArgEffects(), RetEffect::MakeNotOwned());
405}
406
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000407//===----------------------------------------------------------------------===//
Ted Kremenek05cbe1a2008-04-09 23:49:11 +0000408// Bug Descriptions.
409//===----------------------------------------------------------------------===//
410
411namespace {
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 Kremenek6b3a0f72008-03-11 06:39:11 +0000458// Transfer functions.
459//===----------------------------------------------------------------------===//
460
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000461namespace {
462
Ted Kremenek05cbe1a2008-04-09 23:49:11 +0000463class VISIBILITY_HIDDEN RefVal {
Ted Kremenek1ac08d62008-03-11 17:48:22 +0000464 unsigned Data;
465
466 RefVal(unsigned K, unsigned D) : Data((D << 3) | K) {
Ted Kremenek3ea0b6a2008-04-10 22:58:08 +0000467 assert ((K & ~0x7) == 0x0);
Ted Kremenek1ac08d62008-03-11 17:48:22 +0000468 }
469
470 RefVal(unsigned K) : Data(K) {
Ted Kremenek3ea0b6a2008-04-10 22:58:08 +0000471 assert ((K & ~0x7) == 0x0);
Ted Kremenek1ac08d62008-03-11 17:48:22 +0000472 }
473
474public:
Ted Kremenek61b9f872008-04-10 23:09:18 +0000475 enum Kind { Owned = 0, NotOwned = 1, Released = 2,
476 ErrorUseAfterRelease = 3, ErrorReleaseNotOwned = 4 };
Ted Kremenek1ac08d62008-03-11 17:48:22 +0000477
478
Ted Kremenek61b9f872008-04-10 23:09:18 +0000479 Kind getKind() const { return (Kind) (Data & 0x7); }
Ted Kremenek1ac08d62008-03-11 17:48:22 +0000480
481 unsigned getCount() const {
Ted Kremenek61b9f872008-04-10 23:09:18 +0000482 assert (getKind() == Owned || getKind() == NotOwned);
Ted Kremenek1ac08d62008-03-11 17:48:22 +0000483 return Data >> 3;
484 }
485
Ted Kremenek73c750b2008-03-11 18:14:09 +0000486 static bool isError(Kind k) { return k >= ErrorUseAfterRelease; }
487
Ted Kremeneke7bd9c22008-04-11 22:25:11 +0000488 bool isOwned() const {
489 return getKind() == Owned;
490 }
491
Ted Kremenek61b9f872008-04-10 23:09:18 +0000492 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 Kremenek1ac08d62008-03-11 17:48:22 +0000500 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 Kremenekf3948042008-03-11 19:44:10 +0000506
507 void print(std::ostream& Out) const;
Ted Kremenek1ac08d62008-03-11 17:48:22 +0000508};
Ted Kremenekf3948042008-03-11 19:44:10 +0000509
510void RefVal::print(std::ostream& Out) const {
511 switch (getKind()) {
512 default: assert(false);
Ted Kremenek61b9f872008-04-10 23:09:18 +0000513 case Owned: {
514 Out << "Owned";
515 unsigned cnt = getCount();
516 if (cnt) Out << " (+ " << cnt << ")";
Ted Kremenekf3948042008-03-11 19:44:10 +0000517 break;
Ted Kremenek61b9f872008-04-10 23:09:18 +0000518 }
Ted Kremenekf3948042008-03-11 19:44:10 +0000519
Ted Kremenek61b9f872008-04-10 23:09:18 +0000520 case NotOwned: {
Ted Kremenekf3948042008-03-11 19:44:10 +0000521 Out << "Not-Owned";
Ted Kremenek61b9f872008-04-10 23:09:18 +0000522 unsigned cnt = getCount();
523 if (cnt) Out << " (+ " << cnt << ")";
Ted Kremenekf3948042008-03-11 19:44:10 +0000524 break;
Ted Kremenek61b9f872008-04-10 23:09:18 +0000525 }
Ted Kremenekf3948042008-03-11 19:44:10 +0000526
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 Kremenek1ac08d62008-03-11 17:48:22 +0000540
Ted Kremenek05cbe1a2008-04-09 23:49:11 +0000541class VISIBILITY_HIDDEN CFRefCount : public GRSimpleVals {
Ted Kremenekf3948042008-03-11 19:44:10 +0000542
543 // Type definitions.
544
Ted Kremenek1ac08d62008-03-11 17:48:22 +0000545 typedef llvm::ImmutableMap<SymbolID, RefVal> RefBindings;
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000546 typedef RefBindings::Factory RefBFactoryTy;
Ted Kremenek73c750b2008-03-11 18:14:09 +0000547
Ted Kremenekbcf50ad2008-04-11 18:40:51 +0000548 typedef llvm::DenseMap<GRExprEngine::NodeTy*,Expr*> UseAfterReleasesTy;
549 typedef llvm::DenseMap<GRExprEngine::NodeTy*,Expr*> ReleasesNotOwnedTy;
Ted Kremenek73c750b2008-03-11 18:14:09 +0000550
Ted Kremenekf3948042008-03-11 19:44:10 +0000551 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 Kremenek6b3a0f72008-03-11 06:39:11 +0000559 CFRefSummaryManager Summaries;
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000560 RefBFactoryTy RefBFactory;
561
Ted Kremenek73c750b2008-03-11 18:14:09 +0000562 UseAfterReleasesTy UseAfterReleases;
563 ReleasesNotOwnedTy ReleasesNotOwned;
564
Ted Kremenekf3948042008-03-11 19:44:10 +0000565 BindingsPrinter Printer;
566
567 // Private methods.
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000568
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 Kremenek1ac08d62008-03-11 17:48:22 +0000581 RefBindings Update(RefBindings B, SymbolID sym, RefVal V, ArgEffect E,
Ted Kremenek73c750b2008-03-11 18:14:09 +0000582 RefVal::Kind& hasError);
Ted Kremenekf3948042008-03-11 19:44:10 +0000583
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000584
585public:
Ted Kremenek3ea0b6a2008-04-10 22:58:08 +0000586 CFRefCount(ASTContext& Ctx) : Summaries(Ctx) {}
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000587 virtual ~CFRefCount() {}
Ted Kremenek05cbe1a2008-04-09 23:49:11 +0000588
589 virtual void RegisterChecks(GRExprEngine& Eng);
Ted Kremenekf3948042008-03-11 19:44:10 +0000590
591 virtual ValueState::CheckerStatePrinter* getCheckerStatePrinter() {
592 return &Printer;
593 }
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000594
595 // Calls.
596
597 virtual void EvalCall(ExplodedNodeSet<ValueState>& Dst,
Ted Kremenek199e1a02008-03-12 21:06:49 +0000598 GRExprEngine& Eng,
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000599 GRStmtNodeBuilder<ValueState>& Builder,
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000600 CallExpr* CE, LVal L,
601 ExplodedNode<ValueState>* Pred);
Ted Kremenekfa34b332008-04-09 01:10:13 +0000602
Ted Kremeneke7bd9c22008-04-11 22:25:11 +0000603 // End-of-path.
604
605 virtual void EvalEndPath(GRExprEngine& Engine,
606 GREndPathNodeBuilder<ValueState>& Builder);
607
Ted Kremenekfa34b332008-04-09 01:10:13 +0000608 // Error iterators.
609
610 typedef UseAfterReleasesTy::iterator use_after_iterator;
611 typedef ReleasesNotOwnedTy::iterator bad_release_iterator;
612
Ted Kremenek05cbe1a2008-04-09 23:49:11 +0000613 use_after_iterator use_after_begin() { return UseAfterReleases.begin(); }
614 use_after_iterator use_after_end() { return UseAfterReleases.end(); }
Ted Kremenekfa34b332008-04-09 01:10:13 +0000615
Ted Kremenek05cbe1a2008-04-09 23:49:11 +0000616 bad_release_iterator bad_release_begin() { return ReleasesNotOwned.begin(); }
617 bad_release_iterator bad_release_end() { return ReleasesNotOwned.end(); }
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000618};
619
620} // end anonymous namespace
621
Ted Kremenek05cbe1a2008-04-09 23:49:11 +0000622void CFRefCount::RegisterChecks(GRExprEngine& Eng) {
623 GRSimpleVals::RegisterChecks(Eng);
624 Eng.Register(new UseAfterRelease(*this));
625 Eng.Register(new BadRelease(*this));
626}
627
628
Ted Kremenekf3948042008-03-11 19:44:10 +0000629void 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 Kremenekf9561e52008-04-11 20:23:24 +0000644static inline ArgEffect GetArgE(CFRefSummary* Summ, unsigned idx) {
645 return Summ ? Summ->getArg(idx) : DoNothing;
646}
647
648static inline RetEffect GetRetE(CFRefSummary* Summ) {
649 return Summ ? Summ->getRet() : RetEffect::MakeNoRet();
650}
651
Ted Kremenek2fff37e2008-03-06 00:08:09 +0000652void CFRefCount::EvalCall(ExplodedNodeSet<ValueState>& Dst,
Ted Kremenek199e1a02008-03-12 21:06:49 +0000653 GRExprEngine& Eng,
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000654 GRStmtNodeBuilder<ValueState>& Builder,
655 CallExpr* CE, LVal L,
656 ExplodedNode<ValueState>* Pred) {
657
Ted Kremenek199e1a02008-03-12 21:06:49 +0000658 ValueStateManager& StateMgr = Eng.getStateManager();
Ted Kremenek2fff37e2008-03-06 00:08:09 +0000659
Ted Kremenek7ded73c2008-04-14 17:45:13 +0000660 CFRefSummary* Summ = NULL;
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000661
662 // Get the summary.
Ted Kremenek2fff37e2008-03-06 00:08:09 +0000663
Ted Kremenek7ded73c2008-04-14 17:45:13 +0000664 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 Kremenek2fff37e2008-03-06 00:08:09 +0000669
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000670 // Get the state.
671
672 ValueState* St = Builder.GetState(Pred);
673
674 // Evaluate the effects of the call.
675
676 ValueState StVals = *St;
Ted Kremenek73c750b2008-03-11 18:14:09 +0000677 RefVal::Kind hasError = (RefVal::Kind) 0;
Ted Kremenekf9561e52008-04-11 20:23:24 +0000678
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000679 // This function has a summary. Evaluate the effect of the arguments.
680
681 unsigned idx = 0;
682
Ted Kremenekbcf50ad2008-04-11 18:40:51 +0000683 Expr* ErrorExpr = NULL;
684
685 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
686 I != E; ++I, ++idx) {
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000687
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000688 RVal V = StateMgr.GetRVal(St, *I);
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000689
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000690 if (isa<lval::SymbolVal>(V)) {
691 SymbolID Sym = cast<lval::SymbolVal>(V).getSymbol();
Ted Kremenekf9561e52008-04-11 20:23:24 +0000692 RefBindings B = GetRefBindings(StVals);
693
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000694 if (RefBindings::TreeTy* T = B.SlimFind(Sym)) {
Ted Kremenekf9561e52008-04-11 20:23:24 +0000695 B = Update(B, Sym, T->getValue().second, GetArgE(Summ, idx), hasError);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000696 SetRefBindings(StVals, B);
Ted Kremenekbcf50ad2008-04-11 18:40:51 +0000697
698 if (hasError) {
699 ErrorExpr = *I;
700 break;
701 }
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000702 }
Ted Kremenekb8873552008-04-11 20:51:02 +0000703 }
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 Kremenekf9561e52008-04-11 20:23:24 +0000708 StateMgr.Unbind(StVals, cast<LVal>(V));
Ted Kremenekb8873552008-04-11 20:51:02 +0000709 }
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000710 }
711
Ted Kremenek1ac08d62008-03-11 17:48:22 +0000712 if (hasError) {
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000713 St = StateMgr.getPersistentState(StVals);
Ted Kremenek940b1d82008-04-10 23:44:06 +0000714
715 Builder.BuildSinks = true;
716 GRExprEngine::NodeTy* N = Builder.MakeNode(Dst, CE, Pred, St);
Ted Kremenek73c750b2008-03-11 18:14:09 +0000717
718 if (N) {
Ted Kremenek73c750b2008-03-11 18:14:09 +0000719 switch (hasError) {
720 default: assert(false);
721 case RefVal::ErrorUseAfterRelease:
Ted Kremenekbcf50ad2008-04-11 18:40:51 +0000722 UseAfterReleases[N] = ErrorExpr;
Ted Kremenek73c750b2008-03-11 18:14:09 +0000723 break;
724
725 case RefVal::ErrorReleaseNotOwned:
Ted Kremenekbcf50ad2008-04-11 18:40:51 +0000726 ReleasesNotOwned[N] = ErrorExpr;
Ted Kremenek73c750b2008-03-11 18:14:09 +0000727 break;
728 }
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000729 }
730
731 return;
Ted Kremenek1ac08d62008-03-11 17:48:22 +0000732 }
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000733
734 // Finally, consult the summary for the return value.
735
Ted Kremenekf9561e52008-04-11 20:23:24 +0000736 RetEffect RE = GetRetE(Summ);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000737 St = StateMgr.getPersistentState(StVals);
738
739
740 switch (RE.getKind()) {
741 default:
742 assert (false && "Unhandled RetEffect."); break;
743
Ted Kremenek940b1d82008-04-10 23:44:06 +0000744 case RetEffect::NoRet:
Ted Kremenekf9561e52008-04-11 20:23:24 +0000745
746 // Make up a symbol for the return value (not reference counted).
Ted Kremenekb8873552008-04-11 20:51:02 +0000747 // FIXME: This is basically copy-and-paste from GRSimpleVals. We
748 // should compose behavior, not copy it.
Ted Kremenekf9561e52008-04-11 20:23:24 +0000749
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 Kremenek940b1d82008-04-10 23:44:06 +0000761 break;
762
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000763 case RetEffect::Alias: {
764 unsigned idx = RE.getValue();
765 assert (idx < CE->getNumArgs());
766 RVal V = StateMgr.GetRVal(St, CE->getArg(idx));
Ted Kremenek199e1a02008-03-12 21:06:49 +0000767 St = StateMgr.SetRVal(St, CE, V, Eng.getCFG().isBlkExpr(CE), false);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000768 break;
769 }
770
771 case RetEffect::OwnedSymbol: {
772 unsigned Count = Builder.getCurrentBlockCount();
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000773 SymbolID Sym = Eng.getSymbolManager().getConjuredSymbol(CE, Count);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000774
775 ValueState StImpl = *St;
776 RefBindings B = GetRefBindings(StImpl);
Ted Kremenek61b9f872008-04-10 23:09:18 +0000777 SetRefBindings(StImpl, RefBFactory.Add(B, Sym, RefVal::makeOwned()));
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000778
779 St = StateMgr.SetRVal(StateMgr.getPersistentState(StImpl),
780 CE, lval::SymbolVal(Sym),
Ted Kremenek199e1a02008-03-12 21:06:49 +0000781 Eng.getCFG().isBlkExpr(CE), false);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000782
783 break;
784 }
785
786 case RetEffect::NotOwnedSymbol: {
787 unsigned Count = Builder.getCurrentBlockCount();
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000788 SymbolID Sym = Eng.getSymbolManager().getConjuredSymbol(CE, Count);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000789
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 Kremenek199e1a02008-03-12 21:06:49 +0000796 Eng.getCFG().isBlkExpr(CE), false);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000797
798 break;
799 }
800 }
801
Ted Kremenek0e561a32008-03-21 21:30:14 +0000802 Builder.MakeNode(Dst, CE, Pred, St);
Ted Kremenek2fff37e2008-03-06 00:08:09 +0000803}
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000804
Ted Kremeneke7bd9c22008-04-11 22:25:11 +0000805// End-of-path.
806
807void 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 Kremenek6b3a0f72008-03-11 06:39:11 +0000824
825CFRefCount::RefBindings CFRefCount::Update(RefBindings B, SymbolID sym,
Ted Kremenek1ac08d62008-03-11 17:48:22 +0000826 RefVal V, ArgEffect E,
Ted Kremenek73c750b2008-03-11 18:14:09 +0000827 RefVal::Kind& hasError) {
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000828
Ted Kremenek1ac08d62008-03-11 17:48:22 +0000829 // FIXME: This dispatch can potentially be sped up by unifiying it into
830 // a single switch statement. Opt for simplicity for now.
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000831
Ted Kremenek1ac08d62008-03-11 17:48:22 +0000832 switch (E) {
833 default:
834 assert (false && "Unhandled CFRef transition.");
835
836 case DoNothing:
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000837 if (V.getKind() == RefVal::Released) {
838 V = RefVal::makeUseAfterRelease();
839 hasError = V.getKind();
840 break;
841 }
842
Ted Kremenek1ac08d62008-03-11 17:48:22 +0000843 return B;
844
845 case IncRef:
846 switch (V.getKind()) {
847 default:
848 assert(false);
849
850 case RefVal::Owned:
Ted Kremenek940b1d82008-04-10 23:44:06 +0000851 V = RefVal::makeOwned(V.getCount()+1);
852 break;
Ted Kremenek61b9f872008-04-10 23:09:18 +0000853
Ted Kremenek1ac08d62008-03-11 17:48:22 +0000854 case RefVal::NotOwned:
Ted Kremenek61b9f872008-04-10 23:09:18 +0000855 V = RefVal::makeNotOwned(V.getCount()+1);
Ted Kremenek1ac08d62008-03-11 17:48:22 +0000856 break;
857
858 case RefVal::Released:
Ted Kremenek1ac08d62008-03-11 17:48:22 +0000859 V = RefVal::makeUseAfterRelease();
Ted Kremenek73c750b2008-03-11 18:14:09 +0000860 hasError = V.getKind();
Ted Kremenek1ac08d62008-03-11 17:48:22 +0000861 break;
862 }
863
Ted Kremenek940b1d82008-04-10 23:44:06 +0000864 break;
865
Ted Kremenek1ac08d62008-03-11 17:48:22 +0000866 case DecRef:
867 switch (V.getKind()) {
868 default:
869 assert (false);
870
871 case RefVal::Owned: {
Ted Kremenek61b9f872008-04-10 23:09:18 +0000872 signed Count = ((signed) V.getCount()) - 1;
873 V = Count >= 0 ? RefVal::makeOwned(Count) : RefVal::makeReleased();
Ted Kremenek1ac08d62008-03-11 17:48:22 +0000874 break;
875 }
876
Ted Kremenek61b9f872008-04-10 23:09:18 +0000877 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 Kremenek1ac08d62008-03-11 17:48:22 +0000887 break;
888 }
Ted Kremenek1ac08d62008-03-11 17:48:22 +0000889
890 case RefVal::Released:
Ted Kremenek1ac08d62008-03-11 17:48:22 +0000891 V = RefVal::makeUseAfterRelease();
Ted Kremenek73c750b2008-03-11 18:14:09 +0000892 hasError = V.getKind();
Ted Kremenek1ac08d62008-03-11 17:48:22 +0000893 break;
894 }
Ted Kremenek940b1d82008-04-10 23:44:06 +0000895
896 break;
Ted Kremenek1ac08d62008-03-11 17:48:22 +0000897 }
898
899 return RefBFactory.Add(B, sym, V);
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000900}
901
Ted Kremenekfa34b332008-04-09 01:10:13 +0000902
903//===----------------------------------------------------------------------===//
Ted Kremenek05cbe1a2008-04-09 23:49:11 +0000904// Error reporting.
Ted Kremenekfa34b332008-04-09 01:10:13 +0000905//===----------------------------------------------------------------------===//
906
Ted Kremenek05cbe1a2008-04-09 23:49:11 +0000907void UseAfterRelease::EmitWarnings(BugReporter& BR) {
Ted Kremenekfa34b332008-04-09 01:10:13 +0000908
Ted Kremenek05cbe1a2008-04-09 23:49:11 +0000909 for (CFRefCount::use_after_iterator I = TF.use_after_begin(),
910 E = TF.use_after_end(); I != E; ++I) {
911
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000912 RangedBugReport report(*this, I->first);
Ted Kremenekbcf50ad2008-04-11 18:40:51 +0000913 report.addRange(I->second->getSourceRange());
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000914 BR.EmitPathWarning(report);
Ted Kremenekfa34b332008-04-09 01:10:13 +0000915 }
Ted Kremenek05cbe1a2008-04-09 23:49:11 +0000916}
917
918void BadRelease::EmitWarnings(BugReporter& BR) {
Ted Kremenekfa34b332008-04-09 01:10:13 +0000919
Ted Kremenek05cbe1a2008-04-09 23:49:11 +0000920 for (CFRefCount::bad_release_iterator I = TF.bad_release_begin(),
921 E = TF.bad_release_end(); I != E; ++I) {
922
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000923 RangedBugReport report(*this, I->first);
Ted Kremenekbcf50ad2008-04-11 18:40:51 +0000924 report.addRange(I->second->getSourceRange());
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000925 BR.EmitPathWarning(report);
Ted Kremenekbcf50ad2008-04-11 18:40:51 +0000926
Ted Kremenek05cbe1a2008-04-09 23:49:11 +0000927 }
928}
Ted Kremenekfa34b332008-04-09 01:10:13 +0000929
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000930//===----------------------------------------------------------------------===//
Ted Kremenekd71ed262008-04-10 22:16:52 +0000931// Transfer function creation for external clients.
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000932//===----------------------------------------------------------------------===//
933
Ted Kremenek3ea0b6a2008-04-10 22:58:08 +0000934GRTransferFuncs* clang::MakeCFRefCountTF(ASTContext& Ctx) {
935 return new CFRefCount(Ctx);
936}