blob: 4ca02085225d319caa53fd7adb5ed028fbc56756 [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
38StoreManager::CastResult
Ted Kremenek48ce7de2009-07-06 20:21:51 +000039StoreManager::NewCastRegion(const GRState *state, const MemRegion* R,
40 QualType CastToTy) {
41
42 ASTContext& Ctx = StateMgr.getContext();
43
44 // We need to know the real type of CastToTy.
45 QualType ToTy = Ctx.getCanonicalType(CastToTy);
Ted Kremenek411af402009-07-06 22:23:45 +000046
Ted Kremenekb9a44252009-07-06 22:39:40 +000047 // Handle casts to Objective-C objects.
Ted Kremenekfc8f57c2009-07-06 22:56:37 +000048 if (Ctx.isObjCObjectPointerType(CastToTy)) {
49 state = setCastType(state, R, CastToTy);
Ted Kremenek145918c2009-07-06 21:01:16 +000050 return CastResult(state, R);
51 }
Ted Kremenek411af402009-07-06 22:23:45 +000052
Ted Kremenek48ce7de2009-07-06 20:21:51 +000053 // Now assume we are casting from pointer to pointer. Other cases should
54 // already be handled.
Ted Kremenekfc8f57c2009-07-06 22:56:37 +000055 QualType PointeeTy = CastToTy->getAsPointerType()->getPointeeType();
Ted Kremenek411af402009-07-06 22:23:45 +000056
Ted Kremenek48ce7de2009-07-06 20:21:51 +000057 // Process region cast according to the kind of the region being cast.
Ted Kremenekfc8f57c2009-07-06 22:56:37 +000058 switch (R->getKind()) {
59 case MemRegion::BEG_TYPED_REGIONS:
60 case MemRegion::MemSpaceRegionKind:
61 case MemRegion::BEG_DECL_REGIONS:
62 case MemRegion::END_DECL_REGIONS:
63 case MemRegion::END_TYPED_REGIONS:
64 case MemRegion::TypedViewRegionKind: {
65 assert(0 && "Invalid region cast");
66 break;
Ted Kremenek48ce7de2009-07-06 20:21:51 +000067 }
Ted Kremenekfc8f57c2009-07-06 22:56:37 +000068
69 case MemRegion::CodeTextRegionKind: {
70 // CodeTextRegion should be cast to only function pointer type.
71 assert(CastToTy->isFunctionPointerType() || CastToTy->isBlockPointerType()
72 || (CastToTy->isPointerType() &&
73 CastToTy->getAsPointerType()->getPointeeType()->isVoidType()));
74 break;
75 }
76
77 case MemRegion::StringRegionKind:
78 // Handle casts of string literals.
79 return MakeElementRegion(state, R, PointeeTy, CastToTy);
80
81 case MemRegion::ObjCObjectRegionKind:
82 case MemRegion::SymbolicRegionKind:
83 // FIXME: Need to handle arbitrary downcasts.
84 case MemRegion::AllocaRegionKind: {
85 state = setCastType(state, R, CastToTy);
86 break;
87 }
88
89 case MemRegion::CompoundLiteralRegionKind:
90 case MemRegion::ElementRegionKind:
91 case MemRegion::FieldRegionKind:
92 case MemRegion::ObjCIvarRegionKind:
93 case MemRegion::VarRegionKind: {
Ted Kremenekdb5f2662009-07-06 22:59:23 +000094 // VarRegion, ElementRegion, and FieldRegion has an inherent type.
95 // Normally they should not be cast. We only layer an ElementRegion when
96 // the cast-to pointee type is of smaller size. In other cases, we return
97 // the original VarRegion.
Ted Kremenekfc8f57c2009-07-06 22:56:37 +000098
99 // If the pointee type is incomplete, do not compute its size, and return
100 // the original region.
101 if (const RecordType *RT = PointeeTy->getAsRecordType()) {
102 const RecordDecl *D = RT->getDecl();
103 if (!D->getDefinition(Ctx))
104 return CastResult(state, R);
105 }
106
107 QualType ObjTy = cast<TypedRegion>(R)->getValueType(Ctx);
108 uint64_t PointeeTySize = Ctx.getTypeSize(PointeeTy);
109 uint64_t ObjTySize = Ctx.getTypeSize(ObjTy);
110
111 if ((PointeeTySize > 0 && PointeeTySize < ObjTySize) ||
112 (ObjTy->isAggregateType() && PointeeTy->isScalarType()) ||
113 ObjTySize == 0 /* R has 'void*' type. */)
114 return MakeElementRegion(state, R, PointeeTy, ToTy);
115
Ted Kremenek48ce7de2009-07-06 20:21:51 +0000116 state = setCastType(state, R, ToTy);
Ted Kremenekfc8f57c2009-07-06 22:56:37 +0000117 break;
Ted Kremenek48ce7de2009-07-06 20:21:51 +0000118 }
119 }
120
Ted Kremenekfc8f57c2009-07-06 22:56:37 +0000121 return CastResult(state, R);
Ted Kremenek48ce7de2009-07-06 20:21:51 +0000122}
123
124
125StoreManager::CastResult
126StoreManager::OldCastRegion(const GRState* state, const MemRegion* R,
Ted Kremenekfd6b4f32009-05-04 06:35:49 +0000127 QualType CastToTy) {
Ted Kremenekc62abc12009-04-21 21:51:34 +0000128
Ted Kremenek30d1b992009-04-21 23:31:46 +0000129 ASTContext& Ctx = StateMgr.getContext();
130
131 // We need to know the real type of CastToTy.
132 QualType ToTy = Ctx.getCanonicalType(CastToTy);
133
Ted Kremenekc62abc12009-04-21 21:51:34 +0000134 // Return the same region if the region types are compatible.
135 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R)) {
Zhongxing Xuff697822009-05-09 00:50:33 +0000136 QualType Ta = Ctx.getCanonicalType(TR->getLocationType(Ctx));
Ted Kremenek30d1b992009-04-21 23:31:46 +0000137
138 if (Ta == ToTy)
Ted Kremenekc62abc12009-04-21 21:51:34 +0000139 return CastResult(state, R);
140 }
141
Ted Kremenekfd6b4f32009-05-04 06:35:49 +0000142 if (const PointerType* PTy = dyn_cast<PointerType>(ToTy.getTypePtr())) {
143 // Check if we are casting to 'void*'.
144 // FIXME: Handle arbitrary upcasts.
145 QualType Pointee = PTy->getPointeeType();
146 if (Pointee->isVoidType()) {
Ted Kremenekca4e1b72009-07-06 20:53:52 +0000147 while (true) {
Ted Kremenek42530512009-05-06 18:19:24 +0000148 if (const TypedViewRegion *TR = dyn_cast<TypedViewRegion>(R)) {
149 // Casts to void* removes TypedViewRegion. This happens when:
150 //
151 // void foo(void*);
152 // ...
153 // void bar() {
154 // int x;
155 // foo(&x);
156 // }
157 //
158 R = TR->removeViews();
159 continue;
160 }
161 else if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
162 // Casts to void* also removes ElementRegions. This happens when:
163 //
164 // void foo(void*);
165 // ...
166 // void bar() {
167 // int x;
168 // foo((char*)&x);
169 // }
170 //
171 R = ER->getSuperRegion();
172 continue;
173 }
Ted Kremenek145918c2009-07-06 21:01:16 +0000174
175 break;
Ted Kremenek42530512009-05-06 18:19:24 +0000176 }
Ted Kremenek30d1b992009-04-21 23:31:46 +0000177
178 return CastResult(state, R);
179 }
Ted Kremenekfd6b4f32009-05-04 06:35:49 +0000180 else if (Pointee->isIntegerType()) {
181 // FIXME: At some point, it stands to reason that this 'dyn_cast' should
182 // become a 'cast' and that 'R' will always be a TypedRegion.
183 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) {
184 // Check if we are casting to a region with an integer type. We now
185 // the types aren't the same, so we construct an ElementRegion.
Ted Kremenekcd9392f2009-05-04 15:17:38 +0000186 SVal Idx = ValMgr.makeZeroArrayIndex();
Ted Kremenek20bd7462009-05-04 07:04:36 +0000187
188 // If the super region is an element region, strip it away.
189 // FIXME: Is this the right thing to do in all cases?
Ted Kremenek19cfa2b2009-06-30 22:31:23 +0000190 const MemRegion *Base = isa<ElementRegion>(TR) ? TR->getSuperRegion()
191 : TR;
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000192 ElementRegion* ER = MRMgr.getElementRegion(Pointee, Idx, Base,
Ted Kremenek19cfa2b2009-06-30 22:31:23 +0000193 StateMgr.getContext());
Ted Kremenekfd6b4f32009-05-04 06:35:49 +0000194 return CastResult(state, ER);
195 }
196 }
197 }
Ted Kremenek30d1b992009-04-21 23:31:46 +0000198
Ted Kremeneka8607d12009-05-01 19:22:20 +0000199 // FIXME: Need to handle arbitrary downcasts.
200 // FIXME: Handle the case where a TypedViewRegion (layering a SymbolicRegion
201 // or an AllocaRegion is cast to another view, thus causing the memory
202 // to be re-used for a different purpose.
Ted Kremeneka8607d12009-05-01 19:22:20 +0000203 if (isa<SymbolicRegion>(R) || isa<AllocaRegion>(R)) {
204 const MemRegion* ViewR = MRMgr.getTypedViewRegion(CastToTy, R);
205 return CastResult(AddRegionView(state, ViewR, R), ViewR);
206 }
207
208 return CastResult(state, R);
Ted Kremenekc62abc12009-04-21 21:51:34 +0000209}
Zhongxing Xu43e2aaf2009-07-06 03:41:27 +0000210
211const GRState *StoreManager::InvalidateRegion(const GRState *state,
Zhongxing Xu313b6da2009-07-06 06:01:24 +0000212 const MemRegion *R,
Zhongxing Xu43e2aaf2009-07-06 03:41:27 +0000213 const Expr *E, unsigned Count) {
Zhongxing Xu313b6da2009-07-06 06:01:24 +0000214 ASTContext& Ctx = StateMgr.getContext();
215
Zhongxing Xu43e2aaf2009-07-06 03:41:27 +0000216 if (!R->isBoundable())
217 return state;
218
Zhongxing Xu313b6da2009-07-06 06:01:24 +0000219 if (isa<AllocaRegion>(R) || isa<SymbolicRegion>(R)) {
220 // Invalidate the alloca region by setting its default value to
221 // conjured symbol. The type of the symbol is irrelavant.
222 SVal V = ValMgr.getConjuredSymbolVal(E, Ctx.IntTy, Count);
223 state = setDefaultValue(state, R, V);
224 return state;
225 }
226
227 const TypedRegion *TR = cast<TypedRegion>(R);
228
229 QualType T = TR->getValueType(Ctx);
Zhongxing Xu43e2aaf2009-07-06 03:41:27 +0000230
231 if (Loc::IsLocType(T) || (T->isIntegerType() && T->isScalarType())) {
232 SVal V = ValMgr.getConjuredSymbolVal(E, T, Count);
Zhongxing Xu313b6da2009-07-06 06:01:24 +0000233 return Bind(state, ValMgr.makeLoc(TR), V);
Zhongxing Xu43e2aaf2009-07-06 03:41:27 +0000234 }
235 else if (const RecordType *RT = T->getAsStructureType()) {
236 // FIXME: handle structs with default region value.
237 const RecordDecl *RD = RT->getDecl()->getDefinition(Ctx);
238
239 // No record definition. There is nothing we can do.
240 if (!RD)
241 return state;
242
243 // Iterate through the fields and construct new symbols.
244 for (RecordDecl::field_iterator FI=RD->field_begin(),
245 FE=RD->field_end(); FI!=FE; ++FI) {
246
247 // For now just handle scalar fields.
248 FieldDecl *FD = *FI;
249 QualType FT = FD->getType();
Zhongxing Xu313b6da2009-07-06 06:01:24 +0000250 const FieldRegion* FR = MRMgr.getFieldRegion(FD, TR);
Zhongxing Xu43e2aaf2009-07-06 03:41:27 +0000251
252 if (Loc::IsLocType(FT) ||
253 (FT->isIntegerType() && FT->isScalarType())) {
254 SVal V = ValMgr.getConjuredSymbolVal(E, FT, Count);
255 state = state->bindLoc(ValMgr.makeLoc(FR), V);
256 }
257 else if (FT->isStructureType()) {
258 // set the default value of the struct field to conjured
259 // symbol. Note that the type of the symbol is irrelavant.
260 // We cannot use the type of the struct otherwise ValMgr won't
261 // give us the conjured symbol.
262 SVal V = ValMgr.getConjuredSymbolVal(E, Ctx.IntTy, Count);
263 state = setDefaultValue(state, FR, V);
264 }
265 }
266 } else if (const ArrayType *AT = Ctx.getAsArrayType(T)) {
267 // Set the default value of the array to conjured symbol.
268 SVal V = ValMgr.getConjuredSymbolVal(E, AT->getElementType(),
269 Count);
Zhongxing Xu313b6da2009-07-06 06:01:24 +0000270 state = setDefaultValue(state, TR, V);
Zhongxing Xu43e2aaf2009-07-06 03:41:27 +0000271 } else {
272 // Just blast away other values.
Zhongxing Xu313b6da2009-07-06 06:01:24 +0000273 state = Bind(state, ValMgr.makeLoc(TR), UnknownVal());
Zhongxing Xu43e2aaf2009-07-06 03:41:27 +0000274 }
275
276 return state;
277}