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