[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 | |
glider | 4795390 | 2014-10-09 10:09:58 | [diff] [blame] | 8 | import collections |
[email protected] | 0a88a65 | 2012-03-09 00:34:45 | [diff] [blame] | 9 | import os |
[email protected] | 76361be45 | 2012-08-30 22:48:14 | [diff] [blame] | 10 | import stat |
[email protected] | 0a88a65 | 2012-03-09 00:34:45 | [diff] [blame] | 11 | import subprocess |
| 12 | import sys |
| 13 | |
| 14 | # This is hardcoded to be src/ relative to this script. |
| 15 | ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 16 | |
[email protected] | 76361be45 | 2012-08-30 22:48:14 | [diff] [blame] | 17 | CHROME_SANDBOX_ENV = 'CHROME_DEVEL_SANDBOX' |
| 18 | CHROME_SANDBOX_PATH = '/opt/chromium/chrome_sandbox' |
| 19 | |
| 20 | |
jam | 92ee4a3 | 2014-09-30 01:22:09 | [diff] [blame] | 21 | def should_enable_sandbox(cmd, sandbox_path): |
[email protected] | 76361be45 | 2012-08-30 22:48:14 | [diff] [blame] | 22 | """Return a boolean indicating that the current slave is capable of using the |
| 23 | sandbox and should enable it. This should return True iff the slave is a |
| 24 | Linux host with the sandbox file present and configured correctly.""" |
| 25 | if not (sys.platform.startswith('linux') and |
| 26 | os.path.exists(sandbox_path)): |
| 27 | return False |
jam | 92ee4a3 | 2014-09-30 01:22:09 | [diff] [blame] | 28 | |
| 29 | # Copy the check in tools/build/scripts/slave/runtest.py. |
| 30 | if '--lsan=1' in cmd: |
| 31 | return False |
| 32 | |
[email protected] | 76361be45 | 2012-08-30 22:48:14 | [diff] [blame] | 33 | sandbox_stat = os.stat(sandbox_path) |
| 34 | if ((sandbox_stat.st_mode & stat.S_ISUID) and |
| 35 | (sandbox_stat.st_mode & stat.S_IRUSR) and |
| 36 | (sandbox_stat.st_mode & stat.S_IXUSR) and |
| 37 | (sandbox_stat.st_uid == 0)): |
| 38 | return True |
| 39 | return False |
| 40 | |
| 41 | |
jam | 92ee4a3 | 2014-09-30 01:22:09 | [diff] [blame] | 42 | def enable_sandbox_if_required(cmd, env, verbose=False): |
[email protected] | 76361be45 | 2012-08-30 22:48:14 | [diff] [blame] | 43 | """Checks enables the sandbox if it is required, otherwise it disables it.""" |
| 44 | chrome_sandbox_path = env.get(CHROME_SANDBOX_ENV, CHROME_SANDBOX_PATH) |
| 45 | |
jam | 92ee4a3 | 2014-09-30 01:22:09 | [diff] [blame] | 46 | if should_enable_sandbox(cmd, chrome_sandbox_path): |
[email protected] | 76361be45 | 2012-08-30 22:48:14 | [diff] [blame] | 47 | if verbose: |
| 48 | print 'Enabling sandbox. Setting environment variable:' |
| 49 | print ' %s="%s"' % (CHROME_SANDBOX_ENV, chrome_sandbox_path) |
| 50 | env[CHROME_SANDBOX_ENV] = chrome_sandbox_path |
| 51 | else: |
| 52 | if verbose: |
jam | 92ee4a3 | 2014-09-30 01:22:09 | [diff] [blame] | 53 | print 'Disabling sandbox. Setting environment variable:' |
| 54 | print ' CHROME_DEVEL_SANDBOX=""' |
| 55 | env['CHROME_DEVEL_SANDBOX'] = '' |
[email protected] | 76361be45 | 2012-08-30 22:48:14 | [diff] [blame] | 56 | |
[email protected] | 0a88a65 | 2012-03-09 00:34:45 | [diff] [blame] | 57 | |
[email protected] | 4bf4d63 | 2012-05-31 15:50:30 | [diff] [blame] | 58 | def fix_python_path(cmd): |
| 59 | """Returns the fixed command line to call the right python executable.""" |
| 60 | out = cmd[:] |
| 61 | if out[0] == 'python': |
| 62 | out[0] = sys.executable |
| 63 | elif out[0].endswith('.py'): |
| 64 | out.insert(0, sys.executable) |
| 65 | return out |
| 66 | |
| 67 | |
[email protected] | 0a88a65 | 2012-03-09 00:34:45 | [diff] [blame] | 68 | def run_executable(cmd, env): |
| 69 | """Runs an executable with: |
[email protected] | 3766ed1c | 2012-07-26 20:53:56 | [diff] [blame] | 70 | - environment variable CR_SOURCE_ROOT set to the root directory. |
[email protected] | 0a88a65 | 2012-03-09 00:34:45 | [diff] [blame] | 71 | - environment variable LANGUAGE to en_US.UTF-8. |
[email protected] | 76361be45 | 2012-08-30 22:48:14 | [diff] [blame] | 72 | - environment variable CHROME_DEVEL_SANDBOX set if need |
[email protected] | 0a88a65 | 2012-03-09 00:34:45 | [diff] [blame] | 73 | - Reuses sys.executable automatically. |
| 74 | """ |
glider | b73f187 | 2014-10-09 16:24:56 | [diff] [blame^] | 75 | env = collections.defaultdict(str, env) |
[email protected] | 0a88a65 | 2012-03-09 00:34:45 | [diff] [blame] | 76 | # Many tests assume a English interface... |
[email protected] | 331578db | 2012-08-14 16:56:50 | [diff] [blame] | 77 | env['LANG'] = 'en_US.UTF-8' |
[email protected] | 3766ed1c | 2012-07-26 20:53:56 | [diff] [blame] | 78 | # Used by base/base_paths_linux.cc as an override. Just make sure the default |
| 79 | # logic is used. |
| 80 | env.pop('CR_SOURCE_ROOT', None) |
jam | 92ee4a3 | 2014-09-30 01:22:09 | [diff] [blame] | 81 | enable_sandbox_if_required(cmd, env) |
John Abd-El-Malek | 4569c42 | 2014-10-09 05:10:53 | [diff] [blame] | 82 | |
| 83 | # Copy logic from tools/build/scripts/slave/runtest.py. |
| 84 | asan = '--asan=1' in cmd |
| 85 | lsan = '--lsan=1' in cmd |
| 86 | if lsan and sys.platform == 'linux2': |
| 87 | # Use the debug version of libstdc++ under LSan. If we don't, there will |
| 88 | # be a lot of incomplete stack traces in the reports. |
John Abd-El-Malek | 2003984 | 2014-10-09 16:10:54 | [diff] [blame] | 89 | env['LD_LIBRARY_PATH'] = '/usr/lib/x86_64-linux-gnu/debug:' |
John Abd-El-Malek | 4569c42 | 2014-10-09 05:10:53 | [diff] [blame] | 90 | |
| 91 | if asan and 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 it |
| 94 | # that the OS tries to resolve to the cache directory and not the mapped |
| 95 | # directory. |
| 96 | env['DYLD_LIBRARY_PATH'] = str(isolate_output_dir) |
| 97 | |
[email protected] | 8ba9835 | 2012-05-23 20:43:59 | [diff] [blame] | 98 | # Ensure paths are correctly separated on windows. |
| 99 | cmd[0] = cmd[0].replace('/', os.path.sep) |
[email protected] | 4bf4d63 | 2012-05-31 15:50:30 | [diff] [blame] | 100 | cmd = fix_python_path(cmd) |
[email protected] | 50ec9f23 | 2012-03-16 04:18:23 | [diff] [blame] | 101 | try: |
John Abd-El-Malek | 4569c42 | 2014-10-09 05:10:53 | [diff] [blame] | 102 | if asan: |
| 103 | # Need to pipe to the symbolizer script. |
| 104 | p1 = subprocess.Popen(cmd, env=env, stdout=subprocess.PIPE, |
| 105 | stderr=sys.stdout) |
| 106 | p2 = subprocess.Popen(["../tools/valgrind/asan/asan_symbolize.py"], |
| 107 | stdin=p1.stdout) |
| 108 | p1.stdout.close() # Allow p1 to receive a SIGPIPE if p2 exits. |
| 109 | p2.wait() |
| 110 | return p2.returncode |
| 111 | else: |
| 112 | return subprocess.call(cmd, env=env) |
[email protected] | 50ec9f23 | 2012-03-16 04:18:23 | [diff] [blame] | 113 | except OSError: |
| 114 | print >> sys.stderr, 'Failed to start %s' % cmd |
| 115 | raise |
[email protected] | 0a88a65 | 2012-03-09 00:34:45 | [diff] [blame] | 116 | |
| 117 | |
| 118 | def main(): |
glider | b73f187 | 2014-10-09 16:24:56 | [diff] [blame^] | 119 | return run_executable(sys.argv[1:], os.environ.copy()) |
[email protected] | 0a88a65 | 2012-03-09 00:34:45 | [diff] [blame] | 120 | |
| 121 | |
[email protected] | ed763a7 | 2012-08-29 03:51:22 | [diff] [blame] | 122 | if __name__ == '__main__': |
[email protected] | 0a88a65 | 2012-03-09 00:34:45 | [diff] [blame] | 123 | sys.exit(main()) |