headers_check.pl 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #!/usr/bin/perl -w
  2. #
  3. # headers_check.pl execute a number of trivial consistency checks
  4. #
  5. # Usage: headers_check.pl dir arch [files...]
  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. #
  17. # 2) It is checked that prototypes does not use "extern"
  18. #
  19. # 3) Check for leaked CONFIG_ symbols
  20. use strict;
  21. my ($dir, $arch, @files) = @ARGV;
  22. my $ret = 0;
  23. my $line;
  24. my $lineno = 0;
  25. my $filename;
  26. foreach my $file (@files) {
  27. $filename = $file;
  28. open(my $fh, '<', $filename)
  29. or die "$filename: $!\n";
  30. $lineno = 0;
  31. while ($line = <$fh>) {
  32. $lineno++;
  33. &check_include();
  34. &check_asm_types();
  35. &check_sizetypes();
  36. &check_declarations();
  37. # Dropped for now. Too much noise &check_config();
  38. }
  39. close $fh;
  40. }
  41. exit $ret;
  42. sub check_include
  43. {
  44. if ($line =~ m/^\s*#\s*include\s+<((asm|linux).*)>/) {
  45. my $inc = $1;
  46. my $found;
  47. $found = stat($dir . "/" . $inc);
  48. if (!$found) {
  49. $inc =~ s#asm/#asm-$arch/#;
  50. $found = stat($dir . "/" . $inc);
  51. }
  52. if (!$found) {
  53. printf STDERR "$filename:$lineno: included file '$inc' is not exported\n";
  54. $ret = 1;
  55. }
  56. }
  57. }
  58. sub check_declarations
  59. {
  60. if ($line =~m/^(\s*extern|unsigned|char|short|int|long|void)\b/) {
  61. printf STDERR "$filename:$lineno: " .
  62. "userspace cannot reference function or " .
  63. "variable defined in the kernel\n";
  64. }
  65. }
  66. sub check_config
  67. {
  68. if ($line =~ m/[^a-zA-Z0-9_]+CONFIG_([a-zA-Z0-9_]+)[^a-zA-Z0-9_]/) {
  69. printf STDERR "$filename:$lineno: leaks CONFIG_$1 to userspace where it is not valid\n";
  70. }
  71. }
  72. my $linux_asm_types;
  73. sub check_asm_types
  74. {
  75. if ($filename =~ /types.h|int-l64.h|int-ll64.h/o) {
  76. return;
  77. }
  78. if ($lineno == 1) {
  79. $linux_asm_types = 0;
  80. } elsif ($linux_asm_types >= 1) {
  81. return;
  82. }
  83. if ($line =~ m/^\s*#\s*include\s+<asm\/types.h>/) {
  84. $linux_asm_types = 1;
  85. printf STDERR "$filename:$lineno: " .
  86. "include of <linux/types.h> is preferred over <asm/types.h>\n"
  87. # Warn until headers are all fixed
  88. #$ret = 1;
  89. }
  90. }
  91. my $linux_types;
  92. sub check_sizetypes
  93. {
  94. if ($filename =~ /types.h|int-l64.h|int-ll64.h/o) {
  95. return;
  96. }
  97. if ($lineno == 1) {
  98. $linux_types = 0;
  99. } elsif ($linux_types >= 1) {
  100. return;
  101. }
  102. if ($line =~ m/^\s*#\s*include\s+<linux\/types.h>/) {
  103. $linux_types = 1;
  104. return;
  105. }
  106. if ($line =~ m/__[us](8|16|32|64)\b/) {
  107. printf STDERR "$filename:$lineno: " .
  108. "found __[us]{8,16,32,64} type " .
  109. "without #include <linux/types.h>\n";
  110. $linux_types = 2;
  111. # Warn until headers are all fixed
  112. #$ret = 1;
  113. }
  114. }