Static Value-Flow Analysis
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Static Public Member Functions | Private Attributes | Friends | List of all members
SVF::CHGraph Class Reference

#include <CHG.h>

Inheritance diagram for SVF::CHGraph:
SVF::CommonCHGraph SVF::GenericGraph< NodeTy, EdgeTy >

Public Types

enum  RELATIONTYPE { CONSTRUCTOR = 0x1 , DESTRUCTOR = 0x2 }
 
typedef Set< const CHNode * > CHNodeSetTy
 
typedef FIFOWorkList< const CHNode * > WorkList
 
typedef Map< std::string, CHNodeSetTyNameToCHNodesMap
 
typedef Map< const ICFGNode *, CHNodeSetTyCallNodeToCHNodesMap
 
typedef Map< const ICFGNode *, VTableSetCallNodeToVTableSetMap
 
typedef Map< const ICFGNode *, VFunSetCallNodeToVFunSetMap
 
- Public Types inherited from SVF::CommonCHGraph
enum  CHGKind { Standard , DI }
 
- Public Types inherited from SVF::GenericGraph< NodeTy, EdgeTy >
typedef NodeTy NodeType
 
typedef EdgeTy EdgeType
 
typedef OrderedMap< NodeID, NodeType * > IDToNodeMapTy
 NodeID to GenericNode map.
 
typedef IDToNodeMapTy::iterator iterator
 Node Iterators.
 
typedef IDToNodeMapTy::const_iterator const_iterator
 

Public Member Functions

 CHGraph (SVFModule *svfModule)
 
 ~CHGraph () override=default
 
void addEdge (const std::string className, const std::string baseClassName, CHEdge::CHEDGETYPE edgeType)
 
CHNodegetNode (const std::string name) const
 
void getVFnsFromVtbls (const CallICFGNode *cs, const VTableSet &vtbls, VFunSet &virtualFunctions) override
 
void dump (const std::string &filename)
 
void view ()
 
void printCH ()
 
u32_t getVirtualFunctionID (const SVFFunction *vfn) const
 
const SVFFunctiongetVirtualFunctionBasedonID (u32_t id) const
 
void addInstances (const std::string templateName, CHNode *node)
 
const CHNodeSetTygetDescendants (const std::string className)
 
const CHNodeSetTygetInstances (const std::string className)
 
bool csHasVtblsBasedonCHA (const CallICFGNode *cs) override
 
bool csHasVFnsBasedonCHA (const CallICFGNode *cs) override
 
const VTableSetgetCSVtblsBasedonCHA (const CallICFGNode *cs) override
 
const VFunSetgetCSVFsBasedonCHA (const CallICFGNode *cs) override
 
- Public Member Functions inherited from SVF::CommonCHGraph
virtual ~CommonCHGraph ()
 
CHGKind getKind (void) const
 
- Public Member Functions inherited from SVF::GenericGraph< NodeTy, EdgeTy >
 GenericGraph ()
 Constructor.
 
virtual ~GenericGraph ()
 Destructor.
 
void destroy ()
 Release memory.
 
iterator begin ()
 Iterators.
 
iterator end ()
 
const_iterator begin () const
 
const_iterator end () const
 
void addGNode (NodeID id, NodeType *node)
 Add a Node.
 
NodeTypegetGNode (NodeID id) const
 Get a node.
 
bool hasGNode (NodeID id) const
 Has a node.
 
void removeGNode (NodeType *node)
 Delete a node.
 
u32_t getTotalNodeNum () const
 Get total number of node/edge.
 
u32_t getTotalEdgeNum () const
 
void incNodeNum ()
 Increase number of node/edge.
 
void incEdgeNum ()
 

Static Public Member Functions

static bool classof (const CommonCHGraph *chg)
 

Private Attributes

SVFModulesvfMod
 
u32_t classNum
 
u32_t vfID
 
double buildingCHGTime
 
Map< std::string, CHNode * > classNameToNodeMap
 
NameToCHNodesMap classNameToDescendantsMap
 
NameToCHNodesMap classNameToAncestorsMap
 
NameToCHNodesMap classNameToInstAndDescsMap
 
NameToCHNodesMap templateNameToInstancesMap
 
CallNodeToCHNodesMap callNodeToClassesMap
 
Map< const SVFFunction *, u32_tvirtualFunctionToIDMap
 
CallNodeToVTableSetMap callNodeToCHAVtblsMap
 
CallNodeToVFunSetMap callNodeToCHAVFnsMap
 

Friends

class SVFIRWriter
 
class SVFIRReader
 
class CHGBuilder
 

Additional Inherited Members

- Public Attributes inherited from SVF::GenericGraph< NodeTy, EdgeTy >
u32_t edgeNum
 total num of node
 
u32_t nodeNum
 total num of edge
 
- Protected Attributes inherited from SVF::CommonCHGraph
CHGKind kind
 
- Protected Attributes inherited from SVF::GenericGraph< NodeTy, EdgeTy >
IDToNodeMapTy IDToNodeMap
 node map
 

Detailed Description

Definition at line 233 of file CHG.h.

Member Typedef Documentation

◆ CallNodeToCHNodesMap

Definition at line 244 of file CHG.h.

◆ CallNodeToVFunSetMap

Definition at line 246 of file CHG.h.

◆ CallNodeToVTableSetMap

Definition at line 245 of file CHG.h.

◆ CHNodeSetTy

Definition at line 240 of file CHG.h.

◆ NameToCHNodesMap

Definition at line 242 of file CHG.h.

◆ WorkList

Definition at line 241 of file CHG.h.

Member Enumeration Documentation

◆ RELATIONTYPE

Enumerator
CONSTRUCTOR 
DESTRUCTOR 

Definition at line 248 of file CHG.h.

249 {
250 CONSTRUCTOR = 0x1, // connect node based on constructor
251 DESTRUCTOR = 0x2 // connect node based on destructor
252 } RELATIONTYPE;
@ CONSTRUCTOR
Definition CHG.h:250
@ DESTRUCTOR
Definition CHG.h:251

Constructor & Destructor Documentation

◆ CHGraph()

SVF::CHGraph::CHGraph ( SVFModule svfModule)
inline

Definition at line 254 of file CHG.h.

254 : svfMod(svfModule), classNum(0), vfID(0), buildingCHGTime(0)
255 {
256 this->kind = Standard;
257 }
SVFModule * svfMod
Definition CHG.h:320
u32_t vfID
Definition CHG.h:322
double buildingCHGTime
Definition CHG.h:323
u32_t classNum
Definition CHG.h:321
CHGKind kind
Definition CHG.h:73

◆ ~CHGraph()

SVF::CHGraph::~CHGraph ( )
overridedefault

Member Function Documentation

◆ addEdge()

void CHGraph::addEdge ( const std::string  className,
const std::string  baseClassName,
CHEdge::CHEDGETYPE  edgeType 
)

Definition at line 97 of file CHG.cpp.

99{
100 CHNode *srcNode = getNode(className);
102 assert(srcNode && dstNode && "node not found?");
103
104 if (!hasEdge(srcNode, dstNode, edgeType))
105 {
106 CHEdge *edge = new CHEdge(srcNode, dstNode, edgeType);
107 srcNode->addOutgoingEdge(edge);
108 dstNode->addIncomingEdge(edge);
109 }
110}
static bool hasEdge(const CHNode *src, const CHNode *dst, CHEdge::CHEDGETYPE et)
Definition CHG.cpp:39
CHNode * getNode(const std::string name) const
Definition CHG.cpp:112
llvm::IRBuilder IRBuilder
Definition BasicTypes.h:74

◆ addInstances()

void SVF::CHGraph::addInstances ( const std::string  templateName,
CHNode node 
)
inline

Definition at line 290 of file CHG.h.

291 {
292 NameToCHNodesMap::iterator it = templateNameToInstancesMap.find(
294 if (it != templateNameToInstancesMap.end())
295 it->second.insert(node);
296 else
298 }
NameToCHNodesMap templateNameToInstancesMap
Definition CHG.h:328

◆ classof()

static bool SVF::CHGraph::classof ( const CommonCHGraph chg)
inlinestatic

Definition at line 313 of file CHG.h.

314 {
315 return chg->getKind() == Standard;
316 }

◆ csHasVFnsBasedonCHA()

bool CHGraph::csHasVFnsBasedonCHA ( const CallICFGNode cs)
overridevirtual

Implements SVF::CommonCHGraph.

Definition at line 79 of file CHG.cpp.

80{
81 CallNodeToVFunSetMap::const_iterator it = callNodeToCHAVFnsMap.find(cs);
82 return it != callNodeToCHAVFnsMap.end();
83}
CallNodeToVFunSetMap callNodeToCHAVFnsMap
Definition CHG.h:334

◆ csHasVtblsBasedonCHA()

bool CHGraph::csHasVtblsBasedonCHA ( const CallICFGNode cs)
overridevirtual

Implements SVF::CommonCHGraph.

Definition at line 74 of file CHG.cpp.

75{
76 CallNodeToVTableSetMap::const_iterator it = callNodeToCHAVtblsMap.find(cs);
77 return it != callNodeToCHAVtblsMap.end();
78}
CallNodeToVTableSetMap callNodeToCHAVtblsMap
Definition CHG.h:333

◆ dump()

void CHGraph::dump ( const std::string &  filename)

Dump call graph into dot file

Definition at line 242 of file CHG.cpp.

243{
245 printCH();
246}
void printCH()
Definition CHG.cpp:218
static void WriteGraphToFile(SVF::OutStream &O, const std::string &GraphName, const GraphType &GT, bool simple=false)
std::ostream & outs()
Overwrite llvm::outs()
Definition SVFUtil.h:50

◆ getCSVFsBasedonCHA()

const VFunSet & CHGraph::getCSVFsBasedonCHA ( const CallICFGNode cs)
overridevirtual

Implements SVF::CommonCHGraph.

Definition at line 90 of file CHG.cpp.

91{
92 CallNodeToVFunSetMap::const_iterator it = callNodeToCHAVFnsMap.find(cs);
93 assert(it != callNodeToCHAVFnsMap.end() && "cs does not have vfns based on CHA.");
94 return it->second;
95}

◆ getCSVtblsBasedonCHA()

const VTableSet & CHGraph::getCSVtblsBasedonCHA ( const CallICFGNode cs)
overridevirtual

Implements SVF::CommonCHGraph.

Definition at line 84 of file CHG.cpp.

85{
86 CallNodeToVTableSetMap::const_iterator it = callNodeToCHAVtblsMap.find(cs);
87 assert(it != callNodeToCHAVtblsMap.end() && "cs does not have vtabls based on CHA.");
88 return it->second;
89}

◆ getDescendants()

const CHNodeSetTy & SVF::CHGraph::getDescendants ( const std::string  className)
inline

Definition at line 299 of file CHG.h.

300 {
301 return classNameToDescendantsMap[className];
302 }
NameToCHNodesMap classNameToDescendantsMap
Definition CHG.h:325

◆ getInstances()

const CHNodeSetTy & SVF::CHGraph::getInstances ( const std::string  className)
inline

Definition at line 303 of file CHG.h.

304 {
305 return templateNameToInstancesMap[className];
306 }

◆ getNode()

CHNode * CHGraph::getNode ( const std::string  name) const

Definition at line 112 of file CHG.cpp.

113{
114 auto chNode = classNameToNodeMap.find(name);
115 if (chNode != classNameToNodeMap.end()) return chNode->second;
116 else return nullptr;
117}
const char *const name
Definition cJSON.h:264
Map< std::string, CHNode * > classNameToNodeMap
Definition CHG.h:324

◆ getVFnsFromVtbls()

void CHGraph::getVFnsFromVtbls ( const CallICFGNode cs,
const VTableSet vtbls,
VFunSet virtualFunctions 
)
overridevirtual

get target virtual functions

get the function name of the virtual callsite

Implements SVF::CommonCHGraph.

Definition at line 124 of file CHG.cpp.

125{
127 size_t idx = callsite->getFunIdxInVtable();
129 string funName = callsite->getFunNameOfVirtualCall();
130 for (const SVFGlobalValue *vt : vtbls)
131 {
132 const CHNode *child = getNode(vt->getName());
133 if (child == nullptr)
134 continue;
136 child->getVirtualFunctions(idx, vfns);
137 for (CHNode::FuncVector::const_iterator fit = vfns.begin(),
138 feit = vfns.end(); fit != feit; ++fit)
139 {
140 const SVFFunction* callee = *fit;
141 if (callsite->arg_size() == callee->arg_size() ||
142 (callsite->isVarArg() && callee->isVarArg()))
143 {
144
145 // if argument types do not match
146 // skip this one
147 if (!checkArgTypes(callsite, callee))
148 continue;
149
150 string calleeName = callee->getName();
151
152 /*
153 * The compiler will add some special suffix (e.g.,
154 * "[abi:cxx11]") to the end of some virtual function:
155 * In dealII
156 * function: FE_Q<3>::get_name
157 * will be mangled as: _ZNK4FE_QILi3EE8get_nameB5cxx11Ev
158 * after demangling: FE_Q<3>::get_name[abi:cxx11]
159 * The special suffix ("[abi:cxx11]") needs to be removed
160 */
161 const std::string suffix("[abi:cxx11]");
162 size_t suffix_pos = calleeName.rfind(suffix);
163 if (suffix_pos != string::npos)
164 calleeName.erase(suffix_pos, suffix.size());
165
166 /*
167 * if we can't get the function name of a virtual callsite, all virtual
168 * functions calculated by idx will be valid
169 */
170 if (funName.size() == 0)
171 {
172 virtualFunctions.insert(callee);
173 }
174 else if (funName[0] == '~')
175 {
176 /*
177 * if the virtual callsite is calling a destructor, then all
178 * destructors in the ch will be valid
179 * class A { virtual ~A(){} };
180 * class B: public A { virtual ~B(){} };
181 * int main() {
182 * A *a = new B;
183 * delete a; /// the function name of this virtual callsite is ~A()
184 * }
185 */
186 if (calleeName[0] == '~')
187 {
188 virtualFunctions.insert(callee);
189 }
190 }
191 else
192 {
193 /*
194 * for other virtual function calls, the function name of the callsite
195 * and the function name of the target callee should match exactly
196 */
197 if (funName.compare(calleeName) == 0)
198 {
199 virtualFunctions.insert(callee);
200 }
201 }
202 }
203 }
204 }
205}
static bool checkArgTypes(const CallICFGNode *cs, const SVFFunction *fn)
Definition CHG.cpp:53
cJSON * child
Definition cJSON.cpp:2723
std::vector< const SVFFunction * > FuncVector
Definition CHG.h:121
const std::string & getName() const
Definition SVFValue.h:243

◆ getVirtualFunctionBasedonID()

const SVFFunction * SVF::CHGraph::getVirtualFunctionBasedonID ( u32_t  id) const
inline

Definition at line 278 of file CHG.h.

279 {
280 Map<const SVFFunction*, u32_t>::const_iterator it, eit;
281 for (it = virtualFunctionToIDMap.begin(), eit =
282 virtualFunctionToIDMap.end(); it != eit; ++it)
283 {
284 if (it->second == id)
285 return it->first;
286 }
287 return nullptr;
288 }
Map< const SVFFunction *, u32_t > virtualFunctionToIDMap
Definition CHG.h:331

◆ getVirtualFunctionID()

u32_t SVF::CHGraph::getVirtualFunctionID ( const SVFFunction vfn) const
inline

Definition at line 269 of file CHG.h.

270 {
271 Map<const SVFFunction*, u32_t>::const_iterator it =
273 if (it != virtualFunctionToIDMap.end())
274 return it->second;
275 else
276 return -1;
277 }

◆ printCH()

void CHGraph::printCH ( )

Definition at line 218 of file CHG.cpp.

219{
220 for (CHGraph::const_iterator it = this->begin(), eit = this->end();
221 it != eit; ++it)
222 {
223 const CHNode *node = it->second;
224 outs() << "class: " << node->getName() << "\n";
225 for (CHEdge::CHEdgeSetTy::const_iterator it = node->OutEdgeBegin();
226 it != node->OutEdgeEnd(); ++it)
227 {
228 if ((*it)->getEdgeType() == CHEdge::INHERITANCE)
229 outs() << (*it)->getDstNode()->getName() << " --inheritance--> "
230 << (*it)->getSrcNode()->getName() << "\n";
231 else
232 outs() << (*it)->getSrcNode()->getName() << " --instance--> "
233 << (*it)->getDstNode()->getName() << "\n";
234 }
235 }
236 outs() << '\n';
237}
@ INHERITANCE
Definition CHG.h:86
std::string getName() const
Definition CHG.h:130
iterator begin()
Iterators.
IDToNodeMapTy::const_iterator const_iterator
iterator OutEdgeEnd()
iterator OutEdgeBegin()
iterators

◆ view()

void CHGraph::view ( )

Definition at line 248 of file CHG.cpp.

249{
250 SVF::ViewGraph(this, "Class Hierarchy Graph");
251}
void ViewGraph(const GraphType &G, const std::string &name, bool ShortNames=false, GraphProgram::Name Program=GraphProgram::DOT)

Friends And Related Symbol Documentation

◆ CHGBuilder

Definition at line 237 of file CHG.h.

◆ SVFIRReader

Definition at line 236 of file CHG.h.

◆ SVFIRWriter

Definition at line 235 of file CHG.h.

Member Data Documentation

◆ buildingCHGTime

double SVF::CHGraph::buildingCHGTime
private

Definition at line 323 of file CHG.h.

◆ callNodeToCHAVFnsMap

CallNodeToVFunSetMap SVF::CHGraph::callNodeToCHAVFnsMap
private

Definition at line 334 of file CHG.h.

◆ callNodeToCHAVtblsMap

CallNodeToVTableSetMap SVF::CHGraph::callNodeToCHAVtblsMap
private

Definition at line 333 of file CHG.h.

◆ callNodeToClassesMap

CallNodeToCHNodesMap SVF::CHGraph::callNodeToClassesMap
private

Definition at line 329 of file CHG.h.

◆ classNameToAncestorsMap

NameToCHNodesMap SVF::CHGraph::classNameToAncestorsMap
private

Definition at line 326 of file CHG.h.

◆ classNameToDescendantsMap

NameToCHNodesMap SVF::CHGraph::classNameToDescendantsMap
private

Definition at line 325 of file CHG.h.

◆ classNameToInstAndDescsMap

NameToCHNodesMap SVF::CHGraph::classNameToInstAndDescsMap
private

Definition at line 327 of file CHG.h.

◆ classNameToNodeMap

Map<std::string, CHNode*> SVF::CHGraph::classNameToNodeMap
private

Definition at line 324 of file CHG.h.

◆ classNum

u32_t SVF::CHGraph::classNum
private

Definition at line 321 of file CHG.h.

◆ svfMod

SVFModule* SVF::CHGraph::svfMod
private

Definition at line 320 of file CHG.h.

◆ templateNameToInstancesMap

NameToCHNodesMap SVF::CHGraph::templateNameToInstancesMap
private

Definition at line 328 of file CHG.h.

◆ vfID

u32_t SVF::CHGraph::vfID
private

Definition at line 322 of file CHG.h.

◆ virtualFunctionToIDMap

Map<const SVFFunction*, u32_t> SVF::CHGraph::virtualFunctionToIDMap
private

Definition at line 331 of file CHG.h.


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