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