blob: b939a0df9ca72a7346367a6ad7318669ea764c41 [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 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 Kremenek411af402009-07-06 22:23:45 +000062
Ted Kremenek48ce7de2009-07-06 20:21:51 +000063 // Now assume we are casting from pointer to pointer. Other cases should
64 // already be handled.
Ted Kremenekfc8f57c2009-07-06 22:56:37 +000065 QualType PointeeTy = CastToTy->getAsPointerType()->getPointeeType();
Ted Kremenek169077d2009-07-06 23:47:19 +000066
Ted Kremenek48ce7de2009-07-06 20:21:51 +000067 // Process region cast according to the kind of the region being cast.
Ted Kremenekfc8f57c2009-07-06 22:56:37 +000068 switch (R->getKind()) {
69 case MemRegion::BEG_TYPED_REGIONS:
70 case MemRegion::MemSpaceRegionKind:
71 case MemRegion::BEG_DECL_REGIONS:
72 case MemRegion::END_DECL_REGIONS:
73 case MemRegion::END_TYPED_REGIONS:
74 case MemRegion::TypedViewRegionKind: {
75 assert(0 && "Invalid region cast");
76 break;
Ted Kremenek48ce7de2009-07-06 20:21:51 +000077 }
Ted Kremenekfc8f57c2009-07-06 22:56:37 +000078
79 case MemRegion::CodeTextRegionKind: {
Ted Kremenek8d344ae2009-07-10 21:24:45 +000080 // CodeTextRegion should be cast to only function pointer type, although
81 // they can in practice be casted to anything, e.g, void*, char*, etc.
82 // Just pass the region through.
Ted Kremenekfc8f57c2009-07-06 22:56:37 +000083 break;
84 }
85
86 case MemRegion::StringRegionKind:
87 // Handle casts of string literals.
88 return MakeElementRegion(state, R, PointeeTy, CastToTy);
89
90 case MemRegion::ObjCObjectRegionKind:
91 case MemRegion::SymbolicRegionKind:
92 // FIXME: Need to handle arbitrary downcasts.
93 case MemRegion::AllocaRegionKind: {
94 state = setCastType(state, R, CastToTy);
95 break;
96 }
97
98 case MemRegion::CompoundLiteralRegionKind:
99 case MemRegion::ElementRegionKind:
100 case MemRegion::FieldRegionKind:
101 case MemRegion::ObjCIvarRegionKind:
102 case MemRegion::VarRegionKind: {
Ted Kremenekdb5f2662009-07-06 22:59:23 +0000103 // VarRegion, ElementRegion, and FieldRegion has an inherent type.
104 // Normally they should not be cast. We only layer an ElementRegion when
105 // the cast-to pointee type is of smaller size. In other cases, we return
106 // the original VarRegion.
Ted Kremenekfc8f57c2009-07-06 22:56:37 +0000107
Zhongxing Xudb4f5312009-07-07 01:36:53 +0000108 // If the pointee or object type is incomplete, do not compute their
109 // sizes, and return the original region.
Ted Kremenekfc8f57c2009-07-06 22:56:37 +0000110 QualType ObjTy = cast<TypedRegion>(R)->getValueType(Ctx);
Ted Kremenek169077d2009-07-06 23:47:19 +0000111
112 if (!IsCompleteType(Ctx, PointeeTy) || !IsCompleteType(Ctx, ObjTy)) {
113 state = setCastType(state, R, ToTy);
114 break;
115 }
116
Ted Kremenekfc8f57c2009-07-06 22:56:37 +0000117 uint64_t PointeeTySize = Ctx.getTypeSize(PointeeTy);
118 uint64_t ObjTySize = Ctx.getTypeSize(ObjTy);
119
120 if ((PointeeTySize > 0 && PointeeTySize < ObjTySize) ||
121 (ObjTy->isAggregateType() && PointeeTy->isScalarType()) ||
122 ObjTySize == 0 /* R has 'void*' type. */)
123 return MakeElementRegion(state, R, PointeeTy, ToTy);
124
Ted Kremenek48ce7de2009-07-06 20:21:51 +0000125 state = setCastType(state, R, ToTy);
Ted Kremenekfc8f57c2009-07-06 22:56:37 +0000126 break;
Ted Kremenek48ce7de2009-07-06 20:21:51 +0000127 }
128 }
129
Ted Kremenekfc8f57c2009-07-06 22:56:37 +0000130 return CastResult(state, R);
Ted Kremenek48ce7de2009-07-06 20:21:51 +0000131}
132
133
134StoreManager::CastResult
135StoreManager::OldCastRegion(const GRState* state, const MemRegion* R,
Ted Kremenekfd6b4f32009-05-04 06:35:49 +0000136 QualType CastToTy) {
Ted Kremenekc62abc12009-04-21 21:51:34 +0000137
Ted Kremenek30d1b992009-04-21 23:31:46 +0000138 ASTContext& Ctx = StateMgr.getContext();
139
140 // We need to know the real type of CastToTy.
141 QualType ToTy = Ctx.getCanonicalType(CastToTy);
142
Ted Kremenekc62abc12009-04-21 21:51:34 +0000143 // Return the same region if the region types are compatible.
144 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R)) {
Zhongxing Xuff697822009-05-09 00:50:33 +0000145 QualType Ta = Ctx.getCanonicalType(TR->getLocationType(Ctx));
Ted Kremenek30d1b992009-04-21 23:31:46 +0000146
147 if (Ta == ToTy)
Ted Kremenekc62abc12009-04-21 21:51:34 +0000148 return CastResult(state, R);
149 }
150
Ted Kremenekfd6b4f32009-05-04 06:35:49 +0000151 if (const PointerType* PTy = dyn_cast<PointerType>(ToTy.getTypePtr())) {
152 // Check if we are casting to 'void*'.
153 // FIXME: Handle arbitrary upcasts.
154 QualType Pointee = PTy->getPointeeType();
155 if (Pointee->isVoidType()) {
Ted Kremenekca4e1b72009-07-06 20:53:52 +0000156 while (true) {
Ted Kremenek42530512009-05-06 18:19:24 +0000157 if (const TypedViewRegion *TR = dyn_cast<TypedViewRegion>(R)) {
158 // Casts to void* removes TypedViewRegion. This happens when:
159 //
160 // void foo(void*);
161 // ...
162 // void bar() {
163 // int x;
164 // foo(&x);
165 // }
166 //
167 R = TR->removeViews();
168 continue;
169 }
170 else if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
171 // Casts to void* also removes ElementRegions. This happens when:
172 //
173 // void foo(void*);
174 // ...
175 // void bar() {
176 // int x;
177 // foo((char*)&x);
178 // }
179 //
180 R = ER->getSuperRegion();
181 continue;
182 }
Ted Kremenek145918c2009-07-06 21:01:16 +0000183
184 break;
Ted Kremenek42530512009-05-06 18:19:24 +0000185 }
Ted Kremenek30d1b992009-04-21 23:31:46 +0000186
187 return CastResult(state, R);
188 }
Ted Kremenekfd6b4f32009-05-04 06:35:49 +0000189 else if (Pointee->isIntegerType()) {
190 // FIXME: At some point, it stands to reason that this 'dyn_cast' should
191 // become a 'cast' and that 'R' will always be a TypedRegion.
192 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) {
193 // Check if we are casting to a region with an integer type. We now
194 // the types aren't the same, so we construct an ElementRegion.
Ted Kremenekcd9392f2009-05-04 15:17:38 +0000195 SVal Idx = ValMgr.makeZeroArrayIndex();
Ted Kremenek20bd7462009-05-04 07:04:36 +0000196
197 // If the super region is an element region, strip it away.
198 // FIXME: Is this the right thing to do in all cases?
Ted Kremenek19cfa2b2009-06-30 22:31:23 +0000199 const MemRegion *Base = isa<ElementRegion>(TR) ? TR->getSuperRegion()
200 : TR;
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000201 ElementRegion* ER = MRMgr.getElementRegion(Pointee, Idx, Base,
Ted Kremenek19cfa2b2009-06-30 22:31:23 +0000202 StateMgr.getContext());
Ted Kremenekfd6b4f32009-05-04 06:35:49 +0000203 return CastResult(state, ER);
204 }
205 }
206 }
Ted Kremenek30d1b992009-04-21 23:31:46 +0000207
Ted Kremeneka8607d12009-05-01 19:22:20 +0000208 // FIXME: Need to handle arbitrary downcasts.
209 // FIXME: Handle the case where a TypedViewRegion (layering a SymbolicRegion
210 // or an AllocaRegion is cast to another view, thus causing the memory
211 // to be re-used for a different purpose.
Ted Kremeneka8607d12009-05-01 19:22:20 +0000212 if (isa<SymbolicRegion>(R) || isa<AllocaRegion>(R)) {
213 const MemRegion* ViewR = MRMgr.getTypedViewRegion(CastToTy, R);
214 return CastResult(AddRegionView(state, ViewR, R), ViewR);
215 }
216
217 return CastResult(state, R);
Ted Kremenekc62abc12009-04-21 21:51:34 +0000218}
Zhongxing Xu43e2aaf2009-07-06 03:41:27 +0000219
220const GRState *StoreManager::InvalidateRegion(const GRState *state,
Zhongxing Xu313b6da2009-07-06 06:01:24 +0000221 const MemRegion *R,
Zhongxing Xu43e2aaf2009-07-06 03:41:27 +0000222 const Expr *E, unsigned Count) {
Zhongxing Xu313b6da2009-07-06 06:01:24 +0000223 ASTContext& Ctx = StateMgr.getContext();
224
Zhongxing Xu43e2aaf2009-07-06 03:41:27 +0000225 if (!R->isBoundable())
226 return state;
227
Ted Kremenekaa8bc7e2009-07-14 23:52:07 +0000228 if (isa<AllocaRegion>(R) || isa<SymbolicRegion>(R)
229 || isa<ObjCObjectRegion>(R)) {
Zhongxing Xu313b6da2009-07-06 06:01:24 +0000230 // Invalidate the alloca region by setting its default value to
231 // conjured symbol. The type of the symbol is irrelavant.
232 SVal V = ValMgr.getConjuredSymbolVal(E, Ctx.IntTy, Count);
233 state = setDefaultValue(state, R, V);
Ted Kremenekaa8bc7e2009-07-14 23:52:07 +0000234
235 // FIXME: This form of invalidation is a little bogus; we actually need
236 // to invalidate all subregions as well.
Zhongxing Xu313b6da2009-07-06 06:01:24 +0000237 return state;
238 }
239
240 const TypedRegion *TR = cast<TypedRegion>(R);
Ted Kremeneka6275a52009-07-15 02:31:43 +0000241 QualType T = TR->getValueType(Ctx);
Zhongxing Xu313b6da2009-07-06 06:01:24 +0000242
Ted Kremeneka6275a52009-07-15 02:31:43 +0000243 // If the region is cast to another type, use that type.
Zhongxing Xu82037252009-07-14 01:12:46 +0000244 if (const QualType *CastTy = getCastType(state, R)) {
245 assert(!(*CastTy)->isObjCObjectPointerType());
Ted Kremeneka6275a52009-07-15 02:31:43 +0000246 QualType NewT = (*CastTy)->getAsPointerType()->getPointeeType();
Zhongxing Xu43e2aaf2009-07-06 03:41:27 +0000247
Ted Kremeneka6275a52009-07-15 02:31:43 +0000248 // The only exception is if the original region had a location type as its
249 // value type we always want to treat the region as binding to a location.
250 // This issue can arise when pointers are casted to integers and back.
Zhongxing Xu6ce85ee2009-07-15 06:21:18 +0000251
252 if (!(Loc::IsLocType(T) && !Loc::IsLocType(NewT)))
Ted Kremeneka6275a52009-07-15 02:31:43 +0000253 T = NewT;
254 }
255
Zhongxing Xu43e2aaf2009-07-06 03:41:27 +0000256 if (Loc::IsLocType(T) || (T->isIntegerType() && T->isScalarType())) {
257 SVal V = ValMgr.getConjuredSymbolVal(E, T, Count);
Zhongxing Xu313b6da2009-07-06 06:01:24 +0000258 return Bind(state, ValMgr.makeLoc(TR), V);
Zhongxing Xu43e2aaf2009-07-06 03:41:27 +0000259 }
260 else if (const RecordType *RT = T->getAsStructureType()) {
261 // FIXME: handle structs with default region value.
262 const RecordDecl *RD = RT->getDecl()->getDefinition(Ctx);
263
264 // No record definition. There is nothing we can do.
265 if (!RD)
266 return state;
267
268 // Iterate through the fields and construct new symbols.
269 for (RecordDecl::field_iterator FI=RD->field_begin(),
270 FE=RD->field_end(); FI!=FE; ++FI) {
271
272 // For now just handle scalar fields.
273 FieldDecl *FD = *FI;
274 QualType FT = FD->getType();
Zhongxing Xu313b6da2009-07-06 06:01:24 +0000275 const FieldRegion* FR = MRMgr.getFieldRegion(FD, TR);
Zhongxing Xu43e2aaf2009-07-06 03:41:27 +0000276
277 if (Loc::IsLocType(FT) ||
278 (FT->isIntegerType() && FT->isScalarType())) {
279 SVal V = ValMgr.getConjuredSymbolVal(E, FT, Count);
280 state = state->bindLoc(ValMgr.makeLoc(FR), V);
281 }
282 else if (FT->isStructureType()) {
283 // set the default value of the struct field to conjured
284 // symbol. Note that the type of the symbol is irrelavant.
285 // We cannot use the type of the struct otherwise ValMgr won't
286 // give us the conjured symbol.
287 SVal V = ValMgr.getConjuredSymbolVal(E, Ctx.IntTy, Count);
288 state = setDefaultValue(state, FR, V);
289 }
290 }
291 } else if (const ArrayType *AT = Ctx.getAsArrayType(T)) {
292 // Set the default value of the array to conjured symbol.
293 SVal V = ValMgr.getConjuredSymbolVal(E, AT->getElementType(),
294 Count);
Zhongxing Xu313b6da2009-07-06 06:01:24 +0000295 state = setDefaultValue(state, TR, V);
Zhongxing Xu43e2aaf2009-07-06 03:41:27 +0000296 } else {
297 // Just blast away other values.
Zhongxing Xu313b6da2009-07-06 06:01:24 +0000298 state = Bind(state, ValMgr.makeLoc(TR), UnknownVal());
Zhongxing Xu43e2aaf2009-07-06 03:41:27 +0000299 }
300
301 return state;
302}