Static Value-Flow Analysis
Loading...
Searching...
No Matches
SVF::CDGBuilder Class Reference

#include <CDGBuilder.h>

Public Member Functions

 CDGBuilder ()
 constructor
 ~CDGBuilder ()
 destructor
void build ()
 start here
void buildControlDependence ()
 build control dependence for each function
void buildICFGNodeControlMap ()
 build map at icfg node level

Private Member Functions

void extractNodesBetweenPdomNodes (const SVFBasicBlock *succ, const SVFBasicBlock *LCA, std::vector< const SVFBasicBlock * > &tgtNodes)
 extract nodes between two nodes in pdom tree
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

Static Private Member Functions

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

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)
Map< const SVFBasicBlock *, Map< const SVFBasicBlock *, Set< s32_t > > > _svfdependentOnMap
 map a basicblock to its dependent on BBs (position, set of BBs)
Map< const ICFGNode *, Map< const ICFGNode *, Set< s32_t > > > _nodeControlMap
 map an ICFG node to its controlling ICFG nodes (position, set of Nodes)
Map< const ICFGNode *, Map< const ICFGNode *, Set< s32_t > > > _nodeDependentOnMap
 map an ICFG node to its dependent on ICFG nodes (position, set of Nodes)

Detailed Description

Definition at line 44 of file CDGBuilder.h.

Constructor & Destructor Documentation

◆ CDGBuilder()

SVF::CDGBuilder::CDGBuilder ( )
inline

constructor

Definition at line 49 of file CDGBuilder.h.

50 {
51
52 }
static CDG * getCDG()
Singleton design here to make sure we only have one instance during any analysis.
Definition CDG.h:172

◆ ~CDGBuilder()

SVF::CDGBuilder::~CDGBuilder ( )
inline

destructor

Definition at line 55 of file CDGBuilder.h.

56 {
57
58 }

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;
83}
void buildControlDependence()
build control dependence for each function
void buildICFGNodeControlMap()
build map at icfg node level

◆ buildControlDependence()

void CDGBuilder::buildControlDependence ( )

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 123 of file CDGBuilder.cpp.

124{
125 for (const auto& item: *(PAG::getPAG()->getCallGraph()))
126 {
127 const FunObjVar *svfFun = (item.second)->getFunction();
128 if (SVFUtil::isExtCall(svfFun)) continue;
129 // extract basic block edges to be processed
131 extractBBS(svfFun, BBS);
132
133 for (const auto &item: BBS)
134 {
135 const SVFBasicBlock *pred = item.first;
136 // for each bb pair
137 for (const SVFBasicBlock *succ: item.second)
138 {
139 const SVFBasicBlock *SVFLCA = const_cast<FunObjVar *>(svfFun)->
140 getLoopAndDomInfo()->findNearestCommonPDominator(pred, succ);
141 std::vector<const SVFBasicBlock *> tgtNodes;
142 // no common ancestor, may be exit()
143 if (SVFLCA == NULL)
144 tgtNodes.push_back(succ);
145 else
146 {
147 if (SVFLCA == pred) tgtNodes.push_back(SVFLCA);
148 // from succ to LCA
149 extractNodesBetweenPdomNodes(succ, SVFLCA, tgtNodes);
150 }
151
152 s64_t pos = getBBSuccessorBranchID(pred, succ);
153 for (const SVFBasicBlock *bb: tgtNodes)
154 {
155 updateMap(pred, bb, pos);
156 }
157 }
158 }
159 }
160}
cJSON * item
Definition cJSON.h:222
static void extractBBS(const FunObjVar *func, Map< const SVFBasicBlock *, std::vector< const SVFBasicBlock * > > &res)
extract basic block edges to be processed
void updateMap(const SVFBasicBlock *pred, const SVFBasicBlock *bb, s32_t pos)
update map
Definition CDGBuilder.h:96
void extractNodesBetweenPdomNodes(const SVFBasicBlock *succ, const SVFBasicBlock *LCA, std::vector< const SVFBasicBlock * > &tgtNodes)
extract nodes between two nodes in pdom tree
s64_t getBBSuccessorBranchID(const SVFBasicBlock *BB, const SVFBasicBlock *Succ)
static PAG * getPAG(bool buildFromFile=false)
Singleton design here to make sure we only have one instance during any analysis.
Definition PAG.h:121
#define NULL
Definition extapi.c:5
bool isExtCall(const FunObjVar *fun)
Definition SVFUtil.cpp:524
std::unordered_map< Key, Value, Hash, KeyEqual, Allocator > Map
Definition GeneralType.h:56
signed long long s64_t
Definition GeneralType.h:70

◆ buildICFGNodeControlMap()

void CDGBuilder::buildICFGNodeControlMap ( )

build map at icfg node level

Build map at ICFG node level

Definition at line 187 of file CDGBuilder.cpp.

188{
189 for (const auto &it: _svfcontrolMap)
190 {
191 for (const auto &it2: it.second)
192 {
193 const SVFBasicBlock *controllingBB = it2.first;
194 const ICFGNode *controlNode = it.first->getICFGNodeList().back();
195 if (const CallICFGNode* callNode =
197 {
198 // not a branch statement:
199 // invoke void %3(ptr noundef nonnull align 8 dereferenceable(8) %1, ptr noundef %2)
200 // to label %invoke.cont1 unwind label %lpad
201 controlNode = callNode->getRetICFGNode();
202 }
203 if (!controlNode) continue;
204 // controlNode control at pos
205 for (const auto &controllee: controllingBB->getICFGNodeList())
206 {
207 _nodeControlMap[controlNode][controllee].insert(it2.second.begin(), it2.second.end());
208 _nodeDependentOnMap[controllee][controlNode].insert(it2.second.begin(), it2.second.end());
209 for (s32_t pos: it2.second)
210 {
211 if (const IntraICFGNode* intraNode =
212 dyn_cast<IntraICFGNode>(controlNode))
213 {
214 assert(intraNode->getSVFStmts().size() == 1 &&
215 "not a branch stmt?");
216 const SVFVar* condition =
218 intraNode->getSVFStmts().front())
219 ->getCondition();
220 _controlDG->addCDGEdgeFromSrcDst(controlNode, controllee,
221 condition,
222 pos);
223 }
224 else
225 {
226 // not a branch statement:
227 // invoke void %3(ptr noundef nonnull align 8 dereferenceable(8) %1, ptr noundef %2)
228 // to label %invoke.cont1 unwind label %lpad
229 PAG* pag = PAG::getPAG();
230 _controlDG->addCDGEdgeFromSrcDst(
231 controlNode, controllee,
232 pag->getSVFVar(pag->getNullPtr()), pos);
233 }
234
235 }
236 }
237 }
238 }
239}
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:107
Map< const SVFBasicBlock *, Map< const SVFBasicBlock *, Set< s32_t > > > _svfcontrolMap
map a basicblock to its controlling BBs (position, set of BBs)
Definition CDGBuilder.h:105
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:108
NodeID getNullPtr() const
Definition PAGBase.h:262
const SVFVar * getSVFVar(NodeID id) const
ObjVar/GepObjVar/BaseObjVar.
Definition PAG.h:136
const std::vector< const ICFGNode * > & getICFGNodeList() const
std::enable_if_t<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type > cast(const Y &Val)
Definition Casting.h:360
LLVM_NODISCARD std::enable_if_t<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type > dyn_cast(const Y &Val)
Definition Casting.h:405
signed s32_t
Definition GeneralType.h:68

◆ 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)
const Map< const SVFBasicBlock *, BBSet > & getPostDomTreeMap() const

◆ extractBBS()

void CDGBuilder::extractBBS ( const FunObjVar * 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 169 of file CDGBuilder.cpp.

171{
172 for (const auto &it: *func)
173 {
174 const SVFBasicBlock* bb = it.second;
175 for (const auto &succ: bb->getSuccessors())
176 {
177 if (func->postDominate(succ, bb))
178 continue;
179 res[bb].push_back(succ);
180 }
181 }
182}
std::vector< const SVFBasicBlock * > getSuccessors() const

◆ 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<FunObjVar *>(LCA->getFunction())->getLoopAndDomInfo();
71 dfsNodesBetweenPdomNodes(LCA, succ, path, tgtNodes, ld);
72}
const FunObjVar * getFunction() const

◆ getBBSuccessorBranchID()

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

Definition at line 86 of file CDGBuilder.cpp.

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

◆ updateMap()

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

update map

Definition at line 96 of file CDGBuilder.h.

97 {
98 _svfcontrolMap[pred][bb].insert(pos);
99 _svfdependentOnMap[bb][pred].insert(pos);
100 }
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:106

Member Data Documentation

◆ _controlDG

CDG* SVF::CDGBuilder::_controlDG
private

Definition at line 104 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 107 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 108 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 105 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 106 of file CDGBuilder.h.


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