Static Value-Flow Analysis
Loading...
Searching...
No Matches
CHG.h
Go to the documentation of this file.
1//===----- CHG.h -- Class hierarchy graph ---------------------------//
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 * CHG.h (previously CHA.h)
25 *
26 * Created on: Apr 13, 2016
27 * Author: Xiaokang Fan
28 *
29 * Created on: Aug 24, 2019
30 * Author: Mohamad Barbar
31 */
32
33#ifndef CHA_H_
34#define CHA_H_
35
36#include "SVFIR/SVFModule.h"
37#include "Graphs/GenericGraph.h"
38#include "Util/WorkList.h"
39
40namespace SVF
41{
42
43class SVFModule;
44class CHNode;
45
48
51{
52public:
53 virtual ~CommonCHGraph() { };
55 {
57 DI
58 };
59
60 virtual bool csHasVFnsBasedonCHA(const CallICFGNode* cs) = 0;
61 virtual const VFunSet &getCSVFsBasedonCHA(const CallICFGNode* cs) = 0;
62 virtual bool csHasVtblsBasedonCHA(const CallICFGNode* cs) = 0;
63 virtual const VTableSet &getCSVtblsBasedonCHA(const CallICFGNode* cs) = 0;
64 virtual void getVFnsFromVtbls(const CallICFGNode* cs, const VTableSet& vtbls,
66
67 CHGKind getKind(void) const
68 {
69 return kind;
70 }
71
72protected:
74};
75
76
79{
80 friend class SVFIRWriter;
81 friend class SVFIRReader;
82
83public:
84 typedef enum
85 {
86 INHERITANCE = 0x1, // inheritance relation
87 INSTANTCE = 0x2 // template-instance relation
89
91
93 : GenericCHEdgeTy(s, d, k)
94 {
95 edgeType = et;
96 }
97
99 {
100 return edgeType;
101 }
102
103private:
105};
106
109{
110 friend class SVFIRWriter;
111 friend class SVFIRReader;
112
113public:
114 typedef enum
115 {
116 PURE_ABSTRACT = 0x1, // pure virtual abstract class
117 MULTI_INHERITANCE = 0x2, // multi inheritance class
118 TEMPLATE = 0x04 // template class
120
121 typedef std::vector<const SVFFunction*> FuncVector;
122
123 CHNode (const std::string& name, NodeID i = 0, GNodeK k = CHNodeKd):
125 {
126 }
128 {
129 }
130 std::string getName() const
131 {
132 return className;
133 }
135
136 inline void setFlag(CLASSATTR mask)
137 {
138 flags |= mask;
139 }
140 inline bool hasFlag(CLASSATTR mask) const
141 {
142 return (flags & mask) == mask;
143 }
145
147
148 inline void setPureAbstract()
149 {
151 }
153 {
155 }
156 inline void setTemplate()
157 {
159 }
160 inline bool isPureAbstract() const
161 {
162 return hasFlag(PURE_ABSTRACT);
163 }
164 inline bool isMultiInheritance() const
165 {
167 }
168 inline bool isTemplate() const
169 {
170 return hasFlag(TEMPLATE);
171 }
173
178 const std::vector<FuncVector> &getVirtualFunctionVectors() const
179 {
181 }
183
185 {
186 return vtable;
187 }
188
190 {
191 vtable = vtbl;
192 }
193
195
196 static inline bool classof(const CHNode *)
197 {
198 return true;
199 }
200
201 static inline bool classof(const GenericCHNodeTy * node)
202 {
203 return node->getNodeKind() == CHNodeKd;
204 }
205
206 static inline bool classof(const SVFBaseNode* node)
207 {
208 return node->getNodeKind() == CHNodeKd;
209 }
211
212private:
214 std::string className;
215 size_t flags;
216 /*
217 * virtual functions inherited from different classes are separately stored
218 * to model different vtables inherited from different fathers.
219 *
220 * Example:
221 * class C: public A, public B
222 * vtableC = {Af1, Af2, ..., inttoptr, Bg1, Bg2, ...} ("inttoptr"
223 * instruction works as the delimiter for separating virtual functions
224 * inherited from different classes)
225 *
226 * virtualFunctionVectors = {{Af1, Af2, ...}, {Bg1, Bg2, ...}}
227 */
228 std::vector<FuncVector> virtualFunctionVectors;
229};
230
234{
235 friend class SVFIRWriter;
236 friend class SVFIRReader;
237 friend class CHGBuilder;
238
239public:
243
247
248 typedef enum
249 {
250 CONSTRUCTOR = 0x1, // connect node based on constructor
251 DESTRUCTOR = 0x2 // connect node based on destructor
253
254 CHGraph(SVFModule* svfModule): svfMod(svfModule), classNum(0), vfID(0), buildingCHGTime(0)
255 {
256 this->kind = Standard;
257 }
258 ~CHGraph() override = default;
259
260 void addEdge(const std::string className,
261 const std::string baseClassName,
262 CHEdge::CHEDGETYPE edgeType);
263 CHNode *getNode(const std::string name) const;
264 void getVFnsFromVtbls(const CallICFGNode* cs, const VTableSet &vtbls, VFunSet &virtualFunctions) override;
265 void dump(const std::string& filename);
266 void view();
267 void printCH();
268
270 {
273 if (it != virtualFunctionToIDMap.end())
274 return it->second;
275 else
276 return -1;
277 }
279 {
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 }
289
290 inline void addInstances(const std::string templateName, CHNode* node)
291 {
292 NameToCHNodesMap::iterator it = templateNameToInstancesMap.find(
294 if (it != templateNameToInstancesMap.end())
295 it->second.insert(node);
296 else
298 }
299 inline const CHNodeSetTy &getDescendants(const std::string className)
300 {
301 return classNameToDescendantsMap[className];
302 }
303 inline const CHNodeSetTy &getInstances(const std::string className)
304 {
305 return templateNameToInstancesMap[className];
306 }
307
308 bool csHasVtblsBasedonCHA(const CallICFGNode* cs) override;
309 bool csHasVFnsBasedonCHA(const CallICFGNode* cs) override;
310 const VTableSet &getCSVtblsBasedonCHA(const CallICFGNode* cs) override;
311 const VFunSet &getCSVFsBasedonCHA(const CallICFGNode* cs) override;
312
313 static inline bool classof(const CommonCHGraph *chg)
314 {
315 return chg->getKind() == Standard;
316 }
317
318
319private:
330
332
335};
336
337} // End namespace SVF
338
339namespace SVF
340{
341/* !
342 * GenericGraphTraits specializations for generic graph algorithms.
343 * Provide graph traits for traversing from a constraint node using standard graph traversals.
344 */
345template <>
347 : public GenericGraphTraits<SVF::GenericNode<SVF::CHNode, SVF::CHEdge>*>
348{
349};
350
353template <>
355 : public GenericGraphTraits<
356 Inverse<SVF::GenericNode<SVF::CHNode, SVF::CHEdge>*>>
357{
358};
359
360template <>
362 : public GenericGraphTraits<SVF::GenericGraph<SVF::CHNode, SVF::CHEdge>*>
363{
365};
366
367} // End namespace llvm
368
369#endif /* CHA_H_ */
const char *const name
Definition cJSON.h:264
CHEDGETYPE
Definition CHG.h:85
@ INHERITANCE
Definition CHG.h:86
@ INSTANTCE
Definition CHG.h:87
CHEDGETYPE edgeType
Definition CHG.h:104
CHEDGETYPE getEdgeType() const
Definition CHG.h:98
GenericNode< CHNode, CHEdge >::GEdgeSetTy CHEdgeSetTy
Definition CHG.h:90
CHEdge(CHNode *s, CHNode *d, CHEDGETYPE et, GEdgeFlag k=0)
Definition CHG.h:92
NameToCHNodesMap classNameToAncestorsMap
Definition CHG.h:326
CallNodeToCHNodesMap callNodeToClassesMap
Definition CHG.h:329
CallNodeToVFunSetMap callNodeToCHAVFnsMap
Definition CHG.h:334
void dump(const std::string &filename)
Definition CHG.cpp:242
bool csHasVtblsBasedonCHA(const CallICFGNode *cs) override
Definition CHG.cpp:74
@ CONSTRUCTOR
Definition CHG.h:250
@ DESTRUCTOR
Definition CHG.h:251
const CHNodeSetTy & getInstances(const std::string className)
Definition CHG.h:303
void addEdge(const std::string className, const std::string baseClassName, CHEdge::CHEDGETYPE edgeType)
Definition CHG.cpp:97
bool csHasVFnsBasedonCHA(const CallICFGNode *cs) override
Definition CHG.cpp:79
const VFunSet & getCSVFsBasedonCHA(const CallICFGNode *cs) override
Definition CHG.cpp:90
const VTableSet & getCSVtblsBasedonCHA(const CallICFGNode *cs) override
Definition CHG.cpp:84
void addInstances(const std::string templateName, CHNode *node)
Definition CHG.h:290
SVFModule * svfMod
Definition CHG.h:320
~CHGraph() override=default
Map< const SVFFunction *, u32_t > virtualFunctionToIDMap
Definition CHG.h:331
u32_t getVirtualFunctionID(const SVFFunction *vfn) const
Definition CHG.h:269
void getVFnsFromVtbls(const CallICFGNode *cs, const VTableSet &vtbls, VFunSet &virtualFunctions) override
Definition CHG.cpp:124
CHNode * getNode(const std::string name) const
Definition CHG.cpp:112
void view()
Definition CHG.cpp:248
Map< const ICFGNode *, VFunSet > CallNodeToVFunSetMap
Definition CHG.h:246
NameToCHNodesMap classNameToInstAndDescsMap
Definition CHG.h:327
u32_t vfID
Definition CHG.h:322
double buildingCHGTime
Definition CHG.h:323
NameToCHNodesMap classNameToDescendantsMap
Definition CHG.h:325
NameToCHNodesMap templateNameToInstancesMap
Definition CHG.h:328
Set< const CHNode * > CHNodeSetTy
Definition CHG.h:240
Map< const ICFGNode *, VTableSet > CallNodeToVTableSetMap
Definition CHG.h:245
u32_t classNum
Definition CHG.h:321
Map< const ICFGNode *, CHNodeSetTy > CallNodeToCHNodesMap
Definition CHG.h:244
void printCH()
Definition CHG.cpp:218
Map< std::string, CHNode * > classNameToNodeMap
Definition CHG.h:324
static bool classof(const CommonCHGraph *chg)
Definition CHG.h:313
const SVFFunction * getVirtualFunctionBasedonID(u32_t id) const
Definition CHG.h:278
Map< std::string, CHNodeSetTy > NameToCHNodesMap
Definition CHG.h:242
CallNodeToVTableSetMap callNodeToCHAVtblsMap
Definition CHG.h:333
FIFOWorkList< const CHNode * > WorkList
Definition CHG.h:241
const CHNodeSetTy & getDescendants(const std::string className)
Definition CHG.h:299
CHGraph(SVFModule *svfModule)
Definition CHG.h:254
void setTemplate()
Definition CHG.h:156
const SVFGlobalValue * vtable
Definition CHG.h:213
void getVirtualFunctions(u32_t idx, FuncVector &virtualFunctions) const
Definition CHG.cpp:208
bool isMultiInheritance() const
Definition CHG.h:164
bool isPureAbstract() const
Definition CHG.h:160
void setMultiInheritance()
Definition CHG.h:152
static bool classof(const CHNode *)
Methods for support type inquiry through isa, cast, and dyn_cast:
Definition CHG.h:196
size_t flags
Definition CHG.h:215
@ TEMPLATE
Definition CHG.h:118
@ PURE_ABSTRACT
Definition CHG.h:116
@ MULTI_INHERITANCE
Definition CHG.h:117
CHNode(const std::string &name, NodeID i=0, GNodeK k=CHNodeKd)
Definition CHG.h:123
void addVirtualFunctionVector(FuncVector vfuncvec)
Definition CHG.h:174
const SVFGlobalValue * getVTable() const
Definition CHG.h:184
std::vector< FuncVector > virtualFunctionVectors
Definition CHG.h:228
std::vector< const SVFFunction * > FuncVector
Definition CHG.h:121
const std::vector< FuncVector > & getVirtualFunctionVectors() const
Definition CHG.h:178
static bool classof(const GenericCHNodeTy *node)
Definition CHG.h:201
void setVTable(const SVFGlobalValue *vtbl)
Definition CHG.h:189
void setFlag(CLASSATTR mask)
Flags.
Definition CHG.h:136
void setPureAbstract()
Attribute.
Definition CHG.h:148
static bool classof(const SVFBaseNode *node)
Definition CHG.h:206
std::string className
Definition CHG.h:214
bool hasFlag(CLASSATTR mask) const
Definition CHG.h:140
std::string getName() const
Definition CHG.h:130
~CHNode()
Definition CHG.h:127
bool isTemplate() const
Definition CHG.h:168
Common base for class hierarchy graph. Only implements what PointerAnalysis needs.
Definition CHG.h:51
virtual const VFunSet & getCSVFsBasedonCHA(const CallICFGNode *cs)=0
CHGKind kind
Definition CHG.h:73
virtual bool csHasVFnsBasedonCHA(const CallICFGNode *cs)=0
virtual const VTableSet & getCSVtblsBasedonCHA(const CallICFGNode *cs)=0
virtual void getVFnsFromVtbls(const CallICFGNode *cs, const VTableSet &vtbls, VFunSet &virtualFunctions)=0
virtual bool csHasVtblsBasedonCHA(const CallICFGNode *cs)=0
CHGKind getKind(void) const
Definition CHG.h:67
virtual ~CommonCHGraph()
Definition CHG.h:53
OrderedSet< EdgeType *, typename EdgeType::equalGEdge > GEdgeSetTy
Edge kind.
GNodeK getNodeKind() const
Get node kind.
for isBitcode
Definition BasicTypes.h:68
Set< const SVFGlobalValue * > VTableSet
Definition CHG.h:46
u32_t NodeID
Definition GeneralType.h:55
GenericGraph< CHNode, CHEdge > GenericCHGraphTy
class hierarchy graph
Definition CHG.h:232
GenericEdge< CHNode > GenericCHEdgeTy
Definition CHG.h:77
std::unordered_map< Key, Value, Hash, KeyEqual, Allocator > Map
llvm::IRBuilder IRBuilder
Definition BasicTypes.h:74
GenericNode< CHNode, CHEdge > GenericCHNodeTy
Definition CHG.h:107
Set< const SVFFunction * > VFunSet
Definition CHG.h:47
unsigned u32_t
Definition GeneralType.h:46