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 | |
Marcin Swiderski | 11b39d4 | 2010-11-16 07:15:33 +0000 | [diff] [blame] | 14 | #include "clang/AST/DeclCXX.h" |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 15 | #include "clang/AST/ExprObjC.h" |
Ted Kremenek | 5f81c44 | 2008-08-28 23:31:31 +0000 | [diff] [blame] | 16 | #include "clang/Analysis/Analyses/LiveVariables.h" |
Ted Kremenek | 1309f9a | 2010-01-25 04:41:41 +0000 | [diff] [blame] | 17 | #include "clang/Analysis/AnalysisContext.h" |
| 18 | #include "clang/Checker/PathSensitive/GRState.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 | |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 27 | class BasicStoreSubRegionMap : public SubRegionMap { |
Ted Kremenek | 59e8f11 | 2009-03-03 01:35:36 +0000 | [diff] [blame] | 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 | |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 36 | class 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 | |
Zhongxing Xu | f5416bd | 2010-02-05 05:18:47 +0000 | [diff] [blame] | 44 | SubRegionMap *getSubRegionMap(Store store) { |
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 | |
Zhongxing Xu | 576bb92 | 2010-02-05 03:01:53 +0000 | [diff] [blame] | 48 | SVal Retrieve(Store store, Loc loc, QualType T = QualType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 49 | |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 50 | Store InvalidateRegion(Store store, const MemRegion *R, const Expr *E, |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 51 | unsigned Count, InvalidatedSymbols *IS); |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 52 | |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 53 | Store InvalidateRegions(Store store, const MemRegion * const *Begin, |
| 54 | const MemRegion * const *End, const Expr *E, |
| 55 | unsigned Count, InvalidatedSymbols *IS, |
Jordy Rose | c2b7dfa | 2010-08-14 20:44:32 +0000 | [diff] [blame] | 56 | bool invalidateGlobals, InvalidatedRegions *Regions); |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 57 | |
Ted Kremenek | 82cd37c | 2009-08-21 23:25:54 +0000 | [diff] [blame] | 58 | Store scanForIvars(Stmt *B, const Decl* SelfDecl, |
| 59 | const MemRegion *SelfRegion, Store St); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 60 | |
Zhongxing Xu | b241cf6 | 2010-02-08 08:48:05 +0000 | [diff] [blame] | 61 | Store Bind(Store St, Loc loc, SVal V); |
Ted Kremenek | c6ed384 | 2009-01-07 22:56:17 +0000 | [diff] [blame] | 62 | Store Remove(Store St, Loc loc); |
Zhongxing Xu | 17fd863 | 2009-08-17 06:19:58 +0000 | [diff] [blame] | 63 | Store getInitialStore(const LocationContext *InitLoc); |
Zhongxing Xu | bc678fd | 2008-10-07 01:31:04 +0000 | [diff] [blame] | 64 | |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 65 | Store BindCompoundLiteral(Store store, const CompoundLiteralExpr*, |
| 66 | const LocationContext*, SVal val) { |
| 67 | return store; |
Ted Kremenek | 4f09027 | 2008-10-27 21:54:31 +0000 | [diff] [blame] | 68 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 69 | |
Ted Kremenek | 9deb0e3 | 2008-10-24 20:32:16 +0000 | [diff] [blame] | 70 | /// ArrayToPointer - Used by GRExprEngine::VistCast to handle implicit |
| 71 | /// conversions between arrays and pointers. |
Zhongxing Xu | f1d537f | 2009-03-30 05:55:46 +0000 | [diff] [blame] | 72 | SVal ArrayToPointer(Loc Array) { return Array; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 73 | |
Ted Kremenek | 2ed14be | 2008-12-05 00:47:52 +0000 | [diff] [blame] | 74 | /// RemoveDeadBindings - Scans a BasicStore of 'state' for dead values. |
Ted Kremenek | 2f26bc3 | 2009-08-02 04:45:08 +0000 | [diff] [blame] | 75 | /// It updatees the GRState object in place with the values removed. |
Zhongxing Xu | 5e4cb34 | 2010-08-15 12:45:09 +0000 | [diff] [blame] | 76 | Store RemoveDeadBindings(Store store, const StackFrameContext *LCtx, |
| 77 | SymbolReaper& SymReaper, |
Ted Kremenek | 2f26bc3 | 2009-08-02 04:45:08 +0000 | [diff] [blame] | 78 | llvm::SmallVectorImpl<const MemRegion*>& RegionRoots); |
Zhongxing Xu | bbe8ff4 | 2008-08-21 22:34:01 +0000 | [diff] [blame] | 79 | |
Ted Kremenek | 9deb0e3 | 2008-10-24 20:32:16 +0000 | [diff] [blame] | 80 | void iterBindings(Store store, BindingsHandler& f); |
Ted Kremenek | 60dbad8 | 2008-09-03 03:06:11 +0000 | [diff] [blame] | 81 | |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 82 | Store BindDecl(Store store, const VarRegion *VR, SVal InitVal) { |
| 83 | return BindDeclInternal(store, VR, &InitVal); |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 86 | Store BindDeclWithNoInit(Store store, const VarRegion *VR) { |
| 87 | return BindDeclInternal(store, VR, 0); |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Ted Kremenek | f6f56d4 | 2009-11-04 00:09:15 +0000 | [diff] [blame] | 90 | Store BindDeclInternal(Store store, const VarRegion *VR, SVal *InitVal); |
Zhongxing Xu | bbe8ff4 | 2008-08-21 22:34:01 +0000 | [diff] [blame] | 91 | |
Ted Kremenek | 2c8e788 | 2009-03-05 16:32:59 +0000 | [diff] [blame] | 92 | static inline BindingsTy GetBindings(Store store) { |
| 93 | return BindingsTy(static_cast<const BindingsTy::TreeTy*>(store)); |
Ted Kremenek | a622d8c | 2008-08-19 22:24:03 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Ted Kremenek | 53ba0b6 | 2009-06-24 23:06:47 +0000 | [diff] [blame] | 96 | void print(Store store, llvm::raw_ostream& Out, const char* nl, |
| 97 | const char *sep); |
Zhongxing Xu | a1718c7 | 2009-04-03 07:33:13 +0000 | [diff] [blame] | 98 | |
| 99 | private: |
Ted Kremenek | 5ba290a | 2010-03-02 21:43:54 +0000 | [diff] [blame] | 100 | SVal LazyRetrieve(Store store, const TypedRegion *R); |
Ted Kremenek | 60dbad8 | 2008-09-03 03:06:11 +0000 | [diff] [blame] | 101 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 102 | |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 103 | } // end anonymous namespace |
| 104 | |
| 105 | |
Ted Kremenek | 5f81c44 | 2008-08-28 23:31:31 +0000 | [diff] [blame] | 106 | StoreManager* clang::CreateBasicStoreManager(GRStateManager& StMgr) { |
| 107 | return new BasicStoreManager(StMgr); |
Ted Kremenek | d0c4b28 | 2008-08-25 19:33:03 +0000 | [diff] [blame] | 108 | } |
Zhongxing Xu | 933c3e1 | 2008-10-21 06:54:23 +0000 | [diff] [blame] | 109 | |
Ted Kremenek | 5fa93d5 | 2009-04-29 16:03:27 +0000 | [diff] [blame] | 110 | static bool isHigherOrderRawPtr(QualType T, ASTContext &C) { |
Ted Kremenek | 1670e40 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 111 | bool foundPointer = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 112 | while (1) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 113 | const PointerType *PT = T->getAs<PointerType>(); |
Ted Kremenek | 1670e40 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 114 | if (!PT) { |
| 115 | if (!foundPointer) |
| 116 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 117 | |
Ted Kremenek | 5fa93d5 | 2009-04-29 16:03:27 +0000 | [diff] [blame] | 118 | // intptr_t* or intptr_t**, etc? |
| 119 | if (T->isIntegerType() && C.getTypeSize(T) == C.getTypeSize(C.VoidPtrTy)) |
| 120 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 121 | |
Ted Kremenek | 1670e40 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 122 | QualType X = C.getCanonicalType(T).getUnqualifiedType(); |
| 123 | return X == C.VoidTy; |
| 124 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 125 | |
Ted Kremenek | 1670e40 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 126 | foundPointer = true; |
| 127 | T = PT->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 128 | } |
Ted Kremenek | 1670e40 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 129 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 130 | |
Ted Kremenek | 5ba290a | 2010-03-02 21:43:54 +0000 | [diff] [blame] | 131 | SVal BasicStoreManager::LazyRetrieve(Store store, const TypedRegion *R) { |
| 132 | const VarRegion *VR = dyn_cast<VarRegion>(R); |
| 133 | if (!VR) |
| 134 | return UnknownVal(); |
| 135 | |
| 136 | const VarDecl *VD = VR->getDecl(); |
| 137 | QualType T = VD->getType(); |
| 138 | |
| 139 | // Only handle simple types that we can symbolicate. |
| 140 | if (!SymbolManager::canSymbolicate(T) || !T->isScalarType()) |
| 141 | return UnknownVal(); |
| 142 | |
| 143 | // Globals and parameters start with symbolic values. |
| 144 | // Local variables initially are undefined. |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 145 | |
| 146 | // Non-static globals may have had their values reset by InvalidateRegions. |
| 147 | const MemSpaceRegion *MS = VR->getMemorySpace(); |
| 148 | if (isa<NonStaticGlobalSpaceRegion>(MS)) { |
| 149 | BindingsTy B = GetBindings(store); |
| 150 | // FIXME: Copy-and-pasted from RegionStore.cpp. |
| 151 | if (BindingsTy::data_type *Val = B.lookup(MS)) { |
| 152 | if (SymbolRef parentSym = Val->getAsSymbol()) |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame^] | 153 | return svalBuilder.getDerivedRegionValueSymbolVal(parentSym, R); |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 154 | |
| 155 | if (Val->isZeroConstant()) |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame^] | 156 | return svalBuilder.makeZeroVal(T); |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 157 | |
| 158 | if (Val->isUnknownOrUndef()) |
| 159 | return *Val; |
| 160 | |
| 161 | assert(0 && "Unknown default value."); |
| 162 | } |
| 163 | } |
| 164 | |
Ted Kremenek | 30986dd | 2010-03-10 00:18:08 +0000 | [diff] [blame] | 165 | if (VR->hasGlobalsOrParametersStorage() || |
| 166 | isa<UnknownSpaceRegion>(VR->getMemorySpace())) |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame^] | 167 | return svalBuilder.getRegionValueSymbolVal(R); |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 168 | |
Ted Kremenek | 5ba290a | 2010-03-02 21:43:54 +0000 | [diff] [blame] | 169 | return UndefinedVal(); |
| 170 | } |
| 171 | |
Zhongxing Xu | 576bb92 | 2010-02-05 03:01:53 +0000 | [diff] [blame] | 172 | SVal BasicStoreManager::Retrieve(Store store, Loc loc, QualType T) { |
Ted Kremenek | c6ed384 | 2009-01-07 22:56:17 +0000 | [diff] [blame] | 173 | if (isa<UnknownVal>(loc)) |
Zhongxing Xu | c999ed7 | 2010-02-04 02:39:47 +0000 | [diff] [blame] | 174 | return UnknownVal(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 175 | |
Ted Kremenek | 1894dce | 2009-08-25 20:51:30 +0000 | [diff] [blame] | 176 | assert(!isa<UndefinedVal>(loc)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 177 | |
Ted Kremenek | c6ed384 | 2009-01-07 22:56:17 +0000 | [diff] [blame] | 178 | switch (loc.getSubKind()) { |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 179 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 180 | case loc::MemRegionKind: { |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 181 | const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 182 | |
Marcin Swiderski | 11b39d4 | 2010-11-16 07:15:33 +0000 | [diff] [blame] | 183 | if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R) || |
| 184 | isa<CXXThisRegion>(R))) |
Zhongxing Xu | c999ed7 | 2010-02-04 02:39:47 +0000 | [diff] [blame] | 185 | return UnknownVal(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 186 | |
Zhongxing Xu | 576bb92 | 2010-02-05 03:01:53 +0000 | [diff] [blame] | 187 | BindingsTy B = GetBindings(store); |
Ted Kremenek | 1894dce | 2009-08-25 20:51:30 +0000 | [diff] [blame] | 188 | BindingsTy::data_type *Val = B.lookup(R); |
Ted Kremenek | 5ba290a | 2010-03-02 21:43:54 +0000 | [diff] [blame] | 189 | const TypedRegion *TR = cast<TypedRegion>(R); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 190 | |
Ted Kremenek | 5ba290a | 2010-03-02 21:43:54 +0000 | [diff] [blame] | 191 | if (Val) |
| 192 | return CastRetrievedVal(*Val, TR, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 193 | |
Ted Kremenek | 5ba290a | 2010-03-02 21:43:54 +0000 | [diff] [blame] | 194 | SVal V = LazyRetrieve(store, TR); |
| 195 | return V.isUnknownOrUndef() ? V : CastRetrievedVal(V, TR, T); |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 196 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 197 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 198 | case loc::ConcreteIntKind: |
Ted Kremenek | 29836f9 | 2010-11-11 23:10:10 +0000 | [diff] [blame] | 199 | // Support direct accesses to memory. It's up to individual checkers |
| 200 | // to flag an error. |
| 201 | return UnknownVal(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 202 | |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 203 | default: |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 204 | assert (false && "Invalid Loc."); |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 205 | break; |
| 206 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 207 | |
Zhongxing Xu | c999ed7 | 2010-02-04 02:39:47 +0000 | [diff] [blame] | 208 | return UnknownVal(); |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 209 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 210 | |
Zhongxing Xu | b241cf6 | 2010-02-08 08:48:05 +0000 | [diff] [blame] | 211 | Store BasicStoreManager::Bind(Store store, Loc loc, SVal V) { |
Zhongxing Xu | 87453d1 | 2009-06-28 10:16:11 +0000 | [diff] [blame] | 212 | if (isa<loc::ConcreteInt>(loc)) |
| 213 | return store; |
| 214 | |
Zhongxing Xu | c2a650a | 2009-06-28 09:26:15 +0000 | [diff] [blame] | 215 | const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion(); |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 216 | |
| 217 | // Special case: a default symbol assigned to the NonStaticGlobalsSpaceRegion |
| 218 | // that is used to derive other symbols. |
| 219 | if (isa<NonStaticGlobalSpaceRegion>(R)) { |
| 220 | BindingsTy B = GetBindings(store); |
Ted Kremenek | 3baf672 | 2010-11-24 00:54:37 +0000 | [diff] [blame] | 221 | return VBFactory.add(B, R, V).getRoot(); |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Zhongxing Xu | c2a650a | 2009-06-28 09:26:15 +0000 | [diff] [blame] | 224 | // Special case: handle store of pointer values (Loc) to pointers via |
| 225 | // a cast to intXX_t*, void*, etc. This is needed to handle |
| 226 | // OSCompareAndSwap32Barrier/OSCompareAndSwap64Barrier. |
| 227 | if (isa<Loc>(V) || isa<nonloc::LocAsInteger>(V)) |
| 228 | if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) { |
| 229 | // FIXME: Should check for index 0. |
Zhongxing Xu | 018220c | 2010-08-11 06:10:55 +0000 | [diff] [blame] | 230 | QualType T = ER->getLocationType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 231 | |
Zhongxing Xu | 57663fe | 2010-08-15 10:08:38 +0000 | [diff] [blame] | 232 | if (isHigherOrderRawPtr(T, Ctx)) |
Zhongxing Xu | c2a650a | 2009-06-28 09:26:15 +0000 | [diff] [blame] | 233 | R = ER->getSuperRegion(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 234 | } |
| 235 | |
Marcin Swiderski | 11b39d4 | 2010-11-16 07:15:33 +0000 | [diff] [blame] | 236 | if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R) || isa<CXXThisRegion>(R))) |
Zhongxing Xu | c2a650a | 2009-06-28 09:26:15 +0000 | [diff] [blame] | 237 | return store; |
Ted Kremenek | 82cd37c | 2009-08-21 23:25:54 +0000 | [diff] [blame] | 238 | |
Ted Kremenek | ab2f43c | 2009-08-25 23:29:04 +0000 | [diff] [blame] | 239 | const TypedRegion *TyR = cast<TypedRegion>(R); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 240 | |
Ted Kremenek | ab2f43c | 2009-08-25 23:29:04 +0000 | [diff] [blame] | 241 | // Do not bind to arrays. We need to explicitly check for this so that |
| 242 | // we do not encounter any weirdness of trying to load/store from arrays. |
Zhongxing Xu | 018220c | 2010-08-11 06:10:55 +0000 | [diff] [blame] | 243 | if (TyR->isBoundable() && TyR->getValueType()->isArrayType()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 244 | return store; |
Ted Kremenek | ab2f43c | 2009-08-25 23:29:04 +0000 | [diff] [blame] | 245 | |
Zhongxing Xu | c2a650a | 2009-06-28 09:26:15 +0000 | [diff] [blame] | 246 | if (nonloc::LocAsInteger *X = dyn_cast<nonloc::LocAsInteger>(&V)) { |
| 247 | // Only convert 'V' to a location iff the underlying region type |
| 248 | // is a location as well. |
| 249 | // FIXME: We are allowing a store of an arbitrary location to |
| 250 | // a pointer. We may wish to flag a type error here if the types |
| 251 | // are incompatible. This may also cause lots of breakage |
| 252 | // elsewhere. Food for thought. |
Zhongxing Xu | 018220c | 2010-08-11 06:10:55 +0000 | [diff] [blame] | 253 | if (TyR->isBoundable() && Loc::IsLocType(TyR->getValueType())) |
Ted Kremenek | ab2f43c | 2009-08-25 23:29:04 +0000 | [diff] [blame] | 254 | V = X->getLoc(); |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 255 | } |
Zhongxing Xu | c2a650a | 2009-06-28 09:26:15 +0000 | [diff] [blame] | 256 | |
| 257 | BindingsTy B = GetBindings(store); |
| 258 | return V.isUnknown() |
Ted Kremenek | 3baf672 | 2010-11-24 00:54:37 +0000 | [diff] [blame] | 259 | ? VBFactory.remove(B, R).getRoot() |
| 260 | : VBFactory.add(B, R, V).getRoot(); |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Ted Kremenek | c6ed384 | 2009-01-07 22:56:17 +0000 | [diff] [blame] | 263 | Store BasicStoreManager::Remove(Store store, Loc loc) { |
| 264 | switch (loc.getSubKind()) { |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 265 | case loc::MemRegionKind: { |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 266 | const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 267 | |
Marcin Swiderski | 11b39d4 | 2010-11-16 07:15:33 +0000 | [diff] [blame] | 268 | if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R) || |
| 269 | isa<CXXThisRegion>(R))) |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 270 | return store; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 271 | |
Ted Kremenek | 3baf672 | 2010-11-24 00:54:37 +0000 | [diff] [blame] | 272 | return VBFactory.remove(GetBindings(store), R).getRoot(); |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 273 | } |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 274 | default: |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 275 | assert ("Remove for given Loc type not yet implemented."); |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 276 | return store; |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 277 | } |
| 278 | } |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 279 | |
Zhongxing Xu | 5e4cb34 | 2010-08-15 12:45:09 +0000 | [diff] [blame] | 280 | Store BasicStoreManager::RemoveDeadBindings(Store store, |
Zhongxing Xu | 17ddf1c | 2010-03-17 03:35:08 +0000 | [diff] [blame] | 281 | const StackFrameContext *LCtx, |
Zhongxing Xu | 72119c4 | 2010-02-05 05:34:29 +0000 | [diff] [blame] | 282 | SymbolReaper& SymReaper, |
Ted Kremenek | 2f26bc3 | 2009-08-02 04:45:08 +0000 | [diff] [blame] | 283 | llvm::SmallVectorImpl<const MemRegion*>& RegionRoots) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 284 | { |
Ted Kremenek | 2c8e788 | 2009-03-05 16:32:59 +0000 | [diff] [blame] | 285 | BindingsTy B = GetBindings(store); |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 286 | typedef SVal::symbol_iterator symbol_iterator; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 287 | |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 288 | // Iterate over the variable bindings. |
Ted Kremenek | 2c8e788 | 2009-03-05 16:32:59 +0000 | [diff] [blame] | 289 | for (BindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) { |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 290 | if (const VarRegion *VR = dyn_cast<VarRegion>(I.getKey())) { |
Jordy Rose | 7dadf79 | 2010-07-01 20:09:55 +0000 | [diff] [blame] | 291 | if (SymReaper.isLive(VR)) |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 292 | RegionRoots.push_back(VR); |
| 293 | else |
| 294 | continue; |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 295 | } |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 296 | else if (isa<ObjCIvarRegion>(I.getKey()) || |
Marcin Swiderski | 11b39d4 | 2010-11-16 07:15:33 +0000 | [diff] [blame] | 297 | isa<NonStaticGlobalSpaceRegion>(I.getKey()) || |
| 298 | isa<CXXThisRegion>(I.getKey())) |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 299 | RegionRoots.push_back(I.getKey()); |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 300 | else |
| 301 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 302 | |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 303 | // Mark the bindings in the data as live. |
| 304 | SVal X = I.getData(); |
| 305 | for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI) |
| 306 | SymReaper.markLive(*SI); |
Ted Kremenek | be91224 | 2009-03-05 16:31:07 +0000 | [diff] [blame] | 307 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 308 | |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 309 | // Scan for live variables and live symbols. |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 310 | llvm::SmallPtrSet<const MemRegion*, 10> Marked; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 311 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 312 | while (!RegionRoots.empty()) { |
Ted Kremenek | 134e749 | 2008-10-17 22:52:40 +0000 | [diff] [blame] | 313 | const MemRegion* MR = RegionRoots.back(); |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 314 | RegionRoots.pop_back(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 315 | |
Ted Kremenek | 134e749 | 2008-10-17 22:52:40 +0000 | [diff] [blame] | 316 | while (MR) { |
| 317 | if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(MR)) { |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 318 | SymReaper.markLive(SymR->getSymbol()); |
Ted Kremenek | 134e749 | 2008-10-17 22:52:40 +0000 | [diff] [blame] | 319 | break; |
| 320 | } |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 321 | else if (isa<VarRegion>(MR) || isa<ObjCIvarRegion>(MR) || |
Marcin Swiderski | 11b39d4 | 2010-11-16 07:15:33 +0000 | [diff] [blame] | 322 | isa<NonStaticGlobalSpaceRegion>(MR) || isa<CXXThisRegion>(MR)) { |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 323 | if (Marked.count(MR)) |
Ted Kremenek | 134e749 | 2008-10-17 22:52:40 +0000 | [diff] [blame] | 324 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 325 | |
| 326 | Marked.insert(MR); |
Zhongxing Xu | 72119c4 | 2010-02-05 05:34:29 +0000 | [diff] [blame] | 327 | SVal X = Retrieve(store, loc::MemRegionVal(MR)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 328 | |
Ted Kremenek | 134e749 | 2008-10-17 22:52:40 +0000 | [diff] [blame] | 329 | // FIXME: We need to handle symbols nested in region definitions. |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 330 | for (symbol_iterator SI=X.symbol_begin(),SE=X.symbol_end();SI!=SE;++SI) |
| 331 | SymReaper.markLive(*SI); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 332 | |
Ted Kremenek | 134e749 | 2008-10-17 22:52:40 +0000 | [diff] [blame] | 333 | if (!isa<loc::MemRegionVal>(X)) |
| 334 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 335 | |
Ted Kremenek | 134e749 | 2008-10-17 22:52:40 +0000 | [diff] [blame] | 336 | const loc::MemRegionVal& LVD = cast<loc::MemRegionVal>(X); |
| 337 | RegionRoots.push_back(LVD.getRegion()); |
| 338 | break; |
| 339 | } |
| 340 | else if (const SubRegion* R = dyn_cast<SubRegion>(MR)) |
| 341 | MR = R->getSuperRegion(); |
| 342 | else |
| 343 | break; |
| 344 | } |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 345 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 346 | |
| 347 | // Remove dead variable bindings. |
Ted Kremenek | 2c8e788 | 2009-03-05 16:32:59 +0000 | [diff] [blame] | 348 | for (BindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) { |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 349 | const MemRegion* R = I.getKey(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 350 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 351 | if (!Marked.count(R)) { |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame^] | 352 | store = Remove(store, svalBuilder.makeLoc(R)); |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 353 | SVal X = I.getData(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 354 | |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 355 | 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] | 356 | SymReaper.maybeDead(*SI); |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 357 | } |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 358 | } |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 359 | |
Zhongxing Xu | 5e4cb34 | 2010-08-15 12:45:09 +0000 | [diff] [blame] | 360 | return store; |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 361 | } |
Ted Kremenek | caa3724 | 2008-08-19 16:51:45 +0000 | [diff] [blame] | 362 | |
Ted Kremenek | 82cd37c | 2009-08-21 23:25:54 +0000 | [diff] [blame] | 363 | Store BasicStoreManager::scanForIvars(Stmt *B, const Decl* SelfDecl, |
| 364 | const MemRegion *SelfRegion, Store St) { |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 365 | for (Stmt::child_iterator CI=B->child_begin(), CE=B->child_end(); |
| 366 | CI != CE; ++CI) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 367 | |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 368 | if (!*CI) |
| 369 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 370 | |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 371 | // Check if the statement is an ivar reference. We only |
| 372 | // care about self.ivar. |
| 373 | if (ObjCIvarRefExpr *IV = dyn_cast<ObjCIvarRefExpr>(*CI)) { |
| 374 | const Expr *Base = IV->getBase()->IgnoreParenCasts(); |
| 375 | if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Base)) { |
| 376 | if (DR->getDecl() == SelfDecl) { |
Zhongxing Xu | 14d2328 | 2010-03-01 06:56:52 +0000 | [diff] [blame] | 377 | const ObjCIvarRegion *IVR = MRMgr.getObjCIvarRegion(IV->getDecl(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 378 | SelfRegion); |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame^] | 379 | SVal X = svalBuilder.getRegionValueSymbolVal(IVR); |
| 380 | St = Bind(St, svalBuilder.makeLoc(IVR), X); |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 381 | } |
| 382 | } |
| 383 | } |
| 384 | else |
Ted Kremenek | 82cd37c | 2009-08-21 23:25:54 +0000 | [diff] [blame] | 385 | St = scanForIvars(*CI, SelfDecl, SelfRegion, St); |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 386 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 387 | |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 388 | return St; |
| 389 | } |
| 390 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 391 | Store BasicStoreManager::getInitialStore(const LocationContext *InitLoc) { |
Ted Kremenek | caa3724 | 2008-08-19 16:51:45 +0000 | [diff] [blame] | 392 | // The LiveVariables information already has a compilation of all VarDecls |
| 393 | // used in the function. Iterate through this set, and "symbolicate" |
| 394 | // any VarDecl whose value originally comes from outside the function. |
Ted Kremenek | caa3724 | 2008-08-19 16:51:45 +0000 | [diff] [blame] | 395 | typedef LiveVariables::AnalysisDataTy LVDataTy; |
Zhongxing Xu | 17fd863 | 2009-08-17 06:19:58 +0000 | [diff] [blame] | 396 | LVDataTy& D = InitLoc->getLiveVariables()->getAnalysisData(); |
Ted Kremenek | 3baf672 | 2010-11-24 00:54:37 +0000 | [diff] [blame] | 397 | Store St = VBFactory.getEmptyMap().getRoot(); |
Ted Kremenek | caa3724 | 2008-08-19 16:51:45 +0000 | [diff] [blame] | 398 | |
| 399 | for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) { |
Zhongxing Xu | ce2bc7e3 | 2010-07-20 02:46:11 +0000 | [diff] [blame] | 400 | const NamedDecl* ND = I->first; |
Ted Kremenek | caa3724 | 2008-08-19 16:51:45 +0000 | [diff] [blame] | 401 | |
Ted Kremenek | 9deb0e3 | 2008-10-24 20:32:16 +0000 | [diff] [blame] | 402 | // Handle implicit parameters. |
Zhongxing Xu | ce2bc7e3 | 2010-07-20 02:46:11 +0000 | [diff] [blame] | 403 | if (const ImplicitParamDecl* PD = dyn_cast<ImplicitParamDecl>(ND)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 404 | const Decl& CD = *InitLoc->getDecl(); |
Ted Kremenek | 9deb0e3 | 2008-10-24 20:32:16 +0000 | [diff] [blame] | 405 | if (const ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(&CD)) { |
| 406 | if (MD->getSelfDecl() == PD) { |
Ted Kremenek | c410d4d | 2009-12-16 23:53:37 +0000 | [diff] [blame] | 407 | // FIXME: Add type constraints (when they become available) to |
| 408 | // SelfRegion? (i.e., it implements MD->getClassInterface()). |
Zhongxing Xu | 14d2328 | 2010-03-01 06:56:52 +0000 | [diff] [blame] | 409 | const VarRegion *VR = MRMgr.getVarRegion(PD, InitLoc); |
Ted Kremenek | c410d4d | 2009-12-16 23:53:37 +0000 | [diff] [blame] | 410 | const MemRegion *SelfRegion = |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame^] | 411 | svalBuilder.getRegionValueSymbolVal(VR).getAsRegion(); |
Ted Kremenek | 5ba290a | 2010-03-02 21:43:54 +0000 | [diff] [blame] | 412 | assert(SelfRegion); |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame^] | 413 | St = Bind(St, svalBuilder.makeLoc(VR), loc::MemRegionVal(SelfRegion)); |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 414 | // Scan the method for ivar references. While this requires an |
| 415 | // entire AST scan, the cost should not be high in practice. |
Ted Kremenek | 82cd37c | 2009-08-21 23:25:54 +0000 | [diff] [blame] | 416 | St = scanForIvars(MD->getBody(), PD, SelfRegion, St); |
Ted Kremenek | 9deb0e3 | 2008-10-24 20:32:16 +0000 | [diff] [blame] | 417 | } |
| 418 | } |
| 419 | } |
Ted Kremenek | caa3724 | 2008-08-19 16:51:45 +0000 | [diff] [blame] | 420 | } |
Ted Kremenek | 5ba290a | 2010-03-02 21:43:54 +0000 | [diff] [blame] | 421 | |
Marcin Swiderski | 0aac2ba | 2010-11-17 06:22:54 +0000 | [diff] [blame] | 422 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(InitLoc->getDecl())) { |
| 423 | // For C++ methods add symbolic region for 'this' in initial stack frame. |
| 424 | QualType ThisT = MD->getThisType(StateMgr.getContext()); |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame^] | 425 | MemRegionManager &RegMgr = svalBuilder.getRegionManager(); |
Marcin Swiderski | 0aac2ba | 2010-11-17 06:22:54 +0000 | [diff] [blame] | 426 | const CXXThisRegion *ThisR = RegMgr.getCXXThisRegion(ThisT, InitLoc); |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame^] | 427 | SVal ThisV = svalBuilder.getRegionValueSymbolVal(ThisR); |
| 428 | St = Bind(St, svalBuilder.makeLoc(ThisR), ThisV); |
Marcin Swiderski | 0aac2ba | 2010-11-17 06:22:54 +0000 | [diff] [blame] | 429 | } |
| 430 | |
Ted Kremenek | caa3724 | 2008-08-19 16:51:45 +0000 | [diff] [blame] | 431 | return St; |
| 432 | } |
Ted Kremenek | a622d8c | 2008-08-19 22:24:03 +0000 | [diff] [blame] | 433 | |
Ted Kremenek | f6f56d4 | 2009-11-04 00:09:15 +0000 | [diff] [blame] | 434 | Store BasicStoreManager::BindDeclInternal(Store store, const VarRegion* VR, |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 435 | SVal* InitVal) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 436 | |
Ted Kremenek | e53c069 | 2008-08-23 00:50:55 +0000 | [diff] [blame] | 437 | BasicValueFactory& BasicVals = StateMgr.getBasicVals(); |
Ted Kremenek | f6f56d4 | 2009-11-04 00:09:15 +0000 | [diff] [blame] | 438 | const VarDecl *VD = VR->getDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 439 | |
Zhongxing Xu | bbe8ff4 | 2008-08-21 22:34:01 +0000 | [diff] [blame] | 440 | // BasicStore does not model arrays and structs. |
Douglas Gregor | fb87b89 | 2010-04-26 21:31:17 +0000 | [diff] [blame] | 441 | if (VD->getType()->isArrayType() || VD->getType()->isStructureOrClassType()) |
Zhongxing Xu | bbe8ff4 | 2008-08-21 22:34:01 +0000 | [diff] [blame] | 442 | return store; |
| 443 | |
| 444 | if (VD->hasGlobalStorage()) { |
| 445 | // Handle variables with global storage: extern, static, PrivateExtern. |
| 446 | |
| 447 | // FIXME:: static variables may have an initializer, but the second time a |
| 448 | // function is called those values may not be current. Currently, a function |
| 449 | // will not be called more than once. |
| 450 | |
| 451 | // Static global variables should not be visited here. |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 452 | assert(!(VD->getStorageClass() == SC_Static && |
Zhongxing Xu | bbe8ff4 | 2008-08-21 22:34:01 +0000 | [diff] [blame] | 453 | VD->isFileVarDecl())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 454 | |
Zhongxing Xu | bbe8ff4 | 2008-08-21 22:34:01 +0000 | [diff] [blame] | 455 | // Process static variables. |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 456 | if (VD->getStorageClass() == SC_Static) { |
Zhongxing Xu | bbe8ff4 | 2008-08-21 22:34:01 +0000 | [diff] [blame] | 457 | // C99: 6.7.8 Initialization |
| 458 | // If an object that has static storage duration is not initialized |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 459 | // explicitly, then: |
| 460 | // —if it has pointer type, it is initialized to a null pointer; |
| 461 | // —if it has arithmetic type, it is initialized to (positive or |
Zhongxing Xu | bbe8ff4 | 2008-08-21 22:34:01 +0000 | [diff] [blame] | 462 | // unsigned) zero; |
Ted Kremenek | 42577d1 | 2008-11-12 19:18:35 +0000 | [diff] [blame] | 463 | if (!InitVal) { |
Zhongxing Xu | bbe8ff4 | 2008-08-21 22:34:01 +0000 | [diff] [blame] | 464 | QualType T = VD->getType(); |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 465 | if (Loc::IsLocType(T)) |
Zhongxing Xu | b241cf6 | 2010-02-08 08:48:05 +0000 | [diff] [blame] | 466 | store = Bind(store, loc::MemRegionVal(VR), |
Zhongxing Xu | 8485ec6 | 2008-10-21 06:27:32 +0000 | [diff] [blame] | 467 | loc::ConcreteInt(BasicVals.getValue(0, T))); |
Ted Kremenek | c070843 | 2010-07-29 00:28:40 +0000 | [diff] [blame] | 468 | else if (T->isIntegerType() && T->isScalarType()) |
Zhongxing Xu | b241cf6 | 2010-02-08 08:48:05 +0000 | [diff] [blame] | 469 | store = Bind(store, loc::MemRegionVal(VR), |
Zhongxing Xu | 8485ec6 | 2008-10-21 06:27:32 +0000 | [diff] [blame] | 470 | nonloc::ConcreteInt(BasicVals.getValue(0, T))); |
Zhongxing Xu | bbe8ff4 | 2008-08-21 22:34:01 +0000 | [diff] [blame] | 471 | } else { |
Zhongxing Xu | b241cf6 | 2010-02-08 08:48:05 +0000 | [diff] [blame] | 472 | store = Bind(store, loc::MemRegionVal(VR), *InitVal); |
Zhongxing Xu | bbe8ff4 | 2008-08-21 22:34:01 +0000 | [diff] [blame] | 473 | } |
| 474 | } |
| 475 | } else { |
| 476 | // Process local scalar variables. |
| 477 | QualType T = VD->getType(); |
Ted Kremenek | c070843 | 2010-07-29 00:28:40 +0000 | [diff] [blame] | 478 | // BasicStore only supports scalars. |
Marcin Swiderski | 11b39d4 | 2010-11-16 07:15:33 +0000 | [diff] [blame] | 479 | if ((T->isScalarType() || T->isReferenceType()) && |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame^] | 480 | svalBuilder.getSymbolManager().canSymbolicate(T)) { |
Ted Kremenek | 42577d1 | 2008-11-12 19:18:35 +0000 | [diff] [blame] | 481 | SVal V = InitVal ? *InitVal : UndefinedVal(); |
Zhongxing Xu | b241cf6 | 2010-02-08 08:48:05 +0000 | [diff] [blame] | 482 | store = Bind(store, loc::MemRegionVal(VR), V); |
Zhongxing Xu | bbe8ff4 | 2008-08-21 22:34:01 +0000 | [diff] [blame] | 483 | } |
| 484 | } |
| 485 | |
| 486 | return store; |
| 487 | } |
| 488 | |
Ted Kremenek | 53ba0b6 | 2009-06-24 23:06:47 +0000 | [diff] [blame] | 489 | void BasicStoreManager::print(Store store, llvm::raw_ostream& Out, |
Ted Kremenek | a622d8c | 2008-08-19 22:24:03 +0000 | [diff] [blame] | 490 | const char* nl, const char *sep) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 491 | |
Ted Kremenek | 2c8e788 | 2009-03-05 16:32:59 +0000 | [diff] [blame] | 492 | BindingsTy B = GetBindings(store); |
Ted Kremenek | a622d8c | 2008-08-19 22:24:03 +0000 | [diff] [blame] | 493 | Out << "Variables:" << nl; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 494 | |
Ted Kremenek | a622d8c | 2008-08-19 22:24:03 +0000 | [diff] [blame] | 495 | bool isFirst = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 496 | |
Ted Kremenek | 2c8e788 | 2009-03-05 16:32:59 +0000 | [diff] [blame] | 497 | for (BindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I) { |
Ted Kremenek | 53ba0b6 | 2009-06-24 23:06:47 +0000 | [diff] [blame] | 498 | if (isFirst) |
| 499 | isFirst = false; |
| 500 | else |
| 501 | Out << nl; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 502 | |
Ted Kremenek | 6f9b3a4 | 2009-07-13 23:53:06 +0000 | [diff] [blame] | 503 | Out << ' ' << I.getKey() << " : " << I.getData(); |
Ted Kremenek | a622d8c | 2008-08-19 22:24:03 +0000 | [diff] [blame] | 504 | } |
| 505 | } |
Ted Kremenek | 2bc39c6 | 2008-08-29 00:47:32 +0000 | [diff] [blame] | 506 | |
Ted Kremenek | 60dbad8 | 2008-09-03 03:06:11 +0000 | [diff] [blame] | 507 | |
| 508 | void BasicStoreManager::iterBindings(Store store, BindingsHandler& f) { |
Ted Kremenek | 2c8e788 | 2009-03-05 16:32:59 +0000 | [diff] [blame] | 509 | BindingsTy B = GetBindings(store); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 510 | |
Ted Kremenek | 2c8e788 | 2009-03-05 16:32:59 +0000 | [diff] [blame] | 511 | for (BindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I) |
Ted Kremenek | 744b304 | 2010-06-17 00:24:37 +0000 | [diff] [blame] | 512 | if (!f.HandleBinding(*this, store, I.getKey(), I.getData())) |
| 513 | return; |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 514 | |
Ted Kremenek | 2bc39c6 | 2008-08-29 00:47:32 +0000 | [diff] [blame] | 515 | } |
| 516 | |
Ted Kremenek | 60dbad8 | 2008-09-03 03:06:11 +0000 | [diff] [blame] | 517 | StoreManager::BindingsHandler::~BindingsHandler() {} |
Ted Kremenek | 1004a9f | 2009-07-29 18:16:25 +0000 | [diff] [blame] | 518 | |
| 519 | //===----------------------------------------------------------------------===// |
| 520 | // Binding invalidation. |
| 521 | //===----------------------------------------------------------------------===// |
| 522 | |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 523 | |
| 524 | Store BasicStoreManager::InvalidateRegions(Store store, |
Jordy Rose | c2b7dfa | 2010-08-14 20:44:32 +0000 | [diff] [blame] | 525 | const MemRegion * const *I, |
| 526 | const MemRegion * const *End, |
| 527 | const Expr *E, unsigned Count, |
| 528 | InvalidatedSymbols *IS, |
| 529 | bool invalidateGlobals, |
| 530 | InvalidatedRegions *Regions) { |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 531 | if (invalidateGlobals) { |
| 532 | BindingsTy B = GetBindings(store); |
| 533 | for (BindingsTy::iterator I=B.begin(), End=B.end(); I != End; ++I) { |
| 534 | const MemRegion *R = I.getKey(); |
| 535 | if (isa<NonStaticGlobalSpaceRegion>(R->getMemorySpace())) |
| 536 | store = InvalidateRegion(store, R, E, Count, IS); |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | for ( ; I != End ; ++I) { |
| 541 | const MemRegion *R = *I; |
| 542 | // Don't invalidate globals twice. |
| 543 | if (invalidateGlobals) { |
| 544 | if (isa<NonStaticGlobalSpaceRegion>(R->getMemorySpace())) |
| 545 | continue; |
| 546 | } |
| 547 | store = InvalidateRegion(store, *I, E, Count, IS); |
Jordy Rose | c2b7dfa | 2010-08-14 20:44:32 +0000 | [diff] [blame] | 548 | if (Regions) |
| 549 | Regions->push_back(R); |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | // FIXME: This is copy-and-paste from RegionStore.cpp. |
| 553 | if (invalidateGlobals) { |
| 554 | // Bind the non-static globals memory space to a new symbol that we will |
| 555 | // use to derive the bindings for all non-static globals. |
| 556 | const GlobalsSpaceRegion *GS = MRMgr.getGlobalsRegion(); |
| 557 | SVal V = |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame^] | 558 | svalBuilder.getConjuredSymbolVal(/* SymbolTag = */ (void*) GS, E, |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 559 | /* symbol type, doesn't matter */ Ctx.IntTy, |
| 560 | Count); |
| 561 | |
| 562 | store = Bind(store, loc::MemRegionVal(GS), V); |
Jordy Rose | c2b7dfa | 2010-08-14 20:44:32 +0000 | [diff] [blame] | 563 | if (Regions) |
| 564 | Regions->push_back(GS); |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | return store; |
| 568 | } |
| 569 | |
| 570 | |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 571 | Store BasicStoreManager::InvalidateRegion(Store store, |
| 572 | const MemRegion *R, |
| 573 | const Expr *E, |
| 574 | unsigned Count, |
| 575 | InvalidatedSymbols *IS) { |
Zhongxing Xu | 479529e | 2009-11-10 02:17:20 +0000 | [diff] [blame] | 576 | R = R->StripCasts(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 577 | |
Ted Kremenek | 1004a9f | 2009-07-29 18:16:25 +0000 | [diff] [blame] | 578 | if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R))) |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 579 | return store; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 580 | |
Ted Kremenek | 473e167 | 2009-10-16 00:30:49 +0000 | [diff] [blame] | 581 | if (IS) { |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 582 | BindingsTy B = GetBindings(store); |
Ted Kremenek | 473e167 | 2009-10-16 00:30:49 +0000 | [diff] [blame] | 583 | if (BindingsTy::data_type *Val = B.lookup(R)) { |
| 584 | if (SymbolRef Sym = Val->getAsSymbol()) |
| 585 | IS->insert(Sym); |
| 586 | } |
| 587 | } |
| 588 | |
Zhongxing Xu | 018220c | 2010-08-11 06:10:55 +0000 | [diff] [blame] | 589 | QualType T = cast<TypedRegion>(R)->getValueType(); |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame^] | 590 | SVal V = svalBuilder.getConjuredSymbolVal(R, E, T, Count); |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 591 | return Bind(store, loc::MemRegionVal(R), V); |
Ted Kremenek | 1004a9f | 2009-07-29 18:16:25 +0000 | [diff] [blame] | 592 | } |
| 593 | |