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