| 1 |
#!/usr/bin/env perl |
| 2 |
|
| 3 |
#------------------------------------------------------------------- |
| 4 |
# Check header files and #include directives |
| 5 |
# |
| 6 |
# (1) include/*.h must not include pub_core_...h |
| 7 |
# (2) coregrind/pub_core_xyzzy.h may include pub_tool_xyzzy.h |
| 8 |
# other coregrind headers may not include pub_tool_xyzzy.h |
| 9 |
# (3) coregrind/ *.c must not include pub_tool_xyzzy.h |
| 10 |
# (4) tool *.[ch] files must not include pub_core_...h |
| 11 |
# (5) include pub_core/tool_clreq.h instead of valgrind.h except in tools' |
| 12 |
# export headers |
| 13 |
# (6) coregrind/ *.[ch] must not use tl_assert |
| 14 |
# (7) include/*.h and tool *.[ch] must not use vg_assert |
| 15 |
# (8) coregrind/ *.[ch] must not use VG_(tool_panic) |
| 16 |
# (9) include/*.h and tool *.[ch] must not use VG_(core_panic) |
| 17 |
# (10) *.S must unconditionally instantiate MARK_STACK_NO_EXEC |
| 18 |
# |
| 19 |
# There can be false positives as we don't really parse the source files. |
| 20 |
# Instead we only match regular expressions. |
| 21 |
#------------------------------------------------------------------- |
| 22 |
|
| 23 |
use strict; |
| 24 |
use warnings; |
| 25 |
use File::Basename; |
| 26 |
use Getopt::Long; |
| 27 |
|
| 28 |
my $this_script = basename($0); |
| 29 |
|
| 30 |
# The list of top-level directories is divided into three sets: |
| 31 |
# |
| 32 |
# (1) coregrind directories |
| 33 |
# (2) tool directories |
| 34 |
# (3) directories to ignore |
| 35 |
# |
| 36 |
# If a directory is found that does not belong to any of those sets, the |
| 37 |
# script will terminate unsuccessfully. |
| 38 |
|
| 39 |
my %coregrind_dirs = ( |
| 40 |
"include" => 1, |
| 41 |
"coregrind" => 1, |
| 42 |
); |
| 43 |
|
| 44 |
my %tool_dirs = ( |
| 45 |
"memcheck" => 1, |
| 46 |
"cachegrind" => 1, |
| 47 |
"callgrind" => 1, |
| 48 |
"helgrind", => 1, |
| 49 |
"drd" => 1, |
| 50 |
"massif" => 1, |
| 51 |
"dhat" => 1, |
| 52 |
"lackey" => 1, |
| 53 |
"none" => 1, |
| 54 |
"exp-bbv" => 1, |
| 55 |
"shared" => 1, |
| 56 |
); |
| 57 |
|
| 58 |
my %dirs_to_ignore = ( |
| 59 |
".deps" => 1, |
| 60 |
".git" => 1, |
| 61 |
".in_place" => 1, |
| 62 |
"Inst" => 1, # the nightly scripts creates this |
| 63 |
"VEX" => 1, |
| 64 |
"docs" => 1, |
| 65 |
"scripts" => 1, |
| 66 |
"auxprogs" => 1, |
| 67 |
"autom4te.cache" => 1, |
| 68 |
"m4" => 1, |
| 69 |
"nightly" => 1, |
| 70 |
"perf" => 1, |
| 71 |
"tests" => 1, |
| 72 |
"gdbserver_tests" => 1, |
| 73 |
"mpi" => 1, |
| 74 |
"solaris" => 1 |
| 75 |
); |
| 76 |
|
| 77 |
my %tool_export_header = ( |
| 78 |
"dhat/dhat.h" => 1, |
| 79 |
"drd/drd.h" => 1, |
| 80 |
"helgrind/helgrind.h" => 1, |
| 81 |
"memcheck/memcheck.h" => 1, |
| 82 |
"callgrind/callgrind.h" => 1, |
| 83 |
"cachegrind/cachegrind.h" => 1 |
| 84 |
); |
| 85 |
|
| 86 |
my $usage=<<EOF; |
| 87 |
USAGE |
| 88 |
|
| 89 |
$this_script |
| 90 |
|
| 91 |
[--debug] Debugging output |
| 92 |
|
| 93 |
dir ... Directories to process |
| 94 |
EOF |
| 95 |
|
| 96 |
my $debug = 0; |
| 97 |
my $num_errors = 0; |
| 98 |
|
| 99 |
&main; |
| 100 |
|
| 101 |
sub main { |
| 102 |
GetOptions( "debug" => \$debug ) || die $usage; |
| 103 |
|
| 104 |
my $argc = $#ARGV + 1; |
| 105 |
|
| 106 |
if ($argc < 1) { |
| 107 |
die $usage; |
| 108 |
} |
| 109 |
|
| 110 |
foreach my $dir (@ARGV) { |
| 111 |
process_dir(undef, $dir, 0); |
| 112 |
} |
| 113 |
|
| 114 |
my $rc = ($num_errors == 0) ? 0 : 1; |
| 115 |
exit $rc; |
| 116 |
} |
| 117 |
|
| 118 |
sub process_dir { |
| 119 |
my ($path, $dir, $depth) = @_; |
| 120 |
my $hdir; |
| 121 |
|
| 122 |
if ($depth == 0) { |
| 123 |
# The root directory is always processed |
| 124 |
} elsif ($depth == 1) { |
| 125 |
# Toplevel directories |
| 126 |
return if ($dirs_to_ignore{$dir}); |
| 127 |
|
| 128 |
if (! $tool_dirs{$dir} && ! $coregrind_dirs{$dir}) { |
| 129 |
die "Unknown directory '$dir'. Please update $this_script\n"; |
| 130 |
} |
| 131 |
} else { |
| 132 |
# Subdirectories |
| 133 |
return if ($dirs_to_ignore{$dir}); |
| 134 |
} |
| 135 |
|
| 136 |
print "DIR = $dir DEPTH = $depth\n" if ($debug); |
| 137 |
|
| 138 |
chdir($dir) || die "Cannot chdir '$dir'\n"; |
| 139 |
|
| 140 |
opendir($hdir, ".") || die "cannot open directory '.'"; |
| 141 |
|
| 142 |
while (my $file = readdir($hdir)) { |
| 143 |
next if ($file eq "."); |
| 144 |
next if ($file eq ".."); |
| 145 |
|
| 146 |
# Subdirectories |
| 147 |
if (-d $file) { |
| 148 |
my $full_path = defined $path ? "$path/$file" : $file; |
| 149 |
process_dir($full_path, $file, $depth + 1); |
| 150 |
next; |
| 151 |
} |
| 152 |
|
| 153 |
# Regular files; only interested in *.c, *.S and *.h |
| 154 |
next if (! ($file =~ /\.[cSh]$/)); |
| 155 |
my $path_name = defined $path ? "$path/$file" : $file; |
| 156 |
process_file($path_name); |
| 157 |
} |
| 158 |
close($hdir); |
| 159 |
chdir("..") || die "Cannot chdir '..'\n"; |
| 160 |
} |
| 161 |
|
| 162 |
#--------------------------------------------------------------------- |
| 163 |
# Return 1, if file is located in <valgrind>/include |
| 164 |
#--------------------------------------------------------------------- |
| 165 |
sub is_coregrind_export_header { |
| 166 |
my ($path_name) = @_; |
| 167 |
|
| 168 |
return ($path_name =~ /^include\//) ? 1 : 0; |
| 169 |
} |
| 170 |
|
| 171 |
#--------------------------------------------------------------------- |
| 172 |
# Return 1, if file is located underneath <valgrind>/coregrind |
| 173 |
#--------------------------------------------------------------------- |
| 174 |
sub is_coregrind_file { |
| 175 |
my ($path_name) = @_; |
| 176 |
|
| 177 |
return ($path_name =~ /^coregrind\//) ? 1 : 0; |
| 178 |
} |
| 179 |
|
| 180 |
#--------------------------------------------------------------------- |
| 181 |
# Return 1, if file is located underneath <valgrind>/<tool> |
| 182 |
#--------------------------------------------------------------------- |
| 183 |
sub is_tool_file { |
| 184 |
my ($path_name) = @_; |
| 185 |
|
| 186 |
for my $tool (keys %tool_dirs) { |
| 187 |
return 1 if ($path_name =~ /^$tool\//); |
| 188 |
} |
| 189 |
return 0 |
| 190 |
} |
| 191 |
|
| 192 |
#--------------------------------------------------------------------- |
| 193 |
# Return array of files #include'd by file. |
| 194 |
#--------------------------------------------------------------------- |
| 195 |
sub get_included_files { |
| 196 |
my ($path_name) = @_; |
| 197 |
my @includes = (); |
| 198 |
my $file = basename($path_name); |
| 199 |
|
| 200 |
open(FILE, "<$file") || die "Cannot open file '$file'"; |
| 201 |
|
| 202 |
while (my $line = <FILE>) { |
| 203 |
if ($line =~ /^\s*#\s*include "([^"]*)"/) { |
| 204 |
push @includes, $1; |
| 205 |
} |
| 206 |
if ($line =~ /^\s*#\s*include <([^>]*)>/) { |
| 207 |
push @includes, $1; |
| 208 |
} |
| 209 |
} |
| 210 |
close FILE; |
| 211 |
return @includes; |
| 212 |
} |
| 213 |
|
| 214 |
#--------------------------------------------------------------------- |
| 215 |
# Check a file from <valgrind>/include |
| 216 |
#--------------------------------------------------------------------- |
| 217 |
sub check_coregrind_export_header { |
| 218 |
my ($path_name) = @_; |
| 219 |
my $file = basename($path_name); |
| 220 |
|
| 221 |
foreach my $inc (get_included_files($path_name)) { |
| 222 |
$inc = basename($inc); |
| 223 |
# Must not include pub_core_.... |
| 224 |
if ($inc =~ /pub_core_/) { |
| 225 |
error("File $path_name must not include $inc\n"); |
| 226 |
} |
| 227 |
# Only pub_tool_clreq.h may include valgrind.h |
| 228 |
if (($inc eq "valgrind.h") && ($path_name ne "include/pub_tool_clreq.h")) { |
| 229 |
error("File $path_name should include pub_tool_clreq.h instead of $inc\n"); |
| 230 |
} |
| 231 |
} |
| 232 |
# Must not use vg_assert |
| 233 |
my $assert = `grep vg_assert $file`; |
| 234 |
if ($assert ne "") { |
| 235 |
error("File $path_name must not use vg_assert\n"); |
| 236 |
} |
| 237 |
# Must not use VG_(core_panic) |
| 238 |
my $panic = `grep 'VG_(core_panic)' $file`; |
| 239 |
if ($panic ne "") { |
| 240 |
error("File $path_name must not use VG_(core_panic)\n"); |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
#--------------------------------------------------------------------- |
| 245 |
# Check a file from <valgrind>/coregrind |
| 246 |
#--------------------------------------------------------------------- |
| 247 |
sub check_coregrind_file { |
| 248 |
my ($path_name) = @_; |
| 249 |
my $file = basename($path_name); |
| 250 |
|
| 251 |
foreach my $inc (get_included_files($path_name)) { |
| 252 |
print "\tINCLUDE $inc\n" if ($debug); |
| 253 |
# Only pub_tool_xyzzy.h may include pub_core_xyzzy.h |
| 254 |
if ($inc =~ /pub_tool_/) { |
| 255 |
my $buddy = $inc; |
| 256 |
$buddy =~ s/pub_tool/pub_core/; |
| 257 |
if ($file ne $buddy) { |
| 258 |
error("File $path_name must not include $inc\n"); |
| 259 |
} |
| 260 |
} |
| 261 |
# Must not include valgrind.h |
| 262 |
if ($inc eq "valgrind.h") { |
| 263 |
error("File $path_name should include pub_core_clreq.h instead of $inc\n"); |
| 264 |
} |
| 265 |
} |
| 266 |
# Must not use tl_assert |
| 267 |
my $assert = `grep tl_assert $file`; |
| 268 |
if ($assert ne "") { |
| 269 |
error("File $path_name must not use tl_assert\n"); |
| 270 |
} |
| 271 |
# Must not use VG_(tool_panic) |
| 272 |
my $panic = `grep 'VG_(tool_panic)' $file`; |
| 273 |
if ($panic ne "") { |
| 274 |
chomp($panic); |
| 275 |
# Do not complain about the definition of VG_(tool_panic) |
| 276 |
if (($path_name eq "coregrind/m_libcassert.c") && |
| 277 |
($panic eq "void VG_(tool_panic) ( const HChar* str )")) { |
| 278 |
# OK |
| 279 |
} else { |
| 280 |
error("File $path_name must not use VG_(tool_panic)\n"); |
| 281 |
} |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
#--------------------------------------------------------------------- |
| 286 |
# Check a file from <valgrind>/<tool> |
| 287 |
#--------------------------------------------------------------------- |
| 288 |
sub check_tool_file { |
| 289 |
my ($path_name) = @_; |
| 290 |
my $file = basename($path_name); |
| 291 |
|
| 292 |
foreach my $inc (get_included_files($path_name)) { |
| 293 |
print "\tINCLUDE $inc\n" if ($debug); |
| 294 |
# Must not include pub_core_... |
| 295 |
if ($inc =~ /pub_core_/) { |
| 296 |
error("File $path_name must not include $inc\n"); |
| 297 |
} |
| 298 |
# Must not include valgrind.h unless this is an export header |
| 299 |
if ($inc eq "valgrind.h" && ! $tool_export_header{$path_name}) { |
| 300 |
error("File $path_name should include pub_tool_clreq.h instead of $inc\n"); |
| 301 |
} |
| 302 |
} |
| 303 |
# Must not use vg_assert |
| 304 |
my $assert = `grep vg_assert $file`; |
| 305 |
if ($assert ne "") { |
| 306 |
error("File $path_name must not use vg_assert\n"); |
| 307 |
} |
| 308 |
# Must not use VG_(core_panic) |
| 309 |
my $panic = `grep 'VG_(core_panic)' $file`; |
| 310 |
if ($panic ne "") { |
| 311 |
error("File $path_name must not use VG_(core_panic)\n"); |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
#--------------------------------------------------------------------- |
| 316 |
# Check an assembler file |
| 317 |
#--------------------------------------------------------------------- |
| 318 |
sub check_assembler_file { |
| 319 |
my ($path_name) = @_; |
| 320 |
my $file = basename($path_name); |
| 321 |
my $found = 0; |
| 322 |
|
| 323 |
open(FILE, "<$file") || die "Cannot open file '$file'"; |
| 324 |
|
| 325 |
while (my $line = <FILE>) { |
| 326 |
if ($line =~ /^\s*MARK_STACK_NO_EXEC/) { |
| 327 |
$found = 1; |
| 328 |
last; |
| 329 |
} |
| 330 |
} |
| 331 |
if ($found == 0) { |
| 332 |
error("File $path_name does not instantiate MARK_STACK_NO_EXEC\n"); |
| 333 |
} else { |
| 334 |
while (my $line = <FILE>) { |
| 335 |
if ($line =~ /^\s*#\s*endif/) { |
| 336 |
error("File $path_name instantiates MARK_STACK_NO_EXEC" |
| 337 |
. " under a condition\n"); |
| 338 |
last; |
| 339 |
} |
| 340 |
} |
| 341 |
} |
| 342 |
close FILE; |
| 343 |
} |
| 344 |
|
| 345 |
sub process_file { |
| 346 |
my ($path_name) = @_; |
| 347 |
|
| 348 |
print "FILE = $path_name\n" if ($debug); |
| 349 |
|
| 350 |
if (is_coregrind_export_header($path_name)) { |
| 351 |
check_coregrind_export_header($path_name); |
| 352 |
} elsif (is_coregrind_file($path_name)) { |
| 353 |
check_coregrind_file($path_name); |
| 354 |
} elsif (is_tool_file($path_name)) { |
| 355 |
check_tool_file($path_name); |
| 356 |
} |
| 357 |
|
| 358 |
if ($path_name =~ /\.S$/) { |
| 359 |
check_assembler_file($path_name); |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
sub error { |
| 364 |
my ($message) = @_; |
| 365 |
print STDERR "*** $message"; |
| 366 |
++$num_errors; |
| 367 |
} |