| 1 |
# Written by Zack Weinberg <zackw at panix.com> in 2020. |
| 2 |
# To the extent possible under law, Zack Weinberg has waived all |
| 3 |
# copyright and related or neighboring rights to this work. |
| 4 |
# |
| 5 |
# See https://creativecommons.org/publicdomain/zero/1.0/ for further |
| 6 |
# details. |
| 7 |
|
| 8 |
# Code shared among all of the Perl-language tests in this directory. |
| 9 |
|
| 10 |
package TestCommon; |
| 11 |
|
| 12 |
use v5.14; # implicit use strict, use feature ':5.14' |
| 13 |
use warnings FATAL => 'all'; |
| 14 |
use utf8; |
| 15 |
use open qw(:utf8); |
| 16 |
|
| 17 |
no if $] >= 5.022, warnings => 'experimental::re_strict'; |
| 18 |
use if $] >= 5.022, re => 'strict'; |
| 19 |
|
| 20 |
use Cwd qw(realpath); |
| 21 |
use File::Spec::Functions qw( |
| 22 |
catdir |
| 23 |
catpath |
| 24 |
splitpath |
| 25 |
); |
| 26 |
use FindBin (); |
| 27 |
use POSIX (); |
| 28 |
|
| 29 |
use lib "$FindBin::Bin/../build-aux/scripts"; |
| 30 |
## ProhibitUnusedImport does not notice uses from @EXPORT_OK. |
| 31 |
## no critic (TooMuchCode::ProhibitUnusedImport) |
| 32 |
use BuildCommon qw( |
| 33 |
ensure_C_locale |
| 34 |
error |
| 35 |
popen |
| 36 |
sh_split |
| 37 |
sh_quote |
| 38 |
subprocess_error |
| 39 |
which |
| 40 |
); |
| 41 |
## use critic |
| 42 |
|
| 43 |
our @EXPORT_OK; |
| 44 |
use Exporter qw(import); |
| 45 |
|
| 46 |
BEGIN { |
| 47 |
# Re-export all the subprocess handling routines from BuildCommon |
| 48 |
# as a convenience for individual tests. |
| 49 |
@EXPORT_OK = qw( |
| 50 |
compare_symbol_lists |
| 51 |
ensure_C_locale |
| 52 |
error |
| 53 |
fail |
| 54 |
find_real_library |
| 55 |
get_symbols |
| 56 |
popen |
| 57 |
sh_quote |
| 58 |
sh_split |
| 59 |
skip |
| 60 |
subprocess_error |
| 61 |
which |
| 62 |
); |
| 63 |
} |
| 64 |
|
| 65 |
# Diagnostics: report that the test has failed. |
| 66 |
sub fail { ## no critic (Subroutines::RequireArgUnpacking) |
| 67 |
my $msg = join q{ }, @_; |
| 68 |
print {*STDERR} $FindBin::Script, ': FAIL: ', $msg, "\n"; |
| 69 |
exit 1; |
| 70 |
} |
| 71 |
|
| 72 |
# Diagnostics: report that the test should be 'skipped' because |
| 73 |
# some piece of infrastructure we need is missing. |
| 74 |
sub skip { ## no critic (Subroutines::RequireArgUnpacking) |
| 75 |
my $msg = join q{ }, @_; |
| 76 |
print {*STDERR} $FindBin::Script, ': skipping test: ', $msg, "\n"; |
| 77 |
exit 77; |
| 78 |
} |
| 79 |
|
| 80 |
# Parse a .la file (arg 1) and determine the name of the actual .a or |
| 81 |
# .so file it refers to (arg 2: 'static' for .a, 'shared' for .so) |
| 82 |
sub find_real_library { |
| 83 |
my ($lib_la, $type) = @_; |
| 84 |
|
| 85 |
state @SH; |
| 86 |
if (!@SH) { |
| 87 |
@SH = which($ENV{SHELL} || $ENV{CONFIG_SHELL} || '/bin/sh'); |
| 88 |
error('no shell available???') if !@SH; |
| 89 |
} |
| 90 |
|
| 91 |
my $param; |
| 92 |
if ($type eq 'shared') { |
| 93 |
$param = 'dlname'; |
| 94 |
} elsif ($type eq 'static') { |
| 95 |
$param = 'old_library'; |
| 96 |
} else { |
| 97 |
error("unknown library type: '$type'"); |
| 98 |
} |
| 99 |
|
| 100 |
# We're going to interpolate $lib_la into a shell command. |
| 101 |
# Save the unmangled directory part first, then quote it. |
| 102 |
my ($vol, $dir, undef) = splitpath($lib_la); |
| 103 |
$lib_la = sh_quote($lib_la); |
| 104 |
|
| 105 |
# .la files are shell script fragments. The easiest way to learn |
| 106 |
# the name of the actual library is to ask a shell to parse the |
| 107 |
# fragment for us. |
| 108 |
my $fh = popen('-|', @SH, '-c', ". $lib_la; printf %s \"\$$param\""); |
| 109 |
my $real_library; |
| 110 |
{ |
| 111 |
local $/ = undef; # slurp |
| 112 |
$real_library = <$fh>; |
| 113 |
} |
| 114 |
close $fh or subprocess_error($SH[0]); |
| 115 |
|
| 116 |
chomp $real_library; |
| 117 |
$real_library = catpath($vol, catdir($dir, '.libs'), $real_library); |
| 118 |
error("'$real_library' does not exist") unless -f $real_library; |
| 119 |
return realpath($real_library); |
| 120 |
} |
| 121 |
|
| 122 |
# In some object file formats, all symbols defined in C have an |
| 123 |
# underscore prepended to their names. The configure script detects |
| 124 |
# this and the Makefiles set this environment variable appropriately. |
| 125 |
my $symbol_prefix = $ENV{SYMBOL_PREFIX} || q{}; |
| 126 |
|
| 127 |
# Return a hashset of symbols exported by the library $_[0], using readelf. |
| 128 |
# If it is a dynamic library, annotate each symbol with its version tag. |
| 129 |
sub get_symbols_readelf { |
| 130 |
my $lib = shift; |
| 131 |
my $filter = shift // sub { 1 }; |
| 132 |
|
| 133 |
state $readelf_works = 1; |
| 134 |
die "readelf doesn't work\n" unless $readelf_works; |
| 135 |
|
| 136 |
state @READELF; |
| 137 |
if (!@READELF) { |
| 138 |
@READELF = which($ENV{READELF} || 'readelf'); |
| 139 |
die "readelf not available\n" unless @READELF; |
| 140 |
} |
| 141 |
|
| 142 |
my @opts = ('--wide'); |
| 143 |
my $want_version_tags = 0; |
| 144 |
if ($lib =~ /\.(?:a|lib)$/) { |
| 145 |
push @opts, '--syms'; |
| 146 |
} else { |
| 147 |
push @opts, '--dyn-syms'; |
| 148 |
$want_version_tags = 1; |
| 149 |
} |
| 150 |
|
| 151 |
my $fh = popen('-|', @READELF, @opts, $lib); |
| 152 |
|
| 153 |
local $_; |
| 154 |
my %symbols; |
| 155 |
my $saw_version_tags = 0; |
| 156 |
while (<$fh>) { |
| 157 |
chomp; |
| 158 |
s/\s+$//; |
| 159 |
next if /^(?:$|File:|Symbol table)/; |
| 160 |
next if /^\s*Num:\s+Value\s+Size\s+Type\s+Bind\s+Vis\s+Ndx\s+Name$/; |
| 161 |
|
| 162 |
my ($num, $value, $size, $type, $bind, $vis, $ndx, $name) = split; |
| 163 |
|
| 164 |
# We are only interested in globally visible, defined, |
| 165 |
# non-absolute symbols. |
| 166 |
next |
| 167 |
if $ndx eq 'UND' |
| 168 |
|| $ndx eq 'ABS' |
| 169 |
|| $bind eq 'LOCAL'; |
| 170 |
|
| 171 |
# Strip the symbol prefix, if any, from each symbol. |
| 172 |
$name =~ s/^$symbol_prefix// if $symbol_prefix ne q{}; |
| 173 |
|
| 174 |
$saw_version_tags = 1 if $name =~ /@[A-Z_]+[0-9]/; |
| 175 |
|
| 176 |
if (&{$filter}($name)) { |
| 177 |
print {*STDERR} "|+ $name\n"; |
| 178 |
$symbols{$name} = 1; |
| 179 |
} else { |
| 180 |
print {*STDERR} "|- $name\n"; |
| 181 |
} |
| 182 |
} |
| 183 |
if (!close $fh) { |
| 184 |
# If it ran but exited 1 or 2, don't give up yet, we still |
| 185 |
# have nm to try. |
| 186 |
if ($! == 0 && ($? == 256 || $? == 512)) { |
| 187 |
$readelf_works = 0; |
| 188 |
die "$READELF[0] exited " . ($? >> 2) . "\n"; |
| 189 |
} |
| 190 |
subprocess_error($READELF[0]); |
| 191 |
} |
| 192 |
if ($want_version_tags && !$saw_version_tags) { |
| 193 |
$readelf_works = 0; |
| 194 |
die "$READELF[0] did not print version tags\n"; |
| 195 |
} |
| 196 |
return \%symbols; |
| 197 |
} |
| 198 |
|
| 199 |
# Return a hashset of symbols exported by the library $_[0], using nm. |
| 200 |
# If it is a dynamic library, annotate each symbol with its version tag. |
| 201 |
sub get_symbols_nm { |
| 202 |
my $lib = shift; |
| 203 |
my $filter = shift // sub { 1 }; |
| 204 |
|
| 205 |
state $nm_works = 1; |
| 206 |
die "nm doesn't work\n" unless $nm_works; |
| 207 |
|
| 208 |
state @NM; |
| 209 |
if (!@NM) { |
| 210 |
@NM = which($ENV{NM} || 'nm'); |
| 211 |
die "nm not available\n" unless @NM; |
| 212 |
} |
| 213 |
|
| 214 |
my @opts = qw(--format=bsd --extern-only --defined-only); |
| 215 |
my $want_version_tags = 0; |
| 216 |
if ($lib !~ /\.(?:a|lib)$/) { |
| 217 |
push @opts, qw(--dynamic --with-symbol-versions); |
| 218 |
$want_version_tags = 1; |
| 219 |
} |
| 220 |
|
| 221 |
my $fh = popen('-|', @NM, @opts, $lib); |
| 222 |
local $_; |
| 223 |
my %symbols; |
| 224 |
my $saw_version_tags = 0; |
| 225 |
while (<$fh>) { |
| 226 |
chomp; |
| 227 |
s/\s+$//; |
| 228 |
next unless $_; |
| 229 |
|
| 230 |
# BSD-format nm output, when restricted to external, defined |
| 231 |
# symbols, has three fields per line: address type name. |
| 232 |
# We shouldn't ever see symbols with the address field blank, |
| 233 |
# but just in case, discard them. |
| 234 |
next unless /^([0-9a-fA-F]+)\s+([A-Za-z])\s+(\S+)$/; |
| 235 |
my $addr = $1; |
| 236 |
my $type = $2; |
| 237 |
my $name = $3; |
| 238 |
|
| 239 |
# Symbols whose address is 0 and type is A are uninteresting; |
| 240 |
# they define the set of symbol version tags. |
| 241 |
next if $addr =~ /^0+$/ && $type eq 'A'; |
| 242 |
|
| 243 |
# Strip the symbol prefix, if any, from each symbol. |
| 244 |
$name =~ s/^$symbol_prefix// if $symbol_prefix; |
| 245 |
|
| 246 |
# Compensate for a bug in some versions of GNU nm |
| 247 |
# where the symbol version is printed twice. |
| 248 |
$name =~ s/(@+[A-Z0-9_.]+)\1$/$1/; |
| 249 |
|
| 250 |
$saw_version_tags = 1 if $name =~ /@[A-Z_]+[0-9]/; |
| 251 |
|
| 252 |
if (&{$filter}($name)) { |
| 253 |
print {*STDERR} "|+ $name\n"; |
| 254 |
$symbols{$name} = 1; |
| 255 |
} else { |
| 256 |
print {*STDERR} "|- $name\n"; |
| 257 |
} |
| 258 |
} |
| 259 |
if (!close $fh) { |
| 260 |
# If it ran but exited 1 or 2, don't give up yet, we still |
| 261 |
# have readelf to try. |
| 262 |
if ($! == 0 && ($? == 256 || $? == 512)) { |
| 263 |
$nm_works = 0; |
| 264 |
die "$NM[0] exited " . ($? >> 8) . "\n"; |
| 265 |
} |
| 266 |
subprocess_error($NM[0]); |
| 267 |
} |
| 268 |
if ($want_version_tags && !$saw_version_tags) { |
| 269 |
$nm_works = 0; |
| 270 |
die "$NM[0] did not print version tags\n"; |
| 271 |
} |
| 272 |
return \%symbols; |
| 273 |
} |
| 274 |
|
| 275 |
# Return a hashset of symbols exported by the library $_[0], using |
| 276 |
# readelf or nm, whichever works on this system. If it is a dynamic |
| 277 |
# library, annotate each symbol with its version tag. If $_[1] is |
| 278 |
# defined, it is a filter procedure; only symbols for which the filter |
| 279 |
# returns true are included in the hashset. |
| 280 |
sub get_symbols { ## no critic (Subroutines::RequireArgUnpacking) |
| 281 |
my $result; |
| 282 |
|
| 283 |
$result = eval { get_symbols_nm(@_); }; |
| 284 |
return $result if $result; |
| 285 |
print {*STDERR} "get_symbols_nm: $@"; |
| 286 |
|
| 287 |
$result = eval { get_symbols_readelf(@_); }; |
| 288 |
return $result if $result; |
| 289 |
print {*STDERR} "get_symbols_readelf: $@"; |
| 290 |
|
| 291 |
skip('cannot get symbols using either readelf or nm'); |
| 292 |
} |
| 293 |
|
| 294 |
sub compare_symbol_lists { |
| 295 |
my ($found, $expected, $tag, $extra_allowed) = @_; |
| 296 |
my @extra; |
| 297 |
my @missing; |
| 298 |
local $_; |
| 299 |
for (keys %{$expected}) { |
| 300 |
push @missing, $_ unless exists $found->{$_}; |
| 301 |
} |
| 302 |
for (keys %{$found}) { |
| 303 |
push @extra, $_ unless exists $expected->{$_}; |
| 304 |
} |
| 305 |
|
| 306 |
my $error = 0; |
| 307 |
if (@extra) { |
| 308 |
$error = 1 unless $extra_allowed; |
| 309 |
print {*STDERR} "*** Extra $tag:\n"; |
| 310 |
for (sort @extra) { |
| 311 |
s/^_crypt_//; |
| 312 |
print {*STDERR} " $_\n"; |
| 313 |
} |
| 314 |
} |
| 315 |
if (@missing) { |
| 316 |
$error = 1; |
| 317 |
print {*STDERR} "*** Missing $tag:\n"; |
| 318 |
for (sort @missing) { |
| 319 |
s/^_crypt_//; |
| 320 |
print {*STDERR} " $_\n"; |
| 321 |
} |
| 322 |
} |
| 323 |
return $error; |
| 324 |
} |
| 325 |
|
| 326 |
1; |