blob: 3091e4ee6ee1329d6224266ae788f4776f8fe073 [file] [log] [blame]
Kamil Rytarowskicb77f0d2017-05-07 23:25:26 +02001#!/usr/bin/env perl
Sam Ravnborg77124012008-06-15 21:41:09 +02002#
3# headers_check.pl execute a number of trivial consistency checks
4#
Amerigo Wang67b7ebe2009-06-04 22:12:01 -04005# Usage: headers_check.pl dir arch [files...]
Sam Ravnborg77124012008-06-15 21:41:09 +02006# 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 Frysinger46b8af52008-12-27 02:43:36 -050017# 2) It is checked that prototypes does not use "extern"
18#
Sam Ravnborg7e557a22008-12-27 19:52:20 +010019# 3) Check for leaked CONFIG_ symbols
Sam Ravnborg77124012008-06-15 21:41:09 +020020
Kamil Rytarowskicb77f0d2017-05-07 23:25:26 +020021use warnings;
Sam Ravnborg77124012008-06-15 21:41:09 +020022use strict;
Bobby Powersf75a8df2012-03-05 15:08:09 -080023use File::Basename;
Sam Ravnborg77124012008-06-15 21:41:09 +020024
25my ($dir, $arch, @files) = @ARGV;
26
27my $ret = 0;
28my $line;
29my $lineno = 0;
30my $filename;
31
32foreach my $file (@files) {
33 $filename = $file;
Stephen Hemmingerdbbe33e2010-02-22 15:17:24 -080034
35 open(my $fh, '<', $filename)
36 or die "$filename: $!\n";
Sam Ravnborg77124012008-06-15 21:41:09 +020037 $lineno = 0;
Stephen Hemmingerdbbe33e2010-02-22 15:17:24 -080038 while ($line = <$fh>) {
Sam Ravnborg77124012008-06-15 21:41:09 +020039 $lineno++;
Sam Ravnborg483b4122008-12-30 11:34:58 +010040 &check_include();
41 &check_asm_types();
42 &check_sizetypes();
Amerigo Wang67b7ebe2009-06-04 22:12:01 -040043 &check_declarations();
Sam Ravnborg7e3fa562009-01-30 23:56:42 +010044 # Dropped for now. Too much noise &check_config();
Sam Ravnborg77124012008-06-15 21:41:09 +020045 }
Stephen Hemmingerdbbe33e2010-02-22 15:17:24 -080046 close $fh;
Sam Ravnborg77124012008-06-15 21:41:09 +020047}
48exit $ret;
49
50sub 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 Frysinger46b8af52008-12-27 02:43:36 -050066
Amerigo Wang67b7ebe2009-06-04 22:12:01 -040067sub check_declarations
Mike Frysinger46b8af52008-12-27 02:43:36 -050068{
Paul Bollea7e1d982014-01-23 15:54:08 -080069 # soundcard.h is what it is
70 if ($line =~ m/^void seqbuf_dump\(void\);/) {
71 return;
72 }
Arnd Bergmann92181d42016-05-18 18:07:29 +020073 # drm headers are being C++ friendly
74 if ($line =~ m/^extern "C"/) {
75 return;
76 }
Paul Bollea7e1d982014-01-23 15:54:08 -080077 if ($line =~ m/^(\s*extern|unsigned|char|short|int|long|void)\b/) {
Amerigo Wang67b7ebe2009-06-04 22:12:01 -040078 printf STDERR "$filename:$lineno: " .
akpm@linux-foundation.orgd52784e2010-11-30 13:52:14 -080079 "userspace cannot reference function or " .
80 "variable defined in the kernel\n";
Mike Frysinger46b8af52008-12-27 02:43:36 -050081 }
82}
Sam Ravnborg7e557a22008-12-27 19:52:20 +010083
84sub check_config
85{
Robert P. J. Day1581c1c2009-05-12 13:43:36 -070086 if ($line =~ m/[^a-zA-Z0-9_]+CONFIG_([a-zA-Z0-9_]+)[^a-zA-Z0-9_]/) {
Sam Ravnborg7e557a22008-12-27 19:52:20 +010087 printf STDERR "$filename:$lineno: leaks CONFIG_$1 to userspace where it is not valid\n";
88 }
89}
90
Sam Ravnborg483b4122008-12-30 11:34:58 +010091my $linux_asm_types;
Stephen Hemmingerdbbe33e2010-02-22 15:17:24 -080092sub check_asm_types
Sam Ravnborg483b4122008-12-30 11:34:58 +010093{
Sam Ravnborgb67ff8c2008-12-31 09:32:30 +010094 if ($filename =~ /types.h|int-l64.h|int-ll64.h/o) {
95 return;
96 }
Sam Ravnborg483b4122008-12-30 11:34:58 +010097 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
111my $linux_types;
Bobby Powersf75a8df2012-03-05 15:08:09 -0800112my %import_stack = ();
113sub 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 Ravnborg483b4122008-12-30 11:34:58 +0100145sub check_sizetypes
146{
Sam Ravnborgb67ff8c2008-12-31 09:32:30 +0100147 if ($filename =~ /types.h|int-l64.h|int-ll64.h/o) {
148 return;
149 }
Sam Ravnborg483b4122008-12-30 11:34:58 +0100150 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 Powersf75a8df2012-03-05 15:08:09 -0800159 if (my $included = ($line =~ /^\s*#\s*include\s+[<"](\S+)[>"]/)[0]) {
160 check_include_typesh($included);
161 }
Sam Ravnborg483b4122008-12-30 11:34:58 +0100162 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}