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