• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# Copyright 2017 the V8 project authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""
7Wrapper script for verify-predictable mode. D8 is expected to be compiled with
8v8_enable_verify_predictable.
9
10The actual test command is expected to be passed to this wraper as is. E.g.:
11predictable_wrapper.py path/to/d8 --test --predictable --flag1 --flag2
12
13The command is run up to three times and the printed allocation hash is
14compared. Differences are reported as errors.
15"""
16
17
18# for py2/py3 compatibility
19from __future__ import absolute_import
20from __future__ import print_function
21
22import sys
23
24from testrunner.local import command
25from testrunner.local import utils
26
27
28MAX_TRIES = 3
29TIMEOUT = 120
30
31# Predictable mode works only when run on the host os.
32command.setup(utils.GuessOS(), None)
33
34def maybe_decode(message):
35  if not isinstance(message, str):
36    return message.decode()
37  return message
38
39
40def main(args):
41  def allocation_str(stdout):
42    for line in reversed((stdout or '').splitlines()):
43      if maybe_decode(line).startswith('### Allocations = '):
44        return line
45    return None
46
47  cmd = command.Command(
48      args[0], args[1:], timeout=TIMEOUT, handle_sigterm=True)
49
50  previous_allocations = None
51  for run in range(1, MAX_TRIES + 1):
52    print('### Predictable run #%d' % run)
53    output = cmd.execute()
54    if output.stdout:
55      print('### Stdout:')
56      print(output.stdout)
57    if output.stderr:
58      print('### Stderr:')
59      print(output.stderr)
60    print('### Return code: %s' % output.exit_code)
61    if output.HasTimedOut():
62      # If we get a timeout in any run, we are in an unpredictable state. Just
63      # report it as a failure and don't rerun.
64      print('### Test timed out')
65      return 1
66    allocations = allocation_str(output.stdout)
67    if not allocations:
68      print ('### Test had no allocation output. Ensure this is built '
69             'with v8_enable_verify_predictable and that '
70             '--verify-predictable is passed at the cmd line.')
71      return 2
72    if previous_allocations and previous_allocations != allocations:
73      print('### Allocations differ')
74      return 3
75    if run >= MAX_TRIES:
76      # No difference on the last run -> report a success.
77      return 0
78    previous_allocations = allocations
79  # Unreachable.
80  assert False
81
82
83if __name__ == '__main__':
84  sys.exit(main(sys.argv[1:]))
85