blob: 8dd31b54b0baf8dd78684fcfeda9f9483319b352 [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
Ted Kremenek6a9b5352009-03-01 05:44:08 +000060void TypedViewRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, QualType T,
Zhongxing Xu8fbe7ae2008-11-16 04:07:26 +000061 const MemRegion* superRegion) {
Ted Kremenek6a9b5352009-03-01 05:44:08 +000062 ID.AddInteger((unsigned) TypedViewRegionKind);
Zhongxing Xu8fbe7ae2008-11-16 04:07:26 +000063 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
Zhongxing Xu93b80662009-02-05 06:57:29 +0000110//===----------------------------------------------------------------------===//
111// getLValueType() and getRValueType()
112//===----------------------------------------------------------------------===//
113
114QualType SymbolicRegion::getRValueType(ASTContext& C) const {
115 const SymbolData& data = SymMgr.getSymbolData(sym);
116
Zhongxing Xuecd3bb42009-02-06 08:44:27 +0000117 // Get the type of the symbol.
118 QualType T = data.getType(C);
Zhongxing Xu93b80662009-02-05 06:57:29 +0000119
Ted Kremeneke74bf072009-03-04 02:43:08 +0000120 if (const PointerType* PTy = T->getAsPointerType())
121 return PTy->getPointeeType();
122
123 if (const BlockPointerType* PTy = T->getAsBlockPointerType())
124 return PTy->getPointeeType();
Zhongxing Xu93b80662009-02-05 06:57:29 +0000125
Ted Kremeneke74bf072009-03-04 02:43:08 +0000126 assert(!T->getAsObjCQualifiedIdType() &&
127 "There is no rvalue type for id<...>");
128
129 assert(Loc::IsLocType(T) && "Non-location type.");
130 return QualType();
131}
132
133QualType SymbolicRegion::getLValueType(ASTContext& C) const {
134 const SymbolData& data = SymMgr.getSymbolData(sym);
135 return data.getType(C);
Zhongxing Xu93b80662009-02-05 06:57:29 +0000136}
137
Ted Kremenekf5da3252008-12-13 21:49:13 +0000138QualType ElementRegion::getRValueType(ASTContext& C) const {
Ted Kremenek83146912009-01-24 06:11:36 +0000139 // Strip off typedefs from the ArrayRegion's RvalueType.
140 QualType T = getArrayRegion()->getRValueType(C)->getDesugaredType();
Zhongxing Xu02357a42009-01-23 10:19:29 +0000141
Ted Kremenek83146912009-01-24 06:11:36 +0000142 if (ArrayType* AT = dyn_cast<ArrayType>(T.getTypePtr()))
Zhongxing Xu9f801532008-11-13 07:30:58 +0000143 return AT->getElementType();
Ted Kremenek83146912009-01-24 06:11:36 +0000144
Ted Kremeneka25cb052009-01-30 00:08:43 +0000145 // If the RValueType of the array region isn't an ArrayType, then essentially
146 // the element's
147 return T;
Zhongxing Xu1a563da2008-10-27 13:17:02 +0000148}
149
Ted Kremenekf5da3252008-12-13 21:49:13 +0000150QualType StringRegion::getRValueType(ASTContext& C) const {
151 return Str->getType();
152}
153
154//===----------------------------------------------------------------------===//
Ted Kremenekb15eba42008-10-04 05:50:14 +0000155// Region pretty-printing.
156//===----------------------------------------------------------------------===//
157
158std::string MemRegion::getString() const {
159 std::string s;
160 llvm::raw_string_ostream os(s);
161 print(os);
162 return os.str();
163}
164
165void MemRegion::print(llvm::raw_ostream& os) const {
166 os << "<Unknown Region>";
167}
168
Ted Kremenekea9ca502008-11-02 00:34:33 +0000169void AllocaRegion::print(llvm::raw_ostream& os) const {
170 os << "alloca{" << (void*) Ex << ',' << Cnt << '}';
171}
172
Ted Kremenek6a9b5352009-03-01 05:44:08 +0000173void TypedViewRegion::print(llvm::raw_ostream& os) const {
Ted Kremeneke74bf072009-03-04 02:43:08 +0000174 os << "typed_view{" << T.getAsString() << ',';
Ted Kremenek542c34d2008-12-17 19:25:50 +0000175 getSuperRegion()->print(os);
176 os << '}';
177}
178
Ted Kremenekb15eba42008-10-04 05:50:14 +0000179void VarRegion::print(llvm::raw_ostream& os) const {
Chris Lattner271d4c22008-11-24 05:29:24 +0000180 os << cast<VarDecl>(D)->getNameAsString();
Ted Kremenekb15eba42008-10-04 05:50:14 +0000181}
182
Ted Kremenek38a4b4b2008-10-17 20:28:54 +0000183void SymbolicRegion::print(llvm::raw_ostream& os) const {
Ted Kremenek9577c1e2009-03-03 22:06:47 +0000184 os << "SymRegion-" << sym;
Ted Kremenek38a4b4b2008-10-17 20:28:54 +0000185}
186
Ted Kremenekdea0c6b2008-10-17 21:05:44 +0000187void FieldRegion::print(llvm::raw_ostream& os) const {
188 superRegion->print(os);
Chris Lattner271d4c22008-11-24 05:29:24 +0000189 os << "->" << getDecl()->getNameAsString();
Ted Kremenekdea0c6b2008-10-17 21:05:44 +0000190}
191
Zhongxing Xu74a4e3a2008-10-24 06:30:07 +0000192void ElementRegion::print(llvm::raw_ostream& os) const {
193 superRegion->print(os);
194 os << '['; Index.print(os); os << ']';
195}
196
Ted Kremenek6bc91b92008-10-27 20:57:58 +0000197void CompoundLiteralRegion::print(llvm::raw_ostream& os) const {
198 // FIXME: More elaborate pretty-printing.
199 os << "{ " << (void*) CL << " }";
200}
201
Zhongxing Xu4c9390f2008-11-10 13:05:26 +0000202void StringRegion::print(llvm::raw_ostream& os) const {
Ted Kremenek1d536472009-01-16 19:26:50 +0000203 Str->printPretty(os);
Zhongxing Xu4c9390f2008-11-10 13:05:26 +0000204}
205
Ted Kremenekb15eba42008-10-04 05:50:14 +0000206//===----------------------------------------------------------------------===//
207// MemRegionManager methods.
208//===----------------------------------------------------------------------===//
209
210MemSpaceRegion* MemRegionManager::LazyAllocate(MemSpaceRegion*& region) {
211
212 if (!region) {
213 region = (MemSpaceRegion*) A.Allocate<MemSpaceRegion>();
214 new (region) MemSpaceRegion();
215 }
216
217 return region;
218}
219
220MemSpaceRegion* MemRegionManager::getStackRegion() {
221 return LazyAllocate(stack);
222}
223
224MemSpaceRegion* MemRegionManager::getGlobalsRegion() {
225 return LazyAllocate(globals);
226}
227
228MemSpaceRegion* MemRegionManager::getHeapRegion() {
229 return LazyAllocate(heap);
230}
231
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000232MemSpaceRegion* MemRegionManager::getUnknownRegion() {
233 return LazyAllocate(unknown);
234}
235
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000236bool MemRegionManager::onStack(const MemRegion* R) {
237 while (const SubRegion* SR = dyn_cast<SubRegion>(R))
238 R = SR->getSuperRegion();
239
240 return (R != 0) && (R == stack);
241}
242
243bool MemRegionManager::onHeap(const MemRegion* R) {
244 while (const SubRegion* SR = dyn_cast<SubRegion>(R))
245 R = SR->getSuperRegion();
246
247 return (R != 0) && (R == heap);
248}
249
Zhongxing Xu73507bd2008-10-25 14:13:41 +0000250StringRegion* MemRegionManager::getStringRegion(const StringLiteral* Str) {
251 llvm::FoldingSetNodeID ID;
252 MemSpaceRegion* GlobalsR = getGlobalsRegion();
253
254 StringRegion::ProfileRegion(ID, Str, GlobalsR);
255
256 void* InsertPos;
257 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
258 StringRegion* R = cast_or_null<StringRegion>(data);
259
260 if (!R) {
261 R = (StringRegion*) A.Allocate<StringRegion>();
262 new (R) StringRegion(Str, GlobalsR);
263 Regions.InsertNode(R, InsertPos);
264 }
265
266 return R;
267}
268
Ted Kremenek81329ab2008-10-27 21:01:26 +0000269VarRegion* MemRegionManager::getVarRegion(const VarDecl* d) {
270
271 const MemRegion* superRegion = d->hasLocalStorage() ? getStackRegion()
272 : getGlobalsRegion();
273
Ted Kremenekb15eba42008-10-04 05:50:14 +0000274 llvm::FoldingSetNodeID ID;
275 DeclRegion::ProfileRegion(ID, d, superRegion, MemRegion::VarRegionKind);
276
277 void* InsertPos;
278 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
279 VarRegion* R = cast_or_null<VarRegion>(data);
280
281 if (!R) {
282 R = (VarRegion*) A.Allocate<VarRegion>();
283 new (R) VarRegion(d, superRegion);
284 Regions.InsertNode(R, InsertPos);
285 }
286
287 return R;
288}
289
Ted Kremenek6bc91b92008-10-27 20:57:58 +0000290CompoundLiteralRegion*
291MemRegionManager::getCompoundLiteralRegion(const CompoundLiteralExpr* CL) {
292 // Is this compound literal allocated on the stack or is part of the
293 // global constant pool?
294 const MemRegion* superRegion = CL->isFileScope() ?
295 getGlobalsRegion() : getStackRegion();
296
297 // Profile the compound literal.
298 llvm::FoldingSetNodeID ID;
299 CompoundLiteralRegion::ProfileRegion(ID, CL, superRegion);
300
301 void* InsertPos;
302 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
303 CompoundLiteralRegion* R = cast_or_null<CompoundLiteralRegion>(data);
304
305 if (!R) {
306 R = (CompoundLiteralRegion*) A.Allocate<CompoundLiteralRegion>();
307 new (R) CompoundLiteralRegion(CL, superRegion);
308 Regions.InsertNode(R, InsertPos);
309 }
310
311 return R;
312}
313
Ted Kremenek2c0de352008-12-13 19:24:37 +0000314ElementRegion*
315MemRegionManager::getElementRegion(SVal Idx, const TypedRegion* superRegion){
316
Zhongxing Xu54969732008-10-21 05:27:10 +0000317 llvm::FoldingSetNodeID ID;
318 ElementRegion::ProfileRegion(ID, Idx, superRegion);
319
320 void* InsertPos;
321 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
322 ElementRegion* R = cast_or_null<ElementRegion>(data);
323
324 if (!R) {
325 R = (ElementRegion*) A.Allocate<ElementRegion>();
326 new (R) ElementRegion(Idx, superRegion);
327 Regions.InsertNode(R, InsertPos);
328 }
329
330 return R;
331}
332
Ted Kremenek38a4b4b2008-10-17 20:28:54 +0000333/// getSymbolicRegion - Retrieve or create a "symbolic" memory region.
Zhongxing Xu93b80662009-02-05 06:57:29 +0000334SymbolicRegion* MemRegionManager::getSymbolicRegion(const SymbolRef sym,
335 const SymbolManager& mgr) {
Ted Kremenek38a4b4b2008-10-17 20:28:54 +0000336
337 llvm::FoldingSetNodeID ID;
338 SymbolicRegion::ProfileRegion(ID, sym);
339
340 void* InsertPos;
341 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
342 SymbolicRegion* R = cast_or_null<SymbolicRegion>(data);
343
344 if (!R) {
345 R = (SymbolicRegion*) A.Allocate<SymbolicRegion>();
Zhongxing Xu93b80662009-02-05 06:57:29 +0000346 // SymbolicRegion's storage class is usually unknown.
347 new (R) SymbolicRegion(sym, mgr, getUnknownRegion());
Ted Kremenek38a4b4b2008-10-17 20:28:54 +0000348 Regions.InsertNode(R, InsertPos);
349 }
350
351 return R;
352}
353
Ted Kremenekb15eba42008-10-04 05:50:14 +0000354FieldRegion* MemRegionManager::getFieldRegion(const FieldDecl* d,
Ted Kremenek38a4b4b2008-10-17 20:28:54 +0000355 const MemRegion* superRegion) {
Ted Kremenekb15eba42008-10-04 05:50:14 +0000356 llvm::FoldingSetNodeID ID;
357 DeclRegion::ProfileRegion(ID, d, superRegion, MemRegion::FieldRegionKind);
358
359 void* InsertPos;
360 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
361 FieldRegion* R = cast_or_null<FieldRegion>(data);
362
363 if (!R) {
364 R = (FieldRegion*) A.Allocate<FieldRegion>();
365 new (R) FieldRegion(d, superRegion);
366 Regions.InsertNode(R, InsertPos);
367 }
368
369 return R;
370}
371
Ted Kremenek38a4b4b2008-10-17 20:28:54 +0000372ObjCIvarRegion*
373MemRegionManager::getObjCIvarRegion(const ObjCIvarDecl* d,
374 const MemRegion* superRegion) {
Ted Kremenekb15eba42008-10-04 05:50:14 +0000375 llvm::FoldingSetNodeID ID;
376 DeclRegion::ProfileRegion(ID, d, superRegion, MemRegion::ObjCIvarRegionKind);
377
378 void* InsertPos;
379 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
380 ObjCIvarRegion* R = cast_or_null<ObjCIvarRegion>(data);
381
382 if (!R) {
Zhongxing Xu7a861e82008-10-06 03:03:33 +0000383 R = (ObjCIvarRegion*) A.Allocate<ObjCIvarRegion>();
384 new (R) ObjCIvarRegion(d, superRegion);
Ted Kremenekb15eba42008-10-04 05:50:14 +0000385 Regions.InsertNode(R, InsertPos);
386 }
387
388 return R;
389}
390
Ted Kremenek2539c112008-10-24 20:30:08 +0000391ObjCObjectRegion*
392MemRegionManager::getObjCObjectRegion(const ObjCInterfaceDecl* d,
393 const MemRegion* superRegion) {
394 llvm::FoldingSetNodeID ID;
395 DeclRegion::ProfileRegion(ID, d, superRegion,
396 MemRegion::ObjCObjectRegionKind);
397
398 void* InsertPos;
399 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
400 ObjCObjectRegion* R = cast_or_null<ObjCObjectRegion>(data);
401
402 if (!R) {
403 R = (ObjCObjectRegion*) A.Allocate<ObjCObjectRegion>();
404 new (R) ObjCObjectRegion(d, superRegion);
405 Regions.InsertNode(R, InsertPos);
406 }
407
408 return R;
409}
410
Ted Kremenek6a9b5352009-03-01 05:44:08 +0000411TypedViewRegion*
412MemRegionManager::getTypedViewRegion(QualType t, const MemRegion* superRegion) {
Zhongxing Xu8fbe7ae2008-11-16 04:07:26 +0000413 llvm::FoldingSetNodeID ID;
Ted Kremenek6a9b5352009-03-01 05:44:08 +0000414 TypedViewRegion::ProfileRegion(ID, t, superRegion);
Zhongxing Xu8fbe7ae2008-11-16 04:07:26 +0000415
416 void* InsertPos;
417 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
Ted Kremenek6a9b5352009-03-01 05:44:08 +0000418 TypedViewRegion* R = cast_or_null<TypedViewRegion>(data);
Zhongxing Xu8fbe7ae2008-11-16 04:07:26 +0000419
420 if (!R) {
Ted Kremenek6a9b5352009-03-01 05:44:08 +0000421 R = (TypedViewRegion*) A.Allocate<TypedViewRegion>();
422 new (R) TypedViewRegion(t, superRegion);
Zhongxing Xu8fbe7ae2008-11-16 04:07:26 +0000423 Regions.InsertNode(R, InsertPos);
424 }
425
426 return R;
427}
Ted Kremenek2539c112008-10-24 20:30:08 +0000428
Ted Kremenekea9ca502008-11-02 00:34:33 +0000429AllocaRegion* MemRegionManager::getAllocaRegion(const Expr* E, unsigned cnt) {
430 llvm::FoldingSetNodeID ID;
431 AllocaRegion::ProfileRegion(ID, E, cnt);
432
433 void* InsertPos;
434 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
435 AllocaRegion* R = cast_or_null<AllocaRegion>(data);
436
437 if (!R) {
438 R = (AllocaRegion*) A.Allocate<AllocaRegion>();
439 new (R) AllocaRegion(E, cnt, getStackRegion());
440 Regions.InsertNode(R, InsertPos);
441 }
442
443 return R;
444}
445
Ted Kremenekb15eba42008-10-04 05:50:14 +0000446bool MemRegionManager::hasStackStorage(const MemRegion* R) {
Ted Kremenek38a4b4b2008-10-17 20:28:54 +0000447
448 // Only subregions can have stack storage.
Ted Kremenekea9ca502008-11-02 00:34:33 +0000449 const SubRegion* SR = dyn_cast<SubRegion>(R);
450
Ted Kremenek38a4b4b2008-10-17 20:28:54 +0000451 if (!SR)
452 return false;
Ted Kremenekea9ca502008-11-02 00:34:33 +0000453
Ted Kremenekb15eba42008-10-04 05:50:14 +0000454 MemSpaceRegion* S = getStackRegion();
455
Ted Kremenek38a4b4b2008-10-17 20:28:54 +0000456 while (SR) {
457 R = SR->getSuperRegion();
458 if (R == S)
459 return true;
460
461 SR = dyn_cast<SubRegion>(R);
Ted Kremenekb15eba42008-10-04 05:50:14 +0000462 }
463
464 return false;
465}