summaryrefslogtreecommitdiffstats
path: root/testing/mochitest/leaks.py
blob: 76b661096332771ebaee720bd5d192659c3af023 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/

# The content of this file comes orginally from automationutils.py
# and *should* be revised.

import re
from operator import itemgetter

RE_DOCSHELL = re.compile("I\/DocShellAndDOMWindowLeak ([+\-]{2})DOCSHELL")
RE_DOMWINDOW = re.compile("I\/DocShellAndDOMWindowLeak ([+\-]{2})DOMWINDOW")


class ShutdownLeaks(object):

    """
    Parses the mochitest run log when running a debug build, assigns all leaked
    DOM windows (that are still around after test suite shutdown, despite running
    the GC) to the tests that created them and prints leak statistics.
    """

    def __init__(self, logger):
        self.logger = logger
        self.tests = []
        self.leakedWindows = {}
        self.hiddenWindowsCount = 0
        self.leakedDocShells = set()
        self.hiddenDocShellsCount = 0
        self.numDocShellCreatedLogsSeen = 0
        self.numDocShellDestroyedLogsSeen = 0
        self.numDomWindowCreatedLogsSeen = 0
        self.numDomWindowDestroyedLogsSeen = 0
        self.currentTest = None
        self.seenShutdown = set()

    def log(self, message):
        action = message["action"]

        # Remove 'log' when clipboard is gone and/or structured.
        if action in ("log", "process_output"):
            line = message["message"] if action == "log" else message["data"]

            m = RE_DOMWINDOW.search(line)
            if m:
                self._logWindow(line, m.group(1) == "++")
                return

            m = RE_DOCSHELL.search(line)
            if m:
                self._logDocShell(line, m.group(1) == "++")
                return

            if line.startswith("Completed ShutdownLeaks collections in process"):
                pid = int(line.split()[-1])
                self.seenShutdown.add(pid)
        elif action == "test_start":
            fileName = message["test"].replace(
                "chrome://mochitests/content/browser/", ""
            )
            self.currentTest = {
                "fileName": fileName,
                "windows": set(),
                "docShells": set(),
            }
        elif action == "test_end":
            # don't track a test if no windows or docShells leaked
            if self.currentTest and (
                self.currentTest["windows"] or self.currentTest["docShells"]
            ):
                self.tests.append(self.currentTest)
            self.currentTest = None

    def process(self):
        failures = 0

        if not self.seenShutdown:
            self.logger.error(
                "TEST-UNEXPECTED-FAIL | ShutdownLeaks | process() called before end of test suite"
            )
            failures += 1

        if (
            self.numDocShellCreatedLogsSeen == 0
            or self.numDocShellDestroyedLogsSeen == 0
        ):
            self.logger.error(
                "TEST-UNEXPECTED-FAIL | did not see DOCSHELL log strings."
                " this occurs if the DOCSHELL logging gets disabled by"
                " something. %d created seen %d destroyed seen"
                % (self.numDocShellCreatedLogsSeen, self.numDocShellDestroyedLogsSeen)
            )
            failures += 1
        else:
            self.logger.info(
                "TEST-INFO | Confirming we saw %d DOCSHELL created and %d destroyed log"
                " strings."
                % (self.numDocShellCreatedLogsSeen, self.numDocShellDestroyedLogsSeen)
            )

        if (
            self.numDomWindowCreatedLogsSeen == 0
            or self.numDomWindowDestroyedLogsSeen == 0
        ):
            self.logger.error(
                "TEST-UNEXPECTED-FAIL | did not see DOMWINDOW log strings."
                " this occurs if the DOMWINDOW logging gets disabled by"
                " something%d created seen %d destroyed seen"
                % (self.numDomWindowCreatedLogsSeen, self.numDomWindowDestroyedLogsSeen)
            )
            failures += 1
        else:
            self.logger.info(
                "TEST-INFO | Confirming we saw %d DOMWINDOW created and %d destroyed log"
                " strings."
                % (self.numDomWindowCreatedLogsSeen, self.numDomWindowDestroyedLogsSeen)
            )

        errors = []
        for test in self._parseLeakingTests():
            for url, count in self._zipLeakedWindows(test["leakedWindows"]):
                errors.append(
                    {
                        "test": test["fileName"],
                        "msg": "leaked %d window(s) until shutdown [url = %s]"
                        % (count, url),
                    }
                )
                failures += 1

            if test["leakedWindowsString"]:
                self.logger.info(
                    "TEST-INFO | %s | windows(s) leaked: %s"
                    % (test["fileName"], test["leakedWindowsString"])
                )

            if test["leakedDocShells"]:
                errors.append(
                    {
                        "test": test["fileName"],
                        "msg": "leaked %d docShell(s) until shutdown"
                        % (len(test["leakedDocShells"])),
                    }
                )
                failures += 1
                self.logger.info(
                    "TEST-INFO | %s | docShell(s) leaked: %s"
                    % (
                        test["fileName"],
                        ", ".join(
                            [
                                "[pid = %s] [id = %s]" % x
                                for x in test["leakedDocShells"]
                            ]
                        ),
                    )
                )

            if test["hiddenWindowsCount"] > 0:
                # Note: to figure out how many hidden windows were created, we divide
                # this number by 2, because 1 hidden window creation implies in
                # 1 outer window + 1 inner window.
                # pylint --py3k W1619
                self.logger.info(
                    "TEST-INFO | %s | This test created %d hidden window(s)"
                    % (test["fileName"], test["hiddenWindowsCount"] / 2)
                )

            if test["hiddenDocShellsCount"] > 0:
                self.logger.info(
                    "TEST-INFO | %s | This test created %d hidden docshell(s)"
                    % (test["fileName"], test["hiddenDocShellsCount"])
                )

        return failures, errors

    def _logWindow(self, line, created):
        pid = self._parseValue(line, "pid")
        serial = self._parseValue(line, "serial")
        self.numDomWindowCreatedLogsSeen += 1 if created else 0
        self.numDomWindowDestroyedLogsSeen += 0 if created else 1

        # log line has invalid format
        if not pid or not serial:
            self.logger.error(
                "TEST-UNEXPECTED-FAIL | ShutdownLeaks | failed to parse line"
            )
            self.logger.error("TEST-INFO | ShutdownLeaks | Unparsable line <%s>" % line)
            return

        key = (pid, serial)

        if self.currentTest:
            windows = self.currentTest["windows"]
            if created:
                windows.add(key)
            else:
                windows.discard(key)
        elif int(pid) in self.seenShutdown and not created:
            url = self._parseValue(line, "url")
            if not self._isHiddenWindowURL(url):
                self.leakedWindows[key] = url
            else:
                self.hiddenWindowsCount += 1

    def _logDocShell(self, line, created):
        pid = self._parseValue(line, "pid")
        id = self._parseValue(line, "id")
        self.numDocShellCreatedLogsSeen += 1 if created else 0
        self.numDocShellDestroyedLogsSeen += 0 if created else 1

        # log line has invalid format
        if not pid or not id:
            self.logger.error(
                "TEST-UNEXPECTED-FAIL | ShutdownLeaks | failed to parse line"
            )
            self.logger.error("TEST-INFO | ShutdownLeaks | Unparsable line <%s>" % line)
            return

        key = (pid, id)

        if self.currentTest:
            docShells = self.currentTest["docShells"]
            if created:
                docShells.add(key)
            else:
                docShells.discard(key)
        elif int(pid) in self.seenShutdown and not created:
            url = self._parseValue(line, "url")
            if not self._isHiddenWindowURL(url):
                self.leakedDocShells.add(key)
            else:
                self.hiddenDocShellsCount += 1

    def _parseValue(self, line, name):
        match = re.search("\[%s = (.+?)\]" % name, line)
        if match:
            return match.group(1)
        return None

    def _parseLeakingTests(self):
        leakingTests = []

        for test in self.tests:
            leakedWindows = [id for id in test["windows"] if id in self.leakedWindows]
            test["leakedWindows"] = [self.leakedWindows[id] for id in leakedWindows]
            test["hiddenWindowsCount"] = self.hiddenWindowsCount
            test["leakedWindowsString"] = ", ".join(
                ["[pid = %s] [serial = %s]" % x for x in leakedWindows]
            )
            test["leakedDocShells"] = [
                id for id in test["docShells"] if id in self.leakedDocShells
            ]
            test["hiddenDocShellsCount"] = self.hiddenDocShellsCount
            test["leakCount"] = len(test["leakedWindows"]) + len(
                test["leakedDocShells"]
            )

            if (
                test["leakCount"]
                or test["hiddenWindowsCount"]
                or test["hiddenDocShellsCount"]
            ):
                leakingTests.append(test)

        return sorted(leakingTests, key=itemgetter("leakCount"), reverse=True)

    def _zipLeakedWindows(self, leakedWindows):
        counts = []
        counted = set()

        for url in leakedWindows:
            if url not in counted:
                counts.append((url, leakedWindows.count(url)))
                counted.add(url)

        return sorted(counts, key=itemgetter(1), reverse=True)

    def _isHiddenWindowURL(self, url):
        return (
            url == "resource://gre-resources/hiddenWindow.html"
            or url == "chrome://browser/content/hiddenWindowMac.xhtml"  # Win / Linux
        )  # Mac


class LSANLeaks(object):

    """
    Parses the log when running an LSAN build, looking for interesting stack frames
    in allocation stacks, and prints out reports.
    """

    def __init__(self, logger):
        self.logger = logger
        self.inReport = False
        self.fatalError = False
        self.symbolizerError = False
        self.foundFrames = set([])
        self.recordMoreFrames = None
        self.currStack = None
        self.maxNumRecordedFrames = 4

        # Don't various allocation-related stack frames, as they do not help much to
        # distinguish different leaks.
        unescapedSkipList = [
            "malloc",
            "js_malloc",
            "js_arena_malloc",
            "malloc_",
            "__interceptor_malloc",
            "moz_xmalloc",
            "calloc",
            "js_calloc",
            "js_arena_calloc",
            "calloc_",
            "__interceptor_calloc",
            "moz_xcalloc",
            "realloc",
            "js_realloc",
            "js_arena_realloc",
            "realloc_",
            "__interceptor_realloc",
            "moz_xrealloc",
            "new",
            "js::MallocProvider",
        ]
        self.skipListRegExp = re.compile(
            "^" + "|".join([re.escape(f) for f in unescapedSkipList]) + "$"
        )

        self.startRegExp = re.compile(
            "==\d+==ERROR: LeakSanitizer: detected memory leaks"
        )
        self.fatalErrorRegExp = re.compile(
            "==\d+==LeakSanitizer has encountered a fatal error."
        )
        self.symbolizerOomRegExp = re.compile(
            "LLVMSymbolizer: error reading file: Cannot allocate memory"
        )
        self.stackFrameRegExp = re.compile("    #\d+ 0x[0-9a-f]+ in ([^(</]+)")
        self.sysLibStackFrameRegExp = re.compile(
            "    #\d+ 0x[0-9a-f]+ \(([^+]+)\+0x[0-9a-f]+\)"
        )

    def log(self, line, path=""):
        if re.match(self.startRegExp, line):
            self.inReport = True
            return

        if re.match(self.fatalErrorRegExp, line):
            self.fatalError = True
            return

        if re.match(self.symbolizerOomRegExp, line):
            self.symbolizerError = True
            return

        if not self.inReport:
            return

        if line.startswith("Direct leak") or line.startswith("Indirect leak"):
            self._finishStack(path)
            self.recordMoreFrames = True
            self.currStack = []
            return

        if line.startswith("SUMMARY: AddressSanitizer"):
            self._finishStack(path)
            self.inReport = False
            return

        if not self.recordMoreFrames:
            return

        stackFrame = re.match(self.stackFrameRegExp, line)
        if stackFrame:
            # Split the frame to remove any return types.
            frame = stackFrame.group(1).split()[-1]
            if not re.match(self.skipListRegExp, frame):
                self._recordFrame(frame)
            return

        sysLibStackFrame = re.match(self.sysLibStackFrameRegExp, line)
        if sysLibStackFrame:
            # System library stack frames will never match the skip list,
            # so don't bother checking if they do.
            self._recordFrame(sysLibStackFrame.group(1))

        # If we don't match either of these, just ignore the frame.
        # We'll end up with "unknown stack" if everything is ignored.

    def process(self):
        failures = 0

        if self.fatalError:
            self.logger.error(
                "TEST-UNEXPECTED-FAIL | LeakSanitizer | LeakSanitizer "
                "has encountered a fatal error."
            )
            failures += 1

        if self.symbolizerError:
            self.logger.error(
                "TEST-UNEXPECTED-FAIL | LeakSanitizer | LLVMSymbolizer "
                "was unable to allocate memory."
            )
            failures += 1
            self.logger.info(
                "TEST-INFO | LeakSanitizer | This will cause leaks that "
                "should be ignored to instead be reported as an error"
            )

        if self.foundFrames:
            self.logger.info(
                "TEST-INFO | LeakSanitizer | To show the "
                "addresses of leaked objects add report_objects=1 to LSAN_OPTIONS"
            )
            self.logger.info(
                "TEST-INFO | LeakSanitizer | This can be done "
                "in testing/mozbase/mozrunner/mozrunner/utils.py"
            )

        frames = list(self.foundFrames)
        frames.sort()
        for f in frames:
            if self.scope:
                f = "%s | %s" % (f, self.scope)
            self.logger.error("TEST-UNEXPECTED-FAIL | LeakSanitizer leak at " + f)
            failures += 1

        return failures

    def _finishStack(self, path=""):
        if self.recordMoreFrames and len(self.currStack) == 0:
            self.currStack = ["unknown stack"]
        if self.currStack:
            self.foundFrames.add(", ".join(self.currStack))
            self.currStack = None
            self.scope = path
        self.recordMoreFrames = False
        self.numRecordedFrames = 0

    def _recordFrame(self, frame):
        self.currStack.append(frame)
        self.numRecordedFrames += 1
        if self.numRecordedFrames >= self.maxNumRecordedFrames:
            self.recordMoreFrames = False