blob: 0743f340038f4639884a0b131e46c088ce2dd9d6 [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."""
38 internal_flags = frozenset(['--asan=0', '--asan=1', '--lsan=0', '--lsan=1'])
39 return [i for i in cmd if i not in internal_flags]
[email protected]76361be452012-08-30 22:48:1440
[email protected]0a88a652012-03-09 00:34:4541
[email protected]4bf4d632012-05-31 15:50:3042def fix_python_path(cmd):
43 """Returns the fixed command line to call the right python executable."""
44 out = cmd[:]
45 if out[0] == 'python':
46 out[0] = sys.executable
47 elif out[0].endswith('.py'):
48 out.insert(0, sys.executable)
49 return out
50
51
John Abd-El-Malek14ae0542014-10-15 17:52:3152def get_asan_env(cmd, lsan):
53 """Returns the envirnoment flags needed for ASan and LSan."""
54
55 extra_env = {}
56
57 # Instruct GTK to use malloc while running ASan or LSan tests.
earthdoke6c54a42014-10-16 18:02:3658 extra_env['G_SLICE'] = 'always-malloc'
John Abd-El-Malek14ae0542014-10-15 17:52:3159
60 extra_env['NSS_DISABLE_ARENA_FREE_LIST'] = '1'
61 extra_env['NSS_DISABLE_UNLOAD'] = '1'
62
63 # TODO(glider): remove the symbolizer path once
64 # https://code.google.com/p/address-sanitizer/issues/detail?id=134 is fixed.
65 symbolizer_path = os.path.abspath(os.path.join(ROOT_DIR, 'third_party',
66 'llvm-build', 'Release+Asserts', 'bin', 'llvm-symbolizer'))
67
68 asan_options = []
69 if lsan:
70 asan_options.append('detect_leaks=1')
71 if sys.platform == 'linux2':
72 # Use the debug version of libstdc++ under LSan. If we don't, there will
73 # be a lot of incomplete stack traces in the reports.
74 extra_env['LD_LIBRARY_PATH'] = '/usr/lib/x86_64-linux-gnu/debug:'
75
76 # LSan is not sandbox-compatible, so we can use online symbolization. In
77 # fact, it needs symbolization to be able to apply suppressions.
78 symbolization_options = ['symbolize=1',
79 'external_symbolizer_path=%s' % symbolizer_path]
80
81 suppressions_file = os.path.join(ROOT_DIR, 'tools', 'lsan',
82 'suppressions.txt')
83 lsan_options = ['suppressions=%s' % suppressions_file,
84 'print_suppressions=1']
85 extra_env['LSAN_OPTIONS'] = ' '.join(lsan_options)
86 else:
87 # ASan uses a script for offline symbolization.
88 # Important note: when running ASan with leak detection enabled, we must use
89 # the LSan symbolization options above.
90 symbolization_options = ['symbolize=0']
vadimsh5cf3c442014-10-20 10:22:3891 # Set the path to llvm-symbolizer to be used by asan_symbolize.py
92 extra_env['LLVM_SYMBOLIZER_PATH'] = symbolizer_path
John Abd-El-Malek14ae0542014-10-15 17:52:3193
94 asan_options.extend(symbolization_options)
95
96 extra_env['ASAN_OPTIONS'] = ' '.join(asan_options)
97
98 if sys.platform == 'darwin':
99 isolate_output_dir = os.path.abspath(os.path.dirname(cmd[0]))
100 # This is needed because the test binary has @executable_path embedded in it
101 # it that the OS tries to resolve to the cache directory and not the mapped
102 # directory.
103 extra_env['DYLD_LIBRARY_PATH'] = str(isolate_output_dir)
104
105 return extra_env
106
107
earthdok9bd5d582015-01-29 21:19:36108def get_sanitizer_symbolize_command(json_path=None):
109 """Construct the command to invoke offline symbolization script."""
110 script_path = '../tools/valgrind/asan/asan_symbolize.py'
111 cmd = [sys.executable, script_path]
112 if json_path is not None:
113 cmd.append('--test-summary-json-file=%s' % json_path)
114 return cmd
115
116
117def get_json_path(cmd):
118 """Extract the JSON test summary path from a command line."""
119 json_path_flag = '--test-launcher-summary-output='
120 for arg in cmd:
121 if arg.startswith(json_path_flag):
122 return arg.split(json_path_flag).pop()
123 return None
124
125
126def symbolize_snippets_in_json(cmd, env):
127 """Symbolize output snippets inside the JSON test summary."""
128 json_path = get_json_path(cmd)
129 if json_path is None:
130 return
131
132 try:
133 symbolize_command = get_sanitizer_symbolize_command(json_path=json_path)
134 p = subprocess.Popen(symbolize_command, stderr=subprocess.PIPE, env=env)
135 (_, stderr) = p.communicate()
136 except OSError as e:
137 print 'Exception while symbolizing snippets: %s' % e
138
139 if p.returncode != 0:
140 print "Error: failed to symbolize snippets in JSON:\n"
141 print stderr
142
143
[email protected]0a88a652012-03-09 00:34:45144def run_executable(cmd, env):
145 """Runs an executable with:
[email protected]3766ed1c2012-07-26 20:53:56146 - environment variable CR_SOURCE_ROOT set to the root directory.
[email protected]0a88a652012-03-09 00:34:45147 - environment variable LANGUAGE to en_US.UTF-8.
earthdok748e1352015-01-27 23:04:08148 - environment variable CHROME_DEVEL_SANDBOX set
[email protected]0a88a652012-03-09 00:34:45149 - Reuses sys.executable automatically.
150 """
John Abd-El-Malek14ae0542014-10-15 17:52:31151 extra_env = {}
[email protected]0a88a652012-03-09 00:34:45152 # Many tests assume a English interface...
John Abd-El-Malek14ae0542014-10-15 17:52:31153 extra_env['LANG'] = 'en_US.UTF-8'
[email protected]3766ed1c2012-07-26 20:53:56154 # Used by base/base_paths_linux.cc as an override. Just make sure the default
155 # logic is used.
156 env.pop('CR_SOURCE_ROOT', None)
earthdok748e1352015-01-27 23:04:08157 extra_env.update(get_sandbox_env(env))
John Abd-El-Malek4569c422014-10-09 05:10:53158
159 # Copy logic from tools/build/scripts/slave/runtest.py.
160 asan = '--asan=1' in cmd
161 lsan = '--lsan=1' in cmd
earthdok9bd5d582015-01-29 21:19:36162 use_symbolization_script = asan and not lsan
John Abd-El-Malek4569c422014-10-09 05:10:53163
John Abd-El-Malek14ae0542014-10-15 17:52:31164 if asan:
165 extra_env.update(get_asan_env(cmd, lsan))
Nico Weberbfeb3d3a2015-01-28 06:16:25166 # ASan is not yet sandbox-friendly on Windows (http://crbug.com/382867).
167 if sys.platform == 'win32':
168 cmd.append('--no-sandbox')
John Abd-El-Malek14ae0542014-10-15 17:52:31169 if lsan:
170 cmd.append('--no-sandbox')
171
172 cmd = trim_cmd(cmd)
John Abd-El-Malek4569c422014-10-09 05:10:53173
[email protected]8ba98352012-05-23 20:43:59174 # Ensure paths are correctly separated on windows.
175 cmd[0] = cmd[0].replace('/', os.path.sep)
[email protected]4bf4d632012-05-31 15:50:30176 cmd = fix_python_path(cmd)
John Abd-El-Malek14ae0542014-10-15 17:52:31177
178 print('Additional test environment:\n%s\n'
179 'Command: %s\n' % (
180 '\n'.join(' %s=%s' %
181 (k, v) for k, v in sorted(extra_env.iteritems())),
182 ' '.join(cmd)))
183 env.update(extra_env or {})
[email protected]50ec9f232012-03-16 04:18:23184 try:
John Abd-El-Malek14ae0542014-10-15 17:52:31185 # See above comment regarding offline symbolization.
earthdok9bd5d582015-01-29 21:19:36186 if use_symbolization_script:
John Abd-El-Malek4569c422014-10-09 05:10:53187 # Need to pipe to the symbolizer script.
188 p1 = subprocess.Popen(cmd, env=env, stdout=subprocess.PIPE,
189 stderr=sys.stdout)
earthdok9bd5d582015-01-29 21:19:36190 p2 = subprocess.Popen(get_sanitizer_symbolize_command(),
vadimsh1eaeb2982014-10-20 12:28:45191 env=env, stdin=p1.stdout)
John Abd-El-Malek4569c422014-10-09 05:10:53192 p1.stdout.close() # Allow p1 to receive a SIGPIPE if p2 exits.
vadimsh1eaeb2982014-10-20 12:28:45193 p1.wait()
John Abd-El-Malek4569c422014-10-09 05:10:53194 p2.wait()
earthdok9bd5d582015-01-29 21:19:36195 # Also feed the out-of-band JSON output to the symbolizer script.
196 symbolize_snippets_in_json(cmd, env)
vadimsh1eaeb2982014-10-20 12:28:45197 return p1.returncode
John Abd-El-Malek4569c422014-10-09 05:10:53198 else:
199 return subprocess.call(cmd, env=env)
[email protected]50ec9f232012-03-16 04:18:23200 except OSError:
201 print >> sys.stderr, 'Failed to start %s' % cmd
202 raise
[email protected]0a88a652012-03-09 00:34:45203
204
205def main():
gliderb73f1872014-10-09 16:24:56206 return run_executable(sys.argv[1:], os.environ.copy())
[email protected]0a88a652012-03-09 00:34:45207
208
[email protected]ed763a72012-08-29 03:51:22209if __name__ == '__main__':
[email protected]0a88a652012-03-09 00:34:45210 sys.exit(main())