blob: f82e1b20be9b90ac5b2aa27b065a0d79843974c3 [file] [log] [blame]
Ted Kremenek4323a572008-07-10 22:03:41 +00001//== 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 Kremenekf684d562009-03-05 18:08:28 +000014#include "clang/AST/ExprObjC.h"
Ted Kremenek5f81c442008-08-28 23:31:31 +000015#include "clang/Analysis/Analyses/LiveVariables.h"
Ted Kremenek1309f9a2010-01-25 04:41:41 +000016#include "clang/Analysis/AnalysisContext.h"
17#include "clang/Checker/PathSensitive/GRState.h"
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +000018#include "llvm/ADT/ImmutableMap.h"
Ted Kremenek4323a572008-07-10 22:03:41 +000019
20using namespace clang;
21
Mike Stump1eb44332009-09-09 15:08:12 +000022typedef llvm::ImmutableMap<const MemRegion*,SVal> BindingsTy;
Ted Kremenek60dbad82008-09-03 03:06:11 +000023
Ted Kremenek4323a572008-07-10 22:03:41 +000024namespace {
Mike Stump1eb44332009-09-09 15:08:12 +000025
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000026class BasicStoreSubRegionMap : public SubRegionMap {
Ted Kremenek59e8f112009-03-03 01:35:36 +000027public:
28 BasicStoreSubRegionMap() {}
29
Ted Kremenek5dc27462009-03-03 02:51:43 +000030 bool iterSubRegions(const MemRegion* R, Visitor& V) const {
Ted Kremenek39fc7152009-03-05 16:41:21 +000031 return true; // Do nothing. No subregions.
Ted Kremenek59e8f112009-03-03 01:35:36 +000032 }
33};
Mike Stump1eb44332009-09-09 15:08:12 +000034
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000035class BasicStoreManager : public StoreManager {
Ted Kremenek2c8e7882009-03-05 16:32:59 +000036 BindingsTy::Factory VBFactory;
Ted Kremenek4323a572008-07-10 22:03:41 +000037public:
Ted Kremenekf7a0cf42009-07-29 21:43:22 +000038 BasicStoreManager(GRStateManager& mgr)
Ted Kremenek82cd37c2009-08-21 23:25:54 +000039 : StoreManager(mgr), VBFactory(mgr.getAllocator()) {}
Mike Stump1eb44332009-09-09 15:08:12 +000040
Ted Kremenek9deb0e32008-10-24 20:32:16 +000041 ~BasicStoreManager() {}
Ted Kremenek4323a572008-07-10 22:03:41 +000042
Zhongxing Xuf5416bd2010-02-05 05:18:47 +000043 SubRegionMap *getSubRegionMap(Store store) {
Ted Kremenek14453bf2009-03-03 19:02:42 +000044 return new BasicStoreSubRegionMap();
Ted Kremenek59e8f112009-03-03 01:35:36 +000045 }
46
Zhongxing Xu576bb922010-02-05 03:01:53 +000047 SVal Retrieve(Store store, Loc loc, QualType T = QualType());
Mike Stump1eb44332009-09-09 15:08:12 +000048
Ted Kremenekdcee3ce2010-07-01 20:16:50 +000049 Store InvalidateRegion(Store store, const MemRegion *R, const Expr *E,
Zhongxing Xub4a9c612010-02-05 05:06:13 +000050 unsigned Count, InvalidatedSymbols *IS);
Zhongxing Xu4193eca2008-12-20 06:32:12 +000051
Ted Kremenekdcee3ce2010-07-01 20:16:50 +000052 Store InvalidateRegions(Store store, const MemRegion * const *Begin,
53 const MemRegion * const *End, const Expr *E,
54 unsigned Count, InvalidatedSymbols *IS,
Jordy Rosec2b7dfa2010-08-14 20:44:32 +000055 bool invalidateGlobals, InvalidatedRegions *Regions);
Ted Kremenekdcee3ce2010-07-01 20:16:50 +000056
Ted Kremenek82cd37c2009-08-21 23:25:54 +000057 Store scanForIvars(Stmt *B, const Decl* SelfDecl,
58 const MemRegion *SelfRegion, Store St);
Mike Stump1eb44332009-09-09 15:08:12 +000059
Zhongxing Xub241cf62010-02-08 08:48:05 +000060 Store Bind(Store St, Loc loc, SVal V);
Ted Kremenekc6ed3842009-01-07 22:56:17 +000061 Store Remove(Store St, Loc loc);
Zhongxing Xu17fd8632009-08-17 06:19:58 +000062 Store getInitialStore(const LocationContext *InitLoc);
Zhongxing Xubc678fd2008-10-07 01:31:04 +000063
Zhongxing Xub4a9c612010-02-05 05:06:13 +000064 Store BindCompoundLiteral(Store store, const CompoundLiteralExpr*,
65 const LocationContext*, SVal val) {
66 return store;
Ted Kremenek4f090272008-10-27 21:54:31 +000067 }
Mike Stump1eb44332009-09-09 15:08:12 +000068
Ted Kremenek9deb0e32008-10-24 20:32:16 +000069 /// ArrayToPointer - Used by GRExprEngine::VistCast to handle implicit
70 /// conversions between arrays and pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +000071 SVal ArrayToPointer(Loc Array) { return Array; }
Mike Stump1eb44332009-09-09 15:08:12 +000072
Ted Kremenek2ed14be2008-12-05 00:47:52 +000073 /// RemoveDeadBindings - Scans a BasicStore of 'state' for dead values.
Ted Kremenek2f26bc32009-08-02 04:45:08 +000074 /// It updatees the GRState object in place with the values removed.
Zhongxing Xu5e4cb342010-08-15 12:45:09 +000075 Store RemoveDeadBindings(Store store, const StackFrameContext *LCtx,
76 SymbolReaper& SymReaper,
Ted Kremenek2f26bc32009-08-02 04:45:08 +000077 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +000078
Ted Kremenek9deb0e32008-10-24 20:32:16 +000079 void iterBindings(Store store, BindingsHandler& f);
Ted Kremenek60dbad82008-09-03 03:06:11 +000080
Zhongxing Xub4a9c612010-02-05 05:06:13 +000081 Store BindDecl(Store store, const VarRegion *VR, SVal InitVal) {
82 return BindDeclInternal(store, VR, &InitVal);
Zhongxing Xu4193eca2008-12-20 06:32:12 +000083 }
84
Zhongxing Xub4a9c612010-02-05 05:06:13 +000085 Store BindDeclWithNoInit(Store store, const VarRegion *VR) {
86 return BindDeclInternal(store, VR, 0);
Zhongxing Xu4193eca2008-12-20 06:32:12 +000087 }
88
Ted Kremenekf6f56d42009-11-04 00:09:15 +000089 Store BindDeclInternal(Store store, const VarRegion *VR, SVal *InitVal);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +000090
Ted Kremenek2c8e7882009-03-05 16:32:59 +000091 static inline BindingsTy GetBindings(Store store) {
92 return BindingsTy(static_cast<const BindingsTy::TreeTy*>(store));
Ted Kremeneka622d8c2008-08-19 22:24:03 +000093 }
94
Ted Kremenek53ba0b62009-06-24 23:06:47 +000095 void print(Store store, llvm::raw_ostream& Out, const char* nl,
96 const char *sep);
Zhongxing Xua1718c72009-04-03 07:33:13 +000097
98private:
Ted Kremenek5ba290a2010-03-02 21:43:54 +000099 SVal LazyRetrieve(Store store, const TypedRegion *R);
Ted Kremenek60dbad82008-09-03 03:06:11 +0000100};
Mike Stump1eb44332009-09-09 15:08:12 +0000101
Ted Kremenek4323a572008-07-10 22:03:41 +0000102} // end anonymous namespace
103
104
Ted Kremenek5f81c442008-08-28 23:31:31 +0000105StoreManager* clang::CreateBasicStoreManager(GRStateManager& StMgr) {
106 return new BasicStoreManager(StMgr);
Ted Kremenekd0c4b282008-08-25 19:33:03 +0000107}
Zhongxing Xu933c3e12008-10-21 06:54:23 +0000108
Ted Kremenek5fa93d52009-04-29 16:03:27 +0000109static bool isHigherOrderRawPtr(QualType T, ASTContext &C) {
Ted Kremenek1670e402009-04-11 00:11:10 +0000110 bool foundPointer = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000111 while (1) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000112 const PointerType *PT = T->getAs<PointerType>();
Ted Kremenek1670e402009-04-11 00:11:10 +0000113 if (!PT) {
114 if (!foundPointer)
115 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000116
Ted Kremenek5fa93d52009-04-29 16:03:27 +0000117 // intptr_t* or intptr_t**, etc?
118 if (T->isIntegerType() && C.getTypeSize(T) == C.getTypeSize(C.VoidPtrTy))
119 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000120
Ted Kremenek1670e402009-04-11 00:11:10 +0000121 QualType X = C.getCanonicalType(T).getUnqualifiedType();
122 return X == C.VoidTy;
123 }
Mike Stump1eb44332009-09-09 15:08:12 +0000124
Ted Kremenek1670e402009-04-11 00:11:10 +0000125 foundPointer = true;
126 T = PT->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000127 }
Ted Kremenek1670e402009-04-11 00:11:10 +0000128}
Mike Stump1eb44332009-09-09 15:08:12 +0000129
Ted Kremenek5ba290a2010-03-02 21:43:54 +0000130SVal BasicStoreManager::LazyRetrieve(Store store, const TypedRegion *R) {
131 const VarRegion *VR = dyn_cast<VarRegion>(R);
132 if (!VR)
133 return UnknownVal();
134
135 const VarDecl *VD = VR->getDecl();
136 QualType T = VD->getType();
137
138 // Only handle simple types that we can symbolicate.
139 if (!SymbolManager::canSymbolicate(T) || !T->isScalarType())
140 return UnknownVal();
141
142 // Globals and parameters start with symbolic values.
143 // Local variables initially are undefined.
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000144
145 // Non-static globals may have had their values reset by InvalidateRegions.
146 const MemSpaceRegion *MS = VR->getMemorySpace();
147 if (isa<NonStaticGlobalSpaceRegion>(MS)) {
148 BindingsTy B = GetBindings(store);
149 // FIXME: Copy-and-pasted from RegionStore.cpp.
150 if (BindingsTy::data_type *Val = B.lookup(MS)) {
151 if (SymbolRef parentSym = Val->getAsSymbol())
152 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
153
154 if (Val->isZeroConstant())
155 return ValMgr.makeZeroVal(T);
156
157 if (Val->isUnknownOrUndef())
158 return *Val;
159
160 assert(0 && "Unknown default value.");
161 }
162 }
163
Ted Kremenek30986dd2010-03-10 00:18:08 +0000164 if (VR->hasGlobalsOrParametersStorage() ||
165 isa<UnknownSpaceRegion>(VR->getMemorySpace()))
Ted Kremenek5ba290a2010-03-02 21:43:54 +0000166 return ValMgr.getRegionValueSymbolVal(R);
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000167
Ted Kremenek5ba290a2010-03-02 21:43:54 +0000168 return UndefinedVal();
169}
170
Zhongxing Xu576bb922010-02-05 03:01:53 +0000171SVal BasicStoreManager::Retrieve(Store store, Loc loc, QualType T) {
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000172 if (isa<UnknownVal>(loc))
Zhongxing Xuc999ed72010-02-04 02:39:47 +0000173 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000174
Ted Kremenek1894dce2009-08-25 20:51:30 +0000175 assert(!isa<UndefinedVal>(loc));
Mike Stump1eb44332009-09-09 15:08:12 +0000176
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000177 switch (loc.getSubKind()) {
Ted Kremenek4323a572008-07-10 22:03:41 +0000178
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000179 case loc::MemRegionKind: {
Ted Kremenekf684d562009-03-05 18:08:28 +0000180 const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion();
Mike Stump1eb44332009-09-09 15:08:12 +0000181
Ted Kremenekf684d562009-03-05 18:08:28 +0000182 if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
Zhongxing Xuc999ed72010-02-04 02:39:47 +0000183 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000184
Zhongxing Xu576bb922010-02-05 03:01:53 +0000185 BindingsTy B = GetBindings(store);
Ted Kremenek1894dce2009-08-25 20:51:30 +0000186 BindingsTy::data_type *Val = B.lookup(R);
Ted Kremenek5ba290a2010-03-02 21:43:54 +0000187 const TypedRegion *TR = cast<TypedRegion>(R);
Mike Stump1eb44332009-09-09 15:08:12 +0000188
Ted Kremenek5ba290a2010-03-02 21:43:54 +0000189 if (Val)
190 return CastRetrievedVal(*Val, TR, T);
Mike Stump1eb44332009-09-09 15:08:12 +0000191
Ted Kremenek5ba290a2010-03-02 21:43:54 +0000192 SVal V = LazyRetrieve(store, TR);
193 return V.isUnknownOrUndef() ? V : CastRetrievedVal(V, TR, T);
Ted Kremenek4323a572008-07-10 22:03:41 +0000194 }
Mike Stump1eb44332009-09-09 15:08:12 +0000195
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000196 case loc::ConcreteIntKind:
197 // Some clients may call GetSVal with such an option simply because
198 // they are doing a quick scan through their Locs (potentially to
Ted Kremenek4323a572008-07-10 22:03:41 +0000199 // invalidate their bindings). Just return Undefined.
Zhongxing Xuc999ed72010-02-04 02:39:47 +0000200 return UndefinedVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000201
Ted Kremenek4323a572008-07-10 22:03:41 +0000202 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000203 assert (false && "Invalid Loc.");
Ted Kremenek4323a572008-07-10 22:03:41 +0000204 break;
205 }
Mike Stump1eb44332009-09-09 15:08:12 +0000206
Zhongxing Xuc999ed72010-02-04 02:39:47 +0000207 return UnknownVal();
Ted Kremenek4323a572008-07-10 22:03:41 +0000208}
Mike Stump1eb44332009-09-09 15:08:12 +0000209
Zhongxing Xub241cf62010-02-08 08:48:05 +0000210Store BasicStoreManager::Bind(Store store, Loc loc, SVal V) {
Zhongxing Xu87453d12009-06-28 10:16:11 +0000211 if (isa<loc::ConcreteInt>(loc))
212 return store;
213
Zhongxing Xuc2a650a2009-06-28 09:26:15 +0000214 const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion();
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000215
216 // Special case: a default symbol assigned to the NonStaticGlobalsSpaceRegion
217 // that is used to derive other symbols.
218 if (isa<NonStaticGlobalSpaceRegion>(R)) {
219 BindingsTy B = GetBindings(store);
220 return VBFactory.Add(B, R, V).getRoot();
221 }
222
Zhongxing Xuc2a650a2009-06-28 09:26:15 +0000223 // Special case: handle store of pointer values (Loc) to pointers via
224 // a cast to intXX_t*, void*, etc. This is needed to handle
225 // OSCompareAndSwap32Barrier/OSCompareAndSwap64Barrier.
226 if (isa<Loc>(V) || isa<nonloc::LocAsInteger>(V))
227 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
228 // FIXME: Should check for index 0.
Zhongxing Xu018220c2010-08-11 06:10:55 +0000229 QualType T = ER->getLocationType();
Mike Stump1eb44332009-09-09 15:08:12 +0000230
Zhongxing Xu57663fe2010-08-15 10:08:38 +0000231 if (isHigherOrderRawPtr(T, Ctx))
Zhongxing Xuc2a650a2009-06-28 09:26:15 +0000232 R = ER->getSuperRegion();
Mike Stump1eb44332009-09-09 15:08:12 +0000233 }
234
Zhongxing Xuc2a650a2009-06-28 09:26:15 +0000235 if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
236 return store;
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000237
Ted Kremenekab2f43c2009-08-25 23:29:04 +0000238 const TypedRegion *TyR = cast<TypedRegion>(R);
Mike Stump1eb44332009-09-09 15:08:12 +0000239
Ted Kremenekab2f43c2009-08-25 23:29:04 +0000240 // Do not bind to arrays. We need to explicitly check for this so that
241 // we do not encounter any weirdness of trying to load/store from arrays.
Zhongxing Xu018220c2010-08-11 06:10:55 +0000242 if (TyR->isBoundable() && TyR->getValueType()->isArrayType())
Mike Stump1eb44332009-09-09 15:08:12 +0000243 return store;
Ted Kremenekab2f43c2009-08-25 23:29:04 +0000244
Zhongxing Xuc2a650a2009-06-28 09:26:15 +0000245 if (nonloc::LocAsInteger *X = dyn_cast<nonloc::LocAsInteger>(&V)) {
246 // Only convert 'V' to a location iff the underlying region type
247 // is a location as well.
248 // FIXME: We are allowing a store of an arbitrary location to
249 // a pointer. We may wish to flag a type error here if the types
250 // are incompatible. This may also cause lots of breakage
251 // elsewhere. Food for thought.
Zhongxing Xu018220c2010-08-11 06:10:55 +0000252 if (TyR->isBoundable() && Loc::IsLocType(TyR->getValueType()))
Ted Kremenekab2f43c2009-08-25 23:29:04 +0000253 V = X->getLoc();
Ted Kremenek4323a572008-07-10 22:03:41 +0000254 }
Zhongxing Xuc2a650a2009-06-28 09:26:15 +0000255
256 BindingsTy B = GetBindings(store);
257 return V.isUnknown()
258 ? VBFactory.Remove(B, R).getRoot()
259 : VBFactory.Add(B, R, V).getRoot();
Ted Kremenek4323a572008-07-10 22:03:41 +0000260}
261
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000262Store BasicStoreManager::Remove(Store store, Loc loc) {
263 switch (loc.getSubKind()) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000264 case loc::MemRegionKind: {
Ted Kremenekf684d562009-03-05 18:08:28 +0000265 const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion();
Mike Stump1eb44332009-09-09 15:08:12 +0000266
Ted Kremenekf684d562009-03-05 18:08:28 +0000267 if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
Ted Kremenek9e240492008-10-04 05:50:14 +0000268 return store;
Mike Stump1eb44332009-09-09 15:08:12 +0000269
Ted Kremenekf684d562009-03-05 18:08:28 +0000270 return VBFactory.Remove(GetBindings(store), R).getRoot();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000271 }
Ted Kremenek4323a572008-07-10 22:03:41 +0000272 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000273 assert ("Remove for given Loc type not yet implemented.");
Ted Kremenekf59bf482008-07-17 18:38:48 +0000274 return store;
Ted Kremenek4323a572008-07-10 22:03:41 +0000275 }
276}
Ted Kremenekf59bf482008-07-17 18:38:48 +0000277
Zhongxing Xu5e4cb342010-08-15 12:45:09 +0000278Store BasicStoreManager::RemoveDeadBindings(Store store,
Zhongxing Xu17ddf1c2010-03-17 03:35:08 +0000279 const StackFrameContext *LCtx,
Zhongxing Xu72119c42010-02-05 05:34:29 +0000280 SymbolReaper& SymReaper,
Ted Kremenek2f26bc32009-08-02 04:45:08 +0000281 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
Mike Stump1eb44332009-09-09 15:08:12 +0000282{
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000283 BindingsTy B = GetBindings(store);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000284 typedef SVal::symbol_iterator symbol_iterator;
Mike Stump1eb44332009-09-09 15:08:12 +0000285
Ted Kremenekf59bf482008-07-17 18:38:48 +0000286 // Iterate over the variable bindings.
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000287 for (BindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) {
Ted Kremenekf684d562009-03-05 18:08:28 +0000288 if (const VarRegion *VR = dyn_cast<VarRegion>(I.getKey())) {
Jordy Rose7dadf792010-07-01 20:09:55 +0000289 if (SymReaper.isLive(VR))
Ted Kremenekf684d562009-03-05 18:08:28 +0000290 RegionRoots.push_back(VR);
291 else
292 continue;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000293 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000294 else if (isa<ObjCIvarRegion>(I.getKey()) ||
295 isa<NonStaticGlobalSpaceRegion>(I.getKey()))
Ted Kremenekf684d562009-03-05 18:08:28 +0000296 RegionRoots.push_back(I.getKey());
Ted Kremenekf684d562009-03-05 18:08:28 +0000297 else
298 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000299
Ted Kremenekf684d562009-03-05 18:08:28 +0000300 // Mark the bindings in the data as live.
301 SVal X = I.getData();
302 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
303 SymReaper.markLive(*SI);
Ted Kremenekbe912242009-03-05 16:31:07 +0000304 }
Mike Stump1eb44332009-09-09 15:08:12 +0000305
Ted Kremenekf59bf482008-07-17 18:38:48 +0000306 // Scan for live variables and live symbols.
Ted Kremenekf684d562009-03-05 18:08:28 +0000307 llvm::SmallPtrSet<const MemRegion*, 10> Marked;
Mike Stump1eb44332009-09-09 15:08:12 +0000308
Ted Kremenek9e240492008-10-04 05:50:14 +0000309 while (!RegionRoots.empty()) {
Ted Kremenek134e7492008-10-17 22:52:40 +0000310 const MemRegion* MR = RegionRoots.back();
Ted Kremenek9e240492008-10-04 05:50:14 +0000311 RegionRoots.pop_back();
Mike Stump1eb44332009-09-09 15:08:12 +0000312
Ted Kremenek134e7492008-10-17 22:52:40 +0000313 while (MR) {
314 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(MR)) {
Ted Kremenek241677a2009-01-21 22:26:05 +0000315 SymReaper.markLive(SymR->getSymbol());
Ted Kremenek134e7492008-10-17 22:52:40 +0000316 break;
317 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000318 else if (isa<VarRegion>(MR) || isa<ObjCIvarRegion>(MR) ||
319 isa<NonStaticGlobalSpaceRegion>(MR)) {
Ted Kremenekf684d562009-03-05 18:08:28 +0000320 if (Marked.count(MR))
Ted Kremenek134e7492008-10-17 22:52:40 +0000321 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000322
323 Marked.insert(MR);
Zhongxing Xu72119c42010-02-05 05:34:29 +0000324 SVal X = Retrieve(store, loc::MemRegionVal(MR));
Mike Stump1eb44332009-09-09 15:08:12 +0000325
Ted Kremenek134e7492008-10-17 22:52:40 +0000326 // FIXME: We need to handle symbols nested in region definitions.
Ted Kremenek241677a2009-01-21 22:26:05 +0000327 for (symbol_iterator SI=X.symbol_begin(),SE=X.symbol_end();SI!=SE;++SI)
328 SymReaper.markLive(*SI);
Mike Stump1eb44332009-09-09 15:08:12 +0000329
Ted Kremenek134e7492008-10-17 22:52:40 +0000330 if (!isa<loc::MemRegionVal>(X))
331 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000332
Ted Kremenek134e7492008-10-17 22:52:40 +0000333 const loc::MemRegionVal& LVD = cast<loc::MemRegionVal>(X);
334 RegionRoots.push_back(LVD.getRegion());
335 break;
336 }
337 else if (const SubRegion* R = dyn_cast<SubRegion>(MR))
338 MR = R->getSuperRegion();
339 else
340 break;
341 }
Ted Kremenekf59bf482008-07-17 18:38:48 +0000342 }
Mike Stump1eb44332009-09-09 15:08:12 +0000343
344 // Remove dead variable bindings.
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000345 for (BindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) {
Ted Kremenekf684d562009-03-05 18:08:28 +0000346 const MemRegion* R = I.getKey();
Mike Stump1eb44332009-09-09 15:08:12 +0000347
Ted Kremenek9e240492008-10-04 05:50:14 +0000348 if (!Marked.count(R)) {
Zhongxing Xud91ee272009-06-23 09:02:15 +0000349 store = Remove(store, ValMgr.makeLoc(R));
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000350 SVal X = I.getData();
Mike Stump1eb44332009-09-09 15:08:12 +0000351
Ted Kremenekf59bf482008-07-17 18:38:48 +0000352 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
Ted Kremenek241677a2009-01-21 22:26:05 +0000353 SymReaper.maybeDead(*SI);
Ted Kremenekf59bf482008-07-17 18:38:48 +0000354 }
Ted Kremenek9e240492008-10-04 05:50:14 +0000355 }
Ted Kremenekf684d562009-03-05 18:08:28 +0000356
Zhongxing Xu5e4cb342010-08-15 12:45:09 +0000357 return store;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000358}
Ted Kremenekcaa37242008-08-19 16:51:45 +0000359
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000360Store BasicStoreManager::scanForIvars(Stmt *B, const Decl* SelfDecl,
361 const MemRegion *SelfRegion, Store St) {
Ted Kremenekf684d562009-03-05 18:08:28 +0000362 for (Stmt::child_iterator CI=B->child_begin(), CE=B->child_end();
363 CI != CE; ++CI) {
Mike Stump1eb44332009-09-09 15:08:12 +0000364
Ted Kremenekf684d562009-03-05 18:08:28 +0000365 if (!*CI)
366 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000367
Ted Kremenekf684d562009-03-05 18:08:28 +0000368 // Check if the statement is an ivar reference. We only
369 // care about self.ivar.
370 if (ObjCIvarRefExpr *IV = dyn_cast<ObjCIvarRefExpr>(*CI)) {
371 const Expr *Base = IV->getBase()->IgnoreParenCasts();
372 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Base)) {
373 if (DR->getDecl() == SelfDecl) {
Zhongxing Xu14d23282010-03-01 06:56:52 +0000374 const ObjCIvarRegion *IVR = MRMgr.getObjCIvarRegion(IV->getDecl(),
Mike Stump1eb44332009-09-09 15:08:12 +0000375 SelfRegion);
376 SVal X = ValMgr.getRegionValueSymbolVal(IVR);
Zhongxing Xub241cf62010-02-08 08:48:05 +0000377 St = Bind(St, ValMgr.makeLoc(IVR), X);
Ted Kremenekf684d562009-03-05 18:08:28 +0000378 }
379 }
380 }
381 else
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000382 St = scanForIvars(*CI, SelfDecl, SelfRegion, St);
Ted Kremenekf684d562009-03-05 18:08:28 +0000383 }
Mike Stump1eb44332009-09-09 15:08:12 +0000384
Ted Kremenekf684d562009-03-05 18:08:28 +0000385 return St;
386}
387
Mike Stump1eb44332009-09-09 15:08:12 +0000388Store BasicStoreManager::getInitialStore(const LocationContext *InitLoc) {
Ted Kremenekcaa37242008-08-19 16:51:45 +0000389 // The LiveVariables information already has a compilation of all VarDecls
390 // used in the function. Iterate through this set, and "symbolicate"
391 // any VarDecl whose value originally comes from outside the function.
Ted Kremenekcaa37242008-08-19 16:51:45 +0000392 typedef LiveVariables::AnalysisDataTy LVDataTy;
Zhongxing Xu17fd8632009-08-17 06:19:58 +0000393 LVDataTy& D = InitLoc->getLiveVariables()->getAnalysisData();
Ted Kremenekcaa37242008-08-19 16:51:45 +0000394 Store St = VBFactory.GetEmptyMap().getRoot();
395
396 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
Zhongxing Xuce2bc7e32010-07-20 02:46:11 +0000397 const NamedDecl* ND = I->first;
Ted Kremenekcaa37242008-08-19 16:51:45 +0000398
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000399 // Handle implicit parameters.
Zhongxing Xuce2bc7e32010-07-20 02:46:11 +0000400 if (const ImplicitParamDecl* PD = dyn_cast<ImplicitParamDecl>(ND)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000401 const Decl& CD = *InitLoc->getDecl();
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000402 if (const ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(&CD)) {
403 if (MD->getSelfDecl() == PD) {
Ted Kremenekc410d4d2009-12-16 23:53:37 +0000404 // FIXME: Add type constraints (when they become available) to
405 // SelfRegion? (i.e., it implements MD->getClassInterface()).
Zhongxing Xu14d23282010-03-01 06:56:52 +0000406 const VarRegion *VR = MRMgr.getVarRegion(PD, InitLoc);
Ted Kremenekc410d4d2009-12-16 23:53:37 +0000407 const MemRegion *SelfRegion =
Ted Kremenek5ba290a2010-03-02 21:43:54 +0000408 ValMgr.getRegionValueSymbolVal(VR).getAsRegion();
409 assert(SelfRegion);
Zhongxing Xub241cf62010-02-08 08:48:05 +0000410 St = Bind(St, ValMgr.makeLoc(VR), loc::MemRegionVal(SelfRegion));
Ted Kremenekf684d562009-03-05 18:08:28 +0000411 // Scan the method for ivar references. While this requires an
412 // entire AST scan, the cost should not be high in practice.
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000413 St = scanForIvars(MD->getBody(), PD, SelfRegion, St);
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000414 }
415 }
416 }
Ted Kremenekcaa37242008-08-19 16:51:45 +0000417 }
Ted Kremenek5ba290a2010-03-02 21:43:54 +0000418
Ted Kremenekcaa37242008-08-19 16:51:45 +0000419 return St;
420}
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000421
Ted Kremenekf6f56d42009-11-04 00:09:15 +0000422Store BasicStoreManager::BindDeclInternal(Store store, const VarRegion* VR,
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000423 SVal* InitVal) {
Mike Stump1eb44332009-09-09 15:08:12 +0000424
Ted Kremeneke53c0692008-08-23 00:50:55 +0000425 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
Ted Kremenekf6f56d42009-11-04 00:09:15 +0000426 const VarDecl *VD = VR->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000427
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000428 // BasicStore does not model arrays and structs.
Douglas Gregorfb87b892010-04-26 21:31:17 +0000429 if (VD->getType()->isArrayType() || VD->getType()->isStructureOrClassType())
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000430 return store;
431
432 if (VD->hasGlobalStorage()) {
433 // Handle variables with global storage: extern, static, PrivateExtern.
434
435 // FIXME:: static variables may have an initializer, but the second time a
436 // function is called those values may not be current. Currently, a function
437 // will not be called more than once.
438
439 // Static global variables should not be visited here.
John McCalld931b082010-08-26 03:08:43 +0000440 assert(!(VD->getStorageClass() == SC_Static &&
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000441 VD->isFileVarDecl()));
Mike Stump1eb44332009-09-09 15:08:12 +0000442
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000443 // Process static variables.
John McCalld931b082010-08-26 03:08:43 +0000444 if (VD->getStorageClass() == SC_Static) {
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000445 // C99: 6.7.8 Initialization
446 // If an object that has static storage duration is not initialized
Mike Stump1eb44332009-09-09 15:08:12 +0000447 // explicitly, then:
448 // —if it has pointer type, it is initialized to a null pointer;
449 // —if it has arithmetic type, it is initialized to (positive or
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000450 // unsigned) zero;
Ted Kremenek42577d12008-11-12 19:18:35 +0000451 if (!InitVal) {
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000452 QualType T = VD->getType();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000453 if (Loc::IsLocType(T))
Zhongxing Xub241cf62010-02-08 08:48:05 +0000454 store = Bind(store, loc::MemRegionVal(VR),
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000455 loc::ConcreteInt(BasicVals.getValue(0, T)));
Ted Kremenekc0708432010-07-29 00:28:40 +0000456 else if (T->isIntegerType() && T->isScalarType())
Zhongxing Xub241cf62010-02-08 08:48:05 +0000457 store = Bind(store, loc::MemRegionVal(VR),
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000458 nonloc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000459 } else {
Zhongxing Xub241cf62010-02-08 08:48:05 +0000460 store = Bind(store, loc::MemRegionVal(VR), *InitVal);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000461 }
462 }
463 } else {
464 // Process local scalar variables.
465 QualType T = VD->getType();
Ted Kremenekc0708432010-07-29 00:28:40 +0000466 // BasicStore only supports scalars.
467 if (T->isScalarType() && ValMgr.getSymbolManager().canSymbolicate(T)) {
Ted Kremenek42577d12008-11-12 19:18:35 +0000468 SVal V = InitVal ? *InitVal : UndefinedVal();
Zhongxing Xub241cf62010-02-08 08:48:05 +0000469 store = Bind(store, loc::MemRegionVal(VR), V);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000470 }
471 }
472
473 return store;
474}
475
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000476void BasicStoreManager::print(Store store, llvm::raw_ostream& Out,
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000477 const char* nl, const char *sep) {
Mike Stump1eb44332009-09-09 15:08:12 +0000478
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000479 BindingsTy B = GetBindings(store);
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000480 Out << "Variables:" << nl;
Mike Stump1eb44332009-09-09 15:08:12 +0000481
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000482 bool isFirst = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000483
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000484 for (BindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I) {
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000485 if (isFirst)
486 isFirst = false;
487 else
488 Out << nl;
Mike Stump1eb44332009-09-09 15:08:12 +0000489
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000490 Out << ' ' << I.getKey() << " : " << I.getData();
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000491 }
492}
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000493
Ted Kremenek60dbad82008-09-03 03:06:11 +0000494
495void BasicStoreManager::iterBindings(Store store, BindingsHandler& f) {
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000496 BindingsTy B = GetBindings(store);
Mike Stump1eb44332009-09-09 15:08:12 +0000497
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000498 for (BindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I)
Ted Kremenek744b3042010-06-17 00:24:37 +0000499 if (!f.HandleBinding(*this, store, I.getKey(), I.getData()))
500 return;
Ted Kremenek9e240492008-10-04 05:50:14 +0000501
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000502}
503
Ted Kremenek60dbad82008-09-03 03:06:11 +0000504StoreManager::BindingsHandler::~BindingsHandler() {}
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000505
506//===----------------------------------------------------------------------===//
507// Binding invalidation.
508//===----------------------------------------------------------------------===//
509
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000510
511Store BasicStoreManager::InvalidateRegions(Store store,
Jordy Rosec2b7dfa2010-08-14 20:44:32 +0000512 const MemRegion * const *I,
513 const MemRegion * const *End,
514 const Expr *E, unsigned Count,
515 InvalidatedSymbols *IS,
516 bool invalidateGlobals,
517 InvalidatedRegions *Regions) {
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000518 if (invalidateGlobals) {
519 BindingsTy B = GetBindings(store);
520 for (BindingsTy::iterator I=B.begin(), End=B.end(); I != End; ++I) {
521 const MemRegion *R = I.getKey();
522 if (isa<NonStaticGlobalSpaceRegion>(R->getMemorySpace()))
523 store = InvalidateRegion(store, R, E, Count, IS);
524 }
525 }
526
527 for ( ; I != End ; ++I) {
528 const MemRegion *R = *I;
529 // Don't invalidate globals twice.
530 if (invalidateGlobals) {
531 if (isa<NonStaticGlobalSpaceRegion>(R->getMemorySpace()))
532 continue;
533 }
534 store = InvalidateRegion(store, *I, E, Count, IS);
Jordy Rosec2b7dfa2010-08-14 20:44:32 +0000535 if (Regions)
536 Regions->push_back(R);
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000537 }
538
539 // FIXME: This is copy-and-paste from RegionStore.cpp.
540 if (invalidateGlobals) {
541 // Bind the non-static globals memory space to a new symbol that we will
542 // use to derive the bindings for all non-static globals.
543 const GlobalsSpaceRegion *GS = MRMgr.getGlobalsRegion();
544 SVal V =
545 ValMgr.getConjuredSymbolVal(/* SymbolTag = */ (void*) GS, E,
546 /* symbol type, doesn't matter */ Ctx.IntTy,
547 Count);
548
549 store = Bind(store, loc::MemRegionVal(GS), V);
Jordy Rosec2b7dfa2010-08-14 20:44:32 +0000550 if (Regions)
551 Regions->push_back(GS);
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000552 }
553
554 return store;
555}
556
557
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000558Store BasicStoreManager::InvalidateRegion(Store store,
559 const MemRegion *R,
560 const Expr *E,
561 unsigned Count,
562 InvalidatedSymbols *IS) {
Zhongxing Xu479529e2009-11-10 02:17:20 +0000563 R = R->StripCasts();
Mike Stump1eb44332009-09-09 15:08:12 +0000564
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000565 if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000566 return store;
Mike Stump1eb44332009-09-09 15:08:12 +0000567
Ted Kremenek473e1672009-10-16 00:30:49 +0000568 if (IS) {
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000569 BindingsTy B = GetBindings(store);
Ted Kremenek473e1672009-10-16 00:30:49 +0000570 if (BindingsTy::data_type *Val = B.lookup(R)) {
571 if (SymbolRef Sym = Val->getAsSymbol())
572 IS->insert(Sym);
573 }
574 }
575
Zhongxing Xu018220c2010-08-11 06:10:55 +0000576 QualType T = cast<TypedRegion>(R)->getValueType();
Ted Kremenek87806792009-09-27 20:45:21 +0000577 SVal V = ValMgr.getConjuredSymbolVal(R, E, T, Count);
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000578 return Bind(store, loc::MemRegionVal(R), V);
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000579}
580