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