From fdd399f16eb54786677f8ff6a5a8c005179202bc Mon Sep 17 00:00:00 2001 From: Michael Goderbauer Date: Fri, 28 Sep 2018 00:23:01 +0200 Subject: [PATCH] Script to verify exported symbols on release binaries (flutter/engine#6363) --- engine/src/flutter/.gitignore | 1 + .../src/flutter/testing/symbols/pubspec.yaml | 3 + .../testing/symbols/verify_exported.dart | 99 +++++++++++++++++++ 3 files changed, 103 insertions(+) create mode 100644 engine/src/flutter/testing/symbols/pubspec.yaml create mode 100644 engine/src/flutter/testing/symbols/verify_exported.dart diff --git a/engine/src/flutter/.gitignore b/engine/src/flutter/.gitignore index 2e1362f0aa..730c7e4ce1 100644 --- a/engine/src/flutter/.gitignore +++ b/engine/src/flutter/.gitignore @@ -19,3 +19,4 @@ Session.vim tags Thumbs.db .idea +pubspec.lock diff --git a/engine/src/flutter/testing/symbols/pubspec.yaml b/engine/src/flutter/testing/symbols/pubspec.yaml new file mode 100644 index 0000000000..cfea098886 --- /dev/null +++ b/engine/src/flutter/testing/symbols/pubspec.yaml @@ -0,0 +1,3 @@ +name: verify_exported +dependencies: + path: 1.6.2 diff --git a/engine/src/flutter/testing/symbols/verify_exported.dart b/engine/src/flutter/testing/symbols/verify_exported.dart new file mode 100644 index 0000000000..da80ff4db2 --- /dev/null +++ b/engine/src/flutter/testing/symbols/verify_exported.dart @@ -0,0 +1,99 @@ +import 'dart:convert'; +import 'dart:io'; + +import 'package:path/path.dart' as p; + +// 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)". + +/// Takes the path to the out directory as argument. +void main(List arguments) { + assert(arguments.length == 1); + final String outPath = arguments.first; + assert(new Directory(outPath).existsSync()); + + final Iterable releaseBuilds = new Directory(outPath).listSync() + .where((FileSystemEntity entity) => entity is Directory) + .map((FileSystemEntity dir) => p.basename(dir.path)) + .where((String s) => s.contains('_release')); + + final Iterable iosReleaseBuilds = releaseBuilds + .where((String s) => s.startsWith('ios_')); + final Iterable androidReleaseBuilds = releaseBuilds + .where((String s) => s.startsWith('android_')); + + int failures = 0; + failures += _checkIos(outPath, iosReleaseBuilds); + failures += _checkAndroid(outPath, androidReleaseBuilds); + + exit(failures); +} + +int _checkIos(String outPath, Iterable builds) { + int failures = 0; + for (String build in builds) { + final String libFlutter = p.join(outPath, build, 'Flutter.framework', 'Flutter'); + final String stdout = Process.runSync('nm', ['-gUm', libFlutter]).stdout; + final Iterable unexpectedEntries = NmEntry.parse(stdout).where((NmEntry entry) { + return !((entry.type == '(__DATA,__common)' && entry.name.startsWith('_Flutter')) + || (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 previous, NmEntry entry) { + return '${previous == '' ? '' : '$previous\n'} ${entry.type} ${entry.name}'; + })); + failures++; + } else { + print('OK: $libFlutter'); + } + } + return failures; +} + +int _checkAndroid(String outPath, Iterable builds) { + int failures = 0; + for (String build in builds) { + final String libFlutter = p.join(outPath, build, 'libflutter.so'); + final String stdout = Process.runSync('nm', ['-gU', libFlutter]).stdout; + final Iterable entries = NmEntry.parse(stdout); + if (entries.isEmpty) { + print('ERROR: $libFlutter exports no symbol'); + print(' Expected exactly one symbol "JNI_OnLoad" of type "T", but got none.'); + failures++; + } else if (entries.length > 1 || entries.first.type != 'T' || entries.first.name != 'JNI_OnLoad') { + print('ERROR: $libFlutter exports unexpected symbols.'); + print(' Expected exactly one symbol "JNI_OnLoad" of type "T", but got instead:'); + print(entries.fold('', (String previous, NmEntry entry) { + return '${previous == '' ? '' : '$previous\n'} ${entry.type} ${entry.name}'; + })); + 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 parse(String stdout) { + return LineSplitter.split(stdout).map((String line) { + final List parts = line.split(' '); + return new NmEntry._(parts[0], parts[1], parts.last); + }); + } +}