Static Value-Flow Analysis
Loading...
Searching...
No Matches
MemRegion.h
Go to the documentation of this file.
1//===- MemRegion.h -- Memory region -----------------------------------------//
2//
3// SVF: Static Value-Flow Analysis
4//
5// Copyright (C) <2013-> <Yulei Sui>
6//
7
8// This program is free software: you can redistribute it and/or modify
9// it under the terms of the GNU Affero General Public License as published by
10// the Free Software Foundation, either version 3 of the License, or
11// (at your option) any later version.
12
13// This program is distributed in the hope that it will be useful,
14// but WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16// GNU Affero General Public License for more details.
17
18// You should have received a copy of the GNU Affero General Public License
19// along with this program. If not, see <http://www.gnu.org/licenses/>.
20//
21//===----------------------------------------------------------------------===//
22
23/*
24 * MemoryRegion.h
25 *
26 * Created on: Jul 14, 2013
27 * Author: Yulei Sui
28 * The implementation is based on
29 * Yulei Sui, Hua Yan, Yunpeng Zhang, Jingling Xue and Zheng Zheng.
30 * "Parallel Construction of Interprocedural Memory SSA Form".
31 * Journal of Systems and Software (JSS'16)
32 */
33
34#ifndef MEMORYREGION_H_
35#define MEMORYREGION_H_
36
37#include "Graphs/ICFG.h"
38#include "Graphs/PTACallGraph.h"
39#include "Graphs/SCC.h"
40#include "SVFIR/SVFIR.h"
41#include "Util/WorkList.h"
42
43#include <set>
44
45namespace SVF
46{
47
48class BVDataPTAImpl;
49
50typedef NodeID MRID;
53
56{
57
58public:
59 typedef bool Condition;
60private:
65
66public:
70 {
71 }
74 {
75 }
76
78 inline MRID getMRID() const
79 {
80 return rid;
81 }
83 inline const NodeBS &getPointsTo() const
84 {
85 return cptsSet;
86 }
88 inline bool operator==(const MemRegion* rhs) const
89 {
90 return this->getPointsTo() == rhs->getPointsTo();
91 }
93 inline std::string dumpStr() const
94 {
95 std::string str;
96 str += "pts{";
98 ii != ie; ii++)
99 {
100 char int2str[16];
101 snprintf(int2str, sizeof(int2str), "%d", *ii);
102 str += int2str;
103 str += " ";
104 }
105 str += "}";
106 return str;
107 }
108
111
112 typedef struct equalMemRegion
113 {
114 bool operator()(const MemRegion* lhs, const MemRegion* rhs) const
115 {
116 return SVFUtil::cmpNodeBS(lhs->getPointsTo(), rhs->getPointsTo());
117 }
118 } equalMemRegion;
120
122 inline u32_t getRegionSize() const
123 {
124 return cptsSet.count();
125 }
126};
127
132{
133
134public:
136
138
139
140
147
152
157
159
164
166
167
172
174
179
181 {
182 return memRegSet;
183 }
184
186 inline const NodeBS& getRepPointsTo(const NodeBS& cpts) const
187 {
188 PtsToRepPtsSetMap::const_iterator it = cptsToRepCPtsMap.find(cpts);
189 assert(it!=cptsToRepCPtsMap.end() && "can not find superset of cpts??");
190 return it->second;
191 }
193 const MemRegion* getMR(const NodeBS& cpts) const;
194
195private:
196
201
220
225
238
241
244
246 void destroy();
247
248 //Get all objects might pass into callee from a callsite
249 void collectCallSitePts(const CallICFGNode* cs);
250
251 //Recursive collect points-to chain
253
256 {
257 return csToCallSiteArgsPtsMap[cs];
258 }
261 {
262 return csToCallSiteRetPtsMap[cs];
263 }
266 bool isNonLocalObject(NodeID id, const SVFFunction* curFun) const;
267
269 void getEscapObjviaGlobals(NodeBS& globs, const NodeBS& pts);
270
273
274protected:
276
281
283 void createMR(const SVFFunction* fun, const NodeBS& cpts);
284
286 void collectGlobals();
287
289 virtual void collectModRefForLoadStore();
290
292 virtual void collectModRefForCall();
293
295 virtual void partitionMRs();
296
298 virtual void updateAliasMRs();
299
301 virtual void sortPointsTo(const NodeBS& cpts);
302
304 virtual inline bool isAliasedMR(const NodeBS& cpts, const MemRegion* mr)
305 {
306 return mr->getPointsTo().intersects(cpts);
307 }
309 virtual inline void getAliasMemRegions(MRSet& aliasMRs, const NodeBS& cpts, const SVFFunction* fun)
310 {
311 for(MRSet::const_iterator it = funToMRsMap[fun].begin(), eit = funToMRsMap[fun].end(); it!=eit; ++it)
312 {
313 if(isAliasedMR(cpts,*it))
314 aliasMRs.insert(*it);
315 }
316 }
317
319 virtual inline void getMRsForLoad(MRSet& aliasMRs, const NodeBS& cpts, const SVFFunction*)
320 {
321 const MemRegion* mr = getMR(cpts);
322 aliasMRs.insert(mr);
323 }
324
326 virtual inline void getMRsForCallSiteRef(MRSet& aliasMRs, const NodeBS& cpts, const SVFFunction*)
327 {
328 const MemRegion* mr = getMR(cpts);
329 aliasMRs.insert(mr);
330 }
331
333 virtual void modRefAnalysis(PTACallGraphNode* callGraphNode, WorkList& worklist);
334
336 virtual bool handleCallsiteModRef(NodeBS& mod, NodeBS& ref, const CallICFGNode* cs, const SVFFunction* fun);
337
338
340
341 inline void addCPtsToStore(NodeBS& cpts, const StoreStmt *st, const SVFFunction* fun)
342 {
343 storesToPointsToMap[st] = cpts;
344 funToPointsToMap[fun].insert(cpts);
346 }
347 inline void addCPtsToLoad(NodeBS& cpts, const LoadStmt *ld, const SVFFunction* fun)
348 {
349 loadsToPointsToMap[ld] = cpts;
350 funToPointsToMap[fun].insert(cpts);
352 }
353 inline void addCPtsToCallSiteRefs(NodeBS& cpts, const CallICFGNode* cs)
354 {
355 callsiteToRefPointsToMap[cs] |= cpts;
356 funToPointsToMap[cs->getCaller()].insert(cpts);
357 }
358 inline void addCPtsToCallSiteMods(NodeBS& cpts, const CallICFGNode* cs)
359 {
360 callsiteToModPointsToMap[cs] |= cpts;
361 funToPointsToMap[cs->getCaller()].insert(cpts);
362 }
363 inline bool hasCPtsList(const SVFFunction* fun) const
364 {
365 return funToPointsToMap.find(fun)!=funToPointsToMap.end();
366 }
368 {
369 return funToPointsToMap[fun];
370 }
372 {
373 return funToPointsToMap;
374 }
376
378
379 void addRefSideEffectOfFunction(const SVFFunction* fun, const NodeBS& refs);
381 void addModSideEffectOfFunction(const SVFFunction* fun, const NodeBS& mods);
383 bool addRefSideEffectOfCallSite(const CallICFGNode* cs, const NodeBS& refs);
385 bool addModSideEffectOfCallSite(const CallICFGNode* cs, const NodeBS& mods);
386
389 {
390 return funToRefsMap[fun];
391 }
394 {
395 return funToModsMap[fun];
396 }
399 {
400 return csToRefsMap[cs];
401 }
404 {
405 return csToModsMap[cs];
406 }
409 {
410 return csToRefsMap.find(cs) != csToRefsMap.end();
411 }
414 {
415 return csToModsMap.find(cs) != csToModsMap.end();
416 }
418
419public:
420 inline u32_t getMRNum() const
421 {
422 return memRegSet.size();
423 }
424
426 virtual ~MRGenerator()
427 {
428 destroy();
429 }
430
432 virtual void generateMRs();
433
435 const SVFFunction* getFunction(const PAGEdge* pagEdge) const
436 {
437 PAGEdgeToFunMap::const_iterator it = pagEdgeToFunMap.find(pagEdge);
438 assert(it!=pagEdgeToFunMap.end() && "can not find its function, it is a global SVFIR edge");
439 return it->second;
440 }
442
443 inline MRSet& getFunMRSet(const SVFFunction* fun)
444 {
445 return funToMRsMap[fun];
446 }
447 inline MRSet& getLoadMRSet(const LoadStmt* load)
448 {
449 return loadsToMRsMap[load];
450 }
451 inline MRSet& getStoreMRSet(const StoreStmt* store)
452 {
453 return storesToMRsMap[store];
454 }
455 inline bool hasRefMRSet(const CallICFGNode* cs)
456 {
457 return callsiteToRefMRsMap.find(cs)!=callsiteToRefMRsMap.end();
458 }
459 inline bool hasModMRSet(const CallICFGNode* cs)
460 {
461 return callsiteToModMRsMap.find(cs)!=callsiteToModMRsMap.end();
462 }
464 {
465 return callsiteToRefMRsMap[cs];
466 }
468 {
469 return callsiteToModMRsMap[cs];
470 }
472
473 bool hasSVFStmtList(const ICFGNode* icfgNode);
476
478
479
486
487};
488
489} // End namespace SVF
490
491#endif /* MEMORYREGION_H_ */
cJSON * p
Definition cJSON.cpp:2559
const SVFFunction * getCaller() const
Return callsite.
Definition ICFGNode.h:470
virtual void getMRsForCallSiteRef(MRSet &aliasMRs, const NodeBS &cpts, const SVFFunction *)
Get memory regions for call site ref according to cpts.
Definition MemRegion.h:326
PTACallGraph * callGraph
Definition MemRegion.h:199
FunToPointsTosMap funToPointsToMap
Map a function to all of its conditional points-to sets.
Definition MemRegion.h:222
MRSet & getStoreMRSet(const StoreStmt *store)
Definition MemRegion.h:451
Map< const SVFFunction *, MRSet > FunToMRsMap
Map a function to its region set.
Definition MemRegion.h:149
LoadsToMRsMap loadsToMRsMap
Map a load SVFIR Edge to its memory regions sets in order for inserting mus in Memory SSA.
Definition MemRegion.h:205
NodeToPTSSMap cachedPtsChainMap
Map a pointer to its cached points-to chain;.
Definition MemRegion.h:240
MRSet & getCallSiteRefMRSet(const CallICFGNode *cs)
Definition MemRegion.h:463
bool hasModSideEffectOfCallSite(const CallICFGNode *cs)
Has indirect mods of a callsite.
Definition MemRegion.h:413
const SVFFunction * getFunction(const PAGEdge *pagEdge) const
Get the function which SVFIR Edge located.
Definition MemRegion.h:435
bool hasCPtsList(const SVFFunction *fun) const
Definition MemRegion.h:363
virtual void collectModRefForLoadStore()
Generate regions for loads/stores.
CallSiteToPointsToMap csToCallSiteRetPtsMap
Map a callsite to all its object might return from its callees.
Definition MemRegion.h:237
StoresToMRsMap storesToMRsMap
Map a store SVFIR Edge to its memory regions sets in order for inserting chis in Memory SSA.
Definition MemRegion.h:207
Map< const CallICFGNode *, NodeBS > CallSiteToPointsToMap
Definition MemRegion.h:162
Map< const LoadStmt *, MRSet > LoadsToMRsMap
Definition MemRegion.h:153
SVFStmtList & getPAGEdgesFromInst(const ICFGNode *node)
Given an instruction, get all its the PAGEdge (statement) in sequence.
virtual bool isAliasedMR(const NodeBS &cpts, const MemRegion *mr)
Whether a region is aliased with a conditional points-to.
Definition MemRegion.h:304
FunToPointsToMap funToModsMap
Map a function to its indirect defs of memory objects.
Definition MemRegion.h:229
bool isNonLocalObject(NodeID id, const SVFFunction *curFun) const
CallSiteToPointsToMap callsiteToRefPointsToMap
Map a callsite to it refs cpts set.
Definition MemRegion.h:217
Map< const StoreStmt *, NodeBS > StoresToPointsToMap
Definition MemRegion.h:161
void addModSideEffectOfFunction(const SVFFunction *fun, const NodeBS &mods)
Add indirect def an memory object in the function.
FunToMRsMap funToMRsMap
Map a function to all its memory regions.
Definition MemRegion.h:203
virtual ~MRGenerator()
Destructor.
Definition MemRegion.h:426
virtual void generateMRs()
Start generating memory regions.
virtual void collectModRefForCall()
Generate regions for calls/rets.
const MemRegion * getMR(const NodeBS &cpts) const
Get a memory region according to cpts.
Definition MemRegion.cpp:93
FunToPointsTosMap & getFunToPointsToList()
Definition MemRegion.h:371
CallSiteToPointsToMap csToModsMap
Map a callsite to its indirect defs of memory objects.
Definition MemRegion.h:233
OrderedMap< NodeBS, NodeBS, SVFUtil::equalNodeBS > PtsToRepPtsSetMap
Definition MemRegion.h:146
virtual void sortPointsTo(const NodeBS &cpts)
Given a condition pts, insert into cptsToRepCPtsMap for region generation.
FIFOWorkList< NodeID > WorkList
Definition MemRegion.h:135
Map< const SVFFunction *, PointsToList > FunToPointsTosMap
Definition MemRegion.h:145
void addCPtsToLoad(NodeBS &cpts, const LoadStmt *ld, const SVFFunction *fun)
Definition MemRegion.h:347
CallSiteToMRsMap callsiteToRefMRsMap
Map a callsite to its refs regions.
Definition MemRegion.h:209
const NodeBS & getModSideEffectOfFunction(const SVFFunction *fun)
Get indirect mods of a function.
Definition MemRegion.h:393
bool hasModMRSet(const CallICFGNode *cs)
Definition MemRegion.h:459
StoresToPointsToMap storesToPointsToMap
Map a store SVFIR Edge to its CPts set map.
Definition MemRegion.h:215
NodeBS & CollectPtsChain(NodeID id)
MRSet & getFunMRSet(const SVFFunction *fun)
Get Memory Region set.
Definition MemRegion.h:443
Map< const PAGEdge *, const SVFFunction * > PAGEdgeToFunMap
Definition MemRegion.h:142
bool addRefSideEffectOfCallSite(const CallICFGNode *cs, const NodeBS &refs)
Add indirect uses an memory object in the function.
SVFIR::SVFStmtList SVFStmtList
SVFIR edge list.
Definition MemRegion.h:176
void addRefSideEffectOfFunction(const SVFFunction *fun, const NodeBS &refs)
Add/Get methods for side-effect of functions and callsites.
OrderedSet< NodeBS, SVFUtil::equalNodeBS > PointsToList
Definition MemRegion.h:143
MRSet & getMRSet()
Definition MemRegion.h:180
bool addModSideEffectOfCallSite(const CallICFGNode *cs, const NodeBS &mods)
Add indirect def an memory object in the function.
Map< const CallICFGNode *, NodeBS > CallSiteToNodeBSMap
Map a callsite to its indirect refs/mods of memory objects.
Definition MemRegion.h:170
void addCPtsToCallSiteRefs(NodeBS &cpts, const CallICFGNode *cs)
Definition MemRegion.h:353
NodeBS getRefInfoForCall(const CallICFGNode *cs)
bool hasRefSideEffectOfCallSite(const CallICFGNode *cs)
Has indirect refs of a callsite.
Definition MemRegion.h:408
OrderedSet< const MemRegion *, MemRegion::equalMemRegion > MRSet
Get typedef from Pointer Analysis.
Definition MemRegion.h:141
virtual void updateAliasMRs()
Update aliased regions for loads/stores/callsites.
void collectCallSitePts(const CallICFGNode *cs)
const NodeBS & getRepPointsTo(const NodeBS &cpts) const
Get superset cpts set.
Definition MemRegion.h:186
PointsToList & getPointsToList(const SVFFunction *fun)
Definition MemRegion.h:367
bool hasSVFStmtList(const ICFGNode *icfgNode)
Whether this instruction has SVFIR Edge.
Map< const SVFFunction *, NodeBS > FunToNodeBSMap
Maps Mod-Ref analysis.
Definition MemRegion.h:168
Map< NodeID, NodeBS > NodeToPTSSMap
Definition MemRegion.h:173
MRSet & getLoadMRSet(const LoadStmt *load)
Definition MemRegion.h:447
PAGEdgeToFunMap pagEdgeToFunMap
Map a PAGEdge to its fun.
Definition MemRegion.h:224
void getCallGraphSCCRevTopoOrder(WorkList &worklist)
Get reverse topo call graph scc.
PtsToRepPtsSetMap cptsToRepCPtsMap
Map a condition pts to its rep conditional pts (super set points-to)
Definition MemRegion.h:280
void addCPtsToCallSiteMods(NodeBS &cpts, const CallICFGNode *cs)
Definition MemRegion.h:358
CallSiteToPointsToMap callsiteToModPointsToMap
Map a callsite to it mods cpts set.
Definition MemRegion.h:219
const NodeBS & getRefSideEffectOfFunction(const SVFFunction *fun)
Get indirect refs of a function.
Definition MemRegion.h:388
virtual void getMRsForLoad(MRSet &aliasMRs, const NodeBS &cpts, const SVFFunction *)
Get memory regions for a load statement according to cpts.
Definition MemRegion.h:319
virtual void partitionMRs()
Partition regions.
ModRefInfo getModRefInfo(const CallICFGNode *cs)
void createMR(const SVFFunction *fun, const NodeBS &cpts)
Generate a memory region and put in into functions which use it.
Definition MemRegion.cpp:70
void destroy()
Clean up memory.
Definition MemRegion.cpp:52
const NodeBS & getRefSideEffectOfCallSite(const CallICFGNode *cs)
Get indirect refs of a callsite.
Definition MemRegion.h:398
CallSiteToPointsToMap csToRefsMap
Map a callsite to its indirect uses of memory objects.
Definition MemRegion.h:231
virtual bool handleCallsiteModRef(NodeBS &mod, NodeBS &ref, const CallICFGNode *cs, const SVFFunction *fun)
Get Mod-Ref of a callee function.
NodeBS & getCallSiteArgsPts(const CallICFGNode *cs)
Return the pts chain of all callsite arguments.
Definition MemRegion.h:255
Map< const LoadStmt *, NodeBS > LoadsToPointsToMap
Map loads/stores/callsites to their cpts set.
Definition MemRegion.h:160
Map< const SVFFunction *, NodeBS > FunToPointsToMap
Definition MemRegion.h:144
virtual void getAliasMemRegions(MRSet &aliasMRs, const NodeBS &cpts, const SVFFunction *fun)
Get all aliased mem regions from function fun according to cpts.
Definition MemRegion.h:309
virtual void modRefAnalysis(PTACallGraphNode *callGraphNode, WorkList &worklist)
Mod-Ref analysis for callsite invoking this callGraphNode.
void getEscapObjviaGlobals(NodeBS &globs, const NodeBS &pts)
Get all the objects in callee's modref escaped via global objects (the chain pts of globals)
CallSiteToMRsMap callsiteToModMRsMap
Map a callsite to its mods regions.
Definition MemRegion.h:211
MRSet & getCallSiteModMRSet(const CallICFGNode *cs)
Definition MemRegion.h:467
NodeBS getModInfoForCall(const CallICFGNode *cs)
getModRefInfo APIs
const NodeBS & getModSideEffectOfCallSite(const CallICFGNode *cs)
Get indirect mods of a callsite.
Definition MemRegion.h:403
bool hasRefMRSet(const CallICFGNode *cs)
Definition MemRegion.h:455
NodeBS & getCallSiteRetPts(const CallICFGNode *cs)
Return the pts chain of the return parameter of the callsite.
Definition MemRegion.h:260
NodeBS allGlobals
All global variable SVFIR node ids.
Definition MemRegion.h:243
LoadsToPointsToMap loadsToPointsToMap
Map a load SVFIR Edge to its CPts set map.
Definition MemRegion.h:213
void addCPtsToStore(NodeBS &cpts, const StoreStmt *st, const SVFFunction *fun)
Add cpts to store/load.
Definition MemRegion.h:341
FunToPointsToMap funToRefsMap
Map a function to its indirect uses of memory objects.
Definition MemRegion.h:227
SCCDetection< PTACallGraph * > SCC
Call Graph SCC.
Definition MemRegion.h:178
MRSet memRegSet
A set of All memory regions.
Definition MemRegion.h:278
Map< const CallICFGNode *, MRSet > CallSiteToMRsMap
Definition MemRegion.h:155
Map< const StoreStmt *, MRSet > StoresToMRsMap
Definition MemRegion.h:154
BVDataPTAImpl * pta
Definition MemRegion.h:197
void collectGlobals()
Collect all global variables for later escape analysis.
CallSiteToPointsToMap csToCallSiteArgsPtsMap
Map a callsite to all its object might pass into its callees.
Definition MemRegion.h:235
u32_t getMRNum() const
Definition MemRegion.h:420
Memory Region class.
Definition MemRegion.h:56
MemRegion(const NodeBS &cp)
Constructor.
Definition MemRegion.h:68
static u32_t totalMRNum
region ID 0 is reserved
Definition MemRegion.h:62
~MemRegion()
Destructor.
Definition MemRegion.h:73
const NodeBS cptsSet
Definition MemRegion.h:64
MRID getMRID() const
Return memory region ID.
Definition MemRegion.h:78
const NodeBS & getPointsTo() const
Return points-to.
Definition MemRegion.h:83
u32_t getRegionSize() const
Return memory object number inside a region.
Definition MemRegion.h:122
std::string dumpStr() const
Dump string.
Definition MemRegion.h:93
bool operator==(const MemRegion *rhs) const
Operator== overriding.
Definition MemRegion.h:88
std::vector< const SVFStmt * > SVFStmtList
Definition SVFIR.h:59
bool intersects(const SparseBitVector< ElementSize > *RHS) const
unsigned count() const
iterator begin() const
bool cmpNodeBS(const NodeBS &lpts, const NodeBS &rpts)
Definition SVFUtil.h:125
for isBitcode
Definition BasicTypes.h:68
NodeID MRVERID
Definition MemRegion.h:51
NodeID MRID
Definition MemRegion.h:50
NodeID MRVERSION
Definition MemRegion.h:52
ModRefInfo
Definition SVFType.h:519
u32_t NodeID
Definition GeneralType.h:55
llvm::IRBuilder IRBuilder
Definition BasicTypes.h:74
unsigned u32_t
Definition GeneralType.h:46
bool operator()(const MemRegion *lhs, const MemRegion *rhs) const
Definition MemRegion.h:114