blob: d68369dfa5de7b27eae9798452cd52f8f3dd347b [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();
34 if (!D->getDefinition(Ctx))
35 return false;
36 }
Mike Stump1eb44332009-09-09 15:08:12 +000037
Ted Kremenek169077d2009-07-06 23:47:19 +000038 return true;
39}
40
Zhongxing Xu09270cc2009-10-14 06:55:01 +000041const MemRegion *StoreManager::CastRegion(const MemRegion *R, QualType CastToTy) {
Mike Stump1eb44332009-09-09 15:08:12 +000042
Ted Kremenek48ce7de2009-07-06 20:21:51 +000043 ASTContext& Ctx = StateMgr.getContext();
Mike Stump1eb44332009-09-09 15:08:12 +000044
Ted Kremenekb9a44252009-07-06 22:39:40 +000045 // Handle casts to Objective-C objects.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +000046 if (CastToTy->isObjCObjectPointerType())
Zhongxing Xu479529e2009-11-10 02:17:20 +000047 return R->StripCasts();
Ted Kremenek19e1f0b2009-08-01 06:17:29 +000048
Ted Kremenek63b9cfe2009-07-18 06:27:51 +000049 if (CastToTy->isBlockPointerType()) {
Ted Kremenekabd46e12009-08-28 04:49:15 +000050 // FIXME: We may need different solutions, depending on the symbol
Ted Kremenek63b9cfe2009-07-18 06:27:51 +000051 // involved. Blocks can be casted to/from 'id', as they can be treated
Ted Kremenekabd46e12009-08-28 04:49:15 +000052 // as Objective-C objects. This could possibly be handled by enhancing
Mike Stump1eb44332009-09-09 15:08:12 +000053 // our reasoning of downcasts of symbolic objects.
Ted Kremenekabd46e12009-08-28 04:49:15 +000054 if (isa<CodeTextRegion>(R) || isa<SymbolicRegion>(R))
Zhongxing Xu09270cc2009-10-14 06:55:01 +000055 return R;
Ted Kremenek63b9cfe2009-07-18 06:27:51 +000056
57 // We don't know what to make of it. Return a NULL region, which
58 // will be interpretted as UnknownVal.
Zhongxing Xu09270cc2009-10-14 06:55:01 +000059 return NULL;
Ted Kremenek63b9cfe2009-07-18 06:27:51 +000060 }
Ted Kremenek411af402009-07-06 22:23:45 +000061
Ted Kremenek48ce7de2009-07-06 20:21:51 +000062 // Now assume we are casting from pointer to pointer. Other cases should
63 // already be handled.
Ted Kremenek6217b802009-07-29 21:53:49 +000064 QualType PointeeTy = CastToTy->getAs<PointerType>()->getPointeeType();
Ted Kremenek9a108eb2009-08-02 04:12:53 +000065 QualType CanonPointeeTy = Ctx.getCanonicalType(PointeeTy);
66
67 // Handle casts to void*. We just pass the region through.
Douglas Gregora4923eb2009-11-16 21:35:15 +000068 if (CanonPointeeTy.getLocalUnqualifiedType() == Ctx.VoidTy)
Zhongxing Xu09270cc2009-10-14 06:55:01 +000069 return R;
Mike Stump1eb44332009-09-09 15:08:12 +000070
Ted Kremenek9a108eb2009-08-02 04:12:53 +000071 // Handle casts from compatible types.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +000072 if (R->isBoundable())
73 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) {
74 QualType ObjTy = Ctx.getCanonicalType(TR->getValueType(Ctx));
Ted Kremenek9a108eb2009-08-02 04:12:53 +000075 if (CanonPointeeTy == ObjTy)
Zhongxing Xu09270cc2009-10-14 06:55:01 +000076 return R;
Ted Kremenek19e1f0b2009-08-01 06:17:29 +000077 }
78
Ted Kremenek48ce7de2009-07-06 20:21:51 +000079 // Process region cast according to the kind of the region being cast.
Ted Kremenekfc8f57c2009-07-06 22:56:37 +000080 switch (R->getKind()) {
Ted Kremenekde0d2632010-01-05 02:18:06 +000081 case MemRegion::CXXThisRegionKind:
Ted Kremenek67d12872009-12-07 22:05:27 +000082 case MemRegion::GenericMemSpaceRegionKind:
83 case MemRegion::StackLocalsSpaceRegionKind:
84 case MemRegion::StackArgumentsSpaceRegionKind:
85 case MemRegion::HeapSpaceRegionKind:
Ted Kremenek2b87ae42009-12-11 06:43:27 +000086 case MemRegion::UnknownSpaceRegionKind:
Ted Kremenek67d12872009-12-07 22:05:27 +000087 case MemRegion::GlobalsSpaceRegionKind: {
Ted Kremenekfc8f57c2009-07-06 22:56:37 +000088 assert(0 && "Invalid region cast");
89 break;
Mike Stump1eb44332009-09-09 15:08:12 +000090 }
Ted Kremenekeb1c7a02009-11-25 01:32:22 +000091
92 case MemRegion::FunctionTextRegionKind:
Ted Kremenekbf0fe6c2009-11-25 23:58:21 +000093 case MemRegion::BlockTextRegionKind:
94 case MemRegion::BlockDataRegionKind: {
Ted Kremenek63b9cfe2009-07-18 06:27:51 +000095 // CodeTextRegion should be cast to only a function or block pointer type,
Zhongxing Xu09270cc2009-10-14 06:55:01 +000096 // although they can in practice be casted to anything, e.g, void*, char*,
97 // etc.
98 // Just return the region.
99 return R;
Ted Kremenekfc8f57c2009-07-06 22:56:37 +0000100 }
Mike Stump1eb44332009-09-09 15:08:12 +0000101
Ted Kremenekfc8f57c2009-07-06 22:56:37 +0000102 case MemRegion::StringRegionKind:
Ted Kremenekfc8f57c2009-07-06 22:56:37 +0000103 // FIXME: Need to handle arbitrary downcasts.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000104 case MemRegion::SymbolicRegionKind:
105 case MemRegion::AllocaRegionKind:
Ted Kremenekfc8f57c2009-07-06 22:56:37 +0000106 case MemRegion::CompoundLiteralRegionKind:
Ted Kremenekfc8f57c2009-07-06 22:56:37 +0000107 case MemRegion::FieldRegionKind:
108 case MemRegion::ObjCIvarRegionKind:
Mike Stump1eb44332009-09-09 15:08:12 +0000109 case MemRegion::VarRegionKind:
Zhongxing Xubb141212009-12-16 11:27:52 +0000110 case MemRegion::CXXObjectRegionKind:
Zhongxing Xu09270cc2009-10-14 06:55:01 +0000111 return MakeElementRegion(R, PointeeTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000112
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000113 case MemRegion::ElementRegionKind: {
114 // If we are casting from an ElementRegion to another type, the
115 // algorithm is as follows:
116 //
117 // (1) Compute the "raw offset" of the ElementRegion from the
118 // base region. This is done by calling 'getAsRawOffset()'.
119 //
Mike Stump1eb44332009-09-09 15:08:12 +0000120 // (2a) If we get a 'RegionRawOffset' after calling
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000121 // 'getAsRawOffset()', determine if the absolute offset
Mike Stump1eb44332009-09-09 15:08:12 +0000122 // can be exactly divided into chunks of the size of the
123 // casted-pointee type. If so, create a new ElementRegion with
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000124 // the pointee-cast type as the new ElementType and the index
125 // being the offset divded by the chunk size. If not, create
126 // a new ElementRegion at offset 0 off the raw offset region.
127 //
128 // (2b) If we don't a get a 'RegionRawOffset' after calling
129 // 'getAsRawOffset()', it means that we are at offset 0.
Mike Stump1eb44332009-09-09 15:08:12 +0000130 //
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000131 // FIXME: Handle symbolic raw offsets.
Mike Stump1eb44332009-09-09 15:08:12 +0000132
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000133 const ElementRegion *elementR = cast<ElementRegion>(R);
134 const RegionRawOffset &rawOff = elementR->getAsRawOffset();
135 const MemRegion *baseR = rawOff.getRegion();
Mike Stump1eb44332009-09-09 15:08:12 +0000136
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000137 // If we cannot compute a raw offset, throw up our hands and return
138 // a NULL MemRegion*.
139 if (!baseR)
Zhongxing Xu09270cc2009-10-14 06:55:01 +0000140 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000141
Ken Dyck199c3d62010-01-11 17:06:35 +0000142 CharUnits off = CharUnits::fromQuantity(rawOff.getByteOffset());
Mike Stump1eb44332009-09-09 15:08:12 +0000143
Ken Dyck199c3d62010-01-11 17:06:35 +0000144 if (off.isZero()) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000145 // Edge case: we are at 0 bytes off the beginning of baseR. We
146 // check to see if type we are casting to is the same as the base
Mike Stump1eb44332009-09-09 15:08:12 +0000147 // region. If so, just return the base region.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000148 if (const TypedRegion *TR = dyn_cast<TypedRegion>(baseR)) {
149 QualType ObjTy = Ctx.getCanonicalType(TR->getValueType(Ctx));
150 QualType CanonPointeeTy = Ctx.getCanonicalType(PointeeTy);
151 if (CanonPointeeTy == ObjTy)
Zhongxing Xu09270cc2009-10-14 06:55:01 +0000152 return baseR;
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000153 }
Mike Stump1eb44332009-09-09 15:08:12 +0000154
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000155 // Otherwise, create a new ElementRegion at offset 0.
Zhongxing Xu09270cc2009-10-14 06:55:01 +0000156 return MakeElementRegion(baseR, PointeeTy);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000157 }
Mike Stump1eb44332009-09-09 15:08:12 +0000158
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000159 // We have a non-zero offset from the base region. We want to determine
160 // if the offset can be evenly divided by sizeof(PointeeTy). If so,
161 // we create an ElementRegion whose index is that value. Otherwise, we
162 // create two ElementRegions, one that reflects a raw offset and the other
163 // that reflects the cast.
Mike Stump1eb44332009-09-09 15:08:12 +0000164
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000165 // Compute the index for the new ElementRegion.
166 int64_t newIndex = 0;
167 const MemRegion *newSuperR = 0;
168
169 // We can only compute sizeof(PointeeTy) if it is a complete type.
170 if (IsCompleteType(Ctx, PointeeTy)) {
171 // Compute the size in **bytes**.
Ken Dyck199c3d62010-01-11 17:06:35 +0000172 CharUnits pointeeTySize = Ctx.getTypeSizeInChars(PointeeTy);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000173
174 // Is the offset a multiple of the size? If so, we can layer the
175 // ElementRegion (with elementType == PointeeTy) directly on top of
176 // the base region.
177 if (off % pointeeTySize == 0) {
178 newIndex = off / pointeeTySize;
179 newSuperR = baseR;
180 }
181 }
Mike Stump1eb44332009-09-09 15:08:12 +0000182
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000183 if (!newSuperR) {
184 // Create an intermediate ElementRegion to represent the raw byte.
185 // This will be the super region of the final ElementRegion.
Ken Dyck199c3d62010-01-11 17:06:35 +0000186 newSuperR = MakeElementRegion(baseR, Ctx.CharTy, off.getQuantity());
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000187 }
Mike Stump1eb44332009-09-09 15:08:12 +0000188
Zhongxing Xu09270cc2009-10-14 06:55:01 +0000189 return MakeElementRegion(newSuperR, PointeeTy, newIndex);
Ted Kremenek48ce7de2009-07-06 20:21:51 +0000190 }
191 }
Mike Stump1eb44332009-09-09 15:08:12 +0000192
Zhongxing Xu09270cc2009-10-14 06:55:01 +0000193 assert(0 && "unreachable");
194 return 0;
Ted Kremenek48ce7de2009-07-06 20:21:51 +0000195}
Ted Kremenek1894dce2009-08-25 20:51:30 +0000196
197
198/// CastRetrievedVal - Used by subclasses of StoreManager to implement
199/// implicit casts that arise from loads from regions that are reinterpreted
200/// as another region.
Ted Kremenekc50e6df2010-01-11 02:33:26 +0000201SVal StoreManager::CastRetrievedVal(SVal V, const TypedRegion *R,
202 QualType castTy, bool performTestOnly) {
Ted Kremenek852274d2009-12-16 03:18:58 +0000203
Zhongxing Xu652be342009-11-16 04:49:44 +0000204 if (castTy.isNull())
205 return V;
Ted Kremenek852274d2009-12-16 03:18:58 +0000206
207 ASTContext &Ctx = ValMgr.getContext();
Zhongxing Xu2f4a6b22009-12-09 08:32:57 +0000208
Ted Kremenekc50e6df2010-01-11 02:33:26 +0000209 if (performTestOnly) {
210 // Automatically translate references to pointers.
211 QualType T = R->getValueType(Ctx);
212 if (const ReferenceType *RT = T->getAs<ReferenceType>())
213 T = Ctx.getPointerType(RT->getPointeeType());
214
215 assert(ValMgr.getContext().hasSameUnqualifiedType(castTy, T));
216 return V;
217 }
218
219 if (const Loc *L = dyn_cast<Loc>(&V))
220 return ValMgr.getSValuator().EvalCastL(*L, castTy);
221 else if (const NonLoc *NL = dyn_cast<NonLoc>(&V))
222 return ValMgr.getSValuator().EvalCastNL(*NL, castTy);
223
Zhongxing Xu652be342009-11-16 04:49:44 +0000224 return V;
Ted Kremenek1894dce2009-08-25 20:51:30 +0000225}
226
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000227Store StoreManager::InvalidateRegions(Store store,
228 const MemRegion * const *I,
229 const MemRegion * const *End,
230 const Expr *E, unsigned Count,
231 InvalidatedSymbols *IS) {
Ted Kremenek81a95832009-12-03 03:27:11 +0000232 for ( ; I != End ; ++I)
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000233 store = InvalidateRegion(store, *I, E, Count, IS);
Ted Kremenek81a95832009-12-03 03:27:11 +0000234
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000235 return store;
Ted Kremenek81a95832009-12-03 03:27:11 +0000236}
Zhongxing Xuc1511e02010-02-08 07:58:06 +0000237
238SVal StoreManager::getLValueFieldOrIvar(const Decl* D, SVal Base) {
239 if (Base.isUnknownOrUndef())
240 return Base;
241
242 Loc BaseL = cast<Loc>(Base);
243 const MemRegion* BaseR = 0;
244
245 switch (BaseL.getSubKind()) {
246 case loc::MemRegionKind:
247 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
248 break;
249
250 case loc::GotoLabelKind:
251 // These are anormal cases. Flag an undefined value.
252 return UndefinedVal();
253
254 case loc::ConcreteIntKind:
255 // While these seem funny, this can happen through casts.
256 // FIXME: What we should return is the field offset. For example,
257 // add the field offset to the integer value. That way funny things
258 // like this work properly: &(((struct foo *) 0xa)->f)
259 return Base;
260
261 default:
262 assert(0 && "Unhandled Base.");
263 return Base;
264 }
265
266 // NOTE: We must have this check first because ObjCIvarDecl is a subclass
267 // of FieldDecl.
268 if (const ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(D))
269 return loc::MemRegionVal(MRMgr.getObjCIvarRegion(ID, BaseR));
270
271 return loc::MemRegionVal(MRMgr.getFieldRegion(cast<FieldDecl>(D), BaseR));
272}
Zhongxing Xu52535682010-02-08 08:17:02 +0000273
274SVal StoreManager::getLValueElement(QualType elementType, SVal Offset,
275 SVal Base) {
276
277 // If the base is an unknown or undefined value, just return it back.
278 // FIXME: For absolute pointer addresses, we just return that value back as
279 // well, although in reality we should return the offset added to that
280 // value.
281 if (Base.isUnknownOrUndef() || isa<loc::ConcreteInt>(Base))
282 return Base;
283
284 // Only handle integer offsets... for now.
285 if (!isa<nonloc::ConcreteInt>(Offset))
286 return UnknownVal();
287
288 const MemRegion* BaseRegion = cast<loc::MemRegionVal>(Base).getRegion();
289
290 // Pointer of any type can be cast and used as array base.
291 const ElementRegion *ElemR = dyn_cast<ElementRegion>(BaseRegion);
292
293 // Convert the offset to the appropriate size and signedness.
294 Offset = ValMgr.convertToArrayIndex(Offset);
295
296 if (!ElemR) {
297 //
298 // If the base region is not an ElementRegion, create one.
299 // This can happen in the following example:
300 //
301 // char *p = __builtin_alloc(10);
302 // p[1] = 8;
303 //
304 // Observe that 'p' binds to an AllocaRegion.
305 //
306 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, Offset,
307 BaseRegion, Ctx));
308 }
309
310 SVal BaseIdx = ElemR->getIndex();
311
312 if (!isa<nonloc::ConcreteInt>(BaseIdx))
313 return UnknownVal();
314
315 const llvm::APSInt& BaseIdxI = cast<nonloc::ConcreteInt>(BaseIdx).getValue();
316 const llvm::APSInt& OffI = cast<nonloc::ConcreteInt>(Offset).getValue();
317 assert(BaseIdxI.isSigned());
318
319 // Compute the new index.
320 SVal NewIdx = nonloc::ConcreteInt(
321 ValMgr.getBasicValueFactory().getValue(BaseIdxI + OffI));
322
323 // Construct the new ElementRegion.
324 const MemRegion *ArrayR = ElemR->getSuperRegion();
325 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, NewIdx, ArrayR,
326 Ctx));
327}