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, |
| 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. |
| 67 | if (CanonPointeeTy.getUnqualifiedType() == 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()) { |
| 80 | case MemRegion::BEG_TYPED_REGIONS: |
| 81 | case MemRegion::MemSpaceRegionKind: |
| 82 | case MemRegion::BEG_DECL_REGIONS: |
| 83 | case MemRegion::END_DECL_REGIONS: |
Ted Kremenek | f7a0cf4 | 2009-07-29 21:43:22 +0000 | [diff] [blame] | 84 | case MemRegion::END_TYPED_REGIONS: { |
Ted Kremenek | fc8f57c | 2009-07-06 22:56:37 +0000 | [diff] [blame] | 85 | assert(0 && "Invalid region cast"); |
| 86 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 87 | } |
Ted Kremenek | fc8f57c | 2009-07-06 22:56:37 +0000 | [diff] [blame] | 88 | case MemRegion::CodeTextRegionKind: { |
Ted Kremenek | 63b9cfe | 2009-07-18 06:27:51 +0000 | [diff] [blame] | 89 | // CodeTextRegion should be cast to only a function or block pointer type, |
Zhongxing Xu | 09270cc | 2009-10-14 06:55:01 +0000 | [diff] [blame] | 90 | // although they can in practice be casted to anything, e.g, void*, char*, |
| 91 | // etc. |
| 92 | // Just return the region. |
| 93 | return R; |
Ted Kremenek | fc8f57c | 2009-07-06 22:56:37 +0000 | [diff] [blame] | 94 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 95 | |
Ted Kremenek | fc8f57c | 2009-07-06 22:56:37 +0000 | [diff] [blame] | 96 | case MemRegion::StringRegionKind: |
Ted Kremenek | fc8f57c | 2009-07-06 22:56:37 +0000 | [diff] [blame] | 97 | case MemRegion::ObjCObjectRegionKind: |
Ted Kremenek | fc8f57c | 2009-07-06 22:56:37 +0000 | [diff] [blame] | 98 | // FIXME: Need to handle arbitrary downcasts. |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 99 | case MemRegion::SymbolicRegionKind: |
| 100 | case MemRegion::AllocaRegionKind: |
Ted Kremenek | fc8f57c | 2009-07-06 22:56:37 +0000 | [diff] [blame] | 101 | case MemRegion::CompoundLiteralRegionKind: |
Ted Kremenek | fc8f57c | 2009-07-06 22:56:37 +0000 | [diff] [blame] | 102 | case MemRegion::FieldRegionKind: |
| 103 | case MemRegion::ObjCIvarRegionKind: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 104 | case MemRegion::VarRegionKind: |
Zhongxing Xu | 09270cc | 2009-10-14 06:55:01 +0000 | [diff] [blame] | 105 | return MakeElementRegion(R, PointeeTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 106 | |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 107 | case MemRegion::ElementRegionKind: { |
| 108 | // If we are casting from an ElementRegion to another type, the |
| 109 | // algorithm is as follows: |
| 110 | // |
| 111 | // (1) Compute the "raw offset" of the ElementRegion from the |
| 112 | // base region. This is done by calling 'getAsRawOffset()'. |
| 113 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 114 | // (2a) If we get a 'RegionRawOffset' after calling |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 115 | // 'getAsRawOffset()', determine if the absolute offset |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 116 | // can be exactly divided into chunks of the size of the |
| 117 | // casted-pointee type. If so, create a new ElementRegion with |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 118 | // the pointee-cast type as the new ElementType and the index |
| 119 | // being the offset divded by the chunk size. If not, create |
| 120 | // a new ElementRegion at offset 0 off the raw offset region. |
| 121 | // |
| 122 | // (2b) If we don't a get a 'RegionRawOffset' after calling |
| 123 | // 'getAsRawOffset()', it means that we are at offset 0. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 124 | // |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 125 | // FIXME: Handle symbolic raw offsets. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 126 | |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 127 | const ElementRegion *elementR = cast<ElementRegion>(R); |
| 128 | const RegionRawOffset &rawOff = elementR->getAsRawOffset(); |
| 129 | const MemRegion *baseR = rawOff.getRegion(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 130 | |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 131 | // If we cannot compute a raw offset, throw up our hands and return |
| 132 | // a NULL MemRegion*. |
| 133 | if (!baseR) |
Zhongxing Xu | 09270cc | 2009-10-14 06:55:01 +0000 | [diff] [blame] | 134 | return NULL; |
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 | int64_t off = rawOff.getByteOffset(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 137 | |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 138 | if (off == 0) { |
| 139 | // Edge case: we are at 0 bytes off the beginning of baseR. We |
| 140 | // 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] | 141 | // region. If so, just return the base region. |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 142 | if (const TypedRegion *TR = dyn_cast<TypedRegion>(baseR)) { |
| 143 | QualType ObjTy = Ctx.getCanonicalType(TR->getValueType(Ctx)); |
| 144 | QualType CanonPointeeTy = Ctx.getCanonicalType(PointeeTy); |
| 145 | if (CanonPointeeTy == ObjTy) |
Zhongxing Xu | 09270cc | 2009-10-14 06:55:01 +0000 | [diff] [blame] | 146 | return baseR; |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 147 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 148 | |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 149 | // Otherwise, create a new ElementRegion at offset 0. |
Zhongxing Xu | 09270cc | 2009-10-14 06:55:01 +0000 | [diff] [blame] | 150 | return MakeElementRegion(baseR, PointeeTy); |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 151 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 152 | |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 153 | // We have a non-zero offset from the base region. We want to determine |
| 154 | // if the offset can be evenly divided by sizeof(PointeeTy). If so, |
| 155 | // we create an ElementRegion whose index is that value. Otherwise, we |
| 156 | // create two ElementRegions, one that reflects a raw offset and the other |
| 157 | // that reflects the cast. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 158 | |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 159 | // Compute the index for the new ElementRegion. |
| 160 | int64_t newIndex = 0; |
| 161 | const MemRegion *newSuperR = 0; |
| 162 | |
| 163 | // We can only compute sizeof(PointeeTy) if it is a complete type. |
| 164 | if (IsCompleteType(Ctx, PointeeTy)) { |
| 165 | // Compute the size in **bytes**. |
| 166 | int64_t pointeeTySize = (int64_t) (Ctx.getTypeSize(PointeeTy) / 8); |
| 167 | |
| 168 | // Is the offset a multiple of the size? If so, we can layer the |
| 169 | // ElementRegion (with elementType == PointeeTy) directly on top of |
| 170 | // the base region. |
| 171 | if (off % pointeeTySize == 0) { |
| 172 | newIndex = off / pointeeTySize; |
| 173 | newSuperR = baseR; |
| 174 | } |
| 175 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 176 | |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 177 | if (!newSuperR) { |
| 178 | // Create an intermediate ElementRegion to represent the raw byte. |
| 179 | // This will be the super region of the final ElementRegion. |
Zhongxing Xu | 09270cc | 2009-10-14 06:55:01 +0000 | [diff] [blame] | 180 | newSuperR = MakeElementRegion(baseR, Ctx.CharTy, off); |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 181 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 182 | |
Zhongxing Xu | 09270cc | 2009-10-14 06:55:01 +0000 | [diff] [blame] | 183 | return MakeElementRegion(newSuperR, PointeeTy, newIndex); |
Ted Kremenek | 48ce7de | 2009-07-06 20:21:51 +0000 | [diff] [blame] | 184 | } |
| 185 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 186 | |
Zhongxing Xu | 09270cc | 2009-10-14 06:55:01 +0000 | [diff] [blame] | 187 | assert(0 && "unreachable"); |
| 188 | return 0; |
Ted Kremenek | 48ce7de | 2009-07-06 20:21:51 +0000 | [diff] [blame] | 189 | } |
Ted Kremenek | 1894dce | 2009-08-25 20:51:30 +0000 | [diff] [blame] | 190 | |
| 191 | |
| 192 | /// CastRetrievedVal - Used by subclasses of StoreManager to implement |
| 193 | /// implicit casts that arise from loads from regions that are reinterpreted |
| 194 | /// as another region. |
| 195 | SValuator::CastResult StoreManager::CastRetrievedVal(SVal V, |
| 196 | const GRState *state, |
| 197 | const TypedRegion *R, |
| 198 | QualType castTy) { |
| 199 | if (castTy.isNull()) |
| 200 | return SValuator::CastResult(state, V); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 201 | |
| 202 | ASTContext &Ctx = ValMgr.getContext(); |
Ted Kremenek | 1894dce | 2009-08-25 20:51:30 +0000 | [diff] [blame] | 203 | return ValMgr.getSValuator().EvalCast(V, state, castTy, R->getValueType(Ctx)); |
| 204 | } |
| 205 | |