-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeature.h
336 lines (274 loc) · 8.38 KB
/
feature.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
* Copyright (c) 1996-2010 Barton P. Miller
*
* We provide the Paradyn Parallel Performance Tools (below
* described as "Paradyn") on an AS IS basis, and do not warrant its
* validity or performance. We reserve the right to update, modify,
* or discontinue this software at any time. We shall have no
* obligation to supply such updates or modifications or any other
* form of support to you.
*
* By your use of Paradyn, you understand and agree that we (or any
* other person or entity with proprietary rights in Paradyn) are
* under no obligation to provide either maintenance services,
* update services, notices of latent defects, or correction of
* defects for Paradyn.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef _FEATURE_H_
#define _FEATURE_H_
#include <string>
#include <unordered_map>
#include <set>
#include "Operation_impl.h"
#include "Register.h"
#include "CFG.h"
#include "CodeSource.h"
#define ILLEGAL_ENTRY e_No_Entry
#define NOARG 0xffff
#define IMMARG (NOARG-1)
#define MEMARG (NOARG-2)
#define MULTIREG (NOARG-3)
#define CALLEESAVEREG (NOARG-4)
#define ARGUREG (NOARG-5)
#define OTHERREG (NOARG-6)
using namespace std;
using namespace Dyninst;
using namespace Dyninst::ParseAPI;
enum feature_types {
INVALID,
IDIOM,
OPERAND
};
class Feature {
public:
Feature() : _type(INVALID) { }
feature_types type() const { return _type; }
virtual ~Feature() { }
virtual string format() = 0;
protected:
feature_types _type;
};
/* LookupFeature:
A feature that the Lookup knows how to look up.
Composed of LookupTerms.
*/
class LookupTerm {
public:
LookupTerm() { }
LookupTerm(const LookupTerm *) { fprintf(stderr,"ZOMG BAD\n"); }
virtual ~LookupTerm() { }
virtual string format() = 0;
virtual size_t hash() const = 0;
};
class LookupFeature : public Feature {
public:
LookupFeature() { }
LookupFeature(vector<LookupTerm *> & t);
const vector<LookupTerm *> & terms() const { return _terms;}
void add_term(LookupTerm *t);
virtual ~LookupFeature() { }
virtual string format() = 0;
protected:
// this object is not responsible for the contents of this vector
vector<LookupTerm*> _terms;
};
/* IdiomFeature:
Represents an instruction idiom sequence.
*/
#define ENTRY_SHIFT 32ULL
#define ARG1_SHIFT 16ULL
#define ARG2_SHIFT 0ULL
#define ENTRY_SIZE 16ULL
#define ARG_SIZE 16ULL
#define ENTRY_MASK (((uint64_t)(1<<ENTRY_SIZE)-1) << ENTRY_SHIFT)
#define ARG1_MASK (((uint64_t)(1<<ARG_SIZE)-1) << ARG1_SHIFT)
#define ARG2_MASK (((uint64_t)(1<<ARG_SIZE)-1) << ARG2_SHIFT)
class IdiomTerm : public LookupTerm {
public:
IdiomTerm() {}
IdiomTerm(Function *f, Address addr);
IdiomTerm(SymtabCodeSource *sts, Address addr);
IdiomTerm(uint64_t it);
IdiomTerm(const IdiomTerm & it) :
entry_id(it.entry_id),
arg1(it.arg1),
arg2(it.arg2),
len(it.len)
{ }
~IdiomTerm() { }
string format();
bool operator==(const IdiomTerm &it) const;
bool operator<(const IdiomTerm &it) const;
size_t hash() const;
uint64_t to_int() const;
void from_int(uint64_t it);
string human_format();
public:
unsigned short entry_id;
unsigned short arg1;
unsigned short arg2;
unsigned char len;
};
class IdiomFeature : public LookupFeature {
public:
IdiomFeature() { }
IdiomFeature(vector<LookupTerm *> & t) : LookupFeature(t)
{ }
IdiomFeature(char * str);
~IdiomFeature() { }
string format();
string human_format();
typedef IdiomTerm term;
bool operator< (const IdiomFeature& f) const;
};
/*
OperandFeature
Represents a distant bigram pair of operands
*/
class OperandTerm : public LookupTerm {
public:
OperandTerm(unsigned short id, bool write) :
_id(id), _write(write), _wc_dist(0), _formatted(false)
{ }
OperandTerm(const OperandTerm & ot) :
_id(ot._id),
_write(ot._write),
_wc_dist(ot._wc_dist),
_formatted(ot._formatted),
_format(ot._format)
{ }
OperandTerm(unsigned long ot);
~OperandTerm() { }
string format();
bool operator==(const OperandTerm & ot) const;
bool operator<(const OperandTerm & ot) const;
size_t hash() const;
uint64_t to_int() const;
void from_int(uint64_t it);
public:
unsigned short _id;
bool _read;
bool _write;
unsigned char _wc_dist;
bool _formatted;
string _format;
};
class OperandFeature : public LookupFeature {
public:
OperandFeature() : _formatted(false) { }
~OperandFeature() { }
string format();
typedef OperandTerm term;
private:
bool _formatted;
string _format;
};
template<typename T>
class Lookup {
public:
typedef typename T::term LT;
Lookup(bool f = false) :
fixed(false)
{ }
~Lookup();
void lookup(int size, Function * f, Address addr, Address end, vector<Feature *> & feats);
void lookup_cross_blk(int size, Function *f, Block *b, Address addr, vector<Feature*> &feats);
struct lt_hash {
size_t operator()(const LT & x) const {
return x.hash();
}
};
struct lt_equal {
bool operator()(const LT & a, const LT &b) const {
return a == b;
}
};
class Lnode;
typedef unordered_map<LT, Lnode *, lt_hash, lt_equal> lmap_t;
class Lnode {
public:
LookupFeature *f;
lmap_t _next;
Lnode();
~Lnode();
Lnode * next(LT *nt);
};
private:
void lookup_idiom(int size, Function *f, Address addr, Address end, Lnode * cur, int depth,
vector<LookupTerm *> & stack, vector<Feature *> & feats);
void lookup_idiom_cross_blk(int size, Function *f, Block *b, Address addr, Lnode * cur, int depth,
vector<LookupTerm *> & stack, vector<Feature *> & feats);
private:
unordered_map<Address, vector<LT *> > _term_map;
Lnode start;
bool fixed;
};
/* Evaluating a FeatureVector against a Function
produces... a vector of Features. Each of these
can be printed out in the appropriate format.
There are two modes:
1. Feature subset provided. In this case, only
those features indicated will be produced.
The ordering of features under iteration is fixed.
2. All features enabled. There is no guarantee
of feature ordering in this case.
*/
class FeatureVector {
public:
FeatureVector();
FeatureVector(char * featfile);
~FeatureVector();
int eval(int size, Function * f, bool idioms = true, bool operands = false);
int eval_cross_blk(int size, Function * f, bool idioms = true, bool operands = false);
/* iterator */
class iterator {
private:
FeatureVector * _m_fv;
int _m_ind;
private:
iterator(FeatureVector *fv, int ind) :
_m_fv(fv), _m_ind(ind) { }
public:
iterator() : _m_fv(NULL), _m_ind(0) { }
iterator(const iterator &orig) :
_m_fv(orig._m_fv), _m_ind(orig._m_ind) { }
~iterator() { }
iterator& operator++();
iterator operator++(int);
iterator& operator--();
iterator operator--(int);
bool operator==(const iterator &) const;
bool operator!=(const iterator &) const;
Feature* operator*();
friend class FeatureVector;
};
const iterator & begin() const { return *_begin; };
const iterator & end() const { return *_end; }
private:
bool hasmore(int index);
Feature * get(int index);
private:
iterator * _begin;
iterator * _end;
bool _limited;
vector<Feature *> _feats;
// Generators
Lookup<IdiomFeature> iflookup;
Lookup<OperandFeature> oflookup;
friend class FeatureVector::iterator;
};
#endif