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