blob: e211339658152e7be15b12e70d5b8fa4480efd6f [file] [log] [blame]
Ted Kremenek4323a572008-07-10 22:03:41 +00001//== BasicStore.cpp - Basic map from Locations to Values --------*- C++ -*--==//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defined the BasicStore and BasicStoreManager classes.
11//
12//===----------------------------------------------------------------------===//
13
Ted Kremenekf684d562009-03-05 18:08:28 +000014#include "clang/AST/ExprObjC.h"
Ted Kremenek5f81c442008-08-28 23:31:31 +000015#include "clang/Analysis/Analyses/LiveVariables.h"
Zhongxing Xu17fd8632009-08-17 06:19:58 +000016#include "clang/Analysis/PathSensitive/AnalysisContext.h"
Ted Kremenekcaa37242008-08-19 16:51:45 +000017#include "clang/Analysis/PathSensitive/GRState.h"
Ted Kremenek4323a572008-07-10 22:03:41 +000018#include "llvm/Support/Compiler.h"
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +000019#include "llvm/ADT/ImmutableMap.h"
Ted Kremenek4323a572008-07-10 22:03:41 +000020
21using namespace clang;
22
Ted Kremenek2c8e7882009-03-05 16:32:59 +000023typedef llvm::ImmutableMap<const MemRegion*,SVal> BindingsTy;
Ted Kremenek60dbad82008-09-03 03:06:11 +000024
Ted Kremenek4323a572008-07-10 22:03:41 +000025namespace {
26
Ted Kremenek59e8f112009-03-03 01:35:36 +000027class VISIBILITY_HIDDEN BasicStoreSubRegionMap : public SubRegionMap {
28public:
29 BasicStoreSubRegionMap() {}
30
Ted Kremenek5dc27462009-03-03 02:51:43 +000031 bool iterSubRegions(const MemRegion* R, Visitor& V) const {
Ted Kremenek39fc7152009-03-05 16:41:21 +000032 return true; // Do nothing. No subregions.
Ted Kremenek59e8f112009-03-03 01:35:36 +000033 }
34};
35
Ted Kremenek4323a572008-07-10 22:03:41 +000036class VISIBILITY_HIDDEN BasicStoreManager : public StoreManager {
Ted Kremenek2c8e7882009-03-05 16:32:59 +000037 BindingsTy::Factory VBFactory;
Ted Kremenek4323a572008-07-10 22:03:41 +000038public:
Ted Kremenekf7a0cf42009-07-29 21:43:22 +000039 BasicStoreManager(GRStateManager& mgr)
Ted Kremenek82cd37c2009-08-21 23:25:54 +000040 : StoreManager(mgr), VBFactory(mgr.getAllocator()) {}
Ted Kremenekd0c4b282008-08-25 19:33:03 +000041
Ted Kremenek9deb0e32008-10-24 20:32:16 +000042 ~BasicStoreManager() {}
Ted Kremenek4323a572008-07-10 22:03:41 +000043
Ted Kremenek67f28532009-06-17 22:02:04 +000044 SubRegionMap *getSubRegionMap(const GRState *state) {
Ted Kremenek14453bf2009-03-03 19:02:42 +000045 return new BasicStoreSubRegionMap();
Ted Kremenek59e8f112009-03-03 01:35:36 +000046 }
47
Ted Kremenek32c3fa42009-07-21 21:03:30 +000048 SValuator::CastResult Retrieve(const GRState *state, Loc loc,
Ted Kremenek1004a9f2009-07-29 18:16:25 +000049 QualType T = QualType());
50
51 const GRState *InvalidateRegion(const GRState *state, const MemRegion *R,
52 const Expr *E, unsigned Count);
Zhongxing Xu4193eca2008-12-20 06:32:12 +000053
Ted Kremenek67f28532009-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 Xu4193eca2008-12-20 06:32:12 +000056 }
57
Ted Kremenek82cd37c2009-08-21 23:25:54 +000058 Store scanForIvars(Stmt *B, const Decl* SelfDecl,
59 const MemRegion *SelfRegion, Store St);
Ted Kremenekf684d562009-03-05 18:08:28 +000060
Ted Kremenekc6ed3842009-01-07 22:56:17 +000061 Store BindInternal(Store St, Loc loc, SVal V);
62 Store Remove(Store St, Loc loc);
Zhongxing Xu17fd8632009-08-17 06:19:58 +000063 Store getInitialStore(const LocationContext *InitLoc);
Zhongxing Xubc678fd2008-10-07 01:31:04 +000064
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +000065 // FIXME: Investigate what is using this. This method should be removed.
Ted Kremenekd17da2b2009-08-21 22:28:32 +000066 virtual Loc getLoc(const VarDecl* VD, const LocationContext *LC) {
67 return ValMgr.makeLoc(MRMgr.getVarRegion(VD, LC));
Zhongxing Xubc678fd2008-10-07 01:31:04 +000068 }
Ted Kremenekd9bc33e2008-10-17 00:51:01 +000069
Ted Kremenek67f28532009-06-17 22:02:04 +000070 const GRState *BindCompoundLiteral(const GRState *state,
71 const CompoundLiteralExpr* cl,
72 SVal val) {
73 return state;
Ted Kremenek4f090272008-10-27 21:54:31 +000074 }
75
Ted Kremenekd17da2b2009-08-21 22:28:32 +000076 SVal getLValueVar(const GRState *state, const VarDecl *VD,
77 const LocationContext *LC);
78 SVal getLValueString(const GRState *state, const StringLiteral *S);
Ted Kremenek67f28532009-06-17 22:02:04 +000079 SVal getLValueCompoundLiteral(const GRState *state,
Ted Kremenekd17da2b2009-08-21 22:28:32 +000080 const CompoundLiteralExpr *CL);
Ted Kremenek67f28532009-06-17 22:02:04 +000081 SVal getLValueIvar(const GRState *state, const ObjCIvarDecl* D, SVal Base);
Ted Kremenekd17da2b2009-08-21 22:28:32 +000082 SVal getLValueField(const GRState *state, SVal Base, const FieldDecl *D);
Ted Kremenek67f28532009-06-17 22:02:04 +000083 SVal getLValueElement(const GRState *state, QualType elementType,
Ted Kremenekf936f452009-05-04 06:18:28 +000084 SVal Base, SVal Offset);
Zhongxing Xue1911af2008-10-23 03:10:39 +000085
Ted Kremenek9deb0e32008-10-24 20:32:16 +000086 /// ArrayToPointer - Used by GRExprEngine::VistCast to handle implicit
87 /// conversions between arrays and pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +000088 SVal ArrayToPointer(Loc Array) { return Array; }
Ted Kremenek9deb0e32008-10-24 20:32:16 +000089
Ted Kremenek2ed14be2008-12-05 00:47:52 +000090 /// RemoveDeadBindings - Scans a BasicStore of 'state' for dead values.
Ted Kremenek2f26bc32009-08-02 04:45:08 +000091 /// It updatees the GRState object in place with the values removed.
92 void RemoveDeadBindings(GRState &state, Stmt* Loc, SymbolReaper& SymReaper,
93 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +000094
Ted Kremenek9deb0e32008-10-24 20:32:16 +000095 void iterBindings(Store store, BindingsHandler& f);
Ted Kremenek60dbad82008-09-03 03:06:11 +000096
Ted Kremenekd17da2b2009-08-21 22:28:32 +000097 const GRState *BindDecl(const GRState *state, const VarDecl *VD,
98 const LocationContext *LC, SVal InitVal) {
99 return state->makeWithStore(BindDeclInternal(state->getStore(),VD, LC,
100 &InitVal));
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000101 }
102
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000103 const GRState *BindDeclWithNoInit(const GRState *state, const VarDecl *VD,
104 const LocationContext *LC) {
105 return state->makeWithStore(BindDeclInternal(state->getStore(), VD, LC, 0));
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000106 }
107
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000108 Store BindDeclInternal(Store store, const VarDecl *VD,
109 const LocationContext *LC, SVal *InitVal);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000110
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000111 static inline BindingsTy GetBindings(Store store) {
112 return BindingsTy(static_cast<const BindingsTy::TreeTy*>(store));
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000113 }
114
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000115 void print(Store store, llvm::raw_ostream& Out, const char* nl,
116 const char *sep);
Zhongxing Xua1718c72009-04-03 07:33:13 +0000117
118private:
119 ASTContext& getContext() { return StateMgr.getContext(); }
Ted Kremenek60dbad82008-09-03 03:06:11 +0000120};
Ted Kremenek9e240492008-10-04 05:50:14 +0000121
Ted Kremenek4323a572008-07-10 22:03:41 +0000122} // end anonymous namespace
123
124
Ted Kremenek5f81c442008-08-28 23:31:31 +0000125StoreManager* clang::CreateBasicStoreManager(GRStateManager& StMgr) {
126 return new BasicStoreManager(StMgr);
Ted Kremenekd0c4b282008-08-25 19:33:03 +0000127}
Zhongxing Xu933c3e12008-10-21 06:54:23 +0000128
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000129SVal BasicStoreManager::getLValueVar(const GRState *state, const VarDecl* VD,
130 const LocationContext *LC) {
131 return ValMgr.makeLoc(MRMgr.getVarRegion(VD, LC));
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000132}
Zhongxing Xu143bf822008-10-25 14:18:57 +0000133
Ted Kremenek67f28532009-06-17 22:02:04 +0000134SVal BasicStoreManager::getLValueString(const GRState *state,
Zhongxing Xu143bf822008-10-25 14:18:57 +0000135 const StringLiteral* S) {
Zhongxing Xud91ee272009-06-23 09:02:15 +0000136 return ValMgr.makeLoc(MRMgr.getStringRegion(S));
Zhongxing Xu143bf822008-10-25 14:18:57 +0000137}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000138
Ted Kremenek67f28532009-06-17 22:02:04 +0000139SVal BasicStoreManager::getLValueCompoundLiteral(const GRState *state,
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000140 const CompoundLiteralExpr* CL){
Zhongxing Xud91ee272009-06-23 09:02:15 +0000141 return ValMgr.makeLoc(MRMgr.getCompoundLiteralRegion(CL));
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000142}
143
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000144SVal BasicStoreManager::getLValueIvar(const GRState *state,
145 const ObjCIvarDecl* D,
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000146 SVal Base) {
Ted Kremenekf684d562009-03-05 18:08:28 +0000147
148 if (Base.isUnknownOrUndef())
149 return Base;
150
151 Loc BaseL = cast<Loc>(Base);
152
153 if (isa<loc::MemRegionVal>(BaseL)) {
154 const MemRegion *BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000155 return ValMgr.makeLoc(MRMgr.getObjCIvarRegion(D, BaseR));
Ted Kremenekf684d562009-03-05 18:08:28 +0000156 }
157
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000158 return UnknownVal();
159}
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000160
Ted Kremenek67f28532009-06-17 22:02:04 +0000161SVal BasicStoreManager::getLValueField(const GRState *state, SVal Base,
Zhongxing Xuc92e5fe2008-10-22 09:00:19 +0000162 const FieldDecl* D) {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000163
164 if (Base.isUnknownOrUndef())
165 return Base;
166
167 Loc BaseL = cast<Loc>(Base);
168 const MemRegion* BaseR = 0;
169
170 switch(BaseL.getSubKind()) {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000171 case loc::GotoLabelKind:
Ted Kremenek993f1c72008-10-17 20:28:54 +0000172 return UndefinedVal();
173
174 case loc::MemRegionKind:
175 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
176 break;
177
178 case loc::ConcreteIntKind:
Ted Kremenek993f1c72008-10-17 20:28:54 +0000179 // While these seem funny, this can happen through casts.
180 // FIXME: What we should return is the field offset. For example,
181 // add the field offset to the integer value. That way funny things
182 // like this work properly: &(((struct foo *) 0xa)->f)
183 return Base;
184
185 default:
186 assert ("Unhandled Base.");
187 return Base;
188 }
189
Zhongxing Xud91ee272009-06-23 09:02:15 +0000190 return ValMgr.makeLoc(MRMgr.getFieldRegion(D, BaseR));
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000191}
Ted Kremenekd0c4b282008-08-25 19:33:03 +0000192
Ted Kremenek67f28532009-06-17 22:02:04 +0000193SVal BasicStoreManager::getLValueElement(const GRState *state,
Ted Kremenekf936f452009-05-04 06:18:28 +0000194 QualType elementType,
195 SVal Base, SVal Offset) {
Ted Kremenek7d71b292008-12-09 21:20:27 +0000196
Ted Kremenek7d71b292008-12-09 21:20:27 +0000197 if (Base.isUnknownOrUndef())
198 return Base;
199
200 Loc BaseL = cast<Loc>(Base);
Zhongxing Xu2001b8e2009-06-30 07:41:27 +0000201 const MemRegion* BaseR = 0;
Ted Kremenek7d71b292008-12-09 21:20:27 +0000202
203 switch(BaseL.getSubKind()) {
Ted Kremenek7d71b292008-12-09 21:20:27 +0000204 case loc::GotoLabelKind:
Ted Kremenek7d71b292008-12-09 21:20:27 +0000205 // Technically we can get here if people do funny things with casts.
206 return UndefinedVal();
207
Ted Kremenekabb042f2008-12-13 19:24:37 +0000208 case loc::MemRegionKind: {
209 const MemRegion *R = cast<loc::MemRegionVal>(BaseL).getRegion();
Ted Kremenekd76d47e2009-01-27 18:29:03 +0000210
Ted Kremenek921b0b52009-05-05 00:06:16 +0000211 if (isa<ElementRegion>(R)) {
Zhongxing Xub5b848e2009-05-04 08:52:47 +0000212 // int x;
213 // char* y = (char*) &x;
214 // 'y' => ElementRegion(0, VarRegion('x'))
215 // y[0] = 'a';
Ted Kremenekd76d47e2009-01-27 18:29:03 +0000216 return Base;
217 }
218
Ted Kremenek6bf01d62009-06-30 20:24:11 +0000219 if (isa<TypedRegion>(R) || isa<SymbolicRegion>(R)) {
220 BaseR = R;
Ted Kremenekabb042f2008-12-13 19:24:37 +0000221 break;
222 }
223
Ted Kremenek7d71b292008-12-09 21:20:27 +0000224 break;
Ted Kremenekabb042f2008-12-13 19:24:37 +0000225 }
Ted Kremenekd76d47e2009-01-27 18:29:03 +0000226
Ted Kremenek7d71b292008-12-09 21:20:27 +0000227 case loc::ConcreteIntKind:
228 // While these seem funny, this can happen through casts.
229 // FIXME: What we should return is the field offset. For example,
230 // add the field offset to the integer value. That way funny things
231 // like this work properly: &(((struct foo *) 0xa)->f)
232 return Base;
233
234 default:
235 assert ("Unhandled Base.");
236 return Base;
237 }
238
Ted Kremenek6bf01d62009-06-30 20:24:11 +0000239 if (BaseR) {
Zhongxing Xud91ee272009-06-23 09:02:15 +0000240 return ValMgr.makeLoc(MRMgr.getElementRegion(elementType, UnknownVal(),
241 BaseR, getContext()));
Ted Kremenek6bf01d62009-06-30 20:24:11 +0000242 }
Ted Kremenekabb042f2008-12-13 19:24:37 +0000243 else
244 return UnknownVal();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000245}
246
Ted Kremenek5fa93d52009-04-29 16:03:27 +0000247static bool isHigherOrderRawPtr(QualType T, ASTContext &C) {
Ted Kremenek1670e402009-04-11 00:11:10 +0000248 bool foundPointer = false;
249 while (1) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000250 const PointerType *PT = T->getAs<PointerType>();
Ted Kremenek1670e402009-04-11 00:11:10 +0000251 if (!PT) {
252 if (!foundPointer)
253 return false;
254
Ted Kremenek5fa93d52009-04-29 16:03:27 +0000255 // intptr_t* or intptr_t**, etc?
256 if (T->isIntegerType() && C.getTypeSize(T) == C.getTypeSize(C.VoidPtrTy))
257 return true;
258
Ted Kremenek1670e402009-04-11 00:11:10 +0000259 QualType X = C.getCanonicalType(T).getUnqualifiedType();
260 return X == C.VoidTy;
261 }
262
263 foundPointer = true;
264 T = PT->getPointeeType();
265 }
266}
Ted Kremenek5fa93d52009-04-29 16:03:27 +0000267
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000268SValuator::CastResult BasicStoreManager::Retrieve(const GRState *state,
269 Loc loc, QualType T) {
Ted Kremenek4323a572008-07-10 22:03:41 +0000270
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000271 if (isa<UnknownVal>(loc))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000272 return SValuator::CastResult(state, UnknownVal());
Ted Kremenek4323a572008-07-10 22:03:41 +0000273
Ted Kremenek1894dce2009-08-25 20:51:30 +0000274 assert(!isa<UndefinedVal>(loc));
Ted Kremenek4323a572008-07-10 22:03:41 +0000275
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000276 switch (loc.getSubKind()) {
Ted Kremenek4323a572008-07-10 22:03:41 +0000277
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000278 case loc::MemRegionKind: {
Ted Kremenekf684d562009-03-05 18:08:28 +0000279 const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion();
Ted Kremenek9e240492008-10-04 05:50:14 +0000280
Ted Kremenek20bd7462009-05-04 07:04:36 +0000281 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
Ted Kremenek5fa93d52009-04-29 16:03:27 +0000282 // Just support void**, void***, intptr_t*, intptr_t**, etc., for now.
283 // This is needed to handle OSCompareAndSwapPtr() and friends.
Ted Kremenek1670e402009-04-11 00:11:10 +0000284 ASTContext &Ctx = StateMgr.getContext();
Zhongxing Xuff697822009-05-09 00:50:33 +0000285 QualType T = ER->getLocationType(Ctx);
Ted Kremenek1670e402009-04-11 00:11:10 +0000286
Ted Kremenek5fa93d52009-04-29 16:03:27 +0000287 if (!isHigherOrderRawPtr(T, Ctx))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000288 return SValuator::CastResult(state, UnknownVal());
Ted Kremenek1670e402009-04-11 00:11:10 +0000289
Ted Kremenek20bd7462009-05-04 07:04:36 +0000290 // FIXME: Should check for element 0.
291 // Otherwise, strip the element region.
292 R = ER->getSuperRegion();
Ted Kremenek1670e402009-04-11 00:11:10 +0000293 }
294
Ted Kremenekf684d562009-03-05 18:08:28 +0000295 if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000296 return SValuator::CastResult(state, UnknownVal());
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000297
Ted Kremenekf684d562009-03-05 18:08:28 +0000298 BindingsTy B = GetBindings(state->getStore());
Ted Kremenek1894dce2009-08-25 20:51:30 +0000299 BindingsTy::data_type *Val = B.lookup(R);
300
301 if (!Val)
302 break;
303
304 return CastRetrievedVal(*Val, state, cast<TypedRegion>(R), T);
Ted Kremenek4323a572008-07-10 22:03:41 +0000305 }
306
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000307 case loc::ConcreteIntKind:
308 // Some clients may call GetSVal with such an option simply because
309 // they are doing a quick scan through their Locs (potentially to
Ted Kremenek4323a572008-07-10 22:03:41 +0000310 // invalidate their bindings). Just return Undefined.
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000311 return SValuator::CastResult(state, UndefinedVal());
Ted Kremenek4323a572008-07-10 22:03:41 +0000312
Ted Kremenek4323a572008-07-10 22:03:41 +0000313 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000314 assert (false && "Invalid Loc.");
Ted Kremenek4323a572008-07-10 22:03:41 +0000315 break;
316 }
317
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000318 return SValuator::CastResult(state, UnknownVal());
Ted Kremenek4323a572008-07-10 22:03:41 +0000319}
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000320
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000321Store BasicStoreManager::BindInternal(Store store, Loc loc, SVal V) {
Zhongxing Xu87453d12009-06-28 10:16:11 +0000322 if (isa<loc::ConcreteInt>(loc))
323 return store;
324
Zhongxing Xuc2a650a2009-06-28 09:26:15 +0000325 const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion();
326 ASTContext &C = StateMgr.getContext();
Ted Kremenek9e240492008-10-04 05:50:14 +0000327
Zhongxing Xuc2a650a2009-06-28 09:26:15 +0000328 // Special case: handle store of pointer values (Loc) to pointers via
329 // a cast to intXX_t*, void*, etc. This is needed to handle
330 // OSCompareAndSwap32Barrier/OSCompareAndSwap64Barrier.
331 if (isa<Loc>(V) || isa<nonloc::LocAsInteger>(V))
332 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
333 // FIXME: Should check for index 0.
334 QualType T = ER->getLocationType(C);
Ted Kremenek5fa93d52009-04-29 16:03:27 +0000335
Zhongxing Xuc2a650a2009-06-28 09:26:15 +0000336 if (isHigherOrderRawPtr(T, C))
337 R = ER->getSuperRegion();
338 }
Ted Kremenek5fa93d52009-04-29 16:03:27 +0000339
Zhongxing Xuc2a650a2009-06-28 09:26:15 +0000340 if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
341 return store;
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000342
Zhongxing Xuc2a650a2009-06-28 09:26:15 +0000343 if (nonloc::LocAsInteger *X = dyn_cast<nonloc::LocAsInteger>(&V)) {
344 // Only convert 'V' to a location iff the underlying region type
345 // is a location as well.
346 // FIXME: We are allowing a store of an arbitrary location to
347 // a pointer. We may wish to flag a type error here if the types
348 // are incompatible. This may also cause lots of breakage
349 // elsewhere. Food for thought.
350 if (const TypedRegion *TyR = dyn_cast<TypedRegion>(R)) {
351 if (TyR->isBoundable() &&
352 Loc::IsLocType(TyR->getValueType(C)))
353 V = X->getLoc();
354 }
Ted Kremenek4323a572008-07-10 22:03:41 +0000355 }
Zhongxing Xuc2a650a2009-06-28 09:26:15 +0000356
357 BindingsTy B = GetBindings(store);
358 return V.isUnknown()
359 ? VBFactory.Remove(B, R).getRoot()
360 : VBFactory.Add(B, R, V).getRoot();
Ted Kremenek4323a572008-07-10 22:03:41 +0000361}
362
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000363Store BasicStoreManager::Remove(Store store, Loc loc) {
364 switch (loc.getSubKind()) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000365 case loc::MemRegionKind: {
Ted Kremenekf684d562009-03-05 18:08:28 +0000366 const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion();
Ted Kremenek9e240492008-10-04 05:50:14 +0000367
Ted Kremenekf684d562009-03-05 18:08:28 +0000368 if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
Ted Kremenek9e240492008-10-04 05:50:14 +0000369 return store;
370
Ted Kremenekf684d562009-03-05 18:08:28 +0000371 return VBFactory.Remove(GetBindings(store), R).getRoot();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000372 }
Ted Kremenek4323a572008-07-10 22:03:41 +0000373 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000374 assert ("Remove for given Loc type not yet implemented.");
Ted Kremenekf59bf482008-07-17 18:38:48 +0000375 return store;
Ted Kremenek4323a572008-07-10 22:03:41 +0000376 }
377}
Ted Kremenekf59bf482008-07-17 18:38:48 +0000378
Ted Kremenek2f26bc32009-08-02 04:45:08 +0000379void
380BasicStoreManager::RemoveDeadBindings(GRState &state, Stmt* Loc,
Ted Kremenek241677a2009-01-21 22:26:05 +0000381 SymbolReaper& SymReaper,
Ted Kremenek2f26bc32009-08-02 04:45:08 +0000382 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
383{
384 Store store = state.getStore();
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000385 BindingsTy B = GetBindings(store);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000386 typedef SVal::symbol_iterator symbol_iterator;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000387
388 // Iterate over the variable bindings.
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000389 for (BindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) {
Ted Kremenekf684d562009-03-05 18:08:28 +0000390 if (const VarRegion *VR = dyn_cast<VarRegion>(I.getKey())) {
391 if (SymReaper.isLive(Loc, VR->getDecl()))
392 RegionRoots.push_back(VR);
393 else
394 continue;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000395 }
Ted Kremenekf684d562009-03-05 18:08:28 +0000396 else if (isa<ObjCIvarRegion>(I.getKey())) {
397 RegionRoots.push_back(I.getKey());
398 }
399 else
400 continue;
401
402 // Mark the bindings in the data as live.
403 SVal X = I.getData();
404 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
405 SymReaper.markLive(*SI);
Ted Kremenekbe912242009-03-05 16:31:07 +0000406 }
Ted Kremenekf59bf482008-07-17 18:38:48 +0000407
408 // Scan for live variables and live symbols.
Ted Kremenekf684d562009-03-05 18:08:28 +0000409 llvm::SmallPtrSet<const MemRegion*, 10> Marked;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000410
Ted Kremenek9e240492008-10-04 05:50:14 +0000411 while (!RegionRoots.empty()) {
Ted Kremenek134e7492008-10-17 22:52:40 +0000412 const MemRegion* MR = RegionRoots.back();
Ted Kremenek9e240492008-10-04 05:50:14 +0000413 RegionRoots.pop_back();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000414
Ted Kremenek134e7492008-10-17 22:52:40 +0000415 while (MR) {
416 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(MR)) {
Ted Kremenek241677a2009-01-21 22:26:05 +0000417 SymReaper.markLive(SymR->getSymbol());
Ted Kremenek134e7492008-10-17 22:52:40 +0000418 break;
419 }
Ted Kremenekf684d562009-03-05 18:08:28 +0000420 else if (isa<VarRegion>(MR) || isa<ObjCIvarRegion>(MR)) {
421 if (Marked.count(MR))
Ted Kremenek134e7492008-10-17 22:52:40 +0000422 break;
423
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000424 Marked.insert(MR);
Ted Kremenek2f26bc32009-08-02 04:45:08 +0000425 SVal X = Retrieve(&state, loc::MemRegionVal(MR)).getSVal();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000426
Ted Kremenek134e7492008-10-17 22:52:40 +0000427 // FIXME: We need to handle symbols nested in region definitions.
Ted Kremenek241677a2009-01-21 22:26:05 +0000428 for (symbol_iterator SI=X.symbol_begin(),SE=X.symbol_end();SI!=SE;++SI)
429 SymReaper.markLive(*SI);
Ted Kremenekf59bf482008-07-17 18:38:48 +0000430
Ted Kremenek134e7492008-10-17 22:52:40 +0000431 if (!isa<loc::MemRegionVal>(X))
432 break;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000433
Ted Kremenek134e7492008-10-17 22:52:40 +0000434 const loc::MemRegionVal& LVD = cast<loc::MemRegionVal>(X);
435 RegionRoots.push_back(LVD.getRegion());
436 break;
437 }
438 else if (const SubRegion* R = dyn_cast<SubRegion>(MR))
439 MR = R->getSuperRegion();
440 else
441 break;
442 }
Ted Kremenekf59bf482008-07-17 18:38:48 +0000443 }
444
445 // Remove dead variable bindings.
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000446 for (BindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) {
Ted Kremenekf684d562009-03-05 18:08:28 +0000447 const MemRegion* R = I.getKey();
Ted Kremenek9e240492008-10-04 05:50:14 +0000448
449 if (!Marked.count(R)) {
Zhongxing Xud91ee272009-06-23 09:02:15 +0000450 store = Remove(store, ValMgr.makeLoc(R));
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000451 SVal X = I.getData();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000452
453 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
Ted Kremenek241677a2009-01-21 22:26:05 +0000454 SymReaper.maybeDead(*SI);
Ted Kremenekf59bf482008-07-17 18:38:48 +0000455 }
Ted Kremenek9e240492008-10-04 05:50:14 +0000456 }
Ted Kremenekf684d562009-03-05 18:08:28 +0000457
Ted Kremenek2f26bc32009-08-02 04:45:08 +0000458 // Write the store back.
459 state.setStore(store);
Ted Kremenekf59bf482008-07-17 18:38:48 +0000460}
Ted Kremenekcaa37242008-08-19 16:51:45 +0000461
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000462Store BasicStoreManager::scanForIvars(Stmt *B, const Decl* SelfDecl,
463 const MemRegion *SelfRegion, Store St) {
Ted Kremenekf684d562009-03-05 18:08:28 +0000464 for (Stmt::child_iterator CI=B->child_begin(), CE=B->child_end();
465 CI != CE; ++CI) {
466
467 if (!*CI)
468 continue;
469
470 // Check if the statement is an ivar reference. We only
471 // care about self.ivar.
472 if (ObjCIvarRefExpr *IV = dyn_cast<ObjCIvarRefExpr>(*CI)) {
473 const Expr *Base = IV->getBase()->IgnoreParenCasts();
474 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Base)) {
475 if (DR->getDecl() == SelfDecl) {
476 const MemRegion *IVR = MRMgr.getObjCIvarRegion(IV->getDecl(),
Ted Kremenek8d7f5482009-04-09 22:22:44 +0000477 SelfRegion);
Zhongxing Xud9b6ad62009-05-09 04:08:27 +0000478 SVal X = ValMgr.getRegionValueSymbolVal(IVR);
Zhongxing Xud91ee272009-06-23 09:02:15 +0000479 St = BindInternal(St, ValMgr.makeLoc(IVR), X);
Ted Kremenekf684d562009-03-05 18:08:28 +0000480 }
481 }
482 }
483 else
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000484 St = scanForIvars(*CI, SelfDecl, SelfRegion, St);
Ted Kremenekf684d562009-03-05 18:08:28 +0000485 }
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000486
Ted Kremenekf684d562009-03-05 18:08:28 +0000487 return St;
488}
489
Zhongxing Xu17fd8632009-08-17 06:19:58 +0000490Store BasicStoreManager::getInitialStore(const LocationContext *InitLoc) {
Ted Kremenekcaa37242008-08-19 16:51:45 +0000491 // The LiveVariables information already has a compilation of all VarDecls
492 // used in the function. Iterate through this set, and "symbolicate"
493 // any VarDecl whose value originally comes from outside the function.
Ted Kremenekcaa37242008-08-19 16:51:45 +0000494 typedef LiveVariables::AnalysisDataTy LVDataTy;
Zhongxing Xu17fd8632009-08-17 06:19:58 +0000495 LVDataTy& D = InitLoc->getLiveVariables()->getAnalysisData();
Ted Kremenekcaa37242008-08-19 16:51:45 +0000496 Store St = VBFactory.GetEmptyMap().getRoot();
497
498 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000499 NamedDecl* ND = const_cast<NamedDecl*>(I->first);
Ted Kremenekcaa37242008-08-19 16:51:45 +0000500
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000501 // Handle implicit parameters.
502 if (ImplicitParamDecl* PD = dyn_cast<ImplicitParamDecl>(ND)) {
Zhongxing Xueda95462009-08-21 02:58:11 +0000503 const Decl& CD = *InitLoc->getDecl();
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000504 if (const ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(&CD)) {
505 if (MD->getSelfDecl() == PD) {
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000506 // FIXME: Just use a symbolic region, and remove ObjCObjectRegion
507 // entirely.
508 const ObjCObjectRegion *SelfRegion =
509 MRMgr.getObjCObjectRegion(MD->getClassInterface(),
510 MRMgr.getHeapRegion());
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000511
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000512 St = BindInternal(St, ValMgr.makeLoc(MRMgr.getVarRegion(PD, InitLoc)),
Zhongxing Xud91ee272009-06-23 09:02:15 +0000513 ValMgr.makeLoc(SelfRegion));
Ted Kremenekf684d562009-03-05 18:08:28 +0000514
515 // Scan the method for ivar references. While this requires an
516 // entire AST scan, the cost should not be high in practice.
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000517 St = scanForIvars(MD->getBody(), PD, SelfRegion, St);
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000518 }
519 }
520 }
521 else if (VarDecl* VD = dyn_cast<VarDecl>(ND)) {
Ted Kremenek693de5d2009-03-23 15:42:58 +0000522 // Only handle simple types that we can symbolicate.
523 if (!SymbolManager::canSymbolicate(VD->getType()))
524 continue;
Ted Kremenekcaa37242008-08-19 16:51:45 +0000525
Ted Kremenekec099f12009-03-18 22:10:22 +0000526 // Initialize globals and parameters to symbolic values.
527 // Initialize local variables to undefined.
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000528 const MemRegion *R = ValMgr.getRegionManager().getVarRegion(VD, InitLoc);
Ted Kremenek15086362009-07-02 18:25:09 +0000529 SVal X = R->hasGlobalsOrParametersStorage()
530 ? ValMgr.getRegionValueSymbolVal(R)
531 : UndefinedVal();
Ted Kremenekcaa37242008-08-19 16:51:45 +0000532
Zhongxing Xud91ee272009-06-23 09:02:15 +0000533 St = BindInternal(St, ValMgr.makeLoc(R), X);
Ted Kremenekcaa37242008-08-19 16:51:45 +0000534 }
535 }
536 return St;
537}
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000538
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000539Store BasicStoreManager::BindDeclInternal(Store store, const VarDecl* VD,
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000540 const LocationContext *LC,
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000541 SVal* InitVal) {
Ted Kremenek42577d12008-11-12 19:18:35 +0000542
Ted Kremeneke53c0692008-08-23 00:50:55 +0000543 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
Ted Kremenek42577d12008-11-12 19:18:35 +0000544
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000545 // BasicStore does not model arrays and structs.
546 if (VD->getType()->isArrayType() || VD->getType()->isStructureType())
547 return store;
548
549 if (VD->hasGlobalStorage()) {
550 // Handle variables with global storage: extern, static, PrivateExtern.
551
552 // FIXME:: static variables may have an initializer, but the second time a
553 // function is called those values may not be current. Currently, a function
554 // will not be called more than once.
555
556 // Static global variables should not be visited here.
557 assert(!(VD->getStorageClass() == VarDecl::Static &&
558 VD->isFileVarDecl()));
559
560 // Process static variables.
561 if (VD->getStorageClass() == VarDecl::Static) {
562 // C99: 6.7.8 Initialization
563 // If an object that has static storage duration is not initialized
564 // explicitly, then:
565 // —if it has pointer type, it is initialized to a null pointer;
566 // —if it has arithmetic type, it is initialized to (positive or
567 // unsigned) zero;
Ted Kremenek42577d12008-11-12 19:18:35 +0000568 if (!InitVal) {
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000569 QualType T = VD->getType();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000570 if (Loc::IsLocType(T))
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000571 store = BindInternal(store, getLoc(VD, LC),
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000572 loc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000573 else if (T->isIntegerType())
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000574 store = BindInternal(store, getLoc(VD, LC),
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000575 nonloc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000576 else {
577 // assert(0 && "ignore other types of variables");
578 }
579 } else {
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000580 store = BindInternal(store, getLoc(VD, LC), *InitVal);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000581 }
582 }
583 } else {
584 // Process local scalar variables.
585 QualType T = VD->getType();
Ted Kremenekf6c9f422009-07-03 00:36:16 +0000586 if (ValMgr.getSymbolManager().canSymbolicate(T)) {
Ted Kremenek42577d12008-11-12 19:18:35 +0000587 SVal V = InitVal ? *InitVal : UndefinedVal();
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000588 store = BindInternal(store, getLoc(VD, LC), V);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000589 }
590 }
591
592 return store;
593}
594
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000595void BasicStoreManager::print(Store store, llvm::raw_ostream& Out,
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000596 const char* nl, const char *sep) {
597
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000598 BindingsTy B = GetBindings(store);
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000599 Out << "Variables:" << nl;
600
601 bool isFirst = true;
602
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000603 for (BindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I) {
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000604 if (isFirst)
605 isFirst = false;
606 else
607 Out << nl;
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000608
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000609 Out << ' ' << I.getKey() << " : " << I.getData();
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000610 }
611}
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000612
Ted Kremenek60dbad82008-09-03 03:06:11 +0000613
614void BasicStoreManager::iterBindings(Store store, BindingsHandler& f) {
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000615 BindingsTy B = GetBindings(store);
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000616
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000617 for (BindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I)
Ted Kremenekbe912242009-03-05 16:31:07 +0000618 f.HandleBinding(*this, store, I.getKey(), I.getData());
Ted Kremenek9e240492008-10-04 05:50:14 +0000619
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000620}
621
Ted Kremenek60dbad82008-09-03 03:06:11 +0000622StoreManager::BindingsHandler::~BindingsHandler() {}
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000623
624//===----------------------------------------------------------------------===//
625// Binding invalidation.
626//===----------------------------------------------------------------------===//
627
628const GRState *BasicStoreManager::InvalidateRegion(const GRState *state,
629 const MemRegion *R,
630 const Expr *E,
631 unsigned Count) {
632 R = R->getBaseRegion();
633
634 if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
635 return state;
636
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000637 QualType T = cast<TypedRegion>(R)->getValueType(R->getContext());
638 SVal V = ValMgr.getConjuredSymbolVal(E, T, Count);
639 return Bind(state, loc::MemRegionVal(R), V);
640}
641