blob: d7633a46ac9dc7b37e2d4b844ab7ba3c26f525a4 [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.
Ted Kremenekfc8f57c2009-07-06 22:56:37 +000058 if (Ctx.isObjCObjectPointerType(CastToTy)) {
59 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: {
80 // CodeTextRegion should be cast to only function pointer type.
81 assert(CastToTy->isFunctionPointerType() || CastToTy->isBlockPointerType()
82 || (CastToTy->isPointerType() &&
83 CastToTy->getAsPointerType()->getPointeeType()->isVoidType()));
84 break;
85 }
86
87 case MemRegion::StringRegionKind:
88 // Handle casts of string literals.
89 return MakeElementRegion(state, R, PointeeTy, CastToTy);
90
91 case MemRegion::ObjCObjectRegionKind:
92 case MemRegion::SymbolicRegionKind:
93 // FIXME: Need to handle arbitrary downcasts.
94 case MemRegion::AllocaRegionKind: {
95 state = setCastType(state, R, CastToTy);
96 break;
97 }
98
99 case MemRegion::CompoundLiteralRegionKind:
100 case MemRegion::ElementRegionKind:
101 case MemRegion::FieldRegionKind:
102 case MemRegion::ObjCIvarRegionKind:
103 case MemRegion::VarRegionKind: {
Ted Kremenekdb5f2662009-07-06 22:59:23 +0000104 // VarRegion, ElementRegion, and FieldRegion has an inherent type.
105 // Normally they should not be cast. We only layer an ElementRegion when
106 // the cast-to pointee type is of smaller size. In other cases, we return
107 // the original VarRegion.
Ted Kremenekfc8f57c2009-07-06 22:56:37 +0000108
Zhongxing Xudb4f5312009-07-07 01:36:53 +0000109 // If the pointee or object type is incomplete, do not compute their
110 // sizes, and return the original region.
Ted Kremenekfc8f57c2009-07-06 22:56:37 +0000111 QualType ObjTy = cast<TypedRegion>(R)->getValueType(Ctx);
Ted Kremenek169077d2009-07-06 23:47:19 +0000112
113 if (!IsCompleteType(Ctx, PointeeTy) || !IsCompleteType(Ctx, ObjTy)) {
114 state = setCastType(state, R, ToTy);
115 break;
116 }
117
Ted Kremenekfc8f57c2009-07-06 22:56:37 +0000118 uint64_t PointeeTySize = Ctx.getTypeSize(PointeeTy);
119 uint64_t ObjTySize = Ctx.getTypeSize(ObjTy);
120
121 if ((PointeeTySize > 0 && PointeeTySize < ObjTySize) ||
122 (ObjTy->isAggregateType() && PointeeTy->isScalarType()) ||
123 ObjTySize == 0 /* R has 'void*' type. */)
124 return MakeElementRegion(state, R, PointeeTy, ToTy);
125
Ted Kremenek48ce7de2009-07-06 20:21:51 +0000126 state = setCastType(state, R, ToTy);
Ted Kremenekfc8f57c2009-07-06 22:56:37 +0000127 break;
Ted Kremenek48ce7de2009-07-06 20:21:51 +0000128 }
129 }
130
Ted Kremenekfc8f57c2009-07-06 22:56:37 +0000131 return CastResult(state, R);
Ted Kremenek48ce7de2009-07-06 20:21:51 +0000132}
133
134
135StoreManager::CastResult
136StoreManager::OldCastRegion(const GRState* state, const MemRegion* R,
Ted Kremenekfd6b4f32009-05-04 06:35:49 +0000137 QualType CastToTy) {
Ted Kremenekc62abc12009-04-21 21:51:34 +0000138
Ted Kremenek30d1b992009-04-21 23:31:46 +0000139 ASTContext& Ctx = StateMgr.getContext();
140
141 // We need to know the real type of CastToTy.
142 QualType ToTy = Ctx.getCanonicalType(CastToTy);
143
Ted Kremenekc62abc12009-04-21 21:51:34 +0000144 // Return the same region if the region types are compatible.
145 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R)) {
Zhongxing Xuff697822009-05-09 00:50:33 +0000146 QualType Ta = Ctx.getCanonicalType(TR->getLocationType(Ctx));
Ted Kremenek30d1b992009-04-21 23:31:46 +0000147
148 if (Ta == ToTy)
Ted Kremenekc62abc12009-04-21 21:51:34 +0000149 return CastResult(state, R);
150 }
151
Ted Kremenekfd6b4f32009-05-04 06:35:49 +0000152 if (const PointerType* PTy = dyn_cast<PointerType>(ToTy.getTypePtr())) {
153 // Check if we are casting to 'void*'.
154 // FIXME: Handle arbitrary upcasts.
155 QualType Pointee = PTy->getPointeeType();
156 if (Pointee->isVoidType()) {
Ted Kremenekca4e1b72009-07-06 20:53:52 +0000157 while (true) {
Ted Kremenek42530512009-05-06 18:19:24 +0000158 if (const TypedViewRegion *TR = dyn_cast<TypedViewRegion>(R)) {
159 // Casts to void* removes TypedViewRegion. This happens when:
160 //
161 // void foo(void*);
162 // ...
163 // void bar() {
164 // int x;
165 // foo(&x);
166 // }
167 //
168 R = TR->removeViews();
169 continue;
170 }
171 else if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
172 // Casts to void* also removes ElementRegions. This happens when:
173 //
174 // void foo(void*);
175 // ...
176 // void bar() {
177 // int x;
178 // foo((char*)&x);
179 // }
180 //
181 R = ER->getSuperRegion();
182 continue;
183 }
Ted Kremenek145918c2009-07-06 21:01:16 +0000184
185 break;
Ted Kremenek42530512009-05-06 18:19:24 +0000186 }
Ted Kremenek30d1b992009-04-21 23:31:46 +0000187
188 return CastResult(state, R);
189 }
Ted Kremenekfd6b4f32009-05-04 06:35:49 +0000190 else if (Pointee->isIntegerType()) {
191 // FIXME: At some point, it stands to reason that this 'dyn_cast' should
192 // become a 'cast' and that 'R' will always be a TypedRegion.
193 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) {
194 // Check if we are casting to a region with an integer type. We now
195 // the types aren't the same, so we construct an ElementRegion.
Ted Kremenekcd9392f2009-05-04 15:17:38 +0000196 SVal Idx = ValMgr.makeZeroArrayIndex();
Ted Kremenek20bd7462009-05-04 07:04:36 +0000197
198 // If the super region is an element region, strip it away.
199 // FIXME: Is this the right thing to do in all cases?
Ted Kremenek19cfa2b2009-06-30 22:31:23 +0000200 const MemRegion *Base = isa<ElementRegion>(TR) ? TR->getSuperRegion()
201 : TR;
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000202 ElementRegion* ER = MRMgr.getElementRegion(Pointee, Idx, Base,
Ted Kremenek19cfa2b2009-06-30 22:31:23 +0000203 StateMgr.getContext());
Ted Kremenekfd6b4f32009-05-04 06:35:49 +0000204 return CastResult(state, ER);
205 }
206 }
207 }
Ted Kremenek30d1b992009-04-21 23:31:46 +0000208
Ted Kremeneka8607d12009-05-01 19:22:20 +0000209 // FIXME: Need to handle arbitrary downcasts.
210 // FIXME: Handle the case where a TypedViewRegion (layering a SymbolicRegion
211 // or an AllocaRegion is cast to another view, thus causing the memory
212 // to be re-used for a different purpose.
Ted Kremeneka8607d12009-05-01 19:22:20 +0000213 if (isa<SymbolicRegion>(R) || isa<AllocaRegion>(R)) {
214 const MemRegion* ViewR = MRMgr.getTypedViewRegion(CastToTy, R);
215 return CastResult(AddRegionView(state, ViewR, R), ViewR);
216 }
217
218 return CastResult(state, R);
Ted Kremenekc62abc12009-04-21 21:51:34 +0000219}
Zhongxing Xu43e2aaf2009-07-06 03:41:27 +0000220
221const GRState *StoreManager::InvalidateRegion(const GRState *state,
Zhongxing Xu313b6da2009-07-06 06:01:24 +0000222 const MemRegion *R,
Zhongxing Xu43e2aaf2009-07-06 03:41:27 +0000223 const Expr *E, unsigned Count) {
Zhongxing Xu313b6da2009-07-06 06:01:24 +0000224 ASTContext& Ctx = StateMgr.getContext();
225
Zhongxing Xu43e2aaf2009-07-06 03:41:27 +0000226 if (!R->isBoundable())
227 return state;
228
Zhongxing Xu313b6da2009-07-06 06:01:24 +0000229 if (isa<AllocaRegion>(R) || isa<SymbolicRegion>(R)) {
230 // 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);
234 return state;
235 }
236
237 const TypedRegion *TR = cast<TypedRegion>(R);
238
239 QualType T = TR->getValueType(Ctx);
Zhongxing Xu43e2aaf2009-07-06 03:41:27 +0000240
241 if (Loc::IsLocType(T) || (T->isIntegerType() && T->isScalarType())) {
242 SVal V = ValMgr.getConjuredSymbolVal(E, T, Count);
Zhongxing Xu313b6da2009-07-06 06:01:24 +0000243 return Bind(state, ValMgr.makeLoc(TR), V);
Zhongxing Xu43e2aaf2009-07-06 03:41:27 +0000244 }
245 else if (const RecordType *RT = T->getAsStructureType()) {
246 // FIXME: handle structs with default region value.
247 const RecordDecl *RD = RT->getDecl()->getDefinition(Ctx);
248
249 // No record definition. There is nothing we can do.
250 if (!RD)
251 return state;
252
253 // Iterate through the fields and construct new symbols.
254 for (RecordDecl::field_iterator FI=RD->field_begin(),
255 FE=RD->field_end(); FI!=FE; ++FI) {
256
257 // For now just handle scalar fields.
258 FieldDecl *FD = *FI;
259 QualType FT = FD->getType();
Zhongxing Xu313b6da2009-07-06 06:01:24 +0000260 const FieldRegion* FR = MRMgr.getFieldRegion(FD, TR);
Zhongxing Xu43e2aaf2009-07-06 03:41:27 +0000261
262 if (Loc::IsLocType(FT) ||
263 (FT->isIntegerType() && FT->isScalarType())) {
264 SVal V = ValMgr.getConjuredSymbolVal(E, FT, Count);
265 state = state->bindLoc(ValMgr.makeLoc(FR), V);
266 }
267 else if (FT->isStructureType()) {
268 // set the default value of the struct field to conjured
269 // symbol. Note that the type of the symbol is irrelavant.
270 // We cannot use the type of the struct otherwise ValMgr won't
271 // give us the conjured symbol.
272 SVal V = ValMgr.getConjuredSymbolVal(E, Ctx.IntTy, Count);
273 state = setDefaultValue(state, FR, V);
274 }
275 }
276 } else if (const ArrayType *AT = Ctx.getAsArrayType(T)) {
277 // Set the default value of the array to conjured symbol.
278 SVal V = ValMgr.getConjuredSymbolVal(E, AT->getElementType(),
279 Count);
Zhongxing Xu313b6da2009-07-06 06:01:24 +0000280 state = setDefaultValue(state, TR, V);
Zhongxing Xu43e2aaf2009-07-06 03:41:27 +0000281 } else {
282 // Just blast away other values.
Zhongxing Xu313b6da2009-07-06 06:01:24 +0000283 state = Bind(state, ValMgr.makeLoc(TR), UnknownVal());
Zhongxing Xu43e2aaf2009-07-06 03:41:27 +0000284 }
285
286 return state;
287}