-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlookup.cpp
364 lines (301 loc) · 10 KB
/
lookup.cpp
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*
* 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
*/
#include <assert.h>
#include <stdio.h>
#include "CodeObject.h"
#include "InstructionDecoder.h"
#include "Instruction.h"
#include "feature.h"
#include "iapihax.h"
#define MAX_IDIOM_LEN 3
IdiomTerm WILDCARD_IDIOM(0xaaaaffffffff);
/* op1 x x op2 */
#define MAX_OPERAND_DIST 3
OperandTerm WILDCARD_OPERAND_D1(0x1ffff);
OperandTerm WILDCARD_OPERAND_D2(0x2ffff);
OperandTerm * OP_WC[] = { &WILDCARD_OPERAND_D1, &WILDCARD_OPERAND_D2 };
using namespace std;
using namespace __gnu_cxx;
using namespace Dyninst;
using namespace Dyninst::ParseAPI;
using namespace Dyninst::InstructionAPI;
/*
template<typename T>
void Lookup<T>::lookup(Address addr, vector<Feature *> & feats)
{
fprintf(stderr,"generic lookup invoked\n");
}
*/
template<>
void Lookup<IdiomFeature>::lookup_idiom(
int size,
Function * f,
Address addr,
Address end,
Lookup<IdiomFeature>::Lnode * cur,
int depth,
vector<LookupTerm *> & stack,
vector<Feature *> & feats)
{
Lookup<IdiomFeature>::Lnode * next;
IdiomTerm * ct;
if(depth >= size || !f->isrc()->getPtrToInstruction(addr))
return;
// Make sure idioms don't pass the end of a basic block
if (addr >= end) return;
if(_term_map.find(addr) == _term_map.end())
_term_map[addr].push_back(new IdiomTerm(f,addr));
ct = *_term_map[addr].begin();
stack.push_back(ct);
// non-wildcard
next = cur->next(ct);
if(!next->f && !fixed)
next->f = new IdiomFeature( stack );
if(next->f && depth+1 == size) {
feats.push_back(next->f);
}
if(ct->entry_id != ILLEGAL_ENTRY)
lookup_idiom(size, f,addr+ct->len,end, next,depth+1,stack,feats);
stack.pop_back();
// wildcard
stack.push_back(&WILDCARD_IDIOM);
next = cur->next(&WILDCARD_IDIOM);
if(!next->f && !fixed)
next->f = new IdiomFeature( stack );
if(ct->entry_id != ILLEGAL_ENTRY)
lookup_idiom(size, f,addr+ct->len,end, next,depth+1,stack,feats);
stack.pop_back();
}
template<>
void Lookup<IdiomFeature>::lookup(
int size, Function * f, Address addr, Address end, vector<Feature *> & feats)
{
vector<LookupTerm *> stack;
lookup_idiom(size, f,addr,end, &start,0,stack,feats);
}
template<>
void Lookup<IdiomFeature>::lookup_idiom_cross_blk(
int size,
Function * f,
Block * b,
Address addr,
Lookup<IdiomFeature>::Lnode * cur,
int depth,
vector<LookupTerm *> & stack,
vector<Feature *> & feats)
{
Lookup<IdiomFeature>::Lnode * next;
IdiomTerm * ct;
if(depth >= size || !f->isrc()->getPtrToInstruction(addr))
return;
if(_term_map.find(addr) == _term_map.end())
_term_map[addr].push_back(new IdiomTerm(f,addr));
ct = *_term_map[addr].begin();
stack.push_back(ct);
// non-wildcard
next = cur->next(ct);
if(!next->f && !fixed)
next->f = new IdiomFeature( stack );
if(next->f && depth+1 == size) {
feats.push_back(next->f);
}
if(ct->entry_id != ILLEGAL_ENTRY) {
if (addr == b->last()) {
// We reach the end of the block, jump to sucessor blocks
for (auto eit = b->targets().begin(); eit != b->targets().end(); ++eit) {
Edge *e = *eit;
if (e->sinkEdge() || e->interproc()) continue;
lookup_idiom_cross_blk(size, f, e->trg(), e->trg()->start(), next, depth+1, stack, feats);
}
} else {
// Continue in the same block
lookup_idiom_cross_blk(size, f, b, addr+ct->len,next,depth+1,stack,feats);
}
}
stack.pop_back();
// wildcard
stack.push_back(&WILDCARD_IDIOM);
next = cur->next(&WILDCARD_IDIOM);
if(!next->f && !fixed)
next->f = new IdiomFeature( stack );
if(ct->entry_id != ILLEGAL_ENTRY) {
if (addr == b->last()) {
// We reach the end of the block, jump to sucessor blocks
for (auto eit = b->targets().begin(); eit != b->targets().end(); ++eit) {
Edge *e = *eit;
if (e->sinkEdge() || e->interproc()) continue;
lookup_idiom_cross_blk(size, f, e->trg(), e->trg()->start(), next, depth+1, stack, feats);
}
} else {
// Continue in the same block
lookup_idiom_cross_blk(size, f, b, addr+ct->len,next,depth+1,stack,feats);
}
}
stack.pop_back();
}
template<>
void Lookup<IdiomFeature>::lookup_cross_blk(
int size, Function * f, Block *b, Address addr, vector<Feature *> & feats)
{
vector<LookupTerm *> stack;
lookup_idiom_cross_blk(size, f, b, addr,&start,0,stack,feats);
}
void
get_operands(
Function *f, Address addr,
vector<OperandTerm *> & operands)
{
Instruction insn;
if(!f->isrc()->getPtrToInstruction(addr))
return;
InstructionDecoder dec(
(unsigned char*)f->isrc()->getPtrToInstruction(addr),
30, //arbitrary
f->isrc()->getArch());
insn = dec.decode();
if(insn.size() > 0 && insn.isValid()) {
vector<Operand> ops;
ExpTyper typer;
insn.getOperands(ops);
for(unsigned int i=0;i<ops.size();++i) {
Operand & op = ops[i];
unsigned short opid;
bool write = false;
op.getValue()->apply(&typer);
if(typer.reg()) {
set<RegisterAST::Ptr> w_regs, r_regs;
op.getReadSet(r_regs);
op.getWriteSet(w_regs);
write = !w_regs.empty();
if(!r_regs.empty())
opid = (*r_regs.begin())->getID();
else if(!w_regs.empty())
opid = (*w_regs.begin())->getID();
else {
assert(0); // XXX Debugging
continue;
}
} else if(!typer.imm()) {
write = op.writesMemory();
opid = MEMARG;
} else {
continue;
}
OperandTerm * ot = new OperandTerm(opid,write);
//fprintf(stderr," %s [%lx] %d %d\n",op.format().c_str(),ot->to_int(), op.readsMemory(), op.writesMemory());
operands.push_back(ot);
}
}
}
template<>
void Lookup<OperandFeature>::lookup(
int size, Function *f, Address addr, Address end, vector<Feature *> & feats)
{
Instruction insn;
/* for each operand here, we want to record a new feature for bigrams
with operands up to MAX_OPERAND_DIST away */
if (addr >= end) return;
if(_term_map.find(addr) == _term_map.end())
get_operands(f,addr,_term_map[addr]);
vector<OperandTerm *> & terms1 = _term_map[addr];
Address cur = addr;
InstructionDecoder dec(
(unsigned char *)f->isrc()->getPtrToInstruction(addr),
f->isrc()->offset() + f->isrc()->length() - addr,
f->isrc()->getArch());
unsigned d = size;
if(!f->isrc()->getPtrToInstruction(cur))
return;
insn = dec.decode();
cur += insn.size();
if(_term_map.find(cur) == _term_map.end())
get_operands(f,cur,_term_map[cur]);
vector<OperandTerm *> & terms2 = _term_map[cur];
// need a distance-d wildcard
OperandTerm * wc = NULL;
if(d > 0) {
wc = OP_WC[d-1];
}
for(unsigned i=0;i<terms1.size();++i) {
OperandTerm * ot1 = terms1[i];
Lnode * node = start.next(ot1);
if(wc)
node = node->next(wc);
for(unsigned j=0;j<terms2.size();++j) {
OperandTerm * ot2 = terms2[j];
node = node->next(ot2);
if(!node->f && !fixed) {
node->f = new OperandFeature();
node->f->add_term(ot1);
if(wc)
node->f->add_term(wc);
node->f->add_term(ot2);
}
if(node->f)
feats.push_back(node->f);
}
}
}
template<typename T>
Lookup<T>::~Lookup()
{
typename unordered_map<Address, vector<LT *> >::iterator tit = _term_map.begin();
for( ; tit != _term_map.end(); ++tit)
for(unsigned i=0;i<tit->second.size();++i)
delete (tit->second)[i];
}
template<typename T>
typename Lookup<T>::Lnode *
Lookup<T>::Lnode::next(LT *t)
{
if(_next.find(*t) == _next.end()) {
_next[*t] = new Lnode();
}
return _next[*t];
}
template<typename T>
Lookup<T>::Lnode::Lnode() :
f(NULL)
{
}
template<typename T>
Lookup<T>::Lnode::~Lnode()
{
if(f)
delete f;
typename lmap_t::iterator nit = _next.begin();
for( ; nit != _next.end(); ++nit) {
delete (*nit).second;
}
}
/* Required because of partial specialization of lookup */
template class Lookup<IdiomFeature>;
template class Lookup<OperandFeature>;