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 | 262fd03 | 2009-05-20 09:00:16 +0000 | [diff] [blame] | 217 | SVal EvalBinOp(const GRState *state,BinaryOperator::Opcode Op,Loc L,NonLoc R); |
Zhongxing Xu | 94aa6c1 | 2009-03-02 07:52:23 +0000 | [diff] [blame] | 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 | 264e937 | 2009-05-12 10:10:00 +0000 | [diff] [blame] | 279 | const GRState* setDefaultValue(const GRState* St, const MemRegion* R, SVal V); |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 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(); |
| 374 | break; |
| 375 | |
Zhongxing Xu | c4bf72c | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 376 | case loc::GotoLabelKind: |
Zhongxing Xu | c4bf72c | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 377 | // These are anormal cases. Flag an undefined value. |
| 378 | return UndefinedVal(); |
| 379 | |
| 380 | case loc::ConcreteIntKind: |
Zhongxing Xu | c4bf72c | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 381 | // While these seem funny, this can happen through casts. |
| 382 | // FIXME: What we should return is the field offset. For example, |
| 383 | // add the field offset to the integer value. That way funny things |
| 384 | // like this work properly: &(((struct foo *) 0xa)->f) |
| 385 | return Base; |
| 386 | |
| 387 | default: |
Zhongxing Xu | 13d1ee2 | 2008-11-07 08:57:30 +0000 | [diff] [blame] | 388 | assert(0 && "Unhandled Base."); |
Zhongxing Xu | c4bf72c | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 389 | return Base; |
| 390 | } |
Ted Kremenek | 3de2d3c | 2009-03-05 04:50:08 +0000 | [diff] [blame] | 391 | |
| 392 | // NOTE: We must have this check first because ObjCIvarDecl is a subclass |
| 393 | // of FieldDecl. |
| 394 | if (const ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(D)) |
| 395 | return loc::MemRegionVal(MRMgr.getObjCIvarRegion(ID, BaseR)); |
Zhongxing Xu | c4bf72c | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 396 | |
Ted Kremenek | 3de2d3c | 2009-03-05 04:50:08 +0000 | [diff] [blame] | 397 | return loc::MemRegionVal(MRMgr.getFieldRegion(cast<FieldDecl>(D), BaseR)); |
Zhongxing Xu | c4bf72c | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 398 | } |
| 399 | |
Ted Kremenek | f936f45 | 2009-05-04 06:18:28 +0000 | [diff] [blame] | 400 | SVal RegionStoreManager::getLValueElement(const GRState* St, |
| 401 | QualType elementType, |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 402 | SVal Base, SVal Offset) { |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 403 | |
Ted Kremenek | de7ec63 | 2009-03-09 22:44:49 +0000 | [diff] [blame] | 404 | // If the base is an unknown or undefined value, just return it back. |
| 405 | // FIXME: For absolute pointer addresses, we just return that value back as |
| 406 | // well, although in reality we should return the offset added to that |
| 407 | // value. |
| 408 | if (Base.isUnknownOrUndef() || isa<loc::ConcreteInt>(Base)) |
Zhongxing Xu | 4a1513e | 2008-10-27 12:23:17 +0000 | [diff] [blame] | 409 | return Base; |
| 410 | |
Ted Kremenek | a7ac944 | 2009-01-22 20:27:48 +0000 | [diff] [blame] | 411 | // Only handle integer offsets... for now. |
| 412 | if (!isa<nonloc::ConcreteInt>(Offset)) |
Zhongxing Xu | e4d1393 | 2008-11-13 09:48:44 +0000 | [diff] [blame] | 413 | return UnknownVal(); |
Ted Kremenek | a7ac944 | 2009-01-22 20:27:48 +0000 | [diff] [blame] | 414 | |
Zhongxing Xu | ce76078 | 2009-05-09 13:20:07 +0000 | [diff] [blame] | 415 | const MemRegion* BaseRegion = cast<loc::MemRegionVal>(Base).getRegion(); |
Ted Kremenek | a7ac944 | 2009-01-22 20:27:48 +0000 | [diff] [blame] | 416 | |
| 417 | // Pointer of any type can be cast and used as array base. |
| 418 | const ElementRegion *ElemR = dyn_cast<ElementRegion>(BaseRegion); |
| 419 | |
| 420 | if (!ElemR) { |
| 421 | // |
| 422 | // If the base region is not an ElementRegion, create one. |
| 423 | // This can happen in the following example: |
| 424 | // |
| 425 | // char *p = __builtin_alloc(10); |
| 426 | // p[1] = 8; |
| 427 | // |
Zhongxing Xu | ce76078 | 2009-05-09 13:20:07 +0000 | [diff] [blame] | 428 | // Observe that 'p' binds to an AllocaRegion. |
Ted Kremenek | a7ac944 | 2009-01-22 20:27:48 +0000 | [diff] [blame] | 429 | // |
Zhongxing Xu | 5ea2ffc | 2009-02-19 08:37:16 +0000 | [diff] [blame] | 430 | |
| 431 | // Offset might be unsigned. We have to convert it to signed ConcreteInt. |
| 432 | if (nonloc::ConcreteInt* CI = dyn_cast<nonloc::ConcreteInt>(&Offset)) { |
| 433 | const llvm::APSInt& OffI = CI->getValue(); |
| 434 | if (OffI.isUnsigned()) { |
| 435 | llvm::APSInt Tmp = OffI; |
| 436 | Tmp.setIsSigned(true); |
| 437 | Offset = NonLoc::MakeVal(getBasicVals(), Tmp); |
| 438 | } |
| 439 | } |
Ted Kremenek | f936f45 | 2009-05-04 06:18:28 +0000 | [diff] [blame] | 440 | return loc::MemRegionVal(MRMgr.getElementRegion(elementType, Offset, |
| 441 | BaseRegion)); |
Zhongxing Xu | e4d1393 | 2008-11-13 09:48:44 +0000 | [diff] [blame] | 442 | } |
Ted Kremenek | a7ac944 | 2009-01-22 20:27:48 +0000 | [diff] [blame] | 443 | |
| 444 | SVal BaseIdx = ElemR->getIndex(); |
| 445 | |
| 446 | if (!isa<nonloc::ConcreteInt>(BaseIdx)) |
| 447 | return UnknownVal(); |
| 448 | |
| 449 | const llvm::APSInt& BaseIdxI = cast<nonloc::ConcreteInt>(BaseIdx).getValue(); |
| 450 | const llvm::APSInt& OffI = cast<nonloc::ConcreteInt>(Offset).getValue(); |
| 451 | assert(BaseIdxI.isSigned()); |
| 452 | |
| 453 | // FIXME: This appears to be the assumption of this code. We should review |
| 454 | // whether or not BaseIdxI.getBitWidth() < OffI.getBitWidth(). If it |
| 455 | // can't we need to put a comment here. If it can, we should handle it. |
| 456 | assert(BaseIdxI.getBitWidth() >= OffI.getBitWidth()); |
Zhongxing Xu | e4d1393 | 2008-11-13 09:48:44 +0000 | [diff] [blame] | 457 | |
Zhongxing Xu | ce76078 | 2009-05-09 13:20:07 +0000 | [diff] [blame] | 458 | const MemRegion *ArrayR = ElemR->getSuperRegion(); |
Ted Kremenek | a7ac944 | 2009-01-22 20:27:48 +0000 | [diff] [blame] | 459 | SVal NewIdx; |
| 460 | |
| 461 | if (OffI.isUnsigned() || OffI.getBitWidth() < BaseIdxI.getBitWidth()) { |
| 462 | // 'Offset' might be unsigned. We have to convert it to signed and |
| 463 | // possibly extend it. |
| 464 | llvm::APSInt Tmp = OffI; |
| 465 | |
| 466 | if (OffI.getBitWidth() < BaseIdxI.getBitWidth()) |
| 467 | Tmp.extend(BaseIdxI.getBitWidth()); |
| 468 | |
| 469 | Tmp.setIsSigned(true); |
| 470 | Tmp += BaseIdxI; // Compute the new offset. |
Zhongxing Xu | 5ea2ffc | 2009-02-19 08:37:16 +0000 | [diff] [blame] | 471 | NewIdx = NonLoc::MakeVal(getBasicVals(), Tmp); |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 472 | } |
Ted Kremenek | a7ac944 | 2009-01-22 20:27:48 +0000 | [diff] [blame] | 473 | else |
| 474 | NewIdx = nonloc::ConcreteInt(getBasicVals().getValue(BaseIdxI + OffI)); |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 475 | |
Ted Kremenek | f936f45 | 2009-05-04 06:18:28 +0000 | [diff] [blame] | 476 | return loc::MemRegionVal(MRMgr.getElementRegion(elementType, NewIdx, ArrayR)); |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 477 | } |
| 478 | |
Zhongxing Xu | e8a964b | 2008-11-22 13:21:46 +0000 | [diff] [blame] | 479 | SVal RegionStoreManager::getSizeInElements(const GRState* St, |
| 480 | const MemRegion* R) { |
| 481 | if (const VarRegion* VR = dyn_cast<VarRegion>(R)) { |
| 482 | // Get the type of the variable. |
Zhongxing Xu | a82d8aa | 2009-05-09 03:57:34 +0000 | [diff] [blame] | 483 | QualType T = VR->getDesugaredValueType(getContext()); |
Zhongxing Xu | e8a964b | 2008-11-22 13:21:46 +0000 | [diff] [blame] | 484 | |
Ted Kremenek | 14553ab | 2009-01-30 00:08:43 +0000 | [diff] [blame] | 485 | // FIXME: Handle variable-length arrays. |
| 486 | if (isa<VariableArrayType>(T)) |
| 487 | return UnknownVal(); |
| 488 | |
| 489 | if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T)) { |
| 490 | // return the size as signed integer. |
| 491 | return NonLoc::MakeVal(getBasicVals(), CAT->getSize(), false); |
| 492 | } |
Zhongxing Xu | 2079494 | 2009-05-06 08:33:50 +0000 | [diff] [blame] | 493 | |
Zhongxing Xu | 41fd018 | 2009-05-06 11:51:48 +0000 | [diff] [blame] | 494 | GRStateRef state(St, StateMgr); |
| 495 | const QualType* CastTy = state.get<RegionCasts>(VR); |
| 496 | |
Zhongxing Xu | e6536af | 2009-05-08 02:00:55 +0000 | [diff] [blame] | 497 | // If the VarRegion is cast to other type, compute the size with respect to |
| 498 | // that type. |
Zhongxing Xu | 41fd018 | 2009-05-06 11:51:48 +0000 | [diff] [blame] | 499 | if (CastTy) { |
| 500 | QualType EleTy =cast<PointerType>(CastTy->getTypePtr())->getPointeeType(); |
Zhongxing Xu | a82d8aa | 2009-05-09 03:57:34 +0000 | [diff] [blame] | 501 | QualType VarTy = VR->getValueType(getContext()); |
Zhongxing Xu | e6536af | 2009-05-08 02:00:55 +0000 | [diff] [blame] | 502 | uint64_t EleSize = getContext().getTypeSize(EleTy); |
| 503 | uint64_t VarSize = getContext().getTypeSize(VarTy); |
| 504 | return NonLoc::MakeIntVal(getBasicVals(), VarSize / EleSize, false); |
Zhongxing Xu | 41fd018 | 2009-05-06 11:51:48 +0000 | [diff] [blame] | 505 | } |
| 506 | |
Ted Kremenek | 14553ab | 2009-01-30 00:08:43 +0000 | [diff] [blame] | 507 | // Clients can use ordinary variables as if they were arrays. These |
| 508 | // essentially are arrays of size 1. |
| 509 | return NonLoc::MakeIntVal(getBasicVals(), 1, false); |
Zhongxing Xu | e8a964b | 2008-11-22 13:21:46 +0000 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | if (const StringRegion* SR = dyn_cast<StringRegion>(R)) { |
Zhongxing Xu | 6613d08 | 2008-11-24 02:18:56 +0000 | [diff] [blame] | 513 | const StringLiteral* Str = SR->getStringLiteral(); |
Zhongxing Xu | d0fd3b7 | 2008-11-24 02:30:48 +0000 | [diff] [blame] | 514 | // We intentionally made the size value signed because it participates in |
| 515 | // operations with signed indices. |
Ted Kremenek | 14553ab | 2009-01-30 00:08:43 +0000 | [diff] [blame] | 516 | return NonLoc::MakeIntVal(getBasicVals(), Str->getByteLength()+1, false); |
Zhongxing Xu | e8a964b | 2008-11-22 13:21:46 +0000 | [diff] [blame] | 517 | } |
| 518 | |
Zhongxing Xu | e8a964b | 2008-11-22 13:21:46 +0000 | [diff] [blame] | 519 | if (const FieldRegion* FR = dyn_cast<FieldRegion>(R)) { |
| 520 | // FIXME: Unsupported yet. |
| 521 | FR = 0; |
| 522 | return UnknownVal(); |
| 523 | } |
Zhongxing Xu | 369f429 | 2008-11-22 13:23:00 +0000 | [diff] [blame] | 524 | |
Zhongxing Xu | 02ebefb | 2009-02-06 08:51:30 +0000 | [diff] [blame] | 525 | if (isa<SymbolicRegion>(R)) { |
Zhongxing Xu | a48f737 | 2009-02-06 08:44:27 +0000 | [diff] [blame] | 526 | return UnknownVal(); |
| 527 | } |
| 528 | |
Zhongxing Xu | ce76078 | 2009-05-09 13:20:07 +0000 | [diff] [blame] | 529 | if (isa<AllocaRegion>(R)) { |
| 530 | return UnknownVal(); |
| 531 | } |
| 532 | |
Zhongxing Xu | ccb1616 | 2009-05-06 08:08:27 +0000 | [diff] [blame] | 533 | if (isa<ElementRegion>(R)) { |
| 534 | return UnknownVal(); |
| 535 | } |
| 536 | |
Zhongxing Xu | e8a964b | 2008-11-22 13:21:46 +0000 | [diff] [blame] | 537 | assert(0 && "Other regions are not supported yet."); |
Ted Kremenek | a21362d | 2009-01-06 19:12:06 +0000 | [diff] [blame] | 538 | return UnknownVal(); |
Zhongxing Xu | e8a964b | 2008-11-22 13:21:46 +0000 | [diff] [blame] | 539 | } |
| 540 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 541 | /// ArrayToPointer - Emulates the "decay" of an array to a pointer |
| 542 | /// type. 'Array' represents the lvalue of the array being decayed |
| 543 | /// to a pointer, and the returned SVal represents the decayed |
| 544 | /// version of that lvalue (i.e., a pointer to the first element of |
| 545 | /// the array). This is called by GRExprEngine when evaluating casts |
| 546 | /// from arrays to pointers. |
Zhongxing Xu | f1d537f | 2009-03-30 05:55:46 +0000 | [diff] [blame] | 547 | SVal RegionStoreManager::ArrayToPointer(Loc Array) { |
Ted Kremenek | abb042f | 2008-12-13 19:24:37 +0000 | [diff] [blame] | 548 | if (!isa<loc::MemRegionVal>(Array)) |
| 549 | return UnknownVal(); |
| 550 | |
| 551 | const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion(); |
| 552 | const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R); |
| 553 | |
Ted Kremenek | bbee1a7 | 2009-01-13 01:03:27 +0000 | [diff] [blame] | 554 | if (!ArrayR) |
Ted Kremenek | abb042f | 2008-12-13 19:24:37 +0000 | [diff] [blame] | 555 | return UnknownVal(); |
| 556 | |
Zhongxing Xu | a82d8aa | 2009-05-09 03:57:34 +0000 | [diff] [blame] | 557 | // Strip off typedefs from the ArrayRegion's ValueType. |
| 558 | QualType T = ArrayR->getValueType(getContext())->getDesugaredType(); |
Ted Kremenek | f936f45 | 2009-05-04 06:18:28 +0000 | [diff] [blame] | 559 | ArrayType *AT = cast<ArrayType>(T); |
| 560 | T = AT->getElementType(); |
| 561 | |
Zhongxing Xu | 63123d8 | 2008-11-23 04:30:35 +0000 | [diff] [blame] | 562 | nonloc::ConcreteInt Idx(getBasicVals().getZeroWithPtrWidth(false)); |
Ted Kremenek | f936f45 | 2009-05-04 06:18:28 +0000 | [diff] [blame] | 563 | ElementRegion* ER = MRMgr.getElementRegion(T, Idx, ArrayR); |
Zhongxing Xu | 0b7e642 | 2008-10-26 02:23:57 +0000 | [diff] [blame] | 564 | |
| 565 | return loc::MemRegionVal(ER); |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 566 | } |
| 567 | |
Zhongxing Xu | 8898059 | 2009-05-06 02:42:32 +0000 | [diff] [blame] | 568 | RegionStoreManager::CastResult |
| 569 | RegionStoreManager::CastRegion(const GRState* state, const MemRegion* R, |
Zhongxing Xu | 41fd018 | 2009-05-06 11:51:48 +0000 | [diff] [blame] | 570 | QualType CastToTy) { |
Zhongxing Xu | 8898059 | 2009-05-06 02:42:32 +0000 | [diff] [blame] | 571 | |
| 572 | ASTContext& Ctx = StateMgr.getContext(); |
| 573 | |
| 574 | // We need to know the real type of CastToTy. |
| 575 | QualType ToTy = Ctx.getCanonicalType(CastToTy); |
| 576 | |
| 577 | // Check cast to ObjCQualifiedID type. |
| 578 | if (isa<ObjCQualifiedIdType>(ToTy)) { |
| 579 | // FIXME: Record the type information aside. |
| 580 | return CastResult(state, R); |
| 581 | } |
| 582 | |
| 583 | // CodeTextRegion should be cast to only function pointer type. |
| 584 | if (isa<CodeTextRegion>(R)) { |
| 585 | assert(CastToTy->isFunctionPointerType() || CastToTy->isBlockPointerType()); |
| 586 | return CastResult(state, R); |
| 587 | } |
| 588 | |
Zhongxing Xu | db3a098 | 2009-05-09 10:03:08 +0000 | [diff] [blame] | 589 | // Now assume we are casting from pointer to pointer. Other cases should |
| 590 | // already be handled. |
Zhongxing Xu | 8898059 | 2009-05-06 02:42:32 +0000 | [diff] [blame] | 591 | QualType PointeeTy = cast<PointerType>(ToTy.getTypePtr())->getPointeeType(); |
| 592 | |
Zhongxing Xu | 8898059 | 2009-05-06 02:42:32 +0000 | [diff] [blame] | 593 | // Process region cast according to the kind of the region being cast. |
| 594 | |
Zhongxing Xu | 8898059 | 2009-05-06 02:42:32 +0000 | [diff] [blame] | 595 | // FIXME: Need to handle arbitrary downcasts. |
Zhongxing Xu | 8898059 | 2009-05-06 02:42:32 +0000 | [diff] [blame] | 596 | if (isa<SymbolicRegion>(R) || isa<AllocaRegion>(R)) { |
Zhongxing Xu | ce76078 | 2009-05-09 13:20:07 +0000 | [diff] [blame] | 597 | state = setCastType(state, R, ToTy); |
| 598 | return CastResult(state, R); |
Zhongxing Xu | 8898059 | 2009-05-06 02:42:32 +0000 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | // VarRegion, ElementRegion, and FieldRegion has an inherent type. Normally |
| 602 | // they should not be cast. We only layer an ElementRegion when the cast-to |
| 603 | // pointee type is of smaller size. In other cases, we return the original |
| 604 | // VarRegion. |
| 605 | if (isa<VarRegion>(R) || isa<ElementRegion>(R) || isa<FieldRegion>(R) |
| 606 | || isa<ObjCIvarRegion>(R) || isa<CompoundLiteralRegion>(R)) { |
Zhongxing Xu | 2572eda | 2009-05-08 07:28:25 +0000 | [diff] [blame] | 607 | // If the pointee type is incomplete, do not compute its size, and return |
| 608 | // the original region. |
| 609 | if (const RecordType *RT = dyn_cast<RecordType>(PointeeTy.getTypePtr())) { |
| 610 | const RecordDecl *D = RT->getDecl(); |
| 611 | if (!D->getDefinition(getContext())) |
| 612 | return CastResult(state, R); |
| 613 | } |
| 614 | |
Zhongxing Xu | a82d8aa | 2009-05-09 03:57:34 +0000 | [diff] [blame] | 615 | QualType ObjTy = cast<TypedRegion>(R)->getValueType(getContext()); |
Zhongxing Xu | fb1e331 | 2009-05-08 02:12:59 +0000 | [diff] [blame] | 616 | uint64_t PointeeTySize = getContext().getTypeSize(PointeeTy); |
| 617 | uint64_t ObjTySize = getContext().getTypeSize(ObjTy); |
| 618 | |
Zhongxing Xu | 5bf3287 | 2009-05-09 15:34:29 +0000 | [diff] [blame] | 619 | if ((PointeeTySize > 0 && PointeeTySize < ObjTySize) || |
| 620 | (ObjTy->isAggregateType() && PointeeTy->isScalarType())) { |
Zhongxing Xu | 2079494 | 2009-05-06 08:33:50 +0000 | [diff] [blame] | 621 | // Record the cast type of the region. |
| 622 | state = setCastType(state, R, ToTy); |
| 623 | |
Zhongxing Xu | ccb1616 | 2009-05-06 08:08:27 +0000 | [diff] [blame] | 624 | SVal Idx = ValMgr.makeZeroArrayIndex(); |
Zhongxing Xu | 2079494 | 2009-05-06 08:33:50 +0000 | [diff] [blame] | 625 | ElementRegion* ER = MRMgr.getElementRegion(PointeeTy, Idx, R); |
Zhongxing Xu | ccb1616 | 2009-05-06 08:08:27 +0000 | [diff] [blame] | 626 | return CastResult(state, ER); |
| 627 | } else |
| 628 | return CastResult(state, R); |
Zhongxing Xu | 8898059 | 2009-05-06 02:42:32 +0000 | [diff] [blame] | 629 | } |
| 630 | |
Zhongxing Xu | 8898059 | 2009-05-06 02:42:32 +0000 | [diff] [blame] | 631 | if (isa<ObjCObjectRegion>(R)) { |
| 632 | return CastResult(state, R); |
| 633 | } |
| 634 | |
| 635 | assert(0 && "Unprocessed region."); |
Daniel Dunbar | 4e60900 | 2009-05-18 16:48:48 +0000 | [diff] [blame] | 636 | return 0; |
Zhongxing Xu | 8898059 | 2009-05-06 02:42:32 +0000 | [diff] [blame] | 637 | } |
| 638 | |
Zhongxing Xu | 262fd03 | 2009-05-20 09:00:16 +0000 | [diff] [blame] | 639 | SVal RegionStoreManager::EvalBinOp(const GRState *state, |
| 640 | BinaryOperator::Opcode Op, Loc L, NonLoc R) { |
Zhongxing Xu | c4761f5 | 2009-05-09 15:18:12 +0000 | [diff] [blame] | 641 | // Assume the base location is MemRegionVal. |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 642 | if (!isa<loc::MemRegionVal>(L)) |
Zhongxing Xu | 94aa6c1 | 2009-03-02 07:52:23 +0000 | [diff] [blame] | 643 | return UnknownVal(); |
Zhongxing Xu | 94aa6c1 | 2009-03-02 07:52:23 +0000 | [diff] [blame] | 644 | |
Zhongxing Xu | a1718c7 | 2009-04-03 07:33:13 +0000 | [diff] [blame] | 645 | const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion(); |
Zhongxing Xu | c4761f5 | 2009-05-09 15:18:12 +0000 | [diff] [blame] | 646 | const ElementRegion *ER = 0; |
Zhongxing Xu | 262fd03 | 2009-05-20 09:00:16 +0000 | [diff] [blame] | 647 | |
| 648 | // If the operand is a symbolic or alloca region, create the first element |
| 649 | // region on it. |
Zhongxing Xu | c4761f5 | 2009-05-09 15:18:12 +0000 | [diff] [blame] | 650 | if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(MR)) { |
| 651 | // Get symbol's type. It should be a pointer type. |
| 652 | SymbolRef Sym = SR->getSymbol(); |
| 653 | QualType T = Sym->getType(getContext()); |
Zhongxing Xu | 53454dc | 2009-06-12 03:59:12 +0000 | [diff] [blame^] | 654 | QualType EleTy = T->getAsPointerType()->getPointeeType(); |
Zhongxing Xu | a1718c7 | 2009-04-03 07:33:13 +0000 | [diff] [blame] | 655 | |
Zhongxing Xu | c4761f5 | 2009-05-09 15:18:12 +0000 | [diff] [blame] | 656 | SVal ZeroIdx = ValMgr.makeZeroArrayIndex(); |
| 657 | ER = MRMgr.getElementRegion(EleTy, ZeroIdx, SR); |
Zhongxing Xu | 262fd03 | 2009-05-20 09:00:16 +0000 | [diff] [blame] | 658 | } |
| 659 | else if (const AllocaRegion *AR = dyn_cast<AllocaRegion>(MR)) { |
| 660 | // Get the alloca region's current cast type. |
| 661 | GRStateRef StRef(state, StateMgr); |
| 662 | |
| 663 | GRStateTrait<RegionCasts>::lookup_type T = StRef.get<RegionCasts>(AR); |
| 664 | assert(T && "alloca region has no type."); |
| 665 | QualType EleTy = cast<PointerType>(T->getTypePtr())->getPointeeType(); |
| 666 | SVal ZeroIdx = ValMgr.makeZeroArrayIndex(); |
| 667 | ER = MRMgr.getElementRegion(EleTy, ZeroIdx, AR); |
| 668 | } |
| 669 | else |
Zhongxing Xu | c4761f5 | 2009-05-09 15:18:12 +0000 | [diff] [blame] | 670 | ER = cast<ElementRegion>(MR); |
Zhongxing Xu | 2b1dc17 | 2009-03-11 07:43:49 +0000 | [diff] [blame] | 671 | |
Zhongxing Xu | 94aa6c1 | 2009-03-02 07:52:23 +0000 | [diff] [blame] | 672 | SVal Idx = ER->getIndex(); |
| 673 | |
| 674 | nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx); |
| 675 | nonloc::ConcreteInt* Offset = dyn_cast<nonloc::ConcreteInt>(&R); |
| 676 | |
| 677 | // Only support concrete integer indexes for now. |
| 678 | if (Base && Offset) { |
Ted Kremenek | 610e81d | 2009-03-13 15:35:24 +0000 | [diff] [blame] | 679 | // FIXME: For now, convert the signedness and bitwidth of offset in case |
| 680 | // they don't match. This can result from pointer arithmetic. In reality, |
| 681 | // we should figure out what are the proper semantics and implement them. |
| 682 | // |
Ted Kremenek | 1060a3c | 2009-03-13 15:39:16 +0000 | [diff] [blame] | 683 | // This addresses the test case test/Analysis/ptr-arith.c |
| 684 | // |
Ted Kremenek | 610e81d | 2009-03-13 15:35:24 +0000 | [diff] [blame] | 685 | nonloc::ConcreteInt OffConverted(getBasicVals().Convert(Base->getValue(), |
| 686 | Offset->getValue())); |
| 687 | SVal NewIdx = Base->EvalBinOp(getBasicVals(), Op, OffConverted); |
Ted Kremenek | f936f45 | 2009-05-04 06:18:28 +0000 | [diff] [blame] | 688 | const MemRegion* NewER = |
Zhongxing Xu | c4761f5 | 2009-05-09 15:18:12 +0000 | [diff] [blame] | 689 | MRMgr.getElementRegion(ER->getElementType(), NewIdx,ER->getSuperRegion()); |
Zhongxing Xu | 94aa6c1 | 2009-03-02 07:52:23 +0000 | [diff] [blame] | 690 | return Loc::MakeVal(NewER); |
| 691 | |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | return UnknownVal(); |
Zhongxing Xu | 94aa6c1 | 2009-03-02 07:52:23 +0000 | [diff] [blame] | 695 | } |
| 696 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 697 | SVal RegionStoreManager::Retrieve(const GRState* St, Loc L, QualType T) { |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 698 | assert(!isa<UnknownVal>(L) && "location unknown"); |
| 699 | assert(!isa<UndefinedVal>(L) && "location undefined"); |
| 700 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 701 | // FIXME: Is this even possible? Shouldn't this be treated as a null |
| 702 | // dereference at a higher level? |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 703 | if (isa<loc::ConcreteInt>(L)) |
| 704 | return UndefinedVal(); |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 705 | |
Zhongxing Xu | a1718c7 | 2009-04-03 07:33:13 +0000 | [diff] [blame] | 706 | const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion(); |
| 707 | |
Zhongxing Xu | 9184412 | 2009-05-20 09:18:48 +0000 | [diff] [blame] | 708 | // FIXME: return symbolic value for these cases. |
Zhongxing Xu | a1718c7 | 2009-04-03 07:33:13 +0000 | [diff] [blame] | 709 | // Example: |
| 710 | // void f(int* p) { int x = *p; } |
Zhongxing Xu | 9184412 | 2009-05-20 09:18:48 +0000 | [diff] [blame] | 711 | // char* p = alloca(); |
| 712 | // read(p); |
| 713 | // c = *p; |
| 714 | if (isa<SymbolicRegion>(MR) || isa<AllocaRegion>(MR)) |
Zhongxing Xu | a1718c7 | 2009-04-03 07:33:13 +0000 | [diff] [blame] | 715 | return UnknownVal(); |
| 716 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 717 | // FIXME: Perhaps this method should just take a 'const MemRegion*' argument |
| 718 | // 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] | 719 | const TypedRegion* R = cast<TypedRegion>(MR); |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 720 | assert(R && "bad region"); |
| 721 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 722 | // FIXME: We should eventually handle funny addressing. e.g.: |
| 723 | // |
| 724 | // int x = ...; |
| 725 | // int *p = &x; |
| 726 | // char *q = (char*) p; |
| 727 | // char c = *q; // returns the first byte of 'x'. |
| 728 | // |
| 729 | // Such funny addressing will occur due to layering of regions. |
| 730 | |
Zhongxing Xu | a82d8aa | 2009-05-09 03:57:34 +0000 | [diff] [blame] | 731 | QualType RTy = R->getValueType(getContext()); |
Zhongxing Xu | 3e001f3 | 2009-05-03 00:27:40 +0000 | [diff] [blame] | 732 | |
Zhongxing Xu | 1038f9f | 2009-03-09 09:15:51 +0000 | [diff] [blame] | 733 | if (RTy->isStructureType()) |
| 734 | return RetrieveStruct(St, R); |
Zhongxing Xu | 3e001f3 | 2009-05-03 00:27:40 +0000 | [diff] [blame] | 735 | |
| 736 | if (RTy->isArrayType()) |
| 737 | return RetrieveArray(St, R); |
| 738 | |
Zhongxing Xu | 1038f9f | 2009-03-09 09:15:51 +0000 | [diff] [blame] | 739 | // FIXME: handle Vector types. |
| 740 | if (RTy->isVectorType()) |
Ted Kremenek | 265a305 | 2009-02-24 02:23:11 +0000 | [diff] [blame] | 741 | return UnknownVal(); |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 742 | |
| 743 | RegionBindingsTy B = GetRegionBindings(St->getStore()); |
| 744 | RegionBindingsTy::data_type* V = B.lookup(R); |
| 745 | |
| 746 | // Check if the region has a binding. |
| 747 | if (V) |
| 748 | return *V; |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 749 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 750 | GRStateRef state(St, StateMgr); |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 751 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 752 | // Check if the region is in killset. |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 753 | if (state.contains<RegionKills>(R)) |
| 754 | return UnknownVal(); |
| 755 | |
Zhongxing Xu | c87d5fb | 2009-05-11 14:23:36 +0000 | [diff] [blame] | 756 | // Check if the region is an element region of a string literal. |
| 757 | if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) { |
| 758 | if (const StringRegion *StrR=dyn_cast<StringRegion>(ER->getSuperRegion())) { |
| 759 | const StringLiteral *Str = StrR->getStringLiteral(); |
| 760 | SVal Idx = ER->getIndex(); |
| 761 | if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&Idx)) { |
| 762 | int64_t i = CI->getValue().getSExtValue(); |
| 763 | char c; |
| 764 | if (i == Str->getByteLength()) |
| 765 | c = '\0'; |
| 766 | else |
| 767 | c = Str->getStrData()[i]; |
| 768 | const llvm::APSInt &V = getBasicVals().getValue(c, getContext().CharTy); |
| 769 | return nonloc::ConcreteInt(V); |
| 770 | } |
| 771 | } |
| 772 | } |
| 773 | |
Ted Kremenek | 3de2d3c | 2009-03-05 04:50:08 +0000 | [diff] [blame] | 774 | // 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] | 775 | if (isa<ElementRegion>(R) || isa<FieldRegion>(R)) { |
| 776 | const MemRegion* SuperR = cast<SubRegion>(R)->getSuperRegion(); |
| 777 | GRStateTrait<RegionDefaultValue>::lookup_type D = |
| 778 | state.get<RegionDefaultValue>(SuperR); |
Zhongxing Xu | 264e937 | 2009-05-12 10:10:00 +0000 | [diff] [blame] | 779 | if (D) { |
| 780 | // If the default value is symbolic, we need to create a new symbol. |
| 781 | if (D->hasConjuredSymbol()) |
| 782 | return ValMgr.getRegionValueSymbolVal(R); |
| 783 | else |
| 784 | return *D; |
| 785 | } |
Zhongxing Xu | 562c4d9 | 2009-01-23 11:22:12 +0000 | [diff] [blame] | 786 | } |
Ted Kremenek | 3de2d3c | 2009-03-05 04:50:08 +0000 | [diff] [blame] | 787 | |
| 788 | if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R)) { |
| 789 | const MemRegion *SR = IVR->getSuperRegion(); |
| 790 | |
| 791 | // If the super region is 'self' then return the symbol representing |
| 792 | // the value of the ivar upon entry to the method. |
| 793 | if (SR == SelfRegion) { |
| 794 | // FIXME: Do we need to handle the case where the super region |
| 795 | // has a view? We want to canonicalize the bindings. |
Zhongxing Xu | d9b6ad6 | 2009-05-09 04:08:27 +0000 | [diff] [blame] | 796 | return ValMgr.getRegionValueSymbolVal(R); |
Ted Kremenek | 3de2d3c | 2009-03-05 04:50:08 +0000 | [diff] [blame] | 797 | } |
| 798 | |
| 799 | // Otherwise, we need a new symbol. For now return Unknown. |
| 800 | return UnknownVal(); |
| 801 | } |
Zhongxing Xu | 562c4d9 | 2009-01-23 11:22:12 +0000 | [diff] [blame] | 802 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 803 | // The location does not have a bound value. This means that it has |
| 804 | // the value it had upon its creation and/or entry to the analyzed |
| 805 | // function/method. These are either symbolic values or 'undefined'. |
| 806 | |
| 807 | // We treat function parameters as symbolic values. |
Ted Kremenek | 6fd8f91 | 2009-01-22 23:43:57 +0000 | [diff] [blame] | 808 | if (const VarRegion* VR = dyn_cast<VarRegion>(R)) { |
| 809 | const VarDecl *VD = VR->getDecl(); |
| 810 | |
Ted Kremenek | cec3525 | 2009-03-04 00:23:05 +0000 | [diff] [blame] | 811 | if (VD == SelfDecl) |
| 812 | return loc::MemRegionVal(getSelfRegion(0)); |
| 813 | |
| 814 | if (isa<ParmVarDecl>(VD) || isa<ImplicitParamDecl>(VD) || |
| 815 | VD->hasGlobalStorage()) { |
Zhongxing Xu | d8895f6 | 2009-02-19 09:56:08 +0000 | [diff] [blame] | 816 | QualType VTy = VD->getType(); |
| 817 | if (Loc::IsLocType(VTy) || VTy->isIntegerType()) |
Zhongxing Xu | d9b6ad6 | 2009-05-09 04:08:27 +0000 | [diff] [blame] | 818 | return ValMgr.getRegionValueSymbolVal(VR); |
Zhongxing Xu | d8895f6 | 2009-02-19 09:56:08 +0000 | [diff] [blame] | 819 | else |
| 820 | return UnknownVal(); |
| 821 | } |
Ted Kremenek | 3de2d3c | 2009-03-05 04:50:08 +0000 | [diff] [blame] | 822 | } |
Ted Kremenek | 265a305 | 2009-02-24 02:23:11 +0000 | [diff] [blame] | 823 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 824 | if (MRMgr.onStack(R) || MRMgr.onHeap(R)) { |
| 825 | // All stack variables are considered to have undefined values |
| 826 | // upon creation. All heap allocated blocks are considered to |
| 827 | // have undefined values as well unless they are explicitly bound |
| 828 | // to specific values. |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 829 | return UndefinedVal(); |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 830 | } |
| 831 | |
Zhongxing Xu | ff8d204 | 2009-03-09 09:31:22 +0000 | [diff] [blame] | 832 | // All other integer values are symbolic. |
| 833 | if (Loc::IsLocType(RTy) || RTy->isIntegerType()) |
Zhongxing Xu | d9b6ad6 | 2009-05-09 04:08:27 +0000 | [diff] [blame] | 834 | return ValMgr.getRegionValueSymbolVal(R); |
Zhongxing Xu | ff8d204 | 2009-03-09 09:31:22 +0000 | [diff] [blame] | 835 | else |
| 836 | return UnknownVal(); |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 837 | } |
| 838 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 839 | SVal RegionStoreManager::RetrieveStruct(const GRState* St,const TypedRegion* R){ |
Zhongxing Xu | a82d8aa | 2009-05-09 03:57:34 +0000 | [diff] [blame] | 840 | QualType T = R->getValueType(getContext()); |
Zhongxing Xu | 6e3f01c | 2008-10-31 07:16:08 +0000 | [diff] [blame] | 841 | assert(T->isStructureType()); |
| 842 | |
Zhongxing Xu | b7507d1 | 2009-06-11 07:27:30 +0000 | [diff] [blame] | 843 | const RecordType* RT = T->getAsStructureType(); |
Zhongxing Xu | 6e3f01c | 2008-10-31 07:16:08 +0000 | [diff] [blame] | 844 | RecordDecl* RD = RT->getDecl(); |
| 845 | assert(RD->isDefinition()); |
| 846 | |
| 847 | llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList(); |
| 848 | |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 849 | std::vector<FieldDecl *> Fields(RD->field_begin(getContext()), |
| 850 | RD->field_end(getContext())); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 851 | |
Douglas Gregor | e267ff3 | 2008-12-11 20:41:00 +0000 | [diff] [blame] | 852 | for (std::vector<FieldDecl *>::reverse_iterator Field = Fields.rbegin(), |
| 853 | FieldEnd = Fields.rend(); |
| 854 | Field != FieldEnd; ++Field) { |
| 855 | FieldRegion* FR = MRMgr.getFieldRegion(*Field, R); |
Zhongxing Xu | 3e001f3 | 2009-05-03 00:27:40 +0000 | [diff] [blame] | 856 | QualType FTy = (*Field)->getType(); |
| 857 | SVal FieldValue = Retrieve(St, loc::MemRegionVal(FR), FTy); |
Zhongxing Xu | 6e3f01c | 2008-10-31 07:16:08 +0000 | [diff] [blame] | 858 | StructVal = getBasicVals().consVals(FieldValue, StructVal); |
| 859 | } |
| 860 | |
| 861 | return NonLoc::MakeCompoundVal(T, StructVal, getBasicVals()); |
| 862 | } |
| 863 | |
Zhongxing Xu | 3e001f3 | 2009-05-03 00:27:40 +0000 | [diff] [blame] | 864 | SVal RegionStoreManager::RetrieveArray(const GRState* St, const TypedRegion* R){ |
Zhongxing Xu | a82d8aa | 2009-05-09 03:57:34 +0000 | [diff] [blame] | 865 | QualType T = R->getValueType(getContext()); |
Zhongxing Xu | 3e001f3 | 2009-05-03 00:27:40 +0000 | [diff] [blame] | 866 | ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr()); |
| 867 | |
| 868 | llvm::ImmutableList<SVal> ArrayVal = getBasicVals().getEmptySValList(); |
Zhongxing Xu | 3e001f3 | 2009-05-03 00:27:40 +0000 | [diff] [blame] | 869 | llvm::APSInt Size(CAT->getSize(), false); |
Zhongxing Xu | 2ee5214 | 2009-05-11 12:48:56 +0000 | [diff] [blame] | 870 | llvm::APSInt i = getBasicVals().getZeroWithPtrWidth(false); |
Zhongxing Xu | 3e001f3 | 2009-05-03 00:27:40 +0000 | [diff] [blame] | 871 | |
| 872 | for (; i < Size; ++i) { |
| 873 | SVal Idx = NonLoc::MakeVal(getBasicVals(), i); |
Zhongxing Xu | 2ee5214 | 2009-05-11 12:48:56 +0000 | [diff] [blame] | 874 | ElementRegion* ER = MRMgr.getElementRegion(CAT->getElementType(), Idx, R); |
Ted Kremenek | f936f45 | 2009-05-04 06:18:28 +0000 | [diff] [blame] | 875 | QualType ETy = ER->getElementType(); |
Zhongxing Xu | 3e001f3 | 2009-05-03 00:27:40 +0000 | [diff] [blame] | 876 | SVal ElementVal = Retrieve(St, loc::MemRegionVal(ER), ETy); |
| 877 | ArrayVal = getBasicVals().consVals(ElementVal, ArrayVal); |
| 878 | } |
| 879 | |
| 880 | return NonLoc::MakeCompoundVal(T, ArrayVal, getBasicVals()); |
| 881 | } |
| 882 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 883 | const GRState* RegionStoreManager::Bind(const GRState* St, Loc L, SVal V) { |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 884 | // If we get here, the location should be a region. |
| 885 | const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion(); |
Zhongxing Xu | f0dfa8d | 2008-10-31 08:10:01 +0000 | [diff] [blame] | 886 | assert(R); |
| 887 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 888 | // Check if the region is a struct region. |
Zhongxing Xu | f0dfa8d | 2008-10-31 08:10:01 +0000 | [diff] [blame] | 889 | if (const TypedRegion* TR = dyn_cast<TypedRegion>(R)) |
Zhongxing Xu | a82d8aa | 2009-05-09 03:57:34 +0000 | [diff] [blame] | 890 | if (TR->getValueType(getContext())->isStructureType()) |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 891 | return BindStruct(St, TR, V); |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 892 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 893 | Store store = St->getStore(); |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 894 | RegionBindingsTy B = GetRegionBindings(store); |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 895 | |
| 896 | if (V.isUnknown()) { |
| 897 | // Remove the binding. |
| 898 | store = RBFactory.Remove(B, R).getRoot(); |
| 899 | |
| 900 | // Add the region to the killset. |
| 901 | GRStateRef state(St, StateMgr); |
| 902 | St = state.add<RegionKills>(R); |
| 903 | } |
| 904 | else |
| 905 | store = RBFactory.Add(B, R, V).getRoot(); |
| 906 | |
| 907 | return StateMgr.MakeStateWithStore(St, store); |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 908 | } |
| 909 | |
Zhongxing Xu | 9c9ca08 | 2008-12-16 02:36:30 +0000 | [diff] [blame] | 910 | Store RegionStoreManager::Remove(Store store, Loc L) { |
Ted Kremenek | 0964a06 | 2009-01-21 06:57:53 +0000 | [diff] [blame] | 911 | const MemRegion* R = 0; |
| 912 | |
| 913 | if (isa<loc::MemRegionVal>(L)) |
| 914 | R = cast<loc::MemRegionVal>(L).getRegion(); |
Ted Kremenek | 0964a06 | 2009-01-21 06:57:53 +0000 | [diff] [blame] | 915 | |
| 916 | if (R) { |
| 917 | RegionBindingsTy B = GetRegionBindings(store); |
| 918 | return RBFactory.Remove(B, R).getRoot(); |
| 919 | } |
| 920 | |
| 921 | return store; |
Zhongxing Xu | 9c9ca08 | 2008-12-16 02:36:30 +0000 | [diff] [blame] | 922 | } |
| 923 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 924 | const GRState* RegionStoreManager::BindDecl(const GRState* St, |
| 925 | const VarDecl* VD, SVal InitVal) { |
Zhongxing Xu | a4f28ff | 2008-11-13 08:41:36 +0000 | [diff] [blame] | 926 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 927 | QualType T = VD->getType(); |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 928 | VarRegion* VR = MRMgr.getVarRegion(VD); |
Zhongxing Xu | f0dfa8d | 2008-10-31 08:10:01 +0000 | [diff] [blame] | 929 | |
Ted Kremenek | 0964a06 | 2009-01-21 06:57:53 +0000 | [diff] [blame] | 930 | if (T->isArrayType()) |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 931 | return BindArray(St, VR, InitVal); |
Ted Kremenek | 0964a06 | 2009-01-21 06:57:53 +0000 | [diff] [blame] | 932 | if (T->isStructureType()) |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 933 | return BindStruct(St, VR, InitVal); |
Zhongxing Xu | d463d44 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 934 | |
Ted Kremenek | 0964a06 | 2009-01-21 06:57:53 +0000 | [diff] [blame] | 935 | return Bind(St, Loc::MakeVal(VR), InitVal); |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 936 | } |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 937 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 938 | // FIXME: this method should be merged into Bind(). |
| 939 | const GRState* |
| 940 | RegionStoreManager::BindCompoundLiteral(const GRState* St, |
| 941 | const CompoundLiteralExpr* CL, SVal V) { |
Zhongxing Xu | f22679e | 2008-11-07 10:38:33 +0000 | [diff] [blame] | 942 | CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL); |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 943 | return Bind(St, loc::MemRegionVal(R), V); |
Zhongxing Xu | f22679e | 2008-11-07 10:38:33 +0000 | [diff] [blame] | 944 | } |
| 945 | |
Zhongxing Xu | baf03a7 | 2008-11-24 09:44:56 +0000 | [diff] [blame] | 946 | const GRState* RegionStoreManager::setExtent(const GRState* St, |
| 947 | const MemRegion* R, SVal Extent) { |
| 948 | GRStateRef state(St, StateMgr); |
Ted Kremenek | 50dc1b3 | 2008-12-24 01:05:03 +0000 | [diff] [blame] | 949 | return state.set<RegionExtents>(R, Extent); |
Zhongxing Xu | baf03a7 | 2008-11-24 09:44:56 +0000 | [diff] [blame] | 950 | } |
| 951 | |
| 952 | |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 953 | static void UpdateLiveSymbols(SVal X, SymbolReaper& SymReaper) { |
Ted Kremenek | 02c4d2d | 2009-03-04 00:11:38 +0000 | [diff] [blame] | 954 | if (loc::MemRegionVal *XR = dyn_cast<loc::MemRegionVal>(&X)) { |
| 955 | const MemRegion *R = XR->getRegion(); |
| 956 | |
| 957 | while (R) { |
| 958 | if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) { |
| 959 | SymReaper.markLive(SR->getSymbol()); |
| 960 | return; |
| 961 | } |
| 962 | |
| 963 | if (const SubRegion *SR = dyn_cast<SubRegion>(R)) { |
| 964 | R = SR->getSuperRegion(); |
| 965 | continue; |
| 966 | } |
| 967 | |
| 968 | break; |
| 969 | } |
| 970 | |
| 971 | return; |
| 972 | } |
| 973 | |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 974 | for (SVal::symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end();SI!=SE;++SI) |
| 975 | SymReaper.markLive(*SI); |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 976 | } |
| 977 | |
Ted Kremenek | 2ed14be | 2008-12-05 00:47:52 +0000 | [diff] [blame] | 978 | Store RegionStoreManager::RemoveDeadBindings(const GRState* state, Stmt* Loc, |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 979 | SymbolReaper& SymReaper, |
| 980 | llvm::SmallVectorImpl<const MemRegion*>& RegionRoots) |
| 981 | { |
Zhongxing Xu | 8916d5b | 2008-11-10 09:39:04 +0000 | [diff] [blame] | 982 | |
Ted Kremenek | 2ed14be | 2008-12-05 00:47:52 +0000 | [diff] [blame] | 983 | Store store = state->getStore(); |
Zhongxing Xu | 8916d5b | 2008-11-10 09:39:04 +0000 | [diff] [blame] | 984 | RegionBindingsTy B = GetRegionBindings(store); |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 985 | |
| 986 | // Lazily constructed backmap from MemRegions to SubRegions. |
| 987 | typedef llvm::ImmutableSet<const MemRegion*> SubRegionsTy; |
| 988 | typedef llvm::ImmutableMap<const MemRegion*, SubRegionsTy> SubRegionsMapTy; |
| 989 | |
| 990 | // FIXME: As a future optimization we can modifiy BumpPtrAllocator to have |
| 991 | // the ability to reuse memory. This way we can keep TmpAlloc around as |
| 992 | // an instance variable of RegionStoreManager (avoiding repeated malloc |
| 993 | // overhead). |
| 994 | llvm::BumpPtrAllocator TmpAlloc; |
| 995 | |
| 996 | // Factory objects. |
| 997 | SubRegionsMapTy::Factory SubRegMapF(TmpAlloc); |
| 998 | SubRegionsTy::Factory SubRegF(TmpAlloc); |
| 999 | |
| 1000 | // The backmap from regions to subregions. |
| 1001 | SubRegionsMapTy SubRegMap = SubRegMapF.GetEmptyMap(); |
| 1002 | |
| 1003 | // Do a pass over the regions in the store. For VarRegions we check if |
| 1004 | // the variable is still live and if so add it to the list of live roots. |
| 1005 | // For other regions we populate our region backmap. |
Zhongxing Xu | c2fdb07 | 2009-03-18 01:54:31 +0000 | [diff] [blame] | 1006 | |
| 1007 | llvm::SmallVector<const MemRegion*, 10> IntermediateRoots; |
| 1008 | |
Zhongxing Xu | 8916d5b | 2008-11-10 09:39:04 +0000 | [diff] [blame] | 1009 | for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) { |
Zhongxing Xu | c2fdb07 | 2009-03-18 01:54:31 +0000 | [diff] [blame] | 1010 | IntermediateRoots.push_back(I.getKey()); |
| 1011 | } |
| 1012 | |
| 1013 | while (!IntermediateRoots.empty()) { |
| 1014 | const MemRegion* R = IntermediateRoots.back(); |
| 1015 | IntermediateRoots.pop_back(); |
| 1016 | |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 1017 | if (const VarRegion* VR = dyn_cast<VarRegion>(R)) { |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 1018 | if (SymReaper.isLive(Loc, VR->getDecl())) |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 1019 | RegionRoots.push_back(VR); // This is a live "root". |
Zhongxing Xu | 5c86b19 | 2009-04-29 09:24:35 +0000 | [diff] [blame] | 1020 | } |
| 1021 | else if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(R)) { |
| 1022 | if (SymReaper.isLive(SR->getSymbol())) |
| 1023 | RegionRoots.push_back(SR); |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 1024 | } |
| 1025 | else { |
| 1026 | // Get the super region for R. |
| 1027 | const MemRegion* SuperR = cast<SubRegion>(R)->getSuperRegion(); |
Zhongxing Xu | c2fdb07 | 2009-03-18 01:54:31 +0000 | [diff] [blame] | 1028 | |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 1029 | // Get the current set of subregions for SuperR. |
| 1030 | const SubRegionsTy* SRptr = SubRegMap.lookup(SuperR); |
Zhongxing Xu | a3eda83 | 2009-05-08 23:28:07 +0000 | [diff] [blame] | 1031 | SubRegionsTy SRs = SRptr ? *SRptr : SubRegF.GetEmptySet(); |
Zhongxing Xu | c2fdb07 | 2009-03-18 01:54:31 +0000 | [diff] [blame] | 1032 | |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 1033 | // Add R to the subregions of SuperR. |
Zhongxing Xu | a3eda83 | 2009-05-08 23:28:07 +0000 | [diff] [blame] | 1034 | SubRegMap = SubRegMapF.Add(SubRegMap, SuperR, SubRegF.Add(SRs, R)); |
Zhongxing Xu | c2fdb07 | 2009-03-18 01:54:31 +0000 | [diff] [blame] | 1035 | |
| 1036 | // Super region may be VarRegion or subregion of another VarRegion. Add it |
| 1037 | // to the work list. |
| 1038 | if (isa<SubRegion>(SuperR)) |
| 1039 | IntermediateRoots.push_back(SuperR); |
Ted Kremenek | 3de2d3c | 2009-03-05 04:50:08 +0000 | [diff] [blame] | 1040 | } |
Zhongxing Xu | 8916d5b | 2008-11-10 09:39:04 +0000 | [diff] [blame] | 1041 | } |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 1042 | |
| 1043 | // Process the worklist of RegionRoots. This performs a "mark-and-sweep" |
| 1044 | // of the store. We want to find all live symbols and dead regions. |
| 1045 | llvm::SmallPtrSet<const MemRegion*, 10> Marked; |
| 1046 | |
| 1047 | while (!RegionRoots.empty()) { |
| 1048 | // Dequeue the next region on the worklist. |
| 1049 | const MemRegion* R = RegionRoots.back(); |
| 1050 | RegionRoots.pop_back(); |
Zhongxing Xu | 8916d5b | 2008-11-10 09:39:04 +0000 | [diff] [blame] | 1051 | |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 1052 | // Check if we have already processed this region. |
| 1053 | if (Marked.count(R)) continue; |
| 1054 | |
| 1055 | // Mark this region as processed. This is needed for termination in case |
| 1056 | // a region is referenced more than once. |
| 1057 | Marked.insert(R); |
| 1058 | |
| 1059 | // Mark the symbol for any live SymbolicRegion as "live". This means we |
| 1060 | // should continue to track that symbol. |
| 1061 | if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R)) |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 1062 | SymReaper.markLive(SymR->getSymbol()); |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 1063 | |
| 1064 | // Get the data binding for R (if any). |
| 1065 | RegionBindingsTy::data_type* Xptr = B.lookup(R); |
| 1066 | if (Xptr) { |
| 1067 | SVal X = *Xptr; |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 1068 | UpdateLiveSymbols(X, SymReaper); // Update the set of live symbols. |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 1069 | |
| 1070 | // If X is a region, then add it the RegionRoots. |
| 1071 | if (loc::MemRegionVal* RegionX = dyn_cast<loc::MemRegionVal>(&X)) |
| 1072 | RegionRoots.push_back(RegionX->getRegion()); |
| 1073 | } |
| 1074 | |
| 1075 | // Get the subregions of R. These are RegionRoots as well since they |
| 1076 | // represent values that are also bound to R. |
| 1077 | const SubRegionsTy* SRptr = SubRegMap.lookup(R); |
| 1078 | if (!SRptr) continue; |
| 1079 | SubRegionsTy SR = *SRptr; |
| 1080 | |
| 1081 | for (SubRegionsTy::iterator I=SR.begin(), E=SR.end(); I!=E; ++I) |
| 1082 | RegionRoots.push_back(*I); |
| 1083 | } |
| 1084 | |
| 1085 | // We have now scanned the store, marking reachable regions and symbols |
| 1086 | // 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] | 1087 | // 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] | 1088 | for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) { |
| 1089 | const MemRegion* R = I.getKey(); |
| 1090 | |
| 1091 | // If this region live? Is so, none of its symbols are dead. |
| 1092 | if (Marked.count(R)) |
| 1093 | continue; |
| 1094 | |
| 1095 | // Remove this dead region from the store. |
Zhongxing Xu | 9c9ca08 | 2008-12-16 02:36:30 +0000 | [diff] [blame] | 1096 | store = Remove(store, Loc::MakeVal(R)); |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 1097 | |
| 1098 | // Mark all non-live symbols that this region references as dead. |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 1099 | if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R)) |
| 1100 | SymReaper.maybeDead(SymR->getSymbol()); |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 1101 | |
| 1102 | SVal X = I.getData(); |
| 1103 | SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end(); |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 1104 | for (; SI != SE; ++SI) SymReaper.maybeDead(*SI); |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 1105 | } |
| 1106 | |
Zhongxing Xu | 8916d5b | 2008-11-10 09:39:04 +0000 | [diff] [blame] | 1107 | return store; |
| 1108 | } |
| 1109 | |
Zhongxing Xu | a071eb0 | 2008-10-24 06:01:33 +0000 | [diff] [blame] | 1110 | void RegionStoreManager::print(Store store, std::ostream& Out, |
| 1111 | const char* nl, const char *sep) { |
| 1112 | llvm::raw_os_ostream OS(Out); |
| 1113 | RegionBindingsTy B = GetRegionBindings(store); |
| 1114 | OS << "Store:" << nl; |
| 1115 | |
| 1116 | for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) { |
| 1117 | OS << ' '; I.getKey()->print(OS); OS << " : "; |
| 1118 | I.getData().print(OS); OS << nl; |
| 1119 | } |
Zhongxing Xu | 5b8b6f2 | 2008-10-24 04:33:15 +0000 | [diff] [blame] | 1120 | } |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 1121 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1122 | const GRState* RegionStoreManager::BindArray(const GRState* St, |
| 1123 | const TypedRegion* R, SVal Init) { |
Zhongxing Xu | a82d8aa | 2009-05-09 03:57:34 +0000 | [diff] [blame] | 1124 | QualType T = R->getValueType(getContext()); |
Zhongxing Xu | 1a12a0e | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 1125 | assert(T->isArrayType()); |
| 1126 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1127 | // When we are binding the whole array, it always has default value 0. |
| 1128 | GRStateRef state(St, StateMgr); |
Ted Kremenek | 14553ab | 2009-01-30 00:08:43 +0000 | [diff] [blame] | 1129 | St = state.set<RegionDefaultValue>(R, NonLoc::MakeIntVal(getBasicVals(), 0, |
| 1130 | false)); |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1131 | |
Zhongxing Xu | 1a12a0e | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 1132 | ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr()); |
| 1133 | |
Zhongxing Xu | 6987c7b | 2008-11-30 05:49:49 +0000 | [diff] [blame] | 1134 | llvm::APSInt Size(CAT->getSize(), false); |
Sebastian Redl | 1be3da5 | 2009-01-26 19:54:12 +0000 | [diff] [blame] | 1135 | llvm::APSInt i = getBasicVals().getValue(0, Size.getBitWidth(), |
| 1136 | Size.isUnsigned()); |
Zhongxing Xu | 6987c7b | 2008-11-30 05:49:49 +0000 | [diff] [blame] | 1137 | |
| 1138 | // Check if the init expr is a StringLiteral. |
| 1139 | if (isa<loc::MemRegionVal>(Init)) { |
| 1140 | const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion(); |
| 1141 | const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral(); |
| 1142 | const char* str = S->getStrData(); |
| 1143 | unsigned len = S->getByteLength(); |
| 1144 | unsigned j = 0; |
| 1145 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1146 | // Copy bytes from the string literal into the target array. Trailing bytes |
| 1147 | // in the array that are not covered by the string literal are initialized |
| 1148 | // to zero. |
Zhongxing Xu | 6987c7b | 2008-11-30 05:49:49 +0000 | [diff] [blame] | 1149 | for (; i < Size; ++i, ++j) { |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1150 | if (j >= len) |
| 1151 | break; |
| 1152 | |
Zhongxing Xu | 6987c7b | 2008-11-30 05:49:49 +0000 | [diff] [blame] | 1153 | SVal Idx = NonLoc::MakeVal(getBasicVals(), i); |
Ted Kremenek | f936f45 | 2009-05-04 06:18:28 +0000 | [diff] [blame] | 1154 | ElementRegion* ER = |
| 1155 | MRMgr.getElementRegion(cast<ArrayType>(T)->getElementType(), |
| 1156 | Idx, R); |
Zhongxing Xu | 6987c7b | 2008-11-30 05:49:49 +0000 | [diff] [blame] | 1157 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1158 | SVal V = NonLoc::MakeVal(getBasicVals(), str[j], sizeof(char)*8, true); |
| 1159 | St = Bind(St, loc::MemRegionVal(ER), V); |
Zhongxing Xu | 6987c7b | 2008-11-30 05:49:49 +0000 | [diff] [blame] | 1160 | } |
| 1161 | |
Zhongxing Xu | bd4f745 | 2009-03-09 06:49:50 +0000 | [diff] [blame] | 1162 | return St; |
Zhongxing Xu | 6987c7b | 2008-11-30 05:49:49 +0000 | [diff] [blame] | 1163 | } |
| 1164 | |
Zhongxing Xu | 1a12a0e | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 1165 | nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init); |
Zhongxing Xu | 1a12a0e | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 1166 | nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end(); |
| 1167 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1168 | for (; i < Size; ++i, ++VI) { |
| 1169 | // The init list might be shorter than the array decl. |
| 1170 | if (VI == VE) |
| 1171 | break; |
| 1172 | |
Zhongxing Xu | 6987c7b | 2008-11-30 05:49:49 +0000 | [diff] [blame] | 1173 | SVal Idx = NonLoc::MakeVal(getBasicVals(), i); |
Ted Kremenek | f936f45 | 2009-05-04 06:18:28 +0000 | [diff] [blame] | 1174 | ElementRegion* ER = |
| 1175 | MRMgr.getElementRegion(cast<ArrayType>(T)->getElementType(), |
| 1176 | Idx, R); |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1177 | |
| 1178 | if (CAT->getElementType()->isStructureType()) |
| 1179 | St = BindStruct(St, ER, *VI); |
| 1180 | else |
| 1181 | St = Bind(St, Loc::MakeVal(ER), *VI); |
Zhongxing Xu | 1a12a0e | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 1182 | } |
| 1183 | |
Zhongxing Xu | bd4f745 | 2009-03-09 06:49:50 +0000 | [diff] [blame] | 1184 | return St; |
Zhongxing Xu | 1a12a0e | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 1185 | } |
| 1186 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1187 | const GRState* |
| 1188 | RegionStoreManager::BindStruct(const GRState* St, const TypedRegion* R, SVal V){ |
Zhongxing Xu | a82d8aa | 2009-05-09 03:57:34 +0000 | [diff] [blame] | 1189 | QualType T = R->getValueType(getContext()); |
Zhongxing Xu | af0a844 | 2008-10-31 10:53:01 +0000 | [diff] [blame] | 1190 | assert(T->isStructureType()); |
| 1191 | |
Zhongxing Xu | b13ecdb | 2009-03-12 03:45:35 +0000 | [diff] [blame] | 1192 | const RecordType* RT = T->getAsRecordType(); |
Zhongxing Xu | af0a844 | 2008-10-31 10:53:01 +0000 | [diff] [blame] | 1193 | RecordDecl* RD = RT->getDecl(); |
Zhongxing Xu | c45a825 | 2009-03-11 09:07:35 +0000 | [diff] [blame] | 1194 | |
| 1195 | if (!RD->isDefinition()) |
| 1196 | return St; |
Zhongxing Xu | af0a844 | 2008-10-31 10:53:01 +0000 | [diff] [blame] | 1197 | |
Zhongxing Xu | 5834ed6 | 2009-01-13 01:49:57 +0000 | [diff] [blame] | 1198 | if (V.isUnknown()) |
| 1199 | return KillStruct(St, R); |
| 1200 | |
Zhongxing Xu | 3f6978a | 2009-06-11 09:11:27 +0000 | [diff] [blame] | 1201 | if (isa<nonloc::SymbolVal>(V)) |
| 1202 | return setDefaultValue(St, R, V); |
| 1203 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1204 | nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V); |
Zhongxing Xu | af0a844 | 2008-10-31 10:53:01 +0000 | [diff] [blame] | 1205 | nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1206 | RecordDecl::field_iterator FI = RD->field_begin(getContext()), |
| 1207 | FE = RD->field_end(getContext()); |
Zhongxing Xu | af0a844 | 2008-10-31 10:53:01 +0000 | [diff] [blame] | 1208 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1209 | for (; FI != FE; ++FI, ++VI) { |
| 1210 | |
| 1211 | // There may be fewer values than fields only when we are initializing a |
| 1212 | // struct decl. In this case, mark the region as having default value. |
| 1213 | if (VI == VE) { |
Zhongxing Xu | 1302016 | 2008-12-24 07:29:24 +0000 | [diff] [blame] | 1214 | GRStateRef state(St, StateMgr); |
Ted Kremenek | 14553ab | 2009-01-30 00:08:43 +0000 | [diff] [blame] | 1215 | const NonLoc& Idx = NonLoc::MakeIntVal(getBasicVals(), 0, false); |
| 1216 | St = state.set<RegionDefaultValue>(R, Idx); |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1217 | break; |
| 1218 | } |
| 1219 | |
Zhongxing Xu | af0a844 | 2008-10-31 10:53:01 +0000 | [diff] [blame] | 1220 | QualType FTy = (*FI)->getType(); |
| 1221 | FieldRegion* FR = MRMgr.getFieldRegion(*FI, R); |
| 1222 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1223 | if (Loc::IsLocType(FTy) || FTy->isIntegerType()) |
| 1224 | St = Bind(St, Loc::MakeVal(FR), *VI); |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 1225 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1226 | else if (FTy->isArrayType()) |
| 1227 | St = BindArray(St, FR, *VI); |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 1228 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1229 | else if (FTy->isStructureType()) |
| 1230 | St = BindStruct(St, FR, *VI); |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 1231 | } |
| 1232 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1233 | return St; |
Zhongxing Xu | c3a0599 | 2008-11-19 11:06:24 +0000 | [diff] [blame] | 1234 | } |
| 1235 | |
Zhongxing Xu | 5834ed6 | 2009-01-13 01:49:57 +0000 | [diff] [blame] | 1236 | const GRState* RegionStoreManager::KillStruct(const GRState* St, |
| 1237 | const TypedRegion* R){ |
| 1238 | GRStateRef state(St, StateMgr); |
| 1239 | |
| 1240 | // Kill the struct region because it is assigned "unknown". |
| 1241 | St = state.add<RegionKills>(R); |
| 1242 | |
| 1243 | // Set the default value of the struct region to "unknown". |
| 1244 | St = state.set<RegionDefaultValue>(R, UnknownVal()); |
| 1245 | |
| 1246 | Store store = St->getStore(); |
| 1247 | RegionBindingsTy B = GetRegionBindings(store); |
| 1248 | |
| 1249 | // Remove all bindings for the subregions of the struct. |
| 1250 | for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) { |
| 1251 | const MemRegion* r = I.getKey(); |
| 1252 | if (const SubRegion* sr = dyn_cast<SubRegion>(r)) |
| 1253 | if (sr->isSubRegionOf(R)) |
| 1254 | store = Remove(store, Loc::MakeVal(sr)); |
Zhongxing Xu | 9c2a3db | 2009-01-13 03:07:41 +0000 | [diff] [blame] | 1255 | // FIXME: Maybe we should also remove the bindings for the "views" of the |
| 1256 | // subregions. |
Zhongxing Xu | 5834ed6 | 2009-01-13 01:49:57 +0000 | [diff] [blame] | 1257 | } |
| 1258 | |
| 1259 | return StateMgr.MakeStateWithStore(St, store); |
| 1260 | } |
| 1261 | |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 1262 | const GRState* RegionStoreManager::AddRegionView(const GRState* St, |
| 1263 | const MemRegion* View, |
| 1264 | const MemRegion* Base) { |
| 1265 | GRStateRef state(St, StateMgr); |
| 1266 | |
| 1267 | // First, retrieve the region view of the base region. |
Ted Kremenek | 50dc1b3 | 2008-12-24 01:05:03 +0000 | [diff] [blame] | 1268 | const RegionViews* d = state.get<RegionViewMap>(Base); |
Zhongxing Xu | 5834ed6 | 2009-01-13 01:49:57 +0000 | [diff] [blame] | 1269 | RegionViews L = d ? *d : RVFactory.GetEmptySet(); |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 1270 | |
| 1271 | // Now add View to the region view. |
Zhongxing Xu | 5834ed6 | 2009-01-13 01:49:57 +0000 | [diff] [blame] | 1272 | L = RVFactory.Add(L, View); |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 1273 | |
| 1274 | // Create a new state with the new region view. |
Ted Kremenek | 50dc1b3 | 2008-12-24 01:05:03 +0000 | [diff] [blame] | 1275 | return state.set<RegionViewMap>(Base, L); |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 1276 | } |
Zhongxing Xu | 5834ed6 | 2009-01-13 01:49:57 +0000 | [diff] [blame] | 1277 | |
| 1278 | const GRState* RegionStoreManager::RemoveRegionView(const GRState* St, |
| 1279 | const MemRegion* View, |
| 1280 | const MemRegion* Base) { |
| 1281 | GRStateRef state(St, StateMgr); |
| 1282 | |
| 1283 | // Retrieve the region view of the base region. |
| 1284 | const RegionViews* d = state.get<RegionViewMap>(Base); |
| 1285 | |
| 1286 | // If the base region has no view, return. |
| 1287 | if (!d) |
| 1288 | return St; |
| 1289 | |
| 1290 | // Remove the view. |
| 1291 | RegionViews V = *d; |
| 1292 | V = RVFactory.Remove(V, View); |
| 1293 | |
| 1294 | return state.set<RegionViewMap>(Base, V); |
| 1295 | } |
Zhongxing Xu | 2079494 | 2009-05-06 08:33:50 +0000 | [diff] [blame] | 1296 | |
| 1297 | const GRState* RegionStoreManager::setCastType(const GRState* St, |
| 1298 | const MemRegion* R, QualType T) { |
| 1299 | GRStateRef state(St, StateMgr); |
| 1300 | return state.set<RegionCasts>(R, T); |
| 1301 | } |
Zhongxing Xu | 264e937 | 2009-05-12 10:10:00 +0000 | [diff] [blame] | 1302 | |
| 1303 | const GRState* RegionStoreManager::setDefaultValue(const GRState* St, |
| 1304 | const MemRegion* R, SVal V) { |
| 1305 | GRStateRef state(St, StateMgr); |
| 1306 | return state.set<RegionDefaultValue>(R, V); |
| 1307 | } |