Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 1 | //== BasicStore.cpp - Basic map from Locations to Values --------*- 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 defined the BasicStore and BasicStoreManager classes. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Ted Kremenek | 5f81c44 | 2008-08-28 23:31:31 +0000 | [diff] [blame] | 14 | #include "clang/Analysis/Analyses/LiveVariables.h" |
Ted Kremenek | caa3724 | 2008-08-19 16:51:45 +0000 | [diff] [blame] | 15 | #include "clang/Analysis/PathSensitive/GRState.h" |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/ImmutableMap.h" |
| 17 | #include "llvm/Support/Compiler.h" |
Ted Kremenek | a622d8c | 2008-08-19 22:24:03 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Streams.h" |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 19 | |
| 20 | using namespace clang; |
| 21 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 22 | typedef llvm::ImmutableMap<const VarDecl*,SVal> VarBindingsTy; |
Ted Kremenek | 60dbad8 | 2008-09-03 03:06:11 +0000 | [diff] [blame] | 23 | |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 24 | namespace { |
| 25 | |
| 26 | class VISIBILITY_HIDDEN BasicStoreManager : public StoreManager { |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 27 | VarBindingsTy::Factory VBFactory; |
Zhongxing Xu | c1d1bbf | 2008-10-05 12:12:48 +0000 | [diff] [blame] | 28 | GRStateManager& StateMgr; |
Zhongxing Xu | bc678fd | 2008-10-07 01:31:04 +0000 | [diff] [blame] | 29 | MemRegionManager MRMgr; |
Ted Kremenek | 9deb0e3 | 2008-10-24 20:32:16 +0000 | [diff] [blame] | 30 | const MemRegion* SelfRegion; |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 31 | |
| 32 | public: |
Zhongxing Xu | bc678fd | 2008-10-07 01:31:04 +0000 | [diff] [blame] | 33 | BasicStoreManager(GRStateManager& mgr) |
Zhongxing Xu | 0adfbf6 | 2008-11-15 08:19:58 +0000 | [diff] [blame] | 34 | : VBFactory(mgr.getAllocator()), |
| 35 | StateMgr(mgr), |
| 36 | MRMgr(StateMgr.getAllocator()), |
| 37 | SelfRegion(0) {} |
Ted Kremenek | d0c4b28 | 2008-08-25 19:33:03 +0000 | [diff] [blame] | 38 | |
Ted Kremenek | 9deb0e3 | 2008-10-24 20:32:16 +0000 | [diff] [blame] | 39 | ~BasicStoreManager() {} |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 40 | |
Ted Kremenek | 2ed14be | 2008-12-05 00:47:52 +0000 | [diff] [blame] | 41 | SVal Retrieve(const GRState *state, Loc LV, QualType T); |
Ted Kremenek | 9deb0e3 | 2008-10-24 20:32:16 +0000 | [diff] [blame] | 42 | Store Bind(Store St, Loc LV, SVal V); |
| 43 | Store Remove(Store St, Loc LV); |
| 44 | Store getInitialStore(); |
| 45 | MemRegionManager& getRegionManager() { return MRMgr; } |
Zhongxing Xu | bc678fd | 2008-10-07 01:31:04 +0000 | [diff] [blame] | 46 | |
Zhongxing Xu | 6d69b5d | 2008-10-16 06:09:51 +0000 | [diff] [blame] | 47 | // FIXME: Investigate what is using this. This method should be removed. |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 48 | virtual Loc getLoc(const VarDecl* VD) { |
| 49 | return loc::MemRegionVal(MRMgr.getVarRegion(VD)); |
Zhongxing Xu | bc678fd | 2008-10-07 01:31:04 +0000 | [diff] [blame] | 50 | } |
Ted Kremenek | d9bc33e | 2008-10-17 00:51:01 +0000 | [diff] [blame] | 51 | |
Zhongxing Xu | f22679e | 2008-11-07 10:38:33 +0000 | [diff] [blame] | 52 | Store BindCompoundLiteral(Store store, const CompoundLiteralExpr* CL, |
| 53 | SVal V) { |
Ted Kremenek | 4f09027 | 2008-10-27 21:54:31 +0000 | [diff] [blame] | 54 | return store; |
| 55 | } |
| 56 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 57 | SVal getLValueVar(const GRState* St, const VarDecl* VD); |
Zhongxing Xu | 143bf82 | 2008-10-25 14:18:57 +0000 | [diff] [blame] | 58 | SVal getLValueString(const GRState* St, const StringLiteral* S); |
Zhongxing Xu | f22679e | 2008-11-07 10:38:33 +0000 | [diff] [blame] | 59 | SVal getLValueCompoundLiteral(const GRState* St, |
| 60 | const CompoundLiteralExpr* CL); |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 61 | SVal getLValueIvar(const GRState* St, const ObjCIvarDecl* D, SVal Base); |
Zhongxing Xu | c92e5fe | 2008-10-22 09:00:19 +0000 | [diff] [blame] | 62 | SVal getLValueField(const GRState* St, SVal Base, const FieldDecl* D); |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 63 | SVal getLValueElement(const GRState* St, SVal Base, SVal Offset); |
Zhongxing Xu | e1911af | 2008-10-23 03:10:39 +0000 | [diff] [blame] | 64 | |
Ted Kremenek | 9deb0e3 | 2008-10-24 20:32:16 +0000 | [diff] [blame] | 65 | /// ArrayToPointer - Used by GRExprEngine::VistCast to handle implicit |
| 66 | /// conversions between arrays and pointers. |
Zhongxing Xu | e1911af | 2008-10-23 03:10:39 +0000 | [diff] [blame] | 67 | SVal ArrayToPointer(SVal Array) { return Array; } |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 68 | |
Ted Kremenek | 6eddeb1 | 2008-12-13 21:49:13 +0000 | [diff] [blame^] | 69 | /// CastRegion - Used by GRExprEngine::VisitCast to handle casts from |
| 70 | /// a MemRegion* to a specific location type. 'R' is the region being |
| 71 | /// casted and 'CastToTy' the result type of the cast. |
| 72 | CastResult CastRegion(const GRState* state, const MemRegion* R, |
| 73 | QualType CastToTy); |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 74 | |
Ted Kremenek | 9deb0e3 | 2008-10-24 20:32:16 +0000 | [diff] [blame] | 75 | /// getSelfRegion - Returns the region for the 'self' (Objective-C) or |
| 76 | /// 'this' object (C++). When used when analyzing a normal function this |
| 77 | /// method returns NULL. |
| 78 | const MemRegion* getSelfRegion(Store) { |
| 79 | return SelfRegion; |
| 80 | } |
| 81 | |
Ted Kremenek | 2ed14be | 2008-12-05 00:47:52 +0000 | [diff] [blame] | 82 | /// RemoveDeadBindings - Scans a BasicStore of 'state' for dead values. |
| 83 | /// It returns a new Store with these values removed, and populates LSymbols |
| 84 | /// and DSymbols with the known set of live and dead symbols respectively. |
| 85 | Store RemoveDeadBindings(const GRState* state, Stmt* Loc, |
| 86 | const LiveVariables& Live, |
Ted Kremenek | 9deb0e3 | 2008-10-24 20:32:16 +0000 | [diff] [blame] | 87 | llvm::SmallVectorImpl<const MemRegion*>& RegionRoots, |
| 88 | LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols); |
Zhongxing Xu | bbe8ff4 | 2008-08-21 22:34:01 +0000 | [diff] [blame] | 89 | |
Ted Kremenek | 9deb0e3 | 2008-10-24 20:32:16 +0000 | [diff] [blame] | 90 | void iterBindings(Store store, BindingsHandler& f); |
Ted Kremenek | 60dbad8 | 2008-09-03 03:06:11 +0000 | [diff] [blame] | 91 | |
Ted Kremenek | 42577d1 | 2008-11-12 19:18:35 +0000 | [diff] [blame] | 92 | Store BindDecl(Store store, const VarDecl* VD, SVal* InitVal, unsigned Count); |
Zhongxing Xu | bbe8ff4 | 2008-08-21 22:34:01 +0000 | [diff] [blame] | 93 | |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 94 | static inline VarBindingsTy GetVarBindings(Store store) { |
| 95 | return VarBindingsTy(static_cast<const VarBindingsTy::TreeTy*>(store)); |
Ted Kremenek | a622d8c | 2008-08-19 22:24:03 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Ted Kremenek | 9deb0e3 | 2008-10-24 20:32:16 +0000 | [diff] [blame] | 98 | void print(Store store, std::ostream& Out, const char* nl, const char *sep); |
Ted Kremenek | 60dbad8 | 2008-09-03 03:06:11 +0000 | [diff] [blame] | 99 | }; |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 100 | |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 101 | } // end anonymous namespace |
| 102 | |
| 103 | |
Ted Kremenek | 5f81c44 | 2008-08-28 23:31:31 +0000 | [diff] [blame] | 104 | StoreManager* clang::CreateBasicStoreManager(GRStateManager& StMgr) { |
| 105 | return new BasicStoreManager(StMgr); |
Ted Kremenek | d0c4b28 | 2008-08-25 19:33:03 +0000 | [diff] [blame] | 106 | } |
Zhongxing Xu | 933c3e1 | 2008-10-21 06:54:23 +0000 | [diff] [blame] | 107 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 108 | SVal BasicStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) { |
| 109 | return loc::MemRegionVal(MRMgr.getVarRegion(VD)); |
Ted Kremenek | d9bc33e | 2008-10-17 00:51:01 +0000 | [diff] [blame] | 110 | } |
Zhongxing Xu | 143bf82 | 2008-10-25 14:18:57 +0000 | [diff] [blame] | 111 | |
| 112 | SVal BasicStoreManager::getLValueString(const GRState* St, |
| 113 | const StringLiteral* S) { |
| 114 | return loc::MemRegionVal(MRMgr.getStringRegion(S)); |
| 115 | } |
Zhongxing Xu | f22679e | 2008-11-07 10:38:33 +0000 | [diff] [blame] | 116 | |
| 117 | SVal BasicStoreManager::getLValueCompoundLiteral(const GRState* St, |
| 118 | const CompoundLiteralExpr* CL){ |
| 119 | return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL)); |
| 120 | } |
| 121 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 122 | SVal BasicStoreManager::getLValueIvar(const GRState* St, const ObjCIvarDecl* D, |
| 123 | SVal Base) { |
Ted Kremenek | d9bc33e | 2008-10-17 00:51:01 +0000 | [diff] [blame] | 124 | return UnknownVal(); |
| 125 | } |
| 126 | |
Ted Kremenek | 6eddeb1 | 2008-12-13 21:49:13 +0000 | [diff] [blame^] | 127 | /// CastRegion - Used by GRExprEngine::VisitCast to handle casts from |
| 128 | /// a MemRegion* to a specific location type. 'R' is the region being |
| 129 | /// casted and 'CastToTy' the result type of the cast. |
| 130 | StoreManager::CastResult |
| 131 | BasicStoreManager::CastRegion(const GRState* state, const MemRegion* R, |
| 132 | QualType CastToTy) { |
| 133 | |
| 134 | // Return the same region if the region types are compatible. |
| 135 | if (const TypedRegion* TR = dyn_cast<TypedRegion>(R)) { |
| 136 | ASTContext& Ctx = StateMgr.getContext(); |
| 137 | QualType Ta = Ctx.getCanonicalType(TR->getLValueType(Ctx)); |
| 138 | QualType Tb = Ctx.getCanonicalType(CastToTy); |
| 139 | |
| 140 | if (Ta == Tb) |
| 141 | return CastResult(state, R); |
| 142 | } |
| 143 | |
| 144 | return CastResult(state, MRMgr.getAnonTypedRegion(CastToTy, R)); |
| 145 | } |
Ted Kremenek | d9bc33e | 2008-10-17 00:51:01 +0000 | [diff] [blame] | 146 | |
Zhongxing Xu | c92e5fe | 2008-10-22 09:00:19 +0000 | [diff] [blame] | 147 | SVal BasicStoreManager::getLValueField(const GRState* St, SVal Base, |
| 148 | const FieldDecl* D) { |
Ted Kremenek | 993f1c7 | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 149 | |
| 150 | if (Base.isUnknownOrUndef()) |
| 151 | return Base; |
| 152 | |
| 153 | Loc BaseL = cast<Loc>(Base); |
| 154 | const MemRegion* BaseR = 0; |
| 155 | |
| 156 | switch(BaseL.getSubKind()) { |
| 157 | case loc::SymbolValKind: |
| 158 | BaseR = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(&BaseL)->getSymbol()); |
| 159 | break; |
| 160 | |
| 161 | case loc::GotoLabelKind: |
| 162 | case loc::FuncValKind: |
| 163 | // Technically we can get here if people do funny things with casts. |
| 164 | return UndefinedVal(); |
| 165 | |
| 166 | case loc::MemRegionKind: |
| 167 | BaseR = cast<loc::MemRegionVal>(BaseL).getRegion(); |
| 168 | break; |
| 169 | |
| 170 | case loc::ConcreteIntKind: |
Ted Kremenek | 993f1c7 | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 171 | // While these seem funny, this can happen through casts. |
| 172 | // FIXME: What we should return is the field offset. For example, |
| 173 | // add the field offset to the integer value. That way funny things |
| 174 | // like this work properly: &(((struct foo *) 0xa)->f) |
| 175 | return Base; |
| 176 | |
| 177 | default: |
| 178 | assert ("Unhandled Base."); |
| 179 | return Base; |
| 180 | } |
| 181 | |
| 182 | return loc::MemRegionVal(MRMgr.getFieldRegion(D, BaseR)); |
Ted Kremenek | d9bc33e | 2008-10-17 00:51:01 +0000 | [diff] [blame] | 183 | } |
Ted Kremenek | d0c4b28 | 2008-08-25 19:33:03 +0000 | [diff] [blame] | 184 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 185 | SVal BasicStoreManager::getLValueElement(const GRState* St, SVal Base, |
| 186 | SVal Offset) { |
Ted Kremenek | 7d71b29 | 2008-12-09 21:20:27 +0000 | [diff] [blame] | 187 | |
| 188 | |
| 189 | if (Base.isUnknownOrUndef()) |
| 190 | return Base; |
| 191 | |
| 192 | Loc BaseL = cast<Loc>(Base); |
Ted Kremenek | abb042f | 2008-12-13 19:24:37 +0000 | [diff] [blame] | 193 | const TypedRegion* BaseR = 0; |
Ted Kremenek | 7d71b29 | 2008-12-09 21:20:27 +0000 | [diff] [blame] | 194 | |
| 195 | switch(BaseL.getSubKind()) { |
Ted Kremenek | 1d6c14b | 2008-12-09 23:50:57 +0000 | [diff] [blame] | 196 | case loc::SymbolValKind: { |
| 197 | // FIXME: Should we have symbolic regions be typed or typeless? |
| 198 | // Here we assume that these regions are typeless, even though the |
| 199 | // symbol is typed. |
| 200 | SymbolRef Sym = cast<loc::SymbolVal>(&BaseL)->getSymbol(); |
| 201 | // Create a region to represent this symbol. |
| 202 | // FIXME: In the future we may just use symbolic regions instead of |
| 203 | // SymbolVals to reason about symbolic memory chunks. |
| 204 | const MemRegion* SymR = MRMgr.getSymbolicRegion(Sym); |
| 205 | // Layered a typed region on top of this. |
| 206 | QualType T = StateMgr.getSymbolManager().getType(Sym); |
| 207 | BaseR = MRMgr.getAnonTypedRegion(T, SymR); |
Ted Kremenek | 7d71b29 | 2008-12-09 21:20:27 +0000 | [diff] [blame] | 208 | break; |
Ted Kremenek | 1d6c14b | 2008-12-09 23:50:57 +0000 | [diff] [blame] | 209 | } |
Ted Kremenek | 7d71b29 | 2008-12-09 21:20:27 +0000 | [diff] [blame] | 210 | |
| 211 | case loc::GotoLabelKind: |
| 212 | case loc::FuncValKind: |
| 213 | // Technically we can get here if people do funny things with casts. |
| 214 | return UndefinedVal(); |
| 215 | |
Ted Kremenek | abb042f | 2008-12-13 19:24:37 +0000 | [diff] [blame] | 216 | case loc::MemRegionKind: { |
| 217 | const MemRegion *R = cast<loc::MemRegionVal>(BaseL).getRegion(); |
| 218 | if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) { |
| 219 | BaseR = TR; |
| 220 | break; |
| 221 | } |
| 222 | |
| 223 | // FIXME: Handle SymbolRegions? Shouldn't be possible in |
| 224 | // BasicStoreManager. |
| 225 | assert(!isa<SymbolicRegion>(R)); |
| 226 | |
Ted Kremenek | 7d71b29 | 2008-12-09 21:20:27 +0000 | [diff] [blame] | 227 | break; |
Ted Kremenek | abb042f | 2008-12-13 19:24:37 +0000 | [diff] [blame] | 228 | } |
Ted Kremenek | 7d71b29 | 2008-12-09 21:20:27 +0000 | [diff] [blame] | 229 | |
| 230 | case loc::ConcreteIntKind: |
| 231 | // While these seem funny, this can happen through casts. |
| 232 | // FIXME: What we should return is the field offset. For example, |
| 233 | // add the field offset to the integer value. That way funny things |
| 234 | // like this work properly: &(((struct foo *) 0xa)->f) |
| 235 | return Base; |
| 236 | |
| 237 | default: |
| 238 | assert ("Unhandled Base."); |
| 239 | return Base; |
| 240 | } |
| 241 | |
Ted Kremenek | abb042f | 2008-12-13 19:24:37 +0000 | [diff] [blame] | 242 | if (BaseR) |
| 243 | return loc::MemRegionVal(MRMgr.getElementRegion(UnknownVal(), BaseR)); |
| 244 | else |
| 245 | return UnknownVal(); |
Zhongxing Xu | 6d69b5d | 2008-10-16 06:09:51 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Ted Kremenek | 2ed14be | 2008-12-05 00:47:52 +0000 | [diff] [blame] | 248 | SVal BasicStoreManager::Retrieve(const GRState* state, Loc LV, QualType T) { |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 249 | |
| 250 | if (isa<UnknownVal>(LV)) |
| 251 | return UnknownVal(); |
| 252 | |
| 253 | assert (!isa<UndefinedVal>(LV)); |
| 254 | |
| 255 | switch (LV.getSubKind()) { |
| 256 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 257 | case loc::MemRegionKind: { |
Ted Kremenek | 993f1c7 | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 258 | const VarRegion* R = |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 259 | dyn_cast<VarRegion>(cast<loc::MemRegionVal>(LV).getRegion()); |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 260 | |
| 261 | if (!R) |
| 262 | return UnknownVal(); |
Ted Kremenek | 2ed14be | 2008-12-05 00:47:52 +0000 | [diff] [blame] | 263 | |
| 264 | Store store = state->getStore(); |
| 265 | VarBindingsTy B(static_cast<const VarBindingsTy::TreeTy*>(store)); |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 266 | VarBindingsTy::data_type* T = B.lookup(R->getDecl()); |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 267 | return T ? *T : UnknownVal(); |
| 268 | } |
| 269 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 270 | case loc::SymbolValKind: |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 271 | return UnknownVal(); |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 272 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 273 | case loc::ConcreteIntKind: |
| 274 | // Some clients may call GetSVal with such an option simply because |
| 275 | // they are doing a quick scan through their Locs (potentially to |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 276 | // invalidate their bindings). Just return Undefined. |
Ted Kremenek | d9bc33e | 2008-10-17 00:51:01 +0000 | [diff] [blame] | 277 | return UndefinedVal(); |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 278 | case loc::FuncValKind: |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 279 | return LV; |
| 280 | |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 281 | default: |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 282 | assert (false && "Invalid Loc."); |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 283 | break; |
| 284 | } |
| 285 | |
| 286 | return UnknownVal(); |
| 287 | } |
Ted Kremenek | 97ed4f6 | 2008-10-17 00:03:18 +0000 | [diff] [blame] | 288 | |
Zhongxing Xu | 8485ec6 | 2008-10-21 06:27:32 +0000 | [diff] [blame] | 289 | Store BasicStoreManager::Bind(Store store, Loc LV, SVal V) { |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 290 | switch (LV.getSubKind()) { |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 291 | case loc::MemRegionKind: { |
Ted Kremenek | 993f1c7 | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 292 | const VarRegion* R = |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 293 | dyn_cast<VarRegion>(cast<loc::MemRegionVal>(LV).getRegion()); |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 294 | |
| 295 | if (!R) |
| 296 | return store; |
| 297 | |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 298 | VarBindingsTy B = GetVarBindings(store); |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 299 | return V.isUnknown() |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 300 | ? VBFactory.Remove(B, R->getDecl()).getRoot() |
| 301 | : VBFactory.Add(B, R->getDecl(), V).getRoot(); |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 302 | } |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 303 | default: |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 304 | assert ("SetSVal for given Loc type not yet implemented."); |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 305 | return store; |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 306 | } |
| 307 | } |
| 308 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 309 | Store BasicStoreManager::Remove(Store store, Loc LV) { |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 310 | switch (LV.getSubKind()) { |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 311 | case loc::MemRegionKind: { |
Ted Kremenek | 993f1c7 | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 312 | const VarRegion* R = |
| 313 | dyn_cast<VarRegion>(cast<loc::MemRegionVal>(LV).getRegion()); |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 314 | |
| 315 | if (!R) |
| 316 | return store; |
| 317 | |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 318 | VarBindingsTy B = GetVarBindings(store); |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 319 | return VBFactory.Remove(B,R->getDecl()).getRoot(); |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 320 | } |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 321 | default: |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 322 | assert ("Remove for given Loc type not yet implemented."); |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 323 | return store; |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 324 | } |
| 325 | } |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 326 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 327 | Store |
Ted Kremenek | 2ed14be | 2008-12-05 00:47:52 +0000 | [diff] [blame] | 328 | BasicStoreManager::RemoveDeadBindings(const GRState* state, Stmt* Loc, |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 329 | const LiveVariables& Liveness, |
| 330 | llvm::SmallVectorImpl<const MemRegion*>& RegionRoots, |
| 331 | LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols) { |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 332 | |
Ted Kremenek | 2ed14be | 2008-12-05 00:47:52 +0000 | [diff] [blame] | 333 | Store store = state->getStore(); |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 334 | VarBindingsTy B = GetVarBindings(store); |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 335 | typedef SVal::symbol_iterator symbol_iterator; |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 336 | |
| 337 | // Iterate over the variable bindings. |
| 338 | for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) |
| 339 | if (Liveness.isLive(Loc, I.getKey())) { |
Zhongxing Xu | bc678fd | 2008-10-07 01:31:04 +0000 | [diff] [blame] | 340 | RegionRoots.push_back(MRMgr.getVarRegion(I.getKey())); |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 341 | SVal X = I.getData(); |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 342 | |
| 343 | for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI) |
| 344 | LSymbols.insert(*SI); |
| 345 | } |
| 346 | |
| 347 | // Scan for live variables and live symbols. |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 348 | llvm::SmallPtrSet<const VarRegion*, 10> Marked; |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 349 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 350 | while (!RegionRoots.empty()) { |
Ted Kremenek | 134e749 | 2008-10-17 22:52:40 +0000 | [diff] [blame] | 351 | const MemRegion* MR = RegionRoots.back(); |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 352 | RegionRoots.pop_back(); |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 353 | |
Ted Kremenek | 134e749 | 2008-10-17 22:52:40 +0000 | [diff] [blame] | 354 | while (MR) { |
| 355 | if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(MR)) { |
| 356 | LSymbols.insert(SymR->getSymbol()); |
| 357 | break; |
| 358 | } |
| 359 | else if (const VarRegion* R = dyn_cast<VarRegion>(MR)) { |
| 360 | if (Marked.count(R)) |
| 361 | break; |
| 362 | |
| 363 | Marked.insert(R); |
Ted Kremenek | 2ed14be | 2008-12-05 00:47:52 +0000 | [diff] [blame] | 364 | SVal X = GetRegionSVal(state, R); |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 365 | |
Ted Kremenek | 134e749 | 2008-10-17 22:52:40 +0000 | [diff] [blame] | 366 | // FIXME: We need to handle symbols nested in region definitions. |
| 367 | for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI) |
| 368 | LSymbols.insert(*SI); |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 369 | |
Ted Kremenek | 134e749 | 2008-10-17 22:52:40 +0000 | [diff] [blame] | 370 | if (!isa<loc::MemRegionVal>(X)) |
| 371 | break; |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 372 | |
Ted Kremenek | 134e749 | 2008-10-17 22:52:40 +0000 | [diff] [blame] | 373 | const loc::MemRegionVal& LVD = cast<loc::MemRegionVal>(X); |
| 374 | RegionRoots.push_back(LVD.getRegion()); |
| 375 | break; |
| 376 | } |
| 377 | else if (const SubRegion* R = dyn_cast<SubRegion>(MR)) |
| 378 | MR = R->getSuperRegion(); |
| 379 | else |
| 380 | break; |
| 381 | } |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | // Remove dead variable bindings. |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 385 | for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) { |
Zhongxing Xu | bc678fd | 2008-10-07 01:31:04 +0000 | [diff] [blame] | 386 | const VarRegion* R = cast<VarRegion>(MRMgr.getVarRegion(I.getKey())); |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 387 | |
| 388 | if (!Marked.count(R)) { |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 389 | store = Remove(store, loc::MemRegionVal(R)); |
| 390 | SVal X = I.getData(); |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 391 | |
| 392 | for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI) |
| 393 | if (!LSymbols.count(*SI)) DSymbols.insert(*SI); |
| 394 | } |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 395 | } |
| 396 | |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 397 | return store; |
| 398 | } |
Ted Kremenek | caa3724 | 2008-08-19 16:51:45 +0000 | [diff] [blame] | 399 | |
Zhongxing Xu | c1d1bbf | 2008-10-05 12:12:48 +0000 | [diff] [blame] | 400 | Store BasicStoreManager::getInitialStore() { |
Ted Kremenek | 9deb0e3 | 2008-10-24 20:32:16 +0000 | [diff] [blame] | 401 | |
Ted Kremenek | caa3724 | 2008-08-19 16:51:45 +0000 | [diff] [blame] | 402 | // The LiveVariables information already has a compilation of all VarDecls |
| 403 | // used in the function. Iterate through this set, and "symbolicate" |
| 404 | // any VarDecl whose value originally comes from outside the function. |
| 405 | |
| 406 | typedef LiveVariables::AnalysisDataTy LVDataTy; |
| 407 | LVDataTy& D = StateMgr.getLiveVariables().getAnalysisData(); |
| 408 | |
| 409 | Store St = VBFactory.GetEmptyMap().getRoot(); |
| 410 | |
| 411 | for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 412 | NamedDecl* ND = const_cast<NamedDecl*>(I->first); |
Ted Kremenek | caa3724 | 2008-08-19 16:51:45 +0000 | [diff] [blame] | 413 | |
Ted Kremenek | 9deb0e3 | 2008-10-24 20:32:16 +0000 | [diff] [blame] | 414 | // Handle implicit parameters. |
| 415 | if (ImplicitParamDecl* PD = dyn_cast<ImplicitParamDecl>(ND)) { |
| 416 | const Decl& CD = StateMgr.getCodeDecl(); |
| 417 | if (const ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(&CD)) { |
| 418 | if (MD->getSelfDecl() == PD) { |
| 419 | // Create a region for "self". |
| 420 | assert (SelfRegion == 0); |
| 421 | SelfRegion = MRMgr.getObjCObjectRegion(MD->getClassInterface(), |
| 422 | MRMgr.getHeapRegion()); |
| 423 | |
| 424 | St = Bind(St, loc::MemRegionVal(MRMgr.getVarRegion(PD)), |
| 425 | loc::MemRegionVal(SelfRegion)); |
| 426 | } |
| 427 | } |
| 428 | } |
| 429 | else if (VarDecl* VD = dyn_cast<VarDecl>(ND)) { |
Ted Kremenek | caa3724 | 2008-08-19 16:51:45 +0000 | [diff] [blame] | 430 | // Punt on static variables for now. |
| 431 | if (VD->getStorageClass() == VarDecl::Static) |
| 432 | continue; |
| 433 | |
| 434 | // Only handle pointers and integers for now. |
| 435 | QualType T = VD->getType(); |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 436 | if (Loc::IsLocType(T) || T->isIntegerType()) { |
Ted Kremenek | caa3724 | 2008-08-19 16:51:45 +0000 | [diff] [blame] | 437 | // Initialize globals and parameters to symbolic values. |
| 438 | // Initialize local variables to undefined. |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 439 | SVal X = (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) || |
Ted Kremenek | caa3724 | 2008-08-19 16:51:45 +0000 | [diff] [blame] | 440 | isa<ImplicitParamDecl>(VD)) |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 441 | ? SVal::GetSymbolValue(StateMgr.getSymbolManager(), VD) |
Ted Kremenek | caa3724 | 2008-08-19 16:51:45 +0000 | [diff] [blame] | 442 | : UndefinedVal(); |
| 443 | |
Zhongxing Xu | 8485ec6 | 2008-10-21 06:27:32 +0000 | [diff] [blame] | 444 | St = Bind(St, loc::MemRegionVal(MRMgr.getVarRegion(VD)), X); |
Ted Kremenek | caa3724 | 2008-08-19 16:51:45 +0000 | [diff] [blame] | 445 | } |
| 446 | } |
| 447 | } |
| 448 | return St; |
| 449 | } |
Ted Kremenek | a622d8c | 2008-08-19 22:24:03 +0000 | [diff] [blame] | 450 | |
Ted Kremenek | 42577d1 | 2008-11-12 19:18:35 +0000 | [diff] [blame] | 451 | Store BasicStoreManager::BindDecl(Store store, const VarDecl* VD, |
| 452 | SVal* InitVal, unsigned Count) { |
| 453 | |
Ted Kremenek | e53c069 | 2008-08-23 00:50:55 +0000 | [diff] [blame] | 454 | BasicValueFactory& BasicVals = StateMgr.getBasicVals(); |
Ted Kremenek | 42577d1 | 2008-11-12 19:18:35 +0000 | [diff] [blame] | 455 | |
Zhongxing Xu | bbe8ff4 | 2008-08-21 22:34:01 +0000 | [diff] [blame] | 456 | // BasicStore does not model arrays and structs. |
| 457 | if (VD->getType()->isArrayType() || VD->getType()->isStructureType()) |
| 458 | return store; |
| 459 | |
| 460 | if (VD->hasGlobalStorage()) { |
| 461 | // Handle variables with global storage: extern, static, PrivateExtern. |
| 462 | |
| 463 | // FIXME:: static variables may have an initializer, but the second time a |
| 464 | // function is called those values may not be current. Currently, a function |
| 465 | // will not be called more than once. |
| 466 | |
| 467 | // Static global variables should not be visited here. |
| 468 | assert(!(VD->getStorageClass() == VarDecl::Static && |
| 469 | VD->isFileVarDecl())); |
| 470 | |
| 471 | // Process static variables. |
| 472 | if (VD->getStorageClass() == VarDecl::Static) { |
| 473 | // C99: 6.7.8 Initialization |
| 474 | // If an object that has static storage duration is not initialized |
| 475 | // explicitly, then: |
| 476 | // —if it has pointer type, it is initialized to a null pointer; |
| 477 | // —if it has arithmetic type, it is initialized to (positive or |
| 478 | // unsigned) zero; |
Ted Kremenek | 42577d1 | 2008-11-12 19:18:35 +0000 | [diff] [blame] | 479 | if (!InitVal) { |
Zhongxing Xu | bbe8ff4 | 2008-08-21 22:34:01 +0000 | [diff] [blame] | 480 | QualType T = VD->getType(); |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 481 | if (Loc::IsLocType(T)) |
Zhongxing Xu | 8485ec6 | 2008-10-21 06:27:32 +0000 | [diff] [blame] | 482 | store = Bind(store, getLoc(VD), |
| 483 | loc::ConcreteInt(BasicVals.getValue(0, T))); |
Zhongxing Xu | bbe8ff4 | 2008-08-21 22:34:01 +0000 | [diff] [blame] | 484 | else if (T->isIntegerType()) |
Zhongxing Xu | 8485ec6 | 2008-10-21 06:27:32 +0000 | [diff] [blame] | 485 | store = Bind(store, getLoc(VD), |
| 486 | nonloc::ConcreteInt(BasicVals.getValue(0, T))); |
Zhongxing Xu | bbe8ff4 | 2008-08-21 22:34:01 +0000 | [diff] [blame] | 487 | else { |
| 488 | // assert(0 && "ignore other types of variables"); |
| 489 | } |
| 490 | } else { |
Ted Kremenek | 42577d1 | 2008-11-12 19:18:35 +0000 | [diff] [blame] | 491 | store = Bind(store, getLoc(VD), *InitVal); |
Zhongxing Xu | bbe8ff4 | 2008-08-21 22:34:01 +0000 | [diff] [blame] | 492 | } |
| 493 | } |
| 494 | } else { |
| 495 | // Process local scalar variables. |
| 496 | QualType T = VD->getType(); |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 497 | if (Loc::IsLocType(T) || T->isIntegerType()) { |
Ted Kremenek | 42577d1 | 2008-11-12 19:18:35 +0000 | [diff] [blame] | 498 | SVal V = InitVal ? *InitVal : UndefinedVal(); |
Zhongxing Xu | 8485ec6 | 2008-10-21 06:27:32 +0000 | [diff] [blame] | 499 | store = Bind(store, getLoc(VD), V); |
Zhongxing Xu | bbe8ff4 | 2008-08-21 22:34:01 +0000 | [diff] [blame] | 500 | } |
| 501 | } |
| 502 | |
| 503 | return store; |
| 504 | } |
| 505 | |
Ted Kremenek | a622d8c | 2008-08-19 22:24:03 +0000 | [diff] [blame] | 506 | void BasicStoreManager::print(Store store, std::ostream& Out, |
| 507 | const char* nl, const char *sep) { |
| 508 | |
| 509 | VarBindingsTy B = GetVarBindings(store); |
| 510 | Out << "Variables:" << nl; |
| 511 | |
| 512 | bool isFirst = true; |
| 513 | |
| 514 | for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I) { |
| 515 | if (isFirst) isFirst = false; |
| 516 | else Out << nl; |
| 517 | |
Chris Lattner | 39f34e9 | 2008-11-24 04:00:27 +0000 | [diff] [blame] | 518 | Out << ' ' << I.getKey()->getNameAsString() << " : "; |
Ted Kremenek | a622d8c | 2008-08-19 22:24:03 +0000 | [diff] [blame] | 519 | I.getData().print(Out); |
| 520 | } |
| 521 | } |
Ted Kremenek | 2bc39c6 | 2008-08-29 00:47:32 +0000 | [diff] [blame] | 522 | |
Ted Kremenek | 60dbad8 | 2008-09-03 03:06:11 +0000 | [diff] [blame] | 523 | |
| 524 | void BasicStoreManager::iterBindings(Store store, BindingsHandler& f) { |
| 525 | VarBindingsTy B = GetVarBindings(store); |
Ted Kremenek | 2bc39c6 | 2008-08-29 00:47:32 +0000 | [diff] [blame] | 526 | |
Ted Kremenek | 60dbad8 | 2008-09-03 03:06:11 +0000 | [diff] [blame] | 527 | for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I) { |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 528 | |
Zhongxing Xu | bc678fd | 2008-10-07 01:31:04 +0000 | [diff] [blame] | 529 | f.HandleBinding(*this, store, MRMgr.getVarRegion(I.getKey()),I.getData()); |
Ted Kremenek | 2bc39c6 | 2008-08-29 00:47:32 +0000 | [diff] [blame] | 530 | } |
| 531 | } |
| 532 | |
Ted Kremenek | 60dbad8 | 2008-09-03 03:06:11 +0000 | [diff] [blame] | 533 | StoreManager::BindingsHandler::~BindingsHandler() {} |