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