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 | |
Zhongxing Xu | b21ff77 | 2008-10-24 06:30:07 +0000 | [diff] [blame] | 106 | void ElementRegion::print(llvm::raw_ostream& os) const { |
| 107 | superRegion->print(os); |
| 108 | os << '['; Index.print(os); os << ']'; |
| 109 | } |
| 110 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 111 | //===----------------------------------------------------------------------===// |
| 112 | // MemRegionManager methods. |
| 113 | //===----------------------------------------------------------------------===// |
| 114 | |
| 115 | MemSpaceRegion* MemRegionManager::LazyAllocate(MemSpaceRegion*& region) { |
| 116 | |
| 117 | if (!region) { |
| 118 | region = (MemSpaceRegion*) A.Allocate<MemSpaceRegion>(); |
| 119 | new (region) MemSpaceRegion(); |
| 120 | } |
| 121 | |
| 122 | return region; |
| 123 | } |
| 124 | |
| 125 | MemSpaceRegion* MemRegionManager::getStackRegion() { |
| 126 | return LazyAllocate(stack); |
| 127 | } |
| 128 | |
| 129 | MemSpaceRegion* MemRegionManager::getGlobalsRegion() { |
| 130 | return LazyAllocate(globals); |
| 131 | } |
| 132 | |
| 133 | MemSpaceRegion* MemRegionManager::getHeapRegion() { |
| 134 | return LazyAllocate(heap); |
| 135 | } |
| 136 | |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 137 | MemSpaceRegion* MemRegionManager::getUnknownRegion() { |
| 138 | return LazyAllocate(unknown); |
| 139 | } |
| 140 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 141 | VarRegion* MemRegionManager::getVarRegion(const VarDecl* d, |
Ted Kremenek | 993f1c7 | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 142 | const MemRegion* superRegion) { |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 143 | llvm::FoldingSetNodeID ID; |
| 144 | DeclRegion::ProfileRegion(ID, d, superRegion, MemRegion::VarRegionKind); |
| 145 | |
| 146 | void* InsertPos; |
| 147 | MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos); |
| 148 | VarRegion* R = cast_or_null<VarRegion>(data); |
| 149 | |
| 150 | if (!R) { |
| 151 | R = (VarRegion*) A.Allocate<VarRegion>(); |
| 152 | new (R) VarRegion(d, superRegion); |
| 153 | Regions.InsertNode(R, InsertPos); |
| 154 | } |
| 155 | |
| 156 | return R; |
| 157 | } |
| 158 | |
Zhongxing Xu | 511191c | 2008-10-21 05:27:10 +0000 | [diff] [blame] | 159 | ElementRegion* MemRegionManager::getElementRegion(SVal Idx, |
| 160 | const MemRegion* superRegion){ |
| 161 | llvm::FoldingSetNodeID ID; |
| 162 | ElementRegion::ProfileRegion(ID, Idx, superRegion); |
| 163 | |
| 164 | void* InsertPos; |
| 165 | MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos); |
| 166 | ElementRegion* R = cast_or_null<ElementRegion>(data); |
| 167 | |
| 168 | if (!R) { |
| 169 | R = (ElementRegion*) A.Allocate<ElementRegion>(); |
| 170 | new (R) ElementRegion(Idx, superRegion); |
| 171 | Regions.InsertNode(R, InsertPos); |
| 172 | } |
| 173 | |
| 174 | return R; |
| 175 | } |
| 176 | |
Ted Kremenek | 993f1c7 | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 177 | /// getSymbolicRegion - Retrieve or create a "symbolic" memory region. |
| 178 | SymbolicRegion* MemRegionManager::getSymbolicRegion(const SymbolID sym) { |
| 179 | |
| 180 | llvm::FoldingSetNodeID ID; |
| 181 | SymbolicRegion::ProfileRegion(ID, sym); |
| 182 | |
| 183 | void* InsertPos; |
| 184 | MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos); |
| 185 | SymbolicRegion* R = cast_or_null<SymbolicRegion>(data); |
| 186 | |
| 187 | if (!R) { |
| 188 | R = (SymbolicRegion*) A.Allocate<SymbolicRegion>(); |
| 189 | new (R) SymbolicRegion(sym); |
| 190 | Regions.InsertNode(R, InsertPos); |
| 191 | } |
| 192 | |
| 193 | return R; |
| 194 | } |
| 195 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 196 | FieldRegion* MemRegionManager::getFieldRegion(const FieldDecl* d, |
Ted Kremenek | 993f1c7 | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 197 | const MemRegion* superRegion) { |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 198 | llvm::FoldingSetNodeID ID; |
| 199 | DeclRegion::ProfileRegion(ID, d, superRegion, MemRegion::FieldRegionKind); |
| 200 | |
| 201 | void* InsertPos; |
| 202 | MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos); |
| 203 | FieldRegion* R = cast_or_null<FieldRegion>(data); |
| 204 | |
| 205 | if (!R) { |
| 206 | R = (FieldRegion*) A.Allocate<FieldRegion>(); |
| 207 | new (R) FieldRegion(d, superRegion); |
| 208 | Regions.InsertNode(R, InsertPos); |
| 209 | } |
| 210 | |
| 211 | return R; |
| 212 | } |
| 213 | |
Ted Kremenek | 993f1c7 | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 214 | ObjCIvarRegion* |
| 215 | MemRegionManager::getObjCIvarRegion(const ObjCIvarDecl* d, |
| 216 | const MemRegion* superRegion) { |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 217 | llvm::FoldingSetNodeID ID; |
| 218 | DeclRegion::ProfileRegion(ID, d, superRegion, MemRegion::ObjCIvarRegionKind); |
| 219 | |
| 220 | void* InsertPos; |
| 221 | MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos); |
| 222 | ObjCIvarRegion* R = cast_or_null<ObjCIvarRegion>(data); |
| 223 | |
| 224 | if (!R) { |
Zhongxing Xu | 722c288 | 2008-10-06 03:03:33 +0000 | [diff] [blame] | 225 | R = (ObjCIvarRegion*) A.Allocate<ObjCIvarRegion>(); |
| 226 | new (R) ObjCIvarRegion(d, superRegion); |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 227 | Regions.InsertNode(R, InsertPos); |
| 228 | } |
| 229 | |
| 230 | return R; |
| 231 | } |
| 232 | |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 233 | AnonPointeeRegion* MemRegionManager::getAnonPointeeRegion(const VarDecl* d) { |
| 234 | llvm::FoldingSetNodeID ID; |
| 235 | QualType T = d->getType(); |
| 236 | QualType PointeeType = cast<PointerType>(T.getTypePtr())->getPointeeType(); |
| 237 | MemRegion* superRegion = getUnknownRegion(); |
| 238 | |
| 239 | AnonPointeeRegion::ProfileRegion(ID, d, PointeeType, superRegion); |
| 240 | |
| 241 | void* InsertPos; |
| 242 | MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos); |
| 243 | AnonPointeeRegion* R = cast_or_null<AnonPointeeRegion>(data); |
| 244 | |
| 245 | if (!R) { |
| 246 | R = (AnonPointeeRegion*) A.Allocate<AnonPointeeRegion>(); |
| 247 | new (R) AnonPointeeRegion(d, PointeeType, superRegion); |
| 248 | Regions.InsertNode(R, InsertPos); |
| 249 | } |
| 250 | |
| 251 | return R; |
| 252 | } |
| 253 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 254 | bool MemRegionManager::hasStackStorage(const MemRegion* R) { |
Ted Kremenek | 993f1c7 | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 255 | const SubRegion* SR = dyn_cast<SubRegion>(R); |
| 256 | |
| 257 | // Only subregions can have stack storage. |
| 258 | if (!SR) |
| 259 | return false; |
| 260 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 261 | MemSpaceRegion* S = getStackRegion(); |
| 262 | |
Ted Kremenek | 993f1c7 | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 263 | while (SR) { |
| 264 | R = SR->getSuperRegion(); |
| 265 | if (R == S) |
| 266 | return true; |
| 267 | |
| 268 | SR = dyn_cast<SubRegion>(R); |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | return false; |
| 272 | } |