1#!/usr/bin/env python3 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 6import json 7import os 8import re 9import subprocess 10import sys 11 12import common 13 14CHROMIUM_ROOT = os.path.join(os.path.dirname(__file__), os.pardir, os.pardir) 15BUILD_DIR = os.path.join(CHROMIUM_ROOT, 'build') 16 17if BUILD_DIR not in sys.path: 18 sys.path.insert(0, BUILD_DIR) 19import gn_helpers 20 21 22# A list of filename regexes that are allowed to have static initializers. 23# If something adds a static initializer, revert it. We don't accept regressions 24# in static initializers. 25_LINUX_SI_ALLOWLIST = { 26 'chrome': [ 27 # Only in coverage builds, not production. 28 'InstrProfilingRuntime\\.cpp : ' + 29 '_GLOBAL__sub_I_InstrProfilingRuntime\\.cpp', 30 31 # TODO(crbug.com/41464604): Remove. 32 'iostream\\.cpp : _GLOBAL__I_000100', 33 34 # TODO(crbug.com/40268361): Rust stdlib argv handling. 35 # https://github.com/rust-lang/rust/blob/b08148f6a76010ea3d4e91d61245aa7aac59e4b4/library/std/src/sys/unix/args.rs#L107-L127 36 # https://github.com/rust-lang/rust/issues/111921 37 '.* : std::sys::pal::unix::args::imp::ARGV_INIT_ARRAY::init_wrapper', 38 39 # Added by libgcc due to USE_EH_FRAME_REGISTRY. 40 'crtstuff\\.c : frame_dummy', 41 ], 42} 43 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 # Only in coverage builds, not in production. 48 'InstrProfilingRuntime\\.cpp', 49 'sysinfo\\.cc', # Only in coverage builds, not in production. 50 'iostream\\.cpp', # Used to setup std::cin/cout/cerr. 51 '000100', # Used to setup std::cin/cout/cerr 52] 53 54# The minimum for Mac is: 55# _GLOBAL__I_000100 56# InitializeDefaultMallocZoneWithPartitionAlloc() 57FALLBACK_MIN_MAC_SI_COUNT = 2 58 59# Two static initializers are needed on Mac for libc++ to set up 60# std::cin/cout/cerr before main() runs. Only iostream.cpp needs to be counted 61# here. Plus, PartitionAlloc-Everywhere uses one static initializer 62# (InitializeDefaultMallocZoneWithPartitionAlloc) to install a malloc zone. 63FALLBACK_EXPECTED_MAC_SI_COUNT = 3 64 65# Similar to mac, iOS needs the iosstream and PartitionAlloc-Everywhere static 66# initializer (InitializeDefaultMallocZoneWithPartitionAlloc) to install a 67# malloc zone. 68FALLBACK_EXPECTED_IOS_SI_COUNT = 2 69 70# For coverage builds, also allow 'IntrProfilingRuntime.cpp' 71COVERAGE_BUILD_FALLBACK_EXPECTED_MAC_SI_COUNT = 4 72 73 74# Returns true if args contains properties which look like a chromeos-esque 75# builder. 76def check_if_chromeos(args): 77 return 'buildername' in args.properties and \ 78 'chromeos' in args.properties['buildername'] 79 80 81def get_mod_init_count(src_dir, executable, hermetic_xcode_path): 82 show_mod_init_func = os.path.join(src_dir, 'tools', 'mac', 83 'show_mod_init_func.py') 84 args = [show_mod_init_func] 85 args.append(executable) 86 if os.path.exists(hermetic_xcode_path): 87 args.extend(['--xcode-path', hermetic_xcode_path]) 88 stdout = run_process(args) 89 si_count = len(stdout.splitlines()) - 1 # -1 for executable name 90 return (stdout, si_count) 91 92 93def run_process(command): 94 p = subprocess.Popen(command, stdout=subprocess.PIPE, universal_newlines=True) 95 stdout = p.communicate()[0] 96 if p.returncode != 0: 97 raise Exception('ERROR from command "%s": %d' % 98 (' '.join(command), p.returncode)) 99 return stdout 100 101 102def main_ios(src_dir, hermetic_xcode_path): 103 base_names = ('Chromium', 'Chrome') 104 ret = 0 105 for base_name in base_names: 106 app_bundle = base_name + '.app' 107 chromium_executable = os.path.join(app_bundle, base_name) 108 if os.path.exists(chromium_executable): 109 stdout, si_count = get_mod_init_count(src_dir, chromium_executable, 110 hermetic_xcode_path) 111 expected_si_count = FALLBACK_EXPECTED_IOS_SI_COUNT 112 if si_count != expected_si_count: 113 print('Expected %d static initializers in %s, but found %d' % 114 (expected_si_count, chromium_executable, si_count)) 115 print(stdout) 116 ret = 1 117 return ret 118 119 120def main_mac(src_dir, hermetic_xcode_path, allow_coverage_initializer=False): 121 base_names = ('Chromium', 'Google Chrome') 122 ret = 0 123 for base_name in base_names: 124 app_bundle = base_name + '.app' 125 framework_name = base_name + ' Framework' 126 framework_bundle = framework_name + '.framework' 127 chromium_executable = os.path.join(app_bundle, 'Contents', 'MacOS', 128 base_name) 129 chromium_framework_executable = os.path.join(framework_bundle, 130 framework_name) 131 if os.path.exists(chromium_executable): 132 # Count the number static initializers. 133 stdout, si_count = get_mod_init_count(src_dir, 134 chromium_framework_executable, 135 hermetic_xcode_path) 136 min_si_count = FALLBACK_MIN_MAC_SI_COUNT 137 allowed_si_count = FALLBACK_EXPECTED_MAC_SI_COUNT 138 if allow_coverage_initializer: 139 allowed_si_count = COVERAGE_BUILD_FALLBACK_EXPECTED_MAC_SI_COUNT 140 if si_count > allowed_si_count or si_count < min_si_count: 141 print('Expected [%d, %d] static initializers in %s, but found %d' % 142 (min_si_count, allowed_si_count, chromium_framework_executable, 143 si_count)) 144 print(stdout) 145 ret = 1 146 return ret 147 148 149def main_linux(src_dir): 150 ret = 0 151 allowlist = _LINUX_SI_ALLOWLIST 152 for binary_name in allowlist: 153 if not os.path.exists(binary_name): 154 continue 155 156 dump_static_initializers = os.path.join(src_dir, 'tools', 'linux', 157 'dump-static-initializers.py') 158 stdout = run_process([dump_static_initializers, '--json', binary_name]) 159 entries = json.loads(stdout)['entries'] 160 161 for e in entries: 162 # Get the basename and remove line number suffix. 163 basename = os.path.basename(e['filename']).split(':')[0] 164 symbol = e['symbol_name'] 165 descriptor = f'{basename} : {symbol}' 166 if not any(re.match(p, descriptor) for p in allowlist[binary_name]): 167 ret = 1 168 print(('Error: file "%s" is not expected to have static initializers in' 169 ' binary "%s", but found "%s"') % 170 (e['filename'], binary_name, e['symbol_name'])) 171 172 print('\n# Static initializers in %s:' % binary_name) 173 for e in entries: 174 print('# 0x%x %s %s' % (e['address'], e['filename'], e['symbol_name'])) 175 print(e['disassembly']) 176 177 print('Found %d files containing static initializers.' % len(entries)) 178 return ret 179 180 181def main_run(args): 182 with open(os.path.join(args.build_dir, 'args.gn')) as f: 183 gn_args = gn_helpers.FromGNArgs(f.read()) 184 if gn_args.get('is_debug') or gn_args.get('is_official_build'): 185 raise Exception('Only release builds are supported') 186 187 src_dir = args.paths['checkout'] 188 os.chdir(args.build_dir) 189 190 if sys.platform.startswith('darwin'): 191 # If the checkout uses the hermetic xcode binaries, then otool must be 192 # directly invoked. The indirection via /usr/bin/otool won't work unless 193 # there's an actual system install of Xcode. 194 hermetic_xcode_path = os.path.join(src_dir, 'build', 'mac_files', 195 'xcode_binaries') 196 197 is_ios = 'target_platform' in args.properties and \ 198 'ios' in args.properties['target_platform'] 199 if is_ios: 200 rc = main_ios(src_dir, hermetic_xcode_path) 201 else: 202 rc = main_mac(src_dir, hermetic_xcode_path, 203 allow_coverage_initializer = '--allow-coverage-initializer' in \ 204 args.args) 205 elif sys.platform.startswith('linux'): 206 # TODO(crbug.com/40285648): Delete this assert if it's not seen to fail 207 # anywhere. 208 assert not check_if_chromeos(args), ( 209 'This script is no longer supported for CrOS') 210 rc = main_linux(src_dir) 211 else: 212 sys.stderr.write('Unsupported platform %s.\n' % repr(sys.platform)) 213 return 2 214 215 common.record_local_script_results('check_static_initializers', args.output, 216 [], rc == 0) 217 218 return rc 219 220 221def main_compile_targets(args): 222 if sys.platform.startswith('darwin'): 223 if 'ios' in args.properties.get('target_platform', []): 224 compile_targets = ['ios/chrome/app:chrome'] 225 else: 226 compile_targets = ['chrome'] 227 elif sys.platform.startswith('linux'): 228 compile_targets = ['chrome'] 229 else: 230 compile_targets = [] 231 232 with open(args.output.name, 'w') as fd: 233 json.dump(compile_targets, fd) 234 235 return 0 236 237 238if __name__ == '__main__': 239 funcs = { 240 'run': main_run, 241 'compile_targets': main_compile_targets, 242 } 243 sys.exit(common.run_script(sys.argv[1:], funcs)) 244