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