blob: 4f0e4a62e90c366f0053ad5ef057bd3c7873edde [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);
33
34 // Check cast to ObjCQualifiedID type.
35 if (ToTy->isObjCQualifiedIdType()) {
36 // FIXME: Record the type information aside.
37 return CastResult(state, R);
38 }
39
40 // CodeTextRegion should be cast to only function pointer type.
41 if (isa<CodeTextRegion>(R)) {
42 assert(CastToTy->isFunctionPointerType() || CastToTy->isBlockPointerType()
43 || (CastToTy->isPointerType()
44 && CastToTy->getAsPointerType()->getPointeeType()->isVoidType()));
45 return CastResult(state, R);
46 }
47
48 // 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.
68 if (const RecordType *RT = dyn_cast<RecordType>(PointeeTy.getTypePtr())) {
69 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 Kremenek30d1b992009-04-21 23:31:46 +0000124
Ted Kremenekca4e1b72009-07-06 20:53:52 +0000125 while (true) {
Ted Kremenek42530512009-05-06 18:19:24 +0000126 if (const TypedViewRegion *TR = dyn_cast<TypedViewRegion>(R)) {
127 // Casts to void* removes TypedViewRegion. This happens when:
128 //
129 // void foo(void*);
130 // ...
131 // void bar() {
132 // int x;
133 // foo(&x);
134 // }
135 //
136 R = TR->removeViews();
137 continue;
138 }
139 else if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
140 // Casts to void* also removes ElementRegions. This happens when:
141 //
142 // void foo(void*);
143 // ...
144 // void bar() {
145 // int x;
146 // foo((char*)&x);
147 // }
148 //
149 R = ER->getSuperRegion();
150 continue;
151 }
152 else
153 break;
154 }
Ted Kremenek30d1b992009-04-21 23:31:46 +0000155
156 return CastResult(state, R);
157 }
Ted Kremenekfd6b4f32009-05-04 06:35:49 +0000158 else if (Pointee->isIntegerType()) {
159 // FIXME: At some point, it stands to reason that this 'dyn_cast' should
160 // become a 'cast' and that 'R' will always be a TypedRegion.
161 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) {
162 // Check if we are casting to a region with an integer type. We now
163 // the types aren't the same, so we construct an ElementRegion.
Ted Kremenekcd9392f2009-05-04 15:17:38 +0000164 SVal Idx = ValMgr.makeZeroArrayIndex();
Ted Kremenek20bd7462009-05-04 07:04:36 +0000165
166 // If the super region is an element region, strip it away.
167 // FIXME: Is this the right thing to do in all cases?
Ted Kremenek19cfa2b2009-06-30 22:31:23 +0000168 const MemRegion *Base = isa<ElementRegion>(TR) ? TR->getSuperRegion()
169 : TR;
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000170 ElementRegion* ER = MRMgr.getElementRegion(Pointee, Idx, Base,
Ted Kremenek19cfa2b2009-06-30 22:31:23 +0000171 StateMgr.getContext());
Ted Kremenekfd6b4f32009-05-04 06:35:49 +0000172 return CastResult(state, ER);
173 }
174 }
175 }
Ted Kremenek30d1b992009-04-21 23:31:46 +0000176
Ted Kremeneka8607d12009-05-01 19:22:20 +0000177 // FIXME: Need to handle arbitrary downcasts.
178 // FIXME: Handle the case where a TypedViewRegion (layering a SymbolicRegion
179 // or an AllocaRegion is cast to another view, thus causing the memory
180 // to be re-used for a different purpose.
Ted Kremenek30d1b992009-04-21 23:31:46 +0000181
Ted Kremeneka8607d12009-05-01 19:22:20 +0000182 if (isa<SymbolicRegion>(R) || isa<AllocaRegion>(R)) {
183 const MemRegion* ViewR = MRMgr.getTypedViewRegion(CastToTy, R);
184 return CastResult(AddRegionView(state, ViewR, R), ViewR);
185 }
186
187 return CastResult(state, R);
Ted Kremenekc62abc12009-04-21 21:51:34 +0000188}
Zhongxing Xu43e2aaf2009-07-06 03:41:27 +0000189
190const GRState *StoreManager::InvalidateRegion(const GRState *state,
Zhongxing Xu313b6da2009-07-06 06:01:24 +0000191 const MemRegion *R,
Zhongxing Xu43e2aaf2009-07-06 03:41:27 +0000192 const Expr *E, unsigned Count) {
Zhongxing Xu313b6da2009-07-06 06:01:24 +0000193 ASTContext& Ctx = StateMgr.getContext();
194
Zhongxing Xu43e2aaf2009-07-06 03:41:27 +0000195 if (!R->isBoundable())
196 return state;
197
Zhongxing Xu313b6da2009-07-06 06:01:24 +0000198 if (isa<AllocaRegion>(R) || isa<SymbolicRegion>(R)) {
199 // Invalidate the alloca region by setting its default value to
200 // conjured symbol. The type of the symbol is irrelavant.
201 SVal V = ValMgr.getConjuredSymbolVal(E, Ctx.IntTy, Count);
202 state = setDefaultValue(state, R, V);
203 return state;
204 }
205
206 const TypedRegion *TR = cast<TypedRegion>(R);
207
208 QualType T = TR->getValueType(Ctx);
Zhongxing Xu43e2aaf2009-07-06 03:41:27 +0000209
210 if (Loc::IsLocType(T) || (T->isIntegerType() && T->isScalarType())) {
211 SVal V = ValMgr.getConjuredSymbolVal(E, T, Count);
Zhongxing Xu313b6da2009-07-06 06:01:24 +0000212 return Bind(state, ValMgr.makeLoc(TR), V);
Zhongxing Xu43e2aaf2009-07-06 03:41:27 +0000213 }
214 else if (const RecordType *RT = T->getAsStructureType()) {
215 // FIXME: handle structs with default region value.
216 const RecordDecl *RD = RT->getDecl()->getDefinition(Ctx);
217
218 // No record definition. There is nothing we can do.
219 if (!RD)
220 return state;
221
222 // Iterate through the fields and construct new symbols.
223 for (RecordDecl::field_iterator FI=RD->field_begin(),
224 FE=RD->field_end(); FI!=FE; ++FI) {
225
226 // For now just handle scalar fields.
227 FieldDecl *FD = *FI;
228 QualType FT = FD->getType();
Zhongxing Xu313b6da2009-07-06 06:01:24 +0000229 const FieldRegion* FR = MRMgr.getFieldRegion(FD, TR);
Zhongxing Xu43e2aaf2009-07-06 03:41:27 +0000230
231 if (Loc::IsLocType(FT) ||
232 (FT->isIntegerType() && FT->isScalarType())) {
233 SVal V = ValMgr.getConjuredSymbolVal(E, FT, Count);
234 state = state->bindLoc(ValMgr.makeLoc(FR), V);
235 }
236 else if (FT->isStructureType()) {
237 // set the default value of the struct field to conjured
238 // symbol. Note that the type of the symbol is irrelavant.
239 // We cannot use the type of the struct otherwise ValMgr won't
240 // give us the conjured symbol.
241 SVal V = ValMgr.getConjuredSymbolVal(E, Ctx.IntTy, Count);
242 state = setDefaultValue(state, FR, V);
243 }
244 }
245 } else if (const ArrayType *AT = Ctx.getAsArrayType(T)) {
246 // Set the default value of the array to conjured symbol.
247 SVal V = ValMgr.getConjuredSymbolVal(E, AT->getElementType(),
248 Count);
Zhongxing Xu313b6da2009-07-06 06:01:24 +0000249 state = setDefaultValue(state, TR, V);
Zhongxing Xu43e2aaf2009-07-06 03:41:27 +0000250 } else {
251 // Just blast away other values.
Zhongxing Xu313b6da2009-07-06 06:01:24 +0000252 state = Bind(state, ValMgr.makeLoc(TR), UnknownVal());
Zhongxing Xu43e2aaf2009-07-06 03:41:27 +0000253 }
254
255 return state;
256}