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