blob: fd44a80baa542d1f0dd39d1d43fd3bca6a5b646d [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
14#include "clang/Analysis/PathSensitive/Store.h"
15#include "clang/Analysis/PathSensitive/GRState.h"
16
17using namespace clang;
18
Ted Kremenekf7a0cf42009-07-29 21:43:22 +000019StoreManager::StoreManager(GRStateManager &stateMgr)
20 : ValMgr(stateMgr.getValueManager()), StateMgr(stateMgr),
Ted Kremenekc62abc12009-04-21 21:51:34 +000021 MRMgr(ValMgr.getRegionManager()) {}
22
Zhongxing Xu09270cc2009-10-14 06:55:01 +000023const MemRegion *StoreManager::MakeElementRegion(const MemRegion *Base,
Zhongxing Xu652be342009-11-16 04:49:44 +000024 QualType EleTy, uint64_t index) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +000025 SVal idx = ValMgr.makeArrayIndex(index);
Zhongxing Xu09270cc2009-10-14 06:55:01 +000026 return MRMgr.getElementRegion(EleTy, idx, Base, ValMgr.getContext());
Ted Kremenek411af402009-07-06 22:23:45 +000027}
28
Ted Kremenek19e1f0b2009-08-01 06:17:29 +000029// FIXME: Merge with the implementation of the same method in MemRegion.cpp
Ted Kremenek169077d2009-07-06 23:47:19 +000030static bool IsCompleteType(ASTContext &Ctx, QualType Ty) {
Ted Kremenek6217b802009-07-29 21:53:49 +000031 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Ted Kremenek169077d2009-07-06 23:47:19 +000032 const RecordDecl *D = RT->getDecl();
33 if (!D->getDefinition(Ctx))
34 return false;
35 }
Mike Stump1eb44332009-09-09 15:08:12 +000036
Ted Kremenek169077d2009-07-06 23:47:19 +000037 return true;
38}
39
Zhongxing Xu09270cc2009-10-14 06:55:01 +000040const MemRegion *StoreManager::CastRegion(const MemRegion *R, QualType CastToTy) {
Mike Stump1eb44332009-09-09 15:08:12 +000041
Ted Kremenek48ce7de2009-07-06 20:21:51 +000042 ASTContext& Ctx = StateMgr.getContext();
Mike Stump1eb44332009-09-09 15:08:12 +000043
Ted Kremenekb9a44252009-07-06 22:39:40 +000044 // Handle casts to Objective-C objects.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +000045 if (CastToTy->isObjCObjectPointerType())
Zhongxing Xu479529e2009-11-10 02:17:20 +000046 return R->StripCasts();
Ted Kremenek19e1f0b2009-08-01 06:17:29 +000047
Ted Kremenek63b9cfe2009-07-18 06:27:51 +000048 if (CastToTy->isBlockPointerType()) {
Ted Kremenekabd46e12009-08-28 04:49:15 +000049 // FIXME: We may need different solutions, depending on the symbol
Ted Kremenek63b9cfe2009-07-18 06:27:51 +000050 // involved. Blocks can be casted to/from 'id', as they can be treated
Ted Kremenekabd46e12009-08-28 04:49:15 +000051 // as Objective-C objects. This could possibly be handled by enhancing
Mike Stump1eb44332009-09-09 15:08:12 +000052 // our reasoning of downcasts of symbolic objects.
Ted Kremenekabd46e12009-08-28 04:49:15 +000053 if (isa<CodeTextRegion>(R) || isa<SymbolicRegion>(R))
Zhongxing Xu09270cc2009-10-14 06:55:01 +000054 return R;
Ted Kremenek63b9cfe2009-07-18 06:27:51 +000055
56 // We don't know what to make of it. Return a NULL region, which
57 // will be interpretted as UnknownVal.
Zhongxing Xu09270cc2009-10-14 06:55:01 +000058 return NULL;
Ted Kremenek63b9cfe2009-07-18 06:27:51 +000059 }
Ted Kremenek411af402009-07-06 22:23:45 +000060
Ted Kremenek48ce7de2009-07-06 20:21:51 +000061 // Now assume we are casting from pointer to pointer. Other cases should
62 // already be handled.
Ted Kremenek6217b802009-07-29 21:53:49 +000063 QualType PointeeTy = CastToTy->getAs<PointerType>()->getPointeeType();
Ted Kremenek9a108eb2009-08-02 04:12:53 +000064 QualType CanonPointeeTy = Ctx.getCanonicalType(PointeeTy);
65
66 // Handle casts to void*. We just pass the region through.
Douglas Gregora4923eb2009-11-16 21:35:15 +000067 if (CanonPointeeTy.getLocalUnqualifiedType() == Ctx.VoidTy)
Zhongxing Xu09270cc2009-10-14 06:55:01 +000068 return R;
Mike Stump1eb44332009-09-09 15:08:12 +000069
Ted Kremenek9a108eb2009-08-02 04:12:53 +000070 // Handle casts from compatible types.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +000071 if (R->isBoundable())
72 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) {
73 QualType ObjTy = Ctx.getCanonicalType(TR->getValueType(Ctx));
Ted Kremenek9a108eb2009-08-02 04:12:53 +000074 if (CanonPointeeTy == ObjTy)
Zhongxing Xu09270cc2009-10-14 06:55:01 +000075 return R;
Ted Kremenek19e1f0b2009-08-01 06:17:29 +000076 }
77
Ted Kremenek48ce7de2009-07-06 20:21:51 +000078 // Process region cast according to the kind of the region being cast.
Ted Kremenekfc8f57c2009-07-06 22:56:37 +000079 switch (R->getKind()) {
Ted Kremenekde0d2632010-01-05 02:18:06 +000080 case MemRegion::CXXThisRegionKind:
Ted Kremenek67d12872009-12-07 22:05:27 +000081 case MemRegion::GenericMemSpaceRegionKind:
82 case MemRegion::StackLocalsSpaceRegionKind:
83 case MemRegion::StackArgumentsSpaceRegionKind:
84 case MemRegion::HeapSpaceRegionKind:
Ted Kremenek2b87ae42009-12-11 06:43:27 +000085 case MemRegion::UnknownSpaceRegionKind:
Ted Kremenek67d12872009-12-07 22:05:27 +000086 case MemRegion::GlobalsSpaceRegionKind: {
Ted Kremenekfc8f57c2009-07-06 22:56:37 +000087 assert(0 && "Invalid region cast");
88 break;
Mike Stump1eb44332009-09-09 15:08:12 +000089 }
Ted Kremenekeb1c7a02009-11-25 01:32:22 +000090
91 case MemRegion::FunctionTextRegionKind:
Ted Kremenekbf0fe6c2009-11-25 23:58:21 +000092 case MemRegion::BlockTextRegionKind:
93 case MemRegion::BlockDataRegionKind: {
Ted Kremenek63b9cfe2009-07-18 06:27:51 +000094 // CodeTextRegion should be cast to only a function or block pointer type,
Zhongxing Xu09270cc2009-10-14 06:55:01 +000095 // although they can in practice be casted to anything, e.g, void*, char*,
96 // etc.
97 // Just return the region.
98 return R;
Ted Kremenekfc8f57c2009-07-06 22:56:37 +000099 }
Mike Stump1eb44332009-09-09 15:08:12 +0000100
Ted Kremenekfc8f57c2009-07-06 22:56:37 +0000101 case MemRegion::StringRegionKind:
Ted Kremenekfc8f57c2009-07-06 22:56:37 +0000102 // FIXME: Need to handle arbitrary downcasts.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000103 case MemRegion::SymbolicRegionKind:
104 case MemRegion::AllocaRegionKind:
Ted Kremenekfc8f57c2009-07-06 22:56:37 +0000105 case MemRegion::CompoundLiteralRegionKind:
Ted Kremenekfc8f57c2009-07-06 22:56:37 +0000106 case MemRegion::FieldRegionKind:
107 case MemRegion::ObjCIvarRegionKind:
Mike Stump1eb44332009-09-09 15:08:12 +0000108 case MemRegion::VarRegionKind:
Zhongxing Xubb141212009-12-16 11:27:52 +0000109 case MemRegion::CXXObjectRegionKind:
Zhongxing Xu09270cc2009-10-14 06:55:01 +0000110 return MakeElementRegion(R, PointeeTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000111
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000112 case MemRegion::ElementRegionKind: {
113 // If we are casting from an ElementRegion to another type, the
114 // algorithm is as follows:
115 //
116 // (1) Compute the "raw offset" of the ElementRegion from the
117 // base region. This is done by calling 'getAsRawOffset()'.
118 //
Mike Stump1eb44332009-09-09 15:08:12 +0000119 // (2a) If we get a 'RegionRawOffset' after calling
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000120 // 'getAsRawOffset()', determine if the absolute offset
Mike Stump1eb44332009-09-09 15:08:12 +0000121 // can be exactly divided into chunks of the size of the
122 // casted-pointee type. If so, create a new ElementRegion with
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000123 // the pointee-cast type as the new ElementType and the index
124 // being the offset divded by the chunk size. If not, create
125 // a new ElementRegion at offset 0 off the raw offset region.
126 //
127 // (2b) If we don't a get a 'RegionRawOffset' after calling
128 // 'getAsRawOffset()', it means that we are at offset 0.
Mike Stump1eb44332009-09-09 15:08:12 +0000129 //
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000130 // FIXME: Handle symbolic raw offsets.
Mike Stump1eb44332009-09-09 15:08:12 +0000131
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000132 const ElementRegion *elementR = cast<ElementRegion>(R);
133 const RegionRawOffset &rawOff = elementR->getAsRawOffset();
134 const MemRegion *baseR = rawOff.getRegion();
Mike Stump1eb44332009-09-09 15:08:12 +0000135
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000136 // If we cannot compute a raw offset, throw up our hands and return
137 // a NULL MemRegion*.
138 if (!baseR)
Zhongxing Xu09270cc2009-10-14 06:55:01 +0000139 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000140
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000141 int64_t off = rawOff.getByteOffset();
Mike Stump1eb44332009-09-09 15:08:12 +0000142
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000143 if (off == 0) {
144 // Edge case: we are at 0 bytes off the beginning of baseR. We
145 // check to see if type we are casting to is the same as the base
Mike Stump1eb44332009-09-09 15:08:12 +0000146 // region. If so, just return the base region.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000147 if (const TypedRegion *TR = dyn_cast<TypedRegion>(baseR)) {
148 QualType ObjTy = Ctx.getCanonicalType(TR->getValueType(Ctx));
149 QualType CanonPointeeTy = Ctx.getCanonicalType(PointeeTy);
150 if (CanonPointeeTy == ObjTy)
Zhongxing Xu09270cc2009-10-14 06:55:01 +0000151 return baseR;
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000152 }
Mike Stump1eb44332009-09-09 15:08:12 +0000153
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000154 // Otherwise, create a new ElementRegion at offset 0.
Zhongxing Xu09270cc2009-10-14 06:55:01 +0000155 return MakeElementRegion(baseR, PointeeTy);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000156 }
Mike Stump1eb44332009-09-09 15:08:12 +0000157
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000158 // We have a non-zero offset from the base region. We want to determine
159 // if the offset can be evenly divided by sizeof(PointeeTy). If so,
160 // we create an ElementRegion whose index is that value. Otherwise, we
161 // create two ElementRegions, one that reflects a raw offset and the other
162 // that reflects the cast.
Mike Stump1eb44332009-09-09 15:08:12 +0000163
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000164 // Compute the index for the new ElementRegion.
165 int64_t newIndex = 0;
166 const MemRegion *newSuperR = 0;
167
168 // We can only compute sizeof(PointeeTy) if it is a complete type.
169 if (IsCompleteType(Ctx, PointeeTy)) {
170 // Compute the size in **bytes**.
171 int64_t pointeeTySize = (int64_t) (Ctx.getTypeSize(PointeeTy) / 8);
172
173 // Is the offset a multiple of the size? If so, we can layer the
174 // ElementRegion (with elementType == PointeeTy) directly on top of
175 // the base region.
176 if (off % pointeeTySize == 0) {
177 newIndex = off / pointeeTySize;
178 newSuperR = baseR;
179 }
180 }
Mike Stump1eb44332009-09-09 15:08:12 +0000181
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000182 if (!newSuperR) {
183 // Create an intermediate ElementRegion to represent the raw byte.
184 // This will be the super region of the final ElementRegion.
Zhongxing Xu09270cc2009-10-14 06:55:01 +0000185 newSuperR = MakeElementRegion(baseR, Ctx.CharTy, off);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000186 }
Mike Stump1eb44332009-09-09 15:08:12 +0000187
Zhongxing Xu09270cc2009-10-14 06:55:01 +0000188 return MakeElementRegion(newSuperR, PointeeTy, newIndex);
Ted Kremenek48ce7de2009-07-06 20:21:51 +0000189 }
190 }
Mike Stump1eb44332009-09-09 15:08:12 +0000191
Zhongxing Xu09270cc2009-10-14 06:55:01 +0000192 assert(0 && "unreachable");
193 return 0;
Ted Kremenek48ce7de2009-07-06 20:21:51 +0000194}
Ted Kremenek1894dce2009-08-25 20:51:30 +0000195
196
197/// CastRetrievedVal - Used by subclasses of StoreManager to implement
198/// implicit casts that arise from loads from regions that are reinterpreted
199/// as another region.
Ted Kremenekc50e6df2010-01-11 02:33:26 +0000200SVal StoreManager::CastRetrievedVal(SVal V, const TypedRegion *R,
201 QualType castTy, bool performTestOnly) {
Ted Kremenek852274d2009-12-16 03:18:58 +0000202
Zhongxing Xu652be342009-11-16 04:49:44 +0000203 if (castTy.isNull())
204 return V;
Ted Kremenek852274d2009-12-16 03:18:58 +0000205
206 ASTContext &Ctx = ValMgr.getContext();
Zhongxing Xu2f4a6b22009-12-09 08:32:57 +0000207
Ted Kremenekc50e6df2010-01-11 02:33:26 +0000208 if (performTestOnly) {
209 // Automatically translate references to pointers.
210 QualType T = R->getValueType(Ctx);
211 if (const ReferenceType *RT = T->getAs<ReferenceType>())
212 T = Ctx.getPointerType(RT->getPointeeType());
213
214 assert(ValMgr.getContext().hasSameUnqualifiedType(castTy, T));
215 return V;
216 }
217
218 if (const Loc *L = dyn_cast<Loc>(&V))
219 return ValMgr.getSValuator().EvalCastL(*L, castTy);
220 else if (const NonLoc *NL = dyn_cast<NonLoc>(&V))
221 return ValMgr.getSValuator().EvalCastNL(*NL, castTy);
222
Zhongxing Xu652be342009-11-16 04:49:44 +0000223 return V;
Ted Kremenek1894dce2009-08-25 20:51:30 +0000224}
225
Ted Kremenek81a95832009-12-03 03:27:11 +0000226const GRState *StoreManager::InvalidateRegions(const GRState *state,
227 const MemRegion * const *I,
228 const MemRegion * const *End,
229 const Expr *E,
230 unsigned Count,
231 InvalidatedSymbols *IS) {
232 for ( ; I != End ; ++I)
233 state = InvalidateRegion(state, *I, E, Count, IS);
234
235 return state;
236}
Ted Kremenek67d12872009-12-07 22:05:27 +0000237
238//===----------------------------------------------------------------------===//
239// Common getLValueXXX methods.
240//===----------------------------------------------------------------------===//
241
242/// getLValueCompoundLiteral - Returns an SVal representing the lvalue
243/// of a compound literal. Within RegionStore a compound literal
244/// has an associated region, and the lvalue of the compound literal
245/// is the lvalue of that region.
246SVal StoreManager::getLValueCompoundLiteral(const CompoundLiteralExpr* CL,
247 const LocationContext *LC) {
248 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL, LC));
Zhongxing Xu2f4a6b22009-12-09 08:32:57 +0000249}