1 //==--- RetainCountChecker.h - Checks for leaks and other issues -*- C++ -*--//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  This file defines the methods for RetainCountChecker, which implements
11 //  a reference count checker for Core Foundation and Cocoa on (Mac OS X).
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_CLANG_LIB_STATICANALYZER_CHECKERS_RETAINCOUNTCHECKER_H
16 #define LLVM_CLANG_LIB_STATICANALYZER_CHECKERS_RETAINCOUNTCHECKER_H
17 
18 #include "../ClangSACheckers.h"
19 #include "../AllocationDiagnostics.h"
20 #include "RetainCountDiagnostics.h"
21 #include "clang/AST/Attr.h"
22 #include "clang/AST/DeclCXX.h"
23 #include "clang/AST/DeclObjC.h"
24 #include "clang/AST/ParentMap.h"
25 #include "clang/Analysis/DomainSpecific/CocoaConventions.h"
26 #include "clang/Basic/LangOptions.h"
27 #include "clang/Basic/SourceManager.h"
28 #include "clang/Analysis/SelectorExtras.h"
29 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
30 #include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
31 #include "clang/StaticAnalyzer/Core/Checker.h"
32 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
33 #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
34 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
35 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
36 #include "clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h"
37 #include "clang/StaticAnalyzer/Core/RetainSummaryManager.h"
38 #include "llvm/ADT/DenseMap.h"
39 #include "llvm/ADT/FoldingSet.h"
40 #include "llvm/ADT/ImmutableList.h"
41 #include "llvm/ADT/ImmutableMap.h"
42 #include "llvm/ADT/STLExtras.h"
43 #include "llvm/ADT/SmallString.h"
44 #include "llvm/ADT/StringExtras.h"
45 #include <cstdarg>
46 #include <utility>
47 
48 using llvm::StrInStrNoCase;
49 
50 namespace clang {
51 namespace ento {
52 namespace retaincountchecker {
53 
54 /// Metadata on reference.
55 class RefVal {
56 public:
57   enum Kind {
58     Owned = 0, // Owning reference.
59     NotOwned,  // Reference is not owned by still valid (not freed).
60     Released,  // Object has been released.
61     ReturnedOwned, // Returned object passes ownership to caller.
62     ReturnedNotOwned, // Return object does not pass ownership to caller.
63     ERROR_START,
64     ErrorDeallocNotOwned, // -dealloc called on non-owned object.
65     ErrorUseAfterRelease, // Object used after released.
66     ErrorReleaseNotOwned, // Release of an object that was not owned.
67     ERROR_LEAK_START,
68     ErrorLeak,  // A memory leak due to excessive reference counts.
69     ErrorLeakReturned, // A memory leak due to the returning method not having
70                        // the correct naming conventions.
71     ErrorOverAutorelease,
72     ErrorReturnedNotOwned
73   };
74 
75   /// Tracks how an object referenced by an ivar has been used.
76   ///
77   /// This accounts for us not knowing if an arbitrary ivar is supposed to be
78   /// stored at +0 or +1.
79   enum class IvarAccessHistory {
80     None,
81     AccessedDirectly,
82     ReleasedAfterDirectAccess
83   };
84 
85 private:
86   /// The number of outstanding retains.
87   unsigned Cnt;
88   /// The number of outstanding autoreleases.
89   unsigned ACnt;
90   /// The (static) type of the object at the time we started tracking it.
91   QualType T;
92 
93   /// The current state of the object.
94   ///
95   /// See the RefVal::Kind enum for possible values.
96   unsigned RawKind : 5;
97 
98   /// The kind of object being tracked (CF or ObjC), if known.
99   ///
100   /// See the RetEffect::ObjKind enum for possible values.
101   unsigned RawObjectKind : 2;
102 
103   /// True if the current state and/or retain count may turn out to not be the
104   /// best possible approximation of the reference counting state.
105   ///
106   /// If true, the checker may decide to throw away ("override") this state
107   /// in favor of something else when it sees the object being used in new ways.
108   ///
109   /// This setting should not be propagated to state derived from this state.
110   /// Once we start deriving new states, it would be inconsistent to override
111   /// them.
112   unsigned RawIvarAccessHistory : 2;
113 
114   RefVal(Kind k, RetEffect::ObjKind o, unsigned cnt, unsigned acnt, QualType t,
115          IvarAccessHistory IvarAccess)
116     : Cnt(cnt), ACnt(acnt), T(t), RawKind(static_cast<unsigned>(k)),
117       RawObjectKind(static_cast<unsigned>(o)),
118       RawIvarAccessHistory(static_cast<unsigned>(IvarAccess)) {
119     assert(getKind() == k && "not enough bits for the kind");
120     assert(getObjKind() == o && "not enough bits for the object kind");
121     assert(getIvarAccessHistory() == IvarAccess && "not enough bits");
122   }
123 
124 public:
125   Kind getKind() const { return static_cast<Kind>(RawKind); }
126 
127   RetEffect::ObjKind getObjKind() const {
128     return static_cast<RetEffect::ObjKind>(RawObjectKind);
129   }
130 
131   unsigned getCount() const { return Cnt; }
132   unsigned getAutoreleaseCount() const { return ACnt; }
133   unsigned getCombinedCounts() const { return Cnt + ACnt; }
134   void clearCounts() {
135     Cnt = 0;
136     ACnt = 0;
137   }
138   void setCount(unsigned i) {
139     Cnt = i;
140   }
141   void setAutoreleaseCount(unsigned i) {
142     ACnt = i;
143   }
144 
145   QualType getType() const { return T; }
146 
147   /// Returns what the analyzer knows about direct accesses to a particular
148   /// instance variable.
149   ///
150   /// If the object with this refcount wasn't originally from an Objective-C
151   /// ivar region, this should always return IvarAccessHistory::None.
152   IvarAccessHistory getIvarAccessHistory() const {
153     return static_cast<IvarAccessHistory>(RawIvarAccessHistory);
154   }
155 
156   bool isOwned() const {
157     return getKind() == Owned;
158   }
159 
160   bool isNotOwned() const {
161     return getKind() == NotOwned;
162   }
163 
164   bool isReturnedOwned() const {
165     return getKind() == ReturnedOwned;
166   }
167 
168   bool isReturnedNotOwned() const {
169     return getKind() == ReturnedNotOwned;
170   }
171 
172   /// Create a state for an object whose lifetime is the responsibility of the
173   /// current function, at least partially.
174   ///
175   /// Most commonly, this is an owned object with a retain count of +1.
176   static RefVal makeOwned(RetEffect::ObjKind o, QualType t) {
177     return RefVal(Owned, o, /*Count=*/1, 0, t, IvarAccessHistory::None);
178   }
179 
180   /// Create a state for an object whose lifetime is not the responsibility of
181   /// the current function.
182   ///
183   /// Most commonly, this is an unowned object with a retain count of +0.
184   static RefVal makeNotOwned(RetEffect::ObjKind o, QualType t) {
185     return RefVal(NotOwned, o, /*Count=*/0, 0, t, IvarAccessHistory::None);
186   }
187 
188   RefVal operator-(size_t i) const {
189     return RefVal(getKind(), getObjKind(), getCount() - i,
190                   getAutoreleaseCount(), getType(), getIvarAccessHistory());
191   }
192 
193   RefVal operator+(size_t i) const {
194     return RefVal(getKind(), getObjKind(), getCount() + i,
195                   getAutoreleaseCount(), getType(), getIvarAccessHistory());
196   }
197 
198   RefVal operator^(Kind k) const {
199     return RefVal(k, getObjKind(), getCount(), getAutoreleaseCount(),
200                   getType(), getIvarAccessHistory());
201   }
202 
203   RefVal autorelease() const {
204     return RefVal(getKind(), getObjKind(), getCount(), getAutoreleaseCount()+1,
205                   getType(), getIvarAccessHistory());
206   }
207 
208   RefVal withIvarAccess() const {
209     assert(getIvarAccessHistory() == IvarAccessHistory::None);
210     return RefVal(getKind(), getObjKind(), getCount(), getAutoreleaseCount(),
211                   getType(), IvarAccessHistory::AccessedDirectly);
212   }
213 
214   RefVal releaseViaIvar() const {
215     assert(getIvarAccessHistory() == IvarAccessHistory::AccessedDirectly);
216     return RefVal(getKind(), getObjKind(), getCount(), getAutoreleaseCount(),
217                   getType(), IvarAccessHistory::ReleasedAfterDirectAccess);
218   }
219 
220   // Comparison, profiling, and pretty-printing.
221 
222   bool hasSameState(const RefVal &X) const {
223     return getKind() == X.getKind() && Cnt == X.Cnt && ACnt == X.ACnt &&
224            getIvarAccessHistory() == X.getIvarAccessHistory();
225   }
226 
227   bool operator==(const RefVal& X) const {
228     return T == X.T && hasSameState(X) && getObjKind() == X.getObjKind();
229   }
230 
231   void Profile(llvm::FoldingSetNodeID& ID) const {
232     ID.Add(T);
233     ID.AddInteger(RawKind);
234     ID.AddInteger(Cnt);
235     ID.AddInteger(ACnt);
236     ID.AddInteger(RawObjectKind);
237     ID.AddInteger(RawIvarAccessHistory);
238   }
239 
240   void print(raw_ostream &Out) const;
241 };
242 
243 class RetainCountChecker
244   : public Checker< check::Bind,
245                     check::DeadSymbols,
246                     check::EndAnalysis,
247                     check::BeginFunction,
248                     check::EndFunction,
249                     check::PostStmt<BlockExpr>,
250                     check::PostStmt<CastExpr>,
251                     check::PostStmt<ObjCArrayLiteral>,
252                     check::PostStmt<ObjCDictionaryLiteral>,
253                     check::PostStmt<ObjCBoxedExpr>,
254                     check::PostStmt<ObjCIvarRefExpr>,
255                     check::PostCall,
256                     check::PreStmt<ReturnStmt>,
257                     check::RegionChanges,
258                     eval::Assume,
259                     eval::Call > {
260   mutable std::unique_ptr<CFRefBug> useAfterRelease, releaseNotOwned;
261   mutable std::unique_ptr<CFRefBug> deallocNotOwned;
262   mutable std::unique_ptr<CFRefBug> overAutorelease, returnNotOwnedForOwned;
263   mutable std::unique_ptr<CFRefBug> leakWithinFunction, leakAtReturn;
264 
265   typedef llvm::DenseMap<SymbolRef, const CheckerProgramPointTag *> SymbolTagMap;
266 
267   // This map is only used to ensure proper deletion of any allocated tags.
268   mutable SymbolTagMap DeadSymbolTags;
269 
270   mutable std::unique_ptr<RetainSummaryManager> Summaries;
271   mutable SummaryLogTy SummaryLog;
272   mutable bool ShouldResetSummaryLog;
273 
274   /// Optional setting to indicate if leak reports should include
275   /// the allocation line.
276   mutable bool IncludeAllocationLine;
277 
278 public:
279   RetainCountChecker(AnalyzerOptions &AO)
280     : ShouldResetSummaryLog(false),
281       IncludeAllocationLine(shouldIncludeAllocationSiteInLeakDiagnostics(AO)) {}
282 
283   ~RetainCountChecker() override { DeleteContainerSeconds(DeadSymbolTags); }
284 
285   void checkEndAnalysis(ExplodedGraph &G, BugReporter &BR,
286                         ExprEngine &Eng) const {
287     // FIXME: This is a hack to make sure the summary log gets cleared between
288     // analyses of different code bodies.
289     //
290     // Why is this necessary? Because a checker's lifetime is tied to a
291     // translation unit, but an ExplodedGraph's lifetime is just a code body.
292     // Once in a blue moon, a new ExplodedNode will have the same address as an
293     // old one with an associated summary, and the bug report visitor gets very
294     // confused. (To make things worse, the summary lifetime is currently also
295     // tied to a code body, so we get a crash instead of incorrect results.)
296     //
297     // Why is this a bad solution? Because if the lifetime of the ExplodedGraph
298     // changes, things will start going wrong again. Really the lifetime of this
299     // log needs to be tied to either the specific nodes in it or the entire
300     // ExplodedGraph, not to a specific part of the code being analyzed.
301     //
302     // (Also, having stateful local data means that the same checker can't be
303     // used from multiple threads, but a lot of checkers have incorrect
304     // assumptions about that anyway. So that wasn't a priority at the time of
305     // this fix.)
306     //
307     // This happens at the end of analysis, but bug reports are emitted /after/
308     // this point. So we can't just clear the summary log now. Instead, we mark
309     // that the next time we access the summary log, it should be cleared.
310 
311     // If we never reset the summary log during /this/ code body analysis,
312     // there were no new summaries. There might still have been summaries from
313     // the /last/ analysis, so clear them out to make sure the bug report
314     // visitors don't get confused.
315     if (ShouldResetSummaryLog)
316       SummaryLog.clear();
317 
318     ShouldResetSummaryLog = !SummaryLog.empty();
319   }
320 
321   CFRefBug *getLeakWithinFunctionBug(const LangOptions &LOpts) const {
322     if (!leakWithinFunction)
323       leakWithinFunction.reset(new Leak(this, "Leak"));
324     return leakWithinFunction.get();
325   }
326 
327   CFRefBug *getLeakAtReturnBug(const LangOptions &LOpts) const {
328       if (!leakAtReturn)
329         leakAtReturn.reset(new Leak(this, "Leak of returned object"));
330       return leakAtReturn.get();
331   }
332 
333   RetainSummaryManager &getSummaryManager(ASTContext &Ctx) const {
334     // FIXME: We don't support ARC being turned on and off during one analysis.
335     // (nor, for that matter, do we support changing ASTContexts)
336     bool ARCEnabled = (bool)Ctx.getLangOpts().ObjCAutoRefCount;
337     if (!Summaries)
338       Summaries.reset(new RetainSummaryManager(Ctx, ARCEnabled));
339     else
340       assert(Summaries->isARCEnabled() == ARCEnabled);
341     return *Summaries;
342   }
343 
344   RetainSummaryManager &getSummaryManager(CheckerContext &C) const {
345     return getSummaryManager(C.getASTContext());
346   }
347 
348   void printState(raw_ostream &Out, ProgramStateRef State,
349                   const char *NL, const char *Sep) const override;
350 
351   void checkBind(SVal loc, SVal val, const Stmt *S, CheckerContext &C) const;
352   void checkPostStmt(const BlockExpr *BE, CheckerContext &C) const;
353   void checkPostStmt(const CastExpr *CE, CheckerContext &C) const;
354 
355   void checkPostStmt(const ObjCArrayLiteral *AL, CheckerContext &C) const;
356   void checkPostStmt(const ObjCDictionaryLiteral *DL, CheckerContext &C) const;
357   void checkPostStmt(const ObjCBoxedExpr *BE, CheckerContext &C) const;
358 
359   void checkPostStmt(const ObjCIvarRefExpr *IRE, CheckerContext &C) const;
360 
361   void checkPostCall(const CallEvent &Call, CheckerContext &C) const;
362 
363   void checkSummary(const RetainSummary &Summ, const CallEvent &Call,
364                     CheckerContext &C) const;
365 
366   void processSummaryOfInlined(const RetainSummary &Summ,
367                                const CallEvent &Call,
368                                CheckerContext &C) const;
369 
370   bool evalCall(const CallExpr *CE, CheckerContext &C) const;
371 
372   ProgramStateRef evalAssume(ProgramStateRef state, SVal Cond,
373                                  bool Assumption) const;
374 
375   ProgramStateRef
376   checkRegionChanges(ProgramStateRef state,
377                      const InvalidatedSymbols *invalidated,
378                      ArrayRef<const MemRegion *> ExplicitRegions,
379                      ArrayRef<const MemRegion *> Regions,
380                      const LocationContext* LCtx,
381                      const CallEvent *Call) const;
382 
383   void checkPreStmt(const ReturnStmt *S, CheckerContext &C) const;
384   void checkReturnWithRetEffect(const ReturnStmt *S, CheckerContext &C,
385                                 ExplodedNode *Pred, RetEffect RE, RefVal X,
386                                 SymbolRef Sym, ProgramStateRef state) const;
387 
388   void checkDeadSymbols(SymbolReaper &SymReaper, CheckerContext &C) const;
389   void checkBeginFunction(CheckerContext &C) const;
390   void checkEndFunction(const ReturnStmt *RS, CheckerContext &C) const;
391 
392   ProgramStateRef updateSymbol(ProgramStateRef state, SymbolRef sym,
393                                RefVal V, ArgEffect E, RefVal::Kind &hasErr,
394                                CheckerContext &C) const;
395 
396   void processNonLeakError(ProgramStateRef St, SourceRange ErrorRange,
397                            RefVal::Kind ErrorKind, SymbolRef Sym,
398                            CheckerContext &C) const;
399 
400   void processObjCLiterals(CheckerContext &C, const Expr *Ex) const;
401 
402   const ProgramPointTag *getDeadSymbolTag(SymbolRef sym) const;
403 
404   ProgramStateRef handleSymbolDeath(ProgramStateRef state,
405                                     SymbolRef sid, RefVal V,
406                                     SmallVectorImpl<SymbolRef> &Leaked) const;
407 
408   ProgramStateRef
409   handleAutoreleaseCounts(ProgramStateRef state, ExplodedNode *Pred,
410                           const ProgramPointTag *Tag, CheckerContext &Ctx,
411                           SymbolRef Sym, RefVal V) const;
412 
413   ExplodedNode *processLeaks(ProgramStateRef state,
414                              SmallVectorImpl<SymbolRef> &Leaked,
415                              CheckerContext &Ctx,
416                              ExplodedNode *Pred = nullptr) const;
417 };
418 
419 //===----------------------------------------------------------------------===//
420 // RefBindings - State used to track object reference counts.
421 //===----------------------------------------------------------------------===//
422 
423 const RefVal *getRefBinding(ProgramStateRef State, SymbolRef Sym);
424 
425 ProgramStateRef setRefBinding(ProgramStateRef State, SymbolRef Sym,
426                                      RefVal Val);
427 
428 ProgramStateRef removeRefBinding(ProgramStateRef State, SymbolRef Sym);
429 
430 /// Returns true if this stack frame is for an Objective-C method that is a
431 /// property getter or setter whose body has been synthesized by the analyzer.
432 inline bool isSynthesizedAccessor(const StackFrameContext *SFC) {
433   auto Method = dyn_cast_or_null<ObjCMethodDecl>(SFC->getDecl());
434   if (!Method || !Method->isPropertyAccessor())
435     return false;
436 
437   return SFC->getAnalysisDeclContext()->isBodyAutosynthesized();
438 }
439 
440 } // end namespace retaincountchecker
441 } // end namespace ento
442 } // end namespace clang
443 
444 #endif
445