-
Notifications
You must be signed in to change notification settings - Fork 0
/
mmo.pl
executable file
·1792 lines (1562 loc) · 44 KB
/
mmo.pl
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
#!/usr/bin/perl -w
use warnings;
use strict;
use autodie;
use threads;
use threads::shared;
use Tie::File;
use Getopt::Std;
use Term::ANSIColor;
use Term::ANSIColor qw(:constants);
use File::Basename;
use File::Spec::Functions;
use File::Glob;
use Cwd;
##################### GLOBALS ##########################
my $BASE = "~/dev/mmo/";
my $RECURSION_DEPTH_LIMIT = 6; #was once 6
my $RECURSION_DEF_DEPTH_LIMIT = 6;
my $MAX_STACK_SIZE = 512;
my $LOGS_DIR = '/tmp/logs';
my $STRUCT_CACHE = "$BASE/struct_cache";
my $KERNEL_DIR = "$BASE/linux";
my $VMLINUX = "$KERNEL_DIR/vmlinux";
my @ROOT_FUNCS = qw( dma_map_single pci_map_single );
my $verbose = undef;
my $TRY_CONFIG = undef;
my $FH;
my $TID;
my @type_C_funcs = qw(page_frag_alloc
netdev_alloc_frag napi_alloc_frag
__netdev_alloc_frag __napi_alloc_frag
netdev_alloc_skb napi_alloc_skb
__netdev_alloc_skb __napi_alloc_skb
napi_get_frags
); #TODO: recurse and collect callers untill all are exported.
my %cscope_lines : shared = ();
my @cscope_lines : shared = ();
my %exported_symbols : shared = ();
my %assignment_funcs : shared = ();
my %alloc_funcs : shared = ();
my %global_struct_cache : shared = ();
my $CURR_FILE = undef; #per thread variable
my $CURR_FUNC = undef; #per thread variable
my $CURR_DEPTH = 0;
my $CURR_DEF_DEPTH = 0;
my $MAP_DIRECTION = undef;
my $CURR_DEPTH_MAX : shared = 0;
my $CURR_DEF_DEPTH_MAX : shared = 0;
my @REC_HEAP = ();
my $CALLEE = undef; #per thread variable
my $CURR_STACK = undef;
my %struct_log = ();
my %struct_cache = ();
my %local_assignment_cache = ();
my %local_struct_cache = ();
my %local_stats = ();
my %cached_struct_cb_count = ();
my %cached_direct_cb_count = ();
my $DBG_CACHE = "DBG_CACHE::";
####################### INIT #####################
my @BASE_TYPES = qw(void char long int u8 u16 u32 u64 __be64
__u8 uint8_t uint16_t __le16 __le32
__le64 assoc_array_ptr uchar u_char);
my %opts = ();
my $argv = "@ARGV";
getopts('vk:c', \%opts);
$KERNEL_DIR = $opts{'k'} if defined $opts{'k'};
$VMLINUX = "$KERNEL_DIR/vmlinux" if defined $opts{'k'};
$verbose = 1 if defined $opts{'v'};
$TRY_CONFIG = 1 if defined $opts{'c'};
use constant {
NEW => 0,
IN_PROGRESS => 1,
DONE => 2,
};
#usage() unless (defined($opts{'f'}) and $opts{'l'} =~ /^\d+$/);
##################### LEMA ########################
sub usage {
die "bad command: $0 $argv\nusage: $0\n";
}
sub fixup_globals {
$BASE =~ s/~/$ENV{'HOME'}/;
$STRUCT_CACHE = "$BASE/struct_cache";
$KERNEL_DIR = "$BASE/linux";
$VMLINUX = "$KERNEL_DIR/vmlinux";
}
sub new_verbose {
$FH = *STDOUT unless defined $FH;
print $FH UNDERLINE, BOLD, BRIGHT_WHITE, "@_", RESET;
#push @{$CURR_STACK}, "@_" if defined $CURR_STACK;
}
sub trace {
my $space = "\t"x$CURR_DEPTH;
$FH = *STDOUT unless defined $FH;
print $FH ITALIC, BRIGHT_BLUE, "${space}@_", RESET;
push @{$CURR_STACK}, "${space}@_" if defined $CURR_STACK;
panic("Stack overflow...!\n") if @{$CURR_STACK} > $MAX_STACK_SIZE;
}
sub verbose {
$FH = *STDOUT unless defined $FH;
print $FH color('bright_green');
print $FH @_ if defined $verbose;
print $FH color('reset');
}
sub panic {
#print BOLD, RED, "@_", RESET;
error(RED, @_);
die "Error encountered\n";
}
sub error {
$FH = *STDOUT unless defined $FH;
my $prfx = dirname $CURR_FILE;
$prfx = "$TID: $prfx) $CURR_FUNC:\t";
print $FH BOLD, RED, $prfx, @_, RESET;
print BOLD, RED, $prfx, @_, RESET;
}
sub alert {
$FH = *STDOUT unless defined $FH;
my $prfx = "init:";
if (defined $CURR_FILE) {
$prfx = dirname $CURR_FILE;
$prfx = "$TID: $prfx) $CURR_FUNC:\t";
}
print $FH BOLD, MAGENTA, $prfx, @_, RESET;
print BOLD, MAGENTA, $prfx, @_, RESET;
push @{$CURR_STACK}, "ALERT: @_" if defined $CURR_STACK;
}
sub warning {
$FH = *STDOUT unless defined $FH;;
print $FH BOLD, YELLOW, @_, RESET;
push @{$CURR_STACK}, "WARNING: @_" if defined $CURR_STACK;
}
sub get_next {
lock @cscope_lines;
return pop @cscope_lines;
}
sub inc_def_depth {
$CURR_DEF_DEPTH++;
if ($CURR_DEF_DEPTH > $CURR_DEF_DEPTH_MAX) {
lock $CURR_DEF_DEPTH_MAX;
$CURR_DEF_DEPTH_MAX = $CURR_DEF_DEPTH
}
}
sub inc_depth {
$CURR_DEPTH++;
if ($CURR_DEPTH > $CURR_DEPTH_MAX) {
lock $CURR_DEPTH_MAX;
$CURR_DEPTH_MAX = $CURR_DEPTH
}
}
sub add_cached_direct_cb_count {
my ($type, $cnt) = @_;
verbose "storring direct: $type: $cnt\n";
$cached_direct_cb_count{$type} = $cnt;
}
sub get_cached_direct_cb_count {
my $type = shift;
my $cnt = $cached_direct_cb_count{$type};
my $str = defined $cnt ? $cnt : "NaN";
verbose "returning direct: $type: $str\n";
return defined $cnt ? $cnt : 0;
}
sub add_cached_struct_cb_count {
my ($type, $cnt) = @_;
verbose "storing: $type: $cnt\n";
$cached_struct_cb_count{$type} = $cnt;
}
sub get_cached_struct_cb_count {
my $type = shift;
my $cnt = $cached_struct_cb_count{$type};
my $str = defined $cnt ? $cnt : "NaN";
verbose "returning: $type: $str\n";
return $cnt;
}
sub check_exported {
my $name = shift;
lock %exported_symbols;
return $exported_symbols{"$name"};
}
sub add_exported {
my $name = shift;
lock %exported_symbols;
$exported_symbols{"$name"} = 1;
}
sub add_alloc_func {
my $name = shift;
lock %alloc_funcs;
$alloc_funcs{"$name"}++;
}
sub add_assignment_func {
my $name = shift;
lock %assignment_funcs;
$assignment_funcs{"$name"}++;
}
sub extract_var {
my $str = shift;
verbose "extract_var[1]: $str\n";
$str =~ s/\([\w\s]+\*\s*\)//;
$str =~ s/\(\s*unsigned\s+long\s*\)//;
verbose "extract_var[2]: $str\n";
if ($str =~ /\w\s+\-\s+\w/) {
my @split = split /\s+-\s+/, $str;
trace "extract_var: $str|$split[0]\n";
$str = $split[0];
}
$str =~ s/^\s+//;
$str =~ s/\s+$//;
if ($str =~ /\+/) {
my $idx = 0;
my @str = split /\s+/, $str;
verbose "$str -> $str[$idx] ($#str)\n";
while ($str[$idx] =~ /^\s*\(+\s*$/) {$idx++;};
$str = $str[$idx];
$str =~ s/^\(+//;
$str =~ s/\)+$//;
$str =~ s/^\s+//;
$str =~ s/\s+$//;
panic "shit...[$str]\n" unless $str =~ /^[\w&>\-\.]+$/;
}
if ($str =~ /[\(\)]/) {
my $tmp = $str;
$tmp =~ s/[\(\)]//g;
verbose "Removing (): $str|->|$tmp\n";
$str = $tmp;
$str =~ s/^\s+//;
$str =~ s/\s+$//;
}
if ($str =~ /([\w&>\-\.]+)\s*[\[;\+]/) {
my $var = $1;
if ($str =~ /$var\s*(\[.*\])/) {
$var = "$var$1";
$var =~ s/\[.*\]/\[i\]/;
if ($str =~ /\]([^\s,;]+)/) {
verbose "DBG_DEEPER: $str |$var|$1\n";
$var = "$var$1";
}
}
$var =~ s/^\s+//;
$var =~ s/\s+$//;
return $var;
} else {
return $str;
}
}
sub extract_call_only {
my ($str, $func) = @_;
my $out = "";
my $i = 0;
#verbose "extract $str\n";
$str =~ /$func\s*(\(.*\))/;
panic "ERROR: WTF $str:$func\n" unless defined $1;
#verbose "extract: $1\n";
$str = $1;
foreach (split //, $str) {
$i++ if /\(/;
$i-- if /\)/;
panic "ERROR: $str\n" if $i < 0;
$out .= "$_";
last if $i == 0;
}
panic "ERROR: $str\n" if $i != 0;
$out =~ s/(\(\s*\w+\s*\*\))//g; #Squash casting e.g., (void *)
#verbose "Removed $1\n" if defined $1;
#$out =~ s/sizeof\s*\(.*?\)/sizeof/g;
#verbose "out: $out\n";
return $out;
}
sub get_param {
my ($string, $match) = @_;
my $str = extract_call_only $string, $CURR_FUNC;
verbose ("get_idx: $string| $str|$match|\n");
#verbose "##$str|$match|\n";
my @vars = split /,/, $str;
my $i = 0;
my $type = undef;
foreach (@vars) {
if (/\W$match\W*/) {
$type = $_;
$type =~ /(\w+)[\*\s]+$match\W/;
$type = $1 if defined $1;
trace "DECLARATION: $_: $type\n";
last;
}
$i++
}
#TOTO : show the var and check if ptr/field.
panic "HEmm... $str|$match|<$i>" if ($#vars == -1 or $i > $#vars);
verbose "$i> $type|::|$match\n";
return ($i, $type);
}
###TODO:
my @priv_funcs = qw(netdev_priv aead_request_ctx scsi_cmd_priv _request_ctx); #akcipher_request_ctx
sub extract_and_check_var {
my $str = extract_var shift;
my $stop = 0;
if ($str =~ /skb.*\->data/) {
trace "RISK:[SKB] [$str]skb->data exposes sh_info\n";
#TODO: identify skb alloc funciions
return undef, 1;
}
unless (($str =~ /NULL/) or ($str =~ /^\d$/)) {
trace "REPLACE: $str\n";
return $str, 1;
} else {
trace "Trivial: $str\n";
$stop = 0;
}
return undef, $stop;
}
sub extract_assignmet {
my $str = shift;
my $stop = 0;
my $rc = undef;
my @str = split /=/, $str;
#$str =~ s/^\([^\(\)]+\)//g;
#$str =~ s/[^\w\s]\([^\(\)]+\)//g;
warning "Unhandled assignment: $str\n" and return (undef, 1) if ($#str > 1);
$str = $str[$#str];
if ($str =~ /\[.*\]/) {
my $tmp = $str;
$tmp =~ s/\[[^\[\]]+\]/\[i\]/g;
verbose "CHANGE:$str-> $tmp\n";
$str = $tmp;
}
verbose "Extract_assignmet:$str\n";
if ( $str =~ /\W(\w+)\s*\(/) {
my @type_C = grep(/$1/, @type_C_funcs);
$stop = 1;
if (@type_C) {
trace "Type C: may expose shared_info\n";
} elsif ($str =~ /(\w*alloc\w*)\s*\(|(\w*get\w*pages*)\s*\(/) {
panic "WTF?" unless (defined $1 or defined $2);
add_alloc_func defined $1 ? $1 : $2;
trace "SLUB: allocation $str\n";
} elsif ($str =~ /scsi_cmd_priv\s*\((.*)\)/) {
trace "VULNERABILITY:scsi_cmnd_priv: exposes scsi_cmnd: $str\n";
add_assignment_func 'scsi_cmnd_priv';
#TODO: Any point in tracing?
} elsif ($str =~ /current_buf\s*\((\w+)\)/) {
trace "VULNERABILITY: current_buff: exposes : $1\n";
add_assignment_func 'current_buff';
return ($1,1);
} elsif ($str =~ /(\w*_request_ctx)\s*\(\s*(\w+)/) {
trace "VULNERABILITY: $1: exposes state with callbacks: $2\n";
add_assignment_func $2;
return ($2,1);
} elsif ($str =~ /PTR_ALIGN\s*\((.*),.*\)/) {
trace "PTR_ALIGN: $1\n";
($str, $stop) = extract_and_check_var $1;
return $str, $stop;
} elsif ($str =~ /PAGE_ALIGN\s*\((.*)\)/) {
trace "PAGE_ALIGN: $1\n";
($str, $stop) = extract_and_check_var $1;
return $str, $stop;
} else {
my $func = 'nan';
if ($str =~ /(\w+)\s*\(/) {
$func = $1;
}
trace "UNHANDLED FUNCTION: $str:$func\n";
add_assignment_func $func;
}
} else {
($str, $stop) = extract_and_check_var $str;
$rc = $str;
}
return ($rc, $stop);
}
sub get_assignment_from_perma_cache {
my $type = shift;
my $file = "$STRUCT_CACHE/Assignment/$type.txt";
return unless -e $file;
tie my @text, 'Tie::File', $file;
return undef if @text == 0;
verbose "PERMA: read $#{text} lines from $file\n";
return \@text;
}
sub get_assignment_from_cache {
my $type = shift;
my $out = $local_assignment_cache{"$type"};
return $out if defined $out;
return get_assignment_from_perma_cache $type;
}
sub add_assignment_to_perma_cache {
my ($type, $arr) = @_;
my $file = "$STRUCT_CACHE/Assignment/$type.txt";
return unless defined $arr;
return unless defined $STRUCT_CACHE;
return if -e $file;
open my $fh, '>', $file;
foreach (@{$arr}) {
print $fh "$_\n";
}
close $fh;
verbose "${DBG_CACHE}PERMA: Written $#{$arr} lines to $file\n";
}
sub get_assignment {
my $type = shift;
my $text = get_assignment_from_cache $type;
return $text if defined $text;
my @text = qx(cscope -dL -9 $type);
if (@text) {
add_to_assignment_cache($type, \@text);
return \@text;
}
return undef;
}
sub __get_struct_from_perma_cache {
my $type = shift;
my $file = "$STRUCT_CACHE/$type.txt";
return unless -e $file;
tie my @text, 'Tie::File', $file;
return undef if @text == 0;
verbose "PERMA: read $#{text} lines from $file\n";
return \@text;
}
sub get_prfx {
my $prfx = $CURR_FILE;
$prfx =~ s/\//_/g;
$prfx =~ s/\./_/g;
verbose "PRFX:$CURR_FILE>$prfx\n";
return "${prfx}_";
}
sub get_struct_from_perma_cache {
my $type = shift;
my $out = __get_struct_from_perma_cache $type;
return $out if defined $out;
my $prfx = get_prfx();
return __get_struct_from_perma_cache "${prfx}$type";
}
sub add_struct_to_perma_cache {
my ($type, $arr) = @_;
my $file = "$STRUCT_CACHE/$type.txt";
return unless defined $arr;
return unless defined $STRUCT_CACHE;
return if -e $file;
open my $fh, '>', $file;
foreach (@{$arr}) {
print $fh "$_\n";
}
close $fh;
verbose "${DBG_CACHE}PERMA: Written $#{$arr} lines to $file\n";
}
sub add_struct_to_global_cache {
my ($type, $arr) = @_;
lock %global_struct_cache;
$global_struct_cache{"$type"} = $arr;
}
sub add_to_assignment_cache {
my ($type, $arr) = @_;
return unless defined $arr;
#verbose "${DBG_CACHE}CACHE: Written $#{$arr} lines to cache\n";
$local_assignment_cache{"$type"} = $arr;
#add_assignment_to_global_cache $type, $arr;
add_assignment_to_perma_cache $type, $arr;
}
sub add_to_struct_cache {
my ($type, $arr) = @_;
return unless defined $arr;
verbose "${DBG_CACHE}CACHE: Written $#{$arr} lines to cache\n";
$local_struct_cache{"$type"} = $arr;
add_struct_to_global_cache $type, $arr;
add_struct_to_perma_cache $type, $arr;
}
sub get_struct_from_gloabl_cache {
my $type = shift;
lock %global_struct_cache;
my $arr = $global_struct_cache{"$type"};
verbose "${DBG_CACHE}GLOBAL: read $#{$arr} lines from global cache\n" if defined $arr;
return $arr;
}
sub exists_in_global_cache {
my $type = shift;
lock %global_struct_cache;
$local_stats{'global_struct_cache_exists'}++ and return 1 if exists $global_struct_cache{"$type"};
return undef;
}
sub __get_struct_from_cache {
my $type = shift;
my $out;
$out = $local_struct_cache{"$type"};
verbose "${DBG_CACHE}LOCAL: read $#{$out} lines from global cache\n" if defined $out;
$local_stats{'local_struct_cache'}++ and return $out if defined $out;
$out = get_struct_from_gloabl_cache $type;
$local_stats{'global_struct_cache_hit'}++ if defined $out;
$local_stats{'global_struct_cache_miss'}++ unless defined $out;
return $out;
}
sub get_struct_from_cache {
my $type = shift;
my $out = __get_struct_from_cache $type;
return $out if defined $out;
my $prfx = get_prfx();
return __get_struct_from_cache "${prfx}$type";
}
sub exists_in_cache {
my $type = shift;
$local_stats{'local_struct_cache_exists'}++ and return 1 if exists $local_struct_cache{"$type"};
return exists_in_global_cache $type;
}
sub read_down {
my ($file, $line) = @_;
my @struct : shared = ();
until ($$file[$line] =~ /^}/) {
#verbose "$$file[$line]\n";
push @struct, $$file[$line];
$line++;
last if $line == $#{$file};
}
#verbose "$$file[$line]\n";
push @struct, $$file[$line];
return \@struct;
}
sub read_up {
my ($file, $line) = @_;
my @struct : shared = ();
until ($$file[$line] =~ /^\w/) {
#verbose "$$file[$line]";
push @struct, $$file[$line];
$line--;
last if $line == 0;
}
#verbose "$$file[$line]\n";
push @struct, $$file[$line];
verbose "$DBG_CACHE:read_up: $#struct\n";
return \@struct;
}
sub read_struct_cscope {
my $type = shift;
my @cb;
my $cnt = 0;
my $prfx = undef;
my @out = qx(cscope -dL -1 $type);
verbose "cscope -dL -1 $type\n";
for (@out) {
chomp;
if (($_ =~ /struct\s+$type\s*{/) or ($_ =~ /\d+\s+}\s*$type\s*;/)){
verbose "CSCOPE[C:Match]: $_\n";
push @cb, $_;
$cnt++;
} #else {
# verbose "CSCOPE[C]: $_|$type|\n";
#}
#$i++;
}
if ($cnt > 1) {
@cb = grep(/$CURR_FILE/, @cb);
warning "Solvable issue[?] $CURR_FILE:$type\n";
$cnt = @cb;
$prfx = get_prfx();
}
verbose "CSCOPE: Cant cscope parse [$cnt]\n" and return (undef,$prfx) unless $cnt == 1;
my ($file, $tp, $line, @def) = split /\s+/, $cb[0];
tie my @file_text, 'Tie::File', $file;
if ($file_text[$line -1] =~ /struct\s+$type\s*{/) {
verbose "CSCOPE down succeeded\n";
return read_down \@file_text, $line -1, $prfx;
} elsif ($file_text[$line -1] =~ /}\s*$type\s*;/){
verbose "CSCOPE up succeeded\n";
return read_up \@file_text, $line -1, $prfx;
} else {
verbose "CSCOPE failed [$file_text[$line -1]]\n";
}
return (undef, $prfx);
}
sub search_type_pahole {
my ($type, $name) = @_;
my $arr = undef;
return undef unless -e $name;
my @out = qx(/usr/bin/pahole -EAa $name 2>/dev/null);
verbose "/usr/bin/pahole -EAa $name 2>/dev/null: [$type]\n";
if (@out) {
my $line = 0;
for (@out) {
verbose "$DBG_CACHE:[$type]$_\n" if /\W$type\W/;
$arr = read_up(\@out, $line) and last if /^}\s*[\w\(\)]*\s*$type\s*;/;
$line++;
}
}
return undef unless $#{$arr} > 0;
verbose "$DBG_CACHE:PAHOLE: $#{$arr}\n";
return $arr;
}
sub read_struct {
my ($type, $name) = @_;
$name = $CURR_FILE unless defined $name;
my @out : shared;
my $cs_out;
my $prfx;
my $out = get_struct_from_cache $type;
return $out if defined $out;
$out = get_struct_from_perma_cache $type;
return $out if defined $out;
$name =~ s/\.c/\.o/;
verbose "File not Found $name\n" unless -e $name;
if (defined exists_in_cache($type)) {
verbose "$type exists in cache\n";
return undef;
}
($cs_out,$prfx) = read_struct_cscope $type;
return undef unless defined $cs_out or defined $prfx;
$prfx = "" unless defined $prfx;
for ("$name", $VMLINUX) {
next unless defined $_;
@out = qx(/usr/bin/pahole -C $type -EAa $_ 2>/dev/null);
if (@out) {
$out = \@out;
$type = "${prfx}$type";
add_to_struct_cache($type, \@out);
return $out;
}
}
$out = search_type_pahole $type, $name;
verbose "$DBG_CACHE:PAHOLE:$out\n" if defined $out;
add_to_struct_cache($type, $out) and return $out if defined $out;
##cscope for not compiled or missing debug info
#$out = read_struct_cscope $type;
$out = $cs_out;
verbose "$DBG_CACHE:CSCOPE:$out\n" if defined $out;
$type = "${prfx}$type";
add_to_struct_cache($type, $out);
#TODO: Read from cscope if not found
return $out;
}
sub get_type_C {
my %files = ();
my $total = 0;
my $files = 0;
print ITALIC, CYAN, "type_C callers\n", RESET;
foreach (@type_C_funcs) {
my @calls = qx(cscope -dL -3 $_);
foreach my $call (@calls) {
my ($file, $func, @other) = split /\s+/, $call;
#print RED, "$file:$func\n", RESET;
next if grep(/$func/,@type_C_funcs);
$total++;
next if exists $files{$file};
$files{$file} = undef;
$files++;
}
}
print CYAN, "type_C: [$files, $total]\n", RESET;
}
sub prep_build_skb {
my @callers = qx(cscope -dL -3 build_skb);
my $i = 0;
print ITALIC, CYAN, "build_skb callers\n", RESET;
foreach (@callers) {
$i++;
print CYAN, "BUILD_SKB: $i) $_", RESET;
}
}
sub next_line {
my ($file, $line) = @_;
while (1) {
panic "$line not defined ($CURR_FILE)\n" unless defined $$file[$line];
$line-- and next if $$file[$line] =~ /^[\*\w\s]$/;
$line-- and next if $$file[$line] =~ /[%\"]+/;
last;
}
if ($$file[$line] =~ /\*\//) {
#printf "Comment: $file[$l]\n";
until ($$file[$line] =~ /\/\*/) {
#verbose "Comment: $$file[$line]\n";
$line--;
}
if ($$file[$line] =~ /^\s*\/\*/) {
#verbose "Comment: $$file[$line]\n";
$line--;
}
}
return $line;
}
sub get_cb_rec {
my ($prfx, $struct_log, $type, $file_hint) = @_;
my %struct_log;
my $cb_count;
my $direct;
my $struct;
$struct_log = \%struct_log unless defined $struct_log;
${$struct_log}{$type} = undef;
$cb_count = get_cached_direct_cb_count $type;
if (($prfx eq '') and $cb_count > 0) {
trace("DIRECT: $cb_count Callbacks exposed in ${prfx}$type\n");
}
$cb_count = get_cached_struct_cb_count $type;
return $cb_count if defined $cb_count;
$cb_count = 0;
$struct = read_struct $type, $file_hint;
warning "Cant process $type for callbacks\n" and return 0 unless defined $struct;
my @cb = grep(/^\s*\w+\**\s+\(/, @{$struct});
if (@cb > 0) {
my $num = @cb;
#print "@cb\n" if defined $verbose;
$cb_count += $num;
}
$direct = $cb_count;
my @st = grep(/^\s*struct\s+(\w+)\s+\w+.*;/, @{$struct});
foreach (@st) {
/^\s*struct\s+(\w+)\s+\w+.*;/;
next if exists ${$struct_log}{$1};
${$struct_log}{$1} = undef;
$direct += get_cached_direct_cb_count $1;
verbose "DBG_get_cb_rec:$_:$1\n";
#$cb_count += get_cb_rec("${prfx}$type.", $struct_log, $1, $file_hint);
$cb_count += get_cb_rec($prfx, $struct_log, $1, $file_hint);
}
if (($prfx eq '') and $direct > 0) {
trace("DIRECT: $direct Callbacks exposed in ${prfx}$type\n");
add_cached_direct_cb_count $type, $direct;
}
@st = grep(/^\s*struct\s+(\w+)\s+\*+/, @{$struct});
foreach (@st) {
/^\s*struct\s+(\w+)\s+\*+/;
next if exists ${$struct_log}{$1};
${$struct_log}{$1} = undef;
$cb_count += get_cb_rec("${prfx}$type->", $struct_log, $1, $file_hint);
}
#TODO: Cache results
add_cached_struct_cb_count $type, $cb_count;
return $cb_count;
}
sub dump_local_stats {
my ($i, $u);
$i = 0;
$u = 0;
foreach (sort {$a cmp $b} keys %local_stats) {
verbose "$_: $local_stats{$_}\n";
}
verbose "Local struct Cache:\n";
foreach (sort {$a cmp $b} keys %local_struct_cache) {
my $defined = 'Yes';
$i++;
$u++ and $defined = 'No' unless defined $local_struct_cache{$_};
verbose "$i,$u]$_: $defined\n";
}
}
#################### FUNCTIONS ####################
sub collect_cb {
my ($prfx, $struct, $file, $field) = @_;
my $callback_count = 0;
$struct_log{$struct} = undef;
#error "No Such File:$file\n" and
return 0 unless -e $file;
my @out = qx(/usr/bin/pahole -C $struct -EAa $file 2>/dev/null);
verbose "pahole -C $struct -EAa $file [$#out]\n";
if ($#out < 0) {
my @defs = qx(cscope -dL -1 $struct);
#alert "Please get the struct from other fiels [$struct]\n" unless
# exists ($struct_cache{$struct});
if ($#defs < 0) {
error "Cant locate the definition of $struct\n";
}
$struct_cache{$struct} = undef;
#TODO: Please Fix, get the file with cscope
return 0;
}
if (defined $field) {
### is this a pointer
my @def = grep (/\*\s*$field\W/, @out);
if ($#def > -1) {
for (@def) {
chomp;
verbose "Field: [$field]:$_ \n";
if (/^\s*struct\s+(\w+)\s+\*+/) {
verbose "Field: $1\n";
return collect_cb("${prfx}$struct->", $1, $file);
} else {
warning "Find assignment: $_\n";
return 0;
}
}
alert ("No match!!: $field\n");
for (@def) {
chomp;
warning "$_\n";
}
return 0;
} else {
@def = grep (/$field\[/, @out);
if ($#def > -1) {
verbose "mapping of $field includes $struct\n";
trace "Mapped array: $def[0]\n";
} else {
error "No such field: $field in $struct\n";
}
}
}
#direct callbacks
my @cb = grep(/^\s*\w+\**\s+\(/, @out);
if (@cb > 0) {
my $num = @cb;
if ($prfx eq '') {
trace(" $num Callbacks exposed in ${prfx}$struct\n");
}
#print "@cb\n" if defined $verbose;
$callback_count += $num;
}
#struct callbacks - thay may contain cal;lbacks
my @st = grep(/^\s*struct\s+(\w+)\s+\*+/, @out);
#\s*\*+\s*(\w+)
foreach (@st) {
/^\s*struct\s+(\w+)\s+\*+/;
next if exists $struct_log{$1};
$struct_log{$1} = undef;
#print "struct $1\n";
$callback_count += collect_cb("${prfx}$struct->", $1, $file);
}
return $callback_count;
}
#TODO: Handle .h files SMC_insl
sub is_name_conflict {
my ($line, $str, $cfunc) = @_;
return undef if (defined (check_exported $cfunc));
return undef if ($str =~ /^\s*static/); #TODO: check that this is not an h file
my $dir = dirname $CURR_FILE;
my $new_dir = dirname $$line[0];
#verbose "$dir -> $new_dir\n";
#TODO: This is a conjecture, you can possibly have a name conflict in same dir w/o static
return 1 if ($new_dir eq $dir);
verbose "conflict: $str [$cfunc]\n";
unless ((index($new_dir, $dir) == -1) && (index($dir, $new_dir) == -1)) {
verbose "Related Jump...\n";
return 1;
}
#check for name conflict in case different locations
my @definition = qx(cscope -dL -0 $cfunc);
verbose "cscope -dL -1 $cfunc\n";
### Prooning NAme conflicts....
for my $def (@definition) {
chomp $def;
my @def = split /\s+/, $def;
my $nfile = shift @def; #file
my $ndir = dirname $nfile;
shift @def; #func
shift @def; #line
my $test = join ' ', @def;
chomp $test;
#verbose "def: $test\n";
#if ($test =~ /EXPORT_SYMBOL\($CALLEE\)/) {
if ($test =~ /EXPORT\w*SYMBOL\w*/) {
verbose "Ok symbol exported...:$test\n";
add_exported $cfunc;
return 1;
}
if ($test =~ /^#define\s+$cfunc\W/) {
warning "Ok symbol defined...:$test\n";
return 1;
}
#Name collision...
}
warning "BAD Jump: ?\n";
return undef;
}