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