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