Flutter Engine
The Flutter Engine
valgrind.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2#
3# Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
4# for details. All rights reserved. Use of this source code is governed by a
5# BSD-style license that can be found in the LICENSE file.
6
7# Simple wrapper for running Valgrind and checking the output on
8# stderr for memory leaks.
9
10import subprocess
11import sys
12import re
13
14VALGRIND_ARGUMENTS = [
15 'valgrind',
16 '--error-exitcode=1',
17 '--leak-check=full',
18 '--trace-children=yes',
19 '--ignore-ranges=0x000-0xFFF', # Used for implicit null checks.
20 '--vex-iropt-level=1' # Valgrind crashes with the default level (2).
21]
22
23# Compute the command line.
24command = VALGRIND_ARGUMENTS + sys.argv[1:]
25
26# Run Valgrind.
27process = subprocess.Popen(
28 command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
29code = process.wait()
30output = process.stdout.readlines()
31errors = process.stderr.readlines()
32
33# Always print the output, but leave out the 3 line banner printed
34# by certain versions of Valgrind.
35if len(output) > 0 and output[0].startswith("** VALGRIND_ROOT="):
36 output = output[3:]
37sys.stdout.writelines(output)
38
39# If Valgrind produced an error, we report that to the user.
40if code != 0:
41 sys.stderr.writelines(errors)
42 sys.exit(code)
43
44# Look through the leak details and make sure that we don't have
45# any definitely or indirectly lost bytes. We allow possibly lost
46# bytes to lower the risk of false positives.
47LEAK_RE = r"(?:definitely|indirectly) lost:"
48LEAK_LINE_MATCHER = re.compile(LEAK_RE)
49LEAK_OKAY_MATCHER = re.compile(r"lost: 0 bytes in 0 blocks")
50leaks = []
51for line in errors:
52 if LEAK_LINE_MATCHER.search(line):
53 leaks.append(line)
54 if not LEAK_OKAY_MATCHER.search(line):
55 sys.stderr.writelines(errors)
56 sys.exit(1)
57
58# Make sure we found the right number of leak lines.
59if not len(leaks) in [0, 2, 3]:
60 sys.stderr.writelines(errors)
61 sys.stderr.write('\n\n#### Malformed Valgrind output.\n#### Exiting.\n')
62 sys.exit(1)
63
64# Success.
65sys.exit(0)