blob: 6c6e7d272d3cdd349e0eb8a36712f11b81869f26 [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 Kremenek145918c2009-07-06 21:01:16 +000046
Ted Kremenek48ce7de2009-07-06 20:21:51 +000047 // CodeTextRegion should be cast to only function pointer type.
48 if (isa<CodeTextRegion>(R)) {
49 assert(CastToTy->isFunctionPointerType() || CastToTy->isBlockPointerType()
Ted Kremenek07cdec12009-07-06 22:40:36 +000050 || (CastToTy->isPointerType() &&
51 CastToTy->getAsPointerType()->getPointeeType()->isVoidType()));
Ted Kremenek48ce7de2009-07-06 20:21:51 +000052 return CastResult(state, R);
53 }
Ted Kremenek411af402009-07-06 22:23:45 +000054
Ted Kremenekb9a44252009-07-06 22:39:40 +000055 // Handle casts to Objective-C objects.
56 if (Ctx.isObjCObjectPointerType(ToTy)) {
Ted Kremenek1f612cb2009-07-06 22:34:50 +000057 state = setCastType(state, R, ToTy);
Ted Kremenek145918c2009-07-06 21:01:16 +000058 return CastResult(state, R);
59 }
Ted Kremenek411af402009-07-06 22:23:45 +000060
Ted Kremenek48ce7de2009-07-06 20:21:51 +000061 // Now assume we are casting from pointer to pointer. Other cases should
62 // already be handled.
63 QualType PointeeTy = cast<PointerType>(ToTy.getTypePtr())->getPointeeType();
Ted Kremenek411af402009-07-06 22:23:45 +000064
Ted Kremenek48ce7de2009-07-06 20:21:51 +000065 // Process region cast according to the kind of the region being cast.
66
Ted Kremenek411af402009-07-06 22:23:45 +000067 // Handle casts of string literals.
68 if (isa<StringRegion>(R))
69 return MakeElementRegion(state, R, PointeeTy, ToTy);
70
Ted Kremenek48ce7de2009-07-06 20:21:51 +000071 // FIXME: Need to handle arbitrary downcasts.
72 if (isa<SymbolicRegion>(R) || isa<AllocaRegion>(R)) {
73 state = setCastType(state, R, ToTy);
74 return CastResult(state, R);
75 }
76
77 // VarRegion, ElementRegion, and FieldRegion has an inherent type. Normally
78 // they should not be cast. We only layer an ElementRegion when the cast-to
79 // pointee type is of smaller size. In other cases, we return the original
80 // VarRegion.
81 if (isa<VarRegion>(R) || isa<ElementRegion>(R) || isa<FieldRegion>(R)
82 || isa<ObjCIvarRegion>(R) || isa<CompoundLiteralRegion>(R)) {
83 // If the pointee type is incomplete, do not compute its size, and return
84 // the original region.
Ted Kremenek145918c2009-07-06 21:01:16 +000085 if (const RecordType *RT = PointeeTy->getAsRecordType()) {
Ted Kremenek48ce7de2009-07-06 20:21:51 +000086 const RecordDecl *D = RT->getDecl();
87 if (!D->getDefinition(Ctx))
88 return CastResult(state, R);
89 }
90
91 QualType ObjTy = cast<TypedRegion>(R)->getValueType(Ctx);
92 uint64_t PointeeTySize = Ctx.getTypeSize(PointeeTy);
93 uint64_t ObjTySize = Ctx.getTypeSize(ObjTy);
94
95 if ((PointeeTySize > 0 && PointeeTySize < ObjTySize) ||
96 (ObjTy->isAggregateType() && PointeeTy->isScalarType()) ||
Ted Kremenek411af402009-07-06 22:23:45 +000097 ObjTySize == 0 /* R has 'void*' type. */) {
98 return MakeElementRegion(state, R, PointeeTy, ToTy);
Ted Kremenek48ce7de2009-07-06 20:21:51 +000099 } else {
100 state = setCastType(state, R, ToTy);
101 return CastResult(state, R);
102 }
103 }
104
105 if (isa<ObjCObjectRegion>(R)) {
106 return CastResult(state, R);
107 }
108
109 assert(0 && "Unprocessed region.");
110 return 0;
111}
112
113
114StoreManager::CastResult
115StoreManager::OldCastRegion(const GRState* state, const MemRegion* R,
Ted Kremenekfd6b4f32009-05-04 06:35:49 +0000116 QualType CastToTy) {
Ted Kremenekc62abc12009-04-21 21:51:34 +0000117
Ted Kremenek30d1b992009-04-21 23:31:46 +0000118 ASTContext& Ctx = StateMgr.getContext();
119
120 // We need to know the real type of CastToTy.
121 QualType ToTy = Ctx.getCanonicalType(CastToTy);
122
Ted Kremenekc62abc12009-04-21 21:51:34 +0000123 // Return the same region if the region types are compatible.
124 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R)) {
Zhongxing Xuff697822009-05-09 00:50:33 +0000125 QualType Ta = Ctx.getCanonicalType(TR->getLocationType(Ctx));
Ted Kremenek30d1b992009-04-21 23:31:46 +0000126
127 if (Ta == ToTy)
Ted Kremenekc62abc12009-04-21 21:51:34 +0000128 return CastResult(state, R);
129 }
130
Ted Kremenekfd6b4f32009-05-04 06:35:49 +0000131 if (const PointerType* PTy = dyn_cast<PointerType>(ToTy.getTypePtr())) {
132 // Check if we are casting to 'void*'.
133 // FIXME: Handle arbitrary upcasts.
134 QualType Pointee = PTy->getPointeeType();
135 if (Pointee->isVoidType()) {
Ted Kremenekca4e1b72009-07-06 20:53:52 +0000136 while (true) {
Ted Kremenek42530512009-05-06 18:19:24 +0000137 if (const TypedViewRegion *TR = dyn_cast<TypedViewRegion>(R)) {
138 // Casts to void* removes TypedViewRegion. This happens when:
139 //
140 // void foo(void*);
141 // ...
142 // void bar() {
143 // int x;
144 // foo(&x);
145 // }
146 //
147 R = TR->removeViews();
148 continue;
149 }
150 else if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
151 // Casts to void* also removes ElementRegions. This happens when:
152 //
153 // void foo(void*);
154 // ...
155 // void bar() {
156 // int x;
157 // foo((char*)&x);
158 // }
159 //
160 R = ER->getSuperRegion();
161 continue;
162 }
Ted Kremenek145918c2009-07-06 21:01:16 +0000163
164 break;
Ted Kremenek42530512009-05-06 18:19:24 +0000165 }
Ted Kremenek30d1b992009-04-21 23:31:46 +0000166
167 return CastResult(state, R);
168 }
Ted Kremenekfd6b4f32009-05-04 06:35:49 +0000169 else if (Pointee->isIntegerType()) {
170 // FIXME: At some point, it stands to reason that this 'dyn_cast' should
171 // become a 'cast' and that 'R' will always be a TypedRegion.
172 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) {
173 // Check if we are casting to a region with an integer type. We now
174 // the types aren't the same, so we construct an ElementRegion.
Ted Kremenekcd9392f2009-05-04 15:17:38 +0000175 SVal Idx = ValMgr.makeZeroArrayIndex();
Ted Kremenek20bd7462009-05-04 07:04:36 +0000176
177 // If the super region is an element region, strip it away.
178 // FIXME: Is this the right thing to do in all cases?
Ted Kremenek19cfa2b2009-06-30 22:31:23 +0000179 const MemRegion *Base = isa<ElementRegion>(TR) ? TR->getSuperRegion()
180 : TR;
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000181 ElementRegion* ER = MRMgr.getElementRegion(Pointee, Idx, Base,
Ted Kremenek19cfa2b2009-06-30 22:31:23 +0000182 StateMgr.getContext());
Ted Kremenekfd6b4f32009-05-04 06:35:49 +0000183 return CastResult(state, ER);
184 }
185 }
186 }
Ted Kremenek30d1b992009-04-21 23:31:46 +0000187
Ted Kremeneka8607d12009-05-01 19:22:20 +0000188 // FIXME: Need to handle arbitrary downcasts.
189 // FIXME: Handle the case where a TypedViewRegion (layering a SymbolicRegion
190 // or an AllocaRegion is cast to another view, thus causing the memory
191 // to be re-used for a different purpose.
Ted Kremeneka8607d12009-05-01 19:22:20 +0000192 if (isa<SymbolicRegion>(R) || isa<AllocaRegion>(R)) {
193 const MemRegion* ViewR = MRMgr.getTypedViewRegion(CastToTy, R);
194 return CastResult(AddRegionView(state, ViewR, R), ViewR);
195 }
196
197 return CastResult(state, R);
Ted Kremenekc62abc12009-04-21 21:51:34 +0000198}
Zhongxing Xu43e2aaf2009-07-06 03:41:27 +0000199
200const GRState *StoreManager::InvalidateRegion(const GRState *state,
Zhongxing Xu313b6da2009-07-06 06:01:24 +0000201 const MemRegion *R,
Zhongxing Xu43e2aaf2009-07-06 03:41:27 +0000202 const Expr *E, unsigned Count) {
Zhongxing Xu313b6da2009-07-06 06:01:24 +0000203 ASTContext& Ctx = StateMgr.getContext();
204
Zhongxing Xu43e2aaf2009-07-06 03:41:27 +0000205 if (!R->isBoundable())
206 return state;
207
Zhongxing Xu313b6da2009-07-06 06:01:24 +0000208 if (isa<AllocaRegion>(R) || isa<SymbolicRegion>(R)) {
209 // Invalidate the alloca region by setting its default value to
210 // conjured symbol. The type of the symbol is irrelavant.
211 SVal V = ValMgr.getConjuredSymbolVal(E, Ctx.IntTy, Count);
212 state = setDefaultValue(state, R, V);
213 return state;
214 }
215
216 const TypedRegion *TR = cast<TypedRegion>(R);
217
218 QualType T = TR->getValueType(Ctx);
Zhongxing Xu43e2aaf2009-07-06 03:41:27 +0000219
220 if (Loc::IsLocType(T) || (T->isIntegerType() && T->isScalarType())) {
221 SVal V = ValMgr.getConjuredSymbolVal(E, T, Count);
Zhongxing Xu313b6da2009-07-06 06:01:24 +0000222 return Bind(state, ValMgr.makeLoc(TR), V);
Zhongxing Xu43e2aaf2009-07-06 03:41:27 +0000223 }
224 else if (const RecordType *RT = T->getAsStructureType()) {
225 // FIXME: handle structs with default region value.
226 const RecordDecl *RD = RT->getDecl()->getDefinition(Ctx);
227
228 // No record definition. There is nothing we can do.
229 if (!RD)
230 return state;
231
232 // Iterate through the fields and construct new symbols.
233 for (RecordDecl::field_iterator FI=RD->field_begin(),
234 FE=RD->field_end(); FI!=FE; ++FI) {
235
236 // For now just handle scalar fields.
237 FieldDecl *FD = *FI;
238 QualType FT = FD->getType();
Zhongxing Xu313b6da2009-07-06 06:01:24 +0000239 const FieldRegion* FR = MRMgr.getFieldRegion(FD, TR);
Zhongxing Xu43e2aaf2009-07-06 03:41:27 +0000240
241 if (Loc::IsLocType(FT) ||
242 (FT->isIntegerType() && FT->isScalarType())) {
243 SVal V = ValMgr.getConjuredSymbolVal(E, FT, Count);
244 state = state->bindLoc(ValMgr.makeLoc(FR), V);
245 }
246 else if (FT->isStructureType()) {
247 // set the default value of the struct field to conjured
248 // symbol. Note that the type of the symbol is irrelavant.
249 // We cannot use the type of the struct otherwise ValMgr won't
250 // give us the conjured symbol.
251 SVal V = ValMgr.getConjuredSymbolVal(E, Ctx.IntTy, Count);
252 state = setDefaultValue(state, FR, V);
253 }
254 }
255 } else if (const ArrayType *AT = Ctx.getAsArrayType(T)) {
256 // Set the default value of the array to conjured symbol.
257 SVal V = ValMgr.getConjuredSymbolVal(E, AT->getElementType(),
258 Count);
Zhongxing Xu313b6da2009-07-06 06:01:24 +0000259 state = setDefaultValue(state, TR, V);
Zhongxing Xu43e2aaf2009-07-06 03:41:27 +0000260 } else {
261 // Just blast away other values.
Zhongxing Xu313b6da2009-07-06 06:01:24 +0000262 state = Bind(state, ValMgr.makeLoc(TR), UnknownVal());
Zhongxing Xu43e2aaf2009-07-06 03:41:27 +0000263 }
264
265 return state;
266}