verify_exported.dart 7.4 KB
Newer Older
1 2 3 4
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

V
vsmenon 已提交
5
// @dart = 2.6
6 7 8 9
import 'dart:convert';
import 'dart:io';

import 'package:path/path.dart' as p;
10
import 'package:collection/collection.dart' show MapEquality;
11 12 13 14 15 16 17 18 19 20 21

// This script verifies that the release binaries only export the expected
// symbols.
//
// Android binaries (libflutter.so) should only export one symbol "JNI_OnLoad"
// of type "T".
//
// iOS binaries (Flutter.framework/Flutter) should only export Objective-C
// Symbols from the Flutter namespace. These are either of type
// "(__DATA,__common)" or "(__DATA,__objc_data)".

22 23 24 25 26
/// Takes the path to the out directory as the first argument, and the path to
/// the buildtools directory as the second argument.
///
/// If the second argument is not specified, it is assumed that it is the parent
/// of the out directory (for backwards compatibility).
27
void main(List<String> arguments) {
28
  assert(arguments.length == 2 || arguments.length == 1);
29
  final String outPath = arguments.first;
30 31 32 33
  final String buildToolsPath = arguments.length == 1
      ? p.join(p.dirname(outPath), 'buildtools')
      : arguments[1];

34 35 36 37 38 39
  String platform;
  if (Platform.isLinux) {
    platform = 'linux-x64';
  } else if (Platform.isMacOS) {
    platform = 'mac-x64';
  } else {
D
Dan Field 已提交
40
    throw UnimplementedError('Script only support running on Linux or MacOS.');
41
  }
42
  final String nmPath = p.join(buildToolsPath, platform, 'clang', 'bin', 'llvm-nm');
43
  assert(Directory(outPath).existsSync());
44

D
Dan Field 已提交
45
  final Iterable<String> releaseBuilds = Directory(outPath).listSync()
46
      .where((FileSystemEntity entity) => entity is Directory)
47
      .map<String>((FileSystemEntity dir) => p.basename(dir.path))
48 49 50 51 52 53 54 55
      .where((String s) => s.contains('_release'));

  final Iterable<String> iosReleaseBuilds = releaseBuilds
      .where((String s) => s.startsWith('ios_'));
  final Iterable<String> androidReleaseBuilds = releaseBuilds
      .where((String s) => s.startsWith('android_'));

  int failures = 0;
56 57
  failures += _checkIos(outPath, nmPath, iosReleaseBuilds);
  failures += _checkAndroid(outPath, nmPath, androidReleaseBuilds);
58 59
  print('Failing checks: $failures');
  exit(failures);
60 61
}

62
int _checkIos(String outPath, String nmPath, Iterable<String> builds) {
63 64 65
  int failures = 0;
  for (String build in builds) {
    final String libFlutter = p.join(outPath, build, 'Flutter.framework', 'Flutter');
66
    if (!File(libFlutter).existsSync()) {
67 68 69
      print('SKIPPING: $libFlutter does not exist.');
      continue;
    }
70 71 72 73 74 75 76
    final ProcessResult nmResult = Process.runSync(nmPath, <String>['-gUm', libFlutter]);
    if (nmResult.exitCode != 0) {
      print('ERROR: failed to execute "nm -gUm $libFlutter":\n${nmResult.stderr}');
      failures++;
      continue;
    }
    final Iterable<NmEntry> unexpectedEntries = NmEntry.parse(nmResult.stdout).where((NmEntry entry) {
77
      return !(((entry.type == '(__DATA,__common)' || entry.type == '(__DATA,__const)') && entry.name.startsWith('_Flutter'))
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
          || (entry.type == '(__DATA,__objc_data)'
              && (entry.name.startsWith('_OBJC_METACLASS_\$_Flutter') || entry.name.startsWith('_OBJC_CLASS_\$_Flutter'))));
    });
    if (unexpectedEntries.isNotEmpty) {
      print('ERROR: $libFlutter exports unexpected symbols:');
      print(unexpectedEntries.fold<String>('', (String previous, NmEntry entry) {
        return '${previous == '' ? '' : '$previous\n'}     ${entry.type} ${entry.name}';
      }));
      failures++;
    } else {
      print('OK: $libFlutter');
    }
  }
  return failures;
}

94
int _checkAndroid(String outPath, String nmPath, Iterable<String> builds) {
95 96 97
  int failures = 0;
  for (String build in builds) {
    final String libFlutter = p.join(outPath, build, 'libflutter.so');
98
    if (!File(libFlutter).existsSync()) {
99 100 101
      print('SKIPPING: $libFlutter does not exist.');
      continue;
    }
102 103 104 105 106 107 108
    final ProcessResult nmResult = Process.runSync(nmPath, <String>['-gU', libFlutter]);
    if (nmResult.exitCode != 0) {
      print('ERROR: failed to execute "nm -gU $libFlutter":\n${nmResult.stderr}');
      failures++;
      continue;
    }
    final Iterable<NmEntry> entries = NmEntry.parse(nmResult.stdout);
D
Dan Field 已提交
109
    final Map<String, String> entryMap = Map<String, String>.fromIterable(
110
        entries,
D
Dan Field 已提交
111 112 113
        key: (dynamic entry) => entry.name,
        value: (dynamic entry) => entry.type);
    final Map<String, String> expectedSymbols = <String, String>{
114 115 116
      'JNI_OnLoad': 'T',
      '_binary_icudtl_dat_size': 'A',
      '_binary_icudtl_dat_start': 'D',
D
Dan Field 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
      // TODO(fxb/47943): Remove these once Clang lld does not expose them.
      // arm
      '__adddf3': 'T',
      '__addsf3': 'T',
      '__aeabi_cdcmpeq': 'T',
      '__aeabi_cdcmple': 'T',
      '__aeabi_cdrcmple': 'T',
      '__aeabi_d2lz': 'T',
      '__aeabi_d2uiz': 'T',
      '__aeabi_d2ulz': 'T',
      '__aeabi_dadd': 'T',
      '__aeabi_dcmpeq': 'T',
      '__aeabi_dcmpge': 'T',
      '__aeabi_dcmpgt': 'T',
      '__aeabi_dcmple': 'T',
      '__aeabi_dcmplt': 'T',
      '__aeabi_ddiv': 'T',
      '__aeabi_dmul': 'T',
      '__aeabi_drsub': 'T',
      '__aeabi_dsub': 'T',
      '__aeabi_f2d': 'T',
      '__aeabi_fadd': 'T',
      '__aeabi_frsub': 'T',
      '__aeabi_fsub': 'T',
      '__aeabi_i2d': 'T',
      '__aeabi_i2f': 'T',
      '__aeabi_l2d': 'T',
      '__aeabi_l2f': 'T',
      '__aeabi_lasr': 'T',
      '__aeabi_ldivmod': 'T',
      '__aeabi_llsl': 'T',
      '__aeabi_llsr': 'T',
      '__aeabi_ui2d': 'T',
      '__aeabi_ui2f': 'T',
      '__aeabi_uidiv': 'T',
      '__aeabi_uidivmod': 'T',
      '__aeabi_ul2d': 'T',
      '__aeabi_ul2f': 'T',
      '__aeabi_uldivmod': 'T',
      '__ashldi3': 'T',
      '__ashrdi3': 'T',
      '__cmpdf2': 'T',
      '__divdf3': 'T',
      '__divdi3': 'T',
      '__eqdf2': 'T',
      '__extendsfdf2': 'T',
      '__fixdfdi': 'T',
      '__fixunsdfdi': 'T',
      '__fixunsdfsi': 'T',
      '__floatdidf': 'T',
      '__floatdisf': 'T',
      '__floatsidf': 'T',
      '__floatsisf': 'T',
      '__floatundidf': 'T',
      '__floatundisf': 'T',
      '__floatunsidf': 'T',
      '__floatunsisf': 'T',
      '__gedf2': 'T',
      '__gnu_ldivmod_helper': 'T',
      '__gnu_uldivmod_helper': 'T',
      '__gtdf2': 'T',
      '__ledf2': 'T',
      '__lshrdi3': 'T',
      '__ltdf2': 'T',
      '__muldf3': 'T',
      '__nedf2': 'T',
      '__subdf3': 'T',
      '__subsf3': 'T',
      '__udivdi3': 'T',
      '__udivsi3': 'T',
      // arm64
      '__clz_tab': 'R',
      '__udivti3': 'T',
      // arm64 && x64
      '__emutls_get_address': 'T',
      '__emutls_register_common': 'T',
      // jit x86
      '__moddi3': 'T',
      '__umoddi3': 'T',
196
    };
D
Dan Field 已提交
197 198 199 200 201 202 203
    final Map<String, String> badSymbols = <String, String>{};
    for (final String key in entryMap.keys) {
      if (entryMap[key] != expectedSymbols[key]) {
        badSymbols[key] = entryMap[key];
      }
    }
    if (badSymbols.isNotEmpty) {
204 205 206
      print('ERROR: $libFlutter exports the wrong symbols');
      print(' Expected $expectedSymbols');
      print(' Library has $entryMap.');
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
      failures++;
    } else {
      print('OK: $libFlutter');
    }
  }
  return failures;
}

class NmEntry {
  NmEntry._(this.address, this.type, this.name);

  final String address;
  final String type;
  final String name;

  static Iterable<NmEntry> parse(String stdout) {
    return LineSplitter.split(stdout).map((String line) {
      final List<String> parts = line.split(' ');
D
Dan Field 已提交
225
      return NmEntry._(parts[0], parts[1], parts.last);
226 227 228
    });
  }
}