blob: d0798db2c49159627a48d9c65eb347da40b137c0 [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
Zhongxing Xu6149e882008-10-24 04:33:15 +000089 void print(Store store, std::ostream& Out, const char* nl, const char *sep);
Zhongxing Xue4b6fc22008-10-24 01:38:55 +000090
91 void iterBindings(Store store, BindingsHandler& f) {
92 // FIXME: Implement.
93 }
Zhongxing Xu79c57f82008-10-08 02:50:44 +000094};
95
96} // end anonymous namespace
97
Ted Kremenekc3803992008-10-24 01:04:59 +000098StoreManager* clang::CreateRegionStoreManager(GRStateManager& StMgr) {
Zhongxing Xue4b6fc22008-10-24 01:38:55 +000099 return new RegionStoreManager(StMgr);
Ted Kremenekc3803992008-10-24 01:04:59 +0000100}
101
Zhongxing Xue3954d12008-10-21 05:29:26 +0000102Loc RegionStoreManager::getElementLoc(const VarDecl* VD, SVal Idx) {
103 MemRegion* R = MRMgr.getVarRegion(VD);
104 ElementRegion* ER = MRMgr.getElementRegion(Idx, R);
105 return loc::MemRegionVal(ER);
106}
107
Zhongxing Xu6f1b5152008-10-22 13:44:38 +0000108SVal RegionStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) {
109 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
110}
111
112SVal RegionStoreManager::getLValueIvar(const GRState* St, const ObjCIvarDecl* D,
113 SVal Base) {
114 return UnknownVal();
115}
116
117SVal RegionStoreManager::getLValueField(const GRState* St, SVal Base,
118 const FieldDecl* D) {
119 if (Base.isUnknownOrUndef())
120 return Base;
121
122 Loc BaseL = cast<Loc>(Base);
123 const MemRegion* BaseR = 0;
124
125 switch (BaseL.getSubKind()) {
126 case loc::MemRegionKind:
127 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
128 break;
129
130 case loc::SymbolValKind:
131 BaseR = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(&BaseL)->getSymbol());
132 break;
133
134 case loc::GotoLabelKind:
135 case loc::FuncValKind:
136 // These are anormal cases. Flag an undefined value.
137 return UndefinedVal();
138
139 case loc::ConcreteIntKind:
140 case loc::StringLiteralValKind:
141 // While these seem funny, this can happen through casts.
142 // FIXME: What we should return is the field offset. For example,
143 // add the field offset to the integer value. That way funny things
144 // like this work properly: &(((struct foo *) 0xa)->f)
145 return Base;
146
147 default:
148 assert("Unhandled Base.");
149 return Base;
150 }
151
152 return loc::MemRegionVal(MRMgr.getFieldRegion(D, BaseR));
153}
154
Zhongxing Xu0972d0a2008-10-24 01:09:32 +0000155SVal RegionStoreManager::getLValueElement(const GRState* St,
156 SVal Base, SVal Offset) {
157 if (Base.isUnknownOrUndef())
158 return Base;
159
160 loc::MemRegionVal& BaseL = cast<loc::MemRegionVal>(Base);
161
162 // We expect BaseR is an ElementRegion, not a base VarRegion.
163
164 const ElementRegion* ElemR = cast<ElementRegion>(BaseL.getRegion());
165
166 SVal Idx = ElemR->getIndex();
167
168 nonloc::ConcreteInt *CI1, *CI2;
169
170 // Only handle integer indices for now.
171 if ((CI1 = dyn_cast<nonloc::ConcreteInt>(&Idx)) &&
172 (CI2 = dyn_cast<nonloc::ConcreteInt>(&Offset))) {
173 SVal NewIdx = CI1->EvalBinOp(StateMgr.getBasicVals(), BinaryOperator::Add,
174 *CI2);
175 return loc::MemRegionVal(MRMgr.getElementRegion(NewIdx,
176 ElemR->getSuperRegion()));
177 }
178
179 return UnknownVal();
180}
181
182// Cast 'pointer to array' to 'pointer to the first element of array'.
183
184SVal RegionStoreManager::ArrayToPointer(SVal Array) {
185 const MemRegion* ArrayR = cast<loc::MemRegionVal>(&Array)->getRegion();
186
187 const VarDecl* D = cast<VarRegion>(ArrayR)->getDecl();
188
189 if (const ConstantArrayType* CAT =
190 dyn_cast<ConstantArrayType>(D->getType().getTypePtr())) {
191
192 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
193
194 nonloc::ConcreteInt Idx(BasicVals.getValue(0, CAT->getSize().getBitWidth(),
195 false));
196
197 ElementRegion* ER = MRMgr.getElementRegion(Idx, ArrayR);
198
199 return loc::MemRegionVal(ER);
200 }
201
202 return Array;
203}
204
Zhongxing Xu73249322008-10-21 06:27:32 +0000205SVal RegionStoreManager::Retrieve(Store S, Loc L, QualType T) {
Zhongxing Xue3954d12008-10-21 05:29:26 +0000206 assert(!isa<UnknownVal>(L) && "location unknown");
207 assert(!isa<UndefinedVal>(L) && "location undefined");
208
209 switch (L.getSubKind()) {
210 case loc::MemRegionKind: {
211 const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion();
212 assert(R && "bad region");
213
214 RegionBindingsTy B(static_cast<const RegionBindingsTy::TreeTy*>(S));
215 RegionBindingsTy::data_type* V = B.lookup(R);
216 return V ? *V : UnknownVal();
217 }
218
219 case loc::SymbolValKind:
220 return UnknownVal();
221
222 case loc::ConcreteIntKind:
223 return UndefinedVal(); // As in BasicStoreManager.
224
225 case loc::FuncValKind:
226 return L;
227
228 case loc::StringLiteralValKind:
229 return UnknownVal();
230
231 default:
232 assert(false && "Invalid Location");
233 break;
234 }
235}
236
Zhongxing Xu73249322008-10-21 06:27:32 +0000237Store RegionStoreManager::Bind(Store store, Loc LV, SVal V) {
Zhongxing Xu097fc982008-10-17 05:57:07 +0000238 assert(LV.getSubKind() == loc::MemRegionKind);
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000239
Ted Kremenek38a4b4b2008-10-17 20:28:54 +0000240 const MemRegion* R = cast<loc::MemRegionVal>(LV).getRegion();
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000241
242 if (!R)
243 return store;
244
245 RegionBindingsTy B = GetRegionBindings(store);
246 return V.isUnknown()
247 ? RBFactory.Remove(B, R).getRoot()
248 : RBFactory.Add(B, R, V).getRoot();
249}
250
251Store RegionStoreManager::getInitialStore() {
252 typedef LiveVariables::AnalysisDataTy LVDataTy;
253 LVDataTy& D = StateMgr.getLiveVariables().getAnalysisData();
254
255 Store St = RBFactory.GetEmptyMap().getRoot();
256
257 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
Douglas Gregord2baafd2008-10-21 16:13:35 +0000258 NamedDecl* ND = const_cast<NamedDecl*>(I->first);
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000259
Douglas Gregord2baafd2008-10-21 16:13:35 +0000260 if (VarDecl* VD = dyn_cast<VarDecl>(ND)) {
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000261 // Punt on static variables for now.
262 if (VD->getStorageClass() == VarDecl::Static)
263 continue;
264
265 QualType T = VD->getType();
266 // Only handle pointers and integers for now.
Zhongxing Xu097fc982008-10-17 05:57:07 +0000267 if (Loc::IsLocType(T) || T->isIntegerType()) {
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000268 // Initialize globals and parameters to symbolic values.
269 // Initialize local variables to undefined.
Zhongxing Xu097fc982008-10-17 05:57:07 +0000270 SVal X = (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) ||
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000271 isa<ImplicitParamDecl>(VD))
Zhongxing Xu097fc982008-10-17 05:57:07 +0000272 ? SVal::GetSymbolValue(StateMgr.getSymbolManager(), VD)
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000273 : UndefinedVal();
274
Zhongxing Xu73249322008-10-21 06:27:32 +0000275 St = Bind(St, getVarLoc(VD), X);
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000276 }
277 }
278 }
279 return St;
280}
Zhongxing Xue3954d12008-10-21 05:29:26 +0000281
282Store RegionStoreManager::AddDecl(Store store,
283 const VarDecl* VD, Expr* Ex,
284 SVal InitVal, unsigned Count) {
285 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
286 SymbolManager& SymMgr = StateMgr.getSymbolManager();
287
288 if (VD->hasGlobalStorage()) {
289 // Static global variables should not be visited here.
290 assert(!(VD->getStorageClass() == VarDecl::Static &&
291 VD->isFileVarDecl()));
292 // Process static variables.
293 if (VD->getStorageClass() == VarDecl::Static) {
294 if (!Ex) {
295 // Only handle pointer and integer static variables.
296
297 QualType T = VD->getType();
298
299 if (Loc::IsLocType(T))
Zhongxing Xu73249322008-10-21 06:27:32 +0000300 store = Bind(store, getVarLoc(VD),
301 loc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xue3954d12008-10-21 05:29:26 +0000302
303 else if (T->isIntegerType())
Zhongxing Xu73249322008-10-21 06:27:32 +0000304 store = Bind(store, getVarLoc(VD),
305 loc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xue3954d12008-10-21 05:29:26 +0000306 else
307 assert("ignore other types of variables");
308 } else {
Zhongxing Xu73249322008-10-21 06:27:32 +0000309 store = Bind(store, getVarLoc(VD), InitVal);
Zhongxing Xue3954d12008-10-21 05:29:26 +0000310 }
311 }
312 } else {
313 // Process local variables.
314
315 QualType T = VD->getType();
316
317 if (Loc::IsLocType(T) || T->isIntegerType()) {
318 SVal V = Ex ? InitVal : UndefinedVal();
319 if (Ex && InitVal.isUnknown()) {
320 // "Conjured" symbols.
321 SymbolID Sym = SymMgr.getConjuredSymbol(Ex, Count);
322 V = Loc::IsLocType(Ex->getType())
323 ? cast<SVal>(loc::SymbolVal(Sym))
324 : cast<SVal>(nonloc::SymbolVal(Sym));
325 }
Zhongxing Xu73249322008-10-21 06:27:32 +0000326 store = Bind(store, getVarLoc(VD), V);
Zhongxing Xue3954d12008-10-21 05:29:26 +0000327
328 } else if (T->isArrayType()) {
329 // Only handle constant size array.
330 if (ConstantArrayType* CAT=dyn_cast<ConstantArrayType>(T.getTypePtr())) {
331
332 llvm::APInt Size = CAT->getSize();
333
334 for (llvm::APInt i = llvm::APInt::getNullValue(Size.getBitWidth());
335 i != Size; ++i) {
336 nonloc::ConcreteInt Idx(BasicVals.getValue(llvm::APSInt(i)));
Zhongxing Xu73249322008-10-21 06:27:32 +0000337 store = Bind(store, getElementLoc(VD, Idx), UndefinedVal());
Zhongxing Xue3954d12008-10-21 05:29:26 +0000338 }
339 }
340 } else if (T->isStructureType()) {
341 // FIXME: Implement struct initialization.
342 }
343 }
344 return store;
345}
346
Zhongxing Xu6149e882008-10-24 04:33:15 +0000347void RegionStoreManager::print(Store store, std::ostream& Out, const char* nl,
348 const char *sep) {
349
350}