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