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