blob: f64ed76be7221b4c486021ce911e5676fe9d025f [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 Xu6f1b5152008-10-22 13:44:38 +000041 SVal getLValueVar(const GRState* St, const VarDecl* VD);
42
43 SVal getLValueIvar(const GRState* St, const ObjCIvarDecl* D, SVal Base);
44
45 SVal getLValueField(const GRState* St, SVal Base, const FieldDecl* D);
46
Zhongxing Xu0972d0a2008-10-24 01:09:32 +000047 SVal getLValueElement(const GRState* St, SVal Base, SVal Offset);
48
49 SVal ArrayToPointer(SVal Array);
50
Zhongxing Xu73249322008-10-21 06:27:32 +000051 SVal Retrieve(Store S, Loc L, QualType T);
Zhongxing Xu6f1b5152008-10-22 13:44:38 +000052
Zhongxing Xu73249322008-10-21 06:27:32 +000053 Store Bind(Store St, Loc LV, SVal V);
Zhongxing Xu79c57f82008-10-08 02:50:44 +000054
55 Store getInitialStore();
56
Zhongxing Xue3954d12008-10-21 05:29:26 +000057 Store AddDecl(Store store, const VarDecl* VD, Expr* Ex, SVal InitVal,
58 unsigned Count);
59
60 Loc getVarLoc(const VarDecl* VD) {
61 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
62 }
63
64 Loc getElementLoc(const VarDecl* VD, SVal Idx);
65
Zhongxing Xu79c57f82008-10-08 02:50:44 +000066 static inline RegionBindingsTy GetRegionBindings(Store store) {
67 return RegionBindingsTy(static_cast<const RegionBindingsTy::TreeTy*>(store));
68 }
69};
70
71} // end anonymous namespace
72
Ted Kremenekc3803992008-10-24 01:04:59 +000073StoreManager* clang::CreateRegionStoreManager(GRStateManager& StMgr) {
74 // return new RegionStoreManager(StMgr);
75 return 0; // Uncomment the above line when RegionStoreManager is not abstract.
76}
77
Zhongxing Xue3954d12008-10-21 05:29:26 +000078Loc RegionStoreManager::getElementLoc(const VarDecl* VD, SVal Idx) {
79 MemRegion* R = MRMgr.getVarRegion(VD);
80 ElementRegion* ER = MRMgr.getElementRegion(Idx, R);
81 return loc::MemRegionVal(ER);
82}
83
Zhongxing Xu6f1b5152008-10-22 13:44:38 +000084SVal RegionStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) {
85 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
86}
87
88SVal RegionStoreManager::getLValueIvar(const GRState* St, const ObjCIvarDecl* D,
89 SVal Base) {
90 return UnknownVal();
91}
92
93SVal RegionStoreManager::getLValueField(const GRState* St, SVal Base,
94 const FieldDecl* D) {
95 if (Base.isUnknownOrUndef())
96 return Base;
97
98 Loc BaseL = cast<Loc>(Base);
99 const MemRegion* BaseR = 0;
100
101 switch (BaseL.getSubKind()) {
102 case loc::MemRegionKind:
103 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
104 break;
105
106 case loc::SymbolValKind:
107 BaseR = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(&BaseL)->getSymbol());
108 break;
109
110 case loc::GotoLabelKind:
111 case loc::FuncValKind:
112 // These are anormal cases. Flag an undefined value.
113 return UndefinedVal();
114
115 case loc::ConcreteIntKind:
116 case loc::StringLiteralValKind:
117 // While these seem funny, this can happen through casts.
118 // FIXME: What we should return is the field offset. For example,
119 // add the field offset to the integer value. That way funny things
120 // like this work properly: &(((struct foo *) 0xa)->f)
121 return Base;
122
123 default:
124 assert("Unhandled Base.");
125 return Base;
126 }
127
128 return loc::MemRegionVal(MRMgr.getFieldRegion(D, BaseR));
129}
130
Zhongxing Xu0972d0a2008-10-24 01:09:32 +0000131SVal RegionStoreManager::getLValueElement(const GRState* St,
132 SVal Base, SVal Offset) {
133 if (Base.isUnknownOrUndef())
134 return Base;
135
136 loc::MemRegionVal& BaseL = cast<loc::MemRegionVal>(Base);
137
138 // We expect BaseR is an ElementRegion, not a base VarRegion.
139
140 const ElementRegion* ElemR = cast<ElementRegion>(BaseL.getRegion());
141
142 SVal Idx = ElemR->getIndex();
143
144 nonloc::ConcreteInt *CI1, *CI2;
145
146 // Only handle integer indices for now.
147 if ((CI1 = dyn_cast<nonloc::ConcreteInt>(&Idx)) &&
148 (CI2 = dyn_cast<nonloc::ConcreteInt>(&Offset))) {
149 SVal NewIdx = CI1->EvalBinOp(StateMgr.getBasicVals(), BinaryOperator::Add,
150 *CI2);
151 return loc::MemRegionVal(MRMgr.getElementRegion(NewIdx,
152 ElemR->getSuperRegion()));
153 }
154
155 return UnknownVal();
156}
157
158// Cast 'pointer to array' to 'pointer to the first element of array'.
159
160SVal RegionStoreManager::ArrayToPointer(SVal Array) {
161 const MemRegion* ArrayR = cast<loc::MemRegionVal>(&Array)->getRegion();
162
163 const VarDecl* D = cast<VarRegion>(ArrayR)->getDecl();
164
165 if (const ConstantArrayType* CAT =
166 dyn_cast<ConstantArrayType>(D->getType().getTypePtr())) {
167
168 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
169
170 nonloc::ConcreteInt Idx(BasicVals.getValue(0, CAT->getSize().getBitWidth(),
171 false));
172
173 ElementRegion* ER = MRMgr.getElementRegion(Idx, ArrayR);
174
175 return loc::MemRegionVal(ER);
176 }
177
178 return Array;
179}
180
Zhongxing Xu73249322008-10-21 06:27:32 +0000181SVal RegionStoreManager::Retrieve(Store S, Loc L, QualType T) {
Zhongxing Xue3954d12008-10-21 05:29:26 +0000182 assert(!isa<UnknownVal>(L) && "location unknown");
183 assert(!isa<UndefinedVal>(L) && "location undefined");
184
185 switch (L.getSubKind()) {
186 case loc::MemRegionKind: {
187 const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion();
188 assert(R && "bad region");
189
190 RegionBindingsTy B(static_cast<const RegionBindingsTy::TreeTy*>(S));
191 RegionBindingsTy::data_type* V = B.lookup(R);
192 return V ? *V : UnknownVal();
193 }
194
195 case loc::SymbolValKind:
196 return UnknownVal();
197
198 case loc::ConcreteIntKind:
199 return UndefinedVal(); // As in BasicStoreManager.
200
201 case loc::FuncValKind:
202 return L;
203
204 case loc::StringLiteralValKind:
205 return UnknownVal();
206
207 default:
208 assert(false && "Invalid Location");
209 break;
210 }
211}
212
Zhongxing Xu73249322008-10-21 06:27:32 +0000213Store RegionStoreManager::Bind(Store store, Loc LV, SVal V) {
Zhongxing Xu097fc982008-10-17 05:57:07 +0000214 assert(LV.getSubKind() == loc::MemRegionKind);
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000215
Ted Kremenek38a4b4b2008-10-17 20:28:54 +0000216 const MemRegion* R = cast<loc::MemRegionVal>(LV).getRegion();
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000217
218 if (!R)
219 return store;
220
221 RegionBindingsTy B = GetRegionBindings(store);
222 return V.isUnknown()
223 ? RBFactory.Remove(B, R).getRoot()
224 : RBFactory.Add(B, R, V).getRoot();
225}
226
227Store RegionStoreManager::getInitialStore() {
228 typedef LiveVariables::AnalysisDataTy LVDataTy;
229 LVDataTy& D = StateMgr.getLiveVariables().getAnalysisData();
230
231 Store St = RBFactory.GetEmptyMap().getRoot();
232
233 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
Douglas Gregord2baafd2008-10-21 16:13:35 +0000234 NamedDecl* ND = const_cast<NamedDecl*>(I->first);
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000235
Douglas Gregord2baafd2008-10-21 16:13:35 +0000236 if (VarDecl* VD = dyn_cast<VarDecl>(ND)) {
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000237 // Punt on static variables for now.
238 if (VD->getStorageClass() == VarDecl::Static)
239 continue;
240
241 QualType T = VD->getType();
242 // Only handle pointers and integers for now.
Zhongxing Xu097fc982008-10-17 05:57:07 +0000243 if (Loc::IsLocType(T) || T->isIntegerType()) {
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000244 // Initialize globals and parameters to symbolic values.
245 // Initialize local variables to undefined.
Zhongxing Xu097fc982008-10-17 05:57:07 +0000246 SVal X = (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) ||
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000247 isa<ImplicitParamDecl>(VD))
Zhongxing Xu097fc982008-10-17 05:57:07 +0000248 ? SVal::GetSymbolValue(StateMgr.getSymbolManager(), VD)
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000249 : UndefinedVal();
250
Zhongxing Xu73249322008-10-21 06:27:32 +0000251 St = Bind(St, getVarLoc(VD), X);
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000252 }
253 }
254 }
255 return St;
256}
Zhongxing Xue3954d12008-10-21 05:29:26 +0000257
258Store RegionStoreManager::AddDecl(Store store,
259 const VarDecl* VD, Expr* Ex,
260 SVal InitVal, unsigned Count) {
261 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
262 SymbolManager& SymMgr = StateMgr.getSymbolManager();
263
264 if (VD->hasGlobalStorage()) {
265 // Static global variables should not be visited here.
266 assert(!(VD->getStorageClass() == VarDecl::Static &&
267 VD->isFileVarDecl()));
268 // Process static variables.
269 if (VD->getStorageClass() == VarDecl::Static) {
270 if (!Ex) {
271 // Only handle pointer and integer static variables.
272
273 QualType T = VD->getType();
274
275 if (Loc::IsLocType(T))
Zhongxing Xu73249322008-10-21 06:27:32 +0000276 store = Bind(store, getVarLoc(VD),
277 loc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xue3954d12008-10-21 05:29:26 +0000278
279 else if (T->isIntegerType())
Zhongxing Xu73249322008-10-21 06:27:32 +0000280 store = Bind(store, getVarLoc(VD),
281 loc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xue3954d12008-10-21 05:29:26 +0000282 else
283 assert("ignore other types of variables");
284 } else {
Zhongxing Xu73249322008-10-21 06:27:32 +0000285 store = Bind(store, getVarLoc(VD), InitVal);
Zhongxing Xue3954d12008-10-21 05:29:26 +0000286 }
287 }
288 } else {
289 // Process local variables.
290
291 QualType T = VD->getType();
292
293 if (Loc::IsLocType(T) || T->isIntegerType()) {
294 SVal V = Ex ? InitVal : UndefinedVal();
295 if (Ex && InitVal.isUnknown()) {
296 // "Conjured" symbols.
297 SymbolID Sym = SymMgr.getConjuredSymbol(Ex, Count);
298 V = Loc::IsLocType(Ex->getType())
299 ? cast<SVal>(loc::SymbolVal(Sym))
300 : cast<SVal>(nonloc::SymbolVal(Sym));
301 }
Zhongxing Xu73249322008-10-21 06:27:32 +0000302 store = Bind(store, getVarLoc(VD), V);
Zhongxing Xue3954d12008-10-21 05:29:26 +0000303
304 } else if (T->isArrayType()) {
305 // Only handle constant size array.
306 if (ConstantArrayType* CAT=dyn_cast<ConstantArrayType>(T.getTypePtr())) {
307
308 llvm::APInt Size = CAT->getSize();
309
310 for (llvm::APInt i = llvm::APInt::getNullValue(Size.getBitWidth());
311 i != Size; ++i) {
312 nonloc::ConcreteInt Idx(BasicVals.getValue(llvm::APSInt(i)));
Zhongxing Xu73249322008-10-21 06:27:32 +0000313 store = Bind(store, getElementLoc(VD, Idx), UndefinedVal());
Zhongxing Xue3954d12008-10-21 05:29:26 +0000314 }
315 }
316 } else if (T->isStructureType()) {
317 // FIXME: Implement struct initialization.
318 }
319 }
320 return store;
321}
322