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