01-test_symbol_presence.t 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #! /usr/bin/env perl
  2. # -*- mode: Perl -*-
  3. # Copyright 2016-2018 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 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. my $libnumpath = srctop_file("util", "lib$libname.num");
  45. @def_lines = map { s|\R$||; $_ } `$^X $mkdefpath --ordinals $libnumpath --name $libname --OS linux 2> /dev/null`;
  46. ok($? == 0, "running 'cd $bldtop; $^X $mkdefpath --ordinals $libnumpath --name $libname --OS linux' => $?");
  47. }, create => 0, cleanup => 0;
  48. note "Number of lines in \@nm_lines before massaging: ", scalar @nm_lines;
  49. note "Number of lines in \@def_lines before massaging: ", scalar @def_lines;
  50. # Massage the nm output to only contain defined symbols
  51. @nm_lines = sort map { s| .*||; $_ } grep(m|.* [BCDST] .*|, @nm_lines);
  52. # Massage the mkdef.pl output to only contain global symbols
  53. # The output we got is in Unix .map format, which has a global
  54. # and a local section. We're only interested in the global
  55. # section.
  56. my $in_global = 0;
  57. @def_lines =
  58. sort
  59. map { s|;||; s|\s+||g; $_ }
  60. grep { $in_global = 1 if m|global:|;
  61. $in_global = 0 if m|local:|;
  62. $in_global = 0 if m|\}|;
  63. $in_global && m|;|; } @def_lines;
  64. note "Number of lines in \@nm_lines after massaging: ", scalar @nm_lines;
  65. note "Number of lines in \@def_lines after massaging: ", scalar @def_lines;
  66. # Maintain lists of symbols that are missing in the shared library,
  67. # or that are extra.
  68. my @missing = ();
  69. my @extra = ();
  70. while (scalar @nm_lines || scalar @def_lines) {
  71. my $nm_first = $nm_lines[0];
  72. my $def_first = $def_lines[0];
  73. if (!defined($nm_first)) {
  74. push @missing, shift @def_lines;
  75. } elsif (!defined($def_first)) {
  76. push @extra, shift @nm_lines;
  77. } elsif ($nm_first gt $def_first) {
  78. push @missing, shift @def_lines;
  79. } elsif ($nm_first lt $def_first) {
  80. push @extra, shift @nm_lines;
  81. } else {
  82. shift @def_lines;
  83. shift @nm_lines;
  84. }
  85. }
  86. if (scalar @missing) {
  87. note "The following symbols are missing in lib$libname.so:";
  88. foreach (@missing) {
  89. note " $_";
  90. }
  91. }
  92. if (scalar @extra) {
  93. note "The following symbols are extra in lib$libname.so:";
  94. foreach (@extra) {
  95. note " $_";
  96. }
  97. }
  98. ok(scalar @missing == 0,
  99. "check that there are no missing symbols in lib$libname.so");
  100. }
  101. }