blob: c12065b89a04dccf46a5082dabc754e148fc423f [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 Kremenek67d12872009-12-07 22:05:27 +000094 case MemRegion::GlobalsSpaceRegionKind: {
Ted Kremenekfc8f57c2009-07-06 22:56:37 +000095 assert(0 && "Invalid region cast");
96 break;
Mike Stump1eb44332009-09-09 15:08:12 +000097 }
Ted Kremenekeb1c7a02009-11-25 01:32:22 +000098
99 case MemRegion::FunctionTextRegionKind:
Ted Kremenekbf0fe6c2009-11-25 23:58:21 +0000100 case MemRegion::BlockTextRegionKind:
101 case MemRegion::BlockDataRegionKind: {
Ted Kremenek63b9cfe2009-07-18 06:27:51 +0000102 // CodeTextRegion should be cast to only a function or block pointer type,
Zhongxing Xu09270cc2009-10-14 06:55:01 +0000103 // although they can in practice be casted to anything, e.g, void*, char*,
104 // etc.
105 // Just return the region.
106 return R;
Ted Kremenekfc8f57c2009-07-06 22:56:37 +0000107 }
Mike Stump1eb44332009-09-09 15:08:12 +0000108
Ted Kremenekfc8f57c2009-07-06 22:56:37 +0000109 case MemRegion::StringRegionKind:
Ted Kremenekfc8f57c2009-07-06 22:56:37 +0000110 // FIXME: Need to handle arbitrary downcasts.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000111 case MemRegion::SymbolicRegionKind:
112 case MemRegion::AllocaRegionKind:
Ted Kremenekfc8f57c2009-07-06 22:56:37 +0000113 case MemRegion::CompoundLiteralRegionKind:
Ted Kremenekfc8f57c2009-07-06 22:56:37 +0000114 case MemRegion::FieldRegionKind:
115 case MemRegion::ObjCIvarRegionKind:
Mike Stump1eb44332009-09-09 15:08:12 +0000116 case MemRegion::VarRegionKind:
Zhongxing Xubb141212009-12-16 11:27:52 +0000117 case MemRegion::CXXObjectRegionKind:
Zhongxing Xu09270cc2009-10-14 06:55:01 +0000118 return MakeElementRegion(R, PointeeTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000119
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000120 case MemRegion::ElementRegionKind: {
121 // If we are casting from an ElementRegion to another type, the
122 // algorithm is as follows:
123 //
124 // (1) Compute the "raw offset" of the ElementRegion from the
125 // base region. This is done by calling 'getAsRawOffset()'.
126 //
Mike Stump1eb44332009-09-09 15:08:12 +0000127 // (2a) If we get a 'RegionRawOffset' after calling
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000128 // 'getAsRawOffset()', determine if the absolute offset
Mike Stump1eb44332009-09-09 15:08:12 +0000129 // can be exactly divided into chunks of the size of the
130 // casted-pointee type. If so, create a new ElementRegion with
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000131 // the pointee-cast type as the new ElementType and the index
132 // being the offset divded by the chunk size. If not, create
133 // a new ElementRegion at offset 0 off the raw offset region.
134 //
135 // (2b) If we don't a get a 'RegionRawOffset' after calling
136 // 'getAsRawOffset()', it means that we are at offset 0.
Mike Stump1eb44332009-09-09 15:08:12 +0000137 //
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000138 // FIXME: Handle symbolic raw offsets.
Mike Stump1eb44332009-09-09 15:08:12 +0000139
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000140 const ElementRegion *elementR = cast<ElementRegion>(R);
141 const RegionRawOffset &rawOff = elementR->getAsRawOffset();
142 const MemRegion *baseR = rawOff.getRegion();
Mike Stump1eb44332009-09-09 15:08:12 +0000143
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000144 // If we cannot compute a raw offset, throw up our hands and return
145 // a NULL MemRegion*.
146 if (!baseR)
Zhongxing Xu09270cc2009-10-14 06:55:01 +0000147 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000148
Ken Dyck199c3d62010-01-11 17:06:35 +0000149 CharUnits off = CharUnits::fromQuantity(rawOff.getByteOffset());
Mike Stump1eb44332009-09-09 15:08:12 +0000150
Ken Dyck199c3d62010-01-11 17:06:35 +0000151 if (off.isZero()) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000152 // Edge case: we are at 0 bytes off the beginning of baseR. We
153 // check to see if type we are casting to is the same as the base
Mike Stump1eb44332009-09-09 15:08:12 +0000154 // region. If so, just return the base region.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000155 if (const TypedRegion *TR = dyn_cast<TypedRegion>(baseR)) {
156 QualType ObjTy = Ctx.getCanonicalType(TR->getValueType(Ctx));
157 QualType CanonPointeeTy = Ctx.getCanonicalType(PointeeTy);
158 if (CanonPointeeTy == ObjTy)
Zhongxing Xu09270cc2009-10-14 06:55:01 +0000159 return baseR;
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000160 }
Mike Stump1eb44332009-09-09 15:08:12 +0000161
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000162 // Otherwise, create a new ElementRegion at offset 0.
Zhongxing Xu09270cc2009-10-14 06:55:01 +0000163 return MakeElementRegion(baseR, PointeeTy);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000164 }
Mike Stump1eb44332009-09-09 15:08:12 +0000165
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000166 // We have a non-zero offset from the base region. We want to determine
167 // if the offset can be evenly divided by sizeof(PointeeTy). If so,
168 // we create an ElementRegion whose index is that value. Otherwise, we
169 // create two ElementRegions, one that reflects a raw offset and the other
170 // that reflects the cast.
Mike Stump1eb44332009-09-09 15:08:12 +0000171
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000172 // Compute the index for the new ElementRegion.
173 int64_t newIndex = 0;
174 const MemRegion *newSuperR = 0;
175
176 // We can only compute sizeof(PointeeTy) if it is a complete type.
177 if (IsCompleteType(Ctx, PointeeTy)) {
178 // Compute the size in **bytes**.
Ken Dyck199c3d62010-01-11 17:06:35 +0000179 CharUnits pointeeTySize = Ctx.getTypeSizeInChars(PointeeTy);
Ted Kremenek974d97b2010-04-07 00:46:49 +0000180 if (!pointeeTySize.isZero()) {
181 // Is the offset a multiple of the size? If so, we can layer the
182 // ElementRegion (with elementType == PointeeTy) directly on top of
183 // the base region.
184 if (off % pointeeTySize == 0) {
185 newIndex = off / pointeeTySize;
186 newSuperR = baseR;
187 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000188 }
189 }
Mike Stump1eb44332009-09-09 15:08:12 +0000190
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000191 if (!newSuperR) {
192 // Create an intermediate ElementRegion to represent the raw byte.
193 // This will be the super region of the final ElementRegion.
Ken Dyck199c3d62010-01-11 17:06:35 +0000194 newSuperR = MakeElementRegion(baseR, Ctx.CharTy, off.getQuantity());
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000195 }
Mike Stump1eb44332009-09-09 15:08:12 +0000196
Zhongxing Xu09270cc2009-10-14 06:55:01 +0000197 return MakeElementRegion(newSuperR, PointeeTy, newIndex);
Ted Kremenek48ce7de2009-07-06 20:21:51 +0000198 }
199 }
Mike Stump1eb44332009-09-09 15:08:12 +0000200
Zhongxing Xu09270cc2009-10-14 06:55:01 +0000201 assert(0 && "unreachable");
202 return 0;
Ted Kremenek48ce7de2009-07-06 20:21:51 +0000203}
Ted Kremenek1894dce2009-08-25 20:51:30 +0000204
205
206/// CastRetrievedVal - Used by subclasses of StoreManager to implement
207/// implicit casts that arise from loads from regions that are reinterpreted
208/// as another region.
Ted Kremenekc50e6df2010-01-11 02:33:26 +0000209SVal StoreManager::CastRetrievedVal(SVal V, const TypedRegion *R,
210 QualType castTy, bool performTestOnly) {
Ted Kremenek852274d2009-12-16 03:18:58 +0000211
Zhongxing Xu652be342009-11-16 04:49:44 +0000212 if (castTy.isNull())
213 return V;
Ted Kremenek852274d2009-12-16 03:18:58 +0000214
215 ASTContext &Ctx = ValMgr.getContext();
Zhongxing Xu2f4a6b22009-12-09 08:32:57 +0000216
Ted Kremenekc50e6df2010-01-11 02:33:26 +0000217 if (performTestOnly) {
218 // Automatically translate references to pointers.
219 QualType T = R->getValueType(Ctx);
220 if (const ReferenceType *RT = T->getAs<ReferenceType>())
221 T = Ctx.getPointerType(RT->getPointeeType());
222
223 assert(ValMgr.getContext().hasSameUnqualifiedType(castTy, T));
224 return V;
225 }
226
227 if (const Loc *L = dyn_cast<Loc>(&V))
228 return ValMgr.getSValuator().EvalCastL(*L, castTy);
229 else if (const NonLoc *NL = dyn_cast<NonLoc>(&V))
230 return ValMgr.getSValuator().EvalCastNL(*NL, castTy);
231
Zhongxing Xu652be342009-11-16 04:49:44 +0000232 return V;
Ted Kremenek1894dce2009-08-25 20:51:30 +0000233}
234
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000235Store StoreManager::InvalidateRegions(Store store,
236 const MemRegion * const *I,
237 const MemRegion * const *End,
238 const Expr *E, unsigned Count,
239 InvalidatedSymbols *IS) {
Ted Kremenek81a95832009-12-03 03:27:11 +0000240 for ( ; I != End ; ++I)
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000241 store = InvalidateRegion(store, *I, E, Count, IS);
Ted Kremenek81a95832009-12-03 03:27:11 +0000242
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000243 return store;
Ted Kremenek81a95832009-12-03 03:27:11 +0000244}
Zhongxing Xuc1511e02010-02-08 07:58:06 +0000245
246SVal StoreManager::getLValueFieldOrIvar(const Decl* D, SVal Base) {
247 if (Base.isUnknownOrUndef())
248 return Base;
249
250 Loc BaseL = cast<Loc>(Base);
251 const MemRegion* BaseR = 0;
252
253 switch (BaseL.getSubKind()) {
254 case loc::MemRegionKind:
255 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
256 break;
257
258 case loc::GotoLabelKind:
259 // These are anormal cases. Flag an undefined value.
260 return UndefinedVal();
261
262 case loc::ConcreteIntKind:
263 // While these seem funny, this can happen through casts.
264 // FIXME: What we should return is the field offset. For example,
265 // add the field offset to the integer value. That way funny things
266 // like this work properly: &(((struct foo *) 0xa)->f)
267 return Base;
268
269 default:
270 assert(0 && "Unhandled Base.");
271 return Base;
272 }
273
274 // NOTE: We must have this check first because ObjCIvarDecl is a subclass
275 // of FieldDecl.
276 if (const ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(D))
277 return loc::MemRegionVal(MRMgr.getObjCIvarRegion(ID, BaseR));
278
279 return loc::MemRegionVal(MRMgr.getFieldRegion(cast<FieldDecl>(D), BaseR));
280}
Zhongxing Xu52535682010-02-08 08:17:02 +0000281
282SVal StoreManager::getLValueElement(QualType elementType, SVal Offset,
283 SVal Base) {
284
285 // If the base is an unknown or undefined value, just return it back.
286 // FIXME: For absolute pointer addresses, we just return that value back as
287 // well, although in reality we should return the offset added to that
288 // value.
289 if (Base.isUnknownOrUndef() || isa<loc::ConcreteInt>(Base))
290 return Base;
291
292 // Only handle integer offsets... for now.
293 if (!isa<nonloc::ConcreteInt>(Offset))
294 return UnknownVal();
295
296 const MemRegion* BaseRegion = cast<loc::MemRegionVal>(Base).getRegion();
297
298 // Pointer of any type can be cast and used as array base.
299 const ElementRegion *ElemR = dyn_cast<ElementRegion>(BaseRegion);
300
301 // Convert the offset to the appropriate size and signedness.
302 Offset = ValMgr.convertToArrayIndex(Offset);
303
304 if (!ElemR) {
305 //
306 // If the base region is not an ElementRegion, create one.
307 // This can happen in the following example:
308 //
309 // char *p = __builtin_alloc(10);
310 // p[1] = 8;
311 //
312 // Observe that 'p' binds to an AllocaRegion.
313 //
314 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, Offset,
315 BaseRegion, Ctx));
316 }
317
318 SVal BaseIdx = ElemR->getIndex();
319
320 if (!isa<nonloc::ConcreteInt>(BaseIdx))
321 return UnknownVal();
322
323 const llvm::APSInt& BaseIdxI = cast<nonloc::ConcreteInt>(BaseIdx).getValue();
324 const llvm::APSInt& OffI = cast<nonloc::ConcreteInt>(Offset).getValue();
325 assert(BaseIdxI.isSigned());
326
327 // Compute the new index.
328 SVal NewIdx = nonloc::ConcreteInt(
329 ValMgr.getBasicValueFactory().getValue(BaseIdxI + OffI));
330
331 // Construct the new ElementRegion.
332 const MemRegion *ArrayR = ElemR->getSuperRegion();
333 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, NewIdx, ArrayR,
334 Ctx));
335}