Debian Codegraph @

test/symbols-compat.pl

raw
1 #! /usr/bin/perl
2 # Written by Zack Weinberg <zackw at panix.com> in 2017 and 2020.
3 # To the extent possible under law, Zack Weinberg has waived all
4 # copyright and related or neighboring rights to this work.
5 #
6 # See https://creativecommons.org/publicdomain/zero/1.0/ for further
7 # details.
8
9 # This test is only run if we are building a shared library intended
10 # to be binary backward compatible with GNU libc (libcrypt.so.1).
11 # It locates any installed version of libcrypt.so.1, and verifies that
12 # each public symbol exposed by that library is also exposed by our
13 # libcrypt.so.1 with a matching symbol version.
14 #
15 # Due to limitations in Automake, this program takes parameters from
16 # the environment:
17 # $lib_la - full pathname of libcrypt.la
18 # $SYMBOL_PREFIX - prefix, if any, added to global symbols defined from C
19 # $CC, $NM - names of tools to run (defaults to 'cc' and 'nm' respectively)
20 # $CFLAGS, $LDFLAGS - options to pass to $CC when linking (default: empty)
21
22 use v5.14; # implicit use strict, use feature ':5.14'
23 use warnings FATAL => 'all';
24 use utf8;
25 use open qw(:std :utf8);
26 no if $] >= 5.022, warnings => 'experimental::re_strict';
27 use if $] >= 5.022, re => 'strict';
28
29 use FindBin ();
30 use lib $FindBin::Bin;
31 use TestCommon qw(
32 compare_symbol_lists
33 ensure_C_locale
34 find_real_library
35 get_symbols
36 popen
37 sh_split
38 skip
39 subprocess_error
40 which
41 );
42
43 # Some differences between the symbols exported by heritage libcrypt.so.1
44 # and our libcrypt.so.1 are expected:
45 #
46 # * All of the symbols we define with GLIBC_2.xx version tags are
47 # compatibility symbols (nm prints only one @); naturally,
48 # glibc-provided libcrypt.so.1 defines some of those symbols as
49 # linkable symbols (two @).
50 #
51 # * Older versions of libcrypt defined five symbols as linkable,
52 # with the XCRYPT_2.0 version tag, which are now compatibility-only:
53 # crypt_gensalt_r, xcrypt, xcrypt_gensalt, xcrypt_gensalt_r, and
54 # xcrypt_r.
55 #
56 # This sub is applied to the symbol listing from the system-provided
57 # libcrypt.so.1; it edits that listing so that the comparison below
58 # succeeds despite any expected differences.
59 sub filter_expected_differences {
60 my $symbols = shift;
61 my %filtered;
62 my $formerly_linkable = qr{
63 ^ (?: crypt_gensalt_r
64 | xcrypt(?: _r)?
65 | xcrypt_gensalt(?: _r)?
66 ) @@
67 }x;
68 for my $s (keys %{$symbols}) {
69 $s =~ s/\b@@(?=GLIBC_)/@/;
70 $s =~ s/\b@@(?=XCRYPT_2\.0)/@/ if $s =~ $formerly_linkable;
71 $filtered{$s} = 1;
72 }
73 return \%filtered;
74 }
75
76 sub find_system_libcrypt {
77 # Ask the compiler whether a libcrypt.so.1 exists in its search
78 # path. The compiler option -print-file-name should be supported
79 # on all operating systems where there's an older libcrypt that we
80 # can be backward compatible with.
81 state @CC;
82 if (!@CC) {
83 @CC = which($ENV{CC} || 'cc');
84 skip('C compiler not available') unless @CC;
85 }
86
87 state @CFLAGS;
88 if (!@CFLAGS) {
89 @CFLAGS = sh_split($ENV{CFLAGS} || q{});
90 }
91 state @LDFLAGS;
92 if (!@LDFLAGS) {
93 @LDFLAGS = sh_split($ENV{LDFLAGS} || q{});
94 }
95
96 my $fh =
97 popen('-|', @CC, @CFLAGS, @LDFLAGS, '-print-file-name=libcrypt.so.1');
98 my $path;
99 {
100 local $/ = undef; # slurp
101 $path = <$fh>;
102 }
103 close $fh or subprocess_error($CC[0]);
104
105 chomp $path;
106 # If we get back either the empty string or the same string we put
107 # in, it means there is no libcrypt.so.1 on this system.
108 if ($path eq q{} || $path eq 'libcrypt.so.1') {
109 skip('no system-provided libcrypt.so.1');
110 }
111 return $path;
112 }
113
114 sub get_our_symbols {
115 return get_symbols(find_real_library(shift, 'shared'));
116 }
117
118 sub get_their_symbols {
119 return filter_expected_differences(get_symbols(find_system_libcrypt()));
120 }
121
122 #
123 # Main
124 #
125 my $lib_la = $ENV{lib_la} || '/nonexistent';
126 if (!-f $lib_la) {
127 print {*STDERR} "usage: lib_la=/path/to/library.la $0";
128 exit 1;
129 }
130
131 ensure_C_locale();
132 exit compare_symbol_lists(
133 get_our_symbols($lib_la),
134 get_their_symbols(),
135 'symbol versions',
136 1, # extra symbols are allowed
137 );