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