Static Value-Flow Analysis
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 
35 using namespace std;
36 using namespace SVF;
37 using namespace SVFUtil;
38 static 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:
48 5 o
49 6 v
50 7 v
51 8 v
52 9 v
53 5 addr 6 0
54 6 gep 7 4
55 7 copy-ZEXT 8 0
56 6 store 8 0
57 8 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
61 SVFIR* PAGBuilderFromFile::build()
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);
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  {
106  NodeID nodeSrc;
107  NodeID nodeDst;
108  APOffset offsetOrCSId;
109  string edge;
110  istringstream ss(line);
111  ss >> nodeSrc;
112  ss >> edge;
113  ss >> nodeDst;
114  ss >> offsetOrCSId;
115  outs() << "reading edge :" << nodeSrc << " " << edge << " "
116  << nodeDst << " offsetOrCSId=" << offsetOrCSId << " \n";
117  addEdge(nodeSrc, nodeDst, offsetOrCSId, edge);
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 
136  u32_t lower_bound = gepNodeNumIndex;
137  for(u32_t i = 0; i < lower_bound; i++)
138  pag->incNodeNum();
139 
140  pag->setNodeNumAfterPAGBuild(pag->getTotalNodeNum());
141 
142  return pag;
143 }
144 
148 void PAGBuilderFromFile::addEdge(NodeID srcID, NodeID dstID,
149  APOffset offsetOrCSId, std::string edge)
150 {
151 
152  //check whether these two nodes available
153  PAGNode* srcNode = pag->getGNode(srcID);
154  PAGNode* dstNode = pag->getGNode(dstID);
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  {
165  pag->addAddrStmt(srcID, dstID);
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  {
175  pag->addCopyStmt(srcID, dstID, CopyStmt::COPYVAL);
176  }
177  else if (opType == "ZEXT")
178  {
179  pag->addCopyStmt(srcID, dstID, CopyStmt::ZEXT);
180  }
181  else if (opType == "SEXT")
182  {
183  pag->addCopyStmt(srcID, dstID, CopyStmt::SEXT);
184  }
185  else if (opType == "BITCAST")
186  {
187  pag->addCopyStmt(srcID, dstID, CopyStmt::BITCAST);
188  }
189  else if (opType == "TRUNC")
190  {
191  pag->addCopyStmt(srcID, dstID, CopyStmt::TRUNC);
192  }
193  else if (opType == "FPTRUNC")
194  {
195  pag->addCopyStmt(srcID, dstID, CopyStmt::FPTRUNC);
196  }
197  else if (opType == "FPTOUI")
198  {
199  pag->addCopyStmt(srcID, dstID, CopyStmt::FPTOUI);
200  }
201  else if (opType == "FPTOSI")
202  {
203  pag->addCopyStmt(srcID, dstID, CopyStmt::FPTOSI);
204  }
205  else if (opType == "UITOFP")
206  {
207  pag->addCopyStmt(srcID, dstID, CopyStmt::UITOFP);
208  }
209  else if (opType == "SITOFP")
210  {
211  pag->addCopyStmt(srcID, dstID, CopyStmt::SITOFP);
212  }
213  else if (opType == "INTTOPTR")
214  {
215  pag->addCopyStmt(srcID, dstID, CopyStmt::INTTOPTR);
216  }
217  else if (opType == "PTRTOINT")
218  {
219  pag->addCopyStmt(srcID, dstID, CopyStmt::PTRTOINT);
220  }
221  else
222  {
223  assert(false && "format not support, can not create such edge");
224  }
225  }
226  else if (edge == "load")
227  pag->addLoadStmt(srcID, dstID);
228  else if (edge == "store")
229  pag->addStoreStmt(srcID, dstID, nullptr);
230  else if (edge == "gep")
231  pag->addNormalGepStmt(srcID, dstID, AccessPath(offsetOrCSId));
232  else if (edge == "variant-gep")
233  pag->addVariantGepStmt(srcID, dstID, AccessPath(offsetOrCSId));
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")
239  pag->addCmpStmt(srcID, dstID, dstID, dstID);
240  else if (edge == "binary-op")
241  pag->addBinaryOPStmt(srcID, dstID, dstID, dstID);
242  else if (edge == "unary-op")
243  pag->addUnaryOPStmt(srcID, dstID, dstID);
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
const char *const string
Definition: cJSON.h:172
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
unsigned u32_t
Definition: GeneralType.h:46