leakdetector.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. # Copyright (C) 2010 Google Inc. All rights reserved.
  2. #
  3. # Redistribution and use in source and binary forms, with or without
  4. # modification, are permitted provided that the following conditions are
  5. # met:
  6. #
  7. # * Redistributions of source code must retain the above copyright
  8. # notice, this list of conditions and the following disclaimer.
  9. # * Redistributions in binary form must reproduce the above
  10. # copyright notice, this list of conditions and the following disclaimer
  11. # in the documentation and/or other materials provided with the
  12. # distribution.
  13. # * Neither the Google name nor the names of its
  14. # contributors may be used to endorse or promote products derived from
  15. # this software without specific prior written permission.
  16. #
  17. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  20. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  21. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  22. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  23. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  24. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  25. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. import logging
  29. import re
  30. from webkitpy.common.system.executive import ScriptError
  31. _log = logging.getLogger(__name__)
  32. # If other ports/platforms decide to support --leaks, we should see about sharing as much of this code as possible.
  33. # Right now this code is only used by Apple's MacPort.
  34. class LeakDetector(object):
  35. def __init__(self, port):
  36. # We should operate on a "platform" not a port here.
  37. self._port = port
  38. self._executive = port._executive
  39. self._filesystem = port._filesystem
  40. # We exclude the following reported leaks so they do not get in our way when looking for WebKit leaks:
  41. # This allows us ignore known leaks and only be alerted when new leaks occur. Some leaks are in the old
  42. # versions of the system frameworks that are being used by the leaks bots. Even though a leak has been
  43. # fixed, it will be listed here until the bot has been updated with the newer frameworks.
  44. def _types_to_exlude_from_leaks(self):
  45. # Currently we don't have any type excludes from OS leaks, but we will likely again in the future.
  46. return []
  47. def _callstacks_to_exclude_from_leaks(self):
  48. callstacks = [
  49. "Flash_EnforceLocalSecurity", # leaks in Flash plug-in code, rdar://problem/4449747
  50. "ScanFromString", # <http://code.google.com/p/angleproject/issues/detail?id=249> leak in ANGLE
  51. ]
  52. if self._port.is_snowleopard():
  53. callstacks += [
  54. "readMakerNoteProps", # <rdar://problem/7156432> leak in ImageIO
  55. "QTKitMovieControllerView completeUISetup", # <rdar://problem/7155156> leak in QTKit
  56. "getVMInitArgs", # <rdar://problem/7714444> leak in Java
  57. "Java_java_lang_System_initProperties", # <rdar://problem/7714465> leak in Java
  58. "glrCompExecuteKernel", # <rdar://problem/7815391> leak in graphics driver while using OpenGL
  59. "NSNumberFormatter getObjectValue:forString:errorDescription:", # <rdar://problem/7149350> Leak in NSNumberFormatter
  60. ]
  61. elif self._port.is_lion():
  62. callstacks += [
  63. "FigByteFlumeCustomURLCreateWithURL", # <rdar://problem/10461926> leak in CoreMedia
  64. "PDFPage\(PDFPageInternal\) pageLayoutIfAvail", # <rdar://problem/10462055> leak in PDFKit
  65. "SecTransformExecute", # <rdar://problem/10470667> leak in Security.framework
  66. "_NSCopyStyleRefForFocusRingStyleClip", # <rdar://problem/10462031> leak in AppKit
  67. ]
  68. return callstacks
  69. def _leaks_args(self, pid):
  70. leaks_args = []
  71. for callstack in self._callstacks_to_exclude_from_leaks():
  72. leaks_args += ['--exclude-callstack=%s' % callstack]
  73. for excluded_type in self._types_to_exlude_from_leaks():
  74. leaks_args += ['--exclude-type=%s' % excluded_type]
  75. leaks_args.append(pid)
  76. return leaks_args
  77. def _parse_leaks_output(self, leaks_output):
  78. _, count, bytes = re.search(r'Process (?P<pid>\d+): (?P<count>\d+) leaks? for (?P<bytes>\d+) total', leaks_output).groups()
  79. excluded_match = re.search(r'(?P<excluded>\d+) leaks? excluded', leaks_output)
  80. excluded = excluded_match.group('excluded') if excluded_match else 0
  81. return int(count), int(excluded), int(bytes)
  82. def leaks_files_in_directory(self, directory):
  83. return self._filesystem.glob(self._filesystem.join(directory, "*-leaks.txt"))
  84. def leaks_file_name(self, process_name, process_pid):
  85. # We include the number of files this worker has already written in the name to prevent overwritting previous leak results..
  86. return "%s-%s-leaks.txt" % (process_name, process_pid)
  87. def count_total_bytes_and_unique_leaks(self, leak_files):
  88. merge_depth = 5 # ORWT had a --merge-leak-depth argument, but that seems out of scope for the run-webkit-tests tool.
  89. args = [
  90. '--merge-depth',
  91. merge_depth,
  92. ] + leak_files
  93. try:
  94. parse_malloc_history_output = self._port._run_script("parse-malloc-history", args, include_configuration_arguments=False)
  95. except ScriptError, e:
  96. _log.warn("Failed to parse leaks output: %s" % e.message_with_output())
  97. return
  98. # total: 5,888 bytes (0 bytes excluded).
  99. unique_leak_count = len(re.findall(r'^(\d*)\scalls', parse_malloc_history_output, re.MULTILINE))
  100. total_bytes_string = re.search(r'^total\:\s(.+)\s\(', parse_malloc_history_output, re.MULTILINE).group(1)
  101. return (total_bytes_string, unique_leak_count)
  102. def count_total_leaks(self, leak_file_paths):
  103. total_leaks = 0
  104. for leak_file_path in leak_file_paths:
  105. # Leaks have been seen to include non-utf8 data, so we use read_binary_file.
  106. # See https://bugs.webkit.org/show_bug.cgi?id=71112.
  107. leaks_output = self._filesystem.read_binary_file(leak_file_path)
  108. count, _, _ = self._parse_leaks_output(leaks_output)
  109. total_leaks += count
  110. return total_leaks
  111. def check_for_leaks(self, process_name, process_pid):
  112. _log.debug("Checking for leaks in %s" % process_name)
  113. try:
  114. # Oddly enough, run-leaks (or the underlying leaks tool) does not seem to always output utf-8,
  115. # thus we pass decode_output=False. Without this code we've seen errors like:
  116. # "UnicodeDecodeError: 'utf8' codec can't decode byte 0x88 in position 779874: unexpected code byte"
  117. leaks_output = self._port._run_script("run-leaks", self._leaks_args(process_pid), include_configuration_arguments=False, decode_output=False)
  118. except ScriptError, e:
  119. _log.warn("Failed to run leaks tool: %s" % e.message_with_output())
  120. return
  121. # FIXME: We end up parsing this output 3 times. Once here and twice for summarizing.
  122. count, excluded, bytes = self._parse_leaks_output(leaks_output)
  123. adjusted_count = count - excluded
  124. if not adjusted_count:
  125. return
  126. leaks_filename = self.leaks_file_name(process_name, process_pid)
  127. leaks_output_path = self._filesystem.join(self._port.results_directory(), leaks_filename)
  128. self._filesystem.write_binary_file(leaks_output_path, leaks_output)
  129. # FIXME: Ideally we would not be logging from the worker process, but rather pass the leak
  130. # information back to the manager and have it log.
  131. if excluded:
  132. _log.info("%s leaks (%s bytes including %s excluded leaks) were found, details in %s" % (adjusted_count, bytes, excluded, leaks_output_path))
  133. else:
  134. _log.info("%s leaks (%s bytes) were found, details in %s" % (count, bytes, leaks_output_path))