blob: 1b7035184116e73c3c1d2ea70bbf5dc8675c4f15 [file] [log] [blame]
[email protected]0a88a652012-03-09 00:34:451#!/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
Trent Aptedb1852432018-01-25 02:15:108import io
[email protected]0a88a652012-03-09 00:34:459import os
Ben Pastenee5ece812018-06-20 22:39:3410import signal
[email protected]76361be452012-08-30 22:48:1411import stat
[email protected]0a88a652012-03-09 00:34:4512import subprocess
13import sys
Trent Aptedb1852432018-01-25 02:15:1014import time
[email protected]0a88a652012-03-09 00:34:4515
16# This is hardcoded to be src/ relative to this script.
17ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
18
[email protected]76361be452012-08-30 22:48:1419CHROME_SANDBOX_ENV = 'CHROME_DEVEL_SANDBOX'
20CHROME_SANDBOX_PATH = '/opt/chromium/chrome_sandbox'
21
22
earthdok748e1352015-01-27 23:04:0823def get_sandbox_env(env):
24 """Returns the environment flags needed for the SUID sandbox to work."""
John Abd-El-Malek14ae0542014-10-15 17:52:3125 extra_env = {}
[email protected]76361be452012-08-30 22:48:1426 chrome_sandbox_path = env.get(CHROME_SANDBOX_ENV, CHROME_SANDBOX_PATH)
earthdok748e1352015-01-27 23:04:0827 # The above would silently disable the SUID sandbox if the env value were
28 # an empty string. We don't want to allow that. http://crbug.com/245376
29 # TODO(jln): Remove this check once it's no longer possible to disable the
30 # sandbox that way.
31 if not chrome_sandbox_path:
32 chrome_sandbox_path = CHROME_SANDBOX_PATH
33 extra_env[CHROME_SANDBOX_ENV] = chrome_sandbox_path
John Abd-El-Malek14ae0542014-10-15 17:52:3134
35 return extra_env
36
37
38def trim_cmd(cmd):
39 """Removes internal flags from cmd since they're just used to communicate from
40 the host machine to this script running on the swarm slaves."""
earthdokeeb065302015-02-04 18:18:0441 sanitizers = ['asan', 'lsan', 'msan', 'tsan']
42 internal_flags = frozenset('--%s=%d' % (name, value)
43 for name in sanitizers
44 for value in [0, 1])
John Abd-El-Malek14ae0542014-10-15 17:52:3145 return [i for i in cmd if i not in internal_flags]
[email protected]76361be452012-08-30 22:48:1446
[email protected]0a88a652012-03-09 00:34:4547
[email protected]4bf4d632012-05-31 15:50:3048def fix_python_path(cmd):
49 """Returns the fixed command line to call the right python executable."""
50 out = cmd[:]
51 if out[0] == 'python':
52 out[0] = sys.executable
53 elif out[0].endswith('.py'):
54 out.insert(0, sys.executable)
55 return out
56
57
pcc46233c22017-06-20 22:11:4158def get_sanitizer_env(cmd, asan, lsan, msan, tsan, cfi_diag):
earthdokeeb065302015-02-04 18:18:0459 """Returns the envirnoment flags needed for sanitizer tools."""
John Abd-El-Malek14ae0542014-10-15 17:52:3160
61 extra_env = {}
62
earthdokeeb065302015-02-04 18:18:0463 # Instruct GTK to use malloc while running sanitizer-instrumented tests.
earthdoke6c54a42014-10-16 18:02:3664 extra_env['G_SLICE'] = 'always-malloc'
John Abd-El-Malek14ae0542014-10-15 17:52:3165
66 extra_env['NSS_DISABLE_ARENA_FREE_LIST'] = '1'
67 extra_env['NSS_DISABLE_UNLOAD'] = '1'
68
69 # TODO(glider): remove the symbolizer path once
70 # https://code.google.com/p/address-sanitizer/issues/detail?id=134 is fixed.
Nico Weber19097312016-01-29 18:13:1571 symbolizer_path = os.path.join(ROOT_DIR,
72 'third_party', 'llvm-build', 'Release+Asserts', 'bin', 'llvm-symbolizer')
John Abd-El-Malek14ae0542014-10-15 17:52:3173
earthdokeeb065302015-02-04 18:18:0474 if lsan or tsan:
John Abd-El-Malek14ae0542014-10-15 17:52:3175 # LSan is not sandbox-compatible, so we can use online symbolization. In
76 # fact, it needs symbolization to be able to apply suppressions.
77 symbolization_options = ['symbolize=1',
78 'external_symbolizer_path=%s' % symbolizer_path]
pcc46233c22017-06-20 22:11:4179 elif (asan or msan or cfi_diag) and sys.platform not in ['win32', 'cygwin']:
timurrrr064f9522015-02-12 15:46:3280 # ASan uses a script for offline symbolization, except on Windows.
John Abd-El-Malek14ae0542014-10-15 17:52:3181 # Important note: when running ASan with leak detection enabled, we must use
82 # the LSan symbolization options above.
83 symbolization_options = ['symbolize=0']
vadimsh5cf3c442014-10-20 10:22:3884 # Set the path to llvm-symbolizer to be used by asan_symbolize.py
85 extra_env['LLVM_SYMBOLIZER_PATH'] = symbolizer_path
timurrrr7e67df92015-02-12 21:50:5286 else:
87 symbolization_options = []
John Abd-El-Malek14ae0542014-10-15 17:52:3188
earthdokeeb065302015-02-04 18:18:0489 if asan:
90 asan_options = symbolization_options[:]
91 if lsan:
92 asan_options.append('detect_leaks=1')
John Abd-El-Malek14ae0542014-10-15 17:52:3193
timurrrr7e67df92015-02-12 21:50:5294 if asan_options:
95 extra_env['ASAN_OPTIONS'] = ' '.join(asan_options)
John Abd-El-Malek14ae0542014-10-15 17:52:3196
earthdokeeb065302015-02-04 18:18:0497 if sys.platform == 'darwin':
98 isolate_output_dir = os.path.abspath(os.path.dirname(cmd[0]))
99 # This is needed because the test binary has @executable_path embedded in
100 # it that the OS tries to resolve to the cache directory and not the
101 # mapped directory.
102 extra_env['DYLD_LIBRARY_PATH'] = str(isolate_output_dir)
103
104 if lsan:
105 if asan or msan:
106 lsan_options = []
107 else:
108 lsan_options = symbolization_options[:]
109 if sys.platform == 'linux2':
110 # Use the debug version of libstdc++ under LSan. If we don't, there will
111 # be a lot of incomplete stack traces in the reports.
112 extra_env['LD_LIBRARY_PATH'] = '/usr/lib/x86_64-linux-gnu/debug:'
113
earthdokeeb065302015-02-04 18:18:04114 extra_env['LSAN_OPTIONS'] = ' '.join(lsan_options)
115
116 if msan:
117 msan_options = symbolization_options[:]
118 if lsan:
119 msan_options.append('detect_leaks=1')
120 extra_env['MSAN_OPTIONS'] = ' '.join(msan_options)
121
122 if tsan:
123 tsan_options = symbolization_options[:]
124 extra_env['TSAN_OPTIONS'] = ' '.join(tsan_options)
John Abd-El-Malek14ae0542014-10-15 17:52:31125
pcc46233c22017-06-20 22:11:41126 # CFI uses the UBSan runtime to provide diagnostics.
127 if cfi_diag:
128 ubsan_options = symbolization_options[:] + ['print_stacktrace=1']
129 extra_env['UBSAN_OPTIONS'] = ' '.join(ubsan_options)
130
John Abd-El-Malek14ae0542014-10-15 17:52:31131 return extra_env
132
133
glider9d919342015-02-06 17:42:27134def get_sanitizer_symbolize_command(json_path=None, executable_path=None):
earthdok9bd5d582015-01-29 21:19:36135 """Construct the command to invoke offline symbolization script."""
Nico Weber19097312016-01-29 18:13:15136 script_path = os.path.join(
137 ROOT_DIR, 'tools', 'valgrind', 'asan', 'asan_symbolize.py')
earthdok9bd5d582015-01-29 21:19:36138 cmd = [sys.executable, script_path]
139 if json_path is not None:
140 cmd.append('--test-summary-json-file=%s' % json_path)
glider9d919342015-02-06 17:42:27141 if executable_path is not None:
142 cmd.append('--executable-path=%s' % executable_path)
earthdok9bd5d582015-01-29 21:19:36143 return cmd
144
145
146def get_json_path(cmd):
147 """Extract the JSON test summary path from a command line."""
148 json_path_flag = '--test-launcher-summary-output='
149 for arg in cmd:
150 if arg.startswith(json_path_flag):
151 return arg.split(json_path_flag).pop()
152 return None
153
154
155def symbolize_snippets_in_json(cmd, env):
156 """Symbolize output snippets inside the JSON test summary."""
157 json_path = get_json_path(cmd)
158 if json_path is None:
159 return
160
161 try:
glider9d919342015-02-06 17:42:27162 symbolize_command = get_sanitizer_symbolize_command(
163 json_path=json_path, executable_path=cmd[0])
earthdok9bd5d582015-01-29 21:19:36164 p = subprocess.Popen(symbolize_command, stderr=subprocess.PIPE, env=env)
165 (_, stderr) = p.communicate()
166 except OSError as e:
thakis45643b42016-01-29 21:53:42167 print >> sys.stderr, 'Exception while symbolizing snippets: %s' % e
168 raise
earthdok9bd5d582015-01-29 21:19:36169
170 if p.returncode != 0:
thakis45643b42016-01-29 21:53:42171 print >> sys.stderr, "Error: failed to symbolize snippets in JSON:\n"
172 print >> sys.stderr, stderr
173 raise subprocess.CalledProcessError(p.returncode, symbolize_command)
earthdok9bd5d582015-01-29 21:19:36174
175
Trent Aptedb1852432018-01-25 02:15:10176def run_command_with_output(argv, stdoutfile, env=None, cwd=None):
177 """ Run command and stream its stdout/stderr to the console & |stdoutfile|.
178 """
179 print('Running %r in %r (env: %r)' % (argv, cwd, env))
180 assert stdoutfile
181 with io.open(stdoutfile, 'w') as writer, io.open(stdoutfile, 'r', 1) as \
182 reader:
183 process = subprocess.Popen(argv, env=env, cwd=cwd, stdout=writer,
184 stderr=subprocess.STDOUT)
Ben Pastenee5ece812018-06-20 22:39:34185 forward_signals([process])
Trent Aptedb1852432018-01-25 02:15:10186 while process.poll() is None:
187 sys.stdout.write(reader.read())
188 time.sleep(0.1)
189 # Read the remaining.
190 sys.stdout.write(reader.read())
191 print('Command %r returned exit code %d' % (argv, process.returncode))
192 return process.returncode
193
194
Ben Pastenee5ece812018-06-20 22:39:34195def forward_signals(procs):
196 """Forwards unix's SIGTERM or win's CTRL_BREAK_EVENT to the given processes.
197
198 This plays nicely with swarming's timeout handling. See also
199 https://chromium.googlesource.com/infra/luci/luci-py/+/master/appengine/swarming/doc/Bot.md#graceful-termination_aka-the-sigterm-and-sigkill-dance
200
201 Args:
202 procs: A list of subprocess.Popen objects representing child processes.
203 """
204 assert all(isinstance(p, subprocess.Popen) for p in procs)
205 def _sig_handler(sig, _):
206 for p in procs:
207 if sig == signal.SIGBREAK:
208 p.send_signal(signal.CTRL_BREAK_EVENT)
209 else:
210 p.send_signal(sig)
211 if sys.platform == 'win32':
212 signal.signal(signal.SIGBREAK, _sig_handler)
213 else:
214 signal.signal(signal.SIGTERM, _sig_handler)
215
216
Trent Aptedb1852432018-01-25 02:15:10217def run_executable(cmd, env, stdoutfile=None):
[email protected]0a88a652012-03-09 00:34:45218 """Runs an executable with:
Dirk Pranke50e557b2017-12-01 23:48:09219 - CHROME_HEADLESS set to indicate that the test is running on a
220 bot and shouldn't do anything interactive like show modal dialogs.
[email protected]3766ed1c2012-07-26 20:53:56221 - environment variable CR_SOURCE_ROOT set to the root directory.
[email protected]0a88a652012-03-09 00:34:45222 - environment variable LANGUAGE to en_US.UTF-8.
earthdok748e1352015-01-27 23:04:08223 - environment variable CHROME_DEVEL_SANDBOX set
[email protected]0a88a652012-03-09 00:34:45224 - Reuses sys.executable automatically.
225 """
Dirk Pranke50e557b2017-12-01 23:48:09226 extra_env = {
227 # Set to indicate that the executable is running non-interactively on
228 # a bot.
229 'CHROME_HEADLESS': '1',
230
231 # Many tests assume a English interface...
232 'LANG': 'en_US.UTF-8',
233 }
234
[email protected]3766ed1c2012-07-26 20:53:56235 # Used by base/base_paths_linux.cc as an override. Just make sure the default
236 # logic is used.
237 env.pop('CR_SOURCE_ROOT', None)
earthdok748e1352015-01-27 23:04:08238 extra_env.update(get_sandbox_env(env))
John Abd-El-Malek4569c422014-10-09 05:10:53239
240 # Copy logic from tools/build/scripts/slave/runtest.py.
241 asan = '--asan=1' in cmd
242 lsan = '--lsan=1' in cmd
earthdokeeb065302015-02-04 18:18:04243 msan = '--msan=1' in cmd
244 tsan = '--tsan=1' in cmd
pcc46233c22017-06-20 22:11:41245 cfi_diag = '--cfi-diag=1' in cmd
Trent Aptedb1852432018-01-25 02:15:10246 if stdoutfile or sys.platform in ['win32', 'cygwin']:
timurrrr064f9522015-02-12 15:46:32247 # Symbolization works in-process on Windows even when sandboxed.
248 use_symbolization_script = False
249 else:
250 # LSan doesn't support sandboxing yet, so we use the in-process symbolizer.
251 # Note that ASan and MSan can work together with LSan.
pcc46233c22017-06-20 22:11:41252 use_symbolization_script = (asan or msan or cfi_diag) and not lsan
John Abd-El-Malek4569c422014-10-09 05:10:53253
pcc46233c22017-06-20 22:11:41254 if asan or lsan or msan or tsan or cfi_diag:
255 extra_env.update(get_sanitizer_env(cmd, asan, lsan, msan, tsan, cfi_diag))
earthdokeeb065302015-02-04 18:18:04256
Timur Iskhodzhanovfdbfd4e12015-02-05 13:50:23257 if lsan or tsan:
258 # LSan and TSan are not sandbox-friendly.
259 cmd.append('--no-sandbox')
John Abd-El-Malek14ae0542014-10-15 17:52:31260
261 cmd = trim_cmd(cmd)
John Abd-El-Malek4569c422014-10-09 05:10:53262
[email protected]8ba98352012-05-23 20:43:59263 # Ensure paths are correctly separated on windows.
264 cmd[0] = cmd[0].replace('/', os.path.sep)
[email protected]4bf4d632012-05-31 15:50:30265 cmd = fix_python_path(cmd)
John Abd-El-Malek14ae0542014-10-15 17:52:31266
267 print('Additional test environment:\n%s\n'
268 'Command: %s\n' % (
269 '\n'.join(' %s=%s' %
270 (k, v) for k, v in sorted(extra_env.iteritems())),
271 ' '.join(cmd)))
maruelf00125f82016-11-19 00:01:14272 sys.stdout.flush()
John Abd-El-Malek14ae0542014-10-15 17:52:31273 env.update(extra_env or {})
[email protected]50ec9f232012-03-16 04:18:23274 try:
Trent Aptedb1852432018-01-25 02:15:10275 if stdoutfile:
276 # Write to stdoutfile and poll to produce terminal output.
277 return run_command_with_output(cmd, env=env, stdoutfile=stdoutfile)
278 elif use_symbolization_script:
279 # See above comment regarding offline symbolization.
John Abd-El-Malek4569c422014-10-09 05:10:53280 # Need to pipe to the symbolizer script.
281 p1 = subprocess.Popen(cmd, env=env, stdout=subprocess.PIPE,
282 stderr=sys.stdout)
glider9d919342015-02-06 17:42:27283 p2 = subprocess.Popen(
284 get_sanitizer_symbolize_command(executable_path=cmd[0]),
285 env=env, stdin=p1.stdout)
John Abd-El-Malek4569c422014-10-09 05:10:53286 p1.stdout.close() # Allow p1 to receive a SIGPIPE if p2 exits.
Ben Pastenee5ece812018-06-20 22:39:34287 forward_signals([p1, p2])
vadimsh1eaeb2982014-10-20 12:28:45288 p1.wait()
John Abd-El-Malek4569c422014-10-09 05:10:53289 p2.wait()
earthdok9bd5d582015-01-29 21:19:36290 # Also feed the out-of-band JSON output to the symbolizer script.
291 symbolize_snippets_in_json(cmd, env)
vadimsh1eaeb2982014-10-20 12:28:45292 return p1.returncode
John Abd-El-Malek4569c422014-10-09 05:10:53293 else:
Ben Pastenee5ece812018-06-20 22:39:34294 p = subprocess.Popen(cmd, env=env)
295 forward_signals([p])
296 return p.wait()
[email protected]50ec9f232012-03-16 04:18:23297 except OSError:
298 print >> sys.stderr, 'Failed to start %s' % cmd
299 raise
[email protected]0a88a652012-03-09 00:34:45300
301
302def main():
gliderb73f1872014-10-09 16:24:56303 return run_executable(sys.argv[1:], os.environ.copy())
[email protected]0a88a652012-03-09 00:34:45304
305
[email protected]ed763a72012-08-29 03:51:22306if __name__ == '__main__':
[email protected]0a88a652012-03-09 00:34:45307 sys.exit(main())