Fixed a bug that caused extension options not to fit their overlays properly when...
[chromium-blink-merge.git] / testing / test_env.py
blobd19f74feee99b5f27c6d7e7ec9ca1a2133f1c554
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.
6 """Sets environment variables needed to run a chromium unit test."""
8 import os
9 import stat
10 import subprocess
11 import sys
13 # This is hardcoded to be src/ relative to this script.
14 ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
16 CHROME_SANDBOX_ENV = 'CHROME_DEVEL_SANDBOX'
17 CHROME_SANDBOX_PATH = '/opt/chromium/chrome_sandbox'
20 def get_sandbox_env(env):
21 """Returns the environment flags needed for the SUID sandbox to work."""
22 extra_env = {}
23 chrome_sandbox_path = env.get(CHROME_SANDBOX_ENV, CHROME_SANDBOX_PATH)
24 # 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
32 return extra_env
35 def 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."""
38 sanitizers = ['asan', 'lsan', 'msan', 'tsan']
39 internal_flags = frozenset('--%s=%d' % (name, value)
40 for name in sanitizers
41 for value in [0, 1])
42 return [i for i in cmd if i not in internal_flags]
45 def 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
55 def get_sanitizer_env(cmd, asan, lsan, msan, tsan):
56 """Returns the envirnoment flags needed for sanitizer tools."""
58 extra_env = {}
60 # Instruct GTK to use malloc while running sanitizer-instrumented tests.
61 extra_env['G_SLICE'] = 'always-malloc'
63 extra_env['NSS_DISABLE_ARENA_FREE_LIST'] = '1'
64 extra_env['NSS_DISABLE_UNLOAD'] = '1'
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'))
71 if lsan or tsan:
72 # 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]
76 elif (asan or msan) and sys.platform not in ['win32', 'cygwin']:
77 # ASan uses a script for offline symbolization, except on Windows.
78 # Important note: when running ASan with leak detection enabled, we must use
79 # the LSan symbolization options above.
80 symbolization_options = ['symbolize=0']
81 # Set the path to llvm-symbolizer to be used by asan_symbolize.py
82 extra_env['LLVM_SYMBOLIZER_PATH'] = symbolizer_path
83 else:
84 symbolization_options = []
86 if asan:
87 asan_options = symbolization_options[:]
88 if lsan:
89 asan_options.append('detect_leaks=1')
91 if asan_options:
92 extra_env['ASAN_OPTIONS'] = ' '.join(asan_options)
94 if sys.platform == 'darwin':
95 isolate_output_dir = os.path.abspath(os.path.dirname(cmd[0]))
96 # This is needed because the test binary has @executable_path embedded in
97 # it that the OS tries to resolve to the cache directory and not the
98 # mapped directory.
99 extra_env['DYLD_LIBRARY_PATH'] = str(isolate_output_dir)
101 if lsan:
102 if asan or msan:
103 lsan_options = []
104 else:
105 lsan_options = symbolization_options[:]
106 if sys.platform == 'linux2':
107 # Use the debug version of libstdc++ under LSan. If we don't, there will
108 # be a lot of incomplete stack traces in the reports.
109 extra_env['LD_LIBRARY_PATH'] = '/usr/lib/x86_64-linux-gnu/debug:'
111 suppressions_file = os.path.join(ROOT_DIR, 'tools', 'lsan',
112 'suppressions.txt')
113 lsan_options += ['suppressions=%s' % suppressions_file,
114 'print_suppressions=1']
115 extra_env['LSAN_OPTIONS'] = ' '.join(lsan_options)
117 if msan:
118 msan_options = symbolization_options[:]
119 if lsan:
120 msan_options.append('detect_leaks=1')
121 extra_env['MSAN_OPTIONS'] = ' '.join(msan_options)
123 if tsan:
124 tsan_options = symbolization_options[:]
125 extra_env['TSAN_OPTIONS'] = ' '.join(tsan_options)
127 return extra_env
130 def get_sanitizer_symbolize_command(json_path=None, executable_path=None):
131 """Construct the command to invoke offline symbolization script."""
132 script_path = '../tools/valgrind/asan/asan_symbolize.py'
133 cmd = [sys.executable, script_path]
134 if json_path is not None:
135 cmd.append('--test-summary-json-file=%s' % json_path)
136 if executable_path is not None:
137 cmd.append('--executable-path=%s' % executable_path)
138 return cmd
141 def get_json_path(cmd):
142 """Extract the JSON test summary path from a command line."""
143 json_path_flag = '--test-launcher-summary-output='
144 for arg in cmd:
145 if arg.startswith(json_path_flag):
146 return arg.split(json_path_flag).pop()
147 return None
150 def symbolize_snippets_in_json(cmd, env):
151 """Symbolize output snippets inside the JSON test summary."""
152 json_path = get_json_path(cmd)
153 if json_path is None:
154 return
156 try:
157 symbolize_command = get_sanitizer_symbolize_command(
158 json_path=json_path, executable_path=cmd[0])
159 p = subprocess.Popen(symbolize_command, stderr=subprocess.PIPE, env=env)
160 (_, stderr) = p.communicate()
161 except OSError as e:
162 print 'Exception while symbolizing snippets: %s' % e
164 if p.returncode != 0:
165 print "Error: failed to symbolize snippets in JSON:\n"
166 print stderr
169 def run_executable(cmd, env):
170 """Runs an executable with:
171 - environment variable CR_SOURCE_ROOT set to the root directory.
172 - environment variable LANGUAGE to en_US.UTF-8.
173 - environment variable CHROME_DEVEL_SANDBOX set
174 - Reuses sys.executable automatically.
176 extra_env = {}
177 # Many tests assume a English interface...
178 extra_env['LANG'] = 'en_US.UTF-8'
179 # Used by base/base_paths_linux.cc as an override. Just make sure the default
180 # logic is used.
181 env.pop('CR_SOURCE_ROOT', None)
182 extra_env.update(get_sandbox_env(env))
184 # Copy logic from tools/build/scripts/slave/runtest.py.
185 asan = '--asan=1' in cmd
186 lsan = '--lsan=1' in cmd
187 msan = '--msan=1' in cmd
188 tsan = '--tsan=1' in cmd
189 if sys.platform in ['win32', 'cygwin']:
190 # Symbolization works in-process on Windows even when sandboxed.
191 use_symbolization_script = False
192 else:
193 # LSan doesn't support sandboxing yet, so we use the in-process symbolizer.
194 # Note that ASan and MSan can work together with LSan.
195 use_symbolization_script = (asan or msan) and not lsan
197 if asan or lsan or msan or tsan:
198 extra_env.update(get_sanitizer_env(cmd, asan, lsan, msan, tsan))
200 if lsan or tsan:
201 # LSan and TSan are not sandbox-friendly.
202 cmd.append('--no-sandbox')
204 cmd = trim_cmd(cmd)
206 # Ensure paths are correctly separated on windows.
207 cmd[0] = cmd[0].replace('/', os.path.sep)
208 cmd = fix_python_path(cmd)
210 print('Additional test environment:\n%s\n'
211 'Command: %s\n' % (
212 '\n'.join(' %s=%s' %
213 (k, v) for k, v in sorted(extra_env.iteritems())),
214 ' '.join(cmd)))
215 env.update(extra_env or {})
216 try:
217 # See above comment regarding offline symbolization.
218 if use_symbolization_script:
219 # Need to pipe to the symbolizer script.
220 p1 = subprocess.Popen(cmd, env=env, stdout=subprocess.PIPE,
221 stderr=sys.stdout)
222 p2 = subprocess.Popen(
223 get_sanitizer_symbolize_command(executable_path=cmd[0]),
224 env=env, stdin=p1.stdout)
225 p1.stdout.close() # Allow p1 to receive a SIGPIPE if p2 exits.
226 p1.wait()
227 p2.wait()
228 # Also feed the out-of-band JSON output to the symbolizer script.
229 symbolize_snippets_in_json(cmd, env)
230 return p1.returncode
231 else:
232 return subprocess.call(cmd, env=env)
233 except OSError:
234 print >> sys.stderr, 'Failed to start %s' % cmd
235 raise
238 def main():
239 return run_executable(sys.argv[1:], os.environ.copy())
242 if __name__ == '__main__':
243 sys.exit(main())