Ted Kremenek | c62abc1 | 2009-04-21 21:51:34 +0000 | [diff] [blame] | 1 | //== 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 | |
| 17 | using namespace clang; |
| 18 | |
Ted Kremenek | f7a0cf4 | 2009-07-29 21:43:22 +0000 | [diff] [blame] | 19 | StoreManager::StoreManager(GRStateManager &stateMgr) |
| 20 | : ValMgr(stateMgr.getValueManager()), StateMgr(stateMgr), |
Ted Kremenek | c62abc1 | 2009-04-21 21:51:34 +0000 | [diff] [blame] | 21 | MRMgr(ValMgr.getRegionManager()) {} |
| 22 | |
Zhongxing Xu | 09270cc | 2009-10-14 06:55:01 +0000 | [diff] [blame] | 23 | const MemRegion *StoreManager::MakeElementRegion(const MemRegion *Base, |
Zhongxing Xu | 652be34 | 2009-11-16 04:49:44 +0000 | [diff] [blame] | 24 | QualType EleTy, uint64_t index) { |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 25 | SVal idx = ValMgr.makeArrayIndex(index); |
Zhongxing Xu | 09270cc | 2009-10-14 06:55:01 +0000 | [diff] [blame] | 26 | return MRMgr.getElementRegion(EleTy, idx, Base, ValMgr.getContext()); |
Ted Kremenek | 411af40 | 2009-07-06 22:23:45 +0000 | [diff] [blame] | 27 | } |
| 28 | |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 29 | // FIXME: Merge with the implementation of the same method in MemRegion.cpp |
Ted Kremenek | 169077d | 2009-07-06 23:47:19 +0000 | [diff] [blame] | 30 | static bool IsCompleteType(ASTContext &Ctx, QualType Ty) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 31 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
Ted Kremenek | 169077d | 2009-07-06 23:47:19 +0000 | [diff] [blame] | 32 | const RecordDecl *D = RT->getDecl(); |
| 33 | if (!D->getDefinition(Ctx)) |
| 34 | return false; |
| 35 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 36 | |
Ted Kremenek | 169077d | 2009-07-06 23:47:19 +0000 | [diff] [blame] | 37 | return true; |
| 38 | } |
| 39 | |
Zhongxing Xu | 09270cc | 2009-10-14 06:55:01 +0000 | [diff] [blame] | 40 | const MemRegion *StoreManager::CastRegion(const MemRegion *R, QualType CastToTy) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 41 | |
Ted Kremenek | 48ce7de | 2009-07-06 20:21:51 +0000 | [diff] [blame] | 42 | ASTContext& Ctx = StateMgr.getContext(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 43 | |
Ted Kremenek | b9a4425 | 2009-07-06 22:39:40 +0000 | [diff] [blame] | 44 | // Handle casts to Objective-C objects. |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 45 | if (CastToTy->isObjCObjectPointerType()) |
Zhongxing Xu | 479529e | 2009-11-10 02:17:20 +0000 | [diff] [blame] | 46 | return R->StripCasts(); |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 47 | |
Ted Kremenek | 63b9cfe | 2009-07-18 06:27:51 +0000 | [diff] [blame] | 48 | if (CastToTy->isBlockPointerType()) { |
Ted Kremenek | abd46e1 | 2009-08-28 04:49:15 +0000 | [diff] [blame] | 49 | // FIXME: We may need different solutions, depending on the symbol |
Ted Kremenek | 63b9cfe | 2009-07-18 06:27:51 +0000 | [diff] [blame] | 50 | // involved. Blocks can be casted to/from 'id', as they can be treated |
Ted Kremenek | abd46e1 | 2009-08-28 04:49:15 +0000 | [diff] [blame] | 51 | // as Objective-C objects. This could possibly be handled by enhancing |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 52 | // our reasoning of downcasts of symbolic objects. |
Ted Kremenek | abd46e1 | 2009-08-28 04:49:15 +0000 | [diff] [blame] | 53 | if (isa<CodeTextRegion>(R) || isa<SymbolicRegion>(R)) |
Zhongxing Xu | 09270cc | 2009-10-14 06:55:01 +0000 | [diff] [blame] | 54 | return R; |
Ted Kremenek | 63b9cfe | 2009-07-18 06:27:51 +0000 | [diff] [blame] | 55 | |
| 56 | // We don't know what to make of it. Return a NULL region, which |
| 57 | // will be interpretted as UnknownVal. |
Zhongxing Xu | 09270cc | 2009-10-14 06:55:01 +0000 | [diff] [blame] | 58 | return NULL; |
Ted Kremenek | 63b9cfe | 2009-07-18 06:27:51 +0000 | [diff] [blame] | 59 | } |
Ted Kremenek | 411af40 | 2009-07-06 22:23:45 +0000 | [diff] [blame] | 60 | |
Ted Kremenek | 48ce7de | 2009-07-06 20:21:51 +0000 | [diff] [blame] | 61 | // Now assume we are casting from pointer to pointer. Other cases should |
| 62 | // already be handled. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 63 | QualType PointeeTy = CastToTy->getAs<PointerType>()->getPointeeType(); |
Ted Kremenek | 9a108eb | 2009-08-02 04:12:53 +0000 | [diff] [blame] | 64 | QualType CanonPointeeTy = Ctx.getCanonicalType(PointeeTy); |
| 65 | |
| 66 | // Handle casts to void*. We just pass the region through. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 67 | if (CanonPointeeTy.getLocalUnqualifiedType() == Ctx.VoidTy) |
Zhongxing Xu | 09270cc | 2009-10-14 06:55:01 +0000 | [diff] [blame] | 68 | return R; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 69 | |
Ted Kremenek | 9a108eb | 2009-08-02 04:12:53 +0000 | [diff] [blame] | 70 | // Handle casts from compatible types. |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 71 | if (R->isBoundable()) |
| 72 | if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) { |
| 73 | QualType ObjTy = Ctx.getCanonicalType(TR->getValueType(Ctx)); |
Ted Kremenek | 9a108eb | 2009-08-02 04:12:53 +0000 | [diff] [blame] | 74 | if (CanonPointeeTy == ObjTy) |
Zhongxing Xu | 09270cc | 2009-10-14 06:55:01 +0000 | [diff] [blame] | 75 | return R; |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Ted Kremenek | 48ce7de | 2009-07-06 20:21:51 +0000 | [diff] [blame] | 78 | // Process region cast according to the kind of the region being cast. |
Ted Kremenek | fc8f57c | 2009-07-06 22:56:37 +0000 | [diff] [blame] | 79 | switch (R->getKind()) { |
Ted Kremenek | de0d263 | 2010-01-05 02:18:06 +0000 | [diff] [blame] | 80 | case MemRegion::CXXThisRegionKind: |
Ted Kremenek | 67d1287 | 2009-12-07 22:05:27 +0000 | [diff] [blame] | 81 | case MemRegion::GenericMemSpaceRegionKind: |
| 82 | case MemRegion::StackLocalsSpaceRegionKind: |
| 83 | case MemRegion::StackArgumentsSpaceRegionKind: |
| 84 | case MemRegion::HeapSpaceRegionKind: |
Ted Kremenek | 2b87ae4 | 2009-12-11 06:43:27 +0000 | [diff] [blame] | 85 | case MemRegion::UnknownSpaceRegionKind: |
Ted Kremenek | 67d1287 | 2009-12-07 22:05:27 +0000 | [diff] [blame] | 86 | case MemRegion::GlobalsSpaceRegionKind: { |
Ted Kremenek | fc8f57c | 2009-07-06 22:56:37 +0000 | [diff] [blame] | 87 | assert(0 && "Invalid region cast"); |
| 88 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 89 | } |
Ted Kremenek | eb1c7a0 | 2009-11-25 01:32:22 +0000 | [diff] [blame] | 90 | |
| 91 | case MemRegion::FunctionTextRegionKind: |
Ted Kremenek | bf0fe6c | 2009-11-25 23:58:21 +0000 | [diff] [blame] | 92 | case MemRegion::BlockTextRegionKind: |
| 93 | case MemRegion::BlockDataRegionKind: { |
Ted Kremenek | 63b9cfe | 2009-07-18 06:27:51 +0000 | [diff] [blame] | 94 | // CodeTextRegion should be cast to only a function or block pointer type, |
Zhongxing Xu | 09270cc | 2009-10-14 06:55:01 +0000 | [diff] [blame] | 95 | // although they can in practice be casted to anything, e.g, void*, char*, |
| 96 | // etc. |
| 97 | // Just return the region. |
| 98 | return R; |
Ted Kremenek | fc8f57c | 2009-07-06 22:56:37 +0000 | [diff] [blame] | 99 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 100 | |
Ted Kremenek | fc8f57c | 2009-07-06 22:56:37 +0000 | [diff] [blame] | 101 | case MemRegion::StringRegionKind: |
Ted Kremenek | fc8f57c | 2009-07-06 22:56:37 +0000 | [diff] [blame] | 102 | // FIXME: Need to handle arbitrary downcasts. |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 103 | case MemRegion::SymbolicRegionKind: |
| 104 | case MemRegion::AllocaRegionKind: |
Ted Kremenek | fc8f57c | 2009-07-06 22:56:37 +0000 | [diff] [blame] | 105 | case MemRegion::CompoundLiteralRegionKind: |
Ted Kremenek | fc8f57c | 2009-07-06 22:56:37 +0000 | [diff] [blame] | 106 | case MemRegion::FieldRegionKind: |
| 107 | case MemRegion::ObjCIvarRegionKind: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 108 | case MemRegion::VarRegionKind: |
Zhongxing Xu | bb14121 | 2009-12-16 11:27:52 +0000 | [diff] [blame] | 109 | case MemRegion::CXXObjectRegionKind: |
Zhongxing Xu | 09270cc | 2009-10-14 06:55:01 +0000 | [diff] [blame] | 110 | return MakeElementRegion(R, PointeeTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 111 | |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 112 | 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 Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 119 | // (2a) If we get a 'RegionRawOffset' after calling |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 120 | // 'getAsRawOffset()', determine if the absolute offset |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 121 | // can be exactly divided into chunks of the size of the |
| 122 | // casted-pointee type. If so, create a new ElementRegion with |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 123 | // 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 Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 129 | // |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 130 | // FIXME: Handle symbolic raw offsets. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 131 | |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 132 | const ElementRegion *elementR = cast<ElementRegion>(R); |
| 133 | const RegionRawOffset &rawOff = elementR->getAsRawOffset(); |
| 134 | const MemRegion *baseR = rawOff.getRegion(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 135 | |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 136 | // If we cannot compute a raw offset, throw up our hands and return |
| 137 | // a NULL MemRegion*. |
| 138 | if (!baseR) |
Zhongxing Xu | 09270cc | 2009-10-14 06:55:01 +0000 | [diff] [blame] | 139 | return NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 140 | |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 141 | int64_t off = rawOff.getByteOffset(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 142 | |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 143 | 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 Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 146 | // region. If so, just return the base region. |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 147 | 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 Xu | 09270cc | 2009-10-14 06:55:01 +0000 | [diff] [blame] | 151 | return baseR; |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 152 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 153 | |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 154 | // Otherwise, create a new ElementRegion at offset 0. |
Zhongxing Xu | 09270cc | 2009-10-14 06:55:01 +0000 | [diff] [blame] | 155 | return MakeElementRegion(baseR, PointeeTy); |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 156 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 157 | |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 158 | // 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 Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 163 | |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 164 | // 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 Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 181 | |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 182 | if (!newSuperR) { |
| 183 | // Create an intermediate ElementRegion to represent the raw byte. |
| 184 | // This will be the super region of the final ElementRegion. |
Zhongxing Xu | 09270cc | 2009-10-14 06:55:01 +0000 | [diff] [blame] | 185 | newSuperR = MakeElementRegion(baseR, Ctx.CharTy, off); |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 186 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 187 | |
Zhongxing Xu | 09270cc | 2009-10-14 06:55:01 +0000 | [diff] [blame] | 188 | return MakeElementRegion(newSuperR, PointeeTy, newIndex); |
Ted Kremenek | 48ce7de | 2009-07-06 20:21:51 +0000 | [diff] [blame] | 189 | } |
| 190 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 191 | |
Zhongxing Xu | 09270cc | 2009-10-14 06:55:01 +0000 | [diff] [blame] | 192 | assert(0 && "unreachable"); |
| 193 | return 0; |
Ted Kremenek | 48ce7de | 2009-07-06 20:21:51 +0000 | [diff] [blame] | 194 | } |
Ted Kremenek | 1894dce | 2009-08-25 20:51:30 +0000 | [diff] [blame] | 195 | |
| 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 Kremenek | c50e6df | 2010-01-11 02:33:26 +0000 | [diff] [blame] | 200 | SVal StoreManager::CastRetrievedVal(SVal V, const TypedRegion *R, |
| 201 | QualType castTy, bool performTestOnly) { |
Ted Kremenek | 852274d | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 202 | |
Zhongxing Xu | 652be34 | 2009-11-16 04:49:44 +0000 | [diff] [blame] | 203 | if (castTy.isNull()) |
| 204 | return V; |
Ted Kremenek | 852274d | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 205 | |
| 206 | ASTContext &Ctx = ValMgr.getContext(); |
Zhongxing Xu | 2f4a6b2 | 2009-12-09 08:32:57 +0000 | [diff] [blame] | 207 | |
Ted Kremenek | c50e6df | 2010-01-11 02:33:26 +0000 | [diff] [blame] | 208 | 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 Xu | 652be34 | 2009-11-16 04:49:44 +0000 | [diff] [blame] | 223 | return V; |
Ted Kremenek | 1894dce | 2009-08-25 20:51:30 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Ted Kremenek | 81a9583 | 2009-12-03 03:27:11 +0000 | [diff] [blame] | 226 | const 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 Kremenek | 67d1287 | 2009-12-07 22:05:27 +0000 | [diff] [blame] | 237 | |
| 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. |
| 246 | SVal StoreManager::getLValueCompoundLiteral(const CompoundLiteralExpr* CL, |
| 247 | const LocationContext *LC) { |
| 248 | return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL, LC)); |
Zhongxing Xu | 2f4a6b2 | 2009-12-09 08:32:57 +0000 | [diff] [blame] | 249 | } |