SVF
Public Member Functions | Static Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes | Static Private Attributes | List of all members
SVF::ExternalPAG Class Reference

#include <ExternalPAG.h>

Public Member Functions

 ExternalPAG (std::string functionName)
 
 ~ExternalPAG ()
 
std::string getFunctionName () const
 
NodeSetgetValueNodes ()
 
NodeSetgetObjectNodes ()
 
OrderedSet< std::tuple< NodeID, NodeID, std::string, int > > & getEdges ()
 
Map< int, NodeID > & getArgNodes ()
 
NodeID getReturnNode () const
 
void setReturnNode (NodeID returnNode)
 
bool hasReturnNode () const
 Does this function have a return node? More...
 
bool addExternalPAG (const SVFFunction *function)
 

Static Public Member Functions

static void initialise (SVFModule *svfModule)
 
static bool connectCallsiteToExternalPAG (CallSite *cs)
 
static bool hasExternalPAG (const SVFFunction *function)
 Whether an external PAG implementing function exists. More...
 
static void dumpFunctions (std::vector< std::string > functions)
 Dump individual PAGs of specified functions. Currently to outs(). More...
 

Private Member Functions

void readFromFile (std::string filename)
 

Static Private Member Functions

static std::vector< std::pair< std::string, std::string > > parseExternalPAGs (llvm::cl::list< std::string > &extpagsArgs)
 

Private Attributes

std::string functionName
 Name of the function this external PAG represents. More...
 
NodeSet valueNodes
 
NodeSet objectNodes
 
OrderedSet< std::tuple< NodeID, NodeID, std::string, int > > edges
 
Map< int, NodeIDargNodes
 
NodeID returnNode
 Node from which return edges connect. More...
 
bool hasReturn
 Whether this function has a return or not. More...
 

Static Private Attributes

static Map< const SVFFunction *, Map< int, PAGNode * > > functionToExternalPAGEntries
 
static Map< const SVFFunction *, PAGNode * > functionToExternalPAGReturns
 

Detailed Description

Represents the PAG of a function loaded externally (i.e. from file). It's purpose is to be attached to the main PAG (almost) seamlessly.

Definition at line 22 of file ExternalPAG.h.

Constructor & Destructor Documentation

◆ ExternalPAG()

SVF::ExternalPAG::ExternalPAG ( std::string  functionName)
inline

Definition at line 79 of file ExternalPAG.h.

80  returnNode(-1), hasReturn(false) {}
NodeID returnNode
Node from which return edges connect.
Definition: ExternalPAG.h:50
std::string functionName
Name of the function this external PAG represents.
Definition: ExternalPAG.h:32
bool hasReturn
Whether this function has a return or not.
Definition: ExternalPAG.h:53

◆ ~ExternalPAG()

SVF::ExternalPAG::~ExternalPAG ( )
inline

Definition at line 82 of file ExternalPAG.h.

82 {}

Member Function Documentation

◆ addExternalPAG()

bool ExternalPAG::addExternalPAG ( const SVFFunction function)

Adds (creates new equivalents) all the nodes and edges of this extpag to the main PAG. function is used as a key for future lookups. Returns true on success, false otherwise (incl. if it already exists).

Definition at line 377 of file ExternalPAG.cpp.

378 {
379  // The function does not exist in the module - bad arg?
380  // TODO: maybe some warning?
381  if (function == nullptr) return false;
382 
383  PAG *pag = PAG::getPAG();
384  if (hasExternalPAG(function)) return false;
385 
386  outs() << "Adding extpag " << this->getFunctionName() << "\n";
387  // Temporarily trick SVF Module into thinking we are reading from
388  // file to avoid setting BBs/Values (as these nodes don't have any).
389  std::string oldSVFModuleFileName = SVFModule::pagFileName();
391 
392  // We need: the new nodes.
393  // : the new edges.
394  // : to map function names to the entry nodes.
395  // : to map function names to the return node.
396 
397  // To create the new edges.
398  Map<NodeID, PAGNode *> extToNewNodes;
399 
400  // Add the value nodes.
401  for (auto extNodeIt = this->getValueNodes().begin();
402  extNodeIt != this->getValueNodes().end(); ++extNodeIt)
403  {
404  NodeID newNodeId = pag->addDummyValNode();
405  extToNewNodes[*extNodeIt] = pag->getPAGNode(newNodeId);
406  }
407 
408  // Add the object nodes.
409  for (auto extNodeIt = this->getObjectNodes().begin();
410  extNodeIt != this->getObjectNodes().end(); ++extNodeIt)
411  {
412  // TODO: fix obj node - there's more to it?
413  NodeID newNodeId = pag->addDummyObjNode();
414  extToNewNodes[*extNodeIt] = pag->getPAGNode(newNodeId);
415  }
416 
417  // Add the edges.
418  for (auto extEdgeIt = this->getEdges().begin();
419  extEdgeIt != this->getEdges().end(); ++extEdgeIt)
420  {
421  NodeID extSrcId = std::get<0>(*extEdgeIt);
422  NodeID extDstId = std::get<1>(*extEdgeIt);
423  std::string extEdgeType = std::get<2>(*extEdgeIt);
424  int extOffsetOrCSId = std::get<3>(*extEdgeIt);
425 
426  PAGNode *srcNode = extToNewNodes[extSrcId];
427  PAGNode *dstNode = extToNewNodes[extDstId];
428  NodeID srcId = srcNode->getId();
429  NodeID dstId = dstNode->getId();
430 
431  if (extEdgeType == "addr")
432  {
433  pag->addAddrPE(srcId, dstId);
434  }
435  else if (extEdgeType == "copy")
436  {
437  pag->addCopyPE(srcId, dstId);
438  }
439  else if (extEdgeType == "load")
440  {
441  pag->addLoadPE(srcId, dstId);
442  }
443  else if (extEdgeType == "store")
444  {
445  pag->addStorePE(srcId, dstId, nullptr);
446  }
447  else if (extEdgeType == "gep")
448  {
449  pag->addNormalGepPE(srcId, dstId, LocationSet(extOffsetOrCSId));
450  }
451  else if (extEdgeType == "variant-gep")
452  {
453  pag->addVariantGepPE(srcId, dstId);
454  }
455  else if (extEdgeType == "call")
456  {
457  pag->addEdge(srcNode, dstNode, new CallPE(srcNode, dstNode, nullptr));
458  }
459  else if (extEdgeType == "ret")
460  {
461  pag->addEdge(srcNode, dstNode, new RetPE(srcNode, dstNode, nullptr));
462  }
463  else if (extEdgeType == "cmp")
464  {
465  pag->addCmpPE(srcId, dstId);
466  }
467  else if (extEdgeType == "binary-op")
468  {
469  pag->addBinaryOPPE(srcId, dstId);
470  }
471  else if (extEdgeType == "unary-op")
472  {
473  pag->addUnaryOPPE(srcId, dstId);
474  }
475  else
476  {
477  outs() << "Bad edge type found during extpag addition\n";
478  }
479  }
480 
481  // Record the arg nodes.
483  for (auto argNodeIt = this->getArgNodes().begin();
484  argNodeIt != this->getArgNodes().end(); ++argNodeIt)
485  {
486  int index = argNodeIt->first;
487  NodeID extNodeId = argNodeIt->second;
488  argNodes[index] = extToNewNodes[extNodeId];
489  }
490 
492 
493  // Record the return node.
494  if (this->hasReturnNode())
495  {
496  functionToExternalPAGReturns[function] =
497  extToNewNodes[this->getReturnNode()];
498  }
499 
500  // Put it back as if nothing happened.
501  SVFModule::setPagFromTXT(oldSVFModuleFileName);
502 
503  return true;
504 }
static Map< const SVFFunction *, Map< int, PAGNode * > > functionToExternalPAGEntries
Definition: ExternalPAG.h:28
BinaryOPPE * addBinaryOPPE(NodeID src, NodeID dst)
Add Copy edge.
Definition: PAG.cpp:425
u32_t NodeID
Definition: SVFBasicTypes.h:80
bool addEdge(PAGNode *src, PAGNode *dst, PAGEdge *edge)
Add an edge into PAG.
Definition: PAG.cpp:754
bool hasReturnNode() const
Does this function have a return node?
Definition: ExternalPAG.h:133
Map< int, NodeID > argNodes
Definition: ExternalPAG.h:48
NodeID getReturnNode() const
Definition: ExternalPAG.h:122
NodeID addDummyValNode()
Add a dummy value/object node according to node ID (llvm value is null)
Definition: PAG.h:736
Definition: PAG.h:47
std::unordered_map< Key, Value, Hash, KeyEqual, Allocator > Map
Definition: SVFBasicTypes.h:98
NodeSet & getValueNodes()
Definition: ExternalPAG.h:104
static bool hasExternalPAG(const SVFFunction *function)
Whether an external PAG implementing function exists.
Map< int, NodeID > & getArgNodes()
Definition: ExternalPAG.h:117
NodeID addDummyObjNode(const Type *type=nullptr)
Definition: PAG.h:744
static PAG * getPAG(bool buildFromFile=false)
Singleton design here to make sure we only have one instance during any analysis. ...
Definition: PAG.h:160
PAGNode * getPAGNode(NodeID id) const
Get PAGNode ID.
Definition: PAG.h:513
static std::string pagFileName()
Definition: SVFModule.h:77
AddrPE * addAddrPE(NodeID src, NodeID dst)
Add Address edge.
Definition: PAG.cpp:373
static Map< const SVFFunction *, PAGNode * > functionToExternalPAGReturns
Definition: ExternalPAG.h:29
CopyPE * addCopyPE(NodeID src, NodeID dst)
Add Copy edge.
Definition: PAG.cpp:390
NormalGepPE * addNormalGepPE(NodeID src, NodeID dst, const LocationSet &ls)
Add Offset(Gep) edge.
Definition: PAG.cpp:595
VariantGepPE * addVariantGepPE(NodeID src, NodeID dst)
Add Variant(Gep) edge.
Definition: PAG.cpp:614
CmpPE * addCmpPE(NodeID src, NodeID dst)
Add Copy edge.
Definition: PAG.cpp:407
raw_ostream & outs()
Overwrite llvm::outs()
Definition: SVFUtil.h:47
NodeSet & getObjectNodes()
Definition: ExternalPAG.h:108
std::string getFunctionName() const
Definition: ExternalPAG.h:99
NodeID getId() const
Get ID.
Definition: GenericGraph.h:164
StorePE * addStorePE(NodeID src, NodeID dst, const IntraBlockNode *val)
Add Store edge.
Definition: PAG.cpp:477
static void setPagFromTXT(std::string txt)
Definition: SVFModule.h:72
LoadPE * addLoadPE(NodeID src, NodeID dst)
Add Load edge.
Definition: PAG.cpp:459
UnaryOPPE * addUnaryOPPE(NodeID src, NodeID dst)
Add Unary edge.
Definition: PAG.cpp:442
OrderedSet< std::tuple< NodeID, NodeID, std::string, int > > & getEdges()
Definition: ExternalPAG.h:112

◆ connectCallsiteToExternalPAG()

bool ExternalPAG::connectCallsiteToExternalPAG ( CallSite cs)
static

Connects callsite if a external PAG implementing the relevant function has been added. Returns true on success, false otherwise.

Definition at line 72 of file ExternalPAG.cpp.

73 {
74  PAG *pag = PAG::getPAG();
75 
76  Function* function = cs->getCalledFunction();
77  std::string functionName = function->getName().str();
78 
79  const SVFFunction* svfFun = LLVMModuleSet::getLLVMModuleSet()->getSVFFunction(function);
80  if (!hasExternalPAG(svfFun)) return false;
81 
84  PAGNode *retNode = functionToExternalPAGReturns[svfFun];
85 
86  // Handle the return.
87  if (llvm::isa<PointerType>(cs->getType()))
88  {
89  NodeID dstrec = pag->getValueNode(cs->getInstruction());
90  // Does it actually return a pointer?
91  if (SVFUtil::isa<PointerType>(function->getReturnType()))
92  {
93  if (retNode != nullptr)
94  {
95  CallBlockNode* icfgNode = pag->getICFG()->getCallBlockNode(cs->getInstruction());
96  pag->addRetPE(retNode->getId(), dstrec, icfgNode);
97  }
98  }
99  else
100  {
101  // This is a int2ptr cast during parameter passing
102  pag->addBlackHoleAddrPE(dstrec);
103  }
104  }
105 
106  // Handle the arguments;
107  // Actual arguments.
108  CallSite::arg_iterator itA = cs->arg_begin(), ieA = cs->arg_end();
109  Function::const_arg_iterator itF = function->arg_begin(),
110  ieF = function->arg_end();
111  // Formal arguments.
112  size_t formalNodeIndex = 0;
113 
114  for (; itF != ieF ; ++itA, ++itF, ++formalNodeIndex)
115  {
116  if (itA == ieA)
117  {
118  // When unneeded args are left empty, e.g. Linux kernel.
119  break;
120  }
121 
122  // Formal arg node is from the extpag, actual arg node would come from
123  // the main pag.
124  PAGNode *formalArgNode = argNodes[formalNodeIndex];
125  NodeID actualArgNodeId = pag->getValueNode(*itA);
126 
127  const llvm::Value *formalArg = &*itF;
128  if (!SVFUtil::isa<PointerType>(formalArg->getType())) continue;
129 
130  if (SVFUtil::isa<PointerType>((*itA)->getType()))
131  {
132  CallBlockNode* icfgNode = pag->getICFG()->getCallBlockNode(cs->getInstruction());
133  pag->addCallPE(actualArgNodeId, formalArgNode->getId(), icfgNode);
134  }
135  else
136  {
137  // This is a int2ptr cast during parameter passing
138  //addFormalParamBlackHoleAddrEdge(formalArgNode->getId(), &*itF);
139  assert(false && "you need to set the current location of this PAGEdge");
140  pag->addBlackHoleAddrPE(formalArgNode->getId());
141  }
142  // TODO proofread.
143  }
144 
145  return true;
146 }
static Map< const SVFFunction *, Map< int, PAGNode * > > functionToExternalPAGEntries
Definition: ExternalPAG.h:28
NodeID getValueNode(const Value *V)
Get PAG Node according to LLVM value.
Definition: PAG.h:521
std::string functionName
Name of the function this external PAG represents.
Definition: ExternalPAG.h:32
ICFG * getICFG()
Return ICFG.
Definition: PAG.h:133
CallPE * addCallPE(NodeID src, NodeID dst, const CallBlockNode *cs)
Add Call edge.
Definition: PAG.cpp:494
u32_t NodeID
Definition: SVFBasicTypes.h:80
CallBlockNode * getCallBlockNode(const Instruction *inst)
Definition: ICFG.cpp:197
#define assert(ex)
Definition: util.h:141
Map< int, NodeID > argNodes
Definition: ExternalPAG.h:48
CallBase * getInstruction() const
Definition: BasicTypes.h:316
Definition: PAG.h:47
std::unordered_map< Key, Value, Hash, KeyEqual, Allocator > Map
Definition: SVFBasicTypes.h:98
User::const_op_iterator arg_end() const
Definition: BasicTypes.h:321
static bool hasExternalPAG(const SVFFunction *function)
Whether an external PAG implementing function exists.
static PAG * getPAG(bool buildFromFile=false)
Singleton design here to make sure we only have one instance during any analysis. ...
Definition: PAG.h:160
PAGEdge * addBlackHoleAddrPE(NodeID node)
Set a pointer points-to black hole (e.g. int2ptr)
Definition: PAG.cpp:528
llvm::Function Function
Definition: BasicTypes.h:76
User::const_op_iterator arg_begin() const
Definition: BasicTypes.h:320
static Map< const SVFFunction *, PAGNode * > functionToExternalPAGReturns
Definition: ExternalPAG.h:29
RetPE * addRetPE(NodeID src, NodeID dst, const CallBlockNode *cs)
Add Return edge.
Definition: PAG.cpp:511
Function * getCalledFunction() const
Definition: BasicTypes.h:326
static LLVMModuleSet * getLLVMModuleSet()
Definition: LLVMModule.h:69
NodeID getId() const
Get ID.
Definition: GenericGraph.h:164
Type * getType() const
Definition: BasicTypes.h:319
User::const_op_iterator arg_iterator
Definition: BasicTypes.h:317
llvm::Value Value
Definition: BasicTypes.h:78
const SVFFunction * getSVFFunction(const Function *fun) const
Definition: LLVMModule.h:110

◆ dumpFunctions()

void ExternalPAG::dumpFunctions ( std::vector< std::string >  functions)
static

Dump individual PAGs of specified functions. Currently to outs().

Dump PAGs for the functions

Definition at line 256 of file ExternalPAG.cpp.

257 {
258  PAG *pag = PAG::getPAG();
259 
260  // Naive: first map functions to entries in PAG, then dump them.
262 
263  Set<PAGNode *> callDsts;
264  for (PAG::iterator it = pag->begin(); it != pag->end(); ++it)
265  {
266  PAGNode *currNode = it->second;
267  if (!currNode->hasOutgoingEdges(PAGEdge::PEDGEK::Call)) continue;
268 
269  // Where are these calls going?
270  for (PAGEdge::PAGEdgeSetTy::iterator it =
271  currNode->getOutgoingEdgesBegin(PAGEdge::PEDGEK::Call);
272  it != currNode->getOutgoingEdgesEnd(PAGEdge::PEDGEK::Call); ++it)
273  {
274  CallPE *callEdge = static_cast<CallPE *>(*it);
275  const Instruction *inst = callEdge->getCallInst()->getCallSite();
276  :: Function* currFunction =
277  static_cast<const CallInst *>(inst)->getCalledFunction();
278 
279  if (currFunction != nullptr)
280  {
281  // Otherwise, it would be an indirect call which we don't want.
282  std::string currFunctionName = currFunction->getName().str();
283 
284  if (std::find(functions.begin(), functions.end(),
285  currFunctionName) != functions.end())
286  {
287  // If the dst has already been added, we'd be duplicating
288  // due to multiple actual->arg call edges.
289  if (callDsts.find(callEdge->getDstNode()) == callDsts.end())
290  {
291  callDsts.insert(callEdge->getDstNode());
292  const SVFFunction* svfFun = LLVMModuleSet::getLLVMModuleSet()->getSVFFunction(currFunction);
293  functionToPAGNodes[svfFun].push_back(callEdge->getDstNode());
294  }
295  }
296  }
297  }
298  }
299 
300  for (auto it = functionToPAGNodes.begin(); it != functionToPAGNodes.end();
301  ++it)
302  {
303  const SVFFunction* function = it->first;
304  std::string functionName = it->first->getName().str();
305 
306  // The final nodes and edges we will print.
307  Set<PAGNode *> nodes;
309  // The search stack.
310  std::stack<PAGNode *> todoNodes;
311  // The arguments to the function.
312  std::vector<PAGNode *> argNodes = it->second;
313  PAGNode *retNode = nullptr;
314 
315 
316  outs() << "PAG for function: " << functionName << "\n";
317  for (auto node = argNodes.begin(); node != argNodes.end(); ++node)
318  {
319  todoNodes.push(*node);
320  }
321 
322  while (!todoNodes.empty())
323  {
324  PAGNode *currNode = todoNodes.top();
325  todoNodes.pop();
326 
327  // If the node has been dealt with, ignore it.
328  if (nodes.find(currNode) != nodes.end()) continue;
329  nodes.insert(currNode);
330 
331  // Return signifies the end of a path.
332  if (RetPN::classof(currNode))
333  {
334  retNode = currNode;
335  continue;
336  }
337 
338  auto outEdges = currNode->getOutEdges();
339  for (auto outEdge = outEdges.begin(); outEdge != outEdges.end();
340  ++outEdge)
341  {
342  edges.insert(*outEdge);
343  todoNodes.push((*outEdge)->getDstNode());
344  }
345  }
346 
347  for (auto node = nodes.begin(); node != nodes.end(); ++node)
348  {
349  // TODO: proper file.
350  // Argument nodes use extra information: it's argument number.
351  if (std::find(argNodes.begin(), argNodes.end(), *node)
352  != argNodes.end())
353  {
354  outputPAGNode(outs(), *node,
355  getArgNo(function, (*node)->getValue()));
356  }
357  else if (*node == retNode)
358  {
359  outputPAGNode(outs(), *node, "ret");
360  }
361  else
362  {
363  outputPAGNode(outs(), *node);
364  }
365  }
366 
367  for (auto edge = edges.begin(); edge != edges.end(); ++edge)
368  {
369  // TODO: proper file.
370  outputPAGEdge(outs(), *edge);
371  }
372 
373  outs() << "PAG for functionName " << functionName << " done\n";
374  }
375 }
iterator begin()
Iterators.
Definition: GenericGraph.h:365
std::string functionName
Name of the function this external PAG represents.
Definition: ExternalPAG.h:32
OrderedSet< std::tuple< NodeID, NodeID, std::string, int > > edges
Definition: ExternalPAG.h:42
Map< int, NodeID > argNodes
Definition: ExternalPAG.h:48
llvm::CallInst CallInst
Definition: BasicTypes.h:143
Definition: PAG.h:47
static void outputPAGEdge(raw_ostream &o, PAGEdge *pagEdge)
bool hasOutgoingEdges(PAGEdge::PEDGEK kind) const
Has outgoing PAG edges.
Definition: PAGNode.h:209
std::unordered_map< Key, Value, Hash, KeyEqual, Allocator > Map
Definition: SVFBasicTypes.h:98
static PAG * getPAG(bool buildFromFile=false)
Singleton design here to make sure we only have one instance during any analysis. ...
Definition: PAG.h:160
IDToNodeMapTy::iterator iterator
Node Iterators.
Definition: GenericGraph.h:337
static bool classof(const RetPN *)
Definition: PAGNode.h:582
NodeType * getDstNode() const
Definition: GenericGraph.h:89
std::unordered_set< Key, Hash, KeyEqual, Allocator > Set
Definition: SVFBasicTypes.h:93
llvm::Function Function
Definition: BasicTypes.h:76
llvm::Instruction Instruction
Definition: BasicTypes.h:79
const GEdgeSetTy & getOutEdges() const
Definition: GenericGraph.h:177
raw_ostream & outs()
Overwrite llvm::outs()
Definition: SVFUtil.h:47
const CallBlockNode * getCallInst() const
Get method for the call instruction.
Definition: PAGEdge.h:596
static void outputPAGNode(raw_ostream &o, PAGNode *pagNode)
PAGEdge::PAGEdgeSetTy::iterator getOutgoingEdgesBegin(PAGEdge::PEDGEK kind) const
Get outgoing PAGEdge iterator.
Definition: PAGNode.h:219
static LLVMModuleSet * getLLVMModuleSet()
Definition: LLVMModule.h:69
const Instruction * getCallSite() const
Return callsite.
Definition: ICFGNode.h:381
const SVFFunction * getSVFFunction(const Function *fun) const
Definition: LLVMModule.h:110
int getArgNo(const SVFFunction *function, const Value *arg)
PAGEdge::PAGEdgeSetTy::iterator getOutgoingEdgesEnd(PAGEdge::PEDGEK kind) const
Get outgoing PAGEdge iterator.
Definition: PAGNode.h:227

◆ getArgNodes()

Map<int, NodeID>& SVF::ExternalPAG::getArgNodes ( )
inline

Definition at line 117 of file ExternalPAG.h.

118  {
119  return argNodes;
120  }
Map< int, NodeID > argNodes
Definition: ExternalPAG.h:48

◆ getEdges()

OrderedSet<std::tuple<NodeID, NodeID, std::string, int> >& SVF::ExternalPAG::getEdges ( )
inline

Definition at line 112 of file ExternalPAG.h.

113  {
114  return edges;
115  }
OrderedSet< std::tuple< NodeID, NodeID, std::string, int > > edges
Definition: ExternalPAG.h:42

◆ getFunctionName()

std::string SVF::ExternalPAG::getFunctionName ( ) const
inline

Definition at line 99 of file ExternalPAG.h.

100  {
101  return functionName;
102  }
std::string functionName
Name of the function this external PAG represents.
Definition: ExternalPAG.h:32

◆ getObjectNodes()

NodeSet& SVF::ExternalPAG::getObjectNodes ( )
inline

Definition at line 108 of file ExternalPAG.h.

109  {
110  return objectNodes;
111  }
NodeSet objectNodes
Definition: ExternalPAG.h:39

◆ getReturnNode()

NodeID SVF::ExternalPAG::getReturnNode ( ) const
inline

Definition at line 122 of file ExternalPAG.h.

123  {
124  return returnNode;
125  }
NodeID returnNode
Node from which return edges connect.
Definition: ExternalPAG.h:50

◆ getValueNodes()

NodeSet& SVF::ExternalPAG::getValueNodes ( )
inline

Definition at line 104 of file ExternalPAG.h.

105  {
106  return valueNodes;
107  }
NodeSet valueNodes
Definition: ExternalPAG.h:36

◆ hasExternalPAG()

bool ExternalPAG::hasExternalPAG ( const SVFFunction function)
static

Whether an external PAG implementing function exists.

Definition at line 148 of file ExternalPAG.cpp.

149 {
150  bool ret = functionToExternalPAGEntries.find(function)
152  return ret;
153 }
static Map< const SVFFunction *, Map< int, PAGNode * > > functionToExternalPAGEntries
Definition: ExternalPAG.h:28

◆ hasReturnNode()

bool SVF::ExternalPAG::hasReturnNode ( ) const
inline

Does this function have a return node?

Definition at line 133 of file ExternalPAG.h.

134  {
135  return hasReturn;
136  }
bool hasReturn
Whether this function has a return or not.
Definition: ExternalPAG.h:53

◆ initialise()

void ExternalPAG::initialise ( SVFModule svfModule)
static

Parses command line arguments and attaches external PAGs to main PAG.

Definition at line 54 of file ExternalPAG.cpp.

55 {
56  std::vector<std::pair<std::string, std::string>> parsedExternalPAGs
58 
59  // Build ext PAGs (and add them) first to use them in PAG construction.
60  for (auto extpagPair= parsedExternalPAGs.begin();
61  extpagPair != parsedExternalPAGs.end(); ++extpagPair)
62  {
63  std::string fname = extpagPair->first;
64  std::string path = extpagPair->second;
65 
66  ExternalPAG extpag = ExternalPAG(fname);
67  extpag.readFromFile(path);
68  extpag.addExternalPAG(getFunction(fname));
69  }
70 }
llvm::cl::list< std::string > ExternalPAGArgs("extpags", llvm::cl::desc("ExternalPAGs to use during PAG construction (format: func1@/path/to/graph,func2@/foo,..."), llvm::cl::CommaSeparated)
ExternalPAG(std::string functionName)
Definition: ExternalPAG.h:79
const SVFFunction * getFunction(StringRef name)
Get the corresponding Function based on its name.
Definition: SVFUtil.h:191
static std::vector< std::pair< std::string, std::string > > parseExternalPAGs(llvm::cl::list< std::string > &extpagsArgs)
Definition: ExternalPAG.cpp:36
void readFromFile(std::string filename)
bool addExternalPAG(const SVFFunction *function)

◆ parseExternalPAGs()

std::vector< std::pair< std::string, std::string > > ExternalPAG::parseExternalPAGs ( llvm::cl::list< std::string > &  extpagsArgs)
staticprivate

For passing that passed to -extpags option, splitting fname into a pair.

Definition at line 36 of file ExternalPAG.cpp.

37 {
38  std::vector<std::pair<std::string, std::string>> parsedExternalPAGs;
39  for (auto arg = extpagsArgs.begin(); arg != extpagsArgs.end();
40  ++arg)
41  {
42  std::stringstream ss(*arg);
43  std::string functionName;
44  getline(ss, functionName, '@');
45  std::string path;
46  getline(ss, path);
47  parsedExternalPAGs.push_back(
48  std::pair<std::string, std::string>(functionName, path));
49  }
50 
51  return parsedExternalPAGs;
52 }
std::string functionName
Name of the function this external PAG represents.
Definition: ExternalPAG.h:32

◆ readFromFile()

void ExternalPAG::readFromFile ( std::string  filename)
private

Reads nodes and edges from file.

File format: Node: nodeID Nodetype [[0|1|2|...]+|ret]

  • Giving a number means that node represents such argument.
  • Giving ret means that node represents the return node.. Edge: nodeID edgetype NodeID Offset

Example: 1 o 2 v 3 v 4 v 1 addr 2 0 1 addr 3 0 3 gep 4 4

Definition at line 507 of file ExternalPAG.cpp.

508 {
509  std::string line;
510  std::ifstream pagFile(filename.c_str());
511 
512  if (!pagFile.is_open())
513  {
514  outs() << "ExternalPAG::buildFromFile: could not open " << filename
515  << "\n";
516  return;
517  }
518 
519  while (pagFile.good())
520  {
521  std::getline(pagFile, line);
522 
523  Size_t tokenCount = 0;
524  std::string tmps;
525  std::istringstream ss(line);
526  while (ss.good())
527  {
528  ss >> tmps;
529  tokenCount++;
530  }
531 
532  if (tokenCount == 0)
533  {
534  // Empty line.
535  continue;
536  }
537 
538  if (tokenCount == 2 || tokenCount == 3)
539  {
540  // It's a node.
541  NodeID nodeId;
542  std::string nodeType;
543  std::istringstream ss(line);
544  ss >> nodeId;
545  ss >> nodeType;
546 
547  if (nodeType == "v")
548  {
549  valueNodes.insert(nodeId);
550  }
551  else if (nodeType == "o")
552  {
553  objectNodes.insert(nodeId);
554  }
555  else
556  {
557  assert(false
558  && "format not supported, please specify node type");
559  }
560 
561 
562  if (tokenCount == 3)
563  {
564  // If there are 3 tokens, it should be 0, 1, 2, ... or ret.
565  std::string argNoOrRet;
566  ss >> argNoOrRet;
567 
568  if (argNoOrRet == "ret")
569  {
570  setReturnNode(nodeId);
571  }
572  else if (std::all_of(argNoOrRet.begin(), argNoOrRet.end(),
573  ::isdigit))
574  {
575  int argNo = std::stoi(argNoOrRet);
576  argNodes[argNo] = nodeId;
577  }
578  else
579  {
580  assert(false
581  && "format not supported, not arg number or ret");
582  }
583 
584  }
585  }
586  else if (tokenCount == 4)
587  {
588  // It's an edge
589  NodeID nodeSrc;
590  NodeID nodeDst;
591  Size_t offsetOrCSId;
592  std::string edge;
593  std::istringstream ss(line);
594  ss >> nodeSrc;
595  ss >> edge;
596  ss >> nodeDst;
597  ss >> offsetOrCSId;
598 
599  edges.insert(
600  std::tuple<NodeID, NodeID, std::string, int>(nodeSrc, nodeDst,
601  edge,
602  offsetOrCSId));
603  }
604  else
605  {
606  if (!line.empty())
607  {
608  outs() << "format not supported, token count = "
609  << tokenCount << "\n";
610  assert(false && "format not supported");
611  }
612  }
613  }
614 
615  /*
616  TODO: might need to include elsewhere.
618  u32_t lower_bound = 1000;
619  for(u32_t i = 0; i < lower_bound; i++)
620  pag->incNodeNum();
621  */
622 }
u32_t NodeID
Definition: SVFBasicTypes.h:80
OrderedSet< std::tuple< NodeID, NodeID, std::string, int > > edges
Definition: ExternalPAG.h:42
#define assert(ex)
Definition: util.h:141
NodeSet valueNodes
Definition: ExternalPAG.h:36
Map< int, NodeID > argNodes
Definition: ExternalPAG.h:48
void setReturnNode(NodeID returnNode)
Definition: ExternalPAG.h:126
signed long Size_t
Definition: SVFBasicTypes.h:78
raw_ostream & outs()
Overwrite llvm::outs()
Definition: SVFUtil.h:47
NodeSet objectNodes
Definition: ExternalPAG.h:39

◆ setReturnNode()

void SVF::ExternalPAG::setReturnNode ( NodeID  returnNode)
inline

Definition at line 126 of file ExternalPAG.h.

127  {
128  this->returnNode = returnNode;
129  hasReturn = true;
130  }
NodeID returnNode
Node from which return edges connect.
Definition: ExternalPAG.h:50
bool hasReturn
Whether this function has a return or not.
Definition: ExternalPAG.h:53

Member Data Documentation

◆ argNodes

Map<int, NodeID> SVF::ExternalPAG::argNodes
private

Nodes in the ExternalPAG which call edges should connect to. argNodes[0] is arg 0, argNodes[1] is arg 1, ...

Definition at line 48 of file ExternalPAG.h.

◆ edges

OrderedSet<std::tuple<NodeID, NodeID, std::string, int> > SVF::ExternalPAG::edges
private

Edges in this external PAG, represented by the parts of an Edge because we will rebuild these edges in the main PAG.

Definition at line 42 of file ExternalPAG.h.

◆ functionName

std::string SVF::ExternalPAG::functionName
private

Name of the function this external PAG represents.

Definition at line 32 of file ExternalPAG.h.

◆ functionToExternalPAGEntries

Map< const SVFFunction *, Map< int, PAGNode * > > ExternalPAG::functionToExternalPAGEntries
staticprivate

Maps function names to the entry nodes of the extpag which implements it. This is to connect arguments and callsites.

Definition at line 28 of file ExternalPAG.h.

◆ functionToExternalPAGReturns

Map< const SVFFunction *, PAGNode * > ExternalPAG::functionToExternalPAGReturns
staticprivate

Definition at line 29 of file ExternalPAG.h.

◆ hasReturn

bool SVF::ExternalPAG::hasReturn
private

Whether this function has a return or not.

Definition at line 53 of file ExternalPAG.h.

◆ objectNodes

NodeSet SVF::ExternalPAG::objectNodes
private

Object nodes in this external PAG, represented by NodeIDs because we will rebuild these nodes in the main PAG.

Definition at line 39 of file ExternalPAG.h.

◆ returnNode

NodeID SVF::ExternalPAG::returnNode
private

Node from which return edges connect.

Definition at line 50 of file ExternalPAG.h.

◆ valueNodes

NodeSet SVF::ExternalPAG::valueNodes
private

Value nodes in this external PAG, represented by NodeIDs because we will rebuild these nodes in the main PAG.

Definition at line 36 of file ExternalPAG.h.


The documentation for this class was generated from the following files: