blob: ceefe0338ca93300b408db4eac1d9ad000bf4e5e [file] [log] [blame]
Ted Kremeneka7b8ffb2008-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 Kremenekfa3d77b2009-03-05 18:08:28 +000014#include "clang/AST/ExprObjC.h"
Ted Kremeneke91874f72008-08-28 23:31:31 +000015#include "clang/Analysis/Analyses/LiveVariables.h"
Ted Kremenekd6b87082010-01-25 04:41:41 +000016#include "clang/Analysis/AnalysisContext.h"
17#include "clang/Checker/PathSensitive/GRState.h"
Benjamin Kramer89b422c2009-08-23 12:08:50 +000018#include "llvm/ADT/ImmutableMap.h"
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +000019
20using namespace clang;
21
Mike Stump11289f42009-09-09 15:08:12 +000022typedef llvm::ImmutableMap<const MemRegion*,SVal> BindingsTy;
Ted Kremenek1b9e1032008-09-03 03:06:11 +000023
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +000024namespace {
Mike Stump11289f42009-09-09 15:08:12 +000025
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +000026class BasicStoreSubRegionMap : public SubRegionMap {
Ted Kremenek8dc671c2009-03-03 01:35:36 +000027public:
28 BasicStoreSubRegionMap() {}
29
Ted Kremenek4c8a5812009-03-03 02:51:43 +000030 bool iterSubRegions(const MemRegion* R, Visitor& V) const {
Ted Kremenek89f47812009-03-05 16:41:21 +000031 return true; // Do nothing. No subregions.
Ted Kremenek8dc671c2009-03-03 01:35:36 +000032 }
33};
Mike Stump11289f42009-09-09 15:08:12 +000034
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +000035class BasicStoreManager : public StoreManager {
Ted Kremenek1fe63ac2009-03-05 16:32:59 +000036 BindingsTy::Factory VBFactory;
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +000037public:
Ted Kremenek43015262009-07-29 21:43:22 +000038 BasicStoreManager(GRStateManager& mgr)
Ted Kremenek608677a2009-08-21 23:25:54 +000039 : StoreManager(mgr), VBFactory(mgr.getAllocator()) {}
Mike Stump11289f42009-09-09 15:08:12 +000040
Ted Kremenekf3be44f2008-10-24 20:32:16 +000041 ~BasicStoreManager() {}
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +000042
Ted Kremenek609df302009-06-17 22:02:04 +000043 SubRegionMap *getSubRegionMap(const GRState *state) {
Ted Kremenek9f276d62009-03-03 19:02:42 +000044 return new BasicStoreSubRegionMap();
Ted Kremenek8dc671c2009-03-03 01:35:36 +000045 }
46
Zhongxing Xuc7b9f952010-02-05 03:01:53 +000047 SVal Retrieve(Store store, Loc loc, QualType T = QualType());
Mike Stump11289f42009-09-09 15:08:12 +000048
Ted Kremenekbca70672009-07-29 18:16:25 +000049 const GRState *InvalidateRegion(const GRState *state, const MemRegion *R,
Ted Kremenek1eb68092009-10-16 00:30:49 +000050 const Expr *E, unsigned Count,
51 InvalidatedSymbols *IS);
Zhongxing Xuaf7415f2008-12-20 06:32:12 +000052
Ted Kremenek609df302009-06-17 22:02:04 +000053 const GRState *Bind(const GRState *state, Loc L, SVal V) {
54 return state->makeWithStore(BindInternal(state->getStore(), L, V));
Zhongxing Xuaf7415f2008-12-20 06:32:12 +000055 }
56
Ted Kremenek608677a2009-08-21 23:25:54 +000057 Store scanForIvars(Stmt *B, const Decl* SelfDecl,
58 const MemRegion *SelfRegion, Store St);
Mike Stump11289f42009-09-09 15:08:12 +000059
60 Store BindInternal(Store St, Loc loc, SVal V);
Ted Kremenekf929b0a2009-01-07 22:56:17 +000061 Store Remove(Store St, Loc loc);
Zhongxing Xu5f078cb2009-08-17 06:19:58 +000062 Store getInitialStore(const LocationContext *InitLoc);
Zhongxing Xuf5e7c902008-10-07 01:31:04 +000063
Zhongxing Xu232c7922008-10-16 06:09:51 +000064 // FIXME: Investigate what is using this. This method should be removed.
Ted Kremenek14536f62009-08-21 22:28:32 +000065 virtual Loc getLoc(const VarDecl* VD, const LocationContext *LC) {
66 return ValMgr.makeLoc(MRMgr.getVarRegion(VD, LC));
Zhongxing Xuf5e7c902008-10-07 01:31:04 +000067 }
Mike Stump11289f42009-09-09 15:08:12 +000068
Ted Kremenek609df302009-06-17 22:02:04 +000069 const GRState *BindCompoundLiteral(const GRState *state,
Ted Kremenek04af9f22009-12-07 22:05:27 +000070 const CompoundLiteralExpr*,
71 const LocationContext*,
Ted Kremenek609df302009-06-17 22:02:04 +000072 SVal val) {
73 return state;
Ted Kremenekbf263682008-10-27 21:54:31 +000074 }
Mike Stump11289f42009-09-09 15:08:12 +000075
Zhongxing Xu7d6387b2009-10-14 03:33:08 +000076 SVal getLValueVar(const VarDecl *VD, const LocationContext *LC);
77 SVal getLValueString(const StringLiteral *S);
Zhongxing Xu7d6387b2009-10-14 03:33:08 +000078 SVal getLValueIvar(const ObjCIvarDecl* D, SVal Base);
79 SVal getLValueField(const FieldDecl *D, SVal Base);
80 SVal getLValueElement(QualType elementType, SVal Offset, SVal Base);
Zhongxing Xub0a48752008-10-23 03:10:39 +000081
Ted Kremenekf3be44f2008-10-24 20:32:16 +000082 /// ArrayToPointer - Used by GRExprEngine::VistCast to handle implicit
83 /// conversions between arrays and pointers.
Zhongxing Xua865b792009-03-30 05:55:46 +000084 SVal ArrayToPointer(Loc Array) { return Array; }
Mike Stump11289f42009-09-09 15:08:12 +000085
Ted Kremenekd368de72008-12-05 00:47:52 +000086 /// RemoveDeadBindings - Scans a BasicStore of 'state' for dead values.
Ted Kremenekcee28a42009-08-02 04:45:08 +000087 /// It updatees the GRState object in place with the values removed.
88 void RemoveDeadBindings(GRState &state, Stmt* Loc, SymbolReaper& SymReaper,
89 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
Zhongxing Xud95495f2008-08-21 22:34:01 +000090
Ted Kremenekf3be44f2008-10-24 20:32:16 +000091 void iterBindings(Store store, BindingsHandler& f);
Ted Kremenek1b9e1032008-09-03 03:06:11 +000092
Ted Kremenekb006b822009-11-04 00:09:15 +000093 const GRState *BindDecl(const GRState *state, const VarRegion *VR,
94 SVal InitVal) {
95 return state->makeWithStore(BindDeclInternal(state->getStore(), VR,
Ted Kremenek14536f62009-08-21 22:28:32 +000096 &InitVal));
Zhongxing Xuaf7415f2008-12-20 06:32:12 +000097 }
98
Ted Kremenekb006b822009-11-04 00:09:15 +000099 const GRState *BindDeclWithNoInit(const GRState *state, const VarRegion *VR) {
100 return state->makeWithStore(BindDeclInternal(state->getStore(), VR, 0));
Zhongxing Xuaf7415f2008-12-20 06:32:12 +0000101 }
102
Ted Kremenekb006b822009-11-04 00:09:15 +0000103 Store BindDeclInternal(Store store, const VarRegion *VR, SVal *InitVal);
Zhongxing Xud95495f2008-08-21 22:34:01 +0000104
Ted Kremenek1fe63ac2009-03-05 16:32:59 +0000105 static inline BindingsTy GetBindings(Store store) {
106 return BindingsTy(static_cast<const BindingsTy::TreeTy*>(store));
Ted Kremenek19edd212008-08-19 22:24:03 +0000107 }
108
Ted Kremenek799bb6e2009-06-24 23:06:47 +0000109 void print(Store store, llvm::raw_ostream& Out, const char* nl,
110 const char *sep);
Zhongxing Xuec7e7df2009-04-03 07:33:13 +0000111
112private:
113 ASTContext& getContext() { return StateMgr.getContext(); }
Ted Kremenek1b9e1032008-09-03 03:06:11 +0000114};
Mike Stump11289f42009-09-09 15:08:12 +0000115
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +0000116} // end anonymous namespace
117
118
Ted Kremeneke91874f72008-08-28 23:31:31 +0000119StoreManager* clang::CreateBasicStoreManager(GRStateManager& StMgr) {
120 return new BasicStoreManager(StMgr);
Ted Kremenek2a2c8752008-08-25 19:33:03 +0000121}
Zhongxing Xudab76fd2008-10-21 06:54:23 +0000122
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000123SVal BasicStoreManager::getLValueVar(const VarDecl* VD,
Ted Kremenek14536f62009-08-21 22:28:32 +0000124 const LocationContext *LC) {
125 return ValMgr.makeLoc(MRMgr.getVarRegion(VD, LC));
Ted Kremenek3ad391d2008-10-17 00:51:01 +0000126}
Zhongxing Xu0d2706f2008-10-25 14:18:57 +0000127
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000128SVal BasicStoreManager::getLValueString(const StringLiteral* S) {
Zhongxing Xu7718ae42009-06-23 09:02:15 +0000129 return ValMgr.makeLoc(MRMgr.getStringRegion(S));
Zhongxing Xu0d2706f2008-10-25 14:18:57 +0000130}
Zhongxing Xu2c677c32008-11-07 10:38:33 +0000131
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000132SVal BasicStoreManager::getLValueIvar(const ObjCIvarDecl* D, SVal Base) {
Mike Stump11289f42009-09-09 15:08:12 +0000133
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000134 if (Base.isUnknownOrUndef())
135 return Base;
136
137 Loc BaseL = cast<Loc>(Base);
138
139 if (isa<loc::MemRegionVal>(BaseL)) {
140 const MemRegion *BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
Ted Kremenek608677a2009-08-21 23:25:54 +0000141 return ValMgr.makeLoc(MRMgr.getObjCIvarRegion(D, BaseR));
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000142 }
Mike Stump11289f42009-09-09 15:08:12 +0000143
Ted Kremenek3ad391d2008-10-17 00:51:01 +0000144 return UnknownVal();
145}
Ted Kremenekb5670fd2008-12-13 21:49:13 +0000146
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000147SVal BasicStoreManager::getLValueField(const FieldDecl* D, SVal Base) {
Ted Kremenek8b103c62008-10-17 20:28:54 +0000148
149 if (Base.isUnknownOrUndef())
150 return Base;
Mike Stump11289f42009-09-09 15:08:12 +0000151
152 Loc BaseL = cast<Loc>(Base);
Ted Kremenek8b103c62008-10-17 20:28:54 +0000153 const MemRegion* BaseR = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000154
Ted Kremenek8b103c62008-10-17 20:28:54 +0000155 switch(BaseL.getSubKind()) {
Ted Kremenek8b103c62008-10-17 20:28:54 +0000156 case loc::GotoLabelKind:
Ted Kremenek8b103c62008-10-17 20:28:54 +0000157 return UndefinedVal();
158
159 case loc::MemRegionKind:
160 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
161 break;
Mike Stump11289f42009-09-09 15:08:12 +0000162
Ted Kremenek8b103c62008-10-17 20:28:54 +0000163 case loc::ConcreteIntKind:
Ted Kremenek8b103c62008-10-17 20:28:54 +0000164 // While these seem funny, this can happen through casts.
165 // FIXME: What we should return is the field offset. For example,
166 // add the field offset to the integer value. That way funny things
167 // like this work properly: &(((struct foo *) 0xa)->f)
168 return Base;
169
170 default:
171 assert ("Unhandled Base.");
172 return Base;
173 }
Mike Stump11289f42009-09-09 15:08:12 +0000174
Zhongxing Xu7718ae42009-06-23 09:02:15 +0000175 return ValMgr.makeLoc(MRMgr.getFieldRegion(D, BaseR));
Ted Kremenek3ad391d2008-10-17 00:51:01 +0000176}
Ted Kremenek2a2c8752008-08-25 19:33:03 +0000177
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000178SVal BasicStoreManager::getLValueElement(QualType elementType,
179 SVal Offset, SVal Base) {
Ted Kremenek62cc9da2008-12-09 21:20:27 +0000180
Ted Kremenek62cc9da2008-12-09 21:20:27 +0000181 if (Base.isUnknownOrUndef())
182 return Base;
Mike Stump11289f42009-09-09 15:08:12 +0000183
184 Loc BaseL = cast<Loc>(Base);
Zhongxing Xu6377a982009-06-30 07:41:27 +0000185 const MemRegion* BaseR = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000186
Ted Kremenek62cc9da2008-12-09 21:20:27 +0000187 switch(BaseL.getSubKind()) {
Ted Kremenek62cc9da2008-12-09 21:20:27 +0000188 case loc::GotoLabelKind:
Ted Kremenek62cc9da2008-12-09 21:20:27 +0000189 // Technically we can get here if people do funny things with casts.
190 return UndefinedVal();
Mike Stump11289f42009-09-09 15:08:12 +0000191
Ted Kremenekf065b152008-12-13 19:24:37 +0000192 case loc::MemRegionKind: {
193 const MemRegion *R = cast<loc::MemRegionVal>(BaseL).getRegion();
Mike Stump11289f42009-09-09 15:08:12 +0000194
Ted Kremeneke5c31582009-05-05 00:06:16 +0000195 if (isa<ElementRegion>(R)) {
Zhongxing Xu6ebde272009-05-04 08:52:47 +0000196 // int x;
197 // char* y = (char*) &x;
198 // 'y' => ElementRegion(0, VarRegion('x'))
199 // y[0] = 'a';
Ted Kremenek422d81d2009-01-27 18:29:03 +0000200 return Base;
201 }
Mike Stump11289f42009-09-09 15:08:12 +0000202
Ted Kremenek8fd18792009-06-30 20:24:11 +0000203 if (isa<TypedRegion>(R) || isa<SymbolicRegion>(R)) {
204 BaseR = R;
Ted Kremenekf065b152008-12-13 19:24:37 +0000205 break;
206 }
Mike Stump11289f42009-09-09 15:08:12 +0000207
Ted Kremenek62cc9da2008-12-09 21:20:27 +0000208 break;
Ted Kremenekf065b152008-12-13 19:24:37 +0000209 }
Ted Kremenek422d81d2009-01-27 18:29:03 +0000210
Ted Kremenek62cc9da2008-12-09 21:20:27 +0000211 case loc::ConcreteIntKind:
212 // While these seem funny, this can happen through casts.
213 // FIXME: What we should return is the field offset. For example,
214 // add the field offset to the integer value. That way funny things
215 // like this work properly: &(((struct foo *) 0xa)->f)
216 return Base;
Mike Stump11289f42009-09-09 15:08:12 +0000217
Ted Kremenek62cc9da2008-12-09 21:20:27 +0000218 default:
219 assert ("Unhandled Base.");
220 return Base;
221 }
Mike Stump11289f42009-09-09 15:08:12 +0000222
223 if (BaseR) {
Zhongxing Xu7718ae42009-06-23 09:02:15 +0000224 return ValMgr.makeLoc(MRMgr.getElementRegion(elementType, UnknownVal(),
225 BaseR, getContext()));
Ted Kremenek8fd18792009-06-30 20:24:11 +0000226 }
Ted Kremenekf065b152008-12-13 19:24:37 +0000227 else
228 return UnknownVal();
Zhongxing Xu232c7922008-10-16 06:09:51 +0000229}
230
Ted Kremenek3941d222009-04-29 16:03:27 +0000231static bool isHigherOrderRawPtr(QualType T, ASTContext &C) {
Ted Kremenekdf240002009-04-11 00:11:10 +0000232 bool foundPointer = false;
Mike Stump11289f42009-09-09 15:08:12 +0000233 while (1) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000234 const PointerType *PT = T->getAs<PointerType>();
Ted Kremenekdf240002009-04-11 00:11:10 +0000235 if (!PT) {
236 if (!foundPointer)
237 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000238
Ted Kremenek3941d222009-04-29 16:03:27 +0000239 // intptr_t* or intptr_t**, etc?
240 if (T->isIntegerType() && C.getTypeSize(T) == C.getTypeSize(C.VoidPtrTy))
241 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000242
Ted Kremenekdf240002009-04-11 00:11:10 +0000243 QualType X = C.getCanonicalType(T).getUnqualifiedType();
244 return X == C.VoidTy;
245 }
Mike Stump11289f42009-09-09 15:08:12 +0000246
Ted Kremenekdf240002009-04-11 00:11:10 +0000247 foundPointer = true;
248 T = PT->getPointeeType();
Mike Stump11289f42009-09-09 15:08:12 +0000249 }
Ted Kremenekdf240002009-04-11 00:11:10 +0000250}
Mike Stump11289f42009-09-09 15:08:12 +0000251
Zhongxing Xuc7b9f952010-02-05 03:01:53 +0000252SVal BasicStoreManager::Retrieve(Store store, Loc loc, QualType T) {
Ted Kremenekf929b0a2009-01-07 22:56:17 +0000253 if (isa<UnknownVal>(loc))
Zhongxing Xu4f8b9892010-02-04 02:39:47 +0000254 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +0000255
Ted Kremenek3ed95432009-08-25 20:51:30 +0000256 assert(!isa<UndefinedVal>(loc));
Mike Stump11289f42009-09-09 15:08:12 +0000257
Ted Kremenekf929b0a2009-01-07 22:56:17 +0000258 switch (loc.getSubKind()) {
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +0000259
Zhongxing Xu27f17422008-10-17 05:57:07 +0000260 case loc::MemRegionKind: {
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000261 const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion();
Mike Stump11289f42009-09-09 15:08:12 +0000262
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000263 if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
Zhongxing Xu4f8b9892010-02-04 02:39:47 +0000264 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +0000265
Zhongxing Xuc7b9f952010-02-05 03:01:53 +0000266 BindingsTy B = GetBindings(store);
Ted Kremenek3ed95432009-08-25 20:51:30 +0000267 BindingsTy::data_type *Val = B.lookup(R);
Mike Stump11289f42009-09-09 15:08:12 +0000268
Ted Kremenek3ed95432009-08-25 20:51:30 +0000269 if (!Val)
270 break;
Mike Stump11289f42009-09-09 15:08:12 +0000271
Zhongxing Xu4f8b9892010-02-04 02:39:47 +0000272 return CastRetrievedVal(*Val, cast<TypedRegion>(R), T);
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +0000273 }
Mike Stump11289f42009-09-09 15:08:12 +0000274
Zhongxing Xu27f17422008-10-17 05:57:07 +0000275 case loc::ConcreteIntKind:
276 // Some clients may call GetSVal with such an option simply because
277 // they are doing a quick scan through their Locs (potentially to
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +0000278 // invalidate their bindings). Just return Undefined.
Zhongxing Xu4f8b9892010-02-04 02:39:47 +0000279 return UndefinedVal();
Mike Stump11289f42009-09-09 15:08:12 +0000280
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +0000281 default:
Zhongxing Xu27f17422008-10-17 05:57:07 +0000282 assert (false && "Invalid Loc.");
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +0000283 break;
284 }
Mike Stump11289f42009-09-09 15:08:12 +0000285
Zhongxing Xu4f8b9892010-02-04 02:39:47 +0000286 return UnknownVal();
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +0000287}
Mike Stump11289f42009-09-09 15:08:12 +0000288
289Store BasicStoreManager::BindInternal(Store store, Loc loc, SVal V) {
Zhongxing Xud260db12009-06-28 10:16:11 +0000290 if (isa<loc::ConcreteInt>(loc))
291 return store;
292
Zhongxing Xuf7a6de32009-06-28 09:26:15 +0000293 const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion();
294 ASTContext &C = StateMgr.getContext();
Mike Stump11289f42009-09-09 15:08:12 +0000295
Zhongxing Xuf7a6de32009-06-28 09:26:15 +0000296 // Special case: handle store of pointer values (Loc) to pointers via
297 // a cast to intXX_t*, void*, etc. This is needed to handle
298 // OSCompareAndSwap32Barrier/OSCompareAndSwap64Barrier.
299 if (isa<Loc>(V) || isa<nonloc::LocAsInteger>(V))
300 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
301 // FIXME: Should check for index 0.
302 QualType T = ER->getLocationType(C);
Mike Stump11289f42009-09-09 15:08:12 +0000303
Zhongxing Xuf7a6de32009-06-28 09:26:15 +0000304 if (isHigherOrderRawPtr(T, C))
305 R = ER->getSuperRegion();
Mike Stump11289f42009-09-09 15:08:12 +0000306 }
307
Zhongxing Xuf7a6de32009-06-28 09:26:15 +0000308 if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
309 return store;
Ted Kremenek608677a2009-08-21 23:25:54 +0000310
Ted Kremenekc4c48be2009-08-25 23:29:04 +0000311 const TypedRegion *TyR = cast<TypedRegion>(R);
Mike Stump11289f42009-09-09 15:08:12 +0000312
Ted Kremenekc4c48be2009-08-25 23:29:04 +0000313 // Do not bind to arrays. We need to explicitly check for this so that
314 // we do not encounter any weirdness of trying to load/store from arrays.
315 if (TyR->isBoundable() && TyR->getValueType(C)->isArrayType())
Mike Stump11289f42009-09-09 15:08:12 +0000316 return store;
Ted Kremenekc4c48be2009-08-25 23:29:04 +0000317
Zhongxing Xuf7a6de32009-06-28 09:26:15 +0000318 if (nonloc::LocAsInteger *X = dyn_cast<nonloc::LocAsInteger>(&V)) {
319 // Only convert 'V' to a location iff the underlying region type
320 // is a location as well.
321 // FIXME: We are allowing a store of an arbitrary location to
322 // a pointer. We may wish to flag a type error here if the types
323 // are incompatible. This may also cause lots of breakage
324 // elsewhere. Food for thought.
Mike Stump11289f42009-09-09 15:08:12 +0000325 if (TyR->isBoundable() && Loc::IsLocType(TyR->getValueType(C)))
Ted Kremenekc4c48be2009-08-25 23:29:04 +0000326 V = X->getLoc();
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +0000327 }
Zhongxing Xuf7a6de32009-06-28 09:26:15 +0000328
329 BindingsTy B = GetBindings(store);
330 return V.isUnknown()
331 ? VBFactory.Remove(B, R).getRoot()
332 : VBFactory.Add(B, R, V).getRoot();
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +0000333}
334
Ted Kremenekf929b0a2009-01-07 22:56:17 +0000335Store BasicStoreManager::Remove(Store store, Loc loc) {
336 switch (loc.getSubKind()) {
Zhongxing Xu27f17422008-10-17 05:57:07 +0000337 case loc::MemRegionKind: {
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000338 const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion();
Mike Stump11289f42009-09-09 15:08:12 +0000339
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000340 if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
Ted Kremenek5ca90a22008-10-04 05:50:14 +0000341 return store;
Mike Stump11289f42009-09-09 15:08:12 +0000342
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000343 return VBFactory.Remove(GetBindings(store), R).getRoot();
Ted Kremenek88a6b7f2008-07-17 18:38:48 +0000344 }
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +0000345 default:
Zhongxing Xu27f17422008-10-17 05:57:07 +0000346 assert ("Remove for given Loc type not yet implemented.");
Ted Kremenek88a6b7f2008-07-17 18:38:48 +0000347 return store;
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +0000348 }
349}
Ted Kremenek88a6b7f2008-07-17 18:38:48 +0000350
Ted Kremenekcee28a42009-08-02 04:45:08 +0000351void
352BasicStoreManager::RemoveDeadBindings(GRState &state, Stmt* Loc,
Ted Kremenek16fbfe62009-01-21 22:26:05 +0000353 SymbolReaper& SymReaper,
Ted Kremenekcee28a42009-08-02 04:45:08 +0000354 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
Mike Stump11289f42009-09-09 15:08:12 +0000355{
Ted Kremenekcee28a42009-08-02 04:45:08 +0000356 Store store = state.getStore();
Ted Kremenek1fe63ac2009-03-05 16:32:59 +0000357 BindingsTy B = GetBindings(store);
Zhongxing Xu27f17422008-10-17 05:57:07 +0000358 typedef SVal::symbol_iterator symbol_iterator;
Mike Stump11289f42009-09-09 15:08:12 +0000359
Ted Kremenek88a6b7f2008-07-17 18:38:48 +0000360 // Iterate over the variable bindings.
Ted Kremenek1fe63ac2009-03-05 16:32:59 +0000361 for (BindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) {
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000362 if (const VarRegion *VR = dyn_cast<VarRegion>(I.getKey())) {
Ted Kremenekc32f2c22009-12-04 20:32:20 +0000363 if (SymReaper.isLive(Loc, VR))
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000364 RegionRoots.push_back(VR);
365 else
366 continue;
Ted Kremenek88a6b7f2008-07-17 18:38:48 +0000367 }
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000368 else if (isa<ObjCIvarRegion>(I.getKey())) {
369 RegionRoots.push_back(I.getKey());
370 }
371 else
372 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000373
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000374 // Mark the bindings in the data as live.
375 SVal X = I.getData();
376 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
377 SymReaper.markLive(*SI);
Ted Kremenek2f340d6c2009-03-05 16:31:07 +0000378 }
Mike Stump11289f42009-09-09 15:08:12 +0000379
Ted Kremenek88a6b7f2008-07-17 18:38:48 +0000380 // Scan for live variables and live symbols.
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000381 llvm::SmallPtrSet<const MemRegion*, 10> Marked;
Mike Stump11289f42009-09-09 15:08:12 +0000382
Ted Kremenek5ca90a22008-10-04 05:50:14 +0000383 while (!RegionRoots.empty()) {
Ted Kremenekdb5ae0a2008-10-17 22:52:40 +0000384 const MemRegion* MR = RegionRoots.back();
Ted Kremenek5ca90a22008-10-04 05:50:14 +0000385 RegionRoots.pop_back();
Mike Stump11289f42009-09-09 15:08:12 +0000386
Ted Kremenekdb5ae0a2008-10-17 22:52:40 +0000387 while (MR) {
388 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(MR)) {
Ted Kremenek16fbfe62009-01-21 22:26:05 +0000389 SymReaper.markLive(SymR->getSymbol());
Ted Kremenekdb5ae0a2008-10-17 22:52:40 +0000390 break;
391 }
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000392 else if (isa<VarRegion>(MR) || isa<ObjCIvarRegion>(MR)) {
393 if (Marked.count(MR))
Ted Kremenekdb5ae0a2008-10-17 22:52:40 +0000394 break;
Mike Stump11289f42009-09-09 15:08:12 +0000395
396 Marked.insert(MR);
Zhongxing Xuc7b9f952010-02-05 03:01:53 +0000397 SVal X = Retrieve(state.getStore(), loc::MemRegionVal(MR));
Mike Stump11289f42009-09-09 15:08:12 +0000398
Ted Kremenekdb5ae0a2008-10-17 22:52:40 +0000399 // FIXME: We need to handle symbols nested in region definitions.
Ted Kremenek16fbfe62009-01-21 22:26:05 +0000400 for (symbol_iterator SI=X.symbol_begin(),SE=X.symbol_end();SI!=SE;++SI)
401 SymReaper.markLive(*SI);
Mike Stump11289f42009-09-09 15:08:12 +0000402
Ted Kremenekdb5ae0a2008-10-17 22:52:40 +0000403 if (!isa<loc::MemRegionVal>(X))
404 break;
Mike Stump11289f42009-09-09 15:08:12 +0000405
Ted Kremenekdb5ae0a2008-10-17 22:52:40 +0000406 const loc::MemRegionVal& LVD = cast<loc::MemRegionVal>(X);
407 RegionRoots.push_back(LVD.getRegion());
408 break;
409 }
410 else if (const SubRegion* R = dyn_cast<SubRegion>(MR))
411 MR = R->getSuperRegion();
412 else
413 break;
414 }
Ted Kremenek88a6b7f2008-07-17 18:38:48 +0000415 }
Mike Stump11289f42009-09-09 15:08:12 +0000416
417 // Remove dead variable bindings.
Ted Kremenek1fe63ac2009-03-05 16:32:59 +0000418 for (BindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) {
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000419 const MemRegion* R = I.getKey();
Mike Stump11289f42009-09-09 15:08:12 +0000420
Ted Kremenek5ca90a22008-10-04 05:50:14 +0000421 if (!Marked.count(R)) {
Zhongxing Xu7718ae42009-06-23 09:02:15 +0000422 store = Remove(store, ValMgr.makeLoc(R));
Zhongxing Xu27f17422008-10-17 05:57:07 +0000423 SVal X = I.getData();
Mike Stump11289f42009-09-09 15:08:12 +0000424
Ted Kremenek88a6b7f2008-07-17 18:38:48 +0000425 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
Ted Kremenek16fbfe62009-01-21 22:26:05 +0000426 SymReaper.maybeDead(*SI);
Ted Kremenek88a6b7f2008-07-17 18:38:48 +0000427 }
Ted Kremenek5ca90a22008-10-04 05:50:14 +0000428 }
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000429
Ted Kremenekcee28a42009-08-02 04:45:08 +0000430 // Write the store back.
431 state.setStore(store);
Ted Kremenek88a6b7f2008-07-17 18:38:48 +0000432}
Ted Kremenek67102b22008-08-19 16:51:45 +0000433
Ted Kremenek608677a2009-08-21 23:25:54 +0000434Store BasicStoreManager::scanForIvars(Stmt *B, const Decl* SelfDecl,
435 const MemRegion *SelfRegion, Store St) {
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000436 for (Stmt::child_iterator CI=B->child_begin(), CE=B->child_end();
437 CI != CE; ++CI) {
Mike Stump11289f42009-09-09 15:08:12 +0000438
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000439 if (!*CI)
440 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000441
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000442 // Check if the statement is an ivar reference. We only
443 // care about self.ivar.
444 if (ObjCIvarRefExpr *IV = dyn_cast<ObjCIvarRefExpr>(*CI)) {
445 const Expr *Base = IV->getBase()->IgnoreParenCasts();
446 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Base)) {
447 if (DR->getDecl() == SelfDecl) {
448 const MemRegion *IVR = MRMgr.getObjCIvarRegion(IV->getDecl(),
Mike Stump11289f42009-09-09 15:08:12 +0000449 SelfRegion);
450 SVal X = ValMgr.getRegionValueSymbolVal(IVR);
Zhongxing Xu7718ae42009-06-23 09:02:15 +0000451 St = BindInternal(St, ValMgr.makeLoc(IVR), X);
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000452 }
453 }
454 }
455 else
Ted Kremenek608677a2009-08-21 23:25:54 +0000456 St = scanForIvars(*CI, SelfDecl, SelfRegion, St);
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000457 }
Mike Stump11289f42009-09-09 15:08:12 +0000458
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000459 return St;
460}
461
Mike Stump11289f42009-09-09 15:08:12 +0000462Store BasicStoreManager::getInitialStore(const LocationContext *InitLoc) {
Ted Kremenek67102b22008-08-19 16:51:45 +0000463 // The LiveVariables information already has a compilation of all VarDecls
464 // used in the function. Iterate through this set, and "symbolicate"
465 // any VarDecl whose value originally comes from outside the function.
Ted Kremenek67102b22008-08-19 16:51:45 +0000466 typedef LiveVariables::AnalysisDataTy LVDataTy;
Zhongxing Xu5f078cb2009-08-17 06:19:58 +0000467 LVDataTy& D = InitLoc->getLiveVariables()->getAnalysisData();
Ted Kremenek67102b22008-08-19 16:51:45 +0000468 Store St = VBFactory.GetEmptyMap().getRoot();
469
470 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
Douglas Gregor5251f1b2008-10-21 16:13:35 +0000471 NamedDecl* ND = const_cast<NamedDecl*>(I->first);
Ted Kremenek67102b22008-08-19 16:51:45 +0000472
Ted Kremenekf3be44f2008-10-24 20:32:16 +0000473 // Handle implicit parameters.
474 if (ImplicitParamDecl* PD = dyn_cast<ImplicitParamDecl>(ND)) {
Mike Stump11289f42009-09-09 15:08:12 +0000475 const Decl& CD = *InitLoc->getDecl();
Ted Kremenekf3be44f2008-10-24 20:32:16 +0000476 if (const ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(&CD)) {
477 if (MD->getSelfDecl() == PD) {
Ted Kremenekd970acb2009-12-16 23:53:37 +0000478 // FIXME: Add type constraints (when they become available) to
479 // SelfRegion? (i.e., it implements MD->getClassInterface()).
480 const MemRegion *VR = MRMgr.getVarRegion(PD, InitLoc);
481 const MemRegion *SelfRegion =
482 ValMgr.getRegionValueSymbolVal(VR).getAsRegion();
483 assert(SelfRegion);
484 St = BindInternal(St, ValMgr.makeLoc(VR),
485 loc::MemRegionVal(SelfRegion));
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000486 // Scan the method for ivar references. While this requires an
487 // entire AST scan, the cost should not be high in practice.
Ted Kremenek608677a2009-08-21 23:25:54 +0000488 St = scanForIvars(MD->getBody(), PD, SelfRegion, St);
Ted Kremenekf3be44f2008-10-24 20:32:16 +0000489 }
490 }
491 }
492 else if (VarDecl* VD = dyn_cast<VarDecl>(ND)) {
Ted Kremenekc7fef2a2009-03-23 15:42:58 +0000493 // Only handle simple types that we can symbolicate.
494 if (!SymbolManager::canSymbolicate(VD->getType()))
495 continue;
Ted Kremenek67102b22008-08-19 16:51:45 +0000496
Ted Kremenekb36e01d2009-03-18 22:10:22 +0000497 // Initialize globals and parameters to symbolic values.
498 // Initialize local variables to undefined.
Ted Kremenek14536f62009-08-21 22:28:32 +0000499 const MemRegion *R = ValMgr.getRegionManager().getVarRegion(VD, InitLoc);
Ted Kremenek7020eae2009-09-11 22:07:28 +0000500 SVal X = UndefinedVal();
501 if (R->hasGlobalsOrParametersStorage())
502 X = ValMgr.getRegionValueSymbolVal(R);
Ted Kremenek67102b22008-08-19 16:51:45 +0000503
Zhongxing Xu7718ae42009-06-23 09:02:15 +0000504 St = BindInternal(St, ValMgr.makeLoc(R), X);
Ted Kremenek67102b22008-08-19 16:51:45 +0000505 }
506 }
507 return St;
508}
Ted Kremenek19edd212008-08-19 22:24:03 +0000509
Ted Kremenekb006b822009-11-04 00:09:15 +0000510Store BasicStoreManager::BindDeclInternal(Store store, const VarRegion* VR,
Zhongxing Xuaf7415f2008-12-20 06:32:12 +0000511 SVal* InitVal) {
Mike Stump11289f42009-09-09 15:08:12 +0000512
Ted Kremenek4e7713c2008-08-23 00:50:55 +0000513 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
Ted Kremenekb006b822009-11-04 00:09:15 +0000514 const VarDecl *VD = VR->getDecl();
Mike Stump11289f42009-09-09 15:08:12 +0000515
Zhongxing Xud95495f2008-08-21 22:34:01 +0000516 // BasicStore does not model arrays and structs.
517 if (VD->getType()->isArrayType() || VD->getType()->isStructureType())
518 return store;
519
520 if (VD->hasGlobalStorage()) {
521 // Handle variables with global storage: extern, static, PrivateExtern.
522
523 // FIXME:: static variables may have an initializer, but the second time a
524 // function is called those values may not be current. Currently, a function
525 // will not be called more than once.
526
527 // Static global variables should not be visited here.
528 assert(!(VD->getStorageClass() == VarDecl::Static &&
529 VD->isFileVarDecl()));
Mike Stump11289f42009-09-09 15:08:12 +0000530
Zhongxing Xud95495f2008-08-21 22:34:01 +0000531 // Process static variables.
532 if (VD->getStorageClass() == VarDecl::Static) {
533 // C99: 6.7.8 Initialization
534 // If an object that has static storage duration is not initialized
Mike Stump11289f42009-09-09 15:08:12 +0000535 // explicitly, then:
536 // —if it has pointer type, it is initialized to a null pointer;
537 // —if it has arithmetic type, it is initialized to (positive or
Zhongxing Xud95495f2008-08-21 22:34:01 +0000538 // unsigned) zero;
Ted Kremenekcd639212008-11-12 19:18:35 +0000539 if (!InitVal) {
Zhongxing Xud95495f2008-08-21 22:34:01 +0000540 QualType T = VD->getType();
Zhongxing Xu27f17422008-10-17 05:57:07 +0000541 if (Loc::IsLocType(T))
Ted Kremenekb006b822009-11-04 00:09:15 +0000542 store = BindInternal(store, loc::MemRegionVal(VR),
Zhongxing Xu8f6855e642008-10-21 06:27:32 +0000543 loc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xud95495f2008-08-21 22:34:01 +0000544 else if (T->isIntegerType())
Ted Kremenekb006b822009-11-04 00:09:15 +0000545 store = BindInternal(store, loc::MemRegionVal(VR),
Zhongxing Xu8f6855e642008-10-21 06:27:32 +0000546 nonloc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xud95495f2008-08-21 22:34:01 +0000547 else {
548 // assert(0 && "ignore other types of variables");
549 }
550 } else {
Ted Kremenekb006b822009-11-04 00:09:15 +0000551 store = BindInternal(store, loc::MemRegionVal(VR), *InitVal);
Zhongxing Xud95495f2008-08-21 22:34:01 +0000552 }
553 }
554 } else {
555 // Process local scalar variables.
556 QualType T = VD->getType();
Ted Kremenek24c85132009-07-03 00:36:16 +0000557 if (ValMgr.getSymbolManager().canSymbolicate(T)) {
Ted Kremenekcd639212008-11-12 19:18:35 +0000558 SVal V = InitVal ? *InitVal : UndefinedVal();
Ted Kremenekb006b822009-11-04 00:09:15 +0000559 store = BindInternal(store, loc::MemRegionVal(VR), V);
Zhongxing Xud95495f2008-08-21 22:34:01 +0000560 }
561 }
562
563 return store;
564}
565
Ted Kremenek799bb6e2009-06-24 23:06:47 +0000566void BasicStoreManager::print(Store store, llvm::raw_ostream& Out,
Ted Kremenek19edd212008-08-19 22:24:03 +0000567 const char* nl, const char *sep) {
Mike Stump11289f42009-09-09 15:08:12 +0000568
Ted Kremenek1fe63ac2009-03-05 16:32:59 +0000569 BindingsTy B = GetBindings(store);
Ted Kremenek19edd212008-08-19 22:24:03 +0000570 Out << "Variables:" << nl;
Mike Stump11289f42009-09-09 15:08:12 +0000571
Ted Kremenek19edd212008-08-19 22:24:03 +0000572 bool isFirst = true;
Mike Stump11289f42009-09-09 15:08:12 +0000573
Ted Kremenek1fe63ac2009-03-05 16:32:59 +0000574 for (BindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I) {
Ted Kremenek799bb6e2009-06-24 23:06:47 +0000575 if (isFirst)
576 isFirst = false;
577 else
578 Out << nl;
Mike Stump11289f42009-09-09 15:08:12 +0000579
Ted Kremeneka6904ff2009-07-13 23:53:06 +0000580 Out << ' ' << I.getKey() << " : " << I.getData();
Ted Kremenek19edd212008-08-19 22:24:03 +0000581 }
582}
Ted Kremenekc83e7552008-08-29 00:47:32 +0000583
Ted Kremenek1b9e1032008-09-03 03:06:11 +0000584
585void BasicStoreManager::iterBindings(Store store, BindingsHandler& f) {
Ted Kremenek1fe63ac2009-03-05 16:32:59 +0000586 BindingsTy B = GetBindings(store);
Mike Stump11289f42009-09-09 15:08:12 +0000587
Ted Kremenek1fe63ac2009-03-05 16:32:59 +0000588 for (BindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I)
Ted Kremenek2f340d6c2009-03-05 16:31:07 +0000589 f.HandleBinding(*this, store, I.getKey(), I.getData());
Ted Kremenek5ca90a22008-10-04 05:50:14 +0000590
Ted Kremenekc83e7552008-08-29 00:47:32 +0000591}
592
Ted Kremenek1b9e1032008-09-03 03:06:11 +0000593StoreManager::BindingsHandler::~BindingsHandler() {}
Ted Kremenekbca70672009-07-29 18:16:25 +0000594
595//===----------------------------------------------------------------------===//
596// Binding invalidation.
597//===----------------------------------------------------------------------===//
598
599const GRState *BasicStoreManager::InvalidateRegion(const GRState *state,
600 const MemRegion *R,
601 const Expr *E,
Ted Kremenek1eb68092009-10-16 00:30:49 +0000602 unsigned Count,
603 InvalidatedSymbols *IS) {
Zhongxing Xuf8f3f9d2009-11-10 02:17:20 +0000604 R = R->StripCasts();
Mike Stump11289f42009-09-09 15:08:12 +0000605
Ted Kremenekbca70672009-07-29 18:16:25 +0000606 if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
607 return state;
Mike Stump11289f42009-09-09 15:08:12 +0000608
Ted Kremenek1eb68092009-10-16 00:30:49 +0000609 if (IS) {
610 BindingsTy B = GetBindings(state->getStore());
611 if (BindingsTy::data_type *Val = B.lookup(R)) {
612 if (SymbolRef Sym = Val->getAsSymbol())
613 IS->insert(Sym);
614 }
615 }
616
Ted Kremenekbca70672009-07-29 18:16:25 +0000617 QualType T = cast<TypedRegion>(R)->getValueType(R->getContext());
Ted Kremeneke41b81e2009-09-27 20:45:21 +0000618 SVal V = ValMgr.getConjuredSymbolVal(R, E, T, Count);
Ted Kremenekbca70672009-07-29 18:16:25 +0000619 return Bind(state, loc::MemRegionVal(R), V);
620}
621