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 "Util/ThreadAPI.h"
37#include "Graphs/GenericGraph.h"
38#include "Util/WorkList.h"
39
40namespace SVF
41{
42
43class CHNode;
44class GlobalObjVar;
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
81public:
82 typedef enum
83 {
84 INHERITANCE = 0x1, // inheritance relation
85 INSTANTCE = 0x2 // template-instance relation
87
89
91 : GenericCHEdgeTy(s, d, k)
92 {
93 edgeType = et;
94 }
95
97 {
98 return edgeType;
99 }
100
101private:
103};
104
107{
108 friend class GraphDBClient;
109
110public:
111 typedef enum
112 {
113 PURE_ABSTRACT = 0x1, // pure virtual abstract class
114 MULTI_INHERITANCE = 0x2, // multi inheritance class
115 TEMPLATE = 0x04 // template class
117
118 typedef std::vector<const FunObjVar*> FuncVector;
119
120 CHNode (const std::string& name, NodeID i = 0, GNodeK k = CHNodeKd):
122 {
123 }
125 {
126 }
127 virtual const std::string& getName() const
128 {
129 return className;
130 }
132
133 inline void setFlag(CLASSATTR mask)
134 {
135 flags |= mask;
136 }
137 inline bool hasFlag(CLASSATTR mask) const
138 {
139 return (flags & mask) == mask;
140 }
142
144
145 inline void setPureAbstract()
146 {
148 }
150 {
152 }
153 inline void setTemplate()
154 {
156 }
157 inline bool isPureAbstract() const
158 {
159 return hasFlag(PURE_ABSTRACT);
160 }
161 inline bool isMultiInheritance() const
162 {
164 }
165 inline bool isTemplate() const
166 {
167 return hasFlag(TEMPLATE);
168 }
170
175 const std::vector<FuncVector> &getVirtualFunctionVectors() const
176 {
178 }
180
181 const GlobalObjVar *getVTable() const
182 {
183 return vtable;
184 }
185
187 {
188 vtable = vtbl;
189 }
190
192
193 static inline bool classof(const CHNode *)
194 {
195 return true;
196 }
197
198 static inline bool classof(const GenericCHNodeTy * node)
199 {
200 return node->getNodeKind() == CHNodeKd;
201 }
202
203 static inline bool classof(const SVFValue* node)
204 {
205 return node->getNodeKind() == CHNodeKd;
206 }
208
209private:
211 std::string className;
212 size_t flags;
213 /*
214 * virtual functions inherited from different classes are separately stored
215 * to model different vtables inherited from different fathers.
216 *
217 * Example:
218 * class C: public A, public B
219 * vtableC = {Af1, Af2, ..., inttoptr, Bg1, Bg2, ...} ("inttoptr"
220 * instruction works as the delimiter for separating virtual functions
221 * inherited from different classes)
222 *
223 * virtualFunctionVectors = {{Af1, Af2, ...}, {Bg1, Bg2, ...}}
224 */
225 std::vector<FuncVector> virtualFunctionVectors;
226
227protected:
228 inline size_t getFlags() const
229 {
230 return flags;
231 }
232 inline void setFlags(size_t f)
233 {
234 flags = f;
235 }
236};
237
241{
242 friend class CHGBuilder;
243 friend class GraphDBClient;
244
245public:
249
253
254 typedef enum
255 {
256 CONSTRUCTOR = 0x1, // connect node based on constructor
257 DESTRUCTOR = 0x2 // connect node based on destructor
259
261 {
262 this->kind = Standard;
263 }
264 ~CHGraph() override = default;
265
266 void addEdge(const std::string className,
267 const std::string baseClassName,
268 CHEdge::CHEDGETYPE edgeType);
269 CHNode *getNode(const std::string name) const;
270 void getVFnsFromVtbls(const CallICFGNode* cs, const VTableSet &vtbls, VFunSet &virtualFunctions) override;
271 void dump(const std::string& filename);
272 void view();
273 void printCH();
274
276 {
279 if (it != virtualFunctionToIDMap.end())
280 return it->second;
281 else
282 return -1;
283 }
285 {
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 }
295
296 inline void addInstances(const std::string templateName, CHNode* node)
297 {
298 NameToCHNodesMap::iterator it = templateNameToInstancesMap.find(
300 if (it != templateNameToInstancesMap.end())
301 it->second.insert(node);
302 else
304 }
305 inline const CHNodeSetTy &getDescendants(const std::string className)
306 {
307 return classNameToDescendantsMap[className];
308 }
309 inline const CHNodeSetTy &getInstances(const std::string className)
310 {
311 return templateNameToInstancesMap[className];
312 }
313
314 bool csHasVtblsBasedonCHA(const CallICFGNode* cs) override;
315 bool csHasVFnsBasedonCHA(const CallICFGNode* cs) override;
316 const VTableSet &getCSVtblsBasedonCHA(const CallICFGNode* cs) override;
317 const VFunSet &getCSVFsBasedonCHA(const CallICFGNode* cs) override;
318
319 static inline bool classof(const CommonCHGraph *chg)
320 {
321 return chg->getKind() == Standard;
322 }
323
324
325private:
335
337
340};
341
342} // End namespace SVF
343
344namespace SVF
345{
346/* !
347 * GenericGraphTraits specializations for generic graph algorithms.
348 * Provide graph traits for traversing from a constraint node using standard graph traversals.
349 */
350template <>
352 : public GenericGraphTraits<SVF::GenericNode<SVF::CHNode, SVF::CHEdge>*>
353{
354};
355
358template <>
360 : public GenericGraphTraits<
361 Inverse<SVF::GenericNode<SVF::CHNode, SVF::CHEdge>*>>
362{
363};
364
365template <>
367 : public GenericGraphTraits<SVF::GenericGraph<SVF::CHNode, SVF::CHEdge>*>
368{
370};
371
372} // End namespace llvm
373
374#endif /* CHA_H_ */
const char *const name
Definition cJSON.h:264
CHEDGETYPE
Definition CHG.h:83
@ INHERITANCE
Definition CHG.h:84
@ INSTANTCE
Definition CHG.h:85
CHEDGETYPE edgeType
Definition CHG.h:102
CHEDGETYPE getEdgeType() const
Definition CHG.h:96
GenericNode< CHNode, CHEdge >::GEdgeSetTy CHEdgeSetTy
Definition CHG.h:88
CHEdge(CHNode *s, CHNode *d, CHEDGETYPE et, GEdgeFlag k=0)
Definition CHG.h:90
CHGraph()
Definition CHG.h:260
NameToCHNodesMap classNameToAncestorsMap
Definition CHG.h:331
CallNodeToCHNodesMap callNodeToClassesMap
Definition CHG.h:334
CallNodeToVFunSetMap callNodeToCHAVFnsMap
Definition CHG.h:339
void dump(const std::string &filename)
Definition CHG.cpp:242
bool csHasVtblsBasedonCHA(const CallICFGNode *cs) override
Definition CHG.cpp:74
const FunObjVar * getVirtualFunctionBasedonID(u32_t id) const
Definition CHG.h:284
@ CONSTRUCTOR
Definition CHG.h:256
@ DESTRUCTOR
Definition CHG.h:257
const CHNodeSetTy & getInstances(const std::string className)
Definition CHG.h:309
void addEdge(const std::string className, const std::string baseClassName, CHEdge::CHEDGETYPE edgeType)
Definition CHG.cpp:97
Map< const FunObjVar *, u32_t > virtualFunctionToIDMap
Definition CHG.h:336
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:296
~CHGraph() override=default
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:252
NameToCHNodesMap classNameToInstAndDescsMap
Definition CHG.h:332
friend class GraphDBClient
Definition CHG.h:243
u32_t vfID
Definition CHG.h:327
double buildingCHGTime
Definition CHG.h:328
NameToCHNodesMap classNameToDescendantsMap
Definition CHG.h:330
NameToCHNodesMap templateNameToInstancesMap
Definition CHG.h:333
Set< const CHNode * > CHNodeSetTy
Definition CHG.h:246
Map< const ICFGNode *, VTableSet > CallNodeToVTableSetMap
Definition CHG.h:251
u32_t classNum
Definition CHG.h:326
Map< const ICFGNode *, CHNodeSetTy > CallNodeToCHNodesMap
Definition CHG.h:250
void printCH()
Definition CHG.cpp:218
Map< std::string, CHNode * > classNameToNodeMap
Definition CHG.h:329
static bool classof(const CommonCHGraph *chg)
Definition CHG.h:319
Map< std::string, CHNodeSetTy > NameToCHNodesMap
Definition CHG.h:248
CallNodeToVTableSetMap callNodeToCHAVtblsMap
Definition CHG.h:338
FIFOWorkList< const CHNode * > WorkList
Definition CHG.h:247
const CHNodeSetTy & getDescendants(const std::string className)
Definition CHG.h:305
u32_t getVirtualFunctionID(const FunObjVar *vfn) const
Definition CHG.h:275
void setTemplate()
Definition CHG.h:153
virtual const std::string & getName() const
Definition CHG.h:127
void setVTable(const GlobalObjVar *vtbl)
Definition CHG.h:186
void getVirtualFunctions(u32_t idx, FuncVector &virtualFunctions) const
Definition CHG.cpp:208
size_t getFlags() const
Definition CHG.h:228
bool isMultiInheritance() const
Definition CHG.h:161
bool isPureAbstract() const
Definition CHG.h:157
static bool classof(const SVFValue *node)
Definition CHG.h:203
void setMultiInheritance()
Definition CHG.h:149
static bool classof(const CHNode *)
Methods for support type inquiry through isa, cast, and dyn_cast:
Definition CHG.h:193
size_t flags
Definition CHG.h:212
const GlobalObjVar * vtable
Definition CHG.h:210
@ TEMPLATE
Definition CHG.h:115
@ PURE_ABSTRACT
Definition CHG.h:113
@ MULTI_INHERITANCE
Definition CHG.h:114
CHNode(const std::string &name, NodeID i=0, GNodeK k=CHNodeKd)
Definition CHG.h:120
void addVirtualFunctionVector(FuncVector vfuncvec)
Definition CHG.h:171
std::vector< FuncVector > virtualFunctionVectors
Definition CHG.h:225
const std::vector< FuncVector > & getVirtualFunctionVectors() const
Definition CHG.h:175
static bool classof(const GenericCHNodeTy *node)
Definition CHG.h:198
const GlobalObjVar * getVTable() const
Definition CHG.h:181
friend class GraphDBClient
Definition CHG.h:108
void setFlag(CLASSATTR mask)
Flags.
Definition CHG.h:133
void setPureAbstract()
Attribute.
Definition CHG.h:145
std::string className
Definition CHG.h:211
bool hasFlag(CLASSATTR mask) const
Definition CHG.h:137
std::vector< const FunObjVar * > FuncVector
Definition CHG.h:118
void setFlags(size_t f)
Definition CHG.h:232
~CHNode()
Definition CHG.h:124
bool isTemplate() const
Definition CHG.h:165
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.
Definition SVFValue.h:166
std::string name
Definition SVFValue.h:210
for isBitcode
Definition BasicTypes.h:68
u32_t NodeID
Definition GeneralType.h:56
Set< const GlobalObjVar * > VTableSet
Definition CHG.h:46
GenericGraph< CHNode, CHEdge > GenericCHGraphTy
class hierarchy graph
Definition CHG.h:239
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:105
unsigned u32_t
Definition GeneralType.h:47
Set< const FunObjVar * > VFunSet
Definition CHG.h:47