[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 | |
| 8 | import os |
[email protected] | 76361be45 | 2012-08-30 22:48:14 | [diff] [blame] | 9 | import stat |
[email protected] | 0a88a65 | 2012-03-09 00:34:45 | [diff] [blame] | 10 | import subprocess |
| 11 | import sys |
| 12 | |
| 13 | # This is hardcoded to be src/ relative to this script. |
| 14 | ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 15 | |
[email protected] | 76361be45 | 2012-08-30 22:48:14 | [diff] [blame] | 16 | CHROME_SANDBOX_ENV = 'CHROME_DEVEL_SANDBOX' |
| 17 | CHROME_SANDBOX_PATH = '/opt/chromium/chrome_sandbox' |
| 18 | |
| 19 | |
earthdok | 748e135 | 2015-01-27 23:04:08 | [diff] [blame] | 20 | def get_sandbox_env(env): |
| 21 | """Returns the environment flags needed for the SUID sandbox to work.""" |
John Abd-El-Malek | 14ae054 | 2014-10-15 17:52:31 | [diff] [blame] | 22 | extra_env = {} |
[email protected] | 76361be45 | 2012-08-30 22:48:14 | [diff] [blame] | 23 | chrome_sandbox_path = env.get(CHROME_SANDBOX_ENV, CHROME_SANDBOX_PATH) |
earthdok | 748e135 | 2015-01-27 23:04:08 | [diff] [blame] | 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 |
John Abd-El-Malek | 14ae054 | 2014-10-15 17:52:31 | [diff] [blame] | 31 | |
| 32 | return extra_env |
| 33 | |
| 34 | |
| 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.""" |
earthdok | eeb06530 | 2015-02-04 18:18:04 | [diff] [blame] | 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]) |
John Abd-El-Malek | 14ae054 | 2014-10-15 17:52:31 | [diff] [blame] | 42 | return [i for i in cmd if i not in internal_flags] |
[email protected] | 76361be45 | 2012-08-30 22:48:14 | [diff] [blame] | 43 | |
[email protected] | 0a88a65 | 2012-03-09 00:34:45 | [diff] [blame] | 44 | |
[email protected] | 4bf4d63 | 2012-05-31 15:50:30 | [diff] [blame] | 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 |
| 53 | |
| 54 | |
earthdok | eeb06530 | 2015-02-04 18:18:04 | [diff] [blame] | 55 | def get_sanitizer_env(cmd, asan, lsan, msan, tsan): |
| 56 | """Returns the envirnoment flags needed for sanitizer tools.""" |
John Abd-El-Malek | 14ae054 | 2014-10-15 17:52:31 | [diff] [blame] | 57 | |
| 58 | extra_env = {} |
| 59 | |
earthdok | eeb06530 | 2015-02-04 18:18:04 | [diff] [blame] | 60 | # Instruct GTK to use malloc while running sanitizer-instrumented tests. |
earthdok | e6c54a4 | 2014-10-16 18:02:36 | [diff] [blame] | 61 | extra_env['G_SLICE'] = 'always-malloc' |
John Abd-El-Malek | 14ae054 | 2014-10-15 17:52:31 | [diff] [blame] | 62 | |
| 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. |
Nico Weber | 1909731 | 2016-01-29 18:13:15 | [diff] [blame] | 68 | symbolizer_path = os.path.join(ROOT_DIR, |
| 69 | 'third_party', 'llvm-build', 'Release+Asserts', 'bin', 'llvm-symbolizer') |
John Abd-El-Malek | 14ae054 | 2014-10-15 17:52:31 | [diff] [blame] | 70 | |
earthdok | eeb06530 | 2015-02-04 18:18:04 | [diff] [blame] | 71 | if lsan or tsan: |
John Abd-El-Malek | 14ae054 | 2014-10-15 17:52:31 | [diff] [blame] | 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] |
timurrrr | 064f952 | 2015-02-12 15:46:32 | [diff] [blame] | 76 | elif (asan or msan) and sys.platform not in ['win32', 'cygwin']: |
| 77 | # ASan uses a script for offline symbolization, except on Windows. |
John Abd-El-Malek | 14ae054 | 2014-10-15 17:52:31 | [diff] [blame] | 78 | # Important note: when running ASan with leak detection enabled, we must use |
| 79 | # the LSan symbolization options above. |
| 80 | symbolization_options = ['symbolize=0'] |
vadimsh | 5cf3c44 | 2014-10-20 10:22:38 | [diff] [blame] | 81 | # Set the path to llvm-symbolizer to be used by asan_symbolize.py |
| 82 | extra_env['LLVM_SYMBOLIZER_PATH'] = symbolizer_path |
timurrrr | 7e67df9 | 2015-02-12 21:50:52 | [diff] [blame] | 83 | else: |
| 84 | symbolization_options = [] |
John Abd-El-Malek | 14ae054 | 2014-10-15 17:52:31 | [diff] [blame] | 85 | |
earthdok | eeb06530 | 2015-02-04 18:18:04 | [diff] [blame] | 86 | if asan: |
| 87 | asan_options = symbolization_options[:] |
| 88 | if lsan: |
| 89 | asan_options.append('detect_leaks=1') |
John Abd-El-Malek | 14ae054 | 2014-10-15 17:52:31 | [diff] [blame] | 90 | |
timurrrr | 7e67df9 | 2015-02-12 21:50:52 | [diff] [blame] | 91 | if asan_options: |
| 92 | extra_env['ASAN_OPTIONS'] = ' '.join(asan_options) |
John Abd-El-Malek | 14ae054 | 2014-10-15 17:52:31 | [diff] [blame] | 93 | |
earthdok | eeb06530 | 2015-02-04 18:18:04 | [diff] [blame] | 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) |
| 100 | |
| 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:' |
| 110 | |
earthdok | eeb06530 | 2015-02-04 18:18:04 | [diff] [blame] | 111 | extra_env['LSAN_OPTIONS'] = ' '.join(lsan_options) |
| 112 | |
| 113 | if msan: |
| 114 | msan_options = symbolization_options[:] |
| 115 | if lsan: |
| 116 | msan_options.append('detect_leaks=1') |
| 117 | extra_env['MSAN_OPTIONS'] = ' '.join(msan_options) |
| 118 | |
| 119 | if tsan: |
| 120 | tsan_options = symbolization_options[:] |
| 121 | extra_env['TSAN_OPTIONS'] = ' '.join(tsan_options) |
John Abd-El-Malek | 14ae054 | 2014-10-15 17:52:31 | [diff] [blame] | 122 | |
| 123 | return extra_env |
| 124 | |
| 125 | |
glider | 9d91934 | 2015-02-06 17:42:27 | [diff] [blame] | 126 | def get_sanitizer_symbolize_command(json_path=None, executable_path=None): |
earthdok | 9bd5d58 | 2015-01-29 21:19:36 | [diff] [blame] | 127 | """Construct the command to invoke offline symbolization script.""" |
Nico Weber | 1909731 | 2016-01-29 18:13:15 | [diff] [blame] | 128 | script_path = os.path.join( |
| 129 | ROOT_DIR, 'tools', 'valgrind', 'asan', 'asan_symbolize.py') |
earthdok | 9bd5d58 | 2015-01-29 21:19:36 | [diff] [blame] | 130 | cmd = [sys.executable, script_path] |
| 131 | if json_path is not None: |
| 132 | cmd.append('--test-summary-json-file=%s' % json_path) |
glider | 9d91934 | 2015-02-06 17:42:27 | [diff] [blame] | 133 | if executable_path is not None: |
| 134 | cmd.append('--executable-path=%s' % executable_path) |
earthdok | 9bd5d58 | 2015-01-29 21:19:36 | [diff] [blame] | 135 | return cmd |
| 136 | |
| 137 | |
| 138 | def get_json_path(cmd): |
| 139 | """Extract the JSON test summary path from a command line.""" |
| 140 | json_path_flag = '--test-launcher-summary-output=' |
| 141 | for arg in cmd: |
| 142 | if arg.startswith(json_path_flag): |
| 143 | return arg.split(json_path_flag).pop() |
| 144 | return None |
| 145 | |
| 146 | |
| 147 | def symbolize_snippets_in_json(cmd, env): |
| 148 | """Symbolize output snippets inside the JSON test summary.""" |
| 149 | json_path = get_json_path(cmd) |
| 150 | if json_path is None: |
| 151 | return |
| 152 | |
| 153 | try: |
glider | 9d91934 | 2015-02-06 17:42:27 | [diff] [blame] | 154 | symbolize_command = get_sanitizer_symbolize_command( |
| 155 | json_path=json_path, executable_path=cmd[0]) |
earthdok | 9bd5d58 | 2015-01-29 21:19:36 | [diff] [blame] | 156 | p = subprocess.Popen(symbolize_command, stderr=subprocess.PIPE, env=env) |
| 157 | (_, stderr) = p.communicate() |
| 158 | except OSError as e: |
thakis | 45643b4 | 2016-01-29 21:53:42 | [diff] [blame] | 159 | print >> sys.stderr, 'Exception while symbolizing snippets: %s' % e |
| 160 | raise |
earthdok | 9bd5d58 | 2015-01-29 21:19:36 | [diff] [blame] | 161 | |
| 162 | if p.returncode != 0: |
thakis | 45643b4 | 2016-01-29 21:53:42 | [diff] [blame] | 163 | print >> sys.stderr, "Error: failed to symbolize snippets in JSON:\n" |
| 164 | print >> sys.stderr, stderr |
| 165 | raise subprocess.CalledProcessError(p.returncode, symbolize_command) |
earthdok | 9bd5d58 | 2015-01-29 21:19:36 | [diff] [blame] | 166 | |
| 167 | |
[email protected] | 0a88a65 | 2012-03-09 00:34:45 | [diff] [blame] | 168 | def run_executable(cmd, env): |
| 169 | """Runs an executable with: |
[email protected] | 3766ed1c | 2012-07-26 20:53:56 | [diff] [blame] | 170 | - environment variable CR_SOURCE_ROOT set to the root directory. |
[email protected] | 0a88a65 | 2012-03-09 00:34:45 | [diff] [blame] | 171 | - environment variable LANGUAGE to en_US.UTF-8. |
earthdok | 748e135 | 2015-01-27 23:04:08 | [diff] [blame] | 172 | - environment variable CHROME_DEVEL_SANDBOX set |
[email protected] | 0a88a65 | 2012-03-09 00:34:45 | [diff] [blame] | 173 | - Reuses sys.executable automatically. |
| 174 | """ |
John Abd-El-Malek | 14ae054 | 2014-10-15 17:52:31 | [diff] [blame] | 175 | extra_env = {} |
[email protected] | 0a88a65 | 2012-03-09 00:34:45 | [diff] [blame] | 176 | # Many tests assume a English interface... |
John Abd-El-Malek | 14ae054 | 2014-10-15 17:52:31 | [diff] [blame] | 177 | extra_env['LANG'] = 'en_US.UTF-8' |
[email protected] | 3766ed1c | 2012-07-26 20:53:56 | [diff] [blame] | 178 | # Used by base/base_paths_linux.cc as an override. Just make sure the default |
| 179 | # logic is used. |
| 180 | env.pop('CR_SOURCE_ROOT', None) |
earthdok | 748e135 | 2015-01-27 23:04:08 | [diff] [blame] | 181 | extra_env.update(get_sandbox_env(env)) |
John Abd-El-Malek | 4569c42 | 2014-10-09 05:10:53 | [diff] [blame] | 182 | |
| 183 | # Copy logic from tools/build/scripts/slave/runtest.py. |
| 184 | asan = '--asan=1' in cmd |
| 185 | lsan = '--lsan=1' in cmd |
earthdok | eeb06530 | 2015-02-04 18:18:04 | [diff] [blame] | 186 | msan = '--msan=1' in cmd |
| 187 | tsan = '--tsan=1' in cmd |
timurrrr | 064f952 | 2015-02-12 15:46:32 | [diff] [blame] | 188 | if sys.platform in ['win32', 'cygwin']: |
| 189 | # Symbolization works in-process on Windows even when sandboxed. |
| 190 | use_symbolization_script = False |
| 191 | else: |
| 192 | # LSan doesn't support sandboxing yet, so we use the in-process symbolizer. |
| 193 | # Note that ASan and MSan can work together with LSan. |
| 194 | use_symbolization_script = (asan or msan) and not lsan |
John Abd-El-Malek | 4569c42 | 2014-10-09 05:10:53 | [diff] [blame] | 195 | |
earthdok | eeb06530 | 2015-02-04 18:18:04 | [diff] [blame] | 196 | if asan or lsan or msan or tsan: |
| 197 | extra_env.update(get_sanitizer_env(cmd, asan, lsan, msan, tsan)) |
| 198 | |
Timur Iskhodzhanov | fdbfd4e1 | 2015-02-05 13:50:23 | [diff] [blame] | 199 | if lsan or tsan: |
| 200 | # LSan and TSan are not sandbox-friendly. |
| 201 | cmd.append('--no-sandbox') |
John Abd-El-Malek | 14ae054 | 2014-10-15 17:52:31 | [diff] [blame] | 202 | |
| 203 | cmd = trim_cmd(cmd) |
John Abd-El-Malek | 4569c42 | 2014-10-09 05:10:53 | [diff] [blame] | 204 | |
[email protected] | 8ba9835 | 2012-05-23 20:43:59 | [diff] [blame] | 205 | # Ensure paths are correctly separated on windows. |
| 206 | cmd[0] = cmd[0].replace('/', os.path.sep) |
[email protected] | 4bf4d63 | 2012-05-31 15:50:30 | [diff] [blame] | 207 | cmd = fix_python_path(cmd) |
John Abd-El-Malek | 14ae054 | 2014-10-15 17:52:31 | [diff] [blame] | 208 | |
| 209 | print('Additional test environment:\n%s\n' |
| 210 | 'Command: %s\n' % ( |
| 211 | '\n'.join(' %s=%s' % |
| 212 | (k, v) for k, v in sorted(extra_env.iteritems())), |
| 213 | ' '.join(cmd))) |
| 214 | env.update(extra_env or {}) |
[email protected] | 50ec9f23 | 2012-03-16 04:18:23 | [diff] [blame] | 215 | try: |
John Abd-El-Malek | 14ae054 | 2014-10-15 17:52:31 | [diff] [blame] | 216 | # See above comment regarding offline symbolization. |
earthdok | 9bd5d58 | 2015-01-29 21:19:36 | [diff] [blame] | 217 | if use_symbolization_script: |
John Abd-El-Malek | 4569c42 | 2014-10-09 05:10:53 | [diff] [blame] | 218 | # Need to pipe to the symbolizer script. |
| 219 | p1 = subprocess.Popen(cmd, env=env, stdout=subprocess.PIPE, |
| 220 | stderr=sys.stdout) |
glider | 9d91934 | 2015-02-06 17:42:27 | [diff] [blame] | 221 | p2 = subprocess.Popen( |
| 222 | get_sanitizer_symbolize_command(executable_path=cmd[0]), |
| 223 | env=env, stdin=p1.stdout) |
John Abd-El-Malek | 4569c42 | 2014-10-09 05:10:53 | [diff] [blame] | 224 | p1.stdout.close() # Allow p1 to receive a SIGPIPE if p2 exits. |
vadimsh | 1eaeb298 | 2014-10-20 12:28:45 | [diff] [blame] | 225 | p1.wait() |
John Abd-El-Malek | 4569c42 | 2014-10-09 05:10:53 | [diff] [blame] | 226 | p2.wait() |
earthdok | 9bd5d58 | 2015-01-29 21:19:36 | [diff] [blame] | 227 | # Also feed the out-of-band JSON output to the symbolizer script. |
| 228 | symbolize_snippets_in_json(cmd, env) |
vadimsh | 1eaeb298 | 2014-10-20 12:28:45 | [diff] [blame] | 229 | return p1.returncode |
John Abd-El-Malek | 4569c42 | 2014-10-09 05:10:53 | [diff] [blame] | 230 | else: |
| 231 | return subprocess.call(cmd, env=env) |
[email protected] | 50ec9f23 | 2012-03-16 04:18:23 | [diff] [blame] | 232 | except OSError: |
| 233 | print >> sys.stderr, 'Failed to start %s' % cmd |
| 234 | raise |
[email protected] | 0a88a65 | 2012-03-09 00:34:45 | [diff] [blame] | 235 | |
| 236 | |
| 237 | def main(): |
glider | b73f187 | 2014-10-09 16:24:56 | [diff] [blame] | 238 | return run_executable(sys.argv[1:], os.environ.copy()) |
[email protected] | 0a88a65 | 2012-03-09 00:34:45 | [diff] [blame] | 239 | |
| 240 | |
[email protected] | ed763a7 | 2012-08-29 03:51:22 | [diff] [blame] | 241 | if __name__ == '__main__': |
[email protected] | 0a88a65 | 2012-03-09 00:34:45 | [diff] [blame] | 242 | sys.exit(main()) |