blob: 7c80eed0eadc16acb7fb210e87c2f61f48bfb8fc [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 Kremenekeb1c7a02009-11-25 01:32:22 +0000104
105 case MemRegion::FunctionTextRegionKind:
Ted Kremenekbf0fe6c2009-11-25 23:58:21 +0000106 case MemRegion::BlockTextRegionKind:
107 case MemRegion::BlockDataRegionKind: {
Ted Kremenek63b9cfe2009-07-18 06:27:51 +0000108 // CodeTextRegion should be cast to only a function or block pointer type,
Zhongxing Xu09270cc2009-10-14 06:55:01 +0000109 // although they can in practice be casted to anything, e.g, void*, char*,
110 // etc.
111 // Just return the region.
112 return R;
Ted Kremenekfc8f57c2009-07-06 22:56:37 +0000113 }
Mike Stump1eb44332009-09-09 15:08:12 +0000114
Ted Kremenekfc8f57c2009-07-06 22:56:37 +0000115 case MemRegion::StringRegionKind:
Ted Kremenekfc8f57c2009-07-06 22:56:37 +0000116 // FIXME: Need to handle arbitrary downcasts.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000117 case MemRegion::SymbolicRegionKind:
118 case MemRegion::AllocaRegionKind:
Ted Kremenekfc8f57c2009-07-06 22:56:37 +0000119 case MemRegion::CompoundLiteralRegionKind:
Ted Kremenekfc8f57c2009-07-06 22:56:37 +0000120 case MemRegion::FieldRegionKind:
121 case MemRegion::ObjCIvarRegionKind:
Mike Stump1eb44332009-09-09 15:08:12 +0000122 case MemRegion::VarRegionKind:
Zhongxing Xubb141212009-12-16 11:27:52 +0000123 case MemRegion::CXXObjectRegionKind:
Zhongxing Xu09270cc2009-10-14 06:55:01 +0000124 return MakeElementRegion(R, PointeeTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000125
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000126 case MemRegion::ElementRegionKind: {
127 // If we are casting from an ElementRegion to another type, the
128 // algorithm is as follows:
129 //
130 // (1) Compute the "raw offset" of the ElementRegion from the
131 // base region. This is done by calling 'getAsRawOffset()'.
132 //
Mike Stump1eb44332009-09-09 15:08:12 +0000133 // (2a) If we get a 'RegionRawOffset' after calling
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000134 // 'getAsRawOffset()', determine if the absolute offset
Mike Stump1eb44332009-09-09 15:08:12 +0000135 // can be exactly divided into chunks of the size of the
136 // casted-pointee type. If so, create a new ElementRegion with
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000137 // the pointee-cast type as the new ElementType and the index
138 // being the offset divded by the chunk size. If not, create
139 // a new ElementRegion at offset 0 off the raw offset region.
140 //
141 // (2b) If we don't a get a 'RegionRawOffset' after calling
142 // 'getAsRawOffset()', it means that we are at offset 0.
Mike Stump1eb44332009-09-09 15:08:12 +0000143 //
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000144 // FIXME: Handle symbolic raw offsets.
Mike Stump1eb44332009-09-09 15:08:12 +0000145
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000146 const ElementRegion *elementR = cast<ElementRegion>(R);
Zhongxing Xu7caf9b32010-08-02 04:56:14 +0000147 const RegionRawOffset &rawOff = elementR->getAsArrayOffset();
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000148 const MemRegion *baseR = rawOff.getRegion();
Mike Stump1eb44332009-09-09 15:08:12 +0000149
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000150 // If we cannot compute a raw offset, throw up our hands and return
151 // a NULL MemRegion*.
152 if (!baseR)
Zhongxing Xu09270cc2009-10-14 06:55:01 +0000153 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000154
Ken Dyck199c3d62010-01-11 17:06:35 +0000155 CharUnits off = CharUnits::fromQuantity(rawOff.getByteOffset());
Mike Stump1eb44332009-09-09 15:08:12 +0000156
Ken Dyck199c3d62010-01-11 17:06:35 +0000157 if (off.isZero()) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000158 // Edge case: we are at 0 bytes off the beginning of baseR. We
159 // check to see if type we are casting to is the same as the base
Mike Stump1eb44332009-09-09 15:08:12 +0000160 // region. If so, just return the base region.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000161 if (const TypedRegion *TR = dyn_cast<TypedRegion>(baseR)) {
Zhongxing Xu018220c2010-08-11 06:10:55 +0000162 QualType ObjTy = Ctx.getCanonicalType(TR->getValueType());
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000163 QualType CanonPointeeTy = Ctx.getCanonicalType(PointeeTy);
164 if (CanonPointeeTy == ObjTy)
Zhongxing Xu09270cc2009-10-14 06:55:01 +0000165 return baseR;
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000166 }
Mike Stump1eb44332009-09-09 15:08:12 +0000167
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000168 // Otherwise, create a new ElementRegion at offset 0.
Zhongxing Xu09270cc2009-10-14 06:55:01 +0000169 return MakeElementRegion(baseR, PointeeTy);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000170 }
Mike Stump1eb44332009-09-09 15:08:12 +0000171
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000172 // We have a non-zero offset from the base region. We want to determine
173 // if the offset can be evenly divided by sizeof(PointeeTy). If so,
174 // we create an ElementRegion whose index is that value. Otherwise, we
175 // create two ElementRegions, one that reflects a raw offset and the other
176 // that reflects the cast.
Mike Stump1eb44332009-09-09 15:08:12 +0000177
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000178 // Compute the index for the new ElementRegion.
179 int64_t newIndex = 0;
180 const MemRegion *newSuperR = 0;
181
182 // We can only compute sizeof(PointeeTy) if it is a complete type.
183 if (IsCompleteType(Ctx, PointeeTy)) {
184 // Compute the size in **bytes**.
Ken Dyck199c3d62010-01-11 17:06:35 +0000185 CharUnits pointeeTySize = Ctx.getTypeSizeInChars(PointeeTy);
Ted Kremenek974d97b2010-04-07 00:46:49 +0000186 if (!pointeeTySize.isZero()) {
187 // Is the offset a multiple of the size? If so, we can layer the
188 // ElementRegion (with elementType == PointeeTy) directly on top of
189 // the base region.
190 if (off % pointeeTySize == 0) {
191 newIndex = off / pointeeTySize;
192 newSuperR = baseR;
193 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000194 }
195 }
Mike Stump1eb44332009-09-09 15:08:12 +0000196
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000197 if (!newSuperR) {
198 // Create an intermediate ElementRegion to represent the raw byte.
199 // This will be the super region of the final ElementRegion.
Ken Dyck199c3d62010-01-11 17:06:35 +0000200 newSuperR = MakeElementRegion(baseR, Ctx.CharTy, off.getQuantity());
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000201 }
Mike Stump1eb44332009-09-09 15:08:12 +0000202
Zhongxing Xu09270cc2009-10-14 06:55:01 +0000203 return MakeElementRegion(newSuperR, PointeeTy, newIndex);
Ted Kremenek48ce7de2009-07-06 20:21:51 +0000204 }
205 }
Mike Stump1eb44332009-09-09 15:08:12 +0000206
Zhongxing Xu09270cc2009-10-14 06:55:01 +0000207 assert(0 && "unreachable");
208 return 0;
Ted Kremenek48ce7de2009-07-06 20:21:51 +0000209}
Ted Kremenek1894dce2009-08-25 20:51:30 +0000210
211
212/// CastRetrievedVal - Used by subclasses of StoreManager to implement
213/// implicit casts that arise from loads from regions that are reinterpreted
214/// as another region.
Ted Kremenekc50e6df2010-01-11 02:33:26 +0000215SVal StoreManager::CastRetrievedVal(SVal V, const TypedRegion *R,
216 QualType castTy, bool performTestOnly) {
Ted Kremenek852274d2009-12-16 03:18:58 +0000217
Zhongxing Xu652be342009-11-16 04:49:44 +0000218 if (castTy.isNull())
219 return V;
Ted Kremenek852274d2009-12-16 03:18:58 +0000220
221 ASTContext &Ctx = ValMgr.getContext();
Zhongxing Xu2f4a6b22009-12-09 08:32:57 +0000222
Ted Kremenekc50e6df2010-01-11 02:33:26 +0000223 if (performTestOnly) {
224 // Automatically translate references to pointers.
Zhongxing Xu018220c2010-08-11 06:10:55 +0000225 QualType T = R->getValueType();
Ted Kremenekc50e6df2010-01-11 02:33:26 +0000226 if (const ReferenceType *RT = T->getAs<ReferenceType>())
227 T = Ctx.getPointerType(RT->getPointeeType());
228
229 assert(ValMgr.getContext().hasSameUnqualifiedType(castTy, T));
230 return V;
231 }
232
233 if (const Loc *L = dyn_cast<Loc>(&V))
234 return ValMgr.getSValuator().EvalCastL(*L, castTy);
235 else if (const NonLoc *NL = dyn_cast<NonLoc>(&V))
236 return ValMgr.getSValuator().EvalCastNL(*NL, castTy);
237
Zhongxing Xu652be342009-11-16 04:49:44 +0000238 return V;
Ted Kremenek1894dce2009-08-25 20:51:30 +0000239}
240
Zhongxing Xuc1511e02010-02-08 07:58:06 +0000241SVal StoreManager::getLValueFieldOrIvar(const Decl* D, SVal Base) {
242 if (Base.isUnknownOrUndef())
243 return Base;
244
245 Loc BaseL = cast<Loc>(Base);
246 const MemRegion* BaseR = 0;
247
248 switch (BaseL.getSubKind()) {
249 case loc::MemRegionKind:
250 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
251 break;
252
253 case loc::GotoLabelKind:
254 // These are anormal cases. Flag an undefined value.
255 return UndefinedVal();
256
257 case loc::ConcreteIntKind:
258 // While these seem funny, this can happen through casts.
259 // FIXME: What we should return is the field offset. For example,
260 // add the field offset to the integer value. That way funny things
261 // like this work properly: &(((struct foo *) 0xa)->f)
262 return Base;
263
264 default:
265 assert(0 && "Unhandled Base.");
266 return Base;
267 }
268
269 // NOTE: We must have this check first because ObjCIvarDecl is a subclass
270 // of FieldDecl.
271 if (const ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(D))
272 return loc::MemRegionVal(MRMgr.getObjCIvarRegion(ID, BaseR));
273
274 return loc::MemRegionVal(MRMgr.getFieldRegion(cast<FieldDecl>(D), BaseR));
275}
Zhongxing Xu52535682010-02-08 08:17:02 +0000276
277SVal StoreManager::getLValueElement(QualType elementType, SVal Offset,
278 SVal Base) {
279
280 // If the base is an unknown or undefined value, just return it back.
281 // FIXME: For absolute pointer addresses, we just return that value back as
282 // well, although in reality we should return the offset added to that
283 // value.
284 if (Base.isUnknownOrUndef() || isa<loc::ConcreteInt>(Base))
285 return Base;
286
Zhongxing Xu52535682010-02-08 08:17:02 +0000287 const MemRegion* BaseRegion = cast<loc::MemRegionVal>(Base).getRegion();
288
289 // Pointer of any type can be cast and used as array base.
290 const ElementRegion *ElemR = dyn_cast<ElementRegion>(BaseRegion);
291
292 // Convert the offset to the appropriate size and signedness.
293 Offset = ValMgr.convertToArrayIndex(Offset);
294
295 if (!ElemR) {
296 //
297 // If the base region is not an ElementRegion, create one.
298 // This can happen in the following example:
299 //
300 // char *p = __builtin_alloc(10);
301 // p[1] = 8;
302 //
303 // Observe that 'p' binds to an AllocaRegion.
304 //
305 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, Offset,
306 BaseRegion, Ctx));
307 }
308
309 SVal BaseIdx = ElemR->getIndex();
310
311 if (!isa<nonloc::ConcreteInt>(BaseIdx))
312 return UnknownVal();
313
314 const llvm::APSInt& BaseIdxI = cast<nonloc::ConcreteInt>(BaseIdx).getValue();
Jordy Rosee7011172010-08-16 01:15:17 +0000315
316 // Only allow non-integer offsets if the base region has no offset itself.
317 // FIXME: This is a somewhat arbitrary restriction. We should be using
318 // SValuator here to add the two offsets without checking their types.
319 if (!isa<nonloc::ConcreteInt>(Offset)) {
320 if (isa<ElementRegion>(BaseRegion->StripCasts()))
321 return UnknownVal();
322
323 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, Offset,
324 ElemR->getSuperRegion(),
325 Ctx));
326 }
327
Zhongxing Xu52535682010-02-08 08:17:02 +0000328 const llvm::APSInt& OffI = cast<nonloc::ConcreteInt>(Offset).getValue();
329 assert(BaseIdxI.isSigned());
330
331 // Compute the new index.
332 SVal NewIdx = nonloc::ConcreteInt(
333 ValMgr.getBasicValueFactory().getValue(BaseIdxI + OffI));
334
335 // Construct the new ElementRegion.
336 const MemRegion *ArrayR = ElemR->getSuperRegion();
337 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, NewIdx, ArrayR,
338 Ctx));
339}