/
fcn.c
2307 lines (2165 loc) · 61.8 KB
/
fcn.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
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* radare - LGPL - Copyright 2010-2018 - nibble, alvaro, pancake */
#include <r_anal.h>
#include <r_util.h>
#include <r_list.h>
#define VARPREFIX "local"
#define ARGPREFIX "arg"
#define USE_SDB_CACHE 0
#define SDB_KEY_BB "bb.0x%"PFMT64x ".0x%"PFMT64x
// XXX must be configurable by the user
#define FCN_DEPTH 512
#define JMPTBLSZ 512
#define JMPTBL_LEA_SEARCH_SZ 64
#define JMPTBL_MAXFCNSIZE 4096
/* speedup analysis by removing some function overlapping checks */
#define JAYRO_04 0
// 16 KB is the maximum size for a basic block
#define MAXBBSIZE 16 * 1024
#define MAX_FLG_NAME_SIZE 64
#define FIX_JMP_FWD 0
#define JMP_IS_EOB 1
#define JMP_IS_EOB_RANGE 64
#define CALL_IS_EOB 0
// 64KB max size
// 256KB max function size
#define MAX_FCN_SIZE (1024 * 256)
#define DB a->sdb_fcns
#define EXISTS(x, ...) snprintf (key, sizeof (key) - 1, x, ## __VA_ARGS__), sdb_exists (DB, key)
#define SETKEY(x, ...) snprintf (key, sizeof (key) - 1, x, ## __VA_ARGS__);
#define VERBOSE_DELAY if (0)
#define FCN_CONTAINER(x) container_of ((RBNode*)x, RAnalFunction, rb)
#define fcn_tree_foreach_intersect(root, it, data, from, to) \
for (it = _fcn_tree_iter_first (root, from, to); it.cur && (data = FCN_CONTAINER (it.cur), 1); _fcn_tree_iter_next (&(it), from, to))
typedef struct fcn_tree_iter_t {
int len;
RBNode *cur;
RBNode *path[R_RBTREE_MAX_HEIGHT];
} FcnTreeIter;
#if USE_SDB_CACHE
static Sdb *HB = NULL;
#endif
R_API const char *r_anal_fcn_type_tostring(int type) {
switch (type) {
case R_ANAL_FCN_TYPE_NULL: return "null";
case R_ANAL_FCN_TYPE_FCN: return "fcn";
case R_ANAL_FCN_TYPE_LOC: return "loc";
case R_ANAL_FCN_TYPE_SYM: return "sym";
case R_ANAL_FCN_TYPE_IMP: return "imp";
case R_ANAL_FCN_TYPE_INT: return "int"; // interrupt
case R_ANAL_FCN_TYPE_ROOT: return "root";
}
return "unk";
}
static int cmpaddr(const void *_a, const void *_b) {
const RAnalBlock *a = _a, *b = _b;
return (a->addr - b->addr);
}
R_API void r_anal_fcn_update_tinyrange_bbs(RAnalFunction *fcn) {
RAnalBlock *bb;
RListIter *iter;
r_list_sort (fcn->bbs, &cmpaddr);
r_tinyrange_fini (&fcn->bbr);
r_list_foreach (fcn->bbs, iter, bb) {
r_tinyrange_add (&fcn->bbr, bb->addr, bb->addr + bb->size);
}
}
// _fcn_tree_{cmp_addr,calc_max_addr,free,probe} are used by interval tree.
static int _fcn_tree_cmp_addr(const void *a_, const RBNode *b_) {
const RAnalFunction *a = (const RAnalFunction *)a_;
const RAnalFunction *b = FCN_CONTAINER (b_);
ut64 from0 = a->addr, to0 = a->addr + a->_size,
from1 = b->addr, to1 = b->addr + b->_size;
if (from0 != from1) {
return from0 < from1 ? -1 : 1;
}
if (to0 != to1) {
return to0 - 1 < to1 - 1 ? -1 : 1;
}
return 0;
}
static void _fcn_tree_calc_max_addr(RBNode *node) {
int i;
RAnalFunction *fcn = FCN_CONTAINER (node);
fcn->rb_max_addr = fcn->addr + (fcn->_size == 0 ? 0 : fcn->_size - 1);
for (i = 0; i < 2; i++) {
if (node->child[i]) {
RAnalFunction *fcn1 = FCN_CONTAINER (node->child[i]);
if (fcn1->rb_max_addr > fcn->rb_max_addr) {
fcn->rb_max_addr = fcn1->rb_max_addr;
}
}
}
}
static void _fcn_tree_free(RBNode *node) {
// TODO RB tree is an intrusive data structure by embedding RBNode into RAnalFunction.
// Currently fcns takes the ownership of the resources.
// If the ownership transfers from fcns to fcn_tree:
//
// r_anal_fcn_free (FCN_CONTAINER (node));
}
// Descent x_ to find the first node whose interval intersects [from, to)
static RBNode *_fcn_tree_probe(FcnTreeIter *it, RBNode *x_, ut64 from, ut64 to) {
RAnalFunction *x = FCN_CONTAINER (x_), *y;
RBNode *y_;
for (;;) {
if ((y_ = x_->child[0]) && (y = FCN_CONTAINER (y_), from <= y->rb_max_addr)) {
it->path[it->len++] = x_;
x_ = y_;
x = y;
continue;
}
if (x->addr <= to - 1) {
if (from <= x->addr + (x->_size == 0 ? 0 : x->_size - 1)) {
return x_;
}
if ((y_ = x_->child[1])) {
x_ = y_;
x = FCN_CONTAINER (y_);
if (from <= x->rb_max_addr) {
continue;
}
}
}
return NULL;
}
}
R_API bool r_anal_fcn_tree_delete(RBNode **root, RAnalFunction *data) {
return r_rbtree_aug_delete (root, data, _fcn_tree_cmp_addr, _fcn_tree_free, _fcn_tree_calc_max_addr);
}
R_API void r_anal_fcn_tree_insert(RBNode **root, RAnalFunction *fcn) {
r_rbtree_aug_insert (root, fcn, &(fcn->rb), _fcn_tree_cmp_addr, _fcn_tree_calc_max_addr);
}
static void _fcn_tree_update_size(RBNode *root, RAnalFunction *fcn) {
r_rbtree_aug_update_sum (root, fcn, &(fcn->rb), _fcn_tree_cmp_addr, _fcn_tree_calc_max_addr);
}
#if 0
static void _fcn_tree_print_dot_node(RBNode *n) {
int i;
RAnalFunction *fcn = FCN_CONTAINER (n);
ut64 max_addr = fcn->addr + (fcn->_size == 0 ? 0 : fcn->_size - 1);
for (i = 0; i < 2; i++) {
if (n->child[i]) {
RAnalFunction *fcn1 = FCN_CONTAINER (n->child[i]);
if (fcn1->rb_max_addr > max_addr) {
max_addr = fcn1->rb_max_addr;
}
}
}
bool valid = max_addr == fcn->rb_max_addr;
r_cons_printf (" \"%p\" [label=\"%p\\naddr: 0x%08"PFMT64x"\\nmax_addr: 0x%08"PFMT64x"\"%s];\n",
n, fcn, fcn->addr, fcn->rb_max_addr, valid ? "" : ", color=\"red\", fillcolor=\"white\"");
for (i=0; i<2; i++) {
if (n->child[i]) {
_fcn_tree_print_dot_node (n->child[i]);
bool valid = true;
if (n->child[i]) {
RAnalFunction *childfcn = FCN_CONTAINER (n->child[i]);
if ((i == 0 && childfcn->addr >= fcn->addr) || (i == 1 && childfcn->addr <= fcn->addr)) {
valid = false;
}
}
r_cons_printf (" \"%p\" -> \"%p\" [label=\"%d\"%s];\n", n, n->child[i], i, valid ? "" : ", style=\"bold\", color=\"red\"");
} else {
r_cons_printf (" \"null_%p_%d\" [shape=point];\n", n, i);
r_cons_printf (" \"%p\" -> \"null_%p_%d\" [label=\"%d\"];\n", n, n, i, i);
}
}
}
static void _fcn_tree_print_dot(RBNode *n) {
r_cons_print ("digraph fcn_tree {\n");
if (n) {
_fcn_tree_print_dot_node (n);
}
r_cons_print ("}\n");
}
#endif
// Find RAnalFunction whose addr is equal to addr
static RAnalFunction *_fcn_tree_find_addr(RBNode *x_, ut64 addr) {
while (x_) {
RAnalFunction *x = FCN_CONTAINER (x_);
if (x->addr == addr) {
return x;
}
x_ = x_->child[x->addr < addr];
}
return NULL;
}
// _fcn_tree_{iter_first,iter_next} are used to iterate functions whose intervals intersect [from, to) in O(log(n) + |candidates|) time
static FcnTreeIter _fcn_tree_iter_first(RBNode *x_, ut64 from, ut64 to) {
FcnTreeIter it = {0};
it.len = 0;
if (x_ && from <= FCN_CONTAINER (x_)->rb_max_addr) {
it.cur = _fcn_tree_probe (&it, x_, from, to);
} else {
it.cur = NULL;
}
return it;
}
static void _fcn_tree_iter_next(FcnTreeIter *it, ut64 from, ut64 to) {
RBNode *x_ = it->cur, *y_;
RAnalFunction *x = FCN_CONTAINER (x_), *y;
for (;;) {
if ((y_ = x_->child[1]) && (y = FCN_CONTAINER (y_), from <= y->rb_max_addr)) {
it->cur = _fcn_tree_probe (it, y_, from, to);
break;
}
if (!it->len) {
it->cur = NULL;
break;
}
x_ = it->path[--it->len];
x = FCN_CONTAINER (x_);
if (to - 1 < x->addr) {
it->cur = NULL;
break;
}
if (from <= x->addr + (x->_size == 0 ? 0 : x->_size - 1)) {
it->cur = x_;
break;
}
}
}
R_API int r_anal_fcn_resize(const RAnal *anal, RAnalFunction *fcn, int newsize) {
ut64 eof; /* end of function */
RAnalBlock *bb;
RListIter *iter, *iter2;
if (!fcn || newsize < 1) {
return false;
}
r_anal_fcn_set_size (anal, fcn, newsize);
eof = fcn->addr + r_anal_fcn_size (fcn);
r_list_foreach_safe (fcn->bbs, iter, iter2, bb) {
if (bb->addr >= eof) {
// already called by r_list_delete r_anal_bb_free (bb);
r_list_delete (fcn->bbs, iter);
continue;
}
if (bb->addr + bb->size >= eof) {
bb->size = eof - bb->addr;
}
if (bb->jump != UT64_MAX && bb->jump >= eof) {
bb->jump = UT64_MAX;
}
if (bb->fail != UT64_MAX && bb->fail >= eof) {
bb->fail = UT64_MAX;
}
}
r_anal_fcn_update_tinyrange_bbs (fcn);
return true;
}
R_API RAnalFunction *r_anal_fcn_new() {
RAnalFunction *fcn = R_NEW0 (RAnalFunction);
if (!fcn) {
return NULL;
}
/* Function return type */
fcn->rets = 0;
fcn->_size = 0;
/* Function qualifier: static/volatile/inline/naked/virtual */
fcn->fmod = R_ANAL_FQUALIFIER_NONE;
/* Function calling convention: cdecl/stdcall/fastcall/etc */
fcn->cc = NULL;
/* Function attributes: weak/noreturn/format/etc */
fcn->addr = UT64_MAX;
fcn->fcn_locs = NULL;
fcn->bbs = r_anal_bb_list_new ();
fcn->fingerprint = NULL;
fcn->diff = r_anal_diff_new ();
r_tinyrange_init (&fcn->bbr);
return fcn;
}
R_API RList *r_anal_fcn_list_new() {
RList *list = r_list_new ();
if (!list) {
return NULL;
}
list->free = &r_anal_fcn_free;
return list;
}
R_API void r_anal_fcn_free(void *_fcn) {
RAnalFunction *fcn = _fcn;
if (!_fcn) {
return;
}
fcn->_size = 0;
free (fcn->name);
free (fcn->attr);
r_tinyrange_fini (&fcn->bbr);
// all functions are freed in anal->fcns
fcn->fcn_locs = NULL;
if (fcn->bbs) {
fcn->bbs->free = (RListFree) r_anal_bb_free;
r_list_free (fcn->bbs);
fcn->bbs = NULL;
}
free (fcn->fingerprint);
r_anal_diff_free (fcn->diff);
free (fcn->args);
free (fcn);
}
#if 0
static bool refExists(RList *refs, RAnalRef *ref) {
RAnalRef *r;
RListIter *iter;
r_list_foreach (refs, iter, r) {
if (r && r->at == ref->at && ref->addr == r->addr) {
r->type = ref->type;
return true;
}
}
return false;
}
#endif
static RAnalBlock *bbget(RAnalFunction *fcn, ut64 addr) {
RListIter *iter;
RAnalBlock *bb;
r_list_foreach (fcn->bbs, iter, bb) {
ut64 eaddr = bb->addr + bb->size;
if (bb->addr >= eaddr && addr == bb->addr) {
return bb;
}
if ((addr >= bb->addr) && (addr < eaddr)) {
return bb;
}
}
return NULL;
}
static RAnalBlock *appendBasicBlock(RAnal *anal, RAnalFunction *fcn, ut64 addr) {
RAnalBlock *bb = r_anal_bb_new ();
if (!bb) {
return NULL;
}
bb->addr = addr;
bb->size = 0;
bb->jump = UT64_MAX;
bb->fail = UT64_MAX;
bb->type = 0; // TODO
r_anal_fcn_bbadd (fcn, bb);
if (anal->cb.on_fcn_bb_new) {
anal->cb.on_fcn_bb_new (anal, anal->user, fcn, bb);
}
return bb;
}
#define FITFCNSZ() {\
st64 n = bb->addr + bb->size - fcn->addr;\
if (n >= 0 && r_anal_fcn_size (fcn) < n) { r_anal_fcn_set_size (NULL, fcn, n); } }\
if (r_anal_fcn_size (fcn) > MAX_FCN_SIZE) {\
/* eprintf ("Function too big at 0x%"PFMT64x" + %d\n", bb->addr, fcn->size); */\
r_anal_fcn_set_size (NULL, fcn, 0);\
return R_ANAL_RET_ERROR; }
// ETOOSLOW
static char *get_varname(RAnal *a, RAnalFunction *fcn, char type, const char *pfx, int idx) {
char *varname = r_str_newf ("%s_%xh", pfx, idx);
int i = 2;
char v_kind;
int v_delta;
while (1) {
char *name_key = sdb_fmt ("var.0x%"PFMT64x ".%d.%s", fcn->addr, 1, varname);
char *name_value = sdb_get (DB, name_key, 0);
if (!name_value) {
break;
}
const char *comma = strchr (name_value, ',');
if (comma && *comma) {
v_delta = r_num_math (NULL, comma + 1);
v_kind = *name_value;
}
if (v_kind == type && R_ABS (v_delta) == idx) {
free (name_value);
return varname;
}
free (varname);
free (name_value);
varname = r_str_newf ("%s_%xh_%d", pfx, idx, i);
i++;
}
return varname;
}
static int fcn_recurse(RAnal *anal, RAnalFunction *fcn, ut64 addr, ut8 *buf, ut64 len, int depth);
#define recurseAt(x) {\
ut8 *bbuf = malloc (MAXBBSIZE);\
if (bbuf) {\
anal->iob.read_at (anal->iob.io, x, bbuf, MAXBBSIZE);\
ret = fcn_recurse (anal, fcn, x, bbuf, MAXBBSIZE, depth - 1);\
r_anal_fcn_update_tinyrange_bbs (fcn);\
free (bbuf);\
}\
}
static void queue_case(RAnal *anal, ut64 switch_addr, ut64 case_addr, ut64 id, ut64 case_addr_loc) {
// eprintf("\tqueue_case: 0x%"PFMT64x " from 0x%"PFMT64x "\n", case_addr, case_addr_loc);
anal->cmdtail = r_str_appendf (anal->cmdtail,
"axc 0x%"PFMT64x " 0x%"PFMT64x "\n",
case_addr, switch_addr);
anal->cmdtail = r_str_appendf (anal->cmdtail,
"afbe 0x%"PFMT64x " 0x%"PFMT64x "\n",
switch_addr, case_addr);
// anal->cmdtail = r_str_appendf (anal->cmdtail,
// "aho case %d: from 0x%"PFMT64x " @ 0x%"PFMT64x "\n",
// id, switch_addr, case_addr_loc);
// anal->cmdtail = r_str_appendf (anal->cmdtail,
// "CCu case %d: @ 0x%"PFMT64x "\n",
// id, case_addr);
anal->cmdtail = r_str_appendf (anal->cmdtail,
"f case.0x%"PFMT64x ".%d 1 @ 0x%08"PFMT64x "\n",
switch_addr, id, case_addr);
}
static int try_walkthrough_jmptbl(RAnal *anal, RAnalFunction *fcn, int depth, ut64 ip, ut64 jmptbl_loc, ut64 jmptbl_off, ut64 sz, ut64 jmptbl_size, ut64 default_case, int ret0) {
int ret = ret0;
// jmptbl_size can not always be determined
if (jmptbl_size == 0) {
jmptbl_size = JMPTBLSZ;
}
ut64 jmpptr, offs;
ut8 *jmptbl = malloc (jmptbl_size * sz);
if (!jmptbl) {
return 0;
}
// eprintf ("JMPTBL AT 0x%"PFMT64x"\n", jmptbl_loc);
anal->iob.read_at (anal->iob.io, jmptbl_loc, jmptbl, jmptbl_size * sz);
for (offs = 0; offs + sz - 1 < jmptbl_size * sz; offs += sz) {
switch (sz) {
case 1:
jmpptr = r_read_le8 (jmptbl + offs);
break;
case 2:
jmpptr = r_read_le16 (jmptbl + offs);
break;
case 4:
jmpptr = r_read_le32 (jmptbl + offs);
break;
case 8:
jmpptr = r_read_le32 (jmptbl + offs);
break; // XXX
default:
jmpptr = r_read_le64 (jmptbl + offs);
break;
}
// eprintf ("WALKING %llx\n", jmpptr);
// if we don't check for 0 here, the next check with ptr+jmpptr
// will obviously be a good offset since it will be the start
// of the table, which is not what we want
if (jmpptr == 0) {
break;
}
if (!anal->iob.is_valid_offset (anal->iob.io, jmpptr, 0)) {
// jump tables where sign extended movs are used
jmpptr = jmptbl_off + (st32) jmpptr;
if (!anal->iob.is_valid_offset (anal->iob.io, jmpptr, 0)) {
break;
}
}
if (anal->limit) {
if (jmpptr < anal->limit->from || jmpptr > anal->limit->to) {
break;
}
}
queue_case (anal, ip, jmpptr, offs/sz, jmptbl_loc + offs);
recurseAt (jmpptr);
}
if (offs > 0) {
// eprintf("\n\nSwitch statement at 0x%llx:\n", ip);
anal->cmdtail = r_str_appendf (anal->cmdtail,
"CCu switch table (%d cases) at 0x%"PFMT64x " @ 0x%"PFMT64x "\n",
offs/sz, jmptbl_loc, ip);
anal->cmdtail = r_str_appendf (anal->cmdtail,
"f switch.0x%08"PFMT64x" 1 @ 0x%08"PFMT64x"\n",
ip, ip);
anal->cmdtail = r_str_appendf (anal->cmdtail,
"f case.default.0x%"PFMT64x " 1 @ 0x%08"PFMT64x "\n",
default_case, default_case);
}
free (jmptbl);
return ret;
}
#if 0
static ut64 search_reg_val(RAnal *anal, ut8 *buf, ut64 len, ut64 addr, char *regsz) {
ut64 offs, oplen;
RAnalOp op = {
0
};
ut64 ret = UT64_MAX;
const int addrbytes = anal->iob.io ? anal->iob.io->addrbytes : 1;
for (offs = 0; offs < len; offs += addrbytes * oplen) {
r_anal_op_fini (&op);
if ((oplen = r_anal_op (anal, &op, addr + offs, buf + offs, len - offs, R_ANAL_OP_MASK_ALL)) < 1) {
break;
}
if (op.dst && op.dst->reg && op.dst->reg->name && !strcmp (op.dst->reg->name, regsz)) {
if (op.src[0]) {
ret = op.src[0]->delta;
}
}
}
return ret;
}
#endif
#define gotoBeach(x) ret = x; goto beach;
#define gotoBeachRet() goto beach;
static void extract_arg(RAnal *anal, RAnalFunction *fcn, RAnalOp *op, const char *reg, const char *sign, char type) {
char sigstr[16] = {0};
st64 ptr;
char *addr;
if (!anal || !fcn || !op) {
return;
}
// snprintf (sigstr, sizeof (sigstr), ",%s,%s", reg, sign);
snprintf (sigstr, sizeof (sigstr), ",%s,%s", reg, sign);
const char *op_esil = r_strbuf_get (&op->esil);
if (!op_esil) {
return;
}
char *esil_buf = strdup (op_esil);
if (!esil_buf) {
return;
}
char *ptr_end = strstr (esil_buf, sigstr);
if (!ptr_end) {
free (esil_buf);
return;
}
*ptr_end = 0;
addr = ptr_end;
while ((addr[0] != '0' || addr[1] != 'x') && addr >= esil_buf + 1 && *addr != ',') {
addr--;
}
if (strncmp (addr, "0x", 2)) {
//XXX: This is a workaround for inconsistent esil
if ((op->stackop == R_ANAL_STACK_SET) || (op->stackop == R_ANAL_STACK_GET)) {
ptr = R_ABS (op->ptr);
if (ptr%4) {
goto beach;
}
} else {
goto beach;
}
} else {
ptr = (st64) r_num_get (NULL, addr);
}
int rw = (op->direction == R_ANAL_OP_DIR_WRITE) ? 1 : 0;
if (*sign == '+') {
const char *pfx = ((ptr < fcn->maxstack) && (type == 's')) ? VARPREFIX : ARGPREFIX;
bool isarg = strcmp(pfx , ARGPREFIX) ? false : true;
char *varname = get_varname (anal, fcn, type, pfx, R_ABS (ptr));
r_anal_var_add (anal, fcn->addr, 1, ptr, type, NULL, anal->bits / 8, isarg, varname);
r_anal_var_access (anal, fcn->addr, type, 1, ptr, rw, op->addr);
free (varname);
} else {
char *varname = get_varname (anal, fcn, type, VARPREFIX, R_ABS (ptr));
r_anal_var_add (anal, fcn->addr, 1, -ptr, type, NULL, anal->bits / 8, 0, varname);
r_anal_var_access (anal, fcn->addr, type, 1, -ptr, rw, op->addr);
free (varname);
}
beach:
free (esil_buf);
}
R_API void r_anal_fcn_fill_args(RAnal *anal, RAnalFunction *fcn, RAnalOp *op) {
if (!anal || !fcn || !op) {
return;
}
const char *BP = anal->reg->name[R_REG_NAME_BP];
const char *SP = anal->reg->name[R_REG_NAME_SP];
extract_arg (anal, fcn, op, BP, "+", 'b');
extract_arg (anal, fcn, op, BP, "-", 'b');
extract_arg (anal, fcn, op, SP, "+", 's');
}
static bool isInvalidMemory(const ut8 *buf, int len) {
// can be wrong
return !memcmp (buf, "\xff\xff\xff\xff", R_MIN (len, 4));
// return buf[0]==buf[1] && buf[0]==0xff && buf[2]==0xff && buf[3] == 0xff;
}
static bool isSymbolNextInstruction(RAnal *anal, RAnalOp *op) {
if (!anal || !op || !anal->flb.get_at) {
return false;
}
RFlagItem *fi = anal->flb.get_at (anal->flb.f, op->addr + op->size, false);
return (fi && fi->name && (strstr (fi->name, "imp.") || strstr (fi->name, "sym.")
|| strstr (fi->name, "entry") || strstr (fi->name, "main")));
}
static bool is_delta_pointer_table (RAnal *anal, RAnalFunction *fcn, ut64 addr, ut64 lea_ptr, ut64 *jmptbl_addr, RAnalOp *jmp_aop) {
int i;
ut64 dst;
st32 jmptbl[64] = {0};
/* check if current instruction is followed by an ujmp */
ut8 buf[JMPTBL_LEA_SEARCH_SZ];
RAnalOp *aop = jmp_aop;
RAnalOp mov_aop = {0};
RAnalOp add_aop = {0};
anal->iob.read_at (anal->iob.io, addr, (ut8 *)buf, JMPTBL_LEA_SEARCH_SZ);
bool isValid = false;
for (i = 0; i + 8 < JMPTBL_LEA_SEARCH_SZ; i++) {
int len = r_anal_op (anal, aop, addr + i, buf + i, JMPTBL_LEA_SEARCH_SZ - i, R_ANAL_OP_MASK_BASIC);
if (len < 1) {
len = 1;
}
if (aop->type == R_ANAL_OP_TYPE_UJMP || aop->type == R_ANAL_OP_TYPE_RJMP) {
isValid = true;
break;
}
if (aop->type == R_ANAL_OP_TYPE_MOV) {
mov_aop = *aop;
}
if (aop->type == R_ANAL_OP_TYPE_ADD) {
add_aop = *aop;
}
i += len - 1;
}
if (!isValid) {
return false;
}
// check if we have a msvc 19xx style jump table using rva table entries
// lea reg1, [base_addr]
// mov reg2, sword [reg1 + tbl_off*4 + tbl_loc_off]
// add reg2, reg1
// jmp reg2
if (mov_aop.type && add_aop.type && mov_aop.addr < add_aop.addr && add_aop.addr < jmp_aop->addr && mov_aop.ptr) {
// ptr in this case should be tbl_loc_off
// eprintf ("JMPTBL ADDR %llx\n", mov_aop.ptr);
*jmptbl_addr += mov_aop.ptr;
}
#if 0
// required for the last jmptbl.. but seems to work without it and breaks other tests
if (mov_aop.type && mov_aop.ptr) {
*jmptbl_addr += mov_aop.ptr;
// absjmptbl
lea_ptr = mov_aop.ptr;
}
#endif
/* check if jump table contains valid deltas */
anal->iob.read_at (anal->iob.io, *jmptbl_addr, (ut8 *)&jmptbl, 64);
// XXX this is not endian safe
for (i = 0; i < 3; i++) {
dst = lea_ptr + jmptbl[0];
if (!anal->iob.is_valid_offset (anal->iob.io, dst, 0)) {
return false;
}
if (dst > fcn->addr + JMPTBL_MAXFCNSIZE) {
return false;
}
if (anal->opt.jmpabove && dst < (fcn->addr < JMPTBL_MAXFCNSIZE ? 0 : fcn->addr - JMPTBL_MAXFCNSIZE)) {
return false;
}
}
return true;
}
static bool try_get_delta_jmptbl_info (RAnal *anal, RAnalFunction *fcn, ut64 jmp_addr, ut64 lea_addr, ut64 *table_size, ut64 *default_case) {
bool isValid = false, foundCmp = false;
int i;
RAnalOp tmp_aop = {0};
int search_sz = jmp_addr - lea_addr;
if (search_sz < 0) {
return false;
}
ut8 *buf = malloc (search_sz);
if (!buf) {
return false;
}
// search for a cmp register with a resonable size
anal->iob.read_at (anal->iob.io, lea_addr, (ut8 *)buf, search_sz);
for (i = 0; i + 8 < search_sz; i++) {
int len = r_anal_op (anal, &tmp_aop, lea_addr + i, buf + i, JMPTBL_LEA_SEARCH_SZ - i, R_ANAL_OP_MASK_BASIC);
if (len < 1) {
len = 1;
}
if (foundCmp) {
if (tmp_aop.type != R_ANAL_OP_TYPE_CJMP) {
continue;
}
*default_case = tmp_aop.jump == tmp_aop.jump + len ? tmp_aop.fail : tmp_aop.jump;
break;
}
if (tmp_aop.type != R_ANAL_OP_TYPE_CMP) {
continue;
}
// get the value of the cmp
// for operands in op, check if type is immediate and val is sane
// TODO: How? opex?
// for the time being, this seems to work
// might not actually have a value, let the next step figure out the size then
if (tmp_aop.val == UT64_MAX && tmp_aop.refptr == 0) {
isValid = true;
*table_size = 0;
} else if (tmp_aop.refptr == 0) {
isValid = tmp_aop.val < 0x200;
*table_size = tmp_aop.val + 1;
} else {
isValid = tmp_aop.refptr < 0x200;
*table_size = tmp_aop.refptr + 1;
}
// TODO: check the jmp for whether val is included in valid range or not (ja vs jae)
foundCmp = true;
}
free (buf);
if (!isValid) {
return false;
}
// eprintf ("switch at 0x%" PFMT64x "\n\tdefault case 0x%" PFMT64x "\n\t#cases: %d\n",
// addr,
// *default_case,
// *table_size);
return true;
}
static bool try_get_jmptbl_info(RAnal *anal, RAnalFunction *fcn, ut64 addr, RAnalBlock *my_bb, ut64 *table_size, ut64 *default_case) {
bool isValid = false;
int i;
RListIter *iter;
RAnalBlock *tmp_bb, *prev_bb;
prev_bb = 0;
if (!fcn->bbs) {
return false;
}
/* if UJMP is in .plt section just skip it */
RBinSection *s = anal->binb.get_vsect_at (anal->binb.bin, addr);
if (s && s->name[0]) {
bool in_plt = strstr (s->name, ".plt") != NULL;
if (!in_plt && strstr (s->name, "_stubs") != NULL) {
/* for mach0 */
in_plt = true;
}
if (in_plt) {
return false;
}
}
// search for the predecessor bb
r_list_foreach (fcn->bbs, iter, tmp_bb) {
if (tmp_bb->jump == my_bb->addr || tmp_bb->fail == my_bb->addr) {
prev_bb = tmp_bb;
break;
}
}
// predecessor must be a conditional jump
if (!prev_bb || !prev_bb->jump || !prev_bb->fail) {
eprintf ("[anal.jmptbl] Missing cjmp bb in predecesor at 0x%08"PFMT64x"\n", addr);
return false;
}
// default case is the jump target of the unconditional jump
*default_case = prev_bb->jump == my_bb->addr ? prev_bb->fail : prev_bb->jump;
RAnalOp tmp_aop = {0};
ut8 *bb_buf = malloc (prev_bb->size);
if (!bb_buf) {
return false;
}
// search for a cmp register with a resonable size
anal->iob.read_at (anal->iob.io, prev_bb->addr, (ut8 *) bb_buf, prev_bb->size);
isValid = false;
for (i = 0; i < prev_bb->op_pos_size; i++) {
ut64 addr = prev_bb->addr + prev_bb->op_pos[i];
int len = r_anal_op (anal, &tmp_aop, addr, bb_buf + prev_bb->op_pos[i], prev_bb->size - prev_bb->op_pos[i], R_ANAL_OP_MASK_BASIC);
if (len < 1 || tmp_aop.type != R_ANAL_OP_TYPE_CMP) {
continue;
}
// get the value of the cmp
// for operands in op, check if type is immediate and val is sane
// TODO: How? opex?
// for the time being, this seems to work
// might not actually have a value, let the next step figure out the size then
if (tmp_aop.val == UT64_MAX && tmp_aop.refptr == 0) {
isValid = true;
*table_size = 0;
} else if (tmp_aop.refptr == 0) {
isValid = tmp_aop.val < 0x200;
*table_size = tmp_aop.val + 1;
} else {
isValid = tmp_aop.refptr < 0x200;
*table_size = tmp_aop.refptr + 1;
}
// TODO: check the jmp for whether val is included in valid range or not (ja vs jae)
break;
}
free (bb_buf);
if (!isValid) {
return false;
}
// eprintf ("switch at 0x%" PFMT64x "\n\tdefault case 0x%" PFMT64x "\n\t#cases: %d\n",
// addr,
// *default_case,
// *table_size);
return true;
}
static bool regs_exist(RAnalValue *src, RAnalValue *dst) {
return src && dst && src->reg && dst->reg && src->reg->name && dst->reg->name;
}
// 0 if not skipped; 1 if skipped; 2 if skipped before
static int skip_hp(RAnal *anal, RAnalFunction *fcn, RAnalOp *op, RAnalBlock *bb, ut64 addr,
char *tmp_buf, int oplen, int un_idx, int *idx) {
// this step is required in order to prevent infinite recursion in some cases
if ((addr + un_idx - oplen) == fcn->addr) {
if (!anal->flb.exist_at (anal->flb.f, "skip", 4, op->addr)) {
snprintf (tmp_buf + 5, MAX_FLG_NAME_SIZE - 6, "%"PFMT64u, op->addr);
anal->flb.set (anal->flb.f, tmp_buf, op->addr, oplen);
fcn->addr += oplen;
bb->size -= oplen;
bb->addr += oplen;
*idx = un_idx;
return 1;
}
return 2;
}
return 0;
}
R_API int r_anal_case(RAnal *anal, RAnalFunction *fcn, ut64 addr_bbsw, ut64 addr, ut8 *buf, ut64 len, int reftype) {
RAnalOp op = {
0
};
int oplen, idx = 0;
while (idx < len) {
if ((len - idx) < 5) {
break;
}
r_anal_op_fini (&op);
if ((oplen = r_anal_op (anal, &op, addr + idx, buf + idx, len - idx, R_ANAL_OP_MASK_BASIC)) < 1) {
return 0;
}
switch (op.type) {
case R_ANAL_OP_TYPE_TRAP:
case R_ANAL_OP_TYPE_RET:
case R_ANAL_OP_TYPE_JMP:
// eprintf ("CASE AT 0x%llx size %d\n", addr, idx + oplen);
anal->cmdtail = r_str_appendf (anal->cmdtail, "afb+ 0x%"PFMT64x " 0x%"PFMT64x " %d\n",
fcn->addr, addr, idx + oplen);
anal->cmdtail = r_str_appendf (anal->cmdtail, "afbe 0x%"PFMT64x " 0x%"PFMT64x "\n",
addr_bbsw, addr);
return idx + oplen;
}
idx += oplen;
}
return idx;
}
#if 0
static int walk_switch(RAnal *anal, RAnalFunction *fcn, ut64 from, ut64 at) {
ut8 buf[1024];
int i;
eprintf ("WALK SWITCH TABLE INTO (0x%"PFMT64x ") %"PFMT64x "\n", from, at);
for (i = 0; i < 10; i++) {
(void) anal->iob.read_at (anal->iob.io, at, buf, sizeof (buf));
// TODO check for return value
int sz = r_anal_case (anal, fcn, from, at, buf, sizeof (buf), 0);
if (sz < 1) {
break;
}
at += sz;
}
return 0;
}
#endif
static int fcn_recurse(RAnal *anal, RAnalFunction *fcn, ut64 addr, ut8 *buf, ut64 len, int depth) {
const int continue_after_jump = anal->opt.afterjmp;
const int noncode = anal->opt.noncode;
const int addrbytes = anal->iob.io ? anal->iob.io->addrbytes : 1;
RAnalBlock *bb = NULL;
RAnalBlock *bbg = NULL;
int ret = R_ANAL_RET_END, skip_ret = 0;
int overlapped = 0;
RAnalOp op = {
0
};
int oplen, idx = 0;
struct {
int cnt;
int idx;
int after;
int pending;
int adjust;
int un_idx; // delay.un_idx
} delay = {
0
};
char tmp_buf[MAX_FLG_NAME_SIZE + 5] = "skip";
if (r_cons_is_breaked ()) {
return R_ANAL_RET_END;
}
if (anal->sleep) {
r_sys_usleep (anal->sleep);
}
if (depth < 1) {
return R_ANAL_RET_ERROR; // MUST BE TOO DEEP
}
if (!noncode && anal->cur && anal->cur->is_valid_offset && !anal->cur->is_valid_offset (anal, addr, 0)) {
return R_ANAL_RET_END;
}
// check if address is readable //:
if (!anal->iob.is_valid_offset (anal->iob.io, addr, 0)) {
if (addr != UT64_MAX && !anal->iob.io->va) {
eprintf ("Invalid address 0x%"PFMT64x ". Try with io.va=true\n", addr);
}
return R_ANAL_RET_ERROR; // MUST BE TOO DEEP
}
if (r_anal_get_fcn_at (anal, addr, 0)) {
return R_ANAL_RET_ERROR; // MUST BE NOT FOUND
}
bb = bbget (fcn, addr);
if (bb) {
r_anal_fcn_split_bb (anal, fcn, bb, addr);
if (anal->opt.recont) {
return R_ANAL_RET_END;
}
return R_ANAL_RET_ERROR; // MUST BE NOT DUP
}
bb = appendBasicBlock (anal, fcn, addr);
VERBOSE_ANAL eprintf ("Append bb at 0x%08"PFMT64x" (fcn 0x%08"PFMT64x ")\n", addr, fcn->addr);
bool last_is_push = false;
ut64 last_push_addr = UT64_MAX;
if (anal->limit && addr + idx < anal->limit->from) {
return R_ANAL_RET_END;
}
ut64 movptr = UT64_MAX; // used by jmptbl when coded as "mov reg,[R*4+B]"
while (addrbytes * idx < len) {
if (anal->limit && anal->limit->to <= addr + idx) {
break;
}
repeat:
if (r_cons_is_breaked ()) {
break;