check_static_initializers.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. #!/usr/bin/env python
  2. # Copyright 2018 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. from __future__ import print_function
  6. import json
  7. import os
  8. import re
  9. import subprocess
  10. import sys
  11. # Add src/testing/ into sys.path for importing common without pylint errors.
  12. sys.path.append(
  13. os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir)))
  14. from scripts import common
  15. # A list of files that are allowed to have static initializers.
  16. # If something adds a static initializer, revert it. We don't accept regressions
  17. # in static initializers.
  18. _LINUX_SI_FILE_ALLOWLIST = {
  19. 'chrome': [
  20. 'InstrProfilingRuntime.cpp', # Only in coverage builds, not production.
  21. 'atomicops_internals_x86.cc', # TODO(crbug.com/973551): Remove.
  22. 'iostream.cpp:', # TODO(crbug.com/973554): Remove.
  23. '000100', # libc++ uses init_priority 100 for iostreams.
  24. 'spinlock.cc', # TODO(crbug.com/973556): Remove.
  25. ],
  26. 'nacl_helper_bootstrap': [],
  27. }
  28. _LINUX_SI_FILE_ALLOWLIST['nacl_helper'] = _LINUX_SI_FILE_ALLOWLIST['chrome']
  29. # The lists for Chrome OS are conceptually the same as the Linux ones above.
  30. # If something adds a static initializer, revert it. We don't accept regressions
  31. # in static initializers.
  32. _CROS_SI_FILE_ALLOWLIST = {
  33. 'chrome': [
  34. 'InstrProfilingRuntime.cpp', # Only in coverage builds, not production.
  35. 'atomicops_internals_x86.cc', # TODO(crbug.com/973551): Remove.
  36. 'iostream.cpp:', # TODO(crbug.com/973554): Remove.
  37. '000100', # libc++ uses init_priority 100 for iostreams.
  38. 'spinlock.cc', # TODO(crbug.com/973556): Remove.
  39. 'rpc.pb.cc', # TODO(crbug.com/537099): Remove.
  40. ],
  41. 'nacl_helper_bootstrap': [],
  42. }
  43. _CROS_SI_FILE_ALLOWLIST['nacl_helper'] = _LINUX_SI_FILE_ALLOWLIST['chrome']
  44. # Mac can use this list when a dsym is available, otherwise it will fall back
  45. # to checking the count.
  46. _MAC_SI_FILE_ALLOWLIST = [
  47. 'InstrProfilingRuntime.cpp', # Only in coverage builds, not in production.
  48. 'sysinfo.cc', # Only in coverage builds, not in production.
  49. 'iostream.cpp', # Used to setup std::cin/cout/cerr.
  50. '000100', # Used to setup std::cin/cout/cerr
  51. ]
  52. # Two static initializers are needed on Mac for libc++ to set up
  53. # std::cin/cout/cerr before main() runs. Only iostream.cpp needs to be counted
  54. # here. Plus, PartitionAlloc-Everywhere uses one static initializer
  55. # (InitializeDefaultMallocZoneWithPartitionAlloc) to install a malloc zone.
  56. FALLBACK_EXPECTED_MAC_SI_COUNT = 3
  57. # For coverage builds, also allow 'IntrProfilingRuntime.cpp'
  58. COVERAGE_BUILD_FALLBACK_EXPECTED_MAC_SI_COUNT = 4
  59. def run_process(command):
  60. p = subprocess.Popen(command, stdout=subprocess.PIPE, universal_newlines=True)
  61. stdout = p.communicate()[0]
  62. if p.returncode != 0:
  63. raise Exception(
  64. 'ERROR from command "%s": %d' % (' '.join(command), p.returncode))
  65. return stdout
  66. def main_mac(src_dir, allow_coverage_initializer = False):
  67. base_names = ('Chromium', 'Google Chrome')
  68. ret = 0
  69. for base_name in base_names:
  70. app_bundle = base_name + '.app'
  71. framework_name = base_name + ' Framework'
  72. framework_bundle = framework_name + '.framework'
  73. framework_dsym_bundle = framework_bundle + '.dSYM'
  74. framework_unstripped_name = framework_name + '.unstripped'
  75. chromium_executable = os.path.join(app_bundle, 'Contents', 'MacOS',
  76. base_name)
  77. chromium_framework_executable = os.path.join(framework_bundle,
  78. framework_name)
  79. chromium_framework_dsym = os.path.join(framework_dsym_bundle, 'Contents',
  80. 'Resources', 'DWARF', framework_name)
  81. if os.path.exists(chromium_executable):
  82. # Count the number of files with at least one static initializer.
  83. si_count = 0
  84. # Find the __DATA,__mod_init_func section.
  85. # If the checkout uses the hermetic xcode binaries, then otool must be
  86. # directly invoked. The indirection via /usr/bin/otool won't work unless
  87. # there's an actual system install of Xcode.
  88. hermetic_xcode_path = os.path.join(src_dir, 'build', 'mac_files',
  89. 'xcode_binaries')
  90. if os.path.exists(hermetic_xcode_path):
  91. otool_path = os.path.join(hermetic_xcode_path, 'Contents', 'Developer',
  92. 'Toolchains', 'XcodeDefault.xctoolchain', 'usr', 'bin', 'otool')
  93. else:
  94. otool_path = 'otool'
  95. stdout = run_process([otool_path, '-l', chromium_framework_executable])
  96. section_index = stdout.find('sectname __mod_init_func')
  97. if section_index != -1:
  98. # If the section exists, the "size" line must follow it.
  99. initializers_s = re.search('size 0x([0-9a-f]+)',
  100. stdout[section_index:]).group(1)
  101. word_size = 8 # Assume 64 bit
  102. si_count = int(initializers_s, 16) / word_size
  103. # Print the list of static initializers.
  104. if si_count > 0:
  105. # First look for a dSYM to get information about the initializers. If
  106. # one is not present, check if there is an unstripped copy of the build
  107. # output.
  108. mac_tools_path = os.path.join(src_dir, 'tools', 'mac')
  109. if os.path.exists(chromium_framework_dsym):
  110. dump_static_initializers = os.path.join(
  111. mac_tools_path, 'dump-static-initializers.py')
  112. stdout = run_process(
  113. [dump_static_initializers, chromium_framework_dsym])
  114. for line in stdout:
  115. if re.match('0x[0-9a-f]+', line) and not any(
  116. f in line for f in _MAC_SI_FILE_ALLOWLIST):
  117. ret = 1
  118. print('Found invalid static initializer: {}'.format(line))
  119. print(stdout)
  120. else:
  121. allowed_si_count = FALLBACK_EXPECTED_MAC_SI_COUNT
  122. if allow_coverage_initializer:
  123. allowed_si_count = COVERAGE_BUILD_FALLBACK_EXPECTED_MAC_SI_COUNT
  124. if si_count > allowed_si_count:
  125. print('Expected <= %d static initializers in %s, but found %d' %
  126. (allowed_si_count, chromium_framework_executable,
  127. si_count))
  128. ret = 1
  129. show_mod_init_func = os.path.join(mac_tools_path,
  130. 'show_mod_init_func.py')
  131. args = [show_mod_init_func]
  132. if os.path.exists(framework_unstripped_name):
  133. args.append(framework_unstripped_name)
  134. else:
  135. print('# Warning: Falling back to potentially stripped output.')
  136. args.append(chromium_framework_executable)
  137. if os.path.exists(hermetic_xcode_path):
  138. args.extend(['--xcode-path', hermetic_xcode_path])
  139. stdout = run_process(args)
  140. print(stdout)
  141. return ret
  142. def main_linux(src_dir, is_chromeos):
  143. ret = 0
  144. allowlist = _CROS_SI_FILE_ALLOWLIST if is_chromeos else \
  145. _LINUX_SI_FILE_ALLOWLIST
  146. for binary_name in allowlist:
  147. if not os.path.exists(binary_name):
  148. continue
  149. dump_static_initializers = os.path.join(src_dir, 'tools', 'linux',
  150. 'dump-static-initializers.py')
  151. stdout = run_process([dump_static_initializers, '-d', binary_name])
  152. # The output has the following format:
  153. # First lines: '# <file_name> <si_name>'
  154. # Last line: '# Found <num> static initializers in <num> files.'
  155. #
  156. # For example:
  157. # # spinlock.cc GetSystemCPUsCount()
  158. # # spinlock.cc adaptive_spin_count
  159. # # Found 2 static initializers in 1 files.
  160. files_with_si = set()
  161. for line in stdout.splitlines()[:-1]:
  162. parts = line.split(' ', 2)
  163. assert len(parts) == 3 and parts[0] == '#'
  164. files_with_si.add(parts[1])
  165. for f in files_with_si:
  166. if f not in allowlist[binary_name]:
  167. ret = 1
  168. print(('Error: file "%s" is not expected to have static initializers in'
  169. ' binary "%s"') % (f, binary_name))
  170. print('\n# Static initializers in %s:' % binary_name)
  171. print(stdout)
  172. return ret
  173. def main_run(args):
  174. if args.build_config_fs != 'Release':
  175. raise Exception('Only release builds are supported')
  176. src_dir = args.paths['checkout']
  177. build_dir = os.path.join(src_dir, 'out', args.build_config_fs)
  178. os.chdir(build_dir)
  179. if sys.platform.startswith('darwin'):
  180. rc = main_mac(src_dir,
  181. allow_coverage_initializer = '--allow-coverage-initializer' in args.args)
  182. elif sys.platform.startswith('linux'):
  183. is_chromeos = 'buildername' in args.properties and \
  184. 'chromeos' in args.properties['buildername']
  185. rc = main_linux(src_dir, is_chromeos)
  186. else:
  187. sys.stderr.write('Unsupported platform %s.\n' % repr(sys.platform))
  188. return 2
  189. common.record_local_script_results(
  190. 'check_static_initializers', args.output, [], rc == 0)
  191. return rc
  192. def main_compile_targets(args):
  193. if sys.platform.startswith('darwin'):
  194. compile_targets = ['chrome']
  195. elif sys.platform.startswith('linux'):
  196. compile_targets = ['chrome', 'nacl_helper', 'nacl_helper_bootstrap']
  197. else:
  198. compile_targets = []
  199. json.dump(compile_targets, args.output)
  200. return 0
  201. if __name__ == '__main__':
  202. funcs = {
  203. 'run': main_run,
  204. 'compile_targets': main_compile_targets,
  205. }
  206. sys.exit(common.run_script(sys.argv[1:], funcs))