Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 1 | //== MemRegion.cpp - Abstract memory regions for static analysis --*- 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 defines MemRegion and its subclasses. MemRegion defines a |
| 11 | // partially-typed abstraction of memory useful for path-sensitive dataflow |
| 12 | // analyses. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #include "llvm/Support/raw_ostream.h" |
| 17 | #include "clang/Analysis/PathSensitive/MemRegion.h" |
| 18 | |
| 19 | using namespace clang; |
| 20 | |
| 21 | |
| 22 | MemRegion::~MemRegion() {} |
| 23 | |
| 24 | void MemSpaceRegion::Profile(llvm::FoldingSetNodeID& ID) const { |
| 25 | ID.AddInteger((unsigned)getKind()); |
| 26 | } |
| 27 | |
Zhongxing Xu | e9f4e54 | 2008-10-25 14:13:41 +0000 | [diff] [blame] | 28 | void StringRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, |
| 29 | const StringLiteral* Str, |
| 30 | const MemRegion* superRegion) { |
| 31 | ID.AddInteger((unsigned) StringRegionKind); |
| 32 | ID.AddPointer(Str); |
| 33 | ID.AddPointer(superRegion); |
| 34 | } |
| 35 | |
Ted Kremenek | 7090ae1 | 2008-11-02 00:34:33 +0000 | [diff] [blame] | 36 | void AllocaRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, |
| 37 | const Expr* Ex, unsigned cnt) { |
| 38 | ID.AddInteger((unsigned) AllocaRegionKind); |
| 39 | ID.AddPointer(Ex); |
| 40 | ID.AddInteger(cnt); |
| 41 | } |
| 42 | |
| 43 | void AllocaRegion::Profile(llvm::FoldingSetNodeID& ID) const { |
| 44 | ProfileRegion(ID, Ex, Cnt); |
| 45 | } |
| 46 | |
Zhongxing Xu | 817c67d | 2008-11-03 04:12:24 +0000 | [diff] [blame^] | 47 | QualType AnonPointeeRegion::getType(ASTContext& C) const { |
| 48 | QualType T = C.getCanonicalType(Pointer->getType()); |
| 49 | PointerType* PTy = cast<PointerType>(T.getTypePtr()); |
| 50 | |
| 51 | QualType PointeeTy = C.getCanonicalType(PTy->getPointeeType()); |
| 52 | return PointeeTy; |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 55 | void AnonPointeeRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, |
Zhongxing Xu | 817c67d | 2008-11-03 04:12:24 +0000 | [diff] [blame^] | 56 | const VarDecl* VD, |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 57 | const MemRegion* superRegion) { |
| 58 | ID.AddInteger((unsigned) AnonPointeeRegionKind); |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 59 | ID.AddPointer(VD); |
| 60 | ID.AddPointer(superRegion); |
| 61 | } |
| 62 | |
Ted Kremenek | 329d6fd | 2008-10-27 20:57:58 +0000 | [diff] [blame] | 63 | void CompoundLiteralRegion::Profile(llvm::FoldingSetNodeID& ID) const { |
| 64 | CompoundLiteralRegion::ProfileRegion(ID, CL, superRegion); |
| 65 | } |
| 66 | |
| 67 | void CompoundLiteralRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, |
| 68 | const CompoundLiteralExpr* CL, |
| 69 | const MemRegion* superRegion) { |
| 70 | ID.AddInteger((unsigned) CompoundLiteralRegionKind); |
| 71 | ID.AddPointer(CL); |
| 72 | ID.AddPointer(superRegion); |
| 73 | } |
| 74 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 75 | void DeclRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, const Decl* D, |
| 76 | const MemRegion* superRegion, Kind k) { |
| 77 | ID.AddInteger((unsigned) k); |
| 78 | ID.AddPointer(D); |
| 79 | ID.AddPointer(superRegion); |
| 80 | } |
| 81 | |
| 82 | void DeclRegion::Profile(llvm::FoldingSetNodeID& ID) const { |
| 83 | DeclRegion::ProfileRegion(ID, D, superRegion, getKind()); |
| 84 | } |
| 85 | |
Ted Kremenek | 993f1c7 | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 86 | void SymbolicRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, SymbolID sym) { |
| 87 | ID.AddInteger((unsigned) MemRegion::SymbolicRegionKind); |
| 88 | ID.AddInteger(sym.getNumber()); |
| 89 | } |
| 90 | |
| 91 | void SymbolicRegion::Profile(llvm::FoldingSetNodeID& ID) const { |
| 92 | SymbolicRegion::ProfileRegion(ID, sym); |
| 93 | } |
| 94 | |
Zhongxing Xu | 511191c | 2008-10-21 05:27:10 +0000 | [diff] [blame] | 95 | void ElementRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, SVal Idx, |
| 96 | const MemRegion* superRegion) { |
| 97 | ID.AddInteger(MemRegion::ElementRegionKind); |
| 98 | ID.AddPointer(superRegion); |
| 99 | Idx.Profile(ID); |
| 100 | } |
| 101 | |
| 102 | void ElementRegion::Profile(llvm::FoldingSetNodeID& ID) const { |
| 103 | ElementRegion::ProfileRegion(ID, Index, superRegion); |
| 104 | } |
Zhongxing Xu | 27b5706 | 2008-10-27 13:17:02 +0000 | [diff] [blame] | 105 | |
| 106 | QualType ElementRegion::getType(ASTContext& C) const { |
| 107 | QualType T = cast<TypedRegion>(superRegion)->getType(C); |
| 108 | ArrayType* AT = cast<ArrayType>(T.getTypePtr()); |
| 109 | return AT->getElementType(); |
| 110 | } |
| 111 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 112 | //===----------------------------------------------------------------------===// |
| 113 | // Region pretty-printing. |
| 114 | //===----------------------------------------------------------------------===// |
| 115 | |
| 116 | std::string MemRegion::getString() const { |
| 117 | std::string s; |
| 118 | llvm::raw_string_ostream os(s); |
| 119 | print(os); |
| 120 | return os.str(); |
| 121 | } |
| 122 | |
| 123 | void MemRegion::print(llvm::raw_ostream& os) const { |
| 124 | os << "<Unknown Region>"; |
| 125 | } |
| 126 | |
Ted Kremenek | 7090ae1 | 2008-11-02 00:34:33 +0000 | [diff] [blame] | 127 | void AllocaRegion::print(llvm::raw_ostream& os) const { |
| 128 | os << "alloca{" << (void*) Ex << ',' << Cnt << '}'; |
| 129 | } |
| 130 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 131 | void VarRegion::print(llvm::raw_ostream& os) const { |
| 132 | os << cast<VarDecl>(D)->getName(); |
| 133 | } |
| 134 | |
Ted Kremenek | 993f1c7 | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 135 | void SymbolicRegion::print(llvm::raw_ostream& os) const { |
| 136 | os << "$" << sym.getNumber(); |
| 137 | } |
| 138 | |
Ted Kremenek | 4bd1eef | 2008-10-17 21:05:44 +0000 | [diff] [blame] | 139 | void FieldRegion::print(llvm::raw_ostream& os) const { |
| 140 | superRegion->print(os); |
| 141 | os << "->" << getDecl()->getName(); |
| 142 | } |
| 143 | |
Zhongxing Xu | b21ff77 | 2008-10-24 06:30:07 +0000 | [diff] [blame] | 144 | void ElementRegion::print(llvm::raw_ostream& os) const { |
| 145 | superRegion->print(os); |
| 146 | os << '['; Index.print(os); os << ']'; |
| 147 | } |
| 148 | |
Ted Kremenek | 329d6fd | 2008-10-27 20:57:58 +0000 | [diff] [blame] | 149 | void CompoundLiteralRegion::print(llvm::raw_ostream& os) const { |
| 150 | // FIXME: More elaborate pretty-printing. |
| 151 | os << "{ " << (void*) CL << " }"; |
| 152 | } |
| 153 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 154 | //===----------------------------------------------------------------------===// |
| 155 | // MemRegionManager methods. |
| 156 | //===----------------------------------------------------------------------===// |
| 157 | |
| 158 | MemSpaceRegion* MemRegionManager::LazyAllocate(MemSpaceRegion*& region) { |
| 159 | |
| 160 | if (!region) { |
| 161 | region = (MemSpaceRegion*) A.Allocate<MemSpaceRegion>(); |
| 162 | new (region) MemSpaceRegion(); |
| 163 | } |
| 164 | |
| 165 | return region; |
| 166 | } |
| 167 | |
| 168 | MemSpaceRegion* MemRegionManager::getStackRegion() { |
| 169 | return LazyAllocate(stack); |
| 170 | } |
| 171 | |
| 172 | MemSpaceRegion* MemRegionManager::getGlobalsRegion() { |
| 173 | return LazyAllocate(globals); |
| 174 | } |
| 175 | |
| 176 | MemSpaceRegion* MemRegionManager::getHeapRegion() { |
| 177 | return LazyAllocate(heap); |
| 178 | } |
| 179 | |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 180 | MemSpaceRegion* MemRegionManager::getUnknownRegion() { |
| 181 | return LazyAllocate(unknown); |
| 182 | } |
| 183 | |
Zhongxing Xu | e9f4e54 | 2008-10-25 14:13:41 +0000 | [diff] [blame] | 184 | StringRegion* MemRegionManager::getStringRegion(const StringLiteral* Str) { |
| 185 | llvm::FoldingSetNodeID ID; |
| 186 | MemSpaceRegion* GlobalsR = getGlobalsRegion(); |
| 187 | |
| 188 | StringRegion::ProfileRegion(ID, Str, GlobalsR); |
| 189 | |
| 190 | void* InsertPos; |
| 191 | MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos); |
| 192 | StringRegion* R = cast_or_null<StringRegion>(data); |
| 193 | |
| 194 | if (!R) { |
| 195 | R = (StringRegion*) A.Allocate<StringRegion>(); |
| 196 | new (R) StringRegion(Str, GlobalsR); |
| 197 | Regions.InsertNode(R, InsertPos); |
| 198 | } |
| 199 | |
| 200 | return R; |
| 201 | } |
| 202 | |
Ted Kremenek | 9a1f03a | 2008-10-27 21:01:26 +0000 | [diff] [blame] | 203 | VarRegion* MemRegionManager::getVarRegion(const VarDecl* d) { |
| 204 | |
| 205 | const MemRegion* superRegion = d->hasLocalStorage() ? getStackRegion() |
| 206 | : getGlobalsRegion(); |
| 207 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 208 | llvm::FoldingSetNodeID ID; |
| 209 | DeclRegion::ProfileRegion(ID, d, superRegion, MemRegion::VarRegionKind); |
| 210 | |
| 211 | void* InsertPos; |
| 212 | MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos); |
| 213 | VarRegion* R = cast_or_null<VarRegion>(data); |
| 214 | |
| 215 | if (!R) { |
| 216 | R = (VarRegion*) A.Allocate<VarRegion>(); |
| 217 | new (R) VarRegion(d, superRegion); |
| 218 | Regions.InsertNode(R, InsertPos); |
| 219 | } |
| 220 | |
| 221 | return R; |
| 222 | } |
| 223 | |
Ted Kremenek | 329d6fd | 2008-10-27 20:57:58 +0000 | [diff] [blame] | 224 | CompoundLiteralRegion* |
| 225 | MemRegionManager::getCompoundLiteralRegion(const CompoundLiteralExpr* CL) { |
| 226 | // Is this compound literal allocated on the stack or is part of the |
| 227 | // global constant pool? |
| 228 | const MemRegion* superRegion = CL->isFileScope() ? |
| 229 | getGlobalsRegion() : getStackRegion(); |
| 230 | |
| 231 | // Profile the compound literal. |
| 232 | llvm::FoldingSetNodeID ID; |
| 233 | CompoundLiteralRegion::ProfileRegion(ID, CL, superRegion); |
| 234 | |
| 235 | void* InsertPos; |
| 236 | MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos); |
| 237 | CompoundLiteralRegion* R = cast_or_null<CompoundLiteralRegion>(data); |
| 238 | |
| 239 | if (!R) { |
| 240 | R = (CompoundLiteralRegion*) A.Allocate<CompoundLiteralRegion>(); |
| 241 | new (R) CompoundLiteralRegion(CL, superRegion); |
| 242 | Regions.InsertNode(R, InsertPos); |
| 243 | } |
| 244 | |
| 245 | return R; |
| 246 | } |
| 247 | |
Zhongxing Xu | 511191c | 2008-10-21 05:27:10 +0000 | [diff] [blame] | 248 | ElementRegion* MemRegionManager::getElementRegion(SVal Idx, |
| 249 | const MemRegion* superRegion){ |
| 250 | llvm::FoldingSetNodeID ID; |
| 251 | ElementRegion::ProfileRegion(ID, Idx, superRegion); |
| 252 | |
| 253 | void* InsertPos; |
| 254 | MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos); |
| 255 | ElementRegion* R = cast_or_null<ElementRegion>(data); |
| 256 | |
| 257 | if (!R) { |
| 258 | R = (ElementRegion*) A.Allocate<ElementRegion>(); |
| 259 | new (R) ElementRegion(Idx, superRegion); |
| 260 | Regions.InsertNode(R, InsertPos); |
| 261 | } |
| 262 | |
| 263 | return R; |
| 264 | } |
| 265 | |
Ted Kremenek | 993f1c7 | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 266 | /// getSymbolicRegion - Retrieve or create a "symbolic" memory region. |
| 267 | SymbolicRegion* MemRegionManager::getSymbolicRegion(const SymbolID sym) { |
| 268 | |
| 269 | llvm::FoldingSetNodeID ID; |
| 270 | SymbolicRegion::ProfileRegion(ID, sym); |
| 271 | |
| 272 | void* InsertPos; |
| 273 | MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos); |
| 274 | SymbolicRegion* R = cast_or_null<SymbolicRegion>(data); |
| 275 | |
| 276 | if (!R) { |
| 277 | R = (SymbolicRegion*) A.Allocate<SymbolicRegion>(); |
| 278 | new (R) SymbolicRegion(sym); |
| 279 | Regions.InsertNode(R, InsertPos); |
| 280 | } |
| 281 | |
| 282 | return R; |
| 283 | } |
| 284 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 285 | FieldRegion* MemRegionManager::getFieldRegion(const FieldDecl* d, |
Ted Kremenek | 993f1c7 | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 286 | const MemRegion* superRegion) { |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 287 | llvm::FoldingSetNodeID ID; |
| 288 | DeclRegion::ProfileRegion(ID, d, superRegion, MemRegion::FieldRegionKind); |
| 289 | |
| 290 | void* InsertPos; |
| 291 | MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos); |
| 292 | FieldRegion* R = cast_or_null<FieldRegion>(data); |
| 293 | |
| 294 | if (!R) { |
| 295 | R = (FieldRegion*) A.Allocate<FieldRegion>(); |
| 296 | new (R) FieldRegion(d, superRegion); |
| 297 | Regions.InsertNode(R, InsertPos); |
| 298 | } |
| 299 | |
| 300 | return R; |
| 301 | } |
| 302 | |
Ted Kremenek | 993f1c7 | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 303 | ObjCIvarRegion* |
| 304 | MemRegionManager::getObjCIvarRegion(const ObjCIvarDecl* d, |
| 305 | const MemRegion* superRegion) { |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 306 | llvm::FoldingSetNodeID ID; |
| 307 | DeclRegion::ProfileRegion(ID, d, superRegion, MemRegion::ObjCIvarRegionKind); |
| 308 | |
| 309 | void* InsertPos; |
| 310 | MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos); |
| 311 | ObjCIvarRegion* R = cast_or_null<ObjCIvarRegion>(data); |
| 312 | |
| 313 | if (!R) { |
Zhongxing Xu | 722c288 | 2008-10-06 03:03:33 +0000 | [diff] [blame] | 314 | R = (ObjCIvarRegion*) A.Allocate<ObjCIvarRegion>(); |
| 315 | new (R) ObjCIvarRegion(d, superRegion); |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 316 | Regions.InsertNode(R, InsertPos); |
| 317 | } |
| 318 | |
| 319 | return R; |
| 320 | } |
| 321 | |
Ted Kremenek | a7f1b9e | 2008-10-24 20:30:08 +0000 | [diff] [blame] | 322 | ObjCObjectRegion* |
| 323 | MemRegionManager::getObjCObjectRegion(const ObjCInterfaceDecl* d, |
| 324 | const MemRegion* superRegion) { |
| 325 | llvm::FoldingSetNodeID ID; |
| 326 | DeclRegion::ProfileRegion(ID, d, superRegion, |
| 327 | MemRegion::ObjCObjectRegionKind); |
| 328 | |
| 329 | void* InsertPos; |
| 330 | MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos); |
| 331 | ObjCObjectRegion* R = cast_or_null<ObjCObjectRegion>(data); |
| 332 | |
| 333 | if (!R) { |
| 334 | R = (ObjCObjectRegion*) A.Allocate<ObjCObjectRegion>(); |
| 335 | new (R) ObjCObjectRegion(d, superRegion); |
| 336 | Regions.InsertNode(R, InsertPos); |
| 337 | } |
| 338 | |
| 339 | return R; |
| 340 | } |
| 341 | |
| 342 | |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 343 | AnonPointeeRegion* MemRegionManager::getAnonPointeeRegion(const VarDecl* d) { |
| 344 | llvm::FoldingSetNodeID ID; |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 345 | MemRegion* superRegion = getUnknownRegion(); |
| 346 | |
Zhongxing Xu | 817c67d | 2008-11-03 04:12:24 +0000 | [diff] [blame^] | 347 | AnonPointeeRegion::ProfileRegion(ID, d, superRegion); |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 348 | |
| 349 | void* InsertPos; |
| 350 | MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos); |
| 351 | AnonPointeeRegion* R = cast_or_null<AnonPointeeRegion>(data); |
| 352 | |
| 353 | if (!R) { |
| 354 | R = (AnonPointeeRegion*) A.Allocate<AnonPointeeRegion>(); |
Zhongxing Xu | 817c67d | 2008-11-03 04:12:24 +0000 | [diff] [blame^] | 355 | new (R) AnonPointeeRegion(d, superRegion); |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 356 | Regions.InsertNode(R, InsertPos); |
| 357 | } |
| 358 | |
| 359 | return R; |
| 360 | } |
| 361 | |
Ted Kremenek | 7090ae1 | 2008-11-02 00:34:33 +0000 | [diff] [blame] | 362 | AllocaRegion* MemRegionManager::getAllocaRegion(const Expr* E, unsigned cnt) { |
| 363 | llvm::FoldingSetNodeID ID; |
| 364 | AllocaRegion::ProfileRegion(ID, E, cnt); |
| 365 | |
| 366 | void* InsertPos; |
| 367 | MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos); |
| 368 | AllocaRegion* R = cast_or_null<AllocaRegion>(data); |
| 369 | |
| 370 | if (!R) { |
| 371 | R = (AllocaRegion*) A.Allocate<AllocaRegion>(); |
| 372 | new (R) AllocaRegion(E, cnt, getStackRegion()); |
| 373 | Regions.InsertNode(R, InsertPos); |
| 374 | } |
| 375 | |
| 376 | return R; |
| 377 | } |
| 378 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 379 | bool MemRegionManager::hasStackStorage(const MemRegion* R) { |
Ted Kremenek | 993f1c7 | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 380 | |
| 381 | // Only subregions can have stack storage. |
Ted Kremenek | 7090ae1 | 2008-11-02 00:34:33 +0000 | [diff] [blame] | 382 | const SubRegion* SR = dyn_cast<SubRegion>(R); |
| 383 | |
Ted Kremenek | 993f1c7 | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 384 | if (!SR) |
| 385 | return false; |
Ted Kremenek | 7090ae1 | 2008-11-02 00:34:33 +0000 | [diff] [blame] | 386 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 387 | MemSpaceRegion* S = getStackRegion(); |
| 388 | |
Ted Kremenek | 993f1c7 | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 389 | while (SR) { |
| 390 | R = SR->getSuperRegion(); |
| 391 | if (R == S) |
| 392 | return true; |
| 393 | |
| 394 | SR = dyn_cast<SubRegion>(R); |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | return false; |
| 398 | } |