check-for-exit-time-destructors 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #!/usr/bin/perl
  2. # Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved.
  3. #
  4. # Redistribution and use in source and binary forms, with or without
  5. # modification, are permitted provided that the following conditions
  6. # are met:
  7. #
  8. # 1. Redistributions of source code must retain the above copyright
  9. # notice, this list of conditions and the following disclaimer.
  10. # 2. Redistributions in binary form must reproduce the above copyright
  11. # notice, this list of conditions and the following disclaimer in the
  12. # documentation and/or other materials provided with the distribution.
  13. # 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
  14. # its contributors may be used to endorse or promote products derived
  15. # from this software without specific prior written permission.
  16. #
  17. # THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
  18. # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. # DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
  21. # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24. # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. # "check-for-exit-time-destructors" script for WebKit Open Source Project
  28. # Intended to be invoked from an Xcode build step to check if there are
  29. # any exit-time destructors in a target.
  30. use warnings;
  31. use strict;
  32. use File::Basename;
  33. sub touch($);
  34. sub printFunctions($$);
  35. my $arch = $ENV{'CURRENT_ARCH'};
  36. my $configuration = $ENV{'CONFIGURATION'};
  37. my $target = $ENV{'TARGET_NAME'};
  38. my $variant = $ENV{'CURRENT_VARIANT'};
  39. my $coverageBuild = $ENV{'WEBKIT_COVERAGE_BUILD'};
  40. my $debugRoot = $ENV{'WEBKIT_DEBUG_ROOT'};
  41. $arch = $ENV{'NATIVE_ARCH'} if !$arch; # for Xcode 2.1, which does not have CURRENT_ARCH
  42. $variant = "normal" if !$variant; # for Xcode 2.1, which does not have CURRENT_VARIANT
  43. my $executablePath = "$ENV{'TARGET_BUILD_DIR'}/$ENV{'EXECUTABLE_PATH'}";
  44. my $buildTimestampPath = $ENV{'TARGET_TEMP_DIR'} . "/" . basename($0) . ".timestamp";
  45. my $buildTimestampAge = -M $buildTimestampPath;
  46. my $scriptAge = -M $0;
  47. my $list = $ENV{"LINK_FILE_LIST_${variant}_${arch}"};
  48. if (!open LIST, $list) {
  49. print "ERROR: Could not open $list\n";
  50. exit 1;
  51. }
  52. my @files = <LIST>;
  53. chomp @files;
  54. close LIST;
  55. my $sawError = 0;
  56. for my $file (sort @files) {
  57. if (defined $buildTimestampAge && $buildTimestampAge < $scriptAge) {
  58. my $fileAge = -M $file;
  59. next if defined $fileAge && $fileAge > $buildTimestampAge;
  60. }
  61. if (!open NM, "(nm '$file' | sed 's/^/STDOUT:/') 2>&1 |") {
  62. print "ERROR: Could not open $file\n";
  63. $sawError = 1;
  64. next;
  65. }
  66. my $sawAtExit = 0;
  67. my $shortName = $file;
  68. $shortName =~ s/.*\///;
  69. while (<NM>) {
  70. if (/^STDOUT:/) {
  71. # With GC logging enabled Heap.o may contain finalizers, so we ignore them.
  72. $sawAtExit = 1 if (/___cxa_atexit/ && ($shortName ne "Heap.o"));
  73. } else {
  74. print STDERR if $_ ne "nm: no name list\n";
  75. }
  76. }
  77. close NM;
  78. next unless $sawAtExit;
  79. $sawError = 1 if printFunctions($shortName, $file);
  80. }
  81. if ($sawError and !$coverageBuild) {
  82. print "ERROR: Use DEFINE_STATIC_LOCAL from <wtf/StdLibExtras.h>\n";
  83. unlink $executablePath;
  84. exit 1;
  85. }
  86. touch($buildTimestampPath);
  87. exit 0;
  88. sub touch($)
  89. {
  90. my ($path) = @_;
  91. open(TOUCH, ">", $path) or die "$!";
  92. close(TOUCH);
  93. }
  94. sub demangle($)
  95. {
  96. my ($symbol) = @_;
  97. if (!open FILT, "c++filt $symbol |") {
  98. print "ERROR: Could not open c++filt\n";
  99. return;
  100. }
  101. my $result = <FILT>;
  102. close FILT;
  103. chomp $result;
  104. return $result;
  105. }
  106. sub printFunctions($$)
  107. {
  108. my ($shortName, $path) = @_;
  109. if (!open OTOOL, "otool -tV '$path' |") {
  110. print "WARNING: Could not open $path\n";
  111. return 0;
  112. }
  113. my %functions;
  114. my $currentSymbol = "";
  115. while (<OTOOL>) {
  116. $currentSymbol = $1 if /^(\w+):$/;
  117. next unless $currentSymbol;
  118. $functions{demangle($currentSymbol)} = 1 if /___cxa_atexit/;
  119. }
  120. close OTOOL;
  121. my $result = 0;
  122. for my $function (sort keys %functions) {
  123. if (!$result) {
  124. print "ERROR: $shortName has exit time destructors in it! ($path)\n";
  125. $result = 1;
  126. }
  127. print "ERROR: In function $function\n";
  128. }
  129. return $result;
  130. }