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