blob: d1abd57640cdeca500cf53c0db045fb653515abc [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
23StoreManager::CastResult
Ted Kremenek411af402009-07-06 22:23:45 +000024StoreManager::MakeElementRegion(const GRState *state, const MemRegion *region,
Ted Kremenek19e1f0b2009-08-01 06:17:29 +000025 QualType pointeeTy, QualType castToTy,
26 uint64_t index) {
27 // Create a new ElementRegion.
28 SVal idx = ValMgr.makeArrayIndex(index);
Ted Kremenek411af402009-07-06 22:23:45 +000029 return CastResult(state, MRMgr.getElementRegion(pointeeTy, idx, region,
Mike Stump1eb44332009-09-09 15:08:12 +000030 ValMgr.getContext()));
Ted Kremenek411af402009-07-06 22:23:45 +000031}
32
Ted Kremenek19e1f0b2009-08-01 06:17:29 +000033// FIXME: Merge with the implementation of the same method in MemRegion.cpp
Ted Kremenek169077d2009-07-06 23:47:19 +000034static bool IsCompleteType(ASTContext &Ctx, QualType Ty) {
Ted Kremenek6217b802009-07-29 21:53:49 +000035 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Ted Kremenek169077d2009-07-06 23:47:19 +000036 const RecordDecl *D = RT->getDecl();
37 if (!D->getDefinition(Ctx))
38 return false;
39 }
Mike Stump1eb44332009-09-09 15:08:12 +000040
Ted Kremenek169077d2009-07-06 23:47:19 +000041 return true;
42}
43
Ted Kremenek411af402009-07-06 22:23:45 +000044StoreManager::CastResult
Ted Kremenekf7a0cf42009-07-29 21:43:22 +000045StoreManager::CastRegion(const GRState *state, const MemRegion* R,
46 QualType CastToTy) {
Mike Stump1eb44332009-09-09 15:08:12 +000047
Ted Kremenek48ce7de2009-07-06 20:21:51 +000048 ASTContext& Ctx = StateMgr.getContext();
Mike Stump1eb44332009-09-09 15:08:12 +000049
Ted Kremenekb9a44252009-07-06 22:39:40 +000050 // Handle casts to Objective-C objects.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +000051 if (CastToTy->isObjCObjectPointerType())
52 return CastResult(state, R->getBaseRegion());
53
Ted Kremenek63b9cfe2009-07-18 06:27:51 +000054 if (CastToTy->isBlockPointerType()) {
Ted Kremenekabd46e12009-08-28 04:49:15 +000055 // FIXME: We may need different solutions, depending on the symbol
Ted Kremenek63b9cfe2009-07-18 06:27:51 +000056 // involved. Blocks can be casted to/from 'id', as they can be treated
Ted Kremenekabd46e12009-08-28 04:49:15 +000057 // as Objective-C objects. This could possibly be handled by enhancing
Mike Stump1eb44332009-09-09 15:08:12 +000058 // our reasoning of downcasts of symbolic objects.
Ted Kremenekabd46e12009-08-28 04:49:15 +000059 if (isa<CodeTextRegion>(R) || isa<SymbolicRegion>(R))
Ted Kremenek63b9cfe2009-07-18 06:27:51 +000060 return CastResult(state, R);
Ted Kremenek63b9cfe2009-07-18 06:27:51 +000061
62 // We don't know what to make of it. Return a NULL region, which
63 // will be interpretted as UnknownVal.
64 return CastResult(state, NULL);
65 }
Ted Kremenek411af402009-07-06 22:23:45 +000066
Ted Kremenek48ce7de2009-07-06 20:21:51 +000067 // Now assume we are casting from pointer to pointer. Other cases should
68 // already be handled.
Ted Kremenek6217b802009-07-29 21:53:49 +000069 QualType PointeeTy = CastToTy->getAs<PointerType>()->getPointeeType();
Ted Kremenek9a108eb2009-08-02 04:12:53 +000070 QualType CanonPointeeTy = Ctx.getCanonicalType(PointeeTy);
71
72 // Handle casts to void*. We just pass the region through.
73 if (CanonPointeeTy.getUnqualifiedType() == Ctx.VoidTy)
74 return CastResult(state, R);
Mike Stump1eb44332009-09-09 15:08:12 +000075
Ted Kremenek9a108eb2009-08-02 04:12:53 +000076 // Handle casts from compatible types.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +000077 if (R->isBoundable())
78 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) {
79 QualType ObjTy = Ctx.getCanonicalType(TR->getValueType(Ctx));
Ted Kremenek9a108eb2009-08-02 04:12:53 +000080 if (CanonPointeeTy == ObjTy)
Ted Kremenek19e1f0b2009-08-01 06:17:29 +000081 return CastResult(state, R);
82 }
83
Ted Kremenek48ce7de2009-07-06 20:21:51 +000084 // Process region cast according to the kind of the region being cast.
Ted Kremenekfc8f57c2009-07-06 22:56:37 +000085 switch (R->getKind()) {
86 case MemRegion::BEG_TYPED_REGIONS:
87 case MemRegion::MemSpaceRegionKind:
88 case MemRegion::BEG_DECL_REGIONS:
89 case MemRegion::END_DECL_REGIONS:
Ted Kremenekf7a0cf42009-07-29 21:43:22 +000090 case MemRegion::END_TYPED_REGIONS: {
Ted Kremenekfc8f57c2009-07-06 22:56:37 +000091 assert(0 && "Invalid region cast");
92 break;
Mike Stump1eb44332009-09-09 15:08:12 +000093 }
Ted Kremenekfc8f57c2009-07-06 22:56:37 +000094 case MemRegion::CodeTextRegionKind: {
Ted Kremenek63b9cfe2009-07-18 06:27:51 +000095 // CodeTextRegion should be cast to only a function or block pointer type,
96 // although they can in practice be casted to anything, e.g, void*,
97 // char*, etc.
Ted Kremenek8d344ae2009-07-10 21:24:45 +000098 // Just pass the region through.
Ted Kremenekfc8f57c2009-07-06 22:56:37 +000099 break;
100 }
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 case MemRegion::ObjCObjectRegionKind:
Ted Kremenekfc8f57c2009-07-06 22:56:37 +0000104 // FIXME: Need to handle arbitrary downcasts.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000105 case MemRegion::SymbolicRegionKind:
106 case MemRegion::AllocaRegionKind:
Ted Kremenekfc8f57c2009-07-06 22:56:37 +0000107 case MemRegion::CompoundLiteralRegionKind:
Ted Kremenekfc8f57c2009-07-06 22:56:37 +0000108 case MemRegion::FieldRegionKind:
109 case MemRegion::ObjCIvarRegionKind:
Mike Stump1eb44332009-09-09 15:08:12 +0000110 case MemRegion::VarRegionKind:
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000111 return MakeElementRegion(state, R, PointeeTy, CastToTy);
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)
140 return CastResult(state, NULL);
Mike Stump1eb44332009-09-09 15:08:12 +0000141
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000142 int64_t off = rawOff.getByteOffset();
Mike Stump1eb44332009-09-09 15:08:12 +0000143
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000144 if (off == 0) {
145 // 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)
152 return CastResult(state, baseR);
153 }
Mike Stump1eb44332009-09-09 15:08:12 +0000154
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000155 // Otherwise, create a new ElementRegion at offset 0.
156 return MakeElementRegion(state, baseR, PointeeTy, CastToTy, 0);
157 }
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**.
172 int64_t pointeeTySize = (int64_t) (Ctx.getTypeSize(PointeeTy) / 8);
173
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.
186 SVal idx = ValMgr.makeArrayIndex(off);
187 newSuperR = MRMgr.getElementRegion(Ctx.CharTy, idx, baseR, Ctx);
188 }
Mike Stump1eb44332009-09-09 15:08:12 +0000189
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000190 return MakeElementRegion(state, newSuperR, PointeeTy, CastToTy, newIndex);
Ted Kremenek48ce7de2009-07-06 20:21:51 +0000191 }
192 }
Mike Stump1eb44332009-09-09 15:08:12 +0000193
Ted Kremenekfc8f57c2009-07-06 22:56:37 +0000194 return CastResult(state, R);
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.
201SValuator::CastResult StoreManager::CastRetrievedVal(SVal V,
202 const GRState *state,
203 const TypedRegion *R,
204 QualType castTy) {
205 if (castTy.isNull())
206 return SValuator::CastResult(state, V);
Mike Stump1eb44332009-09-09 15:08:12 +0000207
208 ASTContext &Ctx = ValMgr.getContext();
Ted Kremenek1894dce2009-08-25 20:51:30 +0000209 return ValMgr.getSValuator().EvalCast(V, state, castTy, R->getValueType(Ctx));
210}
211