Static Value-Flow Analysis
Loading...
Searching...
No Matches
CFLGraph.h
Go to the documentation of this file.
1//===----- CFLGraph.h -- Graph for context-free language reachability analysis --//
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 * CFLGraph.h
25 *
26 * Created on: March 5, 2022
27 * Author: Yulei Sui
28 */
29
30#ifndef CFLG_H_
31#define CFLG_H_
32
33#include <fstream>
34#include <iostream>
35#include <string>
36#include "CFL/CFGrammar.h"
37#include "Graphs/GenericGraph.h"
38#include "Graphs/ConsG.h"
39
40namespace SVF
41{
42class CFLNode;
43
45
47{
48public:
50
55 ~CFLEdge() override = default;
56
57 inline GEdgeKind getEdgeKind() const
58 {
59 return this->getEdgeKindWithoutMask();
60 }
61
63 {
64 return (EdgeKindMask & this->getEdgeKindWithoutMask());
65 }
66
67 inline GEdgeKind getEdgeAttri() const
68 {
69 return (getEdgeKind() >> this->EdgeKindMaskBits);
70 }
71};
72
73
76{
77public:
80 {
81 }
82
83 ~CFLNode() override = default;
84
86 typedef std::map <GrammarBase::Symbol, CFLEdge::CFLEdgeSetTy> CFLEdgeDataTy;
87
88private:
91
92public:
97
102
104 {
105 assert(inEdge->getDstID() == this->getId());
107 bool added2 = inCFLEdges[s].insert(inEdge).second;
108
109 return added1 && added2;
110 }
111
113 {
114 return addInEdgeWithKind(inEdge, inEdge->getEdgeKind());
115 }
116
118 {
119 assert(outEdge->getSrcID() == this->getId());
121 bool added2 = outCFLEdges[s].insert(outEdge).second;
122
123 return added1 && added2;
124 }
125
127 {
128 return addOutEdgeWithKind(OutEdge, OutEdge->getEdgeKind());
129 }
130
132 {
134
135 GrammarBase::Symbol s = inEdge->getEdgeKind();
136 u32_t num2 = inCFLEdges[s].erase(inEdge);
137
138 return num1 && num2;
139 }
140
142 {
144
145 GrammarBase::Symbol s = outEdge->getEdgeKind();
146 u32_t num2 = outCFLEdges[s].erase(outEdge);
147
148 return num1 && num2;
149 }
150
152
153 static inline bool classof(const CFLNode *)
154 {
155 return true;
156 }
157
158 static inline bool classof(const GenericICFGNodeTy* node)
159 {
160 return node->getNodeKind() == CFLNodeKd;
161 }
162
163 static inline bool classof(const SVFBaseNode* node)
164 {
165 return node->getNodeKind() == CFLNodeKd;
166 }
168};
169
173{
174public:
179
181 {
182 startKind = kind;
183 }
184 ~CFLGraph() override = default;
185
186 Kind getStartKind() const;
187
188 virtual void addCFLNode(NodeID id, CFLNode* node);
189
190 virtual const CFLEdge* addCFLEdge(CFLNode* src, CFLNode* dst, CFLEdge::GEdgeFlag label);
191
192 virtual const CFLEdge* hasEdge(CFLNode* src, CFLNode* dst, CFLEdge::GEdgeFlag label);
193
194 void dump(const std::string& filename);
195
196 void view();
197
198 inline const CFLEdgeSet& getCFLEdges() const
199 {
200 return cflEdgeSet;
201 }
202
203private:
205};
206
207}
208
209namespace SVF
210{
211/* !
212 * GenericGraphTraits specializations for generic graph algorithms.
213 * Provide graph traits for traversing from a constraint node using standard graph traversals.
214 */
215template<> struct GenericGraphTraits<SVF::CFLNode*> : public GenericGraphTraits<SVF::GenericNode<SVF::CFLNode,SVF::CFLEdge>* >
216{
217};
218
220template<>
221struct GenericGraphTraits<Inverse<SVF::CFLNode *> > : public GenericGraphTraits<Inverse<SVF::GenericNode<SVF::CFLNode,SVF::CFLEdge>* > >
222{
223};
224
225template<> struct GenericGraphTraits<SVF::CFLGraph*> : public GenericGraphTraits<SVF::GenericGraph<SVF::CFLNode,SVF::CFLEdge>* >
226{
228};
229
230} // End namespace llvm
231
232#endif /* CFLG_H_ */
GEdgeKind getEdgeKindWithMask() const
Definition CFLGraph.h:62
CFLEdge(CFLNode *s, CFLNode *d, GEdgeFlag k=0)
Definition CFLGraph.h:51
~CFLEdge() override=default
GenericNode< CFLNode, CFLEdge >::GEdgeSetTy CFLEdgeSetTy
Definition CFLGraph.h:49
GEdgeKind getEdgeKind() const
Definition CFLGraph.h:57
GEdgeKind getEdgeAttri() const
Definition CFLGraph.h:67
GenericNode< CFLNode, CFLEdge >::GEdgeSetTy CFLEdgeSet
Definition CFLGraph.h:177
const CFLEdgeSet & getCFLEdges() const
Definition CFLGraph.h:198
CFLGraph(Kind kind)
Definition CFLGraph.h:180
virtual void addCFLNode(NodeID id, CFLNode *node)
Definition CFLGraph.cpp:42
CFGrammar::Kind Kind
Definition CFLGraph.h:176
Kind getStartKind() const
Definition CFLGraph.cpp:37
~CFLGraph() override=default
virtual const CFLEdge * addCFLEdge(CFLNode *src, CFLNode *dst, CFLEdge::GEdgeFlag label)
Definition CFLGraph.cpp:47
void dump(const std::string &filename)
Definition CFLGraph.cpp:73
virtual const CFLEdge * hasEdge(CFLNode *src, CFLNode *dst, CFLEdge::GEdgeFlag label)
Definition CFLGraph.cpp:63
CFGrammar::Symbol Symbol
Definition CFLGraph.h:175
CFLEdgeSet cflEdgeSet
Definition CFLGraph.h:204
bool addIngoingEdge(CFLEdge *inEdge)
Definition CFLGraph.h:112
bool addOutEdgeWithKind(CFLEdge *outEdge, GrammarBase::Symbol s)
Definition CFLGraph.h:117
bool addInEdgeWithKind(CFLEdge *inEdge, GrammarBase::Symbol s)
Definition CFLGraph.h:103
static bool classof(const CFLNode *)
Methods for support type inquiry through isa, cast, and dyn_cast:
Definition CFLGraph.h:153
CFLEdgeDataTy inCFLEdges
Definition CFLGraph.h:89
CFLEdgeDataTy outCFLEdges
Definition CFLGraph.h:90
const CFLEdge::CFLEdgeSetTy & getOutEdgeWithTy(GrammarBase::Symbol s)
Definition CFLGraph.h:98
bool removeCFLInEdge(CFLEdge *inEdge)
Definition CFLGraph.h:131
const CFLEdge::CFLEdgeSetTy & getInEdgeWithTy(GrammarBase::Symbol s)
Definition CFLGraph.h:93
bool addOutgoingEdge(CFLEdge *OutEdge)
Definition CFLGraph.h:126
bool removeCFLOutEdge(CFLEdge *outEdge)
Definition CFLGraph.h:141
~CFLNode() override=default
static bool classof(const GenericICFGNodeTy *node)
Definition CFLGraph.h:158
std::map< GrammarBase::Symbol, CFLEdge::CFLEdgeSetTy > CFLEdgeDataTy
Different Kind(label) associated edges set.
Definition CFLGraph.h:86
static bool classof(const SVFBaseNode *node)
Definition CFLGraph.h:163
CFLNode(NodeID i=0, GNodeK k=CFLNodeKd)
Definition CFLGraph.h:78
GEdgeKind getEdgeKindWithoutMask() const
static constexpr u64_t EdgeKindMask
static constexpr unsigned char EdgeKindMaskBits
We use the lower 8 bits to denote edge kind.
OrderedSet< EdgeType *, typename EdgeType::equalGEdge > GEdgeSetTy
Edge kind.
u32_t removeOutgoingEdge(EdgeType *edge)
bool addIncomingEdge(EdgeType *inEdge)
Add incoming and outgoing edges.
u32_t removeIncomingEdge(EdgeType *edge)
bool addOutgoingEdge(EdgeType *outEdge)
GNodeK getNodeKind() const
Get node kind.
for isBitcode
Definition BasicTypes.h:68
GenericEdge< CFLNode > GenericCFLEdgeTy
Definition CFLGraph.h:44
GenericNode< CFLNode, CFLEdge > GenericCFLNodeTy
Definition CFLGraph.h:74
u32_t NodeID
Definition GeneralType.h:55
GenericGraph< CFLNode, CFLEdge > GenericCFLGraphTy
Edge-labeled graph for CFL Reachability analysis.
Definition CFLGraph.h:171
llvm::IRBuilder IRBuilder
Definition BasicTypes.h:74
unsigned u32_t
Definition GeneralType.h:46