1a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block#!/usr/bin/python
2a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block#
3a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block# Copyright 2009 the V8 project authors. All rights reserved.
4a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block# Redistribution and use in source and binary forms, with or without
5a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block# modification, are permitted provided that the following conditions are
6a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block# met:
7a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block#
8a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block#     * Redistributions of source code must retain the above copyright
9a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block#       notice, this list of conditions and the following disclaimer.
10a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block#     * Redistributions in binary form must reproduce the above
11a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block#       copyright notice, this list of conditions and the following
12a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block#       disclaimer in the documentation and/or other materials provided
13a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block#       with the distribution.
14a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block#     * Neither the name of Google Inc. nor the names of its
15a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block#       contributors may be used to endorse or promote products derived
16a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block#       from this software without specific prior written permission.
17a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block#
18a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
30a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block# Simple wrapper for running valgrind and checking the output on
31a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block# stderr for memory leaks.
32a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
33a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockimport subprocess
34a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockimport sys
35a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockimport re
36a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
37a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockVALGRIND_ARGUMENTS = [
38a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  'valgrind',
39a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  '--error-exitcode=1',
40a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  '--leak-check=full',
41a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  '--smc-check=all'
42a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block]
43a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
44a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block# Compute the command line.
45a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockcommand = VALGRIND_ARGUMENTS + sys.argv[1:]
46a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
47a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block# Run valgrind.
48a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockprocess = subprocess.Popen(command, stderr=subprocess.PIPE)
49a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockcode = process.wait();
50a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockerrors = process.stderr.readlines();
51a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
52a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block# If valgrind produced an error, we report that to the user.
53a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockif code != 0:
54a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  sys.stderr.writelines(errors)
55a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  sys.exit(code)
56a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
57a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block# Look through the leak details and make sure that we don't
58a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block# have any definitely, indirectly, and possibly lost bytes.
59a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockLEAK_RE = r"(?:definitely|indirectly|possibly) lost: "
60a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockLEAK_LINE_MATCHER = re.compile(LEAK_RE)
61a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockLEAK_OKAY_MATCHER = re.compile(r"lost: 0 bytes in 0 blocks")
62a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockleaks = []
63a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockfor line in errors:
64a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  if LEAK_LINE_MATCHER.search(line):
65a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    leaks.append(line)
66a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    if not LEAK_OKAY_MATCHER.search(line):
67a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block      sys.stderr.writelines(errors)
68a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block      sys.exit(1)
69a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
70a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block# Make sure we found between 2 and 3 leak lines.
71a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockif len(leaks) < 2 or len(leaks) > 3:
72a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  sys.stderr.writelines(errors)
73a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  sys.stderr.write('\n\n#### Malformed valgrind output.\n#### Exiting.\n')
74a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  sys.exit(1)
75a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
76a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block# No leaks found.
77a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blocksys.exit(0)
78