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