test_env.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. """Sets environment variables needed to run a chromium unit test."""
  6. from __future__ import print_function
  7. import io
  8. import os
  9. import signal
  10. import subprocess
  11. import sys
  12. import time
  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. CHROME_SANDBOX_ENV = 'CHROME_DEVEL_SANDBOX'
  16. CHROME_SANDBOX_PATH = '/opt/chromium/chrome_sandbox'
  17. def get_sandbox_env(env):
  18. """Returns the environment flags needed for the SUID sandbox to work."""
  19. extra_env = {}
  20. chrome_sandbox_path = env.get(CHROME_SANDBOX_ENV, CHROME_SANDBOX_PATH)
  21. # The above would silently disable the SUID sandbox if the env value were
  22. # an empty string. We don't want to allow that. http://crbug.com/245376
  23. # TODO(jln): Remove this check once it's no longer possible to disable the
  24. # sandbox that way.
  25. if not chrome_sandbox_path:
  26. chrome_sandbox_path = CHROME_SANDBOX_PATH
  27. extra_env[CHROME_SANDBOX_ENV] = chrome_sandbox_path
  28. return extra_env
  29. def trim_cmd(cmd):
  30. """Removes internal flags from cmd since they're just used to communicate from
  31. the host machine to this script running on the swarm slaves."""
  32. sanitizers = ['asan', 'lsan', 'msan', 'tsan', 'coverage-continuous-mode',
  33. 'skip-set-lpac-acls']
  34. internal_flags = frozenset('--%s=%d' % (name, value)
  35. for name in sanitizers
  36. for value in [0, 1])
  37. return [i for i in cmd if i not in internal_flags]
  38. def fix_python_path(cmd):
  39. """Returns the fixed command line to call the right python executable."""
  40. out = cmd[:]
  41. if out[0] == 'python':
  42. out[0] = sys.executable
  43. elif out[0].endswith('.py'):
  44. out.insert(0, sys.executable)
  45. return out
  46. def get_sanitizer_env(asan, lsan, msan, tsan, cfi_diag):
  47. """Returns the environment flags needed for sanitizer tools."""
  48. extra_env = {}
  49. # Instruct GTK to use malloc while running sanitizer-instrumented tests.
  50. extra_env['G_SLICE'] = 'always-malloc'
  51. extra_env['NSS_DISABLE_ARENA_FREE_LIST'] = '1'
  52. extra_env['NSS_DISABLE_UNLOAD'] = '1'
  53. # TODO(glider): remove the symbolizer path once
  54. # https://code.google.com/p/address-sanitizer/issues/detail?id=134 is fixed.
  55. symbolizer_path = os.path.join(ROOT_DIR,
  56. 'third_party', 'llvm-build', 'Release+Asserts', 'bin', 'llvm-symbolizer')
  57. if lsan or tsan:
  58. # LSan is not sandbox-compatible, so we can use online symbolization. In
  59. # fact, it needs symbolization to be able to apply suppressions.
  60. symbolization_options = ['symbolize=1',
  61. 'external_symbolizer_path=%s' % symbolizer_path]
  62. elif (asan or msan or cfi_diag) and sys.platform not in ['win32', 'cygwin']:
  63. # ASan uses a script for offline symbolization, except on Windows.
  64. # Important note: when running ASan with leak detection enabled, we must use
  65. # the LSan symbolization options above.
  66. symbolization_options = ['symbolize=0']
  67. # Set the path to llvm-symbolizer to be used by asan_symbolize.py
  68. extra_env['LLVM_SYMBOLIZER_PATH'] = symbolizer_path
  69. else:
  70. symbolization_options = []
  71. # Leverage sanitizer to print stack trace on abort (e.g. assertion failure).
  72. symbolization_options.append('handle_abort=1')
  73. if asan:
  74. asan_options = symbolization_options[:]
  75. if lsan:
  76. asan_options.append('detect_leaks=1')
  77. # LSan appears to have trouble with later versions of glibc.
  78. # See https://github.com/google/sanitizers/issues/1322
  79. if 'linux' in sys.platform:
  80. asan_options.append('intercept_tls_get_addr=0')
  81. if asan_options:
  82. extra_env['ASAN_OPTIONS'] = ' '.join(asan_options)
  83. if lsan:
  84. if asan or msan:
  85. lsan_options = []
  86. else:
  87. lsan_options = symbolization_options[:]
  88. if sys.platform == 'linux2':
  89. # Use the debug version of libstdc++ under LSan. If we don't, there will
  90. # be a lot of incomplete stack traces in the reports.
  91. extra_env['LD_LIBRARY_PATH'] = '/usr/lib/x86_64-linux-gnu/debug:'
  92. extra_env['LSAN_OPTIONS'] = ' '.join(lsan_options)
  93. if msan:
  94. msan_options = symbolization_options[:]
  95. if lsan:
  96. msan_options.append('detect_leaks=1')
  97. extra_env['MSAN_OPTIONS'] = ' '.join(msan_options)
  98. if tsan:
  99. tsan_options = symbolization_options[:]
  100. extra_env['TSAN_OPTIONS'] = ' '.join(tsan_options)
  101. # CFI uses the UBSan runtime to provide diagnostics.
  102. if cfi_diag:
  103. ubsan_options = symbolization_options[:] + ['print_stacktrace=1']
  104. extra_env['UBSAN_OPTIONS'] = ' '.join(ubsan_options)
  105. return extra_env
  106. def get_coverage_continuous_mode_env(env):
  107. """Append %c (clang code coverage continuous mode) flag to LLVM_PROFILE_FILE
  108. pattern string."""
  109. llvm_profile_file = env.get('LLVM_PROFILE_FILE')
  110. if not llvm_profile_file:
  111. return {}
  112. dirname, basename = os.path.split(llvm_profile_file)
  113. root, ext = os.path.splitext(basename)
  114. return {
  115. 'LLVM_PROFILE_FILE': os.path.join(dirname, root + "%c" + ext)
  116. }
  117. def get_sanitizer_symbolize_command(json_path=None, executable_path=None):
  118. """Construct the command to invoke offline symbolization script."""
  119. script_path = os.path.join(
  120. ROOT_DIR, 'tools', 'valgrind', 'asan', 'asan_symbolize.py')
  121. cmd = [sys.executable, script_path]
  122. if json_path is not None:
  123. cmd.append('--test-summary-json-file=%s' % json_path)
  124. if executable_path is not None:
  125. cmd.append('--executable-path=%s' % executable_path)
  126. return cmd
  127. def get_json_path(cmd):
  128. """Extract the JSON test summary path from a command line."""
  129. json_path_flag = '--test-launcher-summary-output='
  130. for arg in cmd:
  131. if arg.startswith(json_path_flag):
  132. return arg.split(json_path_flag).pop()
  133. return None
  134. def symbolize_snippets_in_json(cmd, env):
  135. """Symbolize output snippets inside the JSON test summary."""
  136. json_path = get_json_path(cmd)
  137. if json_path is None:
  138. return
  139. try:
  140. symbolize_command = get_sanitizer_symbolize_command(
  141. json_path=json_path, executable_path=cmd[0])
  142. p = subprocess.Popen(symbolize_command, stderr=subprocess.PIPE, env=env)
  143. (_, stderr) = p.communicate()
  144. except OSError as e:
  145. print('Exception while symbolizing snippets: %s' % e, file=sys.stderr)
  146. raise
  147. if p.returncode != 0:
  148. print("Error: failed to symbolize snippets in JSON:\n", file=sys.stderr)
  149. print(stderr, file=sys.stderr)
  150. raise subprocess.CalledProcessError(p.returncode, symbolize_command)
  151. def run_command_with_output(argv, stdoutfile, env=None, cwd=None):
  152. """Run command and stream its stdout/stderr to the console & |stdoutfile|.
  153. Also forward_signals to obey
  154. https://chromium.googlesource.com/infra/luci/luci-py/+/main/appengine/swarming/doc/Bot.md#graceful-termination_aka-the-sigterm-and-sigkill-dance
  155. Returns:
  156. integer returncode of the subprocess.
  157. """
  158. print('Running %r in %r (env: %r)' % (argv, cwd, env), file=sys.stderr)
  159. assert stdoutfile
  160. with io.open(stdoutfile, 'wb') as writer, \
  161. io.open(stdoutfile, 'rb', 1) as reader:
  162. process = _popen(argv, env=env, cwd=cwd, stdout=writer,
  163. stderr=subprocess.STDOUT)
  164. forward_signals([process])
  165. while process.poll() is None:
  166. sys.stdout.write(reader.read().decode('utf-8'))
  167. # This sleep is needed for signal propagation. See the
  168. # wait_with_signals() docstring.
  169. time.sleep(0.1)
  170. # Read the remaining.
  171. sys.stdout.write(reader.read().decode('utf-8'))
  172. print('Command %r returned exit code %d' % (argv, process.returncode),
  173. file=sys.stderr)
  174. return process.returncode
  175. def run_command(argv, env=None, cwd=None, log=True):
  176. """Run command and stream its stdout/stderr both to stdout.
  177. Also forward_signals to obey
  178. https://chromium.googlesource.com/infra/luci/luci-py/+/main/appengine/swarming/doc/Bot.md#graceful-termination_aka-the-sigterm-and-sigkill-dance
  179. Returns:
  180. integer returncode of the subprocess.
  181. """
  182. if log:
  183. print('Running %r in %r (env: %r)' % (argv, cwd, env), file=sys.stderr)
  184. process = _popen(argv, env=env, cwd=cwd, stderr=subprocess.STDOUT)
  185. forward_signals([process])
  186. exit_code = wait_with_signals(process)
  187. if log:
  188. print('Command returned exit code %d' % exit_code, file=sys.stderr)
  189. return exit_code
  190. def run_command_output_to_handle(argv, file_handle, env=None, cwd=None):
  191. """Run command and stream its stdout/stderr both to |file_handle|.
  192. Also forward_signals to obey
  193. https://chromium.googlesource.com/infra/luci/luci-py/+/main/appengine/swarming/doc/Bot.md#graceful-termination_aka-the-sigterm-and-sigkill-dance
  194. Returns:
  195. integer returncode of the subprocess.
  196. """
  197. print('Running %r in %r (env: %r)' % (argv, cwd, env))
  198. process = _popen(
  199. argv, env=env, cwd=cwd, stderr=file_handle, stdout=file_handle)
  200. forward_signals([process])
  201. exit_code = wait_with_signals(process)
  202. print('Command returned exit code %d' % exit_code)
  203. return exit_code
  204. def wait_with_signals(process):
  205. """A version of process.wait() that works cross-platform.
  206. This version properly surfaces the SIGBREAK signal.
  207. From reading the subprocess.py source code, it seems we need to explicitly
  208. call time.sleep(). The reason is that subprocess.Popen.wait() on Windows
  209. directly calls WaitForSingleObject(), but only time.sleep() properly surface
  210. the SIGBREAK signal.
  211. Refs:
  212. https://github.com/python/cpython/blob/v2.7.15/Lib/subprocess.py#L692
  213. https://github.com/python/cpython/blob/v2.7.15/Modules/timemodule.c#L1084
  214. Returns:
  215. returncode of the process.
  216. """
  217. while process.poll() is None:
  218. time.sleep(0.1)
  219. return process.returncode
  220. def forward_signals(procs):
  221. """Forwards unix's SIGTERM or win's CTRL_BREAK_EVENT to the given processes.
  222. This plays nicely with swarming's timeout handling. See also
  223. https://chromium.googlesource.com/infra/luci/luci-py/+/main/appengine/swarming/doc/Bot.md#graceful-termination_aka-the-sigterm-and-sigkill-dance
  224. Args:
  225. procs: A list of subprocess.Popen objects representing child processes.
  226. """
  227. assert all(isinstance(p, subprocess.Popen) for p in procs)
  228. def _sig_handler(sig, _):
  229. for p in procs:
  230. if p.poll() is not None:
  231. continue
  232. # SIGBREAK is defined only for win32.
  233. # pylint: disable=no-member
  234. if sys.platform == 'win32' and sig == signal.SIGBREAK:
  235. p.send_signal(signal.CTRL_BREAK_EVENT)
  236. else:
  237. print("Forwarding signal(%d) to process %d" % (sig, p.pid))
  238. p.send_signal(sig)
  239. # pylint: enable=no-member
  240. if sys.platform == 'win32':
  241. signal.signal(signal.SIGBREAK, _sig_handler) # pylint: disable=no-member
  242. else:
  243. signal.signal(signal.SIGTERM, _sig_handler)
  244. signal.signal(signal.SIGINT, _sig_handler)
  245. def run_executable(cmd, env, stdoutfile=None):
  246. """Runs an executable with:
  247. - CHROME_HEADLESS set to indicate that the test is running on a
  248. bot and shouldn't do anything interactive like show modal dialogs.
  249. - environment variable CR_SOURCE_ROOT set to the root directory.
  250. - environment variable LANGUAGE to en_US.UTF-8.
  251. - environment variable CHROME_DEVEL_SANDBOX set
  252. - Reuses sys.executable automatically.
  253. """
  254. extra_env = {
  255. # Set to indicate that the executable is running non-interactively on
  256. # a bot.
  257. 'CHROME_HEADLESS': '1',
  258. # Many tests assume a English interface...
  259. 'LANG': 'en_US.UTF-8',
  260. }
  261. # Used by base/base_paths_linux.cc as an override. Just make sure the default
  262. # logic is used.
  263. env.pop('CR_SOURCE_ROOT', None)
  264. extra_env.update(get_sandbox_env(env))
  265. # Copy logic from tools/build/scripts/slave/runtest.py.
  266. asan = '--asan=1' in cmd
  267. lsan = '--lsan=1' in cmd
  268. msan = '--msan=1' in cmd
  269. tsan = '--tsan=1' in cmd
  270. cfi_diag = '--cfi-diag=1' in cmd
  271. if stdoutfile or sys.platform in ['win32', 'cygwin']:
  272. # Symbolization works in-process on Windows even when sandboxed.
  273. use_symbolization_script = False
  274. else:
  275. # If any sanitizer is enabled, we print unsymbolized stack trace
  276. # that is required to run through symbolization script.
  277. use_symbolization_script = (asan or msan or cfi_diag or lsan or tsan)
  278. if asan or lsan or msan or tsan or cfi_diag:
  279. extra_env.update(get_sanitizer_env(asan, lsan, msan, tsan, cfi_diag))
  280. if lsan or tsan:
  281. # LSan and TSan are not sandbox-friendly.
  282. cmd.append('--no-sandbox')
  283. # Enable clang code coverage continuous mode.
  284. if '--coverage-continuous-mode=1' in cmd:
  285. extra_env.update(get_coverage_continuous_mode_env(env))
  286. # pylint: disable=import-outside-toplevel
  287. if '--skip-set-lpac-acls=1' not in cmd and sys.platform == 'win32':
  288. sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)),
  289. 'scripts'))
  290. from scripts import common
  291. common.set_lpac_acls(ROOT_DIR, is_test_script=True)
  292. # pylint: enable=import-outside-toplevel
  293. cmd = trim_cmd(cmd)
  294. # Ensure paths are correctly separated on windows.
  295. cmd[0] = cmd[0].replace('/', os.path.sep)
  296. cmd = fix_python_path(cmd)
  297. # We also want to print the GTEST env vars that were set by the caller,
  298. # because you need them to reproduce the task properly.
  299. env_to_print = extra_env.copy()
  300. for env_var_name in ('GTEST_SHARD_INDEX', 'GTEST_TOTAL_SHARDS'):
  301. if env_var_name in env:
  302. env_to_print[env_var_name] = env[env_var_name]
  303. print('Additional test environment:\n%s\n'
  304. 'Command: %s\n' % (
  305. '\n'.join(' %s=%s' % (k, v)
  306. for k, v in sorted(env_to_print.items())),
  307. ' '.join(cmd)))
  308. sys.stdout.flush()
  309. env.update(extra_env or {})
  310. try:
  311. if stdoutfile:
  312. # Write to stdoutfile and poll to produce terminal output.
  313. return run_command_with_output(cmd, env=env, stdoutfile=stdoutfile)
  314. if use_symbolization_script:
  315. # See above comment regarding offline symbolization.
  316. # Need to pipe to the symbolizer script.
  317. p1 = _popen(cmd, env=env, stdout=subprocess.PIPE,
  318. stderr=sys.stdout)
  319. p2 = _popen(
  320. get_sanitizer_symbolize_command(executable_path=cmd[0]),
  321. env=env, stdin=p1.stdout)
  322. p1.stdout.close() # Allow p1 to receive a SIGPIPE if p2 exits.
  323. forward_signals([p1, p2])
  324. wait_with_signals(p1)
  325. wait_with_signals(p2)
  326. # Also feed the out-of-band JSON output to the symbolizer script.
  327. symbolize_snippets_in_json(cmd, env)
  328. return p1.returncode
  329. return run_command(cmd, env=env, log=False)
  330. except OSError:
  331. print('Failed to start %s' % cmd, file=sys.stderr)
  332. raise
  333. def _popen(*args, **kwargs):
  334. assert 'creationflags' not in kwargs
  335. if sys.platform == 'win32':
  336. # Necessary for signal handling. See crbug.com/733612#c6.
  337. kwargs['creationflags'] = subprocess.CREATE_NEW_PROCESS_GROUP
  338. return subprocess.Popen(*args, **kwargs)
  339. def main():
  340. return run_executable(sys.argv[1:], os.environ.copy())
  341. if __name__ == '__main__':
  342. sys.exit(main())