blob: 50b90f5813a7ab80f4b5ddabd381e995a0e55669 [file] [log] [blame]
Ted Kremenekf22f8682008-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 Kremenek8d194592009-03-05 18:08:28 +000014#include "clang/AST/ExprObjC.h"
Ted Kremenek6e913672008-08-28 23:31:31 +000015#include "clang/Analysis/Analyses/LiveVariables.h"
Zhongxing Xu43c60b82009-08-17 06:19:58 +000016#include "clang/Analysis/PathSensitive/AnalysisContext.h"
Ted Kremeneke2fb8c72008-08-19 16:51:45 +000017#include "clang/Analysis/PathSensitive/GRState.h"
Ted Kremenekf22f8682008-07-10 22:03:41 +000018#include "llvm/ADT/ImmutableMap.h"
19#include "llvm/Support/Compiler.h"
Ted Kremenekbdff9a92008-08-19 22:24:03 +000020#include "llvm/Support/Streams.h"
Ted Kremenekf22f8682008-07-10 22:03:41 +000021
22using namespace clang;
23
Ted Kremenek246a0472009-03-05 16:32:59 +000024typedef llvm::ImmutableMap<const MemRegion*,SVal> BindingsTy;
Ted Kremenek58c29602008-09-03 03:06:11 +000025
Ted Kremenekf22f8682008-07-10 22:03:41 +000026namespace {
27
Ted Kremenek6f718472009-03-03 01:35:36 +000028class VISIBILITY_HIDDEN BasicStoreSubRegionMap : public SubRegionMap {
29public:
30 BasicStoreSubRegionMap() {}
31
Ted Kremenekfe2e9fa2009-03-03 02:51:43 +000032 bool iterSubRegions(const MemRegion* R, Visitor& V) const {
Ted Kremenek505ea6a2009-03-05 16:41:21 +000033 return true; // Do nothing. No subregions.
Ted Kremenek6f718472009-03-03 01:35:36 +000034 }
35};
36
Ted Kremenekf22f8682008-07-10 22:03:41 +000037class VISIBILITY_HIDDEN BasicStoreManager : public StoreManager {
Ted Kremenek246a0472009-03-05 16:32:59 +000038 BindingsTy::Factory VBFactory;
Ted Kremenekf22f8682008-07-10 22:03:41 +000039public:
Ted Kremenekd492ebc2009-07-29 21:43:22 +000040 BasicStoreManager(GRStateManager& mgr)
Ted Kremenek22e45e82009-08-21 23:25:54 +000041 : StoreManager(mgr), VBFactory(mgr.getAllocator()) {}
Ted Kremenekf5fce102008-08-25 19:33:03 +000042
Ted Kremenek73a36c92008-10-24 20:32:16 +000043 ~BasicStoreManager() {}
Ted Kremenekf22f8682008-07-10 22:03:41 +000044
Ted Kremenek5c9b4542009-06-17 22:02:04 +000045 SubRegionMap *getSubRegionMap(const GRState *state) {
Ted Kremenekb004b1d2009-03-03 19:02:42 +000046 return new BasicStoreSubRegionMap();
Ted Kremenek6f718472009-03-03 01:35:36 +000047 }
48
Ted Kremenek6cd31072009-07-21 21:03:30 +000049 SValuator::CastResult Retrieve(const GRState *state, Loc loc,
Ted Kremenekfa498ae2009-07-29 18:16:25 +000050 QualType T = QualType());
51
52 const GRState *InvalidateRegion(const GRState *state, const MemRegion *R,
53 const Expr *E, unsigned Count);
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +000054
Ted Kremenek5c9b4542009-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 Xu5ea4ad02008-12-20 06:32:12 +000057 }
58
Ted Kremenek22e45e82009-08-21 23:25:54 +000059 Store scanForIvars(Stmt *B, const Decl* SelfDecl,
60 const MemRegion *SelfRegion, Store St);
Ted Kremenek8d194592009-03-05 18:08:28 +000061
Ted Kremenekb1d9eb32009-01-07 22:56:17 +000062 Store BindInternal(Store St, Loc loc, SVal V);
63 Store Remove(Store St, Loc loc);
Zhongxing Xu43c60b82009-08-17 06:19:58 +000064 Store getInitialStore(const LocationContext *InitLoc);
Zhongxing Xu2a59edd2008-10-07 01:31:04 +000065
Zhongxing Xu44e00b02008-10-16 06:09:51 +000066 // FIXME: Investigate what is using this. This method should be removed.
Ted Kremenek6555ff92009-08-21 22:28:32 +000067 virtual Loc getLoc(const VarDecl* VD, const LocationContext *LC) {
68 return ValMgr.makeLoc(MRMgr.getVarRegion(VD, LC));
Zhongxing Xu2a59edd2008-10-07 01:31:04 +000069 }
Ted Kremenek6eaf0e32008-10-17 00:51:01 +000070
Ted Kremenek5c9b4542009-06-17 22:02:04 +000071 const GRState *BindCompoundLiteral(const GRState *state,
72 const CompoundLiteralExpr* cl,
73 SVal val) {
74 return state;
Ted Kremenekd83daa52008-10-27 21:54:31 +000075 }
76
Ted Kremenek6555ff92009-08-21 22:28:32 +000077 SVal getLValueVar(const GRState *state, const VarDecl *VD,
78 const LocationContext *LC);
79 SVal getLValueString(const GRState *state, const StringLiteral *S);
Ted Kremenek5c9b4542009-06-17 22:02:04 +000080 SVal getLValueCompoundLiteral(const GRState *state,
Ted Kremenek6555ff92009-08-21 22:28:32 +000081 const CompoundLiteralExpr *CL);
Ted Kremenek5c9b4542009-06-17 22:02:04 +000082 SVal getLValueIvar(const GRState *state, const ObjCIvarDecl* D, SVal Base);
Ted Kremenek6555ff92009-08-21 22:28:32 +000083 SVal getLValueField(const GRState *state, SVal Base, const FieldDecl *D);
Ted Kremenek5c9b4542009-06-17 22:02:04 +000084 SVal getLValueElement(const GRState *state, QualType elementType,
Ted Kremenekaf81ece2009-05-04 06:18:28 +000085 SVal Base, SVal Offset);
Zhongxing Xua9e8e082008-10-23 03:10:39 +000086
Ted Kremenek73a36c92008-10-24 20:32:16 +000087 /// ArrayToPointer - Used by GRExprEngine::VistCast to handle implicit
88 /// conversions between arrays and pointers.
Zhongxing Xu9ddfd192009-03-30 05:55:46 +000089 SVal ArrayToPointer(Loc Array) { return Array; }
Ted Kremenek73a36c92008-10-24 20:32:16 +000090
Ted Kremenek0de2e0f2008-12-05 00:47:52 +000091 /// RemoveDeadBindings - Scans a BasicStore of 'state' for dead values.
Ted Kremeneke5c07b82009-08-02 04:45:08 +000092 /// It updatees the GRState object in place with the values removed.
93 void RemoveDeadBindings(GRState &state, Stmt* Loc, SymbolReaper& SymReaper,
94 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
Zhongxing Xu2378ba02008-08-21 22:34:01 +000095
Ted Kremenek73a36c92008-10-24 20:32:16 +000096 void iterBindings(Store store, BindingsHandler& f);
Ted Kremenek58c29602008-09-03 03:06:11 +000097
Ted Kremenek6555ff92009-08-21 22:28:32 +000098 const GRState *BindDecl(const GRState *state, const VarDecl *VD,
99 const LocationContext *LC, SVal InitVal) {
100 return state->makeWithStore(BindDeclInternal(state->getStore(),VD, LC,
101 &InitVal));
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000102 }
103
Ted Kremenek6555ff92009-08-21 22:28:32 +0000104 const GRState *BindDeclWithNoInit(const GRState *state, const VarDecl *VD,
105 const LocationContext *LC) {
106 return state->makeWithStore(BindDeclInternal(state->getStore(), VD, LC, 0));
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000107 }
108
Ted Kremenek6555ff92009-08-21 22:28:32 +0000109 Store BindDeclInternal(Store store, const VarDecl *VD,
110 const LocationContext *LC, SVal *InitVal);
Zhongxing Xu2378ba02008-08-21 22:34:01 +0000111
Ted Kremenek246a0472009-03-05 16:32:59 +0000112 static inline BindingsTy GetBindings(Store store) {
113 return BindingsTy(static_cast<const BindingsTy::TreeTy*>(store));
Ted Kremenekbdff9a92008-08-19 22:24:03 +0000114 }
115
Ted Kremenekdd04ed62009-06-24 23:06:47 +0000116 void print(Store store, llvm::raw_ostream& Out, const char* nl,
117 const char *sep);
Zhongxing Xu1f48e432009-04-03 07:33:13 +0000118
119private:
120 ASTContext& getContext() { return StateMgr.getContext(); }
Ted Kremenek58c29602008-09-03 03:06:11 +0000121};
Ted Kremenekb15eba42008-10-04 05:50:14 +0000122
Ted Kremenekf22f8682008-07-10 22:03:41 +0000123} // end anonymous namespace
124
125
Ted Kremenek6e913672008-08-28 23:31:31 +0000126StoreManager* clang::CreateBasicStoreManager(GRStateManager& StMgr) {
127 return new BasicStoreManager(StMgr);
Ted Kremenekf5fce102008-08-25 19:33:03 +0000128}
Zhongxing Xu3739b0b2008-10-21 06:54:23 +0000129
Ted Kremenek6555ff92009-08-21 22:28:32 +0000130SVal BasicStoreManager::getLValueVar(const GRState *state, const VarDecl* VD,
131 const LocationContext *LC) {
132 return ValMgr.makeLoc(MRMgr.getVarRegion(VD, LC));
Ted Kremenek6eaf0e32008-10-17 00:51:01 +0000133}
Zhongxing Xu2abba442008-10-25 14:18:57 +0000134
Ted Kremenek5c9b4542009-06-17 22:02:04 +0000135SVal BasicStoreManager::getLValueString(const GRState *state,
Zhongxing Xu2abba442008-10-25 14:18:57 +0000136 const StringLiteral* S) {
Zhongxing Xue32c7652009-06-23 09:02:15 +0000137 return ValMgr.makeLoc(MRMgr.getStringRegion(S));
Zhongxing Xu2abba442008-10-25 14:18:57 +0000138}
Zhongxing Xuc88ca9d2008-11-07 10:38:33 +0000139
Ted Kremenek5c9b4542009-06-17 22:02:04 +0000140SVal BasicStoreManager::getLValueCompoundLiteral(const GRState *state,
Zhongxing Xuc88ca9d2008-11-07 10:38:33 +0000141 const CompoundLiteralExpr* CL){
Zhongxing Xue32c7652009-06-23 09:02:15 +0000142 return ValMgr.makeLoc(MRMgr.getCompoundLiteralRegion(CL));
Zhongxing Xuc88ca9d2008-11-07 10:38:33 +0000143}
144
Ted Kremenek22e45e82009-08-21 23:25:54 +0000145SVal BasicStoreManager::getLValueIvar(const GRState *state,
146 const ObjCIvarDecl* D,
Zhongxing Xu097fc982008-10-17 05:57:07 +0000147 SVal Base) {
Ted Kremenek8d194592009-03-05 18:08:28 +0000148
149 if (Base.isUnknownOrUndef())
150 return Base;
151
152 Loc BaseL = cast<Loc>(Base);
153
154 if (isa<loc::MemRegionVal>(BaseL)) {
155 const MemRegion *BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
Ted Kremenek22e45e82009-08-21 23:25:54 +0000156 return ValMgr.makeLoc(MRMgr.getObjCIvarRegion(D, BaseR));
Ted Kremenek8d194592009-03-05 18:08:28 +0000157 }
158
Ted Kremenek6eaf0e32008-10-17 00:51:01 +0000159 return UnknownVal();
160}
Ted Kremenekf5da3252008-12-13 21:49:13 +0000161
Ted Kremenek5c9b4542009-06-17 22:02:04 +0000162SVal BasicStoreManager::getLValueField(const GRState *state, SVal Base,
Zhongxing Xu60dabc62008-10-22 09:00:19 +0000163 const FieldDecl* D) {
Ted Kremenek38a4b4b2008-10-17 20:28:54 +0000164
165 if (Base.isUnknownOrUndef())
166 return Base;
167
168 Loc BaseL = cast<Loc>(Base);
169 const MemRegion* BaseR = 0;
170
171 switch(BaseL.getSubKind()) {
Ted Kremenek38a4b4b2008-10-17 20:28:54 +0000172 case loc::GotoLabelKind:
Ted Kremenek38a4b4b2008-10-17 20:28:54 +0000173 return UndefinedVal();
174
175 case loc::MemRegionKind:
176 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
177 break;
178
179 case loc::ConcreteIntKind:
Ted Kremenek38a4b4b2008-10-17 20:28:54 +0000180 // While these seem funny, this can happen through casts.
181 // FIXME: What we should return is the field offset. For example,
182 // add the field offset to the integer value. That way funny things
183 // like this work properly: &(((struct foo *) 0xa)->f)
184 return Base;
185
186 default:
187 assert ("Unhandled Base.");
188 return Base;
189 }
190
Zhongxing Xue32c7652009-06-23 09:02:15 +0000191 return ValMgr.makeLoc(MRMgr.getFieldRegion(D, BaseR));
Ted Kremenek6eaf0e32008-10-17 00:51:01 +0000192}
Ted Kremenekf5fce102008-08-25 19:33:03 +0000193
Ted Kremenek5c9b4542009-06-17 22:02:04 +0000194SVal BasicStoreManager::getLValueElement(const GRState *state,
Ted Kremenekaf81ece2009-05-04 06:18:28 +0000195 QualType elementType,
196 SVal Base, SVal Offset) {
Ted Kremenek371dd112008-12-09 21:20:27 +0000197
Ted Kremenek371dd112008-12-09 21:20:27 +0000198 if (Base.isUnknownOrUndef())
199 return Base;
200
201 Loc BaseL = cast<Loc>(Base);
Zhongxing Xu3658bf72009-06-30 07:41:27 +0000202 const MemRegion* BaseR = 0;
Ted Kremenek371dd112008-12-09 21:20:27 +0000203
204 switch(BaseL.getSubKind()) {
Ted Kremenek371dd112008-12-09 21:20:27 +0000205 case loc::GotoLabelKind:
Ted Kremenek371dd112008-12-09 21:20:27 +0000206 // Technically we can get here if people do funny things with casts.
207 return UndefinedVal();
208
Ted Kremenek2c0de352008-12-13 19:24:37 +0000209 case loc::MemRegionKind: {
210 const MemRegion *R = cast<loc::MemRegionVal>(BaseL).getRegion();
Ted Kremenek622f8df2009-01-27 18:29:03 +0000211
Ted Kremenekabbdd082009-05-05 00:06:16 +0000212 if (isa<ElementRegion>(R)) {
Zhongxing Xu398d8f82009-05-04 08:52:47 +0000213 // int x;
214 // char* y = (char*) &x;
215 // 'y' => ElementRegion(0, VarRegion('x'))
216 // y[0] = 'a';
Ted Kremenek622f8df2009-01-27 18:29:03 +0000217 return Base;
218 }
219
Ted Kremenekbb021592009-06-30 20:24:11 +0000220 if (isa<TypedRegion>(R) || isa<SymbolicRegion>(R)) {
221 BaseR = R;
Ted Kremenek2c0de352008-12-13 19:24:37 +0000222 break;
223 }
224
Ted Kremenek371dd112008-12-09 21:20:27 +0000225 break;
Ted Kremenek2c0de352008-12-13 19:24:37 +0000226 }
Ted Kremenek622f8df2009-01-27 18:29:03 +0000227
Ted Kremenek371dd112008-12-09 21:20:27 +0000228 case loc::ConcreteIntKind:
229 // While these seem funny, this can happen through casts.
230 // FIXME: What we should return is the field offset. For example,
231 // add the field offset to the integer value. That way funny things
232 // like this work properly: &(((struct foo *) 0xa)->f)
233 return Base;
234
235 default:
236 assert ("Unhandled Base.");
237 return Base;
238 }
239
Ted Kremenekbb021592009-06-30 20:24:11 +0000240 if (BaseR) {
Zhongxing Xue32c7652009-06-23 09:02:15 +0000241 return ValMgr.makeLoc(MRMgr.getElementRegion(elementType, UnknownVal(),
242 BaseR, getContext()));
Ted Kremenekbb021592009-06-30 20:24:11 +0000243 }
Ted Kremenek2c0de352008-12-13 19:24:37 +0000244 else
245 return UnknownVal();
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000246}
247
Ted Kremenek4e76b982009-04-29 16:03:27 +0000248static bool isHigherOrderRawPtr(QualType T, ASTContext &C) {
Ted Kremenek8765ebc2009-04-11 00:11:10 +0000249 bool foundPointer = false;
250 while (1) {
Ted Kremenekd00cd9e2009-07-29 21:53:49 +0000251 const PointerType *PT = T->getAs<PointerType>();
Ted Kremenek8765ebc2009-04-11 00:11:10 +0000252 if (!PT) {
253 if (!foundPointer)
254 return false;
255
Ted Kremenek4e76b982009-04-29 16:03:27 +0000256 // intptr_t* or intptr_t**, etc?
257 if (T->isIntegerType() && C.getTypeSize(T) == C.getTypeSize(C.VoidPtrTy))
258 return true;
259
Ted Kremenek8765ebc2009-04-11 00:11:10 +0000260 QualType X = C.getCanonicalType(T).getUnqualifiedType();
261 return X == C.VoidTy;
262 }
263
264 foundPointer = true;
265 T = PT->getPointeeType();
266 }
267}
Ted Kremenek4e76b982009-04-29 16:03:27 +0000268
Ted Kremenek6cd31072009-07-21 21:03:30 +0000269SValuator::CastResult BasicStoreManager::Retrieve(const GRState *state,
270 Loc loc, QualType T) {
Ted Kremenekf22f8682008-07-10 22:03:41 +0000271
Ted Kremenekb1d9eb32009-01-07 22:56:17 +0000272 if (isa<UnknownVal>(loc))
Ted Kremenek6cd31072009-07-21 21:03:30 +0000273 return SValuator::CastResult(state, UnknownVal());
Ted Kremenekf22f8682008-07-10 22:03:41 +0000274
Ted Kremenekb1d9eb32009-01-07 22:56:17 +0000275 assert (!isa<UndefinedVal>(loc));
Ted Kremenekf22f8682008-07-10 22:03:41 +0000276
Ted Kremenekb1d9eb32009-01-07 22:56:17 +0000277 switch (loc.getSubKind()) {
Ted Kremenekf22f8682008-07-10 22:03:41 +0000278
Zhongxing Xu097fc982008-10-17 05:57:07 +0000279 case loc::MemRegionKind: {
Ted Kremenek8d194592009-03-05 18:08:28 +0000280 const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion();
Ted Kremenekb15eba42008-10-04 05:50:14 +0000281
Ted Kremenek440f2ce2009-05-04 07:04:36 +0000282 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
Ted Kremenek4e76b982009-04-29 16:03:27 +0000283 // Just support void**, void***, intptr_t*, intptr_t**, etc., for now.
284 // This is needed to handle OSCompareAndSwapPtr() and friends.
Ted Kremenek8765ebc2009-04-11 00:11:10 +0000285 ASTContext &Ctx = StateMgr.getContext();
Zhongxing Xub02c8522009-05-09 00:50:33 +0000286 QualType T = ER->getLocationType(Ctx);
Ted Kremenek8765ebc2009-04-11 00:11:10 +0000287
Ted Kremenek4e76b982009-04-29 16:03:27 +0000288 if (!isHigherOrderRawPtr(T, Ctx))
Ted Kremenek6cd31072009-07-21 21:03:30 +0000289 return SValuator::CastResult(state, UnknownVal());
Ted Kremenek8765ebc2009-04-11 00:11:10 +0000290
Ted Kremenek440f2ce2009-05-04 07:04:36 +0000291 // FIXME: Should check for element 0.
292 // Otherwise, strip the element region.
293 R = ER->getSuperRegion();
Ted Kremenek8765ebc2009-04-11 00:11:10 +0000294 }
295
Ted Kremenek8d194592009-03-05 18:08:28 +0000296 if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
Ted Kremenek6cd31072009-07-21 21:03:30 +0000297 return SValuator::CastResult(state, UnknownVal());
Ted Kremenek0de2e0f2008-12-05 00:47:52 +0000298
Ted Kremenek8d194592009-03-05 18:08:28 +0000299 BindingsTy B = GetBindings(state->getStore());
Ted Kremenek246a0472009-03-05 16:32:59 +0000300 BindingsTy::data_type* T = B.lookup(R);
Ted Kremenek6cd31072009-07-21 21:03:30 +0000301 return SValuator::CastResult(state, T ? *T : UnknownVal());
Ted Kremenekf22f8682008-07-10 22:03:41 +0000302 }
303
Zhongxing Xu097fc982008-10-17 05:57:07 +0000304 case loc::ConcreteIntKind:
305 // Some clients may call GetSVal with such an option simply because
306 // they are doing a quick scan through their Locs (potentially to
Ted Kremenekf22f8682008-07-10 22:03:41 +0000307 // invalidate their bindings). Just return Undefined.
Ted Kremenek6cd31072009-07-21 21:03:30 +0000308 return SValuator::CastResult(state, UndefinedVal());
Ted Kremenekf22f8682008-07-10 22:03:41 +0000309
Ted Kremenekf22f8682008-07-10 22:03:41 +0000310 default:
Zhongxing Xu097fc982008-10-17 05:57:07 +0000311 assert (false && "Invalid Loc.");
Ted Kremenekf22f8682008-07-10 22:03:41 +0000312 break;
313 }
314
Ted Kremenek6cd31072009-07-21 21:03:30 +0000315 return SValuator::CastResult(state, UnknownVal());
Ted Kremenekf22f8682008-07-10 22:03:41 +0000316}
Ted Kremeneke7b0b272008-10-17 00:03:18 +0000317
Ted Kremenekb1d9eb32009-01-07 22:56:17 +0000318Store BasicStoreManager::BindInternal(Store store, Loc loc, SVal V) {
Zhongxing Xu646547c2009-06-28 10:16:11 +0000319 if (isa<loc::ConcreteInt>(loc))
320 return store;
321
Zhongxing Xue361a9a2009-06-28 09:26:15 +0000322 const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion();
323 ASTContext &C = StateMgr.getContext();
Ted Kremenekb15eba42008-10-04 05:50:14 +0000324
Zhongxing Xue361a9a2009-06-28 09:26:15 +0000325 // Special case: handle store of pointer values (Loc) to pointers via
326 // a cast to intXX_t*, void*, etc. This is needed to handle
327 // OSCompareAndSwap32Barrier/OSCompareAndSwap64Barrier.
328 if (isa<Loc>(V) || isa<nonloc::LocAsInteger>(V))
329 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
330 // FIXME: Should check for index 0.
331 QualType T = ER->getLocationType(C);
Ted Kremenek4e76b982009-04-29 16:03:27 +0000332
Zhongxing Xue361a9a2009-06-28 09:26:15 +0000333 if (isHigherOrderRawPtr(T, C))
334 R = ER->getSuperRegion();
335 }
Ted Kremenek4e76b982009-04-29 16:03:27 +0000336
Zhongxing Xue361a9a2009-06-28 09:26:15 +0000337 if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
338 return store;
Ted Kremenek22e45e82009-08-21 23:25:54 +0000339
Zhongxing Xue361a9a2009-06-28 09:26:15 +0000340 if (nonloc::LocAsInteger *X = dyn_cast<nonloc::LocAsInteger>(&V)) {
341 // Only convert 'V' to a location iff the underlying region type
342 // is a location as well.
343 // FIXME: We are allowing a store of an arbitrary location to
344 // a pointer. We may wish to flag a type error here if the types
345 // are incompatible. This may also cause lots of breakage
346 // elsewhere. Food for thought.
347 if (const TypedRegion *TyR = dyn_cast<TypedRegion>(R)) {
348 if (TyR->isBoundable() &&
349 Loc::IsLocType(TyR->getValueType(C)))
350 V = X->getLoc();
351 }
Ted Kremenekf22f8682008-07-10 22:03:41 +0000352 }
Zhongxing Xue361a9a2009-06-28 09:26:15 +0000353
354 BindingsTy B = GetBindings(store);
355 return V.isUnknown()
356 ? VBFactory.Remove(B, R).getRoot()
357 : VBFactory.Add(B, R, V).getRoot();
Ted Kremenekf22f8682008-07-10 22:03:41 +0000358}
359
Ted Kremenekb1d9eb32009-01-07 22:56:17 +0000360Store BasicStoreManager::Remove(Store store, Loc loc) {
361 switch (loc.getSubKind()) {
Zhongxing Xu097fc982008-10-17 05:57:07 +0000362 case loc::MemRegionKind: {
Ted Kremenek8d194592009-03-05 18:08:28 +0000363 const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion();
Ted Kremenekb15eba42008-10-04 05:50:14 +0000364
Ted Kremenek8d194592009-03-05 18:08:28 +0000365 if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
Ted Kremenekb15eba42008-10-04 05:50:14 +0000366 return store;
367
Ted Kremenek8d194592009-03-05 18:08:28 +0000368 return VBFactory.Remove(GetBindings(store), R).getRoot();
Ted Kremenekee930122008-07-17 18:38:48 +0000369 }
Ted Kremenekf22f8682008-07-10 22:03:41 +0000370 default:
Zhongxing Xu097fc982008-10-17 05:57:07 +0000371 assert ("Remove for given Loc type not yet implemented.");
Ted Kremenekee930122008-07-17 18:38:48 +0000372 return store;
Ted Kremenekf22f8682008-07-10 22:03:41 +0000373 }
374}
Ted Kremenekee930122008-07-17 18:38:48 +0000375
Ted Kremeneke5c07b82009-08-02 04:45:08 +0000376void
377BasicStoreManager::RemoveDeadBindings(GRState &state, Stmt* Loc,
Ted Kremenek5c0729b2009-01-21 22:26:05 +0000378 SymbolReaper& SymReaper,
Ted Kremeneke5c07b82009-08-02 04:45:08 +0000379 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
380{
381 Store store = state.getStore();
Ted Kremenek246a0472009-03-05 16:32:59 +0000382 BindingsTy B = GetBindings(store);
Zhongxing Xu097fc982008-10-17 05:57:07 +0000383 typedef SVal::symbol_iterator symbol_iterator;
Ted Kremenekee930122008-07-17 18:38:48 +0000384
385 // Iterate over the variable bindings.
Ted Kremenek246a0472009-03-05 16:32:59 +0000386 for (BindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) {
Ted Kremenek8d194592009-03-05 18:08:28 +0000387 if (const VarRegion *VR = dyn_cast<VarRegion>(I.getKey())) {
388 if (SymReaper.isLive(Loc, VR->getDecl()))
389 RegionRoots.push_back(VR);
390 else
391 continue;
Ted Kremenekee930122008-07-17 18:38:48 +0000392 }
Ted Kremenek8d194592009-03-05 18:08:28 +0000393 else if (isa<ObjCIvarRegion>(I.getKey())) {
394 RegionRoots.push_back(I.getKey());
395 }
396 else
397 continue;
398
399 // Mark the bindings in the data as live.
400 SVal X = I.getData();
401 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
402 SymReaper.markLive(*SI);
Ted Kremenekb6b0bb82009-03-05 16:31:07 +0000403 }
Ted Kremenekee930122008-07-17 18:38:48 +0000404
405 // Scan for live variables and live symbols.
Ted Kremenek8d194592009-03-05 18:08:28 +0000406 llvm::SmallPtrSet<const MemRegion*, 10> Marked;
Ted Kremenekee930122008-07-17 18:38:48 +0000407
Ted Kremenekb15eba42008-10-04 05:50:14 +0000408 while (!RegionRoots.empty()) {
Ted Kremeneka0f833d2008-10-17 22:52:40 +0000409 const MemRegion* MR = RegionRoots.back();
Ted Kremenekb15eba42008-10-04 05:50:14 +0000410 RegionRoots.pop_back();
Ted Kremenekee930122008-07-17 18:38:48 +0000411
Ted Kremeneka0f833d2008-10-17 22:52:40 +0000412 while (MR) {
413 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(MR)) {
Ted Kremenek5c0729b2009-01-21 22:26:05 +0000414 SymReaper.markLive(SymR->getSymbol());
Ted Kremeneka0f833d2008-10-17 22:52:40 +0000415 break;
416 }
Ted Kremenek8d194592009-03-05 18:08:28 +0000417 else if (isa<VarRegion>(MR) || isa<ObjCIvarRegion>(MR)) {
418 if (Marked.count(MR))
Ted Kremeneka0f833d2008-10-17 22:52:40 +0000419 break;
420
Ted Kremenek6cd31072009-07-21 21:03:30 +0000421 Marked.insert(MR);
Ted Kremeneke5c07b82009-08-02 04:45:08 +0000422 SVal X = Retrieve(&state, loc::MemRegionVal(MR)).getSVal();
Ted Kremenekee930122008-07-17 18:38:48 +0000423
Ted Kremeneka0f833d2008-10-17 22:52:40 +0000424 // FIXME: We need to handle symbols nested in region definitions.
Ted Kremenek5c0729b2009-01-21 22:26:05 +0000425 for (symbol_iterator SI=X.symbol_begin(),SE=X.symbol_end();SI!=SE;++SI)
426 SymReaper.markLive(*SI);
Ted Kremenekee930122008-07-17 18:38:48 +0000427
Ted Kremeneka0f833d2008-10-17 22:52:40 +0000428 if (!isa<loc::MemRegionVal>(X))
429 break;
Ted Kremenekee930122008-07-17 18:38:48 +0000430
Ted Kremeneka0f833d2008-10-17 22:52:40 +0000431 const loc::MemRegionVal& LVD = cast<loc::MemRegionVal>(X);
432 RegionRoots.push_back(LVD.getRegion());
433 break;
434 }
435 else if (const SubRegion* R = dyn_cast<SubRegion>(MR))
436 MR = R->getSuperRegion();
437 else
438 break;
439 }
Ted Kremenekee930122008-07-17 18:38:48 +0000440 }
441
442 // Remove dead variable bindings.
Ted Kremenek246a0472009-03-05 16:32:59 +0000443 for (BindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) {
Ted Kremenek8d194592009-03-05 18:08:28 +0000444 const MemRegion* R = I.getKey();
Ted Kremenekb15eba42008-10-04 05:50:14 +0000445
446 if (!Marked.count(R)) {
Zhongxing Xue32c7652009-06-23 09:02:15 +0000447 store = Remove(store, ValMgr.makeLoc(R));
Zhongxing Xu097fc982008-10-17 05:57:07 +0000448 SVal X = I.getData();
Ted Kremenekee930122008-07-17 18:38:48 +0000449
450 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
Ted Kremenek5c0729b2009-01-21 22:26:05 +0000451 SymReaper.maybeDead(*SI);
Ted Kremenekee930122008-07-17 18:38:48 +0000452 }
Ted Kremenekb15eba42008-10-04 05:50:14 +0000453 }
Ted Kremenek8d194592009-03-05 18:08:28 +0000454
Ted Kremeneke5c07b82009-08-02 04:45:08 +0000455 // Write the store back.
456 state.setStore(store);
Ted Kremenekee930122008-07-17 18:38:48 +0000457}
Ted Kremeneke2fb8c72008-08-19 16:51:45 +0000458
Ted Kremenek22e45e82009-08-21 23:25:54 +0000459Store BasicStoreManager::scanForIvars(Stmt *B, const Decl* SelfDecl,
460 const MemRegion *SelfRegion, Store St) {
Ted Kremenek8d194592009-03-05 18:08:28 +0000461 for (Stmt::child_iterator CI=B->child_begin(), CE=B->child_end();
462 CI != CE; ++CI) {
463
464 if (!*CI)
465 continue;
466
467 // Check if the statement is an ivar reference. We only
468 // care about self.ivar.
469 if (ObjCIvarRefExpr *IV = dyn_cast<ObjCIvarRefExpr>(*CI)) {
470 const Expr *Base = IV->getBase()->IgnoreParenCasts();
471 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Base)) {
472 if (DR->getDecl() == SelfDecl) {
473 const MemRegion *IVR = MRMgr.getObjCIvarRegion(IV->getDecl(),
Ted Kremeneke4cb3c82009-04-09 22:22:44 +0000474 SelfRegion);
Zhongxing Xu93810042009-05-09 04:08:27 +0000475 SVal X = ValMgr.getRegionValueSymbolVal(IVR);
Zhongxing Xue32c7652009-06-23 09:02:15 +0000476 St = BindInternal(St, ValMgr.makeLoc(IVR), X);
Ted Kremenek8d194592009-03-05 18:08:28 +0000477 }
478 }
479 }
480 else
Ted Kremenek22e45e82009-08-21 23:25:54 +0000481 St = scanForIvars(*CI, SelfDecl, SelfRegion, St);
Ted Kremenek8d194592009-03-05 18:08:28 +0000482 }
Ted Kremenek73a36c92008-10-24 20:32:16 +0000483
Ted Kremenek8d194592009-03-05 18:08:28 +0000484 return St;
485}
486
Zhongxing Xu43c60b82009-08-17 06:19:58 +0000487Store BasicStoreManager::getInitialStore(const LocationContext *InitLoc) {
Ted Kremeneke2fb8c72008-08-19 16:51:45 +0000488 // The LiveVariables information already has a compilation of all VarDecls
489 // used in the function. Iterate through this set, and "symbolicate"
490 // any VarDecl whose value originally comes from outside the function.
Ted Kremeneke2fb8c72008-08-19 16:51:45 +0000491 typedef LiveVariables::AnalysisDataTy LVDataTy;
Zhongxing Xu43c60b82009-08-17 06:19:58 +0000492 LVDataTy& D = InitLoc->getLiveVariables()->getAnalysisData();
Ted Kremeneke2fb8c72008-08-19 16:51:45 +0000493 Store St = VBFactory.GetEmptyMap().getRoot();
494
495 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
Douglas Gregord2baafd2008-10-21 16:13:35 +0000496 NamedDecl* ND = const_cast<NamedDecl*>(I->first);
Ted Kremeneke2fb8c72008-08-19 16:51:45 +0000497
Ted Kremenek73a36c92008-10-24 20:32:16 +0000498 // Handle implicit parameters.
499 if (ImplicitParamDecl* PD = dyn_cast<ImplicitParamDecl>(ND)) {
Zhongxing Xu98b18ea2009-08-21 02:58:11 +0000500 const Decl& CD = *InitLoc->getDecl();
Ted Kremenek73a36c92008-10-24 20:32:16 +0000501 if (const ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(&CD)) {
502 if (MD->getSelfDecl() == PD) {
Ted Kremenek22e45e82009-08-21 23:25:54 +0000503 // FIXME: Just use a symbolic region, and remove ObjCObjectRegion
504 // entirely.
505 const ObjCObjectRegion *SelfRegion =
506 MRMgr.getObjCObjectRegion(MD->getClassInterface(),
507 MRMgr.getHeapRegion());
Ted Kremenek73a36c92008-10-24 20:32:16 +0000508
Ted Kremenek6555ff92009-08-21 22:28:32 +0000509 St = BindInternal(St, ValMgr.makeLoc(MRMgr.getVarRegion(PD, InitLoc)),
Zhongxing Xue32c7652009-06-23 09:02:15 +0000510 ValMgr.makeLoc(SelfRegion));
Ted Kremenek8d194592009-03-05 18:08:28 +0000511
512 // Scan the method for ivar references. While this requires an
513 // entire AST scan, the cost should not be high in practice.
Ted Kremenek22e45e82009-08-21 23:25:54 +0000514 St = scanForIvars(MD->getBody(), PD, SelfRegion, St);
Ted Kremenek73a36c92008-10-24 20:32:16 +0000515 }
516 }
517 }
518 else if (VarDecl* VD = dyn_cast<VarDecl>(ND)) {
Ted Kremenekbf090d32009-03-23 15:42:58 +0000519 // Only handle simple types that we can symbolicate.
520 if (!SymbolManager::canSymbolicate(VD->getType()))
521 continue;
Ted Kremeneke2fb8c72008-08-19 16:51:45 +0000522
Ted Kremenek3bd1e482009-03-18 22:10:22 +0000523 // Initialize globals and parameters to symbolic values.
524 // Initialize local variables to undefined.
Ted Kremenek6555ff92009-08-21 22:28:32 +0000525 const MemRegion *R = ValMgr.getRegionManager().getVarRegion(VD, InitLoc);
Ted Kremenek8f1e3732009-07-02 18:25:09 +0000526 SVal X = R->hasGlobalsOrParametersStorage()
527 ? ValMgr.getRegionValueSymbolVal(R)
528 : UndefinedVal();
Ted Kremeneke2fb8c72008-08-19 16:51:45 +0000529
Zhongxing Xue32c7652009-06-23 09:02:15 +0000530 St = BindInternal(St, ValMgr.makeLoc(R), X);
Ted Kremeneke2fb8c72008-08-19 16:51:45 +0000531 }
532 }
533 return St;
534}
Ted Kremenekbdff9a92008-08-19 22:24:03 +0000535
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000536Store BasicStoreManager::BindDeclInternal(Store store, const VarDecl* VD,
Ted Kremenek6555ff92009-08-21 22:28:32 +0000537 const LocationContext *LC,
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000538 SVal* InitVal) {
Ted Kremenek37b78a12008-11-12 19:18:35 +0000539
Ted Kremenek4fbc6412008-08-23 00:50:55 +0000540 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
Ted Kremenek37b78a12008-11-12 19:18:35 +0000541
Zhongxing Xu2378ba02008-08-21 22:34:01 +0000542 // BasicStore does not model arrays and structs.
543 if (VD->getType()->isArrayType() || VD->getType()->isStructureType())
544 return store;
545
546 if (VD->hasGlobalStorage()) {
547 // Handle variables with global storage: extern, static, PrivateExtern.
548
549 // FIXME:: static variables may have an initializer, but the second time a
550 // function is called those values may not be current. Currently, a function
551 // will not be called more than once.
552
553 // Static global variables should not be visited here.
554 assert(!(VD->getStorageClass() == VarDecl::Static &&
555 VD->isFileVarDecl()));
556
557 // Process static variables.
558 if (VD->getStorageClass() == VarDecl::Static) {
559 // C99: 6.7.8 Initialization
560 // If an object that has static storage duration is not initialized
561 // explicitly, then:
562 // —if it has pointer type, it is initialized to a null pointer;
563 // —if it has arithmetic type, it is initialized to (positive or
564 // unsigned) zero;
Ted Kremenek37b78a12008-11-12 19:18:35 +0000565 if (!InitVal) {
Zhongxing Xu2378ba02008-08-21 22:34:01 +0000566 QualType T = VD->getType();
Zhongxing Xu097fc982008-10-17 05:57:07 +0000567 if (Loc::IsLocType(T))
Ted Kremenek6555ff92009-08-21 22:28:32 +0000568 store = BindInternal(store, getLoc(VD, LC),
Zhongxing Xu73249322008-10-21 06:27:32 +0000569 loc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xu2378ba02008-08-21 22:34:01 +0000570 else if (T->isIntegerType())
Ted Kremenek6555ff92009-08-21 22:28:32 +0000571 store = BindInternal(store, getLoc(VD, LC),
Zhongxing Xu73249322008-10-21 06:27:32 +0000572 nonloc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xu2378ba02008-08-21 22:34:01 +0000573 else {
574 // assert(0 && "ignore other types of variables");
575 }
576 } else {
Ted Kremenek6555ff92009-08-21 22:28:32 +0000577 store = BindInternal(store, getLoc(VD, LC), *InitVal);
Zhongxing Xu2378ba02008-08-21 22:34:01 +0000578 }
579 }
580 } else {
581 // Process local scalar variables.
582 QualType T = VD->getType();
Ted Kremenek8b685652009-07-03 00:36:16 +0000583 if (ValMgr.getSymbolManager().canSymbolicate(T)) {
Ted Kremenek37b78a12008-11-12 19:18:35 +0000584 SVal V = InitVal ? *InitVal : UndefinedVal();
Ted Kremenek6555ff92009-08-21 22:28:32 +0000585 store = BindInternal(store, getLoc(VD, LC), V);
Zhongxing Xu2378ba02008-08-21 22:34:01 +0000586 }
587 }
588
589 return store;
590}
591
Ted Kremenekdd04ed62009-06-24 23:06:47 +0000592void BasicStoreManager::print(Store store, llvm::raw_ostream& Out,
Ted Kremenekbdff9a92008-08-19 22:24:03 +0000593 const char* nl, const char *sep) {
594
Ted Kremenek246a0472009-03-05 16:32:59 +0000595 BindingsTy B = GetBindings(store);
Ted Kremenekbdff9a92008-08-19 22:24:03 +0000596 Out << "Variables:" << nl;
597
598 bool isFirst = true;
599
Ted Kremenek246a0472009-03-05 16:32:59 +0000600 for (BindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I) {
Ted Kremenekdd04ed62009-06-24 23:06:47 +0000601 if (isFirst)
602 isFirst = false;
603 else
604 Out << nl;
Ted Kremenekbdff9a92008-08-19 22:24:03 +0000605
Ted Kremenek42d8d0e2009-07-13 23:53:06 +0000606 Out << ' ' << I.getKey() << " : " << I.getData();
Ted Kremenekbdff9a92008-08-19 22:24:03 +0000607 }
608}
Ted Kremenekbe9b6f72008-08-29 00:47:32 +0000609
Ted Kremenek58c29602008-09-03 03:06:11 +0000610
611void BasicStoreManager::iterBindings(Store store, BindingsHandler& f) {
Ted Kremenek246a0472009-03-05 16:32:59 +0000612 BindingsTy B = GetBindings(store);
Ted Kremenekbe9b6f72008-08-29 00:47:32 +0000613
Ted Kremenek246a0472009-03-05 16:32:59 +0000614 for (BindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I)
Ted Kremenekb6b0bb82009-03-05 16:31:07 +0000615 f.HandleBinding(*this, store, I.getKey(), I.getData());
Ted Kremenekb15eba42008-10-04 05:50:14 +0000616
Ted Kremenekbe9b6f72008-08-29 00:47:32 +0000617}
618
Ted Kremenek58c29602008-09-03 03:06:11 +0000619StoreManager::BindingsHandler::~BindingsHandler() {}
Ted Kremenekfa498ae2009-07-29 18:16:25 +0000620
621//===----------------------------------------------------------------------===//
622// Binding invalidation.
623//===----------------------------------------------------------------------===//
624
625const GRState *BasicStoreManager::InvalidateRegion(const GRState *state,
626 const MemRegion *R,
627 const Expr *E,
628 unsigned Count) {
629 R = R->getBaseRegion();
630
631 if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
632 return state;
633
Ted Kremenekfa498ae2009-07-29 18:16:25 +0000634 QualType T = cast<TypedRegion>(R)->getValueType(R->getContext());
635 SVal V = ValMgr.getConjuredSymbolVal(E, T, Count);
636 return Bind(state, loc::MemRegionVal(R), V);
637}
638