01-test_symbol_presence.t 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. use strict;
  10. use File::Spec::Functions qw(devnull);
  11. use OpenSSL::Test qw(:DEFAULT srctop_file srctop_dir bldtop_dir bldtop_file);
  12. use OpenSSL::Test::Utils;
  13. BEGIN {
  14. setup("test_symbol_presence");
  15. }
  16. use lib srctop_dir('Configurations');
  17. use lib bldtop_dir('.');
  18. use platform;
  19. plan skip_all => "Test is disabled on NonStop" if config('target') =~ m|^nonstop|;
  20. # MacOS arranges symbol names differently
  21. plan skip_all => "Test is disabled on MacOS" if config('target') =~ m|^darwin|;
  22. plan skip_all => "This is unsupported on MSYS, MinGW or MSWin32"
  23. if $^O eq 'msys' or $^O eq 'MSWin32' or config('target') =~ m|^mingw|;
  24. plan skip_all => "Only useful when building shared libraries"
  25. if disabled("shared");
  26. my @libnames = ("crypto", "ssl");
  27. my $testcount = scalar @libnames;
  28. plan tests => $testcount * 2;
  29. note
  30. "NOTE: developer test! It's possible that it won't run on your\n",
  31. "platform, and that's perfectly fine. This is mainly for developers\n",
  32. "on Unix to check that our shared libraries are consistent with the\n",
  33. "ordinals (util/*.num in the source tree), something that should be\n",
  34. "good enough a check for the other platforms as well.\n";
  35. foreach my $libname (@libnames) {
  36. SKIP:
  37. {
  38. my $shlibname = platform->sharedlib("lib$libname");
  39. my $shlibpath = bldtop_file($shlibname);
  40. *OSTDERR = *STDERR;
  41. *OSTDOUT = *STDOUT;
  42. open STDERR, ">", devnull();
  43. open STDOUT, ">", devnull();
  44. my @nm_lines = map { s|\R$||; $_ } `nm -DPg $shlibpath 2> /dev/null`;
  45. close STDERR;
  46. close STDOUT;
  47. *STDERR = *OSTDERR;
  48. *STDOUT = *OSTDOUT;
  49. skip "Can't run 'nm -DPg $shlibpath' => $?... ignoring", 2
  50. unless $? == 0;
  51. my $bldtop = bldtop_dir();
  52. my @def_lines;
  53. indir $bldtop => sub {
  54. my $mkdefpath = srctop_file("util", "mkdef.pl");
  55. my $libnumpath = srctop_file("util", "lib$libname.num");
  56. @def_lines = map { s|\R$||; $_ } `$^X $mkdefpath --ordinals $libnumpath --name $libname --OS linux 2> /dev/null`;
  57. ok($? == 0, "running 'cd $bldtop; $^X $mkdefpath --ordinals $libnumpath --name $libname --OS linux' => $?");
  58. }, create => 0, cleanup => 0;
  59. note "Number of lines in \@nm_lines before massaging: ", scalar @nm_lines;
  60. note "Number of lines in \@def_lines before massaging: ", scalar @def_lines;
  61. # Massage the nm output to only contain defined symbols
  62. @nm_lines =
  63. sort
  64. map {
  65. # Drop the first space and everything following it
  66. s| .*||;
  67. # Drop OpenSSL dynamic version information if there is any
  68. s|\@\@.+$||;
  69. # Return the result
  70. $_
  71. }
  72. grep(m|.* [BCDST] .*|, @nm_lines);
  73. # Massage the mkdef.pl output to only contain global symbols
  74. # The output we got is in Unix .map format, which has a global
  75. # and a local section. We're only interested in the global
  76. # section.
  77. my $in_global = 0;
  78. @def_lines =
  79. sort
  80. map { s|;||; s|\s+||g; $_ }
  81. grep { $in_global = 1 if m|global:|;
  82. $in_global = 0 if m|local:|;
  83. $in_global = 0 if m|\}|;
  84. $in_global && m|;|; } @def_lines;
  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. # Maintain lists of symbols that are missing in the shared library,
  88. # or that are extra.
  89. my @missing = ();
  90. my @extra = ();
  91. while (scalar @nm_lines || scalar @def_lines) {
  92. my $nm_first = $nm_lines[0];
  93. my $def_first = $def_lines[0];
  94. if (!defined($nm_first)) {
  95. push @missing, shift @def_lines;
  96. } elsif (!defined($def_first)) {
  97. push @extra, shift @nm_lines;
  98. } elsif ($nm_first gt $def_first) {
  99. push @missing, shift @def_lines;
  100. } elsif ($nm_first lt $def_first) {
  101. push @extra, shift @nm_lines;
  102. } else {
  103. shift @def_lines;
  104. shift @nm_lines;
  105. }
  106. }
  107. if (scalar @missing) {
  108. note "The following symbols are missing in ${shlibname}:";
  109. foreach (@missing) {
  110. note " $_";
  111. }
  112. }
  113. if (scalar @extra) {
  114. note "The following symbols are extra in ${shlibname}:";
  115. foreach (@extra) {
  116. note " $_";
  117. }
  118. }
  119. ok(scalar @missing == 0,
  120. "check that there are no missing symbols in ${shlibname}");
  121. }
  122. }