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