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.
32
from webkitpy.common.system.executive import ScriptError
34
_log = logging.getLogger(__name__)
37
# If other ports/platforms decide to support --leaks, we should see about sharing as much of this code as possible.
38
# Right now this code is only used by Apple's MacPort.
40
class LeakDetector(object):
41
def __init__(self, port):
42
# We should operate on a "platform" not a port here.
44
self._executive = port._executive
45
self._filesystem = port._filesystem
47
# We exclude the following reported leaks so they do not get in our way when looking for WebKit leaks:
48
# This allows us ignore known leaks and only be alerted when new leaks occur. Some leaks are in the old
49
# versions of the system frameworks that are being used by the leaks bots. Even though a leak has been
50
# fixed, it will be listed here until the bot has been updated with the newer frameworks.
51
def _types_to_exlude_from_leaks(self):
52
# Currently we don't have any type excludes from OS leaks, but we will likely again in the future.
55
def _callstacks_to_exclude_from_leaks(self):
57
"Flash_EnforceLocalSecurity", # leaks in Flash plug-in code, rdar://problem/4449747
58
"ScanFromString", # <http://code.google.com/p/angleproject/issues/detail?id=249> leak in ANGLE
60
if self._port.is_snowleopard():
62
"readMakerNoteProps", # <rdar://problem/7156432> leak in ImageIO
63
"QTKitMovieControllerView completeUISetup", # <rdar://problem/7155156> leak in QTKit
64
"getVMInitArgs", # <rdar://problem/7714444> leak in Java
65
"Java_java_lang_System_initProperties", # <rdar://problem/7714465> leak in Java
66
"glrCompExecuteKernel", # <rdar://problem/7815391> leak in graphics driver while using OpenGL
67
"NSNumberFormatter getObjectValue:forString:errorDescription:", # <rdar://problem/7149350> Leak in NSNumberFormatter
69
elif self._port.is_lion():
71
"FigByteFlumeCustomURLCreateWithURL", # <rdar://problem/10461926> leak in CoreMedia
72
"PDFPage\(PDFPageInternal\) pageLayoutIfAvail", # <rdar://problem/10462055> leak in PDFKit
73
"SecTransformExecute", # <rdar://problem/10470667> leak in Security.framework
74
"_NSCopyStyleRefForFocusRingStyleClip", # <rdar://problem/10462031> leak in AppKit
78
def _leaks_args(self, pid):
80
for callstack in self._callstacks_to_exclude_from_leaks():
81
leaks_args += ['--exclude-callstack=%s' % callstack]
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):
88
_, count, bytes = re.search(r'Process (?P<pid>\d+): (?P<count>\d+) leaks? for (?P<bytes>\d+) total', leaks_output).groups()
89
excluded_match = re.search(r'(?P<excluded>\d+) leaks? excluded', leaks_output)
90
excluded = excluded_match.group('excluded') 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 count_total_bytes_and_unique_leaks(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, re.MULTILINE))
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 count_total_leaks(self, leak_file_paths):
119
for leak_file_path in leak_file_paths:
120
# Leaks have been seen to include non-utf8 data, so we use read_binary_file.
121
# See https://bugs.webkit.org/show_bug.cgi?id=71112.
122
leaks_output = self._filesystem.read_binary_file(leak_file_path)
123
count, _, _ = self._parse_leaks_output(leaks_output)
127
def check_for_leaks(self, process_name, process_pid):
128
_log.debug("Checking for leaks in %s" % process_name)
130
# Oddly enough, run-leaks (or the underlying leaks tool) does not seem to always output utf-8,
131
# thus we pass decode_output=False. Without this code we've seen errors like:
132
# "UnicodeDecodeError: 'utf8' codec can't decode byte 0x88 in position 779874: unexpected code byte"
133
leaks_output = self._port._run_script("run-leaks", self._leaks_args(process_pid), include_configuration_arguments=False, decode_output=False)
134
except ScriptError, e:
135
_log.warn("Failed to run leaks tool: %s" % e.message_with_output())
138
# FIXME: We end up parsing this output 3 times. Once here and twice for summarizing.
139
count, excluded, bytes = self._parse_leaks_output(leaks_output)
140
adjusted_count = count - excluded
141
if not adjusted_count:
144
leaks_filename = self.leaks_file_name(process_name, process_pid)
145
leaks_output_path = self._filesystem.join(self._port.results_directory(), leaks_filename)
146
self._filesystem.write_binary_file(leaks_output_path, leaks_output)
148
# FIXME: Ideally we would not be logging from the worker process, but rather pass the leak
149
# information back to the manager and have it log.
151
_log.info("%s leaks (%s bytes including %s excluded leaks) were found, details in %s" % (adjusted_count, bytes, excluded, leaks_output_path))
153
_log.info("%s leaks (%s bytes) were found, details in %s" % (count, bytes, leaks_output_path))