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 | 48ce7de | 2009-07-06 20:21:51 +0000 | [diff] [blame] | 19 | StoreManager::StoreManager(GRStateManager &stateMgr, bool useNewCastRegion) |
Ted Kremenek | c62abc1 | 2009-04-21 21:51:34 +0000 | [diff] [blame] | 20 | : ValMgr(stateMgr.getValueManager()), |
| 21 | StateMgr(stateMgr), |
Ted Kremenek | 48ce7de | 2009-07-06 20:21:51 +0000 | [diff] [blame] | 22 | UseNewCastRegion(useNewCastRegion), |
Ted Kremenek | c62abc1 | 2009-04-21 21:51:34 +0000 | [diff] [blame] | 23 | MRMgr(ValMgr.getRegionManager()) {} |
| 24 | |
| 25 | StoreManager::CastResult |
Ted Kremenek | 411af40 | 2009-07-06 22:23:45 +0000 | [diff] [blame] | 26 | StoreManager::MakeElementRegion(const GRState *state, const MemRegion *region, |
| 27 | QualType pointeeTy, QualType castToTy) { |
| 28 | |
| 29 | // Record the cast type of the region. |
| 30 | state = setCastType(state, region, castToTy); |
| 31 | |
| 32 | // Create a new ElementRegion at offset 0. |
| 33 | SVal idx = ValMgr.makeZeroArrayIndex(); |
| 34 | return CastResult(state, MRMgr.getElementRegion(pointeeTy, idx, region, |
| 35 | ValMgr.getContext())); |
| 36 | } |
| 37 | |
| 38 | StoreManager::CastResult |
Ted Kremenek | 48ce7de | 2009-07-06 20:21:51 +0000 | [diff] [blame] | 39 | StoreManager::NewCastRegion(const GRState *state, const MemRegion* R, |
| 40 | QualType CastToTy) { |
| 41 | |
| 42 | ASTContext& Ctx = StateMgr.getContext(); |
| 43 | |
| 44 | // We need to know the real type of CastToTy. |
| 45 | QualType ToTy = Ctx.getCanonicalType(CastToTy); |
Ted Kremenek | 411af40 | 2009-07-06 22:23:45 +0000 | [diff] [blame] | 46 | |
Ted Kremenek | b9a4425 | 2009-07-06 22:39:40 +0000 | [diff] [blame] | 47 | // Handle casts to Objective-C objects. |
Ted Kremenek | fc8f57c | 2009-07-06 22:56:37 +0000 | [diff] [blame^] | 48 | if (Ctx.isObjCObjectPointerType(CastToTy)) { |
| 49 | state = setCastType(state, R, CastToTy); |
Ted Kremenek | 145918c | 2009-07-06 21:01:16 +0000 | [diff] [blame] | 50 | return CastResult(state, R); |
| 51 | } |
Ted Kremenek | 411af40 | 2009-07-06 22:23:45 +0000 | [diff] [blame] | 52 | |
Ted Kremenek | 48ce7de | 2009-07-06 20:21:51 +0000 | [diff] [blame] | 53 | // Now assume we are casting from pointer to pointer. Other cases should |
| 54 | // already be handled. |
Ted Kremenek | fc8f57c | 2009-07-06 22:56:37 +0000 | [diff] [blame^] | 55 | QualType PointeeTy = CastToTy->getAsPointerType()->getPointeeType(); |
Ted Kremenek | 411af40 | 2009-07-06 22:23:45 +0000 | [diff] [blame] | 56 | |
Ted Kremenek | 48ce7de | 2009-07-06 20:21:51 +0000 | [diff] [blame] | 57 | // Process region cast according to the kind of the region being cast. |
Ted Kremenek | fc8f57c | 2009-07-06 22:56:37 +0000 | [diff] [blame^] | 58 | switch (R->getKind()) { |
| 59 | case MemRegion::BEG_TYPED_REGIONS: |
| 60 | case MemRegion::MemSpaceRegionKind: |
| 61 | case MemRegion::BEG_DECL_REGIONS: |
| 62 | case MemRegion::END_DECL_REGIONS: |
| 63 | case MemRegion::END_TYPED_REGIONS: |
| 64 | case MemRegion::TypedViewRegionKind: { |
| 65 | assert(0 && "Invalid region cast"); |
| 66 | break; |
Ted Kremenek | 48ce7de | 2009-07-06 20:21:51 +0000 | [diff] [blame] | 67 | } |
Ted Kremenek | fc8f57c | 2009-07-06 22:56:37 +0000 | [diff] [blame^] | 68 | |
| 69 | case MemRegion::CodeTextRegionKind: { |
| 70 | // CodeTextRegion should be cast to only function pointer type. |
| 71 | assert(CastToTy->isFunctionPointerType() || CastToTy->isBlockPointerType() |
| 72 | || (CastToTy->isPointerType() && |
| 73 | CastToTy->getAsPointerType()->getPointeeType()->isVoidType())); |
| 74 | break; |
| 75 | } |
| 76 | |
| 77 | case MemRegion::StringRegionKind: |
| 78 | // Handle casts of string literals. |
| 79 | return MakeElementRegion(state, R, PointeeTy, CastToTy); |
| 80 | |
| 81 | case MemRegion::ObjCObjectRegionKind: |
| 82 | case MemRegion::SymbolicRegionKind: |
| 83 | // FIXME: Need to handle arbitrary downcasts. |
| 84 | case MemRegion::AllocaRegionKind: { |
| 85 | state = setCastType(state, R, CastToTy); |
| 86 | break; |
| 87 | } |
| 88 | |
| 89 | case MemRegion::CompoundLiteralRegionKind: |
| 90 | case MemRegion::ElementRegionKind: |
| 91 | case MemRegion::FieldRegionKind: |
| 92 | case MemRegion::ObjCIvarRegionKind: |
| 93 | case MemRegion::VarRegionKind: { |
| 94 | // VarRegion, ElementRegion, and FieldRegion has an inherent type. Normally |
| 95 | // they should not be cast. We only layer an ElementRegion when the cast-to |
| 96 | // pointee type is of smaller size. In other cases, we return the original |
| 97 | // VarRegion. |
| 98 | |
| 99 | // If the pointee type is incomplete, do not compute its size, and return |
| 100 | // the original region. |
| 101 | if (const RecordType *RT = PointeeTy->getAsRecordType()) { |
| 102 | const RecordDecl *D = RT->getDecl(); |
| 103 | if (!D->getDefinition(Ctx)) |
| 104 | return CastResult(state, R); |
| 105 | } |
| 106 | |
| 107 | QualType ObjTy = cast<TypedRegion>(R)->getValueType(Ctx); |
| 108 | uint64_t PointeeTySize = Ctx.getTypeSize(PointeeTy); |
| 109 | uint64_t ObjTySize = Ctx.getTypeSize(ObjTy); |
| 110 | |
| 111 | if ((PointeeTySize > 0 && PointeeTySize < ObjTySize) || |
| 112 | (ObjTy->isAggregateType() && PointeeTy->isScalarType()) || |
| 113 | ObjTySize == 0 /* R has 'void*' type. */) |
| 114 | return MakeElementRegion(state, R, PointeeTy, ToTy); |
| 115 | |
Ted Kremenek | 48ce7de | 2009-07-06 20:21:51 +0000 | [diff] [blame] | 116 | state = setCastType(state, R, ToTy); |
Ted Kremenek | fc8f57c | 2009-07-06 22:56:37 +0000 | [diff] [blame^] | 117 | break; |
Ted Kremenek | 48ce7de | 2009-07-06 20:21:51 +0000 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | |
Ted Kremenek | fc8f57c | 2009-07-06 22:56:37 +0000 | [diff] [blame^] | 121 | return CastResult(state, R); |
Ted Kremenek | 48ce7de | 2009-07-06 20:21:51 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | |
| 125 | StoreManager::CastResult |
| 126 | StoreManager::OldCastRegion(const GRState* state, const MemRegion* R, |
Ted Kremenek | fd6b4f3 | 2009-05-04 06:35:49 +0000 | [diff] [blame] | 127 | QualType CastToTy) { |
Ted Kremenek | c62abc1 | 2009-04-21 21:51:34 +0000 | [diff] [blame] | 128 | |
Ted Kremenek | 30d1b99 | 2009-04-21 23:31:46 +0000 | [diff] [blame] | 129 | ASTContext& Ctx = StateMgr.getContext(); |
| 130 | |
| 131 | // We need to know the real type of CastToTy. |
| 132 | QualType ToTy = Ctx.getCanonicalType(CastToTy); |
| 133 | |
Ted Kremenek | c62abc1 | 2009-04-21 21:51:34 +0000 | [diff] [blame] | 134 | // Return the same region if the region types are compatible. |
| 135 | if (const TypedRegion* TR = dyn_cast<TypedRegion>(R)) { |
Zhongxing Xu | ff69782 | 2009-05-09 00:50:33 +0000 | [diff] [blame] | 136 | QualType Ta = Ctx.getCanonicalType(TR->getLocationType(Ctx)); |
Ted Kremenek | 30d1b99 | 2009-04-21 23:31:46 +0000 | [diff] [blame] | 137 | |
| 138 | if (Ta == ToTy) |
Ted Kremenek | c62abc1 | 2009-04-21 21:51:34 +0000 | [diff] [blame] | 139 | return CastResult(state, R); |
| 140 | } |
| 141 | |
Ted Kremenek | fd6b4f3 | 2009-05-04 06:35:49 +0000 | [diff] [blame] | 142 | if (const PointerType* PTy = dyn_cast<PointerType>(ToTy.getTypePtr())) { |
| 143 | // Check if we are casting to 'void*'. |
| 144 | // FIXME: Handle arbitrary upcasts. |
| 145 | QualType Pointee = PTy->getPointeeType(); |
| 146 | if (Pointee->isVoidType()) { |
Ted Kremenek | ca4e1b7 | 2009-07-06 20:53:52 +0000 | [diff] [blame] | 147 | while (true) { |
Ted Kremenek | 4253051 | 2009-05-06 18:19:24 +0000 | [diff] [blame] | 148 | if (const TypedViewRegion *TR = dyn_cast<TypedViewRegion>(R)) { |
| 149 | // Casts to void* removes TypedViewRegion. This happens when: |
| 150 | // |
| 151 | // void foo(void*); |
| 152 | // ... |
| 153 | // void bar() { |
| 154 | // int x; |
| 155 | // foo(&x); |
| 156 | // } |
| 157 | // |
| 158 | R = TR->removeViews(); |
| 159 | continue; |
| 160 | } |
| 161 | else if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) { |
| 162 | // Casts to void* also removes ElementRegions. This happens when: |
| 163 | // |
| 164 | // void foo(void*); |
| 165 | // ... |
| 166 | // void bar() { |
| 167 | // int x; |
| 168 | // foo((char*)&x); |
| 169 | // } |
| 170 | // |
| 171 | R = ER->getSuperRegion(); |
| 172 | continue; |
| 173 | } |
Ted Kremenek | 145918c | 2009-07-06 21:01:16 +0000 | [diff] [blame] | 174 | |
| 175 | break; |
Ted Kremenek | 4253051 | 2009-05-06 18:19:24 +0000 | [diff] [blame] | 176 | } |
Ted Kremenek | 30d1b99 | 2009-04-21 23:31:46 +0000 | [diff] [blame] | 177 | |
| 178 | return CastResult(state, R); |
| 179 | } |
Ted Kremenek | fd6b4f3 | 2009-05-04 06:35:49 +0000 | [diff] [blame] | 180 | else if (Pointee->isIntegerType()) { |
| 181 | // FIXME: At some point, it stands to reason that this 'dyn_cast' should |
| 182 | // become a 'cast' and that 'R' will always be a TypedRegion. |
| 183 | if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) { |
| 184 | // Check if we are casting to a region with an integer type. We now |
| 185 | // the types aren't the same, so we construct an ElementRegion. |
Ted Kremenek | cd9392f | 2009-05-04 15:17:38 +0000 | [diff] [blame] | 186 | SVal Idx = ValMgr.makeZeroArrayIndex(); |
Ted Kremenek | 20bd746 | 2009-05-04 07:04:36 +0000 | [diff] [blame] | 187 | |
| 188 | // If the super region is an element region, strip it away. |
| 189 | // FIXME: Is this the right thing to do in all cases? |
Ted Kremenek | 19cfa2b | 2009-06-30 22:31:23 +0000 | [diff] [blame] | 190 | const MemRegion *Base = isa<ElementRegion>(TR) ? TR->getSuperRegion() |
| 191 | : TR; |
Zhongxing Xu | 143b2fc | 2009-06-16 09:55:50 +0000 | [diff] [blame] | 192 | ElementRegion* ER = MRMgr.getElementRegion(Pointee, Idx, Base, |
Ted Kremenek | 19cfa2b | 2009-06-30 22:31:23 +0000 | [diff] [blame] | 193 | StateMgr.getContext()); |
Ted Kremenek | fd6b4f3 | 2009-05-04 06:35:49 +0000 | [diff] [blame] | 194 | return CastResult(state, ER); |
| 195 | } |
| 196 | } |
| 197 | } |
Ted Kremenek | 30d1b99 | 2009-04-21 23:31:46 +0000 | [diff] [blame] | 198 | |
Ted Kremenek | a8607d1 | 2009-05-01 19:22:20 +0000 | [diff] [blame] | 199 | // FIXME: Need to handle arbitrary downcasts. |
| 200 | // FIXME: Handle the case where a TypedViewRegion (layering a SymbolicRegion |
| 201 | // or an AllocaRegion is cast to another view, thus causing the memory |
| 202 | // to be re-used for a different purpose. |
Ted Kremenek | a8607d1 | 2009-05-01 19:22:20 +0000 | [diff] [blame] | 203 | if (isa<SymbolicRegion>(R) || isa<AllocaRegion>(R)) { |
| 204 | const MemRegion* ViewR = MRMgr.getTypedViewRegion(CastToTy, R); |
| 205 | return CastResult(AddRegionView(state, ViewR, R), ViewR); |
| 206 | } |
| 207 | |
| 208 | return CastResult(state, R); |
Ted Kremenek | c62abc1 | 2009-04-21 21:51:34 +0000 | [diff] [blame] | 209 | } |
Zhongxing Xu | 43e2aaf | 2009-07-06 03:41:27 +0000 | [diff] [blame] | 210 | |
| 211 | const GRState *StoreManager::InvalidateRegion(const GRState *state, |
Zhongxing Xu | 313b6da | 2009-07-06 06:01:24 +0000 | [diff] [blame] | 212 | const MemRegion *R, |
Zhongxing Xu | 43e2aaf | 2009-07-06 03:41:27 +0000 | [diff] [blame] | 213 | const Expr *E, unsigned Count) { |
Zhongxing Xu | 313b6da | 2009-07-06 06:01:24 +0000 | [diff] [blame] | 214 | ASTContext& Ctx = StateMgr.getContext(); |
| 215 | |
Zhongxing Xu | 43e2aaf | 2009-07-06 03:41:27 +0000 | [diff] [blame] | 216 | if (!R->isBoundable()) |
| 217 | return state; |
| 218 | |
Zhongxing Xu | 313b6da | 2009-07-06 06:01:24 +0000 | [diff] [blame] | 219 | if (isa<AllocaRegion>(R) || isa<SymbolicRegion>(R)) { |
| 220 | // Invalidate the alloca region by setting its default value to |
| 221 | // conjured symbol. The type of the symbol is irrelavant. |
| 222 | SVal V = ValMgr.getConjuredSymbolVal(E, Ctx.IntTy, Count); |
| 223 | state = setDefaultValue(state, R, V); |
| 224 | return state; |
| 225 | } |
| 226 | |
| 227 | const TypedRegion *TR = cast<TypedRegion>(R); |
| 228 | |
| 229 | QualType T = TR->getValueType(Ctx); |
Zhongxing Xu | 43e2aaf | 2009-07-06 03:41:27 +0000 | [diff] [blame] | 230 | |
| 231 | if (Loc::IsLocType(T) || (T->isIntegerType() && T->isScalarType())) { |
| 232 | SVal V = ValMgr.getConjuredSymbolVal(E, T, Count); |
Zhongxing Xu | 313b6da | 2009-07-06 06:01:24 +0000 | [diff] [blame] | 233 | return Bind(state, ValMgr.makeLoc(TR), V); |
Zhongxing Xu | 43e2aaf | 2009-07-06 03:41:27 +0000 | [diff] [blame] | 234 | } |
| 235 | else if (const RecordType *RT = T->getAsStructureType()) { |
| 236 | // FIXME: handle structs with default region value. |
| 237 | const RecordDecl *RD = RT->getDecl()->getDefinition(Ctx); |
| 238 | |
| 239 | // No record definition. There is nothing we can do. |
| 240 | if (!RD) |
| 241 | return state; |
| 242 | |
| 243 | // Iterate through the fields and construct new symbols. |
| 244 | for (RecordDecl::field_iterator FI=RD->field_begin(), |
| 245 | FE=RD->field_end(); FI!=FE; ++FI) { |
| 246 | |
| 247 | // For now just handle scalar fields. |
| 248 | FieldDecl *FD = *FI; |
| 249 | QualType FT = FD->getType(); |
Zhongxing Xu | 313b6da | 2009-07-06 06:01:24 +0000 | [diff] [blame] | 250 | const FieldRegion* FR = MRMgr.getFieldRegion(FD, TR); |
Zhongxing Xu | 43e2aaf | 2009-07-06 03:41:27 +0000 | [diff] [blame] | 251 | |
| 252 | if (Loc::IsLocType(FT) || |
| 253 | (FT->isIntegerType() && FT->isScalarType())) { |
| 254 | SVal V = ValMgr.getConjuredSymbolVal(E, FT, Count); |
| 255 | state = state->bindLoc(ValMgr.makeLoc(FR), V); |
| 256 | } |
| 257 | else if (FT->isStructureType()) { |
| 258 | // set the default value of the struct field to conjured |
| 259 | // symbol. Note that the type of the symbol is irrelavant. |
| 260 | // We cannot use the type of the struct otherwise ValMgr won't |
| 261 | // give us the conjured symbol. |
| 262 | SVal V = ValMgr.getConjuredSymbolVal(E, Ctx.IntTy, Count); |
| 263 | state = setDefaultValue(state, FR, V); |
| 264 | } |
| 265 | } |
| 266 | } else if (const ArrayType *AT = Ctx.getAsArrayType(T)) { |
| 267 | // Set the default value of the array to conjured symbol. |
| 268 | SVal V = ValMgr.getConjuredSymbolVal(E, AT->getElementType(), |
| 269 | Count); |
Zhongxing Xu | 313b6da | 2009-07-06 06:01:24 +0000 | [diff] [blame] | 270 | state = setDefaultValue(state, TR, V); |
Zhongxing Xu | 43e2aaf | 2009-07-06 03:41:27 +0000 | [diff] [blame] | 271 | } else { |
| 272 | // Just blast away other values. |
Zhongxing Xu | 313b6da | 2009-07-06 06:01:24 +0000 | [diff] [blame] | 273 | state = Bind(state, ValMgr.makeLoc(TR), UnknownVal()); |
Zhongxing Xu | 43e2aaf | 2009-07-06 03:41:27 +0000 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | return state; |
| 277 | } |