Static Value-Flow Analysis
Loading...
Searching...
No Matches
CDGBuilder.cpp
Go to the documentation of this file.
1//===----- CDGBuilder.cpp -- Control Dependence Graph Builder -------------//
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 * CDGBuilder.cpp
25 *
26 * Created on: Sep 27, 2023
27 * Author: Xiao Cheng
28 */
29#include "Util/CDGBuilder.h"
30#include "Graphs/CallGraph.h"
31
32using namespace SVF;
33using namespace SVFUtil;
34
36 const SVFBasicBlock *tgt,
37 std::vector<const SVFBasicBlock *> &path,
38 std::vector<const SVFBasicBlock *> &tgtNodes,
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}
56
64void
66 std::vector<const SVFBasicBlock *> &tgtNodes)
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}
73
78{
79 if (_controlDG->getTotalNodeNum() > 0)
80 return;
83}
84
85
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}
112
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}
161
162
170 Map<const SVF::SVFBasicBlock *, std::vector<const SVFBasicBlock *>> &res)
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}
183
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}
cJSON * item
Definition cJSON.h:222
void buildControlDependence()
build control dependence for each function
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
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
void dfsNodesBetweenPdomNodes(const SVFBasicBlock *cur, const SVFBasicBlock *tgt, std::vector< const SVFBasicBlock * > &path, std::vector< const SVFBasicBlock * > &tgtNodes, SVFLoopAndDomInfo *ld)
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
void buildICFGNodeControlMap()
build map at icfg node level
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
void build()
start here
s64_t getBBSuccessorBranchID(const SVFBasicBlock *BB, const SVFBasicBlock *Succ)
bool postDominate(const SVFBasicBlock *bbKey, const SVFBasicBlock *bbValue) const
SVFLoopAndDomInfo * getLoopAndDomInfo() const
ICFGEdge * getICFGEdge(const ICFGNode *src, const ICFGNode *dst, ICFGEdge::ICFGEdgeK kind)
Get a SVFG edge according to src and dst.
Definition ICFG.cpp:312
NodeID getNullPtr() const
Definition PAGBase.h:262
Definition PAG.h:46
static PAG * getPAG(bool buildFromFile=false)
Singleton design here to make sure we only have one instance during any analysis.
Definition PAG.h:121
const SVFVar * getSVFVar(NodeID id) const
ObjVar/GepObjVar/BaseObjVar.
Definition PAG.h:136
ICFG * getICFG() const
Definition PAG.h:232
const std::vector< const ICFGNode * > & getICFGNodeList() const
const FunObjVar * getFunction() const
const ICFGNode * front() const
const ICFGNode * back() const
std::vector< const SVFBasicBlock * > getSuccessors() const
const Map< const SVFBasicBlock *, BBSet > & getPostDomTreeMap() const
#define NULL
Definition extapi.c:5
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
bool isExtCall(const FunObjVar *fun)
Definition SVFUtil.cpp:524
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
for isBitcode
Definition BasicTypes.h:70
std::unordered_map< Key, Value, Hash, KeyEqual, Allocator > Map
Definition GeneralType.h:56
signed s32_t
Definition GeneralType.h:68
signed long long s64_t
Definition GeneralType.h:70