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 | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 14 | #include "clang/AST/ExprObjC.h" |
Ted Kremenek | 5f81c44 | 2008-08-28 23:31:31 +0000 | [diff] [blame] | 15 | #include "clang/Analysis/Analyses/LiveVariables.h" |
Zhongxing Xu | 17fd863 | 2009-08-17 06:19:58 +0000 | [diff] [blame] | 16 | #include "clang/Analysis/PathSensitive/AnalysisContext.h" |
Ted Kremenek | caa3724 | 2008-08-19 16:51:45 +0000 | [diff] [blame] | 17 | #include "clang/Analysis/PathSensitive/GRState.h" |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/ImmutableMap.h" |
| 19 | #include "llvm/Support/Compiler.h" |
Ted Kremenek | a622d8c | 2008-08-19 22:24:03 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Streams.h" |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 21 | |
| 22 | using namespace clang; |
| 23 | |
Ted Kremenek | 2c8e788 | 2009-03-05 16:32:59 +0000 | [diff] [blame] | 24 | typedef llvm::ImmutableMap<const MemRegion*,SVal> BindingsTy; |
Ted Kremenek | 60dbad8 | 2008-09-03 03:06:11 +0000 | [diff] [blame] | 25 | |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 26 | namespace { |
| 27 | |
Ted Kremenek | 59e8f11 | 2009-03-03 01:35:36 +0000 | [diff] [blame] | 28 | class VISIBILITY_HIDDEN BasicStoreSubRegionMap : public SubRegionMap { |
| 29 | public: |
| 30 | BasicStoreSubRegionMap() {} |
| 31 | |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 32 | bool iterSubRegions(const MemRegion* R, Visitor& V) const { |
Ted Kremenek | 39fc715 | 2009-03-05 16:41:21 +0000 | [diff] [blame] | 33 | return true; // Do nothing. No subregions. |
Ted Kremenek | 59e8f11 | 2009-03-03 01:35:36 +0000 | [diff] [blame] | 34 | } |
| 35 | }; |
| 36 | |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 37 | class VISIBILITY_HIDDEN BasicStoreManager : public StoreManager { |
Ted Kremenek | 2c8e788 | 2009-03-05 16:32:59 +0000 | [diff] [blame] | 38 | BindingsTy::Factory VBFactory; |
Ted Kremenek | 9deb0e3 | 2008-10-24 20:32:16 +0000 | [diff] [blame] | 39 | const MemRegion* SelfRegion; |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 40 | |
| 41 | public: |
Ted Kremenek | f7a0cf4 | 2009-07-29 21:43:22 +0000 | [diff] [blame] | 42 | BasicStoreManager(GRStateManager& mgr) |
| 43 | : StoreManager(mgr), VBFactory(mgr.getAllocator()), SelfRegion(0) {} |
Ted Kremenek | d0c4b28 | 2008-08-25 19:33:03 +0000 | [diff] [blame] | 44 | |
Ted Kremenek | 9deb0e3 | 2008-10-24 20:32:16 +0000 | [diff] [blame] | 45 | ~BasicStoreManager() {} |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 46 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 47 | SubRegionMap *getSubRegionMap(const GRState *state) { |
Ted Kremenek | 14453bf | 2009-03-03 19:02:42 +0000 | [diff] [blame] | 48 | return new BasicStoreSubRegionMap(); |
Ted Kremenek | 59e8f11 | 2009-03-03 01:35:36 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 51 | SValuator::CastResult Retrieve(const GRState *state, Loc loc, |
Ted Kremenek | 1004a9f | 2009-07-29 18:16:25 +0000 | [diff] [blame] | 52 | QualType T = QualType()); |
| 53 | |
| 54 | const GRState *InvalidateRegion(const GRState *state, const MemRegion *R, |
| 55 | const Expr *E, unsigned Count); |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 56 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 57 | const GRState *Bind(const GRState *state, Loc L, SVal V) { |
| 58 | return state->makeWithStore(BindInternal(state->getStore(), L, V)); |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 61 | Store scanForIvars(Stmt *B, const Decl* SelfDecl, Store St); |
| 62 | |
Ted Kremenek | c6ed384 | 2009-01-07 22:56:17 +0000 | [diff] [blame] | 63 | Store BindInternal(Store St, Loc loc, SVal V); |
| 64 | Store Remove(Store St, Loc loc); |
Zhongxing Xu | 17fd863 | 2009-08-17 06:19:58 +0000 | [diff] [blame] | 65 | Store getInitialStore(const LocationContext *InitLoc); |
Zhongxing Xu | bc678fd | 2008-10-07 01:31:04 +0000 | [diff] [blame] | 66 | |
Zhongxing Xu | 6d69b5d | 2008-10-16 06:09:51 +0000 | [diff] [blame] | 67 | // FIXME: Investigate what is using this. This method should be removed. |
Ted Kremenek | d17da2b | 2009-08-21 22:28:32 +0000 | [diff] [blame^] | 68 | virtual Loc getLoc(const VarDecl* VD, const LocationContext *LC) { |
| 69 | return ValMgr.makeLoc(MRMgr.getVarRegion(VD, LC)); |
Zhongxing Xu | bc678fd | 2008-10-07 01:31:04 +0000 | [diff] [blame] | 70 | } |
Ted Kremenek | d9bc33e | 2008-10-17 00:51:01 +0000 | [diff] [blame] | 71 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 72 | const GRState *BindCompoundLiteral(const GRState *state, |
| 73 | const CompoundLiteralExpr* cl, |
| 74 | SVal val) { |
| 75 | return state; |
Ted Kremenek | 4f09027 | 2008-10-27 21:54:31 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Ted Kremenek | d17da2b | 2009-08-21 22:28:32 +0000 | [diff] [blame^] | 78 | SVal getLValueVar(const GRState *state, const VarDecl *VD, |
| 79 | const LocationContext *LC); |
| 80 | SVal getLValueString(const GRState *state, const StringLiteral *S); |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 81 | SVal getLValueCompoundLiteral(const GRState *state, |
Ted Kremenek | d17da2b | 2009-08-21 22:28:32 +0000 | [diff] [blame^] | 82 | const CompoundLiteralExpr *CL); |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 83 | SVal getLValueIvar(const GRState *state, const ObjCIvarDecl* D, SVal Base); |
Ted Kremenek | d17da2b | 2009-08-21 22:28:32 +0000 | [diff] [blame^] | 84 | SVal getLValueField(const GRState *state, SVal Base, const FieldDecl *D); |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 85 | SVal getLValueElement(const GRState *state, QualType elementType, |
Ted Kremenek | f936f45 | 2009-05-04 06:18:28 +0000 | [diff] [blame] | 86 | SVal Base, SVal Offset); |
Zhongxing Xu | e1911af | 2008-10-23 03:10:39 +0000 | [diff] [blame] | 87 | |
Ted Kremenek | 9deb0e3 | 2008-10-24 20:32:16 +0000 | [diff] [blame] | 88 | /// ArrayToPointer - Used by GRExprEngine::VistCast to handle implicit |
| 89 | /// conversions between arrays and pointers. |
Zhongxing Xu | f1d537f | 2009-03-30 05:55:46 +0000 | [diff] [blame] | 90 | SVal ArrayToPointer(Loc Array) { return Array; } |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 91 | |
Ted Kremenek | 9deb0e3 | 2008-10-24 20:32:16 +0000 | [diff] [blame] | 92 | /// getSelfRegion - Returns the region for the 'self' (Objective-C) or |
| 93 | /// 'this' object (C++). When used when analyzing a normal function this |
| 94 | /// method returns NULL. |
Ted Kremenek | 39fc715 | 2009-03-05 16:41:21 +0000 | [diff] [blame] | 95 | const MemRegion* getSelfRegion(Store) { return SelfRegion; } |
Ted Kremenek | 9deb0e3 | 2008-10-24 20:32:16 +0000 | [diff] [blame] | 96 | |
Ted Kremenek | 2ed14be | 2008-12-05 00:47:52 +0000 | [diff] [blame] | 97 | /// RemoveDeadBindings - Scans a BasicStore of 'state' for dead values. |
Ted Kremenek | 2f26bc3 | 2009-08-02 04:45:08 +0000 | [diff] [blame] | 98 | /// It updatees the GRState object in place with the values removed. |
| 99 | void RemoveDeadBindings(GRState &state, Stmt* Loc, SymbolReaper& SymReaper, |
| 100 | llvm::SmallVectorImpl<const MemRegion*>& RegionRoots); |
Zhongxing Xu | bbe8ff4 | 2008-08-21 22:34:01 +0000 | [diff] [blame] | 101 | |
Ted Kremenek | 9deb0e3 | 2008-10-24 20:32:16 +0000 | [diff] [blame] | 102 | void iterBindings(Store store, BindingsHandler& f); |
Ted Kremenek | 60dbad8 | 2008-09-03 03:06:11 +0000 | [diff] [blame] | 103 | |
Ted Kremenek | d17da2b | 2009-08-21 22:28:32 +0000 | [diff] [blame^] | 104 | const GRState *BindDecl(const GRState *state, const VarDecl *VD, |
| 105 | const LocationContext *LC, SVal InitVal) { |
| 106 | return state->makeWithStore(BindDeclInternal(state->getStore(),VD, LC, |
| 107 | &InitVal)); |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Ted Kremenek | d17da2b | 2009-08-21 22:28:32 +0000 | [diff] [blame^] | 110 | const GRState *BindDeclWithNoInit(const GRState *state, const VarDecl *VD, |
| 111 | const LocationContext *LC) { |
| 112 | return state->makeWithStore(BindDeclInternal(state->getStore(), VD, LC, 0)); |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Ted Kremenek | d17da2b | 2009-08-21 22:28:32 +0000 | [diff] [blame^] | 115 | Store BindDeclInternal(Store store, const VarDecl *VD, |
| 116 | const LocationContext *LC, SVal *InitVal); |
Zhongxing Xu | bbe8ff4 | 2008-08-21 22:34:01 +0000 | [diff] [blame] | 117 | |
Ted Kremenek | 2c8e788 | 2009-03-05 16:32:59 +0000 | [diff] [blame] | 118 | static inline BindingsTy GetBindings(Store store) { |
| 119 | return BindingsTy(static_cast<const BindingsTy::TreeTy*>(store)); |
Ted Kremenek | a622d8c | 2008-08-19 22:24:03 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Ted Kremenek | 53ba0b6 | 2009-06-24 23:06:47 +0000 | [diff] [blame] | 122 | void print(Store store, llvm::raw_ostream& Out, const char* nl, |
| 123 | const char *sep); |
Zhongxing Xu | a1718c7 | 2009-04-03 07:33:13 +0000 | [diff] [blame] | 124 | |
| 125 | private: |
| 126 | ASTContext& getContext() { return StateMgr.getContext(); } |
Ted Kremenek | 60dbad8 | 2008-09-03 03:06:11 +0000 | [diff] [blame] | 127 | }; |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 128 | |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 129 | } // end anonymous namespace |
| 130 | |
| 131 | |
Ted Kremenek | 5f81c44 | 2008-08-28 23:31:31 +0000 | [diff] [blame] | 132 | StoreManager* clang::CreateBasicStoreManager(GRStateManager& StMgr) { |
| 133 | return new BasicStoreManager(StMgr); |
Ted Kremenek | d0c4b28 | 2008-08-25 19:33:03 +0000 | [diff] [blame] | 134 | } |
Zhongxing Xu | 933c3e1 | 2008-10-21 06:54:23 +0000 | [diff] [blame] | 135 | |
Ted Kremenek | d17da2b | 2009-08-21 22:28:32 +0000 | [diff] [blame^] | 136 | SVal BasicStoreManager::getLValueVar(const GRState *state, const VarDecl* VD, |
| 137 | const LocationContext *LC) { |
| 138 | return ValMgr.makeLoc(MRMgr.getVarRegion(VD, LC)); |
Ted Kremenek | d9bc33e | 2008-10-17 00:51:01 +0000 | [diff] [blame] | 139 | } |
Zhongxing Xu | 143bf82 | 2008-10-25 14:18:57 +0000 | [diff] [blame] | 140 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 141 | SVal BasicStoreManager::getLValueString(const GRState *state, |
Zhongxing Xu | 143bf82 | 2008-10-25 14:18:57 +0000 | [diff] [blame] | 142 | const StringLiteral* S) { |
Zhongxing Xu | d91ee27 | 2009-06-23 09:02:15 +0000 | [diff] [blame] | 143 | return ValMgr.makeLoc(MRMgr.getStringRegion(S)); |
Zhongxing Xu | 143bf82 | 2008-10-25 14:18:57 +0000 | [diff] [blame] | 144 | } |
Zhongxing Xu | f22679e | 2008-11-07 10:38:33 +0000 | [diff] [blame] | 145 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 146 | SVal BasicStoreManager::getLValueCompoundLiteral(const GRState *state, |
Zhongxing Xu | f22679e | 2008-11-07 10:38:33 +0000 | [diff] [blame] | 147 | const CompoundLiteralExpr* CL){ |
Zhongxing Xu | d91ee27 | 2009-06-23 09:02:15 +0000 | [diff] [blame] | 148 | return ValMgr.makeLoc(MRMgr.getCompoundLiteralRegion(CL)); |
Zhongxing Xu | f22679e | 2008-11-07 10:38:33 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 151 | SVal BasicStoreManager::getLValueIvar(const GRState *state, const ObjCIvarDecl* D, |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 152 | SVal Base) { |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 153 | |
| 154 | if (Base.isUnknownOrUndef()) |
| 155 | return Base; |
| 156 | |
| 157 | Loc BaseL = cast<Loc>(Base); |
| 158 | |
| 159 | if (isa<loc::MemRegionVal>(BaseL)) { |
| 160 | const MemRegion *BaseR = cast<loc::MemRegionVal>(BaseL).getRegion(); |
| 161 | |
| 162 | if (BaseR == SelfRegion) |
Zhongxing Xu | d91ee27 | 2009-06-23 09:02:15 +0000 | [diff] [blame] | 163 | return ValMgr.makeLoc(MRMgr.getObjCIvarRegion(D, BaseR)); |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Ted Kremenek | d9bc33e | 2008-10-17 00:51:01 +0000 | [diff] [blame] | 166 | return UnknownVal(); |
| 167 | } |
Ted Kremenek | 6eddeb1 | 2008-12-13 21:49:13 +0000 | [diff] [blame] | 168 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 169 | SVal BasicStoreManager::getLValueField(const GRState *state, SVal Base, |
Zhongxing Xu | c92e5fe | 2008-10-22 09:00:19 +0000 | [diff] [blame] | 170 | const FieldDecl* D) { |
Ted Kremenek | 993f1c7 | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 171 | |
| 172 | if (Base.isUnknownOrUndef()) |
| 173 | return Base; |
| 174 | |
| 175 | Loc BaseL = cast<Loc>(Base); |
| 176 | const MemRegion* BaseR = 0; |
| 177 | |
| 178 | switch(BaseL.getSubKind()) { |
Ted Kremenek | 993f1c7 | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 179 | case loc::GotoLabelKind: |
Ted Kremenek | 993f1c7 | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 180 | return UndefinedVal(); |
| 181 | |
| 182 | case loc::MemRegionKind: |
| 183 | BaseR = cast<loc::MemRegionVal>(BaseL).getRegion(); |
| 184 | break; |
| 185 | |
| 186 | case loc::ConcreteIntKind: |
Ted Kremenek | 993f1c7 | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 187 | // While these seem funny, this can happen through casts. |
| 188 | // FIXME: What we should return is the field offset. For example, |
| 189 | // add the field offset to the integer value. That way funny things |
| 190 | // like this work properly: &(((struct foo *) 0xa)->f) |
| 191 | return Base; |
| 192 | |
| 193 | default: |
| 194 | assert ("Unhandled Base."); |
| 195 | return Base; |
| 196 | } |
| 197 | |
Zhongxing Xu | d91ee27 | 2009-06-23 09:02:15 +0000 | [diff] [blame] | 198 | return ValMgr.makeLoc(MRMgr.getFieldRegion(D, BaseR)); |
Ted Kremenek | d9bc33e | 2008-10-17 00:51:01 +0000 | [diff] [blame] | 199 | } |
Ted Kremenek | d0c4b28 | 2008-08-25 19:33:03 +0000 | [diff] [blame] | 200 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 201 | SVal BasicStoreManager::getLValueElement(const GRState *state, |
Ted Kremenek | f936f45 | 2009-05-04 06:18:28 +0000 | [diff] [blame] | 202 | QualType elementType, |
| 203 | SVal Base, SVal Offset) { |
Ted Kremenek | 7d71b29 | 2008-12-09 21:20:27 +0000 | [diff] [blame] | 204 | |
Ted Kremenek | 7d71b29 | 2008-12-09 21:20:27 +0000 | [diff] [blame] | 205 | if (Base.isUnknownOrUndef()) |
| 206 | return Base; |
| 207 | |
| 208 | Loc BaseL = cast<Loc>(Base); |
Zhongxing Xu | 2001b8e | 2009-06-30 07:41:27 +0000 | [diff] [blame] | 209 | const MemRegion* BaseR = 0; |
Ted Kremenek | 7d71b29 | 2008-12-09 21:20:27 +0000 | [diff] [blame] | 210 | |
| 211 | switch(BaseL.getSubKind()) { |
Ted Kremenek | 7d71b29 | 2008-12-09 21:20:27 +0000 | [diff] [blame] | 212 | case loc::GotoLabelKind: |
Ted Kremenek | 7d71b29 | 2008-12-09 21:20:27 +0000 | [diff] [blame] | 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(); |
Ted Kremenek | d76d47e | 2009-01-27 18:29:03 +0000 | [diff] [blame] | 218 | |
Ted Kremenek | 921b0b5 | 2009-05-05 00:06:16 +0000 | [diff] [blame] | 219 | if (isa<ElementRegion>(R)) { |
Zhongxing Xu | b5b848e | 2009-05-04 08:52:47 +0000 | [diff] [blame] | 220 | // int x; |
| 221 | // char* y = (char*) &x; |
| 222 | // 'y' => ElementRegion(0, VarRegion('x')) |
| 223 | // y[0] = 'a'; |
Ted Kremenek | d76d47e | 2009-01-27 18:29:03 +0000 | [diff] [blame] | 224 | return Base; |
| 225 | } |
| 226 | |
Ted Kremenek | 6bf01d6 | 2009-06-30 20:24:11 +0000 | [diff] [blame] | 227 | if (isa<TypedRegion>(R) || isa<SymbolicRegion>(R)) { |
| 228 | BaseR = R; |
Ted Kremenek | abb042f | 2008-12-13 19:24:37 +0000 | [diff] [blame] | 229 | break; |
| 230 | } |
| 231 | |
Ted Kremenek | 7d71b29 | 2008-12-09 21:20:27 +0000 | [diff] [blame] | 232 | break; |
Ted Kremenek | abb042f | 2008-12-13 19:24:37 +0000 | [diff] [blame] | 233 | } |
Ted Kremenek | d76d47e | 2009-01-27 18:29:03 +0000 | [diff] [blame] | 234 | |
Ted Kremenek | 7d71b29 | 2008-12-09 21:20:27 +0000 | [diff] [blame] | 235 | case loc::ConcreteIntKind: |
| 236 | // While these seem funny, this can happen through casts. |
| 237 | // FIXME: What we should return is the field offset. For example, |
| 238 | // add the field offset to the integer value. That way funny things |
| 239 | // like this work properly: &(((struct foo *) 0xa)->f) |
| 240 | return Base; |
| 241 | |
| 242 | default: |
| 243 | assert ("Unhandled Base."); |
| 244 | return Base; |
| 245 | } |
| 246 | |
Ted Kremenek | 6bf01d6 | 2009-06-30 20:24:11 +0000 | [diff] [blame] | 247 | if (BaseR) { |
Zhongxing Xu | d91ee27 | 2009-06-23 09:02:15 +0000 | [diff] [blame] | 248 | return ValMgr.makeLoc(MRMgr.getElementRegion(elementType, UnknownVal(), |
| 249 | BaseR, getContext())); |
Ted Kremenek | 6bf01d6 | 2009-06-30 20:24:11 +0000 | [diff] [blame] | 250 | } |
Ted Kremenek | abb042f | 2008-12-13 19:24:37 +0000 | [diff] [blame] | 251 | else |
| 252 | return UnknownVal(); |
Zhongxing Xu | 6d69b5d | 2008-10-16 06:09:51 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Ted Kremenek | 5fa93d5 | 2009-04-29 16:03:27 +0000 | [diff] [blame] | 255 | static bool isHigherOrderRawPtr(QualType T, ASTContext &C) { |
Ted Kremenek | 1670e40 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 256 | bool foundPointer = false; |
| 257 | while (1) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 258 | const PointerType *PT = T->getAs<PointerType>(); |
Ted Kremenek | 1670e40 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 259 | if (!PT) { |
| 260 | if (!foundPointer) |
| 261 | return false; |
| 262 | |
Ted Kremenek | 5fa93d5 | 2009-04-29 16:03:27 +0000 | [diff] [blame] | 263 | // intptr_t* or intptr_t**, etc? |
| 264 | if (T->isIntegerType() && C.getTypeSize(T) == C.getTypeSize(C.VoidPtrTy)) |
| 265 | return true; |
| 266 | |
Ted Kremenek | 1670e40 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 267 | QualType X = C.getCanonicalType(T).getUnqualifiedType(); |
| 268 | return X == C.VoidTy; |
| 269 | } |
| 270 | |
| 271 | foundPointer = true; |
| 272 | T = PT->getPointeeType(); |
| 273 | } |
| 274 | } |
Ted Kremenek | 5fa93d5 | 2009-04-29 16:03:27 +0000 | [diff] [blame] | 275 | |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 276 | SValuator::CastResult BasicStoreManager::Retrieve(const GRState *state, |
| 277 | Loc loc, QualType T) { |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 278 | |
Ted Kremenek | c6ed384 | 2009-01-07 22:56:17 +0000 | [diff] [blame] | 279 | if (isa<UnknownVal>(loc)) |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 280 | return SValuator::CastResult(state, UnknownVal()); |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 281 | |
Ted Kremenek | c6ed384 | 2009-01-07 22:56:17 +0000 | [diff] [blame] | 282 | assert (!isa<UndefinedVal>(loc)); |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 283 | |
Ted Kremenek | c6ed384 | 2009-01-07 22:56:17 +0000 | [diff] [blame] | 284 | switch (loc.getSubKind()) { |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 285 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 286 | case loc::MemRegionKind: { |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 287 | const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion(); |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 288 | |
Ted Kremenek | 20bd746 | 2009-05-04 07:04:36 +0000 | [diff] [blame] | 289 | if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) { |
Ted Kremenek | 5fa93d5 | 2009-04-29 16:03:27 +0000 | [diff] [blame] | 290 | // Just support void**, void***, intptr_t*, intptr_t**, etc., for now. |
| 291 | // This is needed to handle OSCompareAndSwapPtr() and friends. |
Ted Kremenek | 1670e40 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 292 | ASTContext &Ctx = StateMgr.getContext(); |
Zhongxing Xu | ff69782 | 2009-05-09 00:50:33 +0000 | [diff] [blame] | 293 | QualType T = ER->getLocationType(Ctx); |
Ted Kremenek | 1670e40 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 294 | |
Ted Kremenek | 5fa93d5 | 2009-04-29 16:03:27 +0000 | [diff] [blame] | 295 | if (!isHigherOrderRawPtr(T, Ctx)) |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 296 | return SValuator::CastResult(state, UnknownVal()); |
Ted Kremenek | 1670e40 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 297 | |
Ted Kremenek | 20bd746 | 2009-05-04 07:04:36 +0000 | [diff] [blame] | 298 | // FIXME: Should check for element 0. |
| 299 | // Otherwise, strip the element region. |
| 300 | R = ER->getSuperRegion(); |
Ted Kremenek | 1670e40 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 301 | } |
| 302 | |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 303 | if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R))) |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 304 | return SValuator::CastResult(state, UnknownVal()); |
Ted Kremenek | 2ed14be | 2008-12-05 00:47:52 +0000 | [diff] [blame] | 305 | |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 306 | BindingsTy B = GetBindings(state->getStore()); |
Ted Kremenek | 2c8e788 | 2009-03-05 16:32:59 +0000 | [diff] [blame] | 307 | BindingsTy::data_type* T = B.lookup(R); |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 308 | return SValuator::CastResult(state, T ? *T : UnknownVal()); |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 309 | } |
| 310 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 311 | case loc::ConcreteIntKind: |
| 312 | // Some clients may call GetSVal with such an option simply because |
| 313 | // they are doing a quick scan through their Locs (potentially to |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 314 | // invalidate their bindings). Just return Undefined. |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 315 | return SValuator::CastResult(state, UndefinedVal()); |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 316 | |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 317 | default: |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 318 | assert (false && "Invalid Loc."); |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 319 | break; |
| 320 | } |
| 321 | |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 322 | return SValuator::CastResult(state, UnknownVal()); |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 323 | } |
Ted Kremenek | 97ed4f6 | 2008-10-17 00:03:18 +0000 | [diff] [blame] | 324 | |
Ted Kremenek | c6ed384 | 2009-01-07 22:56:17 +0000 | [diff] [blame] | 325 | Store BasicStoreManager::BindInternal(Store store, Loc loc, SVal V) { |
Zhongxing Xu | 87453d1 | 2009-06-28 10:16:11 +0000 | [diff] [blame] | 326 | if (isa<loc::ConcreteInt>(loc)) |
| 327 | return store; |
| 328 | |
Zhongxing Xu | c2a650a | 2009-06-28 09:26:15 +0000 | [diff] [blame] | 329 | const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion(); |
| 330 | ASTContext &C = StateMgr.getContext(); |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 331 | |
Zhongxing Xu | c2a650a | 2009-06-28 09:26:15 +0000 | [diff] [blame] | 332 | // Special case: handle store of pointer values (Loc) to pointers via |
| 333 | // a cast to intXX_t*, void*, etc. This is needed to handle |
| 334 | // OSCompareAndSwap32Barrier/OSCompareAndSwap64Barrier. |
| 335 | if (isa<Loc>(V) || isa<nonloc::LocAsInteger>(V)) |
| 336 | if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) { |
| 337 | // FIXME: Should check for index 0. |
| 338 | QualType T = ER->getLocationType(C); |
Ted Kremenek | 5fa93d5 | 2009-04-29 16:03:27 +0000 | [diff] [blame] | 339 | |
Zhongxing Xu | c2a650a | 2009-06-28 09:26:15 +0000 | [diff] [blame] | 340 | if (isHigherOrderRawPtr(T, C)) |
| 341 | R = ER->getSuperRegion(); |
| 342 | } |
Ted Kremenek | 5fa93d5 | 2009-04-29 16:03:27 +0000 | [diff] [blame] | 343 | |
Zhongxing Xu | c2a650a | 2009-06-28 09:26:15 +0000 | [diff] [blame] | 344 | if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R))) |
| 345 | return store; |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 346 | |
Zhongxing Xu | c2a650a | 2009-06-28 09:26:15 +0000 | [diff] [blame] | 347 | // We only track bindings to self.ivar. |
| 348 | if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R)) |
| 349 | if (IVR->getSuperRegion() != SelfRegion) |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 350 | return store; |
Zhongxing Xu | c2a650a | 2009-06-28 09:26:15 +0000 | [diff] [blame] | 351 | |
| 352 | if (nonloc::LocAsInteger *X = dyn_cast<nonloc::LocAsInteger>(&V)) { |
| 353 | // Only convert 'V' to a location iff the underlying region type |
| 354 | // is a location as well. |
| 355 | // FIXME: We are allowing a store of an arbitrary location to |
| 356 | // a pointer. We may wish to flag a type error here if the types |
| 357 | // are incompatible. This may also cause lots of breakage |
| 358 | // elsewhere. Food for thought. |
| 359 | if (const TypedRegion *TyR = dyn_cast<TypedRegion>(R)) { |
| 360 | if (TyR->isBoundable() && |
| 361 | Loc::IsLocType(TyR->getValueType(C))) |
| 362 | V = X->getLoc(); |
| 363 | } |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 364 | } |
Zhongxing Xu | c2a650a | 2009-06-28 09:26:15 +0000 | [diff] [blame] | 365 | |
| 366 | BindingsTy B = GetBindings(store); |
| 367 | return V.isUnknown() |
| 368 | ? VBFactory.Remove(B, R).getRoot() |
| 369 | : VBFactory.Add(B, R, V).getRoot(); |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 370 | } |
| 371 | |
Ted Kremenek | c6ed384 | 2009-01-07 22:56:17 +0000 | [diff] [blame] | 372 | Store BasicStoreManager::Remove(Store store, Loc loc) { |
| 373 | switch (loc.getSubKind()) { |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 374 | case loc::MemRegionKind: { |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 375 | const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion(); |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 376 | |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 377 | if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R))) |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 378 | return store; |
| 379 | |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 380 | return VBFactory.Remove(GetBindings(store), R).getRoot(); |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 381 | } |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 382 | default: |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 383 | assert ("Remove for given Loc type not yet implemented."); |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 384 | return store; |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 385 | } |
| 386 | } |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 387 | |
Ted Kremenek | 2f26bc3 | 2009-08-02 04:45:08 +0000 | [diff] [blame] | 388 | void |
| 389 | BasicStoreManager::RemoveDeadBindings(GRState &state, Stmt* Loc, |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 390 | SymbolReaper& SymReaper, |
Ted Kremenek | 2f26bc3 | 2009-08-02 04:45:08 +0000 | [diff] [blame] | 391 | llvm::SmallVectorImpl<const MemRegion*>& RegionRoots) |
| 392 | { |
| 393 | Store store = state.getStore(); |
Ted Kremenek | 2c8e788 | 2009-03-05 16:32:59 +0000 | [diff] [blame] | 394 | BindingsTy B = GetBindings(store); |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 395 | typedef SVal::symbol_iterator symbol_iterator; |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 396 | |
| 397 | // Iterate over the variable bindings. |
Ted Kremenek | 2c8e788 | 2009-03-05 16:32:59 +0000 | [diff] [blame] | 398 | for (BindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) { |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 399 | if (const VarRegion *VR = dyn_cast<VarRegion>(I.getKey())) { |
| 400 | if (SymReaper.isLive(Loc, VR->getDecl())) |
| 401 | RegionRoots.push_back(VR); |
| 402 | else |
| 403 | continue; |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 404 | } |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 405 | else if (isa<ObjCIvarRegion>(I.getKey())) { |
| 406 | RegionRoots.push_back(I.getKey()); |
| 407 | } |
| 408 | else |
| 409 | continue; |
| 410 | |
| 411 | // Mark the bindings in the data as live. |
| 412 | SVal X = I.getData(); |
| 413 | for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI) |
| 414 | SymReaper.markLive(*SI); |
Ted Kremenek | be91224 | 2009-03-05 16:31:07 +0000 | [diff] [blame] | 415 | } |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 416 | |
| 417 | // Scan for live variables and live symbols. |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 418 | llvm::SmallPtrSet<const MemRegion*, 10> Marked; |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 419 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 420 | while (!RegionRoots.empty()) { |
Ted Kremenek | 134e749 | 2008-10-17 22:52:40 +0000 | [diff] [blame] | 421 | const MemRegion* MR = RegionRoots.back(); |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 422 | RegionRoots.pop_back(); |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 423 | |
Ted Kremenek | 134e749 | 2008-10-17 22:52:40 +0000 | [diff] [blame] | 424 | while (MR) { |
| 425 | if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(MR)) { |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 426 | SymReaper.markLive(SymR->getSymbol()); |
Ted Kremenek | 134e749 | 2008-10-17 22:52:40 +0000 | [diff] [blame] | 427 | break; |
| 428 | } |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 429 | else if (isa<VarRegion>(MR) || isa<ObjCIvarRegion>(MR)) { |
| 430 | if (Marked.count(MR)) |
Ted Kremenek | 134e749 | 2008-10-17 22:52:40 +0000 | [diff] [blame] | 431 | break; |
| 432 | |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 433 | Marked.insert(MR); |
Ted Kremenek | 2f26bc3 | 2009-08-02 04:45:08 +0000 | [diff] [blame] | 434 | SVal X = Retrieve(&state, loc::MemRegionVal(MR)).getSVal(); |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 435 | |
Ted Kremenek | 134e749 | 2008-10-17 22:52:40 +0000 | [diff] [blame] | 436 | // FIXME: We need to handle symbols nested in region definitions. |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 437 | for (symbol_iterator SI=X.symbol_begin(),SE=X.symbol_end();SI!=SE;++SI) |
| 438 | SymReaper.markLive(*SI); |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 439 | |
Ted Kremenek | 134e749 | 2008-10-17 22:52:40 +0000 | [diff] [blame] | 440 | if (!isa<loc::MemRegionVal>(X)) |
| 441 | break; |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 442 | |
Ted Kremenek | 134e749 | 2008-10-17 22:52:40 +0000 | [diff] [blame] | 443 | const loc::MemRegionVal& LVD = cast<loc::MemRegionVal>(X); |
| 444 | RegionRoots.push_back(LVD.getRegion()); |
| 445 | break; |
| 446 | } |
| 447 | else if (const SubRegion* R = dyn_cast<SubRegion>(MR)) |
| 448 | MR = R->getSuperRegion(); |
| 449 | else |
| 450 | break; |
| 451 | } |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | // Remove dead variable bindings. |
Ted Kremenek | 2c8e788 | 2009-03-05 16:32:59 +0000 | [diff] [blame] | 455 | for (BindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) { |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 456 | const MemRegion* R = I.getKey(); |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 457 | |
| 458 | if (!Marked.count(R)) { |
Zhongxing Xu | d91ee27 | 2009-06-23 09:02:15 +0000 | [diff] [blame] | 459 | store = Remove(store, ValMgr.makeLoc(R)); |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 460 | SVal X = I.getData(); |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 461 | |
| 462 | for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI) |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 463 | SymReaper.maybeDead(*SI); |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 464 | } |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 465 | } |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 466 | |
Ted Kremenek | 2f26bc3 | 2009-08-02 04:45:08 +0000 | [diff] [blame] | 467 | // Write the store back. |
| 468 | state.setStore(store); |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 469 | } |
Ted Kremenek | caa3724 | 2008-08-19 16:51:45 +0000 | [diff] [blame] | 470 | |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 471 | Store BasicStoreManager::scanForIvars(Stmt *B, const Decl* SelfDecl, Store St) { |
| 472 | for (Stmt::child_iterator CI=B->child_begin(), CE=B->child_end(); |
| 473 | CI != CE; ++CI) { |
| 474 | |
| 475 | if (!*CI) |
| 476 | continue; |
| 477 | |
| 478 | // Check if the statement is an ivar reference. We only |
| 479 | // care about self.ivar. |
| 480 | if (ObjCIvarRefExpr *IV = dyn_cast<ObjCIvarRefExpr>(*CI)) { |
| 481 | const Expr *Base = IV->getBase()->IgnoreParenCasts(); |
| 482 | if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Base)) { |
| 483 | if (DR->getDecl() == SelfDecl) { |
| 484 | const MemRegion *IVR = MRMgr.getObjCIvarRegion(IV->getDecl(), |
Ted Kremenek | 8d7f548 | 2009-04-09 22:22:44 +0000 | [diff] [blame] | 485 | SelfRegion); |
Zhongxing Xu | d9b6ad6 | 2009-05-09 04:08:27 +0000 | [diff] [blame] | 486 | SVal X = ValMgr.getRegionValueSymbolVal(IVR); |
Zhongxing Xu | d91ee27 | 2009-06-23 09:02:15 +0000 | [diff] [blame] | 487 | St = BindInternal(St, ValMgr.makeLoc(IVR), X); |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 488 | } |
| 489 | } |
| 490 | } |
| 491 | else |
| 492 | St = scanForIvars(*CI, SelfDecl, St); |
| 493 | } |
Ted Kremenek | 9deb0e3 | 2008-10-24 20:32:16 +0000 | [diff] [blame] | 494 | |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 495 | return St; |
| 496 | } |
| 497 | |
Zhongxing Xu | 17fd863 | 2009-08-17 06:19:58 +0000 | [diff] [blame] | 498 | Store BasicStoreManager::getInitialStore(const LocationContext *InitLoc) { |
Ted Kremenek | caa3724 | 2008-08-19 16:51:45 +0000 | [diff] [blame] | 499 | // The LiveVariables information already has a compilation of all VarDecls |
| 500 | // used in the function. Iterate through this set, and "symbolicate" |
| 501 | // any VarDecl whose value originally comes from outside the function. |
Ted Kremenek | caa3724 | 2008-08-19 16:51:45 +0000 | [diff] [blame] | 502 | typedef LiveVariables::AnalysisDataTy LVDataTy; |
Zhongxing Xu | 17fd863 | 2009-08-17 06:19:58 +0000 | [diff] [blame] | 503 | LVDataTy& D = InitLoc->getLiveVariables()->getAnalysisData(); |
Ted Kremenek | caa3724 | 2008-08-19 16:51:45 +0000 | [diff] [blame] | 504 | Store St = VBFactory.GetEmptyMap().getRoot(); |
| 505 | |
| 506 | 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] | 507 | NamedDecl* ND = const_cast<NamedDecl*>(I->first); |
Ted Kremenek | caa3724 | 2008-08-19 16:51:45 +0000 | [diff] [blame] | 508 | |
Ted Kremenek | 9deb0e3 | 2008-10-24 20:32:16 +0000 | [diff] [blame] | 509 | // Handle implicit parameters. |
| 510 | if (ImplicitParamDecl* PD = dyn_cast<ImplicitParamDecl>(ND)) { |
Zhongxing Xu | eda9546 | 2009-08-21 02:58:11 +0000 | [diff] [blame] | 511 | const Decl& CD = *InitLoc->getDecl(); |
Ted Kremenek | 9deb0e3 | 2008-10-24 20:32:16 +0000 | [diff] [blame] | 512 | if (const ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(&CD)) { |
| 513 | if (MD->getSelfDecl() == PD) { |
| 514 | // Create a region for "self". |
| 515 | assert (SelfRegion == 0); |
| 516 | SelfRegion = MRMgr.getObjCObjectRegion(MD->getClassInterface(), |
| 517 | MRMgr.getHeapRegion()); |
| 518 | |
Ted Kremenek | d17da2b | 2009-08-21 22:28:32 +0000 | [diff] [blame^] | 519 | St = BindInternal(St, ValMgr.makeLoc(MRMgr.getVarRegion(PD, InitLoc)), |
Zhongxing Xu | d91ee27 | 2009-06-23 09:02:15 +0000 | [diff] [blame] | 520 | ValMgr.makeLoc(SelfRegion)); |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 521 | |
| 522 | // Scan the method for ivar references. While this requires an |
| 523 | // entire AST scan, the cost should not be high in practice. |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 524 | St = scanForIvars(MD->getBody(), PD, St); |
Ted Kremenek | 9deb0e3 | 2008-10-24 20:32:16 +0000 | [diff] [blame] | 525 | } |
| 526 | } |
| 527 | } |
| 528 | else if (VarDecl* VD = dyn_cast<VarDecl>(ND)) { |
Ted Kremenek | 693de5d | 2009-03-23 15:42:58 +0000 | [diff] [blame] | 529 | // Only handle simple types that we can symbolicate. |
| 530 | if (!SymbolManager::canSymbolicate(VD->getType())) |
| 531 | continue; |
Ted Kremenek | caa3724 | 2008-08-19 16:51:45 +0000 | [diff] [blame] | 532 | |
Ted Kremenek | ec099f1 | 2009-03-18 22:10:22 +0000 | [diff] [blame] | 533 | // Initialize globals and parameters to symbolic values. |
| 534 | // Initialize local variables to undefined. |
Ted Kremenek | d17da2b | 2009-08-21 22:28:32 +0000 | [diff] [blame^] | 535 | const MemRegion *R = ValMgr.getRegionManager().getVarRegion(VD, InitLoc); |
Ted Kremenek | 1508636 | 2009-07-02 18:25:09 +0000 | [diff] [blame] | 536 | SVal X = R->hasGlobalsOrParametersStorage() |
| 537 | ? ValMgr.getRegionValueSymbolVal(R) |
| 538 | : UndefinedVal(); |
Ted Kremenek | caa3724 | 2008-08-19 16:51:45 +0000 | [diff] [blame] | 539 | |
Zhongxing Xu | d91ee27 | 2009-06-23 09:02:15 +0000 | [diff] [blame] | 540 | St = BindInternal(St, ValMgr.makeLoc(R), X); |
Ted Kremenek | caa3724 | 2008-08-19 16:51:45 +0000 | [diff] [blame] | 541 | } |
| 542 | } |
| 543 | return St; |
| 544 | } |
Ted Kremenek | a622d8c | 2008-08-19 22:24:03 +0000 | [diff] [blame] | 545 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 546 | Store BasicStoreManager::BindDeclInternal(Store store, const VarDecl* VD, |
Ted Kremenek | d17da2b | 2009-08-21 22:28:32 +0000 | [diff] [blame^] | 547 | const LocationContext *LC, |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 548 | SVal* InitVal) { |
Ted Kremenek | 42577d1 | 2008-11-12 19:18:35 +0000 | [diff] [blame] | 549 | |
Ted Kremenek | e53c069 | 2008-08-23 00:50:55 +0000 | [diff] [blame] | 550 | BasicValueFactory& BasicVals = StateMgr.getBasicVals(); |
Ted Kremenek | 42577d1 | 2008-11-12 19:18:35 +0000 | [diff] [blame] | 551 | |
Zhongxing Xu | bbe8ff4 | 2008-08-21 22:34:01 +0000 | [diff] [blame] | 552 | // BasicStore does not model arrays and structs. |
| 553 | if (VD->getType()->isArrayType() || VD->getType()->isStructureType()) |
| 554 | return store; |
| 555 | |
| 556 | if (VD->hasGlobalStorage()) { |
| 557 | // Handle variables with global storage: extern, static, PrivateExtern. |
| 558 | |
| 559 | // FIXME:: static variables may have an initializer, but the second time a |
| 560 | // function is called those values may not be current. Currently, a function |
| 561 | // will not be called more than once. |
| 562 | |
| 563 | // Static global variables should not be visited here. |
| 564 | assert(!(VD->getStorageClass() == VarDecl::Static && |
| 565 | VD->isFileVarDecl())); |
| 566 | |
| 567 | // Process static variables. |
| 568 | if (VD->getStorageClass() == VarDecl::Static) { |
| 569 | // C99: 6.7.8 Initialization |
| 570 | // If an object that has static storage duration is not initialized |
| 571 | // explicitly, then: |
| 572 | // —if it has pointer type, it is initialized to a null pointer; |
| 573 | // —if it has arithmetic type, it is initialized to (positive or |
| 574 | // unsigned) zero; |
Ted Kremenek | 42577d1 | 2008-11-12 19:18:35 +0000 | [diff] [blame] | 575 | if (!InitVal) { |
Zhongxing Xu | bbe8ff4 | 2008-08-21 22:34:01 +0000 | [diff] [blame] | 576 | QualType T = VD->getType(); |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 577 | if (Loc::IsLocType(T)) |
Ted Kremenek | d17da2b | 2009-08-21 22:28:32 +0000 | [diff] [blame^] | 578 | store = BindInternal(store, getLoc(VD, LC), |
Zhongxing Xu | 8485ec6 | 2008-10-21 06:27:32 +0000 | [diff] [blame] | 579 | loc::ConcreteInt(BasicVals.getValue(0, T))); |
Zhongxing Xu | bbe8ff4 | 2008-08-21 22:34:01 +0000 | [diff] [blame] | 580 | else if (T->isIntegerType()) |
Ted Kremenek | d17da2b | 2009-08-21 22:28:32 +0000 | [diff] [blame^] | 581 | store = BindInternal(store, getLoc(VD, LC), |
Zhongxing Xu | 8485ec6 | 2008-10-21 06:27:32 +0000 | [diff] [blame] | 582 | nonloc::ConcreteInt(BasicVals.getValue(0, T))); |
Zhongxing Xu | bbe8ff4 | 2008-08-21 22:34:01 +0000 | [diff] [blame] | 583 | else { |
| 584 | // assert(0 && "ignore other types of variables"); |
| 585 | } |
| 586 | } else { |
Ted Kremenek | d17da2b | 2009-08-21 22:28:32 +0000 | [diff] [blame^] | 587 | store = BindInternal(store, getLoc(VD, LC), *InitVal); |
Zhongxing Xu | bbe8ff4 | 2008-08-21 22:34:01 +0000 | [diff] [blame] | 588 | } |
| 589 | } |
| 590 | } else { |
| 591 | // Process local scalar variables. |
| 592 | QualType T = VD->getType(); |
Ted Kremenek | f6c9f42 | 2009-07-03 00:36:16 +0000 | [diff] [blame] | 593 | if (ValMgr.getSymbolManager().canSymbolicate(T)) { |
Ted Kremenek | 42577d1 | 2008-11-12 19:18:35 +0000 | [diff] [blame] | 594 | SVal V = InitVal ? *InitVal : UndefinedVal(); |
Ted Kremenek | d17da2b | 2009-08-21 22:28:32 +0000 | [diff] [blame^] | 595 | store = BindInternal(store, getLoc(VD, LC), V); |
Zhongxing Xu | bbe8ff4 | 2008-08-21 22:34:01 +0000 | [diff] [blame] | 596 | } |
| 597 | } |
| 598 | |
| 599 | return store; |
| 600 | } |
| 601 | |
Ted Kremenek | 53ba0b6 | 2009-06-24 23:06:47 +0000 | [diff] [blame] | 602 | void BasicStoreManager::print(Store store, llvm::raw_ostream& Out, |
Ted Kremenek | a622d8c | 2008-08-19 22:24:03 +0000 | [diff] [blame] | 603 | const char* nl, const char *sep) { |
| 604 | |
Ted Kremenek | 2c8e788 | 2009-03-05 16:32:59 +0000 | [diff] [blame] | 605 | BindingsTy B = GetBindings(store); |
Ted Kremenek | a622d8c | 2008-08-19 22:24:03 +0000 | [diff] [blame] | 606 | Out << "Variables:" << nl; |
| 607 | |
| 608 | bool isFirst = true; |
| 609 | |
Ted Kremenek | 2c8e788 | 2009-03-05 16:32:59 +0000 | [diff] [blame] | 610 | for (BindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I) { |
Ted Kremenek | 53ba0b6 | 2009-06-24 23:06:47 +0000 | [diff] [blame] | 611 | if (isFirst) |
| 612 | isFirst = false; |
| 613 | else |
| 614 | Out << nl; |
Ted Kremenek | a622d8c | 2008-08-19 22:24:03 +0000 | [diff] [blame] | 615 | |
Ted Kremenek | 6f9b3a4 | 2009-07-13 23:53:06 +0000 | [diff] [blame] | 616 | Out << ' ' << I.getKey() << " : " << I.getData(); |
Ted Kremenek | a622d8c | 2008-08-19 22:24:03 +0000 | [diff] [blame] | 617 | } |
| 618 | } |
Ted Kremenek | 2bc39c6 | 2008-08-29 00:47:32 +0000 | [diff] [blame] | 619 | |
Ted Kremenek | 60dbad8 | 2008-09-03 03:06:11 +0000 | [diff] [blame] | 620 | |
| 621 | void BasicStoreManager::iterBindings(Store store, BindingsHandler& f) { |
Ted Kremenek | 2c8e788 | 2009-03-05 16:32:59 +0000 | [diff] [blame] | 622 | BindingsTy B = GetBindings(store); |
Ted Kremenek | 2bc39c6 | 2008-08-29 00:47:32 +0000 | [diff] [blame] | 623 | |
Ted Kremenek | 2c8e788 | 2009-03-05 16:32:59 +0000 | [diff] [blame] | 624 | for (BindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I) |
Ted Kremenek | be91224 | 2009-03-05 16:31:07 +0000 | [diff] [blame] | 625 | f.HandleBinding(*this, store, I.getKey(), I.getData()); |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 626 | |
Ted Kremenek | 2bc39c6 | 2008-08-29 00:47:32 +0000 | [diff] [blame] | 627 | } |
| 628 | |
Ted Kremenek | 60dbad8 | 2008-09-03 03:06:11 +0000 | [diff] [blame] | 629 | StoreManager::BindingsHandler::~BindingsHandler() {} |
Ted Kremenek | 1004a9f | 2009-07-29 18:16:25 +0000 | [diff] [blame] | 630 | |
| 631 | //===----------------------------------------------------------------------===// |
| 632 | // Binding invalidation. |
| 633 | //===----------------------------------------------------------------------===// |
| 634 | |
| 635 | const GRState *BasicStoreManager::InvalidateRegion(const GRState *state, |
| 636 | const MemRegion *R, |
| 637 | const Expr *E, |
| 638 | unsigned Count) { |
| 639 | R = R->getBaseRegion(); |
| 640 | |
| 641 | if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R))) |
| 642 | return state; |
| 643 | |
| 644 | // We only track bindings to self.ivar. |
| 645 | if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R)) |
| 646 | if (IVR->getSuperRegion() != SelfRegion) |
| 647 | return state; |
| 648 | |
| 649 | QualType T = cast<TypedRegion>(R)->getValueType(R->getContext()); |
| 650 | SVal V = ValMgr.getConjuredSymbolVal(E, T, Count); |
| 651 | return Bind(state, loc::MemRegionVal(R), V); |
| 652 | } |
| 653 | |