| 1 | // Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "flutter/testing/elf_loader.h" |
| 6 | |
| 7 | #include <utility> |
| 8 | |
| 9 | #include "flutter/fml/file.h" |
| 10 | #include "flutter/fml/paths.h" |
| 11 | #include "flutter/runtime/dart_vm.h" |
| 12 | #include "flutter/testing/testing.h" |
| 13 | |
| 14 | namespace flutter { |
| 15 | namespace testing { |
| 16 | |
| 17 | ELFAOTSymbols LoadELFSymbolFromFixturesIfNeccessary(std::string elf_filename) { |
| 18 | if (!DartVM::IsRunningPrecompiledCode()) { |
| 19 | return {}; |
| 20 | } |
| 21 | |
| 22 | const auto elf_path = |
| 23 | fml::paths::JoinPaths(components: {GetFixturesPath(), std::move(elf_filename)}); |
| 24 | |
| 25 | if (!fml::IsFile(path: elf_path)) { |
| 26 | FML_LOG(ERROR) << "App AOT file does not exist for this fixture. Attempts " |
| 27 | "to launch the Dart VM with these AOT symbols will fail." ; |
| 28 | return {}; |
| 29 | } |
| 30 | |
| 31 | ELFAOTSymbols symbols; |
| 32 | |
| 33 | // Must not be freed. |
| 34 | const char* error = nullptr; |
| 35 | |
| 36 | #if OS_FUCHSIA |
| 37 | // TODO(gw280): https://github.com/flutter/flutter/issues/50285 |
| 38 | // Dart doesn't implement Dart_LoadELF on Fuchsia |
| 39 | auto loaded_elf = nullptr; |
| 40 | #else |
| 41 | auto loaded_elf = |
| 42 | Dart_LoadELF(filename: elf_path.c_str(), // file path |
| 43 | file_offset: 0, // file offset |
| 44 | error: &error, // error (out) |
| 45 | vm_snapshot_data: &symbols.vm_snapshot_data, // vm snapshot data (out) |
| 46 | vm_snapshot_instrs: &symbols.vm_snapshot_instrs, // vm snapshot instrs (out) |
| 47 | vm_isolate_data: &symbols.vm_isolate_data, // vm isolate data (out) |
| 48 | vm_isolate_instrs: &symbols.vm_isolate_instrs // vm isolate instr (out) |
| 49 | ); |
| 50 | #endif |
| 51 | |
| 52 | if (loaded_elf == nullptr) { |
| 53 | FML_LOG(ERROR) |
| 54 | << "Could not fetch AOT symbols from loaded ELF. Attempts " |
| 55 | "to launch the Dart VM with these AOT symbols will fail. Error: " |
| 56 | << error; |
| 57 | return {}; |
| 58 | } |
| 59 | |
| 60 | symbols.loaded_elf.reset(p: loaded_elf); |
| 61 | |
| 62 | return symbols; |
| 63 | } |
| 64 | |
| 65 | ELFAOTSymbols LoadELFSplitSymbolFromFixturesIfNeccessary( |
| 66 | std::string elf_split_filename) { |
| 67 | if (!DartVM::IsRunningPrecompiledCode()) { |
| 68 | return {}; |
| 69 | } |
| 70 | |
| 71 | const auto elf_path = |
| 72 | fml::paths::JoinPaths(components: {GetFixturesPath(), std::move(elf_split_filename)}); |
| 73 | |
| 74 | if (!fml::IsFile(path: elf_path)) { |
| 75 | // We do not log here, as there is no expectation for a split library to |
| 76 | // exist. |
| 77 | return {}; |
| 78 | } |
| 79 | |
| 80 | ELFAOTSymbols symbols; |
| 81 | |
| 82 | // Must not be freed. |
| 83 | const char* error = nullptr; |
| 84 | |
| 85 | #if OS_FUCHSIA |
| 86 | // TODO(gw280): https://github.com/flutter/flutter/issues/50285 |
| 87 | // Dart doesn't implement Dart_LoadELF on Fuchsia |
| 88 | auto loaded_elf = nullptr; |
| 89 | #else |
| 90 | auto loaded_elf = |
| 91 | Dart_LoadELF(filename: elf_path.c_str(), // file path |
| 92 | file_offset: 0, // file offset |
| 93 | error: &error, // error (out) |
| 94 | vm_snapshot_data: &symbols.vm_snapshot_data, // vm snapshot data (out) |
| 95 | vm_snapshot_instrs: &symbols.vm_snapshot_instrs, // vm snapshot instrs (out) |
| 96 | vm_isolate_data: &symbols.vm_isolate_data, // vm isolate data (out) |
| 97 | vm_isolate_instrs: &symbols.vm_isolate_instrs // vm isolate instr (out) |
| 98 | ); |
| 99 | #endif |
| 100 | |
| 101 | if (loaded_elf == nullptr) { |
| 102 | FML_LOG(ERROR) |
| 103 | << "Could not fetch AOT symbols from loaded ELF. Attempts " |
| 104 | "to launch the Dart VM with these AOT symbols will fail. Error: " |
| 105 | << error; |
| 106 | return {}; |
| 107 | } |
| 108 | |
| 109 | symbols.loaded_elf.reset(p: loaded_elf); |
| 110 | |
| 111 | return symbols; |
| 112 | } |
| 113 | |
| 114 | bool PrepareSettingsForAOTWithSymbols(Settings& settings, |
| 115 | const ELFAOTSymbols& symbols) { |
| 116 | if (!DartVM::IsRunningPrecompiledCode()) { |
| 117 | return false; |
| 118 | } |
| 119 | settings.vm_snapshot_data = [&]() { |
| 120 | return std::make_unique<fml::NonOwnedMapping>(args: symbols.vm_snapshot_data, args: 0u); |
| 121 | }; |
| 122 | settings.isolate_snapshot_data = [&]() { |
| 123 | return std::make_unique<fml::NonOwnedMapping>(args: symbols.vm_isolate_data, args: 0u); |
| 124 | }; |
| 125 | settings.vm_snapshot_instr = [&]() { |
| 126 | return std::make_unique<fml::NonOwnedMapping>(args: symbols.vm_snapshot_instrs, |
| 127 | args: 0u); |
| 128 | }; |
| 129 | settings.isolate_snapshot_instr = [&]() { |
| 130 | return std::make_unique<fml::NonOwnedMapping>(args: symbols.vm_isolate_instrs, |
| 131 | args: 0u); |
| 132 | }; |
| 133 | return true; |
| 134 | } |
| 135 | |
| 136 | } // namespace testing |
| 137 | } // namespace flutter |
| 138 | |