blob: 05cd46e1f90ed941f2a561feca10767966effe5b [file] [log] [blame]
Zhongxing Xu79c57f82008-10-08 02:50:44 +00001//== RegionStore.cpp - Field-sensitive store model --------------*- 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 defines a basic region store model. In this model, we do have field
11// sensitivity. But we assume nothing about the heap shape. So recursive data
12// structures are largely ignored. Basically we do 1-limiting analysis.
13// Parameter pointers are assumed with no aliasing. Pointee objects of
14// parameters are created lazily.
15//
16//===----------------------------------------------------------------------===//
17#include "clang/Analysis/PathSensitive/MemRegion.h"
18#include "clang/Analysis/PathSensitive/GRState.h"
19#include "clang/Analysis/Analyses/LiveVariables.h"
20
21#include "llvm/ADT/ImmutableMap.h"
22#include "llvm/Support/Compiler.h"
23
24using namespace clang;
25
Zhongxing Xu097fc982008-10-17 05:57:07 +000026typedef llvm::ImmutableMap<const MemRegion*, SVal> RegionBindingsTy;
Zhongxing Xu79c57f82008-10-08 02:50:44 +000027
28namespace {
29
30class VISIBILITY_HIDDEN RegionStoreManager : public StoreManager {
31 RegionBindingsTy::Factory RBFactory;
32 GRStateManager& StateMgr;
33 MemRegionManager MRMgr;
34
35public:
36 RegionStoreManager(GRStateManager& mgr)
37 : StateMgr(mgr), MRMgr(StateMgr.getAllocator()) {}
38
39 virtual ~RegionStoreManager() {}
40
Zhongxing Xue4b6fc22008-10-24 01:38:55 +000041 MemRegionManager& getRegionManager() { return MRMgr; }
42
43 // FIXME: Is this function necessary?
44 SVal GetRegionSVal(Store St, const MemRegion* R) {
45 return Retrieve(St, loc::MemRegionVal(R));
46 }
47
Zhongxing Xu6f1b5152008-10-22 13:44:38 +000048 SVal getLValueVar(const GRState* St, const VarDecl* VD);
49
50 SVal getLValueIvar(const GRState* St, const ObjCIvarDecl* D, SVal Base);
51
52 SVal getLValueField(const GRState* St, SVal Base, const FieldDecl* D);
53
Zhongxing Xu0972d0a2008-10-24 01:09:32 +000054 SVal getLValueElement(const GRState* St, SVal Base, SVal Offset);
55
56 SVal ArrayToPointer(SVal Array);
57
Zhongxing Xue4b6fc22008-10-24 01:38:55 +000058 SVal Retrieve(Store S, Loc L, QualType T = QualType());
Zhongxing Xu6f1b5152008-10-22 13:44:38 +000059
Zhongxing Xu73249322008-10-21 06:27:32 +000060 Store Bind(Store St, Loc LV, SVal V);
Zhongxing Xu79c57f82008-10-08 02:50:44 +000061
Zhongxing Xue4b6fc22008-10-24 01:38:55 +000062 Store Remove(Store store, Loc LV) {
63 // FIXME: Implement.
64 return store;
65 }
66
Zhongxing Xu79c57f82008-10-08 02:50:44 +000067 Store getInitialStore();
68
Zhongxing Xue4b6fc22008-10-24 01:38:55 +000069 Store RemoveDeadBindings(Store store, Stmt* Loc, const LiveVariables& Live,
70 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots,
71 LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols) {
72 // FIXME: Implement this.
73 return store;
74 }
75
Zhongxing Xue3954d12008-10-21 05:29:26 +000076 Store AddDecl(Store store, const VarDecl* VD, Expr* Ex, SVal InitVal,
77 unsigned Count);
78
79 Loc getVarLoc(const VarDecl* VD) {
80 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
81 }
82
83 Loc getElementLoc(const VarDecl* VD, SVal Idx);
84
Zhongxing Xu79c57f82008-10-08 02:50:44 +000085 static inline RegionBindingsTy GetRegionBindings(Store store) {
86 return RegionBindingsTy(static_cast<const RegionBindingsTy::TreeTy*>(store));
87 }
Zhongxing Xue4b6fc22008-10-24 01:38:55 +000088
89 void print(Store store, std::ostream& Out, const char* nl, const char *sep) {
90 // FIXME: Implement.
91 }
92
93 void iterBindings(Store store, BindingsHandler& f) {
94 // FIXME: Implement.
95 }
Zhongxing Xu79c57f82008-10-08 02:50:44 +000096};
97
98} // end anonymous namespace
99
Ted Kremenekc3803992008-10-24 01:04:59 +0000100StoreManager* clang::CreateRegionStoreManager(GRStateManager& StMgr) {
Zhongxing Xue4b6fc22008-10-24 01:38:55 +0000101 return new RegionStoreManager(StMgr);
Ted Kremenekc3803992008-10-24 01:04:59 +0000102}
103
Zhongxing Xue3954d12008-10-21 05:29:26 +0000104Loc RegionStoreManager::getElementLoc(const VarDecl* VD, SVal Idx) {
105 MemRegion* R = MRMgr.getVarRegion(VD);
106 ElementRegion* ER = MRMgr.getElementRegion(Idx, R);
107 return loc::MemRegionVal(ER);
108}
109
Zhongxing Xu6f1b5152008-10-22 13:44:38 +0000110SVal RegionStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) {
111 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
112}
113
114SVal RegionStoreManager::getLValueIvar(const GRState* St, const ObjCIvarDecl* D,
115 SVal Base) {
116 return UnknownVal();
117}
118
119SVal RegionStoreManager::getLValueField(const GRState* St, SVal Base,
120 const FieldDecl* D) {
121 if (Base.isUnknownOrUndef())
122 return Base;
123
124 Loc BaseL = cast<Loc>(Base);
125 const MemRegion* BaseR = 0;
126
127 switch (BaseL.getSubKind()) {
128 case loc::MemRegionKind:
129 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
130 break;
131
132 case loc::SymbolValKind:
133 BaseR = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(&BaseL)->getSymbol());
134 break;
135
136 case loc::GotoLabelKind:
137 case loc::FuncValKind:
138 // These are anormal cases. Flag an undefined value.
139 return UndefinedVal();
140
141 case loc::ConcreteIntKind:
142 case loc::StringLiteralValKind:
143 // While these seem funny, this can happen through casts.
144 // FIXME: What we should return is the field offset. For example,
145 // add the field offset to the integer value. That way funny things
146 // like this work properly: &(((struct foo *) 0xa)->f)
147 return Base;
148
149 default:
150 assert("Unhandled Base.");
151 return Base;
152 }
153
154 return loc::MemRegionVal(MRMgr.getFieldRegion(D, BaseR));
155}
156
Zhongxing Xu0972d0a2008-10-24 01:09:32 +0000157SVal RegionStoreManager::getLValueElement(const GRState* St,
158 SVal Base, SVal Offset) {
159 if (Base.isUnknownOrUndef())
160 return Base;
161
162 loc::MemRegionVal& BaseL = cast<loc::MemRegionVal>(Base);
163
164 // We expect BaseR is an ElementRegion, not a base VarRegion.
165
166 const ElementRegion* ElemR = cast<ElementRegion>(BaseL.getRegion());
167
168 SVal Idx = ElemR->getIndex();
169
170 nonloc::ConcreteInt *CI1, *CI2;
171
172 // Only handle integer indices for now.
173 if ((CI1 = dyn_cast<nonloc::ConcreteInt>(&Idx)) &&
174 (CI2 = dyn_cast<nonloc::ConcreteInt>(&Offset))) {
175 SVal NewIdx = CI1->EvalBinOp(StateMgr.getBasicVals(), BinaryOperator::Add,
176 *CI2);
177 return loc::MemRegionVal(MRMgr.getElementRegion(NewIdx,
178 ElemR->getSuperRegion()));
179 }
180
181 return UnknownVal();
182}
183
184// Cast 'pointer to array' to 'pointer to the first element of array'.
185
186SVal RegionStoreManager::ArrayToPointer(SVal Array) {
187 const MemRegion* ArrayR = cast<loc::MemRegionVal>(&Array)->getRegion();
188
189 const VarDecl* D = cast<VarRegion>(ArrayR)->getDecl();
190
191 if (const ConstantArrayType* CAT =
192 dyn_cast<ConstantArrayType>(D->getType().getTypePtr())) {
193
194 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
195
196 nonloc::ConcreteInt Idx(BasicVals.getValue(0, CAT->getSize().getBitWidth(),
197 false));
198
199 ElementRegion* ER = MRMgr.getElementRegion(Idx, ArrayR);
200
201 return loc::MemRegionVal(ER);
202 }
203
204 return Array;
205}
206
Zhongxing Xu73249322008-10-21 06:27:32 +0000207SVal RegionStoreManager::Retrieve(Store S, Loc L, QualType T) {
Zhongxing Xue3954d12008-10-21 05:29:26 +0000208 assert(!isa<UnknownVal>(L) && "location unknown");
209 assert(!isa<UndefinedVal>(L) && "location undefined");
210
211 switch (L.getSubKind()) {
212 case loc::MemRegionKind: {
213 const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion();
214 assert(R && "bad region");
215
216 RegionBindingsTy B(static_cast<const RegionBindingsTy::TreeTy*>(S));
217 RegionBindingsTy::data_type* V = B.lookup(R);
218 return V ? *V : UnknownVal();
219 }
220
221 case loc::SymbolValKind:
222 return UnknownVal();
223
224 case loc::ConcreteIntKind:
225 return UndefinedVal(); // As in BasicStoreManager.
226
227 case loc::FuncValKind:
228 return L;
229
230 case loc::StringLiteralValKind:
231 return UnknownVal();
232
233 default:
234 assert(false && "Invalid Location");
235 break;
236 }
237}
238
Zhongxing Xu73249322008-10-21 06:27:32 +0000239Store RegionStoreManager::Bind(Store store, Loc LV, SVal V) {
Zhongxing Xu097fc982008-10-17 05:57:07 +0000240 assert(LV.getSubKind() == loc::MemRegionKind);
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000241
Ted Kremenek38a4b4b2008-10-17 20:28:54 +0000242 const MemRegion* R = cast<loc::MemRegionVal>(LV).getRegion();
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000243
244 if (!R)
245 return store;
246
247 RegionBindingsTy B = GetRegionBindings(store);
248 return V.isUnknown()
249 ? RBFactory.Remove(B, R).getRoot()
250 : RBFactory.Add(B, R, V).getRoot();
251}
252
253Store RegionStoreManager::getInitialStore() {
254 typedef LiveVariables::AnalysisDataTy LVDataTy;
255 LVDataTy& D = StateMgr.getLiveVariables().getAnalysisData();
256
257 Store St = RBFactory.GetEmptyMap().getRoot();
258
259 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
Douglas Gregord2baafd2008-10-21 16:13:35 +0000260 NamedDecl* ND = const_cast<NamedDecl*>(I->first);
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000261
Douglas Gregord2baafd2008-10-21 16:13:35 +0000262 if (VarDecl* VD = dyn_cast<VarDecl>(ND)) {
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000263 // Punt on static variables for now.
264 if (VD->getStorageClass() == VarDecl::Static)
265 continue;
266
267 QualType T = VD->getType();
268 // Only handle pointers and integers for now.
Zhongxing Xu097fc982008-10-17 05:57:07 +0000269 if (Loc::IsLocType(T) || T->isIntegerType()) {
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000270 // Initialize globals and parameters to symbolic values.
271 // Initialize local variables to undefined.
Zhongxing Xu097fc982008-10-17 05:57:07 +0000272 SVal X = (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) ||
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000273 isa<ImplicitParamDecl>(VD))
Zhongxing Xu097fc982008-10-17 05:57:07 +0000274 ? SVal::GetSymbolValue(StateMgr.getSymbolManager(), VD)
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000275 : UndefinedVal();
276
Zhongxing Xu73249322008-10-21 06:27:32 +0000277 St = Bind(St, getVarLoc(VD), X);
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000278 }
279 }
280 }
281 return St;
282}
Zhongxing Xue3954d12008-10-21 05:29:26 +0000283
284Store RegionStoreManager::AddDecl(Store store,
285 const VarDecl* VD, Expr* Ex,
286 SVal InitVal, unsigned Count) {
287 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
288 SymbolManager& SymMgr = StateMgr.getSymbolManager();
289
290 if (VD->hasGlobalStorage()) {
291 // Static global variables should not be visited here.
292 assert(!(VD->getStorageClass() == VarDecl::Static &&
293 VD->isFileVarDecl()));
294 // Process static variables.
295 if (VD->getStorageClass() == VarDecl::Static) {
296 if (!Ex) {
297 // Only handle pointer and integer static variables.
298
299 QualType T = VD->getType();
300
301 if (Loc::IsLocType(T))
Zhongxing Xu73249322008-10-21 06:27:32 +0000302 store = Bind(store, getVarLoc(VD),
303 loc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xue3954d12008-10-21 05:29:26 +0000304
305 else if (T->isIntegerType())
Zhongxing Xu73249322008-10-21 06:27:32 +0000306 store = Bind(store, getVarLoc(VD),
307 loc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xue3954d12008-10-21 05:29:26 +0000308 else
309 assert("ignore other types of variables");
310 } else {
Zhongxing Xu73249322008-10-21 06:27:32 +0000311 store = Bind(store, getVarLoc(VD), InitVal);
Zhongxing Xue3954d12008-10-21 05:29:26 +0000312 }
313 }
314 } else {
315 // Process local variables.
316
317 QualType T = VD->getType();
318
319 if (Loc::IsLocType(T) || T->isIntegerType()) {
320 SVal V = Ex ? InitVal : UndefinedVal();
321 if (Ex && InitVal.isUnknown()) {
322 // "Conjured" symbols.
323 SymbolID Sym = SymMgr.getConjuredSymbol(Ex, Count);
324 V = Loc::IsLocType(Ex->getType())
325 ? cast<SVal>(loc::SymbolVal(Sym))
326 : cast<SVal>(nonloc::SymbolVal(Sym));
327 }
Zhongxing Xu73249322008-10-21 06:27:32 +0000328 store = Bind(store, getVarLoc(VD), V);
Zhongxing Xue3954d12008-10-21 05:29:26 +0000329
330 } else if (T->isArrayType()) {
331 // Only handle constant size array.
332 if (ConstantArrayType* CAT=dyn_cast<ConstantArrayType>(T.getTypePtr())) {
333
334 llvm::APInt Size = CAT->getSize();
335
336 for (llvm::APInt i = llvm::APInt::getNullValue(Size.getBitWidth());
337 i != Size; ++i) {
338 nonloc::ConcreteInt Idx(BasicVals.getValue(llvm::APSInt(i)));
Zhongxing Xu73249322008-10-21 06:27:32 +0000339 store = Bind(store, getElementLoc(VD, Idx), UndefinedVal());
Zhongxing Xue3954d12008-10-21 05:29:26 +0000340 }
341 }
342 } else if (T->isStructureType()) {
343 // FIXME: Implement struct initialization.
344 }
345 }
346 return store;
347}
348