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 {
70 getline(myfile, line);
71
72 u32_t token_count = 0;
73 string tmps;
74 istringstream ss(line);
75 while (ss.good())
76 {
77 ss >> tmps;
78 token_count++;
79 }
80
81 if (token_count == 0)
82 continue;
83
84 else if (token_count == 2)
85 {
86 NodeID nodeId;
87 string nodetype;
88 istringstream ss(line);
89 ss >> nodeId;
90 ss >> nodetype;
91 outs() << "reading node :" << nodeId << "\n";
92 if (nodetype == "v")
93 pag->addDummyValNode(nodeId, nullptr);
94 else if (nodetype == "o")
95 {
96 pag->addFIObjNode(nodeId, pag->createObjTypeInfo(nullptr), nullptr);
97 }
98 else
99 assert(false && "format not support, pls specify node type");
100 }
101
102 // do consider gep edge
103 else if (token_count == 4)
104 {
105 NodeID nodeSrc;
106 NodeID nodeDst;
107 APOffset offsetOrCSId;
108 string edge;
109 istringstream ss(line);
110 ss >> nodeSrc;
111 ss >> edge;
112 ss >> nodeDst;
113 ss >> offsetOrCSId;
114 outs() << "reading edge :" << nodeSrc << " " << edge << " "
115 << nodeDst << " offsetOrCSId=" << offsetOrCSId << " \n";
116 addEdge(nodeSrc, nodeDst, offsetOrCSId, edge);
117 }
118 else
119 {
120 if (!line.empty())
121 {
122 outs() << "format not supported, token count = "
123 << token_count << "\n";
124 assert(false && "format not supported");
125 }
126 }
127 }
128 myfile.close();
129 }
130
131 else
132 outs() << "Unable to open file\n";
133
135 u32_t lower_bound = gepNodeNumIndex;
136 for(u32_t i = 0; i < lower_bound; i++)
137 pag->incNodeNum();
138
139 pag->setNodeNumAfterPAGBuild(pag->getTotalNodeNum());
140
141 return pag;
142}
143
148 APOffset offsetOrCSId, std::string edge)
149{
150
151 //check whether these two nodes available
152 PAGNode* srcNode = pag->getGNode(srcID);
153 PAGNode* dstNode = pag->getGNode(dstID);
154
156 assert(SVFUtil::isa<ValVar>(dstNode) && "dst not an value node?");
157 if(edge=="addr")
158 assert(SVFUtil::isa<ObjVar>(srcNode) && "src not an value node?");
159 else
160 assert(!SVFUtil::isa<ObjVar>(srcNode) && "src not an object node?");
161
162 if (edge == "addr")
163 {
164 pag->addAddrStmt(srcID, dstID);
165 }
166 if (edge.rfind("copy-", 0) == 0)
167 {
168 // the enum is COPYVAL, ZEXT, SEXT, BITCAST, TRUNC, FPTRUNC,
170 std::string opType = edge.substr(5); // start substring from 5th char
171
172 if (opType == "COPYVAL")
173 {
174 pag->addCopyStmt(srcID, dstID, CopyStmt::COPYVAL);
175 }
176 else if (opType == "ZEXT")
177 {
178 pag->addCopyStmt(srcID, dstID, CopyStmt::ZEXT);
179 }
180 else if (opType == "SEXT")
181 {
182 pag->addCopyStmt(srcID, dstID, CopyStmt::SEXT);
183 }
184 else if (opType == "BITCAST")
185 {
186 pag->addCopyStmt(srcID, dstID, CopyStmt::BITCAST);
187 }
188 else if (opType == "TRUNC")
189 {
190 pag->addCopyStmt(srcID, dstID, CopyStmt::TRUNC);
191 }
192 else if (opType == "FPTRUNC")
193 {
194 pag->addCopyStmt(srcID, dstID, CopyStmt::FPTRUNC);
195 }
196 else if (opType == "FPTOUI")
197 {
198 pag->addCopyStmt(srcID, dstID, CopyStmt::FPTOUI);
199 }
200 else if (opType == "FPTOSI")
201 {
202 pag->addCopyStmt(srcID, dstID, CopyStmt::FPTOSI);
203 }
204 else if (opType == "UITOFP")
205 {
206 pag->addCopyStmt(srcID, dstID, CopyStmt::UITOFP);
207 }
208 else if (opType == "SITOFP")
209 {
210 pag->addCopyStmt(srcID, dstID, CopyStmt::SITOFP);
211 }
212 else if (opType == "INTTOPTR")
213 {
214 pag->addCopyStmt(srcID, dstID, CopyStmt::INTTOPTR);
215 }
216 else if (opType == "PTRTOINT")
217 {
218 pag->addCopyStmt(srcID, dstID, CopyStmt::PTRTOINT);
219 }
220 else
221 {
222 assert(false && "format not support, can not create such edge");
223 }
224 }
225 else if (edge == "load")
226 pag->addLoadStmt(srcID, dstID);
227 else if (edge == "store")
228 pag->addStoreStmt(srcID, dstID, nullptr);
229 else if (edge == "gep")
230 pag->addNormalGepStmt(srcID, dstID, AccessPath(offsetOrCSId));
231 else if (edge == "variant-gep")
232 pag->addVariantGepStmt(srcID, dstID, AccessPath(offsetOrCSId));
233 else if (edge == "call")
234 pag->addEdge(srcNode, dstNode, new CallPE(SVFUtil::cast<ValVar>(dstNode), {SVFUtil::cast<ValVar>(srcNode)}, {nullptr}, nullptr));
235 else if (edge == "ret")
236 pag->addEdge(srcNode, dstNode, new RetPE(srcNode, dstNode, nullptr,nullptr));
237 else if (edge == "cmp")
238 pag->addCmpStmt(srcID, dstID, dstID, dstID);
239 else if (edge == "binary-op")
240 pag->addBinaryOPStmt(srcID, dstID, dstID, dstID);
241 else if (edge == "unary-op")
242 pag->addUnaryOPStmt(srcID, dstID, dstID);
243 else if (edge == "phi")
244 assert(false && "fix phi here!");
245 else if (edge == "select")
246 assert(false && "fix select here!");
247 else if (edge == "branch")
248 {
249 assert(false && "fix successors here!");
250 //pag->addBranchStmt(srcID, dstID, nullptr);
251 }
252 else
253 assert(false && "format not support, can not create such edge");
254}
255
unsigned u32_t
Definition CommandLine.h:18
static u32_t gepNodeNumIndex
void addEdge(NodeID nodeSrc, NodeID nodeDst, APOffset offset, std::string edge)
SVFIR * build()
Start building.
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 bool isa(const Y &Val)
Definition Casting.h:241
std::ostream & outs()
Overwrite llvm::outs().
Definition SVFUtil.h:58
for isBitcode
Definition BasicTypes.h:70
u32_t NodeID
Definition GeneralType.h:76
s64_t APOffset
Definition GeneralType.h:80
SVFVar PAGNode
Definition IRGraph.h:40
unsigned u32_t
Definition GeneralType.h:67