blob: 82f442354192db33000a817906c3acaa2bd7b6e3 [file] [log] [blame]
Ted Kremenekb15eba42008-10-04 05:50:14 +00001//== 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
19using namespace clang;
20
21
22MemRegion::~MemRegion() {}
23
Zhongxing Xub287b212009-01-08 13:17:14 +000024bool SubRegion::isSubRegionOf(const MemRegion* R) const {
25 const MemRegion* r = getSuperRegion();
26 while (r != 0) {
27 if (r == R)
28 return true;
29 if (const SubRegion* sr = dyn_cast<SubRegion>(r))
30 r = sr->getSuperRegion();
31 else
32 break;
33 }
34 return false;
35}
36
Ted Kremenekb15eba42008-10-04 05:50:14 +000037void MemSpaceRegion::Profile(llvm::FoldingSetNodeID& ID) const {
38 ID.AddInteger((unsigned)getKind());
39}
40
Zhongxing Xu73507bd2008-10-25 14:13:41 +000041void StringRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
42 const StringLiteral* Str,
43 const MemRegion* superRegion) {
44 ID.AddInteger((unsigned) StringRegionKind);
45 ID.AddPointer(Str);
46 ID.AddPointer(superRegion);
47}
48
Ted Kremenekea9ca502008-11-02 00:34:33 +000049void AllocaRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
50 const Expr* Ex, unsigned cnt) {
51 ID.AddInteger((unsigned) AllocaRegionKind);
52 ID.AddPointer(Ex);
53 ID.AddInteger(cnt);
54}
55
56void AllocaRegion::Profile(llvm::FoldingSetNodeID& ID) const {
57 ProfileRegion(ID, Ex, Cnt);
58}
59
Zhongxing Xu8fbe7ae2008-11-16 04:07:26 +000060void AnonTypedRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, QualType T,
61 const MemRegion* superRegion) {
62 ID.AddInteger((unsigned) AnonTypedRegionKind);
63 ID.Add(T);
64 ID.AddPointer(superRegion);
65}
66
Ted Kremenek6bc91b92008-10-27 20:57:58 +000067void CompoundLiteralRegion::Profile(llvm::FoldingSetNodeID& ID) const {
68 CompoundLiteralRegion::ProfileRegion(ID, CL, superRegion);
69}
70
71void CompoundLiteralRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
72 const CompoundLiteralExpr* CL,
73 const MemRegion* superRegion) {
74 ID.AddInteger((unsigned) CompoundLiteralRegionKind);
75 ID.AddPointer(CL);
76 ID.AddPointer(superRegion);
77}
78
Ted Kremenekb15eba42008-10-04 05:50:14 +000079void DeclRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, const Decl* D,
80 const MemRegion* superRegion, Kind k) {
81 ID.AddInteger((unsigned) k);
82 ID.AddPointer(D);
83 ID.AddPointer(superRegion);
84}
85
86void DeclRegion::Profile(llvm::FoldingSetNodeID& ID) const {
87 DeclRegion::ProfileRegion(ID, D, superRegion, getKind());
88}
89
Ted Kremenekb9cd9a72008-12-05 02:27:51 +000090void SymbolicRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, SymbolRef sym) {
Ted Kremenek38a4b4b2008-10-17 20:28:54 +000091 ID.AddInteger((unsigned) MemRegion::SymbolicRegionKind);
Ted Kremeneke8993f12008-12-05 02:39:38 +000092 ID.Add(sym);
Ted Kremenek38a4b4b2008-10-17 20:28:54 +000093}
94
95void SymbolicRegion::Profile(llvm::FoldingSetNodeID& ID) const {
96 SymbolicRegion::ProfileRegion(ID, sym);
97}
98
Zhongxing Xu54969732008-10-21 05:27:10 +000099void ElementRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, SVal Idx,
100 const MemRegion* superRegion) {
101 ID.AddInteger(MemRegion::ElementRegionKind);
102 ID.AddPointer(superRegion);
103 Idx.Profile(ID);
104}
105
106void ElementRegion::Profile(llvm::FoldingSetNodeID& ID) const {
107 ElementRegion::ProfileRegion(ID, Index, superRegion);
108}
Zhongxing Xu1a563da2008-10-27 13:17:02 +0000109
Ted Kremenekf5da3252008-12-13 21:49:13 +0000110QualType ElementRegion::getRValueType(ASTContext& C) const {
Ted Kremenek83146912009-01-24 06:11:36 +0000111 // Strip off typedefs from the ArrayRegion's RvalueType.
112 QualType T = getArrayRegion()->getRValueType(C)->getDesugaredType();
Zhongxing Xu02357a42009-01-23 10:19:29 +0000113
Ted Kremenek83146912009-01-24 06:11:36 +0000114 if (ArrayType* AT = dyn_cast<ArrayType>(T.getTypePtr()))
Zhongxing Xu9f801532008-11-13 07:30:58 +0000115 return AT->getElementType();
Ted Kremenek83146912009-01-24 06:11:36 +0000116
Ted Kremeneka25cb052009-01-30 00:08:43 +0000117 // If the RValueType of the array region isn't an ArrayType, then essentially
118 // the element's
119 return T;
Zhongxing Xu1a563da2008-10-27 13:17:02 +0000120}
121
Ted Kremenekb15eba42008-10-04 05:50:14 +0000122//===----------------------------------------------------------------------===//
Ted Kremenekf5da3252008-12-13 21:49:13 +0000123// getLValueType() and getRValueType()
124//===----------------------------------------------------------------------===//
125
126QualType StringRegion::getRValueType(ASTContext& C) const {
127 return Str->getType();
128}
129
130//===----------------------------------------------------------------------===//
Ted Kremenekb15eba42008-10-04 05:50:14 +0000131// Region pretty-printing.
132//===----------------------------------------------------------------------===//
133
134std::string MemRegion::getString() const {
135 std::string s;
136 llvm::raw_string_ostream os(s);
137 print(os);
138 return os.str();
139}
140
141void MemRegion::print(llvm::raw_ostream& os) const {
142 os << "<Unknown Region>";
143}
144
Ted Kremenekea9ca502008-11-02 00:34:33 +0000145void AllocaRegion::print(llvm::raw_ostream& os) const {
146 os << "alloca{" << (void*) Ex << ',' << Cnt << '}';
147}
148
Ted Kremenek542c34d2008-12-17 19:25:50 +0000149void AnonTypedRegion::print(llvm::raw_ostream& os) const {
150 os << "anon_type{" << T.getAsString() << ',';
151 getSuperRegion()->print(os);
152 os << '}';
153}
154
Ted Kremenekb15eba42008-10-04 05:50:14 +0000155void VarRegion::print(llvm::raw_ostream& os) const {
Chris Lattner271d4c22008-11-24 05:29:24 +0000156 os << cast<VarDecl>(D)->getNameAsString();
Ted Kremenekb15eba42008-10-04 05:50:14 +0000157}
158
Ted Kremenek38a4b4b2008-10-17 20:28:54 +0000159void SymbolicRegion::print(llvm::raw_ostream& os) const {
Ted Kremenek8b60bad2008-12-05 02:45:20 +0000160 os << "SymRegion-";
161 sym.print(os);
Ted Kremenek38a4b4b2008-10-17 20:28:54 +0000162}
163
Ted Kremenekdea0c6b2008-10-17 21:05:44 +0000164void FieldRegion::print(llvm::raw_ostream& os) const {
165 superRegion->print(os);
Chris Lattner271d4c22008-11-24 05:29:24 +0000166 os << "->" << getDecl()->getNameAsString();
Ted Kremenekdea0c6b2008-10-17 21:05:44 +0000167}
168
Zhongxing Xu74a4e3a2008-10-24 06:30:07 +0000169void ElementRegion::print(llvm::raw_ostream& os) const {
170 superRegion->print(os);
171 os << '['; Index.print(os); os << ']';
172}
173
Ted Kremenek6bc91b92008-10-27 20:57:58 +0000174void CompoundLiteralRegion::print(llvm::raw_ostream& os) const {
175 // FIXME: More elaborate pretty-printing.
176 os << "{ " << (void*) CL << " }";
177}
178
Zhongxing Xu4c9390f2008-11-10 13:05:26 +0000179void StringRegion::print(llvm::raw_ostream& os) const {
Ted Kremenek1d536472009-01-16 19:26:50 +0000180 Str->printPretty(os);
Zhongxing Xu4c9390f2008-11-10 13:05:26 +0000181}
182
Ted Kremenekb15eba42008-10-04 05:50:14 +0000183//===----------------------------------------------------------------------===//
184// MemRegionManager methods.
185//===----------------------------------------------------------------------===//
186
187MemSpaceRegion* MemRegionManager::LazyAllocate(MemSpaceRegion*& region) {
188
189 if (!region) {
190 region = (MemSpaceRegion*) A.Allocate<MemSpaceRegion>();
191 new (region) MemSpaceRegion();
192 }
193
194 return region;
195}
196
197MemSpaceRegion* MemRegionManager::getStackRegion() {
198 return LazyAllocate(stack);
199}
200
201MemSpaceRegion* MemRegionManager::getGlobalsRegion() {
202 return LazyAllocate(globals);
203}
204
205MemSpaceRegion* MemRegionManager::getHeapRegion() {
206 return LazyAllocate(heap);
207}
208
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000209MemSpaceRegion* MemRegionManager::getUnknownRegion() {
210 return LazyAllocate(unknown);
211}
212
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000213bool MemRegionManager::onStack(const MemRegion* R) {
214 while (const SubRegion* SR = dyn_cast<SubRegion>(R))
215 R = SR->getSuperRegion();
216
217 return (R != 0) && (R == stack);
218}
219
220bool MemRegionManager::onHeap(const MemRegion* R) {
221 while (const SubRegion* SR = dyn_cast<SubRegion>(R))
222 R = SR->getSuperRegion();
223
224 return (R != 0) && (R == heap);
225}
226
Zhongxing Xu73507bd2008-10-25 14:13:41 +0000227StringRegion* MemRegionManager::getStringRegion(const StringLiteral* Str) {
228 llvm::FoldingSetNodeID ID;
229 MemSpaceRegion* GlobalsR = getGlobalsRegion();
230
231 StringRegion::ProfileRegion(ID, Str, GlobalsR);
232
233 void* InsertPos;
234 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
235 StringRegion* R = cast_or_null<StringRegion>(data);
236
237 if (!R) {
238 R = (StringRegion*) A.Allocate<StringRegion>();
239 new (R) StringRegion(Str, GlobalsR);
240 Regions.InsertNode(R, InsertPos);
241 }
242
243 return R;
244}
245
Ted Kremenek81329ab2008-10-27 21:01:26 +0000246VarRegion* MemRegionManager::getVarRegion(const VarDecl* d) {
247
248 const MemRegion* superRegion = d->hasLocalStorage() ? getStackRegion()
249 : getGlobalsRegion();
250
Ted Kremenekb15eba42008-10-04 05:50:14 +0000251 llvm::FoldingSetNodeID ID;
252 DeclRegion::ProfileRegion(ID, d, superRegion, MemRegion::VarRegionKind);
253
254 void* InsertPos;
255 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
256 VarRegion* R = cast_or_null<VarRegion>(data);
257
258 if (!R) {
259 R = (VarRegion*) A.Allocate<VarRegion>();
260 new (R) VarRegion(d, superRegion);
261 Regions.InsertNode(R, InsertPos);
262 }
263
264 return R;
265}
266
Ted Kremenek6bc91b92008-10-27 20:57:58 +0000267CompoundLiteralRegion*
268MemRegionManager::getCompoundLiteralRegion(const CompoundLiteralExpr* CL) {
269 // Is this compound literal allocated on the stack or is part of the
270 // global constant pool?
271 const MemRegion* superRegion = CL->isFileScope() ?
272 getGlobalsRegion() : getStackRegion();
273
274 // Profile the compound literal.
275 llvm::FoldingSetNodeID ID;
276 CompoundLiteralRegion::ProfileRegion(ID, CL, superRegion);
277
278 void* InsertPos;
279 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
280 CompoundLiteralRegion* R = cast_or_null<CompoundLiteralRegion>(data);
281
282 if (!R) {
283 R = (CompoundLiteralRegion*) A.Allocate<CompoundLiteralRegion>();
284 new (R) CompoundLiteralRegion(CL, superRegion);
285 Regions.InsertNode(R, InsertPos);
286 }
287
288 return R;
289}
290
Ted Kremenek2c0de352008-12-13 19:24:37 +0000291ElementRegion*
292MemRegionManager::getElementRegion(SVal Idx, const TypedRegion* superRegion){
293
Zhongxing Xu54969732008-10-21 05:27:10 +0000294 llvm::FoldingSetNodeID ID;
295 ElementRegion::ProfileRegion(ID, Idx, superRegion);
296
297 void* InsertPos;
298 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
299 ElementRegion* R = cast_or_null<ElementRegion>(data);
300
301 if (!R) {
302 R = (ElementRegion*) A.Allocate<ElementRegion>();
303 new (R) ElementRegion(Idx, superRegion);
304 Regions.InsertNode(R, InsertPos);
305 }
306
307 return R;
308}
309
Ted Kremenek38a4b4b2008-10-17 20:28:54 +0000310/// getSymbolicRegion - Retrieve or create a "symbolic" memory region.
Ted Kremenekb9cd9a72008-12-05 02:27:51 +0000311SymbolicRegion* MemRegionManager::getSymbolicRegion(const SymbolRef sym) {
Ted Kremenek38a4b4b2008-10-17 20:28:54 +0000312
313 llvm::FoldingSetNodeID ID;
314 SymbolicRegion::ProfileRegion(ID, sym);
315
316 void* InsertPos;
317 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
318 SymbolicRegion* R = cast_or_null<SymbolicRegion>(data);
319
320 if (!R) {
321 R = (SymbolicRegion*) A.Allocate<SymbolicRegion>();
322 new (R) SymbolicRegion(sym);
323 Regions.InsertNode(R, InsertPos);
324 }
325
326 return R;
327}
328
Ted Kremenekb15eba42008-10-04 05:50:14 +0000329FieldRegion* MemRegionManager::getFieldRegion(const FieldDecl* d,
Ted Kremenek38a4b4b2008-10-17 20:28:54 +0000330 const MemRegion* superRegion) {
Ted Kremenekb15eba42008-10-04 05:50:14 +0000331 llvm::FoldingSetNodeID ID;
332 DeclRegion::ProfileRegion(ID, d, superRegion, MemRegion::FieldRegionKind);
333
334 void* InsertPos;
335 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
336 FieldRegion* R = cast_or_null<FieldRegion>(data);
337
338 if (!R) {
339 R = (FieldRegion*) A.Allocate<FieldRegion>();
340 new (R) FieldRegion(d, superRegion);
341 Regions.InsertNode(R, InsertPos);
342 }
343
344 return R;
345}
346
Ted Kremenek38a4b4b2008-10-17 20:28:54 +0000347ObjCIvarRegion*
348MemRegionManager::getObjCIvarRegion(const ObjCIvarDecl* d,
349 const MemRegion* superRegion) {
Ted Kremenekb15eba42008-10-04 05:50:14 +0000350 llvm::FoldingSetNodeID ID;
351 DeclRegion::ProfileRegion(ID, d, superRegion, MemRegion::ObjCIvarRegionKind);
352
353 void* InsertPos;
354 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
355 ObjCIvarRegion* R = cast_or_null<ObjCIvarRegion>(data);
356
357 if (!R) {
Zhongxing Xu7a861e82008-10-06 03:03:33 +0000358 R = (ObjCIvarRegion*) A.Allocate<ObjCIvarRegion>();
359 new (R) ObjCIvarRegion(d, superRegion);
Ted Kremenekb15eba42008-10-04 05:50:14 +0000360 Regions.InsertNode(R, InsertPos);
361 }
362
363 return R;
364}
365
Ted Kremenek2539c112008-10-24 20:30:08 +0000366ObjCObjectRegion*
367MemRegionManager::getObjCObjectRegion(const ObjCInterfaceDecl* d,
368 const MemRegion* superRegion) {
369 llvm::FoldingSetNodeID ID;
370 DeclRegion::ProfileRegion(ID, d, superRegion,
371 MemRegion::ObjCObjectRegionKind);
372
373 void* InsertPos;
374 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
375 ObjCObjectRegion* R = cast_or_null<ObjCObjectRegion>(data);
376
377 if (!R) {
378 R = (ObjCObjectRegion*) A.Allocate<ObjCObjectRegion>();
379 new (R) ObjCObjectRegion(d, superRegion);
380 Regions.InsertNode(R, InsertPos);
381 }
382
383 return R;
384}
385
Zhongxing Xu8fbe7ae2008-11-16 04:07:26 +0000386AnonTypedRegion*
387MemRegionManager::getAnonTypedRegion(QualType t, const MemRegion* superRegion) {
388 llvm::FoldingSetNodeID ID;
389 AnonTypedRegion::ProfileRegion(ID, t, superRegion);
390
391 void* InsertPos;
392 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
393 AnonTypedRegion* R = cast_or_null<AnonTypedRegion>(data);
394
395 if (!R) {
396 R = (AnonTypedRegion*) A.Allocate<AnonTypedRegion>();
397 new (R) AnonTypedRegion(t, superRegion);
398 Regions.InsertNode(R, InsertPos);
399 }
400
401 return R;
402}
Ted Kremenek2539c112008-10-24 20:30:08 +0000403
Ted Kremenekea9ca502008-11-02 00:34:33 +0000404AllocaRegion* MemRegionManager::getAllocaRegion(const Expr* E, unsigned cnt) {
405 llvm::FoldingSetNodeID ID;
406 AllocaRegion::ProfileRegion(ID, E, cnt);
407
408 void* InsertPos;
409 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
410 AllocaRegion* R = cast_or_null<AllocaRegion>(data);
411
412 if (!R) {
413 R = (AllocaRegion*) A.Allocate<AllocaRegion>();
414 new (R) AllocaRegion(E, cnt, getStackRegion());
415 Regions.InsertNode(R, InsertPos);
416 }
417
418 return R;
419}
420
Ted Kremenekb15eba42008-10-04 05:50:14 +0000421bool MemRegionManager::hasStackStorage(const MemRegion* R) {
Ted Kremenek38a4b4b2008-10-17 20:28:54 +0000422
423 // Only subregions can have stack storage.
Ted Kremenekea9ca502008-11-02 00:34:33 +0000424 const SubRegion* SR = dyn_cast<SubRegion>(R);
425
Ted Kremenek38a4b4b2008-10-17 20:28:54 +0000426 if (!SR)
427 return false;
Ted Kremenekea9ca502008-11-02 00:34:33 +0000428
Ted Kremenekb15eba42008-10-04 05:50:14 +0000429 MemSpaceRegion* S = getStackRegion();
430
Ted Kremenek38a4b4b2008-10-17 20:28:54 +0000431 while (SR) {
432 R = SR->getSuperRegion();
433 if (R == S)
434 return true;
435
436 SR = dyn_cast<SubRegion>(R);
Ted Kremenekb15eba42008-10-04 05:50:14 +0000437 }
438
439 return false;
440}