01-test_symbol_presence.t 4.5 KB

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