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 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 1309f9a | 2010-01-25 04:41:41 +0000 | [diff] [blame] | 17 | #include "clang/Checker/PathSensitive/MemRegion.h" |
| 18 | #include "clang/Analysis/AnalysisContext.h" |
| 19 | #include "clang/Checker/PathSensitive/GRState.h" |
| 20 | #include "clang/Checker/PathSensitive/GRStateTrait.h" |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 21 | #include "clang/Analysis/Analyses/LiveVariables.h" |
Ted Kremenek | d4e5a60 | 2009-08-06 21:43:54 +0000 | [diff] [blame] | 22 | #include "clang/Analysis/Support/Optional.h" |
Zhongxing Xu | 41fd018 | 2009-05-06 11:51:48 +0000 | [diff] [blame] | 23 | #include "clang/Basic/TargetInfo.h" |
Zhongxing Xu | 3ed04d3 | 2010-01-18 08:54:31 +0000 | [diff] [blame] | 24 | #include "clang/AST/CharUnits.h" |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 25 | |
| 26 | #include "llvm/ADT/ImmutableMap.h" |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/ImmutableList.h" |
Zhongxing Xu | a071eb0 | 2008-10-24 06:01:33 +0000 | [diff] [blame] | 28 | #include "llvm/Support/raw_ostream.h" |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 29 | |
| 30 | using namespace clang; |
| 31 | |
Ted Kremenek | a5e81f1 | 2009-08-06 01:20:57 +0000 | [diff] [blame] | 32 | #define USE_EXPLICIT_COMPOUND 0 |
Ted Kremenek | 356e9d6 | 2009-07-22 04:35:42 +0000 | [diff] [blame] | 33 | |
Ted Kremenek | 1c1ae6b | 2010-01-11 00:07:44 +0000 | [diff] [blame] | 34 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 1c1ae6b | 2010-01-11 00:07:44 +0000 | [diff] [blame] | 35 | // Representation of binding keys. |
| 36 | //===----------------------------------------------------------------------===// |
| 37 | |
| 38 | namespace { |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 39 | class BindingKey { |
Ted Kremenek | 1c1ae6b | 2010-01-11 00:07:44 +0000 | [diff] [blame] | 40 | public: |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 41 | enum Kind { Direct = 0x0, Default = 0x1 }; |
| 42 | private: |
| 43 | llvm ::PointerIntPair<const MemRegion*, 1> P; |
| 44 | uint64_t Offset; |
Ted Kremenek | 1c1ae6b | 2010-01-11 00:07:44 +0000 | [diff] [blame] | 45 | |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 46 | explicit BindingKey(const MemRegion *r, uint64_t offset, Kind k) |
Zhongxing Xu | bfcaf80 | 2010-02-05 02:26:30 +0000 | [diff] [blame] | 47 | : P(r, (unsigned) k), Offset(offset) { assert(r); } |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 48 | public: |
| 49 | |
| 50 | bool isDefault() const { return P.getInt() == Default; } |
| 51 | bool isDirect() const { return P.getInt() == Direct; } |
| 52 | |
| 53 | const MemRegion *getRegion() const { return P.getPointer(); } |
| 54 | uint64_t getOffset() const { return Offset; } |
Ted Kremenek | 1c1ae6b | 2010-01-11 00:07:44 +0000 | [diff] [blame] | 55 | |
| 56 | void Profile(llvm::FoldingSetNodeID& ID) const { |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 57 | ID.AddPointer(P.getOpaqueValue()); |
| 58 | ID.AddInteger(Offset); |
Ted Kremenek | 1c1ae6b | 2010-01-11 00:07:44 +0000 | [diff] [blame] | 59 | } |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 60 | |
| 61 | static BindingKey Make(const MemRegion *R, Kind k); |
| 62 | |
| 63 | bool operator<(const BindingKey &X) const { |
| 64 | if (P.getOpaqueValue() < X.P.getOpaqueValue()) |
| 65 | return true; |
| 66 | if (P.getOpaqueValue() > X.P.getOpaqueValue()) |
| 67 | return false; |
| 68 | return Offset < X.Offset; |
| 69 | } |
| 70 | |
| 71 | bool operator==(const BindingKey &X) const { |
| 72 | return P.getOpaqueValue() == X.P.getOpaqueValue() && |
| 73 | Offset == X.Offset; |
| 74 | } |
Ted Kremenek | 1c1ae6b | 2010-01-11 00:07:44 +0000 | [diff] [blame] | 75 | }; |
| 76 | } // end anonymous namespace |
| 77 | |
| 78 | namespace llvm { |
| 79 | static inline |
| 80 | llvm::raw_ostream& operator<<(llvm::raw_ostream& os, BindingKey K) { |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 81 | os << '(' << K.getRegion() << ',' << K.getOffset() |
| 82 | << ',' << (K.isDirect() ? "direct" : "default") |
| 83 | << ')'; |
Ted Kremenek | 1c1ae6b | 2010-01-11 00:07:44 +0000 | [diff] [blame] | 84 | return os; |
| 85 | } |
| 86 | } // end llvm namespace |
| 87 | |
| 88 | //===----------------------------------------------------------------------===// |
Zhongxing Xu | baf03a7 | 2008-11-24 09:44:56 +0000 | [diff] [blame] | 89 | // Actual Store type. |
Ted Kremenek | 1c1ae6b | 2010-01-11 00:07:44 +0000 | [diff] [blame] | 90 | //===----------------------------------------------------------------------===// |
| 91 | |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 92 | typedef llvm::ImmutableMap<BindingKey, SVal> RegionBindings; |
Zhongxing Xu | baf03a7 | 2008-11-24 09:44:56 +0000 | [diff] [blame] | 93 | |
Ted Kremenek | 50dc1b3 | 2008-12-24 01:05:03 +0000 | [diff] [blame] | 94 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 95 | // Fine-grained control of RegionStoreManager. |
| 96 | //===----------------------------------------------------------------------===// |
| 97 | |
| 98 | namespace { |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 99 | struct minimal_features_tag {}; |
| 100 | struct maximal_features_tag {}; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 101 | |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 102 | class RegionStoreFeatures { |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 103 | bool SupportsFields; |
| 104 | bool SupportsRemaining; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 105 | |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 106 | public: |
| 107 | RegionStoreFeatures(minimal_features_tag) : |
| 108 | SupportsFields(false), SupportsRemaining(false) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 109 | |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 110 | RegionStoreFeatures(maximal_features_tag) : |
| 111 | SupportsFields(true), SupportsRemaining(false) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 112 | |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 113 | void enableFields(bool t) { SupportsFields = t; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 114 | |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 115 | bool supportsFields() const { return SupportsFields; } |
| 116 | bool supportsRemaining() const { return SupportsRemaining; } |
| 117 | }; |
| 118 | } |
| 119 | |
| 120 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 50dc1b3 | 2008-12-24 01:05:03 +0000 | [diff] [blame] | 121 | // Region "Extents" |
| 122 | //===----------------------------------------------------------------------===// |
| 123 | // |
| 124 | // MemRegions represent chunks of memory with a size (their "extent"). This |
| 125 | // GDM entry tracks the extents for regions. Extents are in bytes. |
Ted Kremenek | d6cfbe4 | 2009-01-07 22:18:50 +0000 | [diff] [blame] | 126 | // |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 127 | namespace { class RegionExtents {}; } |
Ted Kremenek | 50dc1b3 | 2008-12-24 01:05:03 +0000 | [diff] [blame] | 128 | static int RegionExtentsIndex = 0; |
Zhongxing Xu | baf03a7 | 2008-11-24 09:44:56 +0000 | [diff] [blame] | 129 | namespace clang { |
Ted Kremenek | 50dc1b3 | 2008-12-24 01:05:03 +0000 | [diff] [blame] | 130 | template<> struct GRStateTrait<RegionExtents> |
| 131 | : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*, SVal> > { |
| 132 | static void* GDMIndex() { return &RegionExtentsIndex; } |
| 133 | }; |
Zhongxing Xu | baf03a7 | 2008-11-24 09:44:56 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Ted Kremenek | 50dc1b3 | 2008-12-24 01:05:03 +0000 | [diff] [blame] | 136 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 137 | // Utility functions. |
| 138 | //===----------------------------------------------------------------------===// |
| 139 | |
| 140 | static bool IsAnyPointerOrIntptr(QualType ty, ASTContext &Ctx) { |
| 141 | if (ty->isAnyPointerType()) |
| 142 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 143 | |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 144 | return ty->isIntegerType() && ty->isScalarType() && |
| 145 | Ctx.getTypeSize(ty) == Ctx.getTypeSize(Ctx.VoidPtrTy); |
| 146 | } |
| 147 | |
| 148 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 50dc1b3 | 2008-12-24 01:05:03 +0000 | [diff] [blame] | 149 | // Main RegionStore logic. |
| 150 | //===----------------------------------------------------------------------===// |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 151 | |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 152 | namespace { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 153 | |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 154 | class RegionStoreSubRegionMap : public SubRegionMap { |
Ted Kremenek | df16501 | 2010-02-02 22:38:47 +0000 | [diff] [blame] | 155 | public: |
| 156 | typedef llvm::ImmutableSet<const MemRegion*> Set; |
| 157 | typedef llvm::DenseMap<const MemRegion*, Set> Map; |
| 158 | private: |
| 159 | Set::Factory F; |
Ted Kremenek | 59e8f11 | 2009-03-03 01:35:36 +0000 | [diff] [blame] | 160 | Map M; |
Ted Kremenek | 59e8f11 | 2009-03-03 01:35:36 +0000 | [diff] [blame] | 161 | public: |
Ted Kremenek | d8c0192 | 2009-08-05 19:09:24 +0000 | [diff] [blame] | 162 | bool add(const MemRegion* Parent, const MemRegion* SubRegion) { |
Ted Kremenek | 59e8f11 | 2009-03-03 01:35:36 +0000 | [diff] [blame] | 163 | Map::iterator I = M.find(Parent); |
Ted Kremenek | d8c0192 | 2009-08-05 19:09:24 +0000 | [diff] [blame] | 164 | |
| 165 | if (I == M.end()) { |
Ted Kremenek | 4ed4598 | 2009-08-05 05:31:02 +0000 | [diff] [blame] | 166 | M.insert(std::make_pair(Parent, F.Add(F.GetEmptySet(), SubRegion))); |
Ted Kremenek | d8c0192 | 2009-08-05 19:09:24 +0000 | [diff] [blame] | 167 | return true; |
| 168 | } |
| 169 | |
| 170 | I->second = F.Add(I->second, SubRegion); |
| 171 | return false; |
Ted Kremenek | 59e8f11 | 2009-03-03 01:35:36 +0000 | [diff] [blame] | 172 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 173 | |
Ted Kremenek | a5e81f1 | 2009-08-06 01:20:57 +0000 | [diff] [blame] | 174 | void process(llvm::SmallVectorImpl<const SubRegion*> &WL, const SubRegion *R); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 175 | |
Ted Kremenek | 59e8f11 | 2009-03-03 01:35:36 +0000 | [diff] [blame] | 176 | ~RegionStoreSubRegionMap() {} |
Ted Kremenek | df16501 | 2010-02-02 22:38:47 +0000 | [diff] [blame] | 177 | |
| 178 | const Set *getSubRegions(const MemRegion *Parent) const { |
| 179 | Map::const_iterator I = M.find(Parent); |
| 180 | return I == M.end() ? NULL : &I->second; |
| 181 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 182 | |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 183 | bool iterSubRegions(const MemRegion* Parent, Visitor& V) const { |
Jeffrey Yasskin | 3958b50 | 2009-11-10 01:17:45 +0000 | [diff] [blame] | 184 | Map::const_iterator I = M.find(Parent); |
Ted Kremenek | 59e8f11 | 2009-03-03 01:35:36 +0000 | [diff] [blame] | 185 | |
| 186 | if (I == M.end()) |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 187 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 188 | |
Ted Kremenek | df16501 | 2010-02-02 22:38:47 +0000 | [diff] [blame] | 189 | Set S = I->second; |
| 190 | for (Set::iterator SI=S.begin(),SE=S.end(); SI != SE; ++SI) { |
Ted Kremenek | 59e8f11 | 2009-03-03 01:35:36 +0000 | [diff] [blame] | 191 | if (!V.Visit(Parent, *SI)) |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 192 | return false; |
Ted Kremenek | 59e8f11 | 2009-03-03 01:35:36 +0000 | [diff] [blame] | 193 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 194 | |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 195 | return true; |
Ted Kremenek | 59e8f11 | 2009-03-03 01:35:36 +0000 | [diff] [blame] | 196 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 197 | }; |
Ted Kremenek | 59e8f11 | 2009-03-03 01:35:36 +0000 | [diff] [blame] | 198 | |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 199 | |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 200 | class RegionStoreManager : public StoreManager { |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 201 | const RegionStoreFeatures Features; |
Ted Kremenek | 451ac09 | 2009-08-06 04:50:20 +0000 | [diff] [blame] | 202 | RegionBindings::Factory RBFactory; |
Ted Kremenek | 9e17cc6 | 2009-09-29 06:35:00 +0000 | [diff] [blame] | 203 | |
Zhongxing Xu | bfcaf80 | 2010-02-05 02:26:30 +0000 | [diff] [blame] | 204 | typedef llvm::DenseMap<Store, RegionStoreSubRegionMap*> SMCache; |
Ted Kremenek | 9e17cc6 | 2009-09-29 06:35:00 +0000 | [diff] [blame] | 205 | SMCache SC; |
| 206 | |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 207 | public: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 208 | RegionStoreManager(GRStateManager& mgr, const RegionStoreFeatures &f) |
Ted Kremenek | f7a0cf4 | 2009-07-29 21:43:22 +0000 | [diff] [blame] | 209 | : StoreManager(mgr), |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 210 | Features(f), |
Ted Kremenek | 8928d41 | 2010-02-04 04:14:49 +0000 | [diff] [blame] | 211 | RBFactory(mgr.getAllocator()) {} |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 212 | |
Ted Kremenek | 9e17cc6 | 2009-09-29 06:35:00 +0000 | [diff] [blame] | 213 | virtual ~RegionStoreManager() { |
| 214 | for (SMCache::iterator I = SC.begin(), E = SC.end(); I != E; ++I) |
| 215 | delete (*I).second; |
| 216 | } |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 217 | |
Zhongxing Xu | f5416bd | 2010-02-05 05:18:47 +0000 | [diff] [blame] | 218 | SubRegionMap *getSubRegionMap(Store store) { |
| 219 | return getRegionStoreSubRegionMap(store); |
| 220 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 221 | |
Zhongxing Xu | 13d5017 | 2009-10-11 08:08:02 +0000 | [diff] [blame] | 222 | RegionStoreSubRegionMap *getRegionStoreSubRegionMap(Store store); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 223 | |
Zhongxing Xu | 13d5017 | 2009-10-11 08:08:02 +0000 | [diff] [blame] | 224 | Optional<SVal> getBinding(RegionBindings B, const MemRegion *R); |
| 225 | Optional<SVal> getDirectBinding(RegionBindings B, const MemRegion *R); |
Ted Kremenek | d4e5a60 | 2009-08-06 21:43:54 +0000 | [diff] [blame] | 226 | /// getDefaultBinding - Returns an SVal* representing an optional default |
| 227 | /// binding associated with a region and its subregions. |
Zhongxing Xu | 13d5017 | 2009-10-11 08:08:02 +0000 | [diff] [blame] | 228 | Optional<SVal> getDefaultBinding(RegionBindings B, const MemRegion *R); |
Ted Kremenek | 027e266 | 2009-11-19 20:20:24 +0000 | [diff] [blame] | 229 | |
| 230 | /// setImplicitDefaultValue - Set the default binding for the provided |
| 231 | /// MemRegion to the value implicitly defined for compound literals when |
| 232 | /// the value is not specified. |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 233 | Store setImplicitDefaultValue(Store store, const MemRegion *R, QualType T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 234 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 235 | /// ArrayToPointer - Emulates the "decay" of an array to a pointer |
| 236 | /// type. 'Array' represents the lvalue of the array being decayed |
| 237 | /// to a pointer, and the returned SVal represents the decayed |
| 238 | /// version of that lvalue (i.e., a pointer to the first element of |
| 239 | /// the array). This is called by GRExprEngine when evaluating |
| 240 | /// casts from arrays to pointers. |
Zhongxing Xu | f1d537f | 2009-03-30 05:55:46 +0000 | [diff] [blame] | 241 | SVal ArrayToPointer(Loc Array); |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 242 | |
Zhongxing Xu | 461147f | 2010-02-05 05:24:20 +0000 | [diff] [blame] | 243 | SVal EvalBinOp(BinaryOperator::Opcode Op,Loc L, NonLoc R, QualType resultTy); |
Zhongxing Xu | 24194ef | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 244 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 245 | Store getInitialStore(const LocationContext *InitLoc) { |
Ted Kremenek | 82cd37c | 2009-08-21 23:25:54 +0000 | [diff] [blame] | 246 | return RBFactory.GetEmptyMap().getRoot(); |
Zhongxing Xu | 17fd863 | 2009-08-17 06:19:58 +0000 | [diff] [blame] | 247 | } |
Ted Kremenek | 82cd37c | 2009-08-21 23:25:54 +0000 | [diff] [blame] | 248 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 249 | //===-------------------------------------------------------------------===// |
| 250 | // Binding values to regions. |
| 251 | //===-------------------------------------------------------------------===// |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 252 | |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 253 | Store InvalidateRegion(Store store, const MemRegion *R, const Expr *E, |
| 254 | unsigned Count, InvalidatedSymbols *IS) { |
| 255 | return RegionStoreManager::InvalidateRegions(store, &R, &R+1, E, Count, IS); |
Ted Kremenek | 81a9583 | 2009-12-03 03:27:11 +0000 | [diff] [blame] | 256 | } |
| 257 | |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 258 | Store InvalidateRegions(Store store, |
| 259 | const MemRegion * const *Begin, |
| 260 | const MemRegion * const *End, |
| 261 | const Expr *E, unsigned Count, |
| 262 | InvalidatedSymbols *IS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 263 | |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 264 | public: // Made public for helper classes. |
| 265 | |
Zhongxing Xu | 13d5017 | 2009-10-11 08:08:02 +0000 | [diff] [blame] | 266 | void RemoveSubRegionBindings(RegionBindings &B, const MemRegion *R, |
Ted Kremenek | a5e81f1 | 2009-08-06 01:20:57 +0000 | [diff] [blame] | 267 | RegionStoreSubRegionMap &M); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 268 | |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 269 | RegionBindings Add(RegionBindings B, BindingKey K, SVal V); |
| 270 | |
| 271 | RegionBindings Add(RegionBindings B, const MemRegion *R, |
| 272 | BindingKey::Kind k, SVal V); |
Ted Kremenek | 1c1ae6b | 2010-01-11 00:07:44 +0000 | [diff] [blame] | 273 | |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 274 | const SVal *Lookup(RegionBindings B, BindingKey K); |
| 275 | const SVal *Lookup(RegionBindings B, const MemRegion *R, BindingKey::Kind k); |
Ted Kremenek | 1c1ae6b | 2010-01-11 00:07:44 +0000 | [diff] [blame] | 276 | |
| 277 | RegionBindings Remove(RegionBindings B, BindingKey K); |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 278 | RegionBindings Remove(RegionBindings B, const MemRegion *R, |
| 279 | BindingKey::Kind k); |
| 280 | |
| 281 | RegionBindings Remove(RegionBindings B, const MemRegion *R) { |
| 282 | return Remove(Remove(B, R, BindingKey::Direct), R, BindingKey::Default); |
| 283 | } |
| 284 | |
Ted Kremenek | 1c1ae6b | 2010-01-11 00:07:44 +0000 | [diff] [blame] | 285 | Store Remove(Store store, BindingKey K); |
| 286 | |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 287 | public: // Part of public interface to class. |
| 288 | |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 289 | Store Bind(Store store, Loc LV, SVal V); |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 290 | |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 291 | Store BindCompoundLiteral(Store store, const CompoundLiteralExpr* CL, |
| 292 | const LocationContext *LC, SVal V); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 293 | |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 294 | Store BindDecl(Store store, const VarRegion *VR, SVal InitVal); |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 295 | |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 296 | Store BindDeclWithNoInit(Store store, const VarRegion *) { |
| 297 | return store; |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 298 | } |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 299 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 300 | /// BindStruct - Bind a compound value to a structure. |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 301 | Store BindStruct(Store store, const TypedRegion* R, SVal V); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 302 | |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 303 | Store BindArray(Store store, const TypedRegion* R, SVal V); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 304 | |
| 305 | /// KillStruct - Set the entire struct to unknown. |
Zhongxing Xu | 13d5017 | 2009-10-11 08:08:02 +0000 | [diff] [blame] | 306 | Store KillStruct(Store store, const TypedRegion* R); |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 307 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 308 | Store Remove(Store store, Loc LV); |
Ted Kremenek | 1c1ae6b | 2010-01-11 00:07:44 +0000 | [diff] [blame] | 309 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 310 | |
| 311 | //===------------------------------------------------------------------===// |
| 312 | // Loading values from regions. |
| 313 | //===------------------------------------------------------------------===// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 314 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 315 | /// The high level logic for this method is this: |
| 316 | /// Retrieve (L) |
| 317 | /// if L has binding |
| 318 | /// return L's binding |
| 319 | /// else if L is in killset |
| 320 | /// return unknown |
| 321 | /// else |
| 322 | /// if L is on stack or heap |
| 323 | /// return undefined |
| 324 | /// else |
| 325 | /// return symbolic |
Zhongxing Xu | 576bb92 | 2010-02-05 03:01:53 +0000 | [diff] [blame] | 326 | SVal Retrieve(Store store, Loc L, QualType T = QualType()); |
Zhongxing Xu | 490b0f0 | 2009-06-25 04:50:44 +0000 | [diff] [blame] | 327 | |
Zhongxing Xu | bfcaf80 | 2010-02-05 02:26:30 +0000 | [diff] [blame] | 328 | SVal RetrieveElement(Store store, const ElementRegion *R); |
Zhongxing Xu | c00346f | 2009-06-25 05:29:39 +0000 | [diff] [blame] | 329 | |
Zhongxing Xu | bfcaf80 | 2010-02-05 02:26:30 +0000 | [diff] [blame] | 330 | SVal RetrieveField(Store store, const FieldRegion *R); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 331 | |
Zhongxing Xu | 576bb92 | 2010-02-05 03:01:53 +0000 | [diff] [blame] | 332 | SVal RetrieveObjCIvar(Store store, const ObjCIvarRegion *R); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 333 | |
Zhongxing Xu | 576bb92 | 2010-02-05 03:01:53 +0000 | [diff] [blame] | 334 | SVal RetrieveVar(Store store, const VarRegion *R); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 335 | |
Zhongxing Xu | 576bb92 | 2010-02-05 03:01:53 +0000 | [diff] [blame] | 336 | SVal RetrieveLazySymbol(const TypedRegion *R); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 337 | |
Zhongxing Xu | bfcaf80 | 2010-02-05 02:26:30 +0000 | [diff] [blame] | 338 | SVal RetrieveFieldOrElementCommon(Store store, const TypedRegion *R, |
Ted Kremenek | 566a6fa | 2009-08-06 22:33:36 +0000 | [diff] [blame] | 339 | QualType Ty, const MemRegion *superR); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 340 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 341 | /// Retrieve the values in a struct and return a CompoundVal, used when doing |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 342 | /// struct copy: |
| 343 | /// struct s x, y; |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 344 | /// x = y; |
| 345 | /// y's value is retrieved by this method. |
Zhongxing Xu | 576bb92 | 2010-02-05 03:01:53 +0000 | [diff] [blame] | 346 | SVal RetrieveStruct(Store store, const TypedRegion* R); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 347 | |
Zhongxing Xu | 576bb92 | 2010-02-05 03:01:53 +0000 | [diff] [blame] | 348 | SVal RetrieveArray(Store store, const TypedRegion* R); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 349 | |
Zhongxing Xu | 944ebc6 | 2009-12-21 06:52:24 +0000 | [diff] [blame] | 350 | /// Get the state and region whose binding this region R corresponds to. |
Zhongxing Xu | bfcaf80 | 2010-02-05 02:26:30 +0000 | [diff] [blame] | 351 | std::pair<Store, const MemRegion*> |
Ted Kremenek | 451ac09 | 2009-08-06 04:50:20 +0000 | [diff] [blame] | 352 | GetLazyBinding(RegionBindings B, const MemRegion *R); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 353 | |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 354 | Store CopyLazyBindings(nonloc::LazyCompoundVal V, Store store, |
| 355 | const TypedRegion *R); |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 356 | |
Zhongxing Xu | 8149185 | 2010-02-08 08:43:02 +0000 | [diff] [blame] | 357 | const ElementRegion *GetElementZeroRegion(const MemRegion *R, QualType T); |
Ted Kremenek | 0954cde | 2009-09-24 04:11:44 +0000 | [diff] [blame] | 358 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 359 | //===------------------------------------------------------------------===// |
| 360 | // State pruning. |
| 361 | //===------------------------------------------------------------------===// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 362 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 363 | /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values. |
| 364 | /// It returns a new Store with these values removed. |
Zhongxing Xu | 72119c4 | 2010-02-05 05:34:29 +0000 | [diff] [blame] | 365 | Store RemoveDeadBindings(Store store, Stmt* Loc, SymbolReaper& SymReaper, |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 366 | llvm::SmallVectorImpl<const MemRegion*>& RegionRoots); |
| 367 | |
Zhongxing Xu | 4e3c1f7 | 2009-10-13 02:24:55 +0000 | [diff] [blame] | 368 | const GRState *EnterStackFrame(const GRState *state, |
| 369 | const StackFrameContext *frame); |
| 370 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 371 | //===------------------------------------------------------------------===// |
| 372 | // Region "extents". |
| 373 | //===------------------------------------------------------------------===// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 374 | |
Zhongxing Xu | 3ed04d3 | 2010-01-18 08:54:31 +0000 | [diff] [blame] | 375 | const GRState *setExtent(const GRState *state,const MemRegion* R,SVal Extent); |
Zhongxing Xu | e884ff8 | 2009-11-12 02:48:32 +0000 | [diff] [blame] | 376 | DefinedOrUnknownSVal getSizeInElements(const GRState *state, |
Zhongxing Xu | 3ed04d3 | 2010-01-18 08:54:31 +0000 | [diff] [blame] | 377 | const MemRegion* R, QualType EleTy); |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 378 | |
| 379 | //===------------------------------------------------------------------===// |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 380 | // Utility methods. |
| 381 | //===------------------------------------------------------------------===// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 382 | |
Ted Kremenek | 451ac09 | 2009-08-06 04:50:20 +0000 | [diff] [blame] | 383 | static inline RegionBindings GetRegionBindings(Store store) { |
Zhongxing Xu | 13d5017 | 2009-10-11 08:08:02 +0000 | [diff] [blame] | 384 | return RegionBindings(static_cast<const RegionBindings::TreeTy*>(store)); |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 385 | } |
Zhongxing Xu | 24194ef | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 386 | |
Ted Kremenek | 53ba0b6 | 2009-06-24 23:06:47 +0000 | [diff] [blame] | 387 | void print(Store store, llvm::raw_ostream& Out, const char* nl, |
| 388 | const char *sep); |
Zhongxing Xu | 24194ef | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 389 | |
| 390 | void iterBindings(Store store, BindingsHandler& f) { |
| 391 | // FIXME: Implement. |
| 392 | } |
Zhongxing Xu | 6e3f01c | 2008-10-31 07:16:08 +0000 | [diff] [blame] | 393 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 394 | // FIXME: Remove. |
| 395 | BasicValueFactory& getBasicVals() { |
| 396 | return StateMgr.getBasicVals(); |
| 397 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 398 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 399 | // FIXME: Remove. |
Zhongxing Xu | 6e3f01c | 2008-10-31 07:16:08 +0000 | [diff] [blame] | 400 | ASTContext& getContext() { return StateMgr.getContext(); } |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 401 | }; |
| 402 | |
| 403 | } // end anonymous namespace |
| 404 | |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 405 | //===----------------------------------------------------------------------===// |
| 406 | // RegionStore creation. |
| 407 | //===----------------------------------------------------------------------===// |
| 408 | |
| 409 | StoreManager *clang::CreateRegionStoreManager(GRStateManager& StMgr) { |
| 410 | RegionStoreFeatures F = maximal_features_tag(); |
| 411 | return new RegionStoreManager(StMgr, F); |
| 412 | } |
| 413 | |
| 414 | StoreManager *clang::CreateFieldsOnlyRegionStoreManager(GRStateManager &StMgr) { |
| 415 | RegionStoreFeatures F = minimal_features_tag(); |
| 416 | F.enableFields(true); |
| 417 | return new RegionStoreManager(StMgr, F); |
Ted Kremenek | 95c7b00 | 2008-10-24 01:04:59 +0000 | [diff] [blame] | 418 | } |
| 419 | |
Ted Kremenek | a5e81f1 | 2009-08-06 01:20:57 +0000 | [diff] [blame] | 420 | void |
| 421 | RegionStoreSubRegionMap::process(llvm::SmallVectorImpl<const SubRegion*> &WL, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 422 | const SubRegion *R) { |
Ted Kremenek | a5e81f1 | 2009-08-06 01:20:57 +0000 | [diff] [blame] | 423 | const MemRegion *superR = R->getSuperRegion(); |
| 424 | if (add(superR, R)) |
| 425 | if (const SubRegion *sr = dyn_cast<SubRegion>(superR)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 426 | WL.push_back(sr); |
Ted Kremenek | a5e81f1 | 2009-08-06 01:20:57 +0000 | [diff] [blame] | 427 | } |
| 428 | |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 429 | RegionStoreSubRegionMap* |
Zhongxing Xu | 13d5017 | 2009-10-11 08:08:02 +0000 | [diff] [blame] | 430 | RegionStoreManager::getRegionStoreSubRegionMap(Store store) { |
| 431 | RegionBindings B = GetRegionBindings(store); |
Ted Kremenek | 59e8f11 | 2009-03-03 01:35:36 +0000 | [diff] [blame] | 432 | RegionStoreSubRegionMap *M = new RegionStoreSubRegionMap(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 433 | |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 434 | llvm::SmallVector<const SubRegion*, 10> WL; |
| 435 | |
Ted Kremenek | 451ac09 | 2009-08-06 04:50:20 +0000 | [diff] [blame] | 436 | for (RegionBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I) |
Ted Kremenek | 1c1ae6b | 2010-01-11 00:07:44 +0000 | [diff] [blame] | 437 | if (const SubRegion *R = dyn_cast<SubRegion>(I.getKey().getRegion())) |
Ted Kremenek | a5e81f1 | 2009-08-06 01:20:57 +0000 | [diff] [blame] | 438 | M->process(WL, R); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 439 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 440 | // We also need to record in the subregion map "intermediate" regions that |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 441 | // don't have direct bindings but are super regions of those that do. |
| 442 | while (!WL.empty()) { |
| 443 | const SubRegion *R = WL.back(); |
| 444 | WL.pop_back(); |
Ted Kremenek | a5e81f1 | 2009-08-06 01:20:57 +0000 | [diff] [blame] | 445 | M->process(WL, R); |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 446 | } |
| 447 | |
Ted Kremenek | 14453bf | 2009-03-03 19:02:42 +0000 | [diff] [blame] | 448 | return M; |
Ted Kremenek | 59e8f11 | 2009-03-03 01:35:36 +0000 | [diff] [blame] | 449 | } |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 450 | |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 451 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 1004a9f | 2009-07-29 18:16:25 +0000 | [diff] [blame] | 452 | // Binding invalidation. |
| 453 | //===----------------------------------------------------------------------===// |
| 454 | |
Zhongxing Xu | 13d5017 | 2009-10-11 08:08:02 +0000 | [diff] [blame] | 455 | void RegionStoreManager::RemoveSubRegionBindings(RegionBindings &B, |
| 456 | const MemRegion *R, |
| 457 | RegionStoreSubRegionMap &M) { |
Ted Kremenek | df16501 | 2010-02-02 22:38:47 +0000 | [diff] [blame] | 458 | |
| 459 | if (const RegionStoreSubRegionMap::Set *S = M.getSubRegions(R)) |
| 460 | for (RegionStoreSubRegionMap::Set::iterator I = S->begin(), E = S->end(); |
| 461 | I != E; ++I) |
| 462 | RemoveSubRegionBindings(B, *I, M); |
Ted Kremenek | 1c1ae6b | 2010-01-11 00:07:44 +0000 | [diff] [blame] | 463 | |
| 464 | B = Remove(B, R); |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 465 | } |
| 466 | |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 467 | namespace { |
| 468 | class InvalidateRegionsWorker { |
Ted Kremenek | 5b29065 | 2010-02-03 04:16:00 +0000 | [diff] [blame] | 469 | typedef BumpVector<BindingKey> RegionCluster; |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 470 | typedef llvm::DenseMap<const MemRegion *, RegionCluster *> ClusterMap; |
Ted Kremenek | 5b29065 | 2010-02-03 04:16:00 +0000 | [diff] [blame] | 471 | typedef llvm::SmallVector<std::pair<const MemRegion *,RegionCluster*>, 10> |
| 472 | WorkList; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 473 | |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 474 | BumpVectorContext BVC; |
| 475 | ClusterMap ClusterM; |
| 476 | WorkList WL; |
| 477 | public: |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 478 | Store InvalidateRegions(RegionStoreManager &RM, Store store, |
| 479 | const MemRegion * const *I,const MemRegion * const *E, |
| 480 | const Expr *Ex, unsigned Count, |
| 481 | StoreManager::InvalidatedSymbols *IS, |
| 482 | ASTContext &Ctx, ValueManager &ValMgr); |
Ted Kremenek | 8780679 | 2009-09-27 20:45:21 +0000 | [diff] [blame] | 483 | |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 484 | private: |
Ted Kremenek | 5b29065 | 2010-02-03 04:16:00 +0000 | [diff] [blame] | 485 | void AddToWorkList(BindingKey K); |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 486 | void AddToWorkList(const MemRegion *R); |
Ted Kremenek | 5b29065 | 2010-02-03 04:16:00 +0000 | [diff] [blame] | 487 | void AddToCluster(BindingKey K); |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 488 | RegionCluster **getCluster(const MemRegion *R); |
| 489 | }; |
| 490 | } |
Zhongxing Xu | 13d5017 | 2009-10-11 08:08:02 +0000 | [diff] [blame] | 491 | |
Ted Kremenek | 5b29065 | 2010-02-03 04:16:00 +0000 | [diff] [blame] | 492 | void InvalidateRegionsWorker::AddToCluster(BindingKey K) { |
| 493 | const MemRegion *R = K.getRegion(); |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 494 | const MemRegion *baseR = R->getBaseRegion(); |
| 495 | RegionCluster **CPtr = getCluster(baseR); |
Ted Kremenek | 5b29065 | 2010-02-03 04:16:00 +0000 | [diff] [blame] | 496 | assert(*CPtr); |
| 497 | (*CPtr)->push_back(K, BVC); |
| 498 | } |
| 499 | |
| 500 | void InvalidateRegionsWorker::AddToWorkList(BindingKey K) { |
| 501 | AddToWorkList(K.getRegion()); |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | void InvalidateRegionsWorker::AddToWorkList(const MemRegion *R) { |
Ted Kremenek | 5b29065 | 2010-02-03 04:16:00 +0000 | [diff] [blame] | 505 | const MemRegion *baseR = R->getBaseRegion(); |
| 506 | RegionCluster **CPtr = getCluster(baseR); |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 507 | if (RegionCluster *C = *CPtr) { |
Ted Kremenek | 5b29065 | 2010-02-03 04:16:00 +0000 | [diff] [blame] | 508 | WL.push_back(std::make_pair(baseR, C)); |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 509 | *CPtr = NULL; |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | InvalidateRegionsWorker::RegionCluster ** |
| 514 | InvalidateRegionsWorker::getCluster(const MemRegion *R) { |
| 515 | RegionCluster *&CRef = ClusterM[R]; |
| 516 | if (!CRef) { |
| 517 | void *Mem = BVC.getAllocator().Allocate<RegionCluster>(); |
| 518 | CRef = new (Mem) RegionCluster(BVC, 10); |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 519 | } |
| 520 | return &CRef; |
| 521 | } |
| 522 | |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 523 | Store InvalidateRegionsWorker::InvalidateRegions(RegionStoreManager &RM, |
| 524 | Store store, |
| 525 | const MemRegion * const *I, |
| 526 | const MemRegion * const *E, |
| 527 | const Expr *Ex, unsigned Count, |
| 528 | StoreManager::InvalidatedSymbols *IS, |
| 529 | ASTContext &Ctx, |
| 530 | ValueManager &ValMgr) { |
| 531 | RegionBindings B = RegionStoreManager::GetRegionBindings(store); |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 532 | |
| 533 | // Scan the entire store and make the region clusters. |
| 534 | for (RegionBindings::iterator RI = B.begin(), RE = B.end(); RI != RE; ++RI) { |
Ted Kremenek | 5b29065 | 2010-02-03 04:16:00 +0000 | [diff] [blame] | 535 | AddToCluster(RI.getKey()); |
| 536 | if (const MemRegion *R = RI.getData().getAsRegion()) { |
| 537 | // Generate a cluster, but don't add the region to the cluster |
| 538 | // if there aren't any bindings. |
| 539 | getCluster(R->getBaseRegion()); |
| 540 | } |
Ted Kremenek | 81a9583 | 2009-12-03 03:27:11 +0000 | [diff] [blame] | 541 | } |
Ted Kremenek | 8780679 | 2009-09-27 20:45:21 +0000 | [diff] [blame] | 542 | |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 543 | // Add the cluster for I .. E to a worklist. |
| 544 | for ( ; I != E; ++I) |
| 545 | AddToWorkList(*I); |
| 546 | |
| 547 | while (!WL.empty()) { |
Ted Kremenek | 5b29065 | 2010-02-03 04:16:00 +0000 | [diff] [blame] | 548 | const MemRegion *baseR; |
| 549 | RegionCluster *C; |
| 550 | llvm::tie(baseR, C) = WL.back(); |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 551 | WL.pop_back(); |
Ted Kremenek | 8780679 | 2009-09-27 20:45:21 +0000 | [diff] [blame] | 552 | |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 553 | for (RegionCluster::iterator I = C->begin(), E = C->end(); I != E; ++I) { |
Ted Kremenek | 5b29065 | 2010-02-03 04:16:00 +0000 | [diff] [blame] | 554 | BindingKey K = *I; |
Ted Kremenek | 473e167 | 2009-10-16 00:30:49 +0000 | [diff] [blame] | 555 | |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 556 | // Get the old binding. Is it a region? If so, add it to the worklist. |
Ted Kremenek | 5b29065 | 2010-02-03 04:16:00 +0000 | [diff] [blame] | 557 | if (const SVal *V = RM.Lookup(B, K)) { |
| 558 | if (const MemRegion *R = V->getAsRegion()) |
| 559 | AddToWorkList(R); |
Ted Kremenek | 2ffbfd9 | 2009-12-03 08:25:47 +0000 | [diff] [blame] | 560 | |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 561 | // A symbol? Mark it touched by the invalidation. |
| 562 | if (IS) |
| 563 | if (SymbolRef Sym = V->getAsSymbol()) |
| 564 | IS->insert(Sym); |
Ted Kremenek | 2ffbfd9 | 2009-12-03 08:25:47 +0000 | [diff] [blame] | 565 | } |
Ted Kremenek | a5971b3 | 2009-09-29 03:34:03 +0000 | [diff] [blame] | 566 | |
Ted Kremenek | 5b29065 | 2010-02-03 04:16:00 +0000 | [diff] [blame] | 567 | B = RM.Remove(B, K); |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 568 | } |
Ted Kremenek | 5b29065 | 2010-02-03 04:16:00 +0000 | [diff] [blame] | 569 | |
| 570 | // Now inspect the base region. |
| 571 | |
| 572 | if (IS) { |
| 573 | // Symbolic region? Mark that symbol touched by the invalidation. |
| 574 | if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(baseR)) |
| 575 | IS->insert(SR->getSymbol()); |
| 576 | } |
| 577 | |
| 578 | // BlockDataRegion? If so, invalidate captured variables that are passed |
| 579 | // by reference. |
| 580 | if (const BlockDataRegion *BR = dyn_cast<BlockDataRegion>(baseR)) { |
| 581 | for (BlockDataRegion::referenced_vars_iterator |
Ted Kremenek | 8524873 | 2010-02-06 00:30:00 +0000 | [diff] [blame] | 582 | BI = BR->referenced_vars_begin(), BE = BR->referenced_vars_end() ; |
| 583 | BI != BE; ++BI) { |
| 584 | const VarRegion *VR = *BI; |
| 585 | const VarDecl *VD = VR->getDecl(); |
| 586 | if (VD->getAttr<BlocksAttr>() || !VD->hasLocalStorage()) |
Ted Kremenek | 5b29065 | 2010-02-03 04:16:00 +0000 | [diff] [blame] | 587 | AddToWorkList(VR); |
| 588 | } |
| 589 | continue; |
| 590 | } |
| 591 | |
| 592 | if (isa<AllocaRegion>(baseR) || isa<SymbolicRegion>(baseR)) { |
| 593 | // Invalidate the region by setting its default value to |
| 594 | // conjured symbol. The type of the symbol is irrelavant. |
| 595 | DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(baseR, Ex, Ctx.IntTy, |
| 596 | Count); |
| 597 | B = RM.Add(B, baseR, BindingKey::Default, V); |
| 598 | continue; |
| 599 | } |
| 600 | |
| 601 | if (!baseR->isBoundable()) |
| 602 | continue; |
| 603 | |
| 604 | const TypedRegion *TR = cast<TypedRegion>(baseR); |
| 605 | QualType T = TR->getValueType(Ctx); |
| 606 | |
| 607 | // Invalidate the binding. |
| 608 | if (const RecordType *RT = T->getAsStructureType()) { |
| 609 | const RecordDecl *RD = RT->getDecl()->getDefinition(Ctx); |
| 610 | // No record definition. There is nothing we can do. |
| 611 | if (!RD) { |
| 612 | B = RM.Remove(B, baseR); |
| 613 | continue; |
| 614 | } |
| 615 | |
| 616 | // Invalidate the region by setting its default value to |
| 617 | // conjured symbol. The type of the symbol is irrelavant. |
| 618 | DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(baseR, Ex, Ctx.IntTy, |
| 619 | Count); |
| 620 | B = RM.Add(B, baseR, BindingKey::Default, V); |
| 621 | continue; |
| 622 | } |
| 623 | |
| 624 | if (const ArrayType *AT = Ctx.getAsArrayType(T)) { |
| 625 | // Set the default value of the array to conjured symbol. |
| 626 | DefinedOrUnknownSVal V = |
| 627 | ValMgr.getConjuredSymbolVal(baseR, Ex, AT->getElementType(), Count); |
| 628 | B = RM.Add(B, baseR, BindingKey::Default, V); |
| 629 | continue; |
| 630 | } |
| 631 | |
| 632 | DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(baseR, Ex, T, Count); |
| 633 | assert(SymbolManager::canSymbolicate(T) || V.isUnknown()); |
| 634 | B = RM.Add(B, baseR, BindingKey::Direct, V); |
Ted Kremenek | a5e81f1 | 2009-08-06 01:20:57 +0000 | [diff] [blame] | 635 | } |
| 636 | |
Ted Kremenek | 8780679 | 2009-09-27 20:45:21 +0000 | [diff] [blame] | 637 | // Create a new state with the updated bindings. |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 638 | return B.getRoot(); |
Ted Kremenek | 1004a9f | 2009-07-29 18:16:25 +0000 | [diff] [blame] | 639 | } |
| 640 | |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 641 | Store RegionStoreManager::InvalidateRegions(Store store, |
| 642 | const MemRegion * const *I, |
| 643 | const MemRegion * const *E, |
| 644 | const Expr *Ex, unsigned Count, |
| 645 | InvalidatedSymbols *IS) { |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 646 | InvalidateRegionsWorker W; |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 647 | return W.InvalidateRegions(*this, store, I, E, Ex, Count, IS, getContext(), |
| 648 | StateMgr.getValueManager()); |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 649 | } |
| 650 | |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 651 | //===----------------------------------------------------------------------===// |
| 652 | // Extents for regions. |
| 653 | //===----------------------------------------------------------------------===// |
| 654 | |
Zhongxing Xu | e884ff8 | 2009-11-12 02:48:32 +0000 | [diff] [blame] | 655 | DefinedOrUnknownSVal RegionStoreManager::getSizeInElements(const GRState *state, |
Zhongxing Xu | 3ed04d3 | 2010-01-18 08:54:31 +0000 | [diff] [blame] | 656 | const MemRegion *R, |
| 657 | QualType EleTy) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 658 | |
Ted Kremenek | 7ecbfbc | 2009-07-10 22:30:06 +0000 | [diff] [blame] | 659 | switch (R->getKind()) { |
Ted Kremenek | de0d263 | 2010-01-05 02:18:06 +0000 | [diff] [blame] | 660 | case MemRegion::CXXThisRegionKind: |
| 661 | assert(0 && "Cannot get size of 'this' region"); |
Ted Kremenek | 67d1287 | 2009-12-07 22:05:27 +0000 | [diff] [blame] | 662 | case MemRegion::GenericMemSpaceRegionKind: |
| 663 | case MemRegion::StackLocalsSpaceRegionKind: |
| 664 | case MemRegion::StackArgumentsSpaceRegionKind: |
| 665 | case MemRegion::HeapSpaceRegionKind: |
| 666 | case MemRegion::GlobalsSpaceRegionKind: |
Ted Kremenek | 2b87ae4 | 2009-12-11 06:43:27 +0000 | [diff] [blame] | 667 | case MemRegion::UnknownSpaceRegionKind: |
Ted Kremenek | 7ecbfbc | 2009-07-10 22:30:06 +0000 | [diff] [blame] | 668 | assert(0 && "Cannot index into a MemSpace"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 669 | return UnknownVal(); |
| 670 | |
Ted Kremenek | eb1c7a0 | 2009-11-25 01:32:22 +0000 | [diff] [blame] | 671 | case MemRegion::FunctionTextRegionKind: |
| 672 | case MemRegion::BlockTextRegionKind: |
Ted Kremenek | 0a8112a | 2009-11-25 23:53:07 +0000 | [diff] [blame] | 673 | case MemRegion::BlockDataRegionKind: |
Ted Kremenek | 7ecbfbc | 2009-07-10 22:30:06 +0000 | [diff] [blame] | 674 | // Technically this can happen if people do funny things with casts. |
Ted Kremenek | 14553ab | 2009-01-30 00:08:43 +0000 | [diff] [blame] | 675 | return UnknownVal(); |
Ted Kremenek | 7ecbfbc | 2009-07-10 22:30:06 +0000 | [diff] [blame] | 676 | |
| 677 | // Not yet handled. |
| 678 | case MemRegion::AllocaRegionKind: |
| 679 | case MemRegion::CompoundLiteralRegionKind: |
| 680 | case MemRegion::ElementRegionKind: |
| 681 | case MemRegion::FieldRegionKind: |
| 682 | case MemRegion::ObjCIvarRegionKind: |
Zhongxing Xu | bb14121 | 2009-12-16 11:27:52 +0000 | [diff] [blame] | 683 | case MemRegion::CXXObjectRegionKind: |
Ted Kremenek | 7ecbfbc | 2009-07-10 22:30:06 +0000 | [diff] [blame] | 684 | return UnknownVal(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 685 | |
Zhongxing Xu | 3ed04d3 | 2010-01-18 08:54:31 +0000 | [diff] [blame] | 686 | case MemRegion::SymbolicRegionKind: { |
| 687 | const SVal *Size = state->get<RegionExtents>(R); |
| 688 | if (!Size) |
| 689 | return UnknownVal(); |
| 690 | const nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(Size); |
| 691 | if (!CI) |
| 692 | return UnknownVal(); |
| 693 | |
| 694 | CharUnits RegionSize = |
| 695 | CharUnits::fromQuantity(CI->getValue().getSExtValue()); |
| 696 | CharUnits EleSize = getContext().getTypeSizeInChars(EleTy); |
| 697 | assert(RegionSize % EleSize == 0); |
| 698 | |
| 699 | return ValMgr.makeIntVal(RegionSize / EleSize, false); |
| 700 | } |
| 701 | |
Ted Kremenek | 7ecbfbc | 2009-07-10 22:30:06 +0000 | [diff] [blame] | 702 | case MemRegion::StringRegionKind: { |
| 703 | const StringLiteral* Str = cast<StringRegion>(R)->getStringLiteral(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 704 | // We intentionally made the size value signed because it participates in |
Ted Kremenek | 7ecbfbc | 2009-07-10 22:30:06 +0000 | [diff] [blame] | 705 | // operations with signed indices. |
| 706 | return ValMgr.makeIntVal(Str->getByteLength()+1, false); |
Ted Kremenek | 14553ab | 2009-01-30 00:08:43 +0000 | [diff] [blame] | 707 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 708 | |
Ted Kremenek | 7ecbfbc | 2009-07-10 22:30:06 +0000 | [diff] [blame] | 709 | case MemRegion::VarRegionKind: { |
| 710 | const VarRegion* VR = cast<VarRegion>(R); |
| 711 | // Get the type of the variable. |
| 712 | QualType T = VR->getDesugaredValueType(getContext()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 713 | |
Ted Kremenek | 7ecbfbc | 2009-07-10 22:30:06 +0000 | [diff] [blame] | 714 | // FIXME: Handle variable-length arrays. |
| 715 | if (isa<VariableArrayType>(T)) |
| 716 | return UnknownVal(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 717 | |
Ted Kremenek | 7ecbfbc | 2009-07-10 22:30:06 +0000 | [diff] [blame] | 718 | if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T)) { |
| 719 | // return the size as signed integer. |
| 720 | return ValMgr.makeIntVal(CAT->getSize(), false); |
| 721 | } |
Ted Kremenek | df74e25 | 2009-08-02 05:15:23 +0000 | [diff] [blame] | 722 | |
Ted Kremenek | 7ecbfbc | 2009-07-10 22:30:06 +0000 | [diff] [blame] | 723 | // Clients can use ordinary variables as if they were arrays. These |
| 724 | // essentially are arrays of size 1. |
| 725 | return ValMgr.makeIntVal(1, false); |
Zhongxing Xu | 41fd018 | 2009-05-06 11:51:48 +0000 | [diff] [blame] | 726 | } |
Zhongxing Xu | e8a964b | 2008-11-22 13:21:46 +0000 | [diff] [blame] | 727 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 728 | |
Ted Kremenek | 7ecbfbc | 2009-07-10 22:30:06 +0000 | [diff] [blame] | 729 | assert(0 && "Unreachable"); |
Ted Kremenek | a21362d | 2009-01-06 19:12:06 +0000 | [diff] [blame] | 730 | return UnknownVal(); |
Zhongxing Xu | e8a964b | 2008-11-22 13:21:46 +0000 | [diff] [blame] | 731 | } |
| 732 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 733 | const GRState *RegionStoreManager::setExtent(const GRState *state, |
| 734 | const MemRegion *region, |
| 735 | SVal extent) { |
| 736 | return state->set<RegionExtents>(region, extent); |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | //===----------------------------------------------------------------------===// |
| 740 | // Location and region casting. |
| 741 | //===----------------------------------------------------------------------===// |
| 742 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 743 | /// ArrayToPointer - Emulates the "decay" of an array to a pointer |
| 744 | /// type. 'Array' represents the lvalue of the array being decayed |
| 745 | /// to a pointer, and the returned SVal represents the decayed |
| 746 | /// version of that lvalue (i.e., a pointer to the first element of |
| 747 | /// the array). This is called by GRExprEngine when evaluating casts |
| 748 | /// from arrays to pointers. |
Zhongxing Xu | f1d537f | 2009-03-30 05:55:46 +0000 | [diff] [blame] | 749 | SVal RegionStoreManager::ArrayToPointer(Loc Array) { |
Ted Kremenek | abb042f | 2008-12-13 19:24:37 +0000 | [diff] [blame] | 750 | if (!isa<loc::MemRegionVal>(Array)) |
| 751 | return UnknownVal(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 752 | |
Ted Kremenek | abb042f | 2008-12-13 19:24:37 +0000 | [diff] [blame] | 753 | const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion(); |
| 754 | const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 755 | |
Ted Kremenek | bbee1a7 | 2009-01-13 01:03:27 +0000 | [diff] [blame] | 756 | if (!ArrayR) |
Ted Kremenek | abb042f | 2008-12-13 19:24:37 +0000 | [diff] [blame] | 757 | return UnknownVal(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 758 | |
Zhongxing Xu | a82d8aa | 2009-05-09 03:57:34 +0000 | [diff] [blame] | 759 | // Strip off typedefs from the ArrayRegion's ValueType. |
John McCall | bf1cc05 | 2009-09-29 23:03:30 +0000 | [diff] [blame] | 760 | QualType T = ArrayR->getValueType(getContext()).getDesugaredType(); |
Ted Kremenek | f936f45 | 2009-05-04 06:18:28 +0000 | [diff] [blame] | 761 | ArrayType *AT = cast<ArrayType>(T); |
| 762 | T = AT->getElementType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 763 | |
Ted Kremenek | 75185b5 | 2009-07-16 00:00:11 +0000 | [diff] [blame] | 764 | SVal ZeroIdx = ValMgr.makeZeroArrayIndex(); |
Ted Kremenek | b48ad64 | 2009-12-04 00:26:31 +0000 | [diff] [blame] | 765 | return loc::MemRegionVal(MRMgr.getElementRegion(T, ZeroIdx, ArrayR, |
| 766 | getContext())); |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 767 | } |
| 768 | |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 769 | //===----------------------------------------------------------------------===// |
| 770 | // Pointer arithmetic. |
| 771 | //===----------------------------------------------------------------------===// |
| 772 | |
Zhongxing Xu | 461147f | 2010-02-05 05:24:20 +0000 | [diff] [blame] | 773 | SVal RegionStoreManager::EvalBinOp(BinaryOperator::Opcode Op, Loc L, NonLoc R, |
Ted Kremenek | 5c73462 | 2009-06-26 00:41:43 +0000 | [diff] [blame] | 774 | QualType resultTy) { |
Zhongxing Xu | c4761f5 | 2009-05-09 15:18:12 +0000 | [diff] [blame] | 775 | // Assume the base location is MemRegionVal. |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 776 | if (!isa<loc::MemRegionVal>(L)) |
Zhongxing Xu | 94aa6c1 | 2009-03-02 07:52:23 +0000 | [diff] [blame] | 777 | return UnknownVal(); |
Zhongxing Xu | 94aa6c1 | 2009-03-02 07:52:23 +0000 | [diff] [blame] | 778 | |
Zhongxing Xu | a1718c7 | 2009-04-03 07:33:13 +0000 | [diff] [blame] | 779 | const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion(); |
Zhongxing Xu | c4761f5 | 2009-05-09 15:18:12 +0000 | [diff] [blame] | 780 | const ElementRegion *ER = 0; |
Zhongxing Xu | 262fd03 | 2009-05-20 09:00:16 +0000 | [diff] [blame] | 781 | |
Ted Kremenek | 3bccf08 | 2009-07-11 00:58:27 +0000 | [diff] [blame] | 782 | switch (MR->getKind()) { |
| 783 | case MemRegion::SymbolicRegionKind: { |
| 784 | const SymbolicRegion *SR = cast<SymbolicRegion>(MR); |
Ted Kremenek | df74e25 | 2009-08-02 05:15:23 +0000 | [diff] [blame] | 785 | SymbolRef Sym = SR->getSymbol(); |
Ted Kremenek | bcf62a9 | 2009-08-25 22:55:09 +0000 | [diff] [blame] | 786 | QualType T = Sym->getType(getContext()); |
| 787 | QualType EleTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 788 | |
Ted Kremenek | bcf62a9 | 2009-08-25 22:55:09 +0000 | [diff] [blame] | 789 | if (const PointerType *PT = T->getAs<PointerType>()) |
| 790 | EleTy = PT->getPointeeType(); |
| 791 | else |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 792 | EleTy = T->getAs<ObjCObjectPointerType>()->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 793 | |
Ted Kremenek | 3bccf08 | 2009-07-11 00:58:27 +0000 | [diff] [blame] | 794 | SVal ZeroIdx = ValMgr.makeZeroArrayIndex(); |
| 795 | ER = MRMgr.getElementRegion(EleTy, ZeroIdx, SR, getContext()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 796 | break; |
Zhongxing Xu | 005f07b | 2009-06-19 04:51:14 +0000 | [diff] [blame] | 797 | } |
Ted Kremenek | 3bccf08 | 2009-07-11 00:58:27 +0000 | [diff] [blame] | 798 | case MemRegion::AllocaRegionKind: { |
Ted Kremenek | 3bccf08 | 2009-07-11 00:58:27 +0000 | [diff] [blame] | 799 | const AllocaRegion *AR = cast<AllocaRegion>(MR); |
Ted Kremenek | df74e25 | 2009-08-02 05:15:23 +0000 | [diff] [blame] | 800 | QualType T = getContext().CharTy; // Create an ElementRegion of bytes. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 801 | QualType EleTy = T->getAs<PointerType>()->getPointeeType(); |
Ted Kremenek | 3bccf08 | 2009-07-11 00:58:27 +0000 | [diff] [blame] | 802 | SVal ZeroIdx = ValMgr.makeZeroArrayIndex(); |
| 803 | ER = MRMgr.getElementRegion(EleTy, ZeroIdx, AR, getContext()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 804 | break; |
Ted Kremenek | 3bccf08 | 2009-07-11 00:58:27 +0000 | [diff] [blame] | 805 | } |
Zhongxing Xu | a1718c7 | 2009-04-03 07:33:13 +0000 | [diff] [blame] | 806 | |
Ted Kremenek | 3bccf08 | 2009-07-11 00:58:27 +0000 | [diff] [blame] | 807 | case MemRegion::ElementRegionKind: { |
| 808 | ER = cast<ElementRegion>(MR); |
| 809 | break; |
| 810 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 811 | |
Ted Kremenek | 3bccf08 | 2009-07-11 00:58:27 +0000 | [diff] [blame] | 812 | // Not yet handled. |
| 813 | case MemRegion::VarRegionKind: |
Ted Kremenek | cd8f6ac | 2009-10-06 01:39:48 +0000 | [diff] [blame] | 814 | case MemRegion::StringRegionKind: { |
| 815 | |
| 816 | } |
| 817 | // Fall-through. |
Ted Kremenek | 3bccf08 | 2009-07-11 00:58:27 +0000 | [diff] [blame] | 818 | case MemRegion::CompoundLiteralRegionKind: |
| 819 | case MemRegion::FieldRegionKind: |
Ted Kremenek | 3bccf08 | 2009-07-11 00:58:27 +0000 | [diff] [blame] | 820 | case MemRegion::ObjCIvarRegionKind: |
Zhongxing Xu | bb14121 | 2009-12-16 11:27:52 +0000 | [diff] [blame] | 821 | case MemRegion::CXXObjectRegionKind: |
Ted Kremenek | 3bccf08 | 2009-07-11 00:58:27 +0000 | [diff] [blame] | 822 | return UnknownVal(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 823 | |
Ted Kremenek | eb1c7a0 | 2009-11-25 01:32:22 +0000 | [diff] [blame] | 824 | case MemRegion::FunctionTextRegionKind: |
| 825 | case MemRegion::BlockTextRegionKind: |
Ted Kremenek | 0a8112a | 2009-11-25 23:53:07 +0000 | [diff] [blame] | 826 | case MemRegion::BlockDataRegionKind: |
Ted Kremenek | 3bccf08 | 2009-07-11 00:58:27 +0000 | [diff] [blame] | 827 | // Technically this can happen if people do funny things with casts. |
| 828 | return UnknownVal(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 829 | |
Ted Kremenek | de0d263 | 2010-01-05 02:18:06 +0000 | [diff] [blame] | 830 | case MemRegion::CXXThisRegionKind: |
| 831 | assert(0 && |
| 832 | "Cannot perform pointer arithmetic on implicit argument 'this'"); |
Ted Kremenek | 67d1287 | 2009-12-07 22:05:27 +0000 | [diff] [blame] | 833 | case MemRegion::GenericMemSpaceRegionKind: |
| 834 | case MemRegion::StackLocalsSpaceRegionKind: |
| 835 | case MemRegion::StackArgumentsSpaceRegionKind: |
| 836 | case MemRegion::HeapSpaceRegionKind: |
| 837 | case MemRegion::GlobalsSpaceRegionKind: |
Ted Kremenek | 2b87ae4 | 2009-12-11 06:43:27 +0000 | [diff] [blame] | 838 | case MemRegion::UnknownSpaceRegionKind: |
Ted Kremenek | 3bccf08 | 2009-07-11 00:58:27 +0000 | [diff] [blame] | 839 | assert(0 && "Cannot perform pointer arithmetic on a MemSpace"); |
| 840 | return UnknownVal(); |
Zhongxing Xu | 5414a5c | 2009-06-21 13:24:24 +0000 | [diff] [blame] | 841 | } |
Zhongxing Xu | 2b1dc17 | 2009-03-11 07:43:49 +0000 | [diff] [blame] | 842 | |
Zhongxing Xu | 94aa6c1 | 2009-03-02 07:52:23 +0000 | [diff] [blame] | 843 | SVal Idx = ER->getIndex(); |
Zhongxing Xu | 94aa6c1 | 2009-03-02 07:52:23 +0000 | [diff] [blame] | 844 | nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx); |
Zhongxing Xu | 94aa6c1 | 2009-03-02 07:52:23 +0000 | [diff] [blame] | 845 | |
Ted Kremenek | cd8f6ac | 2009-10-06 01:39:48 +0000 | [diff] [blame] | 846 | // For now, only support: |
| 847 | // (a) concrete integer indices that can easily be resolved |
| 848 | // (b) 0 + symbolic index |
| 849 | if (Base) { |
| 850 | if (nonloc::ConcreteInt *Offset = dyn_cast<nonloc::ConcreteInt>(&R)) { |
| 851 | // FIXME: Should use SValuator here. |
| 852 | SVal NewIdx = |
| 853 | Base->evalBinOp(ValMgr, Op, |
Ted Kremenek | 4653739 | 2009-07-16 01:33:37 +0000 | [diff] [blame] | 854 | cast<nonloc::ConcreteInt>(ValMgr.convertToArrayIndex(*Offset))); |
Ted Kremenek | cd8f6ac | 2009-10-06 01:39:48 +0000 | [diff] [blame] | 855 | const MemRegion* NewER = |
| 856 | MRMgr.getElementRegion(ER->getElementType(), NewIdx, |
| 857 | ER->getSuperRegion(), getContext()); |
| 858 | return ValMgr.makeLoc(NewER); |
| 859 | } |
| 860 | if (0 == Base->getValue()) { |
| 861 | const MemRegion* NewER = |
| 862 | MRMgr.getElementRegion(ER->getElementType(), R, |
| 863 | ER->getSuperRegion(), getContext()); |
| 864 | return ValMgr.makeLoc(NewER); |
| 865 | } |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 866 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 867 | |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 868 | return UnknownVal(); |
Zhongxing Xu | 94aa6c1 | 2009-03-02 07:52:23 +0000 | [diff] [blame] | 869 | } |
| 870 | |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 871 | //===----------------------------------------------------------------------===// |
| 872 | // Loading values from regions. |
| 873 | //===----------------------------------------------------------------------===// |
| 874 | |
Zhongxing Xu | 13d5017 | 2009-10-11 08:08:02 +0000 | [diff] [blame] | 875 | Optional<SVal> RegionStoreManager::getDirectBinding(RegionBindings B, |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 876 | const MemRegion *R) { |
| 877 | if (const SVal *V = Lookup(B, R, BindingKey::Direct)) |
| 878 | return *V; |
| 879 | |
Zhongxing Xu | 13d5017 | 2009-10-11 08:08:02 +0000 | [diff] [blame] | 880 | return Optional<SVal>(); |
| 881 | } |
| 882 | |
| 883 | Optional<SVal> RegionStoreManager::getDefaultBinding(RegionBindings B, |
Ted Kremenek | d4e5a60 | 2009-08-06 21:43:54 +0000 | [diff] [blame] | 884 | const MemRegion *R) { |
Ted Kremenek | d4e5a60 | 2009-08-06 21:43:54 +0000 | [diff] [blame] | 885 | if (R->isBoundable()) |
| 886 | if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) |
| 887 | if (TR->getValueType(getContext())->isUnionType()) |
| 888 | return UnknownVal(); |
| 889 | |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 890 | if (const SVal *V = Lookup(B, R, BindingKey::Default)) |
| 891 | return *V; |
Zhongxing Xu | 13d5017 | 2009-10-11 08:08:02 +0000 | [diff] [blame] | 892 | |
| 893 | return Optional<SVal>(); |
| 894 | } |
| 895 | |
| 896 | Optional<SVal> RegionStoreManager::getBinding(RegionBindings B, |
| 897 | const MemRegion *R) { |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 898 | |
| 899 | if (Optional<SVal> V = getDirectBinding(B, R)) |
| 900 | return V; |
| 901 | |
| 902 | return getDefaultBinding(B, R); |
Ted Kremenek | d4e5a60 | 2009-08-06 21:43:54 +0000 | [diff] [blame] | 903 | } |
| 904 | |
Ted Kremenek | a6275a5 | 2009-07-15 02:31:43 +0000 | [diff] [blame] | 905 | static bool IsReinterpreted(QualType RTy, QualType UsedTy, ASTContext &Ctx) { |
| 906 | RTy = Ctx.getCanonicalType(RTy); |
| 907 | UsedTy = Ctx.getCanonicalType(UsedTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 908 | |
Ted Kremenek | a6275a5 | 2009-07-15 02:31:43 +0000 | [diff] [blame] | 909 | if (RTy == UsedTy) |
| 910 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 911 | |
| 912 | |
Ted Kremenek | 25c5457 | 2009-07-20 22:58:02 +0000 | [diff] [blame] | 913 | // Recursively check the types. We basically want to see if a pointer value |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 914 | // is ever reinterpreted as a non-pointer, e.g. void** and intptr_t* |
Ted Kremenek | 25c5457 | 2009-07-20 22:58:02 +0000 | [diff] [blame] | 915 | // represents a reinterpretation. |
| 916 | if (Loc::IsLocType(RTy) && Loc::IsLocType(UsedTy)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 917 | const PointerType *PRTy = RTy->getAs<PointerType>(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 918 | const PointerType *PUsedTy = UsedTy->getAs<PointerType>(); |
Ted Kremenek | 25c5457 | 2009-07-20 22:58:02 +0000 | [diff] [blame] | 919 | |
| 920 | return PUsedTy && PRTy && |
| 921 | IsReinterpreted(PRTy->getPointeeType(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 922 | PUsedTy->getPointeeType(), Ctx); |
Ted Kremenek | 25c5457 | 2009-07-20 22:58:02 +0000 | [diff] [blame] | 923 | } |
| 924 | |
| 925 | return true; |
Ted Kremenek | a6275a5 | 2009-07-15 02:31:43 +0000 | [diff] [blame] | 926 | } |
| 927 | |
Ted Kremenek | 0954cde | 2009-09-24 04:11:44 +0000 | [diff] [blame] | 928 | const ElementRegion * |
Zhongxing Xu | 8149185 | 2010-02-08 08:43:02 +0000 | [diff] [blame] | 929 | RegionStoreManager::GetElementZeroRegion(const MemRegion *R, QualType T) { |
Ted Kremenek | 0954cde | 2009-09-24 04:11:44 +0000 | [diff] [blame] | 930 | ASTContext &Ctx = getContext(); |
| 931 | SVal idx = ValMgr.makeZeroArrayIndex(); |
| 932 | assert(!T.isNull()); |
Zhongxing Xu | 8149185 | 2010-02-08 08:43:02 +0000 | [diff] [blame] | 933 | return MRMgr.getElementRegion(T, idx, R, Ctx); |
Ted Kremenek | 0954cde | 2009-09-24 04:11:44 +0000 | [diff] [blame] | 934 | } |
Ted Kremenek | 0954cde | 2009-09-24 04:11:44 +0000 | [diff] [blame] | 935 | |
Zhongxing Xu | 576bb92 | 2010-02-05 03:01:53 +0000 | [diff] [blame] | 936 | SVal RegionStoreManager::Retrieve(Store store, Loc L, QualType T) { |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 937 | assert(!isa<UnknownVal>(L) && "location unknown"); |
| 938 | assert(!isa<UndefinedVal>(L) && "location undefined"); |
Ted Kremenek | 28172a2 | 2009-12-15 23:23:27 +0000 | [diff] [blame] | 939 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 940 | // FIXME: Is this even possible? Shouldn't this be treated as a null |
| 941 | // dereference at a higher level? |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 942 | if (isa<loc::ConcreteInt>(L)) |
Zhongxing Xu | c999ed7 | 2010-02-04 02:39:47 +0000 | [diff] [blame] | 943 | return UndefinedVal(); |
Ted Kremenek | 28172a2 | 2009-12-15 23:23:27 +0000 | [diff] [blame] | 944 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 945 | const MemRegion *MR = cast<loc::MemRegionVal>(L).getRegion(); |
Zhongxing Xu | a1718c7 | 2009-04-03 07:33:13 +0000 | [diff] [blame] | 946 | |
Zhongxing Xu | 8149185 | 2010-02-08 08:43:02 +0000 | [diff] [blame] | 947 | if (isa<AllocaRegion>(MR) || isa<SymbolicRegion>(MR)) |
| 948 | MR = GetElementZeroRegion(MR, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 949 | |
Ted Kremenek | 968f0a6 | 2009-08-03 21:41:46 +0000 | [diff] [blame] | 950 | if (isa<CodeTextRegion>(MR)) |
Zhongxing Xu | c999ed7 | 2010-02-04 02:39:47 +0000 | [diff] [blame] | 951 | return UnknownVal(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 952 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 953 | // FIXME: Perhaps this method should just take a 'const MemRegion*' argument |
| 954 | // 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] | 955 | const TypedRegion *R = cast<TypedRegion>(MR); |
Ted Kremenek | a6275a5 | 2009-07-15 02:31:43 +0000 | [diff] [blame] | 956 | QualType RTy = R->getValueType(getContext()); |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 957 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 958 | // FIXME: We should eventually handle funny addressing. e.g.: |
| 959 | // |
| 960 | // int x = ...; |
| 961 | // int *p = &x; |
| 962 | // char *q = (char*) p; |
| 963 | // char c = *q; // returns the first byte of 'x'. |
| 964 | // |
| 965 | // Such funny addressing will occur due to layering of regions. |
| 966 | |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 967 | #if 0 |
Ted Kremenek | a6275a5 | 2009-07-15 02:31:43 +0000 | [diff] [blame] | 968 | ASTContext &Ctx = getContext(); |
| 969 | if (!T.isNull() && IsReinterpreted(RTy, T, Ctx)) { |
Ted Kremenek | 4653739 | 2009-07-16 01:33:37 +0000 | [diff] [blame] | 970 | SVal ZeroIdx = ValMgr.makeZeroArrayIndex(); |
| 971 | R = MRMgr.getElementRegion(T, ZeroIdx, R, Ctx); |
Ted Kremenek | a6275a5 | 2009-07-15 02:31:43 +0000 | [diff] [blame] | 972 | RTy = T; |
Ted Kremenek | 41fb0df | 2009-07-15 04:23:32 +0000 | [diff] [blame] | 973 | assert(Ctx.getCanonicalType(RTy) == |
| 974 | Ctx.getCanonicalType(R->getValueType(Ctx))); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 975 | } |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 976 | #endif |
Zhongxing Xu | 3e001f3 | 2009-05-03 00:27:40 +0000 | [diff] [blame] | 977 | |
Zhongxing Xu | 1038f9f | 2009-03-09 09:15:51 +0000 | [diff] [blame] | 978 | if (RTy->isStructureType()) |
Zhongxing Xu | 576bb92 | 2010-02-05 03:01:53 +0000 | [diff] [blame] | 979 | return RetrieveStruct(store, R); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 980 | |
Ted Kremenek | d4e5a60 | 2009-08-06 21:43:54 +0000 | [diff] [blame] | 981 | // FIXME: Handle unions. |
| 982 | if (RTy->isUnionType()) |
Zhongxing Xu | c999ed7 | 2010-02-04 02:39:47 +0000 | [diff] [blame] | 983 | return UnknownVal(); |
Zhongxing Xu | 3e001f3 | 2009-05-03 00:27:40 +0000 | [diff] [blame] | 984 | |
| 985 | if (RTy->isArrayType()) |
Zhongxing Xu | 576bb92 | 2010-02-05 03:01:53 +0000 | [diff] [blame] | 986 | return RetrieveArray(store, R); |
Zhongxing Xu | 3e001f3 | 2009-05-03 00:27:40 +0000 | [diff] [blame] | 987 | |
Zhongxing Xu | 1038f9f | 2009-03-09 09:15:51 +0000 | [diff] [blame] | 988 | // FIXME: handle Vector types. |
| 989 | if (RTy->isVectorType()) |
Zhongxing Xu | c999ed7 | 2010-02-04 02:39:47 +0000 | [diff] [blame] | 990 | return UnknownVal(); |
Zhongxing Xu | 99c2030 | 2009-06-28 14:16:39 +0000 | [diff] [blame] | 991 | |
| 992 | if (const FieldRegion* FR = dyn_cast<FieldRegion>(R)) |
Zhongxing Xu | 576bb92 | 2010-02-05 03:01:53 +0000 | [diff] [blame] | 993 | return CastRetrievedVal(RetrieveField(store, FR), FR, T, false); |
Zhongxing Xu | 99c2030 | 2009-06-28 14:16:39 +0000 | [diff] [blame] | 994 | |
Ted Kremenek | c50e6df | 2010-01-11 02:33:26 +0000 | [diff] [blame] | 995 | if (const ElementRegion* ER = dyn_cast<ElementRegion>(R)) { |
| 996 | // FIXME: Here we actually perform an implicit conversion from the loaded |
| 997 | // value to the element type. Eventually we want to compose these values |
| 998 | // more intelligently. For example, an 'element' can encompass multiple |
| 999 | // bound regions (e.g., several bound bytes), or could be a subset of |
| 1000 | // a larger value. |
Zhongxing Xu | 576bb92 | 2010-02-05 03:01:53 +0000 | [diff] [blame] | 1001 | return CastRetrievedVal(RetrieveElement(store, ER), ER, T, false); |
Ted Kremenek | c50e6df | 2010-01-11 02:33:26 +0000 | [diff] [blame] | 1002 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1003 | |
Ted Kremenek | c50e6df | 2010-01-11 02:33:26 +0000 | [diff] [blame] | 1004 | if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R)) { |
| 1005 | // FIXME: Here we actually perform an implicit conversion from the loaded |
| 1006 | // value to the ivar type. What we should model is stores to ivars |
| 1007 | // that blow past the extent of the ivar. If the address of the ivar is |
| 1008 | // reinterpretted, it is possible we stored a different value that could |
| 1009 | // fit within the ivar. Either we need to cast these when storing them |
| 1010 | // or reinterpret them lazily (as we do here). |
Zhongxing Xu | 576bb92 | 2010-02-05 03:01:53 +0000 | [diff] [blame] | 1011 | return CastRetrievedVal(RetrieveObjCIvar(store, IVR), IVR, T, false); |
Ted Kremenek | c50e6df | 2010-01-11 02:33:26 +0000 | [diff] [blame] | 1012 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1013 | |
Ted Kremenek | c50e6df | 2010-01-11 02:33:26 +0000 | [diff] [blame] | 1014 | if (const VarRegion *VR = dyn_cast<VarRegion>(R)) { |
| 1015 | // FIXME: Here we actually perform an implicit conversion from the loaded |
| 1016 | // value to the variable type. What we should model is stores to variables |
| 1017 | // that blow past the extent of the variable. If the address of the |
| 1018 | // variable is reinterpretted, it is possible we stored a different value |
| 1019 | // that could fit within the variable. Either we need to cast these when |
| 1020 | // storing them or reinterpret them lazily (as we do here). |
Zhongxing Xu | 576bb92 | 2010-02-05 03:01:53 +0000 | [diff] [blame] | 1021 | return CastRetrievedVal(RetrieveVar(store, VR), VR, T, false); |
Ted Kremenek | c50e6df | 2010-01-11 02:33:26 +0000 | [diff] [blame] | 1022 | } |
Ted Kremenek | 25c5457 | 2009-07-20 22:58:02 +0000 | [diff] [blame] | 1023 | |
Zhongxing Xu | 576bb92 | 2010-02-05 03:01:53 +0000 | [diff] [blame] | 1024 | RegionBindings B = GetRegionBindings(store); |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 1025 | const SVal *V = Lookup(B, R, BindingKey::Direct); |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1026 | |
| 1027 | // Check if the region has a binding. |
| 1028 | if (V) |
Zhongxing Xu | c999ed7 | 2010-02-04 02:39:47 +0000 | [diff] [blame] | 1029 | return *V; |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 1030 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 1031 | // The location does not have a bound value. This means that it has |
| 1032 | // the value it had upon its creation and/or entry to the analyzed |
| 1033 | // function/method. These are either symbolic values or 'undefined'. |
Ted Kremenek | de0d263 | 2010-01-05 02:18:06 +0000 | [diff] [blame] | 1034 | if (R->hasStackNonParametersStorage()) { |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 1035 | // All stack variables are considered to have undefined values |
| 1036 | // upon creation. All heap allocated blocks are considered to |
| 1037 | // have undefined values as well unless they are explicitly bound |
| 1038 | // to specific values. |
Zhongxing Xu | c999ed7 | 2010-02-04 02:39:47 +0000 | [diff] [blame] | 1039 | return UndefinedVal(); |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 1040 | } |
| 1041 | |
Ted Kremenek | bb2b433 | 2009-07-02 22:16:42 +0000 | [diff] [blame] | 1042 | // All other values are symbolic. |
Zhongxing Xu | c999ed7 | 2010-02-04 02:39:47 +0000 | [diff] [blame] | 1043 | return ValMgr.getRegionValueSymbolVal(R, RTy); |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 1044 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1045 | |
Zhongxing Xu | bfcaf80 | 2010-02-05 02:26:30 +0000 | [diff] [blame] | 1046 | std::pair<Store, const MemRegion *> |
Ted Kremenek | 451ac09 | 2009-08-06 04:50:20 +0000 | [diff] [blame] | 1047 | RegionStoreManager::GetLazyBinding(RegionBindings B, const MemRegion *R) { |
Zhongxing Xu | 13d5017 | 2009-10-11 08:08:02 +0000 | [diff] [blame] | 1048 | if (Optional<SVal> OV = getDirectBinding(B, R)) |
| 1049 | if (const nonloc::LazyCompoundVal *V = |
| 1050 | dyn_cast<nonloc::LazyCompoundVal>(OV.getPointer())) |
Zhongxing Xu | bfcaf80 | 2010-02-05 02:26:30 +0000 | [diff] [blame] | 1051 | return std::make_pair(V->getStore(), V->getRegion()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1052 | |
Ted Kremenek | a5e81f1 | 2009-08-06 01:20:57 +0000 | [diff] [blame] | 1053 | if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) { |
Zhongxing Xu | bfcaf80 | 2010-02-05 02:26:30 +0000 | [diff] [blame] | 1054 | const std::pair<Store, const MemRegion *> &X = |
Ted Kremenek | a5e81f1 | 2009-08-06 01:20:57 +0000 | [diff] [blame] | 1055 | GetLazyBinding(B, ER->getSuperRegion()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1056 | |
Ted Kremenek | a5e81f1 | 2009-08-06 01:20:57 +0000 | [diff] [blame] | 1057 | if (X.first) |
| 1058 | return std::make_pair(X.first, |
| 1059 | MRMgr.getElementRegionWithSuper(ER, X.second)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1060 | } |
Ted Kremenek | a5e81f1 | 2009-08-06 01:20:57 +0000 | [diff] [blame] | 1061 | else if (const FieldRegion *FR = dyn_cast<FieldRegion>(R)) { |
Zhongxing Xu | bfcaf80 | 2010-02-05 02:26:30 +0000 | [diff] [blame] | 1062 | const std::pair<Store, const MemRegion *> &X = |
Ted Kremenek | a5e81f1 | 2009-08-06 01:20:57 +0000 | [diff] [blame] | 1063 | GetLazyBinding(B, FR->getSuperRegion()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1064 | |
Ted Kremenek | a5e81f1 | 2009-08-06 01:20:57 +0000 | [diff] [blame] | 1065 | if (X.first) |
| 1066 | return std::make_pair(X.first, |
| 1067 | MRMgr.getFieldRegionWithSuper(FR, X.second)); |
| 1068 | } |
| 1069 | |
Zhongxing Xu | bfcaf80 | 2010-02-05 02:26:30 +0000 | [diff] [blame] | 1070 | return std::make_pair((Store) 0, (const MemRegion *) 0); |
Ted Kremenek | a5e81f1 | 2009-08-06 01:20:57 +0000 | [diff] [blame] | 1071 | } |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 1072 | |
Zhongxing Xu | bfcaf80 | 2010-02-05 02:26:30 +0000 | [diff] [blame] | 1073 | SVal RegionStoreManager::RetrieveElement(Store store, |
Zhongxing Xu | c00346f | 2009-06-25 05:29:39 +0000 | [diff] [blame] | 1074 | const ElementRegion* R) { |
| 1075 | // Check if the region has a binding. |
Zhongxing Xu | bfcaf80 | 2010-02-05 02:26:30 +0000 | [diff] [blame] | 1076 | RegionBindings B = GetRegionBindings(store); |
Ted Kremenek | c50e6df | 2010-01-11 02:33:26 +0000 | [diff] [blame] | 1077 | if (Optional<SVal> V = getDirectBinding(B, R)) |
Zhongxing Xu | c00346f | 2009-06-25 05:29:39 +0000 | [diff] [blame] | 1078 | return *V; |
| 1079 | |
Ted Kremenek | 921109a | 2009-07-01 23:19:52 +0000 | [diff] [blame] | 1080 | const MemRegion* superR = R->getSuperRegion(); |
| 1081 | |
Zhongxing Xu | c00346f | 2009-06-25 05:29:39 +0000 | [diff] [blame] | 1082 | // Check if the region is an element region of a string literal. |
Ted Kremenek | 921109a | 2009-07-01 23:19:52 +0000 | [diff] [blame] | 1083 | if (const StringRegion *StrR=dyn_cast<StringRegion>(superR)) { |
Ted Kremenek | 95efe0f | 2009-09-29 16:36:48 +0000 | [diff] [blame] | 1084 | // FIXME: Handle loads from strings where the literal is treated as |
| 1085 | // an integer, e.g., *((unsigned int*)"hello") |
| 1086 | ASTContext &Ctx = getContext(); |
Douglas Gregor | 89c49f0 | 2009-11-09 22:08:55 +0000 | [diff] [blame] | 1087 | QualType T = Ctx.getAsArrayType(StrR->getValueType(Ctx))->getElementType(); |
Ted Kremenek | 95efe0f | 2009-09-29 16:36:48 +0000 | [diff] [blame] | 1088 | if (T != Ctx.getCanonicalType(R->getElementType())) |
| 1089 | return UnknownVal(); |
| 1090 | |
Zhongxing Xu | c00346f | 2009-06-25 05:29:39 +0000 | [diff] [blame] | 1091 | const StringLiteral *Str = StrR->getStringLiteral(); |
| 1092 | SVal Idx = R->getIndex(); |
| 1093 | if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&Idx)) { |
| 1094 | int64_t i = CI->getValue().getSExtValue(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1095 | int64_t byteLength = Str->getByteLength(); |
Ted Kremenek | 0667db3 | 2009-09-05 17:59:01 +0000 | [diff] [blame] | 1096 | if (i > byteLength) { |
| 1097 | // Buffer overflow checking in GRExprEngine should handle this case, |
| 1098 | // but we shouldn't rely on it to not overflow here if that checking |
| 1099 | // is disabled. |
| 1100 | return UnknownVal(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1101 | } |
Ted Kremenek | 0667db3 | 2009-09-05 17:59:01 +0000 | [diff] [blame] | 1102 | char c = (i == byteLength) ? '\0' : Str->getStrData()[i]; |
Ted Kremenek | 95efe0f | 2009-09-29 16:36:48 +0000 | [diff] [blame] | 1103 | return ValMgr.makeIntVal(c, T); |
Zhongxing Xu | c00346f | 2009-06-25 05:29:39 +0000 | [diff] [blame] | 1104 | } |
| 1105 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1106 | |
Ted Kremenek | 566a6fa | 2009-08-06 22:33:36 +0000 | [diff] [blame] | 1107 | // Check if the immediate super region has a direct binding. |
Zhongxing Xu | 13d5017 | 2009-10-11 08:08:02 +0000 | [diff] [blame] | 1108 | if (Optional<SVal> V = getDirectBinding(B, superR)) { |
Ted Kremenek | a6275a5 | 2009-07-15 02:31:43 +0000 | [diff] [blame] | 1109 | if (SymbolRef parentSym = V->getAsSymbol()) |
| 1110 | return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R); |
Ted Kremenek | 356e9d6 | 2009-07-22 04:35:42 +0000 | [diff] [blame] | 1111 | |
| 1112 | if (V->isUnknownOrUndef()) |
| 1113 | return *V; |
Ted Kremenek | 566a6fa | 2009-08-06 22:33:36 +0000 | [diff] [blame] | 1114 | |
| 1115 | // Handle LazyCompoundVals for the immediate super region. Other cases |
| 1116 | // are handled in 'RetrieveFieldOrElementCommon'. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1117 | if (const nonloc::LazyCompoundVal *LCV = |
Ted Kremenek | 566a6fa | 2009-08-06 22:33:36 +0000 | [diff] [blame] | 1118 | dyn_cast<nonloc::LazyCompoundVal>(V)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1119 | |
Ted Kremenek | 566a6fa | 2009-08-06 22:33:36 +0000 | [diff] [blame] | 1120 | R = MRMgr.getElementRegionWithSuper(R, LCV->getRegion()); |
Zhongxing Xu | bfcaf80 | 2010-02-05 02:26:30 +0000 | [diff] [blame] | 1121 | return RetrieveElement(LCV->getStore(), R); |
Ted Kremenek | 566a6fa | 2009-08-06 22:33:36 +0000 | [diff] [blame] | 1122 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1123 | |
Ted Kremenek | a6275a5 | 2009-07-15 02:31:43 +0000 | [diff] [blame] | 1124 | // Other cases: give up. |
Zhongxing Xu | 8834af3 | 2009-07-03 06:11:41 +0000 | [diff] [blame] | 1125 | return UnknownVal(); |
Zhongxing Xu | 7abe019 | 2009-06-30 12:32:59 +0000 | [diff] [blame] | 1126 | } |
Ted Kremenek | c50e6df | 2010-01-11 02:33:26 +0000 | [diff] [blame] | 1127 | |
Zhongxing Xu | bfcaf80 | 2010-02-05 02:26:30 +0000 | [diff] [blame] | 1128 | return RetrieveFieldOrElementCommon(store, R, R->getElementType(), superR); |
Zhongxing Xu | c00346f | 2009-06-25 05:29:39 +0000 | [diff] [blame] | 1129 | } |
| 1130 | |
Zhongxing Xu | bfcaf80 | 2010-02-05 02:26:30 +0000 | [diff] [blame] | 1131 | SVal RegionStoreManager::RetrieveField(Store store, |
Zhongxing Xu | 490b0f0 | 2009-06-25 04:50:44 +0000 | [diff] [blame] | 1132 | const FieldRegion* R) { |
Zhongxing Xu | 490b0f0 | 2009-06-25 04:50:44 +0000 | [diff] [blame] | 1133 | |
| 1134 | // Check if the region has a binding. |
Zhongxing Xu | bfcaf80 | 2010-02-05 02:26:30 +0000 | [diff] [blame] | 1135 | RegionBindings B = GetRegionBindings(store); |
Zhongxing Xu | 13d5017 | 2009-10-11 08:08:02 +0000 | [diff] [blame] | 1136 | if (Optional<SVal> V = getDirectBinding(B, R)) |
Zhongxing Xu | 490b0f0 | 2009-06-25 04:50:44 +0000 | [diff] [blame] | 1137 | return *V; |
| 1138 | |
Ted Kremenek | 566a6fa | 2009-08-06 22:33:36 +0000 | [diff] [blame] | 1139 | QualType Ty = R->getValueType(getContext()); |
Zhongxing Xu | bfcaf80 | 2010-02-05 02:26:30 +0000 | [diff] [blame] | 1140 | return RetrieveFieldOrElementCommon(store, R, Ty, R->getSuperRegion()); |
Ted Kremenek | 566a6fa | 2009-08-06 22:33:36 +0000 | [diff] [blame] | 1141 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1142 | |
Zhongxing Xu | bfcaf80 | 2010-02-05 02:26:30 +0000 | [diff] [blame] | 1143 | SVal RegionStoreManager::RetrieveFieldOrElementCommon(Store store, |
Ted Kremenek | 566a6fa | 2009-08-06 22:33:36 +0000 | [diff] [blame] | 1144 | const TypedRegion *R, |
| 1145 | QualType Ty, |
| 1146 | const MemRegion *superR) { |
| 1147 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1148 | // At this point we have already checked in either RetrieveElement or |
Ted Kremenek | 566a6fa | 2009-08-06 22:33:36 +0000 | [diff] [blame] | 1149 | // RetrieveField if 'R' has a direct binding. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1150 | |
Zhongxing Xu | bfcaf80 | 2010-02-05 02:26:30 +0000 | [diff] [blame] | 1151 | RegionBindings B = GetRegionBindings(store); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1152 | |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 1153 | while (superR) { |
Zhongxing Xu | 13d5017 | 2009-10-11 08:08:02 +0000 | [diff] [blame] | 1154 | if (const Optional<SVal> &D = getDefaultBinding(B, superR)) { |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 1155 | if (SymbolRef parentSym = D->getAsSymbol()) |
| 1156 | return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1157 | |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 1158 | if (D->isZeroConstant()) |
| 1159 | return ValMgr.makeZeroVal(Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1160 | |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 1161 | if (D->isUnknown()) |
| 1162 | return *D; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1163 | |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 1164 | assert(0 && "Unknown default value"); |
| 1165 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1166 | |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 1167 | // If our super region is a field or element itself, walk up the region |
| 1168 | // hierarchy to see if there is a default value installed in an ancestor. |
| 1169 | if (isa<FieldRegion>(superR) || isa<ElementRegion>(superR)) { |
| 1170 | superR = cast<SubRegion>(superR)->getSuperRegion(); |
| 1171 | continue; |
| 1172 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1173 | |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 1174 | break; |
Ted Kremenek | a5e81f1 | 2009-08-06 01:20:57 +0000 | [diff] [blame] | 1175 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1176 | |
Ted Kremenek | a5e81f1 | 2009-08-06 01:20:57 +0000 | [diff] [blame] | 1177 | // Lazy binding? |
Zhongxing Xu | bfcaf80 | 2010-02-05 02:26:30 +0000 | [diff] [blame] | 1178 | Store lazyBindingStore = NULL; |
Ted Kremenek | 566a6fa | 2009-08-06 22:33:36 +0000 | [diff] [blame] | 1179 | const MemRegion *lazyBindingRegion = NULL; |
Zhongxing Xu | bfcaf80 | 2010-02-05 02:26:30 +0000 | [diff] [blame] | 1180 | llvm::tie(lazyBindingStore, lazyBindingRegion) = GetLazyBinding(B, R); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1181 | |
Zhongxing Xu | bfcaf80 | 2010-02-05 02:26:30 +0000 | [diff] [blame] | 1182 | if (lazyBindingStore) { |
Ted Kremenek | 566a6fa | 2009-08-06 22:33:36 +0000 | [diff] [blame] | 1183 | assert(lazyBindingRegion && "Lazy-binding region not set"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1184 | |
Ted Kremenek | 566a6fa | 2009-08-06 22:33:36 +0000 | [diff] [blame] | 1185 | if (isa<ElementRegion>(R)) |
Zhongxing Xu | bfcaf80 | 2010-02-05 02:26:30 +0000 | [diff] [blame] | 1186 | return RetrieveElement(lazyBindingStore, |
Ted Kremenek | 566a6fa | 2009-08-06 22:33:36 +0000 | [diff] [blame] | 1187 | cast<ElementRegion>(lazyBindingRegion)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1188 | |
Zhongxing Xu | bfcaf80 | 2010-02-05 02:26:30 +0000 | [diff] [blame] | 1189 | return RetrieveField(lazyBindingStore, |
Ted Kremenek | 566a6fa | 2009-08-06 22:33:36 +0000 | [diff] [blame] | 1190 | cast<FieldRegion>(lazyBindingRegion)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1191 | } |
| 1192 | |
Ted Kremenek | de0d263 | 2010-01-05 02:18:06 +0000 | [diff] [blame] | 1193 | if (R->hasStackNonParametersStorage()) { |
Ted Kremenek | 566a6fa | 2009-08-06 22:33:36 +0000 | [diff] [blame] | 1194 | if (isa<ElementRegion>(R)) { |
| 1195 | // Currently we don't reason specially about Clang-style vectors. Check |
| 1196 | // if superR is a vector and if so return Unknown. |
| 1197 | if (const TypedRegion *typedSuperR = dyn_cast<TypedRegion>(superR)) { |
| 1198 | if (typedSuperR->getValueType(getContext())->isVectorType()) |
| 1199 | return UnknownVal(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1200 | } |
Ted Kremenek | 566a6fa | 2009-08-06 22:33:36 +0000 | [diff] [blame] | 1201 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1202 | |
Zhongxing Xu | 490b0f0 | 2009-06-25 04:50:44 +0000 | [diff] [blame] | 1203 | return UndefinedVal(); |
Ted Kremenek | 566a6fa | 2009-08-06 22:33:36 +0000 | [diff] [blame] | 1204 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1205 | |
Ted Kremenek | bb2b433 | 2009-07-02 22:16:42 +0000 | [diff] [blame] | 1206 | // All other values are symbolic. |
Ted Kremenek | 28172a2 | 2009-12-15 23:23:27 +0000 | [diff] [blame] | 1207 | return ValMgr.getRegionValueSymbolVal(R, Ty); |
Zhongxing Xu | 490b0f0 | 2009-06-25 04:50:44 +0000 | [diff] [blame] | 1208 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1209 | |
Zhongxing Xu | 576bb92 | 2010-02-05 03:01:53 +0000 | [diff] [blame] | 1210 | SVal RegionStoreManager::RetrieveObjCIvar(Store store, const ObjCIvarRegion* R){ |
Ted Kremenek | 5bd2fe3 | 2009-07-15 06:09:28 +0000 | [diff] [blame] | 1211 | |
Ted Kremenek | 5bd2fe3 | 2009-07-15 06:09:28 +0000 | [diff] [blame] | 1212 | // Check if the region has a binding. |
Zhongxing Xu | 576bb92 | 2010-02-05 03:01:53 +0000 | [diff] [blame] | 1213 | RegionBindings B = GetRegionBindings(store); |
Ted Kremenek | 5bd2fe3 | 2009-07-15 06:09:28 +0000 | [diff] [blame] | 1214 | |
Zhongxing Xu | 13d5017 | 2009-10-11 08:08:02 +0000 | [diff] [blame] | 1215 | if (Optional<SVal> V = getDirectBinding(B, R)) |
Ted Kremenek | 5bd2fe3 | 2009-07-15 06:09:28 +0000 | [diff] [blame] | 1216 | return *V; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1217 | |
Ted Kremenek | 5bd2fe3 | 2009-07-15 06:09:28 +0000 | [diff] [blame] | 1218 | const MemRegion *superR = R->getSuperRegion(); |
| 1219 | |
Ted Kremenek | ab22ee9 | 2009-10-20 01:20:57 +0000 | [diff] [blame] | 1220 | // Check if the super region has a default binding. |
| 1221 | if (Optional<SVal> V = getDefaultBinding(B, superR)) { |
Ted Kremenek | 5bd2fe3 | 2009-07-15 06:09:28 +0000 | [diff] [blame] | 1222 | if (SymbolRef parentSym = V->getAsSymbol()) |
| 1223 | return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1224 | |
Ted Kremenek | 5bd2fe3 | 2009-07-15 06:09:28 +0000 | [diff] [blame] | 1225 | // Other cases: give up. |
| 1226 | return UnknownVal(); |
| 1227 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1228 | |
Zhongxing Xu | 576bb92 | 2010-02-05 03:01:53 +0000 | [diff] [blame] | 1229 | return RetrieveLazySymbol(R); |
Ted Kremenek | 25c5457 | 2009-07-20 22:58:02 +0000 | [diff] [blame] | 1230 | } |
| 1231 | |
Zhongxing Xu | 576bb92 | 2010-02-05 03:01:53 +0000 | [diff] [blame] | 1232 | SVal RegionStoreManager::RetrieveVar(Store store, const VarRegion *R) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1233 | |
Ted Kremenek | 9031dd7 | 2009-07-21 00:12:07 +0000 | [diff] [blame] | 1234 | // Check if the region has a binding. |
Zhongxing Xu | 576bb92 | 2010-02-05 03:01:53 +0000 | [diff] [blame] | 1235 | RegionBindings B = GetRegionBindings(store); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1236 | |
Zhongxing Xu | 13d5017 | 2009-10-11 08:08:02 +0000 | [diff] [blame] | 1237 | if (Optional<SVal> V = getDirectBinding(B, R)) |
Ted Kremenek | 9031dd7 | 2009-07-21 00:12:07 +0000 | [diff] [blame] | 1238 | return *V; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1239 | |
Ted Kremenek | 9031dd7 | 2009-07-21 00:12:07 +0000 | [diff] [blame] | 1240 | // Lazily derive a value for the VarRegion. |
| 1241 | const VarDecl *VD = R->getDecl(); |
Ted Kremenek | 4dc1566 | 2010-02-06 03:57:59 +0000 | [diff] [blame] | 1242 | QualType T = VD->getType(); |
| 1243 | const MemSpaceRegion *MS = R->getMemorySpace(); |
| 1244 | |
| 1245 | if (isa<UnknownSpaceRegion>(MS) || |
| 1246 | isa<StackArgumentsSpaceRegion>(MS)) |
| 1247 | return ValMgr.getRegionValueSymbolVal(R, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1248 | |
Ted Kremenek | 4dc1566 | 2010-02-06 03:57:59 +0000 | [diff] [blame] | 1249 | if (isa<GlobalsSpaceRegion>(MS)) { |
| 1250 | if (VD->isFileVarDecl()) |
| 1251 | return ValMgr.getRegionValueSymbolVal(R, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1252 | |
Ted Kremenek | 4dc1566 | 2010-02-06 03:57:59 +0000 | [diff] [blame] | 1253 | if (T->isIntegerType()) |
| 1254 | return ValMgr.makeIntVal(0, T); |
Ted Kremenek | 81861ab | 2010-02-06 04:04:46 +0000 | [diff] [blame] | 1255 | if (T->isPointerType()) |
| 1256 | return ValMgr.makeNull(); |
| 1257 | |
Ted Kremenek | 4dc1566 | 2010-02-06 03:57:59 +0000 | [diff] [blame] | 1258 | return UnknownVal(); |
| 1259 | } |
| 1260 | |
Ted Kremenek | 9031dd7 | 2009-07-21 00:12:07 +0000 | [diff] [blame] | 1261 | return UndefinedVal(); |
| 1262 | } |
| 1263 | |
Zhongxing Xu | 576bb92 | 2010-02-05 03:01:53 +0000 | [diff] [blame] | 1264 | SVal RegionStoreManager::RetrieveLazySymbol(const TypedRegion *R) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1265 | |
Ted Kremenek | 25c5457 | 2009-07-20 22:58:02 +0000 | [diff] [blame] | 1266 | QualType valTy = R->getValueType(getContext()); |
Ted Kremenek | 356e9d6 | 2009-07-22 04:35:42 +0000 | [diff] [blame] | 1267 | |
Ted Kremenek | 5bd2fe3 | 2009-07-15 06:09:28 +0000 | [diff] [blame] | 1268 | // All other values are symbolic. |
Ted Kremenek | 28172a2 | 2009-12-15 23:23:27 +0000 | [diff] [blame] | 1269 | return ValMgr.getRegionValueSymbolVal(R, valTy); |
Ted Kremenek | 5bd2fe3 | 2009-07-15 06:09:28 +0000 | [diff] [blame] | 1270 | } |
| 1271 | |
Zhongxing Xu | 576bb92 | 2010-02-05 03:01:53 +0000 | [diff] [blame] | 1272 | SVal RegionStoreManager::RetrieveStruct(Store store, const TypedRegion* R) { |
Zhongxing Xu | a82d8aa | 2009-05-09 03:57:34 +0000 | [diff] [blame] | 1273 | QualType T = R->getValueType(getContext()); |
Zhongxing Xu | 6e3f01c | 2008-10-31 07:16:08 +0000 | [diff] [blame] | 1274 | assert(T->isStructureType()); |
| 1275 | |
Zhongxing Xu | b7507d1 | 2009-06-11 07:27:30 +0000 | [diff] [blame] | 1276 | const RecordType* RT = T->getAsStructureType(); |
Zhongxing Xu | 6e3f01c | 2008-10-31 07:16:08 +0000 | [diff] [blame] | 1277 | RecordDecl* RD = RT->getDecl(); |
| 1278 | assert(RD->isDefinition()); |
Mike Stump | 1aeb247 | 2009-08-06 12:56:50 +0000 | [diff] [blame] | 1279 | (void)RD; |
Ted Kremenek | a5e81f1 | 2009-08-06 01:20:57 +0000 | [diff] [blame] | 1280 | #if USE_EXPLICIT_COMPOUND |
Zhongxing Xu | 6e3f01c | 2008-10-31 07:16:08 +0000 | [diff] [blame] | 1281 | llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList(); |
| 1282 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 1283 | // FIXME: We shouldn't use a std::vector. If RecordDecl doesn't have a |
| 1284 | // reverse iterator, we should implement one. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1285 | std::vector<FieldDecl *> Fields(RD->field_begin(), RD->field_end()); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1286 | |
Douglas Gregor | e267ff3 | 2008-12-11 20:41:00 +0000 | [diff] [blame] | 1287 | for (std::vector<FieldDecl *>::reverse_iterator Field = Fields.rbegin(), |
| 1288 | FieldEnd = Fields.rend(); |
| 1289 | Field != FieldEnd; ++Field) { |
| 1290 | FieldRegion* FR = MRMgr.getFieldRegion(*Field, R); |
Zhongxing Xu | 3e001f3 | 2009-05-03 00:27:40 +0000 | [diff] [blame] | 1291 | QualType FTy = (*Field)->getType(); |
Zhongxing Xu | 576bb92 | 2010-02-05 03:01:53 +0000 | [diff] [blame] | 1292 | SVal FieldValue = Retrieve(store, loc::MemRegionVal(FR), FTy).getSVal(); |
Zhongxing Xu | 6e3f01c | 2008-10-31 07:16:08 +0000 | [diff] [blame] | 1293 | StructVal = getBasicVals().consVals(FieldValue, StructVal); |
| 1294 | } |
| 1295 | |
Zhongxing Xu | d91ee27 | 2009-06-23 09:02:15 +0000 | [diff] [blame] | 1296 | return ValMgr.makeCompoundVal(T, StructVal); |
Ted Kremenek | a5e81f1 | 2009-08-06 01:20:57 +0000 | [diff] [blame] | 1297 | #else |
Zhongxing Xu | 576bb92 | 2010-02-05 03:01:53 +0000 | [diff] [blame] | 1298 | return ValMgr.makeLazyCompoundVal(store, R); |
Ted Kremenek | a5e81f1 | 2009-08-06 01:20:57 +0000 | [diff] [blame] | 1299 | #endif |
Zhongxing Xu | 6e3f01c | 2008-10-31 07:16:08 +0000 | [diff] [blame] | 1300 | } |
| 1301 | |
Zhongxing Xu | 576bb92 | 2010-02-05 03:01:53 +0000 | [diff] [blame] | 1302 | SVal RegionStoreManager::RetrieveArray(Store store, const TypedRegion * R) { |
Ted Kremenek | a5e81f1 | 2009-08-06 01:20:57 +0000 | [diff] [blame] | 1303 | #if USE_EXPLICIT_COMPOUND |
Zhongxing Xu | a82d8aa | 2009-05-09 03:57:34 +0000 | [diff] [blame] | 1304 | QualType T = R->getValueType(getContext()); |
Zhongxing Xu | 3e001f3 | 2009-05-03 00:27:40 +0000 | [diff] [blame] | 1305 | ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr()); |
| 1306 | |
| 1307 | llvm::ImmutableList<SVal> ArrayVal = getBasicVals().getEmptySValList(); |
Ted Kremenek | 4653739 | 2009-07-16 01:33:37 +0000 | [diff] [blame] | 1308 | uint64_t size = CAT->getSize().getZExtValue(); |
| 1309 | for (uint64_t i = 0; i < size; ++i) { |
| 1310 | SVal Idx = ValMgr.makeArrayIndex(i); |
Zhongxing Xu | 143b2fc | 2009-06-16 09:55:50 +0000 | [diff] [blame] | 1311 | ElementRegion* ER = MRMgr.getElementRegion(CAT->getElementType(), Idx, R, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1312 | getContext()); |
Ted Kremenek | f936f45 | 2009-05-04 06:18:28 +0000 | [diff] [blame] | 1313 | QualType ETy = ER->getElementType(); |
Zhongxing Xu | 576bb92 | 2010-02-05 03:01:53 +0000 | [diff] [blame] | 1314 | SVal ElementVal = Retrieve(store, loc::MemRegionVal(ER), ETy).getSVal(); |
Zhongxing Xu | 3e001f3 | 2009-05-03 00:27:40 +0000 | [diff] [blame] | 1315 | ArrayVal = getBasicVals().consVals(ElementVal, ArrayVal); |
| 1316 | } |
| 1317 | |
Zhongxing Xu | d91ee27 | 2009-06-23 09:02:15 +0000 | [diff] [blame] | 1318 | return ValMgr.makeCompoundVal(T, ArrayVal); |
Ted Kremenek | a5e81f1 | 2009-08-06 01:20:57 +0000 | [diff] [blame] | 1319 | #else |
| 1320 | assert(isa<ConstantArrayType>(R->getValueType(getContext()))); |
Zhongxing Xu | 576bb92 | 2010-02-05 03:01:53 +0000 | [diff] [blame] | 1321 | return ValMgr.makeLazyCompoundVal(store, R); |
Ted Kremenek | a5e81f1 | 2009-08-06 01:20:57 +0000 | [diff] [blame] | 1322 | #endif |
Zhongxing Xu | 3e001f3 | 2009-05-03 00:27:40 +0000 | [diff] [blame] | 1323 | } |
| 1324 | |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 1325 | //===----------------------------------------------------------------------===// |
| 1326 | // Binding values to regions. |
| 1327 | //===----------------------------------------------------------------------===// |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 1328 | |
Zhongxing Xu | 9c9ca08 | 2008-12-16 02:36:30 +0000 | [diff] [blame] | 1329 | Store RegionStoreManager::Remove(Store store, Loc L) { |
Ted Kremenek | 0964a06 | 2009-01-21 06:57:53 +0000 | [diff] [blame] | 1330 | if (isa<loc::MemRegionVal>(L)) |
Ted Kremenek | c50e6df | 2010-01-11 02:33:26 +0000 | [diff] [blame] | 1331 | if (const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion()) |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 1332 | return Remove(GetRegionBindings(store), R).getRoot(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1333 | |
Ted Kremenek | 0964a06 | 2009-01-21 06:57:53 +0000 | [diff] [blame] | 1334 | return store; |
Zhongxing Xu | 9c9ca08 | 2008-12-16 02:36:30 +0000 | [diff] [blame] | 1335 | } |
| 1336 | |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 1337 | Store RegionStoreManager::Bind(Store store, Loc L, SVal V) { |
Zhongxing Xu | 87453d1 | 2009-06-28 10:16:11 +0000 | [diff] [blame] | 1338 | if (isa<loc::ConcreteInt>(L)) |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 1339 | return store; |
Zhongxing Xu | 87453d1 | 2009-06-28 10:16:11 +0000 | [diff] [blame] | 1340 | |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 1341 | // If we get here, the location should be a region. |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 1342 | const MemRegion *R = cast<loc::MemRegionVal>(L).getRegion(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1343 | |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 1344 | // Check if the region is a struct region. |
| 1345 | if (const TypedRegion* TR = dyn_cast<TypedRegion>(R)) |
| 1346 | if (TR->getValueType(getContext())->isStructureType()) |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 1347 | return BindStruct(store, TR, V); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1348 | |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 1349 | // Special case: the current region represents a cast and it and the super |
| 1350 | // region both have pointer types or intptr_t types. If so, perform the |
| 1351 | // bind to the super region. |
| 1352 | // This is needed to support OSAtomicCompareAndSwap and friends or other |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1353 | // loads that treat integers as pointers and vis versa. |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 1354 | if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) { |
| 1355 | if (ER->getIndex().isZeroConstant()) { |
| 1356 | if (const TypedRegion *superR = |
| 1357 | dyn_cast<TypedRegion>(ER->getSuperRegion())) { |
| 1358 | ASTContext &Ctx = getContext(); |
| 1359 | QualType superTy = superR->getValueType(Ctx); |
| 1360 | QualType erTy = ER->getValueType(Ctx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1361 | |
| 1362 | if (IsAnyPointerOrIntptr(superTy, Ctx) && |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 1363 | IsAnyPointerOrIntptr(erTy, Ctx)) { |
Zhongxing Xu | 814e6b9 | 2010-02-04 04:56:43 +0000 | [diff] [blame] | 1364 | V = ValMgr.getSValuator().EvalCast(V, superTy, erTy); |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 1365 | return Bind(store, loc::MemRegionVal(superR), V); |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 1366 | } |
Ted Kremenek | 69181a8 | 2009-09-21 22:58:52 +0000 | [diff] [blame] | 1367 | // For now, just invalidate the fields of the struct/union/class. |
| 1368 | // FIXME: Precisely handle the fields of the record. |
| 1369 | if (superTy->isRecordType()) |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 1370 | return InvalidateRegion(store, superR, NULL, 0, NULL); |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 1371 | } |
| 1372 | } |
| 1373 | } |
Ted Kremenek | 0954cde | 2009-09-24 04:11:44 +0000 | [diff] [blame] | 1374 | else if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) { |
| 1375 | // Binding directly to a symbolic region should be treated as binding |
| 1376 | // to element 0. |
| 1377 | QualType T = SR->getSymbol()->getType(getContext()); |
Ted Kremenek | 852274d | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 1378 | |
| 1379 | // FIXME: Is this the right way to handle symbols that are references? |
| 1380 | if (const PointerType *PT = T->getAs<PointerType>()) |
| 1381 | T = PT->getPointeeType(); |
| 1382 | else |
| 1383 | T = T->getAs<ReferenceType>()->getPointeeType(); |
| 1384 | |
Ted Kremenek | 0954cde | 2009-09-24 04:11:44 +0000 | [diff] [blame] | 1385 | R = GetElementZeroRegion(SR, T); |
| 1386 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1387 | |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 1388 | // Perform the binding. |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 1389 | RegionBindings B = GetRegionBindings(store); |
| 1390 | return Add(B, R, BindingKey::Direct, V).getRoot(); |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 1391 | } |
| 1392 | |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 1393 | Store RegionStoreManager::BindDecl(Store store, const VarRegion *VR, |
| 1394 | SVal InitVal) { |
Zhongxing Xu | a4f28ff | 2008-11-13 08:41:36 +0000 | [diff] [blame] | 1395 | |
Ted Kremenek | f6f56d4 | 2009-11-04 00:09:15 +0000 | [diff] [blame] | 1396 | QualType T = VR->getDecl()->getType(); |
Zhongxing Xu | f0dfa8d | 2008-10-31 08:10:01 +0000 | [diff] [blame] | 1397 | |
Ted Kremenek | 0964a06 | 2009-01-21 06:57:53 +0000 | [diff] [blame] | 1398 | if (T->isArrayType()) |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 1399 | return BindArray(store, VR, InitVal); |
Ted Kremenek | 0964a06 | 2009-01-21 06:57:53 +0000 | [diff] [blame] | 1400 | if (T->isStructureType()) |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 1401 | return BindStruct(store, VR, InitVal); |
Zhongxing Xu | d463d44 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 1402 | |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 1403 | return Bind(store, ValMgr.makeLoc(VR), InitVal); |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 1404 | } |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 1405 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1406 | // FIXME: this method should be merged into Bind(). |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 1407 | Store RegionStoreManager::BindCompoundLiteral(Store store, |
| 1408 | const CompoundLiteralExpr *CL, |
| 1409 | const LocationContext *LC, |
| 1410 | SVal V) { |
| 1411 | return Bind(store, loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL, LC)), |
Ted Kremenek | 67d1287 | 2009-12-07 22:05:27 +0000 | [diff] [blame] | 1412 | V); |
Zhongxing Xu | f22679e | 2008-11-07 10:38:33 +0000 | [diff] [blame] | 1413 | } |
| 1414 | |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 1415 | Store RegionStoreManager::setImplicitDefaultValue(Store store, |
| 1416 | const MemRegion *R, |
| 1417 | QualType T) { |
Ted Kremenek | 027e266 | 2009-11-19 20:20:24 +0000 | [diff] [blame] | 1418 | RegionBindings B = GetRegionBindings(store); |
| 1419 | SVal V; |
| 1420 | |
| 1421 | if (Loc::IsLocType(T)) |
| 1422 | V = ValMgr.makeNull(); |
| 1423 | else if (T->isIntegerType()) |
| 1424 | V = ValMgr.makeZeroVal(T); |
| 1425 | else if (T->isStructureType() || T->isArrayType()) { |
| 1426 | // Set the default value to a zero constant when it is a structure |
| 1427 | // or array. The type doesn't really matter. |
| 1428 | V = ValMgr.makeZeroVal(ValMgr.getContext().IntTy); |
| 1429 | } |
| 1430 | else { |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 1431 | return store; |
Ted Kremenek | 027e266 | 2009-11-19 20:20:24 +0000 | [diff] [blame] | 1432 | } |
Ted Kremenek | 1c1ae6b | 2010-01-11 00:07:44 +0000 | [diff] [blame] | 1433 | |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 1434 | return Add(B, R, BindingKey::Default, V).getRoot(); |
Ted Kremenek | 027e266 | 2009-11-19 20:20:24 +0000 | [diff] [blame] | 1435 | } |
| 1436 | |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 1437 | Store RegionStoreManager::BindArray(Store store, const TypedRegion* R, |
| 1438 | SVal Init) { |
Ted Kremenek | fee9081 | 2010-01-26 23:51:00 +0000 | [diff] [blame] | 1439 | |
| 1440 | ASTContext &Ctx = getContext(); |
| 1441 | const ArrayType *AT = |
| 1442 | cast<ArrayType>(Ctx.getCanonicalType(R->getValueType(Ctx))); |
| 1443 | QualType ElementTy = AT->getElementType(); |
| 1444 | Optional<uint64_t> Size; |
| 1445 | |
| 1446 | if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(AT)) |
| 1447 | Size = CAT->getSize().getZExtValue(); |
| 1448 | |
Zhongxing Xu | 6987c7b | 2008-11-30 05:49:49 +0000 | [diff] [blame] | 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 | fee9081 | 2010-01-26 23:51:00 +0000 | [diff] [blame] | 1460 | |
| 1461 | // We assume that string constants are bound to |
| 1462 | // constant arrays. |
Ted Kremenek | 39c2ea1 | 2010-01-27 16:31:37 +0000 | [diff] [blame] | 1463 | uint64_t size = Size.getValue(); |
Ted Kremenek | fee9081 | 2010-01-26 23:51:00 +0000 | [diff] [blame] | 1464 | |
Ted Kremenek | 4653739 | 2009-07-16 01:33:37 +0000 | [diff] [blame] | 1465 | for (uint64_t i = 0; i < size; ++i, ++j) { |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1466 | if (j >= len) |
| 1467 | break; |
| 1468 | |
Ted Kremenek | 4653739 | 2009-07-16 01:33:37 +0000 | [diff] [blame] | 1469 | SVal Idx = ValMgr.makeArrayIndex(i); |
Ted Kremenek | b48ad64 | 2009-12-04 00:26:31 +0000 | [diff] [blame] | 1470 | const ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R, |
| 1471 | getContext()); |
Zhongxing Xu | 6987c7b | 2008-11-30 05:49:49 +0000 | [diff] [blame] | 1472 | |
Zhongxing Xu | d91ee27 | 2009-06-23 09:02:15 +0000 | [diff] [blame] | 1473 | SVal V = ValMgr.makeIntVal(str[j], sizeof(char)*8, true); |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 1474 | store = Bind(store, loc::MemRegionVal(ER), V); |
Zhongxing Xu | 6987c7b | 2008-11-30 05:49:49 +0000 | [diff] [blame] | 1475 | } |
| 1476 | |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 1477 | return store; |
Zhongxing Xu | 6987c7b | 2008-11-30 05:49:49 +0000 | [diff] [blame] | 1478 | } |
| 1479 | |
Ted Kremenek | a5e81f1 | 2009-08-06 01:20:57 +0000 | [diff] [blame] | 1480 | // Handle lazy compound values. |
| 1481 | if (nonloc::LazyCompoundVal *LCV = dyn_cast<nonloc::LazyCompoundVal>(&Init)) |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 1482 | return CopyLazyBindings(*LCV, store, R); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1483 | |
| 1484 | // Remaining case: explicit compound values. |
Ted Kremenek | 027e266 | 2009-11-19 20:20:24 +0000 | [diff] [blame] | 1485 | |
| 1486 | if (Init.isUnknown()) |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 1487 | return setImplicitDefaultValue(store, R, ElementTy); |
Ted Kremenek | 027e266 | 2009-11-19 20:20:24 +0000 | [diff] [blame] | 1488 | |
Zhongxing Xu | 1a12a0e | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 1489 | nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init); |
Zhongxing Xu | 1a12a0e | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 1490 | nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end(); |
Ted Kremenek | 4653739 | 2009-07-16 01:33:37 +0000 | [diff] [blame] | 1491 | uint64_t i = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1492 | |
Ted Kremenek | fee9081 | 2010-01-26 23:51:00 +0000 | [diff] [blame] | 1493 | for (; Size.hasValue() ? i < Size.getValue() : true ; ++i, ++VI) { |
Zhongxing Xu | 087d6c2 | 2009-06-23 05:23:38 +0000 | [diff] [blame] | 1494 | // The init list might be shorter than the array length. |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1495 | if (VI == VE) |
| 1496 | break; |
| 1497 | |
Ted Kremenek | 4653739 | 2009-07-16 01:33:37 +0000 | [diff] [blame] | 1498 | SVal Idx = ValMgr.makeArrayIndex(i); |
Ted Kremenek | b48ad64 | 2009-12-04 00:26:31 +0000 | [diff] [blame] | 1499 | const ElementRegion *ER = MRMgr.getElementRegion(ElementTy, Idx, R, getContext()); |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1500 | |
Ted Kremenek | fee9081 | 2010-01-26 23:51:00 +0000 | [diff] [blame] | 1501 | if (ElementTy->isStructureType()) |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 1502 | store = BindStruct(store, ER, *VI); |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1503 | else |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 1504 | store = Bind(store, ValMgr.makeLoc(ER), *VI); |
Zhongxing Xu | 1a12a0e | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 1505 | } |
| 1506 | |
Ted Kremenek | 027e266 | 2009-11-19 20:20:24 +0000 | [diff] [blame] | 1507 | // If the init list is shorter than the array length, set the |
| 1508 | // array default value. |
Ted Kremenek | fee9081 | 2010-01-26 23:51:00 +0000 | [diff] [blame] | 1509 | if (Size.hasValue() && i < Size.getValue()) |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 1510 | store = setImplicitDefaultValue(store, R, ElementTy); |
Zhongxing Xu | 087d6c2 | 2009-06-23 05:23:38 +0000 | [diff] [blame] | 1511 | |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 1512 | return store; |
Zhongxing Xu | 1a12a0e | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 1513 | } |
| 1514 | |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 1515 | Store RegionStoreManager::BindStruct(Store store, const TypedRegion* R, |
| 1516 | SVal V) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1517 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 1518 | if (!Features.supportsFields()) |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 1519 | return store; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1520 | |
Zhongxing Xu | a82d8aa | 2009-05-09 03:57:34 +0000 | [diff] [blame] | 1521 | QualType T = R->getValueType(getContext()); |
Zhongxing Xu | af0a844 | 2008-10-31 10:53:01 +0000 | [diff] [blame] | 1522 | assert(T->isStructureType()); |
| 1523 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1524 | const RecordType* RT = T->getAs<RecordType>(); |
Zhongxing Xu | af0a844 | 2008-10-31 10:53:01 +0000 | [diff] [blame] | 1525 | RecordDecl* RD = RT->getDecl(); |
Zhongxing Xu | c45a825 | 2009-03-11 09:07:35 +0000 | [diff] [blame] | 1526 | |
| 1527 | if (!RD->isDefinition()) |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 1528 | return store; |
Zhongxing Xu | af0a844 | 2008-10-31 10:53:01 +0000 | [diff] [blame] | 1529 | |
Ted Kremenek | a5e81f1 | 2009-08-06 01:20:57 +0000 | [diff] [blame] | 1530 | // Handle lazy compound values. |
Zhongxing Xu | 13d5017 | 2009-10-11 08:08:02 +0000 | [diff] [blame] | 1531 | if (const nonloc::LazyCompoundVal *LCV=dyn_cast<nonloc::LazyCompoundVal>(&V)) |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 1532 | return CopyLazyBindings(*LCV, store, R); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1533 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 1534 | // We may get non-CompoundVal accidentally due to imprecise cast logic. |
| 1535 | // Ignore them and kill the field values. |
| 1536 | if (V.isUnknown() || !isa<nonloc::CompoundVal>(V)) |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 1537 | return KillStruct(store, R); |
Zhongxing Xu | 3f6978a | 2009-06-11 09:11:27 +0000 | [diff] [blame] | 1538 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1539 | nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V); |
Zhongxing Xu | af0a844 | 2008-10-31 10:53:01 +0000 | [diff] [blame] | 1540 | nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end(); |
Zhongxing Xu | dbdf219 | 2009-06-23 05:43:16 +0000 | [diff] [blame] | 1541 | |
| 1542 | RecordDecl::field_iterator FI, FE; |
| 1543 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1544 | 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] | 1545 | |
Zhongxing Xu | dbdf219 | 2009-06-23 05:43:16 +0000 | [diff] [blame] | 1546 | if (VI == VE) |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1547 | break; |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1548 | |
Zhongxing Xu | af0a844 | 2008-10-31 10:53:01 +0000 | [diff] [blame] | 1549 | QualType FTy = (*FI)->getType(); |
Ted Kremenek | cf54959 | 2009-09-22 21:19:14 +0000 | [diff] [blame] | 1550 | const FieldRegion* FR = MRMgr.getFieldRegion(*FI, R); |
Zhongxing Xu | af0a844 | 2008-10-31 10:53:01 +0000 | [diff] [blame] | 1551 | |
Ted Kremenek | cf54959 | 2009-09-22 21:19:14 +0000 | [diff] [blame] | 1552 | if (FTy->isArrayType()) |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 1553 | store = BindArray(store, FR, *VI); |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1554 | else if (FTy->isStructureType()) |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 1555 | store = BindStruct(store, FR, *VI); |
Ted Kremenek | cf54959 | 2009-09-22 21:19:14 +0000 | [diff] [blame] | 1556 | else |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 1557 | store = Bind(store, ValMgr.makeLoc(FR), *VI); |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 1558 | } |
| 1559 | |
Zhongxing Xu | dbdf219 | 2009-06-23 05:43:16 +0000 | [diff] [blame] | 1560 | // There may be fewer values in the initialize list than the fields of struct. |
Zhongxing Xu | 13d5017 | 2009-10-11 08:08:02 +0000 | [diff] [blame] | 1561 | if (FI != FE) { |
Zhongxing Xu | 13d5017 | 2009-10-11 08:08:02 +0000 | [diff] [blame] | 1562 | RegionBindings B = GetRegionBindings(store); |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 1563 | B = Add(B, R, BindingKey::Default, ValMgr.makeIntVal(0, false)); |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 1564 | store = B.getRoot(); |
Zhongxing Xu | 13d5017 | 2009-10-11 08:08:02 +0000 | [diff] [blame] | 1565 | } |
Zhongxing Xu | dbdf219 | 2009-06-23 05:43:16 +0000 | [diff] [blame] | 1566 | |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 1567 | return store; |
Zhongxing Xu | c3a0599 | 2008-11-19 11:06:24 +0000 | [diff] [blame] | 1568 | } |
| 1569 | |
Zhongxing Xu | 13d5017 | 2009-10-11 08:08:02 +0000 | [diff] [blame] | 1570 | Store RegionStoreManager::KillStruct(Store store, const TypedRegion* R) { |
| 1571 | RegionBindings B = GetRegionBindings(store); |
| 1572 | llvm::OwningPtr<RegionStoreSubRegionMap> |
| 1573 | SubRegions(getRegionStoreSubRegionMap(store)); |
| 1574 | RemoveSubRegionBindings(B, R, *SubRegions); |
Zhongxing Xu | 5834ed6 | 2009-01-13 01:49:57 +0000 | [diff] [blame] | 1575 | |
Zhongxing Xu | e4df9c4 | 2009-06-25 05:52:16 +0000 | [diff] [blame] | 1576 | // Set the default value of the struct region to "unknown". |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 1577 | return Add(B, R, BindingKey::Default, UnknownVal()).getRoot(); |
Zhongxing Xu | 5834ed6 | 2009-01-13 01:49:57 +0000 | [diff] [blame] | 1578 | } |
| 1579 | |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 1580 | Store RegionStoreManager::CopyLazyBindings(nonloc::LazyCompoundVal V, |
| 1581 | Store store, const TypedRegion *R) { |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 1582 | |
Ted Kremenek | a5e81f1 | 2009-08-06 01:20:57 +0000 | [diff] [blame] | 1583 | // Nuke the old bindings stemming from R. |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 1584 | RegionBindings B = GetRegionBindings(store); |
Ted Kremenek | a5e81f1 | 2009-08-06 01:20:57 +0000 | [diff] [blame] | 1585 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1586 | llvm::OwningPtr<RegionStoreSubRegionMap> |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 1587 | SubRegions(getRegionStoreSubRegionMap(store)); |
Ted Kremenek | a5e81f1 | 2009-08-06 01:20:57 +0000 | [diff] [blame] | 1588 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1589 | // B and DVM are updated after the call to RemoveSubRegionBindings. |
Zhongxing Xu | 13d5017 | 2009-10-11 08:08:02 +0000 | [diff] [blame] | 1590 | RemoveSubRegionBindings(B, R, *SubRegions.get()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1591 | |
Ted Kremenek | a5e81f1 | 2009-08-06 01:20:57 +0000 | [diff] [blame] | 1592 | // Now copy the bindings. This amounts to just binding 'V' to 'R'. This |
| 1593 | // results in a zero-copy algorithm. |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 1594 | return Add(B, R, BindingKey::Direct, V).getRoot(); |
Ted Kremenek | 1c1ae6b | 2010-01-11 00:07:44 +0000 | [diff] [blame] | 1595 | } |
| 1596 | |
| 1597 | //===----------------------------------------------------------------------===// |
| 1598 | // "Raw" retrievals and bindings. |
| 1599 | //===----------------------------------------------------------------------===// |
| 1600 | |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 1601 | BindingKey BindingKey::Make(const MemRegion *R, Kind k) { |
Ted Kremenek | c50e6df | 2010-01-11 02:33:26 +0000 | [diff] [blame] | 1602 | if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) { |
| 1603 | const RegionRawOffset &O = ER->getAsRawOffset(); |
| 1604 | |
| 1605 | if (O.getRegion()) |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 1606 | return BindingKey(O.getRegion(), O.getByteOffset(), k); |
Ted Kremenek | c50e6df | 2010-01-11 02:33:26 +0000 | [diff] [blame] | 1607 | |
| 1608 | // FIXME: There are some ElementRegions for which we cannot compute |
| 1609 | // raw offsets yet, including regions with symbolic offsets. |
| 1610 | } |
| 1611 | |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 1612 | return BindingKey(R, 0, k); |
Ted Kremenek | c50e6df | 2010-01-11 02:33:26 +0000 | [diff] [blame] | 1613 | } |
| 1614 | |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 1615 | RegionBindings RegionStoreManager::Add(RegionBindings B, BindingKey K, SVal V) { |
Ted Kremenek | 1c1ae6b | 2010-01-11 00:07:44 +0000 | [diff] [blame] | 1616 | return RBFactory.Add(B, K, V); |
| 1617 | } |
| 1618 | |
| 1619 | RegionBindings RegionStoreManager::Add(RegionBindings B, const MemRegion *R, |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 1620 | BindingKey::Kind k, SVal V) { |
| 1621 | return Add(B, BindingKey::Make(R, k), V); |
Ted Kremenek | 1c1ae6b | 2010-01-11 00:07:44 +0000 | [diff] [blame] | 1622 | } |
| 1623 | |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 1624 | const SVal *RegionStoreManager::Lookup(RegionBindings B, BindingKey K) { |
Ted Kremenek | 1c1ae6b | 2010-01-11 00:07:44 +0000 | [diff] [blame] | 1625 | return B.lookup(K); |
| 1626 | } |
| 1627 | |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 1628 | const SVal *RegionStoreManager::Lookup(RegionBindings B, |
| 1629 | const MemRegion *R, |
| 1630 | BindingKey::Kind k) { |
| 1631 | return Lookup(B, BindingKey::Make(R, k)); |
Ted Kremenek | 1c1ae6b | 2010-01-11 00:07:44 +0000 | [diff] [blame] | 1632 | } |
| 1633 | |
| 1634 | RegionBindings RegionStoreManager::Remove(RegionBindings B, BindingKey K) { |
| 1635 | return RBFactory.Remove(B, K); |
| 1636 | } |
| 1637 | |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 1638 | RegionBindings RegionStoreManager::Remove(RegionBindings B, const MemRegion *R, |
| 1639 | BindingKey::Kind k){ |
| 1640 | return Remove(B, BindingKey::Make(R, k)); |
Ted Kremenek | 1c1ae6b | 2010-01-11 00:07:44 +0000 | [diff] [blame] | 1641 | } |
| 1642 | |
| 1643 | Store RegionStoreManager::Remove(Store store, BindingKey K) { |
| 1644 | RegionBindings B = GetRegionBindings(store); |
| 1645 | return Remove(B, K).getRoot(); |
Ted Kremenek | a5e81f1 | 2009-08-06 01:20:57 +0000 | [diff] [blame] | 1646 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1647 | |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 1648 | //===----------------------------------------------------------------------===// |
| 1649 | // State pruning. |
| 1650 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 9e17cc6 | 2009-09-29 06:35:00 +0000 | [diff] [blame] | 1651 | |
Zhongxing Xu | 72119c4 | 2010-02-05 05:34:29 +0000 | [diff] [blame] | 1652 | Store RegionStoreManager::RemoveDeadBindings(Store store, Stmt* Loc, |
| 1653 | SymbolReaper& SymReaper, |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 1654 | llvm::SmallVectorImpl<const MemRegion*>& RegionRoots) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1655 | { |
Zhongxing Xu | bfcaf80 | 2010-02-05 02:26:30 +0000 | [diff] [blame] | 1656 | typedef std::pair<Store, const MemRegion *> RBDNode; |
Ted Kremenek | 781115c | 2009-10-17 17:45:11 +0000 | [diff] [blame] | 1657 | |
Ted Kremenek | 451ac09 | 2009-08-06 04:50:20 +0000 | [diff] [blame] | 1658 | RegionBindings B = GetRegionBindings(store); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1659 | |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 1660 | // The backmap from regions to subregions. |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 1661 | llvm::OwningPtr<RegionStoreSubRegionMap> |
Zhongxing Xu | 13d5017 | 2009-10-11 08:08:02 +0000 | [diff] [blame] | 1662 | SubRegions(getRegionStoreSubRegionMap(store)); |
Ted Kremenek | 9e17cc6 | 2009-09-29 06:35:00 +0000 | [diff] [blame] | 1663 | |
Ted Kremenek | a6d73af | 2009-11-26 02:35:42 +0000 | [diff] [blame] | 1664 | // Do a pass over the regions in the store. For VarRegions we check if |
| 1665 | // the variable is still live and if so add it to the list of live roots. |
| 1666 | // For other regions we populate our region backmap. |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 1667 | llvm::SmallVector<const MemRegion*, 10> IntermediateRoots; |
Ted Kremenek | 9e17cc6 | 2009-09-29 06:35:00 +0000 | [diff] [blame] | 1668 | |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 1669 | // Scan the direct bindings for "intermediate" roots. |
Ted Kremenek | 451ac09 | 2009-08-06 04:50:20 +0000 | [diff] [blame] | 1670 | for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) { |
Ted Kremenek | 1c1ae6b | 2010-01-11 00:07:44 +0000 | [diff] [blame] | 1671 | const MemRegion *R = I.getKey().getRegion(); |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 1672 | IntermediateRoots.push_back(R); |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 1673 | } |
Ted Kremenek | 9e17cc6 | 2009-09-29 06:35:00 +0000 | [diff] [blame] | 1674 | |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 1675 | // Process the "intermediate" roots to find if they are referenced by |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1676 | // real roots. |
Zhongxing Xu | f77869f | 2009-10-17 08:39:24 +0000 | [diff] [blame] | 1677 | llvm::SmallVector<RBDNode, 10> WorkList; |
Ted Kremenek | 0175619 | 2009-10-29 05:14:17 +0000 | [diff] [blame] | 1678 | llvm::SmallVector<RBDNode, 10> Postponed; |
| 1679 | |
Zhongxing Xu | 6800b33 | 2009-10-18 04:15:47 +0000 | [diff] [blame] | 1680 | llvm::DenseSet<const MemRegion*> IntermediateVisited; |
Ted Kremenek | 9e17cc6 | 2009-09-29 06:35:00 +0000 | [diff] [blame] | 1681 | |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 1682 | while (!IntermediateRoots.empty()) { |
| 1683 | const MemRegion* R = IntermediateRoots.back(); |
| 1684 | IntermediateRoots.pop_back(); |
Ted Kremenek | 9e17cc6 | 2009-09-29 06:35:00 +0000 | [diff] [blame] | 1685 | |
Zhongxing Xu | f77869f | 2009-10-17 08:39:24 +0000 | [diff] [blame] | 1686 | if (IntermediateVisited.count(R)) |
Ted Kremenek | 9e17cc6 | 2009-09-29 06:35:00 +0000 | [diff] [blame] | 1687 | continue; |
Zhongxing Xu | f77869f | 2009-10-17 08:39:24 +0000 | [diff] [blame] | 1688 | IntermediateVisited.insert(R); |
Ted Kremenek | 9e17cc6 | 2009-09-29 06:35:00 +0000 | [diff] [blame] | 1689 | |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 1690 | if (const VarRegion* VR = dyn_cast<VarRegion>(R)) { |
Ted Kremenek | edeb5b6 | 2009-12-04 20:32:20 +0000 | [diff] [blame] | 1691 | if (SymReaper.isLive(Loc, VR)) |
Zhongxing Xu | bfcaf80 | 2010-02-05 02:26:30 +0000 | [diff] [blame] | 1692 | WorkList.push_back(std::make_pair(store, VR)); |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 1693 | continue; |
| 1694 | } |
Ted Kremenek | 9e17cc6 | 2009-09-29 06:35:00 +0000 | [diff] [blame] | 1695 | |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 1696 | if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(R)) { |
Ted Kremenek | 0175619 | 2009-10-29 05:14:17 +0000 | [diff] [blame] | 1697 | llvm::SmallVectorImpl<RBDNode> &Q = |
| 1698 | SymReaper.isLive(SR->getSymbol()) ? WorkList : Postponed; |
| 1699 | |
Zhongxing Xu | bfcaf80 | 2010-02-05 02:26:30 +0000 | [diff] [blame] | 1700 | Q.push_back(std::make_pair(store, SR)); |
Ted Kremenek | 0175619 | 2009-10-29 05:14:17 +0000 | [diff] [blame] | 1701 | |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 1702 | continue; |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 1703 | } |
Ted Kremenek | 9e17cc6 | 2009-09-29 06:35:00 +0000 | [diff] [blame] | 1704 | |
| 1705 | // Add the super region for R to the worklist if it is a subregion. |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 1706 | if (const SubRegion* superR = |
Ted Kremenek | 9e17cc6 | 2009-09-29 06:35:00 +0000 | [diff] [blame] | 1707 | dyn_cast<SubRegion>(cast<SubRegion>(R)->getSuperRegion())) |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 1708 | IntermediateRoots.push_back(superR); |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 1709 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1710 | |
Ted Kremenek | 9e17cc6 | 2009-09-29 06:35:00 +0000 | [diff] [blame] | 1711 | // Enqueue the RegionRoots onto WorkList. |
| 1712 | for (llvm::SmallVectorImpl<const MemRegion*>::iterator I=RegionRoots.begin(), |
| 1713 | E=RegionRoots.end(); I!=E; ++I) { |
Zhongxing Xu | bfcaf80 | 2010-02-05 02:26:30 +0000 | [diff] [blame] | 1714 | WorkList.push_back(std::make_pair(store, *I)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1715 | } |
Ted Kremenek | 9e17cc6 | 2009-09-29 06:35:00 +0000 | [diff] [blame] | 1716 | RegionRoots.clear(); |
| 1717 | |
Zhongxing Xu | 6800b33 | 2009-10-18 04:15:47 +0000 | [diff] [blame] | 1718 | llvm::DenseSet<RBDNode> Visited; |
Ted Kremenek | 9e17cc6 | 2009-09-29 06:35:00 +0000 | [diff] [blame] | 1719 | |
Ted Kremenek | 0175619 | 2009-10-29 05:14:17 +0000 | [diff] [blame] | 1720 | tryAgain: |
Ted Kremenek | 9e17cc6 | 2009-09-29 06:35:00 +0000 | [diff] [blame] | 1721 | while (!WorkList.empty()) { |
Zhongxing Xu | f77869f | 2009-10-17 08:39:24 +0000 | [diff] [blame] | 1722 | RBDNode N = WorkList.back(); |
Ted Kremenek | 9e17cc6 | 2009-09-29 06:35:00 +0000 | [diff] [blame] | 1723 | WorkList.pop_back(); |
| 1724 | |
| 1725 | // Have we visited this node before? |
Zhongxing Xu | f77869f | 2009-10-17 08:39:24 +0000 | [diff] [blame] | 1726 | if (Visited.count(N)) |
Ted Kremenek | 9e17cc6 | 2009-09-29 06:35:00 +0000 | [diff] [blame] | 1727 | continue; |
Zhongxing Xu | f77869f | 2009-10-17 08:39:24 +0000 | [diff] [blame] | 1728 | Visited.insert(N); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1729 | |
Zhongxing Xu | f77869f | 2009-10-17 08:39:24 +0000 | [diff] [blame] | 1730 | const MemRegion *R = N.second; |
Zhongxing Xu | bfcaf80 | 2010-02-05 02:26:30 +0000 | [diff] [blame] | 1731 | Store store_N = N.first; |
Ted Kremenek | 9e17cc6 | 2009-09-29 06:35:00 +0000 | [diff] [blame] | 1732 | |
Zhongxing Xu | f77869f | 2009-10-17 08:39:24 +0000 | [diff] [blame] | 1733 | // Enqueue subregions. |
| 1734 | RegionStoreSubRegionMap *M; |
| 1735 | |
Zhongxing Xu | bfcaf80 | 2010-02-05 02:26:30 +0000 | [diff] [blame] | 1736 | if (store == store_N) |
Zhongxing Xu | f77869f | 2009-10-17 08:39:24 +0000 | [diff] [blame] | 1737 | M = SubRegions.get(); |
| 1738 | else { |
Zhongxing Xu | bfcaf80 | 2010-02-05 02:26:30 +0000 | [diff] [blame] | 1739 | RegionStoreSubRegionMap *& SM = SC[store_N]; |
Zhongxing Xu | f77869f | 2009-10-17 08:39:24 +0000 | [diff] [blame] | 1740 | if (!SM) |
Zhongxing Xu | bfcaf80 | 2010-02-05 02:26:30 +0000 | [diff] [blame] | 1741 | SM = getRegionStoreSubRegionMap(store_N); |
Zhongxing Xu | f77869f | 2009-10-17 08:39:24 +0000 | [diff] [blame] | 1742 | M = SM; |
| 1743 | } |
Ted Kremenek | df16501 | 2010-02-02 22:38:47 +0000 | [diff] [blame] | 1744 | |
| 1745 | if (const RegionStoreSubRegionMap::Set *S = M->getSubRegions(R)) |
| 1746 | for (RegionStoreSubRegionMap::Set::iterator I = S->begin(), E = S->end(); |
| 1747 | I != E; ++I) |
Zhongxing Xu | bfcaf80 | 2010-02-05 02:26:30 +0000 | [diff] [blame] | 1748 | WorkList.push_back(std::make_pair(store_N, *I)); |
Zhongxing Xu | f77869f | 2009-10-17 08:39:24 +0000 | [diff] [blame] | 1749 | |
Ted Kremenek | 9e17cc6 | 2009-09-29 06:35:00 +0000 | [diff] [blame] | 1750 | // Enqueue the super region. |
| 1751 | if (const SubRegion *SR = dyn_cast<SubRegion>(R)) { |
| 1752 | const MemRegion *superR = SR->getSuperRegion(); |
| 1753 | if (!isa<MemSpaceRegion>(superR)) { |
| 1754 | // If 'R' is a field or an element, we want to keep the bindings |
| 1755 | // for the other fields and elements around. The reason is that |
Zhongxing Xu | 13d5017 | 2009-10-11 08:08:02 +0000 | [diff] [blame] | 1756 | // pointer arithmetic can get us to the other fields or elements. |
Zhongxing Xu | 8801beb | 2009-10-17 07:32:08 +0000 | [diff] [blame] | 1757 | assert(isa<FieldRegion>(R) || isa<ElementRegion>(R) |
| 1758 | || isa<ObjCIvarRegion>(R)); |
Zhongxing Xu | bfcaf80 | 2010-02-05 02:26:30 +0000 | [diff] [blame] | 1759 | WorkList.push_back(std::make_pair(store_N, superR)); |
Ted Kremenek | 9e17cc6 | 2009-09-29 06:35:00 +0000 | [diff] [blame] | 1760 | } |
| 1761 | } |
| 1762 | |
| 1763 | // Mark the symbol for any live SymbolicRegion as "live". This means we |
| 1764 | // should continue to track that symbol. |
Ted Kremenek | a6d73af | 2009-11-26 02:35:42 +0000 | [diff] [blame] | 1765 | if (const SymbolicRegion *SymR = dyn_cast<SymbolicRegion>(R)) |
Ted Kremenek | 9e17cc6 | 2009-09-29 06:35:00 +0000 | [diff] [blame] | 1766 | SymReaper.markLive(SymR->getSymbol()); |
Ted Kremenek | a6d73af | 2009-11-26 02:35:42 +0000 | [diff] [blame] | 1767 | |
Ted Kremenek | a7a8dfd | 2009-12-03 17:48:05 +0000 | [diff] [blame] | 1768 | // For BlockDataRegions, enqueue the VarRegions for variables marked |
| 1769 | // with __block (passed-by-reference). |
Ted Kremenek | a6d73af | 2009-11-26 02:35:42 +0000 | [diff] [blame] | 1770 | // via BlockDeclRefExprs. |
| 1771 | if (const BlockDataRegion *BD = dyn_cast<BlockDataRegion>(R)) { |
| 1772 | for (BlockDataRegion::referenced_vars_iterator |
| 1773 | RI = BD->referenced_vars_begin(), RE = BD->referenced_vars_end(); |
Ted Kremenek | a7a8dfd | 2009-12-03 17:48:05 +0000 | [diff] [blame] | 1774 | RI != RE; ++RI) { |
| 1775 | if ((*RI)->getDecl()->getAttr<BlocksAttr>()) |
Zhongxing Xu | bfcaf80 | 2010-02-05 02:26:30 +0000 | [diff] [blame] | 1776 | WorkList.push_back(std::make_pair(store_N, *RI)); |
Ted Kremenek | a7a8dfd | 2009-12-03 17:48:05 +0000 | [diff] [blame] | 1777 | } |
Ted Kremenek | a6d73af | 2009-11-26 02:35:42 +0000 | [diff] [blame] | 1778 | // No possible data bindings on a BlockDataRegion. Continue to the |
| 1779 | // next region in the worklist. |
| 1780 | continue; |
| 1781 | } |
Ted Kremenek | 9e17cc6 | 2009-09-29 06:35:00 +0000 | [diff] [blame] | 1782 | |
Ted Kremenek | 9e17cc6 | 2009-09-29 06:35:00 +0000 | [diff] [blame] | 1783 | RegionBindings B_N = GetRegionBindings(store_N); |
| 1784 | |
| 1785 | // Get the data binding for R (if any). |
Zhongxing Xu | 13d5017 | 2009-10-11 08:08:02 +0000 | [diff] [blame] | 1786 | Optional<SVal> V = getBinding(B_N, R); |
Ted Kremenek | 9e17cc6 | 2009-09-29 06:35:00 +0000 | [diff] [blame] | 1787 | |
Zhongxing Xu | 13d5017 | 2009-10-11 08:08:02 +0000 | [diff] [blame] | 1788 | if (V) { |
| 1789 | // Check for lazy bindings. |
| 1790 | if (const nonloc::LazyCompoundVal *LCV = |
| 1791 | dyn_cast<nonloc::LazyCompoundVal>(V.getPointer())) { |
Ted Kremenek | 9e17cc6 | 2009-09-29 06:35:00 +0000 | [diff] [blame] | 1792 | |
Zhongxing Xu | 13d5017 | 2009-10-11 08:08:02 +0000 | [diff] [blame] | 1793 | const LazyCompoundValData *D = LCV->getCVData(); |
Zhongxing Xu | bfcaf80 | 2010-02-05 02:26:30 +0000 | [diff] [blame] | 1794 | WorkList.push_back(std::make_pair(D->getStore(), D->getRegion())); |
Zhongxing Xu | 13d5017 | 2009-10-11 08:08:02 +0000 | [diff] [blame] | 1795 | } |
| 1796 | else { |
Ted Kremenek | 9e17cc6 | 2009-09-29 06:35:00 +0000 | [diff] [blame] | 1797 | // Update the set of live symbols. |
Zhongxing Xu | 13d5017 | 2009-10-11 08:08:02 +0000 | [diff] [blame] | 1798 | for (SVal::symbol_iterator SI=V->symbol_begin(), SE=V->symbol_end(); |
Ted Kremenek | 9e17cc6 | 2009-09-29 06:35:00 +0000 | [diff] [blame] | 1799 | SI!=SE;++SI) |
| 1800 | SymReaper.markLive(*SI); |
| 1801 | |
Zhongxing Xu | 13d5017 | 2009-10-11 08:08:02 +0000 | [diff] [blame] | 1802 | // If V is a region, then add it to the worklist. |
| 1803 | if (const MemRegion *RX = V->getAsRegion()) |
Zhongxing Xu | bfcaf80 | 2010-02-05 02:26:30 +0000 | [diff] [blame] | 1804 | WorkList.push_back(std::make_pair(store_N, RX)); |
Ted Kremenek | 9e17cc6 | 2009-09-29 06:35:00 +0000 | [diff] [blame] | 1805 | } |
| 1806 | } |
| 1807 | } |
| 1808 | |
Ted Kremenek | 0175619 | 2009-10-29 05:14:17 +0000 | [diff] [blame] | 1809 | // See if any postponed SymbolicRegions are actually live now, after |
| 1810 | // having done a scan. |
| 1811 | for (llvm::SmallVectorImpl<RBDNode>::iterator I = Postponed.begin(), |
| 1812 | E = Postponed.end() ; I != E ; ++I) { |
| 1813 | if (const SymbolicRegion *SR = cast_or_null<SymbolicRegion>(I->second)) { |
| 1814 | if (SymReaper.isLive(SR->getSymbol())) { |
| 1815 | WorkList.push_back(*I); |
| 1816 | I->second = NULL; |
| 1817 | } |
| 1818 | } |
| 1819 | } |
| 1820 | |
| 1821 | if (!WorkList.empty()) |
| 1822 | goto tryAgain; |
| 1823 | |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 1824 | // We have now scanned the store, marking reachable regions and symbols |
| 1825 | // as live. We now remove all the regions that are dead from the store |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1826 | // as well as update DSymbols with the set symbols that are now dead. |
Zhongxing Xu | bfcaf80 | 2010-02-05 02:26:30 +0000 | [diff] [blame] | 1827 | Store new_store = store; |
Ted Kremenek | 451ac09 | 2009-08-06 04:50:20 +0000 | [diff] [blame] | 1828 | for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) { |
Ted Kremenek | 1c1ae6b | 2010-01-11 00:07:44 +0000 | [diff] [blame] | 1829 | const MemRegion* R = I.getKey().getRegion(); |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 1830 | // If this region live? Is so, none of its symbols are dead. |
Zhongxing Xu | bfcaf80 | 2010-02-05 02:26:30 +0000 | [diff] [blame] | 1831 | if (Visited.count(std::make_pair(store, R))) |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 1832 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1833 | |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 1834 | // Remove this dead region from the store. |
Zhongxing Xu | bfcaf80 | 2010-02-05 02:26:30 +0000 | [diff] [blame] | 1835 | new_store = Remove(new_store, I.getKey()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1836 | |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 1837 | // Mark all non-live symbols that this region references as dead. |
| 1838 | if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R)) |
| 1839 | SymReaper.maybeDead(SymR->getSymbol()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1840 | |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 1841 | SVal X = I.getData(); |
Ted Kremenek | 093569c | 2009-08-02 05:00:15 +0000 | [diff] [blame] | 1842 | SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end(); |
| 1843 | for (; SI != SE; ++SI) |
| 1844 | SymReaper.maybeDead(*SI); |
| 1845 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1846 | |
Zhongxing Xu | 72119c4 | 2010-02-05 05:34:29 +0000 | [diff] [blame] | 1847 | return new_store; |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 1848 | } |
| 1849 | |
Zhongxing Xu | 4e3c1f7 | 2009-10-13 02:24:55 +0000 | [diff] [blame] | 1850 | GRState const *RegionStoreManager::EnterStackFrame(GRState const *state, |
| 1851 | StackFrameContext const *frame) { |
| 1852 | FunctionDecl const *FD = cast<FunctionDecl>(frame->getDecl()); |
| 1853 | CallExpr const *CE = cast<CallExpr>(frame->getCallSite()); |
| 1854 | |
| 1855 | FunctionDecl::param_const_iterator PI = FD->param_begin(); |
| 1856 | |
| 1857 | CallExpr::const_arg_iterator AI = CE->arg_begin(), AE = CE->arg_end(); |
| 1858 | |
| 1859 | // Copy the arg expression value to the arg variables. |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 1860 | Store store = state->getStore(); |
Zhongxing Xu | 4e3c1f7 | 2009-10-13 02:24:55 +0000 | [diff] [blame] | 1861 | for (; AI != AE; ++AI, ++PI) { |
Zhongxing Xu | 6f8c430 | 2010-02-08 09:30:02 +0000 | [diff] [blame^] | 1862 | SVal ArgVal = state->getExprVal(*AI); |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 1863 | store = Bind(store, ValMgr.makeLoc(MRMgr.getVarRegion(*PI, frame)), ArgVal); |
Zhongxing Xu | 4e3c1f7 | 2009-10-13 02:24:55 +0000 | [diff] [blame] | 1864 | } |
| 1865 | |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 1866 | return state->makeWithStore(store); |
Zhongxing Xu | 4e3c1f7 | 2009-10-13 02:24:55 +0000 | [diff] [blame] | 1867 | } |
| 1868 | |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 1869 | //===----------------------------------------------------------------------===// |
| 1870 | // Utility methods. |
| 1871 | //===----------------------------------------------------------------------===// |
| 1872 | |
Ted Kremenek | 53ba0b6 | 2009-06-24 23:06:47 +0000 | [diff] [blame] | 1873 | void RegionStoreManager::print(Store store, llvm::raw_ostream& OS, |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 1874 | const char* nl, const char *sep) { |
Ted Kremenek | 451ac09 | 2009-08-06 04:50:20 +0000 | [diff] [blame] | 1875 | RegionBindings B = GetRegionBindings(store); |
Ted Kremenek | ab22ee9 | 2009-10-20 01:20:57 +0000 | [diff] [blame] | 1876 | OS << "Store (direct and default bindings):" << nl; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1877 | |
Ted Kremenek | 451ac09 | 2009-08-06 04:50:20 +0000 | [diff] [blame] | 1878 | for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1879 | OS << ' ' << I.getKey() << " : " << I.getData() << nl; |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 1880 | } |