Chris McDonald | c3e0f26b | 2020-06-04 02:15:14 | [diff] [blame^] | 1 | #!/usr/bin/env python |
[email protected] | 0a88a65 | 2012-03-09 00:34:45 | [diff] [blame] | 2 | # Copyright (c) 2012 The Chromium 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 | """Sets environment variables needed to run a chromium unit test.""" |
| 7 | |
Chris McDonald | c3e0f26b | 2020-06-04 02:15:14 | [diff] [blame^] | 8 | from __future__ import print_function |
Trent Apted | b185243 | 2018-01-25 02:15:10 | [diff] [blame] | 9 | import io |
[email protected] | 0a88a65 | 2012-03-09 00:34:45 | [diff] [blame] | 10 | import os |
Ben Pastene | e5ece81 | 2018-06-20 22:39:34 | [diff] [blame] | 11 | import signal |
[email protected] | 76361be45 | 2012-08-30 22:48:14 | [diff] [blame] | 12 | import stat |
[email protected] | 0a88a65 | 2012-03-09 00:34:45 | [diff] [blame] | 13 | import subprocess |
| 14 | import sys |
Trent Apted | b185243 | 2018-01-25 02:15:10 | [diff] [blame] | 15 | import time |
[email protected] | 0a88a65 | 2012-03-09 00:34:45 | [diff] [blame] | 16 | |
Chris McDonald | c3e0f26b | 2020-06-04 02:15:14 | [diff] [blame^] | 17 | SIX_DIR = os.path.join( |
| 18 | os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'third_party', |
| 19 | 'six') |
| 20 | sys.path.insert(0, SIX_DIR) |
| 21 | |
| 22 | import six |
| 23 | |
[email protected] | 0a88a65 | 2012-03-09 00:34:45 | [diff] [blame] | 24 | # This is hardcoded to be src/ relative to this script. |
| 25 | ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 26 | |
[email protected] | 76361be45 | 2012-08-30 22:48:14 | [diff] [blame] | 27 | CHROME_SANDBOX_ENV = 'CHROME_DEVEL_SANDBOX' |
| 28 | CHROME_SANDBOX_PATH = '/opt/chromium/chrome_sandbox' |
| 29 | |
| 30 | |
earthdok | 748e135 | 2015-01-27 23:04:08 | [diff] [blame] | 31 | def get_sandbox_env(env): |
| 32 | """Returns the environment flags needed for the SUID sandbox to work.""" |
John Abd-El-Malek | 14ae054 | 2014-10-15 17:52:31 | [diff] [blame] | 33 | extra_env = {} |
[email protected] | 76361be45 | 2012-08-30 22:48:14 | [diff] [blame] | 34 | chrome_sandbox_path = env.get(CHROME_SANDBOX_ENV, CHROME_SANDBOX_PATH) |
earthdok | 748e135 | 2015-01-27 23:04:08 | [diff] [blame] | 35 | # The above would silently disable the SUID sandbox if the env value were |
| 36 | # an empty string. We don't want to allow that. http://crbug.com/245376 |
| 37 | # TODO(jln): Remove this check once it's no longer possible to disable the |
| 38 | # sandbox that way. |
| 39 | if not chrome_sandbox_path: |
| 40 | chrome_sandbox_path = CHROME_SANDBOX_PATH |
| 41 | extra_env[CHROME_SANDBOX_ENV] = chrome_sandbox_path |
John Abd-El-Malek | 14ae054 | 2014-10-15 17:52:31 | [diff] [blame] | 42 | |
| 43 | return extra_env |
| 44 | |
| 45 | |
| 46 | def trim_cmd(cmd): |
| 47 | """Removes internal flags from cmd since they're just used to communicate from |
| 48 | the host machine to this script running on the swarm slaves.""" |
earthdok | eeb06530 | 2015-02-04 18:18:04 | [diff] [blame] | 49 | sanitizers = ['asan', 'lsan', 'msan', 'tsan'] |
| 50 | internal_flags = frozenset('--%s=%d' % (name, value) |
| 51 | for name in sanitizers |
| 52 | for value in [0, 1]) |
John Abd-El-Malek | 14ae054 | 2014-10-15 17:52:31 | [diff] [blame] | 53 | return [i for i in cmd if i not in internal_flags] |
[email protected] | 76361be45 | 2012-08-30 22:48:14 | [diff] [blame] | 54 | |
[email protected] | 0a88a65 | 2012-03-09 00:34:45 | [diff] [blame] | 55 | |
[email protected] | 4bf4d63 | 2012-05-31 15:50:30 | [diff] [blame] | 56 | def fix_python_path(cmd): |
| 57 | """Returns the fixed command line to call the right python executable.""" |
| 58 | out = cmd[:] |
| 59 | if out[0] == 'python': |
| 60 | out[0] = sys.executable |
| 61 | elif out[0].endswith('.py'): |
| 62 | out.insert(0, sys.executable) |
| 63 | return out |
| 64 | |
| 65 | |
pcc | 46233c2 | 2017-06-20 22:11:41 | [diff] [blame] | 66 | def get_sanitizer_env(cmd, asan, lsan, msan, tsan, cfi_diag): |
earthdok | eeb06530 | 2015-02-04 18:18:04 | [diff] [blame] | 67 | """Returns the envirnoment flags needed for sanitizer tools.""" |
John Abd-El-Malek | 14ae054 | 2014-10-15 17:52:31 | [diff] [blame] | 68 | |
| 69 | extra_env = {} |
| 70 | |
earthdok | eeb06530 | 2015-02-04 18:18:04 | [diff] [blame] | 71 | # Instruct GTK to use malloc while running sanitizer-instrumented tests. |
earthdok | e6c54a4 | 2014-10-16 18:02:36 | [diff] [blame] | 72 | extra_env['G_SLICE'] = 'always-malloc' |
John Abd-El-Malek | 14ae054 | 2014-10-15 17:52:31 | [diff] [blame] | 73 | |
| 74 | extra_env['NSS_DISABLE_ARENA_FREE_LIST'] = '1' |
| 75 | extra_env['NSS_DISABLE_UNLOAD'] = '1' |
| 76 | |
| 77 | # TODO(glider): remove the symbolizer path once |
| 78 | # https://code.google.com/p/address-sanitizer/issues/detail?id=134 is fixed. |
Nico Weber | 1909731 | 2016-01-29 18:13:15 | [diff] [blame] | 79 | symbolizer_path = os.path.join(ROOT_DIR, |
| 80 | 'third_party', 'llvm-build', 'Release+Asserts', 'bin', 'llvm-symbolizer') |
John Abd-El-Malek | 14ae054 | 2014-10-15 17:52:31 | [diff] [blame] | 81 | |
earthdok | eeb06530 | 2015-02-04 18:18:04 | [diff] [blame] | 82 | if lsan or tsan: |
John Abd-El-Malek | 14ae054 | 2014-10-15 17:52:31 | [diff] [blame] | 83 | # LSan is not sandbox-compatible, so we can use online symbolization. In |
| 84 | # fact, it needs symbolization to be able to apply suppressions. |
| 85 | symbolization_options = ['symbolize=1', |
| 86 | 'external_symbolizer_path=%s' % symbolizer_path] |
pcc | 46233c2 | 2017-06-20 22:11:41 | [diff] [blame] | 87 | elif (asan or msan or cfi_diag) and sys.platform not in ['win32', 'cygwin']: |
timurrrr | 064f952 | 2015-02-12 15:46:32 | [diff] [blame] | 88 | # ASan uses a script for offline symbolization, except on Windows. |
John Abd-El-Malek | 14ae054 | 2014-10-15 17:52:31 | [diff] [blame] | 89 | # Important note: when running ASan with leak detection enabled, we must use |
| 90 | # the LSan symbolization options above. |
| 91 | symbolization_options = ['symbolize=0'] |
vadimsh | 5cf3c44 | 2014-10-20 10:22:38 | [diff] [blame] | 92 | # Set the path to llvm-symbolizer to be used by asan_symbolize.py |
| 93 | extra_env['LLVM_SYMBOLIZER_PATH'] = symbolizer_path |
timurrrr | 7e67df9 | 2015-02-12 21:50:52 | [diff] [blame] | 94 | else: |
| 95 | symbolization_options = [] |
John Abd-El-Malek | 14ae054 | 2014-10-15 17:52:31 | [diff] [blame] | 96 | |
Yves Gerey | 2e22152 | 2019-03-13 08:46:19 | [diff] [blame] | 97 | # Leverage sanitizer to print stack trace on abort (e.g. assertion failure). |
| 98 | symbolization_options.append('handle_abort=1') |
| 99 | |
earthdok | eeb06530 | 2015-02-04 18:18:04 | [diff] [blame] | 100 | if asan: |
| 101 | asan_options = symbolization_options[:] |
| 102 | if lsan: |
| 103 | asan_options.append('detect_leaks=1') |
John Abd-El-Malek | 14ae054 | 2014-10-15 17:52:31 | [diff] [blame] | 104 | |
timurrrr | 7e67df9 | 2015-02-12 21:50:52 | [diff] [blame] | 105 | if asan_options: |
| 106 | extra_env['ASAN_OPTIONS'] = ' '.join(asan_options) |
John Abd-El-Malek | 14ae054 | 2014-10-15 17:52:31 | [diff] [blame] | 107 | |
earthdok | eeb06530 | 2015-02-04 18:18:04 | [diff] [blame] | 108 | if sys.platform == 'darwin': |
| 109 | isolate_output_dir = os.path.abspath(os.path.dirname(cmd[0])) |
| 110 | # This is needed because the test binary has @executable_path embedded in |
| 111 | # it that the OS tries to resolve to the cache directory and not the |
| 112 | # mapped directory. |
| 113 | extra_env['DYLD_LIBRARY_PATH'] = str(isolate_output_dir) |
| 114 | |
| 115 | if lsan: |
| 116 | if asan or msan: |
| 117 | lsan_options = [] |
| 118 | else: |
| 119 | lsan_options = symbolization_options[:] |
| 120 | if sys.platform == 'linux2': |
| 121 | # Use the debug version of libstdc++ under LSan. If we don't, there will |
| 122 | # be a lot of incomplete stack traces in the reports. |
| 123 | extra_env['LD_LIBRARY_PATH'] = '/usr/lib/x86_64-linux-gnu/debug:' |
| 124 | |
earthdok | eeb06530 | 2015-02-04 18:18:04 | [diff] [blame] | 125 | extra_env['LSAN_OPTIONS'] = ' '.join(lsan_options) |
| 126 | |
| 127 | if msan: |
| 128 | msan_options = symbolization_options[:] |
| 129 | if lsan: |
| 130 | msan_options.append('detect_leaks=1') |
| 131 | extra_env['MSAN_OPTIONS'] = ' '.join(msan_options) |
| 132 | |
| 133 | if tsan: |
| 134 | tsan_options = symbolization_options[:] |
| 135 | extra_env['TSAN_OPTIONS'] = ' '.join(tsan_options) |
John Abd-El-Malek | 14ae054 | 2014-10-15 17:52:31 | [diff] [blame] | 136 | |
pcc | 46233c2 | 2017-06-20 22:11:41 | [diff] [blame] | 137 | # CFI uses the UBSan runtime to provide diagnostics. |
| 138 | if cfi_diag: |
| 139 | ubsan_options = symbolization_options[:] + ['print_stacktrace=1'] |
| 140 | extra_env['UBSAN_OPTIONS'] = ' '.join(ubsan_options) |
| 141 | |
John Abd-El-Malek | 14ae054 | 2014-10-15 17:52:31 | [diff] [blame] | 142 | return extra_env |
| 143 | |
| 144 | |
glider | 9d91934 | 2015-02-06 17:42:27 | [diff] [blame] | 145 | def get_sanitizer_symbolize_command(json_path=None, executable_path=None): |
earthdok | 9bd5d58 | 2015-01-29 21:19:36 | [diff] [blame] | 146 | """Construct the command to invoke offline symbolization script.""" |
Nico Weber | 1909731 | 2016-01-29 18:13:15 | [diff] [blame] | 147 | script_path = os.path.join( |
| 148 | ROOT_DIR, 'tools', 'valgrind', 'asan', 'asan_symbolize.py') |
earthdok | 9bd5d58 | 2015-01-29 21:19:36 | [diff] [blame] | 149 | cmd = [sys.executable, script_path] |
| 150 | if json_path is not None: |
| 151 | cmd.append('--test-summary-json-file=%s' % json_path) |
glider | 9d91934 | 2015-02-06 17:42:27 | [diff] [blame] | 152 | if executable_path is not None: |
| 153 | cmd.append('--executable-path=%s' % executable_path) |
earthdok | 9bd5d58 | 2015-01-29 21:19:36 | [diff] [blame] | 154 | return cmd |
| 155 | |
| 156 | |
| 157 | def get_json_path(cmd): |
| 158 | """Extract the JSON test summary path from a command line.""" |
| 159 | json_path_flag = '--test-launcher-summary-output=' |
| 160 | for arg in cmd: |
| 161 | if arg.startswith(json_path_flag): |
| 162 | return arg.split(json_path_flag).pop() |
| 163 | return None |
| 164 | |
| 165 | |
| 166 | def symbolize_snippets_in_json(cmd, env): |
| 167 | """Symbolize output snippets inside the JSON test summary.""" |
| 168 | json_path = get_json_path(cmd) |
| 169 | if json_path is None: |
| 170 | return |
| 171 | |
| 172 | try: |
glider | 9d91934 | 2015-02-06 17:42:27 | [diff] [blame] | 173 | symbolize_command = get_sanitizer_symbolize_command( |
| 174 | json_path=json_path, executable_path=cmd[0]) |
earthdok | 9bd5d58 | 2015-01-29 21:19:36 | [diff] [blame] | 175 | p = subprocess.Popen(symbolize_command, stderr=subprocess.PIPE, env=env) |
| 176 | (_, stderr) = p.communicate() |
| 177 | except OSError as e: |
Chris McDonald | c3e0f26b | 2020-06-04 02:15:14 | [diff] [blame^] | 178 | print('Exception while symbolizing snippets: %s' % e, file=sys.stderr) |
thakis | 45643b4 | 2016-01-29 21:53:42 | [diff] [blame] | 179 | raise |
earthdok | 9bd5d58 | 2015-01-29 21:19:36 | [diff] [blame] | 180 | |
| 181 | if p.returncode != 0: |
Chris McDonald | c3e0f26b | 2020-06-04 02:15:14 | [diff] [blame^] | 182 | print("Error: failed to symbolize snippets in JSON:\n", file=sys.stderr) |
| 183 | print(stderr, file=sys.stderr) |
thakis | 45643b4 | 2016-01-29 21:53:42 | [diff] [blame] | 184 | raise subprocess.CalledProcessError(p.returncode, symbolize_command) |
earthdok | 9bd5d58 | 2015-01-29 21:19:36 | [diff] [blame] | 185 | |
| 186 | |
Trent Apted | b185243 | 2018-01-25 02:15:10 | [diff] [blame] | 187 | def run_command_with_output(argv, stdoutfile, env=None, cwd=None): |
Caleb Rouleau | fd51129 | 2019-02-22 18:22:00 | [diff] [blame] | 188 | """Run command and stream its stdout/stderr to the console & |stdoutfile|. |
| 189 | |
| 190 | Also forward_signals to obey |
| 191 | https://chromium.googlesource.com/infra/luci/luci-py/+/master/appengine/swarming/doc/Bot.md#graceful-termination_aka-the-sigterm-and-sigkill-dance |
| 192 | |
| 193 | Returns: |
| 194 | integer returncode of the subprocess. |
Trent Apted | b185243 | 2018-01-25 02:15:10 | [diff] [blame] | 195 | """ |
Sorin Jianu | e11c985 | 2019-09-12 20:43:55 | [diff] [blame] | 196 | print('Running %r in %r (env: %r)' % (argv, cwd, env)) |
Trent Apted | b185243 | 2018-01-25 02:15:10 | [diff] [blame] | 197 | assert stdoutfile |
Ned Nguyen | 7f43ff6 | 2018-10-04 04:58:46 | [diff] [blame] | 198 | with io.open(stdoutfile, 'wb') as writer, \ |
| 199 | io.open(stdoutfile, 'rb', 1) as reader: |
Caleb Rouleau | 6844df15 | 2019-09-11 01:11:59 | [diff] [blame] | 200 | process = _popen(argv, env=env, cwd=cwd, stdout=writer, |
| 201 | stderr=subprocess.STDOUT) |
Ben Pastene | e5ece81 | 2018-06-20 22:39:34 | [diff] [blame] | 202 | forward_signals([process]) |
Trent Apted | b185243 | 2018-01-25 02:15:10 | [diff] [blame] | 203 | while process.poll() is None: |
| 204 | sys.stdout.write(reader.read()) |
Caleb Rouleau | fd51129 | 2019-02-22 18:22:00 | [diff] [blame] | 205 | # This sleep is needed for signal propagation. See the |
| 206 | # wait_with_signals() docstring. |
Trent Apted | b185243 | 2018-01-25 02:15:10 | [diff] [blame] | 207 | time.sleep(0.1) |
| 208 | # Read the remaining. |
| 209 | sys.stdout.write(reader.read()) |
Sorin Jianu | e11c985 | 2019-09-12 20:43:55 | [diff] [blame] | 210 | print('Command %r returned exit code %d' % (argv, process.returncode)) |
Trent Apted | b185243 | 2018-01-25 02:15:10 | [diff] [blame] | 211 | return process.returncode |
| 212 | |
| 213 | |
Caleb Rouleau | fd51129 | 2019-02-22 18:22:00 | [diff] [blame] | 214 | def run_command(argv, env=None, cwd=None, log=True): |
| 215 | """Run command and stream its stdout/stderr both to stdout. |
| 216 | |
| 217 | Also forward_signals to obey |
| 218 | https://chromium.googlesource.com/infra/luci/luci-py/+/master/appengine/swarming/doc/Bot.md#graceful-termination_aka-the-sigterm-and-sigkill-dance |
| 219 | |
| 220 | Returns: |
| 221 | integer returncode of the subprocess. |
| 222 | """ |
| 223 | if log: |
Sorin Jianu | e11c985 | 2019-09-12 20:43:55 | [diff] [blame] | 224 | print('Running %r in %r (env: %r)' % (argv, cwd, env)) |
Caleb Rouleau | 6844df15 | 2019-09-11 01:11:59 | [diff] [blame] | 225 | process = _popen(argv, env=env, cwd=cwd, stderr=subprocess.STDOUT) |
Caleb Rouleau | fd51129 | 2019-02-22 18:22:00 | [diff] [blame] | 226 | forward_signals([process]) |
| 227 | return wait_with_signals(process) |
| 228 | |
| 229 | |
Caleb Rouleau | 84e3e81 | 2019-05-30 23:34:50 | [diff] [blame] | 230 | def run_command_output_to_handle(argv, file_handle, env=None, cwd=None): |
| 231 | """Run command and stream its stdout/stderr both to |file_handle|. |
| 232 | |
| 233 | Also forward_signals to obey |
| 234 | https://chromium.googlesource.com/infra/luci/luci-py/+/master/appengine/swarming/doc/Bot.md#graceful-termination_aka-the-sigterm-and-sigkill-dance |
| 235 | |
| 236 | Returns: |
| 237 | integer returncode of the subprocess. |
| 238 | """ |
Sorin Jianu | e11c985 | 2019-09-12 20:43:55 | [diff] [blame] | 239 | print('Running %r in %r (env: %r)' % (argv, cwd, env)) |
Caleb Rouleau | 6844df15 | 2019-09-11 01:11:59 | [diff] [blame] | 240 | process = _popen( |
Caleb Rouleau | 84e3e81 | 2019-05-30 23:34:50 | [diff] [blame] | 241 | argv, env=env, cwd=cwd, stderr=file_handle, stdout=file_handle) |
| 242 | forward_signals([process]) |
| 243 | exit_code = wait_with_signals(process) |
Sorin Jianu | e11c985 | 2019-09-12 20:43:55 | [diff] [blame] | 244 | print('Command returned exit code %d' % exit_code) |
Caleb Rouleau | 84e3e81 | 2019-05-30 23:34:50 | [diff] [blame] | 245 | return exit_code |
| 246 | |
| 247 | |
Caleb Rouleau | fd51129 | 2019-02-22 18:22:00 | [diff] [blame] | 248 | def wait_with_signals(process): |
| 249 | """A version of process.wait() that works cross-platform. |
| 250 | |
| 251 | This version properly surfaces the SIGBREAK signal. |
| 252 | |
| 253 | From reading the subprocess.py source code, it seems we need to explicitly |
| 254 | call time.sleep(). The reason is that subprocess.Popen.wait() on Windows |
| 255 | directly calls WaitForSingleObject(), but only time.sleep() properly surface |
| 256 | the SIGBREAK signal. |
| 257 | |
| 258 | Refs: |
| 259 | https://github.com/python/cpython/blob/v2.7.15/Lib/subprocess.py#L692 |
| 260 | https://github.com/python/cpython/blob/v2.7.15/Modules/timemodule.c#L1084 |
| 261 | |
| 262 | Returns: |
| 263 | returncode of the process. |
| 264 | """ |
| 265 | while process.poll() is None: |
| 266 | time.sleep(0.1) |
| 267 | return process.returncode |
| 268 | |
| 269 | |
Ben Pastene | e5ece81 | 2018-06-20 22:39:34 | [diff] [blame] | 270 | def forward_signals(procs): |
| 271 | """Forwards unix's SIGTERM or win's CTRL_BREAK_EVENT to the given processes. |
| 272 | |
| 273 | This plays nicely with swarming's timeout handling. See also |
| 274 | https://chromium.googlesource.com/infra/luci/luci-py/+/master/appengine/swarming/doc/Bot.md#graceful-termination_aka-the-sigterm-and-sigkill-dance |
| 275 | |
| 276 | Args: |
| 277 | procs: A list of subprocess.Popen objects representing child processes. |
| 278 | """ |
| 279 | assert all(isinstance(p, subprocess.Popen) for p in procs) |
| 280 | def _sig_handler(sig, _): |
| 281 | for p in procs: |
Ilia Samsonov | a0083530 | 2019-04-19 17:37:59 | [diff] [blame] | 282 | if p.poll() is not None: |
| 283 | continue |
Andrew Grieve | 95a2d33 | 2018-06-28 02:22:59 | [diff] [blame] | 284 | # SIGBREAK is defined only for win32. |
| 285 | if sys.platform == 'win32' and sig == signal.SIGBREAK: |
Ben Pastene | e5ece81 | 2018-06-20 22:39:34 | [diff] [blame] | 286 | p.send_signal(signal.CTRL_BREAK_EVENT) |
| 287 | else: |
| 288 | p.send_signal(sig) |
| 289 | if sys.platform == 'win32': |
| 290 | signal.signal(signal.SIGBREAK, _sig_handler) |
| 291 | else: |
| 292 | signal.signal(signal.SIGTERM, _sig_handler) |
Ilia Samsonov | a0083530 | 2019-04-19 17:37:59 | [diff] [blame] | 293 | signal.signal(signal.SIGINT, _sig_handler) |
Ben Pastene | e5ece81 | 2018-06-20 22:39:34 | [diff] [blame] | 294 | |
Haiyang Pan | 94c2659 | 2019-08-13 17:48:11 | [diff] [blame] | 295 | |
Trent Apted | b185243 | 2018-01-25 02:15:10 | [diff] [blame] | 296 | def run_executable(cmd, env, stdoutfile=None): |
[email protected] | 0a88a65 | 2012-03-09 00:34:45 | [diff] [blame] | 297 | """Runs an executable with: |
Dirk Pranke | 50e557b | 2017-12-01 23:48:09 | [diff] [blame] | 298 | - CHROME_HEADLESS set to indicate that the test is running on a |
| 299 | bot and shouldn't do anything interactive like show modal dialogs. |
[email protected] | 3766ed1c | 2012-07-26 20:53:56 | [diff] [blame] | 300 | - environment variable CR_SOURCE_ROOT set to the root directory. |
[email protected] | 0a88a65 | 2012-03-09 00:34:45 | [diff] [blame] | 301 | - environment variable LANGUAGE to en_US.UTF-8. |
earthdok | 748e135 | 2015-01-27 23:04:08 | [diff] [blame] | 302 | - environment variable CHROME_DEVEL_SANDBOX set |
[email protected] | 0a88a65 | 2012-03-09 00:34:45 | [diff] [blame] | 303 | - Reuses sys.executable automatically. |
| 304 | """ |
Dirk Pranke | 50e557b | 2017-12-01 23:48:09 | [diff] [blame] | 305 | extra_env = { |
| 306 | # Set to indicate that the executable is running non-interactively on |
| 307 | # a bot. |
| 308 | 'CHROME_HEADLESS': '1', |
| 309 | |
| 310 | # Many tests assume a English interface... |
| 311 | 'LANG': 'en_US.UTF-8', |
| 312 | } |
| 313 | |
[email protected] | 3766ed1c | 2012-07-26 20:53:56 | [diff] [blame] | 314 | # Used by base/base_paths_linux.cc as an override. Just make sure the default |
| 315 | # logic is used. |
| 316 | env.pop('CR_SOURCE_ROOT', None) |
earthdok | 748e135 | 2015-01-27 23:04:08 | [diff] [blame] | 317 | extra_env.update(get_sandbox_env(env)) |
John Abd-El-Malek | 4569c42 | 2014-10-09 05:10:53 | [diff] [blame] | 318 | |
| 319 | # Copy logic from tools/build/scripts/slave/runtest.py. |
| 320 | asan = '--asan=1' in cmd |
| 321 | lsan = '--lsan=1' in cmd |
earthdok | eeb06530 | 2015-02-04 18:18:04 | [diff] [blame] | 322 | msan = '--msan=1' in cmd |
| 323 | tsan = '--tsan=1' in cmd |
pcc | 46233c2 | 2017-06-20 22:11:41 | [diff] [blame] | 324 | cfi_diag = '--cfi-diag=1' in cmd |
Trent Apted | b185243 | 2018-01-25 02:15:10 | [diff] [blame] | 325 | if stdoutfile or sys.platform in ['win32', 'cygwin']: |
timurrrr | 064f952 | 2015-02-12 15:46:32 | [diff] [blame] | 326 | # Symbolization works in-process on Windows even when sandboxed. |
| 327 | use_symbolization_script = False |
| 328 | else: |
Ilia Samsonov | 4ea01676 | 2019-12-02 22:11:13 | [diff] [blame] | 329 | # If any sanitizer is enabled, we print unsymbolized stack trace |
| 330 | # that is required to run through symbolization script. |
| 331 | use_symbolization_script = (asan or msan or cfi_diag or lsan or tsan) |
John Abd-El-Malek | 4569c42 | 2014-10-09 05:10:53 | [diff] [blame] | 332 | |
pcc | 46233c2 | 2017-06-20 22:11:41 | [diff] [blame] | 333 | if asan or lsan or msan or tsan or cfi_diag: |
| 334 | extra_env.update(get_sanitizer_env(cmd, asan, lsan, msan, tsan, cfi_diag)) |
earthdok | eeb06530 | 2015-02-04 18:18:04 | [diff] [blame] | 335 | |
Timur Iskhodzhanov | fdbfd4e1 | 2015-02-05 13:50:23 | [diff] [blame] | 336 | if lsan or tsan: |
| 337 | # LSan and TSan are not sandbox-friendly. |
| 338 | cmd.append('--no-sandbox') |
John Abd-El-Malek | 14ae054 | 2014-10-15 17:52:31 | [diff] [blame] | 339 | |
| 340 | cmd = trim_cmd(cmd) |
John Abd-El-Malek | 4569c42 | 2014-10-09 05:10:53 | [diff] [blame] | 341 | |
[email protected] | 8ba9835 | 2012-05-23 20:43:59 | [diff] [blame] | 342 | # Ensure paths are correctly separated on windows. |
| 343 | cmd[0] = cmd[0].replace('/', os.path.sep) |
[email protected] | 4bf4d63 | 2012-05-31 15:50:30 | [diff] [blame] | 344 | cmd = fix_python_path(cmd) |
John Abd-El-Malek | 14ae054 | 2014-10-15 17:52:31 | [diff] [blame] | 345 | |
Dirk Pranke | 2cf1a15 | 2019-04-03 01:34:38 | [diff] [blame] | 346 | # We also want to print the GTEST env vars that were set by the caller, |
| 347 | # because you need them to reproduce the task properly. |
| 348 | env_to_print = extra_env.copy() |
| 349 | for env_var_name in ('GTEST_SHARD_INDEX', 'GTEST_TOTAL_SHARDS'): |
Chris McDonald | c3e0f26b | 2020-06-04 02:15:14 | [diff] [blame^] | 350 | if env_var_name in env: |
| 351 | env_to_print[env_var_name] = env[env_var_name] |
Dirk Pranke | 2cf1a15 | 2019-04-03 01:34:38 | [diff] [blame] | 352 | |
Sorin Jianu | e11c985 | 2019-09-12 20:43:55 | [diff] [blame] | 353 | print('Additional test environment:\n%s\n' |
John Abd-El-Malek | 14ae054 | 2014-10-15 17:52:31 | [diff] [blame] | 354 | 'Command: %s\n' % ( |
| 355 | '\n'.join(' %s=%s' % |
Chris McDonald | c3e0f26b | 2020-06-04 02:15:14 | [diff] [blame^] | 356 | (k, v) for k, v in sorted(six.iteritems(env_to_print))), |
Sorin Jianu | e11c985 | 2019-09-12 20:43:55 | [diff] [blame] | 357 | ' '.join(cmd))) |
maruel | f00125f8 | 2016-11-19 00:01:14 | [diff] [blame] | 358 | sys.stdout.flush() |
John Abd-El-Malek | 14ae054 | 2014-10-15 17:52:31 | [diff] [blame] | 359 | env.update(extra_env or {}) |
[email protected] | 50ec9f23 | 2012-03-16 04:18:23 | [diff] [blame] | 360 | try: |
Trent Apted | b185243 | 2018-01-25 02:15:10 | [diff] [blame] | 361 | if stdoutfile: |
| 362 | # Write to stdoutfile and poll to produce terminal output. |
| 363 | return run_command_with_output(cmd, env=env, stdoutfile=stdoutfile) |
| 364 | elif use_symbolization_script: |
| 365 | # See above comment regarding offline symbolization. |
John Abd-El-Malek | 4569c42 | 2014-10-09 05:10:53 | [diff] [blame] | 366 | # Need to pipe to the symbolizer script. |
Caleb Rouleau | 6844df15 | 2019-09-11 01:11:59 | [diff] [blame] | 367 | p1 = _popen(cmd, env=env, stdout=subprocess.PIPE, |
| 368 | stderr=sys.stdout) |
| 369 | p2 = _popen( |
glider | 9d91934 | 2015-02-06 17:42:27 | [diff] [blame] | 370 | get_sanitizer_symbolize_command(executable_path=cmd[0]), |
| 371 | env=env, stdin=p1.stdout) |
John Abd-El-Malek | 4569c42 | 2014-10-09 05:10:53 | [diff] [blame] | 372 | p1.stdout.close() # Allow p1 to receive a SIGPIPE if p2 exits. |
Ben Pastene | e5ece81 | 2018-06-20 22:39:34 | [diff] [blame] | 373 | forward_signals([p1, p2]) |
Caleb Rouleau | fd51129 | 2019-02-22 18:22:00 | [diff] [blame] | 374 | wait_with_signals(p1) |
| 375 | wait_with_signals(p2) |
earthdok | 9bd5d58 | 2015-01-29 21:19:36 | [diff] [blame] | 376 | # Also feed the out-of-band JSON output to the symbolizer script. |
| 377 | symbolize_snippets_in_json(cmd, env) |
vadimsh | 1eaeb298 | 2014-10-20 12:28:45 | [diff] [blame] | 378 | return p1.returncode |
John Abd-El-Malek | 4569c42 | 2014-10-09 05:10:53 | [diff] [blame] | 379 | else: |
Caleb Rouleau | fd51129 | 2019-02-22 18:22:00 | [diff] [blame] | 380 | return run_command(cmd, env=env, log=False) |
[email protected] | 50ec9f23 | 2012-03-16 04:18:23 | [diff] [blame] | 381 | except OSError: |
Chris McDonald | c3e0f26b | 2020-06-04 02:15:14 | [diff] [blame^] | 382 | print('Failed to start %s' % cmd, file=sys.stderr) |
[email protected] | 50ec9f23 | 2012-03-16 04:18:23 | [diff] [blame] | 383 | raise |
[email protected] | 0a88a65 | 2012-03-09 00:34:45 | [diff] [blame] | 384 | |
| 385 | |
Caleb Rouleau | 6844df15 | 2019-09-11 01:11:59 | [diff] [blame] | 386 | def _popen(*args, **kwargs): |
| 387 | assert 'creationflags' not in kwargs |
| 388 | if sys.platform == 'win32': |
| 389 | # Necessary for signal handling. See crbug.com/733612#c6. |
| 390 | kwargs['creationflags'] = subprocess.CREATE_NEW_PROCESS_GROUP |
| 391 | return subprocess.Popen(*args, **kwargs) |
| 392 | |
| 393 | |
[email protected] | 0a88a65 | 2012-03-09 00:34:45 | [diff] [blame] | 394 | def main(): |
glider | b73f187 | 2014-10-09 16:24:56 | [diff] [blame] | 395 | return run_executable(sys.argv[1:], os.environ.copy()) |
[email protected] | 0a88a65 | 2012-03-09 00:34:45 | [diff] [blame] | 396 | |
| 397 | |
[email protected] | ed763a7 | 2012-08-29 03:51:22 | [diff] [blame] | 398 | if __name__ == '__main__': |
[email protected] | 0a88a65 | 2012-03-09 00:34:45 | [diff] [blame] | 399 | sys.exit(main()) |