blob: 800a76fb0aee0dc97cb71f5b74ab08cce55505ca [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"
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +000018#include "llvm/Support/Compiler.h"
Benjamin Kramer89b422c2009-08-23 12:08:50 +000019#include "llvm/ADT/ImmutableMap.h"
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +000020
21using namespace clang;
22
Mike Stump11289f42009-09-09 15:08:12 +000023typedef llvm::ImmutableMap<const MemRegion*,SVal> BindingsTy;
Ted Kremenek1b9e1032008-09-03 03:06:11 +000024
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +000025namespace {
Mike Stump11289f42009-09-09 15:08:12 +000026
Ted Kremenek8dc671c2009-03-03 01:35:36 +000027class VISIBILITY_HIDDEN BasicStoreSubRegionMap : public SubRegionMap {
28public:
29 BasicStoreSubRegionMap() {}
30
Ted Kremenek4c8a5812009-03-03 02:51:43 +000031 bool iterSubRegions(const MemRegion* R, Visitor& V) const {
Ted Kremenek89f47812009-03-05 16:41:21 +000032 return true; // Do nothing. No subregions.
Ted Kremenek8dc671c2009-03-03 01:35:36 +000033 }
34};
Mike Stump11289f42009-09-09 15:08:12 +000035
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +000036class VISIBILITY_HIDDEN BasicStoreManager : public StoreManager {
Ted Kremenek1fe63ac2009-03-05 16:32:59 +000037 BindingsTy::Factory VBFactory;
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +000038public:
Ted Kremenek43015262009-07-29 21:43:22 +000039 BasicStoreManager(GRStateManager& mgr)
Ted Kremenek608677a2009-08-21 23:25:54 +000040 : StoreManager(mgr), VBFactory(mgr.getAllocator()) {}
Mike Stump11289f42009-09-09 15:08:12 +000041
Ted Kremenekf3be44f2008-10-24 20:32:16 +000042 ~BasicStoreManager() {}
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +000043
Ted Kremenek609df302009-06-17 22:02:04 +000044 SubRegionMap *getSubRegionMap(const GRState *state) {
Ted Kremenek9f276d62009-03-03 19:02:42 +000045 return new BasicStoreSubRegionMap();
Ted Kremenek8dc671c2009-03-03 01:35:36 +000046 }
47
Ted Kremenekac7c7242009-07-21 21:03:30 +000048 SValuator::CastResult Retrieve(const GRState *state, Loc loc,
Ted Kremenekbca70672009-07-29 18:16:25 +000049 QualType T = QualType());
Mike Stump11289f42009-09-09 15:08:12 +000050
Ted Kremenekbca70672009-07-29 18:16:25 +000051 const GRState *InvalidateRegion(const GRState *state, const MemRegion *R,
Ted Kremenek1eb68092009-10-16 00:30:49 +000052 const Expr *E, unsigned Count,
53 InvalidatedSymbols *IS);
Zhongxing Xuaf7415f2008-12-20 06:32:12 +000054
Ted Kremenek609df302009-06-17 22:02:04 +000055 const GRState *Bind(const GRState *state, Loc L, SVal V) {
56 return state->makeWithStore(BindInternal(state->getStore(), L, V));
Zhongxing Xuaf7415f2008-12-20 06:32:12 +000057 }
58
Ted Kremenek608677a2009-08-21 23:25:54 +000059 Store scanForIvars(Stmt *B, const Decl* SelfDecl,
60 const MemRegion *SelfRegion, Store St);
Mike Stump11289f42009-09-09 15:08:12 +000061
62 Store BindInternal(Store St, Loc loc, SVal V);
Ted Kremenekf929b0a2009-01-07 22:56:17 +000063 Store Remove(Store St, Loc loc);
Zhongxing Xu5f078cb2009-08-17 06:19:58 +000064 Store getInitialStore(const LocationContext *InitLoc);
Zhongxing Xuf5e7c902008-10-07 01:31:04 +000065
Zhongxing Xu232c7922008-10-16 06:09:51 +000066 // FIXME: Investigate what is using this. This method should be removed.
Ted Kremenek14536f62009-08-21 22:28:32 +000067 virtual Loc getLoc(const VarDecl* VD, const LocationContext *LC) {
68 return ValMgr.makeLoc(MRMgr.getVarRegion(VD, LC));
Zhongxing Xuf5e7c902008-10-07 01:31:04 +000069 }
Mike Stump11289f42009-09-09 15:08:12 +000070
Ted Kremenek609df302009-06-17 22:02:04 +000071 const GRState *BindCompoundLiteral(const GRState *state,
72 const CompoundLiteralExpr* cl,
73 SVal val) {
74 return state;
Ted Kremenekbf263682008-10-27 21:54:31 +000075 }
Mike Stump11289f42009-09-09 15:08:12 +000076
Zhongxing Xu7d6387b2009-10-14 03:33:08 +000077 SVal getLValueVar(const VarDecl *VD, const LocationContext *LC);
78 SVal getLValueString(const StringLiteral *S);
79 SVal getLValueCompoundLiteral(const CompoundLiteralExpr *CL);
80 SVal getLValueIvar(const ObjCIvarDecl* D, SVal Base);
81 SVal getLValueField(const FieldDecl *D, SVal Base);
82 SVal getLValueElement(QualType elementType, SVal Offset, SVal Base);
Zhongxing Xub0a48752008-10-23 03:10:39 +000083
Ted Kremenekf3be44f2008-10-24 20:32:16 +000084 /// ArrayToPointer - Used by GRExprEngine::VistCast to handle implicit
85 /// conversions between arrays and pointers.
Zhongxing Xua865b792009-03-30 05:55:46 +000086 SVal ArrayToPointer(Loc Array) { return Array; }
Mike Stump11289f42009-09-09 15:08:12 +000087
Ted Kremenekd368de72008-12-05 00:47:52 +000088 /// RemoveDeadBindings - Scans a BasicStore of 'state' for dead values.
Ted Kremenekcee28a42009-08-02 04:45:08 +000089 /// It updatees the GRState object in place with the values removed.
90 void RemoveDeadBindings(GRState &state, Stmt* Loc, SymbolReaper& SymReaper,
91 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
Zhongxing Xud95495f2008-08-21 22:34:01 +000092
Ted Kremenekf3be44f2008-10-24 20:32:16 +000093 void iterBindings(Store store, BindingsHandler& f);
Ted Kremenek1b9e1032008-09-03 03:06:11 +000094
Ted Kremenekb006b822009-11-04 00:09:15 +000095 const GRState *BindDecl(const GRState *state, const VarRegion *VR,
96 SVal InitVal) {
97 return state->makeWithStore(BindDeclInternal(state->getStore(), VR,
Ted Kremenek14536f62009-08-21 22:28:32 +000098 &InitVal));
Zhongxing Xuaf7415f2008-12-20 06:32:12 +000099 }
100
Ted Kremenekb006b822009-11-04 00:09:15 +0000101 const GRState *BindDeclWithNoInit(const GRState *state, const VarRegion *VR) {
102 return state->makeWithStore(BindDeclInternal(state->getStore(), VR, 0));
Zhongxing Xuaf7415f2008-12-20 06:32:12 +0000103 }
104
Ted Kremenekb006b822009-11-04 00:09:15 +0000105 Store BindDeclInternal(Store store, const VarRegion *VR, SVal *InitVal);
Zhongxing Xud95495f2008-08-21 22:34:01 +0000106
Ted Kremenek1fe63ac2009-03-05 16:32:59 +0000107 static inline BindingsTy GetBindings(Store store) {
108 return BindingsTy(static_cast<const BindingsTy::TreeTy*>(store));
Ted Kremenek19edd212008-08-19 22:24:03 +0000109 }
110
Ted Kremenek799bb6e2009-06-24 23:06:47 +0000111 void print(Store store, llvm::raw_ostream& Out, const char* nl,
112 const char *sep);
Zhongxing Xuec7e7df2009-04-03 07:33:13 +0000113
114private:
115 ASTContext& getContext() { return StateMgr.getContext(); }
Ted Kremenek1b9e1032008-09-03 03:06:11 +0000116};
Mike Stump11289f42009-09-09 15:08:12 +0000117
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +0000118} // end anonymous namespace
119
120
Ted Kremeneke91874f72008-08-28 23:31:31 +0000121StoreManager* clang::CreateBasicStoreManager(GRStateManager& StMgr) {
122 return new BasicStoreManager(StMgr);
Ted Kremenek2a2c8752008-08-25 19:33:03 +0000123}
Zhongxing Xudab76fd2008-10-21 06:54:23 +0000124
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000125SVal BasicStoreManager::getLValueVar(const VarDecl* VD,
Ted Kremenek14536f62009-08-21 22:28:32 +0000126 const LocationContext *LC) {
127 return ValMgr.makeLoc(MRMgr.getVarRegion(VD, LC));
Ted Kremenek3ad391d2008-10-17 00:51:01 +0000128}
Zhongxing Xu0d2706f2008-10-25 14:18:57 +0000129
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000130SVal BasicStoreManager::getLValueString(const StringLiteral* S) {
Zhongxing Xu7718ae42009-06-23 09:02:15 +0000131 return ValMgr.makeLoc(MRMgr.getStringRegion(S));
Zhongxing Xu0d2706f2008-10-25 14:18:57 +0000132}
Zhongxing Xu2c677c32008-11-07 10:38:33 +0000133
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000134SVal BasicStoreManager::getLValueCompoundLiteral(const CompoundLiteralExpr* CL){
Zhongxing Xu7718ae42009-06-23 09:02:15 +0000135 return ValMgr.makeLoc(MRMgr.getCompoundLiteralRegion(CL));
Zhongxing Xu2c677c32008-11-07 10:38:33 +0000136}
137
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000138SVal BasicStoreManager::getLValueIvar(const ObjCIvarDecl* D, SVal Base) {
Mike Stump11289f42009-09-09 15:08:12 +0000139
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000140 if (Base.isUnknownOrUndef())
141 return Base;
142
143 Loc BaseL = cast<Loc>(Base);
144
145 if (isa<loc::MemRegionVal>(BaseL)) {
146 const MemRegion *BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
Ted Kremenek608677a2009-08-21 23:25:54 +0000147 return ValMgr.makeLoc(MRMgr.getObjCIvarRegion(D, BaseR));
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000148 }
Mike Stump11289f42009-09-09 15:08:12 +0000149
Ted Kremenek3ad391d2008-10-17 00:51:01 +0000150 return UnknownVal();
151}
Ted Kremenekb5670fd2008-12-13 21:49:13 +0000152
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000153SVal BasicStoreManager::getLValueField(const FieldDecl* D, SVal Base) {
Ted Kremenek8b103c62008-10-17 20:28:54 +0000154
155 if (Base.isUnknownOrUndef())
156 return Base;
Mike Stump11289f42009-09-09 15:08:12 +0000157
158 Loc BaseL = cast<Loc>(Base);
Ted Kremenek8b103c62008-10-17 20:28:54 +0000159 const MemRegion* BaseR = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000160
Ted Kremenek8b103c62008-10-17 20:28:54 +0000161 switch(BaseL.getSubKind()) {
Ted Kremenek8b103c62008-10-17 20:28:54 +0000162 case loc::GotoLabelKind:
Ted Kremenek8b103c62008-10-17 20:28:54 +0000163 return UndefinedVal();
164
165 case loc::MemRegionKind:
166 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
167 break;
Mike Stump11289f42009-09-09 15:08:12 +0000168
Ted Kremenek8b103c62008-10-17 20:28:54 +0000169 case loc::ConcreteIntKind:
Ted Kremenek8b103c62008-10-17 20:28:54 +0000170 // While these seem funny, this can happen through casts.
171 // FIXME: What we should return is the field offset. For example,
172 // add the field offset to the integer value. That way funny things
173 // like this work properly: &(((struct foo *) 0xa)->f)
174 return Base;
175
176 default:
177 assert ("Unhandled Base.");
178 return Base;
179 }
Mike Stump11289f42009-09-09 15:08:12 +0000180
Zhongxing Xu7718ae42009-06-23 09:02:15 +0000181 return ValMgr.makeLoc(MRMgr.getFieldRegion(D, BaseR));
Ted Kremenek3ad391d2008-10-17 00:51:01 +0000182}
Ted Kremenek2a2c8752008-08-25 19:33:03 +0000183
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000184SVal BasicStoreManager::getLValueElement(QualType elementType,
185 SVal Offset, SVal Base) {
Ted Kremenek62cc9da2008-12-09 21:20:27 +0000186
Ted Kremenek62cc9da2008-12-09 21:20:27 +0000187 if (Base.isUnknownOrUndef())
188 return Base;
Mike Stump11289f42009-09-09 15:08:12 +0000189
190 Loc BaseL = cast<Loc>(Base);
Zhongxing Xu6377a982009-06-30 07:41:27 +0000191 const MemRegion* BaseR = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000192
Ted Kremenek62cc9da2008-12-09 21:20:27 +0000193 switch(BaseL.getSubKind()) {
Ted Kremenek62cc9da2008-12-09 21:20:27 +0000194 case loc::GotoLabelKind:
Ted Kremenek62cc9da2008-12-09 21:20:27 +0000195 // Technically we can get here if people do funny things with casts.
196 return UndefinedVal();
Mike Stump11289f42009-09-09 15:08:12 +0000197
Ted Kremenekf065b152008-12-13 19:24:37 +0000198 case loc::MemRegionKind: {
199 const MemRegion *R = cast<loc::MemRegionVal>(BaseL).getRegion();
Mike Stump11289f42009-09-09 15:08:12 +0000200
Ted Kremeneke5c31582009-05-05 00:06:16 +0000201 if (isa<ElementRegion>(R)) {
Zhongxing Xu6ebde272009-05-04 08:52:47 +0000202 // int x;
203 // char* y = (char*) &x;
204 // 'y' => ElementRegion(0, VarRegion('x'))
205 // y[0] = 'a';
Ted Kremenek422d81d2009-01-27 18:29:03 +0000206 return Base;
207 }
Mike Stump11289f42009-09-09 15:08:12 +0000208
Ted Kremenek8fd18792009-06-30 20:24:11 +0000209 if (isa<TypedRegion>(R) || isa<SymbolicRegion>(R)) {
210 BaseR = R;
Ted Kremenekf065b152008-12-13 19:24:37 +0000211 break;
212 }
Mike Stump11289f42009-09-09 15:08:12 +0000213
Ted Kremenek62cc9da2008-12-09 21:20:27 +0000214 break;
Ted Kremenekf065b152008-12-13 19:24:37 +0000215 }
Ted Kremenek422d81d2009-01-27 18:29:03 +0000216
Ted Kremenek62cc9da2008-12-09 21:20:27 +0000217 case loc::ConcreteIntKind:
218 // While these seem funny, this can happen through casts.
219 // FIXME: What we should return is the field offset. For example,
220 // add the field offset to the integer value. That way funny things
221 // like this work properly: &(((struct foo *) 0xa)->f)
222 return Base;
Mike Stump11289f42009-09-09 15:08:12 +0000223
Ted Kremenek62cc9da2008-12-09 21:20:27 +0000224 default:
225 assert ("Unhandled Base.");
226 return Base;
227 }
Mike Stump11289f42009-09-09 15:08:12 +0000228
229 if (BaseR) {
Zhongxing Xu7718ae42009-06-23 09:02:15 +0000230 return ValMgr.makeLoc(MRMgr.getElementRegion(elementType, UnknownVal(),
231 BaseR, getContext()));
Ted Kremenek8fd18792009-06-30 20:24:11 +0000232 }
Ted Kremenekf065b152008-12-13 19:24:37 +0000233 else
234 return UnknownVal();
Zhongxing Xu232c7922008-10-16 06:09:51 +0000235}
236
Ted Kremenek3941d222009-04-29 16:03:27 +0000237static bool isHigherOrderRawPtr(QualType T, ASTContext &C) {
Ted Kremenekdf240002009-04-11 00:11:10 +0000238 bool foundPointer = false;
Mike Stump11289f42009-09-09 15:08:12 +0000239 while (1) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000240 const PointerType *PT = T->getAs<PointerType>();
Ted Kremenekdf240002009-04-11 00:11:10 +0000241 if (!PT) {
242 if (!foundPointer)
243 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000244
Ted Kremenek3941d222009-04-29 16:03:27 +0000245 // intptr_t* or intptr_t**, etc?
246 if (T->isIntegerType() && C.getTypeSize(T) == C.getTypeSize(C.VoidPtrTy))
247 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000248
Ted Kremenekdf240002009-04-11 00:11:10 +0000249 QualType X = C.getCanonicalType(T).getUnqualifiedType();
250 return X == C.VoidTy;
251 }
Mike Stump11289f42009-09-09 15:08:12 +0000252
Ted Kremenekdf240002009-04-11 00:11:10 +0000253 foundPointer = true;
254 T = PT->getPointeeType();
Mike Stump11289f42009-09-09 15:08:12 +0000255 }
Ted Kremenekdf240002009-04-11 00:11:10 +0000256}
Mike Stump11289f42009-09-09 15:08:12 +0000257
Ted Kremenekac7c7242009-07-21 21:03:30 +0000258SValuator::CastResult BasicStoreManager::Retrieve(const GRState *state,
259 Loc loc, QualType T) {
Mike Stump11289f42009-09-09 15:08:12 +0000260
Ted Kremenekf929b0a2009-01-07 22:56:17 +0000261 if (isa<UnknownVal>(loc))
Ted Kremenekac7c7242009-07-21 21:03:30 +0000262 return SValuator::CastResult(state, UnknownVal());
Mike Stump11289f42009-09-09 15:08:12 +0000263
Ted Kremenek3ed95432009-08-25 20:51:30 +0000264 assert(!isa<UndefinedVal>(loc));
Mike Stump11289f42009-09-09 15:08:12 +0000265
Ted Kremenekf929b0a2009-01-07 22:56:17 +0000266 switch (loc.getSubKind()) {
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +0000267
Zhongxing Xu27f17422008-10-17 05:57:07 +0000268 case loc::MemRegionKind: {
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000269 const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion();
Mike Stump11289f42009-09-09 15:08:12 +0000270
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000271 if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
Ted Kremenekac7c7242009-07-21 21:03:30 +0000272 return SValuator::CastResult(state, UnknownVal());
Mike Stump11289f42009-09-09 15:08:12 +0000273
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000274 BindingsTy B = GetBindings(state->getStore());
Ted Kremenek3ed95432009-08-25 20:51:30 +0000275 BindingsTy::data_type *Val = B.lookup(R);
Mike Stump11289f42009-09-09 15:08:12 +0000276
Ted Kremenek3ed95432009-08-25 20:51:30 +0000277 if (!Val)
278 break;
Mike Stump11289f42009-09-09 15:08:12 +0000279
Zhongxing Xu731f4622009-11-16 04:49:44 +0000280 return SValuator::CastResult(state,
281 CastRetrievedVal(*Val, cast<TypedRegion>(R), T));
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +0000282 }
Mike Stump11289f42009-09-09 15:08:12 +0000283
Zhongxing Xu27f17422008-10-17 05:57:07 +0000284 case loc::ConcreteIntKind:
285 // Some clients may call GetSVal with such an option simply because
286 // they are doing a quick scan through their Locs (potentially to
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +0000287 // invalidate their bindings). Just return Undefined.
Ted Kremenekac7c7242009-07-21 21:03:30 +0000288 return SValuator::CastResult(state, UndefinedVal());
Mike Stump11289f42009-09-09 15:08:12 +0000289
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +0000290 default:
Zhongxing Xu27f17422008-10-17 05:57:07 +0000291 assert (false && "Invalid Loc.");
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +0000292 break;
293 }
Mike Stump11289f42009-09-09 15:08:12 +0000294
Ted Kremenekac7c7242009-07-21 21:03:30 +0000295 return SValuator::CastResult(state, UnknownVal());
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +0000296}
Mike Stump11289f42009-09-09 15:08:12 +0000297
298Store BasicStoreManager::BindInternal(Store store, Loc loc, SVal V) {
Zhongxing Xud260db12009-06-28 10:16:11 +0000299 if (isa<loc::ConcreteInt>(loc))
300 return store;
301
Zhongxing Xuf7a6de32009-06-28 09:26:15 +0000302 const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion();
303 ASTContext &C = StateMgr.getContext();
Mike Stump11289f42009-09-09 15:08:12 +0000304
Zhongxing Xuf7a6de32009-06-28 09:26:15 +0000305 // Special case: handle store of pointer values (Loc) to pointers via
306 // a cast to intXX_t*, void*, etc. This is needed to handle
307 // OSCompareAndSwap32Barrier/OSCompareAndSwap64Barrier.
308 if (isa<Loc>(V) || isa<nonloc::LocAsInteger>(V))
309 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
310 // FIXME: Should check for index 0.
311 QualType T = ER->getLocationType(C);
Mike Stump11289f42009-09-09 15:08:12 +0000312
Zhongxing Xuf7a6de32009-06-28 09:26:15 +0000313 if (isHigherOrderRawPtr(T, C))
314 R = ER->getSuperRegion();
Mike Stump11289f42009-09-09 15:08:12 +0000315 }
316
Zhongxing Xuf7a6de32009-06-28 09:26:15 +0000317 if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
318 return store;
Ted Kremenek608677a2009-08-21 23:25:54 +0000319
Ted Kremenekc4c48be2009-08-25 23:29:04 +0000320 const TypedRegion *TyR = cast<TypedRegion>(R);
Mike Stump11289f42009-09-09 15:08:12 +0000321
Ted Kremenekc4c48be2009-08-25 23:29:04 +0000322 // Do not bind to arrays. We need to explicitly check for this so that
323 // we do not encounter any weirdness of trying to load/store from arrays.
324 if (TyR->isBoundable() && TyR->getValueType(C)->isArrayType())
Mike Stump11289f42009-09-09 15:08:12 +0000325 return store;
Ted Kremenekc4c48be2009-08-25 23:29:04 +0000326
Zhongxing Xuf7a6de32009-06-28 09:26:15 +0000327 if (nonloc::LocAsInteger *X = dyn_cast<nonloc::LocAsInteger>(&V)) {
328 // Only convert 'V' to a location iff the underlying region type
329 // is a location as well.
330 // FIXME: We are allowing a store of an arbitrary location to
331 // a pointer. We may wish to flag a type error here if the types
332 // are incompatible. This may also cause lots of breakage
333 // elsewhere. Food for thought.
Mike Stump11289f42009-09-09 15:08:12 +0000334 if (TyR->isBoundable() && Loc::IsLocType(TyR->getValueType(C)))
Ted Kremenekc4c48be2009-08-25 23:29:04 +0000335 V = X->getLoc();
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +0000336 }
Zhongxing Xuf7a6de32009-06-28 09:26:15 +0000337
338 BindingsTy B = GetBindings(store);
339 return V.isUnknown()
340 ? VBFactory.Remove(B, R).getRoot()
341 : VBFactory.Add(B, R, V).getRoot();
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +0000342}
343
Ted Kremenekf929b0a2009-01-07 22:56:17 +0000344Store BasicStoreManager::Remove(Store store, Loc loc) {
345 switch (loc.getSubKind()) {
Zhongxing Xu27f17422008-10-17 05:57:07 +0000346 case loc::MemRegionKind: {
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000347 const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion();
Mike Stump11289f42009-09-09 15:08:12 +0000348
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000349 if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
Ted Kremenek5ca90a22008-10-04 05:50:14 +0000350 return store;
Mike Stump11289f42009-09-09 15:08:12 +0000351
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000352 return VBFactory.Remove(GetBindings(store), R).getRoot();
Ted Kremenek88a6b7f2008-07-17 18:38:48 +0000353 }
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +0000354 default:
Zhongxing Xu27f17422008-10-17 05:57:07 +0000355 assert ("Remove for given Loc type not yet implemented.");
Ted Kremenek88a6b7f2008-07-17 18:38:48 +0000356 return store;
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +0000357 }
358}
Ted Kremenek88a6b7f2008-07-17 18:38:48 +0000359
Ted Kremenekcee28a42009-08-02 04:45:08 +0000360void
361BasicStoreManager::RemoveDeadBindings(GRState &state, Stmt* Loc,
Ted Kremenek16fbfe62009-01-21 22:26:05 +0000362 SymbolReaper& SymReaper,
Ted Kremenekcee28a42009-08-02 04:45:08 +0000363 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
Mike Stump11289f42009-09-09 15:08:12 +0000364{
Ted Kremenekcee28a42009-08-02 04:45:08 +0000365 Store store = state.getStore();
Ted Kremenek1fe63ac2009-03-05 16:32:59 +0000366 BindingsTy B = GetBindings(store);
Zhongxing Xu27f17422008-10-17 05:57:07 +0000367 typedef SVal::symbol_iterator symbol_iterator;
Mike Stump11289f42009-09-09 15:08:12 +0000368
Ted Kremenek88a6b7f2008-07-17 18:38:48 +0000369 // Iterate over the variable bindings.
Ted Kremenek1fe63ac2009-03-05 16:32:59 +0000370 for (BindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) {
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000371 if (const VarRegion *VR = dyn_cast<VarRegion>(I.getKey())) {
372 if (SymReaper.isLive(Loc, VR->getDecl()))
373 RegionRoots.push_back(VR);
374 else
375 continue;
Ted Kremenek88a6b7f2008-07-17 18:38:48 +0000376 }
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000377 else if (isa<ObjCIvarRegion>(I.getKey())) {
378 RegionRoots.push_back(I.getKey());
379 }
380 else
381 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000382
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000383 // Mark the bindings in the data as live.
384 SVal X = I.getData();
385 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
386 SymReaper.markLive(*SI);
Ted Kremenek2f340d6c2009-03-05 16:31:07 +0000387 }
Mike Stump11289f42009-09-09 15:08:12 +0000388
Ted Kremenek88a6b7f2008-07-17 18:38:48 +0000389 // Scan for live variables and live symbols.
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000390 llvm::SmallPtrSet<const MemRegion*, 10> Marked;
Mike Stump11289f42009-09-09 15:08:12 +0000391
Ted Kremenek5ca90a22008-10-04 05:50:14 +0000392 while (!RegionRoots.empty()) {
Ted Kremenekdb5ae0a2008-10-17 22:52:40 +0000393 const MemRegion* MR = RegionRoots.back();
Ted Kremenek5ca90a22008-10-04 05:50:14 +0000394 RegionRoots.pop_back();
Mike Stump11289f42009-09-09 15:08:12 +0000395
Ted Kremenekdb5ae0a2008-10-17 22:52:40 +0000396 while (MR) {
397 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(MR)) {
Ted Kremenek16fbfe62009-01-21 22:26:05 +0000398 SymReaper.markLive(SymR->getSymbol());
Ted Kremenekdb5ae0a2008-10-17 22:52:40 +0000399 break;
400 }
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000401 else if (isa<VarRegion>(MR) || isa<ObjCIvarRegion>(MR)) {
402 if (Marked.count(MR))
Ted Kremenekdb5ae0a2008-10-17 22:52:40 +0000403 break;
Mike Stump11289f42009-09-09 15:08:12 +0000404
405 Marked.insert(MR);
Ted Kremenekcee28a42009-08-02 04:45:08 +0000406 SVal X = Retrieve(&state, loc::MemRegionVal(MR)).getSVal();
Mike Stump11289f42009-09-09 15:08:12 +0000407
Ted Kremenekdb5ae0a2008-10-17 22:52:40 +0000408 // FIXME: We need to handle symbols nested in region definitions.
Ted Kremenek16fbfe62009-01-21 22:26:05 +0000409 for (symbol_iterator SI=X.symbol_begin(),SE=X.symbol_end();SI!=SE;++SI)
410 SymReaper.markLive(*SI);
Mike Stump11289f42009-09-09 15:08:12 +0000411
Ted Kremenekdb5ae0a2008-10-17 22:52:40 +0000412 if (!isa<loc::MemRegionVal>(X))
413 break;
Mike Stump11289f42009-09-09 15:08:12 +0000414
Ted Kremenekdb5ae0a2008-10-17 22:52:40 +0000415 const loc::MemRegionVal& LVD = cast<loc::MemRegionVal>(X);
416 RegionRoots.push_back(LVD.getRegion());
417 break;
418 }
419 else if (const SubRegion* R = dyn_cast<SubRegion>(MR))
420 MR = R->getSuperRegion();
421 else
422 break;
423 }
Ted Kremenek88a6b7f2008-07-17 18:38:48 +0000424 }
Mike Stump11289f42009-09-09 15:08:12 +0000425
426 // Remove dead variable bindings.
Ted Kremenek1fe63ac2009-03-05 16:32:59 +0000427 for (BindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) {
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000428 const MemRegion* R = I.getKey();
Mike Stump11289f42009-09-09 15:08:12 +0000429
Ted Kremenek5ca90a22008-10-04 05:50:14 +0000430 if (!Marked.count(R)) {
Zhongxing Xu7718ae42009-06-23 09:02:15 +0000431 store = Remove(store, ValMgr.makeLoc(R));
Zhongxing Xu27f17422008-10-17 05:57:07 +0000432 SVal X = I.getData();
Mike Stump11289f42009-09-09 15:08:12 +0000433
Ted Kremenek88a6b7f2008-07-17 18:38:48 +0000434 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
Ted Kremenek16fbfe62009-01-21 22:26:05 +0000435 SymReaper.maybeDead(*SI);
Ted Kremenek88a6b7f2008-07-17 18:38:48 +0000436 }
Ted Kremenek5ca90a22008-10-04 05:50:14 +0000437 }
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000438
Ted Kremenekcee28a42009-08-02 04:45:08 +0000439 // Write the store back.
440 state.setStore(store);
Ted Kremenek88a6b7f2008-07-17 18:38:48 +0000441}
Ted Kremenek67102b22008-08-19 16:51:45 +0000442
Ted Kremenek608677a2009-08-21 23:25:54 +0000443Store BasicStoreManager::scanForIvars(Stmt *B, const Decl* SelfDecl,
444 const MemRegion *SelfRegion, Store St) {
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000445 for (Stmt::child_iterator CI=B->child_begin(), CE=B->child_end();
446 CI != CE; ++CI) {
Mike Stump11289f42009-09-09 15:08:12 +0000447
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000448 if (!*CI)
449 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000450
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000451 // Check if the statement is an ivar reference. We only
452 // care about self.ivar.
453 if (ObjCIvarRefExpr *IV = dyn_cast<ObjCIvarRefExpr>(*CI)) {
454 const Expr *Base = IV->getBase()->IgnoreParenCasts();
455 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Base)) {
456 if (DR->getDecl() == SelfDecl) {
457 const MemRegion *IVR = MRMgr.getObjCIvarRegion(IV->getDecl(),
Mike Stump11289f42009-09-09 15:08:12 +0000458 SelfRegion);
459 SVal X = ValMgr.getRegionValueSymbolVal(IVR);
Zhongxing Xu7718ae42009-06-23 09:02:15 +0000460 St = BindInternal(St, ValMgr.makeLoc(IVR), X);
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000461 }
462 }
463 }
464 else
Ted Kremenek608677a2009-08-21 23:25:54 +0000465 St = scanForIvars(*CI, SelfDecl, SelfRegion, St);
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000466 }
Mike Stump11289f42009-09-09 15:08:12 +0000467
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000468 return St;
469}
470
Mike Stump11289f42009-09-09 15:08:12 +0000471Store BasicStoreManager::getInitialStore(const LocationContext *InitLoc) {
Ted Kremenek67102b22008-08-19 16:51:45 +0000472 // The LiveVariables information already has a compilation of all VarDecls
473 // used in the function. Iterate through this set, and "symbolicate"
474 // any VarDecl whose value originally comes from outside the function.
Ted Kremenek67102b22008-08-19 16:51:45 +0000475 typedef LiveVariables::AnalysisDataTy LVDataTy;
Zhongxing Xu5f078cb2009-08-17 06:19:58 +0000476 LVDataTy& D = InitLoc->getLiveVariables()->getAnalysisData();
Ted Kremenek67102b22008-08-19 16:51:45 +0000477 Store St = VBFactory.GetEmptyMap().getRoot();
478
479 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
Douglas Gregor5251f1b2008-10-21 16:13:35 +0000480 NamedDecl* ND = const_cast<NamedDecl*>(I->first);
Ted Kremenek67102b22008-08-19 16:51:45 +0000481
Ted Kremenekf3be44f2008-10-24 20:32:16 +0000482 // Handle implicit parameters.
483 if (ImplicitParamDecl* PD = dyn_cast<ImplicitParamDecl>(ND)) {
Mike Stump11289f42009-09-09 15:08:12 +0000484 const Decl& CD = *InitLoc->getDecl();
Ted Kremenekf3be44f2008-10-24 20:32:16 +0000485 if (const ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(&CD)) {
486 if (MD->getSelfDecl() == PD) {
Ted Kremenek608677a2009-08-21 23:25:54 +0000487 // FIXME: Just use a symbolic region, and remove ObjCObjectRegion
488 // entirely.
489 const ObjCObjectRegion *SelfRegion =
490 MRMgr.getObjCObjectRegion(MD->getClassInterface(),
491 MRMgr.getHeapRegion());
Mike Stump11289f42009-09-09 15:08:12 +0000492
Ted Kremenek14536f62009-08-21 22:28:32 +0000493 St = BindInternal(St, ValMgr.makeLoc(MRMgr.getVarRegion(PD, InitLoc)),
Zhongxing Xu7718ae42009-06-23 09:02:15 +0000494 ValMgr.makeLoc(SelfRegion));
Mike Stump11289f42009-09-09 15:08:12 +0000495
Ted Kremenekfa3d77b2009-03-05 18:08:28 +0000496 // Scan the method for ivar references. While this requires an
497 // entire AST scan, the cost should not be high in practice.
Ted Kremenek608677a2009-08-21 23:25:54 +0000498 St = scanForIvars(MD->getBody(), PD, SelfRegion, St);
Ted Kremenekf3be44f2008-10-24 20:32:16 +0000499 }
500 }
501 }
502 else if (VarDecl* VD = dyn_cast<VarDecl>(ND)) {
Ted Kremenekc7fef2a2009-03-23 15:42:58 +0000503 // Only handle simple types that we can symbolicate.
504 if (!SymbolManager::canSymbolicate(VD->getType()))
505 continue;
Ted Kremenek67102b22008-08-19 16:51:45 +0000506
Ted Kremenekb36e01d2009-03-18 22:10:22 +0000507 // Initialize globals and parameters to symbolic values.
508 // Initialize local variables to undefined.
Ted Kremenek14536f62009-08-21 22:28:32 +0000509 const MemRegion *R = ValMgr.getRegionManager().getVarRegion(VD, InitLoc);
Ted Kremenek7020eae2009-09-11 22:07:28 +0000510 SVal X = UndefinedVal();
511 if (R->hasGlobalsOrParametersStorage())
512 X = ValMgr.getRegionValueSymbolVal(R);
Ted Kremenek67102b22008-08-19 16:51:45 +0000513
Zhongxing Xu7718ae42009-06-23 09:02:15 +0000514 St = BindInternal(St, ValMgr.makeLoc(R), X);
Ted Kremenek67102b22008-08-19 16:51:45 +0000515 }
516 }
517 return St;
518}
Ted Kremenek19edd212008-08-19 22:24:03 +0000519
Ted Kremenekb006b822009-11-04 00:09:15 +0000520Store BasicStoreManager::BindDeclInternal(Store store, const VarRegion* VR,
Zhongxing Xuaf7415f2008-12-20 06:32:12 +0000521 SVal* InitVal) {
Mike Stump11289f42009-09-09 15:08:12 +0000522
Ted Kremenek4e7713c2008-08-23 00:50:55 +0000523 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
Ted Kremenekb006b822009-11-04 00:09:15 +0000524 const VarDecl *VD = VR->getDecl();
Mike Stump11289f42009-09-09 15:08:12 +0000525
Zhongxing Xud95495f2008-08-21 22:34:01 +0000526 // BasicStore does not model arrays and structs.
527 if (VD->getType()->isArrayType() || VD->getType()->isStructureType())
528 return store;
529
530 if (VD->hasGlobalStorage()) {
531 // Handle variables with global storage: extern, static, PrivateExtern.
532
533 // FIXME:: static variables may have an initializer, but the second time a
534 // function is called those values may not be current. Currently, a function
535 // will not be called more than once.
536
537 // Static global variables should not be visited here.
538 assert(!(VD->getStorageClass() == VarDecl::Static &&
539 VD->isFileVarDecl()));
Mike Stump11289f42009-09-09 15:08:12 +0000540
Zhongxing Xud95495f2008-08-21 22:34:01 +0000541 // Process static variables.
542 if (VD->getStorageClass() == VarDecl::Static) {
543 // C99: 6.7.8 Initialization
544 // If an object that has static storage duration is not initialized
Mike Stump11289f42009-09-09 15:08:12 +0000545 // explicitly, then:
546 // —if it has pointer type, it is initialized to a null pointer;
547 // —if it has arithmetic type, it is initialized to (positive or
Zhongxing Xud95495f2008-08-21 22:34:01 +0000548 // unsigned) zero;
Ted Kremenekcd639212008-11-12 19:18:35 +0000549 if (!InitVal) {
Zhongxing Xud95495f2008-08-21 22:34:01 +0000550 QualType T = VD->getType();
Zhongxing Xu27f17422008-10-17 05:57:07 +0000551 if (Loc::IsLocType(T))
Ted Kremenekb006b822009-11-04 00:09:15 +0000552 store = BindInternal(store, loc::MemRegionVal(VR),
Zhongxing Xu8f6855e642008-10-21 06:27:32 +0000553 loc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xud95495f2008-08-21 22:34:01 +0000554 else if (T->isIntegerType())
Ted Kremenekb006b822009-11-04 00:09:15 +0000555 store = BindInternal(store, loc::MemRegionVal(VR),
Zhongxing Xu8f6855e642008-10-21 06:27:32 +0000556 nonloc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xud95495f2008-08-21 22:34:01 +0000557 else {
558 // assert(0 && "ignore other types of variables");
559 }
560 } else {
Ted Kremenekb006b822009-11-04 00:09:15 +0000561 store = BindInternal(store, loc::MemRegionVal(VR), *InitVal);
Zhongxing Xud95495f2008-08-21 22:34:01 +0000562 }
563 }
564 } else {
565 // Process local scalar variables.
566 QualType T = VD->getType();
Ted Kremenek24c85132009-07-03 00:36:16 +0000567 if (ValMgr.getSymbolManager().canSymbolicate(T)) {
Ted Kremenekcd639212008-11-12 19:18:35 +0000568 SVal V = InitVal ? *InitVal : UndefinedVal();
Ted Kremenekb006b822009-11-04 00:09:15 +0000569 store = BindInternal(store, loc::MemRegionVal(VR), V);
Zhongxing Xud95495f2008-08-21 22:34:01 +0000570 }
571 }
572
573 return store;
574}
575
Ted Kremenek799bb6e2009-06-24 23:06:47 +0000576void BasicStoreManager::print(Store store, llvm::raw_ostream& Out,
Ted Kremenek19edd212008-08-19 22:24:03 +0000577 const char* nl, const char *sep) {
Mike Stump11289f42009-09-09 15:08:12 +0000578
Ted Kremenek1fe63ac2009-03-05 16:32:59 +0000579 BindingsTy B = GetBindings(store);
Ted Kremenek19edd212008-08-19 22:24:03 +0000580 Out << "Variables:" << nl;
Mike Stump11289f42009-09-09 15:08:12 +0000581
Ted Kremenek19edd212008-08-19 22:24:03 +0000582 bool isFirst = true;
Mike Stump11289f42009-09-09 15:08:12 +0000583
Ted Kremenek1fe63ac2009-03-05 16:32:59 +0000584 for (BindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I) {
Ted Kremenek799bb6e2009-06-24 23:06:47 +0000585 if (isFirst)
586 isFirst = false;
587 else
588 Out << nl;
Mike Stump11289f42009-09-09 15:08:12 +0000589
Ted Kremeneka6904ff2009-07-13 23:53:06 +0000590 Out << ' ' << I.getKey() << " : " << I.getData();
Ted Kremenek19edd212008-08-19 22:24:03 +0000591 }
592}
Ted Kremenekc83e7552008-08-29 00:47:32 +0000593
Ted Kremenek1b9e1032008-09-03 03:06:11 +0000594
595void BasicStoreManager::iterBindings(Store store, BindingsHandler& f) {
Ted Kremenek1fe63ac2009-03-05 16:32:59 +0000596 BindingsTy B = GetBindings(store);
Mike Stump11289f42009-09-09 15:08:12 +0000597
Ted Kremenek1fe63ac2009-03-05 16:32:59 +0000598 for (BindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I)
Ted Kremenek2f340d6c2009-03-05 16:31:07 +0000599 f.HandleBinding(*this, store, I.getKey(), I.getData());
Ted Kremenek5ca90a22008-10-04 05:50:14 +0000600
Ted Kremenekc83e7552008-08-29 00:47:32 +0000601}
602
Ted Kremenek1b9e1032008-09-03 03:06:11 +0000603StoreManager::BindingsHandler::~BindingsHandler() {}
Ted Kremenekbca70672009-07-29 18:16:25 +0000604
605//===----------------------------------------------------------------------===//
606// Binding invalidation.
607//===----------------------------------------------------------------------===//
608
609const GRState *BasicStoreManager::InvalidateRegion(const GRState *state,
610 const MemRegion *R,
611 const Expr *E,
Ted Kremenek1eb68092009-10-16 00:30:49 +0000612 unsigned Count,
613 InvalidatedSymbols *IS) {
Zhongxing Xuf8f3f9d2009-11-10 02:17:20 +0000614 R = R->StripCasts();
Mike Stump11289f42009-09-09 15:08:12 +0000615
Ted Kremenekbca70672009-07-29 18:16:25 +0000616 if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
617 return state;
Mike Stump11289f42009-09-09 15:08:12 +0000618
Ted Kremenek1eb68092009-10-16 00:30:49 +0000619 if (IS) {
620 BindingsTy B = GetBindings(state->getStore());
621 if (BindingsTy::data_type *Val = B.lookup(R)) {
622 if (SymbolRef Sym = Val->getAsSymbol())
623 IS->insert(Sym);
624 }
625 }
626
Ted Kremenekbca70672009-07-29 18:16:25 +0000627 QualType T = cast<TypedRegion>(R)->getValueType(R->getContext());
Ted Kremeneke41b81e2009-09-27 20:45:21 +0000628 SVal V = ValMgr.getConjuredSymbolVal(R, E, T, Count);
Ted Kremenekbca70672009-07-29 18:16:25 +0000629 return Bind(state, loc::MemRegionVal(R), V);
630}
631