01-test_symbol_presence.t 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #! /usr/bin/env perl
  2. # -*- mode: Perl -*-
  3. # Copyright 2016 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 -Pg $shlibpath 2> /dev/null`;
  34. close STDERR;
  35. close STDOUT;
  36. *STDERR = *OSTDERR;
  37. *STDOUT = *OSTDOUT;
  38. skip "Can't run 'nm -Pg $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 = sort map { s| .*||; $_ } grep(m|.* [BCDST] .*|, @nm_lines);
  51. # Massage the mkdef.pl output to only contain global symbols
  52. # The output we got is in Unix .map format, which has a global
  53. # and a local section. We're only interested in the global
  54. # section.
  55. my $in_global = 0;
  56. @def_lines =
  57. sort
  58. map { s|;||; s|\s+||g; $_ }
  59. grep { $in_global = 1 if m|global:|;
  60. $in_global = 0 if m|local:|;
  61. $in_global = 0 if m|\}|;
  62. $in_global && m|;|; } @def_lines;
  63. note "Number of lines in \@nm_lines after massaging: ", scalar @nm_lines;
  64. note "Number of lines in \@def_lines after massaging: ", scalar @def_lines;
  65. # Maintain lists of symbols that are missing in the shared library,
  66. # or that are extra.
  67. my @missing = ();
  68. my @extra = ();
  69. while (scalar @nm_lines || scalar @def_lines) {
  70. my $nm_first = $nm_lines[0];
  71. my $def_first = $def_lines[0];
  72. if (!defined($nm_first)) {
  73. push @missing, shift @def_lines;
  74. } elsif (!defined($def_first)) {
  75. push @extra, shift @nm_lines;
  76. } elsif ($nm_first gt $def_first) {
  77. push @missing, shift @def_lines;
  78. } elsif ($nm_first lt $def_first) {
  79. push @extra, shift @nm_lines;
  80. } else {
  81. shift @def_lines;
  82. shift @nm_lines;
  83. }
  84. }
  85. if (scalar @missing) {
  86. note "The following symbols are missing in lib$libname.so:";
  87. foreach (@missing) {
  88. note " $_";
  89. }
  90. }
  91. if (scalar @extra) {
  92. note "The following symbols are extra in lib$libname.so:";
  93. foreach (@extra) {
  94. note " $_";
  95. }
  96. }
  97. ok(scalar @missing == 0,
  98. "check that there are no missing symbols in lib$libname.so");
  99. }
  100. }