SymbolManager.h revision 226633
1102050Stjr//== SymbolManager.h - Management of Symbolic Values ------------*- C++ -*--==//
2102050Stjr//
3102050Stjr//                     The LLVM Compiler Infrastructure
4102050Stjr//
5102050Stjr// This file is distributed under the University of Illinois Open Source
6102050Stjr// License. See LICENSE.TXT for details.
7102050Stjr//
8102050Stjr//===----------------------------------------------------------------------===//
9102050Stjr//
10102050Stjr//  This file defines SymbolManager, a class that manages symbolic values
11102050Stjr//  created for use by ExprEngine and related classes.
12102050Stjr//
13102050Stjr//===----------------------------------------------------------------------===//
14102050Stjr
15102050Stjr#ifndef LLVM_CLANG_GR_SYMMGR_H
16102050Stjr#define LLVM_CLANG_GR_SYMMGR_H
17102050Stjr
18102050Stjr#include "clang/AST/Decl.h"
19102050Stjr#include "clang/AST/Expr.h"
20102050Stjr#include "clang/Analysis/AnalysisContext.h"
21102050Stjr#include "clang/Basic/LLVM.h"
22102050Stjr#include "clang/StaticAnalyzer/Core/PathSensitive/StoreRef.h"
23102050Stjr#include "llvm/Support/DataTypes.h"
24102050Stjr#include "llvm/ADT/FoldingSet.h"
25102050Stjr#include "llvm/ADT/DenseSet.h"
26102050Stjr#include "llvm/ADT/DenseMap.h"
27102050Stjr
28102050Stjrnamespace llvm {
29102050Stjrclass BumpPtrAllocator;
30102050Stjr}
31102050Stjr
32102050Stjrnamespace clang {
33102050Stjr  class ASTContext;
34102050Stjr  class StackFrameContext;
35102050Stjr
36102050Stjrnamespace ento {
37102050Stjr  class BasicValueFactory;
38102050Stjr  class MemRegion;
39102050Stjr  class SubRegion;
40102050Stjr  class TypedValueRegion;
41102050Stjr  class VarRegion;
42102050Stjr
43class SymExpr : public llvm::FoldingSetNode {
44public:
45  enum Kind { RegionValueKind, ConjuredKind, DerivedKind, ExtentKind,
46              MetadataKind,
47              BEGIN_SYMBOLS = RegionValueKind,
48              END_SYMBOLS = MetadataKind,
49              SymIntKind, SymSymKind };
50private:
51  Kind K;
52
53protected:
54  SymExpr(Kind k) : K(k) {}
55
56public:
57  virtual ~SymExpr() {}
58
59  Kind getKind() const { return K; }
60
61  void dump() const;
62
63  virtual void dumpToStream(raw_ostream &os) const = 0;
64
65  virtual QualType getType(ASTContext&) const = 0;
66  virtual void Profile(llvm::FoldingSetNodeID& profile) = 0;
67
68  // Implement isa<T> support.
69  static inline bool classof(const SymExpr*) { return true; }
70};
71
72typedef unsigned SymbolID;
73
74class SymbolData : public SymExpr {
75private:
76  const SymbolID Sym;
77
78protected:
79  SymbolData(Kind k, SymbolID sym) : SymExpr(k), Sym(sym) {}
80
81public:
82  virtual ~SymbolData() {}
83
84  SymbolID getSymbolID() const { return Sym; }
85
86  // Implement isa<T> support.
87  static inline bool classof(const SymExpr *SE) {
88    Kind k = SE->getKind();
89    return k >= BEGIN_SYMBOLS && k <= END_SYMBOLS;
90  }
91};
92
93typedef const SymbolData* SymbolRef;
94typedef llvm::SmallVector<SymbolRef, 2> SymbolRefSmallVectorTy;
95
96/// A symbol representing the value of a MemRegion.
97class SymbolRegionValue : public SymbolData {
98  const TypedValueRegion *R;
99
100public:
101  SymbolRegionValue(SymbolID sym, const TypedValueRegion *r)
102    : SymbolData(RegionValueKind, sym), R(r) {}
103
104  const TypedValueRegion* getRegion() const { return R; }
105
106  static void Profile(llvm::FoldingSetNodeID& profile, const TypedValueRegion* R) {
107    profile.AddInteger((unsigned) RegionValueKind);
108    profile.AddPointer(R);
109  }
110
111  virtual void Profile(llvm::FoldingSetNodeID& profile) {
112    Profile(profile, R);
113  }
114
115  void dumpToStream(raw_ostream &os) const;
116
117  QualType getType(ASTContext&) const;
118
119  // Implement isa<T> support.
120  static inline bool classof(const SymExpr *SE) {
121    return SE->getKind() == RegionValueKind;
122  }
123};
124
125/// A symbol representing the result of an expression.
126class SymbolConjured : public SymbolData {
127  const Stmt *S;
128  QualType T;
129  unsigned Count;
130  const void *SymbolTag;
131
132public:
133  SymbolConjured(SymbolID sym, const Stmt *s, QualType t, unsigned count,
134                 const void *symbolTag)
135    : SymbolData(ConjuredKind, sym), S(s), T(t), Count(count),
136      SymbolTag(symbolTag) {}
137
138  const Stmt *getStmt() const { return S; }
139  unsigned getCount() const { return Count; }
140  const void *getTag() const { return SymbolTag; }
141
142  QualType getType(ASTContext&) const;
143
144  void dumpToStream(raw_ostream &os) const;
145
146  static void Profile(llvm::FoldingSetNodeID& profile, const Stmt *S,
147                      QualType T, unsigned Count, const void *SymbolTag) {
148    profile.AddInteger((unsigned) ConjuredKind);
149    profile.AddPointer(S);
150    profile.Add(T);
151    profile.AddInteger(Count);
152    profile.AddPointer(SymbolTag);
153  }
154
155  virtual void Profile(llvm::FoldingSetNodeID& profile) {
156    Profile(profile, S, T, Count, SymbolTag);
157  }
158
159  // Implement isa<T> support.
160  static inline bool classof(const SymExpr *SE) {
161    return SE->getKind() == ConjuredKind;
162  }
163};
164
165/// A symbol representing the value of a MemRegion whose parent region has
166/// symbolic value.
167class SymbolDerived : public SymbolData {
168  SymbolRef parentSymbol;
169  const TypedValueRegion *R;
170
171public:
172  SymbolDerived(SymbolID sym, SymbolRef parent, const TypedValueRegion *r)
173    : SymbolData(DerivedKind, sym), parentSymbol(parent), R(r) {}
174
175  SymbolRef getParentSymbol() const { return parentSymbol; }
176  const TypedValueRegion *getRegion() const { return R; }
177
178  QualType getType(ASTContext&) const;
179
180  void dumpToStream(raw_ostream &os) const;
181
182  static void Profile(llvm::FoldingSetNodeID& profile, SymbolRef parent,
183                      const TypedValueRegion *r) {
184    profile.AddInteger((unsigned) DerivedKind);
185    profile.AddPointer(r);
186    profile.AddPointer(parent);
187  }
188
189  virtual void Profile(llvm::FoldingSetNodeID& profile) {
190    Profile(profile, parentSymbol, R);
191  }
192
193  // Implement isa<T> support.
194  static inline bool classof(const SymExpr *SE) {
195    return SE->getKind() == DerivedKind;
196  }
197};
198
199/// SymbolExtent - Represents the extent (size in bytes) of a bounded region.
200///  Clients should not ask the SymbolManager for a region's extent. Always use
201///  SubRegion::getExtent instead -- the value returned may not be a symbol.
202class SymbolExtent : public SymbolData {
203  const SubRegion *R;
204
205public:
206  SymbolExtent(SymbolID sym, const SubRegion *r)
207  : SymbolData(ExtentKind, sym), R(r) {}
208
209  const SubRegion *getRegion() const { return R; }
210
211  QualType getType(ASTContext&) const;
212
213  void dumpToStream(raw_ostream &os) const;
214
215  static void Profile(llvm::FoldingSetNodeID& profile, const SubRegion *R) {
216    profile.AddInteger((unsigned) ExtentKind);
217    profile.AddPointer(R);
218  }
219
220  virtual void Profile(llvm::FoldingSetNodeID& profile) {
221    Profile(profile, R);
222  }
223
224  // Implement isa<T> support.
225  static inline bool classof(const SymExpr *SE) {
226    return SE->getKind() == ExtentKind;
227  }
228};
229
230/// SymbolMetadata - Represents path-dependent metadata about a specific region.
231///  Metadata symbols remain live as long as they are marked as in use before
232///  dead-symbol sweeping AND their associated regions are still alive.
233///  Intended for use by checkers.
234class SymbolMetadata : public SymbolData {
235  const MemRegion* R;
236  const Stmt *S;
237  QualType T;
238  unsigned Count;
239  const void *Tag;
240public:
241  SymbolMetadata(SymbolID sym, const MemRegion* r, const Stmt *s, QualType t,
242                 unsigned count, const void *tag)
243  : SymbolData(MetadataKind, sym), R(r), S(s), T(t), Count(count), Tag(tag) {}
244
245  const MemRegion *getRegion() const { return R; }
246  const Stmt *getStmt() const { return S; }
247  unsigned getCount() const { return Count; }
248  const void *getTag() const { return Tag; }
249
250  QualType getType(ASTContext&) const;
251
252  void dumpToStream(raw_ostream &os) const;
253
254  static void Profile(llvm::FoldingSetNodeID& profile, const MemRegion *R,
255                      const Stmt *S, QualType T, unsigned Count,
256                      const void *Tag) {
257    profile.AddInteger((unsigned) MetadataKind);
258    profile.AddPointer(R);
259    profile.AddPointer(S);
260    profile.Add(T);
261    profile.AddInteger(Count);
262    profile.AddPointer(Tag);
263  }
264
265  virtual void Profile(llvm::FoldingSetNodeID& profile) {
266    Profile(profile, R, S, T, Count, Tag);
267  }
268
269  // Implement isa<T> support.
270  static inline bool classof(const SymExpr *SE) {
271    return SE->getKind() == MetadataKind;
272  }
273};
274
275/// SymIntExpr - Represents symbolic expression like 'x' + 3.
276class SymIntExpr : public SymExpr {
277  const SymExpr *LHS;
278  BinaryOperator::Opcode Op;
279  const llvm::APSInt& RHS;
280  QualType T;
281
282public:
283  SymIntExpr(const SymExpr *lhs, BinaryOperator::Opcode op,
284             const llvm::APSInt& rhs, QualType t)
285    : SymExpr(SymIntKind), LHS(lhs), Op(op), RHS(rhs), T(t) {}
286
287  // FIXME: We probably need to make this out-of-line to avoid redundant
288  // generation of virtual functions.
289  QualType getType(ASTContext &C) const { return T; }
290
291  BinaryOperator::Opcode getOpcode() const { return Op; }
292
293  void dumpToStream(raw_ostream &os) const;
294
295  const SymExpr *getLHS() const { return LHS; }
296  const llvm::APSInt &getRHS() const { return RHS; }
297
298  static void Profile(llvm::FoldingSetNodeID& ID, const SymExpr *lhs,
299                      BinaryOperator::Opcode op, const llvm::APSInt& rhs,
300                      QualType t) {
301    ID.AddInteger((unsigned) SymIntKind);
302    ID.AddPointer(lhs);
303    ID.AddInteger(op);
304    ID.AddPointer(&rhs);
305    ID.Add(t);
306  }
307
308  void Profile(llvm::FoldingSetNodeID& ID) {
309    Profile(ID, LHS, Op, RHS, T);
310  }
311
312  // Implement isa<T> support.
313  static inline bool classof(const SymExpr *SE) {
314    return SE->getKind() == SymIntKind;
315  }
316};
317
318/// SymSymExpr - Represents symbolic expression like 'x' + 'y'.
319class SymSymExpr : public SymExpr {
320  const SymExpr *LHS;
321  BinaryOperator::Opcode Op;
322  const SymExpr *RHS;
323  QualType T;
324
325public:
326  SymSymExpr(const SymExpr *lhs, BinaryOperator::Opcode op, const SymExpr *rhs,
327             QualType t)
328    : SymExpr(SymSymKind), LHS(lhs), Op(op), RHS(rhs), T(t) {}
329
330  BinaryOperator::Opcode getOpcode() const { return Op; }
331  const SymExpr *getLHS() const { return LHS; }
332  const SymExpr *getRHS() const { return RHS; }
333
334  // FIXME: We probably need to make this out-of-line to avoid redundant
335  // generation of virtual functions.
336  QualType getType(ASTContext &C) const { return T; }
337
338  void dumpToStream(raw_ostream &os) const;
339
340  static void Profile(llvm::FoldingSetNodeID& ID, const SymExpr *lhs,
341                    BinaryOperator::Opcode op, const SymExpr *rhs, QualType t) {
342    ID.AddInteger((unsigned) SymSymKind);
343    ID.AddPointer(lhs);
344    ID.AddInteger(op);
345    ID.AddPointer(rhs);
346    ID.Add(t);
347  }
348
349  void Profile(llvm::FoldingSetNodeID& ID) {
350    Profile(ID, LHS, Op, RHS, T);
351  }
352
353  // Implement isa<T> support.
354  static inline bool classof(const SymExpr *SE) {
355    return SE->getKind() == SymSymKind;
356  }
357};
358
359class SymbolManager {
360  typedef llvm::FoldingSet<SymExpr> DataSetTy;
361  typedef llvm::DenseMap<SymbolRef, SymbolRefSmallVectorTy*> SymbolDependTy;
362
363  DataSetTy DataSet;
364  /// Stores the extra dependencies between symbols: the data should be kept
365  /// alive as long as the key is live.
366  SymbolDependTy SymbolDependencies;
367  unsigned SymbolCounter;
368  llvm::BumpPtrAllocator& BPAlloc;
369  BasicValueFactory &BV;
370  ASTContext &Ctx;
371
372public:
373  SymbolManager(ASTContext &ctx, BasicValueFactory &bv,
374                llvm::BumpPtrAllocator& bpalloc)
375    : SymbolDependencies(16), SymbolCounter(0),
376      BPAlloc(bpalloc), BV(bv), Ctx(ctx) {}
377
378  ~SymbolManager();
379
380  static bool canSymbolicate(QualType T);
381
382  /// \brief Make a unique symbol for MemRegion R according to its kind.
383  const SymbolRegionValue* getRegionValueSymbol(const TypedValueRegion* R);
384
385  const SymbolConjured* getConjuredSymbol(const Stmt *E, QualType T,
386                                          unsigned VisitCount,
387                                          const void *SymbolTag = 0);
388
389  const SymbolConjured* getConjuredSymbol(const Expr *E, unsigned VisitCount,
390                                          const void *SymbolTag = 0) {
391    return getConjuredSymbol(E, E->getType(), VisitCount, SymbolTag);
392  }
393
394  const SymbolDerived *getDerivedSymbol(SymbolRef parentSymbol,
395                                        const TypedValueRegion *R);
396
397  const SymbolExtent *getExtentSymbol(const SubRegion *R);
398
399  /// \brief Creates a metadata symbol associated with a specific region.
400  ///
401  /// VisitCount can be used to differentiate regions corresponding to
402  /// different loop iterations, thus, making the symbol path-dependent.
403  const SymbolMetadata* getMetadataSymbol(const MemRegion* R, const Stmt *S,
404                                          QualType T, unsigned VisitCount,
405                                          const void *SymbolTag = 0);
406
407  const SymIntExpr *getSymIntExpr(const SymExpr *lhs, BinaryOperator::Opcode op,
408                                  const llvm::APSInt& rhs, QualType t);
409
410  const SymIntExpr *getSymIntExpr(const SymExpr &lhs, BinaryOperator::Opcode op,
411                                  const llvm::APSInt& rhs, QualType t) {
412    return getSymIntExpr(&lhs, op, rhs, t);
413  }
414
415  const SymSymExpr *getSymSymExpr(const SymExpr *lhs, BinaryOperator::Opcode op,
416                                  const SymExpr *rhs, QualType t);
417
418  QualType getType(const SymExpr *SE) const {
419    return SE->getType(Ctx);
420  }
421
422  /// \brief Add artificial symbol dependency.
423  ///
424  /// The dependent symbol should stay alive as long as the primary is alive.
425  void addSymbolDependency(const SymbolRef Primary, const SymbolRef Dependent);
426
427  const SymbolRefSmallVectorTy *getDependentSymbols(const SymbolRef Primary);
428
429  ASTContext &getContext() { return Ctx; }
430  BasicValueFactory &getBasicVals() { return BV; }
431};
432
433class SymbolReaper {
434  enum SymbolStatus {
435    NotProcessed,
436    HaveMarkedDependents
437  };
438
439  typedef llvm::DenseSet<SymbolRef> SymbolSetTy;
440  typedef llvm::DenseMap<SymbolRef, SymbolStatus> SymbolMapTy;
441  typedef llvm::DenseSet<const MemRegion *> RegionSetTy;
442
443  SymbolMapTy TheLiving;
444  SymbolSetTy MetadataInUse;
445  SymbolSetTy TheDead;
446
447  RegionSetTy RegionRoots;
448
449  const LocationContext *LCtx;
450  const Stmt *Loc;
451  SymbolManager& SymMgr;
452  StoreRef reapedStore;
453  llvm::DenseMap<const MemRegion *, unsigned> includedRegionCache;
454
455public:
456  SymbolReaper(const LocationContext *ctx, const Stmt *s, SymbolManager& symmgr,
457               StoreManager &storeMgr)
458   : LCtx(ctx), Loc(s), SymMgr(symmgr), reapedStore(0, storeMgr) {}
459
460  ~SymbolReaper() {}
461
462  const LocationContext *getLocationContext() const { return LCtx; }
463  const Stmt *getCurrentStatement() const { return Loc; }
464
465  bool isLive(SymbolRef sym);
466  bool isLiveRegion(const MemRegion *region);
467  bool isLive(const Stmt *ExprVal) const;
468  bool isLive(const VarRegion *VR, bool includeStoreBindings = false) const;
469
470  /// \brief Unconditionally marks a symbol as live.
471  ///
472  /// This should never be
473  /// used by checkers, only by the state infrastructure such as the store and
474  /// environment. Checkers should instead use metadata symbols and markInUse.
475  void markLive(SymbolRef sym);
476
477  /// \brief Marks a symbol as important to a checker.
478  ///
479  /// For metadata symbols,
480  /// this will keep the symbol alive as long as its associated region is also
481  /// live. For other symbols, this has no effect; checkers are not permitted
482  /// to influence the life of other symbols. This should be used before any
483  /// symbol marking has occurred, i.e. in the MarkLiveSymbols callback.
484  void markInUse(SymbolRef sym);
485
486  /// \brief If a symbol is known to be live, marks the symbol as live.
487  ///
488  ///  Otherwise, if the symbol cannot be proven live, it is marked as dead.
489  ///  Returns true if the symbol is dead, false if live.
490  bool maybeDead(SymbolRef sym);
491
492  typedef SymbolSetTy::const_iterator dead_iterator;
493  dead_iterator dead_begin() const { return TheDead.begin(); }
494  dead_iterator dead_end() const { return TheDead.end(); }
495
496  bool hasDeadSymbols() const {
497    return !TheDead.empty();
498  }
499
500  typedef RegionSetTy::const_iterator region_iterator;
501  region_iterator region_begin() const { return RegionRoots.begin(); }
502  region_iterator region_end() const { return RegionRoots.end(); }
503
504  /// \brief Returns whether or not a symbol has been confirmed dead.
505  ///
506  /// This should only be called once all marking of dead symbols has completed.
507  /// (For checkers, this means only in the evalDeadSymbols callback.)
508  bool isDead(SymbolRef sym) const {
509    return TheDead.count(sym);
510  }
511
512  void markLive(const MemRegion *region);
513
514  /// \brief Set to the value of the symbolic store after
515  /// StoreManager::removeDeadBindings has been called.
516  void setReapedStore(StoreRef st) { reapedStore = st; }
517
518private:
519  /// Mark the symbols dependent on the input symbol as live.
520  void markDependentsLive(SymbolRef sym);
521};
522
523class SymbolVisitor {
524public:
525  /// \brief A visitor method invoked by ProgramStateManager::scanReachableSymbols.
526  ///
527  /// The method returns \c true if symbols should continue be scanned and \c
528  /// false otherwise.
529  virtual bool VisitSymbol(SymbolRef sym) = 0;
530  virtual bool VisitMemRegion(const MemRegion *region) { return true; }
531  virtual ~SymbolVisitor();
532};
533
534} // end GR namespace
535
536} // end clang namespace
537
538namespace llvm {
539static inline raw_ostream &operator<<(raw_ostream &os,
540                                            const clang::ento::SymExpr *SE) {
541  SE->dumpToStream(os);
542  return os;
543}
544} // end llvm namespace
545#endif
546