From e95df2d3af7bd8e3d993a7d1c60d8beb92e5c59d Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Wed, 29 Jan 2020 15:49:29 -0800 Subject: [PATCH] =?UTF-8?q?In=20the=20test=20harness,=20don=E2=80=99t=20at?= =?UTF-8?q?tempt=20to=20load=20AOT=20snapshots=20from=20non-existent=20dyl?= =?UTF-8?q?ibs.=20(flutter/engine#16210)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This condition may arise when the embedder passes in a path to a non-existent dylib. Instead of crashing, the embedder will now return an error response. Fixes https://github.com/flutter/flutter/issues/49732 --- .../embedder/tests/embedder_test_context.cc | 88 ++++++++++++------- .../embedder/tests/embedder_test_context.h | 3 +- 2 files changed, 59 insertions(+), 32 deletions(-) diff --git a/engine/src/flutter/shell/platform/embedder/tests/embedder_test_context.cc b/engine/src/flutter/shell/platform/embedder/tests/embedder_test_context.cc index 24868a572a..4ac7c24316 100644 --- a/engine/src/flutter/shell/platform/embedder/tests/embedder_test_context.cc +++ b/engine/src/flutter/shell/platform/embedder/tests/embedder_test_context.cc @@ -5,6 +5,7 @@ #include "flutter/shell/platform/embedder/tests/embedder_test_context.h" #include "flutter/fml/make_copyable.h" +#include "flutter/fml/paths.h" #include "flutter/runtime/dart_vm.h" #include "flutter/shell/platform/embedder/tests/embedder_assertions.h" #include "third_party/dart/runtime/bin/elf_loader.h" @@ -13,40 +14,12 @@ namespace flutter { namespace testing { +static constexpr const char* kAppAOTELFFileName = "app_elf_snapshot.so"; + EmbedderTestContext::EmbedderTestContext(std::string assets_path) : assets_path_(std::move(assets_path)), native_resolver_(std::make_shared()) { - auto assets_dir = fml::OpenDirectory(assets_path_.c_str(), false, - fml::FilePermission::kRead); - - if (flutter::DartVM::IsRunningPrecompiledCode()) { - std::string filename(assets_path_); - filename += "/app_elf_snapshot.so"; - - const uint8_t *vm_snapshot_data = nullptr, - *vm_snapshot_instructions = nullptr, - *isolate_snapshot_data = nullptr, - *isolate_snapshot_instructions = nullptr; - const char* error = nullptr; - - elf_library_handle_ = - Dart_LoadELF(filename.c_str(), /*file_offset=*/0, &error, - &vm_snapshot_data, &vm_snapshot_instructions, - &isolate_snapshot_data, &isolate_snapshot_instructions); - - if (elf_library_handle_ != nullptr) { - vm_snapshot_data_.reset(new fml::NonOwnedMapping(vm_snapshot_data, 0)); - vm_snapshot_instructions_.reset( - new fml::NonOwnedMapping(vm_snapshot_instructions, 0)); - isolate_snapshot_data_.reset( - new fml::NonOwnedMapping(isolate_snapshot_data, 0)); - isolate_snapshot_instructions_.reset( - new fml::NonOwnedMapping(isolate_snapshot_instructions, 0)); - } else { - FML_LOG(WARNING) << "Could not load snapshot: " << error; - } - } - + SetupAOTMappingsIfNecessary(); isolate_create_callbacks_.push_back( [weak_resolver = std::weak_ptr{native_resolver_}]() { @@ -57,11 +30,64 @@ EmbedderTestContext::EmbedderTestContext(std::string assets_path) } EmbedderTestContext::~EmbedderTestContext() { + vm_snapshot_data_.reset(); + vm_snapshot_instructions_.reset(); + isolate_snapshot_data_.reset(); + isolate_snapshot_instructions_.reset(); if (elf_library_handle_ != nullptr) { Dart_UnloadELF(elf_library_handle_); } } +void EmbedderTestContext::SetupAOTMappingsIfNecessary() { + if (!DartVM::IsRunningPrecompiledCode()) { + // Not in AOT mode. Nothing to do. + return; + } + + auto elf_path = fml::paths::JoinPaths({assets_path_, kAppAOTELFFileName}); + + if (!fml::IsFile(elf_path.c_str())) { + FML_LOG(ERROR) << "Could not find the applications AOT ELF file in an " + "AOT environment. Engine launches attempted with this " + "context will fail. File not found: " + << elf_path; + return; + } + + const uint8_t* vm_snapshot_data = nullptr; + const uint8_t* vm_snapshot_instructions = nullptr; + const uint8_t* isolate_snapshot_data = nullptr; + const uint8_t* isolate_snapshot_instructions = nullptr; + + const char* error = nullptr; + + elf_library_handle_ = + Dart_LoadELF(elf_path.c_str(), // filename + 0u, // file offset + &error, // error (must not be freed) + &vm_snapshot_data, // VM snapshot data + &vm_snapshot_instructions, // VM snapshot instructions + &isolate_snapshot_data, // VM isolate data + &isolate_snapshot_instructions // VM isolate instructions + ); + + if (elf_library_handle_ == nullptr) { + FML_LOG(ERROR) << "Could not load snapshot in an AOT environment. Engine " + "launches attempted with this context will fail. Error: " + << error; + return; + } + + vm_snapshot_data_.reset(new fml::NonOwnedMapping(vm_snapshot_data, 0)); + vm_snapshot_instructions_.reset( + new fml::NonOwnedMapping(vm_snapshot_instructions, 0)); + isolate_snapshot_data_.reset( + new fml::NonOwnedMapping(isolate_snapshot_data, 0)); + isolate_snapshot_instructions_.reset( + new fml::NonOwnedMapping(isolate_snapshot_instructions, 0)); +} + const std::string& EmbedderTestContext::GetAssetsPath() const { return assets_path_; } diff --git a/engine/src/flutter/shell/platform/embedder/tests/embedder_test_context.h b/engine/src/flutter/shell/platform/embedder/tests/embedder_test_context.h index 00017fa872..eb5ae42c5a 100644 --- a/engine/src/flutter/shell/platform/embedder/tests/embedder_test_context.h +++ b/engine/src/flutter/shell/platform/embedder/tests/embedder_test_context.h @@ -81,7 +81,6 @@ class EmbedderTestContext { std::unique_ptr vm_snapshot_instructions_; std::unique_ptr isolate_snapshot_data_; std::unique_ptr isolate_snapshot_instructions_; - std::vector isolate_create_callbacks_; std::shared_ptr native_resolver_; SemanticsNodeCallback update_semantics_node_callback_; @@ -102,6 +101,8 @@ class EmbedderTestContext { static FlutterUpdateSemanticsCustomActionCallback GetUpdateSemanticsCustomActionCallbackHook(); + void SetupAOTMappingsIfNecessary(); + void SetupCompositor(); void FireIsolateCreateCallbacks();