| 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; | 
| Sam Ravnborg | 7712401 | 2008-06-15 21:41:09 +0200 | [diff] [blame] | 22 |  | 
 | 23 | my ($dir, $arch, @files) = @ARGV; | 
 | 24 |  | 
 | 25 | my $ret = 0; | 
 | 26 | my $line; | 
 | 27 | my $lineno = 0; | 
 | 28 | my $filename; | 
 | 29 |  | 
 | 30 | foreach my $file (@files) { | 
 | 31 | 	$filename = $file; | 
| Stephen Hemminger | dbbe33e | 2010-02-22 15:17:24 -0800 | [diff] [blame] | 32 |  | 
 | 33 | 	open(my $fh, '<', $filename) | 
 | 34 | 		or die "$filename: $!\n"; | 
| Sam Ravnborg | 7712401 | 2008-06-15 21:41:09 +0200 | [diff] [blame] | 35 | 	$lineno = 0; | 
| Stephen Hemminger | dbbe33e | 2010-02-22 15:17:24 -0800 | [diff] [blame] | 36 | 	while ($line = <$fh>) { | 
| Sam Ravnborg | 7712401 | 2008-06-15 21:41:09 +0200 | [diff] [blame] | 37 | 		$lineno++; | 
| Sam Ravnborg | 483b412 | 2008-12-30 11:34:58 +0100 | [diff] [blame] | 38 | 		&check_include(); | 
 | 39 | 		&check_asm_types(); | 
 | 40 | 		&check_sizetypes(); | 
| Amerigo Wang | 67b7ebe | 2009-06-04 22:12:01 -0400 | [diff] [blame] | 41 | 		&check_declarations(); | 
| Sam Ravnborg | 7e3fa56 | 2009-01-30 23:56:42 +0100 | [diff] [blame] | 42 | 		# Dropped for now. Too much noise &check_config(); | 
| Sam Ravnborg | 7712401 | 2008-06-15 21:41:09 +0200 | [diff] [blame] | 43 | 	} | 
| Stephen Hemminger | dbbe33e | 2010-02-22 15:17:24 -0800 | [diff] [blame] | 44 | 	close $fh; | 
| Sam Ravnborg | 7712401 | 2008-06-15 21:41:09 +0200 | [diff] [blame] | 45 | } | 
 | 46 | exit $ret; | 
 | 47 |  | 
 | 48 | sub check_include | 
 | 49 | { | 
 | 50 | 	if ($line =~ m/^\s*#\s*include\s+<((asm|linux).*)>/) { | 
 | 51 | 		my $inc = $1; | 
 | 52 | 		my $found; | 
 | 53 | 		$found = stat($dir . "/" . $inc); | 
 | 54 | 		if (!$found) { | 
 | 55 | 			$inc =~ s#asm/#asm-$arch/#; | 
 | 56 | 			$found = stat($dir . "/" . $inc); | 
 | 57 | 		} | 
 | 58 | 		if (!$found) { | 
 | 59 | 			printf STDERR "$filename:$lineno: included file '$inc' is not exported\n"; | 
 | 60 | 			$ret = 1; | 
 | 61 | 		} | 
 | 62 | 	} | 
 | 63 | } | 
| Mike Frysinger | 46b8af5 | 2008-12-27 02:43:36 -0500 | [diff] [blame] | 64 |  | 
| Amerigo Wang | 67b7ebe | 2009-06-04 22:12:01 -0400 | [diff] [blame] | 65 | sub check_declarations | 
| Mike Frysinger | 46b8af5 | 2008-12-27 02:43:36 -0500 | [diff] [blame] | 66 | { | 
| akpm@linux-foundation.org | de323f2 | 2010-11-30 13:51:13 -0800 | [diff] [blame] | 67 | 	if ($line =~m/^(\s*extern|unsigned|char|short|int|long|void)\b/) { | 
| Amerigo Wang | 67b7ebe | 2009-06-04 22:12:01 -0400 | [diff] [blame] | 68 | 		printf STDERR "$filename:$lineno: " . | 
| akpm@linux-foundation.org | d52784e | 2010-11-30 13:52:14 -0800 | [diff] [blame] | 69 | 			      "userspace cannot reference function or " . | 
 | 70 | 			      "variable defined in the kernel\n"; | 
| Mike Frysinger | 46b8af5 | 2008-12-27 02:43:36 -0500 | [diff] [blame] | 71 | 	} | 
 | 72 | } | 
| Sam Ravnborg | 7e557a2 | 2008-12-27 19:52:20 +0100 | [diff] [blame] | 73 |  | 
 | 74 | sub check_config | 
 | 75 | { | 
| Robert P. J. Day | 1581c1c | 2009-05-12 13:43:36 -0700 | [diff] [blame] | 76 | 	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] | 77 | 		printf STDERR "$filename:$lineno: leaks CONFIG_$1 to userspace where it is not valid\n"; | 
 | 78 | 	} | 
 | 79 | } | 
 | 80 |  | 
| Sam Ravnborg | 483b412 | 2008-12-30 11:34:58 +0100 | [diff] [blame] | 81 | my $linux_asm_types; | 
| Stephen Hemminger | dbbe33e | 2010-02-22 15:17:24 -0800 | [diff] [blame] | 82 | sub check_asm_types | 
| Sam Ravnborg | 483b412 | 2008-12-30 11:34:58 +0100 | [diff] [blame] | 83 | { | 
| Sam Ravnborg | b67ff8c | 2008-12-31 09:32:30 +0100 | [diff] [blame] | 84 | 	if ($filename =~ /types.h|int-l64.h|int-ll64.h/o) { | 
 | 85 | 		return; | 
 | 86 | 	} | 
| Sam Ravnborg | 483b412 | 2008-12-30 11:34:58 +0100 | [diff] [blame] | 87 | 	if ($lineno == 1) { | 
 | 88 | 		$linux_asm_types = 0; | 
 | 89 | 	} elsif ($linux_asm_types >= 1) { | 
 | 90 | 		return; | 
 | 91 | 	} | 
 | 92 | 	if ($line =~ m/^\s*#\s*include\s+<asm\/types.h>/) { | 
 | 93 | 		$linux_asm_types = 1; | 
 | 94 | 		printf STDERR "$filename:$lineno: " . | 
 | 95 | 		"include of <linux/types.h> is preferred over <asm/types.h>\n" | 
 | 96 | 		# Warn until headers are all fixed | 
 | 97 | 		#$ret = 1; | 
 | 98 | 	} | 
 | 99 | } | 
 | 100 |  | 
 | 101 | my $linux_types; | 
 | 102 | sub check_sizetypes | 
 | 103 | { | 
| Sam Ravnborg | b67ff8c | 2008-12-31 09:32:30 +0100 | [diff] [blame] | 104 | 	if ($filename =~ /types.h|int-l64.h|int-ll64.h/o) { | 
 | 105 | 		return; | 
 | 106 | 	} | 
| Sam Ravnborg | 483b412 | 2008-12-30 11:34:58 +0100 | [diff] [blame] | 107 | 	if ($lineno == 1) { | 
 | 108 | 		$linux_types = 0; | 
 | 109 | 	} elsif ($linux_types >= 1) { | 
 | 110 | 		return; | 
 | 111 | 	} | 
 | 112 | 	if ($line =~ m/^\s*#\s*include\s+<linux\/types.h>/) { | 
 | 113 | 		$linux_types = 1; | 
 | 114 | 		return; | 
 | 115 | 	} | 
 | 116 | 	if ($line =~ m/__[us](8|16|32|64)\b/) { | 
 | 117 | 		printf STDERR "$filename:$lineno: " . | 
 | 118 | 		              "found __[us]{8,16,32,64} type " . | 
 | 119 | 		              "without #include <linux/types.h>\n"; | 
 | 120 | 		$linux_types = 2; | 
 | 121 | 		# Warn until headers are all fixed | 
 | 122 | 		#$ret = 1; | 
 | 123 | 	} | 
 | 124 | } | 
 | 125 |  |