blob: 1cb5cd70cae6957731e05df71536a57178c9522b [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
Jordy Roseff59efd2010-08-03 20:44:35 +000024Store StoreManager::EnterStackFrame(const GRState *state,
25 const StackFrameContext *frame) {
26 return state->getStore();
27}
28
Zhongxing Xu09270cc2009-10-14 06:55:01 +000029const MemRegion *StoreManager::MakeElementRegion(const MemRegion *Base,
Zhongxing Xu652be342009-11-16 04:49:44 +000030 QualType EleTy, uint64_t index) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +000031 SVal idx = ValMgr.makeArrayIndex(index);
Zhongxing Xu09270cc2009-10-14 06:55:01 +000032 return MRMgr.getElementRegion(EleTy, idx, Base, ValMgr.getContext());
Ted Kremenek411af402009-07-06 22:23:45 +000033}
34
Ted Kremenek19e1f0b2009-08-01 06:17:29 +000035// FIXME: Merge with the implementation of the same method in MemRegion.cpp
Ted Kremenek169077d2009-07-06 23:47:19 +000036static bool IsCompleteType(ASTContext &Ctx, QualType Ty) {
Ted Kremenek6217b802009-07-29 21:53:49 +000037 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Ted Kremenek169077d2009-07-06 23:47:19 +000038 const RecordDecl *D = RT->getDecl();
Douglas Gregor952b0172010-02-11 01:04:33 +000039 if (!D->getDefinition())
Ted Kremenek169077d2009-07-06 23:47:19 +000040 return false;
41 }
Mike Stump1eb44332009-09-09 15:08:12 +000042
Ted Kremenek169077d2009-07-06 23:47:19 +000043 return true;
44}
45
Zhongxing Xu856c6bc2010-04-19 11:47:28 +000046const ElementRegion *StoreManager::GetElementZeroRegion(const MemRegion *R,
47 QualType T) {
48 SVal idx = ValMgr.makeZeroArrayIndex();
49 assert(!T.isNull());
50 return MRMgr.getElementRegion(T, idx, R, Ctx);
51}
52
Zhongxing Xu09270cc2009-10-14 06:55:01 +000053const MemRegion *StoreManager::CastRegion(const MemRegion *R, QualType CastToTy) {
Mike Stump1eb44332009-09-09 15:08:12 +000054
Ted Kremenek48ce7de2009-07-06 20:21:51 +000055 ASTContext& Ctx = StateMgr.getContext();
Mike Stump1eb44332009-09-09 15:08:12 +000056
Ted Kremenekb9a44252009-07-06 22:39:40 +000057 // Handle casts to Objective-C objects.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +000058 if (CastToTy->isObjCObjectPointerType())
Zhongxing Xu479529e2009-11-10 02:17:20 +000059 return R->StripCasts();
Ted Kremenek19e1f0b2009-08-01 06:17:29 +000060
Ted Kremenek63b9cfe2009-07-18 06:27:51 +000061 if (CastToTy->isBlockPointerType()) {
Ted Kremenekabd46e12009-08-28 04:49:15 +000062 // FIXME: We may need different solutions, depending on the symbol
Ted Kremenek63b9cfe2009-07-18 06:27:51 +000063 // involved. Blocks can be casted to/from 'id', as they can be treated
Ted Kremenekabd46e12009-08-28 04:49:15 +000064 // as Objective-C objects. This could possibly be handled by enhancing
Mike Stump1eb44332009-09-09 15:08:12 +000065 // our reasoning of downcasts of symbolic objects.
Ted Kremenekabd46e12009-08-28 04:49:15 +000066 if (isa<CodeTextRegion>(R) || isa<SymbolicRegion>(R))
Zhongxing Xu09270cc2009-10-14 06:55:01 +000067 return R;
Ted Kremenek63b9cfe2009-07-18 06:27:51 +000068
69 // We don't know what to make of it. Return a NULL region, which
70 // will be interpretted as UnknownVal.
Zhongxing Xu09270cc2009-10-14 06:55:01 +000071 return NULL;
Ted Kremenek63b9cfe2009-07-18 06:27:51 +000072 }
Ted Kremenek411af402009-07-06 22:23:45 +000073
Ted Kremenek48ce7de2009-07-06 20:21:51 +000074 // Now assume we are casting from pointer to pointer. Other cases should
75 // already be handled.
Ted Kremenek6217b802009-07-29 21:53:49 +000076 QualType PointeeTy = CastToTy->getAs<PointerType>()->getPointeeType();
Ted Kremenek9a108eb2009-08-02 04:12:53 +000077 QualType CanonPointeeTy = Ctx.getCanonicalType(PointeeTy);
78
79 // Handle casts to void*. We just pass the region through.
Douglas Gregora4923eb2009-11-16 21:35:15 +000080 if (CanonPointeeTy.getLocalUnqualifiedType() == Ctx.VoidTy)
Zhongxing Xu09270cc2009-10-14 06:55:01 +000081 return R;
Mike Stump1eb44332009-09-09 15:08:12 +000082
Ted Kremenek9a108eb2009-08-02 04:12:53 +000083 // Handle casts from compatible types.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +000084 if (R->isBoundable())
85 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) {
Zhongxing Xu018220c2010-08-11 06:10:55 +000086 QualType ObjTy = Ctx.getCanonicalType(TR->getValueType());
Ted Kremenek9a108eb2009-08-02 04:12:53 +000087 if (CanonPointeeTy == ObjTy)
Zhongxing Xu09270cc2009-10-14 06:55:01 +000088 return R;
Ted Kremenek19e1f0b2009-08-01 06:17:29 +000089 }
90
Ted Kremenek48ce7de2009-07-06 20:21:51 +000091 // Process region cast according to the kind of the region being cast.
Ted Kremenekfc8f57c2009-07-06 22:56:37 +000092 switch (R->getKind()) {
Ted Kremenekde0d2632010-01-05 02:18:06 +000093 case MemRegion::CXXThisRegionKind:
Ted Kremenek67d12872009-12-07 22:05:27 +000094 case MemRegion::GenericMemSpaceRegionKind:
95 case MemRegion::StackLocalsSpaceRegionKind:
96 case MemRegion::StackArgumentsSpaceRegionKind:
97 case MemRegion::HeapSpaceRegionKind:
Ted Kremenek2b87ae42009-12-11 06:43:27 +000098 case MemRegion::UnknownSpaceRegionKind:
Ted Kremenekdcee3ce2010-07-01 20:16:50 +000099 case MemRegion::NonStaticGlobalSpaceRegionKind:
100 case MemRegion::StaticGlobalSpaceRegionKind: {
Ted Kremenekfc8f57c2009-07-06 22:56:37 +0000101 assert(0 && "Invalid region cast");
102 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000103 }
Ted Kremenek1e4a32a2010-09-01 23:00:46 +0000104
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000105 case MemRegion::FunctionTextRegionKind:
Ted Kremenekbf0fe6c2009-11-25 23:58:21 +0000106 case MemRegion::BlockTextRegionKind:
Ted Kremenek1e4a32a2010-09-01 23:00:46 +0000107 case MemRegion::BlockDataRegionKind:
Ted Kremenekfc8f57c2009-07-06 22:56:37 +0000108 case MemRegion::StringRegionKind:
Ted Kremenekfc8f57c2009-07-06 22:56:37 +0000109 // FIXME: Need to handle arbitrary downcasts.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000110 case MemRegion::SymbolicRegionKind:
111 case MemRegion::AllocaRegionKind:
Ted Kremenekfc8f57c2009-07-06 22:56:37 +0000112 case MemRegion::CompoundLiteralRegionKind:
Ted Kremenekfc8f57c2009-07-06 22:56:37 +0000113 case MemRegion::FieldRegionKind:
114 case MemRegion::ObjCIvarRegionKind:
Mike Stump1eb44332009-09-09 15:08:12 +0000115 case MemRegion::VarRegionKind:
Zhongxing Xubb141212009-12-16 11:27:52 +0000116 case MemRegion::CXXObjectRegionKind:
Zhongxing Xu09270cc2009-10-14 06:55:01 +0000117 return MakeElementRegion(R, PointeeTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000118
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000119 case MemRegion::ElementRegionKind: {
120 // If we are casting from an ElementRegion to another type, the
121 // algorithm is as follows:
122 //
123 // (1) Compute the "raw offset" of the ElementRegion from the
124 // base region. This is done by calling 'getAsRawOffset()'.
125 //
Mike Stump1eb44332009-09-09 15:08:12 +0000126 // (2a) If we get a 'RegionRawOffset' after calling
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000127 // 'getAsRawOffset()', determine if the absolute offset
Mike Stump1eb44332009-09-09 15:08:12 +0000128 // can be exactly divided into chunks of the size of the
129 // casted-pointee type. If so, create a new ElementRegion with
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000130 // the pointee-cast type as the new ElementType and the index
131 // being the offset divded by the chunk size. If not, create
132 // a new ElementRegion at offset 0 off the raw offset region.
133 //
134 // (2b) If we don't a get a 'RegionRawOffset' after calling
135 // 'getAsRawOffset()', it means that we are at offset 0.
Mike Stump1eb44332009-09-09 15:08:12 +0000136 //
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000137 // FIXME: Handle symbolic raw offsets.
Mike Stump1eb44332009-09-09 15:08:12 +0000138
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000139 const ElementRegion *elementR = cast<ElementRegion>(R);
Zhongxing Xu7caf9b32010-08-02 04:56:14 +0000140 const RegionRawOffset &rawOff = elementR->getAsArrayOffset();
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000141 const MemRegion *baseR = rawOff.getRegion();
Mike Stump1eb44332009-09-09 15:08:12 +0000142
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000143 // If we cannot compute a raw offset, throw up our hands and return
144 // a NULL MemRegion*.
145 if (!baseR)
Zhongxing Xu09270cc2009-10-14 06:55:01 +0000146 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000147
Ken Dyck199c3d62010-01-11 17:06:35 +0000148 CharUnits off = CharUnits::fromQuantity(rawOff.getByteOffset());
Mike Stump1eb44332009-09-09 15:08:12 +0000149
Ken Dyck199c3d62010-01-11 17:06:35 +0000150 if (off.isZero()) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000151 // Edge case: we are at 0 bytes off the beginning of baseR. We
152 // check to see if type we are casting to is the same as the base
Mike Stump1eb44332009-09-09 15:08:12 +0000153 // region. If so, just return the base region.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000154 if (const TypedRegion *TR = dyn_cast<TypedRegion>(baseR)) {
Zhongxing Xu018220c2010-08-11 06:10:55 +0000155 QualType ObjTy = Ctx.getCanonicalType(TR->getValueType());
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000156 QualType CanonPointeeTy = Ctx.getCanonicalType(PointeeTy);
157 if (CanonPointeeTy == ObjTy)
Zhongxing Xu09270cc2009-10-14 06:55:01 +0000158 return baseR;
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000159 }
Mike Stump1eb44332009-09-09 15:08:12 +0000160
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000161 // Otherwise, create a new ElementRegion at offset 0.
Zhongxing Xu09270cc2009-10-14 06:55:01 +0000162 return MakeElementRegion(baseR, PointeeTy);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000163 }
Mike Stump1eb44332009-09-09 15:08:12 +0000164
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000165 // We have a non-zero offset from the base region. We want to determine
166 // if the offset can be evenly divided by sizeof(PointeeTy). If so,
167 // we create an ElementRegion whose index is that value. Otherwise, we
168 // create two ElementRegions, one that reflects a raw offset and the other
169 // that reflects the cast.
Mike Stump1eb44332009-09-09 15:08:12 +0000170
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000171 // Compute the index for the new ElementRegion.
172 int64_t newIndex = 0;
173 const MemRegion *newSuperR = 0;
174
175 // We can only compute sizeof(PointeeTy) if it is a complete type.
176 if (IsCompleteType(Ctx, PointeeTy)) {
177 // Compute the size in **bytes**.
Ken Dyck199c3d62010-01-11 17:06:35 +0000178 CharUnits pointeeTySize = Ctx.getTypeSizeInChars(PointeeTy);
Ted Kremenek974d97b2010-04-07 00:46:49 +0000179 if (!pointeeTySize.isZero()) {
180 // Is the offset a multiple of the size? If so, we can layer the
181 // ElementRegion (with elementType == PointeeTy) directly on top of
182 // the base region.
183 if (off % pointeeTySize == 0) {
184 newIndex = off / pointeeTySize;
185 newSuperR = baseR;
186 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000187 }
188 }
Mike Stump1eb44332009-09-09 15:08:12 +0000189
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000190 if (!newSuperR) {
191 // Create an intermediate ElementRegion to represent the raw byte.
192 // This will be the super region of the final ElementRegion.
Ken Dyck199c3d62010-01-11 17:06:35 +0000193 newSuperR = MakeElementRegion(baseR, Ctx.CharTy, off.getQuantity());
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000194 }
Mike Stump1eb44332009-09-09 15:08:12 +0000195
Zhongxing Xu09270cc2009-10-14 06:55:01 +0000196 return MakeElementRegion(newSuperR, PointeeTy, newIndex);
Ted Kremenek48ce7de2009-07-06 20:21:51 +0000197 }
198 }
Mike Stump1eb44332009-09-09 15:08:12 +0000199
Zhongxing Xu09270cc2009-10-14 06:55:01 +0000200 assert(0 && "unreachable");
201 return 0;
Ted Kremenek48ce7de2009-07-06 20:21:51 +0000202}
Ted Kremenek1894dce2009-08-25 20:51:30 +0000203
204
205/// CastRetrievedVal - Used by subclasses of StoreManager to implement
206/// implicit casts that arise from loads from regions that are reinterpreted
207/// as another region.
Ted Kremenekc50e6df2010-01-11 02:33:26 +0000208SVal StoreManager::CastRetrievedVal(SVal V, const TypedRegion *R,
209 QualType castTy, bool performTestOnly) {
Ted Kremenek852274d2009-12-16 03:18:58 +0000210
Zhongxing Xu652be342009-11-16 04:49:44 +0000211 if (castTy.isNull())
212 return V;
Ted Kremenek852274d2009-12-16 03:18:58 +0000213
214 ASTContext &Ctx = ValMgr.getContext();
Zhongxing Xu2f4a6b22009-12-09 08:32:57 +0000215
Ted Kremenekc50e6df2010-01-11 02:33:26 +0000216 if (performTestOnly) {
217 // Automatically translate references to pointers.
Zhongxing Xu018220c2010-08-11 06:10:55 +0000218 QualType T = R->getValueType();
Ted Kremenekc50e6df2010-01-11 02:33:26 +0000219 if (const ReferenceType *RT = T->getAs<ReferenceType>())
220 T = Ctx.getPointerType(RT->getPointeeType());
221
222 assert(ValMgr.getContext().hasSameUnqualifiedType(castTy, T));
223 return V;
224 }
225
226 if (const Loc *L = dyn_cast<Loc>(&V))
227 return ValMgr.getSValuator().EvalCastL(*L, castTy);
228 else if (const NonLoc *NL = dyn_cast<NonLoc>(&V))
229 return ValMgr.getSValuator().EvalCastNL(*NL, castTy);
230
Zhongxing Xu652be342009-11-16 04:49:44 +0000231 return V;
Ted Kremenek1894dce2009-08-25 20:51:30 +0000232}
233
Zhongxing Xuc1511e02010-02-08 07:58:06 +0000234SVal StoreManager::getLValueFieldOrIvar(const Decl* D, SVal Base) {
235 if (Base.isUnknownOrUndef())
236 return Base;
237
238 Loc BaseL = cast<Loc>(Base);
239 const MemRegion* BaseR = 0;
240
241 switch (BaseL.getSubKind()) {
242 case loc::MemRegionKind:
243 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
244 break;
245
246 case loc::GotoLabelKind:
247 // These are anormal cases. Flag an undefined value.
248 return UndefinedVal();
249
250 case loc::ConcreteIntKind:
251 // While these seem funny, this can happen through casts.
252 // FIXME: What we should return is the field offset. For example,
253 // add the field offset to the integer value. That way funny things
254 // like this work properly: &(((struct foo *) 0xa)->f)
255 return Base;
256
257 default:
258 assert(0 && "Unhandled Base.");
259 return Base;
260 }
261
262 // NOTE: We must have this check first because ObjCIvarDecl is a subclass
263 // of FieldDecl.
264 if (const ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(D))
265 return loc::MemRegionVal(MRMgr.getObjCIvarRegion(ID, BaseR));
266
267 return loc::MemRegionVal(MRMgr.getFieldRegion(cast<FieldDecl>(D), BaseR));
268}
Zhongxing Xu52535682010-02-08 08:17:02 +0000269
270SVal StoreManager::getLValueElement(QualType elementType, SVal Offset,
271 SVal Base) {
272
273 // If the base is an unknown or undefined value, just return it back.
274 // FIXME: For absolute pointer addresses, we just return that value back as
275 // well, although in reality we should return the offset added to that
276 // value.
277 if (Base.isUnknownOrUndef() || isa<loc::ConcreteInt>(Base))
278 return Base;
279
Zhongxing Xu52535682010-02-08 08:17:02 +0000280 const MemRegion* BaseRegion = cast<loc::MemRegionVal>(Base).getRegion();
281
282 // Pointer of any type can be cast and used as array base.
283 const ElementRegion *ElemR = dyn_cast<ElementRegion>(BaseRegion);
284
285 // Convert the offset to the appropriate size and signedness.
286 Offset = ValMgr.convertToArrayIndex(Offset);
287
288 if (!ElemR) {
289 //
290 // If the base region is not an ElementRegion, create one.
291 // This can happen in the following example:
292 //
293 // char *p = __builtin_alloc(10);
294 // p[1] = 8;
295 //
296 // Observe that 'p' binds to an AllocaRegion.
297 //
298 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, Offset,
299 BaseRegion, Ctx));
300 }
301
302 SVal BaseIdx = ElemR->getIndex();
303
304 if (!isa<nonloc::ConcreteInt>(BaseIdx))
305 return UnknownVal();
306
307 const llvm::APSInt& BaseIdxI = cast<nonloc::ConcreteInt>(BaseIdx).getValue();
Jordy Rosee7011172010-08-16 01:15:17 +0000308
309 // Only allow non-integer offsets if the base region has no offset itself.
310 // FIXME: This is a somewhat arbitrary restriction. We should be using
311 // SValuator here to add the two offsets without checking their types.
312 if (!isa<nonloc::ConcreteInt>(Offset)) {
313 if (isa<ElementRegion>(BaseRegion->StripCasts()))
314 return UnknownVal();
315
316 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, Offset,
317 ElemR->getSuperRegion(),
318 Ctx));
319 }
320
Zhongxing Xu52535682010-02-08 08:17:02 +0000321 const llvm::APSInt& OffI = cast<nonloc::ConcreteInt>(Offset).getValue();
322 assert(BaseIdxI.isSigned());
323
324 // Compute the new index.
325 SVal NewIdx = nonloc::ConcreteInt(
326 ValMgr.getBasicValueFactory().getValue(BaseIdxI + OffI));
327
328 // Construct the new ElementRegion.
329 const MemRegion *ArrayR = ElemR->getSuperRegion();
330 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, NewIdx, ArrayR,
331 Ctx));
332}