[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 | |
jam | 92ee4a3 | 2014-09-30 01:22:09 | [diff] [blame] | 20 | def should_enable_sandbox(cmd, sandbox_path): |
[email protected] | 76361be45 | 2012-08-30 22:48:14 | [diff] [blame] | 21 | """Return a boolean indicating that the current slave is capable of using the |
| 22 | sandbox and should enable it. This should return True iff the slave is a |
| 23 | Linux host with the sandbox file present and configured correctly.""" |
| 24 | if not (sys.platform.startswith('linux') and |
| 25 | os.path.exists(sandbox_path)): |
| 26 | return False |
jam | 92ee4a3 | 2014-09-30 01:22:09 | [diff] [blame] | 27 | |
| 28 | # Copy the check in tools/build/scripts/slave/runtest.py. |
| 29 | if '--lsan=1' in cmd: |
| 30 | return False |
| 31 | |
[email protected] | 76361be45 | 2012-08-30 22:48:14 | [diff] [blame] | 32 | sandbox_stat = os.stat(sandbox_path) |
| 33 | if ((sandbox_stat.st_mode & stat.S_ISUID) and |
| 34 | (sandbox_stat.st_mode & stat.S_IRUSR) and |
| 35 | (sandbox_stat.st_mode & stat.S_IXUSR) and |
| 36 | (sandbox_stat.st_uid == 0)): |
| 37 | return True |
| 38 | return False |
| 39 | |
| 40 | |
John Abd-El-Malek | 14ae054 | 2014-10-15 17:52:31 | [diff] [blame] | 41 | def get_sandbox_env(cmd, env, verbose=False): |
| 42 | """Checks enables the sandbox if it is required, otherwise it disables it. |
| 43 | Returns the environment flags to set.""" |
| 44 | extra_env = {} |
[email protected] | 76361be45 | 2012-08-30 22:48:14 | [diff] [blame] | 45 | chrome_sandbox_path = env.get(CHROME_SANDBOX_ENV, CHROME_SANDBOX_PATH) |
| 46 | |
jam | 92ee4a3 | 2014-09-30 01:22:09 | [diff] [blame] | 47 | if should_enable_sandbox(cmd, chrome_sandbox_path): |
[email protected] | 76361be45 | 2012-08-30 22:48:14 | [diff] [blame] | 48 | if verbose: |
| 49 | print 'Enabling sandbox. Setting environment variable:' |
| 50 | print ' %s="%s"' % (CHROME_SANDBOX_ENV, chrome_sandbox_path) |
John Abd-El-Malek | 14ae054 | 2014-10-15 17:52:31 | [diff] [blame] | 51 | extra_env[CHROME_SANDBOX_ENV] = chrome_sandbox_path |
[email protected] | 76361be45 | 2012-08-30 22:48:14 | [diff] [blame] | 52 | else: |
| 53 | if verbose: |
jam | 92ee4a3 | 2014-09-30 01:22:09 | [diff] [blame] | 54 | print 'Disabling sandbox. Setting environment variable:' |
| 55 | print ' CHROME_DEVEL_SANDBOX=""' |
John Abd-El-Malek | 14ae054 | 2014-10-15 17:52:31 | [diff] [blame] | 56 | extra_env['CHROME_DEVEL_SANDBOX'] = '' |
| 57 | |
| 58 | return extra_env |
| 59 | |
| 60 | |
| 61 | def trim_cmd(cmd): |
| 62 | """Removes internal flags from cmd since they're just used to communicate from |
| 63 | the host machine to this script running on the swarm slaves.""" |
| 64 | internal_flags = frozenset(['--asan=0', '--asan=1', '--lsan=0', '--lsan=1']) |
| 65 | return [i for i in cmd if i not in internal_flags] |
[email protected] | 76361be45 | 2012-08-30 22:48:14 | [diff] [blame] | 66 | |
[email protected] | 0a88a65 | 2012-03-09 00:34:45 | [diff] [blame] | 67 | |
[email protected] | 4bf4d63 | 2012-05-31 15:50:30 | [diff] [blame] | 68 | def fix_python_path(cmd): |
| 69 | """Returns the fixed command line to call the right python executable.""" |
| 70 | out = cmd[:] |
| 71 | if out[0] == 'python': |
| 72 | out[0] = sys.executable |
| 73 | elif out[0].endswith('.py'): |
| 74 | out.insert(0, sys.executable) |
| 75 | return out |
| 76 | |
| 77 | |
John Abd-El-Malek | 14ae054 | 2014-10-15 17:52:31 | [diff] [blame] | 78 | def get_asan_env(cmd, lsan): |
| 79 | """Returns the envirnoment flags needed for ASan and LSan.""" |
| 80 | |
| 81 | extra_env = {} |
| 82 | |
| 83 | # Instruct GTK to use malloc while running ASan or LSan tests. |
earthdok | e6c54a4 | 2014-10-16 18:02:36 | [diff] [blame] | 84 | extra_env['G_SLICE'] = 'always-malloc' |
John Abd-El-Malek | 14ae054 | 2014-10-15 17:52:31 | [diff] [blame] | 85 | |
| 86 | extra_env['NSS_DISABLE_ARENA_FREE_LIST'] = '1' |
| 87 | extra_env['NSS_DISABLE_UNLOAD'] = '1' |
| 88 | |
| 89 | # TODO(glider): remove the symbolizer path once |
| 90 | # https://code.google.com/p/address-sanitizer/issues/detail?id=134 is fixed. |
| 91 | symbolizer_path = os.path.abspath(os.path.join(ROOT_DIR, 'third_party', |
| 92 | 'llvm-build', 'Release+Asserts', 'bin', 'llvm-symbolizer')) |
| 93 | |
| 94 | asan_options = [] |
| 95 | if lsan: |
| 96 | asan_options.append('detect_leaks=1') |
| 97 | if sys.platform == 'linux2': |
| 98 | # Use the debug version of libstdc++ under LSan. If we don't, there will |
| 99 | # be a lot of incomplete stack traces in the reports. |
| 100 | extra_env['LD_LIBRARY_PATH'] = '/usr/lib/x86_64-linux-gnu/debug:' |
| 101 | |
| 102 | # LSan is not sandbox-compatible, so we can use online symbolization. In |
| 103 | # fact, it needs symbolization to be able to apply suppressions. |
| 104 | symbolization_options = ['symbolize=1', |
| 105 | 'external_symbolizer_path=%s' % symbolizer_path] |
| 106 | |
| 107 | suppressions_file = os.path.join(ROOT_DIR, 'tools', 'lsan', |
| 108 | 'suppressions.txt') |
| 109 | lsan_options = ['suppressions=%s' % suppressions_file, |
| 110 | 'print_suppressions=1'] |
| 111 | extra_env['LSAN_OPTIONS'] = ' '.join(lsan_options) |
| 112 | else: |
| 113 | # ASan uses a script for offline symbolization. |
| 114 | # Important note: when running ASan with leak detection enabled, we must use |
| 115 | # the LSan symbolization options above. |
| 116 | symbolization_options = ['symbolize=0'] |
vadimsh | 5cf3c44 | 2014-10-20 10:22:38 | [diff] [blame] | 117 | # Set the path to llvm-symbolizer to be used by asan_symbolize.py |
| 118 | extra_env['LLVM_SYMBOLIZER_PATH'] = symbolizer_path |
John Abd-El-Malek | 14ae054 | 2014-10-15 17:52:31 | [diff] [blame] | 119 | |
| 120 | asan_options.extend(symbolization_options) |
| 121 | |
| 122 | extra_env['ASAN_OPTIONS'] = ' '.join(asan_options) |
| 123 | |
| 124 | if sys.platform == 'darwin': |
| 125 | isolate_output_dir = os.path.abspath(os.path.dirname(cmd[0])) |
| 126 | # This is needed because the test binary has @executable_path embedded in it |
| 127 | # it that the OS tries to resolve to the cache directory and not the mapped |
| 128 | # directory. |
| 129 | extra_env['DYLD_LIBRARY_PATH'] = str(isolate_output_dir) |
| 130 | |
| 131 | return extra_env |
| 132 | |
| 133 | |
[email protected] | 0a88a65 | 2012-03-09 00:34:45 | [diff] [blame] | 134 | def run_executable(cmd, env): |
| 135 | """Runs an executable with: |
[email protected] | 3766ed1c | 2012-07-26 20:53:56 | [diff] [blame] | 136 | - environment variable CR_SOURCE_ROOT set to the root directory. |
[email protected] | 0a88a65 | 2012-03-09 00:34:45 | [diff] [blame] | 137 | - environment variable LANGUAGE to en_US.UTF-8. |
[email protected] | 76361be45 | 2012-08-30 22:48:14 | [diff] [blame] | 138 | - environment variable CHROME_DEVEL_SANDBOX set if need |
[email protected] | 0a88a65 | 2012-03-09 00:34:45 | [diff] [blame] | 139 | - Reuses sys.executable automatically. |
| 140 | """ |
John Abd-El-Malek | 14ae054 | 2014-10-15 17:52:31 | [diff] [blame] | 141 | extra_env = {} |
[email protected] | 0a88a65 | 2012-03-09 00:34:45 | [diff] [blame] | 142 | # Many tests assume a English interface... |
John Abd-El-Malek | 14ae054 | 2014-10-15 17:52:31 | [diff] [blame] | 143 | extra_env['LANG'] = 'en_US.UTF-8' |
[email protected] | 3766ed1c | 2012-07-26 20:53:56 | [diff] [blame] | 144 | # Used by base/base_paths_linux.cc as an override. Just make sure the default |
| 145 | # logic is used. |
| 146 | env.pop('CR_SOURCE_ROOT', None) |
John Abd-El-Malek | 14ae054 | 2014-10-15 17:52:31 | [diff] [blame] | 147 | extra_env.update(get_sandbox_env(cmd, env)) |
John Abd-El-Malek | 4569c42 | 2014-10-09 05:10:53 | [diff] [blame] | 148 | |
| 149 | # Copy logic from tools/build/scripts/slave/runtest.py. |
| 150 | asan = '--asan=1' in cmd |
| 151 | lsan = '--lsan=1' in cmd |
John Abd-El-Malek | 4569c42 | 2014-10-09 05:10:53 | [diff] [blame] | 152 | |
John Abd-El-Malek | 14ae054 | 2014-10-15 17:52:31 | [diff] [blame] | 153 | if asan: |
| 154 | extra_env.update(get_asan_env(cmd, lsan)) |
| 155 | if lsan: |
| 156 | cmd.append('--no-sandbox') |
| 157 | |
| 158 | cmd = trim_cmd(cmd) |
John Abd-El-Malek | 4569c42 | 2014-10-09 05:10:53 | [diff] [blame] | 159 | |
[email protected] | 8ba9835 | 2012-05-23 20:43:59 | [diff] [blame] | 160 | # Ensure paths are correctly separated on windows. |
| 161 | cmd[0] = cmd[0].replace('/', os.path.sep) |
[email protected] | 4bf4d63 | 2012-05-31 15:50:30 | [diff] [blame] | 162 | cmd = fix_python_path(cmd) |
John Abd-El-Malek | 14ae054 | 2014-10-15 17:52:31 | [diff] [blame] | 163 | |
| 164 | print('Additional test environment:\n%s\n' |
| 165 | 'Command: %s\n' % ( |
| 166 | '\n'.join(' %s=%s' % |
| 167 | (k, v) for k, v in sorted(extra_env.iteritems())), |
| 168 | ' '.join(cmd))) |
| 169 | env.update(extra_env or {}) |
[email protected] | 50ec9f23 | 2012-03-16 04:18:23 | [diff] [blame] | 170 | try: |
John Abd-El-Malek | 14ae054 | 2014-10-15 17:52:31 | [diff] [blame] | 171 | # See above comment regarding offline symbolization. |
| 172 | if asan and not lsan: |
John Abd-El-Malek | 4569c42 | 2014-10-09 05:10:53 | [diff] [blame] | 173 | # Need to pipe to the symbolizer script. |
| 174 | p1 = subprocess.Popen(cmd, env=env, stdout=subprocess.PIPE, |
| 175 | stderr=sys.stdout) |
| 176 | p2 = subprocess.Popen(["../tools/valgrind/asan/asan_symbolize.py"], |
vadimsh | 1eaeb298 | 2014-10-20 12:28:45 | [diff] [blame^] | 177 | env=env, stdin=p1.stdout) |
John Abd-El-Malek | 4569c42 | 2014-10-09 05:10:53 | [diff] [blame] | 178 | p1.stdout.close() # Allow p1 to receive a SIGPIPE if p2 exits. |
vadimsh | 1eaeb298 | 2014-10-20 12:28:45 | [diff] [blame^] | 179 | p1.wait() |
John Abd-El-Malek | 4569c42 | 2014-10-09 05:10:53 | [diff] [blame] | 180 | p2.wait() |
vadimsh | 1eaeb298 | 2014-10-20 12:28:45 | [diff] [blame^] | 181 | return p1.returncode |
John Abd-El-Malek | 4569c42 | 2014-10-09 05:10:53 | [diff] [blame] | 182 | else: |
| 183 | return subprocess.call(cmd, env=env) |
[email protected] | 50ec9f23 | 2012-03-16 04:18:23 | [diff] [blame] | 184 | except OSError: |
| 185 | print >> sys.stderr, 'Failed to start %s' % cmd |
| 186 | raise |
[email protected] | 0a88a65 | 2012-03-09 00:34:45 | [diff] [blame] | 187 | |
| 188 | |
| 189 | def main(): |
glider | b73f187 | 2014-10-09 16:24:56 | [diff] [blame] | 190 | return run_executable(sys.argv[1:], os.environ.copy()) |
[email protected] | 0a88a65 | 2012-03-09 00:34:45 | [diff] [blame] | 191 | |
| 192 | |
[email protected] | ed763a7 | 2012-08-29 03:51:22 | [diff] [blame] | 193 | if __name__ == '__main__': |
[email protected] | 0a88a65 | 2012-03-09 00:34:45 | [diff] [blame] | 194 | sys.exit(main()) |