blob: b12833170506170fef083878aab36c554ce5270c [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
Ted Kremenek1309f9a2010-01-25 04:41:41 +000014#include "clang/Checker/PathSensitive/Store.h"
15#include "clang/Checker/PathSensitive/GRState.h"
Ken Dyck199c3d62010-01-11 17:06:35 +000016#include "clang/AST/CharUnits.h"
Ted Kremenekc62abc12009-04-21 21:51:34 +000017
18using namespace clang;
19
Ted Kremenekf7a0cf42009-07-29 21:43:22 +000020StoreManager::StoreManager(GRStateManager &stateMgr)
21 : ValMgr(stateMgr.getValueManager()), StateMgr(stateMgr),
Zhongxing Xu2a393db2010-02-08 06:00:22 +000022 MRMgr(ValMgr.getRegionManager()), Ctx(stateMgr.getContext()) {}
Ted Kremenekc62abc12009-04-21 21:51:34 +000023
Zhongxing Xu09270cc2009-10-14 06:55:01 +000024const MemRegion *StoreManager::MakeElementRegion(const MemRegion *Base,
Zhongxing Xu652be342009-11-16 04:49:44 +000025 QualType EleTy, uint64_t index) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +000026 SVal idx = ValMgr.makeArrayIndex(index);
Zhongxing Xu09270cc2009-10-14 06:55:01 +000027 return MRMgr.getElementRegion(EleTy, idx, Base, ValMgr.getContext());
Ted Kremenek411af402009-07-06 22:23:45 +000028}
29
Ted Kremenek19e1f0b2009-08-01 06:17:29 +000030// FIXME: Merge with the implementation of the same method in MemRegion.cpp
Ted Kremenek169077d2009-07-06 23:47:19 +000031static bool IsCompleteType(ASTContext &Ctx, QualType Ty) {
Ted Kremenek6217b802009-07-29 21:53:49 +000032 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Ted Kremenek169077d2009-07-06 23:47:19 +000033 const RecordDecl *D = RT->getDecl();
Douglas Gregor952b0172010-02-11 01:04:33 +000034 if (!D->getDefinition())
Ted Kremenek169077d2009-07-06 23:47:19 +000035 return false;
36 }
Mike Stump1eb44332009-09-09 15:08:12 +000037
Ted Kremenek169077d2009-07-06 23:47:19 +000038 return true;
39}
40
Zhongxing Xu856c6bc2010-04-19 11:47:28 +000041const ElementRegion *StoreManager::GetElementZeroRegion(const MemRegion *R,
42 QualType T) {
43 SVal idx = ValMgr.makeZeroArrayIndex();
44 assert(!T.isNull());
45 return MRMgr.getElementRegion(T, idx, R, Ctx);
46}
47
Zhongxing Xu09270cc2009-10-14 06:55:01 +000048const MemRegion *StoreManager::CastRegion(const MemRegion *R, QualType CastToTy) {
Mike Stump1eb44332009-09-09 15:08:12 +000049
Ted Kremenek48ce7de2009-07-06 20:21:51 +000050 ASTContext& Ctx = StateMgr.getContext();
Mike Stump1eb44332009-09-09 15:08:12 +000051
Ted Kremenekb9a44252009-07-06 22:39:40 +000052 // Handle casts to Objective-C objects.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +000053 if (CastToTy->isObjCObjectPointerType())
Zhongxing Xu479529e2009-11-10 02:17:20 +000054 return R->StripCasts();
Ted Kremenek19e1f0b2009-08-01 06:17:29 +000055
Ted Kremenek63b9cfe2009-07-18 06:27:51 +000056 if (CastToTy->isBlockPointerType()) {
Ted Kremenekabd46e12009-08-28 04:49:15 +000057 // FIXME: We may need different solutions, depending on the symbol
Ted Kremenek63b9cfe2009-07-18 06:27:51 +000058 // involved. Blocks can be casted to/from 'id', as they can be treated
Ted Kremenekabd46e12009-08-28 04:49:15 +000059 // as Objective-C objects. This could possibly be handled by enhancing
Mike Stump1eb44332009-09-09 15:08:12 +000060 // our reasoning of downcasts of symbolic objects.
Ted Kremenekabd46e12009-08-28 04:49:15 +000061 if (isa<CodeTextRegion>(R) || isa<SymbolicRegion>(R))
Zhongxing Xu09270cc2009-10-14 06:55:01 +000062 return R;
Ted Kremenek63b9cfe2009-07-18 06:27:51 +000063
64 // We don't know what to make of it. Return a NULL region, which
65 // will be interpretted as UnknownVal.
Zhongxing Xu09270cc2009-10-14 06:55:01 +000066 return NULL;
Ted Kremenek63b9cfe2009-07-18 06:27:51 +000067 }
Ted Kremenek411af402009-07-06 22:23:45 +000068
Ted Kremenek48ce7de2009-07-06 20:21:51 +000069 // Now assume we are casting from pointer to pointer. Other cases should
70 // already be handled.
Ted Kremenek6217b802009-07-29 21:53:49 +000071 QualType PointeeTy = CastToTy->getAs<PointerType>()->getPointeeType();
Ted Kremenek9a108eb2009-08-02 04:12:53 +000072 QualType CanonPointeeTy = Ctx.getCanonicalType(PointeeTy);
73
74 // Handle casts to void*. We just pass the region through.
Douglas Gregora4923eb2009-11-16 21:35:15 +000075 if (CanonPointeeTy.getLocalUnqualifiedType() == Ctx.VoidTy)
Zhongxing Xu09270cc2009-10-14 06:55:01 +000076 return R;
Mike Stump1eb44332009-09-09 15:08:12 +000077
Ted Kremenek9a108eb2009-08-02 04:12:53 +000078 // Handle casts from compatible types.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +000079 if (R->isBoundable())
80 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) {
81 QualType ObjTy = Ctx.getCanonicalType(TR->getValueType(Ctx));
Ted Kremenek9a108eb2009-08-02 04:12:53 +000082 if (CanonPointeeTy == ObjTy)
Zhongxing Xu09270cc2009-10-14 06:55:01 +000083 return R;
Ted Kremenek19e1f0b2009-08-01 06:17:29 +000084 }
85
Ted Kremenek48ce7de2009-07-06 20:21:51 +000086 // Process region cast according to the kind of the region being cast.
Ted Kremenekfc8f57c2009-07-06 22:56:37 +000087 switch (R->getKind()) {
Ted Kremenekde0d2632010-01-05 02:18:06 +000088 case MemRegion::CXXThisRegionKind:
Ted Kremenek67d12872009-12-07 22:05:27 +000089 case MemRegion::GenericMemSpaceRegionKind:
90 case MemRegion::StackLocalsSpaceRegionKind:
91 case MemRegion::StackArgumentsSpaceRegionKind:
92 case MemRegion::HeapSpaceRegionKind:
Ted Kremenek2b87ae42009-12-11 06:43:27 +000093 case MemRegion::UnknownSpaceRegionKind:
Ted Kremenekdcee3ce2010-07-01 20:16:50 +000094 case MemRegion::NonStaticGlobalSpaceRegionKind:
95 case MemRegion::StaticGlobalSpaceRegionKind: {
Ted Kremenekfc8f57c2009-07-06 22:56:37 +000096 assert(0 && "Invalid region cast");
97 break;
Mike Stump1eb44332009-09-09 15:08:12 +000098 }
Ted Kremenekeb1c7a02009-11-25 01:32:22 +000099
100 case MemRegion::FunctionTextRegionKind:
Ted Kremenekbf0fe6c2009-11-25 23:58:21 +0000101 case MemRegion::BlockTextRegionKind:
102 case MemRegion::BlockDataRegionKind: {
Ted Kremenek63b9cfe2009-07-18 06:27:51 +0000103 // CodeTextRegion should be cast to only a function or block pointer type,
Zhongxing Xu09270cc2009-10-14 06:55:01 +0000104 // although they can in practice be casted to anything, e.g, void*, char*,
105 // etc.
106 // Just return the region.
107 return R;
Ted Kremenekfc8f57c2009-07-06 22:56:37 +0000108 }
Mike Stump1eb44332009-09-09 15:08:12 +0000109
Ted Kremenekfc8f57c2009-07-06 22:56:37 +0000110 case MemRegion::StringRegionKind:
Ted Kremenekfc8f57c2009-07-06 22:56:37 +0000111 // FIXME: Need to handle arbitrary downcasts.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000112 case MemRegion::SymbolicRegionKind:
113 case MemRegion::AllocaRegionKind:
Ted Kremenekfc8f57c2009-07-06 22:56:37 +0000114 case MemRegion::CompoundLiteralRegionKind:
Ted Kremenekfc8f57c2009-07-06 22:56:37 +0000115 case MemRegion::FieldRegionKind:
116 case MemRegion::ObjCIvarRegionKind:
Mike Stump1eb44332009-09-09 15:08:12 +0000117 case MemRegion::VarRegionKind:
Zhongxing Xubb141212009-12-16 11:27:52 +0000118 case MemRegion::CXXObjectRegionKind:
Zhongxing Xu09270cc2009-10-14 06:55:01 +0000119 return MakeElementRegion(R, PointeeTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000120
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000121 case MemRegion::ElementRegionKind: {
122 // If we are casting from an ElementRegion to another type, the
123 // algorithm is as follows:
124 //
125 // (1) Compute the "raw offset" of the ElementRegion from the
126 // base region. This is done by calling 'getAsRawOffset()'.
127 //
Mike Stump1eb44332009-09-09 15:08:12 +0000128 // (2a) If we get a 'RegionRawOffset' after calling
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000129 // 'getAsRawOffset()', determine if the absolute offset
Mike Stump1eb44332009-09-09 15:08:12 +0000130 // can be exactly divided into chunks of the size of the
131 // casted-pointee type. If so, create a new ElementRegion with
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000132 // the pointee-cast type as the new ElementType and the index
133 // being the offset divded by the chunk size. If not, create
134 // a new ElementRegion at offset 0 off the raw offset region.
135 //
136 // (2b) If we don't a get a 'RegionRawOffset' after calling
137 // 'getAsRawOffset()', it means that we are at offset 0.
Mike Stump1eb44332009-09-09 15:08:12 +0000138 //
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000139 // FIXME: Handle symbolic raw offsets.
Mike Stump1eb44332009-09-09 15:08:12 +0000140
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000141 const ElementRegion *elementR = cast<ElementRegion>(R);
142 const RegionRawOffset &rawOff = elementR->getAsRawOffset();
143 const MemRegion *baseR = rawOff.getRegion();
Mike Stump1eb44332009-09-09 15:08:12 +0000144
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000145 // If we cannot compute a raw offset, throw up our hands and return
146 // a NULL MemRegion*.
147 if (!baseR)
Zhongxing Xu09270cc2009-10-14 06:55:01 +0000148 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000149
Ken Dyck199c3d62010-01-11 17:06:35 +0000150 CharUnits off = CharUnits::fromQuantity(rawOff.getByteOffset());
Mike Stump1eb44332009-09-09 15:08:12 +0000151
Ken Dyck199c3d62010-01-11 17:06:35 +0000152 if (off.isZero()) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000153 // Edge case: we are at 0 bytes off the beginning of baseR. We
154 // check to see if type we are casting to is the same as the base
Mike Stump1eb44332009-09-09 15:08:12 +0000155 // region. If so, just return the base region.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000156 if (const TypedRegion *TR = dyn_cast<TypedRegion>(baseR)) {
157 QualType ObjTy = Ctx.getCanonicalType(TR->getValueType(Ctx));
158 QualType CanonPointeeTy = Ctx.getCanonicalType(PointeeTy);
159 if (CanonPointeeTy == ObjTy)
Zhongxing Xu09270cc2009-10-14 06:55:01 +0000160 return baseR;
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000161 }
Mike Stump1eb44332009-09-09 15:08:12 +0000162
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000163 // Otherwise, create a new ElementRegion at offset 0.
Zhongxing Xu09270cc2009-10-14 06:55:01 +0000164 return MakeElementRegion(baseR, PointeeTy);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000165 }
Mike Stump1eb44332009-09-09 15:08:12 +0000166
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000167 // We have a non-zero offset from the base region. We want to determine
168 // if the offset can be evenly divided by sizeof(PointeeTy). If so,
169 // we create an ElementRegion whose index is that value. Otherwise, we
170 // create two ElementRegions, one that reflects a raw offset and the other
171 // that reflects the cast.
Mike Stump1eb44332009-09-09 15:08:12 +0000172
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000173 // Compute the index for the new ElementRegion.
174 int64_t newIndex = 0;
175 const MemRegion *newSuperR = 0;
176
177 // We can only compute sizeof(PointeeTy) if it is a complete type.
178 if (IsCompleteType(Ctx, PointeeTy)) {
179 // Compute the size in **bytes**.
Ken Dyck199c3d62010-01-11 17:06:35 +0000180 CharUnits pointeeTySize = Ctx.getTypeSizeInChars(PointeeTy);
Ted Kremenek974d97b2010-04-07 00:46:49 +0000181 if (!pointeeTySize.isZero()) {
182 // Is the offset a multiple of the size? If so, we can layer the
183 // ElementRegion (with elementType == PointeeTy) directly on top of
184 // the base region.
185 if (off % pointeeTySize == 0) {
186 newIndex = off / pointeeTySize;
187 newSuperR = baseR;
188 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000189 }
190 }
Mike Stump1eb44332009-09-09 15:08:12 +0000191
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000192 if (!newSuperR) {
193 // Create an intermediate ElementRegion to represent the raw byte.
194 // This will be the super region of the final ElementRegion.
Ken Dyck199c3d62010-01-11 17:06:35 +0000195 newSuperR = MakeElementRegion(baseR, Ctx.CharTy, off.getQuantity());
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000196 }
Mike Stump1eb44332009-09-09 15:08:12 +0000197
Zhongxing Xu09270cc2009-10-14 06:55:01 +0000198 return MakeElementRegion(newSuperR, PointeeTy, newIndex);
Ted Kremenek48ce7de2009-07-06 20:21:51 +0000199 }
200 }
Mike Stump1eb44332009-09-09 15:08:12 +0000201
Zhongxing Xu09270cc2009-10-14 06:55:01 +0000202 assert(0 && "unreachable");
203 return 0;
Ted Kremenek48ce7de2009-07-06 20:21:51 +0000204}
Ted Kremenek1894dce2009-08-25 20:51:30 +0000205
206
207/// CastRetrievedVal - Used by subclasses of StoreManager to implement
208/// implicit casts that arise from loads from regions that are reinterpreted
209/// as another region.
Ted Kremenekc50e6df2010-01-11 02:33:26 +0000210SVal StoreManager::CastRetrievedVal(SVal V, const TypedRegion *R,
211 QualType castTy, bool performTestOnly) {
Ted Kremenek852274d2009-12-16 03:18:58 +0000212
Zhongxing Xu652be342009-11-16 04:49:44 +0000213 if (castTy.isNull())
214 return V;
Ted Kremenek852274d2009-12-16 03:18:58 +0000215
216 ASTContext &Ctx = ValMgr.getContext();
Zhongxing Xu2f4a6b22009-12-09 08:32:57 +0000217
Ted Kremenekc50e6df2010-01-11 02:33:26 +0000218 if (performTestOnly) {
219 // Automatically translate references to pointers.
220 QualType T = R->getValueType(Ctx);
221 if (const ReferenceType *RT = T->getAs<ReferenceType>())
222 T = Ctx.getPointerType(RT->getPointeeType());
223
224 assert(ValMgr.getContext().hasSameUnqualifiedType(castTy, T));
225 return V;
226 }
227
228 if (const Loc *L = dyn_cast<Loc>(&V))
229 return ValMgr.getSValuator().EvalCastL(*L, castTy);
230 else if (const NonLoc *NL = dyn_cast<NonLoc>(&V))
231 return ValMgr.getSValuator().EvalCastNL(*NL, castTy);
232
Zhongxing Xu652be342009-11-16 04:49:44 +0000233 return V;
Ted Kremenek1894dce2009-08-25 20:51:30 +0000234}
235
Zhongxing Xuc1511e02010-02-08 07:58:06 +0000236SVal StoreManager::getLValueFieldOrIvar(const Decl* D, SVal Base) {
237 if (Base.isUnknownOrUndef())
238 return Base;
239
240 Loc BaseL = cast<Loc>(Base);
241 const MemRegion* BaseR = 0;
242
243 switch (BaseL.getSubKind()) {
244 case loc::MemRegionKind:
245 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
246 break;
247
248 case loc::GotoLabelKind:
249 // These are anormal cases. Flag an undefined value.
250 return UndefinedVal();
251
252 case loc::ConcreteIntKind:
253 // While these seem funny, this can happen through casts.
254 // FIXME: What we should return is the field offset. For example,
255 // add the field offset to the integer value. That way funny things
256 // like this work properly: &(((struct foo *) 0xa)->f)
257 return Base;
258
259 default:
260 assert(0 && "Unhandled Base.");
261 return Base;
262 }
263
264 // NOTE: We must have this check first because ObjCIvarDecl is a subclass
265 // of FieldDecl.
266 if (const ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(D))
267 return loc::MemRegionVal(MRMgr.getObjCIvarRegion(ID, BaseR));
268
269 return loc::MemRegionVal(MRMgr.getFieldRegion(cast<FieldDecl>(D), BaseR));
270}
Zhongxing Xu52535682010-02-08 08:17:02 +0000271
272SVal StoreManager::getLValueElement(QualType elementType, SVal Offset,
273 SVal Base) {
274
275 // If the base is an unknown or undefined value, just return it back.
276 // FIXME: For absolute pointer addresses, we just return that value back as
277 // well, although in reality we should return the offset added to that
278 // value.
279 if (Base.isUnknownOrUndef() || isa<loc::ConcreteInt>(Base))
280 return Base;
281
282 // Only handle integer offsets... for now.
283 if (!isa<nonloc::ConcreteInt>(Offset))
284 return UnknownVal();
285
286 const MemRegion* BaseRegion = cast<loc::MemRegionVal>(Base).getRegion();
287
288 // Pointer of any type can be cast and used as array base.
289 const ElementRegion *ElemR = dyn_cast<ElementRegion>(BaseRegion);
290
291 // Convert the offset to the appropriate size and signedness.
292 Offset = ValMgr.convertToArrayIndex(Offset);
293
294 if (!ElemR) {
295 //
296 // If the base region is not an ElementRegion, create one.
297 // This can happen in the following example:
298 //
299 // char *p = __builtin_alloc(10);
300 // p[1] = 8;
301 //
302 // Observe that 'p' binds to an AllocaRegion.
303 //
304 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, Offset,
305 BaseRegion, Ctx));
306 }
307
308 SVal BaseIdx = ElemR->getIndex();
309
310 if (!isa<nonloc::ConcreteInt>(BaseIdx))
311 return UnknownVal();
312
313 const llvm::APSInt& BaseIdxI = cast<nonloc::ConcreteInt>(BaseIdx).getValue();
314 const llvm::APSInt& OffI = cast<nonloc::ConcreteInt>(Offset).getValue();
315 assert(BaseIdxI.isSigned());
316
317 // Compute the new index.
318 SVal NewIdx = nonloc::ConcreteInt(
319 ValMgr.getBasicValueFactory().getValue(BaseIdxI + OffI));
320
321 // Construct the new ElementRegion.
322 const MemRegion *ArrayR = ElemR->getSuperRegion();
323 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, NewIdx, ArrayR,
324 Ctx));
325}