• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2# Copyright 2018 The Chromium Authors
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6from __future__ import print_function
7
8import copy
9import json
10import os
11import re
12import subprocess
13import sys
14
15# Add src/testing/ into sys.path for importing common without pylint errors.
16sys.path.append(
17    os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir)))
18from scripts import common
19
20# A list of filename regexes that are allowed to have static initializers.
21# If something adds a static initializer, revert it. We don't accept regressions
22# in static initializers.
23_LINUX_SI_ALLOWLIST = {
24    'chrome': [
25        # Only in coverage builds, not production.
26        'InstrProfilingRuntime\\.cpp : ' +
27        '_GLOBAL__sub_I_InstrProfilingRuntime\\.cpp',
28
29        # TODO(crbug.com/973554): Remove.
30        'iostream\\.cpp : _GLOBAL__I_000100',
31
32        # TODO(crbug.com/1445935): Rust stdlib argv handling.
33        # https://github.com/rust-lang/rust/blob/b08148f6a76010ea3d4e91d61245aa7aac59e4b4/library/std/src/sys/unix/args.rs#L107-L127
34        # https://github.com/rust-lang/rust/issues/111921
35        '.* : std::sys::unix::args::imp::ARGV_INIT_ARRAY::init_wrapper',
36
37        # Added by libgcc due to USE_EH_FRAME_REGISTRY.
38        'crtstuff\\.c : frame_dummy',
39    ],
40}
41
42# Mac can use this list when a dsym is available, otherwise it will fall back
43# to checking the count.
44_MAC_SI_FILE_ALLOWLIST = [
45    'InstrProfilingRuntime\\.cpp', # Only in coverage builds, not in production.
46    'sysinfo\\.cc', # Only in coverage builds, not in production.
47    'iostream\\.cpp', # Used to setup std::cin/cout/cerr.
48    '000100', # Used to setup std::cin/cout/cerr
49]
50
51# Two static initializers are needed on Mac for libc++ to set up
52# std::cin/cout/cerr before main() runs. Only iostream.cpp needs to be counted
53# here. Plus, PartitionAlloc-Everywhere uses one static initializer
54# (InitializeDefaultMallocZoneWithPartitionAlloc) to install a malloc zone.
55FALLBACK_EXPECTED_MAC_SI_COUNT = 3
56
57# Similar to mac, iOS needs the iosstream and PartitionAlloc-Everywhere static
58# initializer (InitializeDefaultMallocZoneWithPartitionAlloc) to install a
59# malloc zone.
60FALLBACK_EXPECTED_IOS_SI_COUNT = 2
61
62# For coverage builds, also allow 'IntrProfilingRuntime.cpp'
63COVERAGE_BUILD_FALLBACK_EXPECTED_MAC_SI_COUNT = 4
64
65
66# Returns true if args contains properties which look like a chromeos-esque
67# builder.
68def check_if_chromeos(args):
69  return 'buildername' in args.properties and \
70      'chromeos' in args.properties['buildername']
71
72def get_mod_init_count(executable, hermetic_xcode_path):
73  # Find the __DATA,__mod_init_func section.
74  if os.path.exists(hermetic_xcode_path):
75    otool_path = os.path.join(hermetic_xcode_path, 'Contents', 'Developer',
76        'Toolchains', 'XcodeDefault.xctoolchain', 'usr', 'bin', 'otool')
77  else:
78    otool_path = 'otool'
79
80  stdout = run_process([otool_path, '-l', executable])
81  section_index = stdout.find('sectname __mod_init_func')
82  if section_index == -1:
83    return 0
84
85  # If the section exists, the "size" line must follow it.
86  initializers_s = re.search('size 0x([0-9a-f]+)',
87                             stdout[section_index:]).group(1)
88  word_size = 8  # Assume 64 bit
89  return int(initializers_s, 16) / word_size
90
91def run_process(command):
92  p = subprocess.Popen(command, stdout=subprocess.PIPE, universal_newlines=True)
93  stdout = p.communicate()[0]
94  if p.returncode != 0:
95    raise Exception(
96        'ERROR from command "%s": %d' % (' '.join(command), p.returncode))
97  return stdout
98
99def main_ios(src_dir, hermetic_xcode_path):
100  base_names = ('Chromium', 'Chrome')
101  ret = 0
102  for base_name in base_names:
103    app_bundle = base_name + '.app'
104    chromium_executable = os.path.join(app_bundle, base_name)
105    if os.path.exists(chromium_executable):
106      si_count = get_mod_init_count(chromium_executable,
107                                    hermetic_xcode_path)
108      if si_count > 0:
109        allowed_si_count = FALLBACK_EXPECTED_IOS_SI_COUNT
110        if si_count > allowed_si_count:
111          print('Expected <= %d static initializers in %s, but found %d' %
112              (allowed_si_count, chromium_executable,
113              si_count))
114          ret = 1
115          show_mod_init_func = os.path.join(src_dir, 'tools', 'mac',
116                                            'show_mod_init_func.py')
117          args = [show_mod_init_func]
118          args.append(chromium_executable)
119
120          if os.path.exists(hermetic_xcode_path):
121            args.extend(['--xcode-path', hermetic_xcode_path])
122          stdout = run_process(args)
123          print(stdout)
124  return ret
125
126
127def main_mac(src_dir, hermetic_xcode_path, allow_coverage_initializer = False):
128  base_names = ('Chromium', 'Google Chrome')
129  ret = 0
130  for base_name in base_names:
131    app_bundle = base_name + '.app'
132    framework_name = base_name + ' Framework'
133    framework_bundle = framework_name + '.framework'
134    framework_dsym_bundle = framework_bundle + '.dSYM'
135    framework_unstripped_name = framework_name + '.unstripped'
136    chromium_executable = os.path.join(app_bundle, 'Contents', 'MacOS',
137                                       base_name)
138    chromium_framework_executable = os.path.join(framework_bundle,
139                                                 framework_name)
140    chromium_framework_dsym = os.path.join(framework_dsym_bundle, 'Contents',
141                                           'Resources', 'DWARF', framework_name)
142    if os.path.exists(chromium_executable):
143      # Count the number of files with at least one static initializer.
144      si_count = get_mod_init_count(chromium_framework_executable,
145                                    hermetic_xcode_path)
146
147      # Print the list of static initializers.
148      if si_count > 0:
149        # First look for a dSYM to get information about the initializers. If
150        # one is not present, check if there is an unstripped copy of the build
151        # output.
152        mac_tools_path = os.path.join(src_dir, 'tools', 'mac')
153        if os.path.exists(chromium_framework_dsym):
154          dump_static_initializers = os.path.join(
155              mac_tools_path, 'dump-static-initializers.py')
156          stdout = run_process(
157              [dump_static_initializers, chromium_framework_dsym])
158          for line in stdout:
159            if re.match('0x[0-9a-f]+', line) and not any(
160                re.match(f, line) for f in _MAC_SI_FILE_ALLOWLIST):
161              ret = 1
162              print('Found invalid static initializer: {}'.format(line))
163          print(stdout)
164        else:
165          allowed_si_count = FALLBACK_EXPECTED_MAC_SI_COUNT
166          if allow_coverage_initializer:
167            allowed_si_count = COVERAGE_BUILD_FALLBACK_EXPECTED_MAC_SI_COUNT
168          if si_count > allowed_si_count:
169            print('Expected <= %d static initializers in %s, but found %d' %
170                (allowed_si_count, chromium_framework_executable,
171                si_count))
172            ret = 1
173            show_mod_init_func = os.path.join(mac_tools_path,
174                                              'show_mod_init_func.py')
175            args = [show_mod_init_func]
176            if os.path.exists(framework_unstripped_name):
177              args.append(framework_unstripped_name)
178            else:
179              print('# Warning: Falling back to potentially stripped output.')
180              args.append(chromium_framework_executable)
181
182            if os.path.exists(hermetic_xcode_path):
183              args.extend(['--xcode-path', hermetic_xcode_path])
184
185            stdout = run_process(args)
186            print(stdout)
187  return ret
188
189
190def main_linux(src_dir):
191  ret = 0
192  allowlist = _LINUX_SI_ALLOWLIST
193  for binary_name in allowlist:
194    if not os.path.exists(binary_name):
195      continue
196
197    dump_static_initializers = os.path.join(src_dir, 'tools', 'linux',
198                                            'dump-static-initializers.py')
199    stdout = run_process([dump_static_initializers, '--json', binary_name])
200    entries = json.loads(stdout)['entries']
201
202    for e in entries:
203      # Get the basename and remove line number suffix.
204      basename = os.path.basename(e['filename']).split(':')[0]
205      symbol = e['symbol_name']
206      descriptor = f"{basename} : {symbol}"
207      if not any(re.match(p, descriptor) for p in allowlist[binary_name]):
208        ret = 1
209        print(('Error: file "%s" is not expected to have static initializers in'
210               ' binary "%s", but found "%s"') % (e['filename'], binary_name,
211                                                  e['symbol_name']))
212
213    print('\n# Static initializers in %s:' % binary_name)
214    for e in entries:
215      print('# 0x%x %s %s' % (e['address'], e['filename'], e['symbol_name']))
216      print(e['disassembly'])
217
218    print('Found %d files containing static initializers.' % len(entries))
219  return ret
220
221
222def main_run(args):
223  if args.build_config_fs != 'Release':
224    raise Exception('Only release builds are supported')
225
226  src_dir = args.paths['checkout']
227  build_dir = os.path.join(src_dir, 'out', args.build_config_fs)
228  os.chdir(build_dir)
229
230  if sys.platform.startswith('darwin'):
231    # If the checkout uses the hermetic xcode binaries, then otool must be
232    # directly invoked. The indirection via /usr/bin/otool won't work unless
233    # there's an actual system install of Xcode.
234    hermetic_xcode_path = os.path.join(src_dir, 'build', 'mac_files',
235        'xcode_binaries')
236
237    is_ios = 'target_platform' in args.properties and \
238      'ios' in args.properties['target_platform']
239    if is_ios:
240      rc = main_ios(src_dir, hermetic_xcode_path)
241    else:
242      rc = main_mac(src_dir, hermetic_xcode_path,
243        allow_coverage_initializer = '--allow-coverage-initializer' in \
244          args.args)
245  elif sys.platform.startswith('linux'):
246    # TODO(crbug.com/1492865): Delete this assert if it's not seen to fail
247    # anywhere.
248    assert not check_if_chromeos(args), (
249        "This script is no longer supported for CrOS")
250    rc = main_linux(src_dir)
251  else:
252    sys.stderr.write('Unsupported platform %s.\n' % repr(sys.platform))
253    return 2
254
255  common.record_local_script_results(
256      'check_static_initializers', args.output, [], rc == 0)
257
258  return rc
259
260
261def main_compile_targets(args):
262  if sys.platform.startswith('darwin'):
263    compile_targets = ['chrome']
264  elif sys.platform.startswith('linux'):
265    compile_targets = ['chrome']
266  else:
267    compile_targets = []
268
269  json.dump(compile_targets, args.output)
270
271  return 0
272
273
274if __name__ == '__main__':
275  funcs = {
276      'run': main_run,
277      'compile_targets': main_compile_targets,
278  }
279  sys.exit(common.run_script(sys.argv[1:], funcs))
280