blob: bd46a68d0135b7d4e1104238b4ad14273e4c9908 [file] [log] [blame]
Ted Kremenekc62abc12009-04-21 21:51:34 +00001//== Store.cpp - Interface for maps 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 types Store and StoreManager.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Analysis/PathSensitive/Store.h"
15#include "clang/Analysis/PathSensitive/GRState.h"
16
17using namespace clang;
18
Ted Kremenek48ce7de2009-07-06 20:21:51 +000019StoreManager::StoreManager(GRStateManager &stateMgr, bool useNewCastRegion)
Ted Kremenekc62abc12009-04-21 21:51:34 +000020 : ValMgr(stateMgr.getValueManager()),
21 StateMgr(stateMgr),
Ted Kremenek48ce7de2009-07-06 20:21:51 +000022 UseNewCastRegion(useNewCastRegion),
Ted Kremenekc62abc12009-04-21 21:51:34 +000023 MRMgr(ValMgr.getRegionManager()) {}
24
25StoreManager::CastResult
Ted Kremenek411af402009-07-06 22:23:45 +000026StoreManager::MakeElementRegion(const GRState *state, const MemRegion *region,
27 QualType pointeeTy, QualType castToTy) {
28
29 // Record the cast type of the region.
30 state = setCastType(state, region, castToTy);
31
32 // Create a new ElementRegion at offset 0.
33 SVal idx = ValMgr.makeZeroArrayIndex();
34 return CastResult(state, MRMgr.getElementRegion(pointeeTy, idx, region,
35 ValMgr.getContext()));
36}
37
Ted Kremenek169077d2009-07-06 23:47:19 +000038static bool IsCompleteType(ASTContext &Ctx, QualType Ty) {
Ted Kremenek35366a62009-07-17 17:50:17 +000039 if (const RecordType *RT = Ty->getAsRecordType()) {
Ted Kremenek169077d2009-07-06 23:47:19 +000040 const RecordDecl *D = RT->getDecl();
41 if (!D->getDefinition(Ctx))
42 return false;
43 }
44
45 return true;
46}
47
Ted Kremenek411af402009-07-06 22:23:45 +000048StoreManager::CastResult
Ted Kremenek48ce7de2009-07-06 20:21:51 +000049StoreManager::NewCastRegion(const GRState *state, const MemRegion* R,
50 QualType CastToTy) {
51
52 ASTContext& Ctx = StateMgr.getContext();
53
54 // We need to know the real type of CastToTy.
55 QualType ToTy = Ctx.getCanonicalType(CastToTy);
Ted Kremenek411af402009-07-06 22:23:45 +000056
Ted Kremenekb9a44252009-07-06 22:39:40 +000057 // Handle casts to Objective-C objects.
Steve Narofff4954562009-07-16 15:41:00 +000058 if (CastToTy->isObjCObjectPointerType()) {
Ted Kremenekfc8f57c2009-07-06 22:56:37 +000059 state = setCastType(state, R, CastToTy);
Ted Kremenek145918c2009-07-06 21:01:16 +000060 return CastResult(state, R);
61 }
Ted Kremenek63b9cfe2009-07-18 06:27:51 +000062
63 if (CastToTy->isBlockPointerType()) {
64 if (isa<CodeTextRegion>(R))
65 return CastResult(state, R);
66
67 // FIXME: This may not be the right approach, depending on the symbol
68 // involved. Blocks can be casted to/from 'id', as they can be treated
69 // as Objective-C objects.
70 if (SymbolRef sym = loc::MemRegionVal(R).getAsSymbol()) {
71 R = MRMgr.getCodeTextRegion(sym, CastToTy);
72 return CastResult(state, R);
73 }
74
75 // We don't know what to make of it. Return a NULL region, which
76 // will be interpretted as UnknownVal.
77 return CastResult(state, NULL);
78 }
Ted Kremenek411af402009-07-06 22:23:45 +000079
Ted Kremenek48ce7de2009-07-06 20:21:51 +000080 // Now assume we are casting from pointer to pointer. Other cases should
81 // already be handled.
Ted Kremenek35366a62009-07-17 17:50:17 +000082 QualType PointeeTy = CastToTy->getAsPointerType()->getPointeeType();
Ted Kremenek169077d2009-07-06 23:47:19 +000083
Ted Kremenek48ce7de2009-07-06 20:21:51 +000084 // Process region cast according to the kind of the region being cast.
Ted Kremenekfc8f57c2009-07-06 22:56:37 +000085 switch (R->getKind()) {
86 case MemRegion::BEG_TYPED_REGIONS:
87 case MemRegion::MemSpaceRegionKind:
88 case MemRegion::BEG_DECL_REGIONS:
89 case MemRegion::END_DECL_REGIONS:
90 case MemRegion::END_TYPED_REGIONS:
91 case MemRegion::TypedViewRegionKind: {
92 assert(0 && "Invalid region cast");
93 break;
Ted Kremenek48ce7de2009-07-06 20:21:51 +000094 }
Ted Kremenekfc8f57c2009-07-06 22:56:37 +000095
96 case MemRegion::CodeTextRegionKind: {
Ted Kremenek63b9cfe2009-07-18 06:27:51 +000097 // CodeTextRegion should be cast to only a function or block pointer type,
98 // although they can in practice be casted to anything, e.g, void*,
99 // char*, etc.
Ted Kremenek8d344ae2009-07-10 21:24:45 +0000100 // Just pass the region through.
Ted Kremenekfc8f57c2009-07-06 22:56:37 +0000101 break;
102 }
103
104 case MemRegion::StringRegionKind:
105 // Handle casts of string literals.
106 return MakeElementRegion(state, R, PointeeTy, CastToTy);
107
108 case MemRegion::ObjCObjectRegionKind:
109 case MemRegion::SymbolicRegionKind:
110 // FIXME: Need to handle arbitrary downcasts.
111 case MemRegion::AllocaRegionKind: {
112 state = setCastType(state, R, CastToTy);
113 break;
114 }
115
116 case MemRegion::CompoundLiteralRegionKind:
117 case MemRegion::ElementRegionKind:
118 case MemRegion::FieldRegionKind:
119 case MemRegion::ObjCIvarRegionKind:
120 case MemRegion::VarRegionKind: {
Ted Kremenekdb5f2662009-07-06 22:59:23 +0000121 // VarRegion, ElementRegion, and FieldRegion has an inherent type.
122 // Normally they should not be cast. We only layer an ElementRegion when
123 // the cast-to pointee type is of smaller size. In other cases, we return
124 // the original VarRegion.
Ted Kremenekfc8f57c2009-07-06 22:56:37 +0000125
Zhongxing Xudb4f5312009-07-07 01:36:53 +0000126 // If the pointee or object type is incomplete, do not compute their
127 // sizes, and return the original region.
Ted Kremenekfc8f57c2009-07-06 22:56:37 +0000128 QualType ObjTy = cast<TypedRegion>(R)->getValueType(Ctx);
Ted Kremenek169077d2009-07-06 23:47:19 +0000129
130 if (!IsCompleteType(Ctx, PointeeTy) || !IsCompleteType(Ctx, ObjTy)) {
131 state = setCastType(state, R, ToTy);
132 break;
133 }
134
Ted Kremenekfc8f57c2009-07-06 22:56:37 +0000135 uint64_t PointeeTySize = Ctx.getTypeSize(PointeeTy);
136 uint64_t ObjTySize = Ctx.getTypeSize(ObjTy);
137
138 if ((PointeeTySize > 0 && PointeeTySize < ObjTySize) ||
139 (ObjTy->isAggregateType() && PointeeTy->isScalarType()) ||
140 ObjTySize == 0 /* R has 'void*' type. */)
141 return MakeElementRegion(state, R, PointeeTy, ToTy);
142
Ted Kremenek48ce7de2009-07-06 20:21:51 +0000143 state = setCastType(state, R, ToTy);
Ted Kremenekfc8f57c2009-07-06 22:56:37 +0000144 break;
Ted Kremenek48ce7de2009-07-06 20:21:51 +0000145 }
146 }
147
Ted Kremenekfc8f57c2009-07-06 22:56:37 +0000148 return CastResult(state, R);
Ted Kremenek48ce7de2009-07-06 20:21:51 +0000149}
150
151
152StoreManager::CastResult
153StoreManager::OldCastRegion(const GRState* state, const MemRegion* R,
Ted Kremenekfd6b4f32009-05-04 06:35:49 +0000154 QualType CastToTy) {
Ted Kremenekc62abc12009-04-21 21:51:34 +0000155
Ted Kremenek30d1b992009-04-21 23:31:46 +0000156 ASTContext& Ctx = StateMgr.getContext();
157
158 // We need to know the real type of CastToTy.
159 QualType ToTy = Ctx.getCanonicalType(CastToTy);
160
Ted Kremenekc62abc12009-04-21 21:51:34 +0000161 // Return the same region if the region types are compatible.
162 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R)) {
Zhongxing Xuff697822009-05-09 00:50:33 +0000163 QualType Ta = Ctx.getCanonicalType(TR->getLocationType(Ctx));
Ted Kremenek30d1b992009-04-21 23:31:46 +0000164
165 if (Ta == ToTy)
Ted Kremenekc62abc12009-04-21 21:51:34 +0000166 return CastResult(state, R);
167 }
168
Ted Kremenekfd6b4f32009-05-04 06:35:49 +0000169 if (const PointerType* PTy = dyn_cast<PointerType>(ToTy.getTypePtr())) {
170 // Check if we are casting to 'void*'.
171 // FIXME: Handle arbitrary upcasts.
172 QualType Pointee = PTy->getPointeeType();
173 if (Pointee->isVoidType()) {
Ted Kremenekca4e1b72009-07-06 20:53:52 +0000174 while (true) {
Ted Kremenek42530512009-05-06 18:19:24 +0000175 if (const TypedViewRegion *TR = dyn_cast<TypedViewRegion>(R)) {
176 // Casts to void* removes TypedViewRegion. This happens when:
177 //
178 // void foo(void*);
179 // ...
180 // void bar() {
181 // int x;
182 // foo(&x);
183 // }
184 //
185 R = TR->removeViews();
186 continue;
187 }
188 else if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
189 // Casts to void* also removes ElementRegions. This happens when:
190 //
191 // void foo(void*);
192 // ...
193 // void bar() {
194 // int x;
195 // foo((char*)&x);
196 // }
197 //
198 R = ER->getSuperRegion();
199 continue;
200 }
Ted Kremenek145918c2009-07-06 21:01:16 +0000201
202 break;
Ted Kremenek42530512009-05-06 18:19:24 +0000203 }
Ted Kremenek30d1b992009-04-21 23:31:46 +0000204
205 return CastResult(state, R);
206 }
Ted Kremenekfd6b4f32009-05-04 06:35:49 +0000207 else if (Pointee->isIntegerType()) {
208 // FIXME: At some point, it stands to reason that this 'dyn_cast' should
209 // become a 'cast' and that 'R' will always be a TypedRegion.
210 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) {
211 // Check if we are casting to a region with an integer type. We now
212 // the types aren't the same, so we construct an ElementRegion.
Ted Kremenekcd9392f2009-05-04 15:17:38 +0000213 SVal Idx = ValMgr.makeZeroArrayIndex();
Ted Kremenek20bd7462009-05-04 07:04:36 +0000214
215 // If the super region is an element region, strip it away.
216 // FIXME: Is this the right thing to do in all cases?
Ted Kremenek19cfa2b2009-06-30 22:31:23 +0000217 const MemRegion *Base = isa<ElementRegion>(TR) ? TR->getSuperRegion()
218 : TR;
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000219 ElementRegion* ER = MRMgr.getElementRegion(Pointee, Idx, Base,
Ted Kremenek19cfa2b2009-06-30 22:31:23 +0000220 StateMgr.getContext());
Ted Kremenekfd6b4f32009-05-04 06:35:49 +0000221 return CastResult(state, ER);
222 }
223 }
224 }
Ted Kremenek30d1b992009-04-21 23:31:46 +0000225
Ted Kremeneka8607d12009-05-01 19:22:20 +0000226 // FIXME: Need to handle arbitrary downcasts.
227 // FIXME: Handle the case where a TypedViewRegion (layering a SymbolicRegion
228 // or an AllocaRegion is cast to another view, thus causing the memory
229 // to be re-used for a different purpose.
Ted Kremeneka8607d12009-05-01 19:22:20 +0000230 if (isa<SymbolicRegion>(R) || isa<AllocaRegion>(R)) {
231 const MemRegion* ViewR = MRMgr.getTypedViewRegion(CastToTy, R);
232 return CastResult(AddRegionView(state, ViewR, R), ViewR);
233 }
234
235 return CastResult(state, R);
Ted Kremenekc62abc12009-04-21 21:51:34 +0000236}
Zhongxing Xu43e2aaf2009-07-06 03:41:27 +0000237
238const GRState *StoreManager::InvalidateRegion(const GRState *state,
Zhongxing Xu313b6da2009-07-06 06:01:24 +0000239 const MemRegion *R,
Zhongxing Xu43e2aaf2009-07-06 03:41:27 +0000240 const Expr *E, unsigned Count) {
Zhongxing Xu313b6da2009-07-06 06:01:24 +0000241 ASTContext& Ctx = StateMgr.getContext();
242
Zhongxing Xu43e2aaf2009-07-06 03:41:27 +0000243 if (!R->isBoundable())
244 return state;
245
Ted Kremenekaa8bc7e2009-07-14 23:52:07 +0000246 if (isa<AllocaRegion>(R) || isa<SymbolicRegion>(R)
247 || isa<ObjCObjectRegion>(R)) {
Zhongxing Xu313b6da2009-07-06 06:01:24 +0000248 // Invalidate the alloca region by setting its default value to
249 // conjured symbol. The type of the symbol is irrelavant.
250 SVal V = ValMgr.getConjuredSymbolVal(E, Ctx.IntTy, Count);
251 state = setDefaultValue(state, R, V);
Ted Kremenekaa8bc7e2009-07-14 23:52:07 +0000252
253 // FIXME: This form of invalidation is a little bogus; we actually need
254 // to invalidate all subregions as well.
Zhongxing Xu313b6da2009-07-06 06:01:24 +0000255 return state;
256 }
257
258 const TypedRegion *TR = cast<TypedRegion>(R);
Ted Kremeneka6275a52009-07-15 02:31:43 +0000259 QualType T = TR->getValueType(Ctx);
Zhongxing Xu313b6da2009-07-06 06:01:24 +0000260
Ted Kremeneka6275a52009-07-15 02:31:43 +0000261 // If the region is cast to another type, use that type.
Zhongxing Xu82037252009-07-14 01:12:46 +0000262 if (const QualType *CastTy = getCastType(state, R)) {
263 assert(!(*CastTy)->isObjCObjectPointerType());
Ted Kremenek35366a62009-07-17 17:50:17 +0000264 QualType NewT = (*CastTy)->getAsPointerType()->getPointeeType();
Zhongxing Xu43e2aaf2009-07-06 03:41:27 +0000265
Ted Kremeneka6275a52009-07-15 02:31:43 +0000266 // The only exception is if the original region had a location type as its
267 // value type we always want to treat the region as binding to a location.
268 // This issue can arise when pointers are casted to integers and back.
Zhongxing Xu6ce85ee2009-07-15 06:21:18 +0000269
270 if (!(Loc::IsLocType(T) && !Loc::IsLocType(NewT)))
Ted Kremeneka6275a52009-07-15 02:31:43 +0000271 T = NewT;
272 }
273
Zhongxing Xu43e2aaf2009-07-06 03:41:27 +0000274 if (Loc::IsLocType(T) || (T->isIntegerType() && T->isScalarType())) {
275 SVal V = ValMgr.getConjuredSymbolVal(E, T, Count);
Zhongxing Xu313b6da2009-07-06 06:01:24 +0000276 return Bind(state, ValMgr.makeLoc(TR), V);
Zhongxing Xu43e2aaf2009-07-06 03:41:27 +0000277 }
278 else if (const RecordType *RT = T->getAsStructureType()) {
279 // FIXME: handle structs with default region value.
280 const RecordDecl *RD = RT->getDecl()->getDefinition(Ctx);
281
282 // No record definition. There is nothing we can do.
283 if (!RD)
284 return state;
285
286 // Iterate through the fields and construct new symbols.
287 for (RecordDecl::field_iterator FI=RD->field_begin(),
288 FE=RD->field_end(); FI!=FE; ++FI) {
289
290 // For now just handle scalar fields.
291 FieldDecl *FD = *FI;
292 QualType FT = FD->getType();
Zhongxing Xu313b6da2009-07-06 06:01:24 +0000293 const FieldRegion* FR = MRMgr.getFieldRegion(FD, TR);
Zhongxing Xu43e2aaf2009-07-06 03:41:27 +0000294
295 if (Loc::IsLocType(FT) ||
296 (FT->isIntegerType() && FT->isScalarType())) {
297 SVal V = ValMgr.getConjuredSymbolVal(E, FT, Count);
298 state = state->bindLoc(ValMgr.makeLoc(FR), V);
299 }
300 else if (FT->isStructureType()) {
301 // set the default value of the struct field to conjured
302 // symbol. Note that the type of the symbol is irrelavant.
303 // We cannot use the type of the struct otherwise ValMgr won't
304 // give us the conjured symbol.
305 SVal V = ValMgr.getConjuredSymbolVal(E, Ctx.IntTy, Count);
306 state = setDefaultValue(state, FR, V);
307 }
308 }
309 } else if (const ArrayType *AT = Ctx.getAsArrayType(T)) {
310 // Set the default value of the array to conjured symbol.
311 SVal V = ValMgr.getConjuredSymbolVal(E, AT->getElementType(),
312 Count);
Zhongxing Xu313b6da2009-07-06 06:01:24 +0000313 state = setDefaultValue(state, TR, V);
Zhongxing Xu43e2aaf2009-07-06 03:41:27 +0000314 } else {
315 // Just blast away other values.
Zhongxing Xu313b6da2009-07-06 06:01:24 +0000316 state = Bind(state, ValMgr.makeLoc(TR), UnknownVal());
Zhongxing Xu43e2aaf2009-07-06 03:41:27 +0000317 }
318
319 return state;
320}