svf-lib 1.0.1613 → 1.0.1614
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/SVF-linux/Release-build/svf/libSvfCore.a +0 -0
- package/SVF-linux/Release-build/svf-llvm/libSvfLLVM.a +0 -0
- package/SVF-linux/svf/include/CFL/CFGNormalizer.h +11 -11
- package/SVF-linux/svf/include/CFL/CFGrammar.h +468 -0
- package/SVF-linux/svf/include/CFL/CFLAlias.h +2 -2
- package/SVF-linux/svf/include/CFL/CFLBase.h +2 -2
- package/SVF-linux/svf/include/CFL/CFLGramGraphChecker.h +1 -1
- package/SVF-linux/svf/include/CFL/CFLGraphBuilder.h +30 -81
- package/SVF-linux/svf/include/CFL/CFLSolver.h +8 -8
- package/SVF-linux/svf/include/CFL/GrammarBuilder.h +1 -1
- package/SVF-linux/svf/include/CFL/grammar/PAGGrammar.txt +15 -0
- package/SVF-linux/svf/include/CFL/grammar/PEGGrammar.txt +11 -0
- package/SVF-linux/svf/include/CFL/grammar/VFGGrammar.txt +7 -0
- package/SVF-linux/svf/include/Graphs/CFLGraph.h +3 -3
- package/package.json +1 -1
|
Binary file
|
|
Binary file
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
#ifndef INCLUDE_CFL_CFGNORMALIZER_H_
|
|
31
31
|
#define INCLUDE_CFL_CFGNORMALIZER_H_
|
|
32
32
|
|
|
33
|
-
#include "
|
|
33
|
+
#include "CFGrammar.h"
|
|
34
34
|
|
|
35
35
|
namespace SVF
|
|
36
36
|
{
|
|
@@ -53,30 +53,30 @@ public:
|
|
|
53
53
|
}
|
|
54
54
|
|
|
55
55
|
/// Binary Normal Form(BNF) normalization with variable attribute expanded
|
|
56
|
-
|
|
56
|
+
CFGrammar* normalize(GrammarBase *generalGrammar);
|
|
57
57
|
|
|
58
58
|
/// Expand every variable attribute in rawProductions of grammarbase
|
|
59
|
-
|
|
59
|
+
CFGrammar* fillAttribute(CFGrammar *grammar, const Map<CFGrammar::Kind, Set<CFGrammar::Attribute>>& kindToAttrsMap);
|
|
60
60
|
|
|
61
61
|
private:
|
|
62
62
|
/// Add nonterminal to tranfer long rules to binary rules
|
|
63
|
-
void ebnf_bin(
|
|
63
|
+
void ebnf_bin(CFGrammar *grammar);
|
|
64
64
|
|
|
65
|
-
void ebnfSignReplace(char sign,
|
|
65
|
+
void ebnfSignReplace(char sign, CFGrammar *grammar);
|
|
66
66
|
|
|
67
|
-
void barReplace(
|
|
67
|
+
void barReplace(CFGrammar *grammar);
|
|
68
68
|
|
|
69
|
-
void insertToCFLGrammar(
|
|
69
|
+
void insertToCFLGrammar(CFGrammar *grammar, GrammarBase::Production &prod);
|
|
70
70
|
|
|
71
|
-
int ebnfBracketMatch(GrammarBase::Production& prod, int i,
|
|
71
|
+
int ebnfBracketMatch(GrammarBase::Production& prod, int i, CFGrammar *grammar) ;
|
|
72
72
|
|
|
73
73
|
GrammarBase::Symbol check_head(GrammarBase::SymbolMap<GrammarBase::Symbol, GrammarBase::Productions>& grammar, GrammarBase::Production& rule);
|
|
74
74
|
|
|
75
|
-
void strTrans(std::string strPro,
|
|
75
|
+
void strTrans(std::string strPro, CFGrammar *grammar, GrammarBase::Production& normalProd);
|
|
76
76
|
|
|
77
|
-
void getFilledProductions(GrammarBase::Production &prod,const NodeSet& nodeSet,
|
|
77
|
+
void getFilledProductions(GrammarBase::Production &prod,const NodeSet& nodeSet, CFGrammar *grammar, GrammarBase::Productions& normalProds);
|
|
78
78
|
|
|
79
|
-
void removeFirstSymbol(
|
|
79
|
+
void removeFirstSymbol(CFGrammar *grammar);
|
|
80
80
|
};
|
|
81
81
|
|
|
82
82
|
} // End namespace SVF
|
|
@@ -0,0 +1,468 @@
|
|
|
1
|
+
//===----- CFGrammar.h -- Context-free grammar --------------------------//
|
|
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
|
+
* CFGrammar.h
|
|
25
|
+
*
|
|
26
|
+
* Created on: March 5, 2022
|
|
27
|
+
* Author: Yulei Sui
|
|
28
|
+
*/
|
|
29
|
+
#ifndef CFLGrammar_H_
|
|
30
|
+
#define CFLGrammar_H_
|
|
31
|
+
#include "SVFIR/SVFType.h"
|
|
32
|
+
|
|
33
|
+
namespace SVF
|
|
34
|
+
{
|
|
35
|
+
|
|
36
|
+
class GrammarBase
|
|
37
|
+
{
|
|
38
|
+
public:
|
|
39
|
+
typedef u32_t Kind;
|
|
40
|
+
typedef u32_t Attribute;
|
|
41
|
+
typedef u32_t VariableAttribute;
|
|
42
|
+
typedef struct Symbol
|
|
43
|
+
{
|
|
44
|
+
Kind kind: 8;
|
|
45
|
+
Attribute attribute: 16;
|
|
46
|
+
VariableAttribute variableAttribute: 8;
|
|
47
|
+
|
|
48
|
+
/// Default Value for Symbol is 0.
|
|
49
|
+
Symbol() : kind(0), attribute(0), variableAttribute(0) {}
|
|
50
|
+
|
|
51
|
+
/// Contruct from u32_t move the bit to right field
|
|
52
|
+
Symbol(const u32_t& num) : kind(num & 0xFF), attribute((num >> 8 ) & 0xFFFF), variableAttribute((num >> 24)) {}
|
|
53
|
+
|
|
54
|
+
/// Conversion of u32_t
|
|
55
|
+
operator u32_t()
|
|
56
|
+
{
|
|
57
|
+
static_assert(sizeof(struct Symbol)==sizeof(u32_t), "sizeof(struct Symbol)!=sizeof(u32_t)");
|
|
58
|
+
u32_t num = 0;
|
|
59
|
+
num += this->variableAttribute << 24;
|
|
60
|
+
num += this->attribute << 8;
|
|
61
|
+
num += this->kind;
|
|
62
|
+
return num;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
operator u32_t() const
|
|
66
|
+
{
|
|
67
|
+
static_assert(sizeof(struct Symbol)==sizeof(u32_t), "sizeof(struct Symbol)!=sizeof(u32_t)");
|
|
68
|
+
u32_t num = 0;
|
|
69
|
+
num += this->variableAttribute << 24;
|
|
70
|
+
num += this->attribute << 8;
|
|
71
|
+
num += this->kind;
|
|
72
|
+
return num;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
bool operator<(const Symbol& rhs)
|
|
76
|
+
{
|
|
77
|
+
return u32_t(*this) < u32_t(rhs);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
void operator=(const u32_t& i)
|
|
81
|
+
{
|
|
82
|
+
this->kind = EdgeKindMask & i;
|
|
83
|
+
this->attribute = i >> EdgeKindMaskBits;
|
|
84
|
+
this->variableAttribute = i >> AttributedKindMaskBits;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
void operator=(unsigned long long num)
|
|
88
|
+
{
|
|
89
|
+
this->kind = num & 0xFF;
|
|
90
|
+
this->attribute = (num >> 8 ) & 0xFFFF;
|
|
91
|
+
this->variableAttribute = num >> 24;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
bool operator==(const Symbol& s)
|
|
95
|
+
{
|
|
96
|
+
return ((this->kind == s.kind) && (this->attribute == s.attribute) && (this->variableAttribute == s.variableAttribute));
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
bool operator==(const Symbol& s) const
|
|
100
|
+
{
|
|
101
|
+
return ((kind == s.kind) && (attribute == s.attribute) && (variableAttribute == s.variableAttribute));
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
bool operator!=(const Symbol& s) const
|
|
105
|
+
{
|
|
106
|
+
return ! (*this == s) ;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
bool operator==(const u32_t& i)
|
|
110
|
+
{
|
|
111
|
+
return u32_t(*this) == u32_t(i);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
bool operator==(const Kind& k) const
|
|
115
|
+
{
|
|
116
|
+
return (this->kind == k);
|
|
117
|
+
}
|
|
118
|
+
} Symbol;
|
|
119
|
+
|
|
120
|
+
class SymbolHash
|
|
121
|
+
{
|
|
122
|
+
public:
|
|
123
|
+
size_t operator()(const Symbol &s) const
|
|
124
|
+
{
|
|
125
|
+
std::hash<u32_t> h;
|
|
126
|
+
return h(u32_t(s));
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
struct SymbolVectorHash
|
|
132
|
+
{
|
|
133
|
+
size_t operator()(const std::vector<Symbol> &v) const
|
|
134
|
+
{
|
|
135
|
+
size_t h = v.size();
|
|
136
|
+
|
|
137
|
+
SymbolHash hf;
|
|
138
|
+
for (const Symbol &t : v)
|
|
139
|
+
{
|
|
140
|
+
h ^= hf(t) + 0x9e3779b9 + (h << 6) + (h >> 2);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
return h;
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
template<typename Key, typename Value, typename Hash = SymbolHash,
|
|
148
|
+
typename KeyEqual = std::equal_to<Key>,
|
|
149
|
+
typename Allocator = std::allocator<std::pair<const Key, Value>>>
|
|
150
|
+
using SymbolMap = std::unordered_map<Key, Value, Hash, KeyEqual, Allocator>;
|
|
151
|
+
|
|
152
|
+
template <typename Key, typename Hash = SymbolVectorHash, typename KeyEqual = std::equal_to<Key>,
|
|
153
|
+
typename Allocator = std::allocator<Key>>
|
|
154
|
+
using SymbolSet = std::unordered_set<Key, Hash, KeyEqual, Allocator>;
|
|
155
|
+
|
|
156
|
+
typedef std::vector<Symbol> Production;
|
|
157
|
+
typedef SymbolSet<Production> Productions;
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
inline Map<std::string, Kind>& getNonterminals()
|
|
161
|
+
{
|
|
162
|
+
return this->nonterminals;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
inline void setNonterminals(Map<std::string, Kind>& nonterminals)
|
|
166
|
+
{
|
|
167
|
+
this->nonterminals = nonterminals;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
inline Map<std::string, Kind>& getTerminals()
|
|
171
|
+
{
|
|
172
|
+
return this->terminals;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
inline void setTerminals(Map<std::string, Kind>& terminals)
|
|
176
|
+
{
|
|
177
|
+
this->terminals = terminals;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
inline Map<std::string, Kind>& getEBNFSigns()
|
|
181
|
+
{
|
|
182
|
+
return this->EBNFSigns;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
inline void setEBNFSigns(Map<std::string, Kind>& EBNFSigns)
|
|
186
|
+
{
|
|
187
|
+
this->EBNFSigns = EBNFSigns;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
inline SymbolMap<Symbol, Productions>& getRawProductions()
|
|
191
|
+
{
|
|
192
|
+
return this->rawProductions;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
inline const Map<Kind, Set<Attribute>>& getKindToAttrsMap() const
|
|
196
|
+
{
|
|
197
|
+
return this->kindToAttrsMap;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
inline Kind getTotalKind()
|
|
201
|
+
{
|
|
202
|
+
return this->totalKind;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
inline Kind getStartKind()
|
|
206
|
+
{
|
|
207
|
+
return this->startKind;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
inline void setStartKind(Kind startKind)
|
|
211
|
+
{
|
|
212
|
+
this->startKind = startKind;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
inline void setTotalKind(Kind totalKind)
|
|
216
|
+
{
|
|
217
|
+
this->totalKind = totalKind;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
std::string extractKindStrFromSymbolStr(const std::string& symbolStr) const;
|
|
221
|
+
|
|
222
|
+
std::string extractAttributeStrFromSymbolStr(const std::string& symbolStr) const;
|
|
223
|
+
|
|
224
|
+
void setRawProductions(SymbolMap<Symbol, Productions>& rawProductions);
|
|
225
|
+
|
|
226
|
+
void setKindToAttrsMap(const Map<Kind, Set<Attribute>>& kindToAttrsMap);
|
|
227
|
+
|
|
228
|
+
void setAttributeKinds(const Set<Kind>& attributeKind);
|
|
229
|
+
|
|
230
|
+
Kind strToKind(std::string str) const;
|
|
231
|
+
|
|
232
|
+
Symbol strToSymbol(const std::string str) const;
|
|
233
|
+
|
|
234
|
+
std::string kindToStr(Kind kind) const;
|
|
235
|
+
|
|
236
|
+
std::string symToStrDump(Symbol sym) const;
|
|
237
|
+
|
|
238
|
+
Symbol getSymbol(const Production& prod, u32_t pos)
|
|
239
|
+
{
|
|
240
|
+
return prod.at(pos);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
inline const Set<Kind>& getAttrSyms() const
|
|
244
|
+
{
|
|
245
|
+
return this->attributeKinds;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/// Insert kind to nonterminal and return kind.
|
|
249
|
+
Kind insertNonterminalKind(std::string const kindStr);
|
|
250
|
+
|
|
251
|
+
/// From SymbolStr extract kind to inserted in nonterminal
|
|
252
|
+
/// symbolStr = <kindStr> [_] [ attributeStr | variableattributeStr ]
|
|
253
|
+
Kind insertTerminalKind(std::string strLit);
|
|
254
|
+
|
|
255
|
+
Symbol insertSymbol(std::string strLit);
|
|
256
|
+
|
|
257
|
+
Symbol insertNonTerminalSymbol(std::string strLit);
|
|
258
|
+
|
|
259
|
+
Symbol insertTerminalSymbol(std::string strLit);
|
|
260
|
+
|
|
261
|
+
Symbol insertEBNFSigns(std::string strLit);
|
|
262
|
+
|
|
263
|
+
void insertAttribute(Kind kind, Attribute a);
|
|
264
|
+
|
|
265
|
+
inline static Kind getAttributedKind(Attribute attribute, Kind kind)
|
|
266
|
+
{
|
|
267
|
+
return ((attribute << EdgeKindMaskBits)| kind );
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
inline static Kind getVariabledKind(VariableAttribute variableAttribute, Kind kind)
|
|
271
|
+
{
|
|
272
|
+
return ((variableAttribute << AttributedKindMaskBits) | kind);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
protected:
|
|
276
|
+
static constexpr unsigned char EdgeKindMaskBits = 8; ///< We use the lower 8 bits to denote edge kind
|
|
277
|
+
static constexpr unsigned char AttributedKindMaskBits = 24; ///< We use the lower 24 bits to denote attributed kind
|
|
278
|
+
static constexpr u64_t EdgeKindMask = (~0ULL) >> (64 - EdgeKindMaskBits);
|
|
279
|
+
Kind startKind;
|
|
280
|
+
private:
|
|
281
|
+
Map<std::string, Kind> nonterminals;
|
|
282
|
+
Map<std::string, Kind> terminals;
|
|
283
|
+
Map<std::string, Kind> EBNFSigns; /// Map contains Signs' String and associated Symbols
|
|
284
|
+
Set<Kind> attributeKinds;
|
|
285
|
+
Map<Kind, Set<Attribute>> kindToAttrsMap;
|
|
286
|
+
SymbolMap<Symbol, Productions> rawProductions;
|
|
287
|
+
u32_t totalKind;
|
|
288
|
+
};
|
|
289
|
+
|
|
290
|
+
class CFGrammar : public GrammarBase
|
|
291
|
+
{
|
|
292
|
+
|
|
293
|
+
public:
|
|
294
|
+
CFGrammar();
|
|
295
|
+
|
|
296
|
+
/// Methods for support type inquiry through isa, cast, and dyn_cast:
|
|
297
|
+
//@{
|
|
298
|
+
static inline bool classof(const CFGrammar *)
|
|
299
|
+
{
|
|
300
|
+
return true;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
static inline bool classof(const GrammarBase *node)
|
|
304
|
+
{
|
|
305
|
+
return true;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
Productions& getEpsilonProds()
|
|
309
|
+
{
|
|
310
|
+
return epsilonProds;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
SymbolMap<Symbol, Productions>& getSingleRHSToProds()
|
|
314
|
+
{
|
|
315
|
+
return singleRHSToProds;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
SymbolMap<Symbol, Productions>& getFirstRHSToProds()
|
|
319
|
+
{
|
|
320
|
+
return firstRHSToProds;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
SymbolMap<Symbol, Productions>& getSecondRHSToProds()
|
|
324
|
+
{
|
|
325
|
+
return secondRHSToProds;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
const bool hasProdsFromFirstRHS(const Symbol sym) const
|
|
329
|
+
{
|
|
330
|
+
auto it = firstRHSToProds.find(sym);
|
|
331
|
+
return it!=firstRHSToProds.end();
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
const bool hasProdsFromSingleRHS(const Symbol sym) const
|
|
335
|
+
{
|
|
336
|
+
auto it = singleRHSToProds.find(sym);
|
|
337
|
+
return it!=singleRHSToProds.end();
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
const bool hasProdsFromSecondRHS(const Symbol sym) const
|
|
341
|
+
{
|
|
342
|
+
auto it = secondRHSToProds.find(sym);
|
|
343
|
+
return it!=secondRHSToProds.end();
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
const Productions& getProdsFromSingleRHS(const Symbol sym) const
|
|
347
|
+
{
|
|
348
|
+
auto it = singleRHSToProds.find(sym);
|
|
349
|
+
assert(it!=singleRHSToProds.end() && "production (X -> sym) not found for sym!!");
|
|
350
|
+
return it->second;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
const Productions& getProdsFromFirstRHS(const Symbol sym) const
|
|
354
|
+
{
|
|
355
|
+
auto it = firstRHSToProds.find(sym);
|
|
356
|
+
assert(it!=firstRHSToProds.end() && "production (X -> sym Y ) not found for sym!!");
|
|
357
|
+
return it->second;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
const Productions& getProdsFromSecondRHS(const Symbol sym) const
|
|
361
|
+
{
|
|
362
|
+
auto it = secondRHSToProds.find(sym);
|
|
363
|
+
assert(it!=secondRHSToProds.end() && "production (X -> Y sym) not found for sym!!");
|
|
364
|
+
return it->second;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
|
|
368
|
+
const Symbol& getLHSSymbol(const Production& prod) const
|
|
369
|
+
{
|
|
370
|
+
return prod.at(0);
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
const Symbol& getFirstRHSSymbol(const Production& prod) const
|
|
374
|
+
{
|
|
375
|
+
return prod.at(1);
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
const Symbol& getSecondRHSSymbol(const Production& prod) const
|
|
379
|
+
{
|
|
380
|
+
return prod.at(2);
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
void dump() const;
|
|
384
|
+
|
|
385
|
+
void dump(std::string fileName) const;
|
|
386
|
+
|
|
387
|
+
|
|
388
|
+
const inline u32_t num_generator()
|
|
389
|
+
{
|
|
390
|
+
return newTerminalSubscript++;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
private:
|
|
394
|
+
SymbolSet<Production> epsilonProds;
|
|
395
|
+
SymbolMap<Symbol, Productions> singleRHSToProds;
|
|
396
|
+
SymbolMap<Symbol, Productions> firstRHSToProds;
|
|
397
|
+
SymbolMap<Symbol, Productions> secondRHSToProds;
|
|
398
|
+
u32_t newTerminalSubscript;
|
|
399
|
+
};
|
|
400
|
+
|
|
401
|
+
/**
|
|
402
|
+
* Worklist with "first in first out" order.
|
|
403
|
+
* New nodes will be pushed at back and popped from front.
|
|
404
|
+
* Elements in the list are unique as they're recorded by Set.
|
|
405
|
+
*/
|
|
406
|
+
template<class Data>
|
|
407
|
+
class CFLFIFOWorkList
|
|
408
|
+
{
|
|
409
|
+
typedef GrammarBase::SymbolSet<Data> DataSet;
|
|
410
|
+
typedef std::deque<Data> DataDeque;
|
|
411
|
+
public:
|
|
412
|
+
CFLFIFOWorkList() {}
|
|
413
|
+
|
|
414
|
+
~CFLFIFOWorkList() {}
|
|
415
|
+
|
|
416
|
+
inline bool empty() const
|
|
417
|
+
{
|
|
418
|
+
return data_list.empty();
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
inline bool find(Data data) const
|
|
422
|
+
{
|
|
423
|
+
return (data_set.find(data) == data_set.end() ? false : true);
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
/**
|
|
427
|
+
* Push a data into the work list.
|
|
428
|
+
*/
|
|
429
|
+
inline bool push(Data data)
|
|
430
|
+
{
|
|
431
|
+
if (data_set.find(data) == data_set.end())
|
|
432
|
+
{
|
|
433
|
+
data_list.push_back(data);
|
|
434
|
+
data_set.insert(data);
|
|
435
|
+
return true;
|
|
436
|
+
}
|
|
437
|
+
else
|
|
438
|
+
return false;
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
/**
|
|
442
|
+
* Pop a data from the END of work list.
|
|
443
|
+
*/
|
|
444
|
+
inline Data pop()
|
|
445
|
+
{
|
|
446
|
+
assert(!empty() && "work list is empty");
|
|
447
|
+
Data data = data_list.front();
|
|
448
|
+
data_list.pop_front();
|
|
449
|
+
data_set.erase(data);
|
|
450
|
+
return data;
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
/*!
|
|
454
|
+
* Clear all the data
|
|
455
|
+
*/
|
|
456
|
+
inline void clear()
|
|
457
|
+
{
|
|
458
|
+
data_list.clear();
|
|
459
|
+
data_set.clear();
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
private:
|
|
463
|
+
DataSet data_set; ///< store all data in the work list.
|
|
464
|
+
DataDeque data_list; ///< work list using std::vector.
|
|
465
|
+
};
|
|
466
|
+
|
|
467
|
+
}
|
|
468
|
+
#endif /* CFLGrammar_H_ */
|
|
@@ -112,8 +112,8 @@ public:
|
|
|
112
112
|
{
|
|
113
113
|
return false;
|
|
114
114
|
}
|
|
115
|
-
|
|
116
|
-
|
|
115
|
+
CFGrammar::Kind copyKind = grammar->strToKind("copy");
|
|
116
|
+
CFGrammar::Kind copybarKind = grammar->strToKind("copybar");
|
|
117
117
|
solver->pushIntoWorklist(graph->addCFLEdge(graph->getGNode(src),graph->getGNode(dst), copyKind));
|
|
118
118
|
solver->pushIntoWorklist(graph->addCFLEdge(graph->getGNode(dst),graph->getGNode(src), copybarKind));
|
|
119
119
|
return true;
|
|
@@ -92,7 +92,7 @@ public:
|
|
|
92
92
|
//@{
|
|
93
93
|
// Grammar
|
|
94
94
|
static double timeOfBuildCFLGrammar; // Time of building grammarBase from text file
|
|
95
|
-
static double timeOfNormalizeGrammar; // Time of normalizing grammarBase to
|
|
95
|
+
static double timeOfNormalizeGrammar; // Time of normalizing grammarBase to CFGrammar
|
|
96
96
|
// Graph
|
|
97
97
|
static double timeOfBuildCFLGraph; // Time of building CFLGraph
|
|
98
98
|
static double numOfTerminalEdges; // Number of terminal labeled edges
|
|
@@ -109,7 +109,7 @@ protected:
|
|
|
109
109
|
SVFIR* svfir;
|
|
110
110
|
CFLGraph* graph;
|
|
111
111
|
GrammarBase* grammarBase;
|
|
112
|
-
|
|
112
|
+
CFGrammar* grammar;
|
|
113
113
|
CFLSolver* solver;
|
|
114
114
|
};
|
|
115
115
|
|
|
@@ -30,37 +30,43 @@
|
|
|
30
30
|
#ifndef INCLUDE_CFL_CFLGRAPHBUILDER_H_
|
|
31
31
|
#define INCLUDE_CFL_CFLGRAPHBUILDER_H_
|
|
32
32
|
|
|
33
|
-
#include "CFL/
|
|
33
|
+
#include "CFL/CFGrammar.h"
|
|
34
34
|
#include "Graphs/CFLGraph.h"
|
|
35
35
|
#include "Graphs/SVFG.h"
|
|
36
36
|
|
|
37
37
|
namespace SVF
|
|
38
38
|
{
|
|
39
39
|
|
|
40
|
+
enum class BuildDirection
|
|
41
|
+
{
|
|
42
|
+
plain,
|
|
43
|
+
bidirection,
|
|
44
|
+
};
|
|
45
|
+
|
|
40
46
|
/**
|
|
41
47
|
* CFLGraphBuilder class is responsible for building CFL (Context-Free Language) graphs
|
|
42
|
-
* from text files
|
|
48
|
+
* from text files, dot files, json file or from memory graph.
|
|
43
49
|
*/
|
|
44
50
|
class CFLGraphBuilder
|
|
45
51
|
{
|
|
46
52
|
protected:
|
|
47
|
-
/// Define Kind(Not contain attribute) and Symbol(May contain attribute) as types derived from
|
|
53
|
+
/// Define Kind(Not contain attribute) and Symbol(May contain attribute) as types derived from CFGrammar
|
|
48
54
|
/// to numerically represent label
|
|
49
|
-
typedef
|
|
50
|
-
typedef
|
|
55
|
+
typedef CFGrammar::Kind Kind;
|
|
56
|
+
typedef CFGrammar::Symbol Symbol;
|
|
51
57
|
|
|
52
58
|
/// Maps to maintain mapping between labels and kinds
|
|
53
59
|
Map<std::string, Kind> labelToKindMap;
|
|
54
60
|
Map<Kind, std::string> kindToLabelMap;
|
|
55
61
|
|
|
56
62
|
/// Map to maintain attributes associated with each kind
|
|
57
|
-
Map<
|
|
63
|
+
Map<CFGrammar::Kind, Set<CFGrammar::Attribute>> kindToAttrsMap;
|
|
58
64
|
|
|
59
65
|
Kind current;
|
|
60
66
|
CFLGraph *cflGraph;
|
|
61
67
|
|
|
62
68
|
/// Method to add an attribute to a specific kind
|
|
63
|
-
void addAttribute(
|
|
69
|
+
void addAttribute(CFGrammar::Kind kind, CFGrammar::Attribute attribute);
|
|
64
70
|
|
|
65
71
|
/// build label and kind connect from the grammar
|
|
66
72
|
void buildlabelToKindMap(GrammarBase *grammar);
|
|
@@ -68,80 +74,23 @@ protected:
|
|
|
68
74
|
/// add src and dst node from file
|
|
69
75
|
CFLNode* addGNode(u32_t NodeID);
|
|
70
76
|
|
|
77
|
+
/// Method to build a CFL graph from a Text file
|
|
78
|
+
CFLGraph* buildFromText(std::string fileName, GrammarBase *grammar, BuildDirection direction = BuildDirection::plain);
|
|
79
|
+
|
|
80
|
+
/// Method to build a CFL graph from a Dot file
|
|
81
|
+
CFLGraph *buildFromDot(std::string filename, GrammarBase *grammar, BuildDirection direction = BuildDirection::plain);
|
|
82
|
+
|
|
83
|
+
/// Method to build a CFL graph from a Json file
|
|
84
|
+
CFLGraph *buildFromJson(std::string filename, GrammarBase *grammar, BuildDirection direction = BuildDirection::plain);
|
|
85
|
+
|
|
71
86
|
public:
|
|
72
87
|
/// Method to build a CFL graph by copying nodes and edges from any graph
|
|
73
88
|
/// inherited from GenericGraph
|
|
74
89
|
template<class N, class E>
|
|
75
|
-
|
|
76
|
-
{
|
|
77
|
-
for(auto it = graph->begin(); it!= graph->end(); it++)
|
|
78
|
-
{
|
|
79
|
-
CFLNode* node = new CFLNode((*it).first);
|
|
80
|
-
cflGraph->addCFLNode((*it).first, node);
|
|
81
|
-
}
|
|
82
|
-
for(auto it = graph->begin(); it!= graph->end(); it++)
|
|
83
|
-
{
|
|
84
|
-
N* node = (*it).second;
|
|
85
|
-
for(E* edge : node->getOutEdges())
|
|
86
|
-
{
|
|
87
|
-
cflGraph->addCFLEdge(cflGraph->getGNode(edge->getSrcID()), cflGraph->getGNode(edge->getDstID()), edge->getEdgeKind());
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
/// Method to build a bidirectional CFL graph by copying nodes and edges
|
|
93
|
-
/// from any graph inherited from GenericGraph
|
|
94
|
-
template<class N, class E>
|
|
95
|
-
CFLGraph* buildBigraph(GenericGraph<N,E>* graph, Kind startKind, GrammarBase *grammar)
|
|
96
|
-
{
|
|
97
|
-
CFLGraph *cflGraph = new CFLGraph(startKind);
|
|
98
|
-
for(auto pairV : grammar->getTerminals())
|
|
99
|
-
{
|
|
100
|
-
if(labelToKindMap.find(pairV.first) == labelToKindMap.end())
|
|
101
|
-
{
|
|
102
|
-
labelToKindMap.insert(pairV);
|
|
103
|
-
}
|
|
104
|
-
if(kindToLabelMap.find(pairV.second) == kindToLabelMap.end())
|
|
105
|
-
{
|
|
106
|
-
kindToLabelMap.insert(make_pair(pairV.second, pairV.first));
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
for(auto pairV : grammar->getNonterminals())
|
|
110
|
-
{
|
|
111
|
-
if(labelToKindMap.find(pairV.first) == labelToKindMap.end())
|
|
112
|
-
{
|
|
113
|
-
labelToKindMap.insert(pairV);
|
|
114
|
-
}
|
|
115
|
-
if(kindToLabelMap.find(pairV.second) == kindToLabelMap.end())
|
|
116
|
-
{
|
|
117
|
-
kindToLabelMap.insert(make_pair(pairV.second, pairV.first));
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
for(auto it = graph->begin(); it!= graph->end(); it++)
|
|
121
|
-
{
|
|
122
|
-
CFLNode* node = new CFLNode((*it).first);
|
|
123
|
-
cflGraph->addCFLNode((*it).first, node);
|
|
124
|
-
}
|
|
125
|
-
for(auto it = graph->begin(); it!= graph->end(); it++)
|
|
126
|
-
{
|
|
127
|
-
N* node = (*it).second;
|
|
128
|
-
for(E* edge : node->getOutEdges())
|
|
129
|
-
{
|
|
130
|
-
CFLGrammar::Kind edgeLabel = edge->getEdgeKind();
|
|
131
|
-
cflGraph->addCFLEdge(cflGraph->getGNode(edge->getSrcID()), cflGraph->getGNode(edge->getDstID()), edgeLabel);
|
|
132
|
-
std::string key = kindToLabelMap[edge->getEdgeKind()];
|
|
133
|
-
key.append("bar");
|
|
134
|
-
cflGraph->addCFLEdge(cflGraph->getGNode(edge->getDstID()), cflGraph->getGNode(edge->getSrcID()), labelToKindMap[key]);
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
return cflGraph;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
/// Method to build a CFL graph from a Dot file
|
|
141
|
-
CFLGraph *buildFromDot(std::string filename, GrammarBase *grammar);
|
|
90
|
+
CFLGraph* build(GenericGraph<N,E>* graph, GrammarBase *grammar, BuildDirection direction = BuildDirection::plain);
|
|
142
91
|
|
|
143
|
-
/// Method to build a CFL graph from
|
|
144
|
-
CFLGraph*
|
|
92
|
+
/// Method to build a CFL graph from external file
|
|
93
|
+
CFLGraph* build(std::string fileName, GrammarBase *grammar, BuildDirection direction = BuildDirection::plain);
|
|
145
94
|
|
|
146
95
|
/// @{
|
|
147
96
|
/// Getter methods for accessing class variables
|
|
@@ -159,7 +108,7 @@ public:
|
|
|
159
108
|
}
|
|
160
109
|
|
|
161
110
|
/// Returns a reference to the map that associates Kinds with their corresponding attributes
|
|
162
|
-
Map<
|
|
111
|
+
Map<CFGrammar::Kind, Set<CFGrammar::Attribute>>& getKindToAttrsMap()
|
|
163
112
|
{
|
|
164
113
|
return this->kindToAttrsMap;
|
|
165
114
|
}
|
|
@@ -183,10 +132,10 @@ private:
|
|
|
183
132
|
void connectVGep(CFLGraph *cflGraph, ConstraintGraph *graph, ConstraintNode *src, ConstraintNode *dst, u32_t level, SVFIR* pag);
|
|
184
133
|
|
|
185
134
|
/// Handles edges, with the exception of the GEP
|
|
186
|
-
void addBiCFLEdge(CFLGraph *cflGraph, ConstraintNode* src, ConstraintNode* dst,
|
|
135
|
+
void addBiCFLEdge(CFLGraph *cflGraph, ConstraintNode* src, ConstraintNode* dst, CFGrammar::Kind label);
|
|
187
136
|
|
|
188
137
|
/// Adds bidirectional GEP edges with attributes
|
|
189
|
-
void addBiGepCFLEdge(CFLGraph *cflGraph, ConstraintNode* src, ConstraintNode* dst,
|
|
138
|
+
void addBiGepCFLEdge(CFLGraph *cflGraph, ConstraintNode* src, ConstraintNode* dst, CFGrammar::Attribute attri);
|
|
190
139
|
};
|
|
191
140
|
|
|
192
141
|
/// VFCFLGraphBuilder: a CFLGraphBuilder specialized for handling value-flow
|
|
@@ -205,10 +154,10 @@ private:
|
|
|
205
154
|
void connectVGep(CFLGraph *cflGraph, ConstraintGraph *graph, ConstraintNode *src, ConstraintNode *dst, u32_t level, SVFIR* pag);
|
|
206
155
|
|
|
207
156
|
/// Handles edges, with the exception of the GEP
|
|
208
|
-
void addBiCFLEdge(CFLGraph *cflGraph, ConstraintNode* src, ConstraintNode* dst,
|
|
157
|
+
void addBiCFLEdge(CFLGraph *cflGraph, ConstraintNode* src, ConstraintNode* dst, CFGrammar::Kind label);
|
|
209
158
|
|
|
210
159
|
/// Adds bidirectional GEP edges with attributes
|
|
211
|
-
void addBiGepCFLEdge(CFLGraph *cflGraph, ConstraintNode* src, ConstraintNode* dst,
|
|
160
|
+
void addBiGepCFLEdge(CFLGraph *cflGraph, ConstraintNode* src, ConstraintNode* dst, CFGrammar::Attribute attri);
|
|
212
161
|
};
|
|
213
162
|
|
|
214
163
|
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
#define INCLUDE_CFL_CFLSolver_H_
|
|
32
32
|
|
|
33
33
|
#include "Graphs/CFLGraph.h"
|
|
34
|
-
#include "CFL/
|
|
34
|
+
#include "CFL/CFGrammar.h"
|
|
35
35
|
#include "Util/WorkList.h"
|
|
36
36
|
|
|
37
37
|
using namespace std;
|
|
@@ -46,12 +46,12 @@ class CFLSolver
|
|
|
46
46
|
public:
|
|
47
47
|
/// Define worklist
|
|
48
48
|
typedef FIFOWorkList<const CFLEdge*> WorkList;
|
|
49
|
-
typedef
|
|
50
|
-
typedef
|
|
49
|
+
typedef CFGrammar::Production Production;
|
|
50
|
+
typedef CFGrammar::Symbol Symbol;
|
|
51
51
|
|
|
52
52
|
static double numOfChecks;
|
|
53
53
|
|
|
54
|
-
CFLSolver(CFLGraph* _graph,
|
|
54
|
+
CFLSolver(CFLGraph* _graph, CFGrammar* _grammar): graph(_graph), grammar(_grammar)
|
|
55
55
|
{
|
|
56
56
|
}
|
|
57
57
|
|
|
@@ -77,7 +77,7 @@ public:
|
|
|
77
77
|
}
|
|
78
78
|
|
|
79
79
|
/// Return CFL Grammar
|
|
80
|
-
inline const
|
|
80
|
+
inline const CFGrammar* getGrammar() const
|
|
81
81
|
{
|
|
82
82
|
return grammar;
|
|
83
83
|
}
|
|
@@ -106,7 +106,7 @@ protected:
|
|
|
106
106
|
|
|
107
107
|
protected:
|
|
108
108
|
CFLGraph* graph;
|
|
109
|
-
|
|
109
|
+
CFGrammar* grammar;
|
|
110
110
|
/// Worklist for resolution
|
|
111
111
|
WorkList worklist;
|
|
112
112
|
|
|
@@ -266,7 +266,7 @@ public:
|
|
|
266
266
|
}
|
|
267
267
|
//@}
|
|
268
268
|
|
|
269
|
-
POCRSolver(CFLGraph* _graph,
|
|
269
|
+
POCRSolver(CFLGraph* _graph, CFGrammar* _grammar) : CFLSolver(_graph, _grammar)
|
|
270
270
|
{
|
|
271
271
|
buildCFLData();
|
|
272
272
|
}
|
|
@@ -344,7 +344,7 @@ public:
|
|
|
344
344
|
void meld_h(NodeID x, TreeNode* uNode, TreeNode* vNode);
|
|
345
345
|
//@}
|
|
346
346
|
public:
|
|
347
|
-
POCRHybridSolver(CFLGraph* _graph,
|
|
347
|
+
POCRHybridSolver(CFLGraph* _graph, CFGrammar* _grammar) : POCRSolver(_graph, _grammar)
|
|
348
348
|
{
|
|
349
349
|
}
|
|
350
350
|
/// Destructor
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
Start:
|
|
2
|
+
V
|
|
3
|
+
Terminal:
|
|
4
|
+
addr copy store load gep vgep
|
|
5
|
+
Productions:
|
|
6
|
+
F -> epsilon | F copy | addr Memflow | F store V load | store Memflow load | F F;
|
|
7
|
+
Fbar -> epsilon | copybar Fbar | Memflowbar addrbar | loadbar V storebar Fbar | loadbar Memflowbar storebar;
|
|
8
|
+
V -> Fbar V F | addrbar addr | gepbar_i V gep_i | gepbarpath V gep_0 | gepbar_i F gep_i | gepbar_i Fbar gep_i;
|
|
9
|
+
copy -> vgep;
|
|
10
|
+
copybar -> vgepbar;
|
|
11
|
+
gep_1 -> gep_0 F vgep | gep_0 F gep_1;
|
|
12
|
+
gepbar_1 -> gepbar_1 Fbar gepbar_0 | vgepbar Fbar gepbar_0;
|
|
13
|
+
gepbarpath -> gepbar_0 gepbar_0 | gepbarpath gepbar_0;
|
|
14
|
+
Memflow -> load store | Fbar Memflow | F Memflow Fbar | gep_i Memflow gepbar_i | gepbar_i Memflow gep_i;
|
|
15
|
+
Memflowbar -> storebar loadbar | Memflowbar F | F Memflowbar Fbar | gep_i Memflowbar gepbar_i | gepbar_i Memflowbar gep_i;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
Start:
|
|
2
|
+
V
|
|
3
|
+
Terminal:
|
|
4
|
+
addr copy store load gep
|
|
5
|
+
Productions:
|
|
6
|
+
F -> ( copy M ? ) *;
|
|
7
|
+
Fbar -> ( M ? copybar ) *;
|
|
8
|
+
M -> addr V addrbar;
|
|
9
|
+
V -> Fbar V F | gepbar_i V gep_i | M | epsilon | gepbar_i Memcpy gep_i | gep_i Memcpy gepbar_i | ArrayPath V gep_0;
|
|
10
|
+
ArrayPath -> gepbar_0 gepbar_0 | ArrayPath gepbar_0;
|
|
11
|
+
Memcpy -> addrbar V addr | gep_i Memcpy gepbar_i | F Memcpy Fbar;
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
#include <iostream>
|
|
35
35
|
#include <string>
|
|
36
36
|
#include <regex>
|
|
37
|
-
#include "CFL/
|
|
37
|
+
#include "CFL/CFGrammar.h"
|
|
38
38
|
#include "Graphs/GenericGraph.h"
|
|
39
39
|
#include "Graphs/ConsG.h"
|
|
40
40
|
|
|
@@ -155,8 +155,8 @@ typedef GenericGraph<CFLNode,CFLEdge> GenericCFLGraphTy;
|
|
|
155
155
|
class CFLGraph: public GenericCFLGraphTy
|
|
156
156
|
{
|
|
157
157
|
public:
|
|
158
|
-
typedef
|
|
159
|
-
typedef
|
|
158
|
+
typedef CFGrammar::Symbol Symbol;
|
|
159
|
+
typedef CFGrammar::Kind Kind;
|
|
160
160
|
typedef GenericNode<CFLNode,CFLEdge>::GEdgeSetTy CFLEdgeSet;
|
|
161
161
|
Kind startKind;
|
|
162
162
|
|