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 ()
 
 ~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 FunObjVar *vfn) const
 
const FunObjVargetVirtualFunctionBasedonID (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

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 FunObjVar *, u32_tvirtualFunctionToIDMap
 
CallNodeToVTableSetMap callNodeToCHAVtblsMap
 
CallNodeToVFunSetMap callNodeToCHAVFnsMap
 

Friends

class CHGBuilder
 
class GraphDBClient
 

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 240 of file CHG.h.

Member Typedef Documentation

◆ CallNodeToCHNodesMap

Definition at line 250 of file CHG.h.

◆ CallNodeToVFunSetMap

Definition at line 252 of file CHG.h.

◆ CallNodeToVTableSetMap

Definition at line 251 of file CHG.h.

◆ CHNodeSetTy

Definition at line 246 of file CHG.h.

◆ NameToCHNodesMap

Definition at line 248 of file CHG.h.

◆ WorkList

Definition at line 247 of file CHG.h.

Member Enumeration Documentation

◆ RELATIONTYPE

Enumerator
CONSTRUCTOR 
DESTRUCTOR 

Definition at line 254 of file CHG.h.

255 {
256 CONSTRUCTOR = 0x1, // connect node based on constructor
257 DESTRUCTOR = 0x2 // connect node based on destructor
258 } RELATIONTYPE;
@ CONSTRUCTOR
Definition CHG.h:256
@ DESTRUCTOR
Definition CHG.h:257

Constructor & Destructor Documentation

◆ CHGraph()

SVF::CHGraph::CHGraph ( )
inline

Definition at line 260 of file CHG.h.

260 : classNum(0), vfID(0), buildingCHGTime(0)
261 {
262 this->kind = Standard;
263 }
u32_t vfID
Definition CHG.h:327
double buildingCHGTime
Definition CHG.h:328
u32_t classNum
Definition CHG.h:326
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 98 of file CHG.cpp.

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

◆ addInstances()

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

Definition at line 296 of file CHG.h.

297 {
298 NameToCHNodesMap::iterator it = templateNameToInstancesMap.find(
300 if (it != templateNameToInstancesMap.end())
301 it->second.insert(node);
302 else
304 }
NameToCHNodesMap templateNameToInstancesMap
Definition CHG.h:333

◆ classof()

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

Definition at line 319 of file CHG.h.

320 {
321 return chg->getKind() == Standard;
322 }

◆ csHasVFnsBasedonCHA()

bool CHGraph::csHasVFnsBasedonCHA ( const CallICFGNode cs)
overridevirtual

Implements SVF::CommonCHGraph.

Definition at line 80 of file CHG.cpp.

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

◆ csHasVtblsBasedonCHA()

bool CHGraph::csHasVtblsBasedonCHA ( const CallICFGNode cs)
overridevirtual

Implements SVF::CommonCHGraph.

Definition at line 75 of file CHG.cpp.

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

◆ dump()

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

Dump call graph into dot file

Definition at line 243 of file CHG.cpp.

244{
246 printCH();
247}
void printCH()
Definition CHG.cpp:219
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:52

◆ getCSVFsBasedonCHA()

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

Implements SVF::CommonCHGraph.

Definition at line 91 of file CHG.cpp.

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

◆ getCSVtblsBasedonCHA()

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

Implements SVF::CommonCHGraph.

Definition at line 85 of file CHG.cpp.

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

◆ getDescendants()

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

Definition at line 305 of file CHG.h.

306 {
307 return classNameToDescendantsMap[className];
308 }
NameToCHNodesMap classNameToDescendantsMap
Definition CHG.h:330

◆ getInstances()

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

Definition at line 309 of file CHG.h.

310 {
311 return templateNameToInstancesMap[className];
312 }

◆ getNode()

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

Definition at line 113 of file CHG.cpp.

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

◆ 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 125 of file CHG.cpp.

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

◆ getVirtualFunctionBasedonID()

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

Definition at line 284 of file CHG.h.

285 {
286 Map<const FunObjVar*, u32_t>::const_iterator it, eit;
287 for (it = virtualFunctionToIDMap.begin(), eit =
288 virtualFunctionToIDMap.end(); it != eit; ++it)
289 {
290 if (it->second == id)
291 return it->first;
292 }
293 return nullptr;
294 }
Map< const FunObjVar *, u32_t > virtualFunctionToIDMap
Definition CHG.h:336

◆ getVirtualFunctionID()

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

Definition at line 275 of file CHG.h.

276 {
277 Map<const FunObjVar*, u32_t>::const_iterator it =
279 if (it != virtualFunctionToIDMap.end())
280 return it->second;
281 else
282 return -1;
283 }

◆ printCH()

void CHGraph::printCH ( )

Definition at line 219 of file CHG.cpp.

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

◆ view()

void CHGraph::view ( )

Definition at line 249 of file CHG.cpp.

250{
251 SVF::ViewGraph(this, "Class Hierarchy Graph");
252}
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 242 of file CHG.h.

◆ GraphDBClient

friend class GraphDBClient
friend

Definition at line 243 of file CHG.h.

Member Data Documentation

◆ buildingCHGTime

double SVF::CHGraph::buildingCHGTime
private

Definition at line 328 of file CHG.h.

◆ callNodeToCHAVFnsMap

CallNodeToVFunSetMap SVF::CHGraph::callNodeToCHAVFnsMap
private

Definition at line 339 of file CHG.h.

◆ callNodeToCHAVtblsMap

CallNodeToVTableSetMap SVF::CHGraph::callNodeToCHAVtblsMap
private

Definition at line 338 of file CHG.h.

◆ callNodeToClassesMap

CallNodeToCHNodesMap SVF::CHGraph::callNodeToClassesMap
private

Definition at line 334 of file CHG.h.

◆ classNameToAncestorsMap

NameToCHNodesMap SVF::CHGraph::classNameToAncestorsMap
private

Definition at line 331 of file CHG.h.

◆ classNameToDescendantsMap

NameToCHNodesMap SVF::CHGraph::classNameToDescendantsMap
private

Definition at line 330 of file CHG.h.

◆ classNameToInstAndDescsMap

NameToCHNodesMap SVF::CHGraph::classNameToInstAndDescsMap
private

Definition at line 332 of file CHG.h.

◆ classNameToNodeMap

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

Definition at line 329 of file CHG.h.

◆ classNum

u32_t SVF::CHGraph::classNum
private

Definition at line 326 of file CHG.h.

◆ templateNameToInstancesMap

NameToCHNodesMap SVF::CHGraph::templateNameToInstancesMap
private

Definition at line 333 of file CHG.h.

◆ vfID

u32_t SVF::CHGraph::vfID
private

Definition at line 327 of file CHG.h.

◆ virtualFunctionToIDMap

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

Definition at line 336 of file CHG.h.


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