blob: cd123995faa39b0419e1d222c8aaa16ee752722c [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 Kremenek48ce7de2009-07-06 20:21:51 +000026StoreManager::NewCastRegion(const GRState *state, const MemRegion* R,
27 QualType CastToTy) {
28
29 ASTContext& Ctx = StateMgr.getContext();
30
31 // We need to know the real type of CastToTy.
32 QualType ToTy = Ctx.getCanonicalType(CastToTy);
Ted Kremenek145918c2009-07-06 21:01:16 +000033
Ted Kremenek48ce7de2009-07-06 20:21:51 +000034 // CodeTextRegion should be cast to only function pointer type.
35 if (isa<CodeTextRegion>(R)) {
36 assert(CastToTy->isFunctionPointerType() || CastToTy->isBlockPointerType()
37 || (CastToTy->isPointerType()
38 && CastToTy->getAsPointerType()->getPointeeType()->isVoidType()));
39 return CastResult(state, R);
40 }
41
Ted Kremenek145918c2009-07-06 21:01:16 +000042 // Check cast to ObjCQualifiedID type.
43 if (ToTy->isObjCQualifiedIdType()) {
44 // FIXME: Record the type information aside.
45 return CastResult(state, R);
46 }
47
Ted Kremenek48ce7de2009-07-06 20:21:51 +000048 // Now assume we are casting from pointer to pointer. Other cases should
49 // already be handled.
50 QualType PointeeTy = cast<PointerType>(ToTy.getTypePtr())->getPointeeType();
51
52 // Process region cast according to the kind of the region being cast.
53
54 // FIXME: Need to handle arbitrary downcasts.
55 if (isa<SymbolicRegion>(R) || isa<AllocaRegion>(R)) {
56 state = setCastType(state, R, ToTy);
57 return CastResult(state, R);
58 }
59
60 // VarRegion, ElementRegion, and FieldRegion has an inherent type. Normally
61 // they should not be cast. We only layer an ElementRegion when the cast-to
62 // pointee type is of smaller size. In other cases, we return the original
63 // VarRegion.
64 if (isa<VarRegion>(R) || isa<ElementRegion>(R) || isa<FieldRegion>(R)
65 || isa<ObjCIvarRegion>(R) || isa<CompoundLiteralRegion>(R)) {
66 // If the pointee type is incomplete, do not compute its size, and return
67 // the original region.
Ted Kremenek145918c2009-07-06 21:01:16 +000068 if (const RecordType *RT = PointeeTy->getAsRecordType()) {
Ted Kremenek48ce7de2009-07-06 20:21:51 +000069 const RecordDecl *D = RT->getDecl();
70 if (!D->getDefinition(Ctx))
71 return CastResult(state, R);
72 }
73
74 QualType ObjTy = cast<TypedRegion>(R)->getValueType(Ctx);
75 uint64_t PointeeTySize = Ctx.getTypeSize(PointeeTy);
76 uint64_t ObjTySize = Ctx.getTypeSize(ObjTy);
77
78 if ((PointeeTySize > 0 && PointeeTySize < ObjTySize) ||
79 (ObjTy->isAggregateType() && PointeeTy->isScalarType()) ||
80 ObjTySize == 0 /* R has 'void*' type. */) {
81 // Record the cast type of the region.
82 state = setCastType(state, R, ToTy);
83
84 SVal Idx = ValMgr.makeZeroArrayIndex();
85 ElementRegion* ER = MRMgr.getElementRegion(PointeeTy, Idx,R, Ctx);
86 return CastResult(state, ER);
87 } else {
88 state = setCastType(state, R, ToTy);
89 return CastResult(state, R);
90 }
91 }
92
93 if (isa<ObjCObjectRegion>(R)) {
94 return CastResult(state, R);
95 }
96
97 assert(0 && "Unprocessed region.");
98 return 0;
99}
100
101
102StoreManager::CastResult
103StoreManager::OldCastRegion(const GRState* state, const MemRegion* R,
Ted Kremenekfd6b4f32009-05-04 06:35:49 +0000104 QualType CastToTy) {
Ted Kremenekc62abc12009-04-21 21:51:34 +0000105
Ted Kremenek30d1b992009-04-21 23:31:46 +0000106 ASTContext& Ctx = StateMgr.getContext();
107
108 // We need to know the real type of CastToTy.
109 QualType ToTy = Ctx.getCanonicalType(CastToTy);
110
Ted Kremenekc62abc12009-04-21 21:51:34 +0000111 // Return the same region if the region types are compatible.
112 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R)) {
Zhongxing Xuff697822009-05-09 00:50:33 +0000113 QualType Ta = Ctx.getCanonicalType(TR->getLocationType(Ctx));
Ted Kremenek30d1b992009-04-21 23:31:46 +0000114
115 if (Ta == ToTy)
Ted Kremenekc62abc12009-04-21 21:51:34 +0000116 return CastResult(state, R);
117 }
118
Ted Kremenekfd6b4f32009-05-04 06:35:49 +0000119 if (const PointerType* PTy = dyn_cast<PointerType>(ToTy.getTypePtr())) {
120 // Check if we are casting to 'void*'.
121 // FIXME: Handle arbitrary upcasts.
122 QualType Pointee = PTy->getPointeeType();
123 if (Pointee->isVoidType()) {
Ted Kremenekca4e1b72009-07-06 20:53:52 +0000124 while (true) {
Ted Kremenek42530512009-05-06 18:19:24 +0000125 if (const TypedViewRegion *TR = dyn_cast<TypedViewRegion>(R)) {
126 // Casts to void* removes TypedViewRegion. This happens when:
127 //
128 // void foo(void*);
129 // ...
130 // void bar() {
131 // int x;
132 // foo(&x);
133 // }
134 //
135 R = TR->removeViews();
136 continue;
137 }
138 else if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
139 // Casts to void* also removes ElementRegions. This happens when:
140 //
141 // void foo(void*);
142 // ...
143 // void bar() {
144 // int x;
145 // foo((char*)&x);
146 // }
147 //
148 R = ER->getSuperRegion();
149 continue;
150 }
Ted Kremenek145918c2009-07-06 21:01:16 +0000151
152 break;
Ted Kremenek42530512009-05-06 18:19:24 +0000153 }
Ted Kremenek30d1b992009-04-21 23:31:46 +0000154
155 return CastResult(state, R);
156 }
Ted Kremenekfd6b4f32009-05-04 06:35:49 +0000157 else if (Pointee->isIntegerType()) {
158 // FIXME: At some point, it stands to reason that this 'dyn_cast' should
159 // become a 'cast' and that 'R' will always be a TypedRegion.
160 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) {
161 // Check if we are casting to a region with an integer type. We now
162 // the types aren't the same, so we construct an ElementRegion.
Ted Kremenekcd9392f2009-05-04 15:17:38 +0000163 SVal Idx = ValMgr.makeZeroArrayIndex();
Ted Kremenek20bd7462009-05-04 07:04:36 +0000164
165 // If the super region is an element region, strip it away.
166 // FIXME: Is this the right thing to do in all cases?
Ted Kremenek19cfa2b2009-06-30 22:31:23 +0000167 const MemRegion *Base = isa<ElementRegion>(TR) ? TR->getSuperRegion()
168 : TR;
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000169 ElementRegion* ER = MRMgr.getElementRegion(Pointee, Idx, Base,
Ted Kremenek19cfa2b2009-06-30 22:31:23 +0000170 StateMgr.getContext());
Ted Kremenekfd6b4f32009-05-04 06:35:49 +0000171 return CastResult(state, ER);
172 }
173 }
174 }
Ted Kremenek30d1b992009-04-21 23:31:46 +0000175
Ted Kremeneka8607d12009-05-01 19:22:20 +0000176 // FIXME: Need to handle arbitrary downcasts.
177 // FIXME: Handle the case where a TypedViewRegion (layering a SymbolicRegion
178 // or an AllocaRegion is cast to another view, thus causing the memory
179 // to be re-used for a different purpose.
Ted Kremeneka8607d12009-05-01 19:22:20 +0000180 if (isa<SymbolicRegion>(R) || isa<AllocaRegion>(R)) {
181 const MemRegion* ViewR = MRMgr.getTypedViewRegion(CastToTy, R);
182 return CastResult(AddRegionView(state, ViewR, R), ViewR);
183 }
184
185 return CastResult(state, R);
Ted Kremenekc62abc12009-04-21 21:51:34 +0000186}
Zhongxing Xu43e2aaf2009-07-06 03:41:27 +0000187
188const GRState *StoreManager::InvalidateRegion(const GRState *state,
Zhongxing Xu313b6da2009-07-06 06:01:24 +0000189 const MemRegion *R,
Zhongxing Xu43e2aaf2009-07-06 03:41:27 +0000190 const Expr *E, unsigned Count) {
Zhongxing Xu313b6da2009-07-06 06:01:24 +0000191 ASTContext& Ctx = StateMgr.getContext();
192
Zhongxing Xu43e2aaf2009-07-06 03:41:27 +0000193 if (!R->isBoundable())
194 return state;
195
Zhongxing Xu313b6da2009-07-06 06:01:24 +0000196 if (isa<AllocaRegion>(R) || isa<SymbolicRegion>(R)) {
197 // Invalidate the alloca region by setting its default value to
198 // conjured symbol. The type of the symbol is irrelavant.
199 SVal V = ValMgr.getConjuredSymbolVal(E, Ctx.IntTy, Count);
200 state = setDefaultValue(state, R, V);
201 return state;
202 }
203
204 const TypedRegion *TR = cast<TypedRegion>(R);
205
206 QualType T = TR->getValueType(Ctx);
Zhongxing Xu43e2aaf2009-07-06 03:41:27 +0000207
208 if (Loc::IsLocType(T) || (T->isIntegerType() && T->isScalarType())) {
209 SVal V = ValMgr.getConjuredSymbolVal(E, T, Count);
Zhongxing Xu313b6da2009-07-06 06:01:24 +0000210 return Bind(state, ValMgr.makeLoc(TR), V);
Zhongxing Xu43e2aaf2009-07-06 03:41:27 +0000211 }
212 else if (const RecordType *RT = T->getAsStructureType()) {
213 // FIXME: handle structs with default region value.
214 const RecordDecl *RD = RT->getDecl()->getDefinition(Ctx);
215
216 // No record definition. There is nothing we can do.
217 if (!RD)
218 return state;
219
220 // Iterate through the fields and construct new symbols.
221 for (RecordDecl::field_iterator FI=RD->field_begin(),
222 FE=RD->field_end(); FI!=FE; ++FI) {
223
224 // For now just handle scalar fields.
225 FieldDecl *FD = *FI;
226 QualType FT = FD->getType();
Zhongxing Xu313b6da2009-07-06 06:01:24 +0000227 const FieldRegion* FR = MRMgr.getFieldRegion(FD, TR);
Zhongxing Xu43e2aaf2009-07-06 03:41:27 +0000228
229 if (Loc::IsLocType(FT) ||
230 (FT->isIntegerType() && FT->isScalarType())) {
231 SVal V = ValMgr.getConjuredSymbolVal(E, FT, Count);
232 state = state->bindLoc(ValMgr.makeLoc(FR), V);
233 }
234 else if (FT->isStructureType()) {
235 // set the default value of the struct field to conjured
236 // symbol. Note that the type of the symbol is irrelavant.
237 // We cannot use the type of the struct otherwise ValMgr won't
238 // give us the conjured symbol.
239 SVal V = ValMgr.getConjuredSymbolVal(E, Ctx.IntTy, Count);
240 state = setDefaultValue(state, FR, V);
241 }
242 }
243 } else if (const ArrayType *AT = Ctx.getAsArrayType(T)) {
244 // Set the default value of the array to conjured symbol.
245 SVal V = ValMgr.getConjuredSymbolVal(E, AT->getElementType(),
246 Count);
Zhongxing Xu313b6da2009-07-06 06:01:24 +0000247 state = setDefaultValue(state, TR, V);
Zhongxing Xu43e2aaf2009-07-06 03:41:27 +0000248 } else {
249 // Just blast away other values.
Zhongxing Xu313b6da2009-07-06 06:01:24 +0000250 state = Bind(state, ValMgr.makeLoc(TR), UnknownVal());
Zhongxing Xu43e2aaf2009-07-06 03:41:27 +0000251 }
252
253 return state;
254}