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 | |
| 28 | void AnonTypedRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, QualType T, |
| 29 | const MemRegion* superRegion) { |
| 30 | ID.AddInteger((unsigned) AnonTypedRegionKind); |
| 31 | ID.Add(T); |
| 32 | ID.AddPointer(superRegion); |
| 33 | } |
| 34 | |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 35 | void AnonPointeeRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, |
| 36 | const VarDecl* VD, QualType T, |
| 37 | const MemRegion* superRegion) { |
| 38 | ID.AddInteger((unsigned) AnonPointeeRegionKind); |
| 39 | ID.Add(T); |
| 40 | ID.AddPointer(VD); |
| 41 | ID.AddPointer(superRegion); |
| 42 | } |
| 43 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 44 | void AnonTypedRegion::Profile(llvm::FoldingSetNodeID& ID) const { |
| 45 | AnonTypedRegion::ProfileRegion(ID, T, superRegion); |
| 46 | } |
| 47 | |
| 48 | void DeclRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, const Decl* D, |
| 49 | const MemRegion* superRegion, Kind k) { |
| 50 | ID.AddInteger((unsigned) k); |
| 51 | ID.AddPointer(D); |
| 52 | ID.AddPointer(superRegion); |
| 53 | } |
| 54 | |
| 55 | void DeclRegion::Profile(llvm::FoldingSetNodeID& ID) const { |
| 56 | DeclRegion::ProfileRegion(ID, D, superRegion, getKind()); |
| 57 | } |
| 58 | |
Ted Kremenek | 993f1c7 | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 59 | void SymbolicRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, SymbolID sym) { |
| 60 | ID.AddInteger((unsigned) MemRegion::SymbolicRegionKind); |
| 61 | ID.AddInteger(sym.getNumber()); |
| 62 | } |
| 63 | |
| 64 | void SymbolicRegion::Profile(llvm::FoldingSetNodeID& ID) const { |
| 65 | SymbolicRegion::ProfileRegion(ID, sym); |
| 66 | } |
| 67 | |
Zhongxing Xu | 511191c | 2008-10-21 05:27:10 +0000 | [diff] [blame^] | 68 | void ElementRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, SVal Idx, |
| 69 | const MemRegion* superRegion) { |
| 70 | ID.AddInteger(MemRegion::ElementRegionKind); |
| 71 | ID.AddPointer(superRegion); |
| 72 | Idx.Profile(ID); |
| 73 | } |
| 74 | |
| 75 | void ElementRegion::Profile(llvm::FoldingSetNodeID& ID) const { |
| 76 | ElementRegion::ProfileRegion(ID, Index, superRegion); |
| 77 | } |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 78 | //===----------------------------------------------------------------------===// |
| 79 | // Region pretty-printing. |
| 80 | //===----------------------------------------------------------------------===// |
| 81 | |
| 82 | std::string MemRegion::getString() const { |
| 83 | std::string s; |
| 84 | llvm::raw_string_ostream os(s); |
| 85 | print(os); |
| 86 | return os.str(); |
| 87 | } |
| 88 | |
| 89 | void MemRegion::print(llvm::raw_ostream& os) const { |
| 90 | os << "<Unknown Region>"; |
| 91 | } |
| 92 | |
| 93 | void VarRegion::print(llvm::raw_ostream& os) const { |
| 94 | os << cast<VarDecl>(D)->getName(); |
| 95 | } |
| 96 | |
Ted Kremenek | 993f1c7 | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 97 | void SymbolicRegion::print(llvm::raw_ostream& os) const { |
| 98 | os << "$" << sym.getNumber(); |
| 99 | } |
| 100 | |
Ted Kremenek | 4bd1eef | 2008-10-17 21:05:44 +0000 | [diff] [blame] | 101 | void FieldRegion::print(llvm::raw_ostream& os) const { |
| 102 | superRegion->print(os); |
| 103 | os << "->" << getDecl()->getName(); |
| 104 | } |
| 105 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 106 | //===----------------------------------------------------------------------===// |
| 107 | // MemRegionManager methods. |
| 108 | //===----------------------------------------------------------------------===// |
| 109 | |
| 110 | MemSpaceRegion* MemRegionManager::LazyAllocate(MemSpaceRegion*& region) { |
| 111 | |
| 112 | if (!region) { |
| 113 | region = (MemSpaceRegion*) A.Allocate<MemSpaceRegion>(); |
| 114 | new (region) MemSpaceRegion(); |
| 115 | } |
| 116 | |
| 117 | return region; |
| 118 | } |
| 119 | |
| 120 | MemSpaceRegion* MemRegionManager::getStackRegion() { |
| 121 | return LazyAllocate(stack); |
| 122 | } |
| 123 | |
| 124 | MemSpaceRegion* MemRegionManager::getGlobalsRegion() { |
| 125 | return LazyAllocate(globals); |
| 126 | } |
| 127 | |
| 128 | MemSpaceRegion* MemRegionManager::getHeapRegion() { |
| 129 | return LazyAllocate(heap); |
| 130 | } |
| 131 | |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 132 | MemSpaceRegion* MemRegionManager::getUnknownRegion() { |
| 133 | return LazyAllocate(unknown); |
| 134 | } |
| 135 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 136 | VarRegion* MemRegionManager::getVarRegion(const VarDecl* d, |
Ted Kremenek | 993f1c7 | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 137 | const MemRegion* superRegion) { |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 138 | llvm::FoldingSetNodeID ID; |
| 139 | DeclRegion::ProfileRegion(ID, d, superRegion, MemRegion::VarRegionKind); |
| 140 | |
| 141 | void* InsertPos; |
| 142 | MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos); |
| 143 | VarRegion* R = cast_or_null<VarRegion>(data); |
| 144 | |
| 145 | if (!R) { |
| 146 | R = (VarRegion*) A.Allocate<VarRegion>(); |
| 147 | new (R) VarRegion(d, superRegion); |
| 148 | Regions.InsertNode(R, InsertPos); |
| 149 | } |
| 150 | |
| 151 | return R; |
| 152 | } |
| 153 | |
Zhongxing Xu | 511191c | 2008-10-21 05:27:10 +0000 | [diff] [blame^] | 154 | ElementRegion* MemRegionManager::getElementRegion(SVal Idx, |
| 155 | const MemRegion* superRegion){ |
| 156 | llvm::FoldingSetNodeID ID; |
| 157 | ElementRegion::ProfileRegion(ID, Idx, superRegion); |
| 158 | |
| 159 | void* InsertPos; |
| 160 | MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos); |
| 161 | ElementRegion* R = cast_or_null<ElementRegion>(data); |
| 162 | |
| 163 | if (!R) { |
| 164 | R = (ElementRegion*) A.Allocate<ElementRegion>(); |
| 165 | new (R) ElementRegion(Idx, superRegion); |
| 166 | Regions.InsertNode(R, InsertPos); |
| 167 | } |
| 168 | |
| 169 | return R; |
| 170 | } |
| 171 | |
Ted Kremenek | 993f1c7 | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 172 | /// getSymbolicRegion - Retrieve or create a "symbolic" memory region. |
| 173 | SymbolicRegion* MemRegionManager::getSymbolicRegion(const SymbolID sym) { |
| 174 | |
| 175 | llvm::FoldingSetNodeID ID; |
| 176 | SymbolicRegion::ProfileRegion(ID, sym); |
| 177 | |
| 178 | void* InsertPos; |
| 179 | MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos); |
| 180 | SymbolicRegion* R = cast_or_null<SymbolicRegion>(data); |
| 181 | |
| 182 | if (!R) { |
| 183 | R = (SymbolicRegion*) A.Allocate<SymbolicRegion>(); |
| 184 | new (R) SymbolicRegion(sym); |
| 185 | Regions.InsertNode(R, InsertPos); |
| 186 | } |
| 187 | |
| 188 | return R; |
| 189 | } |
| 190 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 191 | FieldRegion* MemRegionManager::getFieldRegion(const FieldDecl* d, |
Ted Kremenek | 993f1c7 | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 192 | const MemRegion* superRegion) { |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 193 | llvm::FoldingSetNodeID ID; |
| 194 | DeclRegion::ProfileRegion(ID, d, superRegion, MemRegion::FieldRegionKind); |
| 195 | |
| 196 | void* InsertPos; |
| 197 | MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos); |
| 198 | FieldRegion* R = cast_or_null<FieldRegion>(data); |
| 199 | |
| 200 | if (!R) { |
| 201 | R = (FieldRegion*) A.Allocate<FieldRegion>(); |
| 202 | new (R) FieldRegion(d, superRegion); |
| 203 | Regions.InsertNode(R, InsertPos); |
| 204 | } |
| 205 | |
| 206 | return R; |
| 207 | } |
| 208 | |
Ted Kremenek | 993f1c7 | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 209 | ObjCIvarRegion* |
| 210 | MemRegionManager::getObjCIvarRegion(const ObjCIvarDecl* d, |
| 211 | const MemRegion* superRegion) { |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 212 | llvm::FoldingSetNodeID ID; |
| 213 | DeclRegion::ProfileRegion(ID, d, superRegion, MemRegion::ObjCIvarRegionKind); |
| 214 | |
| 215 | void* InsertPos; |
| 216 | MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos); |
| 217 | ObjCIvarRegion* R = cast_or_null<ObjCIvarRegion>(data); |
| 218 | |
| 219 | if (!R) { |
Zhongxing Xu | 722c288 | 2008-10-06 03:03:33 +0000 | [diff] [blame] | 220 | R = (ObjCIvarRegion*) A.Allocate<ObjCIvarRegion>(); |
| 221 | new (R) ObjCIvarRegion(d, superRegion); |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 222 | Regions.InsertNode(R, InsertPos); |
| 223 | } |
| 224 | |
| 225 | return R; |
| 226 | } |
| 227 | |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 228 | AnonPointeeRegion* MemRegionManager::getAnonPointeeRegion(const VarDecl* d) { |
| 229 | llvm::FoldingSetNodeID ID; |
| 230 | QualType T = d->getType(); |
| 231 | QualType PointeeType = cast<PointerType>(T.getTypePtr())->getPointeeType(); |
| 232 | MemRegion* superRegion = getUnknownRegion(); |
| 233 | |
| 234 | AnonPointeeRegion::ProfileRegion(ID, d, PointeeType, superRegion); |
| 235 | |
| 236 | void* InsertPos; |
| 237 | MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos); |
| 238 | AnonPointeeRegion* R = cast_or_null<AnonPointeeRegion>(data); |
| 239 | |
| 240 | if (!R) { |
| 241 | R = (AnonPointeeRegion*) A.Allocate<AnonPointeeRegion>(); |
| 242 | new (R) AnonPointeeRegion(d, PointeeType, superRegion); |
| 243 | Regions.InsertNode(R, InsertPos); |
| 244 | } |
| 245 | |
| 246 | return R; |
| 247 | } |
| 248 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 249 | bool MemRegionManager::hasStackStorage(const MemRegion* R) { |
Ted Kremenek | 993f1c7 | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 250 | const SubRegion* SR = dyn_cast<SubRegion>(R); |
| 251 | |
| 252 | // Only subregions can have stack storage. |
| 253 | if (!SR) |
| 254 | return false; |
| 255 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 256 | MemSpaceRegion* S = getStackRegion(); |
| 257 | |
Ted Kremenek | 993f1c7 | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 258 | while (SR) { |
| 259 | R = SR->getSuperRegion(); |
| 260 | if (R == S) |
| 261 | return true; |
| 262 | |
| 263 | SR = dyn_cast<SubRegion>(R); |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | return false; |
| 267 | } |