3 # Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved.
5 # Licensed under the Apache License 2.0 (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
11 use File::Spec::Functions qw(devnull);
12 use OpenSSL::Test qw(:DEFAULT srctop_file srctop_dir bldtop_dir bldtop_file);
13 use OpenSSL::Test::Utils;
16 setup("test_symbol_presence");
19 use lib srctop_dir('Configurations');
20 use lib bldtop_dir('.');
23 plan skip_all => "Test is disabled on NonStop" if config('target') =~ m|^nonstop|;
24 # MacOS arranges symbol names differently
25 plan skip_all => "Test is disabled on MacOS" if config('target') =~ m|^darwin|;
26 plan skip_all => "This is unsupported on MSYS, MinGW or MSWin32"
27 if $^O eq 'msys' or $^O eq 'MSWin32' or config('target') =~ m|^mingw|;
28 plan skip_all => "Only useful when building shared libraries"
29 if disabled("shared");
31 my @libnames = ("crypto", "ssl");
32 my $testcount = scalar @libnames;
34 plan tests => $testcount * 2;
37 "NOTE: developer test! It's possible that it won't run on your\n",
38 "platform, and that's perfectly fine. This is mainly for developers\n",
39 "on Unix to check that our shared libraries are consistent with the\n",
40 "ordinals (util/*.num in the source tree), something that should be\n",
41 "good enough a check for the other platforms as well.\n";
43 foreach my $libname (@libnames) {
46 my $shlibname = platform->sharedlib("lib$libname");
47 my $shlibpath = bldtop_file($shlibname);
50 open STDERR, ">", devnull();
51 open STDOUT, ">", devnull();
52 my @nm_lines = map { s|\R$||; $_ } `nm -DPg $shlibpath 2> /dev/null`;
57 skip "Can't run 'nm -DPg $shlibpath' => $?... ignoring", 2
60 my $bldtop = bldtop_dir();
62 indir $bldtop => sub {
63 my $mkdefpath = srctop_file("util", "mkdef.pl");
64 my $libnumpath = srctop_file("util", "lib$libname.num");
65 @def_lines = map { s|\R$||; $_ } `$^X $mkdefpath --ordinals $libnumpath --name $libname --OS linux 2> /dev/null`;
66 ok($? == 0, "running 'cd $bldtop; $^X $mkdefpath --ordinals $libnumpath --name $libname --OS linux' => $?");
67 }, create => 0, cleanup => 0;
69 note "Number of lines in \@nm_lines before massaging: ", scalar @nm_lines;
70 note "Number of lines in \@def_lines before massaging: ", scalar @def_lines;
72 # Massage the nm output to only contain defined symbols
76 # Drop the first space and everything following it
78 # Drop OpenSSL dynamic version information if there is any
83 grep(m|.* [BCDST] .*|, @nm_lines);
85 # Massage the mkdef.pl output to only contain global symbols
86 # The output we got is in Unix .map format, which has a global
87 # and a local section. We're only interested in the global
92 map { s|;||; s|\s+||g; $_ }
93 grep { $in_global = 1 if m|global:|;
94 $in_global = 0 if m|local:|;
95 $in_global = 0 if m|\}|;
96 $in_global && m|;|; } @def_lines;
98 note "Number of lines in \@nm_lines after massaging: ", scalar @nm_lines;
99 note "Number of lines in \@def_lines after massaging: ", scalar @def_lines;
101 # Maintain lists of symbols that are missing in the shared library,
106 while (scalar @nm_lines || scalar @def_lines) {
107 my $nm_first = $nm_lines[0];
108 my $def_first = $def_lines[0];
110 if (!defined($nm_first)) {
111 push @missing, shift @def_lines;
112 } elsif (!defined($def_first)) {
113 push @extra, shift @nm_lines;
114 } elsif ($nm_first gt $def_first) {
115 push @missing, shift @def_lines;
116 } elsif ($nm_first lt $def_first) {
117 push @extra, shift @nm_lines;
124 if (scalar @missing) {
125 note "The following symbols are missing in ${shlibname}:";
131 note "The following symbols are extra in ${shlibname}:";
136 ok(scalar @missing == 0,
137 "check that there are no missing symbols in ${shlibname}");