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" |
| 18 | #include "clang/Analysis/PathSensitive/GRState.h" |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 19 | #include "clang/Analysis/PathSensitive/GRStateTrait.h" |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 20 | #include "clang/Analysis/Analyses/LiveVariables.h" |
| 21 | |
| 22 | #include "llvm/ADT/ImmutableMap.h" |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/ImmutableList.h" |
Zhongxing Xu | a071eb0 | 2008-10-24 06:01:33 +0000 | [diff] [blame] | 24 | #include "llvm/Support/raw_ostream.h" |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Compiler.h" |
| 26 | |
| 27 | using namespace clang; |
| 28 | |
Zhongxing Xu | baf03a7 | 2008-11-24 09:44:56 +0000 | [diff] [blame] | 29 | // Actual Store type. |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 30 | typedef llvm::ImmutableMap<const MemRegion*, SVal> RegionBindingsTy; |
Zhongxing Xu | baf03a7 | 2008-11-24 09:44:56 +0000 | [diff] [blame] | 31 | |
| 32 | // RegionView GDM stuff. |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 33 | typedef llvm::ImmutableList<const MemRegion*> RegionViewTy; |
| 34 | typedef llvm::ImmutableMap<const MemRegion*, RegionViewTy> RegionViewMapTy; |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 35 | static int RegionViewMapTyIndex = 0; |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 36 | namespace clang { |
| 37 | template<> struct GRStateTrait<RegionViewMapTy> |
| 38 | : public GRStatePartialTrait<RegionViewMapTy> { |
| 39 | static void* GDMIndex() { return &RegionViewMapTyIndex; } |
| 40 | }; |
| 41 | } |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 42 | |
Zhongxing Xu | baf03a7 | 2008-11-24 09:44:56 +0000 | [diff] [blame] | 43 | // RegionExtents GDM stuff. |
| 44 | // Currently RegionExtents are in bytes. We can change this representation when |
| 45 | // there are real requirements. |
| 46 | typedef llvm::ImmutableMap<const MemRegion*, SVal> RegionExtentsTy; |
| 47 | static int RegionExtentsTyIndex = 0; |
| 48 | namespace clang { |
| 49 | template<> struct GRStateTrait<RegionExtentsTy> |
| 50 | : public GRStatePartialTrait<RegionExtentsTy> { |
| 51 | static void* GDMIndex() { return &RegionExtentsTyIndex; } |
| 52 | }; |
| 53 | } |
| 54 | |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 55 | // KillSet GDM stuff. |
Ted Kremenek | 2ed14be | 2008-12-05 00:47:52 +0000 | [diff] [blame] | 56 | typedef llvm::ImmutableSet<const MemRegion*> RegionKills; |
| 57 | static int RegionKillsIndex = 0; |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 58 | namespace clang { |
Ted Kremenek | 2ed14be | 2008-12-05 00:47:52 +0000 | [diff] [blame] | 59 | template<> struct GRStateTrait<RegionKills> |
| 60 | : public GRStatePartialTrait<RegionKills> { |
| 61 | static void* GDMIndex() { return &RegionKillsIndex; } |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 62 | }; |
| 63 | } |
| 64 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame^] | 65 | // Regions that have default value zero. |
| 66 | // FIXME: redefinition! |
| 67 | // typedef llvm::ImmutableMap<const MemRegion*, SVal> RegionDefaultValue; |
| 68 | // static int RegionDefaultValueIndex = 0; |
| 69 | // namespace clang { |
| 70 | // template<> struct GRStateTrait<RegionDefaultValue> |
| 71 | // : public GRStatePartialTrait<RegionDefaultValue> { |
| 72 | // static void* GDMIndex() { return &RegionDefaultValueIndex; } |
| 73 | // }; |
| 74 | // } |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 75 | |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 76 | namespace { |
| 77 | |
| 78 | class VISIBILITY_HIDDEN RegionStoreManager : public StoreManager { |
| 79 | RegionBindingsTy::Factory RBFactory; |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 80 | RegionViewTy::Factory RVFactory; |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 81 | |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 82 | GRStateManager& StateMgr; |
| 83 | MemRegionManager MRMgr; |
| 84 | |
| 85 | public: |
| 86 | RegionStoreManager(GRStateManager& mgr) |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 87 | : RBFactory(mgr.getAllocator()), |
| 88 | RVFactory(mgr.getAllocator()), |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 89 | StateMgr(mgr), |
| 90 | MRMgr(StateMgr.getAllocator()) {} |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 91 | |
| 92 | virtual ~RegionStoreManager() {} |
| 93 | |
Zhongxing Xu | 24194ef | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 94 | MemRegionManager& getRegionManager() { return MRMgr; } |
Ted Kremenek | 4f09027 | 2008-10-27 21:54:31 +0000 | [diff] [blame] | 95 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame^] | 96 | const GRState* BindCompoundLiteral(const GRState* St, |
| 97 | const CompoundLiteralExpr* CL, SVal V); |
Zhongxing Xu | 24194ef | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 98 | |
Zhongxing Xu | 143bf82 | 2008-10-25 14:18:57 +0000 | [diff] [blame] | 99 | SVal getLValueString(const GRState* St, const StringLiteral* S); |
| 100 | |
Zhongxing Xu | f22679e | 2008-11-07 10:38:33 +0000 | [diff] [blame] | 101 | SVal getLValueCompoundLiteral(const GRState* St, const CompoundLiteralExpr*); |
| 102 | |
Zhongxing Xu | c4bf72c | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 103 | SVal getLValueVar(const GRState* St, const VarDecl* VD); |
| 104 | |
| 105 | SVal getLValueIvar(const GRState* St, const ObjCIvarDecl* D, SVal Base); |
| 106 | |
| 107 | SVal getLValueField(const GRState* St, SVal Base, const FieldDecl* D); |
| 108 | |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 109 | SVal getLValueElement(const GRState* St, SVal Base, SVal Offset); |
| 110 | |
Zhongxing Xu | e8a964b | 2008-11-22 13:21:46 +0000 | [diff] [blame] | 111 | SVal getSizeInElements(const GRState* St, const MemRegion* R); |
| 112 | |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 113 | SVal ArrayToPointer(SVal Array); |
| 114 | |
Ted Kremenek | 6eddeb1 | 2008-12-13 21:49:13 +0000 | [diff] [blame] | 115 | /// CastRegion - Used by GRExprEngine::VisitCast to handle casts from |
| 116 | /// a MemRegion* to a specific location type. 'R' is the region being |
| 117 | /// casted and 'CastToTy' the result type of the cast. |
| 118 | CastResult CastRegion(const GRState* state, const MemRegion* R, |
| 119 | QualType CastToTy); |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 120 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame^] | 121 | /// The high level logic for this method is this: |
| 122 | /// Retrieve (L) |
| 123 | /// if L has binding |
| 124 | /// return L's binding |
| 125 | /// else if L is in killset |
| 126 | /// return unknown |
| 127 | /// else |
| 128 | /// if L is on stack or heap |
| 129 | /// return undefined |
| 130 | /// else |
| 131 | /// return symbolic |
Ted Kremenek | 2ed14be | 2008-12-05 00:47:52 +0000 | [diff] [blame] | 132 | SVal Retrieve(const GRState* state, Loc L, QualType T = QualType()); |
Zhongxing Xu | c4bf72c | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 133 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame^] | 134 | const GRState* Bind(const GRState* St, Loc LV, SVal V); |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 135 | |
Zhongxing Xu | 9c9ca08 | 2008-12-16 02:36:30 +0000 | [diff] [blame] | 136 | Store Remove(Store store, Loc LV); |
Zhongxing Xu | 24194ef | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 137 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame^] | 138 | Store getInitialStore() { return RBFactory.GetEmptyMap().getRoot(); } |
Ted Kremenek | 9deb0e3 | 2008-10-24 20:32:16 +0000 | [diff] [blame] | 139 | |
| 140 | /// getSelfRegion - Returns the region for the 'self' (Objective-C) or |
| 141 | /// 'this' object (C++). When used when analyzing a normal function this |
| 142 | /// method returns NULL. |
| 143 | const MemRegion* getSelfRegion(Store) { |
| 144 | assert (false && "Not implemented."); |
| 145 | return 0; |
| 146 | } |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 147 | |
Ted Kremenek | 2ed14be | 2008-12-05 00:47:52 +0000 | [diff] [blame] | 148 | /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values. |
| 149 | /// It returns a new Store with these values removed, and populates LSymbols |
| 150 | // and DSymbols with the known set of live and dead symbols respectively. |
| 151 | Store RemoveDeadBindings(const GRState* state, Stmt* Loc, |
| 152 | const LiveVariables& Live, |
Zhongxing Xu | 24194ef | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 153 | llvm::SmallVectorImpl<const MemRegion*>& RegionRoots, |
Zhongxing Xu | 8916d5b | 2008-11-10 09:39:04 +0000 | [diff] [blame] | 154 | LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols); |
Ted Kremenek | 2ed14be | 2008-12-05 00:47:52 +0000 | [diff] [blame] | 155 | |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 156 | void UpdateLiveSymbols(SVal X, LiveSymbolsTy& LSymbols); |
Zhongxing Xu | 24194ef | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 157 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame^] | 158 | const GRState* BindDecl(const GRState* St, const VarDecl* VD, SVal InitVal); |
| 159 | |
| 160 | const GRState* BindDeclWithNoInit(const GRState* St, const VarDecl* VD) { |
| 161 | return St; |
| 162 | } |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 163 | |
Zhongxing Xu | baf03a7 | 2008-11-24 09:44:56 +0000 | [diff] [blame] | 164 | const GRState* setExtent(const GRState* St, const MemRegion* R, SVal Extent); |
| 165 | |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 166 | static inline RegionBindingsTy GetRegionBindings(Store store) { |
Zhongxing Xu | 9c9ca08 | 2008-12-16 02:36:30 +0000 | [diff] [blame] | 167 | return RegionBindingsTy(static_cast<const RegionBindingsTy::TreeTy*>(store)); |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 168 | } |
Zhongxing Xu | 24194ef | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 169 | |
Zhongxing Xu | 5b8b6f2 | 2008-10-24 04:33:15 +0000 | [diff] [blame] | 170 | void print(Store store, std::ostream& Out, const char* nl, const char *sep); |
Zhongxing Xu | 24194ef | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 171 | |
| 172 | void iterBindings(Store store, BindingsHandler& f) { |
| 173 | // FIXME: Implement. |
| 174 | } |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 175 | |
| 176 | private: |
| 177 | Loc getVarLoc(const VarDecl* VD) { |
| 178 | return loc::MemRegionVal(MRMgr.getVarRegion(VD)); |
| 179 | } |
| 180 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame^] | 181 | const GRState* BindArray(const GRState* St, const TypedRegion* R, SVal V); |
Zhongxing Xu | 6e3f01c | 2008-10-31 07:16:08 +0000 | [diff] [blame] | 182 | |
Zhongxing Xu | 0b242ec | 2008-12-04 01:12:41 +0000 | [diff] [blame] | 183 | /// Retrieve the values in a struct and return a CompoundVal, used when doing |
| 184 | /// struct copy: |
| 185 | /// struct s x, y; |
| 186 | /// x = y; |
| 187 | /// y's value is retrieved by this method. |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame^] | 188 | SVal RetrieveStruct(const GRState* St, const TypedRegion* R); |
Zhongxing Xu | 0b242ec | 2008-12-04 01:12:41 +0000 | [diff] [blame] | 189 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame^] | 190 | const GRState* BindStruct(const GRState* St, const TypedRegion* R, SVal V); |
Zhongxing Xu | 63123d8 | 2008-11-23 04:30:35 +0000 | [diff] [blame] | 191 | |
Zhongxing Xu | 6e3f01c | 2008-10-31 07:16:08 +0000 | [diff] [blame] | 192 | // Utility methods. |
| 193 | BasicValueFactory& getBasicVals() { return StateMgr.getBasicVals(); } |
| 194 | ASTContext& getContext() { return StateMgr.getContext(); } |
Zhongxing Xu | 63123d8 | 2008-11-23 04:30:35 +0000 | [diff] [blame] | 195 | SymbolManager& getSymbolManager() { return StateMgr.getSymbolManager(); } |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 196 | |
| 197 | const GRState* AddRegionView(const GRState* St, |
| 198 | const MemRegion* View, const MemRegion* Base); |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 199 | }; |
| 200 | |
| 201 | } // end anonymous namespace |
| 202 | |
Ted Kremenek | 95c7b00 | 2008-10-24 01:04:59 +0000 | [diff] [blame] | 203 | StoreManager* clang::CreateRegionStoreManager(GRStateManager& StMgr) { |
Zhongxing Xu | 24194ef | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 204 | return new RegionStoreManager(StMgr); |
Ted Kremenek | 95c7b00 | 2008-10-24 01:04:59 +0000 | [diff] [blame] | 205 | } |
| 206 | |
Zhongxing Xu | 143bf82 | 2008-10-25 14:18:57 +0000 | [diff] [blame] | 207 | SVal RegionStoreManager::getLValueString(const GRState* St, |
| 208 | const StringLiteral* S) { |
| 209 | return loc::MemRegionVal(MRMgr.getStringRegion(S)); |
| 210 | } |
| 211 | |
Zhongxing Xu | c4bf72c | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 212 | SVal RegionStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) { |
| 213 | return loc::MemRegionVal(MRMgr.getVarRegion(VD)); |
| 214 | } |
Zhongxing Xu | f22679e | 2008-11-07 10:38:33 +0000 | [diff] [blame] | 215 | |
| 216 | SVal RegionStoreManager::getLValueCompoundLiteral(const GRState* St, |
| 217 | const CompoundLiteralExpr* CL) { |
| 218 | return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL)); |
| 219 | } |
| 220 | |
Zhongxing Xu | c4bf72c | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 221 | SVal RegionStoreManager::getLValueIvar(const GRState* St, const ObjCIvarDecl* D, |
| 222 | SVal Base) { |
| 223 | return UnknownVal(); |
| 224 | } |
| 225 | |
| 226 | SVal RegionStoreManager::getLValueField(const GRState* St, SVal Base, |
| 227 | const FieldDecl* D) { |
| 228 | if (Base.isUnknownOrUndef()) |
| 229 | return Base; |
| 230 | |
| 231 | Loc BaseL = cast<Loc>(Base); |
| 232 | const MemRegion* BaseR = 0; |
| 233 | |
| 234 | switch (BaseL.getSubKind()) { |
| 235 | case loc::MemRegionKind: |
| 236 | BaseR = cast<loc::MemRegionVal>(BaseL).getRegion(); |
| 237 | break; |
| 238 | |
| 239 | case loc::SymbolValKind: |
| 240 | BaseR = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(&BaseL)->getSymbol()); |
| 241 | break; |
| 242 | |
| 243 | case loc::GotoLabelKind: |
| 244 | case loc::FuncValKind: |
| 245 | // These are anormal cases. Flag an undefined value. |
| 246 | return UndefinedVal(); |
| 247 | |
| 248 | case loc::ConcreteIntKind: |
Zhongxing Xu | c4bf72c | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 249 | // While these seem funny, this can happen through casts. |
| 250 | // FIXME: What we should return is the field offset. For example, |
| 251 | // add the field offset to the integer value. That way funny things |
| 252 | // like this work properly: &(((struct foo *) 0xa)->f) |
| 253 | return Base; |
| 254 | |
| 255 | default: |
Zhongxing Xu | 13d1ee2 | 2008-11-07 08:57:30 +0000 | [diff] [blame] | 256 | assert(0 && "Unhandled Base."); |
Zhongxing Xu | c4bf72c | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 257 | return Base; |
| 258 | } |
| 259 | |
| 260 | return loc::MemRegionVal(MRMgr.getFieldRegion(D, BaseR)); |
| 261 | } |
| 262 | |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 263 | SVal RegionStoreManager::getLValueElement(const GRState* St, |
| 264 | SVal Base, SVal Offset) { |
| 265 | if (Base.isUnknownOrUndef()) |
| 266 | return Base; |
| 267 | |
Zhongxing Xu | 4a1513e | 2008-10-27 12:23:17 +0000 | [diff] [blame] | 268 | if (isa<loc::SymbolVal>(Base)) |
| 269 | return Base; |
| 270 | |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 271 | loc::MemRegionVal& BaseL = cast<loc::MemRegionVal>(Base); |
| 272 | |
Zhongxing Xu | e4d1393 | 2008-11-13 09:48:44 +0000 | [diff] [blame] | 273 | // Pointer of any type can be cast and used as array base. We do not support |
| 274 | // that case yet. |
| 275 | if (!isa<ElementRegion>(BaseL.getRegion())) { |
| 276 | // Record what we have seen in real code. |
| 277 | assert(isa<FieldRegion>(BaseL.getRegion())); |
| 278 | return UnknownVal(); |
| 279 | } |
| 280 | |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 281 | // We expect BaseR is an ElementRegion, not a base VarRegion. |
| 282 | |
| 283 | const ElementRegion* ElemR = cast<ElementRegion>(BaseL.getRegion()); |
| 284 | |
| 285 | SVal Idx = ElemR->getIndex(); |
| 286 | |
| 287 | nonloc::ConcreteInt *CI1, *CI2; |
| 288 | |
| 289 | // Only handle integer indices for now. |
| 290 | if ((CI1 = dyn_cast<nonloc::ConcreteInt>(&Idx)) && |
| 291 | (CI2 = dyn_cast<nonloc::ConcreteInt>(&Offset))) { |
Zhongxing Xu | cc0d0ec | 2008-11-13 09:15:14 +0000 | [diff] [blame] | 292 | |
Sebastian Redl | e95db4f | 2008-11-24 19:35:33 +0000 | [diff] [blame] | 293 | // Temporary SVal to hold a potential signed and extended APSInt. |
Zhongxing Xu | cc0d0ec | 2008-11-13 09:15:14 +0000 | [diff] [blame] | 294 | SVal SignedInt; |
| 295 | |
Sebastian Redl | e95db4f | 2008-11-24 19:35:33 +0000 | [diff] [blame] | 296 | // Index might be unsigned. We have to convert it to signed. It might also |
| 297 | // be less wide than the size. We have to extend it. |
| 298 | if (CI2->getValue().isUnsigned() || |
| 299 | CI2->getValue().getBitWidth() < CI1->getValue().getBitWidth()) { |
Zhongxing Xu | cc0d0ec | 2008-11-13 09:15:14 +0000 | [diff] [blame] | 300 | llvm::APSInt SI = CI2->getValue(); |
Sebastian Redl | ddee68b | 2008-11-24 19:39:40 +0000 | [diff] [blame] | 301 | if (CI2->getValue().getBitWidth() < CI1->getValue().getBitWidth()) |
| 302 | SI.extend(CI1->getValue().getBitWidth()); |
Zhongxing Xu | cc0d0ec | 2008-11-13 09:15:14 +0000 | [diff] [blame] | 303 | SI.setIsSigned(true); |
| 304 | SignedInt = nonloc::ConcreteInt(getBasicVals().getValue(SI)); |
| 305 | CI2 = cast<nonloc::ConcreteInt>(&SignedInt); |
| 306 | } |
| 307 | |
Zhongxing Xu | 63123d8 | 2008-11-23 04:30:35 +0000 | [diff] [blame] | 308 | SVal NewIdx = CI1->EvalBinOp(getBasicVals(), BinaryOperator::Add, *CI2); |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 309 | return loc::MemRegionVal(MRMgr.getElementRegion(NewIdx, |
Ted Kremenek | abb042f | 2008-12-13 19:24:37 +0000 | [diff] [blame] | 310 | ElemR->getArrayRegion())); |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | return UnknownVal(); |
| 314 | } |
| 315 | |
Zhongxing Xu | e8a964b | 2008-11-22 13:21:46 +0000 | [diff] [blame] | 316 | SVal RegionStoreManager::getSizeInElements(const GRState* St, |
| 317 | const MemRegion* R) { |
| 318 | if (const VarRegion* VR = dyn_cast<VarRegion>(R)) { |
| 319 | // Get the type of the variable. |
Ted Kremenek | 6eddeb1 | 2008-12-13 21:49:13 +0000 | [diff] [blame] | 320 | QualType T = VR->getRValueType(getContext()); |
Zhongxing Xu | e8a964b | 2008-11-22 13:21:46 +0000 | [diff] [blame] | 321 | |
| 322 | // It must be of array type. |
| 323 | const ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr()); |
| 324 | |
| 325 | // return the size as signed integer. |
| 326 | return NonLoc::MakeVal(getBasicVals(), CAT->getSize(), false); |
| 327 | } |
| 328 | |
| 329 | if (const StringRegion* SR = dyn_cast<StringRegion>(R)) { |
Zhongxing Xu | 6613d08 | 2008-11-24 02:18:56 +0000 | [diff] [blame] | 330 | const StringLiteral* Str = SR->getStringLiteral(); |
Zhongxing Xu | d0fd3b7 | 2008-11-24 02:30:48 +0000 | [diff] [blame] | 331 | // We intentionally made the size value signed because it participates in |
| 332 | // operations with signed indices. |
Zhongxing Xu | 4b89e03 | 2008-11-24 05:16:01 +0000 | [diff] [blame] | 333 | return NonLoc::MakeVal(getBasicVals(), Str->getByteLength() + 1, false); |
Zhongxing Xu | e8a964b | 2008-11-22 13:21:46 +0000 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | if (const AnonTypedRegion* ATR = dyn_cast<AnonTypedRegion>(R)) { |
Zhongxing Xu | baf03a7 | 2008-11-24 09:44:56 +0000 | [diff] [blame] | 337 | GRStateRef state(St, StateMgr); |
| 338 | |
| 339 | // Get the size of the super region in bytes. |
| 340 | RegionExtentsTy::data_type* T |
| 341 | = state.get<RegionExtentsTy>(ATR->getSuperRegion()); |
| 342 | |
| 343 | assert(T && "region extent not exist"); |
| 344 | |
| 345 | // Assume it's ConcreteInt for now. |
| 346 | llvm::APSInt SSize = cast<nonloc::ConcreteInt>(*T).getValue(); |
| 347 | |
| 348 | // Get the size of the element in bits. |
Ted Kremenek | 6eddeb1 | 2008-12-13 21:49:13 +0000 | [diff] [blame] | 349 | QualType LvT = ATR->getLValueType(getContext()); |
| 350 | QualType ElemTy = cast<PointerType>(LvT.getTypePtr())->getPointeeType(); |
Zhongxing Xu | baf03a7 | 2008-11-24 09:44:56 +0000 | [diff] [blame] | 351 | |
| 352 | uint64_t X = getContext().getTypeSize(ElemTy); |
| 353 | |
| 354 | const llvm::APSInt& ESize = getBasicVals().getValue(X, SSize.getBitWidth(), |
| 355 | false); |
| 356 | |
| 357 | // Calculate the number of elements. |
| 358 | |
| 359 | // FIXME: What do we do with signed-ness problem? Shall we make all APSInts |
| 360 | // signed? |
| 361 | if (SSize.isUnsigned()) |
| 362 | SSize.setIsSigned(true); |
| 363 | |
| 364 | // FIXME: move this operation into BasicVals. |
| 365 | const llvm::APSInt S = |
| 366 | (SSize * getBasicVals().getValue(8, SSize.getBitWidth(), false)) / ESize; |
| 367 | |
| 368 | return NonLoc::MakeVal(getBasicVals(), S); |
Zhongxing Xu | e8a964b | 2008-11-22 13:21:46 +0000 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | if (const FieldRegion* FR = dyn_cast<FieldRegion>(R)) { |
| 372 | // FIXME: Unsupported yet. |
| 373 | FR = 0; |
| 374 | return UnknownVal(); |
| 375 | } |
Zhongxing Xu | 369f429 | 2008-11-22 13:23:00 +0000 | [diff] [blame] | 376 | |
Zhongxing Xu | e8a964b | 2008-11-22 13:21:46 +0000 | [diff] [blame] | 377 | assert(0 && "Other regions are not supported yet."); |
| 378 | } |
| 379 | |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 380 | // Cast 'pointer to array' to 'pointer to the first element of array'. |
| 381 | |
| 382 | SVal RegionStoreManager::ArrayToPointer(SVal Array) { |
Ted Kremenek | abb042f | 2008-12-13 19:24:37 +0000 | [diff] [blame] | 383 | if (Array.isUnknownOrUndef()) |
| 384 | return Array; |
| 385 | |
| 386 | if (!isa<loc::MemRegionVal>(Array)) |
| 387 | return UnknownVal(); |
| 388 | |
| 389 | const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion(); |
| 390 | const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R); |
| 391 | |
| 392 | if (ArrayR) |
| 393 | return UnknownVal(); |
| 394 | |
Zhongxing Xu | 63123d8 | 2008-11-23 04:30:35 +0000 | [diff] [blame] | 395 | nonloc::ConcreteInt Idx(getBasicVals().getZeroWithPtrWidth(false)); |
Zhongxing Xu | 0b7e642 | 2008-10-26 02:23:57 +0000 | [diff] [blame] | 396 | ElementRegion* ER = MRMgr.getElementRegion(Idx, ArrayR); |
| 397 | |
| 398 | return loc::MemRegionVal(ER); |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 399 | } |
| 400 | |
Ted Kremenek | 6eddeb1 | 2008-12-13 21:49:13 +0000 | [diff] [blame] | 401 | StoreManager::CastResult |
| 402 | RegionStoreManager::CastRegion(const GRState* state, const MemRegion* R, |
| 403 | QualType CastToTy) { |
| 404 | |
| 405 | // Return the same region if the region types are compatible. |
| 406 | if (const TypedRegion* TR = dyn_cast<TypedRegion>(R)) { |
| 407 | ASTContext& Ctx = StateMgr.getContext(); |
| 408 | QualType Ta = Ctx.getCanonicalType(TR->getLValueType(Ctx)); |
| 409 | QualType Tb = Ctx.getCanonicalType(CastToTy); |
| 410 | |
| 411 | if (Ta == Tb) |
| 412 | return CastResult(state, R); |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 413 | } |
Ted Kremenek | 6eddeb1 | 2008-12-13 21:49:13 +0000 | [diff] [blame] | 414 | |
| 415 | const MemRegion* ViewR = MRMgr.getAnonTypedRegion(CastToTy, R); |
| 416 | return CastResult(AddRegionView(state, ViewR, R), ViewR); |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 417 | } |
| 418 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame^] | 419 | SVal RegionStoreManager::Retrieve(const GRState* St, Loc L, QualType T) { |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 420 | assert(!isa<UnknownVal>(L) && "location unknown"); |
| 421 | assert(!isa<UndefinedVal>(L) && "location undefined"); |
| 422 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame^] | 423 | if (isa<loc::SymbolVal>(L)) |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 424 | return UnknownVal(); |
| 425 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame^] | 426 | if (isa<loc::ConcreteInt>(L)) |
| 427 | return UndefinedVal(); |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 428 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame^] | 429 | if (isa<loc::FuncVal>(L)) |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 430 | return L; |
| 431 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame^] | 432 | const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion(); |
| 433 | assert(R && "bad region"); |
| 434 | |
| 435 | if (const TypedRegion* TR = dyn_cast<TypedRegion>(R)) |
| 436 | if (TR->getRValueType(getContext())->isStructureType()) |
| 437 | return RetrieveStruct(St, TR); |
| 438 | |
| 439 | RegionBindingsTy B = GetRegionBindings(St->getStore()); |
| 440 | RegionBindingsTy::data_type* V = B.lookup(R); |
| 441 | |
| 442 | // Check if the region has a binding. |
| 443 | if (V) |
| 444 | return *V; |
| 445 | |
| 446 | // Check if the region is in killset. |
| 447 | GRStateRef state(St, StateMgr); |
| 448 | if (state.contains<RegionKills>(R)) |
| 449 | return UnknownVal(); |
| 450 | |
| 451 | // The location is not initialized. |
| 452 | |
| 453 | // We treat parameters as symbolic values. |
| 454 | if (const VarRegion* VR = dyn_cast<VarRegion>(R)) |
| 455 | if (isa<ParmVarDecl>(VR->getDecl())) |
| 456 | return SVal::MakeSymbolValue(getSymbolManager(), VR, |
| 457 | VR->getRValueType(getContext())); |
| 458 | |
| 459 | if (MRMgr.onStack(R) || MRMgr.onHeap(R)) |
| 460 | return UndefinedVal(); |
| 461 | else |
| 462 | return SVal::MakeSymbolValue(getSymbolManager(), R, |
| 463 | cast<TypedRegion>(R)->getRValueType(getContext())); |
| 464 | |
| 465 | // FIXME: consider default values for elements and fields. |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 466 | } |
| 467 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame^] | 468 | SVal RegionStoreManager::RetrieveStruct(const GRState* St,const TypedRegion* R){ |
| 469 | |
| 470 | Store store = St->getStore(); |
| 471 | GRStateRef state(St, StateMgr); |
| 472 | |
Ted Kremenek | 6eddeb1 | 2008-12-13 21:49:13 +0000 | [diff] [blame] | 473 | // FIXME: Verify we want getRValueType instead of getLValueType. |
| 474 | QualType T = R->getRValueType(getContext()); |
Zhongxing Xu | 6e3f01c | 2008-10-31 07:16:08 +0000 | [diff] [blame] | 475 | assert(T->isStructureType()); |
| 476 | |
| 477 | const RecordType* RT = cast<RecordType>(T.getTypePtr()); |
| 478 | RecordDecl* RD = RT->getDecl(); |
| 479 | assert(RD->isDefinition()); |
| 480 | |
| 481 | llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList(); |
| 482 | |
Douglas Gregor | e267ff3 | 2008-12-11 20:41:00 +0000 | [diff] [blame] | 483 | std::vector<FieldDecl *> Fields(RD->field_begin(), RD->field_end()); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 484 | |
Douglas Gregor | e267ff3 | 2008-12-11 20:41:00 +0000 | [diff] [blame] | 485 | for (std::vector<FieldDecl *>::reverse_iterator Field = Fields.rbegin(), |
| 486 | FieldEnd = Fields.rend(); |
| 487 | Field != FieldEnd; ++Field) { |
| 488 | FieldRegion* FR = MRMgr.getFieldRegion(*Field, R); |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame^] | 489 | RegionBindingsTy B = GetRegionBindings(store); |
Zhongxing Xu | f0dfa8d | 2008-10-31 08:10:01 +0000 | [diff] [blame] | 490 | RegionBindingsTy::data_type* data = B.lookup(FR); |
Zhongxing Xu | 6e3f01c | 2008-10-31 07:16:08 +0000 | [diff] [blame] | 491 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame^] | 492 | SVal FieldValue; |
| 493 | if (data) |
| 494 | FieldValue = *data; |
| 495 | else if (state.contains<RegionKills>(FR)) |
| 496 | FieldValue = UnknownVal(); |
| 497 | else { |
| 498 | if (MRMgr.onStack(FR) || MRMgr.onHeap(FR)) |
| 499 | FieldValue = UndefinedVal(); |
| 500 | else |
| 501 | FieldValue = SVal::MakeSymbolValue(getSymbolManager(), FR, |
| 502 | FR->getRValueType(getContext())); |
| 503 | } |
Zhongxing Xu | 6e3f01c | 2008-10-31 07:16:08 +0000 | [diff] [blame] | 504 | |
| 505 | StructVal = getBasicVals().consVals(FieldValue, StructVal); |
| 506 | } |
| 507 | |
| 508 | return NonLoc::MakeCompoundVal(T, StructVal, getBasicVals()); |
| 509 | } |
| 510 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame^] | 511 | const GRState* RegionStoreManager::Bind(const GRState* St, Loc L, SVal V) { |
| 512 | // Currently we don't bind value to symbolic location. But if the logic is |
| 513 | // made clear, we might change this decision. |
| 514 | if (isa<loc::SymbolVal>(L)) |
| 515 | return St; |
Zhongxing Xu | 8fe63af | 2008-10-27 09:24:07 +0000 | [diff] [blame] | 516 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame^] | 517 | // If we get here, the location should be a region. |
| 518 | const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion(); |
Zhongxing Xu | f0dfa8d | 2008-10-31 08:10:01 +0000 | [diff] [blame] | 519 | assert(R); |
| 520 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame^] | 521 | // Check if the region is a struct region. |
Zhongxing Xu | f0dfa8d | 2008-10-31 08:10:01 +0000 | [diff] [blame] | 522 | if (const TypedRegion* TR = dyn_cast<TypedRegion>(R)) |
Ted Kremenek | 6eddeb1 | 2008-12-13 21:49:13 +0000 | [diff] [blame] | 523 | // FIXME: Verify we want getRValueType(). |
| 524 | if (TR->getRValueType(getContext())->isStructureType()) |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame^] | 525 | return BindStruct(St, TR, V); |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 526 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame^] | 527 | Store store = St->getStore(); |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 528 | RegionBindingsTy B = GetRegionBindings(store); |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame^] | 529 | |
| 530 | if (V.isUnknown()) { |
| 531 | // Remove the binding. |
| 532 | store = RBFactory.Remove(B, R).getRoot(); |
| 533 | |
| 534 | // Add the region to the killset. |
| 535 | GRStateRef state(St, StateMgr); |
| 536 | St = state.add<RegionKills>(R); |
| 537 | } |
| 538 | else |
| 539 | store = RBFactory.Add(B, R, V).getRoot(); |
| 540 | |
| 541 | return StateMgr.MakeStateWithStore(St, store); |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 542 | } |
| 543 | |
Zhongxing Xu | 9c9ca08 | 2008-12-16 02:36:30 +0000 | [diff] [blame] | 544 | Store RegionStoreManager::Remove(Store store, Loc L) { |
| 545 | RegionBindingsTy B = GetRegionBindings(store); |
| 546 | |
| 547 | const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion(); |
| 548 | assert(R); |
| 549 | |
| 550 | return RBFactory.Remove(B, R).getRoot(); |
| 551 | } |
| 552 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame^] | 553 | const GRState* RegionStoreManager::BindDecl(const GRState* St, |
| 554 | const VarDecl* VD, SVal InitVal) { |
| 555 | // All static variables are treated as symbolic values. |
| 556 | if (VD->hasGlobalStorage()) |
| 557 | return St; |
Zhongxing Xu | f0dfa8d | 2008-10-31 08:10:01 +0000 | [diff] [blame] | 558 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame^] | 559 | // Process local variables. |
Zhongxing Xu | a4f28ff | 2008-11-13 08:41:36 +0000 | [diff] [blame] | 560 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame^] | 561 | QualType T = VD->getType(); |
| 562 | |
| 563 | VarRegion* VR = MRMgr.getVarRegion(VD); |
| 564 | |
| 565 | if (Loc::IsLocType(T) || T->isIntegerType()) |
| 566 | return Bind(St, Loc::MakeVal(VR), InitVal); |
Zhongxing Xu | f0dfa8d | 2008-10-31 08:10:01 +0000 | [diff] [blame] | 567 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame^] | 568 | else if (T->isArrayType()) |
| 569 | return BindArray(St, VR, InitVal); |
Zhongxing Xu | f0dfa8d | 2008-10-31 08:10:01 +0000 | [diff] [blame] | 570 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame^] | 571 | else if (T->isStructureType()) |
| 572 | return BindStruct(St, VR, InitVal); |
Zhongxing Xu | d463d44 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 573 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame^] | 574 | // Other types of variable are not supported yet. |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 575 | return St; |
| 576 | } |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 577 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame^] | 578 | // FIXME: this method should be merged into Bind(). |
| 579 | const GRState* |
| 580 | RegionStoreManager::BindCompoundLiteral(const GRState* St, |
| 581 | const CompoundLiteralExpr* CL, SVal V) { |
Zhongxing Xu | f22679e | 2008-11-07 10:38:33 +0000 | [diff] [blame] | 582 | CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL); |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame^] | 583 | return Bind(St, loc::MemRegionVal(R), V); |
Zhongxing Xu | f22679e | 2008-11-07 10:38:33 +0000 | [diff] [blame] | 584 | } |
| 585 | |
Zhongxing Xu | baf03a7 | 2008-11-24 09:44:56 +0000 | [diff] [blame] | 586 | const GRState* RegionStoreManager::setExtent(const GRState* St, |
| 587 | const MemRegion* R, SVal Extent) { |
| 588 | GRStateRef state(St, StateMgr); |
| 589 | return state.set<RegionExtentsTy>(R, Extent); |
| 590 | } |
| 591 | |
| 592 | |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 593 | void RegionStoreManager::UpdateLiveSymbols(SVal X, LiveSymbolsTy& LSymbols) { |
| 594 | for (SVal::symbol_iterator SI=X.symbol_begin(),SE=X.symbol_end();SI!=SE;++SI) |
| 595 | LSymbols.insert(*SI); |
| 596 | } |
| 597 | |
Ted Kremenek | 2ed14be | 2008-12-05 00:47:52 +0000 | [diff] [blame] | 598 | Store RegionStoreManager::RemoveDeadBindings(const GRState* state, Stmt* Loc, |
Zhongxing Xu | 8916d5b | 2008-11-10 09:39:04 +0000 | [diff] [blame] | 599 | const LiveVariables& Live, |
| 600 | llvm::SmallVectorImpl<const MemRegion*>& RegionRoots, |
| 601 | LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols) { |
| 602 | |
Ted Kremenek | 2ed14be | 2008-12-05 00:47:52 +0000 | [diff] [blame] | 603 | Store store = state->getStore(); |
Zhongxing Xu | 8916d5b | 2008-11-10 09:39:04 +0000 | [diff] [blame] | 604 | RegionBindingsTy B = GetRegionBindings(store); |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 605 | |
| 606 | // Lazily constructed backmap from MemRegions to SubRegions. |
| 607 | typedef llvm::ImmutableSet<const MemRegion*> SubRegionsTy; |
| 608 | typedef llvm::ImmutableMap<const MemRegion*, SubRegionsTy> SubRegionsMapTy; |
| 609 | |
| 610 | // FIXME: As a future optimization we can modifiy BumpPtrAllocator to have |
| 611 | // the ability to reuse memory. This way we can keep TmpAlloc around as |
| 612 | // an instance variable of RegionStoreManager (avoiding repeated malloc |
| 613 | // overhead). |
| 614 | llvm::BumpPtrAllocator TmpAlloc; |
| 615 | |
| 616 | // Factory objects. |
| 617 | SubRegionsMapTy::Factory SubRegMapF(TmpAlloc); |
| 618 | SubRegionsTy::Factory SubRegF(TmpAlloc); |
| 619 | |
| 620 | // The backmap from regions to subregions. |
| 621 | SubRegionsMapTy SubRegMap = SubRegMapF.GetEmptyMap(); |
| 622 | |
| 623 | // Do a pass over the regions in the store. For VarRegions we check if |
| 624 | // the variable is still live and if so add it to the list of live roots. |
| 625 | // For other regions we populate our region backmap. |
Zhongxing Xu | 8916d5b | 2008-11-10 09:39:04 +0000 | [diff] [blame] | 626 | for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) { |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 627 | const MemRegion* R = I.getKey(); |
| 628 | if (const VarRegion* VR = dyn_cast<VarRegion>(R)) { |
| 629 | if (Live.isLive(Loc, VR->getDecl())) |
| 630 | RegionRoots.push_back(VR); // This is a live "root". |
| 631 | } |
| 632 | else { |
| 633 | // Get the super region for R. |
| 634 | const MemRegion* SuperR = cast<SubRegion>(R)->getSuperRegion(); |
| 635 | // Get the current set of subregions for SuperR. |
| 636 | const SubRegionsTy* SRptr = SubRegMap.lookup(SuperR); |
| 637 | SubRegionsTy SR = SRptr ? *SRptr : SubRegF.GetEmptySet(); |
| 638 | // Add R to the subregions of SuperR. |
| 639 | SubRegMap = SubRegMapF.Add(SubRegMap, SuperR, SubRegF.Add(SR, R)); |
| 640 | |
| 641 | // Finally, check if SuperR is a VarRegion. We need to do this |
| 642 | // to also mark SuperR as a root (as it may not have a value directly |
| 643 | // bound to it in the store). |
| 644 | if (const VarRegion* VR = dyn_cast<VarRegion>(SuperR)) { |
| 645 | if (Live.isLive(Loc, VR->getDecl())) |
| 646 | RegionRoots.push_back(VR); // This is a live "root". |
| 647 | } |
| 648 | } |
Zhongxing Xu | 8916d5b | 2008-11-10 09:39:04 +0000 | [diff] [blame] | 649 | } |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 650 | |
| 651 | // Process the worklist of RegionRoots. This performs a "mark-and-sweep" |
| 652 | // of the store. We want to find all live symbols and dead regions. |
| 653 | llvm::SmallPtrSet<const MemRegion*, 10> Marked; |
| 654 | |
| 655 | while (!RegionRoots.empty()) { |
| 656 | // Dequeue the next region on the worklist. |
| 657 | const MemRegion* R = RegionRoots.back(); |
| 658 | RegionRoots.pop_back(); |
Zhongxing Xu | 8916d5b | 2008-11-10 09:39:04 +0000 | [diff] [blame] | 659 | |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 660 | // Check if we have already processed this region. |
| 661 | if (Marked.count(R)) continue; |
| 662 | |
| 663 | // Mark this region as processed. This is needed for termination in case |
| 664 | // a region is referenced more than once. |
| 665 | Marked.insert(R); |
| 666 | |
| 667 | // Mark the symbol for any live SymbolicRegion as "live". This means we |
| 668 | // should continue to track that symbol. |
| 669 | if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R)) |
| 670 | LSymbols.insert(SymR->getSymbol()); |
| 671 | |
| 672 | // Get the data binding for R (if any). |
| 673 | RegionBindingsTy::data_type* Xptr = B.lookup(R); |
| 674 | if (Xptr) { |
| 675 | SVal X = *Xptr; |
| 676 | UpdateLiveSymbols(X, LSymbols); // Update the set of live symbols. |
| 677 | |
| 678 | // If X is a region, then add it the RegionRoots. |
| 679 | if (loc::MemRegionVal* RegionX = dyn_cast<loc::MemRegionVal>(&X)) |
| 680 | RegionRoots.push_back(RegionX->getRegion()); |
| 681 | } |
| 682 | |
| 683 | // Get the subregions of R. These are RegionRoots as well since they |
| 684 | // represent values that are also bound to R. |
| 685 | const SubRegionsTy* SRptr = SubRegMap.lookup(R); |
| 686 | if (!SRptr) continue; |
| 687 | SubRegionsTy SR = *SRptr; |
| 688 | |
| 689 | for (SubRegionsTy::iterator I=SR.begin(), E=SR.end(); I!=E; ++I) |
| 690 | RegionRoots.push_back(*I); |
| 691 | } |
| 692 | |
| 693 | // We have now scanned the store, marking reachable regions and symbols |
| 694 | // as live. We now remove all the regions that are dead from the store |
| 695 | // as well as update DSymbols with the set symbols that are now dead. |
| 696 | |
| 697 | for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) { |
| 698 | const MemRegion* R = I.getKey(); |
| 699 | |
| 700 | // If this region live? Is so, none of its symbols are dead. |
| 701 | if (Marked.count(R)) |
| 702 | continue; |
| 703 | |
| 704 | // Remove this dead region from the store. |
Zhongxing Xu | 9c9ca08 | 2008-12-16 02:36:30 +0000 | [diff] [blame] | 705 | store = Remove(store, Loc::MakeVal(R)); |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 706 | |
| 707 | // Mark all non-live symbols that this region references as dead. |
| 708 | if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R)) { |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 709 | SymbolRef Sym = SymR->getSymbol(); |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 710 | if (!LSymbols.count(Sym)) DSymbols.insert(Sym); |
| 711 | } |
| 712 | |
| 713 | SVal X = I.getData(); |
| 714 | SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end(); |
| 715 | for (; SI != SE; ++SI) { if (!LSymbols.count(*SI)) DSymbols.insert(*SI); } |
| 716 | } |
| 717 | |
Zhongxing Xu | 8916d5b | 2008-11-10 09:39:04 +0000 | [diff] [blame] | 718 | return store; |
| 719 | } |
| 720 | |
Zhongxing Xu | a071eb0 | 2008-10-24 06:01:33 +0000 | [diff] [blame] | 721 | void RegionStoreManager::print(Store store, std::ostream& Out, |
| 722 | const char* nl, const char *sep) { |
| 723 | llvm::raw_os_ostream OS(Out); |
| 724 | RegionBindingsTy B = GetRegionBindings(store); |
| 725 | OS << "Store:" << nl; |
| 726 | |
| 727 | for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) { |
| 728 | OS << ' '; I.getKey()->print(OS); OS << " : "; |
| 729 | I.getData().print(OS); OS << nl; |
| 730 | } |
Zhongxing Xu | 5b8b6f2 | 2008-10-24 04:33:15 +0000 | [diff] [blame] | 731 | } |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 732 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame^] | 733 | const GRState* RegionStoreManager::BindArray(const GRState* St, |
| 734 | const TypedRegion* R, SVal Init) { |
Ted Kremenek | 6eddeb1 | 2008-12-13 21:49:13 +0000 | [diff] [blame] | 735 | |
| 736 | // FIXME: Verify we should use getLValueType or getRValueType. |
Zhongxing Xu | 2ef9372 | 2008-12-14 03:14:52 +0000 | [diff] [blame] | 737 | QualType T = R->getRValueType(getContext()); |
Zhongxing Xu | 1a12a0e | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 738 | assert(T->isArrayType()); |
| 739 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame^] | 740 | // When we are binding the whole array, it always has default value 0. |
| 741 | GRStateRef state(St, StateMgr); |
| 742 | // St = state.set<RegionDefaultValue>(R, NonLoc::MakeVal(getBasicVals(), 0, |
| 743 | // false)); |
| 744 | |
| 745 | Store store = St->getStore(); |
| 746 | |
Zhongxing Xu | 1a12a0e | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 747 | ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr()); |
| 748 | |
Zhongxing Xu | 6987c7b | 2008-11-30 05:49:49 +0000 | [diff] [blame] | 749 | llvm::APSInt Size(CAT->getSize(), false); |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame^] | 750 | llvm::APSInt i = getBasicVals().getZeroWithPtrWidth(false); |
Zhongxing Xu | 6987c7b | 2008-11-30 05:49:49 +0000 | [diff] [blame] | 751 | |
| 752 | // Check if the init expr is a StringLiteral. |
| 753 | if (isa<loc::MemRegionVal>(Init)) { |
| 754 | const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion(); |
| 755 | const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral(); |
| 756 | const char* str = S->getStrData(); |
| 757 | unsigned len = S->getByteLength(); |
| 758 | unsigned j = 0; |
| 759 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame^] | 760 | // Copy bytes from the string literal into the target array. Trailing bytes |
| 761 | // in the array that are not covered by the string literal are initialized |
| 762 | // to zero. |
Zhongxing Xu | 6987c7b | 2008-11-30 05:49:49 +0000 | [diff] [blame] | 763 | for (; i < Size; ++i, ++j) { |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame^] | 764 | if (j >= len) |
| 765 | break; |
| 766 | |
Zhongxing Xu | 6987c7b | 2008-11-30 05:49:49 +0000 | [diff] [blame] | 767 | SVal Idx = NonLoc::MakeVal(getBasicVals(), i); |
| 768 | ElementRegion* ER = MRMgr.getElementRegion(Idx, R); |
| 769 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame^] | 770 | SVal V = NonLoc::MakeVal(getBasicVals(), str[j], sizeof(char)*8, true); |
| 771 | St = Bind(St, loc::MemRegionVal(ER), V); |
Zhongxing Xu | 6987c7b | 2008-11-30 05:49:49 +0000 | [diff] [blame] | 772 | } |
| 773 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame^] | 774 | return StateMgr.MakeStateWithStore(St, store); |
Zhongxing Xu | 6987c7b | 2008-11-30 05:49:49 +0000 | [diff] [blame] | 775 | } |
| 776 | |
Zhongxing Xu | 1a12a0e | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 777 | |
| 778 | nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init); |
| 779 | |
| 780 | nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end(); |
| 781 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame^] | 782 | for (; i < Size; ++i, ++VI) { |
| 783 | // The init list might be shorter than the array decl. |
| 784 | if (VI == VE) |
| 785 | break; |
| 786 | |
Zhongxing Xu | 6987c7b | 2008-11-30 05:49:49 +0000 | [diff] [blame] | 787 | SVal Idx = NonLoc::MakeVal(getBasicVals(), i); |
Zhongxing Xu | 1a12a0e | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 788 | ElementRegion* ER = MRMgr.getElementRegion(Idx, R); |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame^] | 789 | |
| 790 | if (CAT->getElementType()->isStructureType()) |
| 791 | St = BindStruct(St, ER, *VI); |
| 792 | else |
| 793 | St = Bind(St, Loc::MakeVal(ER), *VI); |
Zhongxing Xu | 1a12a0e | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 794 | } |
| 795 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame^] | 796 | return StateMgr.MakeStateWithStore(St, store); |
Zhongxing Xu | 1a12a0e | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 797 | } |
| 798 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame^] | 799 | const GRState* |
| 800 | RegionStoreManager::BindStruct(const GRState* St, const TypedRegion* R, SVal V){ |
Ted Kremenek | 6eddeb1 | 2008-12-13 21:49:13 +0000 | [diff] [blame] | 801 | // FIXME: Verify that we should use getRValueType or getLValueType. |
| 802 | QualType T = R->getRValueType(getContext()); |
Zhongxing Xu | af0a844 | 2008-10-31 10:53:01 +0000 | [diff] [blame] | 803 | assert(T->isStructureType()); |
| 804 | |
| 805 | RecordType* RT = cast<RecordType>(T.getTypePtr()); |
| 806 | RecordDecl* RD = RT->getDecl(); |
| 807 | assert(RD->isDefinition()); |
| 808 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame^] | 809 | nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V); |
Zhongxing Xu | af0a844 | 2008-10-31 10:53:01 +0000 | [diff] [blame] | 810 | nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end(); |
| 811 | RecordDecl::field_iterator FI = RD->field_begin(), FE = RD->field_end(); |
| 812 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame^] | 813 | for (; FI != FE; ++FI, ++VI) { |
| 814 | |
| 815 | // There may be fewer values than fields only when we are initializing a |
| 816 | // struct decl. In this case, mark the region as having default value. |
| 817 | if (VI == VE) { |
| 818 | // GRStateRef state(St, StateMgr); |
| 819 | //St = state.set<RegionDefaultValue>(R, NonLoc::MakeVal(getBasicVals(), 0, |
| 820 | // false)); |
| 821 | break; |
| 822 | } |
| 823 | |
Zhongxing Xu | af0a844 | 2008-10-31 10:53:01 +0000 | [diff] [blame] | 824 | QualType FTy = (*FI)->getType(); |
| 825 | FieldRegion* FR = MRMgr.getFieldRegion(*FI, R); |
| 826 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame^] | 827 | if (Loc::IsLocType(FTy) || FTy->isIntegerType()) |
| 828 | St = Bind(St, Loc::MakeVal(FR), *VI); |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 829 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame^] | 830 | else if (FTy->isArrayType()) |
| 831 | St = BindArray(St, FR, *VI); |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 832 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame^] | 833 | else if (FTy->isStructureType()) |
| 834 | St = BindStruct(St, FR, *VI); |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 835 | } |
| 836 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame^] | 837 | return St; |
Zhongxing Xu | c3a0599 | 2008-11-19 11:06:24 +0000 | [diff] [blame] | 838 | } |
| 839 | |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 840 | const GRState* RegionStoreManager::AddRegionView(const GRState* St, |
| 841 | const MemRegion* View, |
| 842 | const MemRegion* Base) { |
| 843 | GRStateRef state(St, StateMgr); |
| 844 | |
| 845 | // First, retrieve the region view of the base region. |
| 846 | RegionViewMapTy::data_type* d = state.get<RegionViewMapTy>(Base); |
| 847 | RegionViewTy L = d ? *d : RVFactory.GetEmptyList(); |
| 848 | |
| 849 | // Now add View to the region view. |
| 850 | L = RVFactory.Add(View, L); |
| 851 | |
| 852 | // Create a new state with the new region view. |
| 853 | return state.set<RegionViewMapTy>(Base, L); |
| 854 | } |