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 | |
| 23 | StoreManager::CastResult |
Ted Kremenek | 411af40 | 2009-07-06 22:23:45 +0000 | [diff] [blame] | 24 | StoreManager::MakeElementRegion(const GRState *state, const MemRegion *region, |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 25 | QualType pointeeTy, QualType castToTy, |
| 26 | uint64_t index) { |
| 27 | // Create a new ElementRegion. |
| 28 | SVal idx = ValMgr.makeArrayIndex(index); |
Ted Kremenek | 411af40 | 2009-07-06 22:23:45 +0000 | [diff] [blame] | 29 | return CastResult(state, MRMgr.getElementRegion(pointeeTy, idx, region, |
| 30 | ValMgr.getContext())); |
| 31 | } |
| 32 | |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 33 | // FIXME: Merge with the implementation of the same method in MemRegion.cpp |
Ted Kremenek | 169077d | 2009-07-06 23:47:19 +0000 | [diff] [blame] | 34 | static bool IsCompleteType(ASTContext &Ctx, QualType Ty) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 35 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
Ted Kremenek | 169077d | 2009-07-06 23:47:19 +0000 | [diff] [blame] | 36 | const RecordDecl *D = RT->getDecl(); |
| 37 | if (!D->getDefinition(Ctx)) |
| 38 | return false; |
| 39 | } |
| 40 | |
| 41 | return true; |
| 42 | } |
| 43 | |
Ted Kremenek | 411af40 | 2009-07-06 22:23:45 +0000 | [diff] [blame] | 44 | StoreManager::CastResult |
Ted Kremenek | f7a0cf4 | 2009-07-29 21:43:22 +0000 | [diff] [blame] | 45 | StoreManager::CastRegion(const GRState *state, const MemRegion* R, |
| 46 | QualType CastToTy) { |
Ted Kremenek | 48ce7de | 2009-07-06 20:21:51 +0000 | [diff] [blame] | 47 | |
| 48 | ASTContext& Ctx = StateMgr.getContext(); |
| 49 | |
Ted Kremenek | b9a4425 | 2009-07-06 22:39:40 +0000 | [diff] [blame] | 50 | // Handle casts to Objective-C objects. |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 51 | if (CastToTy->isObjCObjectPointerType()) |
| 52 | return CastResult(state, R->getBaseRegion()); |
| 53 | |
Ted Kremenek | 63b9cfe | 2009-07-18 06:27:51 +0000 | [diff] [blame] | 54 | 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 Kremenek | 411af40 | 2009-07-06 22:23:45 +0000 | [diff] [blame] | 70 | |
Ted Kremenek | 48ce7de | 2009-07-06 20:21:51 +0000 | [diff] [blame] | 71 | // Now assume we are casting from pointer to pointer. Other cases should |
| 72 | // already be handled. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 73 | QualType PointeeTy = CastToTy->getAs<PointerType>()->getPointeeType(); |
Ted Kremenek | 9a108eb | 2009-08-02 04:12:53 +0000 | [diff] [blame] | 74 | QualType CanonPointeeTy = Ctx.getCanonicalType(PointeeTy); |
| 75 | |
| 76 | // Handle casts to void*. We just pass the region through. |
| 77 | if (CanonPointeeTy.getUnqualifiedType() == Ctx.VoidTy) |
| 78 | return CastResult(state, R); |
Ted Kremenek | 169077d | 2009-07-06 23:47:19 +0000 | [diff] [blame] | 79 | |
Ted Kremenek | 9a108eb | 2009-08-02 04:12:53 +0000 | [diff] [blame] | 80 | // Handle casts from compatible types. |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 81 | if (R->isBoundable()) |
| 82 | if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) { |
| 83 | QualType ObjTy = Ctx.getCanonicalType(TR->getValueType(Ctx)); |
Ted Kremenek | 9a108eb | 2009-08-02 04:12:53 +0000 | [diff] [blame] | 84 | if (CanonPointeeTy == ObjTy) |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 85 | return CastResult(state, R); |
| 86 | } |
| 87 | |
Ted Kremenek | 48ce7de | 2009-07-06 20:21:51 +0000 | [diff] [blame] | 88 | // Process region cast according to the kind of the region being cast. |
Ted Kremenek | fc8f57c | 2009-07-06 22:56:37 +0000 | [diff] [blame] | 89 | switch (R->getKind()) { |
| 90 | case MemRegion::BEG_TYPED_REGIONS: |
| 91 | case MemRegion::MemSpaceRegionKind: |
| 92 | case MemRegion::BEG_DECL_REGIONS: |
| 93 | case MemRegion::END_DECL_REGIONS: |
Ted Kremenek | f7a0cf4 | 2009-07-29 21:43:22 +0000 | [diff] [blame] | 94 | case MemRegion::END_TYPED_REGIONS: { |
Ted Kremenek | fc8f57c | 2009-07-06 22:56:37 +0000 | [diff] [blame] | 95 | assert(0 && "Invalid region cast"); |
| 96 | break; |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 97 | } |
Ted Kremenek | fc8f57c | 2009-07-06 22:56:37 +0000 | [diff] [blame] | 98 | case MemRegion::CodeTextRegionKind: { |
Ted Kremenek | 63b9cfe | 2009-07-18 06:27:51 +0000 | [diff] [blame] | 99 | // CodeTextRegion should be cast to only a function or block pointer type, |
| 100 | // although they can in practice be casted to anything, e.g, void*, |
| 101 | // char*, etc. |
Ted Kremenek | 8d344ae | 2009-07-10 21:24:45 +0000 | [diff] [blame] | 102 | // Just pass the region through. |
Ted Kremenek | fc8f57c | 2009-07-06 22:56:37 +0000 | [diff] [blame] | 103 | break; |
| 104 | } |
| 105 | |
| 106 | case MemRegion::StringRegionKind: |
Ted Kremenek | fc8f57c | 2009-07-06 22:56:37 +0000 | [diff] [blame] | 107 | case MemRegion::ObjCObjectRegionKind: |
Ted Kremenek | fc8f57c | 2009-07-06 22:56:37 +0000 | [diff] [blame] | 108 | // FIXME: Need to handle arbitrary downcasts. |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 109 | case MemRegion::SymbolicRegionKind: |
| 110 | case MemRegion::AllocaRegionKind: |
Ted Kremenek | fc8f57c | 2009-07-06 22:56:37 +0000 | [diff] [blame] | 111 | case MemRegion::CompoundLiteralRegionKind: |
Ted Kremenek | fc8f57c | 2009-07-06 22:56:37 +0000 | [diff] [blame] | 112 | case MemRegion::FieldRegionKind: |
| 113 | case MemRegion::ObjCIvarRegionKind: |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 114 | case MemRegion::VarRegionKind: |
| 115 | return MakeElementRegion(state, R, PointeeTy, CastToTy); |
Ted Kremenek | fc8f57c | 2009-07-06 22:56:37 +0000 | [diff] [blame] | 116 | |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 117 | case MemRegion::ElementRegionKind: { |
| 118 | // If we are casting from an ElementRegion to another type, the |
| 119 | // algorithm is as follows: |
| 120 | // |
| 121 | // (1) Compute the "raw offset" of the ElementRegion from the |
| 122 | // base region. This is done by calling 'getAsRawOffset()'. |
| 123 | // |
| 124 | // (2a) If we get a 'RegionRawOffset' after calling |
| 125 | // 'getAsRawOffset()', determine if the absolute offset |
| 126 | // can be exactly divided into chunks of the size of the |
| 127 | // casted-pointee type. If so, create a new ElementRegion with |
| 128 | // the pointee-cast type as the new ElementType and the index |
| 129 | // being the offset divded by the chunk size. If not, create |
| 130 | // a new ElementRegion at offset 0 off the raw offset region. |
| 131 | // |
| 132 | // (2b) If we don't a get a 'RegionRawOffset' after calling |
| 133 | // 'getAsRawOffset()', it means that we are at offset 0. |
| 134 | // |
| 135 | // FIXME: Handle symbolic raw offsets. |
Ted Kremenek | 169077d | 2009-07-06 23:47:19 +0000 | [diff] [blame] | 136 | |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 137 | const ElementRegion *elementR = cast<ElementRegion>(R); |
| 138 | const RegionRawOffset &rawOff = elementR->getAsRawOffset(); |
| 139 | const MemRegion *baseR = rawOff.getRegion(); |
Ted Kremenek | fc8f57c | 2009-07-06 22:56:37 +0000 | [diff] [blame] | 140 | |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 141 | // If we cannot compute a raw offset, throw up our hands and return |
| 142 | // a NULL MemRegion*. |
| 143 | if (!baseR) |
| 144 | return CastResult(state, NULL); |
| 145 | |
| 146 | int64_t off = rawOff.getByteOffset(); |
| 147 | |
| 148 | if (off == 0) { |
| 149 | // Edge case: we are at 0 bytes off the beginning of baseR. We |
| 150 | // check to see if type we are casting to is the same as the base |
| 151 | // region. If so, just return the base region. |
| 152 | if (const TypedRegion *TR = dyn_cast<TypedRegion>(baseR)) { |
| 153 | QualType ObjTy = Ctx.getCanonicalType(TR->getValueType(Ctx)); |
| 154 | QualType CanonPointeeTy = Ctx.getCanonicalType(PointeeTy); |
| 155 | if (CanonPointeeTy == ObjTy) |
| 156 | return CastResult(state, baseR); |
| 157 | } |
Ted Kremenek | fc8f57c | 2009-07-06 22:56:37 +0000 | [diff] [blame] | 158 | |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 159 | // Otherwise, create a new ElementRegion at offset 0. |
| 160 | return MakeElementRegion(state, baseR, PointeeTy, CastToTy, 0); |
| 161 | } |
| 162 | |
| 163 | // We have a non-zero offset from the base region. We want to determine |
| 164 | // if the offset can be evenly divided by sizeof(PointeeTy). If so, |
| 165 | // we create an ElementRegion whose index is that value. Otherwise, we |
| 166 | // create two ElementRegions, one that reflects a raw offset and the other |
| 167 | // that reflects the cast. |
| 168 | |
| 169 | // Compute the index for the new ElementRegion. |
| 170 | int64_t newIndex = 0; |
| 171 | const MemRegion *newSuperR = 0; |
| 172 | |
| 173 | // We can only compute sizeof(PointeeTy) if it is a complete type. |
| 174 | if (IsCompleteType(Ctx, PointeeTy)) { |
| 175 | // Compute the size in **bytes**. |
| 176 | int64_t pointeeTySize = (int64_t) (Ctx.getTypeSize(PointeeTy) / 8); |
| 177 | |
| 178 | // Is the offset a multiple of the size? If so, we can layer the |
| 179 | // ElementRegion (with elementType == PointeeTy) directly on top of |
| 180 | // the base region. |
| 181 | if (off % pointeeTySize == 0) { |
| 182 | newIndex = off / pointeeTySize; |
| 183 | newSuperR = baseR; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | if (!newSuperR) { |
| 188 | // Create an intermediate ElementRegion to represent the raw byte. |
| 189 | // This will be the super region of the final ElementRegion. |
| 190 | SVal idx = ValMgr.makeArrayIndex(off); |
| 191 | newSuperR = MRMgr.getElementRegion(Ctx.CharTy, idx, baseR, Ctx); |
| 192 | } |
| 193 | |
| 194 | return MakeElementRegion(state, newSuperR, PointeeTy, CastToTy, newIndex); |
Ted Kremenek | 48ce7de | 2009-07-06 20:21:51 +0000 | [diff] [blame] | 195 | } |
| 196 | } |
| 197 | |
Ted Kremenek | fc8f57c | 2009-07-06 22:56:37 +0000 | [diff] [blame] | 198 | return CastResult(state, R); |
Ted Kremenek | 48ce7de | 2009-07-06 20:21:51 +0000 | [diff] [blame] | 199 | } |
Ted Kremenek | 1894dce | 2009-08-25 20:51:30 +0000 | [diff] [blame] | 200 | |
| 201 | |
| 202 | /// CastRetrievedVal - Used by subclasses of StoreManager to implement |
| 203 | /// implicit casts that arise from loads from regions that are reinterpreted |
| 204 | /// as another region. |
| 205 | SValuator::CastResult StoreManager::CastRetrievedVal(SVal V, |
| 206 | const GRState *state, |
| 207 | const TypedRegion *R, |
| 208 | QualType castTy) { |
| 209 | if (castTy.isNull()) |
| 210 | return SValuator::CastResult(state, V); |
| 211 | |
| 212 | ASTContext &Ctx = ValMgr.getContext(); |
| 213 | return ValMgr.getSValuator().EvalCast(V, state, castTy, R->getValueType(Ctx)); |
| 214 | } |
| 215 | |