-
Notifications
You must be signed in to change notification settings - Fork 149
/
IndirectASTVisitor.C
606 lines (565 loc) · 22.5 KB
/
IndirectASTVisitor.C
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
#include "dyntypes.h"
#include "IndirectASTVisitor.h"
#include "debug_parse.h"
#include "CodeObject.h"
#include <algorithm>
using namespace Dyninst::ParseAPI;
#define SIGNEX_64_32 0xffffffff00000000LL
#define SIGNEX_64_16 0xffffffffffff0000LL
#define SIGNEX_64_8 0xffffffffffffff00LL
#define SIGNEX_32_16 0xffff0000
#define SIGNEX_32_8 0xffffff00
Address PCValue(Address cur, size_t insnSize, Architecture a) {
switch (a) {
case Arch_x86:
case Arch_x86_64:
return cur + insnSize;
case Arch_aarch64:{
return cur;
}
}
return cur + insnSize;
}
AST::Ptr SimplifyVisitor::visit(DataflowAPI::RoseAST *ast) {
unsigned totalChildren = ast->numChildren();
for (unsigned i = 0 ; i < totalChildren; ++i) {
ast->child(i)->accept(this);
ast->setChild(i, SimplifyRoot(ast->child(i), addr));
}
return AST::Ptr();
}
AST::Ptr SimplifyRoot(AST::Ptr ast, Address addr) {
if (ast->getID() == AST::V_RoseAST) {
RoseAST::Ptr roseAST = boost::static_pointer_cast<RoseAST>(ast);
switch (roseAST->val().op) {
case ROSEOperation::invertOp:
if (roseAST->child(0)->getID() == AST::V_RoseAST) {
RoseAST::Ptr child = boost::static_pointer_cast<RoseAST>(roseAST->child(0));
if (child->val().op == ROSEOperation::invertOp) return child->child(0);
} else if (roseAST->child(0)->getID() == AST::V_ConstantAST) {
ConstantAST::Ptr child = boost::static_pointer_cast<ConstantAST>(roseAST->child(0));
size_t size = child->val().size;
uint64_t val = child->val().val;
if (size < 64) {
uint64_t mask = (1ULL << size) - 1;
val = (~val) & mask;
} else
val = ~val;
return ConstantAST::create(Constant(val, size));
}
break;
case ROSEOperation::extendMSBOp:
case ROSEOperation::extractOp:
case ROSEOperation::signExtendOp:
case ROSEOperation::concatOp:
return roseAST->child(0);
case ROSEOperation::addOp:
// We simplify the addition as much as we can
// Case 1: two constants
if (roseAST->child(0)->getID() == AST::V_ConstantAST && roseAST->child(1)->getID() == AST::V_ConstantAST) {
ConstantAST::Ptr child0 = boost::static_pointer_cast<ConstantAST>(roseAST->child(0));
ConstantAST::Ptr child1 = boost::static_pointer_cast<ConstantAST>(roseAST->child(1));
uint64_t val = child0->val().val + child1->val().val;
size_t size;
if (child0->val().size > child1->val().size)
size = child0->val().size;
else
size = child1->val().size;
return ConstantAST::create(Constant(val,size));
}
// Case 2: anything adding zero stays the same
if (roseAST->child(0)->getID() == AST::V_ConstantAST) {
ConstantAST::Ptr child = boost::static_pointer_cast<ConstantAST>(roseAST->child(0));
if (child->val().val == 0) return roseAST->child(1);
}
if (roseAST->child(1)->getID() == AST::V_ConstantAST) {
ConstantAST::Ptr child = boost::static_pointer_cast<ConstantAST>(roseAST->child(1));
if (child->val().val == 0) return roseAST->child(0);
}
// Case 3: if v + v * c = v * (c+1), where v is a variable and c is a constant
if (roseAST->child(0)->getID() == AST::V_VariableAST && roseAST->child(1)->getID() == AST::V_RoseAST) {
RoseAST::Ptr rOp = boost::static_pointer_cast<RoseAST>(roseAST->child(1));
if (rOp->val().op == ROSEOperation::uMultOp || rOp->val().op == ROSEOperation::sMultOp) {
if (rOp->child(0)->getID() == AST::V_VariableAST && rOp->child(1)->getID() == AST::V_ConstantAST) {
VariableAST::Ptr varAST1 = boost::static_pointer_cast<VariableAST>(roseAST->child(0));
VariableAST::Ptr varAST2 = boost::static_pointer_cast<VariableAST>(rOp->child(0));
if (varAST1->val().reg == varAST2->val().reg) {
ConstantAST::Ptr oldC = boost::static_pointer_cast<ConstantAST>(rOp->child(1));
ConstantAST::Ptr newC = ConstantAST::create(Constant(oldC->val().val + 1, oldC->val().size));
RoseAST::Ptr newRoot = RoseAST::create(ROSEOperation(rOp->val()), varAST1, newC);
return newRoot;
}
}
}
}
break;
case ROSEOperation::sMultOp:
case ROSEOperation::uMultOp:
if (roseAST->child(0)->getID() == AST::V_ConstantAST) {
ConstantAST::Ptr child0 = boost::static_pointer_cast<ConstantAST>(roseAST->child(0));
if (child0->val().val == 1) return roseAST->child(1);
}
if (roseAST->child(1)->getID() == AST::V_ConstantAST) {
ConstantAST::Ptr child1 = boost::static_pointer_cast<ConstantAST>(roseAST->child(1));
if (child1->val().val == 1) return roseAST->child(0);
}
break;
case ROSEOperation::xorOp:
if (roseAST->child(0)->getID() == AST::V_VariableAST && roseAST->child(1)->getID() == AST::V_VariableAST) {
VariableAST::Ptr child0 = boost::static_pointer_cast<VariableAST>(roseAST->child(0));
VariableAST::Ptr child1 = boost::static_pointer_cast<VariableAST>(roseAST->child(1));
if (child0->val() == child1->val()) {
return ConstantAST::create(Constant(0 , 32));
}
}
break;
case ROSEOperation::derefOp:
// Any 8-bit value is bounded in [0,255].
// Need to keep the length of the dereference if it is 8-bit.
// However, dereference longer than 8-bit should be regarded the same.
if (roseAST->val().size == 8)
return ast;
else
return RoseAST::create(ROSEOperation(ROSEOperation::derefOp), ast->child(0));
break;
case ROSEOperation::shiftLOp:
if (roseAST->child(0)->getID() == AST::V_ConstantAST && roseAST->child(1)->getID() == AST::V_ConstantAST) {
ConstantAST::Ptr child0 = boost::static_pointer_cast<ConstantAST>(roseAST->child(0));
ConstantAST::Ptr child1 = boost::static_pointer_cast<ConstantAST>(roseAST->child(1));
return ConstantAST::create(Constant(child0->val().val << child1->val().val, 64));
}
break;
case ROSEOperation::andOp:
if (roseAST->child(0)->getID() == AST::V_ConstantAST && roseAST->child(1)->getID() == AST::V_ConstantAST) {
ConstantAST::Ptr child0 = boost::static_pointer_cast<ConstantAST>(roseAST->child(0));
ConstantAST::Ptr child1 = boost::static_pointer_cast<ConstantAST>(roseAST->child(1));
return ConstantAST::create(Constant(child0->val().val & child1->val().val, 64));
}
break;
case ROSEOperation::orOp:
if (roseAST->child(0)->getID() == AST::V_ConstantAST && roseAST->child(1)->getID() == AST::V_ConstantAST) {
ConstantAST::Ptr child0 = boost::static_pointer_cast<ConstantAST>(roseAST->child(0));
ConstantAST::Ptr child1 = boost::static_pointer_cast<ConstantAST>(roseAST->child(1));
return ConstantAST::create(Constant(child0->val().val | child1->val().val, 64));
}
break;
default:
break;
}
} else if (ast->getID() == AST::V_VariableAST) {
VariableAST::Ptr varAST = boost::static_pointer_cast<VariableAST>(ast);
if (varAST->val().reg.absloc().isPC()) {
MachRegister pc = varAST->val().reg.absloc().reg();
return ConstantAST::create(Constant(addr, getArchAddressWidth(pc.getArchitecture()) * 8));
}
// We do not care about the address of the a-loc
// because we will keep tracking the changes of
// each a-loc. Also, this brings a benefit that
// we can directly use ast->isStrictEqual() to
// compare two ast.
return VariableAST::create(Variable(varAST->val().reg));
} else if (ast->getID() == AST::V_ConstantAST) {
ConstantAST::Ptr constAST = boost::static_pointer_cast<ConstantAST>(ast);
size_t size = constAST->val().size;
uint64_t val = constAST->val().val;
if (size == 32)
if (!(val & (1ULL << (size - 1))))
return ConstantAST::create(Constant(val, 64));
}
return ast;
}
AST::Ptr SimplifyAnAST(AST::Ptr ast, Address addr) {
SimplifyVisitor sv(addr);
ast->accept(&sv);
return SimplifyRoot(ast, addr);
}
AST::Ptr BoundCalcVisitor::visit(DataflowAPI::RoseAST *ast) {
BoundValue *astBound = boundFact.GetBound(ast);
if (astBound != NULL) {
bound.insert(make_pair(ast, new BoundValue(*astBound)));
return AST::Ptr();
}
unsigned totalChildren = ast->numChildren();
for (unsigned i = 0 ; i < totalChildren; ++i) {
ast->child(i)->accept(this);
}
switch (ast->val().op) {
case ROSEOperation::addOp:
if (IsResultBounded(ast->child(0)) && IsResultBounded(ast->child(1))) {
BoundValue* val = new BoundValue(*GetResultBound(ast->child(0)));
val->Add(*GetResultBound(ast->child(1)));
if (*val != BoundValue::top)
bound.insert(make_pair(ast, val));
}
break;
case ROSEOperation::invertOp:
if (IsResultBounded(ast->child(0))) {
BoundValue *val = new BoundValue(*GetResultBound(ast->child(0)));
if (val->tableReadSize)
val->isInverted = true;
else {
val->Invert();
}
if (*val != BoundValue::top)
bound.insert(make_pair(ast,val));
}
break;
case ROSEOperation::andOp: {
// For and operation, even one of them is a top,
// we may still produce bound result.
// For example, top And 0[3,3] => 1[0,3]
//
// the bound produced by and may be more relaxed than
// a cmp bound not found yet. So we only apply and
// bound when this is the last attempt
if (handleOneByteRead) {
BoundValue *val = NULL;
if (IsResultBounded(ast->child(0)))
val = new BoundValue(*GetResultBound(ast->child(0)));
else
val = new BoundValue(BoundValue::top);
if (IsResultBounded(ast->child(1)))
val->And(GetResultBound(ast->child(1))->interval);
else
val->And(StridedInterval::top);
// the result of an AND operation should not be
// the table lookup. Set all other values to default
val->ClearTableCheck();
if (*val != BoundValue::top)
bound.insert(make_pair(ast, val));
}
break;
}
case ROSEOperation::sMultOp:
case ROSEOperation::uMultOp:
if (IsResultBounded(ast->child(0)) && IsResultBounded(ast->child(1))) {
BoundValue *val = new BoundValue(*GetResultBound(ast->child(0)));
val->Mul(*GetResultBound(ast->child(1)));
if (*val != BoundValue::top)
bound.insert(make_pair(ast, val));
}
break;
case ROSEOperation::shiftLOp:
if (IsResultBounded(ast->child(0)) && IsResultBounded(ast->child(1))) {
BoundValue *val = new BoundValue(*GetResultBound(ast->child(0)));
val->ShiftLeft(*GetResultBound(ast->child(1)));
if (*val != BoundValue::top)
bound.insert(make_pair(ast, val));
}
break;
case ROSEOperation::shiftROp:
if (IsResultBounded(ast->child(0)) && IsResultBounded(ast->child(1))) {
BoundValue *val = new BoundValue(*GetResultBound(ast->child(0)));
val->ShiftRight(*GetResultBound(ast->child(1)));
if (*val != BoundValue::top)
bound.insert(make_pair(ast, val));
}
break;
case ROSEOperation::derefOp:
if (IsResultBounded(ast->child(0))) {
BoundValue *val = new BoundValue(*GetResultBound(ast->child(0)));
val->MemoryRead(block, derefSize);
if (*val != BoundValue::top)
bound.insert(make_pair(ast, val));
} else if (handleOneByteRead && ast->val().size == 8) {
// Any 8-bit value is bounded in [0,255]
// But I should only do this when I know the read
// itself is not a jump table
bound.insert(make_pair(ast, new BoundValue(StridedInterval(1,0,255))));
}
break;
case ROSEOperation::orOp:
if (IsResultBounded(ast->child(0)) && IsResultBounded(ast->child(1))) {
BoundValue *val = new BoundValue(*GetResultBound(ast->child(0)));
val->Or(*GetResultBound(ast->child(1)));
if (*val != BoundValue::top)
bound.insert(make_pair(ast, val));
}
break;
case ROSEOperation::ifOp:
if (IsResultBounded(ast->child(1)) && IsResultBounded(ast->child(2))) {
BoundValue *val = new BoundValue(*GetResultBound(ast->child(1)));
val->Join(*GetResultBound(ast->child(2)), block);
if (*val != BoundValue::top)
bound.insert(make_pair(ast, val));
}
default:
break;
}
return AST::Ptr();
}
AST::Ptr BoundCalcVisitor::visit(DataflowAPI::ConstantAST *ast) {
const Constant &v = ast->val();
int64_t value = v.val;
if (v.size != 1 && v.size != 64 && (value & (1ULL << (v.size - 1)))) {
// Compute the two complements in bits of v.size
// and change it to a negative number
value = -(((~value) & ((1ULL << v.size) - 1)) + 1);
}
bound.insert(make_pair(ast, new BoundValue(value)));
return AST::Ptr();
}
AST::Ptr BoundCalcVisitor::visit(DataflowAPI::VariableAST *ast) {
BoundValue *astBound = boundFact.GetBound(ast);
if (astBound != NULL)
bound.insert(make_pair(ast, new BoundValue(*astBound)));
return AST::Ptr();
}
BoundValue* BoundCalcVisitor::GetResultBound(AST::Ptr ast) {
if (IsResultBounded(ast)) {
return bound.find(ast.get())->second;
} else {
return NULL;
}
}
BoundCalcVisitor::~BoundCalcVisitor() {
for (auto bit = bound.begin(); bit != bound.end(); ++bit)
if (bit->second != NULL)
delete bit->second;
bound.clear();
}
AST::Ptr JumpCondVisitor::visit(DataflowAPI::RoseAST *ast) {
if (ast->val().op == ROSEOperation::invertOp) {
invertFlag = true;
return AST::Ptr();
}
unsigned totalChildren = ast->numChildren();
for (unsigned i = 0 ; i < totalChildren; ++i) {
ast->child(i)->accept(this);
}
return AST::Ptr();
}
AST::Ptr ComparisonVisitor::visit(DataflowAPI::RoseAST *ast) {
// For cmp type instruction setting zf
// Looking like <eqZero?>(<add>(<V([x86_64::rbx])>,<Imm:8>,),)
// Assuming ast has been simplified
if (ast->val().op == ROSEOperation::equalToZeroOp) {
bool minuendIsZero = true;
AST::Ptr child = ast->child(0);
if (child->getID() == AST::V_RoseAST) {
RoseAST::Ptr childRose = boost::static_pointer_cast<RoseAST>(child);
if (childRose->val().op == ROSEOperation::addOp) {
minuendIsZero = false;
subtrahend = childRose->child(0);
minuend = childRose->child(1);
// If the minuend is a constant, then
// the minuend is currently in its two-complement form
if (minuend->getID() == AST::V_ConstantAST) {
ConstantAST::Ptr constAST = boost::static_pointer_cast<ConstantAST>(minuend);
uint64_t val = constAST->val().val;
int size = constAST->val().size;
if (size < 64)
val = ((~val)+ 1) & ((1ULL << size) - 1);
else if (size == 64)
val = (~val) + 1;
else
parsing_printf("WARNING: constant bit size %d exceeds 64!\n", size);
minuend = ConstantAST::create(Constant(val, size));
} else if (minuend->getID() == AST::V_RoseAST) {
RoseAST::Ptr sub = boost::static_pointer_cast<RoseAST>(minuend);
minuend = AST::Ptr();
if (sub->val().op == ROSEOperation::addOp && sub->child(0)->getID() == AST::V_RoseAST) {
sub = boost::static_pointer_cast<RoseAST>(sub->child(0));
if (sub->val().op == ROSEOperation::invertOp) {
// Otherwise, the minuend ast is in the form of add(invert(minuend), 1)
// Need to extract the real minuend
minuend = sub->child(0);
}
}
}
}
}
if (minuendIsZero) {
// The minuend is 0, thus the add operation is subsume.
subtrahend = ast->child(0);
minuend = ConstantAST::create(Constant(0));
}
}
return AST::Ptr();
}
AST::Ptr SubstituteAnAST(AST::Ptr ast, const BoundFact::AliasMap &aliasMap) {
for (auto ait = aliasMap.begin(); ait != aliasMap.end(); ++ait)
if (*ast == *(ait->first)) {
return ait->second;
}
unsigned totalChildren = ast->numChildren();
for (unsigned i = 0 ; i < totalChildren; ++i) {
ast->setChild(i, SubstituteAnAST(ast->child(i), aliasMap));
}
if (ast->getID() == AST::V_VariableAST) {
// If this variable is not in the aliasMap yet,
// this variable is from the input.
VariableAST::Ptr varAST = boost::static_pointer_cast<VariableAST>(ast);
return VariableAST::create(Variable(varAST->val().reg, 1));
}
return ast;
}
bool ContainAnAST(AST::Ptr root, AST::Ptr check) {
if (*root == *check) return true;
bool ret = false;
unsigned totalChildren = root->numChildren();
for (unsigned i = 0 ; i < totalChildren && !ret; ++i) {
ret |= ContainAnAST(root->child(i), check);
}
return ret;
}
AST::Ptr DeepCopyAnAST(AST::Ptr ast) {
if (ast->getID() == AST::V_RoseAST) {
RoseAST::Ptr roseAST = boost::static_pointer_cast<RoseAST>(ast);
AST::Children kids;
unsigned totalChildren = ast->numChildren();
for (unsigned i = 0 ; i < totalChildren; ++i) {
kids.push_back(DeepCopyAnAST(ast->child(i)));
}
return RoseAST::create(ROSEOperation(roseAST->val()), kids);
} else if (ast->getID() == AST::V_VariableAST) {
VariableAST::Ptr varAST = boost::static_pointer_cast<VariableAST>(ast);
return VariableAST::create(Variable(varAST->val()));
} else if (ast->getID() == AST::V_ConstantAST) {
ConstantAST::Ptr constAST = boost::static_pointer_cast<ConstantAST>(ast);
return ConstantAST::create(Constant(constAST->val()));
} else if (ast->getID() == AST::V_BottomAST) {
BottomAST::Ptr bottomAST = boost::static_pointer_cast<BottomAST>(ast);
return BottomAST::create(bottomAST->val());
}
fprintf(stderr, "ast type %d, %s\n", ast->getID(), ast->format().c_str());
assert(0);
return AST::Ptr();
}
AST::Ptr JumpTableFormatVisitor::visit(DataflowAPI::RoseAST *ast) {
bool findIncorrectFormat = false;
if (ast->val().op == ROSEOperation::derefOp) {
// We only check the first memory read
if (ast->child(0)->getID() == AST::V_RoseAST) {
RoseAST::Ptr roseAST = boost::static_pointer_cast<RoseAST>(ast->child(0));
if (roseAST->val().op == ROSEOperation::derefOp) {
// Two directly nested memory accesses cannot be jump tables
parsing_printf("Two directly nested memory access, not jump table format\n");
findIncorrectFormat = true;
} else if (roseAST->val().op == ROSEOperation::addOp) {
Address tableBase = 0;
if (roseAST->child(0)->getID() == AST::V_ConstantAST && roseAST->child(1)->getID() == AST::V_VariableAST) {
ConstantAST::Ptr constAST = boost::static_pointer_cast<ConstantAST>(roseAST->child(0));
tableBase = (Address)constAST->val().val;
}
if (roseAST->child(1)->getID() == AST::V_ConstantAST && roseAST->child(0)->getID() == AST::V_VariableAST) {
ConstantAST::Ptr constAST = boost::static_pointer_cast<ConstantAST>(roseAST->child(1));
tableBase = (Address)constAST->val().val;
}
if (tableBase) {
Architecture arch = b->obj()->cs()->getArch();
if (arch == Arch_x86) {
tableBase &= 0xffffffff;
}
#if defined(os_windows)
tableBase -= b->obj()->cs()->loadAddress();
#endif
if (!b->obj()->cs()->isValidAddress(tableBase)) {
parsing_printf("\ttableBase 0x%lx invalid, not jump table format\n", tableBase);
findIncorrectFormat = true;
}
if (!b->obj()->cs()->isReadOnly(tableBase)) {
parsing_printf("\ttableBase 0x%lx not read only, not jump table format\n", tableBase);
findIncorrectFormat = true;
}
}
}
}
if (findIncorrectFormat) {
format = false;
}
return AST::Ptr();
}
if (!findIncorrectFormat) {
unsigned totalChildren = ast->numChildren();
for (unsigned i = 0 ; i < totalChildren; ++i) {
ast->child(i)->accept(this);
}
}
return AST::Ptr();
}
bool PerformTableRead(BoundValue &target, set<int> & jumpTargets, CodeSource *cs) {
Address tableBase = (Address)target.interval.low;
Address tableLastEntry = (Address)target.interval.high;
int addressWidth = cs->getAddressWidth();
if (addressWidth == 4) {
tableBase &= 0xffffffff;
tableLastEntry &= 0xffffffff;
}
#if defined(os_windows)
tableBase -= cs->loadAddress();
tableLastEntry -= cs->loadAddress();
#endif
if (!cs->isCode(tableBase) && !cs->isData(tableBase)) {
parsing_printf("\ttableBase 0x%lx invalid, returning false\n", tableBase);
parsing_printf("Not jump table format!\n");
return false;
}
if (!cs->isReadOnly(tableBase)) {
parsing_printf("\ttableBase 0x%lx not read only, returning false\n", tableBase);
parsing_printf("Not jump table format!\n");
return false;
}
for (Address tableEntry = tableBase; tableEntry <= tableLastEntry; tableEntry += target.interval.stride) {
if (!cs->isCode(tableEntry) && !cs->isData(tableEntry)) continue;
if (!cs->isReadOnly(tableEntry)) continue;
int targetAddress = 0;
if (target.tableReadSize > 0) {
switch (target.tableReadSize) {
case 8:
targetAddress = *(const uint64_t *) cs->getPtrToInstruction(tableEntry);
break;
case 4:
targetAddress = *(const uint32_t *) cs->getPtrToInstruction(tableEntry);
if (target.isZeroExtend) break;
if ((addressWidth == 8) && (targetAddress & 0x80000000)) {
targetAddress |= SIGNEX_64_32;
}
break;
case 2:
targetAddress = *(const uint16_t *) cs->getPtrToInstruction(tableEntry);
if (target.isZeroExtend) break;
if ((addressWidth == 8) && (targetAddress & 0x8000)) {
targetAddress |= SIGNEX_64_16;
}
if ((addressWidth == 4) && (targetAddress & 0x8000)) {
targetAddress |= SIGNEX_32_16;
}
break;
case 1:
targetAddress = *(const uint8_t *) cs->getPtrToInstruction(tableEntry);
if (target.isZeroExtend) break;
if ((addressWidth == 8) && (targetAddress & 0x80)) {
targetAddress |= SIGNEX_64_8;
}
if ((addressWidth == 4) && (targetAddress & 0x80)) {
targetAddress |= SIGNEX_32_8;
}
break;
default:
parsing_printf("Invalid memory read size %d\n", target.tableReadSize);
return false;
}
targetAddress *= target.multiply;
if (target.targetBase != 0) {
if (target.isSubReadContent)
targetAddress = target.targetBase - targetAddress;
else
targetAddress += target.targetBase;
}
#if defined(os_windows)
targetAddress -= cs->loadAddress();
#endif
} else targetAddress = tableEntry;
if (addressWidth == 4) targetAddress &= 0xffffffff;
parsing_printf("Jumping to target %lx,", targetAddress);
if (cs->isCode(targetAddress)) {
// Jump tables may contain may repetitious entries.
// We only want to create one edge for disctinct each jump target.
jumpTargets.insert(targetAddress);
}
// If the jump target is resolved to be a constant,
if (target.interval.stride == 0) break;
}
return true;
}