blob: bfcb0f41ca31627d076543c3f001a50248c9c660 [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,
30 ValMgr.getContext()));
31}
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 }
40
41 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) {
Ted Kremenek48ce7de2009-07-06 20:21:51 +000047
48 ASTContext& Ctx = StateMgr.getContext();
49
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()) {
55 if (isa<CodeTextRegion>(R))
56 return CastResult(state, R);
57
58 // FIXME: This may not be the right approach, depending on the symbol
59 // involved. Blocks can be casted to/from 'id', as they can be treated
60 // as Objective-C objects.
61 if (SymbolRef sym = loc::MemRegionVal(R).getAsSymbol()) {
62 R = MRMgr.getCodeTextRegion(sym, CastToTy);
63 return CastResult(state, R);
64 }
65
66 // We don't know what to make of it. Return a NULL region, which
67 // will be interpretted as UnknownVal.
68 return CastResult(state, NULL);
69 }
Ted Kremenek411af402009-07-06 22:23:45 +000070
Ted Kremenek48ce7de2009-07-06 20:21:51 +000071 // Now assume we are casting from pointer to pointer. Other cases should
72 // already be handled.
Ted Kremenek6217b802009-07-29 21:53:49 +000073 QualType PointeeTy = CastToTy->getAs<PointerType>()->getPointeeType();
Ted Kremenek169077d2009-07-06 23:47:19 +000074
Ted Kremenek19e1f0b2009-08-01 06:17:29 +000075 // Handle casts from compatible types or to void*.
76 if (R->isBoundable())
77 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) {
78 QualType ObjTy = Ctx.getCanonicalType(TR->getValueType(Ctx));
79 QualType CanonPointeeTy = Ctx.getCanonicalType(PointeeTy);
80 if (CanonPointeeTy == ObjTy || CanonPointeeTy == Ctx.VoidTy)
81 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;
Ted Kremenek19e1f0b2009-08-01 06:17:29 +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 }
101
102 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:
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000110 case MemRegion::VarRegionKind:
111 return MakeElementRegion(state, R, PointeeTy, CastToTy);
Ted Kremenekfc8f57c2009-07-06 22:56:37 +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 //
120 // (2a) If we get a 'RegionRawOffset' after calling
121 // 'getAsRawOffset()', determine if the absolute offset
122 // can be exactly divided into chunks of the size of the
123 // casted-pointee type. If so, create a new ElementRegion with
124 // 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.
130 //
131 // FIXME: Handle symbolic raw offsets.
Ted Kremenek169077d2009-07-06 23:47:19 +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();
Ted Kremenekfc8f57c2009-07-06 22:56:37 +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);
141
142 int64_t off = rawOff.getByteOffset();
143
144 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
147 // region. If so, just return the base region.
148 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 }
Ted Kremenekfc8f57c2009-07-06 22:56:37 +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 }
158
159 // 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.
164
165 // 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 }
182
183 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 }
189
190 return MakeElementRegion(state, newSuperR, PointeeTy, CastToTy, newIndex);
Ted Kremenek48ce7de2009-07-06 20:21:51 +0000191 }
192 }
193
Ted Kremenekfc8f57c2009-07-06 22:56:37 +0000194 return CastResult(state, R);
Ted Kremenek48ce7de2009-07-06 20:21:51 +0000195}