| Jeremy Huntwork | 15a2ee7 | 2008-10-29 14:20:13 -0700 | [diff] [blame] | 1 | #!/usr/bin/perl -w | 
| Sam Ravnborg | 7712401 | 2008-06-15 21:41:09 +0200 | [diff] [blame] | 2 | # | 
|  | 3 | # headers_check.pl execute a number of trivial consistency checks | 
|  | 4 | # | 
| Amerigo Wang | 67b7ebe | 2009-06-04 22:12:01 -0400 | [diff] [blame] | 5 | # Usage: headers_check.pl dir arch [files...] | 
| Sam Ravnborg | 7712401 | 2008-06-15 21:41:09 +0200 | [diff] [blame] | 6 | # dir:   dir to look for included files | 
|  | 7 | # arch:  architecture | 
|  | 8 | # files: list of files to check | 
|  | 9 | # | 
|  | 10 | # The script reads the supplied files line by line and: | 
|  | 11 | # | 
|  | 12 | # 1) for each include statement it checks if the | 
|  | 13 | #    included file actually exists. | 
|  | 14 | #    Only include files located in asm* and linux* are checked. | 
|  | 15 | #    The rest are assumed to be system include files. | 
|  | 16 | # | 
| Mike Frysinger | 46b8af5 | 2008-12-27 02:43:36 -0500 | [diff] [blame] | 17 | # 2) It is checked that prototypes does not use "extern" | 
|  | 18 | # | 
| Sam Ravnborg | 7e557a2 | 2008-12-27 19:52:20 +0100 | [diff] [blame] | 19 | # 3) Check for leaked CONFIG_ symbols | 
| Sam Ravnborg | 7712401 | 2008-06-15 21:41:09 +0200 | [diff] [blame] | 20 |  | 
|  | 21 | use strict; | 
| Bobby Powers | f75a8df | 2012-03-05 15:08:09 -0800 | [diff] [blame] | 22 | use File::Basename; | 
| Sam Ravnborg | 7712401 | 2008-06-15 21:41:09 +0200 | [diff] [blame] | 23 |  | 
|  | 24 | my ($dir, $arch, @files) = @ARGV; | 
|  | 25 |  | 
|  | 26 | my $ret = 0; | 
|  | 27 | my $line; | 
|  | 28 | my $lineno = 0; | 
|  | 29 | my $filename; | 
|  | 30 |  | 
|  | 31 | foreach my $file (@files) { | 
|  | 32 | $filename = $file; | 
| Stephen Hemminger | dbbe33e | 2010-02-22 15:17:24 -0800 | [diff] [blame] | 33 |  | 
|  | 34 | open(my $fh, '<', $filename) | 
|  | 35 | or die "$filename: $!\n"; | 
| Sam Ravnborg | 7712401 | 2008-06-15 21:41:09 +0200 | [diff] [blame] | 36 | $lineno = 0; | 
| Stephen Hemminger | dbbe33e | 2010-02-22 15:17:24 -0800 | [diff] [blame] | 37 | while ($line = <$fh>) { | 
| Sam Ravnborg | 7712401 | 2008-06-15 21:41:09 +0200 | [diff] [blame] | 38 | $lineno++; | 
| Sam Ravnborg | 483b412 | 2008-12-30 11:34:58 +0100 | [diff] [blame] | 39 | &check_include(); | 
|  | 40 | &check_asm_types(); | 
|  | 41 | &check_sizetypes(); | 
| Amerigo Wang | 67b7ebe | 2009-06-04 22:12:01 -0400 | [diff] [blame] | 42 | &check_declarations(); | 
| Sam Ravnborg | 7e3fa56 | 2009-01-30 23:56:42 +0100 | [diff] [blame] | 43 | # Dropped for now. Too much noise &check_config(); | 
| Sam Ravnborg | 7712401 | 2008-06-15 21:41:09 +0200 | [diff] [blame] | 44 | } | 
| Stephen Hemminger | dbbe33e | 2010-02-22 15:17:24 -0800 | [diff] [blame] | 45 | close $fh; | 
| Sam Ravnborg | 7712401 | 2008-06-15 21:41:09 +0200 | [diff] [blame] | 46 | } | 
|  | 47 | exit $ret; | 
|  | 48 |  | 
|  | 49 | sub check_include | 
|  | 50 | { | 
|  | 51 | if ($line =~ m/^\s*#\s*include\s+<((asm|linux).*)>/) { | 
|  | 52 | my $inc = $1; | 
|  | 53 | my $found; | 
|  | 54 | $found = stat($dir . "/" . $inc); | 
|  | 55 | if (!$found) { | 
|  | 56 | $inc =~ s#asm/#asm-$arch/#; | 
|  | 57 | $found = stat($dir . "/" . $inc); | 
|  | 58 | } | 
|  | 59 | if (!$found) { | 
|  | 60 | printf STDERR "$filename:$lineno: included file '$inc' is not exported\n"; | 
|  | 61 | $ret = 1; | 
|  | 62 | } | 
|  | 63 | } | 
|  | 64 | } | 
| Mike Frysinger | 46b8af5 | 2008-12-27 02:43:36 -0500 | [diff] [blame] | 65 |  | 
| Amerigo Wang | 67b7ebe | 2009-06-04 22:12:01 -0400 | [diff] [blame] | 66 | sub check_declarations | 
| Mike Frysinger | 46b8af5 | 2008-12-27 02:43:36 -0500 | [diff] [blame] | 67 | { | 
| akpm@linux-foundation.org | de323f2 | 2010-11-30 13:51:13 -0800 | [diff] [blame] | 68 | if ($line =~m/^(\s*extern|unsigned|char|short|int|long|void)\b/) { | 
| Amerigo Wang | 67b7ebe | 2009-06-04 22:12:01 -0400 | [diff] [blame] | 69 | printf STDERR "$filename:$lineno: " . | 
| akpm@linux-foundation.org | d52784e | 2010-11-30 13:52:14 -0800 | [diff] [blame] | 70 | "userspace cannot reference function or " . | 
|  | 71 | "variable defined in the kernel\n"; | 
| Mike Frysinger | 46b8af5 | 2008-12-27 02:43:36 -0500 | [diff] [blame] | 72 | } | 
|  | 73 | } | 
| Sam Ravnborg | 7e557a2 | 2008-12-27 19:52:20 +0100 | [diff] [blame] | 74 |  | 
|  | 75 | sub check_config | 
|  | 76 | { | 
| Robert P. J. Day | 1581c1c | 2009-05-12 13:43:36 -0700 | [diff] [blame] | 77 | if ($line =~ m/[^a-zA-Z0-9_]+CONFIG_([a-zA-Z0-9_]+)[^a-zA-Z0-9_]/) { | 
| Sam Ravnborg | 7e557a2 | 2008-12-27 19:52:20 +0100 | [diff] [blame] | 78 | printf STDERR "$filename:$lineno: leaks CONFIG_$1 to userspace where it is not valid\n"; | 
|  | 79 | } | 
|  | 80 | } | 
|  | 81 |  | 
| Sam Ravnborg | 483b412 | 2008-12-30 11:34:58 +0100 | [diff] [blame] | 82 | my $linux_asm_types; | 
| Stephen Hemminger | dbbe33e | 2010-02-22 15:17:24 -0800 | [diff] [blame] | 83 | sub check_asm_types | 
| Sam Ravnborg | 483b412 | 2008-12-30 11:34:58 +0100 | [diff] [blame] | 84 | { | 
| Sam Ravnborg | b67ff8c | 2008-12-31 09:32:30 +0100 | [diff] [blame] | 85 | if ($filename =~ /types.h|int-l64.h|int-ll64.h/o) { | 
|  | 86 | return; | 
|  | 87 | } | 
| Sam Ravnborg | 483b412 | 2008-12-30 11:34:58 +0100 | [diff] [blame] | 88 | if ($lineno == 1) { | 
|  | 89 | $linux_asm_types = 0; | 
|  | 90 | } elsif ($linux_asm_types >= 1) { | 
|  | 91 | return; | 
|  | 92 | } | 
|  | 93 | if ($line =~ m/^\s*#\s*include\s+<asm\/types.h>/) { | 
|  | 94 | $linux_asm_types = 1; | 
|  | 95 | printf STDERR "$filename:$lineno: " . | 
|  | 96 | "include of <linux/types.h> is preferred over <asm/types.h>\n" | 
|  | 97 | # Warn until headers are all fixed | 
|  | 98 | #$ret = 1; | 
|  | 99 | } | 
|  | 100 | } | 
|  | 101 |  | 
|  | 102 | my $linux_types; | 
| Bobby Powers | f75a8df | 2012-03-05 15:08:09 -0800 | [diff] [blame] | 103 | my %import_stack = (); | 
|  | 104 | sub check_include_typesh | 
|  | 105 | { | 
|  | 106 | my $path = $_[0]; | 
|  | 107 | my $import_path; | 
|  | 108 |  | 
|  | 109 | my $fh; | 
|  | 110 | my @file_paths = ($path, $dir . "/" .  $path, dirname($filename) . "/" . $path); | 
|  | 111 | for my $possible ( @file_paths ) { | 
|  | 112 | if (not $import_stack{$possible} and open($fh, '<', $possible)) { | 
|  | 113 | $import_path = $possible; | 
|  | 114 | $import_stack{$import_path} = 1; | 
|  | 115 | last; | 
|  | 116 | } | 
|  | 117 | } | 
|  | 118 | if (eof $fh) { | 
|  | 119 | return; | 
|  | 120 | } | 
|  | 121 |  | 
|  | 122 | my $line; | 
|  | 123 | while ($line = <$fh>) { | 
|  | 124 | if ($line =~ m/^\s*#\s*include\s+<linux\/types.h>/) { | 
|  | 125 | $linux_types = 1; | 
|  | 126 | last; | 
|  | 127 | } | 
|  | 128 | if (my $included = ($line =~ /^\s*#\s*include\s+[<"](\S+)[>"]/)[0]) { | 
|  | 129 | check_include_typesh($included); | 
|  | 130 | } | 
|  | 131 | } | 
|  | 132 | close $fh; | 
|  | 133 | delete $import_stack{$import_path}; | 
|  | 134 | } | 
|  | 135 |  | 
| Sam Ravnborg | 483b412 | 2008-12-30 11:34:58 +0100 | [diff] [blame] | 136 | sub check_sizetypes | 
|  | 137 | { | 
| Sam Ravnborg | b67ff8c | 2008-12-31 09:32:30 +0100 | [diff] [blame] | 138 | if ($filename =~ /types.h|int-l64.h|int-ll64.h/o) { | 
|  | 139 | return; | 
|  | 140 | } | 
| Sam Ravnborg | 483b412 | 2008-12-30 11:34:58 +0100 | [diff] [blame] | 141 | if ($lineno == 1) { | 
|  | 142 | $linux_types = 0; | 
|  | 143 | } elsif ($linux_types >= 1) { | 
|  | 144 | return; | 
|  | 145 | } | 
|  | 146 | if ($line =~ m/^\s*#\s*include\s+<linux\/types.h>/) { | 
|  | 147 | $linux_types = 1; | 
|  | 148 | return; | 
|  | 149 | } | 
| Bobby Powers | f75a8df | 2012-03-05 15:08:09 -0800 | [diff] [blame] | 150 | if (my $included = ($line =~ /^\s*#\s*include\s+[<"](\S+)[>"]/)[0]) { | 
|  | 151 | check_include_typesh($included); | 
|  | 152 | } | 
| Sam Ravnborg | 483b412 | 2008-12-30 11:34:58 +0100 | [diff] [blame] | 153 | if ($line =~ m/__[us](8|16|32|64)\b/) { | 
|  | 154 | printf STDERR "$filename:$lineno: " . | 
|  | 155 | "found __[us]{8,16,32,64} type " . | 
|  | 156 | "without #include <linux/types.h>\n"; | 
|  | 157 | $linux_types = 2; | 
|  | 158 | # Warn until headers are all fixed | 
|  | 159 | #$ret = 1; | 
|  | 160 | } | 
|  | 161 | } |