blob: 114942177cc79e89c68e8df13985f76210c51cdd [file] [log] [blame]
Ted Kremenek4c91a402009-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 Kremenek026c5f82009-07-06 20:21:51 +000019StoreManager::StoreManager(GRStateManager &stateMgr, bool useNewCastRegion)
Ted Kremenek4c91a402009-04-21 21:51:34 +000020 : ValMgr(stateMgr.getValueManager()),
21 StateMgr(stateMgr),
Ted Kremenek026c5f82009-07-06 20:21:51 +000022 UseNewCastRegion(useNewCastRegion),
Ted Kremenek4c91a402009-04-21 21:51:34 +000023 MRMgr(ValMgr.getRegionManager()) {}
24
25StoreManager::CastResult
Ted Kremenekc36ff182009-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 Kremenek212538c2009-07-06 23:47:19 +000038static bool IsCompleteType(ASTContext &Ctx, QualType Ty) {
Ted Kremenekd9b39bf2009-07-17 17:50:17 +000039 if (const RecordType *RT = Ty->getAsRecordType()) {
Ted Kremenek212538c2009-07-06 23:47:19 +000040 const RecordDecl *D = RT->getDecl();
41 if (!D->getDefinition(Ctx))
42 return false;
43 }
44
45 return true;
46}
47
Ted Kremenekc36ff182009-07-06 22:23:45 +000048StoreManager::CastResult
Ted Kremenek026c5f82009-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 Kremenekc36ff182009-07-06 22:23:45 +000056
Ted Kremenek89f81252009-07-06 22:39:40 +000057 // Handle casts to Objective-C objects.
Steve Naroffad75bd22009-07-16 15:41:00 +000058 if (CastToTy->isObjCObjectPointerType()) {
Ted Kremenekd868f102009-07-06 22:56:37 +000059 state = setCastType(state, R, CastToTy);
Ted Kremenek6af5f112009-07-06 21:01:16 +000060 return CastResult(state, R);
61 }
Ted Kremenek6fde9872009-07-18 06:27:51 +000062
63 if (CastToTy->isBlockPointerType()) {
64 if (isa<CodeTextRegion>(R))
65 return CastResult(state, R);
66
67 // FIXME: This may not be the right approach, depending on the symbol
68 // involved. Blocks can be casted to/from 'id', as they can be treated
69 // as Objective-C objects.
70 if (SymbolRef sym = loc::MemRegionVal(R).getAsSymbol()) {
71 R = MRMgr.getCodeTextRegion(sym, CastToTy);
72 return CastResult(state, R);
73 }
74
75 // We don't know what to make of it. Return a NULL region, which
76 // will be interpretted as UnknownVal.
77 return CastResult(state, NULL);
78 }
Ted Kremenekc36ff182009-07-06 22:23:45 +000079
Ted Kremenek026c5f82009-07-06 20:21:51 +000080 // Now assume we are casting from pointer to pointer. Other cases should
81 // already be handled.
Ted Kremenekd9b39bf2009-07-17 17:50:17 +000082 QualType PointeeTy = CastToTy->getAsPointerType()->getPointeeType();
Ted Kremenek212538c2009-07-06 23:47:19 +000083
Ted Kremenek026c5f82009-07-06 20:21:51 +000084 // Process region cast according to the kind of the region being cast.
Ted Kremenekd868f102009-07-06 22:56:37 +000085 switch (R->getKind()) {
86 case MemRegion::BEG_TYPED_REGIONS:
87 case MemRegion::MemSpaceRegionKind:
88 case MemRegion::BEG_DECL_REGIONS:
89 case MemRegion::END_DECL_REGIONS:
90 case MemRegion::END_TYPED_REGIONS:
91 case MemRegion::TypedViewRegionKind: {
92 assert(0 && "Invalid region cast");
93 break;
Ted Kremenek026c5f82009-07-06 20:21:51 +000094 }
Ted Kremenekd868f102009-07-06 22:56:37 +000095
96 case MemRegion::CodeTextRegionKind: {
Ted Kremenek6fde9872009-07-18 06:27:51 +000097 // CodeTextRegion should be cast to only a function or block pointer type,
98 // although they can in practice be casted to anything, e.g, void*,
99 // char*, etc.
Ted Kremenek92e43982009-07-10 21:24:45 +0000100 // Just pass the region through.
Ted Kremenekd868f102009-07-06 22:56:37 +0000101 break;
102 }
103
104 case MemRegion::StringRegionKind:
105 // Handle casts of string literals.
106 return MakeElementRegion(state, R, PointeeTy, CastToTy);
107
108 case MemRegion::ObjCObjectRegionKind:
109 case MemRegion::SymbolicRegionKind:
110 // FIXME: Need to handle arbitrary downcasts.
111 case MemRegion::AllocaRegionKind: {
112 state = setCastType(state, R, CastToTy);
113 break;
114 }
115
116 case MemRegion::CompoundLiteralRegionKind:
117 case MemRegion::ElementRegionKind:
118 case MemRegion::FieldRegionKind:
119 case MemRegion::ObjCIvarRegionKind:
120 case MemRegion::VarRegionKind: {
Ted Kremenek8da696e2009-07-06 22:59:23 +0000121 // VarRegion, ElementRegion, and FieldRegion has an inherent type.
122 // Normally they should not be cast. We only layer an ElementRegion when
123 // the cast-to pointee type is of smaller size. In other cases, we return
124 // the original VarRegion.
Ted Kremenekd868f102009-07-06 22:56:37 +0000125
Zhongxing Xuabf51292009-07-07 01:36:53 +0000126 // If the pointee or object type is incomplete, do not compute their
127 // sizes, and return the original region.
Ted Kremenekd868f102009-07-06 22:56:37 +0000128 QualType ObjTy = cast<TypedRegion>(R)->getValueType(Ctx);
Ted Kremenek212538c2009-07-06 23:47:19 +0000129
130 if (!IsCompleteType(Ctx, PointeeTy) || !IsCompleteType(Ctx, ObjTy)) {
131 state = setCastType(state, R, ToTy);
132 break;
133 }
134
Ted Kremenekd868f102009-07-06 22:56:37 +0000135 uint64_t PointeeTySize = Ctx.getTypeSize(PointeeTy);
136 uint64_t ObjTySize = Ctx.getTypeSize(ObjTy);
137
138 if ((PointeeTySize > 0 && PointeeTySize < ObjTySize) ||
139 (ObjTy->isAggregateType() && PointeeTy->isScalarType()) ||
140 ObjTySize == 0 /* R has 'void*' type. */)
141 return MakeElementRegion(state, R, PointeeTy, ToTy);
142
Ted Kremenek026c5f82009-07-06 20:21:51 +0000143 state = setCastType(state, R, ToTy);
Ted Kremenekd868f102009-07-06 22:56:37 +0000144 break;
Ted Kremenek026c5f82009-07-06 20:21:51 +0000145 }
146 }
147
Ted Kremenekd868f102009-07-06 22:56:37 +0000148 return CastResult(state, R);
Ted Kremenek026c5f82009-07-06 20:21:51 +0000149}
150
151
152StoreManager::CastResult
153StoreManager::OldCastRegion(const GRState* state, const MemRegion* R,
Ted Kremeneka7db02a2009-05-04 06:35:49 +0000154 QualType CastToTy) {
Ted Kremenek4c91a402009-04-21 21:51:34 +0000155
Ted Kremenekb68251c2009-04-21 23:31:46 +0000156 ASTContext& Ctx = StateMgr.getContext();
157
158 // We need to know the real type of CastToTy.
159 QualType ToTy = Ctx.getCanonicalType(CastToTy);
160
Ted Kremenek4c91a402009-04-21 21:51:34 +0000161 // Return the same region if the region types are compatible.
162 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R)) {
Zhongxing Xub02c8522009-05-09 00:50:33 +0000163 QualType Ta = Ctx.getCanonicalType(TR->getLocationType(Ctx));
Ted Kremenekb68251c2009-04-21 23:31:46 +0000164
165 if (Ta == ToTy)
Ted Kremenek4c91a402009-04-21 21:51:34 +0000166 return CastResult(state, R);
167 }
168
Ted Kremeneka7db02a2009-05-04 06:35:49 +0000169 if (const PointerType* PTy = dyn_cast<PointerType>(ToTy.getTypePtr())) {
170 // Check if we are casting to 'void*'.
171 // FIXME: Handle arbitrary upcasts.
172 QualType Pointee = PTy->getPointeeType();
173 if (Pointee->isVoidType()) {
Ted Kremenek7774fec2009-07-06 20:53:52 +0000174 while (true) {
Ted Kremenek73ec7732009-05-06 18:19:24 +0000175 if (const TypedViewRegion *TR = dyn_cast<TypedViewRegion>(R)) {
176 // Casts to void* removes TypedViewRegion. This happens when:
177 //
178 // void foo(void*);
179 // ...
180 // void bar() {
181 // int x;
182 // foo(&x);
183 // }
184 //
185 R = TR->removeViews();
186 continue;
187 }
188 else if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
189 // Casts to void* also removes ElementRegions. This happens when:
190 //
191 // void foo(void*);
192 // ...
193 // void bar() {
194 // int x;
195 // foo((char*)&x);
196 // }
197 //
198 R = ER->getSuperRegion();
199 continue;
200 }
Ted Kremenek6af5f112009-07-06 21:01:16 +0000201
202 break;
Ted Kremenek73ec7732009-05-06 18:19:24 +0000203 }
Ted Kremenekb68251c2009-04-21 23:31:46 +0000204
205 return CastResult(state, R);
206 }
Ted Kremeneka7db02a2009-05-04 06:35:49 +0000207 else if (Pointee->isIntegerType()) {
208 // FIXME: At some point, it stands to reason that this 'dyn_cast' should
209 // become a 'cast' and that 'R' will always be a TypedRegion.
210 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) {
211 // Check if we are casting to a region with an integer type. We now
212 // the types aren't the same, so we construct an ElementRegion.
Ted Kremenekd8ccb2b2009-05-04 15:17:38 +0000213 SVal Idx = ValMgr.makeZeroArrayIndex();
Ted Kremenek440f2ce2009-05-04 07:04:36 +0000214
215 // If the super region is an element region, strip it away.
216 // FIXME: Is this the right thing to do in all cases?
Ted Kremenek97c443c2009-06-30 22:31:23 +0000217 const MemRegion *Base = isa<ElementRegion>(TR) ? TR->getSuperRegion()
218 : TR;
Zhongxing Xufe483ee2009-06-16 09:55:50 +0000219 ElementRegion* ER = MRMgr.getElementRegion(Pointee, Idx, Base,
Ted Kremenek97c443c2009-06-30 22:31:23 +0000220 StateMgr.getContext());
Ted Kremeneka7db02a2009-05-04 06:35:49 +0000221 return CastResult(state, ER);
222 }
223 }
224 }
Ted Kremenekb68251c2009-04-21 23:31:46 +0000225
Ted Kremenek8871d5a2009-05-01 19:22:20 +0000226 // FIXME: Need to handle arbitrary downcasts.
227 // FIXME: Handle the case where a TypedViewRegion (layering a SymbolicRegion
228 // or an AllocaRegion is cast to another view, thus causing the memory
229 // to be re-used for a different purpose.
Ted Kremenek8871d5a2009-05-01 19:22:20 +0000230 if (isa<SymbolicRegion>(R) || isa<AllocaRegion>(R)) {
231 const MemRegion* ViewR = MRMgr.getTypedViewRegion(CastToTy, R);
232 return CastResult(AddRegionView(state, ViewR, R), ViewR);
233 }
234
235 return CastResult(state, R);
Ted Kremenek4c91a402009-04-21 21:51:34 +0000236}