blob: c2bf7a8d712620672d41ec8b484412f187f169ab [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"
Zhongxing Xu5f078cb2009-08-17 06:19:58 +000016#include "clang/Analysis/PathSensitive/AnalysisContext.h"
Ted Kremenek67102b22008-08-19 16:51:45 +000017#include "clang/Analysis/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
Ted Kremenekac7c7242009-07-21 21:03:30 +000047 SValuator::CastResult Retrieve(const GRState *state, Loc loc,
Ted Kremenekbca70672009-07-29 18:16:25 +000048 QualType T = QualType());
Mike Stump11289f42009-09-09 15:08:12 +000049
Ted Kremenekbca70672009-07-29 18:16:25 +000050 const GRState *InvalidateRegion(const GRState *state, const MemRegion *R,
Ted Kremenek1eb68092009-10-16 00:30:49 +000051 const Expr *E, unsigned Count,
52 InvalidatedSymbols *IS);
Zhongxing Xuaf7415f2008-12-20 06:32:12 +000053
Ted Kremenek609df302009-06-17 22:02:04 +000054 const GRState *Bind(const GRState *state, Loc L, SVal V) {
55 return state->makeWithStore(BindInternal(state->getStore(), L, V));
Zhongxing Xuaf7415f2008-12-20 06:32:12 +000056 }
57
Ted Kremenek608677a2009-08-21 23:25:54 +000058 Store scanForIvars(Stmt *B, const Decl* SelfDecl,
59 const MemRegion *SelfRegion, Store St);
Mike Stump11289f42009-09-09 15:08:12 +000060
61 Store BindInternal(Store St, Loc loc, SVal V);
Ted Kremenekf929b0a2009-01-07 22:56:17 +000062 Store Remove(Store St, Loc loc);
Zhongxing Xu5f078cb2009-08-17 06:19:58 +000063 Store getInitialStore(const LocationContext *InitLoc);
Zhongxing Xuf5e7c902008-10-07 01:31:04 +000064
Zhongxing Xu232c7922008-10-16 06:09:51 +000065 // FIXME: Investigate what is using this. This method should be removed.
Ted Kremenek14536f62009-08-21 22:28:32 +000066 virtual Loc getLoc(const VarDecl* VD, const LocationContext *LC) {
67 return ValMgr.makeLoc(MRMgr.getVarRegion(VD, LC));
Zhongxing Xuf5e7c902008-10-07 01:31:04 +000068 }
Mike Stump11289f42009-09-09 15:08:12 +000069
Ted Kremenek609df302009-06-17 22:02:04 +000070 const GRState *BindCompoundLiteral(const GRState *state,
71 const CompoundLiteralExpr* cl,
72 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);
78 SVal getLValueCompoundLiteral(const CompoundLiteralExpr *CL);
79 SVal getLValueIvar(const ObjCIvarDecl* D, SVal Base);
80 SVal getLValueField(const FieldDecl *D, SVal Base);
81 SVal getLValueElement(QualType elementType, SVal Offset, SVal Base);
Zhongxing Xub0a48752008-10-23 03:10:39 +000082
Ted Kremenekf3be44f2008-10-24 20:32:16 +000083 /// ArrayToPointer - Used by GRExprEngine::VistCast to handle implicit
84 /// conversions between arrays and pointers.
Zhongxing Xua865b792009-03-30 05:55:46 +000085 SVal ArrayToPointer(Loc Array) { return Array; }
Mike Stump11289f42009-09-09 15:08:12 +000086
Ted Kremenekd368de72008-12-05 00:47:52 +000087 /// RemoveDeadBindings - Scans a BasicStore of 'state' for dead values.
Ted Kremenekcee28a42009-08-02 04:45:08 +000088 /// It updatees the GRState object in place with the values removed.
89 void RemoveDeadBindings(GRState &state, Stmt* Loc, SymbolReaper& SymReaper,
90 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
Zhongxing Xud95495f2008-08-21 22:34:01 +000091
Ted Kremenekf3be44f2008-10-24 20:32:16 +000092 void iterBindings(Store store, BindingsHandler& f);
Ted Kremenek1b9e1032008-09-03 03:06:11 +000093
Ted Kremenekb006b822009-11-04 00:09:15 +000094 const GRState *BindDecl(const GRState *state, const VarRegion *VR,
95 SVal InitVal) {
96 return state->makeWithStore(BindDeclInternal(state->getStore(), VR,
Ted Kremenek14536f62009-08-21 22:28:32 +000097 &InitVal));
Zhongxing Xuaf7415f2008-12-20 06:32:12 +000098 }
99
Ted Kremenekb006b822009-11-04 00:09:15 +0000100 const GRState *BindDeclWithNoInit(const GRState *state, const VarRegion *VR) {
101 return state->makeWithStore(BindDeclInternal(state->getStore(), VR, 0));
Zhongxing Xuaf7415f2008-12-20 06:32:12 +0000102 }
103
Ted Kremenekb006b822009-11-04 00:09:15 +0000104 Store BindDeclInternal(Store store, const VarRegion *VR, SVal *InitVal);
Zhongxing Xud95495f2008-08-21 22:34:01 +0000105
Ted Kremenek1fe63ac2009-03-05 16:32:59 +0000106 static inline BindingsTy GetBindings(Store store) {
107 return BindingsTy(static_cast<const BindingsTy::TreeTy*>(store));
Ted Kremenek19edd212008-08-19 22:24:03 +0000108 }
109
Ted Kremenek799bb6e2009-06-24 23:06:47 +0000110 void print(Store store, llvm::raw_ostream& Out, const char* nl,
111 const char *sep);
Zhongxing Xuec7e7df2009-04-03 07:33:13 +0000112
113private:
114 ASTContext& getContext() { return StateMgr.getContext(); }
Ted Kremenek1b9e1032008-09-03 03:06:11 +0000115};
Mike Stump11289f42009-09-09 15:08:12 +0000116
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +0000117} // end anonymous namespace
118
119
Ted Kremeneke91874f72008-08-28 23:31:31 +0000120StoreManager* clang::CreateBasicStoreManager(GRStateManager& StMgr) {
121 return new BasicStoreManager(StMgr);
Ted Kremenek2a2c8752008-08-25 19:33:03 +0000122}
Zhongxing Xudab76fd2008-10-21 06:54:23 +0000123
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000124SVal BasicStoreManager::getLValueVar(const VarDecl* VD,
Ted Kremenek14536f62009-08-21 22:28:32 +0000125 const LocationContext *LC) {
126 return ValMgr.makeLoc(MRMgr.getVarRegion(VD, LC));
Ted Kremenek3ad391d2008-10-17 00:51:01 +0000127}
Zhongxing Xu0d2706f2008-10-25 14:18:57 +0000128
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000129SVal BasicStoreManager::getLValueString(const StringLiteral* S) {
Zhongxing Xu7718ae42009-06-23 09:02:15 +0000130 return ValMgr.makeLoc(MRMgr.getStringRegion(S));
Zhongxing Xu0d2706f2008-10-25 14:18:57 +0000131}
Zhongxing Xu2c677c32008-11-07 10:38:33 +0000132
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000133SVal BasicStoreManager::getLValueCompoundLiteral(const CompoundLiteralExpr* CL){
Zhongxing Xu7718ae42009-06-23 09:02:15 +0000134 return ValMgr.makeLoc(MRMgr.getCompoundLiteralRegion(CL));
Zhongxing Xu2c677c32008-11-07 10:38:33 +0000135}
136
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000137SVal BasicStoreManager::getLValueIvar(const ObjCIvarDecl* D, SVal Base) {
Mike Stump11289f42009-09-09 15:08:12 +0000138
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000139 if (Base.isUnknownOrUndef())
140 return Base;
141
142 Loc BaseL = cast<Loc>(Base);
143
144 if (isa<loc::MemRegionVal>(BaseL)) {
145 const MemRegion *BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
Ted Kremenek608677a2009-08-21 23:25:54 +0000146 return ValMgr.makeLoc(MRMgr.getObjCIvarRegion(D, BaseR));
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000147 }
Mike Stump11289f42009-09-09 15:08:12 +0000148
Ted Kremenek3ad391d2008-10-17 00:51:01 +0000149 return UnknownVal();
150}
Ted Kremenekb5670fd2008-12-13 21:49:13 +0000151
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000152SVal BasicStoreManager::getLValueField(const FieldDecl* D, SVal Base) {
Ted Kremenek8b103c62008-10-17 20:28:54 +0000153
154 if (Base.isUnknownOrUndef())
155 return Base;
Mike Stump11289f42009-09-09 15:08:12 +0000156
157 Loc BaseL = cast<Loc>(Base);
Ted Kremenek8b103c62008-10-17 20:28:54 +0000158 const MemRegion* BaseR = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000159
Ted Kremenek8b103c62008-10-17 20:28:54 +0000160 switch(BaseL.getSubKind()) {
Ted Kremenek8b103c62008-10-17 20:28:54 +0000161 case loc::GotoLabelKind:
Ted Kremenek8b103c62008-10-17 20:28:54 +0000162 return UndefinedVal();
163
164 case loc::MemRegionKind:
165 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
166 break;
Mike Stump11289f42009-09-09 15:08:12 +0000167
Ted Kremenek8b103c62008-10-17 20:28:54 +0000168 case loc::ConcreteIntKind:
Ted Kremenek8b103c62008-10-17 20:28:54 +0000169 // While these seem funny, this can happen through casts.
170 // FIXME: What we should return is the field offset. For example,
171 // add the field offset to the integer value. That way funny things
172 // like this work properly: &(((struct foo *) 0xa)->f)
173 return Base;
174
175 default:
176 assert ("Unhandled Base.");
177 return Base;
178 }
Mike Stump11289f42009-09-09 15:08:12 +0000179
Zhongxing Xu7718ae42009-06-23 09:02:15 +0000180 return ValMgr.makeLoc(MRMgr.getFieldRegion(D, BaseR));
Ted Kremenek3ad391d2008-10-17 00:51:01 +0000181}
Ted Kremenek2a2c8752008-08-25 19:33:03 +0000182
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000183SVal BasicStoreManager::getLValueElement(QualType elementType,
184 SVal Offset, SVal Base) {
Ted Kremenek62cc9da2008-12-09 21:20:27 +0000185
Ted Kremenek62cc9da2008-12-09 21:20:27 +0000186 if (Base.isUnknownOrUndef())
187 return Base;
Mike Stump11289f42009-09-09 15:08:12 +0000188
189 Loc BaseL = cast<Loc>(Base);
Zhongxing Xu6377a982009-06-30 07:41:27 +0000190 const MemRegion* BaseR = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000191
Ted Kremenek62cc9da2008-12-09 21:20:27 +0000192 switch(BaseL.getSubKind()) {
Ted Kremenek62cc9da2008-12-09 21:20:27 +0000193 case loc::GotoLabelKind:
Ted Kremenek62cc9da2008-12-09 21:20:27 +0000194 // Technically we can get here if people do funny things with casts.
195 return UndefinedVal();
Mike Stump11289f42009-09-09 15:08:12 +0000196
Ted Kremenekf065b152008-12-13 19:24:37 +0000197 case loc::MemRegionKind: {
198 const MemRegion *R = cast<loc::MemRegionVal>(BaseL).getRegion();
Mike Stump11289f42009-09-09 15:08:12 +0000199
Ted Kremeneke5c31582009-05-05 00:06:16 +0000200 if (isa<ElementRegion>(R)) {
Zhongxing Xu6ebde272009-05-04 08:52:47 +0000201 // int x;
202 // char* y = (char*) &x;
203 // 'y' => ElementRegion(0, VarRegion('x'))
204 // y[0] = 'a';
Ted Kremenek422d81d2009-01-27 18:29:03 +0000205 return Base;
206 }
Mike Stump11289f42009-09-09 15:08:12 +0000207
Ted Kremenek8fd18792009-06-30 20:24:11 +0000208 if (isa<TypedRegion>(R) || isa<SymbolicRegion>(R)) {
209 BaseR = R;
Ted Kremenekf065b152008-12-13 19:24:37 +0000210 break;
211 }
Mike Stump11289f42009-09-09 15:08:12 +0000212
Ted Kremenek62cc9da2008-12-09 21:20:27 +0000213 break;
Ted Kremenekf065b152008-12-13 19:24:37 +0000214 }
Ted Kremenek422d81d2009-01-27 18:29:03 +0000215
Ted Kremenek62cc9da2008-12-09 21:20:27 +0000216 case loc::ConcreteIntKind:
217 // While these seem funny, this can happen through casts.
218 // FIXME: What we should return is the field offset. For example,
219 // add the field offset to the integer value. That way funny things
220 // like this work properly: &(((struct foo *) 0xa)->f)
221 return Base;
Mike Stump11289f42009-09-09 15:08:12 +0000222
Ted Kremenek62cc9da2008-12-09 21:20:27 +0000223 default:
224 assert ("Unhandled Base.");
225 return Base;
226 }
Mike Stump11289f42009-09-09 15:08:12 +0000227
228 if (BaseR) {
Zhongxing Xu7718ae42009-06-23 09:02:15 +0000229 return ValMgr.makeLoc(MRMgr.getElementRegion(elementType, UnknownVal(),
230 BaseR, getContext()));
Ted Kremenek8fd18792009-06-30 20:24:11 +0000231 }
Ted Kremenekf065b152008-12-13 19:24:37 +0000232 else
233 return UnknownVal();
Zhongxing Xu232c7922008-10-16 06:09:51 +0000234}
235
Ted Kremenek3941d222009-04-29 16:03:27 +0000236static bool isHigherOrderRawPtr(QualType T, ASTContext &C) {
Ted Kremenekdf240002009-04-11 00:11:10 +0000237 bool foundPointer = false;
Mike Stump11289f42009-09-09 15:08:12 +0000238 while (1) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000239 const PointerType *PT = T->getAs<PointerType>();
Ted Kremenekdf240002009-04-11 00:11:10 +0000240 if (!PT) {
241 if (!foundPointer)
242 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000243
Ted Kremenek3941d222009-04-29 16:03:27 +0000244 // intptr_t* or intptr_t**, etc?
245 if (T->isIntegerType() && C.getTypeSize(T) == C.getTypeSize(C.VoidPtrTy))
246 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000247
Ted Kremenekdf240002009-04-11 00:11:10 +0000248 QualType X = C.getCanonicalType(T).getUnqualifiedType();
249 return X == C.VoidTy;
250 }
Mike Stump11289f42009-09-09 15:08:12 +0000251
Ted Kremenekdf240002009-04-11 00:11:10 +0000252 foundPointer = true;
253 T = PT->getPointeeType();
Mike Stump11289f42009-09-09 15:08:12 +0000254 }
Ted Kremenekdf240002009-04-11 00:11:10 +0000255}
Mike Stump11289f42009-09-09 15:08:12 +0000256
Ted Kremenekac7c7242009-07-21 21:03:30 +0000257SValuator::CastResult BasicStoreManager::Retrieve(const GRState *state,
258 Loc loc, QualType T) {
Mike Stump11289f42009-09-09 15:08:12 +0000259
Ted Kremenekf929b0a2009-01-07 22:56:17 +0000260 if (isa<UnknownVal>(loc))
Ted Kremenekac7c7242009-07-21 21:03:30 +0000261 return SValuator::CastResult(state, UnknownVal());
Mike Stump11289f42009-09-09 15:08:12 +0000262
Ted Kremenek3ed95432009-08-25 20:51:30 +0000263 assert(!isa<UndefinedVal>(loc));
Mike Stump11289f42009-09-09 15:08:12 +0000264
Ted Kremenekf929b0a2009-01-07 22:56:17 +0000265 switch (loc.getSubKind()) {
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +0000266
Zhongxing Xu27f17422008-10-17 05:57:07 +0000267 case loc::MemRegionKind: {
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000268 const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion();
Mike Stump11289f42009-09-09 15:08:12 +0000269
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000270 if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
Ted Kremenekac7c7242009-07-21 21:03:30 +0000271 return SValuator::CastResult(state, UnknownVal());
Mike Stump11289f42009-09-09 15:08:12 +0000272
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000273 BindingsTy B = GetBindings(state->getStore());
Ted Kremenek3ed95432009-08-25 20:51:30 +0000274 BindingsTy::data_type *Val = B.lookup(R);
Mike Stump11289f42009-09-09 15:08:12 +0000275
Ted Kremenek3ed95432009-08-25 20:51:30 +0000276 if (!Val)
277 break;
Mike Stump11289f42009-09-09 15:08:12 +0000278
Zhongxing Xu731f4622009-11-16 04:49:44 +0000279 return SValuator::CastResult(state,
280 CastRetrievedVal(*Val, cast<TypedRegion>(R), T));
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +0000281 }
Mike Stump11289f42009-09-09 15:08:12 +0000282
Zhongxing Xu27f17422008-10-17 05:57:07 +0000283 case loc::ConcreteIntKind:
284 // Some clients may call GetSVal with such an option simply because
285 // they are doing a quick scan through their Locs (potentially to
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +0000286 // invalidate their bindings). Just return Undefined.
Ted Kremenekac7c7242009-07-21 21:03:30 +0000287 return SValuator::CastResult(state, UndefinedVal());
Mike Stump11289f42009-09-09 15:08:12 +0000288
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +0000289 default:
Zhongxing Xu27f17422008-10-17 05:57:07 +0000290 assert (false && "Invalid Loc.");
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +0000291 break;
292 }
Mike Stump11289f42009-09-09 15:08:12 +0000293
Ted Kremenekac7c7242009-07-21 21:03:30 +0000294 return SValuator::CastResult(state, UnknownVal());
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +0000295}
Mike Stump11289f42009-09-09 15:08:12 +0000296
297Store BasicStoreManager::BindInternal(Store store, Loc loc, SVal V) {
Zhongxing Xud260db12009-06-28 10:16:11 +0000298 if (isa<loc::ConcreteInt>(loc))
299 return store;
300
Zhongxing Xuf7a6de32009-06-28 09:26:15 +0000301 const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion();
302 ASTContext &C = StateMgr.getContext();
Mike Stump11289f42009-09-09 15:08:12 +0000303
Zhongxing Xuf7a6de32009-06-28 09:26:15 +0000304 // Special case: handle store of pointer values (Loc) to pointers via
305 // a cast to intXX_t*, void*, etc. This is needed to handle
306 // OSCompareAndSwap32Barrier/OSCompareAndSwap64Barrier.
307 if (isa<Loc>(V) || isa<nonloc::LocAsInteger>(V))
308 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
309 // FIXME: Should check for index 0.
310 QualType T = ER->getLocationType(C);
Mike Stump11289f42009-09-09 15:08:12 +0000311
Zhongxing Xuf7a6de32009-06-28 09:26:15 +0000312 if (isHigherOrderRawPtr(T, C))
313 R = ER->getSuperRegion();
Mike Stump11289f42009-09-09 15:08:12 +0000314 }
315
Zhongxing Xuf7a6de32009-06-28 09:26:15 +0000316 if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
317 return store;
Ted Kremenek608677a2009-08-21 23:25:54 +0000318
Ted Kremenekc4c48be2009-08-25 23:29:04 +0000319 const TypedRegion *TyR = cast<TypedRegion>(R);
Mike Stump11289f42009-09-09 15:08:12 +0000320
Ted Kremenekc4c48be2009-08-25 23:29:04 +0000321 // Do not bind to arrays. We need to explicitly check for this so that
322 // we do not encounter any weirdness of trying to load/store from arrays.
323 if (TyR->isBoundable() && TyR->getValueType(C)->isArrayType())
Mike Stump11289f42009-09-09 15:08:12 +0000324 return store;
Ted Kremenekc4c48be2009-08-25 23:29:04 +0000325
Zhongxing Xuf7a6de32009-06-28 09:26:15 +0000326 if (nonloc::LocAsInteger *X = dyn_cast<nonloc::LocAsInteger>(&V)) {
327 // Only convert 'V' to a location iff the underlying region type
328 // is a location as well.
329 // FIXME: We are allowing a store of an arbitrary location to
330 // a pointer. We may wish to flag a type error here if the types
331 // are incompatible. This may also cause lots of breakage
332 // elsewhere. Food for thought.
Mike Stump11289f42009-09-09 15:08:12 +0000333 if (TyR->isBoundable() && Loc::IsLocType(TyR->getValueType(C)))
Ted Kremenekc4c48be2009-08-25 23:29:04 +0000334 V = X->getLoc();
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +0000335 }
Zhongxing Xuf7a6de32009-06-28 09:26:15 +0000336
337 BindingsTy B = GetBindings(store);
338 return V.isUnknown()
339 ? VBFactory.Remove(B, R).getRoot()
340 : VBFactory.Add(B, R, V).getRoot();
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +0000341}
342
Ted Kremenekf929b0a2009-01-07 22:56:17 +0000343Store BasicStoreManager::Remove(Store store, Loc loc) {
344 switch (loc.getSubKind()) {
Zhongxing Xu27f17422008-10-17 05:57:07 +0000345 case loc::MemRegionKind: {
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000346 const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion();
Mike Stump11289f42009-09-09 15:08:12 +0000347
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000348 if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
Ted Kremenek5ca90a22008-10-04 05:50:14 +0000349 return store;
Mike Stump11289f42009-09-09 15:08:12 +0000350
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000351 return VBFactory.Remove(GetBindings(store), R).getRoot();
Ted Kremenek88a6b7f2008-07-17 18:38:48 +0000352 }
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +0000353 default:
Zhongxing Xu27f17422008-10-17 05:57:07 +0000354 assert ("Remove for given Loc type not yet implemented.");
Ted Kremenek88a6b7f2008-07-17 18:38:48 +0000355 return store;
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +0000356 }
357}
Ted Kremenek88a6b7f2008-07-17 18:38:48 +0000358
Ted Kremenekcee28a42009-08-02 04:45:08 +0000359void
360BasicStoreManager::RemoveDeadBindings(GRState &state, Stmt* Loc,
Ted Kremenek16fbfe62009-01-21 22:26:05 +0000361 SymbolReaper& SymReaper,
Ted Kremenekcee28a42009-08-02 04:45:08 +0000362 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
Mike Stump11289f42009-09-09 15:08:12 +0000363{
Ted Kremenekcee28a42009-08-02 04:45:08 +0000364 Store store = state.getStore();
Ted Kremenek1fe63ac2009-03-05 16:32:59 +0000365 BindingsTy B = GetBindings(store);
Zhongxing Xu27f17422008-10-17 05:57:07 +0000366 typedef SVal::symbol_iterator symbol_iterator;
Mike Stump11289f42009-09-09 15:08:12 +0000367
Ted Kremenek88a6b7f2008-07-17 18:38:48 +0000368 // Iterate over the variable bindings.
Ted Kremenek1fe63ac2009-03-05 16:32:59 +0000369 for (BindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) {
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000370 if (const VarRegion *VR = dyn_cast<VarRegion>(I.getKey())) {
Ted Kremenekc32f2c22009-12-04 20:32:20 +0000371 if (SymReaper.isLive(Loc, VR))
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000372 RegionRoots.push_back(VR);
373 else
374 continue;
Ted Kremenek88a6b7f2008-07-17 18:38:48 +0000375 }
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000376 else if (isa<ObjCIvarRegion>(I.getKey())) {
377 RegionRoots.push_back(I.getKey());
378 }
379 else
380 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000381
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000382 // Mark the bindings in the data as live.
383 SVal X = I.getData();
384 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
385 SymReaper.markLive(*SI);
Ted Kremenek2f340d6c2009-03-05 16:31:07 +0000386 }
Mike Stump11289f42009-09-09 15:08:12 +0000387
Ted Kremenek88a6b7f2008-07-17 18:38:48 +0000388 // Scan for live variables and live symbols.
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000389 llvm::SmallPtrSet<const MemRegion*, 10> Marked;
Mike Stump11289f42009-09-09 15:08:12 +0000390
Ted Kremenek5ca90a22008-10-04 05:50:14 +0000391 while (!RegionRoots.empty()) {
Ted Kremenekdb5ae0a2008-10-17 22:52:40 +0000392 const MemRegion* MR = RegionRoots.back();
Ted Kremenek5ca90a22008-10-04 05:50:14 +0000393 RegionRoots.pop_back();
Mike Stump11289f42009-09-09 15:08:12 +0000394
Ted Kremenekdb5ae0a2008-10-17 22:52:40 +0000395 while (MR) {
396 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(MR)) {
Ted Kremenek16fbfe62009-01-21 22:26:05 +0000397 SymReaper.markLive(SymR->getSymbol());
Ted Kremenekdb5ae0a2008-10-17 22:52:40 +0000398 break;
399 }
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000400 else if (isa<VarRegion>(MR) || isa<ObjCIvarRegion>(MR)) {
401 if (Marked.count(MR))
Ted Kremenekdb5ae0a2008-10-17 22:52:40 +0000402 break;
Mike Stump11289f42009-09-09 15:08:12 +0000403
404 Marked.insert(MR);
Ted Kremenekcee28a42009-08-02 04:45:08 +0000405 SVal X = Retrieve(&state, loc::MemRegionVal(MR)).getSVal();
Mike Stump11289f42009-09-09 15:08:12 +0000406
Ted Kremenekdb5ae0a2008-10-17 22:52:40 +0000407 // FIXME: We need to handle symbols nested in region definitions.
Ted Kremenek16fbfe62009-01-21 22:26:05 +0000408 for (symbol_iterator SI=X.symbol_begin(),SE=X.symbol_end();SI!=SE;++SI)
409 SymReaper.markLive(*SI);
Mike Stump11289f42009-09-09 15:08:12 +0000410
Ted Kremenekdb5ae0a2008-10-17 22:52:40 +0000411 if (!isa<loc::MemRegionVal>(X))
412 break;
Mike Stump11289f42009-09-09 15:08:12 +0000413
Ted Kremenekdb5ae0a2008-10-17 22:52:40 +0000414 const loc::MemRegionVal& LVD = cast<loc::MemRegionVal>(X);
415 RegionRoots.push_back(LVD.getRegion());
416 break;
417 }
418 else if (const SubRegion* R = dyn_cast<SubRegion>(MR))
419 MR = R->getSuperRegion();
420 else
421 break;
422 }
Ted Kremenek88a6b7f2008-07-17 18:38:48 +0000423 }
Mike Stump11289f42009-09-09 15:08:12 +0000424
425 // Remove dead variable bindings.
Ted Kremenek1fe63ac2009-03-05 16:32:59 +0000426 for (BindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) {
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000427 const MemRegion* R = I.getKey();
Mike Stump11289f42009-09-09 15:08:12 +0000428
Ted Kremenek5ca90a22008-10-04 05:50:14 +0000429 if (!Marked.count(R)) {
Zhongxing Xu7718ae42009-06-23 09:02:15 +0000430 store = Remove(store, ValMgr.makeLoc(R));
Zhongxing Xu27f17422008-10-17 05:57:07 +0000431 SVal X = I.getData();
Mike Stump11289f42009-09-09 15:08:12 +0000432
Ted Kremenek88a6b7f2008-07-17 18:38:48 +0000433 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
Ted Kremenek16fbfe62009-01-21 22:26:05 +0000434 SymReaper.maybeDead(*SI);
Ted Kremenek88a6b7f2008-07-17 18:38:48 +0000435 }
Ted Kremenek5ca90a22008-10-04 05:50:14 +0000436 }
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000437
Ted Kremenekcee28a42009-08-02 04:45:08 +0000438 // Write the store back.
439 state.setStore(store);
Ted Kremenek88a6b7f2008-07-17 18:38:48 +0000440}
Ted Kremenek67102b22008-08-19 16:51:45 +0000441
Ted Kremenek608677a2009-08-21 23:25:54 +0000442Store BasicStoreManager::scanForIvars(Stmt *B, const Decl* SelfDecl,
443 const MemRegion *SelfRegion, Store St) {
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000444 for (Stmt::child_iterator CI=B->child_begin(), CE=B->child_end();
445 CI != CE; ++CI) {
Mike Stump11289f42009-09-09 15:08:12 +0000446
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000447 if (!*CI)
448 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000449
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000450 // Check if the statement is an ivar reference. We only
451 // care about self.ivar.
452 if (ObjCIvarRefExpr *IV = dyn_cast<ObjCIvarRefExpr>(*CI)) {
453 const Expr *Base = IV->getBase()->IgnoreParenCasts();
454 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Base)) {
455 if (DR->getDecl() == SelfDecl) {
456 const MemRegion *IVR = MRMgr.getObjCIvarRegion(IV->getDecl(),
Mike Stump11289f42009-09-09 15:08:12 +0000457 SelfRegion);
458 SVal X = ValMgr.getRegionValueSymbolVal(IVR);
Zhongxing Xu7718ae42009-06-23 09:02:15 +0000459 St = BindInternal(St, ValMgr.makeLoc(IVR), X);
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000460 }
461 }
462 }
463 else
Ted Kremenek608677a2009-08-21 23:25:54 +0000464 St = scanForIvars(*CI, SelfDecl, SelfRegion, St);
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000465 }
Mike Stump11289f42009-09-09 15:08:12 +0000466
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000467 return St;
468}
469
Mike Stump11289f42009-09-09 15:08:12 +0000470Store BasicStoreManager::getInitialStore(const LocationContext *InitLoc) {
Ted Kremenek67102b22008-08-19 16:51:45 +0000471 // The LiveVariables information already has a compilation of all VarDecls
472 // used in the function. Iterate through this set, and "symbolicate"
473 // any VarDecl whose value originally comes from outside the function.
Ted Kremenek67102b22008-08-19 16:51:45 +0000474 typedef LiveVariables::AnalysisDataTy LVDataTy;
Zhongxing Xu5f078cb2009-08-17 06:19:58 +0000475 LVDataTy& D = InitLoc->getLiveVariables()->getAnalysisData();
Ted Kremenek67102b22008-08-19 16:51:45 +0000476 Store St = VBFactory.GetEmptyMap().getRoot();
477
478 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
Douglas Gregor5251f1b2008-10-21 16:13:35 +0000479 NamedDecl* ND = const_cast<NamedDecl*>(I->first);
Ted Kremenek67102b22008-08-19 16:51:45 +0000480
Ted Kremenekf3be44f2008-10-24 20:32:16 +0000481 // Handle implicit parameters.
482 if (ImplicitParamDecl* PD = dyn_cast<ImplicitParamDecl>(ND)) {
Mike Stump11289f42009-09-09 15:08:12 +0000483 const Decl& CD = *InitLoc->getDecl();
Ted Kremenekf3be44f2008-10-24 20:32:16 +0000484 if (const ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(&CD)) {
485 if (MD->getSelfDecl() == PD) {
Ted Kremenek608677a2009-08-21 23:25:54 +0000486 // FIXME: Just use a symbolic region, and remove ObjCObjectRegion
487 // entirely.
488 const ObjCObjectRegion *SelfRegion =
489 MRMgr.getObjCObjectRegion(MD->getClassInterface(),
490 MRMgr.getHeapRegion());
Mike Stump11289f42009-09-09 15:08:12 +0000491
Ted Kremenek14536f62009-08-21 22:28:32 +0000492 St = BindInternal(St, ValMgr.makeLoc(MRMgr.getVarRegion(PD, InitLoc)),
Zhongxing Xu7718ae42009-06-23 09:02:15 +0000493 ValMgr.makeLoc(SelfRegion));
Mike Stump11289f42009-09-09 15:08:12 +0000494
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000495 // Scan the method for ivar references. While this requires an
496 // entire AST scan, the cost should not be high in practice.
Ted Kremenek608677a2009-08-21 23:25:54 +0000497 St = scanForIvars(MD->getBody(), PD, SelfRegion, St);
Ted Kremenekf3be44f2008-10-24 20:32:16 +0000498 }
499 }
500 }
501 else if (VarDecl* VD = dyn_cast<VarDecl>(ND)) {
Ted Kremenekc7fef2a2009-03-23 15:42:58 +0000502 // Only handle simple types that we can symbolicate.
503 if (!SymbolManager::canSymbolicate(VD->getType()))
504 continue;
Ted Kremenek67102b22008-08-19 16:51:45 +0000505
Ted Kremenekb36e01d2009-03-18 22:10:22 +0000506 // Initialize globals and parameters to symbolic values.
507 // Initialize local variables to undefined.
Ted Kremenek14536f62009-08-21 22:28:32 +0000508 const MemRegion *R = ValMgr.getRegionManager().getVarRegion(VD, InitLoc);
Ted Kremenek7020eae2009-09-11 22:07:28 +0000509 SVal X = UndefinedVal();
510 if (R->hasGlobalsOrParametersStorage())
511 X = ValMgr.getRegionValueSymbolVal(R);
Ted Kremenek67102b22008-08-19 16:51:45 +0000512
Zhongxing Xu7718ae42009-06-23 09:02:15 +0000513 St = BindInternal(St, ValMgr.makeLoc(R), X);
Ted Kremenek67102b22008-08-19 16:51:45 +0000514 }
515 }
516 return St;
517}
Ted Kremenek19edd212008-08-19 22:24:03 +0000518
Ted Kremenekb006b822009-11-04 00:09:15 +0000519Store BasicStoreManager::BindDeclInternal(Store store, const VarRegion* VR,
Zhongxing Xuaf7415f2008-12-20 06:32:12 +0000520 SVal* InitVal) {
Mike Stump11289f42009-09-09 15:08:12 +0000521
Ted Kremenek4e7713c2008-08-23 00:50:55 +0000522 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
Ted Kremenekb006b822009-11-04 00:09:15 +0000523 const VarDecl *VD = VR->getDecl();
Mike Stump11289f42009-09-09 15:08:12 +0000524
Zhongxing Xud95495f2008-08-21 22:34:01 +0000525 // BasicStore does not model arrays and structs.
526 if (VD->getType()->isArrayType() || VD->getType()->isStructureType())
527 return store;
528
529 if (VD->hasGlobalStorage()) {
530 // Handle variables with global storage: extern, static, PrivateExtern.
531
532 // FIXME:: static variables may have an initializer, but the second time a
533 // function is called those values may not be current. Currently, a function
534 // will not be called more than once.
535
536 // Static global variables should not be visited here.
537 assert(!(VD->getStorageClass() == VarDecl::Static &&
538 VD->isFileVarDecl()));
Mike Stump11289f42009-09-09 15:08:12 +0000539
Zhongxing Xud95495f2008-08-21 22:34:01 +0000540 // Process static variables.
541 if (VD->getStorageClass() == VarDecl::Static) {
542 // C99: 6.7.8 Initialization
543 // If an object that has static storage duration is not initialized
Mike Stump11289f42009-09-09 15:08:12 +0000544 // explicitly, then:
545 // —if it has pointer type, it is initialized to a null pointer;
546 // —if it has arithmetic type, it is initialized to (positive or
Zhongxing Xud95495f2008-08-21 22:34:01 +0000547 // unsigned) zero;
Ted Kremenekcd639212008-11-12 19:18:35 +0000548 if (!InitVal) {
Zhongxing Xud95495f2008-08-21 22:34:01 +0000549 QualType T = VD->getType();
Zhongxing Xu27f17422008-10-17 05:57:07 +0000550 if (Loc::IsLocType(T))
Ted Kremenekb006b822009-11-04 00:09:15 +0000551 store = BindInternal(store, loc::MemRegionVal(VR),
Zhongxing Xu8f6855e642008-10-21 06:27:32 +0000552 loc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xud95495f2008-08-21 22:34:01 +0000553 else if (T->isIntegerType())
Ted Kremenekb006b822009-11-04 00:09:15 +0000554 store = BindInternal(store, loc::MemRegionVal(VR),
Zhongxing Xu8f6855e642008-10-21 06:27:32 +0000555 nonloc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xud95495f2008-08-21 22:34:01 +0000556 else {
557 // assert(0 && "ignore other types of variables");
558 }
559 } else {
Ted Kremenekb006b822009-11-04 00:09:15 +0000560 store = BindInternal(store, loc::MemRegionVal(VR), *InitVal);
Zhongxing Xud95495f2008-08-21 22:34:01 +0000561 }
562 }
563 } else {
564 // Process local scalar variables.
565 QualType T = VD->getType();
Ted Kremenek24c85132009-07-03 00:36:16 +0000566 if (ValMgr.getSymbolManager().canSymbolicate(T)) {
Ted Kremenekcd639212008-11-12 19:18:35 +0000567 SVal V = InitVal ? *InitVal : UndefinedVal();
Ted Kremenekb006b822009-11-04 00:09:15 +0000568 store = BindInternal(store, loc::MemRegionVal(VR), V);
Zhongxing Xud95495f2008-08-21 22:34:01 +0000569 }
570 }
571
572 return store;
573}
574
Ted Kremenek799bb6e2009-06-24 23:06:47 +0000575void BasicStoreManager::print(Store store, llvm::raw_ostream& Out,
Ted Kremenek19edd212008-08-19 22:24:03 +0000576 const char* nl, const char *sep) {
Mike Stump11289f42009-09-09 15:08:12 +0000577
Ted Kremenek1fe63ac2009-03-05 16:32:59 +0000578 BindingsTy B = GetBindings(store);
Ted Kremenek19edd212008-08-19 22:24:03 +0000579 Out << "Variables:" << nl;
Mike Stump11289f42009-09-09 15:08:12 +0000580
Ted Kremenek19edd212008-08-19 22:24:03 +0000581 bool isFirst = true;
Mike Stump11289f42009-09-09 15:08:12 +0000582
Ted Kremenek1fe63ac2009-03-05 16:32:59 +0000583 for (BindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I) {
Ted Kremenek799bb6e2009-06-24 23:06:47 +0000584 if (isFirst)
585 isFirst = false;
586 else
587 Out << nl;
Mike Stump11289f42009-09-09 15:08:12 +0000588
Ted Kremeneka6904ff2009-07-13 23:53:06 +0000589 Out << ' ' << I.getKey() << " : " << I.getData();
Ted Kremenek19edd212008-08-19 22:24:03 +0000590 }
591}
Ted Kremenekc83e7552008-08-29 00:47:32 +0000592
Ted Kremenek1b9e1032008-09-03 03:06:11 +0000593
594void BasicStoreManager::iterBindings(Store store, BindingsHandler& f) {
Ted Kremenek1fe63ac2009-03-05 16:32:59 +0000595 BindingsTy B = GetBindings(store);
Mike Stump11289f42009-09-09 15:08:12 +0000596
Ted Kremenek1fe63ac2009-03-05 16:32:59 +0000597 for (BindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I)
Ted Kremenek2f340d6c2009-03-05 16:31:07 +0000598 f.HandleBinding(*this, store, I.getKey(), I.getData());
Ted Kremenek5ca90a22008-10-04 05:50:14 +0000599
Ted Kremenekc83e7552008-08-29 00:47:32 +0000600}
601
Ted Kremenek1b9e1032008-09-03 03:06:11 +0000602StoreManager::BindingsHandler::~BindingsHandler() {}
Ted Kremenekbca70672009-07-29 18:16:25 +0000603
604//===----------------------------------------------------------------------===//
605// Binding invalidation.
606//===----------------------------------------------------------------------===//
607
608const GRState *BasicStoreManager::InvalidateRegion(const GRState *state,
609 const MemRegion *R,
610 const Expr *E,
Ted Kremenek1eb68092009-10-16 00:30:49 +0000611 unsigned Count,
612 InvalidatedSymbols *IS) {
Zhongxing Xuf8f3f9d2009-11-10 02:17:20 +0000613 R = R->StripCasts();
Mike Stump11289f42009-09-09 15:08:12 +0000614
Ted Kremenekbca70672009-07-29 18:16:25 +0000615 if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
616 return state;
Mike Stump11289f42009-09-09 15:08:12 +0000617
Ted Kremenek1eb68092009-10-16 00:30:49 +0000618 if (IS) {
619 BindingsTy B = GetBindings(state->getStore());
620 if (BindingsTy::data_type *Val = B.lookup(R)) {
621 if (SymbolRef Sym = Val->getAsSymbol())
622 IS->insert(Sym);
623 }
624 }
625
Ted Kremenekbca70672009-07-29 18:16:25 +0000626 QualType T = cast<TypedRegion>(R)->getValueType(R->getContext());
Ted Kremeneke41b81e2009-09-27 20:45:21 +0000627 SVal V = ValMgr.getConjuredSymbolVal(R, E, T, Count);
Ted Kremenekbca70672009-07-29 18:16:25 +0000628 return Bind(state, loc::MemRegionVal(R), V);
629}
630