01-test_symbol_presence.t 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. #! /usr/bin/env perl
  2. # -*- mode: Perl -*-
  3. # Copyright 2016-2023 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. use strict;
  10. use File::Spec::Functions qw(devnull);
  11. use IPC::Cmd;
  12. use OpenSSL::Test qw(:DEFAULT srctop_file srctop_dir bldtop_dir bldtop_file);
  13. use OpenSSL::Test::Utils;
  14. BEGIN {
  15. setup("test_symbol_presence");
  16. }
  17. use lib srctop_dir('Configurations');
  18. use lib bldtop_dir('.');
  19. use platform;
  20. plan skip_all => "Test is disabled on NonStop" if config('target') =~ m|^nonstop|;
  21. # MacOS arranges symbol names differently
  22. plan skip_all => "Test is disabled on MacOS" if config('target') =~ m|^darwin|;
  23. plan skip_all => "This is unsupported on platforms that don't have 'nm'"
  24. unless IPC::Cmd::can_run('nm');
  25. note
  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), and that our static libraries\n",
  30. "don't share symbols, something that should be a good enough check for\n",
  31. "the other platforms as well.\n";
  32. my %stlibname;
  33. my %shlibname;
  34. my %stlibpath;
  35. my %shlibpath;
  36. my %defpath;
  37. foreach (qw(crypto ssl)) {
  38. $stlibname{$_} = platform->staticlib("lib$_");
  39. $stlibpath{$_} = bldtop_file($stlibname{$_});
  40. $shlibname{$_} = platform->sharedlib("lib$_") unless disabled('shared');
  41. $shlibpath{$_} = bldtop_file($shlibname{$_}) unless disabled('shared');
  42. }
  43. my $testcount
  44. = 1 # Check for static library symbols duplicates
  45. ;
  46. $testcount
  47. += (scalar keys %shlibpath) # Check for missing symbols in shared lib
  48. unless disabled('shared');
  49. plan tests => $testcount;
  50. ######################################################################
  51. # Collect symbols
  52. # [3 tests per library]
  53. my %stsymbols; # Static library symbols
  54. my %shsymbols; # Shared library symbols
  55. my %defsymbols; # Symbols taken from ordinals
  56. foreach (sort keys %stlibname) {
  57. my $stlib_cmd = "nm -Pg $stlibpath{$_} 2> /dev/null";
  58. my $shlib_cmd = "nm -DPg $shlibpath{$_} 2> /dev/null";
  59. my @stlib_lines;
  60. my @shlib_lines;
  61. *OSTDERR = *STDERR;
  62. *OSTDOUT = *STDOUT;
  63. open STDERR, ">", devnull();
  64. open STDOUT, ">", devnull();
  65. @stlib_lines = map { s|\R$||; $_ } `$stlib_cmd`;
  66. if ($? != 0) {
  67. note "running '$stlib_cmd' => $?";
  68. @stlib_lines = ();
  69. }
  70. unless (disabled('shared')) {
  71. @shlib_lines = map { s|\R$||; $_ } `$shlib_cmd`;
  72. if ($? != 0) {
  73. note "running '$shlib_cmd' => $?";
  74. @shlib_lines = ();
  75. }
  76. }
  77. close STDERR;
  78. close STDOUT;
  79. *STDERR = *OSTDERR;
  80. *STDOUT = *OSTDOUT;
  81. my $bldtop = bldtop_dir();
  82. my @def_lines;
  83. unless (disabled('shared')) {
  84. indir $bldtop => sub {
  85. my $mkdefpath = srctop_file("util", "mkdef.pl");
  86. my $def_path = srctop_file("util", "lib$_.num");
  87. my $def_cmd = "$^X $mkdefpath --ordinals $def_path --name $_ --OS linux 2> /dev/null";
  88. @def_lines = map { s|\R$||; $_ } `$def_cmd`;
  89. if ($? != 0) {
  90. note "running 'cd $bldtop; $def_cmd' => $?";
  91. @def_lines = ();
  92. }
  93. }, create => 0, cleanup => 0;
  94. }
  95. note "Number of lines in \@stlib_lines before massaging: ", scalar @stlib_lines;
  96. unless (disabled('shared')) {
  97. note "Number of lines in \@shlib_lines before massaging: ", scalar @shlib_lines;
  98. note "Number of lines in \@def_lines before massaging: ", scalar @def_lines;
  99. }
  100. # Massage the nm output to only contain defined symbols
  101. my @arrays = ( \@stlib_lines );
  102. push @arrays, \@shlib_lines unless disabled('shared');
  103. foreach (@arrays) {
  104. @$_ =
  105. sort
  106. map {
  107. # Drop the first space and everything following it
  108. s| .*||;
  109. # Drop OpenSSL dynamic version information if there is any
  110. s|\@\@.+$||;
  111. # Return the result
  112. $_
  113. }
  114. grep(m|.* [BCDST] .*|, @$_);
  115. }
  116. # Massage the mkdef.pl output to only contain global symbols
  117. # The output we got is in Unix .map format, which has a global
  118. # and a local section. We're only interested in the global
  119. # section.
  120. my $in_global = 0;
  121. unless (disabled('shared')) {
  122. @def_lines =
  123. sort
  124. map { s|;||; s|\s+||g; $_ }
  125. grep { $in_global = 1 if m|global:|;
  126. $in_global = 0 if m|local:|;
  127. $in_global = 0 if m|\}|;
  128. $in_global && m|;|; } @def_lines;
  129. }
  130. note "Number of lines in \@stlib_lines after massaging: ", scalar @stlib_lines;
  131. unless (disabled('shared')) {
  132. note "Number of lines in \@shlib_lines after massaging: ", scalar @shlib_lines;
  133. note "Number of lines in \@def_lines after massaging: ", scalar @def_lines;
  134. }
  135. $stsymbols{$_} = [ @stlib_lines ];
  136. unless (disabled('shared')) {
  137. $shsymbols{$_} = [ @shlib_lines ];
  138. $defsymbols{$_} = [ @def_lines ];
  139. }
  140. }
  141. ######################################################################
  142. # Check that there are no duplicate symbols in all our static libraries
  143. # combined
  144. # [1 test]
  145. my %symbols;
  146. foreach (sort keys %stlibname) {
  147. foreach (@{$stsymbols{$_}}) {
  148. $symbols{$_}++;
  149. }
  150. }
  151. my @duplicates = sort grep { $symbols{$_} > 1 } keys %symbols;
  152. if (@duplicates) {
  153. note "Duplicates:";
  154. note join('\n', @duplicates);
  155. }
  156. ok(scalar @duplicates == 0, "checking no duplicate symbols in static libraries");
  157. ######################################################################
  158. # Check that the exported symbols in our shared libraries are consistent
  159. # with our ordinals files.
  160. # [1 test per library]
  161. unless (disabled('shared')) {
  162. foreach (sort keys %stlibname) {
  163. # Maintain lists of symbols that are missing in the shared library,
  164. # or that are extra.
  165. my @missing = ();
  166. my @extra = ();
  167. my @sh_symbols = ( @{$shsymbols{$_}} );
  168. my @def_symbols = ( @{$defsymbols{$_}} );
  169. while (scalar @sh_symbols || scalar @def_symbols) {
  170. my $sh_first = $sh_symbols[0];
  171. my $def_first = $def_symbols[0];
  172. if (!defined($sh_first)) {
  173. push @missing, shift @def_symbols;
  174. } elsif (!defined($def_first)) {
  175. push @extra, shift @sh_symbols;
  176. } elsif ($sh_first gt $def_first) {
  177. push @missing, shift @def_symbols;
  178. } elsif ($sh_first lt $def_first) {
  179. push @extra, shift @sh_symbols;
  180. } else {
  181. shift @def_symbols;
  182. shift @sh_symbols;
  183. }
  184. }
  185. if (scalar @missing) {
  186. note "The following symbols are missing in $_:";
  187. foreach (@missing) {
  188. note " $_";
  189. }
  190. }
  191. if (scalar @extra) {
  192. note "The following symbols are extra in $_:";
  193. foreach (@extra) {
  194. note " $_";
  195. }
  196. }
  197. ok(scalar @missing == 0,
  198. "check that there are no missing symbols in $_");
  199. }
  200. }