Blame view

scripts/headers_check.pl 3.73 KB
cb77f0d62   Kamil Rytarowski   scripts: Switch t...
1
  #!/usr/bin/env perl
b24413180   Greg Kroah-Hartman   License cleanup: ...
2
  # SPDX-License-Identifier: GPL-2.0
7712401ae   Sam Ravnborg   kbuild: optimize ...
3
4
5
  #
  # headers_check.pl execute a number of trivial consistency checks
  #
67b7ebe09   Amerigo Wang   kbuild/headers_ch...
6
  # Usage: headers_check.pl dir arch [files...]
7712401ae   Sam Ravnborg   kbuild: optimize ...
7
8
9
10
11
12
13
14
15
16
17
  # dir:   dir to look for included files
  # arch:  architecture
  # files: list of files to check
  #
  # The script reads the supplied files line by line and:
  #
  # 1) for each include statement it checks if the
  #    included file actually exists.
  #    Only include files located in asm* and linux* are checked.
  #    The rest are assumed to be system include files.
  #
46b8af50b   Mike Frysinger   headers_check.pl:...
18
19
  # 2) It is checked that prototypes does not use "extern"
  #
7e557a250   Sam Ravnborg   kbuild: check for...
20
  # 3) Check for leaked CONFIG_ symbols
7712401ae   Sam Ravnborg   kbuild: optimize ...
21

cb77f0d62   Kamil Rytarowski   scripts: Switch t...
22
  use warnings;
7712401ae   Sam Ravnborg   kbuild: optimize ...
23
  use strict;
f75a8df3b   Bobby Powers   headers_check: re...
24
  use File::Basename;
7712401ae   Sam Ravnborg   kbuild: optimize ...
25
26
27
28
29
30
31
32
33
34
  
  my ($dir, $arch, @files) = @ARGV;
  
  my $ret = 0;
  my $line;
  my $lineno = 0;
  my $filename;
  
  foreach my $file (@files) {
  	$filename = $file;
dbbe33e99   Stephen Hemminger   headers_check: fi...
35
36
37
38
  
  	open(my $fh, '<', $filename)
  		or die "$filename: $!
  ";
7712401ae   Sam Ravnborg   kbuild: optimize ...
39
  	$lineno = 0;
dbbe33e99   Stephen Hemminger   headers_check: fi...
40
  	while ($line = <$fh>) {
7712401ae   Sam Ravnborg   kbuild: optimize ...
41
  		$lineno++;
483b41218   Sam Ravnborg   kbuild: add check...
42
43
44
  		&check_include();
  		&check_asm_types();
  		&check_sizetypes();
67b7ebe09   Amerigo Wang   kbuild/headers_ch...
45
  		&check_declarations();
7e3fa5614   Sam Ravnborg   kbuild: drop chec...
46
  		# Dropped for now. Too much noise &check_config();
7712401ae   Sam Ravnborg   kbuild: optimize ...
47
  	}
dbbe33e99   Stephen Hemminger   headers_check: fi...
48
  	close $fh;
7712401ae   Sam Ravnborg   kbuild: optimize ...
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
  }
  exit $ret;
  
  sub check_include
  {
  	if ($line =~ m/^\s*#\s*include\s+<((asm|linux).*)>/) {
  		my $inc = $1;
  		my $found;
  		$found = stat($dir . "/" . $inc);
  		if (!$found) {
  			$inc =~ s#asm/#asm-$arch/#;
  			$found = stat($dir . "/" . $inc);
  		}
  		if (!$found) {
  			printf STDERR "$filename:$lineno: included file '$inc' is not exported
  ";
  			$ret = 1;
  		}
  	}
  }
46b8af50b   Mike Frysinger   headers_check.pl:...
69

67b7ebe09   Amerigo Wang   kbuild/headers_ch...
70
  sub check_declarations
46b8af50b   Mike Frysinger   headers_check.pl:...
71
  {
a7e1d98f3   Paul Bolle   headers_check: sp...
72
73
74
75
  	# soundcard.h is what it is
  	if ($line =~ m/^void seqbuf_dump\(void\);/) {
  		return;
  	}
92181d47e   Arnd Bergmann   headers_check: do...
76
77
78
79
  	# drm headers are being C++ friendly
  	if ($line =~ m/^extern "C"/) {
  		return;
  	}
a7e1d98f3   Paul Bolle   headers_check: sp...
80
  	if ($line =~ m/^(\s*extern|unsigned|char|short|int|long|void)\b/) {
67b7ebe09   Amerigo Wang   kbuild/headers_ch...
81
  		printf STDERR "$filename:$lineno: " .
d52784eb3   akpm@linux-foundation.org   headers_check: Fi...
82
83
84
  			      "userspace cannot reference function or " .
  			      "variable defined in the kernel
  ";
46b8af50b   Mike Frysinger   headers_check.pl:...
85
86
  	}
  }
7e557a250   Sam Ravnborg   kbuild: check for...
87
88
89
  
  sub check_config
  {
1581c1ced   Robert P. J. Day   scripts/headers_c...
90
  	if ($line =~ m/[^a-zA-Z0-9_]+CONFIG_([a-zA-Z0-9_]+)[^a-zA-Z0-9_]/) {
7e557a250   Sam Ravnborg   kbuild: check for...
91
92
93
94
  		printf STDERR "$filename:$lineno: leaks CONFIG_$1 to userspace where it is not valid
  ";
  	}
  }
483b41218   Sam Ravnborg   kbuild: add check...
95
  my $linux_asm_types;
dbbe33e99   Stephen Hemminger   headers_check: fi...
96
  sub check_asm_types
483b41218   Sam Ravnborg   kbuild: add check...
97
  {
b67ff8ce1   Sam Ravnborg   kbuild: ignore a ...
98
99
100
  	if ($filename =~ /types.h|int-l64.h|int-ll64.h/o) {
  		return;
  	}
483b41218   Sam Ravnborg   kbuild: add check...
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
  	if ($lineno == 1) {
  		$linux_asm_types = 0;
  	} elsif ($linux_asm_types >= 1) {
  		return;
  	}
  	if ($line =~ m/^\s*#\s*include\s+<asm\/types.h>/) {
  		$linux_asm_types = 1;
  		printf STDERR "$filename:$lineno: " .
  		"include of <linux/types.h> is preferred over <asm/types.h>
  "
  		# Warn until headers are all fixed
  		#$ret = 1;
  	}
  }
  
  my $linux_types;
f75a8df3b   Bobby Powers   headers_check: re...
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
  my %import_stack = ();
  sub check_include_typesh
  {
  	my $path = $_[0];
  	my $import_path;
  
  	my $fh;
  	my @file_paths = ($path, $dir . "/" .  $path, dirname($filename) . "/" . $path);
  	for my $possible ( @file_paths ) {
  	    if (not $import_stack{$possible} and open($fh, '<', $possible)) {
  		$import_path = $possible;
  		$import_stack{$import_path} = 1;
  		last;
  	    }
  	}
  	if (eof $fh) {
  	    return;
  	}
  
  	my $line;
  	while ($line = <$fh>) {
  		if ($line =~ m/^\s*#\s*include\s+<linux\/types.h>/) {
  			$linux_types = 1;
  			last;
  		}
  		if (my $included = ($line =~ /^\s*#\s*include\s+[<"](\S+)[>"]/)[0]) {
  			check_include_typesh($included);
  		}
  	}
  	close $fh;
  	delete $import_stack{$import_path};
  }
483b41218   Sam Ravnborg   kbuild: add check...
149
150
  sub check_sizetypes
  {
b67ff8ce1   Sam Ravnborg   kbuild: ignore a ...
151
152
153
  	if ($filename =~ /types.h|int-l64.h|int-ll64.h/o) {
  		return;
  	}
483b41218   Sam Ravnborg   kbuild: add check...
154
155
156
157
158
159
160
161
162
  	if ($lineno == 1) {
  		$linux_types = 0;
  	} elsif ($linux_types >= 1) {
  		return;
  	}
  	if ($line =~ m/^\s*#\s*include\s+<linux\/types.h>/) {
  		$linux_types = 1;
  		return;
  	}
f75a8df3b   Bobby Powers   headers_check: re...
163
164
165
  	if (my $included = ($line =~ /^\s*#\s*include\s+[<"](\S+)[>"]/)[0]) {
  		check_include_typesh($included);
  	}
483b41218   Sam Ravnborg   kbuild: add check...
166
167
168
169
170
171
172
173
174
175
  	if ($line =~ m/__[us](8|16|32|64)\b/) {
  		printf STDERR "$filename:$lineno: " .
  		              "found __[us]{8,16,32,64} type " .
  		              "without #include <linux/types.h>
  ";
  		$linux_types = 2;
  		# Warn until headers are all fixed
  		#$ret = 1;
  	}
  }