Static Value-Flow Analysis
Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes | List of all members
SVF::CDGBuilder Class Reference

#include <CDGBuilder.h>

Public Member Functions

 CDGBuilder ()
 constructor More...
 
 ~CDGBuilder ()
 destructor More...
 
void build ()
 start here More...
 
void buildControlDependence (const SVFModule *svfgModule)
 build control dependence for each function More...
 
void buildICFGNodeControlMap ()
 build map at icfg node level More...
 

Private Member Functions

void extractNodesBetweenPdomNodes (const SVFBasicBlock *succ, const SVFBasicBlock *LCA, std::vector< const SVFBasicBlock * > &tgtNodes)
 extract nodes between two nodes in pdom tree More...
 
void dfsNodesBetweenPdomNodes (const SVFBasicBlock *cur, const SVFBasicBlock *tgt, std::vector< const SVFBasicBlock * > &path, std::vector< const SVFBasicBlock * > &tgtNodes, SVFLoopAndDomInfo *ld)
 
s64_t getBBSuccessorBranchID (const SVFBasicBlock *BB, const SVFBasicBlock *Succ)
 
void updateMap (const SVFBasicBlock *pred, const SVFBasicBlock *bb, s32_t pos)
 update map More...
 

Static Private Member Functions

static void extractBBS (const SVFFunction *func, Map< const SVFBasicBlock *, std::vector< const SVFBasicBlock * >> &res)
 extract basic block edges to be processed More...
 

Private Attributes

CDG_controlDG
 
Map< const SVFBasicBlock *, Map< const SVFBasicBlock *, Set< s32_t > > > _svfcontrolMap
 map a basicblock to its controlling BBs (position, set of BBs) More...
 
Map< const SVFBasicBlock *, Map< const SVFBasicBlock *, Set< s32_t > > > _svfdependentOnMap
 map a basicblock to its dependent on BBs (position, set of BBs) More...
 
Map< const ICFGNode *, Map< const ICFGNode *, Set< s32_t > > > _nodeControlMap
 map an ICFG node to its controlling ICFG nodes (position, set of Nodes) More...
 
Map< const ICFGNode *, Map< const ICFGNode *, Set< s32_t > > > _nodeDependentOnMap
 map an ICFG node to its dependent on ICFG nodes (position, set of Nodes) More...
 

Detailed Description

Definition at line 38 of file CDGBuilder.h.

Constructor & Destructor Documentation

◆ CDGBuilder()

SVF::CDGBuilder::CDGBuilder ( )
inline

constructor

Definition at line 43 of file CDGBuilder.h.

44  {
45 
46  }
CDG * _controlDG
Definition: CDGBuilder.h:98
static CDG * getCDG()
Singleton design here to make sure we only have one instance during any analysis.
Definition: CDG.h:166

◆ ~CDGBuilder()

SVF::CDGBuilder::~CDGBuilder ( )
inline

destructor

Definition at line 49 of file CDGBuilder.h.

50  {
51 
52  }

Member Function Documentation

◆ build()

void CDGBuilder::build ( )

start here

Start here

Definition at line 77 of file CDGBuilder.cpp.

78 {
79  if (_controlDG->getTotalNodeNum() > 0)
80  return;
81  PAG *pag = PAG::getPAG();
84 }
void buildControlDependence(const SVFModule *svfgModule)
build control dependence for each function
Definition: CDGBuilder.cpp:124
void buildICFGNodeControlMap()
build map at icfg node level
Definition: CDGBuilder.cpp:188
u32_t getTotalNodeNum() const
Get total number of node/edge.
Definition: GenericGraph.h:680
static SVFIR * getPAG(bool buildFromFile=false)
Singleton design here to make sure we only have one instance during any analysis.
Definition: SVFIR.h:115
SVFModule * getModule()
Definition: SVFIR.h:161

◆ buildControlDependence()

void CDGBuilder::buildControlDependence ( const SVFModule svfgModule)

build control dependence for each function

Build control dependence for each function

(1) construct CFG for each function (2) extract basic block edges (pred->succ) on the CFG to be processed succ does not post-dominates pred (!postDT->dominates(succ, pred)) (3) extract nodes from succ to the least common ancestor LCA of pred and succ including LCA if LCA is pred, excluding LCA if LCA is not pred

Parameters
svfgModule

Definition at line 124 of file CDGBuilder.cpp.

125 {
126  PTACallGraph* svfirCallGraph = PAG::getPAG()->getCallGraph();
127  for (const auto& item: *svfirCallGraph)
128  {
129  const SVFFunction *svfFun = (item.second)->getFunction();
130  if (SVFUtil::isExtCall(svfFun)) continue;
131  // extract basic block edges to be processed
133  extractBBS(svfFun, BBS);
134 
135  for (const auto &item: BBS)
136  {
137  const SVFBasicBlock *pred = item.first;
138  // for each bb pair
139  for (const SVFBasicBlock *succ: item.second)
140  {
141  const SVFBasicBlock *SVFLCA = const_cast<SVFFunction *>(svfFun)->
142  getLoopAndDomInfo()->findNearestCommonPDominator(pred, succ);
143  std::vector<const SVFBasicBlock *> tgtNodes;
144  // no common ancestor, may be exit()
145  if (SVFLCA == NULL)
146  tgtNodes.push_back(succ);
147  else
148  {
149  if (SVFLCA == pred) tgtNodes.push_back(SVFLCA);
150  // from succ to LCA
151  extractNodesBetweenPdomNodes(succ, SVFLCA, tgtNodes);
152  }
153 
154  s64_t pos = getBBSuccessorBranchID(pred, succ);
155  for (const SVFBasicBlock *bb: tgtNodes)
156  {
157  updateMap(pred, bb, pos);
158  }
159  }
160  }
161  }
162 }
return NULL
Definition: cJSON.cpp:1173
cJSON * item
Definition: cJSON.h:222
void updateMap(const SVFBasicBlock *pred, const SVFBasicBlock *bb, s32_t pos)
update map
Definition: CDGBuilder.h:90
void extractNodesBetweenPdomNodes(const SVFBasicBlock *succ, const SVFBasicBlock *LCA, std::vector< const SVFBasicBlock * > &tgtNodes)
extract nodes between two nodes in pdom tree
Definition: CDGBuilder.cpp:65
static void extractBBS(const SVFFunction *func, Map< const SVFBasicBlock *, std::vector< const SVFBasicBlock * >> &res)
extract basic block edges to be processed
Definition: CDGBuilder.cpp:171
s64_t getBBSuccessorBranchID(const SVFBasicBlock *BB, const SVFBasicBlock *Succ)
Definition: CDGBuilder.cpp:87
PTACallGraph * getCallGraph()
Definition: SVFIR.h:192
const SVFFunction * getFunction(const std::string &name)
Get the corresponding Function based on its name.
Definition: LLVMUtil.cpp:412
bool isExtCall(const SVFFunction *fun)
Definition: SVFUtil.h:278
std::unordered_map< Key, Value, Hash, KeyEqual, Allocator > Map
Definition: GeneralType.h:101
signed long long s64_t
Definition: GeneralType.h:49

◆ buildICFGNodeControlMap()

void CDGBuilder::buildICFGNodeControlMap ( )

build map at icfg node level

Build map at ICFG node level

Definition at line 188 of file CDGBuilder.cpp.

189 {
190  for (const auto &it: _svfcontrolMap)
191  {
192  for (const auto &it2: it.second)
193  {
194  const SVFBasicBlock *controllingBB = it2.first;
195  const ICFGNode *controlNode = it.first->getICFGNodeList().back();
196  if (const CallICFGNode* callNode =
197  SVFUtil::dyn_cast<CallICFGNode>(controlNode))
198  {
199  // not a branch statement:
200  // invoke void %3(ptr noundef nonnull align 8 dereferenceable(8) %1, ptr noundef %2)
201  // to label %invoke.cont1 unwind label %lpad
202  controlNode = callNode->getRetICFGNode();
203  }
204  if (!controlNode) continue;
205  // controlNode control at pos
206  for (const auto &controllee: controllingBB->getICFGNodeList())
207  {
208  _nodeControlMap[controlNode][controllee].insert(it2.second.begin(), it2.second.end());
209  _nodeDependentOnMap[controllee][controlNode].insert(it2.second.begin(), it2.second.end());
210  for (s32_t pos: it2.second)
211  {
212  if (const IntraICFGNode* intraNode =
213  dyn_cast<IntraICFGNode>(controlNode))
214  {
215  assert(intraNode->getSVFStmts().size() == 1 &&
216  "not a branch stmt?");
217  const SVFVar* condition =
218  SVFUtil::cast<BranchStmt>(
219  intraNode->getSVFStmts().front())
220  ->getCondition();
221  _controlDG->addCDGEdgeFromSrcDst(controlNode, controllee,
222  condition,
223  pos);
224  }
225  else
226  {
227  // not a branch statement:
228  // invoke void %3(ptr noundef nonnull align 8 dereferenceable(8) %1, ptr noundef %2)
229  // to label %invoke.cont1 unwind label %lpad
230  SVFIR* pag = PAG::getPAG();
232  controlNode, controllee,
233  pag->getGNode(pag->getNullPtr()), pos);
234  }
235 
236  }
237  }
238  }
239  }
240 }
Map< const ICFGNode *, Map< const ICFGNode *, Set< s32_t > > > _nodeControlMap
map an ICFG node to its controlling ICFG nodes (position, set of Nodes)
Definition: CDGBuilder.h:101
Map< const SVFBasicBlock *, Map< const SVFBasicBlock *, Set< s32_t > > > _svfcontrolMap
map a basicblock to its controlling BBs (position, set of BBs)
Definition: CDGBuilder.h:99
Map< const ICFGNode *, Map< const ICFGNode *, Set< s32_t > > > _nodeDependentOnMap
map an ICFG node to its dependent on ICFG nodes (position, set of Nodes)
Definition: CDGBuilder.h:102
void addCDGEdgeFromSrcDst(const ICFGNode *src, const ICFGNode *dst, const SVFVar *pNode, s32_t branchID)
Add CDG edges from nodeid pair.
Definition: CDG.cpp:35
NodeType * getGNode(NodeID id) const
Get a node.
Definition: GenericGraph.h:653
NodeID getNullPtr() const
Definition: IRGraph.h:173
const std::vector< const ICFGNode * > & getICFGNodeList() const
Definition: SVFValue.h:569
signed s32_t
Definition: GeneralType.h:47

◆ dfsNodesBetweenPdomNodes()

void CDGBuilder::dfsNodesBetweenPdomNodes ( const SVFBasicBlock cur,
const SVFBasicBlock tgt,
std::vector< const SVFBasicBlock * > &  path,
std::vector< const SVFBasicBlock * > &  tgtNodes,
SVFLoopAndDomInfo ld 
)
private

Definition at line 35 of file CDGBuilder.cpp.

40 {
41  path.push_back(cur);
42  if (cur == tgt)
43  {
44  tgtNodes.insert(tgtNodes.end(), path.begin() + 1, path.end());
45  }
46  else
47  {
48  auto it = ld->getPostDomTreeMap().find(cur);
49  for (const auto &nxt: it->second)
50  {
51  dfsNodesBetweenPdomNodes(nxt, tgt, path, tgtNodes, ld);
52  }
53  }
54  path.pop_back();
55 }
void dfsNodesBetweenPdomNodes(const SVFBasicBlock *cur, const SVFBasicBlock *tgt, std::vector< const SVFBasicBlock * > &path, std::vector< const SVFBasicBlock * > &tgtNodes, SVFLoopAndDomInfo *ld)
Definition: CDGBuilder.cpp:35
const Map< const SVFBasicBlock *, BBSet > & getPostDomTreeMap() const
Definition: SVFValue.h:108

◆ extractBBS()

void CDGBuilder::extractBBS ( const SVFFunction func,
Map< const SVFBasicBlock *, std::vector< const SVFBasicBlock * >> &  res 
)
staticprivate

extract basic block edges to be processed

(2) extract basic block edges on the CFG (pred->succ) to be processed succ does not post-dominates pred (!postDT->dominates(succ, pred))

Parameters
func
res

Definition at line 171 of file CDGBuilder.cpp.

173 {
174  for (const auto &bb: *func)
175  {
176  for (const auto &succ: bb->getSuccessors())
177  {
178  if (func->postDominate(succ, bb))
179  continue;
180  res[bb].push_back(succ);
181  }
182  }
183 }

◆ extractNodesBetweenPdomNodes()

void CDGBuilder::extractNodesBetweenPdomNodes ( const SVFBasicBlock succ,
const SVFBasicBlock LCA,
std::vector< const SVFBasicBlock * > &  tgtNodes 
)
private

extract nodes between two nodes in pdom tree

(3) extract nodes from succ to the least common ancestor LCA of pred and succ including LCA if LCA is pred, excluding LCA if LCA is not pred

Parameters
succ
LCA
tgtNodes

Definition at line 65 of file CDGBuilder.cpp.

67 {
68  if (succ == LCA) return;
69  std::vector<const SVFBasicBlock *> path;
70  SVFLoopAndDomInfo *ld = const_cast<SVFFunction *>(LCA->getFunction())->getLoopAndDomInfo();
71  dfsNodesBetweenPdomNodes(LCA, succ, path, tgtNodes, ld);
72 }
const SVFFunction * getFunction() const
Definition: SVFValue.h:589

◆ getBBSuccessorBranchID()

s64_t CDGBuilder::getBBSuccessorBranchID ( const SVFBasicBlock BB,
const SVFBasicBlock Succ 
)
private

Definition at line 87 of file CDGBuilder.cpp.

88 {
89  ICFG *icfg = PAG::getPAG()->getICFG();
90  assert(!BB->getICFGNodeList().empty() && "empty bb?");
91  const ICFGNode *pred = BB->back();
92  if (const CallICFGNode* callNode = dyn_cast<CallICFGNode>(pred))
93  {
94  // not a branch statement:
95  // invoke void %3(ptr noundef nonnull align 8 dereferenceable(8) %1, ptr noundef %2)
96  // to label %invoke.cont1 unwind label %lpad
97  pred = callNode->getRetICFGNode();
98  }
99  const ICFGEdge *edge = icfg->getICFGEdge(pred, Succ->front(), ICFGEdge::ICFGEdgeK::IntraCF);
100  if (const IntraCFGEdge *intraEdge = SVFUtil::dyn_cast<IntraCFGEdge>(edge))
101  {
102  if(intraEdge->getCondition())
103  return intraEdge->getSuccessorCondValue();
104  else
105  return 0;
106  }
107  else
108  {
109  assert(false && "not intra edge?");
110  abort();
111  }
112 }
Definition: ICFG.h:48
ICFGEdge * getICFGEdge(const ICFGNode *src, const ICFGNode *dst, ICFGEdge::ICFGEdgeK kind)
Get a SVFG edge according to src and dst.
Definition: ICFG.cpp:303
const ICFGNode * front() const
Definition: SVFValue.h:594
const ICFGNode * back() const
Definition: SVFValue.h:600
ICFG * getICFG() const
Definition: SVFIR.h:171

◆ updateMap()

void SVF::CDGBuilder::updateMap ( const SVFBasicBlock pred,
const SVFBasicBlock bb,
s32_t  pos 
)
inlineprivate

update map

Definition at line 90 of file CDGBuilder.h.

91  {
92  _svfcontrolMap[pred][bb].insert(pos);
93  _svfdependentOnMap[bb][pred].insert(pos);
94  }
Map< const SVFBasicBlock *, Map< const SVFBasicBlock *, Set< s32_t > > > _svfdependentOnMap
map a basicblock to its dependent on BBs (position, set of BBs)
Definition: CDGBuilder.h:100

Member Data Documentation

◆ _controlDG

CDG* SVF::CDGBuilder::_controlDG
private

Definition at line 98 of file CDGBuilder.h.

◆ _nodeControlMap

Map<const ICFGNode *, Map<const ICFGNode *, Set<s32_t> > > SVF::CDGBuilder::_nodeControlMap
private

map an ICFG node to its controlling ICFG nodes (position, set of Nodes)

Definition at line 101 of file CDGBuilder.h.

◆ _nodeDependentOnMap

Map<const ICFGNode *, Map<const ICFGNode *, Set<s32_t> > > SVF::CDGBuilder::_nodeDependentOnMap
private

map an ICFG node to its dependent on ICFG nodes (position, set of Nodes)

Definition at line 102 of file CDGBuilder.h.

◆ _svfcontrolMap

Map<const SVFBasicBlock *, Map<const SVFBasicBlock *, Set<s32_t> > > SVF::CDGBuilder::_svfcontrolMap
private

map a basicblock to its controlling BBs (position, set of BBs)

Definition at line 99 of file CDGBuilder.h.

◆ _svfdependentOnMap

Map<const SVFBasicBlock *, Map<const SVFBasicBlock *, Set<s32_t> > > SVF::CDGBuilder::_svfdependentOnMap
private

map a basicblock to its dependent on BBs (position, set of BBs)

Definition at line 100 of file CDGBuilder.h.


The documentation for this class was generated from the following files: