1 # Copyright (C) 2010 Google Inc. All rights reserved.
3 # Redistribution and use in source and binary forms, with or without
4 # modification, are permitted provided that the following conditions are
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
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.
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.
31 from webkitpy.common.system.executive import ScriptError
34 # If other ports/platforms decide to support --leaks, we should see about sharing as much of this code as possible.
35 # Right now this code is only used by Apple's MacPort.
37 class LeakDetector(object):
38 def __init__(self, port):
39 # We should operate on a "platform" not a port here.
41 self._executive = port._executive
42 self._filesystem = port._filesystem
44 # We exclude the following reported leaks so they do not get in our way when looking for WebKit leaks:
45 # This allows us ignore known leaks and only be alerted when new leaks occur. Some leaks are in the old
46 # versions of the system frameworks that are being used by the leaks bots. Even though a leak has been
47 # fixed, it will be listed here until the bot has been updated with the newer frameworks.
48 def _types_to_exlude_from_leaks(self):
49 # Currently we don't have any type excludes from OS leaks, but we will likely again in the future.
52 def _callstacks_to_exclude_from_leaks(self):
54 "Flash_EnforceLocalSecurity", # leaks in Flash plug-in code, rdar://problem/4449747
56 if self._port.is_leopard():
58 "CFHTTPMessageAppendBytes", # leak in CFNetwork, rdar://problem/5435912
59 "sendDidReceiveDataCallback", # leak in CFNetwork, rdar://problem/5441619
60 "_CFHTTPReadStreamReadMark", # leak in CFNetwork, rdar://problem/5441468
61 "httpProtocolStart", # leak in CFNetwork, rdar://problem/5468837
62 "_CFURLConnectionSendCallbacks", # leak in CFNetwork, rdar://problem/5441600
63 "DispatchQTMsg", # leak in QuickTime, PPC only, rdar://problem/5667132
64 "QTMovieContentView createVisualContext", # leak in QuickTime, PPC only, rdar://problem/5667132
65 "_CopyArchitecturesForJVMVersion", # leak in Java, rdar://problem/5910823
67 elif self._port.is_snowleopard():
69 "readMakerNoteProps", # <rdar://problem/7156432> leak in ImageIO
70 "QTKitMovieControllerView completeUISetup", # <rdar://problem/7155156> leak in QTKit
71 "getVMInitArgs", # <rdar://problem/7714444> leak in Java
72 "Java_java_lang_System_initProperties", # <rdar://problem/7714465> leak in Java
73 "glrCompExecuteKernel", # <rdar://problem/7815391> leak in graphics driver while using OpenGL
74 "NSNumberFormatter getObjectValue:forString:errorDescription:", # <rdar://problem/7149350> Leak in NSNumberFormatter
78 def _leaks_args(self, pid):
80 for callstack in self._callstacks_to_exclude_from_leaks():
81 leaks_args += ['--exclude-callstack="%s"' % callstack] # Callstacks can have spaces in them, so we quote the arg to prevent confusing perl's optparse.
82 for excluded_type in self._types_to_exlude_from_leaks():
83 leaks_args += ['--exclude-type="%s"' % excluded_type]
84 leaks_args.append(pid)
87 def _parse_leaks_output(self, leaks_output, process_pid):
88 count, bytes = re.search(r'Process %s: (\d+) leaks? for (\d+) total' % process_pid, leaks_output).groups()
89 excluded_match = re.search(r'(\d+) leaks? excluded', leaks_output)
90 excluded = excluded_match.group(0) if excluded_match else 0
91 return int(count), int(excluded), int(bytes)
93 def leaks_files_in_directory(self, directory):
94 return self._filesystem.glob(self._filesystem.join(directory, "*-leaks.txt"))
96 def leaks_file_name(self, process_name, process_pid):
97 # We include the number of files this worker has already written in the name to prevent overwritting previous leak results..
98 return "%s-%s-leaks.txt" % (process_name, process_pid)
100 def parse_leak_files(self, leak_files):
101 merge_depth = 5 # ORWT had a --merge-leak-depth argument, but that seems out of scope for the run-webkit-tests tool.
107 parse_malloc_history_output = self._port._run_script("parse-malloc-history", args, include_configuration_arguments=False)
108 except ScriptError, e:
109 _log.warn("Failed to parse leaks output: %s" % e.message_with_output())
112 # total: 5,888 bytes (0 bytes excluded).
113 unique_leak_count = len(re.findall(r'^(\d*)\scalls', parse_malloc_history_output))
114 total_bytes_string = re.search(r'^total\:\s(.+)\s\(', parse_malloc_history_output, re.MULTILINE).group(1)
115 return (total_bytes_string, unique_leak_count)
117 def check_for_leaks(self, process_name, process_pid):
118 _log.debug("Checking for leaks in %s" % process_name)
120 # Oddly enough, run-leaks (or the underlying leaks tool) does not seem to always output utf-8,
121 # thus we pass decode_output=False. Without this code we've seen errors like:
122 # "UnicodeDecodeError: 'utf8' codec can't decode byte 0x88 in position 779874: unexpected code byte"
123 leaks_output = self._port._run_script("run-leaks", self._leaks_args(process_pid), include_configuration_arguments=False, decode_output=False)
124 except ScriptError, e:
125 _log.warn("Failed to run leaks tool: %s" % e.message_with_output())
128 # FIXME: We should consider moving leaks parsing to the end when summarizing is done.
129 count, excluded, bytes = self._parse_leaks_output(leaks_output, process_pid)
130 adjusted_count = count - excluded
131 if not adjusted_count:
134 leaks_filename = self.leaks_file_name(process_name, process_pid)
135 leaks_output_path = self._filesystem.join(self._port.results_directory(), leaks_filename)
136 self._filesystem.write_binary_file(leaks_output_path, leaks_output)
138 # FIXME: Ideally we would not be logging from the worker process, but rather pass the leak
139 # information back to the manager and have it log.
141 _log.info("%s leaks (%s bytes including %s excluded leaks) were found, details in %s" % (adjusted_count, bytes, excluded, leaks_output_path))
143 _log.info("%s leaks (%s bytes) were found, details in %s" % (count, bytes, leaks_output_path))