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 | |
| 19 | StoreManager::StoreManager(GRStateManager &stateMgr) |
| 20 | : ValMgr(stateMgr.getValueManager()), |
| 21 | StateMgr(stateMgr), |
| 22 | MRMgr(ValMgr.getRegionManager()) {} |
| 23 | |
| 24 | StoreManager::CastResult |
| 25 | StoreManager::CastRegion(const GRState* state, const MemRegion* R, |
Ted Kremenek | fd6b4f3 | 2009-05-04 06:35:49 +0000 | [diff] [blame] | 26 | QualType CastToTy) { |
Ted Kremenek | c62abc1 | 2009-04-21 21:51:34 +0000 | [diff] [blame] | 27 | |
Ted Kremenek | 30d1b99 | 2009-04-21 23:31:46 +0000 | [diff] [blame] | 28 | ASTContext& Ctx = StateMgr.getContext(); |
| 29 | |
| 30 | // We need to know the real type of CastToTy. |
| 31 | QualType ToTy = Ctx.getCanonicalType(CastToTy); |
| 32 | |
Ted Kremenek | c62abc1 | 2009-04-21 21:51:34 +0000 | [diff] [blame] | 33 | // Return the same region if the region types are compatible. |
| 34 | if (const TypedRegion* TR = dyn_cast<TypedRegion>(R)) { |
Zhongxing Xu | ff69782 | 2009-05-09 00:50:33 +0000 | [diff] [blame] | 35 | QualType Ta = Ctx.getCanonicalType(TR->getLocationType(Ctx)); |
Ted Kremenek | 30d1b99 | 2009-04-21 23:31:46 +0000 | [diff] [blame] | 36 | |
| 37 | if (Ta == ToTy) |
Ted Kremenek | c62abc1 | 2009-04-21 21:51:34 +0000 | [diff] [blame] | 38 | return CastResult(state, R); |
| 39 | } |
| 40 | |
Ted Kremenek | fd6b4f3 | 2009-05-04 06:35:49 +0000 | [diff] [blame] | 41 | if (const PointerType* PTy = dyn_cast<PointerType>(ToTy.getTypePtr())) { |
| 42 | // Check if we are casting to 'void*'. |
| 43 | // FIXME: Handle arbitrary upcasts. |
| 44 | QualType Pointee = PTy->getPointeeType(); |
| 45 | if (Pointee->isVoidType()) { |
Ted Kremenek | 30d1b99 | 2009-04-21 23:31:46 +0000 | [diff] [blame] | 46 | |
Ted Kremenek | 4253051 | 2009-05-06 18:19:24 +0000 | [diff] [blame] | 47 | do { |
| 48 | if (const TypedViewRegion *TR = dyn_cast<TypedViewRegion>(R)) { |
| 49 | // Casts to void* removes TypedViewRegion. This happens when: |
| 50 | // |
| 51 | // void foo(void*); |
| 52 | // ... |
| 53 | // void bar() { |
| 54 | // int x; |
| 55 | // foo(&x); |
| 56 | // } |
| 57 | // |
| 58 | R = TR->removeViews(); |
| 59 | continue; |
| 60 | } |
| 61 | else if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) { |
| 62 | // Casts to void* also removes ElementRegions. This happens when: |
| 63 | // |
| 64 | // void foo(void*); |
| 65 | // ... |
| 66 | // void bar() { |
| 67 | // int x; |
| 68 | // foo((char*)&x); |
| 69 | // } |
| 70 | // |
| 71 | R = ER->getSuperRegion(); |
| 72 | continue; |
| 73 | } |
| 74 | else |
| 75 | break; |
| 76 | } |
| 77 | while (0); |
Ted Kremenek | 30d1b99 | 2009-04-21 23:31:46 +0000 | [diff] [blame] | 78 | |
| 79 | return CastResult(state, R); |
| 80 | } |
Ted Kremenek | fd6b4f3 | 2009-05-04 06:35:49 +0000 | [diff] [blame] | 81 | else if (Pointee->isIntegerType()) { |
| 82 | // FIXME: At some point, it stands to reason that this 'dyn_cast' should |
| 83 | // become a 'cast' and that 'R' will always be a TypedRegion. |
| 84 | if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) { |
| 85 | // Check if we are casting to a region with an integer type. We now |
| 86 | // the types aren't the same, so we construct an ElementRegion. |
Ted Kremenek | cd9392f | 2009-05-04 15:17:38 +0000 | [diff] [blame] | 87 | SVal Idx = ValMgr.makeZeroArrayIndex(); |
Ted Kremenek | 20bd746 | 2009-05-04 07:04:36 +0000 | [diff] [blame] | 88 | |
| 89 | // If the super region is an element region, strip it away. |
| 90 | // FIXME: Is this the right thing to do in all cases? |
Ted Kremenek | 19cfa2b | 2009-06-30 22:31:23 +0000 | [diff] [blame] | 91 | const MemRegion *Base = isa<ElementRegion>(TR) ? TR->getSuperRegion() |
| 92 | : TR; |
Zhongxing Xu | 143b2fc | 2009-06-16 09:55:50 +0000 | [diff] [blame] | 93 | ElementRegion* ER = MRMgr.getElementRegion(Pointee, Idx, Base, |
Ted Kremenek | 19cfa2b | 2009-06-30 22:31:23 +0000 | [diff] [blame] | 94 | StateMgr.getContext()); |
Ted Kremenek | fd6b4f3 | 2009-05-04 06:35:49 +0000 | [diff] [blame] | 95 | return CastResult(state, ER); |
| 96 | } |
| 97 | } |
| 98 | } |
Ted Kremenek | 30d1b99 | 2009-04-21 23:31:46 +0000 | [diff] [blame] | 99 | |
Ted Kremenek | a8607d1 | 2009-05-01 19:22:20 +0000 | [diff] [blame] | 100 | // FIXME: Need to handle arbitrary downcasts. |
| 101 | // FIXME: Handle the case where a TypedViewRegion (layering a SymbolicRegion |
| 102 | // or an AllocaRegion is cast to another view, thus causing the memory |
| 103 | // to be re-used for a different purpose. |
Ted Kremenek | 30d1b99 | 2009-04-21 23:31:46 +0000 | [diff] [blame] | 104 | |
Ted Kremenek | a8607d1 | 2009-05-01 19:22:20 +0000 | [diff] [blame] | 105 | if (isa<SymbolicRegion>(R) || isa<AllocaRegion>(R)) { |
| 106 | const MemRegion* ViewR = MRMgr.getTypedViewRegion(CastToTy, R); |
| 107 | return CastResult(AddRegionView(state, ViewR, R), ViewR); |
| 108 | } |
| 109 | |
| 110 | return CastResult(state, R); |
Ted Kremenek | c62abc1 | 2009-04-21 21:51:34 +0000 | [diff] [blame] | 111 | } |
Zhongxing Xu | 43e2aaf | 2009-07-06 03:41:27 +0000 | [diff] [blame] | 112 | |
| 113 | const GRState *StoreManager::InvalidateRegion(const GRState *state, |
| 114 | const TypedRegion *R, |
| 115 | const Expr *E, unsigned Count) { |
| 116 | if (!R->isBoundable()) |
| 117 | return state; |
| 118 | |
| 119 | ASTContext& Ctx = StateMgr.getContext(); |
| 120 | QualType T = R->getValueType(Ctx); |
| 121 | |
| 122 | if (Loc::IsLocType(T) || (T->isIntegerType() && T->isScalarType())) { |
| 123 | SVal V = ValMgr.getConjuredSymbolVal(E, T, Count); |
| 124 | return Bind(state, ValMgr.makeLoc(R), V); |
| 125 | } |
| 126 | else if (const RecordType *RT = T->getAsStructureType()) { |
| 127 | // FIXME: handle structs with default region value. |
| 128 | const RecordDecl *RD = RT->getDecl()->getDefinition(Ctx); |
| 129 | |
| 130 | // No record definition. There is nothing we can do. |
| 131 | if (!RD) |
| 132 | return state; |
| 133 | |
| 134 | // Iterate through the fields and construct new symbols. |
| 135 | for (RecordDecl::field_iterator FI=RD->field_begin(), |
| 136 | FE=RD->field_end(); FI!=FE; ++FI) { |
| 137 | |
| 138 | // For now just handle scalar fields. |
| 139 | FieldDecl *FD = *FI; |
| 140 | QualType FT = FD->getType(); |
| 141 | const FieldRegion* FR = MRMgr.getFieldRegion(FD, R); |
| 142 | |
| 143 | if (Loc::IsLocType(FT) || |
| 144 | (FT->isIntegerType() && FT->isScalarType())) { |
| 145 | SVal V = ValMgr.getConjuredSymbolVal(E, FT, Count); |
| 146 | state = state->bindLoc(ValMgr.makeLoc(FR), V); |
| 147 | } |
| 148 | else if (FT->isStructureType()) { |
| 149 | // set the default value of the struct field to conjured |
| 150 | // symbol. Note that the type of the symbol is irrelavant. |
| 151 | // We cannot use the type of the struct otherwise ValMgr won't |
| 152 | // give us the conjured symbol. |
| 153 | SVal V = ValMgr.getConjuredSymbolVal(E, Ctx.IntTy, Count); |
| 154 | state = setDefaultValue(state, FR, V); |
| 155 | } |
| 156 | } |
| 157 | } else if (const ArrayType *AT = Ctx.getAsArrayType(T)) { |
| 158 | // Set the default value of the array to conjured symbol. |
| 159 | SVal V = ValMgr.getConjuredSymbolVal(E, AT->getElementType(), |
| 160 | Count); |
| 161 | state = setDefaultValue(state, R, V); |
| 162 | } else { |
| 163 | // Just blast away other values. |
| 164 | state = Bind(state, ValMgr.makeLoc(R), UnknownVal()); |
| 165 | } |
| 166 | |
| 167 | return state; |
| 168 | } |