1#! /usr/bin/env perl 2# -*- mode: Perl -*- 3# Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved. 4# 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 9 10use strict; 11use File::Spec::Functions qw(devnull); 12use OpenSSL::Test qw(:DEFAULT srctop_file srctop_dir bldtop_dir bldtop_file); 13use OpenSSL::Test::Utils; 14 15BEGIN { 16 setup("test_symbol_presence"); 17} 18 19use lib srctop_dir('Configurations'); 20use lib bldtop_dir('.'); 21use platform; 22 23plan skip_all => "Test is disabled on NonStop" if config('target') =~ m|^nonstop|; 24# MacOS arranges symbol names differently 25plan skip_all => "Test is disabled on MacOS" if config('target') =~ m|^darwin|; 26plan skip_all => "Test is disabled on MinGW" if config('target') =~ m|^mingw|; 27plan skip_all => "Only useful when building shared libraries" 28 if disabled("shared"); 29 30my @libnames = ("crypto", "ssl"); 31my $testcount = scalar @libnames; 32 33plan tests => $testcount * 2; 34 35note 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"; 41 42foreach my $libname (@libnames) { 43 SKIP: 44 { 45 my $shlibname = platform->sharedlib("lib$libname"); 46 my $shlibpath = bldtop_file($shlibname); 47 *OSTDERR = *STDERR; 48 *OSTDOUT = *STDOUT; 49 open STDERR, ">", devnull(); 50 open STDOUT, ">", devnull(); 51 my @nm_lines = map { s|\R$||; $_ } `nm -DPg $shlibpath 2> /dev/null`; 52 close STDERR; 53 close STDOUT; 54 *STDERR = *OSTDERR; 55 *STDOUT = *OSTDOUT; 56 skip "Can't run 'nm -DPg $shlibpath' => $?... ignoring", 2 57 unless $? == 0; 58 59 my $bldtop = bldtop_dir(); 60 my @def_lines; 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; 67 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; 70 71 # Massage the nm output to only contain defined symbols 72 @nm_lines = 73 sort 74 map { 75 # Drop the first space and everything following it 76 s| .*||; 77 # Drop OpenSSL dynamic version information if there is any 78 s|\@\@OPENSSL_[0-9._]+[a-z]?$||; 79 # Return the result 80 $_ 81 } 82 grep(m|.* [BCDST] .*|, @nm_lines); 83 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 87 # section. 88 my $in_global = 0; 89 @def_lines = 90 sort 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; 96 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; 99 100 # Maintain lists of symbols that are missing in the shared library, 101 # or that are extra. 102 my @missing = (); 103 my @extra = (); 104 105 while (scalar @nm_lines || scalar @def_lines) { 106 my $nm_first = $nm_lines[0]; 107 my $def_first = $def_lines[0]; 108 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; 117 } else { 118 shift @def_lines; 119 shift @nm_lines; 120 } 121 } 122 123 if (scalar @missing) { 124 note "The following symbols are missing in ${shlibname}:"; 125 foreach (@missing) { 126 note " $_"; 127 } 128 } 129 if (scalar @extra) { 130 note "The following symbols are extra in ${shlibname}:"; 131 foreach (@extra) { 132 note " $_"; 133 } 134 } 135 ok(scalar @missing == 0, 136 "check that there are no missing symbols in ${shlibname}"); 137 } 138} 139