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