Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 1 | //== RegionStore.cpp - Field-sensitive store model --------------*- 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 a basic region store model. In this model, we do have field |
| 11 | // sensitivity. But we assume nothing about the heap shape. So recursive data |
| 12 | // structures are largely ignored. Basically we do 1-limiting analysis. |
| 13 | // Parameter pointers are assumed with no aliasing. Pointee objects of |
| 14 | // parameters are created lazily. |
| 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | #include "clang/Analysis/PathSensitive/MemRegion.h" |
| 18 | #include "clang/Analysis/PathSensitive/GRState.h" |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 19 | #include "clang/Analysis/PathSensitive/GRStateTrait.h" |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 20 | #include "clang/Analysis/Analyses/LiveVariables.h" |
| 21 | |
| 22 | #include "llvm/ADT/ImmutableMap.h" |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/ImmutableList.h" |
Zhongxing Xu | a071eb0 | 2008-10-24 06:01:33 +0000 | [diff] [blame] | 24 | #include "llvm/Support/raw_ostream.h" |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Compiler.h" |
| 26 | |
| 27 | using namespace clang; |
| 28 | |
Zhongxing Xu | baf03a7 | 2008-11-24 09:44:56 +0000 | [diff] [blame] | 29 | // Actual Store type. |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 30 | typedef llvm::ImmutableMap<const MemRegion*, SVal> RegionBindingsTy; |
Zhongxing Xu | baf03a7 | 2008-11-24 09:44:56 +0000 | [diff] [blame] | 31 | |
Ted Kremenek | 50dc1b3 | 2008-12-24 01:05:03 +0000 | [diff] [blame] | 32 | //===----------------------------------------------------------------------===// |
| 33 | // Region "Views" |
| 34 | //===----------------------------------------------------------------------===// |
| 35 | // |
| 36 | // MemRegions can be layered on top of each other. This GDM entry tracks |
| 37 | // what are the MemRegions that layer a given MemRegion. |
| 38 | // |
Zhongxing Xu | 5834ed6 | 2009-01-13 01:49:57 +0000 | [diff] [blame] | 39 | typedef llvm::ImmutableSet<const MemRegion*> RegionViews; |
Ted Kremenek | 50dc1b3 | 2008-12-24 01:05:03 +0000 | [diff] [blame] | 40 | namespace { class VISIBILITY_HIDDEN RegionViewMap {}; } |
| 41 | static int RegionViewMapIndex = 0; |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 42 | namespace clang { |
Ted Kremenek | 50dc1b3 | 2008-12-24 01:05:03 +0000 | [diff] [blame] | 43 | template<> struct GRStateTrait<RegionViewMap> |
| 44 | : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*, |
| 45 | RegionViews> > { |
| 46 | |
| 47 | static void* GDMIndex() { return &RegionViewMapIndex; } |
| 48 | }; |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 49 | } |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 50 | |
Ted Kremenek | 50dc1b3 | 2008-12-24 01:05:03 +0000 | [diff] [blame] | 51 | //===----------------------------------------------------------------------===// |
| 52 | // Region "Extents" |
| 53 | //===----------------------------------------------------------------------===// |
| 54 | // |
| 55 | // MemRegions represent chunks of memory with a size (their "extent"). This |
| 56 | // GDM entry tracks the extents for regions. Extents are in bytes. |
Ted Kremenek | d6cfbe4 | 2009-01-07 22:18:50 +0000 | [diff] [blame] | 57 | // |
Ted Kremenek | 50dc1b3 | 2008-12-24 01:05:03 +0000 | [diff] [blame] | 58 | namespace { class VISIBILITY_HIDDEN RegionExtents {}; } |
| 59 | static int RegionExtentsIndex = 0; |
Zhongxing Xu | baf03a7 | 2008-11-24 09:44:56 +0000 | [diff] [blame] | 60 | namespace clang { |
Ted Kremenek | 50dc1b3 | 2008-12-24 01:05:03 +0000 | [diff] [blame] | 61 | template<> struct GRStateTrait<RegionExtents> |
| 62 | : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*, SVal> > { |
| 63 | static void* GDMIndex() { return &RegionExtentsIndex; } |
| 64 | }; |
Zhongxing Xu | baf03a7 | 2008-11-24 09:44:56 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Ted Kremenek | 50dc1b3 | 2008-12-24 01:05:03 +0000 | [diff] [blame] | 67 | //===----------------------------------------------------------------------===// |
| 68 | // Region "killsets". |
| 69 | //===----------------------------------------------------------------------===// |
| 70 | // |
Zhongxing Xu | 5834ed6 | 2009-01-13 01:49:57 +0000 | [diff] [blame] | 71 | // RegionStore lazily adds value bindings to regions when the analyzer handles |
| 72 | // assignment statements. Killsets track which default values have been |
| 73 | // killed, thus distinguishing between "unknown" values and default |
| 74 | // values. Regions are added to killset only when they are assigned "unknown" |
| 75 | // directly, otherwise we should have their value in the region bindings. |
Ted Kremenek | 50dc1b3 | 2008-12-24 01:05:03 +0000 | [diff] [blame] | 76 | // |
| 77 | namespace { class VISIBILITY_HIDDEN RegionKills {}; } |
Ted Kremenek | 2ed14be | 2008-12-05 00:47:52 +0000 | [diff] [blame] | 78 | static int RegionKillsIndex = 0; |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 79 | namespace clang { |
Ted Kremenek | 2ed14be | 2008-12-05 00:47:52 +0000 | [diff] [blame] | 80 | template<> struct GRStateTrait<RegionKills> |
Ted Kremenek | 50dc1b3 | 2008-12-24 01:05:03 +0000 | [diff] [blame] | 81 | : public GRStatePartialTrait< llvm::ImmutableSet<const MemRegion*> > { |
Ted Kremenek | 2ed14be | 2008-12-05 00:47:52 +0000 | [diff] [blame] | 82 | static void* GDMIndex() { return &RegionKillsIndex; } |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 83 | }; |
| 84 | } |
| 85 | |
Ted Kremenek | 50dc1b3 | 2008-12-24 01:05:03 +0000 | [diff] [blame] | 86 | //===----------------------------------------------------------------------===// |
Zhongxing Xu | 5834ed6 | 2009-01-13 01:49:57 +0000 | [diff] [blame] | 87 | // Regions with default values. |
Ted Kremenek | 50dc1b3 | 2008-12-24 01:05:03 +0000 | [diff] [blame] | 88 | //===----------------------------------------------------------------------===// |
| 89 | // |
Zhongxing Xu | 5834ed6 | 2009-01-13 01:49:57 +0000 | [diff] [blame] | 90 | // This GDM entry tracks what regions have a default value if they have no bound |
| 91 | // value and have not been killed. |
Ted Kremenek | 50dc1b3 | 2008-12-24 01:05:03 +0000 | [diff] [blame] | 92 | // |
| 93 | namespace { class VISIBILITY_HIDDEN RegionDefaultValue {}; } |
| 94 | static int RegionDefaultValueIndex = 0; |
| 95 | namespace clang { |
| 96 | template<> struct GRStateTrait<RegionDefaultValue> |
| 97 | : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*, SVal> > { |
| 98 | static void* GDMIndex() { return &RegionDefaultValueIndex; } |
| 99 | }; |
| 100 | } |
| 101 | |
| 102 | //===----------------------------------------------------------------------===// |
| 103 | // Main RegionStore logic. |
| 104 | //===----------------------------------------------------------------------===// |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 105 | |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 106 | namespace { |
| 107 | |
Ted Kremenek | 59e8f11 | 2009-03-03 01:35:36 +0000 | [diff] [blame] | 108 | class VISIBILITY_HIDDEN RegionStoreSubRegionMap : public SubRegionMap { |
| 109 | typedef llvm::DenseMap<const MemRegion*, |
| 110 | llvm::ImmutableSet<const MemRegion*> > Map; |
| 111 | |
| 112 | llvm::ImmutableSet<const MemRegion*>::Factory F; |
| 113 | Map M; |
| 114 | |
| 115 | public: |
| 116 | void add(const MemRegion* Parent, const MemRegion* SubRegion) { |
| 117 | Map::iterator I = M.find(Parent); |
| 118 | M.insert(std::make_pair(Parent, |
| 119 | F.Add(I == M.end() ? F.GetEmptySet() : I->second, SubRegion))); |
| 120 | } |
| 121 | |
| 122 | ~RegionStoreSubRegionMap() {} |
| 123 | |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 124 | bool iterSubRegions(const MemRegion* Parent, Visitor& V) const { |
Ted Kremenek | 59e8f11 | 2009-03-03 01:35:36 +0000 | [diff] [blame] | 125 | Map::iterator I = M.find(Parent); |
| 126 | |
| 127 | if (I == M.end()) |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 128 | return true; |
Ted Kremenek | 59e8f11 | 2009-03-03 01:35:36 +0000 | [diff] [blame] | 129 | |
| 130 | llvm::ImmutableSet<const MemRegion*> S = I->second; |
| 131 | for (llvm::ImmutableSet<const MemRegion*>::iterator SI=S.begin(),SE=S.end(); |
| 132 | SI != SE; ++SI) { |
| 133 | if (!V.Visit(Parent, *SI)) |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 134 | return false; |
Ted Kremenek | 59e8f11 | 2009-03-03 01:35:36 +0000 | [diff] [blame] | 135 | } |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 136 | |
| 137 | return true; |
Ted Kremenek | 59e8f11 | 2009-03-03 01:35:36 +0000 | [diff] [blame] | 138 | } |
| 139 | }; |
| 140 | |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 141 | class VISIBILITY_HIDDEN RegionStoreManager : public StoreManager { |
| 142 | RegionBindingsTy::Factory RBFactory; |
Ted Kremenek | 50dc1b3 | 2008-12-24 01:05:03 +0000 | [diff] [blame] | 143 | RegionViews::Factory RVFactory; |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 144 | |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 145 | GRStateManager& StateMgr; |
Ted Kremenek | 6fd8f91 | 2009-01-22 23:43:57 +0000 | [diff] [blame] | 146 | const MemRegion* SelfRegion; |
| 147 | const ImplicitParamDecl *SelfDecl; |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 148 | |
| 149 | public: |
| 150 | RegionStoreManager(GRStateManager& mgr) |
Ted Kremenek | d6cfbe4 | 2009-01-07 22:18:50 +0000 | [diff] [blame] | 151 | : StoreManager(mgr.getAllocator()), |
| 152 | RBFactory(mgr.getAllocator()), |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 153 | RVFactory(mgr.getAllocator()), |
Ted Kremenek | 6fd8f91 | 2009-01-22 23:43:57 +0000 | [diff] [blame] | 154 | StateMgr(mgr), SelfRegion(0), SelfDecl(0) { |
| 155 | if (const ObjCMethodDecl* MD = |
| 156 | dyn_cast<ObjCMethodDecl>(&StateMgr.getCodeDecl())) |
| 157 | SelfDecl = MD->getSelfDecl(); |
| 158 | } |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 159 | |
| 160 | virtual ~RegionStoreManager() {} |
| 161 | |
Zhongxing Xu | 24194ef | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 162 | MemRegionManager& getRegionManager() { return MRMgr; } |
Ted Kremenek | 4f09027 | 2008-10-27 21:54:31 +0000 | [diff] [blame] | 163 | |
Ted Kremenek | 14453bf | 2009-03-03 19:02:42 +0000 | [diff] [blame] | 164 | SubRegionMap* getSubRegionMap(const GRState *state); |
Ted Kremenek | 59e8f11 | 2009-03-03 01:35:36 +0000 | [diff] [blame] | 165 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 166 | const GRState* BindCompoundLiteral(const GRState* St, |
| 167 | const CompoundLiteralExpr* CL, SVal V); |
Zhongxing Xu | 24194ef | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 168 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 169 | /// getLValueString - Returns an SVal representing the lvalue of a |
| 170 | /// StringLiteral. Within RegionStore a StringLiteral has an |
| 171 | /// associated StringRegion, and the lvalue of a StringLiteral is |
| 172 | /// the lvalue of that region. |
Zhongxing Xu | 143bf82 | 2008-10-25 14:18:57 +0000 | [diff] [blame] | 173 | SVal getLValueString(const GRState* St, const StringLiteral* S); |
| 174 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 175 | /// getLValueCompoundLiteral - Returns an SVal representing the |
| 176 | /// lvalue of a compound literal. Within RegionStore a compound |
| 177 | /// literal has an associated region, and the lvalue of the |
| 178 | /// compound literal is the lvalue of that region. |
Zhongxing Xu | f22679e | 2008-11-07 10:38:33 +0000 | [diff] [blame] | 179 | SVal getLValueCompoundLiteral(const GRState* St, const CompoundLiteralExpr*); |
| 180 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 181 | /// getLValueVar - Returns an SVal that represents the lvalue of a |
| 182 | /// variable. Within RegionStore a variable has an associated |
| 183 | /// VarRegion, and the lvalue of the variable is the lvalue of that region. |
Zhongxing Xu | c4bf72c | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 184 | SVal getLValueVar(const GRState* St, const VarDecl* VD); |
| 185 | |
| 186 | SVal getLValueIvar(const GRState* St, const ObjCIvarDecl* D, SVal Base); |
| 187 | |
| 188 | SVal getLValueField(const GRState* St, SVal Base, const FieldDecl* D); |
Ted Kremenek | 3de2d3c | 2009-03-05 04:50:08 +0000 | [diff] [blame] | 189 | |
| 190 | SVal getLValueFieldOrIvar(const GRState* St, SVal Base, const Decl* D); |
Zhongxing Xu | c4bf72c | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 191 | |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 192 | SVal getLValueElement(const GRState* St, SVal Base, SVal Offset); |
| 193 | |
Zhongxing Xu | e8a964b | 2008-11-22 13:21:46 +0000 | [diff] [blame] | 194 | SVal getSizeInElements(const GRState* St, const MemRegion* R); |
| 195 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 196 | /// ArrayToPointer - Emulates the "decay" of an array to a pointer |
| 197 | /// type. 'Array' represents the lvalue of the array being decayed |
| 198 | /// to a pointer, and the returned SVal represents the decayed |
| 199 | /// version of that lvalue (i.e., a pointer to the first element of |
| 200 | /// the array). This is called by GRExprEngine when evaluating |
| 201 | /// casts from arrays to pointers. |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 202 | SVal ArrayToPointer(SVal Array); |
| 203 | |
Ted Kremenek | 6eddeb1 | 2008-12-13 21:49:13 +0000 | [diff] [blame] | 204 | /// CastRegion - Used by GRExprEngine::VisitCast to handle casts from |
| 205 | /// a MemRegion* to a specific location type. 'R' is the region being |
| 206 | /// casted and 'CastToTy' the result type of the cast. |
| 207 | CastResult CastRegion(const GRState* state, const MemRegion* R, |
| 208 | QualType CastToTy); |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 209 | |
Zhongxing Xu | 94aa6c1 | 2009-03-02 07:52:23 +0000 | [diff] [blame] | 210 | SVal EvalBinOp(BinaryOperator::Opcode Op, Loc L, NonLoc R); |
| 211 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 212 | /// The high level logic for this method is this: |
| 213 | /// Retrieve (L) |
| 214 | /// if L has binding |
| 215 | /// return L's binding |
| 216 | /// else if L is in killset |
| 217 | /// return unknown |
| 218 | /// else |
| 219 | /// if L is on stack or heap |
| 220 | /// return undefined |
| 221 | /// else |
| 222 | /// return symbolic |
Ted Kremenek | 2ed14be | 2008-12-05 00:47:52 +0000 | [diff] [blame] | 223 | SVal Retrieve(const GRState* state, Loc L, QualType T = QualType()); |
Zhongxing Xu | c4bf72c | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 224 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 225 | const GRState* Bind(const GRState* St, Loc LV, SVal V); |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 226 | |
Zhongxing Xu | 9c9ca08 | 2008-12-16 02:36:30 +0000 | [diff] [blame] | 227 | Store Remove(Store store, Loc LV); |
Zhongxing Xu | 24194ef | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 228 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 229 | Store getInitialStore() { return RBFactory.GetEmptyMap().getRoot(); } |
Ted Kremenek | 9deb0e3 | 2008-10-24 20:32:16 +0000 | [diff] [blame] | 230 | |
| 231 | /// getSelfRegion - Returns the region for the 'self' (Objective-C) or |
| 232 | /// 'this' object (C++). When used when analyzing a normal function this |
| 233 | /// method returns NULL. |
| 234 | const MemRegion* getSelfRegion(Store) { |
Ted Kremenek | 6fd8f91 | 2009-01-22 23:43:57 +0000 | [diff] [blame] | 235 | if (!SelfDecl) |
| 236 | return 0; |
| 237 | |
| 238 | if (!SelfRegion) { |
| 239 | const ObjCMethodDecl *MD = cast<ObjCMethodDecl>(&StateMgr.getCodeDecl()); |
| 240 | SelfRegion = MRMgr.getObjCObjectRegion(MD->getClassInterface(), |
| 241 | MRMgr.getHeapRegion()); |
| 242 | } |
| 243 | |
| 244 | return SelfRegion; |
Ted Kremenek | 9deb0e3 | 2008-10-24 20:32:16 +0000 | [diff] [blame] | 245 | } |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 246 | |
Ted Kremenek | 2ed14be | 2008-12-05 00:47:52 +0000 | [diff] [blame] | 247 | /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values. |
| 248 | /// It returns a new Store with these values removed, and populates LSymbols |
| 249 | // and DSymbols with the known set of live and dead symbols respectively. |
| 250 | Store RemoveDeadBindings(const GRState* state, Stmt* Loc, |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 251 | SymbolReaper& SymReaper, |
| 252 | llvm::SmallVectorImpl<const MemRegion*>& RegionRoots); |
Zhongxing Xu | 24194ef | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 253 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 254 | const GRState* BindDecl(const GRState* St, const VarDecl* VD, SVal InitVal); |
| 255 | |
| 256 | const GRState* BindDeclWithNoInit(const GRState* St, const VarDecl* VD) { |
| 257 | return St; |
| 258 | } |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 259 | |
Zhongxing Xu | baf03a7 | 2008-11-24 09:44:56 +0000 | [diff] [blame] | 260 | const GRState* setExtent(const GRState* St, const MemRegion* R, SVal Extent); |
| 261 | |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 262 | static inline RegionBindingsTy GetRegionBindings(Store store) { |
Zhongxing Xu | 9c9ca08 | 2008-12-16 02:36:30 +0000 | [diff] [blame] | 263 | return RegionBindingsTy(static_cast<const RegionBindingsTy::TreeTy*>(store)); |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 264 | } |
Zhongxing Xu | 24194ef | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 265 | |
Zhongxing Xu | 5b8b6f2 | 2008-10-24 04:33:15 +0000 | [diff] [blame] | 266 | void print(Store store, std::ostream& Out, const char* nl, const char *sep); |
Zhongxing Xu | 24194ef | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 267 | |
| 268 | void iterBindings(Store store, BindingsHandler& f) { |
| 269 | // FIXME: Implement. |
| 270 | } |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 271 | |
| 272 | private: |
| 273 | Loc getVarLoc(const VarDecl* VD) { |
| 274 | return loc::MemRegionVal(MRMgr.getVarRegion(VD)); |
| 275 | } |
| 276 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 277 | const GRState* BindArray(const GRState* St, const TypedRegion* R, SVal V); |
Zhongxing Xu | 6e3f01c | 2008-10-31 07:16:08 +0000 | [diff] [blame] | 278 | |
Zhongxing Xu | 0b242ec | 2008-12-04 01:12:41 +0000 | [diff] [blame] | 279 | /// Retrieve the values in a struct and return a CompoundVal, used when doing |
| 280 | /// struct copy: |
| 281 | /// struct s x, y; |
| 282 | /// x = y; |
| 283 | /// y's value is retrieved by this method. |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 284 | SVal RetrieveStruct(const GRState* St, const TypedRegion* R); |
Zhongxing Xu | 0b242ec | 2008-12-04 01:12:41 +0000 | [diff] [blame] | 285 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 286 | const GRState* BindStruct(const GRState* St, const TypedRegion* R, SVal V); |
Zhongxing Xu | 63123d8 | 2008-11-23 04:30:35 +0000 | [diff] [blame] | 287 | |
Zhongxing Xu | 5834ed6 | 2009-01-13 01:49:57 +0000 | [diff] [blame] | 288 | /// KillStruct - Set the entire struct to unknown. |
| 289 | const GRState* KillStruct(const GRState* St, const TypedRegion* R); |
| 290 | |
Zhongxing Xu | 6e3f01c | 2008-10-31 07:16:08 +0000 | [diff] [blame] | 291 | // Utility methods. |
| 292 | BasicValueFactory& getBasicVals() { return StateMgr.getBasicVals(); } |
| 293 | ASTContext& getContext() { return StateMgr.getContext(); } |
Zhongxing Xu | 63123d8 | 2008-11-23 04:30:35 +0000 | [diff] [blame] | 294 | SymbolManager& getSymbolManager() { return StateMgr.getSymbolManager(); } |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 295 | |
| 296 | const GRState* AddRegionView(const GRState* St, |
| 297 | const MemRegion* View, const MemRegion* Base); |
Zhongxing Xu | 5834ed6 | 2009-01-13 01:49:57 +0000 | [diff] [blame] | 298 | const GRState* RemoveRegionView(const GRState* St, |
| 299 | const MemRegion* View, const MemRegion* Base); |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 300 | }; |
| 301 | |
| 302 | } // end anonymous namespace |
| 303 | |
Ted Kremenek | 95c7b00 | 2008-10-24 01:04:59 +0000 | [diff] [blame] | 304 | StoreManager* clang::CreateRegionStoreManager(GRStateManager& StMgr) { |
Zhongxing Xu | 24194ef | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 305 | return new RegionStoreManager(StMgr); |
Ted Kremenek | 95c7b00 | 2008-10-24 01:04:59 +0000 | [diff] [blame] | 306 | } |
| 307 | |
Ted Kremenek | 14453bf | 2009-03-03 19:02:42 +0000 | [diff] [blame] | 308 | SubRegionMap* RegionStoreManager::getSubRegionMap(const GRState *state) { |
Ted Kremenek | 59e8f11 | 2009-03-03 01:35:36 +0000 | [diff] [blame] | 309 | RegionBindingsTy B = GetRegionBindings(state->getStore()); |
| 310 | RegionStoreSubRegionMap *M = new RegionStoreSubRegionMap(); |
| 311 | |
| 312 | for (RegionBindingsTy::iterator I=B.begin(), E=B.end(); I!=E; ++I) { |
| 313 | if (const SubRegion* R = dyn_cast<SubRegion>(I.getKey())) |
| 314 | M->add(R->getSuperRegion(), R); |
| 315 | } |
| 316 | |
Ted Kremenek | 14453bf | 2009-03-03 19:02:42 +0000 | [diff] [blame] | 317 | return M; |
Ted Kremenek | 59e8f11 | 2009-03-03 01:35:36 +0000 | [diff] [blame] | 318 | } |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 319 | |
| 320 | /// getLValueString - Returns an SVal representing the lvalue of a |
| 321 | /// StringLiteral. Within RegionStore a StringLiteral has an |
| 322 | /// associated StringRegion, and the lvalue of a StringLiteral is the |
| 323 | /// lvalue of that region. |
Zhongxing Xu | 143bf82 | 2008-10-25 14:18:57 +0000 | [diff] [blame] | 324 | SVal RegionStoreManager::getLValueString(const GRState* St, |
| 325 | const StringLiteral* S) { |
| 326 | return loc::MemRegionVal(MRMgr.getStringRegion(S)); |
| 327 | } |
| 328 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 329 | /// getLValueVar - Returns an SVal that represents the lvalue of a |
| 330 | /// variable. Within RegionStore a variable has an associated |
| 331 | /// VarRegion, and the lvalue of the variable is the lvalue of that region. |
Zhongxing Xu | c4bf72c | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 332 | SVal RegionStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) { |
| 333 | return loc::MemRegionVal(MRMgr.getVarRegion(VD)); |
| 334 | } |
Zhongxing Xu | f22679e | 2008-11-07 10:38:33 +0000 | [diff] [blame] | 335 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 336 | /// getLValueCompoundLiteral - Returns an SVal representing the lvalue |
| 337 | /// of a compound literal. Within RegionStore a compound literal |
| 338 | /// has an associated region, and the lvalue of the compound literal |
| 339 | /// is the lvalue of that region. |
| 340 | SVal |
| 341 | RegionStoreManager::getLValueCompoundLiteral(const GRState* St, |
| 342 | const CompoundLiteralExpr* CL) { |
Zhongxing Xu | f22679e | 2008-11-07 10:38:33 +0000 | [diff] [blame] | 343 | return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL)); |
| 344 | } |
| 345 | |
Zhongxing Xu | c4bf72c | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 346 | SVal RegionStoreManager::getLValueIvar(const GRState* St, const ObjCIvarDecl* D, |
| 347 | SVal Base) { |
Ted Kremenek | 3de2d3c | 2009-03-05 04:50:08 +0000 | [diff] [blame] | 348 | return getLValueFieldOrIvar(St, Base, D); |
Zhongxing Xu | c4bf72c | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | SVal RegionStoreManager::getLValueField(const GRState* St, SVal Base, |
| 352 | const FieldDecl* D) { |
Ted Kremenek | 3de2d3c | 2009-03-05 04:50:08 +0000 | [diff] [blame] | 353 | return getLValueFieldOrIvar(St, Base, D); |
| 354 | } |
| 355 | |
| 356 | SVal RegionStoreManager::getLValueFieldOrIvar(const GRState* St, SVal Base, |
| 357 | const Decl* D) { |
Zhongxing Xu | c4bf72c | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 358 | if (Base.isUnknownOrUndef()) |
| 359 | return Base; |
| 360 | |
| 361 | Loc BaseL = cast<Loc>(Base); |
| 362 | const MemRegion* BaseR = 0; |
| 363 | |
| 364 | switch (BaseL.getSubKind()) { |
| 365 | case loc::MemRegionKind: |
| 366 | BaseR = cast<loc::MemRegionVal>(BaseL).getRegion(); |
| 367 | break; |
| 368 | |
| 369 | case loc::SymbolValKind: |
Zhongxing Xu | 026c663 | 2009-02-05 06:57:29 +0000 | [diff] [blame] | 370 | BaseR = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(&BaseL)->getSymbol(), |
| 371 | StateMgr.getSymbolManager()); |
Zhongxing Xu | c4bf72c | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 372 | break; |
| 373 | |
| 374 | case loc::GotoLabelKind: |
| 375 | case loc::FuncValKind: |
| 376 | // These are anormal cases. Flag an undefined value. |
| 377 | return UndefinedVal(); |
| 378 | |
| 379 | case loc::ConcreteIntKind: |
Zhongxing Xu | c4bf72c | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 380 | // While these seem funny, this can happen through casts. |
| 381 | // FIXME: What we should return is the field offset. For example, |
| 382 | // add the field offset to the integer value. That way funny things |
| 383 | // like this work properly: &(((struct foo *) 0xa)->f) |
| 384 | return Base; |
| 385 | |
| 386 | default: |
Zhongxing Xu | 13d1ee2 | 2008-11-07 08:57:30 +0000 | [diff] [blame] | 387 | assert(0 && "Unhandled Base."); |
Zhongxing Xu | c4bf72c | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 388 | return Base; |
| 389 | } |
Ted Kremenek | 3de2d3c | 2009-03-05 04:50:08 +0000 | [diff] [blame] | 390 | |
| 391 | // NOTE: We must have this check first because ObjCIvarDecl is a subclass |
| 392 | // of FieldDecl. |
| 393 | if (const ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(D)) |
| 394 | return loc::MemRegionVal(MRMgr.getObjCIvarRegion(ID, BaseR)); |
Zhongxing Xu | c4bf72c | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 395 | |
Ted Kremenek | 3de2d3c | 2009-03-05 04:50:08 +0000 | [diff] [blame] | 396 | return loc::MemRegionVal(MRMgr.getFieldRegion(cast<FieldDecl>(D), BaseR)); |
Zhongxing Xu | c4bf72c | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 397 | } |
| 398 | |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 399 | SVal RegionStoreManager::getLValueElement(const GRState* St, |
| 400 | SVal Base, SVal Offset) { |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 401 | |
Ted Kremenek | de7ec63 | 2009-03-09 22:44:49 +0000 | [diff] [blame^] | 402 | // If the base is an unknown or undefined value, just return it back. |
| 403 | // FIXME: For absolute pointer addresses, we just return that value back as |
| 404 | // well, although in reality we should return the offset added to that |
| 405 | // value. |
| 406 | if (Base.isUnknownOrUndef() || isa<loc::ConcreteInt>(Base)) |
Zhongxing Xu | 4a1513e | 2008-10-27 12:23:17 +0000 | [diff] [blame] | 407 | return Base; |
| 408 | |
Ted Kremenek | a7ac944 | 2009-01-22 20:27:48 +0000 | [diff] [blame] | 409 | // Only handle integer offsets... for now. |
| 410 | if (!isa<nonloc::ConcreteInt>(Offset)) |
Zhongxing Xu | e4d1393 | 2008-11-13 09:48:44 +0000 | [diff] [blame] | 411 | return UnknownVal(); |
Ted Kremenek | a7ac944 | 2009-01-22 20:27:48 +0000 | [diff] [blame] | 412 | |
Zhongxing Xu | a48f737 | 2009-02-06 08:44:27 +0000 | [diff] [blame] | 413 | const TypedRegion* BaseRegion = 0; |
| 414 | |
| 415 | if (isa<loc::SymbolVal>(Base)) |
| 416 | BaseRegion = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(Base).getSymbol(), |
| 417 | StateMgr.getSymbolManager()); |
| 418 | else |
| 419 | BaseRegion = cast<TypedRegion>(cast<loc::MemRegionVal>(Base).getRegion()); |
Ted Kremenek | a7ac944 | 2009-01-22 20:27:48 +0000 | [diff] [blame] | 420 | |
| 421 | // Pointer of any type can be cast and used as array base. |
| 422 | const ElementRegion *ElemR = dyn_cast<ElementRegion>(BaseRegion); |
| 423 | |
| 424 | if (!ElemR) { |
| 425 | // |
| 426 | // If the base region is not an ElementRegion, create one. |
| 427 | // This can happen in the following example: |
| 428 | // |
| 429 | // char *p = __builtin_alloc(10); |
| 430 | // p[1] = 8; |
| 431 | // |
Ted Kremenek | 0312c0e | 2009-03-01 05:44:08 +0000 | [diff] [blame] | 432 | // Observe that 'p' binds to an TypedViewRegion<AllocaRegion>. |
Ted Kremenek | a7ac944 | 2009-01-22 20:27:48 +0000 | [diff] [blame] | 433 | // |
Zhongxing Xu | 5ea2ffc | 2009-02-19 08:37:16 +0000 | [diff] [blame] | 434 | |
| 435 | // Offset might be unsigned. We have to convert it to signed ConcreteInt. |
| 436 | if (nonloc::ConcreteInt* CI = dyn_cast<nonloc::ConcreteInt>(&Offset)) { |
| 437 | const llvm::APSInt& OffI = CI->getValue(); |
| 438 | if (OffI.isUnsigned()) { |
| 439 | llvm::APSInt Tmp = OffI; |
| 440 | Tmp.setIsSigned(true); |
| 441 | Offset = NonLoc::MakeVal(getBasicVals(), Tmp); |
| 442 | } |
| 443 | } |
Ted Kremenek | a7ac944 | 2009-01-22 20:27:48 +0000 | [diff] [blame] | 444 | return loc::MemRegionVal(MRMgr.getElementRegion(Offset, BaseRegion)); |
Zhongxing Xu | e4d1393 | 2008-11-13 09:48:44 +0000 | [diff] [blame] | 445 | } |
Ted Kremenek | a7ac944 | 2009-01-22 20:27:48 +0000 | [diff] [blame] | 446 | |
| 447 | SVal BaseIdx = ElemR->getIndex(); |
| 448 | |
| 449 | if (!isa<nonloc::ConcreteInt>(BaseIdx)) |
| 450 | return UnknownVal(); |
| 451 | |
| 452 | const llvm::APSInt& BaseIdxI = cast<nonloc::ConcreteInt>(BaseIdx).getValue(); |
| 453 | const llvm::APSInt& OffI = cast<nonloc::ConcreteInt>(Offset).getValue(); |
| 454 | assert(BaseIdxI.isSigned()); |
| 455 | |
| 456 | // FIXME: This appears to be the assumption of this code. We should review |
| 457 | // whether or not BaseIdxI.getBitWidth() < OffI.getBitWidth(). If it |
| 458 | // can't we need to put a comment here. If it can, we should handle it. |
| 459 | assert(BaseIdxI.getBitWidth() >= OffI.getBitWidth()); |
Zhongxing Xu | e4d1393 | 2008-11-13 09:48:44 +0000 | [diff] [blame] | 460 | |
Ted Kremenek | a7ac944 | 2009-01-22 20:27:48 +0000 | [diff] [blame] | 461 | const TypedRegion *ArrayR = ElemR->getArrayRegion(); |
| 462 | SVal NewIdx; |
| 463 | |
| 464 | if (OffI.isUnsigned() || OffI.getBitWidth() < BaseIdxI.getBitWidth()) { |
| 465 | // 'Offset' might be unsigned. We have to convert it to signed and |
| 466 | // possibly extend it. |
| 467 | llvm::APSInt Tmp = OffI; |
| 468 | |
| 469 | if (OffI.getBitWidth() < BaseIdxI.getBitWidth()) |
| 470 | Tmp.extend(BaseIdxI.getBitWidth()); |
| 471 | |
| 472 | Tmp.setIsSigned(true); |
| 473 | Tmp += BaseIdxI; // Compute the new offset. |
Zhongxing Xu | 5ea2ffc | 2009-02-19 08:37:16 +0000 | [diff] [blame] | 474 | NewIdx = NonLoc::MakeVal(getBasicVals(), Tmp); |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 475 | } |
Ted Kremenek | a7ac944 | 2009-01-22 20:27:48 +0000 | [diff] [blame] | 476 | else |
| 477 | NewIdx = nonloc::ConcreteInt(getBasicVals().getValue(BaseIdxI + OffI)); |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 478 | |
Ted Kremenek | a7ac944 | 2009-01-22 20:27:48 +0000 | [diff] [blame] | 479 | return loc::MemRegionVal(MRMgr.getElementRegion(NewIdx, ArrayR)); |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 480 | } |
| 481 | |
Zhongxing Xu | e8a964b | 2008-11-22 13:21:46 +0000 | [diff] [blame] | 482 | SVal RegionStoreManager::getSizeInElements(const GRState* St, |
| 483 | const MemRegion* R) { |
| 484 | if (const VarRegion* VR = dyn_cast<VarRegion>(R)) { |
| 485 | // Get the type of the variable. |
Ted Kremenek | 14553ab | 2009-01-30 00:08:43 +0000 | [diff] [blame] | 486 | QualType T = VR->getDesugaredRValueType(getContext()); |
Zhongxing Xu | e8a964b | 2008-11-22 13:21:46 +0000 | [diff] [blame] | 487 | |
Ted Kremenek | 14553ab | 2009-01-30 00:08:43 +0000 | [diff] [blame] | 488 | // FIXME: Handle variable-length arrays. |
| 489 | if (isa<VariableArrayType>(T)) |
| 490 | return UnknownVal(); |
| 491 | |
| 492 | if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T)) { |
| 493 | // return the size as signed integer. |
| 494 | return NonLoc::MakeVal(getBasicVals(), CAT->getSize(), false); |
| 495 | } |
| 496 | |
| 497 | // Clients can use ordinary variables as if they were arrays. These |
| 498 | // essentially are arrays of size 1. |
| 499 | return NonLoc::MakeIntVal(getBasicVals(), 1, false); |
Zhongxing Xu | e8a964b | 2008-11-22 13:21:46 +0000 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | if (const StringRegion* SR = dyn_cast<StringRegion>(R)) { |
Zhongxing Xu | 6613d08 | 2008-11-24 02:18:56 +0000 | [diff] [blame] | 503 | const StringLiteral* Str = SR->getStringLiteral(); |
Zhongxing Xu | d0fd3b7 | 2008-11-24 02:30:48 +0000 | [diff] [blame] | 504 | // We intentionally made the size value signed because it participates in |
| 505 | // operations with signed indices. |
Ted Kremenek | 14553ab | 2009-01-30 00:08:43 +0000 | [diff] [blame] | 506 | return NonLoc::MakeIntVal(getBasicVals(), Str->getByteLength()+1, false); |
Zhongxing Xu | e8a964b | 2008-11-22 13:21:46 +0000 | [diff] [blame] | 507 | } |
| 508 | |
Ted Kremenek | 0312c0e | 2009-03-01 05:44:08 +0000 | [diff] [blame] | 509 | if (const TypedViewRegion* ATR = dyn_cast<TypedViewRegion>(R)) { |
Ted Kremenek | 2e84257 | 2009-01-22 23:56:56 +0000 | [diff] [blame] | 510 | #if 0 |
| 511 | // FIXME: This logic doesn't really work, as we can have all sorts of |
| 512 | // weird cases. For example, this crashes on test case 'rdar-6442306-1.m'. |
| 513 | // The weird cases come in when arbitrary casting comes into play, violating |
| 514 | // any type-safe programming. |
| 515 | |
Zhongxing Xu | baf03a7 | 2008-11-24 09:44:56 +0000 | [diff] [blame] | 516 | GRStateRef state(St, StateMgr); |
| 517 | |
| 518 | // Get the size of the super region in bytes. |
Ted Kremenek | 50dc1b3 | 2008-12-24 01:05:03 +0000 | [diff] [blame] | 519 | const SVal* Extent = state.get<RegionExtents>(ATR->getSuperRegion()); |
| 520 | assert(Extent && "region extent not exist"); |
Zhongxing Xu | baf03a7 | 2008-11-24 09:44:56 +0000 | [diff] [blame] | 521 | |
| 522 | // Assume it's ConcreteInt for now. |
Ted Kremenek | 50dc1b3 | 2008-12-24 01:05:03 +0000 | [diff] [blame] | 523 | llvm::APSInt SSize = cast<nonloc::ConcreteInt>(*Extent).getValue(); |
Zhongxing Xu | baf03a7 | 2008-11-24 09:44:56 +0000 | [diff] [blame] | 524 | |
| 525 | // Get the size of the element in bits. |
Ted Kremenek | 6eddeb1 | 2008-12-13 21:49:13 +0000 | [diff] [blame] | 526 | QualType LvT = ATR->getLValueType(getContext()); |
| 527 | QualType ElemTy = cast<PointerType>(LvT.getTypePtr())->getPointeeType(); |
Zhongxing Xu | baf03a7 | 2008-11-24 09:44:56 +0000 | [diff] [blame] | 528 | |
| 529 | uint64_t X = getContext().getTypeSize(ElemTy); |
| 530 | |
| 531 | const llvm::APSInt& ESize = getBasicVals().getValue(X, SSize.getBitWidth(), |
| 532 | false); |
| 533 | |
| 534 | // Calculate the number of elements. |
| 535 | |
| 536 | // FIXME: What do we do with signed-ness problem? Shall we make all APSInts |
| 537 | // signed? |
| 538 | if (SSize.isUnsigned()) |
| 539 | SSize.setIsSigned(true); |
| 540 | |
| 541 | // FIXME: move this operation into BasicVals. |
| 542 | const llvm::APSInt S = |
| 543 | (SSize * getBasicVals().getValue(8, SSize.getBitWidth(), false)) / ESize; |
| 544 | |
| 545 | return NonLoc::MakeVal(getBasicVals(), S); |
Ted Kremenek | 2e84257 | 2009-01-22 23:56:56 +0000 | [diff] [blame] | 546 | #else |
| 547 | ATR = ATR; |
| 548 | return UnknownVal(); |
| 549 | #endif |
Zhongxing Xu | e8a964b | 2008-11-22 13:21:46 +0000 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | if (const FieldRegion* FR = dyn_cast<FieldRegion>(R)) { |
| 553 | // FIXME: Unsupported yet. |
| 554 | FR = 0; |
| 555 | return UnknownVal(); |
| 556 | } |
Zhongxing Xu | 369f429 | 2008-11-22 13:23:00 +0000 | [diff] [blame] | 557 | |
Zhongxing Xu | 02ebefb | 2009-02-06 08:51:30 +0000 | [diff] [blame] | 558 | if (isa<SymbolicRegion>(R)) { |
Zhongxing Xu | a48f737 | 2009-02-06 08:44:27 +0000 | [diff] [blame] | 559 | return UnknownVal(); |
| 560 | } |
| 561 | |
Zhongxing Xu | e8a964b | 2008-11-22 13:21:46 +0000 | [diff] [blame] | 562 | assert(0 && "Other regions are not supported yet."); |
Ted Kremenek | a21362d | 2009-01-06 19:12:06 +0000 | [diff] [blame] | 563 | return UnknownVal(); |
Zhongxing Xu | e8a964b | 2008-11-22 13:21:46 +0000 | [diff] [blame] | 564 | } |
| 565 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 566 | /// ArrayToPointer - Emulates the "decay" of an array to a pointer |
| 567 | /// type. 'Array' represents the lvalue of the array being decayed |
| 568 | /// to a pointer, and the returned SVal represents the decayed |
| 569 | /// version of that lvalue (i.e., a pointer to the first element of |
| 570 | /// the array). This is called by GRExprEngine when evaluating casts |
| 571 | /// from arrays to pointers. |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 572 | SVal RegionStoreManager::ArrayToPointer(SVal Array) { |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 573 | // FIXME: This should be factored into GRExprEngine. This allows |
| 574 | // us to pass a "loc" instead of an "SVal" for "Array". |
Ted Kremenek | abb042f | 2008-12-13 19:24:37 +0000 | [diff] [blame] | 575 | if (Array.isUnknownOrUndef()) |
| 576 | return Array; |
| 577 | |
| 578 | if (!isa<loc::MemRegionVal>(Array)) |
| 579 | return UnknownVal(); |
| 580 | |
| 581 | const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion(); |
| 582 | const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R); |
| 583 | |
Ted Kremenek | bbee1a7 | 2009-01-13 01:03:27 +0000 | [diff] [blame] | 584 | if (!ArrayR) |
Ted Kremenek | abb042f | 2008-12-13 19:24:37 +0000 | [diff] [blame] | 585 | return UnknownVal(); |
| 586 | |
Zhongxing Xu | 63123d8 | 2008-11-23 04:30:35 +0000 | [diff] [blame] | 587 | nonloc::ConcreteInt Idx(getBasicVals().getZeroWithPtrWidth(false)); |
Zhongxing Xu | 0b7e642 | 2008-10-26 02:23:57 +0000 | [diff] [blame] | 588 | ElementRegion* ER = MRMgr.getElementRegion(Idx, ArrayR); |
| 589 | |
| 590 | return loc::MemRegionVal(ER); |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 591 | } |
| 592 | |
Ted Kremenek | 6eddeb1 | 2008-12-13 21:49:13 +0000 | [diff] [blame] | 593 | StoreManager::CastResult |
| 594 | RegionStoreManager::CastRegion(const GRState* state, const MemRegion* R, |
| 595 | QualType CastToTy) { |
| 596 | |
| 597 | // Return the same region if the region types are compatible. |
| 598 | if (const TypedRegion* TR = dyn_cast<TypedRegion>(R)) { |
| 599 | ASTContext& Ctx = StateMgr.getContext(); |
| 600 | QualType Ta = Ctx.getCanonicalType(TR->getLValueType(Ctx)); |
| 601 | QualType Tb = Ctx.getCanonicalType(CastToTy); |
| 602 | |
| 603 | if (Ta == Tb) |
| 604 | return CastResult(state, R); |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 605 | } |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 606 | |
| 607 | // FIXME: We should handle the case when we are casting *back* to a |
| 608 | // previous type. For example: |
| 609 | // |
| 610 | // void* x = ...; |
| 611 | // char* y = (char*) x; |
| 612 | // void* z = (void*) y; // <-- we should get the same region that is |
| 613 | // bound to 'x' |
Ted Kremenek | 0312c0e | 2009-03-01 05:44:08 +0000 | [diff] [blame] | 614 | const MemRegion* ViewR = MRMgr.getTypedViewRegion(CastToTy, R); |
Ted Kremenek | 6eddeb1 | 2008-12-13 21:49:13 +0000 | [diff] [blame] | 615 | return CastResult(AddRegionView(state, ViewR, R), ViewR); |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 616 | } |
| 617 | |
Zhongxing Xu | 94aa6c1 | 2009-03-02 07:52:23 +0000 | [diff] [blame] | 618 | SVal RegionStoreManager::EvalBinOp(BinaryOperator::Opcode Op, Loc L, NonLoc R) { |
| 619 | // Assume the base location is MemRegionVal(ElementRegion). |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 620 | if (!isa<loc::MemRegionVal>(L)) |
Zhongxing Xu | 94aa6c1 | 2009-03-02 07:52:23 +0000 | [diff] [blame] | 621 | return UnknownVal(); |
Zhongxing Xu | 94aa6c1 | 2009-03-02 07:52:23 +0000 | [diff] [blame] | 622 | |
| 623 | const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion(); |
| 624 | |
| 625 | const ElementRegion* ER = cast<ElementRegion>(MR); |
| 626 | SVal Idx = ER->getIndex(); |
| 627 | |
| 628 | nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx); |
| 629 | nonloc::ConcreteInt* Offset = dyn_cast<nonloc::ConcreteInt>(&R); |
| 630 | |
| 631 | // Only support concrete integer indexes for now. |
| 632 | if (Base && Offset) { |
| 633 | SVal NewIdx = Base->EvalBinOp(getBasicVals(), Op, *Offset); |
| 634 | |
| 635 | const MemRegion* NewER = MRMgr.getElementRegion(NewIdx, |
| 636 | ER->getArrayRegion()); |
| 637 | return Loc::MakeVal(NewER); |
| 638 | |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | return UnknownVal(); |
Zhongxing Xu | 94aa6c1 | 2009-03-02 07:52:23 +0000 | [diff] [blame] | 642 | } |
| 643 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 644 | SVal RegionStoreManager::Retrieve(const GRState* St, Loc L, QualType T) { |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 645 | assert(!isa<UnknownVal>(L) && "location unknown"); |
| 646 | assert(!isa<UndefinedVal>(L) && "location undefined"); |
| 647 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 648 | // FIXME: What does loc::SymbolVal represent? It represents the value |
| 649 | // of a location but that value is not known. In the future we should |
| 650 | // handle potential aliasing relationships; e.g. a loc::SymbolVal could |
| 651 | // be an alias for a particular region. |
Zhongxing Xu | 779747c | 2009-02-20 05:19:30 +0000 | [diff] [blame] | 652 | // Example: |
| 653 | // void foo(char* buf) { |
| 654 | // char c = *buf; |
| 655 | // } |
| 656 | if (isa<loc::SymbolVal>(L)) { |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 657 | return UnknownVal(); |
Zhongxing Xu | 779747c | 2009-02-20 05:19:30 +0000 | [diff] [blame] | 658 | } |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 659 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 660 | // FIXME: Is this even possible? Shouldn't this be treated as a null |
| 661 | // dereference at a higher level? |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 662 | if (isa<loc::ConcreteInt>(L)) |
| 663 | return UndefinedVal(); |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 664 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 665 | // FIXME: Should this be refactored into GRExprEngine or GRStateManager? |
| 666 | // It seems that all StoreManagers would do the same thing here. |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 667 | if (isa<loc::FuncVal>(L)) |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 668 | return L; |
| 669 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 670 | // FIXME: Perhaps this method should just take a 'const MemRegion*' argument |
| 671 | // instead of 'Loc', and have the other Loc cases handled at a higher level. |
Zhongxing Xu | 1038f9f | 2009-03-09 09:15:51 +0000 | [diff] [blame] | 672 | const TypedRegion* R |
| 673 | = cast<TypedRegion>(cast<loc::MemRegionVal>(L).getRegion()); |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 674 | assert(R && "bad region"); |
| 675 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 676 | // FIXME: We should eventually handle funny addressing. e.g.: |
| 677 | // |
| 678 | // int x = ...; |
| 679 | // int *p = &x; |
| 680 | // char *q = (char*) p; |
| 681 | // char c = *q; // returns the first byte of 'x'. |
| 682 | // |
| 683 | // Such funny addressing will occur due to layering of regions. |
| 684 | |
Zhongxing Xu | 1038f9f | 2009-03-09 09:15:51 +0000 | [diff] [blame] | 685 | QualType RTy = R->getRValueType(getContext()); |
| 686 | if (RTy->isStructureType()) |
| 687 | return RetrieveStruct(St, R); |
| 688 | // FIXME: handle Vector types. |
| 689 | if (RTy->isVectorType()) |
Ted Kremenek | 265a305 | 2009-02-24 02:23:11 +0000 | [diff] [blame] | 690 | return UnknownVal(); |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 691 | |
| 692 | RegionBindingsTy B = GetRegionBindings(St->getStore()); |
| 693 | RegionBindingsTy::data_type* V = B.lookup(R); |
| 694 | |
| 695 | // Check if the region has a binding. |
| 696 | if (V) |
| 697 | return *V; |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 698 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 699 | GRStateRef state(St, StateMgr); |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 700 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 701 | // Check if the region is in killset. |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 702 | if (state.contains<RegionKills>(R)) |
| 703 | return UnknownVal(); |
| 704 | |
Ted Kremenek | 3de2d3c | 2009-03-05 04:50:08 +0000 | [diff] [blame] | 705 | // If the region is an element or field, it may have a default value. |
Zhongxing Xu | 562c4d9 | 2009-01-23 11:22:12 +0000 | [diff] [blame] | 706 | if (isa<ElementRegion>(R) || isa<FieldRegion>(R)) { |
| 707 | const MemRegion* SuperR = cast<SubRegion>(R)->getSuperRegion(); |
| 708 | GRStateTrait<RegionDefaultValue>::lookup_type D = |
| 709 | state.get<RegionDefaultValue>(SuperR); |
| 710 | if (D) |
| 711 | return *D; |
| 712 | } |
Ted Kremenek | 3de2d3c | 2009-03-05 04:50:08 +0000 | [diff] [blame] | 713 | |
| 714 | if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R)) { |
| 715 | const MemRegion *SR = IVR->getSuperRegion(); |
| 716 | |
| 717 | // If the super region is 'self' then return the symbol representing |
| 718 | // the value of the ivar upon entry to the method. |
| 719 | if (SR == SelfRegion) { |
| 720 | // FIXME: Do we need to handle the case where the super region |
| 721 | // has a view? We want to canonicalize the bindings. |
| 722 | return SVal::GetRValueSymbolVal(getSymbolManager(), R); |
| 723 | } |
| 724 | |
| 725 | // Otherwise, we need a new symbol. For now return Unknown. |
| 726 | return UnknownVal(); |
| 727 | } |
Zhongxing Xu | 562c4d9 | 2009-01-23 11:22:12 +0000 | [diff] [blame] | 728 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 729 | // The location does not have a bound value. This means that it has |
| 730 | // the value it had upon its creation and/or entry to the analyzed |
| 731 | // function/method. These are either symbolic values or 'undefined'. |
| 732 | |
| 733 | // We treat function parameters as symbolic values. |
Ted Kremenek | 6fd8f91 | 2009-01-22 23:43:57 +0000 | [diff] [blame] | 734 | if (const VarRegion* VR = dyn_cast<VarRegion>(R)) { |
| 735 | const VarDecl *VD = VR->getDecl(); |
| 736 | |
Ted Kremenek | cec3525 | 2009-03-04 00:23:05 +0000 | [diff] [blame] | 737 | if (VD == SelfDecl) |
| 738 | return loc::MemRegionVal(getSelfRegion(0)); |
| 739 | |
| 740 | if (isa<ParmVarDecl>(VD) || isa<ImplicitParamDecl>(VD) || |
| 741 | VD->hasGlobalStorage()) { |
Zhongxing Xu | d8895f6 | 2009-02-19 09:56:08 +0000 | [diff] [blame] | 742 | QualType VTy = VD->getType(); |
| 743 | if (Loc::IsLocType(VTy) || VTy->isIntegerType()) |
| 744 | return SVal::GetRValueSymbolVal(getSymbolManager(), VR); |
| 745 | else |
| 746 | return UnknownVal(); |
| 747 | } |
Ted Kremenek | 3de2d3c | 2009-03-05 04:50:08 +0000 | [diff] [blame] | 748 | } |
Ted Kremenek | 265a305 | 2009-02-24 02:23:11 +0000 | [diff] [blame] | 749 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 750 | if (MRMgr.onStack(R) || MRMgr.onHeap(R)) { |
| 751 | // All stack variables are considered to have undefined values |
| 752 | // upon creation. All heap allocated blocks are considered to |
| 753 | // have undefined values as well unless they are explicitly bound |
| 754 | // to specific values. |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 755 | return UndefinedVal(); |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 756 | } |
| 757 | |
Zhongxing Xu | ff8d204 | 2009-03-09 09:31:22 +0000 | [diff] [blame] | 758 | // All other integer values are symbolic. |
| 759 | if (Loc::IsLocType(RTy) || RTy->isIntegerType()) |
| 760 | return SVal::GetRValueSymbolVal(getSymbolManager(), R); |
| 761 | else |
| 762 | return UnknownVal(); |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 763 | } |
| 764 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 765 | SVal RegionStoreManager::RetrieveStruct(const GRState* St,const TypedRegion* R){ |
| 766 | |
| 767 | Store store = St->getStore(); |
| 768 | GRStateRef state(St, StateMgr); |
| 769 | |
Ted Kremenek | 6eddeb1 | 2008-12-13 21:49:13 +0000 | [diff] [blame] | 770 | // FIXME: Verify we want getRValueType instead of getLValueType. |
| 771 | QualType T = R->getRValueType(getContext()); |
Zhongxing Xu | 6e3f01c | 2008-10-31 07:16:08 +0000 | [diff] [blame] | 772 | assert(T->isStructureType()); |
| 773 | |
| 774 | const RecordType* RT = cast<RecordType>(T.getTypePtr()); |
| 775 | RecordDecl* RD = RT->getDecl(); |
| 776 | assert(RD->isDefinition()); |
| 777 | |
| 778 | llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList(); |
| 779 | |
Douglas Gregor | e267ff3 | 2008-12-11 20:41:00 +0000 | [diff] [blame] | 780 | std::vector<FieldDecl *> Fields(RD->field_begin(), RD->field_end()); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 781 | |
Douglas Gregor | e267ff3 | 2008-12-11 20:41:00 +0000 | [diff] [blame] | 782 | for (std::vector<FieldDecl *>::reverse_iterator Field = Fields.rbegin(), |
| 783 | FieldEnd = Fields.rend(); |
| 784 | Field != FieldEnd; ++Field) { |
| 785 | FieldRegion* FR = MRMgr.getFieldRegion(*Field, R); |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 786 | RegionBindingsTy B = GetRegionBindings(store); |
Zhongxing Xu | f0dfa8d | 2008-10-31 08:10:01 +0000 | [diff] [blame] | 787 | RegionBindingsTy::data_type* data = B.lookup(FR); |
Zhongxing Xu | 6e3f01c | 2008-10-31 07:16:08 +0000 | [diff] [blame] | 788 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 789 | SVal FieldValue; |
| 790 | if (data) |
| 791 | FieldValue = *data; |
| 792 | else if (state.contains<RegionKills>(FR)) |
| 793 | FieldValue = UnknownVal(); |
| 794 | else { |
| 795 | if (MRMgr.onStack(FR) || MRMgr.onHeap(FR)) |
| 796 | FieldValue = UndefinedVal(); |
| 797 | else |
Ted Kremenek | 9ab6b9c | 2009-01-22 18:23:34 +0000 | [diff] [blame] | 798 | FieldValue = SVal::GetRValueSymbolVal(getSymbolManager(), FR); |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 799 | } |
Zhongxing Xu | 6e3f01c | 2008-10-31 07:16:08 +0000 | [diff] [blame] | 800 | |
| 801 | StructVal = getBasicVals().consVals(FieldValue, StructVal); |
| 802 | } |
| 803 | |
| 804 | return NonLoc::MakeCompoundVal(T, StructVal, getBasicVals()); |
| 805 | } |
| 806 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 807 | const GRState* RegionStoreManager::Bind(const GRState* St, Loc L, SVal V) { |
| 808 | // Currently we don't bind value to symbolic location. But if the logic is |
| 809 | // made clear, we might change this decision. |
| 810 | if (isa<loc::SymbolVal>(L)) |
| 811 | return St; |
Zhongxing Xu | 8fe63af | 2008-10-27 09:24:07 +0000 | [diff] [blame] | 812 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 813 | // If we get here, the location should be a region. |
| 814 | const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion(); |
Zhongxing Xu | f0dfa8d | 2008-10-31 08:10:01 +0000 | [diff] [blame] | 815 | assert(R); |
| 816 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 817 | // Check if the region is a struct region. |
Zhongxing Xu | f0dfa8d | 2008-10-31 08:10:01 +0000 | [diff] [blame] | 818 | if (const TypedRegion* TR = dyn_cast<TypedRegion>(R)) |
Ted Kremenek | 6eddeb1 | 2008-12-13 21:49:13 +0000 | [diff] [blame] | 819 | // FIXME: Verify we want getRValueType(). |
| 820 | if (TR->getRValueType(getContext())->isStructureType()) |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 821 | return BindStruct(St, TR, V); |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 822 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 823 | Store store = St->getStore(); |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 824 | RegionBindingsTy B = GetRegionBindings(store); |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 825 | |
| 826 | if (V.isUnknown()) { |
| 827 | // Remove the binding. |
| 828 | store = RBFactory.Remove(B, R).getRoot(); |
| 829 | |
| 830 | // Add the region to the killset. |
| 831 | GRStateRef state(St, StateMgr); |
| 832 | St = state.add<RegionKills>(R); |
| 833 | } |
| 834 | else |
| 835 | store = RBFactory.Add(B, R, V).getRoot(); |
| 836 | |
| 837 | return StateMgr.MakeStateWithStore(St, store); |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 838 | } |
| 839 | |
Zhongxing Xu | 9c9ca08 | 2008-12-16 02:36:30 +0000 | [diff] [blame] | 840 | Store RegionStoreManager::Remove(Store store, Loc L) { |
Ted Kremenek | 0964a06 | 2009-01-21 06:57:53 +0000 | [diff] [blame] | 841 | const MemRegion* R = 0; |
| 842 | |
| 843 | if (isa<loc::MemRegionVal>(L)) |
| 844 | R = cast<loc::MemRegionVal>(L).getRegion(); |
| 845 | else if (isa<loc::SymbolVal>(L)) |
Zhongxing Xu | 026c663 | 2009-02-05 06:57:29 +0000 | [diff] [blame] | 846 | R = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(L).getSymbol(), |
| 847 | StateMgr.getSymbolManager()); |
Ted Kremenek | 0964a06 | 2009-01-21 06:57:53 +0000 | [diff] [blame] | 848 | |
| 849 | if (R) { |
| 850 | RegionBindingsTy B = GetRegionBindings(store); |
| 851 | return RBFactory.Remove(B, R).getRoot(); |
| 852 | } |
| 853 | |
| 854 | return store; |
Zhongxing Xu | 9c9ca08 | 2008-12-16 02:36:30 +0000 | [diff] [blame] | 855 | } |
| 856 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 857 | const GRState* RegionStoreManager::BindDecl(const GRState* St, |
| 858 | const VarDecl* VD, SVal InitVal) { |
Zhongxing Xu | a4f28ff | 2008-11-13 08:41:36 +0000 | [diff] [blame] | 859 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 860 | QualType T = VD->getType(); |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 861 | VarRegion* VR = MRMgr.getVarRegion(VD); |
Zhongxing Xu | f0dfa8d | 2008-10-31 08:10:01 +0000 | [diff] [blame] | 862 | |
Ted Kremenek | 0964a06 | 2009-01-21 06:57:53 +0000 | [diff] [blame] | 863 | if (T->isArrayType()) |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 864 | return BindArray(St, VR, InitVal); |
Ted Kremenek | 0964a06 | 2009-01-21 06:57:53 +0000 | [diff] [blame] | 865 | if (T->isStructureType()) |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 866 | return BindStruct(St, VR, InitVal); |
Zhongxing Xu | d463d44 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 867 | |
Ted Kremenek | 0964a06 | 2009-01-21 06:57:53 +0000 | [diff] [blame] | 868 | return Bind(St, Loc::MakeVal(VR), InitVal); |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 869 | } |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 870 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 871 | // FIXME: this method should be merged into Bind(). |
| 872 | const GRState* |
| 873 | RegionStoreManager::BindCompoundLiteral(const GRState* St, |
| 874 | const CompoundLiteralExpr* CL, SVal V) { |
Zhongxing Xu | f22679e | 2008-11-07 10:38:33 +0000 | [diff] [blame] | 875 | CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL); |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 876 | return Bind(St, loc::MemRegionVal(R), V); |
Zhongxing Xu | f22679e | 2008-11-07 10:38:33 +0000 | [diff] [blame] | 877 | } |
| 878 | |
Zhongxing Xu | baf03a7 | 2008-11-24 09:44:56 +0000 | [diff] [blame] | 879 | const GRState* RegionStoreManager::setExtent(const GRState* St, |
| 880 | const MemRegion* R, SVal Extent) { |
| 881 | GRStateRef state(St, StateMgr); |
Ted Kremenek | 50dc1b3 | 2008-12-24 01:05:03 +0000 | [diff] [blame] | 882 | return state.set<RegionExtents>(R, Extent); |
Zhongxing Xu | baf03a7 | 2008-11-24 09:44:56 +0000 | [diff] [blame] | 883 | } |
| 884 | |
| 885 | |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 886 | static void UpdateLiveSymbols(SVal X, SymbolReaper& SymReaper) { |
Ted Kremenek | 02c4d2d | 2009-03-04 00:11:38 +0000 | [diff] [blame] | 887 | if (loc::MemRegionVal *XR = dyn_cast<loc::MemRegionVal>(&X)) { |
| 888 | const MemRegion *R = XR->getRegion(); |
| 889 | |
| 890 | while (R) { |
| 891 | if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) { |
| 892 | SymReaper.markLive(SR->getSymbol()); |
| 893 | return; |
| 894 | } |
| 895 | |
| 896 | if (const SubRegion *SR = dyn_cast<SubRegion>(R)) { |
| 897 | R = SR->getSuperRegion(); |
| 898 | continue; |
| 899 | } |
| 900 | |
| 901 | break; |
| 902 | } |
| 903 | |
| 904 | return; |
| 905 | } |
| 906 | |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 907 | for (SVal::symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end();SI!=SE;++SI) |
| 908 | SymReaper.markLive(*SI); |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 909 | } |
| 910 | |
Ted Kremenek | 2ed14be | 2008-12-05 00:47:52 +0000 | [diff] [blame] | 911 | Store RegionStoreManager::RemoveDeadBindings(const GRState* state, Stmt* Loc, |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 912 | SymbolReaper& SymReaper, |
| 913 | llvm::SmallVectorImpl<const MemRegion*>& RegionRoots) |
| 914 | { |
Zhongxing Xu | 8916d5b | 2008-11-10 09:39:04 +0000 | [diff] [blame] | 915 | |
Ted Kremenek | 2ed14be | 2008-12-05 00:47:52 +0000 | [diff] [blame] | 916 | Store store = state->getStore(); |
Zhongxing Xu | 8916d5b | 2008-11-10 09:39:04 +0000 | [diff] [blame] | 917 | RegionBindingsTy B = GetRegionBindings(store); |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 918 | |
| 919 | // Lazily constructed backmap from MemRegions to SubRegions. |
| 920 | typedef llvm::ImmutableSet<const MemRegion*> SubRegionsTy; |
| 921 | typedef llvm::ImmutableMap<const MemRegion*, SubRegionsTy> SubRegionsMapTy; |
| 922 | |
| 923 | // FIXME: As a future optimization we can modifiy BumpPtrAllocator to have |
| 924 | // the ability to reuse memory. This way we can keep TmpAlloc around as |
| 925 | // an instance variable of RegionStoreManager (avoiding repeated malloc |
| 926 | // overhead). |
| 927 | llvm::BumpPtrAllocator TmpAlloc; |
| 928 | |
| 929 | // Factory objects. |
| 930 | SubRegionsMapTy::Factory SubRegMapF(TmpAlloc); |
| 931 | SubRegionsTy::Factory SubRegF(TmpAlloc); |
| 932 | |
| 933 | // The backmap from regions to subregions. |
| 934 | SubRegionsMapTy SubRegMap = SubRegMapF.GetEmptyMap(); |
| 935 | |
| 936 | // Do a pass over the regions in the store. For VarRegions we check if |
| 937 | // the variable is still live and if so add it to the list of live roots. |
| 938 | // For other regions we populate our region backmap. |
Zhongxing Xu | 8916d5b | 2008-11-10 09:39:04 +0000 | [diff] [blame] | 939 | for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) { |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 940 | const MemRegion* R = I.getKey(); |
| 941 | if (const VarRegion* VR = dyn_cast<VarRegion>(R)) { |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 942 | if (SymReaper.isLive(Loc, VR->getDecl())) |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 943 | RegionRoots.push_back(VR); // This is a live "root". |
| 944 | } |
| 945 | else { |
| 946 | // Get the super region for R. |
| 947 | const MemRegion* SuperR = cast<SubRegion>(R)->getSuperRegion(); |
| 948 | // Get the current set of subregions for SuperR. |
| 949 | const SubRegionsTy* SRptr = SubRegMap.lookup(SuperR); |
| 950 | SubRegionsTy SR = SRptr ? *SRptr : SubRegF.GetEmptySet(); |
| 951 | // Add R to the subregions of SuperR. |
| 952 | SubRegMap = SubRegMapF.Add(SubRegMap, SuperR, SubRegF.Add(SR, R)); |
| 953 | |
| 954 | // Finally, check if SuperR is a VarRegion. We need to do this |
| 955 | // to also mark SuperR as a root (as it may not have a value directly |
| 956 | // bound to it in the store). |
| 957 | if (const VarRegion* VR = dyn_cast<VarRegion>(SuperR)) { |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 958 | if (SymReaper.isLive(Loc, VR->getDecl())) |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 959 | RegionRoots.push_back(VR); // This is a live "root". |
| 960 | } |
Ted Kremenek | 3de2d3c | 2009-03-05 04:50:08 +0000 | [diff] [blame] | 961 | } |
Zhongxing Xu | 8916d5b | 2008-11-10 09:39:04 +0000 | [diff] [blame] | 962 | } |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 963 | |
| 964 | // Process the worklist of RegionRoots. This performs a "mark-and-sweep" |
| 965 | // of the store. We want to find all live symbols and dead regions. |
| 966 | llvm::SmallPtrSet<const MemRegion*, 10> Marked; |
| 967 | |
| 968 | while (!RegionRoots.empty()) { |
| 969 | // Dequeue the next region on the worklist. |
| 970 | const MemRegion* R = RegionRoots.back(); |
| 971 | RegionRoots.pop_back(); |
Zhongxing Xu | 8916d5b | 2008-11-10 09:39:04 +0000 | [diff] [blame] | 972 | |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 973 | // Check if we have already processed this region. |
| 974 | if (Marked.count(R)) continue; |
| 975 | |
| 976 | // Mark this region as processed. This is needed for termination in case |
| 977 | // a region is referenced more than once. |
| 978 | Marked.insert(R); |
| 979 | |
| 980 | // Mark the symbol for any live SymbolicRegion as "live". This means we |
| 981 | // should continue to track that symbol. |
| 982 | if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R)) |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 983 | SymReaper.markLive(SymR->getSymbol()); |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 984 | |
| 985 | // Get the data binding for R (if any). |
| 986 | RegionBindingsTy::data_type* Xptr = B.lookup(R); |
| 987 | if (Xptr) { |
| 988 | SVal X = *Xptr; |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 989 | UpdateLiveSymbols(X, SymReaper); // Update the set of live symbols. |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 990 | |
| 991 | // If X is a region, then add it the RegionRoots. |
| 992 | if (loc::MemRegionVal* RegionX = dyn_cast<loc::MemRegionVal>(&X)) |
| 993 | RegionRoots.push_back(RegionX->getRegion()); |
| 994 | } |
| 995 | |
| 996 | // Get the subregions of R. These are RegionRoots as well since they |
| 997 | // represent values that are also bound to R. |
| 998 | const SubRegionsTy* SRptr = SubRegMap.lookup(R); |
| 999 | if (!SRptr) continue; |
| 1000 | SubRegionsTy SR = *SRptr; |
| 1001 | |
| 1002 | for (SubRegionsTy::iterator I=SR.begin(), E=SR.end(); I!=E; ++I) |
| 1003 | RegionRoots.push_back(*I); |
| 1004 | } |
| 1005 | |
| 1006 | // We have now scanned the store, marking reachable regions and symbols |
| 1007 | // as live. We now remove all the regions that are dead from the store |
Ted Kremenek | 3de2d3c | 2009-03-05 04:50:08 +0000 | [diff] [blame] | 1008 | // as well as update DSymbols with the set symbols that are now dead. |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 1009 | for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) { |
| 1010 | const MemRegion* R = I.getKey(); |
| 1011 | |
| 1012 | // If this region live? Is so, none of its symbols are dead. |
| 1013 | if (Marked.count(R)) |
| 1014 | continue; |
| 1015 | |
| 1016 | // Remove this dead region from the store. |
Zhongxing Xu | 9c9ca08 | 2008-12-16 02:36:30 +0000 | [diff] [blame] | 1017 | store = Remove(store, Loc::MakeVal(R)); |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 1018 | |
| 1019 | // Mark all non-live symbols that this region references as dead. |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 1020 | if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R)) |
| 1021 | SymReaper.maybeDead(SymR->getSymbol()); |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 1022 | |
| 1023 | SVal X = I.getData(); |
| 1024 | SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end(); |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 1025 | for (; SI != SE; ++SI) SymReaper.maybeDead(*SI); |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 1026 | } |
| 1027 | |
Zhongxing Xu | 8916d5b | 2008-11-10 09:39:04 +0000 | [diff] [blame] | 1028 | return store; |
| 1029 | } |
| 1030 | |
Zhongxing Xu | a071eb0 | 2008-10-24 06:01:33 +0000 | [diff] [blame] | 1031 | void RegionStoreManager::print(Store store, std::ostream& Out, |
| 1032 | const char* nl, const char *sep) { |
| 1033 | llvm::raw_os_ostream OS(Out); |
| 1034 | RegionBindingsTy B = GetRegionBindings(store); |
| 1035 | OS << "Store:" << nl; |
| 1036 | |
| 1037 | for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) { |
| 1038 | OS << ' '; I.getKey()->print(OS); OS << " : "; |
| 1039 | I.getData().print(OS); OS << nl; |
| 1040 | } |
Zhongxing Xu | 5b8b6f2 | 2008-10-24 04:33:15 +0000 | [diff] [blame] | 1041 | } |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 1042 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1043 | const GRState* RegionStoreManager::BindArray(const GRState* St, |
| 1044 | const TypedRegion* R, SVal Init) { |
Ted Kremenek | 6eddeb1 | 2008-12-13 21:49:13 +0000 | [diff] [blame] | 1045 | |
| 1046 | // FIXME: Verify we should use getLValueType or getRValueType. |
Zhongxing Xu | 2ef9372 | 2008-12-14 03:14:52 +0000 | [diff] [blame] | 1047 | QualType T = R->getRValueType(getContext()); |
Zhongxing Xu | 1a12a0e | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 1048 | assert(T->isArrayType()); |
| 1049 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1050 | // When we are binding the whole array, it always has default value 0. |
| 1051 | GRStateRef state(St, StateMgr); |
Ted Kremenek | 14553ab | 2009-01-30 00:08:43 +0000 | [diff] [blame] | 1052 | St = state.set<RegionDefaultValue>(R, NonLoc::MakeIntVal(getBasicVals(), 0, |
| 1053 | false)); |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1054 | |
Zhongxing Xu | 1a12a0e | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 1055 | ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr()); |
| 1056 | |
Zhongxing Xu | 6987c7b | 2008-11-30 05:49:49 +0000 | [diff] [blame] | 1057 | llvm::APSInt Size(CAT->getSize(), false); |
Sebastian Redl | 1be3da5 | 2009-01-26 19:54:12 +0000 | [diff] [blame] | 1058 | llvm::APSInt i = getBasicVals().getValue(0, Size.getBitWidth(), |
| 1059 | Size.isUnsigned()); |
Zhongxing Xu | 6987c7b | 2008-11-30 05:49:49 +0000 | [diff] [blame] | 1060 | |
| 1061 | // Check if the init expr is a StringLiteral. |
| 1062 | if (isa<loc::MemRegionVal>(Init)) { |
| 1063 | const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion(); |
| 1064 | const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral(); |
| 1065 | const char* str = S->getStrData(); |
| 1066 | unsigned len = S->getByteLength(); |
| 1067 | unsigned j = 0; |
| 1068 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1069 | // Copy bytes from the string literal into the target array. Trailing bytes |
| 1070 | // in the array that are not covered by the string literal are initialized |
| 1071 | // to zero. |
Zhongxing Xu | 6987c7b | 2008-11-30 05:49:49 +0000 | [diff] [blame] | 1072 | for (; i < Size; ++i, ++j) { |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1073 | if (j >= len) |
| 1074 | break; |
| 1075 | |
Zhongxing Xu | 6987c7b | 2008-11-30 05:49:49 +0000 | [diff] [blame] | 1076 | SVal Idx = NonLoc::MakeVal(getBasicVals(), i); |
| 1077 | ElementRegion* ER = MRMgr.getElementRegion(Idx, R); |
| 1078 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1079 | SVal V = NonLoc::MakeVal(getBasicVals(), str[j], sizeof(char)*8, true); |
| 1080 | St = Bind(St, loc::MemRegionVal(ER), V); |
Zhongxing Xu | 6987c7b | 2008-11-30 05:49:49 +0000 | [diff] [blame] | 1081 | } |
| 1082 | |
Zhongxing Xu | bd4f745 | 2009-03-09 06:49:50 +0000 | [diff] [blame] | 1083 | return St; |
Zhongxing Xu | 6987c7b | 2008-11-30 05:49:49 +0000 | [diff] [blame] | 1084 | } |
| 1085 | |
Zhongxing Xu | 1a12a0e | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 1086 | |
| 1087 | nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init); |
| 1088 | |
| 1089 | nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end(); |
| 1090 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1091 | for (; i < Size; ++i, ++VI) { |
| 1092 | // The init list might be shorter than the array decl. |
| 1093 | if (VI == VE) |
| 1094 | break; |
| 1095 | |
Zhongxing Xu | 6987c7b | 2008-11-30 05:49:49 +0000 | [diff] [blame] | 1096 | SVal Idx = NonLoc::MakeVal(getBasicVals(), i); |
Zhongxing Xu | 1a12a0e | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 1097 | ElementRegion* ER = MRMgr.getElementRegion(Idx, R); |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1098 | |
| 1099 | if (CAT->getElementType()->isStructureType()) |
| 1100 | St = BindStruct(St, ER, *VI); |
| 1101 | else |
| 1102 | St = Bind(St, Loc::MakeVal(ER), *VI); |
Zhongxing Xu | 1a12a0e | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 1103 | } |
| 1104 | |
Zhongxing Xu | bd4f745 | 2009-03-09 06:49:50 +0000 | [diff] [blame] | 1105 | return St; |
Zhongxing Xu | 1a12a0e | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 1106 | } |
| 1107 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1108 | const GRState* |
| 1109 | RegionStoreManager::BindStruct(const GRState* St, const TypedRegion* R, SVal V){ |
Ted Kremenek | 6eddeb1 | 2008-12-13 21:49:13 +0000 | [diff] [blame] | 1110 | // FIXME: Verify that we should use getRValueType or getLValueType. |
| 1111 | QualType T = R->getRValueType(getContext()); |
Zhongxing Xu | af0a844 | 2008-10-31 10:53:01 +0000 | [diff] [blame] | 1112 | assert(T->isStructureType()); |
| 1113 | |
| 1114 | RecordType* RT = cast<RecordType>(T.getTypePtr()); |
| 1115 | RecordDecl* RD = RT->getDecl(); |
| 1116 | assert(RD->isDefinition()); |
| 1117 | |
Zhongxing Xu | 5834ed6 | 2009-01-13 01:49:57 +0000 | [diff] [blame] | 1118 | if (V.isUnknown()) |
| 1119 | return KillStruct(St, R); |
| 1120 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1121 | nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V); |
Zhongxing Xu | af0a844 | 2008-10-31 10:53:01 +0000 | [diff] [blame] | 1122 | nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end(); |
| 1123 | RecordDecl::field_iterator FI = RD->field_begin(), FE = RD->field_end(); |
| 1124 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1125 | for (; FI != FE; ++FI, ++VI) { |
| 1126 | |
| 1127 | // There may be fewer values than fields only when we are initializing a |
| 1128 | // struct decl. In this case, mark the region as having default value. |
| 1129 | if (VI == VE) { |
Zhongxing Xu | 1302016 | 2008-12-24 07:29:24 +0000 | [diff] [blame] | 1130 | GRStateRef state(St, StateMgr); |
Ted Kremenek | 14553ab | 2009-01-30 00:08:43 +0000 | [diff] [blame] | 1131 | const NonLoc& Idx = NonLoc::MakeIntVal(getBasicVals(), 0, false); |
| 1132 | St = state.set<RegionDefaultValue>(R, Idx); |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1133 | break; |
| 1134 | } |
| 1135 | |
Zhongxing Xu | af0a844 | 2008-10-31 10:53:01 +0000 | [diff] [blame] | 1136 | QualType FTy = (*FI)->getType(); |
| 1137 | FieldRegion* FR = MRMgr.getFieldRegion(*FI, R); |
| 1138 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1139 | if (Loc::IsLocType(FTy) || FTy->isIntegerType()) |
| 1140 | St = Bind(St, Loc::MakeVal(FR), *VI); |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 1141 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1142 | else if (FTy->isArrayType()) |
| 1143 | St = BindArray(St, FR, *VI); |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 1144 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1145 | else if (FTy->isStructureType()) |
| 1146 | St = BindStruct(St, FR, *VI); |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 1147 | } |
| 1148 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1149 | return St; |
Zhongxing Xu | c3a0599 | 2008-11-19 11:06:24 +0000 | [diff] [blame] | 1150 | } |
| 1151 | |
Zhongxing Xu | 5834ed6 | 2009-01-13 01:49:57 +0000 | [diff] [blame] | 1152 | const GRState* RegionStoreManager::KillStruct(const GRState* St, |
| 1153 | const TypedRegion* R){ |
| 1154 | GRStateRef state(St, StateMgr); |
| 1155 | |
| 1156 | // Kill the struct region because it is assigned "unknown". |
| 1157 | St = state.add<RegionKills>(R); |
| 1158 | |
| 1159 | // Set the default value of the struct region to "unknown". |
| 1160 | St = state.set<RegionDefaultValue>(R, UnknownVal()); |
| 1161 | |
| 1162 | Store store = St->getStore(); |
| 1163 | RegionBindingsTy B = GetRegionBindings(store); |
| 1164 | |
| 1165 | // Remove all bindings for the subregions of the struct. |
| 1166 | for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) { |
| 1167 | const MemRegion* r = I.getKey(); |
| 1168 | if (const SubRegion* sr = dyn_cast<SubRegion>(r)) |
| 1169 | if (sr->isSubRegionOf(R)) |
| 1170 | store = Remove(store, Loc::MakeVal(sr)); |
Zhongxing Xu | 9c2a3db | 2009-01-13 03:07:41 +0000 | [diff] [blame] | 1171 | // FIXME: Maybe we should also remove the bindings for the "views" of the |
| 1172 | // subregions. |
Zhongxing Xu | 5834ed6 | 2009-01-13 01:49:57 +0000 | [diff] [blame] | 1173 | } |
| 1174 | |
| 1175 | return StateMgr.MakeStateWithStore(St, store); |
| 1176 | } |
| 1177 | |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 1178 | const GRState* RegionStoreManager::AddRegionView(const GRState* St, |
| 1179 | const MemRegion* View, |
| 1180 | const MemRegion* Base) { |
| 1181 | GRStateRef state(St, StateMgr); |
| 1182 | |
| 1183 | // First, retrieve the region view of the base region. |
Ted Kremenek | 50dc1b3 | 2008-12-24 01:05:03 +0000 | [diff] [blame] | 1184 | const RegionViews* d = state.get<RegionViewMap>(Base); |
Zhongxing Xu | 5834ed6 | 2009-01-13 01:49:57 +0000 | [diff] [blame] | 1185 | RegionViews L = d ? *d : RVFactory.GetEmptySet(); |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 1186 | |
| 1187 | // Now add View to the region view. |
Zhongxing Xu | 5834ed6 | 2009-01-13 01:49:57 +0000 | [diff] [blame] | 1188 | L = RVFactory.Add(L, View); |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 1189 | |
| 1190 | // Create a new state with the new region view. |
Ted Kremenek | 50dc1b3 | 2008-12-24 01:05:03 +0000 | [diff] [blame] | 1191 | return state.set<RegionViewMap>(Base, L); |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 1192 | } |
Zhongxing Xu | 5834ed6 | 2009-01-13 01:49:57 +0000 | [diff] [blame] | 1193 | |
| 1194 | const GRState* RegionStoreManager::RemoveRegionView(const GRState* St, |
| 1195 | const MemRegion* View, |
| 1196 | const MemRegion* Base) { |
| 1197 | GRStateRef state(St, StateMgr); |
| 1198 | |
| 1199 | // Retrieve the region view of the base region. |
| 1200 | const RegionViews* d = state.get<RegionViewMap>(Base); |
| 1201 | |
| 1202 | // If the base region has no view, return. |
| 1203 | if (!d) |
| 1204 | return St; |
| 1205 | |
| 1206 | // Remove the view. |
| 1207 | RegionViews V = *d; |
| 1208 | V = RVFactory.Remove(V, View); |
| 1209 | |
| 1210 | return state.set<RegionViewMap>(Base, V); |
| 1211 | } |