1 //== SymbolManager.h - Management of Symbolic Values ------------*- C++ -*--==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines SymbolManager, a class that manages symbolic values
11 // created for use by ExprEngine and related classes.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_SYMBOLMANAGER_H
16 #define LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_SYMBOLMANAGER_H
18 #include "clang/AST/Decl.h"
19 #include "clang/AST/Expr.h"
20 #include "clang/Analysis/AnalysisContext.h"
21 #include "clang/Basic/LLVM.h"
22 #include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h"
23 #include "clang/StaticAnalyzer/Core/PathSensitive/StoreRef.h"
24 #include "clang/StaticAnalyzer/Core/PathSensitive/SymExpr.h"
25 #include "llvm/ADT/DenseMap.h"
26 #include "llvm/ADT/DenseSet.h"
27 #include "llvm/ADT/FoldingSet.h"
28 #include "llvm/Support/Allocator.h"
29 #include "llvm/Support/DataTypes.h"
33 class StackFrameContext;
36 class BasicValueFactory;
38 class TypedValueRegion;
41 ///\brief A symbol representing the value stored at a MemRegion.
42 class SymbolRegionValue : public SymbolData {
43 const TypedValueRegion *R;
46 SymbolRegionValue(SymbolID sym, const TypedValueRegion *r)
47 : SymbolData(SymbolRegionValueKind, sym), R(r) {
49 assert(isValidTypeForSymbol(r->getValueType()));
52 const TypedValueRegion* getRegion() const { return R; }
54 static void Profile(llvm::FoldingSetNodeID& profile, const TypedValueRegion* R) {
55 profile.AddInteger((unsigned) SymbolRegionValueKind);
56 profile.AddPointer(R);
59 void Profile(llvm::FoldingSetNodeID& profile) override {
63 void dumpToStream(raw_ostream &os) const override;
64 const MemRegion *getOriginRegion() const override { return getRegion(); }
66 QualType getType() const override;
68 // Implement isa<T> support.
69 static inline bool classof(const SymExpr *SE) {
70 return SE->getKind() == SymbolRegionValueKind;
74 /// A symbol representing the result of an expression in the case when we do
75 /// not know anything about what the expression is.
76 class SymbolConjured : public SymbolData {
80 const LocationContext *LCtx;
81 const void *SymbolTag;
84 SymbolConjured(SymbolID sym, const Stmt *s, const LocationContext *lctx,
85 QualType t, unsigned count, const void *symbolTag)
86 : SymbolData(SymbolConjuredKind, sym), S(s), T(t), Count(count),
87 LCtx(lctx), SymbolTag(symbolTag) {
88 // FIXME: 's' might be a nullptr if we're conducting invalidation
89 // that was caused by a destructor call on a temporary object,
90 // which has no statement associated with it.
91 // Due to this, we might be creating the same invalidation symbol for
92 // two different invalidation passes (for two different temporaries).
94 assert(isValidTypeForSymbol(t));
97 const Stmt *getStmt() const { return S; }
98 unsigned getCount() const { return Count; }
99 const void *getTag() const { return SymbolTag; }
101 QualType getType() const override;
103 void dumpToStream(raw_ostream &os) const override;
105 static void Profile(llvm::FoldingSetNodeID& profile, const Stmt *S,
106 QualType T, unsigned Count, const LocationContext *LCtx,
107 const void *SymbolTag) {
108 profile.AddInteger((unsigned) SymbolConjuredKind);
109 profile.AddPointer(S);
110 profile.AddPointer(LCtx);
112 profile.AddInteger(Count);
113 profile.AddPointer(SymbolTag);
116 void Profile(llvm::FoldingSetNodeID& profile) override {
117 Profile(profile, S, T, Count, LCtx, SymbolTag);
120 // Implement isa<T> support.
121 static inline bool classof(const SymExpr *SE) {
122 return SE->getKind() == SymbolConjuredKind;
126 /// A symbol representing the value of a MemRegion whose parent region has
128 class SymbolDerived : public SymbolData {
129 SymbolRef parentSymbol;
130 const TypedValueRegion *R;
133 SymbolDerived(SymbolID sym, SymbolRef parent, const TypedValueRegion *r)
134 : SymbolData(SymbolDerivedKind, sym), parentSymbol(parent), R(r) {
137 assert(isValidTypeForSymbol(r->getValueType()));
140 SymbolRef getParentSymbol() const { return parentSymbol; }
141 const TypedValueRegion *getRegion() const { return R; }
143 QualType getType() const override;
145 void dumpToStream(raw_ostream &os) const override;
146 const MemRegion *getOriginRegion() const override { return getRegion(); }
148 static void Profile(llvm::FoldingSetNodeID& profile, SymbolRef parent,
149 const TypedValueRegion *r) {
150 profile.AddInteger((unsigned) SymbolDerivedKind);
151 profile.AddPointer(r);
152 profile.AddPointer(parent);
155 void Profile(llvm::FoldingSetNodeID& profile) override {
156 Profile(profile, parentSymbol, R);
159 // Implement isa<T> support.
160 static inline bool classof(const SymExpr *SE) {
161 return SE->getKind() == SymbolDerivedKind;
165 /// SymbolExtent - Represents the extent (size in bytes) of a bounded region.
166 /// Clients should not ask the SymbolManager for a region's extent. Always use
167 /// SubRegion::getExtent instead -- the value returned may not be a symbol.
168 class SymbolExtent : public SymbolData {
172 SymbolExtent(SymbolID sym, const SubRegion *r)
173 : SymbolData(SymbolExtentKind, sym), R(r) {
177 const SubRegion *getRegion() const { return R; }
179 QualType getType() const override;
181 void dumpToStream(raw_ostream &os) const override;
183 static void Profile(llvm::FoldingSetNodeID& profile, const SubRegion *R) {
184 profile.AddInteger((unsigned) SymbolExtentKind);
185 profile.AddPointer(R);
188 void Profile(llvm::FoldingSetNodeID& profile) override {
192 // Implement isa<T> support.
193 static inline bool classof(const SymExpr *SE) {
194 return SE->getKind() == SymbolExtentKind;
198 /// SymbolMetadata - Represents path-dependent metadata about a specific region.
199 /// Metadata symbols remain live as long as they are marked as in use before
200 /// dead-symbol sweeping AND their associated regions are still alive.
201 /// Intended for use by checkers.
202 class SymbolMetadata : public SymbolData {
206 const LocationContext *LCtx;
210 SymbolMetadata(SymbolID sym, const MemRegion* r, const Stmt *s, QualType t,
211 const LocationContext *LCtx, unsigned count, const void *tag)
212 : SymbolData(SymbolMetadataKind, sym), R(r), S(s), T(t), LCtx(LCtx),
213 Count(count), Tag(tag) {
216 assert(isValidTypeForSymbol(t));
221 const MemRegion *getRegion() const { return R; }
222 const Stmt *getStmt() const { return S; }
223 const LocationContext *getLocationContext() const { return LCtx; }
224 unsigned getCount() const { return Count; }
225 const void *getTag() const { return Tag; }
227 QualType getType() const override;
229 void dumpToStream(raw_ostream &os) const override;
231 static void Profile(llvm::FoldingSetNodeID& profile, const MemRegion *R,
232 const Stmt *S, QualType T, const LocationContext *LCtx,
233 unsigned Count, const void *Tag) {
234 profile.AddInteger((unsigned) SymbolMetadataKind);
235 profile.AddPointer(R);
236 profile.AddPointer(S);
238 profile.AddPointer(LCtx);
239 profile.AddInteger(Count);
240 profile.AddPointer(Tag);
243 void Profile(llvm::FoldingSetNodeID& profile) override {
244 Profile(profile, R, S, T, LCtx, Count, Tag);
247 // Implement isa<T> support.
248 static inline bool classof(const SymExpr *SE) {
249 return SE->getKind() == SymbolMetadataKind;
253 /// \brief Represents a cast expression.
254 class SymbolCast : public SymExpr {
255 const SymExpr *Operand;
256 /// Type of the operand.
258 /// The type of the result.
262 SymbolCast(const SymExpr *In, QualType From, QualType To)
263 : SymExpr(SymbolCastKind), Operand(In), FromTy(From), ToTy(To) {
265 assert(isValidTypeForSymbol(From));
266 // FIXME: GenericTaintChecker creates symbols of void type.
267 // Otherwise, 'To' should also be a valid type.
270 QualType getType() const override { return ToTy; }
272 const SymExpr *getOperand() const { return Operand; }
274 void dumpToStream(raw_ostream &os) const override;
276 static void Profile(llvm::FoldingSetNodeID& ID,
277 const SymExpr *In, QualType From, QualType To) {
278 ID.AddInteger((unsigned) SymbolCastKind);
284 void Profile(llvm::FoldingSetNodeID& ID) override {
285 Profile(ID, Operand, FromTy, ToTy);
288 // Implement isa<T> support.
289 static inline bool classof(const SymExpr *SE) {
290 return SE->getKind() == SymbolCastKind;
294 /// \brief Represents a symbolic expression involving a binary operator
295 class BinarySymExpr : public SymExpr {
296 BinaryOperator::Opcode Op;
300 BinarySymExpr(Kind k, BinaryOperator::Opcode op, QualType t)
301 : SymExpr(k), Op(op), T(t) {
302 assert(classof(this));
303 assert(isValidTypeForSymbol(t));
307 // FIXME: We probably need to make this out-of-line to avoid redundant
308 // generation of virtual functions.
309 QualType getType() const override { return T; }
311 BinaryOperator::Opcode getOpcode() const { return Op; }
313 // Implement isa<T> support.
314 static inline bool classof(const SymExpr *SE) {
315 Kind k = SE->getKind();
316 return k >= BEGIN_BINARYSYMEXPRS && k <= END_BINARYSYMEXPRS;
320 /// \brief Represents a symbolic expression like 'x' + 3.
321 class SymIntExpr : public BinarySymExpr {
323 const llvm::APSInt& RHS;
326 SymIntExpr(const SymExpr *lhs, BinaryOperator::Opcode op,
327 const llvm::APSInt &rhs, QualType t)
328 : BinarySymExpr(SymIntExprKind, op, t), LHS(lhs), RHS(rhs) {
332 void dumpToStream(raw_ostream &os) const override;
334 const SymExpr *getLHS() const { return LHS; }
335 const llvm::APSInt &getRHS() const { return RHS; }
337 static void Profile(llvm::FoldingSetNodeID& ID, const SymExpr *lhs,
338 BinaryOperator::Opcode op, const llvm::APSInt& rhs,
340 ID.AddInteger((unsigned) SymIntExprKind);
347 void Profile(llvm::FoldingSetNodeID& ID) override {
348 Profile(ID, LHS, getOpcode(), RHS, getType());
351 // Implement isa<T> support.
352 static inline bool classof(const SymExpr *SE) {
353 return SE->getKind() == SymIntExprKind;
357 /// \brief Represents a symbolic expression like 3 - 'x'.
358 class IntSymExpr : public BinarySymExpr {
359 const llvm::APSInt& LHS;
363 IntSymExpr(const llvm::APSInt &lhs, BinaryOperator::Opcode op,
364 const SymExpr *rhs, QualType t)
365 : BinarySymExpr(IntSymExprKind, op, t), LHS(lhs), RHS(rhs) {
369 void dumpToStream(raw_ostream &os) const override;
371 const SymExpr *getRHS() const { return RHS; }
372 const llvm::APSInt &getLHS() const { return LHS; }
374 static void Profile(llvm::FoldingSetNodeID& ID, const llvm::APSInt& lhs,
375 BinaryOperator::Opcode op, const SymExpr *rhs,
377 ID.AddInteger((unsigned) IntSymExprKind);
384 void Profile(llvm::FoldingSetNodeID& ID) override {
385 Profile(ID, LHS, getOpcode(), RHS, getType());
388 // Implement isa<T> support.
389 static inline bool classof(const SymExpr *SE) {
390 return SE->getKind() == IntSymExprKind;
394 /// \brief Represents a symbolic expression like 'x' + 'y'.
395 class SymSymExpr : public BinarySymExpr {
400 SymSymExpr(const SymExpr *lhs, BinaryOperator::Opcode op, const SymExpr *rhs,
402 : BinarySymExpr(SymSymExprKind, op, t), LHS(lhs), RHS(rhs) {
407 const SymExpr *getLHS() const { return LHS; }
408 const SymExpr *getRHS() const { return RHS; }
410 void dumpToStream(raw_ostream &os) const override;
412 static void Profile(llvm::FoldingSetNodeID& ID, const SymExpr *lhs,
413 BinaryOperator::Opcode op, const SymExpr *rhs, QualType t) {
414 ID.AddInteger((unsigned) SymSymExprKind);
421 void Profile(llvm::FoldingSetNodeID& ID) override {
422 Profile(ID, LHS, getOpcode(), RHS, getType());
425 // Implement isa<T> support.
426 static inline bool classof(const SymExpr *SE) {
427 return SE->getKind() == SymSymExprKind;
431 class SymbolManager {
432 typedef llvm::FoldingSet<SymExpr> DataSetTy;
433 typedef llvm::DenseMap<SymbolRef, SymbolRefSmallVectorTy*> SymbolDependTy;
436 /// Stores the extra dependencies between symbols: the data should be kept
437 /// alive as long as the key is live.
438 SymbolDependTy SymbolDependencies;
439 unsigned SymbolCounter;
440 llvm::BumpPtrAllocator& BPAlloc;
441 BasicValueFactory &BV;
445 SymbolManager(ASTContext &ctx, BasicValueFactory &bv,
446 llvm::BumpPtrAllocator& bpalloc)
447 : SymbolDependencies(16), SymbolCounter(0),
448 BPAlloc(bpalloc), BV(bv), Ctx(ctx) {}
452 static bool canSymbolicate(QualType T);
454 /// \brief Make a unique symbol for MemRegion R according to its kind.
455 const SymbolRegionValue* getRegionValueSymbol(const TypedValueRegion* R);
457 const SymbolConjured* conjureSymbol(const Stmt *E,
458 const LocationContext *LCtx,
461 const void *SymbolTag = nullptr);
463 const SymbolConjured* conjureSymbol(const Expr *E,
464 const LocationContext *LCtx,
466 const void *SymbolTag = nullptr) {
467 return conjureSymbol(E, LCtx, E->getType(), VisitCount, SymbolTag);
470 const SymbolDerived *getDerivedSymbol(SymbolRef parentSymbol,
471 const TypedValueRegion *R);
473 const SymbolExtent *getExtentSymbol(const SubRegion *R);
475 /// \brief Creates a metadata symbol associated with a specific region.
477 /// VisitCount can be used to differentiate regions corresponding to
478 /// different loop iterations, thus, making the symbol path-dependent.
479 const SymbolMetadata *getMetadataSymbol(const MemRegion *R, const Stmt *S,
481 const LocationContext *LCtx,
483 const void *SymbolTag = nullptr);
485 const SymbolCast* getCastSymbol(const SymExpr *Operand,
486 QualType From, QualType To);
488 const SymIntExpr *getSymIntExpr(const SymExpr *lhs, BinaryOperator::Opcode op,
489 const llvm::APSInt& rhs, QualType t);
491 const SymIntExpr *getSymIntExpr(const SymExpr &lhs, BinaryOperator::Opcode op,
492 const llvm::APSInt& rhs, QualType t) {
493 return getSymIntExpr(&lhs, op, rhs, t);
496 const IntSymExpr *getIntSymExpr(const llvm::APSInt& lhs,
497 BinaryOperator::Opcode op,
498 const SymExpr *rhs, QualType t);
500 const SymSymExpr *getSymSymExpr(const SymExpr *lhs, BinaryOperator::Opcode op,
501 const SymExpr *rhs, QualType t);
503 QualType getType(const SymExpr *SE) const {
504 return SE->getType();
507 /// \brief Add artificial symbol dependency.
509 /// The dependent symbol should stay alive as long as the primary is alive.
510 void addSymbolDependency(const SymbolRef Primary, const SymbolRef Dependent);
512 const SymbolRefSmallVectorTy *getDependentSymbols(const SymbolRef Primary);
514 ASTContext &getContext() { return Ctx; }
515 BasicValueFactory &getBasicVals() { return BV; }
518 /// \brief A class responsible for cleaning up unused symbols.
525 typedef llvm::DenseSet<SymbolRef> SymbolSetTy;
526 typedef llvm::DenseMap<SymbolRef, SymbolStatus> SymbolMapTy;
527 typedef llvm::DenseSet<const MemRegion *> RegionSetTy;
529 SymbolMapTy TheLiving;
530 SymbolSetTy MetadataInUse;
533 RegionSetTy RegionRoots;
535 const StackFrameContext *LCtx;
537 SymbolManager& SymMgr;
538 StoreRef reapedStore;
539 llvm::DenseMap<const MemRegion *, unsigned> includedRegionCache;
542 /// \brief Construct a reaper object, which removes everything which is not
543 /// live before we execute statement s in the given location context.
545 /// If the statement is NULL, everything is this and parent contexts is
547 /// If the stack frame context is NULL, everything on stack is considered
549 SymbolReaper(const StackFrameContext *Ctx, const Stmt *s, SymbolManager& symmgr,
550 StoreManager &storeMgr)
551 : LCtx(Ctx), Loc(s), SymMgr(symmgr),
552 reapedStore(nullptr, storeMgr) {}
554 const LocationContext *getLocationContext() const { return LCtx; }
556 bool isLive(SymbolRef sym);
557 bool isLiveRegion(const MemRegion *region);
558 bool isLive(const Stmt *ExprVal, const LocationContext *LCtx) const;
559 bool isLive(const VarRegion *VR, bool includeStoreBindings = false) const;
561 /// \brief Unconditionally marks a symbol as live.
563 /// This should never be
564 /// used by checkers, only by the state infrastructure such as the store and
565 /// environment. Checkers should instead use metadata symbols and markInUse.
566 void markLive(SymbolRef sym);
568 /// \brief Marks a symbol as important to a checker.
570 /// For metadata symbols,
571 /// this will keep the symbol alive as long as its associated region is also
572 /// live. For other symbols, this has no effect; checkers are not permitted
573 /// to influence the life of other symbols. This should be used before any
574 /// symbol marking has occurred, i.e. in the MarkLiveSymbols callback.
575 void markInUse(SymbolRef sym);
577 /// \brief If a symbol is known to be live, marks the symbol as live.
579 /// Otherwise, if the symbol cannot be proven live, it is marked as dead.
580 /// Returns true if the symbol is dead, false if live.
581 bool maybeDead(SymbolRef sym);
583 typedef SymbolSetTy::const_iterator dead_iterator;
584 dead_iterator dead_begin() const { return TheDead.begin(); }
585 dead_iterator dead_end() const { return TheDead.end(); }
587 bool hasDeadSymbols() const {
588 return !TheDead.empty();
591 typedef RegionSetTy::const_iterator region_iterator;
592 region_iterator region_begin() const { return RegionRoots.begin(); }
593 region_iterator region_end() const { return RegionRoots.end(); }
595 /// \brief Returns whether or not a symbol has been confirmed dead.
597 /// This should only be called once all marking of dead symbols has completed.
598 /// (For checkers, this means only in the evalDeadSymbols callback.)
599 bool isDead(SymbolRef sym) const {
600 return TheDead.count(sym);
603 void markLive(const MemRegion *region);
604 void markElementIndicesLive(const MemRegion *region);
606 /// \brief Set to the value of the symbolic store after
607 /// StoreManager::removeDeadBindings has been called.
608 void setReapedStore(StoreRef st) { reapedStore = st; }
611 /// Mark the symbols dependent on the input symbol as live.
612 void markDependentsLive(SymbolRef sym);
615 class SymbolVisitor {
617 ~SymbolVisitor() = default;
620 SymbolVisitor() = default;
621 SymbolVisitor(const SymbolVisitor &) = default;
622 SymbolVisitor(SymbolVisitor &&) {}
624 /// \brief A visitor method invoked by ProgramStateManager::scanReachableSymbols.
626 /// The method returns \c true if symbols should continue be scanned and \c
628 virtual bool VisitSymbol(SymbolRef sym) = 0;
629 virtual bool VisitMemRegion(const MemRegion *region) { return true; }
632 } // end GR namespace
634 } // end clang namespace
637 static inline raw_ostream &operator<<(raw_ostream &os,
638 const clang::ento::SymExpr *SE) {
639 SE->dumpToStream(os);
642 } // end llvm namespace