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