forked from vext01/dismantle
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdm_cfg.c
More file actions
1435 lines (1271 loc) · 40.3 KB
/
dm_cfg.c
File metadata and controls
1435 lines (1271 loc) · 40.3 KB
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
/*
* Copyright (c) 2011, Ed Robbins <edd.robbins@gmail.com>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#define _GNU_SOURCE
#include <stdio.h>
#include "dm_cfg.h"
#include "dm_gviz.h"
#include "dm_dwarf.h"
#include "dm_util.h"
#include "dm_ssa.h"
#include <assert.h>
extern char *fname;
char *sym_name = NULL;
extern int flatten;
extern int transform;
struct indirect_branch *iBranches = NULL;
int iBranchesCount = 0;
struct branch *branches = NULL;
int branchesCount = 0;
/* Head of the list and a list iterator */
struct ptrs *p_head = NULL;
struct ptrs *p = NULL;
struct ptrs *p_iter = NULL;
/* Explicitly record the list length */
int p_length = 0;
struct dm_instruction_se *instructions = NULL;
void **rpost; /* Pointers to nodes in reverse post-order */
int fcalls_i = 0;
int verbosity = 1;
/*
* Generate static CFG for a function.
* Continues until it reaches ret, does not follow calls.
*/
int
dm_cmd_cfg(char **args) {
struct dm_cfg_node *cfg = NULL;
struct dm_dwarf_sym_cache_entry *ent = NULL;
(void) args;
dm_dwarf_find_nearest_sym_to_offset(cur_addr, &ent);
xasprintf(&sym_name, "%s", ent->name);
/* Initialise structures */
dm_init_cfg();
/* Get CFG */
cfg = dm_recover_cfg();
/* Graph CFG */
dm_graph_cfg();
/* Check CFG for consistency! */
dm_check_cfg_consistency();
/* Print CFG */
dm_print_cfg(cfg);
dm_print_post_messages();
/* Free all memory */
dm_free_cfg();
dm_free_post_messages();
dm_free_jumps();
return (0);
}
/*
* This function actually starts the building process
* Returns completed CFG
*/
struct dm_cfg_node*
dm_recover_cfg() {
NADDR addr = cur_addr;
struct dm_cfg_node *cfg = NULL;
/* Create first node */
cfg = dm_new_cfg_node(addr, 0);
cfg->function_return = xmalloc(sizeof(void*));
(*cfg->function_return) = NULL;
/* Create CFG */
dm_gen_cfg_block(cfg, cfg, cfg->function_return);
/* Get reverse postorder, preorder and postorder of nodes */
rpost = xcalloc(p_length, sizeof(void*));
dm_depth_first_walk(cfg);
/* Rewind back */
dm_seek(addr);
return cfg;
}
void
dm_check_cfg_consistency()
{
struct dm_cfg_node *node = NULL;
int i = 0, j = 0, consistent = 0;
for (p = p_head; p != NULL; p = p->next) {
node = (struct dm_cfg_node*)p->ptr;
if (!node->function_head)
printf("Node %d has no function head!\n", node->post);
if (!node->function_return)
printf("Node %d has no function return!\n", node->post);
//for (i = 0; node->children[i] != NULL; i++) {
for (i = 0; i < node->c_count; i++) {
consistent = 0;
for (j = 0; j < node->children[i]->p_count; j++) {
if (node->children[i]->parents[j] == node)
consistent = 1;
}
if (!consistent)
printf("No link from node %d (start addr "NADDR_FMT ") to parent %d!\n",
node->children[i]->post, node->children[i]->start, node->post);
}
for (i = 0; i < node->p_count; i++) {
consistent = 0;
for (j = 0; node->parents[i]->children[j] != NULL; j++) {
if (node->parents[i]->children[j] == node)
consistent = 1;
}
if (!consistent)
printf("No link from node %d (start addr "NADDR_FMT") to child %d (start addr "NADDR_FMT")!\n",
node->parents[i]->post, node->parents[i]->start, node->post, node->start);
}
}
}
/*
* Initialise structures used for CFG recovery
*/
void
dm_init_cfg()
{
struct dm_setting *fcalls = NULL;
char *fcallmessage;
struct dm_setting *set_transform = NULL, *set_flatten = NULL, *cfg_verbosity = NULL;
/* Get flatten/transform settings */
dm_find_setting("ssa.transform", &set_transform);
dm_find_setting("ssa.flatten", &set_flatten);
flatten = set_flatten->val.ival;
transform = set_transform->val.ival;
/* Get verbosity setting */
dm_find_setting("cfg.verbose", &cfg_verbosity);
verbosity = cfg_verbosity->val.ival;
/* Get fcalls setting */
dm_find_setting("cfg.fcalls", &fcalls);
fcalls_i = fcalls->val.ival;
switch (fcalls_i) {
case 4:
xasprintf(&fcallmessage, "(fcalls = 4) complete cfg/cg reconstruction, flow trigger followed all reachable branches");
break;
case 3:
xasprintf(&fcallmessage, "(fcalls = 3) cfg/cg reconstruction, flow trigger followed all local (.text) branches");
break;
case 2:
xasprintf(&fcallmessage, "(fcalls = 2) function cfg reconstruction, flow trigger followed all local jumps");
break;
case 1:
xasprintf(&fcallmessage, "(fcalls = 1) single block reconstruction, flow trigger stopped at first branch");
break;
case 0:
xasprintf(&fcallmessage, "(fcalls = 0) function block recovery, flow trigger ignored all branches and halted at ret");
break;
}
dm_new_post_message(fcallmessage);
dm_instruction_se_init();
}
void
dm_assign(int dest[4], int src[4])
{
memcpy(dest, src, 4 * sizeof(int));
}
/*
* We create an array of structures indicating semantics of each instruction
*/
/* nasty hack, overapproximates size of ud enum in itab.h, fix XXX */
#define DM_UD_ENUM_HACK 600
void
dm_instruction_se_init()
{
/* XXX store in linked list (queue.h) */
int c;
instructions = xmalloc(sizeof(struct dm_instruction_se) * (DM_UD_ENUM_HACK));
/* Initialise struct recording which instructions write to registers */
for (c = 0; c < DM_UD_ENUM_HACK; c++) {
instructions[c].instruction = c;
dm_assign(instructions[c].write, (int[]){ 1, 0, 0, 0 });
instructions[c].jump = 0;
instructions[c].ret = 0;
instructions[c].disjunctive = 0;
instructions[c].shift = 0;
instructions[c]._signed = 1;
instructions[c]._unsigned = 1;
instructions[c]._float = 1; /* 1 indicates float, 2 indicates packed float */
instructions[c]._double = 1; /* 1 indicates double, 2 indicates packed double */
instructions[c]._string = 0;
instructions[c].unknown = 1;
}
instructions[UD_Inop].unknown = 0;
dm_assign(instructions[UD_Inop].write, (int[]) { 0, 0, 0, 0 });
instructions[UD_Ipop].unknown = 0;
dm_assign(instructions[UD_Ipop].write, (int[]) { 1, 0, 0, 0 });
dm_assign(instructions[UD_Ipush].write, (int[]) { 0, 0, 0, 0 });
instructions[UD_Ipush].unknown = 0;
dm_assign(instructions[UD_Itest].write, (int[]){ 0, 0, 0, 0 });
instructions[UD_Itest].unknown = 0;
instructions[UD_Itest]._class = 0;
dm_assign(instructions[UD_Icmp].write, (int[]) { 0, 0, 0, 0 });
instructions[UD_Icmp].unknown = 0;
instructions[UD_Icmp]._class = 0;
/* Control flow instructions */
dm_assign(instructions[UD_Icall].write, (int[]) { 0, 1, 0, 0 });
instructions[UD_Icall].unknown = 0;
if (fcalls_i > 2)
instructions[UD_Icall].jump = 2;
dm_assign(instructions[UD_Iret].write, (int[]) { 0, 0, 0, 0 });
instructions[UD_Iret].ret = 1;
instructions[UD_Iret].unknown = 0;
dm_assign(instructions[UD_Iretf].write, (int[]) { 0, 0, 0, 0 });
instructions[UD_Iretf].ret = 1;
instructions[UD_Iretf].unknown = 0;
dm_assign(instructions[UD_Ijmp].write, (int[]) { 0, 0, 0, 0 });
instructions[UD_Ijmp].jump = 1;
instructions[UD_Ijmp].unknown = 0;
dm_assign(instructions[UD_Ijz].write, (int[]) { 0, 0, 0, 0 });
instructions[UD_Ijz].jump = 2;
instructions[UD_Ijz].unknown = 0;
dm_assign(instructions[UD_Ijnz].write, (int[]) { 0, 0, 0, 0 });
instructions[UD_Ijnz].jump = 2;
instructions[UD_Ijnz].unknown = 0;
dm_assign(instructions[UD_Ijg].write, (int[]) { 0, 0, 0, 0 });
instructions[UD_Ijg].jump = 2;
instructions[UD_Ijg].unknown = 0;
dm_assign(instructions[UD_Ijae].write, (int[]) { 0, 0, 0, 0 });
instructions[UD_Ijae].jump = 2;
instructions[UD_Ijae].unknown = 0;
dm_assign(instructions[UD_Ijle].write, (int[]) { 0, 0, 0, 0 });
instructions[UD_Ijle].jump = 2;
instructions[UD_Ijle].unknown = 0;
dm_assign(instructions[UD_Ijl].write, (int[]) { 0, 0, 0, 0 });
instructions[UD_Ijl].jump = 2;
instructions[UD_Ijl].unknown = 0;
dm_assign(instructions[UD_Ija].write, (int[]) { 0, 0, 0, 0 });
instructions[UD_Ija].jump = 2;
instructions[UD_Ija].unknown = 0;
dm_assign(instructions[UD_Ijb].write, (int[]) { 0, 0, 0, 0 });
instructions[UD_Ijb].jump = 2;
instructions[UD_Ijb].unknown = 0;
dm_assign(instructions[UD_Ijbe].write, (int[]) { 0, 0, 0, 0 });
instructions[UD_Ijbe].jump = 2;
instructions[UD_Ijbe].unknown = 0;
dm_assign(instructions[UD_Ijcxz].write, (int[]) { 0, 0, 0, 0 });
instructions[UD_Ijcxz].jump = 2;
instructions[UD_Ijcxz].unknown = 0;
dm_assign(instructions[UD_Ijnp].write, (int[]) { 0, 0, 0, 0 });
instructions[UD_Ijnp].jump = 2;
instructions[UD_Ijnp].unknown = 0;
dm_assign(instructions[UD_Ijge].write, (int[]) { 0, 0, 0, 0 });
instructions[UD_Ijge].jump = 2;
instructions[UD_Ijge].unknown = 0;
dm_assign(instructions[UD_Ijs].write, (int[]) { 0, 0, 0, 0 });
instructions[UD_Ijs].jump = 2;
instructions[UD_Ijs].unknown = 0;
dm_assign(instructions[UD_Ijns].write, (int[]) { 0, 0, 0, 0 });
instructions[UD_Ijns].jump = 2;
instructions[UD_Ijns].unknown = 0;
instructions[UD_Ileave].unknown = 0;
instructions[UD_Ipop].unknown = 0;
instructions[UD_Ipush].unknown = 0;
/* Standard arithemetic - add, sub, and etc */
if (transform) {
dm_assign(instructions[UD_Iidiv].write, (int[]) { 1, 1, 0, 0 });
dm_assign(instructions[UD_Idiv].write, (int[]) { 1, 1, 0, 0 });
}
else {
dm_assign(instructions[UD_Iidiv].write, (int[]) { 0, 0, 0, 0 });
dm_assign(instructions[UD_Idiv].write, (int[]) { 0, 0, 0, 0 });
}
instructions[UD_Iadd].disjunctive = 1;
instructions[UD_Iadd].unknown = 0;
instructions[UD_Iadd]._class = 2;
instructions[UD_Iadd]._float = 0;
instructions[UD_Iadd]._double = 0;
instructions[UD_Iadc].disjunctive = 1;
instructions[UD_Iadc].unknown = 0;
instructions[UD_Iadc]._class = 2;
instructions[UD_Iadc]._float = 0;
instructions[UD_Iadc]._double = 0;
instructions[UD_Isub].disjunctive = 1;
instructions[UD_Isub].unknown = 0;
instructions[UD_Isub]._class = 2;
instructions[UD_Isub]._float = 0;
instructions[UD_Isub]._double = 0;
instructions[UD_Iand].disjunctive = 1;
instructions[UD_Iand].unknown = 0;
instructions[UD_Iand]._class = 2;
instructions[UD_Iand]._float = 0;
instructions[UD_Iand]._double = 0;
/* Shifts, xor, or */
instructions[UD_Isar].unknown = 0;
instructions[UD_Isar]._signed = 1;
instructions[UD_Isar].shift = 1;
instructions[UD_Isalc].unknown = 0;
instructions[UD_Isalc].shift = 1;
instructions[UD_Ishl].unknown = 0;
instructions[UD_Ishl].shift = 1;
instructions[UD_Ishr].unknown = 0;
instructions[UD_Ishr].shift = 1;
instructions[UD_Ishr]._unsigned = 1;
instructions[UD_Ixor].unknown = 0;
instructions[UD_Ixor]._class = 4;
instructions[UD_Ixor]._float = 0;
instructions[UD_Ixor]._double = 0;
instructions[UD_Ior].unknown = 0;
instructions[UD_Ior]._class = 4;
instructions[UD_Ior]._float = 0;
instructions[UD_Ior]._double = 0;
instructions[UD_Inot].unknown = 0;
instructions[UD_Inot]._class = 4;
instructions[UD_Inot]._float = 0;
instructions[UD_Inot]._double = 0;
instructions[UD_Inot]._signed = 0;
/* LEA */
/*instructions[UD_Imov].unknown = 0;
instructions[UD_Imov]._class = 5;*/
/* Mov etc */
instructions[UD_Imov].unknown = 0;
instructions[UD_Imov]._class = 1;
instructions[UD_Imovsx]._unsigned = 0;
instructions[UD_Imovsx]._float = 0;
instructions[UD_Imovsx]._double = 0;
instructions[UD_Imovsx].unknown = 0;
instructions[UD_Imovsx]._class = 1;
instructions[UD_Imovsxd]._unsigned = 0;
instructions[UD_Imovsxd]._float = 0;
instructions[UD_Imovsxd]._double = 0;
instructions[UD_Imovsxd].unknown = 0;
instructions[UD_Imovsxd]._class = 1;
/* SSE arithmetic */
instructions[UD_Iaddpd]._unsigned = 0;
instructions[UD_Iaddpd]._signed = 0;
instructions[UD_Iaddpd]._float = 0;
instructions[UD_Iaddpd].unknown = 0;
instructions[UD_Iaddpd]._class = 3;
instructions[UD_Iaddps]._unsigned = 0;
instructions[UD_Iaddps]._signed = 0;
instructions[UD_Iaddps]._double = 0;
instructions[UD_Iaddps].unknown = 0;
instructions[UD_Iaddps]._class = 3;
instructions[UD_Iaddsd]._unsigned = 0;
instructions[UD_Iaddsd]._signed = 0;
instructions[UD_Iaddsd]._float = 0;
instructions[UD_Iaddsd].unknown = 0;
instructions[UD_Iaddsd]._class = 3;
instructions[UD_Iaddss]._unsigned = 0;
instructions[UD_Iaddss]._signed = 0;
instructions[UD_Iaddss]._double = 0;
instructions[UD_Iaddss].unknown = 0;
instructions[UD_Iaddss]._class = 3;
instructions[UD_Iaddsubpd]._unsigned = 0;
instructions[UD_Iaddsubpd]._signed = 0;
instructions[UD_Iaddsubpd]._float = 0;
instructions[UD_Iaddsubpd].unknown = 0;
instructions[UD_Iaddsubpd]._class = 3;
instructions[UD_Iaddsubps]._unsigned = 0;
instructions[UD_Iaddsubps]._signed = 0;
instructions[UD_Iaddsubps]._double = 0;
instructions[UD_Iaddsubps].unknown = 0;
instructions[UD_Iaddsubps]._class = 3;
instructions[UD_Iandpd]._unsigned = 0;
instructions[UD_Iandpd]._signed = 0;
instructions[UD_Iandpd]._float = 0;
instructions[UD_Iandpd].unknown = 0;
instructions[UD_Iandpd]._class = 3;
instructions[UD_Iandps]._unsigned = 0;
instructions[UD_Iandps]._signed = 0;
instructions[UD_Iandps]._double = 0;
instructions[UD_Iandps].unknown = 0;
instructions[UD_Iandps]._class = 3;
instructions[UD_Iandnpd]._unsigned = 0;
instructions[UD_Iandnpd]._signed = 0;
instructions[UD_Iandnpd]._float = 0;
instructions[UD_Iandnpd].unknown = 0;
instructions[UD_Iandnpd]._class = 3;
instructions[UD_Iandnps]._unsigned = 0;
instructions[UD_Iandnps]._signed = 0;
instructions[UD_Iandnps]._double = 0;
instructions[UD_Iandnps].unknown = 0;
instructions[UD_Iandnps]._class = 3;
}
/*
* Create a new node in the CFG
*/
struct dm_cfg_node *
dm_new_cfg_node(NADDR nstart, NADDR nend)
{
struct dm_cfg_node *node;
node = xmalloc(sizeof(struct dm_cfg_node));
node->start = nstart;
node->end = nend;
node->children = NULL;
node->c_count = 0;
node->parents = NULL;
node->p_count = 0;
node->nonlocal = 0;
node->visited = 0;
node->pre = 0;
node->rpost = 0;
node->idom = NULL;
node->df_set = NULL;
node->df_count = 0;
node->def_vars = NULL;
node->dv_count = 0;
node->phi_functions = NULL;
node->pf_count = 0;
node->instructions = NULL;
node->i_count = 0;
node->function_head = NULL;
node->function_return = NULL;
node->is_function_head = 0;
node->is_function_return = 0;
node->return_nodes = NULL;
node->rn_count = 0;
node->function_nodes = NULL;
node->fn_count = 0;
node->superphi = NULL;
/* Add node to the free list so we can free the memory at the end */
if (p) {
p->next = xcalloc(1, sizeof(struct ptrs));
p = p->next;
p->ptr = (void*)node;
}
else {
p = xcalloc(1, sizeof(struct ptrs));
p->ptr = (void*)node;
p_head = p;
}
p_length++;
return (node);
}
void
dm_add_parent(struct dm_cfg_node *node, struct dm_cfg_node *parent)
{
node->parents = xrealloc(node->parents, ++(node->p_count) * sizeof(void*));
node->parents[node->p_count - 1] = parent;
}
void
dm_add_child(struct dm_cfg_node *node, struct dm_cfg_node *child)
{
node->children = xrealloc(node->children, ++(node->c_count) * sizeof(void*));
node->children[node->c_count - 1] = child;
}
void
dm_add_return_node(struct dm_cfg_node *node, struct dm_cfg_node *return_node)
{
struct dm_cfg_node *function_head = NULL;
if (node->is_function_head)
function_head = node;
else
function_head = node->function_head;
if (function_head == NULL) {
printf("Tried to add recursive caller with NULL function head!\n");
return;
}
function_head->return_nodes = xrealloc(function_head->return_nodes, ++(function_head->rn_count) * sizeof(void*));
function_head->return_nodes[function_head->rn_count - 1] = return_node;
}
void
dm_add_node_to_function(struct dm_cfg_node *func, struct dm_cfg_node *node)
{
struct dm_cfg_node *function_head = NULL;
if (func->is_function_head)
function_head = func;
else
function_head = func->function_head;
function_head->function_nodes = xrealloc(function_head->function_nodes, ++(function_head->fn_count) * sizeof(void*));
function_head->function_nodes[function_head->fn_count - 1] = node;
}
static char **post_messages = NULL;
static int npm = 0;
void
dm_print_post_messages()
{
int i = 0;
printf("Post messages:\n");
for (; i < npm; i++) {
printf("\t%s", post_messages[i]);
}
}
void
dm_new_post_message(char *message)
{
npm++;
post_messages = xrealloc(post_messages, npm * sizeof(void*));
xasprintf(&post_messages[npm - 1], "%s\n", message);
}
void
dm_free_post_messages()
{
int i = 0;
for (; i < npm; i++) {
free(post_messages[i]);
}
free(post_messages);
post_messages = NULL;
npm = 0;
}
void
dm_free_jumps()
{
free(iBranches);
iBranches = NULL;
iBranchesCount = 0;
free(branches);
branches = NULL;
branchesCount = 0;
}
/*
* Main part of CFG recovery. Recursively find blocks.
*/
struct dm_cfg_node *
dm_gen_cfg_block(struct dm_cfg_node *node, struct dm_cfg_node *function_head, struct dm_cfg_node **function_return)
{
NADDR addr = node->start;
unsigned int read = 0, oldRead = 0;
struct dm_cfg_node *foundNode = NULL, *child = NULL;
NADDR target = 0;
int i = 0, duplicate = 0, local_target = 1, call = 0;
char pm[300];
node->function_head = function_head;
node->function_return = function_return;
if (function_head == node)
node->is_function_head = 1;
if (*function_return == node)
node->is_function_return = 1;
dm_seek(node->start);
while (1) {
oldRead = read;
read = ud_disassemble(&ud);
/* Check we haven't run into the start of another block */
if ((foundNode = dm_find_cfg_node_starting(addr))
&& (foundNode != node)) {
//printf("Ran into the start of existing block at address " NADDR_FMT "!\n", addr);
addr -= oldRead;
/*free(node->children);
node->children = xcalloc(2, sizeof(void*));
node->children[0] = foundNode;
node->c_count = 1;*/
dm_add_child(node, foundNode);
dm_add_parent(foundNode, node);
break;
}
/*
* Check for jump instructions and create
* new nodes as necessary
*
* Make sure the target is inside the .text
* section */
local_target = 1;
if (instructions[ud.mnemonic].jump) {
target = dm_get_jump_target(ud);
if (ud.operand[0].index) {
if ((ud.operand[0].base) && (ud.operand[0].scale) && (ud.operand[0].offset))
sprintf(pm, "Branch to indirect address [%s+%s*%d+" NADDR_FMT "] at " NADDR_FMT, ud_reg_tab[ud.operand[0].base-UD_R_AL],
ud_reg_tab[ud.operand[0].index-UD_R_AL], ud.operand[0].scale, dm_get_operand_lval(ud, 0, 1, 0), addr);
else if ((ud.operand[0].scale) && (ud.operand[0].offset))
sprintf(pm, "Branch to indirect address [%s*%d+" NADDR_FMT "] at " NADDR_FMT, ud_reg_tab[ud.operand[0].index-UD_R_AL],
ud.operand[0].scale, dm_get_operand_lval(ud, 0, 1, 0), addr);
else if (ud.operand[0].index)
sprintf(pm, "Branch to indirect address [%s] at " NADDR_FMT, ud_reg_tab[ud.operand[0].index-UD_R_AL], addr);
else
sprintf(pm, "Branch to address outside of .text (" NADDR_FMT ") at " NADDR_FMT, target, addr);
dm_new_post_message(pm);
iBranchesCount++;
iBranches = xrealloc(iBranches, iBranchesCount * sizeof(struct indirect_branch));
iBranches[iBranchesCount - 1].address = addr;
iBranches[iBranchesCount - 1].insn = NULL;
iBranches[iBranchesCount - 1].node = node;
local_target = 0;
}
if (!dm_is_target_in_text(target))
local_target = 0;
if (!ud.operand[0].index) {
branchesCount++;
branches = xrealloc(branches, branchesCount * sizeof(struct branch));
branches[branchesCount - 1].addr = addr;
branches[branchesCount - 1].target = target;
branches[branchesCount - 1].insn = NULL;
branches[branchesCount - 1].node = node;
}
}
/* fcalls_i controls CFG reconstruction behaviour:
0 = ignore all branch instructions
1 = end block at first branch instruction and finish there
2 = follow all local jump instructions (but not calls)
3 = follow all local branch instructions (of any type)
4 = attempt to follow all branch instructions (local or otherwise) (except indirect branches)
*/
if ((fcalls_i == 1) && instructions[ud.mnemonic].jump)
break;
if (instructions[ud.mnemonic].jump &&
/*((local_target && fcalls_i && !((fcalls_i == 1) && (ud.mnemonic == UD_Icall)))
|| ((!local_target) && (fcalls_i == 3)) || (local_target)) */
((local_target && (fcalls_i > 0) && (ud.mnemonic != UD_Icall)) ||
(local_target && (fcalls_i > 2)) ||
((!local_target) && (fcalls_i >3)))
&& !ud.operand[0].index) {
/* Get the target of the jump instruction */
target = dm_get_jump_target(ud);
/* End the block here */
node->end = addr;
//free(node->children);
/* Make space for the children of this block */
//node->children = xcalloc(instructions[ud.mnemonic].jump
// + 1, sizeof(void*));
//node->c_count = instructions[ud.mnemonic].jump;
/* Record if this is a call rather than jump */
if (ud.mnemonic == UD_Icall) call = 1;
/* Check if we are jumping to the start of an already
* existing block, if so use that as child of current
* block */
if (((foundNode = dm_find_cfg_node_starting(target))
!= NULL) && local_target) {
//node->children[0] = foundNode;
dm_add_child(node, foundNode);
dm_add_parent(foundNode, node);
}
/* Check if we are jumping to the *middle* of an
* existing block, if so split it and use 2nd half as
* child of current block */
else if (((foundNode = dm_find_cfg_node_containing(target)) != NULL) && local_target) {
/* We found a matching block. Now find address
* before addr and split the block */
//node->children[0] = dm_split_cfg_block(foundNode, target);
child = dm_split_cfg_block(foundNode, target);
dm_add_child(node, child);
dm_add_node_to_function(node->function_head, child);
duplicate = 0;
/* Child may already have node as parent */
for (i = 0; i < child->p_count; i++)
if (child->parents[i] == node)
duplicate = 1;
/* Node is recursive, make it it's own parent */
//if (duplicate)
// dm_add_parent(node->children[0],
// node->children[0]);
//else
if (!duplicate)
dm_add_parent(child, node);
}
/* This is a new block, so scan with a recursive call
* to find it's start, end, and children, assuming it's
* a local block (inside the binary) */
else if (local_target) {
child = dm_new_cfg_node(target, 0);
dm_add_child(node, child);
//node->children[0] = foundNode;
dm_add_parent(child, node);
if (call) {
child->function_return = xmalloc(sizeof(void*));
(*child->function_return) = NULL;
dm_gen_cfg_block(child, child, child->function_return);
}
else {
dm_add_node_to_function(node->function_head, child);
//child->function_return = function_return;
dm_gen_cfg_block(child, node->function_head, function_return);
}
}
/* This target is outside of the binary. Just make a
* basic block for it and continue with a new
* block from the next insn */
if (!local_target) {
if ((foundNode = dm_find_cfg_node_starting(target)) != NULL) {
dm_add_parent(foundNode, node);
dm_add_child(node, foundNode);
//node->children[0] = foundNode;
}
else {
/* New block starts and ends at target addr */
foundNode = dm_new_cfg_node(target, target);
//node->children[0] = child;
dm_add_child(node, foundNode);
dm_add_parent(foundNode, node);
foundNode->nonlocal = 1;
foundNode->function_head = foundNode;
foundNode->is_function_head = 1;
foundNode->is_function_return = 1;
foundNode->function_return = xmalloc(sizeof(void*));
*(foundNode->function_return) = foundNode;
}
/* New node has child starting at next insn */
dm_seek(addr);
read = ud_disassemble(&ud);
child = dm_new_cfg_node(ud.pc, 0);
dm_add_child(foundNode, child);
dm_add_parent(child, foundNode);
dm_add_node_to_function(node->function_head, child);
//child->function_head = function_head;
//child->function_return = function_return;
dm_gen_cfg_block(child, node->function_head, function_return);
//foundNode->children =
// xrealloc(foundNode->children, (1 + ++(foundNode->c_count))*sizeof(void*));
//foundNode->children[foundNode->c_count-1] = dm_new_cfg_node(ud.pc, 0);
//foundNode->children[foundNode->c_count] = NULL;
//dm_add_parent(foundNode->children[foundNode->c_count-1], foundNode);
//dm_gen_cfg_block(foundNode->children[foundNode->c_count-1], node->function_head, function_return);
}
else {
/* Seek back to before we followed the jump */
dm_seek(addr);
read = ud_disassemble(&ud);
}
/* Check whether there was some sneaky splitting of the
* block we're working on while we were away! */
if (node->end < addr) {
/* Now we must find the right block to continue
* from */
foundNode = dm_find_cfg_node_ending(addr);
if (foundNode != NULL) {
/*if (instructions[ud.mnemonic].jump > 1) {
node->children = xrealloc(node->children, 2*sizeof(void*));
node->children[1] = NULL;
node->c_count = 1;
}*/
node = foundNode;
}
}
/*
* If the jump was a conditional, now we must
* follow the other leg of the jump
*/
if (call && local_target) {
/* Non-recursive case */
if (node->children[0]->function_head != function_head) {
if ((foundNode = dm_find_cfg_node_starting(ud.pc)) != NULL) {
/* Already found the function return of called function */
if (node->children[0]->function_return != NULL) {
dm_add_child(*(node->children[0]->function_return), foundNode);
dm_add_parent(foundNode, *(node->children[0]->function_return));
call = 0;
break;
}
else {
dm_add_return_node(node->children[0], foundNode);
call = 0;
break;
}
}
else {
if (node->children[0]->function_return != NULL) {
child = dm_new_cfg_node(ud.pc, 0);
//node->children[1] = foundNode;
dm_add_child(*(node->children[0]->function_return), child);
dm_add_parent(child, *(node->children[0]->function_return));
dm_add_node_to_function(node->function_head, child);
child->function_head = node->function_head;
child->function_return = function_return;
node = child;
call = 0;
}
else {
child = dm_new_cfg_node(ud.pc, 0);
dm_add_return_node(node->children[0], child);
dm_add_node_to_function(node->function_head, child);
child->function_head = node->function_head;
child->function_return = function_return;
node = child;
call = 0;
}
}
}
/* Recursive case */
else if (node->children[0]->function_head == function_head) {
if ((foundNode = dm_find_cfg_node_starting(ud.pc)) != NULL) {
if (*function_return != NULL) {
dm_add_child(*function_return, foundNode);
dm_add_parent(foundNode, *function_return);
call = 0;
break;
}
else {
dm_add_return_node(function_head, foundNode);
call = 0;
break;
}
}
else {
if (*function_return != NULL) {
child = dm_new_cfg_node(ud.pc, 0);
dm_add_child(*function_return, child);
dm_add_parent(child, *function_return);
dm_add_node_to_function(node->function_head, child);
child->function_head = node->function_head;
child->function_return = function_return;
node = child;
call = 0;
}
else {
child = dm_new_cfg_node(ud.pc, 0);
dm_add_return_node(node->function_head, child);
dm_add_node_to_function(node->function_head, child);
child->function_head = node->function_head;
child->function_return = function_return;
node = child;
call = 0;
}
}
}
}
/* Conditional jump */
else if (instructions[ud.mnemonic].jump > 1) {
if ((foundNode = dm_find_cfg_node_starting(ud.pc)) != NULL) {
dm_add_parent(foundNode, node);
dm_add_child(node, foundNode);
break;
}
else {
child = dm_new_cfg_node(ud.pc, 0);
//node->children[1] = foundNode;
dm_add_child(node, child);
dm_add_parent(child, node);
dm_add_node_to_function(node->function_head, child);
child->function_head = node->function_head;
child->function_return = function_return;
node = child;
}
}
else
break;
}
/* If we find a return end the block/node */
if (instructions[ud.mnemonic].ret) {
node->end = ud.pc;
node->is_function_return = 1;
*function_return = node;
for (i = 0; i < node->function_head->rn_count; i++) {
dm_add_child(node, node->function_head->return_nodes[i]);
dm_add_parent(node->function_head->return_nodes[i], node);
}
free(node->function_head->return_nodes);
node->function_head->return_nodes = NULL;
node->function_head->rn_count = 0;
break;
}
addr += read;
}
node->end = addr;
return node;
}
void
dm_print_node_info(struct dm_cfg_node *node)
{
int i = 0;
printf("Node %d. \n\tStart: " NADDR_FMT " \n\tEnd: " NADDR_FMT ".\n\tHas children? ", node->post, node->start, node->end);
if (node->c_count != 0) {
printf("Yes, nodes ");
for (; i < node->c_count; i++) {
if (i != node->c_count - 1)
printf("%d, ", node->children[i]->post);
else
printf("%d\n", node->children[i]->post);
}
}
else
printf("No\n");
printf("Has parents? ");
if (node->p_count != 0) {
printf("Yes, nodes ");
for (i = 0; i < node->p_count; i++) {
if (i != node->p_count - 1)
printf("%d, ", node->parents[i]->post);
else
printf("%d\n", node->parents[i]->post);
}
}
else
printf("No\n");
}
int
dm_is_target_in_text(NADDR addr)
{
NADDR start = 0, size = 0;
GElf_Shdr shdr;
if ((dm_find_section(".text", &shdr)) == DM_FAIL) {