From cf6fbe1fdcda6db2ed5d18a254b2c7c91f54f7aa Mon Sep 17 00:00:00 2001 From: Jason Simmons Date: Wed, 14 Aug 2019 12:40:40 -0700 Subject: [PATCH] Avoid dynamic lookups of the engine library's symbols on Android (flutter/engine#11001) The dynamic linker on some older versions of Android on x86 fails when doing dlsym(RTLD_DEFAULT) lookups of symbols exported by the engine library itself. The engine needs to do this for some data files that are linked into the engine library (ICU data and Dart snapshot blobs). To work around this, the engine will declare static symbols for these data objects on the affected platforms. Fixes https://github.com/flutter/flutter/issues/20091 --- engine/src/flutter/runtime/dart_snapshot.cc | 34 +++++++++++++-------- engine/src/flutter/shell/common/switches.cc | 19 ++++++++++++ 2 files changed, 41 insertions(+), 12 deletions(-) diff --git a/engine/src/flutter/runtime/dart_snapshot.cc b/engine/src/flutter/runtime/dart_snapshot.cc index ca4b75e467..8fe63c095f 100644 --- a/engine/src/flutter/runtime/dart_snapshot.cc +++ b/engine/src/flutter/runtime/dart_snapshot.cc @@ -20,6 +20,14 @@ const char* DartSnapshot::kIsolateDataSymbol = "kDartIsolateSnapshotData"; const char* DartSnapshot::kIsolateInstructionsSymbol = "kDartIsolateSnapshotInstructions"; +// On Windows and Android (in debug mode) the engine finds the Dart snapshot +// data through symbols that are statically linked into the executable. +// On other platforms this data is obtained by a dynamic symbol lookup. +#define DART_SNAPSHOT_STATIC_LINK \ + (OS_WIN || (OS_ANDROID && FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_DEBUG)) + +#if !DART_SNAPSHOT_STATIC_LINK + static std::unique_ptr GetFileMapping( const std::string& path, bool executable) { @@ -83,11 +91,13 @@ static std::shared_ptr SearchMapping( return nullptr; } +#endif // !DART_SNAPSHOT_STATIC_LINK + static std::shared_ptr ResolveVMData( const Settings& settings) { -#if OS_WIN +#if DART_SNAPSHOT_STATIC_LINK return std::make_unique(kDartVmSnapshotData, 0); -#else // OS_WIN +#else // DART_SNAPSHOT_STATIC_LINK return SearchMapping( settings.vm_snapshot_data, // embedder_mapping_callback settings.vm_snapshot_data_path, // file_path @@ -95,14 +105,14 @@ static std::shared_ptr ResolveVMData( DartSnapshot::kVMDataSymbol, // native_library_symbol_name false // is_executable ); -#endif // OS_WIN +#endif // DART_SNAPSHOT_STATIC_LINK } static std::shared_ptr ResolveVMInstructions( const Settings& settings) { -#if OS_WIN +#if DART_SNAPSHOT_STATIC_LINK return std::make_unique(kDartVmSnapshotInstructions, 0); -#else // OS_WIN +#else // DART_SNAPSHOT_STATIC_LINK return SearchMapping( settings.vm_snapshot_instr, // embedder_mapping_callback settings.vm_snapshot_instr_path, // file_path @@ -110,14 +120,14 @@ static std::shared_ptr ResolveVMInstructions( DartSnapshot::kVMInstructionsSymbol, // native_library_symbol_name true // is_executable ); -#endif // OS_WIN +#endif // DART_SNAPSHOT_STATIC_LINK } static std::shared_ptr ResolveIsolateData( const Settings& settings) { -#if OS_WIN +#if DART_SNAPSHOT_STATIC_LINK return std::make_unique(kDartIsolateSnapshotData, 0); -#else // OS_WIN +#else // DART_SNAPSHOT_STATIC_LINK return SearchMapping( settings.isolate_snapshot_data, // embedder_mapping_callback settings.isolate_snapshot_data_path, // file_path @@ -125,15 +135,15 @@ static std::shared_ptr ResolveIsolateData( DartSnapshot::kIsolateDataSymbol, // native_library_symbol_name false // is_executable ); -#endif // OS_WIN +#endif // DART_SNAPSHOT_STATIC_LINK } static std::shared_ptr ResolveIsolateInstructions( const Settings& settings) { -#if OS_WIN +#if DART_SNAPSHOT_STATIC_LINK return std::make_unique( kDartIsolateSnapshotInstructions, 0); -#else // OS_WIN +#else // DART_SNAPSHOT_STATIC_LINK return SearchMapping( settings.isolate_snapshot_instr, // embedder_mapping_callback settings.isolate_snapshot_instr_path, // file_path @@ -141,7 +151,7 @@ static std::shared_ptr ResolveIsolateInstructions( DartSnapshot::kIsolateInstructionsSymbol, // native_library_symbol_name true // is_executable ); -#endif // OS_WIN +#endif // DART_SNAPSHOT_STATIC_LINK } fml::RefPtr DartSnapshot::VMSnapshotFromSettings( diff --git a/engine/src/flutter/shell/common/switches.cc b/engine/src/flutter/shell/common/switches.cc index e7d892414c..dfea70a0ea 100644 --- a/engine/src/flutter/shell/common/switches.cc +++ b/engine/src/flutter/shell/common/switches.cc @@ -53,6 +53,20 @@ static const std::string gDartFlagsWhitelist[] = { // Include again for struct definition. #include "flutter/shell/common/switches.h" +// Define symbols for the ICU data that is linked into the Flutter library on +// Android. This is a workaround for crashes seen when doing dynamic lookups +// of the engine's own symbols on some older versions of Android. +#if OS_ANDROID +extern uint8_t _binary_icudtl_dat_start[]; +extern uint8_t _binary_icudtl_dat_end[]; + +static std::unique_ptr GetICUStaticMapping() { + return std::make_unique( + _binary_icudtl_dat_start, + _binary_icudtl_dat_end - _binary_icudtl_dat_start); +} +#endif + namespace flutter { void PrintUsage(const std::string& executable_name) { @@ -298,9 +312,14 @@ Settings SettingsFromCommandLine(const fml::CommandLine& command_line) { &icu_symbol_prefix); command_line.GetOptionValue(FlagForSwitch(Switch::ICUNativeLibPath), &native_lib_path); + +#if OS_ANDROID + settings.icu_mapper = GetICUStaticMapping; +#else settings.icu_mapper = [icu_symbol_prefix, native_lib_path] { return GetSymbolMapping(icu_symbol_prefix, native_lib_path); }; +#endif } }