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