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