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/SCC.h"
38#include "SVFIR/SVFIR.h"
39#include "Util/WorkList.h"
40
41#include <set>
42
43namespace SVF
44{
45
46class BVDataPTAImpl;
47
48class CallICFGNode;
49
50class CallGraph;
51class CallGraphSCC;
52class CallGraphNode;
53
54typedef NodeID MRID;
57
60{
61
62public:
63 typedef bool Condition;
64private:
69
70public:
74 {
75 }
78 {
79 }
80
82 inline MRID getMRID() const
83 {
84 return rid;
85 }
87 inline const NodeBS &getPointsTo() const
88 {
89 return cptsSet;
90 }
92 inline bool operator==(const MemRegion* rhs) const
93 {
94 return this->getPointsTo() == rhs->getPointsTo();
95 }
97 inline std::string dumpStr() const
98 {
99 std::string str;
100 str += "pts{";
102 ii != ie; ii++)
103 {
104 char int2str[16];
105 snprintf(int2str, sizeof(int2str), "%d", *ii);
106 str += int2str;
107 str += " ";
108 }
109 str += "}";
110 return str;
111 }
112
115
116 typedef struct equalMemRegion
117 {
118 bool operator()(const MemRegion* lhs, const MemRegion* rhs) const
119 {
120 return SVFUtil::cmpNodeBS(lhs->getPointsTo(), rhs->getPointsTo());
121 }
122 } equalMemRegion;
124
126 inline u32_t getRegionSize() const
127 {
128 return cptsSet.count();
129 }
130};
131
136{
137
138public:
140
142
143
144
151
156
161
163
168
170
171
176
178
183
185 {
186 return memRegSet;
187 }
188
190 inline const NodeBS& getRepPointsTo(const NodeBS& cpts) const
191 {
192 PtsToRepPtsSetMap::const_iterator it = cptsToRepCPtsMap.find(cpts);
193 assert(it!=cptsToRepCPtsMap.end() && "can not find superset of cpts??");
194 return it->second;
195 }
197 const MemRegion* getMR(const NodeBS& cpts) const;
198
199private:
200
205
224
229
242
245
248
250 void destroy();
251
252 //Get all objects might pass into callee from a callsite
253 void collectCallSitePts(const CallICFGNode* cs);
254
255 //Recursive collect points-to chain
257
260 {
261 return csToCallSiteArgsPtsMap[cs];
262 }
265 {
266 return csToCallSiteRetPtsMap[cs];
267 }
270 bool isNonLocalObject(NodeID id, const FunObjVar* curFun) const;
271
273 void getEscapObjviaGlobals(NodeBS& globs, const NodeBS& pts);
274
275
276protected:
278
283
285 void createMR(const FunObjVar* fun, const NodeBS& cpts);
286
288 void collectGlobals();
289
291 virtual void collectModRefForLoadStore();
292
294 virtual void collectModRefForCall();
295
297 virtual void partitionMRs();
298
300 virtual void updateAliasMRs();
301
303 virtual void sortPointsTo(const NodeBS& cpts);
304
306 virtual inline bool isAliasedMR(const NodeBS& cpts, const MemRegion* mr)
307 {
308 return mr->getPointsTo().intersects(cpts);
309 }
311 virtual inline void getAliasMemRegions(MRSet& aliasMRs, const NodeBS& cpts, const FunObjVar* fun)
312 {
313 for(MRSet::const_iterator it = funToMRsMap[fun].begin(), eit = funToMRsMap[fun].end(); it!=eit; ++it)
314 {
315 if(isAliasedMR(cpts,*it))
316 aliasMRs.insert(*it);
317 }
318 }
319
321 virtual inline void getMRsForLoad(MRSet& aliasMRs, const NodeBS& cpts, const FunObjVar*)
322 {
323 const MemRegion* mr = getMR(cpts);
324 aliasMRs.insert(mr);
325 }
326
328 virtual inline void getMRsForCallSiteRef(MRSet& aliasMRs, const NodeBS& cpts, const FunObjVar*)
329 {
330 const MemRegion* mr = getMR(cpts);
331 aliasMRs.insert(mr);
332 }
333
335 virtual void modRefAnalysis(CallGraphNode* callGraphNode, WorkList& worklist);
336
338 virtual bool handleCallsiteModRef(NodeBS& mod, NodeBS& ref, const CallICFGNode* cs, const FunObjVar* fun);
339
340
342
343 inline void addCPtsToStore(NodeBS& cpts, const StoreStmt *st, const FunObjVar* fun)
344 {
345 storesToPointsToMap[st] = cpts;
346 funToPointsToMap[fun].insert(cpts);
348 }
349 inline void addCPtsToLoad(NodeBS& cpts, const LoadStmt *ld, const FunObjVar* fun)
350 {
351 loadsToPointsToMap[ld] = cpts;
352 funToPointsToMap[fun].insert(cpts);
354 }
355 inline void addCPtsToCallSiteRefs(NodeBS& cpts, const CallICFGNode* cs)
356 {
357 callsiteToRefPointsToMap[cs] |= cpts;
358 funToPointsToMap[cs->getCaller()].insert(cpts);
359 }
360 inline void addCPtsToCallSiteMods(NodeBS& cpts, const CallICFGNode* cs)
361 {
362 callsiteToModPointsToMap[cs] |= cpts;
363 funToPointsToMap[cs->getCaller()].insert(cpts);
364 }
365 inline bool hasCPtsList(const FunObjVar* fun) const
366 {
367 return funToPointsToMap.find(fun)!=funToPointsToMap.end();
368 }
370 {
371 return funToPointsToMap[fun];
372 }
374 {
375 return funToPointsToMap;
376 }
378
380
381 void addRefSideEffectOfFunction(const FunObjVar* fun, const NodeBS& refs);
383 void addModSideEffectOfFunction(const FunObjVar* fun, const NodeBS& mods);
385 bool addRefSideEffectOfCallSite(const CallICFGNode* cs, const NodeBS& refs);
387 bool addModSideEffectOfCallSite(const CallICFGNode* cs, const NodeBS& mods);
388
391 {
392 return funToRefsMap[fun];
393 }
396 {
397 return funToModsMap[fun];
398 }
401 {
402 return csToRefsMap[cs];
403 }
406 {
407 return csToModsMap[cs];
408 }
411 {
412 return csToRefsMap.find(cs) != csToRefsMap.end();
413 }
416 {
417 return csToModsMap.find(cs) != csToModsMap.end();
418 }
420
421public:
422 inline u32_t getMRNum() const
423 {
424 return memRegSet.size();
425 }
426
428 virtual ~MRGenerator()
429 {
430 destroy();
431 }
432
434 virtual void generateMRs();
435
438 {
439 PAGEdgeToFunMap::const_iterator it = pagEdgeToFunMap.find(pagEdge);
440 assert(it!=pagEdgeToFunMap.end() && "can not find its function, it is a global SVFIR edge");
441 return it->second;
442 }
444
445 inline MRSet& getFunMRSet(const FunObjVar* fun)
446 {
447 return funToMRsMap[fun];
448 }
449 inline MRSet& getLoadMRSet(const LoadStmt* load)
450 {
451 return loadsToMRsMap[load];
452 }
453 inline MRSet& getStoreMRSet(const StoreStmt* store)
454 {
455 return storesToMRsMap[store];
456 }
457 inline bool hasRefMRSet(const CallICFGNode* cs)
458 {
459 return callsiteToRefMRsMap.find(cs)!=callsiteToRefMRsMap.end();
460 }
461 inline bool hasModMRSet(const CallICFGNode* cs)
462 {
463 return callsiteToModMRsMap.find(cs)!=callsiteToModMRsMap.end();
464 }
466 {
467 return callsiteToRefMRsMap[cs];
468 }
470 {
471 return callsiteToModMRsMap[cs];
472 }
474
475 bool hasSVFStmtList(const ICFGNode* icfgNode);
478
480
481
485 ModRefInfo getModRefInfo(const CallICFGNode* cs, const SVFVar* V);
488
489};
490
491} // End namespace SVF
492
493#endif /* MEMORYREGION_H_ */
cJSON * p
Definition cJSON.cpp:2559
const FunObjVar * getCaller() const
Return callsite.
Definition ICFGNode.h:453
MRSet & getFunMRSet(const FunObjVar *fun)
Get Memory Region set.
Definition MemRegion.h:445
FunToPointsTosMap funToPointsToMap
Map a function to all of its conditional points-to sets.
Definition MemRegion.h:226
MRSet & getStoreMRSet(const StoreStmt *store)
Definition MemRegion.h:453
LoadsToMRsMap loadsToMRsMap
Map a load SVFIR Edge to its memory regions sets in order for inserting mus in Memory SSA.
Definition MemRegion.h:209
NodeToPTSSMap cachedPtsChainMap
Map a pointer to its cached points-to chain;.
Definition MemRegion.h:244
const NodeBS & getModSideEffectOfFunction(const FunObjVar *fun)
Get indirect mods of a function.
Definition MemRegion.h:395
MRSet & getCallSiteRefMRSet(const CallICFGNode *cs)
Definition MemRegion.h:465
bool hasModSideEffectOfCallSite(const CallICFGNode *cs)
Has indirect mods of a callsite.
Definition MemRegion.h:415
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:241
StoresToMRsMap storesToMRsMap
Map a store SVFIR Edge to its memory regions sets in order for inserting chis in Memory SSA.
Definition MemRegion.h:211
Map< const CallICFGNode *, NodeBS > CallSiteToPointsToMap
Definition MemRegion.h:166
Map< const LoadStmt *, MRSet > LoadsToMRsMap
Definition MemRegion.h:157
virtual bool isAliasedMR(const NodeBS &cpts, const MemRegion *mr)
Whether a region is aliased with a conditional points-to.
Definition MemRegion.h:306
const NodeBS & getRefSideEffectOfFunction(const FunObjVar *fun)
Get indirect refs of a function.
Definition MemRegion.h:390
FunToPointsToMap funToModsMap
Map a function to its indirect defs of memory objects.
Definition MemRegion.h:233
PointsToList & getPointsToList(const FunObjVar *fun)
Definition MemRegion.h:369
CallSiteToPointsToMap callsiteToRefPointsToMap
Map a callsite to it refs cpts set.
Definition MemRegion.h:221
void addRefSideEffectOfFunction(const FunObjVar *fun, const NodeBS &refs)
Add/Get methods for side-effect of functions and callsites.
Map< const StoreStmt *, NodeBS > StoresToPointsToMap
Definition MemRegion.h:165
FunToMRsMap funToMRsMap
Map a function to all its memory regions.
Definition MemRegion.h:207
virtual bool handleCallsiteModRef(NodeBS &mod, NodeBS &ref, const CallICFGNode *cs, const FunObjVar *fun)
Get Mod-Ref of a callee function.
Map< const PAGEdge *, const FunObjVar * > PAGEdgeToFunMap
Definition MemRegion.h:146
CallGraph * callGraph
Definition MemRegion.h:203
virtual ~MRGenerator()
Destructor.
Definition MemRegion.h:428
virtual void generateMRs()
Start generating memory regions.
virtual void collectModRefForCall()
Generate regions for calls/rets.
void createMR(const FunObjVar *fun, const NodeBS &cpts)
Generate a memory region and put in into functions which use it.
Definition MemRegion.cpp:70
const MemRegion * getMR(const NodeBS &cpts) const
Get a memory region according to cpts.
Definition MemRegion.cpp:93
void addCPtsToLoad(NodeBS &cpts, const LoadStmt *ld, const FunObjVar *fun)
Definition MemRegion.h:349
FunToPointsTosMap & getFunToPointsToList()
Definition MemRegion.h:373
void addModSideEffectOfFunction(const FunObjVar *fun, const NodeBS &mods)
Add indirect def an memory object in the function.
CallSiteToPointsToMap csToModsMap
Map a callsite to its indirect defs of memory objects.
Definition MemRegion.h:237
OrderedMap< NodeBS, NodeBS, SVFUtil::equalNodeBS > PtsToRepPtsSetMap
Definition MemRegion.h:150
virtual void sortPointsTo(const NodeBS &cpts)
Given a condition pts, insert into cptsToRepCPtsMap for region generation.
void addCPtsToStore(NodeBS &cpts, const StoreStmt *st, const FunObjVar *fun)
Add cpts to store/load.
Definition MemRegion.h:343
FIFOWorkList< NodeID > WorkList
Definition MemRegion.h:139
Map< const FunObjVar *, NodeBS > FunToNodeBSMap
Maps Mod-Ref analysis.
Definition MemRegion.h:172
CallSiteToMRsMap callsiteToRefMRsMap
Map a callsite to its refs regions.
Definition MemRegion.h:213
virtual void getAliasMemRegions(MRSet &aliasMRs, const NodeBS &cpts, const FunObjVar *fun)
Get all aliased mem regions from function fun according to cpts.
Definition MemRegion.h:311
bool hasModMRSet(const CallICFGNode *cs)
Definition MemRegion.h:461
StoresToPointsToMap storesToPointsToMap
Map a store SVFIR Edge to its CPts set map.
Definition MemRegion.h:219
const FunObjVar * getFunction(const PAGEdge *pagEdge) const
Get the function which SVFIR Edge located.
Definition MemRegion.h:437
NodeBS & CollectPtsChain(NodeID id)
Map< const FunObjVar *, MRSet > FunToMRsMap
Map a function to its region set.
Definition MemRegion.h:153
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:180
OrderedSet< NodeBS, SVFUtil::equalNodeBS > PointsToList
Definition MemRegion.h:147
Map< const FunObjVar *, NodeBS > FunToPointsToMap
Definition MemRegion.h:148
MRSet & getMRSet()
Definition MemRegion.h:184
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:174
void addCPtsToCallSiteRefs(NodeBS &cpts, const CallICFGNode *cs)
Definition MemRegion.h:355
NodeBS getRefInfoForCall(const CallICFGNode *cs)
bool hasRefSideEffectOfCallSite(const CallICFGNode *cs)
Has indirect refs of a callsite.
Definition MemRegion.h:410
OrderedSet< const MemRegion *, MemRegion::equalMemRegion > MRSet
Get typedef from Pointer Analysis.
Definition MemRegion.h:145
Map< const FunObjVar *, PointsToList > FunToPointsTosMap
Definition MemRegion.h:149
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:190
bool hasCPtsList(const FunObjVar *fun) const
Definition MemRegion.h:365
virtual void getMRsForCallSiteRef(MRSet &aliasMRs, const NodeBS &cpts, const FunObjVar *)
Get memory regions for call site ref according to cpts.
Definition MemRegion.h:328
bool hasSVFStmtList(const ICFGNode *icfgNode)
Whether this instruction has SVFIR Edge.
Map< NodeID, NodeBS > NodeToPTSSMap
Definition MemRegion.h:177
MRSet & getLoadMRSet(const LoadStmt *load)
Definition MemRegion.h:449
PAGEdgeToFunMap pagEdgeToFunMap
Map a PAGEdge to its fun.
Definition MemRegion.h:228
PtsToRepPtsSetMap cptsToRepCPtsMap
Map a condition pts to its rep conditional pts (super set points-to)
Definition MemRegion.h:282
void addCPtsToCallSiteMods(NodeBS &cpts, const CallICFGNode *cs)
Definition MemRegion.h:360
virtual void getMRsForLoad(MRSet &aliasMRs, const NodeBS &cpts, const FunObjVar *)
Get memory regions for a load statement according to cpts.
Definition MemRegion.h:321
CallSiteToPointsToMap callsiteToModPointsToMap
Map a callsite to it mods cpts set.
Definition MemRegion.h:223
SVFStmtList & getSVFStmtsFromInst(const ICFGNode *node)
Given an instruction, get all its the PAGEdge (statement) in sequence.
virtual void partitionMRs()
Partition regions.
ModRefInfo getModRefInfo(const CallICFGNode *cs)
void destroy()
Clean up memory.
Definition MemRegion.cpp:52
const NodeBS & getRefSideEffectOfCallSite(const CallICFGNode *cs)
Get indirect refs of a callsite.
Definition MemRegion.h:400
CallSiteToPointsToMap csToRefsMap
Map a callsite to its indirect uses of memory objects.
Definition MemRegion.h:235
NodeBS & getCallSiteArgsPts(const CallICFGNode *cs)
Return the pts chain of all callsite arguments.
Definition MemRegion.h:259
Map< const LoadStmt *, NodeBS > LoadsToPointsToMap
Map loads/stores/callsites to their cpts set.
Definition MemRegion.h:164
bool isNonLocalObject(NodeID id, const FunObjVar *curFun) const
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:215
MRSet & getCallSiteModMRSet(const CallICFGNode *cs)
Definition MemRegion.h:469
NodeBS getModInfoForCall(const CallICFGNode *cs)
getModRefInfo APIs
const NodeBS & getModSideEffectOfCallSite(const CallICFGNode *cs)
Get indirect mods of a callsite.
Definition MemRegion.h:405
SCCDetection< CallGraph * > SCC
Call Graph SCC.
Definition MemRegion.h:182
virtual void modRefAnalysis(CallGraphNode *callGraphNode, WorkList &worklist)
Mod-Ref analysis for callsite invoking this callGraphNode.
bool hasRefMRSet(const CallICFGNode *cs)
Definition MemRegion.h:457
NodeBS & getCallSiteRetPts(const CallICFGNode *cs)
Return the pts chain of the return parameter of the callsite.
Definition MemRegion.h:264
NodeBS allGlobals
All global variable SVFIR node ids.
Definition MemRegion.h:247
LoadsToPointsToMap loadsToPointsToMap
Map a load SVFIR Edge to its CPts set map.
Definition MemRegion.h:217
FunToPointsToMap funToRefsMap
Map a function to its indirect uses of memory objects.
Definition MemRegion.h:231
MRSet memRegSet
A set of All memory regions.
Definition MemRegion.h:280
Map< const CallICFGNode *, MRSet > CallSiteToMRsMap
Definition MemRegion.h:159
Map< const StoreStmt *, MRSet > StoresToMRsMap
Definition MemRegion.h:158
BVDataPTAImpl * pta
Definition MemRegion.h:201
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:239
u32_t getMRNum() const
Definition MemRegion.h:422
Memory Region class.
Definition MemRegion.h:60
MemRegion(const NodeBS &cp)
Constructor.
Definition MemRegion.h:72
static u32_t totalMRNum
region ID 0 is reserved
Definition MemRegion.h:66
~MemRegion()
Destructor.
Definition MemRegion.h:77
const NodeBS cptsSet
Definition MemRegion.h:68
MRID getMRID() const
Return memory region ID.
Definition MemRegion.h:82
const NodeBS & getPointsTo() const
Return points-to.
Definition MemRegion.h:87
u32_t getRegionSize() const
Return memory object number inside a region.
Definition MemRegion.h:126
std::string dumpStr() const
Dump string.
Definition MemRegion.h:97
bool operator==(const MemRegion *rhs) const
Operator== overriding.
Definition MemRegion.h:92
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:127
for isBitcode
Definition BasicTypes.h:70
NodeID MRVERID
Definition MemRegion.h:55
NodeID MRID
Definition MemRegion.h:54
NodeID MRVERSION
Definition MemRegion.h:56
ModRefInfo
Definition SVFType.h:611
u32_t NodeID
Definition GeneralType.h:76
llvm::IRBuilder IRBuilder
Definition BasicTypes.h:76
unsigned u32_t
Definition GeneralType.h:67
bool operator()(const MemRegion *lhs, const MemRegion *rhs) const
Definition MemRegion.h:118