blob: ad6bd7e4f664d43a6594631d0d2ff841cb104461 [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) {
39 if (const RecordType *RT = Ty->getAsRecordType()) {
40 const RecordDecl *D = RT->getDecl();
41 if (!D->getDefinition(Ctx))
42 return false;
43 }
44
45 return true;
46}
47
Ted Kremenek3f9811b2009-07-10 21:11:16 +000048static bool isVoidOrHigherOrderVoidPtr(ASTContext &Ctx, QualType Ty) {
49 while (true) {
50 Ty = Ctx.getCanonicalType(Ty);
51
52 if (Ty->isVoidType())
53 return true;
54
55 if (const PointerType *PT = Ty->getAsPointerType()) {
56 Ty = PT->getPointeeType();
57 continue;
58 }
59
60 break;
61 }
62
63 return false;
64}
65
Ted Kremenek411af402009-07-06 22:23:45 +000066StoreManager::CastResult
Ted Kremenek48ce7de2009-07-06 20:21:51 +000067StoreManager::NewCastRegion(const GRState *state, const MemRegion* R,
68 QualType CastToTy) {
69
70 ASTContext& Ctx = StateMgr.getContext();
71
72 // We need to know the real type of CastToTy.
73 QualType ToTy = Ctx.getCanonicalType(CastToTy);
Ted Kremenek411af402009-07-06 22:23:45 +000074
Ted Kremenekb9a44252009-07-06 22:39:40 +000075 // Handle casts to Objective-C objects.
Ted Kremenekfc8f57c2009-07-06 22:56:37 +000076 if (Ctx.isObjCObjectPointerType(CastToTy)) {
77 state = setCastType(state, R, CastToTy);
Ted Kremenek145918c2009-07-06 21:01:16 +000078 return CastResult(state, R);
79 }
Ted Kremenek411af402009-07-06 22:23:45 +000080
Ted Kremenek48ce7de2009-07-06 20:21:51 +000081 // Now assume we are casting from pointer to pointer. Other cases should
82 // already be handled.
Ted Kremenekfc8f57c2009-07-06 22:56:37 +000083 QualType PointeeTy = CastToTy->getAsPointerType()->getPointeeType();
Ted Kremenek169077d2009-07-06 23:47:19 +000084
Ted Kremenek3f9811b2009-07-10 21:11:16 +000085 // Casts to 'void*', 'void**', 'void***', etc., should just pass through.
86 if (isVoidOrHigherOrderVoidPtr(Ctx, PointeeTy))
87 return CastResult(state, R);
88
Ted Kremenek48ce7de2009-07-06 20:21:51 +000089 // Process region cast according to the kind of the region being cast.
Ted Kremenekfc8f57c2009-07-06 22:56:37 +000090 switch (R->getKind()) {
91 case MemRegion::BEG_TYPED_REGIONS:
92 case MemRegion::MemSpaceRegionKind:
93 case MemRegion::BEG_DECL_REGIONS:
94 case MemRegion::END_DECL_REGIONS:
95 case MemRegion::END_TYPED_REGIONS:
96 case MemRegion::TypedViewRegionKind: {
97 assert(0 && "Invalid region cast");
98 break;
Ted Kremenek48ce7de2009-07-06 20:21:51 +000099 }
Ted Kremenekfc8f57c2009-07-06 22:56:37 +0000100
101 case MemRegion::CodeTextRegionKind: {
102 // CodeTextRegion should be cast to only function pointer type.
Ted Kremenek3f9811b2009-07-10 21:11:16 +0000103 assert(CastToTy->isFunctionPointerType() ||
104 CastToTy->isBlockPointerType());
Ted Kremenekfc8f57c2009-07-06 22:56:37 +0000105 break;
106 }
107
108 case MemRegion::StringRegionKind:
109 // Handle casts of string literals.
110 return MakeElementRegion(state, R, PointeeTy, CastToTy);
111
112 case MemRegion::ObjCObjectRegionKind:
113 case MemRegion::SymbolicRegionKind:
114 // FIXME: Need to handle arbitrary downcasts.
115 case MemRegion::AllocaRegionKind: {
116 state = setCastType(state, R, CastToTy);
117 break;
118 }
119
120 case MemRegion::CompoundLiteralRegionKind:
121 case MemRegion::ElementRegionKind:
122 case MemRegion::FieldRegionKind:
123 case MemRegion::ObjCIvarRegionKind:
124 case MemRegion::VarRegionKind: {
Ted Kremenekdb5f2662009-07-06 22:59:23 +0000125 // VarRegion, ElementRegion, and FieldRegion has an inherent type.
126 // Normally they should not be cast. We only layer an ElementRegion when
127 // the cast-to pointee type is of smaller size. In other cases, we return
128 // the original VarRegion.
Ted Kremenekfc8f57c2009-07-06 22:56:37 +0000129
Zhongxing Xudb4f5312009-07-07 01:36:53 +0000130 // If the pointee or object type is incomplete, do not compute their
131 // sizes, and return the original region.
Ted Kremenekfc8f57c2009-07-06 22:56:37 +0000132 QualType ObjTy = cast<TypedRegion>(R)->getValueType(Ctx);
Ted Kremenek169077d2009-07-06 23:47:19 +0000133
134 if (!IsCompleteType(Ctx, PointeeTy) || !IsCompleteType(Ctx, ObjTy)) {
135 state = setCastType(state, R, ToTy);
136 break;
137 }
138
Ted Kremenekfc8f57c2009-07-06 22:56:37 +0000139 uint64_t PointeeTySize = Ctx.getTypeSize(PointeeTy);
140 uint64_t ObjTySize = Ctx.getTypeSize(ObjTy);
141
142 if ((PointeeTySize > 0 && PointeeTySize < ObjTySize) ||
143 (ObjTy->isAggregateType() && PointeeTy->isScalarType()) ||
144 ObjTySize == 0 /* R has 'void*' type. */)
145 return MakeElementRegion(state, R, PointeeTy, ToTy);
146
Ted Kremenek48ce7de2009-07-06 20:21:51 +0000147 state = setCastType(state, R, ToTy);
Ted Kremenekfc8f57c2009-07-06 22:56:37 +0000148 break;
Ted Kremenek48ce7de2009-07-06 20:21:51 +0000149 }
150 }
151
Ted Kremenekfc8f57c2009-07-06 22:56:37 +0000152 return CastResult(state, R);
Ted Kremenek48ce7de2009-07-06 20:21:51 +0000153}
154
155
156StoreManager::CastResult
157StoreManager::OldCastRegion(const GRState* state, const MemRegion* R,
Ted Kremenekfd6b4f32009-05-04 06:35:49 +0000158 QualType CastToTy) {
Ted Kremenekc62abc12009-04-21 21:51:34 +0000159
Ted Kremenek30d1b992009-04-21 23:31:46 +0000160 ASTContext& Ctx = StateMgr.getContext();
161
162 // We need to know the real type of CastToTy.
163 QualType ToTy = Ctx.getCanonicalType(CastToTy);
164
Ted Kremenekc62abc12009-04-21 21:51:34 +0000165 // Return the same region if the region types are compatible.
166 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R)) {
Zhongxing Xuff697822009-05-09 00:50:33 +0000167 QualType Ta = Ctx.getCanonicalType(TR->getLocationType(Ctx));
Ted Kremenek30d1b992009-04-21 23:31:46 +0000168
169 if (Ta == ToTy)
Ted Kremenekc62abc12009-04-21 21:51:34 +0000170 return CastResult(state, R);
171 }
172
Ted Kremenekfd6b4f32009-05-04 06:35:49 +0000173 if (const PointerType* PTy = dyn_cast<PointerType>(ToTy.getTypePtr())) {
174 // Check if we are casting to 'void*'.
175 // FIXME: Handle arbitrary upcasts.
176 QualType Pointee = PTy->getPointeeType();
177 if (Pointee->isVoidType()) {
Ted Kremenekca4e1b72009-07-06 20:53:52 +0000178 while (true) {
Ted Kremenek42530512009-05-06 18:19:24 +0000179 if (const TypedViewRegion *TR = dyn_cast<TypedViewRegion>(R)) {
180 // Casts to void* removes TypedViewRegion. This happens when:
181 //
182 // void foo(void*);
183 // ...
184 // void bar() {
185 // int x;
186 // foo(&x);
187 // }
188 //
189 R = TR->removeViews();
190 continue;
191 }
192 else if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
193 // Casts to void* also removes ElementRegions. This happens when:
194 //
195 // void foo(void*);
196 // ...
197 // void bar() {
198 // int x;
199 // foo((char*)&x);
200 // }
201 //
202 R = ER->getSuperRegion();
203 continue;
204 }
Ted Kremenek145918c2009-07-06 21:01:16 +0000205
206 break;
Ted Kremenek42530512009-05-06 18:19:24 +0000207 }
Ted Kremenek30d1b992009-04-21 23:31:46 +0000208
209 return CastResult(state, R);
210 }
Ted Kremenekfd6b4f32009-05-04 06:35:49 +0000211 else if (Pointee->isIntegerType()) {
212 // FIXME: At some point, it stands to reason that this 'dyn_cast' should
213 // become a 'cast' and that 'R' will always be a TypedRegion.
214 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) {
215 // Check if we are casting to a region with an integer type. We now
216 // the types aren't the same, so we construct an ElementRegion.
Ted Kremenekcd9392f2009-05-04 15:17:38 +0000217 SVal Idx = ValMgr.makeZeroArrayIndex();
Ted Kremenek20bd7462009-05-04 07:04:36 +0000218
219 // If the super region is an element region, strip it away.
220 // FIXME: Is this the right thing to do in all cases?
Ted Kremenek19cfa2b2009-06-30 22:31:23 +0000221 const MemRegion *Base = isa<ElementRegion>(TR) ? TR->getSuperRegion()
222 : TR;
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000223 ElementRegion* ER = MRMgr.getElementRegion(Pointee, Idx, Base,
Ted Kremenek19cfa2b2009-06-30 22:31:23 +0000224 StateMgr.getContext());
Ted Kremenekfd6b4f32009-05-04 06:35:49 +0000225 return CastResult(state, ER);
226 }
227 }
228 }
Ted Kremenek30d1b992009-04-21 23:31:46 +0000229
Ted Kremeneka8607d12009-05-01 19:22:20 +0000230 // FIXME: Need to handle arbitrary downcasts.
231 // FIXME: Handle the case where a TypedViewRegion (layering a SymbolicRegion
232 // or an AllocaRegion is cast to another view, thus causing the memory
233 // to be re-used for a different purpose.
Ted Kremeneka8607d12009-05-01 19:22:20 +0000234 if (isa<SymbolicRegion>(R) || isa<AllocaRegion>(R)) {
235 const MemRegion* ViewR = MRMgr.getTypedViewRegion(CastToTy, R);
236 return CastResult(AddRegionView(state, ViewR, R), ViewR);
237 }
238
239 return CastResult(state, R);
Ted Kremenekc62abc12009-04-21 21:51:34 +0000240}
Zhongxing Xu43e2aaf2009-07-06 03:41:27 +0000241
242const GRState *StoreManager::InvalidateRegion(const GRState *state,
Zhongxing Xu313b6da2009-07-06 06:01:24 +0000243 const MemRegion *R,
Zhongxing Xu43e2aaf2009-07-06 03:41:27 +0000244 const Expr *E, unsigned Count) {
Zhongxing Xu313b6da2009-07-06 06:01:24 +0000245 ASTContext& Ctx = StateMgr.getContext();
246
Zhongxing Xu43e2aaf2009-07-06 03:41:27 +0000247 if (!R->isBoundable())
248 return state;
249
Zhongxing Xu313b6da2009-07-06 06:01:24 +0000250 if (isa<AllocaRegion>(R) || isa<SymbolicRegion>(R)) {
251 // Invalidate the alloca region by setting its default value to
252 // conjured symbol. The type of the symbol is irrelavant.
253 SVal V = ValMgr.getConjuredSymbolVal(E, Ctx.IntTy, Count);
254 state = setDefaultValue(state, R, V);
255 return state;
256 }
257
258 const TypedRegion *TR = cast<TypedRegion>(R);
259
260 QualType T = TR->getValueType(Ctx);
Zhongxing Xu43e2aaf2009-07-06 03:41:27 +0000261
262 if (Loc::IsLocType(T) || (T->isIntegerType() && T->isScalarType())) {
263 SVal V = ValMgr.getConjuredSymbolVal(E, T, Count);
Zhongxing Xu313b6da2009-07-06 06:01:24 +0000264 return Bind(state, ValMgr.makeLoc(TR), V);
Zhongxing Xu43e2aaf2009-07-06 03:41:27 +0000265 }
266 else if (const RecordType *RT = T->getAsStructureType()) {
267 // FIXME: handle structs with default region value.
268 const RecordDecl *RD = RT->getDecl()->getDefinition(Ctx);
269
270 // No record definition. There is nothing we can do.
271 if (!RD)
272 return state;
273
274 // Iterate through the fields and construct new symbols.
275 for (RecordDecl::field_iterator FI=RD->field_begin(),
276 FE=RD->field_end(); FI!=FE; ++FI) {
277
278 // For now just handle scalar fields.
279 FieldDecl *FD = *FI;
280 QualType FT = FD->getType();
Zhongxing Xu313b6da2009-07-06 06:01:24 +0000281 const FieldRegion* FR = MRMgr.getFieldRegion(FD, TR);
Zhongxing Xu43e2aaf2009-07-06 03:41:27 +0000282
283 if (Loc::IsLocType(FT) ||
284 (FT->isIntegerType() && FT->isScalarType())) {
285 SVal V = ValMgr.getConjuredSymbolVal(E, FT, Count);
286 state = state->bindLoc(ValMgr.makeLoc(FR), V);
287 }
288 else if (FT->isStructureType()) {
289 // set the default value of the struct field to conjured
290 // symbol. Note that the type of the symbol is irrelavant.
291 // We cannot use the type of the struct otherwise ValMgr won't
292 // give us the conjured symbol.
293 SVal V = ValMgr.getConjuredSymbolVal(E, Ctx.IntTy, Count);
294 state = setDefaultValue(state, FR, V);
295 }
296 }
297 } else if (const ArrayType *AT = Ctx.getAsArrayType(T)) {
298 // Set the default value of the array to conjured symbol.
299 SVal V = ValMgr.getConjuredSymbolVal(E, AT->getElementType(),
300 Count);
Zhongxing Xu313b6da2009-07-06 06:01:24 +0000301 state = setDefaultValue(state, TR, V);
Zhongxing Xu43e2aaf2009-07-06 03:41:27 +0000302 } else {
303 // Just blast away other values.
Zhongxing Xu313b6da2009-07-06 06:01:24 +0000304 state = Bind(state, ValMgr.makeLoc(TR), UnknownVal());
Zhongxing Xu43e2aaf2009-07-06 03:41:27 +0000305 }
306
307 return state;
308}