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