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