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