1#! /usr/bin/env perl 2# -*- mode: Perl -*- 3# Copyright 2016-2021 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 -DPg $shlibpath 2> /dev/null`; 41 close STDERR; 42 close STDOUT; 43 *STDERR = *OSTDERR; 44 *STDOUT = *OSTDOUT; 45 skip "Can't run 'nm -DPg $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 = 61 sort 62 map { 63 # Drop the first space and everything following it 64 s| .*||; 65 # Drop OpenSSL dynamic version information if there is any 66 s|\@\@OPENSSL_[0-9._]+[a-z]?$||; 67 # Return the result 68 $_ 69 } 70 grep(m|.* [BCDST] .*|, @nm_lines); 71 72 # Massage the mkdef.pl output to only contain global symbols 73 # The output we got is in Unix .map format, which has a global 74 # and a local section. We're only interested in the global 75 # section. 76 my $in_global = 0; 77 @def_lines = 78 sort 79 map { s|;||; s|\s+||g; $_ } 80 grep { $in_global = 1 if m|global:|; 81 $in_global = 0 if m|local:|; 82 $in_global = 0 if m|\}|; 83 $in_global && m|;|; } @def_lines; 84 85 note "Number of lines in \@nm_lines after massaging: ", scalar @nm_lines; 86 note "Number of lines in \@def_lines after massaging: ", scalar @def_lines; 87 88 # Maintain lists of symbols that are missing in the shared library, 89 # or that are extra. 90 my @missing = (); 91 my @extra = (); 92 93 while (scalar @nm_lines || scalar @def_lines) { 94 my $nm_first = $nm_lines[0]; 95 my $def_first = $def_lines[0]; 96 97 if (!defined($nm_first)) { 98 push @missing, shift @def_lines; 99 } elsif (!defined($def_first)) { 100 push @extra, shift @nm_lines; 101 } elsif ($nm_first gt $def_first) { 102 push @missing, shift @def_lines; 103 } elsif ($nm_first lt $def_first) { 104 push @extra, shift @nm_lines; 105 } else { 106 shift @def_lines; 107 shift @nm_lines; 108 } 109 } 110 111 if (scalar @missing) { 112 note "The following symbols are missing in lib$libname.so:"; 113 foreach (@missing) { 114 note " $_"; 115 } 116 } 117 if (scalar @extra) { 118 note "The following symbols are extra in lib$libname.so:"; 119 foreach (@extra) { 120 note " $_"; 121 } 122 } 123 ok(scalar @missing == 0, 124 "check that there are no missing symbols in lib$libname.so"); 125 } 126} 127