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