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 => "Test is disabled on MinGW" if config('target') =~ m|^mingw|;
27 plan skip_all => "Only useful when building shared libraries"
28 if disabled("shared");
30 my @libnames = ("crypto", "ssl");
31 my $testcount = scalar @libnames;
33 plan tests => $testcount * 2;
36 "NOTE: developer test! It's possible that it won't run on your\n",
37 "platform, and that's perfectly fine. This is mainly for developers\n",
38 "on Unix to check that our shared libraries are consistent with the\n",
39 "ordinals (util/*.num in the source tree), something that should be\n",
40 "good enough a check for the other platforms as well.\n";
42 foreach my $libname (@libnames) {
45 my $shlibname = platform->sharedlib("lib$libname");
46 my $shlibpath = bldtop_file($shlibname);
49 open STDERR, ">", devnull();
50 open STDOUT, ">", devnull();
51 my @nm_lines = map { s|\R$||; $_ } `nm -DPg $shlibpath 2> /dev/null`;
56 skip "Can't run 'nm -DPg $shlibpath' => $?... ignoring", 2
59 my $bldtop = bldtop_dir();
61 indir $bldtop => sub {
62 my $mkdefpath = srctop_file("util", "mkdef.pl");
63 my $libnumpath = srctop_file("util", "lib$libname.num");
64 @def_lines = map { s|\R$||; $_ } `$^X $mkdefpath --ordinals $libnumpath --name $libname --OS linux 2> /dev/null`;
65 ok($? == 0, "running 'cd $bldtop; $^X $mkdefpath --ordinals $libnumpath --name $libname --OS linux' => $?");
66 }, create => 0, cleanup => 0;
68 note "Number of lines in \@nm_lines before massaging: ", scalar @nm_lines;
69 note "Number of lines in \@def_lines before massaging: ", scalar @def_lines;
71 # Massage the nm output to only contain defined symbols
75 # Drop the first space and everything following it
77 # Drop OpenSSL dynamic version information if there is any
78 s|\@\@OPENSSL_[0-9._]+[a-z]?$||;
82 grep(m|.* [BCDST] .*|, @nm_lines);
84 # Massage the mkdef.pl output to only contain global symbols
85 # The output we got is in Unix .map format, which has a global
86 # and a local section. We're only interested in the global
91 map { s|;||; s|\s+||g; $_ }
92 grep { $in_global = 1 if m|global:|;
93 $in_global = 0 if m|local:|;
94 $in_global = 0 if m|\}|;
95 $in_global && m|;|; } @def_lines;
97 note "Number of lines in \@nm_lines after massaging: ", scalar @nm_lines;
98 note "Number of lines in \@def_lines after massaging: ", scalar @def_lines;
100 # Maintain lists of symbols that are missing in the shared library,
105 while (scalar @nm_lines || scalar @def_lines) {
106 my $nm_first = $nm_lines[0];
107 my $def_first = $def_lines[0];
109 if (!defined($nm_first)) {
110 push @missing, shift @def_lines;
111 } elsif (!defined($def_first)) {
112 push @extra, shift @nm_lines;
113 } elsif ($nm_first gt $def_first) {
114 push @missing, shift @def_lines;
115 } elsif ($nm_first lt $def_first) {
116 push @extra, shift @nm_lines;
123 if (scalar @missing) {
124 note "The following symbols are missing in ${shlibname}:";
130 note "The following symbols are extra in ${shlibname}:";
135 ok(scalar @missing == 0,
136 "check that there are no missing symbols in ${shlibname}");