Static Value-Flow Analysis
Loading...
Searching...
No Matches
PAGBuilderFromFile.cpp
Go to the documentation of this file.
1//===- PAGBuilderFromFile.cpp -- SVFIR 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 * PAGBuilderFromFile.cpp
25 *
26 * Created on: 20 Sep. 2018
27 * Author: 136884
28 */
29
31#include <fstream> // for PAGBuilderFromFile
32#include <string> // for PAGBuilderFromFile
33#include <sstream> // for PAGBuilderFromFile
34
35using namespace std;
36using namespace SVF;
37using namespace SVFUtil;
38static u32_t gepNodeNumIndex = 100000;
39
40/*
41 * You can build a SVFIR from a file written by yourself
42 *
43 * The file should follow the format:
44 * Node: nodeID Nodetype
45 * Edge: nodeID edgetype NodeID Offset
46 *
47 * like:
485 o
496 v
507 v
518 v
529 v
535 addr 6 0
546 gep 7 4
557 copy-ZEXT 8 0
566 store 8 0
578 load 9 0
58 */
59// for copy stmt, the enum is COPYVAL, ZEXT, SEXT, BITCAST, TRUNC, FPTRUNC,
60// FPTOUI, FPTOSI, UITOFP, SITOFP, INTTOPTR, PTRTOINT, UNKNOWN
62{
63
64 string line;
65 ifstream myfile(file.c_str());
66 if (myfile.is_open())
67 {
68 while (myfile.good())
69 {
71
73 string tmps;
75 while (ss.good())
76 {
77 ss >> tmps;
79 }
80
81 if (token_count == 0)
82 continue;
83
84 else if (token_count == 2)
85 {
87 string nodetype;
89 ss >> nodeId;
90 ss >> nodetype;
91 outs() << "reading node :" << nodeId << "\n";
92 if (nodetype == "v")
94 else if (nodetype == "o")
95 {
96 const MemObj* mem = pag->addDummyMemObj(nodeId, nullptr);
97 pag->addFIObjNode(mem);
98 }
99 else
100 assert(false && "format not support, pls specify node type");
101 }
102
103 // do consider gep edge
104 else if (token_count == 4)
105 {
109 string edge;
111 ss >> nodeSrc;
112 ss >> edge;
113 ss >> nodeDst;
114 ss >> offsetOrCSId;
115 outs() << "reading edge :" << nodeSrc << " " << edge << " "
116 << nodeDst << " offsetOrCSId=" << offsetOrCSId << " \n";
118 }
119 else
120 {
121 if (!line.empty())
122 {
123 outs() << "format not supported, token count = "
124 << token_count << "\n";
125 assert(false && "format not supported");
126 }
127 }
128 }
129 myfile.close();
130 }
131
132 else
133 outs() << "Unable to open file\n";
134
137 for(u32_t i = 0; i < lower_bound; i++)
138 pag->incNodeNum();
139
141
142 return pag;
143}
144
149 APOffset offsetOrCSId, std::string edge)
150{
151
152 //check whether these two nodes available
155
157 assert(SVFUtil::isa<ValVar>(dstNode) && "dst not an value node?");
158 if(edge=="addr")
159 assert(SVFUtil::isa<ObjVar>(srcNode) && "src not an value node?");
160 else
161 assert(!SVFUtil::isa<ObjVar>(srcNode) && "src not an object node?");
162
163 if (edge == "addr")
164 {
166 }
167 if (edge.rfind("copy-", 0) == 0)
168 {
169 // the enum is COPYVAL, ZEXT, SEXT, BITCAST, TRUNC, FPTRUNC,
171 std::string opType = edge.substr(5); // start substring from 5th char
172
173 if (opType == "COPYVAL")
174 {
176 }
177 else if (opType == "ZEXT")
178 {
180 }
181 else if (opType == "SEXT")
182 {
184 }
185 else if (opType == "BITCAST")
186 {
188 }
189 else if (opType == "TRUNC")
190 {
192 }
193 else if (opType == "FPTRUNC")
194 {
196 }
197 else if (opType == "FPTOUI")
198 {
200 }
201 else if (opType == "FPTOSI")
202 {
204 }
205 else if (opType == "UITOFP")
206 {
208 }
209 else if (opType == "SITOFP")
210 {
212 }
213 else if (opType == "INTTOPTR")
214 {
216 }
217 else if (opType == "PTRTOINT")
218 {
220 }
221 else
222 {
223 assert(false && "format not support, can not create such edge");
224 }
225 }
226 else if (edge == "load")
228 else if (edge == "store")
229 pag->addStoreStmt(srcID, dstID, nullptr);
230 else if (edge == "gep")
232 else if (edge == "variant-gep")
234 else if (edge == "call")
235 pag->addEdge(srcNode, dstNode, new CallPE(srcNode, dstNode, nullptr, nullptr));
236 else if (edge == "ret")
237 pag->addEdge(srcNode, dstNode, new RetPE(srcNode, dstNode, nullptr,nullptr));
238 else if (edge == "cmp")
240 else if (edge == "binary-op")
242 else if (edge == "unary-op")
244 else if (edge == "phi")
245 assert(false && "fix phi here!");
246 else if (edge == "select")
247 assert(false && "fix select here!");
248 else if (edge == "branch")
249 {
250 assert(false && "fix successors here!");
251 //pag->addBranchStmt(srcID, dstID, nullptr);
252 }
253 else
254 assert(false && "format not support, can not create such edge");
255}
256
unsigned u32_t
Definition CommandLine.h:18
static u32_t gepNodeNumIndex
void incNodeNum()
Increase number of node/edge.
u32_t getTotalNodeNum() const
Get total number of node/edge.
NodeType * getGNode(NodeID id) const
Get a node.
void setNodeNumAfterPAGBuild(u32_t num)
Definition IRGraph.h:198
bool addEdge(SVFVar *src, SVFVar *dst, SVFStmt *edge)
Add an edge into the graph.
Definition IRGraph.cpp:45
void addEdge(NodeID nodeSrc, NodeID nodeDst, APOffset offset, std::string edge)
SVFIR * build()
Start building.
NodeID addFIObjNode(const MemObj *obj)
Add a field-insensitive node, this method can only invoked by getFIGepObjNode.
Definition SVFIR.cpp:466
GepStmt * addVariantGepStmt(NodeID src, NodeID dst, const AccessPath &ap)
Add Variant(Gep) edge.
Definition SVFIR.cpp:366
CopyStmt * addCopyStmt(NodeID src, NodeID dst, CopyStmt::CopyKind type)
Add Copy edge.
Definition SVFIR.cpp:65
const MemObj * addDummyMemObj(NodeID i, const SVFType *type)
Definition SVFIR.h:730
LoadStmt * addLoadStmt(NodeID src, NodeID dst)
Add Load edge.
Definition SVFIR.cpp:205
GepStmt * addNormalGepStmt(NodeID src, NodeID dst, const AccessPath &ap)
Add Offset(Gep) edge.
Definition SVFIR.cpp:347
CmpStmt * addCmpStmt(NodeID op1, NodeID op2, NodeID dst, u32_t predict)
Add Copy edge.
Definition SVFIR.cpp:128
AddrStmt * addAddrStmt(NodeID src, NodeID dst)
Add an edge into SVFIR.
Definition SVFIR.cpp:47
UnaryOPStmt * addUnaryOPStmt(NodeID src, NodeID dst, u32_t opcode)
Add Unary edge.
Definition SVFIR.cpp:169
BinaryOPStmt * addBinaryOPStmt(NodeID op1, NodeID op2, NodeID dst, u32_t opcode)
Add Copy edge.
Definition SVFIR.cpp:149
StoreStmt * addStoreStmt(NodeID src, NodeID dst, const ICFGNode *val)
Add Store edge.
Definition SVFIR.cpp:224
NodeID addDummyValNode()
Definition SVFIR.h:496
std::ostream & outs()
Overwrite llvm::outs()
Definition SVFUtil.h:50
for isBitcode
Definition BasicTypes.h:68
u32_t NodeID
Definition GeneralType.h:55
s64_t APOffset
Definition GeneralType.h:60
llvm::IRBuilder IRBuilder
Definition BasicTypes.h:74
unsigned u32_t
Definition GeneralType.h:46