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