blob: 417521a14ec5e55e8dae4bb978c47d4498552153 [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"
Zhongxing Xuca892b82008-10-24 06:01:33 +000022#include "llvm/Support/raw_ostream.h"
Zhongxing Xu79c57f82008-10-08 02:50:44 +000023#include "llvm/Support/Compiler.h"
24
25using namespace clang;
26
Zhongxing Xu097fc982008-10-17 05:57:07 +000027typedef llvm::ImmutableMap<const MemRegion*, SVal> RegionBindingsTy;
Zhongxing Xu79c57f82008-10-08 02:50:44 +000028
29namespace {
30
31class VISIBILITY_HIDDEN RegionStoreManager : public StoreManager {
32 RegionBindingsTy::Factory RBFactory;
33 GRStateManager& StateMgr;
34 MemRegionManager MRMgr;
35
36public:
37 RegionStoreManager(GRStateManager& mgr)
38 : StateMgr(mgr), MRMgr(StateMgr.getAllocator()) {}
39
40 virtual ~RegionStoreManager() {}
41
Zhongxing Xue4b6fc22008-10-24 01:38:55 +000042 MemRegionManager& getRegionManager() { return MRMgr; }
43
44 // FIXME: Is this function necessary?
45 SVal GetRegionSVal(Store St, const MemRegion* R) {
46 return Retrieve(St, loc::MemRegionVal(R));
47 }
48
Zhongxing Xu6f1b5152008-10-22 13:44:38 +000049 SVal getLValueVar(const GRState* St, const VarDecl* VD);
50
51 SVal getLValueIvar(const GRState* St, const ObjCIvarDecl* D, SVal Base);
52
53 SVal getLValueField(const GRState* St, SVal Base, const FieldDecl* D);
54
Zhongxing Xu0972d0a2008-10-24 01:09:32 +000055 SVal getLValueElement(const GRState* St, SVal Base, SVal Offset);
56
57 SVal ArrayToPointer(SVal Array);
58
Zhongxing Xue4b6fc22008-10-24 01:38:55 +000059 SVal Retrieve(Store S, Loc L, QualType T = QualType());
Zhongxing Xu6f1b5152008-10-22 13:44:38 +000060
Zhongxing Xu73249322008-10-21 06:27:32 +000061 Store Bind(Store St, Loc LV, SVal V);
Zhongxing Xu79c57f82008-10-08 02:50:44 +000062
Zhongxing Xue4b6fc22008-10-24 01:38:55 +000063 Store Remove(Store store, Loc LV) {
64 // FIXME: Implement.
65 return store;
66 }
67
Zhongxing Xu79c57f82008-10-08 02:50:44 +000068 Store getInitialStore();
69
Zhongxing Xue4b6fc22008-10-24 01:38:55 +000070 Store RemoveDeadBindings(Store store, Stmt* Loc, const LiveVariables& Live,
71 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots,
72 LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols) {
73 // FIXME: Implement this.
74 return store;
75 }
76
Zhongxing Xue3954d12008-10-21 05:29:26 +000077 Store AddDecl(Store store, const VarDecl* VD, Expr* Ex, SVal InitVal,
78 unsigned Count);
79
80 Loc getVarLoc(const VarDecl* VD) {
81 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
82 }
83
84 Loc getElementLoc(const VarDecl* VD, SVal Idx);
85
Zhongxing Xu79c57f82008-10-08 02:50:44 +000086 static inline RegionBindingsTy GetRegionBindings(Store store) {
87 return RegionBindingsTy(static_cast<const RegionBindingsTy::TreeTy*>(store));
88 }
Zhongxing Xue4b6fc22008-10-24 01:38:55 +000089
Zhongxing Xu6149e882008-10-24 04:33:15 +000090 void print(Store store, std::ostream& Out, const char* nl, const char *sep);
Zhongxing Xue4b6fc22008-10-24 01:38:55 +000091
92 void iterBindings(Store store, BindingsHandler& f) {
93 // FIXME: Implement.
94 }
Zhongxing Xu79c57f82008-10-08 02:50:44 +000095};
96
97} // end anonymous namespace
98
Ted Kremenekc3803992008-10-24 01:04:59 +000099StoreManager* clang::CreateRegionStoreManager(GRStateManager& StMgr) {
Zhongxing Xue4b6fc22008-10-24 01:38:55 +0000100 return new RegionStoreManager(StMgr);
Ted Kremenekc3803992008-10-24 01:04:59 +0000101}
102
Zhongxing Xue3954d12008-10-21 05:29:26 +0000103Loc RegionStoreManager::getElementLoc(const VarDecl* VD, SVal Idx) {
104 MemRegion* R = MRMgr.getVarRegion(VD);
105 ElementRegion* ER = MRMgr.getElementRegion(Idx, R);
106 return loc::MemRegionVal(ER);
107}
108
Zhongxing Xu6f1b5152008-10-22 13:44:38 +0000109SVal RegionStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) {
110 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
111}
112
113SVal RegionStoreManager::getLValueIvar(const GRState* St, const ObjCIvarDecl* D,
114 SVal Base) {
115 return UnknownVal();
116}
117
118SVal RegionStoreManager::getLValueField(const GRState* St, SVal Base,
119 const FieldDecl* D) {
120 if (Base.isUnknownOrUndef())
121 return Base;
122
123 Loc BaseL = cast<Loc>(Base);
124 const MemRegion* BaseR = 0;
125
126 switch (BaseL.getSubKind()) {
127 case loc::MemRegionKind:
128 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
129 break;
130
131 case loc::SymbolValKind:
132 BaseR = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(&BaseL)->getSymbol());
133 break;
134
135 case loc::GotoLabelKind:
136 case loc::FuncValKind:
137 // These are anormal cases. Flag an undefined value.
138 return UndefinedVal();
139
140 case loc::ConcreteIntKind:
141 case loc::StringLiteralValKind:
142 // While these seem funny, this can happen through casts.
143 // FIXME: What we should return is the field offset. For example,
144 // add the field offset to the integer value. That way funny things
145 // like this work properly: &(((struct foo *) 0xa)->f)
146 return Base;
147
148 default:
149 assert("Unhandled Base.");
150 return Base;
151 }
152
153 return loc::MemRegionVal(MRMgr.getFieldRegion(D, BaseR));
154}
155
Zhongxing Xu0972d0a2008-10-24 01:09:32 +0000156SVal RegionStoreManager::getLValueElement(const GRState* St,
157 SVal Base, SVal Offset) {
158 if (Base.isUnknownOrUndef())
159 return Base;
160
161 loc::MemRegionVal& BaseL = cast<loc::MemRegionVal>(Base);
162
163 // We expect BaseR is an ElementRegion, not a base VarRegion.
164
165 const ElementRegion* ElemR = cast<ElementRegion>(BaseL.getRegion());
166
167 SVal Idx = ElemR->getIndex();
168
169 nonloc::ConcreteInt *CI1, *CI2;
170
171 // Only handle integer indices for now.
172 if ((CI1 = dyn_cast<nonloc::ConcreteInt>(&Idx)) &&
173 (CI2 = dyn_cast<nonloc::ConcreteInt>(&Offset))) {
174 SVal NewIdx = CI1->EvalBinOp(StateMgr.getBasicVals(), BinaryOperator::Add,
175 *CI2);
176 return loc::MemRegionVal(MRMgr.getElementRegion(NewIdx,
177 ElemR->getSuperRegion()));
178 }
179
180 return UnknownVal();
181}
182
183// Cast 'pointer to array' to 'pointer to the first element of array'.
184
185SVal RegionStoreManager::ArrayToPointer(SVal Array) {
186 const MemRegion* ArrayR = cast<loc::MemRegionVal>(&Array)->getRegion();
187
188 const VarDecl* D = cast<VarRegion>(ArrayR)->getDecl();
189
190 if (const ConstantArrayType* CAT =
191 dyn_cast<ConstantArrayType>(D->getType().getTypePtr())) {
192
193 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
194
195 nonloc::ConcreteInt Idx(BasicVals.getValue(0, CAT->getSize().getBitWidth(),
196 false));
197
198 ElementRegion* ER = MRMgr.getElementRegion(Idx, ArrayR);
199
200 return loc::MemRegionVal(ER);
201 }
202
203 return Array;
204}
205
Zhongxing Xu73249322008-10-21 06:27:32 +0000206SVal RegionStoreManager::Retrieve(Store S, Loc L, QualType T) {
Zhongxing Xue3954d12008-10-21 05:29:26 +0000207 assert(!isa<UnknownVal>(L) && "location unknown");
208 assert(!isa<UndefinedVal>(L) && "location undefined");
209
210 switch (L.getSubKind()) {
211 case loc::MemRegionKind: {
212 const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion();
213 assert(R && "bad region");
214
215 RegionBindingsTy B(static_cast<const RegionBindingsTy::TreeTy*>(S));
216 RegionBindingsTy::data_type* V = B.lookup(R);
217 return V ? *V : UnknownVal();
218 }
219
220 case loc::SymbolValKind:
221 return UnknownVal();
222
223 case loc::ConcreteIntKind:
224 return UndefinedVal(); // As in BasicStoreManager.
225
226 case loc::FuncValKind:
227 return L;
228
229 case loc::StringLiteralValKind:
230 return UnknownVal();
231
232 default:
233 assert(false && "Invalid Location");
234 break;
235 }
236}
237
Zhongxing Xu73249322008-10-21 06:27:32 +0000238Store RegionStoreManager::Bind(Store store, Loc LV, SVal V) {
Zhongxing Xu097fc982008-10-17 05:57:07 +0000239 assert(LV.getSubKind() == loc::MemRegionKind);
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000240
Ted Kremenek38a4b4b2008-10-17 20:28:54 +0000241 const MemRegion* R = cast<loc::MemRegionVal>(LV).getRegion();
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000242
243 if (!R)
244 return store;
245
246 RegionBindingsTy B = GetRegionBindings(store);
247 return V.isUnknown()
248 ? RBFactory.Remove(B, R).getRoot()
249 : RBFactory.Add(B, R, V).getRoot();
250}
251
252Store RegionStoreManager::getInitialStore() {
253 typedef LiveVariables::AnalysisDataTy LVDataTy;
254 LVDataTy& D = StateMgr.getLiveVariables().getAnalysisData();
255
256 Store St = RBFactory.GetEmptyMap().getRoot();
257
258 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
Douglas Gregord2baafd2008-10-21 16:13:35 +0000259 NamedDecl* ND = const_cast<NamedDecl*>(I->first);
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000260
Douglas Gregord2baafd2008-10-21 16:13:35 +0000261 if (VarDecl* VD = dyn_cast<VarDecl>(ND)) {
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000262 // Punt on static variables for now.
263 if (VD->getStorageClass() == VarDecl::Static)
264 continue;
265
266 QualType T = VD->getType();
267 // Only handle pointers and integers for now.
Zhongxing Xu097fc982008-10-17 05:57:07 +0000268 if (Loc::IsLocType(T) || T->isIntegerType()) {
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000269 // Initialize globals and parameters to symbolic values.
270 // Initialize local variables to undefined.
Zhongxing Xu097fc982008-10-17 05:57:07 +0000271 SVal X = (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) ||
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000272 isa<ImplicitParamDecl>(VD))
Zhongxing Xu097fc982008-10-17 05:57:07 +0000273 ? SVal::GetSymbolValue(StateMgr.getSymbolManager(), VD)
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000274 : UndefinedVal();
275
Zhongxing Xu73249322008-10-21 06:27:32 +0000276 St = Bind(St, getVarLoc(VD), X);
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000277 }
278 }
279 }
280 return St;
281}
Zhongxing Xue3954d12008-10-21 05:29:26 +0000282
283Store RegionStoreManager::AddDecl(Store store,
284 const VarDecl* VD, Expr* Ex,
285 SVal InitVal, unsigned Count) {
286 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
287 SymbolManager& SymMgr = StateMgr.getSymbolManager();
288
289 if (VD->hasGlobalStorage()) {
290 // Static global variables should not be visited here.
291 assert(!(VD->getStorageClass() == VarDecl::Static &&
292 VD->isFileVarDecl()));
293 // Process static variables.
294 if (VD->getStorageClass() == VarDecl::Static) {
295 if (!Ex) {
296 // Only handle pointer and integer static variables.
297
298 QualType T = VD->getType();
299
300 if (Loc::IsLocType(T))
Zhongxing Xu73249322008-10-21 06:27:32 +0000301 store = Bind(store, getVarLoc(VD),
302 loc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xue3954d12008-10-21 05:29:26 +0000303
304 else if (T->isIntegerType())
Zhongxing Xu73249322008-10-21 06:27:32 +0000305 store = Bind(store, getVarLoc(VD),
306 loc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xue3954d12008-10-21 05:29:26 +0000307 else
308 assert("ignore other types of variables");
309 } else {
Zhongxing Xu73249322008-10-21 06:27:32 +0000310 store = Bind(store, getVarLoc(VD), InitVal);
Zhongxing Xue3954d12008-10-21 05:29:26 +0000311 }
312 }
313 } else {
314 // Process local variables.
315
316 QualType T = VD->getType();
317
318 if (Loc::IsLocType(T) || T->isIntegerType()) {
319 SVal V = Ex ? InitVal : UndefinedVal();
320 if (Ex && InitVal.isUnknown()) {
321 // "Conjured" symbols.
322 SymbolID Sym = SymMgr.getConjuredSymbol(Ex, Count);
323 V = Loc::IsLocType(Ex->getType())
324 ? cast<SVal>(loc::SymbolVal(Sym))
325 : cast<SVal>(nonloc::SymbolVal(Sym));
326 }
Zhongxing Xu73249322008-10-21 06:27:32 +0000327 store = Bind(store, getVarLoc(VD), V);
Zhongxing Xue3954d12008-10-21 05:29:26 +0000328
329 } else if (T->isArrayType()) {
330 // Only handle constant size array.
331 if (ConstantArrayType* CAT=dyn_cast<ConstantArrayType>(T.getTypePtr())) {
332
333 llvm::APInt Size = CAT->getSize();
334
335 for (llvm::APInt i = llvm::APInt::getNullValue(Size.getBitWidth());
336 i != Size; ++i) {
337 nonloc::ConcreteInt Idx(BasicVals.getValue(llvm::APSInt(i)));
Zhongxing Xu73249322008-10-21 06:27:32 +0000338 store = Bind(store, getElementLoc(VD, Idx), UndefinedVal());
Zhongxing Xue3954d12008-10-21 05:29:26 +0000339 }
340 }
341 } else if (T->isStructureType()) {
342 // FIXME: Implement struct initialization.
343 }
344 }
345 return store;
346}
347
Zhongxing Xuca892b82008-10-24 06:01:33 +0000348void RegionStoreManager::print(Store store, std::ostream& Out,
349 const char* nl, const char *sep) {
350 llvm::raw_os_ostream OS(Out);
351 RegionBindingsTy B = GetRegionBindings(store);
352 OS << "Store:" << nl;
353
354 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
355 OS << ' '; I.getKey()->print(OS); OS << " : ";
356 I.getData().print(OS); OS << nl;
357 }
Zhongxing Xu6149e882008-10-24 04:33:15 +0000358}