blob: 597ca2ab2343e92762f8665f8040e9bc802d63ae [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
8import os
[email protected]76361be452012-08-30 22:48:149import stat
[email protected]0a88a652012-03-09 00:34:4510import subprocess
11import sys
12
13# This is hardcoded to be src/ relative to this script.
14ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
15
[email protected]76361be452012-08-30 22:48:1416CHROME_SANDBOX_ENV = 'CHROME_DEVEL_SANDBOX'
17CHROME_SANDBOX_PATH = '/opt/chromium/chrome_sandbox'
18
19
earthdok748e1352015-01-27 23:04:0820def get_sandbox_env(env):
21 """Returns the environment flags needed for the SUID sandbox to work."""
John Abd-El-Malek14ae0542014-10-15 17:52:3122 extra_env = {}
[email protected]76361be452012-08-30 22:48:1423 chrome_sandbox_path = env.get(CHROME_SANDBOX_ENV, CHROME_SANDBOX_PATH)
earthdok748e1352015-01-27 23:04:0824 # The above would silently disable the SUID sandbox if the env value were
25 # an empty string. We don't want to allow that. http://crbug.com/245376
26 # TODO(jln): Remove this check once it's no longer possible to disable the
27 # sandbox that way.
28 if not chrome_sandbox_path:
29 chrome_sandbox_path = CHROME_SANDBOX_PATH
30 extra_env[CHROME_SANDBOX_ENV] = chrome_sandbox_path
John Abd-El-Malek14ae0542014-10-15 17:52:3131
32 return extra_env
33
34
35def trim_cmd(cmd):
36 """Removes internal flags from cmd since they're just used to communicate from
37 the host machine to this script running on the swarm slaves."""
earthdokeeb065302015-02-04 18:18:0438 sanitizers = ['asan', 'lsan', 'msan', 'tsan']
39 internal_flags = frozenset('--%s=%d' % (name, value)
40 for name in sanitizers
41 for value in [0, 1])
John Abd-El-Malek14ae0542014-10-15 17:52:3142 return [i for i in cmd if i not in internal_flags]
[email protected]76361be452012-08-30 22:48:1443
[email protected]0a88a652012-03-09 00:34:4544
[email protected]4bf4d632012-05-31 15:50:3045def fix_python_path(cmd):
46 """Returns the fixed command line to call the right python executable."""
47 out = cmd[:]
48 if out[0] == 'python':
49 out[0] = sys.executable
50 elif out[0].endswith('.py'):
51 out.insert(0, sys.executable)
52 return out
53
54
earthdokeeb065302015-02-04 18:18:0455def get_sanitizer_env(cmd, asan, lsan, msan, tsan):
56 """Returns the envirnoment flags needed for sanitizer tools."""
John Abd-El-Malek14ae0542014-10-15 17:52:3157
58 extra_env = {}
59
earthdokeeb065302015-02-04 18:18:0460 # Instruct GTK to use malloc while running sanitizer-instrumented tests.
earthdoke6c54a42014-10-16 18:02:3661 extra_env['G_SLICE'] = 'always-malloc'
John Abd-El-Malek14ae0542014-10-15 17:52:3162
63 extra_env['NSS_DISABLE_ARENA_FREE_LIST'] = '1'
64 extra_env['NSS_DISABLE_UNLOAD'] = '1'
65
66 # TODO(glider): remove the symbolizer path once
67 # https://code.google.com/p/address-sanitizer/issues/detail?id=134 is fixed.
68 symbolizer_path = os.path.abspath(os.path.join(ROOT_DIR, 'third_party',
69 'llvm-build', 'Release+Asserts', 'bin', 'llvm-symbolizer'))
70
earthdokeeb065302015-02-04 18:18:0471 if lsan or tsan:
John Abd-El-Malek14ae0542014-10-15 17:52:3172 # LSan is not sandbox-compatible, so we can use online symbolization. In
73 # fact, it needs symbolization to be able to apply suppressions.
74 symbolization_options = ['symbolize=1',
75 'external_symbolizer_path=%s' % symbolizer_path]
earthdokeeb065302015-02-04 18:18:0476 elif asan or msan:
John Abd-El-Malek14ae0542014-10-15 17:52:3177 # ASan uses a script for offline symbolization.
78 # Important note: when running ASan with leak detection enabled, we must use
79 # the LSan symbolization options above.
80 symbolization_options = ['symbolize=0']
vadimsh5cf3c442014-10-20 10:22:3881 # Set the path to llvm-symbolizer to be used by asan_symbolize.py
82 extra_env['LLVM_SYMBOLIZER_PATH'] = symbolizer_path
John Abd-El-Malek14ae0542014-10-15 17:52:3183
earthdokeeb065302015-02-04 18:18:0484 if asan:
85 asan_options = symbolization_options[:]
86 if lsan:
87 asan_options.append('detect_leaks=1')
John Abd-El-Malek14ae0542014-10-15 17:52:3188
earthdokeeb065302015-02-04 18:18:0489 extra_env['ASAN_OPTIONS'] = ' '.join(asan_options)
John Abd-El-Malek14ae0542014-10-15 17:52:3190
earthdokeeb065302015-02-04 18:18:0491 if sys.platform == 'darwin':
92 isolate_output_dir = os.path.abspath(os.path.dirname(cmd[0]))
93 # This is needed because the test binary has @executable_path embedded in
94 # it that the OS tries to resolve to the cache directory and not the
95 # mapped directory.
96 extra_env['DYLD_LIBRARY_PATH'] = str(isolate_output_dir)
97
98 if lsan:
99 if asan or msan:
100 lsan_options = []
101 else:
102 lsan_options = symbolization_options[:]
103 if sys.platform == 'linux2':
104 # Use the debug version of libstdc++ under LSan. If we don't, there will
105 # be a lot of incomplete stack traces in the reports.
106 extra_env['LD_LIBRARY_PATH'] = '/usr/lib/x86_64-linux-gnu/debug:'
107
108 suppressions_file = os.path.join(ROOT_DIR, 'tools', 'lsan',
109 'suppressions.txt')
110 lsan_options += ['suppressions=%s' % suppressions_file,
111 'print_suppressions=1']
112 extra_env['LSAN_OPTIONS'] = ' '.join(lsan_options)
113
114 if msan:
115 msan_options = symbolization_options[:]
116 if lsan:
117 msan_options.append('detect_leaks=1')
118 extra_env['MSAN_OPTIONS'] = ' '.join(msan_options)
119
120 if tsan:
121 tsan_options = symbolization_options[:]
122 extra_env['TSAN_OPTIONS'] = ' '.join(tsan_options)
John Abd-El-Malek14ae0542014-10-15 17:52:31123
124 return extra_env
125
126
earthdok9bd5d582015-01-29 21:19:36127def get_sanitizer_symbolize_command(json_path=None):
128 """Construct the command to invoke offline symbolization script."""
129 script_path = '../tools/valgrind/asan/asan_symbolize.py'
130 cmd = [sys.executable, script_path]
131 if json_path is not None:
132 cmd.append('--test-summary-json-file=%s' % json_path)
133 return cmd
134
135
136def get_json_path(cmd):
137 """Extract the JSON test summary path from a command line."""
138 json_path_flag = '--test-launcher-summary-output='
139 for arg in cmd:
140 if arg.startswith(json_path_flag):
141 return arg.split(json_path_flag).pop()
142 return None
143
144
145def symbolize_snippets_in_json(cmd, env):
146 """Symbolize output snippets inside the JSON test summary."""
147 json_path = get_json_path(cmd)
148 if json_path is None:
149 return
150
151 try:
152 symbolize_command = get_sanitizer_symbolize_command(json_path=json_path)
153 p = subprocess.Popen(symbolize_command, stderr=subprocess.PIPE, env=env)
154 (_, stderr) = p.communicate()
155 except OSError as e:
156 print 'Exception while symbolizing snippets: %s' % e
157
158 if p.returncode != 0:
159 print "Error: failed to symbolize snippets in JSON:\n"
160 print stderr
161
162
[email protected]0a88a652012-03-09 00:34:45163def run_executable(cmd, env):
164 """Runs an executable with:
[email protected]3766ed1c2012-07-26 20:53:56165 - environment variable CR_SOURCE_ROOT set to the root directory.
[email protected]0a88a652012-03-09 00:34:45166 - environment variable LANGUAGE to en_US.UTF-8.
earthdok748e1352015-01-27 23:04:08167 - environment variable CHROME_DEVEL_SANDBOX set
[email protected]0a88a652012-03-09 00:34:45168 - Reuses sys.executable automatically.
169 """
John Abd-El-Malek14ae0542014-10-15 17:52:31170 extra_env = {}
[email protected]0a88a652012-03-09 00:34:45171 # Many tests assume a English interface...
John Abd-El-Malek14ae0542014-10-15 17:52:31172 extra_env['LANG'] = 'en_US.UTF-8'
[email protected]3766ed1c2012-07-26 20:53:56173 # Used by base/base_paths_linux.cc as an override. Just make sure the default
174 # logic is used.
175 env.pop('CR_SOURCE_ROOT', None)
earthdok748e1352015-01-27 23:04:08176 extra_env.update(get_sandbox_env(env))
John Abd-El-Malek4569c422014-10-09 05:10:53177
178 # Copy logic from tools/build/scripts/slave/runtest.py.
179 asan = '--asan=1' in cmd
180 lsan = '--lsan=1' in cmd
earthdokeeb065302015-02-04 18:18:04181 msan = '--msan=1' in cmd
182 tsan = '--tsan=1' in cmd
183 use_symbolization_script = (asan or msan) and not lsan
John Abd-El-Malek4569c422014-10-09 05:10:53184
earthdokeeb065302015-02-04 18:18:04185 if asan or lsan or msan or tsan:
186 extra_env.update(get_sanitizer_env(cmd, asan, lsan, msan, tsan))
187
Timur Iskhodzhanovfdbfd4e12015-02-05 13:50:23188 if lsan or tsan:
189 # LSan and TSan are not sandbox-friendly.
190 cmd.append('--no-sandbox')
John Abd-El-Malek14ae0542014-10-15 17:52:31191
192 cmd = trim_cmd(cmd)
John Abd-El-Malek4569c422014-10-09 05:10:53193
[email protected]8ba98352012-05-23 20:43:59194 # Ensure paths are correctly separated on windows.
195 cmd[0] = cmd[0].replace('/', os.path.sep)
[email protected]4bf4d632012-05-31 15:50:30196 cmd = fix_python_path(cmd)
John Abd-El-Malek14ae0542014-10-15 17:52:31197
198 print('Additional test environment:\n%s\n'
199 'Command: %s\n' % (
200 '\n'.join(' %s=%s' %
201 (k, v) for k, v in sorted(extra_env.iteritems())),
202 ' '.join(cmd)))
203 env.update(extra_env or {})
[email protected]50ec9f232012-03-16 04:18:23204 try:
John Abd-El-Malek14ae0542014-10-15 17:52:31205 # See above comment regarding offline symbolization.
earthdok9bd5d582015-01-29 21:19:36206 if use_symbolization_script:
John Abd-El-Malek4569c422014-10-09 05:10:53207 # Need to pipe to the symbolizer script.
208 p1 = subprocess.Popen(cmd, env=env, stdout=subprocess.PIPE,
209 stderr=sys.stdout)
earthdok9bd5d582015-01-29 21:19:36210 p2 = subprocess.Popen(get_sanitizer_symbolize_command(),
vadimsh1eaeb2982014-10-20 12:28:45211 env=env, stdin=p1.stdout)
John Abd-El-Malek4569c422014-10-09 05:10:53212 p1.stdout.close() # Allow p1 to receive a SIGPIPE if p2 exits.
vadimsh1eaeb2982014-10-20 12:28:45213 p1.wait()
John Abd-El-Malek4569c422014-10-09 05:10:53214 p2.wait()
earthdok9bd5d582015-01-29 21:19:36215 # Also feed the out-of-band JSON output to the symbolizer script.
216 symbolize_snippets_in_json(cmd, env)
vadimsh1eaeb2982014-10-20 12:28:45217 return p1.returncode
John Abd-El-Malek4569c422014-10-09 05:10:53218 else:
219 return subprocess.call(cmd, env=env)
[email protected]50ec9f232012-03-16 04:18:23220 except OSError:
221 print >> sys.stderr, 'Failed to start %s' % cmd
222 raise
[email protected]0a88a652012-03-09 00:34:45223
224
225def main():
gliderb73f1872014-10-09 16:24:56226 return run_executable(sys.argv[1:], os.environ.copy())
[email protected]0a88a652012-03-09 00:34:45227
228
[email protected]ed763a72012-08-29 03:51:22229if __name__ == '__main__':
[email protected]0a88a652012-03-09 00:34:45230 sys.exit(main())