xref: /netbsd-src/crypto/external/bsd/openssl/dist/test/recipes/01-test_symbol_presence.t (revision c38e7cc395b1472a774ff828e46123de44c628e9)
1#! /usr/bin/env perl
2# -*- mode: Perl -*-
3# Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
4#
5# Licensed under the OpenSSL license (the "License").  You may not use
6# this file except in compliance with the License.  You can obtain a copy
7# in the file LICENSE in the source distribution or at
8# https://www.openssl.org/source/license.html
9
10use strict;
11use File::Spec::Functions qw(devnull);
12use OpenSSL::Test qw(:DEFAULT srctop_file bldtop_dir bldtop_file);
13use OpenSSL::Test::Utils;
14
15setup("test_symbol_presence");
16
17plan skip_all => "Only useful when building shared libraries"
18    if disabled("shared");
19
20my @libnames = ("crypto", "ssl");
21my $testcount = scalar @libnames;
22
23plan tests => $testcount * 2;
24
25note
26    "NOTE: developer test!  It's possible that it won't run on your\n",
27    "platform, and that's perfectly fine.  This is mainly for developers\n",
28    "on Unix to check that our shared libraries are consistent with the\n",
29    "ordinals (util/*.num in the source tree), something that should be\n",
30    "good enough a check for the other platforms as well.\n";
31
32foreach my $libname (@libnames) {
33 SKIP:
34    {
35        my $shlibpath = bldtop_file("lib" . $libname . ".so");
36        *OSTDERR = *STDERR;
37        *OSTDOUT = *STDOUT;
38        open STDERR, ">", devnull();
39        open STDOUT, ">", devnull();
40        my @nm_lines = map { s|\R$||; $_ } `nm -Pg $shlibpath 2> /dev/null`;
41        close STDERR;
42        close STDOUT;
43        *STDERR = *OSTDERR;
44        *STDOUT = *OSTDOUT;
45        skip "Can't run 'nm -Pg $shlibpath' => $?...  ignoring", 2
46            unless $? == 0;
47
48        my $bldtop = bldtop_dir();
49        my @def_lines;
50        indir $bldtop => sub {
51            my $mkdefpath = srctop_file("util", "mkdef.pl");
52            @def_lines = map { s|\R$||; $_ } `$^X $mkdefpath $libname linux 2> /dev/null`;
53            ok($? == 0, "running 'cd $bldtop; $^X $mkdefpath $libname linux' => $?");
54        }, create => 0, cleanup => 0;
55
56        note "Number of lines in \@nm_lines before massaging: ", scalar @nm_lines;
57        note "Number of lines in \@def_lines before massaging: ", scalar @def_lines;
58
59        # Massage the nm output to only contain defined symbols
60        @nm_lines = sort map { s| .*||; $_ } grep(m|.* [BCDST] .*|, @nm_lines);
61
62        # Massage the mkdef.pl output to only contain global symbols
63        # The output we got is in Unix .map format, which has a global
64        # and a local section.  We're only interested in the global
65        # section.
66        my $in_global = 0;
67        @def_lines =
68            sort
69            map { s|;||; s|\s+||g; $_ }
70            grep { $in_global = 1 if m|global:|;
71                   $in_global = 0 if m|local:|;
72                   $in_global = 0 if m|\}|;
73                   $in_global && m|;|; } @def_lines;
74
75        note "Number of lines in \@nm_lines after massaging: ", scalar @nm_lines;
76        note "Number of lines in \@def_lines after massaging: ", scalar @def_lines;
77
78        # Maintain lists of symbols that are missing in the shared library,
79        # or that are extra.
80        my @missing = ();
81        my @extra = ();
82
83        while (scalar @nm_lines || scalar @def_lines) {
84            my $nm_first = $nm_lines[0];
85            my $def_first = $def_lines[0];
86
87            if (!defined($nm_first)) {
88                push @missing, shift @def_lines;
89            } elsif (!defined($def_first)) {
90                push @extra, shift @nm_lines;
91            } elsif ($nm_first gt $def_first) {
92                push @missing, shift @def_lines;
93            } elsif ($nm_first lt $def_first) {
94                push @extra, shift @nm_lines;
95            } else {
96                shift @def_lines;
97                shift @nm_lines;
98            }
99        }
100
101        if (scalar @missing) {
102            note "The following symbols are missing in lib$libname.so:";
103            foreach (@missing) {
104                note "  $_";
105            }
106        }
107        if (scalar @extra) {
108            note "The following symbols are extra in lib$libname.so:";
109            foreach (@extra) {
110                note "  $_";
111            }
112        }
113        ok(scalar @missing == 0,
114           "check that there are no missing symbols in lib$libname.so");
115    }
116}
117