Static Value-Flow Analysis
Loading...
Searching...
No Matches
svf-ex.cpp
Go to the documentation of this file.
1//===- svf-ex.cpp -- A driver example of SVF-------------------------------------//
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 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 General Public License for more details.
17
18// You should have received a copy of the GNU General Public License
19// along with this program. If not, see <http://www.gnu.org/licenses/>.
20//
21//===-----------------------------------------------------------------------===//
22
23/*
24 // A driver program of SVF including usages of SVF APIs
25 //
26 // Author: Yulei Sui,
27 */
28
30#include "Graphs/SVFG.h"
31#include "MSSA/SVFGBuilder.h"
32#include "SVF-LLVM/LLVMUtil.h"
34#include "Util/CommandLine.h"
35#include "Util/Options.h"
36#include "WPA/Andersen.h"
37#include <llvm/Support/ManagedStatic.h>
38
39using namespace llvm;
40using namespace std;
41using namespace SVF;
42
47{
48 return pta->alias(v1->getId(), v2->getId());
49}
50
54std::string printPts(PointerAnalysis* pta, const SVFVar* svfval)
55{
56
57 std::string str;
59
60 NodeID pNodeId = svfval->getId();
61 const PointsTo& pts = pta->getPts(pNodeId);
62 for (PointsTo::iterator ii = pts.begin(), ie = pts.end();
63 ii != ie; ii++)
64 {
65 rawstr << " " << *ii << " ";
66 PAGNode* targetObj = pta->getPAG()->getGNode(*ii);
67 rawstr << "(" << targetObj->toString() << ")\t ";
68 }
69
70 return rawstr.str();
71
72}
76void traverseOnICFG(ICFG* icfg, const ICFGNode* iNode)
77{
80 worklist.push(iNode);
81
83 while (!worklist.empty())
84 {
85 const ICFGNode* vNode = worklist.pop();
86 for (ICFGNode::const_iterator it = vNode->OutEdgeBegin(), eit =
87 vNode->OutEdgeEnd(); it != eit; ++it)
88 {
89 ICFGEdge* edge = *it;
90 ICFGNode* succNode = edge->getDstNode();
91 if (visited.find(succNode) == visited.end())
92 {
93 visited.insert(succNode);
94 worklist.push(succNode);
95 }
96 }
97 }
98}
99
100void dummyVisit(const VFGNode* node)
101{
102
103}
107void traverseOnVFG(const SVFG* vfg, const ValVar* svfval)
108{
109 if (!vfg->hasDefSVFGNode(svfval))
110 return;
111 const VFGNode* vNode = vfg->getDefSVFGNode(svfval);
113 Set<const VFGNode*> visited;
114 worklist.push(vNode);
115
117 while (!worklist.empty())
118 {
119 const VFGNode* vNode = worklist.pop();
120 for (VFGNode::const_iterator it = vNode->OutEdgeBegin(), eit =
121 vNode->OutEdgeEnd(); it != eit; ++it)
122 {
123 VFGEdge* edge = *it;
124 VFGNode* succNode = edge->getDstNode();
125 if (visited.find(succNode) == visited.end())
126 {
127 visited.insert(succNode);
128 worklist.push(succNode);
129 }
130 }
131 }
132
134 for(Set<const VFGNode*>::const_iterator it = visited.begin(), eit = visited.end(); it!=eit; ++it)
135 {
136 const VFGNode* node = *it;
137 dummyVisit(node);
141 }
142}
143
144int main(int argc, char ** argv)
145{
146
147 std::vector<std::string> moduleNameVec;
149 argc, argv, "Whole Program Points-to Analysis", "[options] <input-bitcode...>"
150 );
151
154
157 SVFIR* pag = builder.build();
158
161
162
164 CallGraph* callgraph = ander->getCallGraph();
165
167 ICFG* icfg = pag->getICFG();
168
170 VFG* vfg = new VFG(callgraph);
171
174 SVFG* svfg = svfBuilder.buildFullSVFG(ander);
175
177 if (Options::PTSPrint())
178 {
179 for (const auto& it : *svfg)
180 {
181 const SVFGNode* node = it.second;
182 if (node->getValue())
183 {
184 if (const ValVar* valVar = SVFUtil::dyn_cast<ValVar>(node->getValue()))
185 {
186 traverseOnVFG(svfg, valVar);
187 }
189 printPts(ander, node->getValue());
190 for (const SVFGEdge* edge : node->getOutEdges())
191 {
192 const SVFGNode* node2 = edge->getDstNode();
193 if (node2->getValue())
194 aliasQuery(ander, node->getValue(), node2->getValue());
195 }
196 }
197 }
198 }
199
201 if (Options::PTSPrint())
202 {
203 for (const auto& it : *icfg)
204 {
205 const ICFGNode* node = it.second;
206 traverseOnICFG(icfg, node);
207 }
208 }
209
210 // clean up memory
211 delete vfg;
214
217#if LLVM_VERSION_MAJOR < 21
218 llvm::llvm_shutdown();
219#endif
220 return 0;
221}
static std::vector< std::string > parseOptions(int argc, char *argv[], std::string description, std::string callFormat)
Definition CommandLine.h:75
static AndersenWaveDiff * createAndersenWaveDiff(SVFIR *_pag)
Create an singleton instance directly instead of invoking llvm pass manager.
Definition Andersen.h:408
static void releaseAndersenWaveDiff()
Definition Andersen.h:418
bool push(const Data &data)
Definition WorkList.h:180
bool empty() const
Definition WorkList.h:161
NodeType * getGNode(NodeID id) const
Get a node.
const GEdgeSetTy & getOutEdges() const
ICFGEdge::ICFGEdgeSetTy::const_iterator const_iterator
Definition ICFGNode.h:61
static LLVMModuleSet * getLLVMModuleSet()
Definition LLVMModule.h:133
static void releaseLLVMModuleSet()
Definition LLVMModule.h:140
static void preProcessBCs(std::vector< std::string > &moduleNameVec)
static void buildSVFModule(Module &mod)
void dumpModulesToFile(const std::string &suffix)
static const Option< bool > PTSPrint
Definition Options.h:112
virtual const PointsTo & getPts(NodeID ptr)=0
Get points-to targets of a pointer. It needs to be implemented in child class.
SVFIR * getPAG() const
CallGraph * getCallGraph() const
Return call graph.
virtual AliasResult alias(const SVFVar *V1, const SVFVar *V2)=0
Interface exposed to users of our pointer analysis, given Value infos.
bool hasDefSVFGNode(const ValVar *valVar) const
Given a valVar, return whether it has definition site.
Definition SVFG.h:177
const SVFGNode * getDefSVFGNode(const ValVar *valVar) const
Given a valVar, return its definition site.
Definition SVFG.h:171
static void releaseSVFIR()
Definition SVFIR.h:128
ICFG * getICFG() const
Definition SVFIR.h:231
virtual const std::string toString() const
Get string representation.
virtual const SVFVar * getValue() const
Return the corresponding LLVM value, if possible, nullptr otherwise.
Definition VFGNode.h:85
VFGEdge::VFGEdgeSetTy::const_iterator const_iterator
Definition VFGNode.h:55
Definition VFG.h:51
for isBitcode
Definition BasicTypes.h:70
u32_t NodeID
Definition GeneralType.h:76
AliasResult
Definition SVFType.h:619
llvm::IRBuilder IRBuilder
Definition BasicTypes.h:76
std::unordered_set< Key, Hash, KeyEqual, Allocator > Set
Definition GeneralType.h:51
void traverseOnICFG(ICFG *icfg, const ICFGNode *iNode)
Definition svf-ex.cpp:76
int main(int argc, char **argv)
Definition svf-ex.cpp:144
std::string printPts(PointerAnalysis *pta, const SVFVar *svfval)
Definition svf-ex.cpp:54
SVF::AliasResult aliasQuery(PointerAnalysis *pta, const SVFVar *v1, const SVFVar *v2)
Definition svf-ex.cpp:46
void dummyVisit(const VFGNode *node)
Definition svf-ex.cpp:100
void traverseOnVFG(const SVFG *vfg, const ValVar *svfval)
Definition svf-ex.cpp:107