blob: b6aec5e4365f9bf2b58c615fd3c7e591da5c20b6 [file] [log] [blame]
Kamil Rytarowskicb77f0d2017-05-07 23:25:26 +02001#!/usr/bin/env perl
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01002# SPDX-License-Identifier: GPL-2.0
Sam Ravnborg77124012008-06-15 21:41:09 +02003#
4# headers_check.pl execute a number of trivial consistency checks
5#
Amerigo Wang67b7ebe2009-06-04 22:12:01 -04006# Usage: headers_check.pl dir arch [files...]
Sam Ravnborg77124012008-06-15 21:41:09 +02007# dir: dir to look for included files
8# arch: architecture
9# files: list of files to check
10#
11# The script reads the supplied files line by line and:
12#
13# 1) for each include statement it checks if the
14# included file actually exists.
15# Only include files located in asm* and linux* are checked.
16# The rest are assumed to be system include files.
17#
Mike Frysinger46b8af52008-12-27 02:43:36 -050018# 2) It is checked that prototypes does not use "extern"
19#
Sam Ravnborg7e557a22008-12-27 19:52:20 +010020# 3) Check for leaked CONFIG_ symbols
Sam Ravnborg77124012008-06-15 21:41:09 +020021
Kamil Rytarowskicb77f0d2017-05-07 23:25:26 +020022use warnings;
Sam Ravnborg77124012008-06-15 21:41:09 +020023use strict;
Bobby Powersf75a8df2012-03-05 15:08:09 -080024use File::Basename;
Sam Ravnborg77124012008-06-15 21:41:09 +020025
26my ($dir, $arch, @files) = @ARGV;
27
28my $ret = 0;
29my $line;
30my $lineno = 0;
31my $filename;
32
33foreach my $file (@files) {
34 $filename = $file;
Stephen Hemmingerdbbe33e2010-02-22 15:17:24 -080035
36 open(my $fh, '<', $filename)
37 or die "$filename: $!\n";
Sam Ravnborg77124012008-06-15 21:41:09 +020038 $lineno = 0;
Stephen Hemmingerdbbe33e2010-02-22 15:17:24 -080039 while ($line = <$fh>) {
Sam Ravnborg77124012008-06-15 21:41:09 +020040 $lineno++;
Sam Ravnborg483b4122008-12-30 11:34:58 +010041 &check_include();
42 &check_asm_types();
43 &check_sizetypes();
Amerigo Wang67b7ebe2009-06-04 22:12:01 -040044 &check_declarations();
Sam Ravnborg7e3fa562009-01-30 23:56:42 +010045 # Dropped for now. Too much noise &check_config();
Sam Ravnborg77124012008-06-15 21:41:09 +020046 }
Stephen Hemmingerdbbe33e2010-02-22 15:17:24 -080047 close $fh;
Sam Ravnborg77124012008-06-15 21:41:09 +020048}
49exit $ret;
50
51sub check_include
52{
53 if ($line =~ m/^\s*#\s*include\s+<((asm|linux).*)>/) {
54 my $inc = $1;
55 my $found;
56 $found = stat($dir . "/" . $inc);
57 if (!$found) {
58 $inc =~ s#asm/#asm-$arch/#;
59 $found = stat($dir . "/" . $inc);
60 }
61 if (!$found) {
62 printf STDERR "$filename:$lineno: included file '$inc' is not exported\n";
63 $ret = 1;
64 }
65 }
66}
Mike Frysinger46b8af52008-12-27 02:43:36 -050067
Amerigo Wang67b7ebe2009-06-04 22:12:01 -040068sub check_declarations
Mike Frysinger46b8af52008-12-27 02:43:36 -050069{
Paul Bollea7e1d982014-01-23 15:54:08 -080070 # soundcard.h is what it is
71 if ($line =~ m/^void seqbuf_dump\(void\);/) {
72 return;
73 }
Arnd Bergmann92181d42016-05-18 18:07:29 +020074 # drm headers are being C++ friendly
75 if ($line =~ m/^extern "C"/) {
76 return;
77 }
Paul Bollea7e1d982014-01-23 15:54:08 -080078 if ($line =~ m/^(\s*extern|unsigned|char|short|int|long|void)\b/) {
Amerigo Wang67b7ebe2009-06-04 22:12:01 -040079 printf STDERR "$filename:$lineno: " .
akpm@linux-foundation.orgd52784e2010-11-30 13:52:14 -080080 "userspace cannot reference function or " .
81 "variable defined in the kernel\n";
Mike Frysinger46b8af52008-12-27 02:43:36 -050082 }
83}
Sam Ravnborg7e557a22008-12-27 19:52:20 +010084
85sub check_config
86{
Robert P. J. Day1581c1c2009-05-12 13:43:36 -070087 if ($line =~ m/[^a-zA-Z0-9_]+CONFIG_([a-zA-Z0-9_]+)[^a-zA-Z0-9_]/) {
Sam Ravnborg7e557a22008-12-27 19:52:20 +010088 printf STDERR "$filename:$lineno: leaks CONFIG_$1 to userspace where it is not valid\n";
89 }
90}
91
Sam Ravnborg483b4122008-12-30 11:34:58 +010092my $linux_asm_types;
Stephen Hemmingerdbbe33e2010-02-22 15:17:24 -080093sub check_asm_types
Sam Ravnborg483b4122008-12-30 11:34:58 +010094{
Sam Ravnborgb67ff8c2008-12-31 09:32:30 +010095 if ($filename =~ /types.h|int-l64.h|int-ll64.h/o) {
96 return;
97 }
Sam Ravnborg483b4122008-12-30 11:34:58 +010098 if ($lineno == 1) {
99 $linux_asm_types = 0;
100 } elsif ($linux_asm_types >= 1) {
101 return;
102 }
103 if ($line =~ m/^\s*#\s*include\s+<asm\/types.h>/) {
104 $linux_asm_types = 1;
105 printf STDERR "$filename:$lineno: " .
106 "include of <linux/types.h> is preferred over <asm/types.h>\n"
107 # Warn until headers are all fixed
108 #$ret = 1;
109 }
110}
111
112my $linux_types;
Bobby Powersf75a8df2012-03-05 15:08:09 -0800113my %import_stack = ();
114sub check_include_typesh
115{
116 my $path = $_[0];
117 my $import_path;
118
119 my $fh;
120 my @file_paths = ($path, $dir . "/" . $path, dirname($filename) . "/" . $path);
121 for my $possible ( @file_paths ) {
122 if (not $import_stack{$possible} and open($fh, '<', $possible)) {
123 $import_path = $possible;
124 $import_stack{$import_path} = 1;
125 last;
126 }
127 }
128 if (eof $fh) {
129 return;
130 }
131
132 my $line;
133 while ($line = <$fh>) {
134 if ($line =~ m/^\s*#\s*include\s+<linux\/types.h>/) {
135 $linux_types = 1;
136 last;
137 }
138 if (my $included = ($line =~ /^\s*#\s*include\s+[<"](\S+)[>"]/)[0]) {
139 check_include_typesh($included);
140 }
141 }
142 close $fh;
143 delete $import_stack{$import_path};
144}
145
Sam Ravnborg483b4122008-12-30 11:34:58 +0100146sub check_sizetypes
147{
Sam Ravnborgb67ff8c2008-12-31 09:32:30 +0100148 if ($filename =~ /types.h|int-l64.h|int-ll64.h/o) {
149 return;
150 }
Sam Ravnborg483b4122008-12-30 11:34:58 +0100151 if ($lineno == 1) {
152 $linux_types = 0;
153 } elsif ($linux_types >= 1) {
154 return;
155 }
156 if ($line =~ m/^\s*#\s*include\s+<linux\/types.h>/) {
157 $linux_types = 1;
158 return;
159 }
Bobby Powersf75a8df2012-03-05 15:08:09 -0800160 if (my $included = ($line =~ /^\s*#\s*include\s+[<"](\S+)[>"]/)[0]) {
161 check_include_typesh($included);
162 }
Sam Ravnborg483b4122008-12-30 11:34:58 +0100163 if ($line =~ m/__[us](8|16|32|64)\b/) {
164 printf STDERR "$filename:$lineno: " .
165 "found __[us]{8,16,32,64} type " .
166 "without #include <linux/types.h>\n";
167 $linux_types = 2;
168 # Warn until headers are all fixed
169 #$ret = 1;
170 }
171}