Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 17 | #include <fstream> |
Igor Murashkin | 5573c37 | 2017-11-16 13:34:30 -0800 | [diff] [blame] | 18 | #include <regex> |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 19 | #include <sstream> |
| 20 | #include <string> |
| 21 | #include <vector> |
| 22 | |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 23 | #include <sys/mman.h> |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 24 | #include <sys/wait.h> |
| 25 | #include <unistd.h> |
| 26 | |
Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 27 | #include <android-base/logging.h> |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 28 | #include <android-base/stringprintf.h> |
| 29 | #include <android-base/strings.h> |
Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 30 | |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 31 | #include "common_runtime_test.h" |
| 32 | |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 33 | #include "base/array_ref.h" |
David Sehr | 891a50e | 2017-10-27 17:01:07 -0700 | [diff] [blame] | 34 | #include "base/file_utils.h" |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 35 | #include "base/macros.h" |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 36 | #include "base/mem_map.h" |
| 37 | #include "base/string_view_cpp20.h" |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 38 | #include "base/unix_file/fd_file.h" |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 39 | #include "base/utils.h" |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 40 | #include "dex/art_dex_file_loader.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 41 | #include "dex/dex_file-inl.h" |
| 42 | #include "dex/dex_file_loader.h" |
David Sehr | 312f3b2 | 2018-03-19 08:39:26 -0700 | [diff] [blame] | 43 | #include "dex/method_reference.h" |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 44 | #include "gc/space/image_space.h" |
David Sehr | 82d046e | 2018-04-23 08:14:19 -0700 | [diff] [blame] | 45 | #include "profile/profile_compilation_info.h" |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 46 | #include "runtime.h" |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 47 | #include "scoped_thread_state_change-inl.h" |
| 48 | #include "thread-current-inl.h" |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 49 | |
| 50 | namespace art { |
| 51 | |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 52 | // A suitable address for loading the core images. |
| 53 | constexpr uint32_t kBaseAddress = ART_BASE_ADDRESS; |
| 54 | |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 55 | struct ImageSizes { |
| 56 | size_t art_size = 0; |
| 57 | size_t oat_size = 0; |
| 58 | size_t vdex_size = 0; |
| 59 | }; |
| 60 | |
| 61 | std::ostream& operator<<(std::ostream& os, const ImageSizes& sizes) { |
| 62 | os << "art=" << sizes.art_size << " oat=" << sizes.oat_size << " vdex=" << sizes.vdex_size; |
| 63 | return os; |
| 64 | } |
| 65 | |
| 66 | class Dex2oatImageTest : public CommonRuntimeTest { |
| 67 | public: |
Roland Levillain | f73caca | 2018-08-24 17:19:07 +0100 | [diff] [blame] | 68 | void TearDown() override {} |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 69 | |
| 70 | protected: |
Vladimir Marko | a8f3918 | 2019-11-21 10:08:58 +0000 | [diff] [blame] | 71 | void SetUpRuntimeOptions(RuntimeOptions* options) override { |
| 72 | // Disable implicit dex2oat invocations when loading image spaces. |
| 73 | options->emplace_back("-Xnoimage-dex2oat", nullptr); |
| 74 | } |
| 75 | |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 76 | // Visitors take method and type references |
| 77 | template <typename MethodVisitor, typename ClassVisitor> |
| 78 | void VisitLibcoreDexes(const MethodVisitor& method_visitor, |
| 79 | const ClassVisitor& class_visitor, |
| 80 | size_t method_frequency = 1, |
| 81 | size_t class_frequency = 1) { |
Vladimir Marko | c526e42 | 2019-11-20 10:27:14 +0000 | [diff] [blame] | 82 | std::vector<std::string> dexes = GetLibCoreDexFileNames(); |
Vladimir Marko | c0e0e5e | 2020-01-23 17:43:05 +0000 | [diff] [blame] | 83 | ArrayRef<const std::string> dexes_array(dexes); |
| 84 | VisitDexes(dexes_array, method_visitor, class_visitor, method_frequency, class_frequency); |
Vladimir Marko | c526e42 | 2019-11-20 10:27:14 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | // Visitors take method and type references |
| 88 | template <typename MethodVisitor, typename ClassVisitor> |
Vladimir Marko | c0e0e5e | 2020-01-23 17:43:05 +0000 | [diff] [blame] | 89 | void VisitDexes(ArrayRef<const std::string> dexes, |
Vladimir Marko | c526e42 | 2019-11-20 10:27:14 +0000 | [diff] [blame] | 90 | const MethodVisitor& method_visitor, |
| 91 | const ClassVisitor& class_visitor, |
| 92 | size_t method_frequency = 1, |
| 93 | size_t class_frequency = 1) { |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 94 | size_t method_counter = 0; |
| 95 | size_t class_counter = 0; |
Vladimir Marko | c526e42 | 2019-11-20 10:27:14 +0000 | [diff] [blame] | 96 | for (const std::string& dex : dexes) { |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 97 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 98 | std::string error_msg; |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 99 | const ArtDexFileLoader dex_file_loader; |
| 100 | CHECK(dex_file_loader.Open(dex.c_str(), |
| 101 | dex, |
| 102 | /*verify*/ true, |
| 103 | /*verify_checksum*/ false, |
| 104 | &error_msg, |
| 105 | &dex_files)) |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 106 | << error_msg; |
| 107 | for (const std::unique_ptr<const DexFile>& dex_file : dex_files) { |
| 108 | for (size_t i = 0; i < dex_file->NumMethodIds(); ++i) { |
| 109 | if (++method_counter % method_frequency == 0) { |
| 110 | method_visitor(MethodReference(dex_file.get(), i)); |
| 111 | } |
| 112 | } |
| 113 | for (size_t i = 0; i < dex_file->NumTypeIds(); ++i) { |
| 114 | if (++class_counter % class_frequency == 0) { |
| 115 | class_visitor(TypeReference(dex_file.get(), dex::TypeIndex(i))); |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | static void WriteLine(File* file, std::string line) { |
| 123 | line += '\n'; |
| 124 | EXPECT_TRUE(file->WriteFully(&line[0], line.length())); |
| 125 | } |
| 126 | |
Vladimir Marko | c0e0e5e | 2020-01-23 17:43:05 +0000 | [diff] [blame] | 127 | void GenerateProfile(ArrayRef<const std::string> dexes, |
Vladimir Marko | c526e42 | 2019-11-20 10:27:14 +0000 | [diff] [blame] | 128 | File* out_file, |
| 129 | size_t method_frequency, |
| 130 | size_t type_frequency) { |
Mathieu Chartier | f68b698 | 2019-06-27 09:40:20 -0700 | [diff] [blame] | 131 | ProfileCompilationInfo profile; |
Vladimir Marko | c526e42 | 2019-11-20 10:27:14 +0000 | [diff] [blame] | 132 | VisitDexes( |
| 133 | dexes, |
| 134 | [&profile](MethodReference ref) { |
| 135 | uint32_t flags = ProfileCompilationInfo::MethodHotness::kFlagHot | |
| 136 | ProfileCompilationInfo::MethodHotness::kFlagStartup; |
| 137 | EXPECT_TRUE(profile.AddMethod( |
| 138 | ProfileMethodInfo(ref), |
| 139 | static_cast<ProfileCompilationInfo::MethodHotness::Flag>(flags))); |
| 140 | }, |
| 141 | [&profile](TypeReference ref) { |
| 142 | std::set<dex::TypeIndex> classes; |
| 143 | classes.insert(ref.TypeIndex()); |
| 144 | EXPECT_TRUE(profile.AddClassesForDex(ref.dex_file, classes.begin(), classes.end())); |
| 145 | }, |
| 146 | method_frequency, |
| 147 | type_frequency); |
Mathieu Chartier | f68b698 | 2019-06-27 09:40:20 -0700 | [diff] [blame] | 148 | profile.Save(out_file->Fd()); |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 149 | EXPECT_EQ(out_file->Flush(), 0); |
| 150 | } |
| 151 | |
| 152 | void GenerateMethods(File* out_file, size_t frequency = 1) { |
| 153 | VisitLibcoreDexes([out_file](MethodReference ref) { |
Mathieu Chartier | fc8b422 | 2017-09-17 13:44:24 -0700 | [diff] [blame] | 154 | WriteLine(out_file, ref.PrettyMethod()); |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 155 | }, VoidFunctor(), frequency, frequency); |
| 156 | EXPECT_EQ(out_file->Flush(), 0); |
| 157 | } |
| 158 | |
| 159 | void AddRuntimeArg(std::vector<std::string>& args, const std::string& arg) { |
| 160 | args.push_back("--runtime-arg"); |
| 161 | args.push_back(arg); |
| 162 | } |
| 163 | |
Vladimir Marko | a8f3918 | 2019-11-21 10:08:58 +0000 | [diff] [blame] | 164 | ImageSizes CompileImageAndGetSizes(ArrayRef<const std::string> dex_files, |
| 165 | const std::vector<std::string>& extra_args) { |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 166 | ImageSizes ret; |
David Srbecky | cf0c6ef | 2020-02-05 16:25:36 +0000 | [diff] [blame^] | 167 | ScratchDir scratch; |
| 168 | std::string filename_prefix = scratch.GetPath() + "boot"; |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 169 | std::vector<std::string> local_extra_args = extra_args; |
| 170 | local_extra_args.push_back(android::base::StringPrintf("--base=0x%08x", kBaseAddress)); |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 171 | std::string error_msg; |
David Srbecky | cf0c6ef | 2020-02-05 16:25:36 +0000 | [diff] [blame^] | 172 | if (!CompileBootImage(local_extra_args, filename_prefix, dex_files, &error_msg)) { |
| 173 | LOG(ERROR) << "Failed to compile image " << filename_prefix << error_msg; |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 174 | } |
David Srbecky | cf0c6ef | 2020-02-05 16:25:36 +0000 | [diff] [blame^] | 175 | std::string art_file = filename_prefix + ".art"; |
| 176 | std::string oat_file = filename_prefix + ".oat"; |
| 177 | std::string vdex_file = filename_prefix + ".vdex"; |
Vladimir Marko | a5785a2 | 2018-03-08 14:25:47 +0000 | [diff] [blame] | 178 | int64_t art_size = OS::GetFileSizeBytes(art_file.c_str()); |
| 179 | int64_t oat_size = OS::GetFileSizeBytes(oat_file.c_str()); |
| 180 | int64_t vdex_size = OS::GetFileSizeBytes(vdex_file.c_str()); |
| 181 | CHECK_GT(art_size, 0u) << art_file; |
| 182 | CHECK_GT(oat_size, 0u) << oat_file; |
| 183 | CHECK_GT(vdex_size, 0u) << vdex_file; |
| 184 | ret.art_size = art_size; |
| 185 | ret.oat_size = oat_size; |
| 186 | ret.vdex_size = vdex_size; |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 187 | return ret; |
| 188 | } |
| 189 | |
Vladimir Marko | a8f3918 | 2019-11-21 10:08:58 +0000 | [diff] [blame] | 190 | MemMap ReserveCoreImageAddressSpace(/*out*/std::string* error_msg) { |
| 191 | constexpr size_t kReservationSize = 256 * MB; // This should be enough for the compiled images. |
| 192 | // Extend to both directions for maximum relocation difference. |
| 193 | static_assert(ART_BASE_ADDRESS_MIN_DELTA < 0); |
| 194 | static_assert(ART_BASE_ADDRESS_MAX_DELTA > 0); |
| 195 | static_assert(IsAligned<kPageSize>(ART_BASE_ADDRESS_MIN_DELTA)); |
| 196 | static_assert(IsAligned<kPageSize>(ART_BASE_ADDRESS_MAX_DELTA)); |
| 197 | constexpr size_t kExtra = ART_BASE_ADDRESS_MAX_DELTA - ART_BASE_ADDRESS_MIN_DELTA; |
| 198 | uint32_t min_relocated_address = kBaseAddress + ART_BASE_ADDRESS_MIN_DELTA; |
| 199 | return MemMap::MapAnonymous("Reservation", |
| 200 | reinterpret_cast<uint8_t*>(min_relocated_address), |
| 201 | kReservationSize + kExtra, |
| 202 | PROT_NONE, |
| 203 | /*low_4gb=*/ true, |
| 204 | /*reuse=*/ false, |
| 205 | /*reservation=*/ nullptr, |
| 206 | error_msg); |
| 207 | } |
| 208 | |
| 209 | void CopyDexFiles(const std::string& dir, /*inout*/std::vector<std::string>* dex_files) { |
| 210 | CHECK(EndsWith(dir, "/")); |
| 211 | for (std::string& dex_file : *dex_files) { |
| 212 | size_t slash_pos = dex_file.rfind('/'); |
David Srbecky | cf0c6ef | 2020-02-05 16:25:36 +0000 | [diff] [blame^] | 213 | CHECK(OS::FileExists(dex_file.c_str())) << dex_file; |
Vladimir Marko | a8f3918 | 2019-11-21 10:08:58 +0000 | [diff] [blame] | 214 | CHECK_NE(std::string::npos, slash_pos); |
| 215 | std::string new_location = dir + dex_file.substr(slash_pos + 1u); |
| 216 | std::ifstream src_stream(dex_file, std::ios::binary); |
| 217 | std::ofstream dst_stream(new_location, std::ios::binary); |
| 218 | dst_stream << src_stream.rdbuf(); |
| 219 | dex_file = new_location; |
| 220 | } |
| 221 | } |
Vladimir Marko | d0036ac | 2019-11-21 11:47:12 +0000 | [diff] [blame] | 222 | |
| 223 | bool CompareFiles(const std::string& filename1, const std::string& filename2) { |
| 224 | std::unique_ptr<File> file1(OS::OpenFileForReading(filename1.c_str())); |
| 225 | std::unique_ptr<File> file2(OS::OpenFileForReading(filename2.c_str())); |
| 226 | // Did we open the files? |
| 227 | if (file1 == nullptr || file2 == nullptr) { |
| 228 | return false; |
| 229 | } |
| 230 | // Are they non-empty and the same length? |
| 231 | if (file1->GetLength() <= 0 || file2->GetLength() != file1->GetLength()) { |
| 232 | return false; |
| 233 | } |
| 234 | return file1->Compare(file2.get()) == 0; |
| 235 | } |
Vladimir Marko | c0e0e5e | 2020-01-23 17:43:05 +0000 | [diff] [blame] | 236 | |
| 237 | void AddAndroidRootToImageCompilerOptions() { |
| 238 | const char* android_root = getenv("ANDROID_ROOT"); |
| 239 | CHECK(android_root != nullptr); |
| 240 | Runtime::Current()->image_compiler_options_.push_back( |
| 241 | "--android-root=" + std::string(android_root)); |
| 242 | } |
| 243 | |
| 244 | void EnableImageDex2Oat() { |
| 245 | Runtime::Current()->image_dex2oat_enabled_ = true; |
| 246 | } |
| 247 | |
| 248 | void DisableImageDex2Oat() { |
| 249 | Runtime::Current()->image_dex2oat_enabled_ = false; |
| 250 | } |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 251 | }; |
| 252 | |
Vladimir Marko | a5785a2 | 2018-03-08 14:25:47 +0000 | [diff] [blame] | 253 | TEST_F(Dex2oatImageTest, TestModesAndFilters) { |
Roland Levillain | f5dd114 | 2018-07-03 13:29:18 +0100 | [diff] [blame] | 254 | // This test crashes on the gtest-heap-poisoning configuration |
| 255 | // (AddressSanitizer + CMS/RosAlloc + heap-poisoning); see b/111061592. |
| 256 | // Temporarily disable this test on this configuration to keep |
| 257 | // our automated build/testing green while we work on a fix. |
| 258 | TEST_DISABLED_FOR_MEMORY_TOOL_WITH_HEAP_POISONING_WITHOUT_READ_BARRIERS(); |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 259 | if (kIsTargetBuild) { |
| 260 | // This test is too slow for target builds. |
| 261 | return; |
| 262 | } |
Vladimir Marko | a8f3918 | 2019-11-21 10:08:58 +0000 | [diff] [blame] | 263 | // Compile only a subset of the libcore dex files to make this test shorter. |
| 264 | std::vector<std::string> libcore_dex_files = GetLibCoreDexFileNames(); |
| 265 | // The primary image must contain at least core-oj and core-libart to initialize the runtime |
| 266 | // and we also need the core-icu4j if we want to compile these with full profile. |
| 267 | ASSERT_NE(std::string::npos, libcore_dex_files[0].find("core-oj")); |
| 268 | ASSERT_NE(std::string::npos, libcore_dex_files[1].find("core-libart")); |
| 269 | ASSERT_NE(std::string::npos, libcore_dex_files[2].find("core-icu4j")); |
| 270 | ArrayRef<const std::string> dex_files = |
| 271 | ArrayRef<const std::string>(libcore_dex_files).SubArray(/*pos=*/ 0u, /*length=*/ 3u); |
| 272 | |
| 273 | ImageSizes base_sizes = CompileImageAndGetSizes(dex_files, {}); |
Mathieu Chartier | f68b698 | 2019-06-27 09:40:20 -0700 | [diff] [blame] | 274 | ImageSizes everything_sizes; |
| 275 | ImageSizes filter_sizes; |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 276 | std::cout << "Base compile sizes " << base_sizes << std::endl; |
Mathieu Chartier | f68b698 | 2019-06-27 09:40:20 -0700 | [diff] [blame] | 277 | // Compile all methods and classes |
Vladimir Marko | c0e0e5e | 2020-01-23 17:43:05 +0000 | [diff] [blame] | 278 | std::vector<std::string> libcore_dexes = GetLibCoreDexFileNames(); |
| 279 | ArrayRef<const std::string> libcore_dexes_array(libcore_dexes); |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 280 | { |
Mathieu Chartier | f68b698 | 2019-06-27 09:40:20 -0700 | [diff] [blame] | 281 | ScratchFile profile_file; |
Vladimir Marko | c0e0e5e | 2020-01-23 17:43:05 +0000 | [diff] [blame] | 282 | GenerateProfile(libcore_dexes_array, |
Vladimir Marko | c526e42 | 2019-11-20 10:27:14 +0000 | [diff] [blame] | 283 | profile_file.GetFile(), |
| 284 | /*method_frequency=*/ 1u, |
| 285 | /*type_frequency=*/ 1u); |
Mathieu Chartier | f68b698 | 2019-06-27 09:40:20 -0700 | [diff] [blame] | 286 | everything_sizes = CompileImageAndGetSizes( |
Vladimir Marko | a8f3918 | 2019-11-21 10:08:58 +0000 | [diff] [blame] | 287 | dex_files, |
Mathieu Chartier | f68b698 | 2019-06-27 09:40:20 -0700 | [diff] [blame] | 288 | {"--profile-file=" + profile_file.GetFilename(), |
| 289 | "--compiler-filter=speed-profile"}); |
| 290 | profile_file.Close(); |
| 291 | std::cout << "All methods and classes sizes " << everything_sizes << std::endl; |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 292 | // Putting all classes as image classes should increase art size |
Mathieu Chartier | f68b698 | 2019-06-27 09:40:20 -0700 | [diff] [blame] | 293 | EXPECT_GE(everything_sizes.art_size, base_sizes.art_size); |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 294 | // Sanity check that dex is the same size. |
Mathieu Chartier | f68b698 | 2019-06-27 09:40:20 -0700 | [diff] [blame] | 295 | EXPECT_EQ(everything_sizes.vdex_size, base_sizes.vdex_size); |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 296 | } |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 297 | static size_t kMethodFrequency = 3; |
| 298 | static size_t kTypeFrequency = 4; |
| 299 | // Test compiling fewer methods and classes. |
| 300 | { |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 301 | ScratchFile profile_file; |
Vladimir Marko | c0e0e5e | 2020-01-23 17:43:05 +0000 | [diff] [blame] | 302 | GenerateProfile(libcore_dexes_array, |
Vladimir Marko | c526e42 | 2019-11-20 10:27:14 +0000 | [diff] [blame] | 303 | profile_file.GetFile(), |
| 304 | kMethodFrequency, |
| 305 | kTypeFrequency); |
Mathieu Chartier | f68b698 | 2019-06-27 09:40:20 -0700 | [diff] [blame] | 306 | filter_sizes = CompileImageAndGetSizes( |
Vladimir Marko | a8f3918 | 2019-11-21 10:08:58 +0000 | [diff] [blame] | 307 | dex_files, |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 308 | {"--profile-file=" + profile_file.GetFilename(), |
| 309 | "--compiler-filter=speed-profile"}); |
| 310 | profile_file.Close(); |
Mathieu Chartier | f68b698 | 2019-06-27 09:40:20 -0700 | [diff] [blame] | 311 | std::cout << "Fewer methods and classes sizes " << filter_sizes << std::endl; |
| 312 | EXPECT_LE(filter_sizes.art_size, everything_sizes.art_size); |
| 313 | EXPECT_LE(filter_sizes.oat_size, everything_sizes.oat_size); |
| 314 | EXPECT_LE(filter_sizes.vdex_size, everything_sizes.vdex_size); |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 315 | } |
Jeff Hao | c23b0c0 | 2017-07-27 18:19:38 -0700 | [diff] [blame] | 316 | // Test dirty image objects. |
| 317 | { |
| 318 | ScratchFile classes; |
Mathieu Chartier | f68b698 | 2019-06-27 09:40:20 -0700 | [diff] [blame] | 319 | VisitLibcoreDexes(VoidFunctor(), |
| 320 | [&](TypeReference ref) { |
| 321 | WriteLine(classes.GetFile(), ref.dex_file->PrettyType(ref.TypeIndex())); |
| 322 | }, /*method_frequency=*/ 1u, /*class_frequency=*/ 1u); |
| 323 | ImageSizes image_classes_sizes = CompileImageAndGetSizes( |
Vladimir Marko | a8f3918 | 2019-11-21 10:08:58 +0000 | [diff] [blame] | 324 | dex_files, |
Jeff Hao | c23b0c0 | 2017-07-27 18:19:38 -0700 | [diff] [blame] | 325 | {"--dirty-image-objects=" + classes.GetFilename()}); |
| 326 | classes.Close(); |
| 327 | std::cout << "Dirty image object sizes " << image_classes_sizes << std::endl; |
| 328 | } |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 329 | } |
| 330 | |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 331 | TEST_F(Dex2oatImageTest, TestExtension) { |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 332 | std::string error_msg; |
Vladimir Marko | a8f3918 | 2019-11-21 10:08:58 +0000 | [diff] [blame] | 333 | MemMap reservation = ReserveCoreImageAddressSpace(&error_msg); |
| 334 | ASSERT_TRUE(reservation.IsValid()) << error_msg; |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 335 | |
David Srbecky | cf0c6ef | 2020-02-05 16:25:36 +0000 | [diff] [blame^] | 336 | ScratchDir scratch; |
| 337 | const std::string& scratch_dir = scratch.GetPath(); |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 338 | std::string image_dir = scratch_dir + GetInstructionSetString(kRuntimeISA); |
David Srbecky | cf0c6ef | 2020-02-05 16:25:36 +0000 | [diff] [blame^] | 339 | int mkdir_result = mkdir(image_dir.c_str(), 0700); |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 340 | ASSERT_EQ(0, mkdir_result); |
| 341 | std::string filename_prefix = image_dir + "/core"; |
| 342 | |
| 343 | // Copy the libcore dex files to a custom dir inside `scratch_dir` so that we do not |
| 344 | // accidentally load pre-compiled core images from their original directory based on BCP paths. |
| 345 | std::string jar_dir = scratch_dir + "jars"; |
| 346 | mkdir_result = mkdir(jar_dir.c_str(), 0700); |
| 347 | ASSERT_EQ(0, mkdir_result); |
| 348 | jar_dir += '/'; |
| 349 | std::vector<std::string> libcore_dex_files = GetLibCoreDexFileNames(); |
Vladimir Marko | a8f3918 | 2019-11-21 10:08:58 +0000 | [diff] [blame] | 350 | CopyDexFiles(jar_dir, &libcore_dex_files); |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 351 | |
| 352 | ArrayRef<const std::string> full_bcp(libcore_dex_files); |
| 353 | size_t total_dex_files = full_bcp.size(); |
Vladimir Marko | c526e42 | 2019-11-20 10:27:14 +0000 | [diff] [blame] | 354 | ASSERT_GE(total_dex_files, 5u); // 3 for "head", 1 for "tail", at least one for "mid", see below. |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 355 | |
Vladimir Marko | c526e42 | 2019-11-20 10:27:14 +0000 | [diff] [blame] | 356 | // The primary image must contain at least core-oj and core-libart to initialize the runtime |
| 357 | // and we also need the core-icu4j if we want to compile these with full profile. |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 358 | ASSERT_NE(std::string::npos, full_bcp[0].find("core-oj")); |
| 359 | ASSERT_NE(std::string::npos, full_bcp[1].find("core-libart")); |
Vladimir Marko | c526e42 | 2019-11-20 10:27:14 +0000 | [diff] [blame] | 360 | ASSERT_NE(std::string::npos, full_bcp[2].find("core-icu4j")); |
| 361 | ArrayRef<const std::string> head_dex_files = full_bcp.SubArray(/*pos=*/ 0u, /*length=*/ 3u); |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 362 | // Middle part is everything else except for conscrypt. |
| 363 | ASSERT_NE(std::string::npos, full_bcp[full_bcp.size() - 1u].find("conscrypt")); |
| 364 | ArrayRef<const std::string> mid_bcp = |
| 365 | full_bcp.SubArray(/*pos=*/ 0u, /*length=*/ total_dex_files - 1u); |
Vladimir Marko | c526e42 | 2019-11-20 10:27:14 +0000 | [diff] [blame] | 366 | ArrayRef<const std::string> mid_dex_files = mid_bcp.SubArray(/*pos=*/ 3u); |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 367 | // Tail is just the conscrypt. |
| 368 | ArrayRef<const std::string> tail_dex_files = |
| 369 | full_bcp.SubArray(/*pos=*/ total_dex_files - 1u, /*length=*/ 1u); |
| 370 | |
| 371 | // Prepare the "head", "mid" and "tail" names and locations. |
| 372 | std::string base_name = "core.art"; |
| 373 | std::string base_location = scratch_dir + base_name; |
| 374 | std::vector<std::string> expanded_mid = gc::space::ImageSpace::ExpandMultiImageLocations( |
| 375 | mid_dex_files.SubArray(/*pos=*/ 0u, /*length=*/ 1u), |
| 376 | base_location, |
| 377 | /*boot_image_extension=*/ true); |
| 378 | CHECK_EQ(1u, expanded_mid.size()); |
| 379 | std::string mid_location = expanded_mid[0]; |
| 380 | size_t mid_slash_pos = mid_location.rfind('/'); |
| 381 | ASSERT_NE(std::string::npos, mid_slash_pos); |
| 382 | std::string mid_name = mid_location.substr(mid_slash_pos + 1u); |
| 383 | CHECK_EQ(1u, tail_dex_files.size()); |
| 384 | std::vector<std::string> expanded_tail = gc::space::ImageSpace::ExpandMultiImageLocations( |
| 385 | tail_dex_files, base_location, /*boot_image_extension=*/ true); |
| 386 | CHECK_EQ(1u, expanded_tail.size()); |
| 387 | std::string tail_location = expanded_tail[0]; |
| 388 | size_t tail_slash_pos = tail_location.rfind('/'); |
| 389 | ASSERT_NE(std::string::npos, tail_slash_pos); |
| 390 | std::string tail_name = tail_location.substr(tail_slash_pos + 1u); |
| 391 | |
Vladimir Marko | c0e0e5e | 2020-01-23 17:43:05 +0000 | [diff] [blame] | 392 | // Create profiles. |
| 393 | ScratchFile head_profile_file; |
| 394 | GenerateProfile(head_dex_files, |
| 395 | head_profile_file.GetFile(), |
| 396 | /*method_frequency=*/ 1u, |
| 397 | /*type_frequency=*/ 1u); |
| 398 | const std::string& head_profile_filename = head_profile_file.GetFilename(); |
| 399 | ScratchFile mid_profile_file; |
| 400 | GenerateProfile(mid_dex_files, |
| 401 | mid_profile_file.GetFile(), |
| 402 | /*method_frequency=*/ 5u, |
| 403 | /*type_frequency=*/ 4u); |
| 404 | const std::string& mid_profile_filename = mid_profile_file.GetFilename(); |
| 405 | ScratchFile tail_profile_file; |
| 406 | GenerateProfile(tail_dex_files, |
| 407 | tail_profile_file.GetFile(), |
| 408 | /*method_frequency=*/ 5u, |
| 409 | /*type_frequency=*/ 4u); |
| 410 | const std::string& tail_profile_filename = tail_profile_file.GetFilename(); |
| 411 | |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 412 | // Compile the "head", i.e. the primary boot image. |
Vladimir Marko | c0e0e5e | 2020-01-23 17:43:05 +0000 | [diff] [blame] | 413 | std::vector<std::string> extra_args; |
| 414 | extra_args.push_back("--profile-file=" + head_profile_filename); |
Vladimir Marko | c526e42 | 2019-11-20 10:27:14 +0000 | [diff] [blame] | 415 | extra_args.push_back(android::base::StringPrintf("--base=0x%08x", kBaseAddress)); |
| 416 | bool head_ok = CompileBootImage(extra_args, filename_prefix, head_dex_files, &error_msg); |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 417 | ASSERT_TRUE(head_ok) << error_msg; |
| 418 | |
| 419 | // Compile the "mid", i.e. the first extension. |
| 420 | std::string mid_bcp_string = android::base::Join(mid_bcp, ':'); |
Vladimir Marko | c0e0e5e | 2020-01-23 17:43:05 +0000 | [diff] [blame] | 421 | extra_args.clear(); |
| 422 | extra_args.push_back("--profile-file=" + mid_profile_filename); |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 423 | AddRuntimeArg(extra_args, "-Xbootclasspath:" + mid_bcp_string); |
| 424 | AddRuntimeArg(extra_args, "-Xbootclasspath-locations:" + mid_bcp_string); |
| 425 | extra_args.push_back("--boot-image=" + base_location); |
| 426 | bool mid_ok = CompileBootImage(extra_args, filename_prefix, mid_dex_files, &error_msg); |
| 427 | ASSERT_TRUE(mid_ok) << error_msg; |
| 428 | |
| 429 | // Try to compile the "tail" without specifying the "mid" extension. This shall fail. |
Vladimir Marko | c0e0e5e | 2020-01-23 17:43:05 +0000 | [diff] [blame] | 430 | extra_args.clear(); |
| 431 | extra_args.push_back("--profile-file=" + tail_profile_filename); |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 432 | std::string full_bcp_string = android::base::Join(full_bcp, ':'); |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 433 | AddRuntimeArg(extra_args, "-Xbootclasspath:" + full_bcp_string); |
| 434 | AddRuntimeArg(extra_args, "-Xbootclasspath-locations:" + full_bcp_string); |
| 435 | extra_args.push_back("--boot-image=" + base_location); |
| 436 | bool tail_ok = CompileBootImage(extra_args, filename_prefix, tail_dex_files, &error_msg); |
| 437 | ASSERT_FALSE(tail_ok) << error_msg; |
| 438 | |
| 439 | // Now compile the tail against both "head" and "mid". |
| 440 | CHECK(StartsWith(extra_args.back(), "--boot-image=")); |
| 441 | extra_args.back() = "--boot-image=" + base_location + ':' + mid_location; |
| 442 | tail_ok = CompileBootImage(extra_args, filename_prefix, tail_dex_files, &error_msg); |
| 443 | ASSERT_TRUE(tail_ok) << error_msg; |
| 444 | |
Vladimir Marko | 3ea6627 | 2019-12-12 14:28:50 +0000 | [diff] [blame] | 445 | // Prepare directory for the single-image test that squashes the "mid" and "tail". |
| 446 | std::string single_dir = scratch_dir + "single"; |
| 447 | mkdir_result = mkdir(single_dir.c_str(), 0700); |
| 448 | ASSERT_EQ(0, mkdir_result); |
| 449 | single_dir += '/'; |
| 450 | std::string single_image_dir = single_dir + GetInstructionSetString(kRuntimeISA); |
| 451 | mkdir_result = mkdir(single_image_dir.c_str(), 0700); |
| 452 | ASSERT_EQ(0, mkdir_result); |
| 453 | std::string single_filename_prefix = single_image_dir + "/core"; |
| 454 | |
Vladimir Marko | c0e0e5e | 2020-01-23 17:43:05 +0000 | [diff] [blame] | 455 | // The dex files for the single-image are everything not in the "head". |
| 456 | ArrayRef<const std::string> single_dex_files = full_bcp.SubArray(/*pos=*/ head_dex_files.size()); |
| 457 | |
Vladimir Marko | 3ea6627 | 2019-12-12 14:28:50 +0000 | [diff] [blame] | 458 | // Create a smaller profile for the single-image test that squashes the "mid" and "tail". |
| 459 | ScratchFile single_profile_file; |
Vladimir Marko | c0e0e5e | 2020-01-23 17:43:05 +0000 | [diff] [blame] | 460 | GenerateProfile(single_dex_files, |
Vladimir Marko | 3ea6627 | 2019-12-12 14:28:50 +0000 | [diff] [blame] | 461 | single_profile_file.GetFile(), |
| 462 | /*method_frequency=*/ 5u, |
| 463 | /*type_frequency=*/ 4u); |
Vladimir Marko | c0e0e5e | 2020-01-23 17:43:05 +0000 | [diff] [blame] | 464 | const std::string& single_profile_filename = single_profile_file.GetFilename(); |
Vladimir Marko | 3ea6627 | 2019-12-12 14:28:50 +0000 | [diff] [blame] | 465 | |
| 466 | // Prepare the single image name and location. |
| 467 | CHECK_GE(single_dex_files.size(), 2u); |
| 468 | std::string single_base_location = single_dir + base_name; |
| 469 | std::vector<std::string> expanded_single = gc::space::ImageSpace::ExpandMultiImageLocations( |
| 470 | single_dex_files.SubArray(/*pos=*/ 0u, /*length=*/ 1u), |
| 471 | single_base_location, |
| 472 | /*boot_image_extension=*/ true); |
| 473 | CHECK_EQ(1u, expanded_single.size()); |
| 474 | std::string single_location = expanded_single[0]; |
| 475 | size_t single_slash_pos = single_location.rfind('/'); |
| 476 | ASSERT_NE(std::string::npos, single_slash_pos); |
| 477 | std::string single_name = single_location.substr(single_slash_pos + 1u); |
| 478 | CHECK_EQ(single_name, mid_name); |
| 479 | |
| 480 | // Compile the single-image against the primary boot image. |
Vladimir Marko | c0e0e5e | 2020-01-23 17:43:05 +0000 | [diff] [blame] | 481 | extra_args.clear(); |
| 482 | extra_args.push_back("--profile-file=" + single_profile_filename); |
Vladimir Marko | 3ea6627 | 2019-12-12 14:28:50 +0000 | [diff] [blame] | 483 | AddRuntimeArg(extra_args, "-Xbootclasspath:" + full_bcp_string); |
| 484 | AddRuntimeArg(extra_args, "-Xbootclasspath-locations:" + full_bcp_string); |
| 485 | extra_args.push_back("--boot-image=" + base_location); |
| 486 | extra_args.push_back("--single-image"); |
| 487 | extra_args.push_back("--avoid-storing-invocation"); // For comparison below. |
| 488 | error_msg.clear(); |
| 489 | bool single_ok = |
| 490 | CompileBootImage(extra_args, single_filename_prefix, single_dex_files, &error_msg); |
| 491 | ASSERT_TRUE(single_ok) << error_msg; |
| 492 | |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 493 | reservation = MemMap::Invalid(); // Free the reserved memory for loading images. |
| 494 | |
| 495 | // Try to load the boot image with different image locations. |
| 496 | std::vector<std::string> boot_class_path = libcore_dex_files; |
| 497 | std::vector<std::unique_ptr<gc::space::ImageSpace>> boot_image_spaces; |
| 498 | bool relocate = false; |
| 499 | MemMap extra_reservation; |
| 500 | auto load = [&](const std::string& image_location) { |
| 501 | boot_image_spaces.clear(); |
| 502 | extra_reservation = MemMap::Invalid(); |
| 503 | ScopedObjectAccess soa(Thread::Current()); |
| 504 | return gc::space::ImageSpace::LoadBootImage(/*boot_class_path=*/ boot_class_path, |
| 505 | /*boot_class_path_locations=*/ libcore_dex_files, |
| 506 | image_location, |
| 507 | kRuntimeISA, |
| 508 | gc::space::ImageSpaceLoadingOrder::kSystemFirst, |
| 509 | relocate, |
| 510 | /*executable=*/ true, |
| 511 | /*is_zygote=*/ false, |
| 512 | /*extra_reservation_size=*/ 0u, |
| 513 | &boot_image_spaces, |
| 514 | &extra_reservation); |
| 515 | }; |
Vladimir Marko | a8f3918 | 2019-11-21 10:08:58 +0000 | [diff] [blame] | 516 | auto silent_load = [&](const std::string& image_location) { |
| 517 | ScopedLogSeverity quiet(LogSeverity::FATAL); |
| 518 | return load(image_location); |
| 519 | }; |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 520 | |
Vladimir Marko | a8f3918 | 2019-11-21 10:08:58 +0000 | [diff] [blame] | 521 | for (bool r : { false, true }) { |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 522 | relocate = r; |
| 523 | |
| 524 | // Load primary image with full path. |
| 525 | bool load_ok = load(base_location); |
| 526 | ASSERT_TRUE(load_ok) << error_msg; |
| 527 | ASSERT_FALSE(extra_reservation.IsValid()); |
| 528 | ASSERT_EQ(head_dex_files.size(), boot_image_spaces.size()); |
| 529 | |
| 530 | // Fail to load primary image with just the name. |
Vladimir Marko | a8f3918 | 2019-11-21 10:08:58 +0000 | [diff] [blame] | 531 | load_ok = silent_load(base_name); |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 532 | ASSERT_FALSE(load_ok); |
| 533 | |
| 534 | // Fail to load primary image with a search path. |
Vladimir Marko | a8f3918 | 2019-11-21 10:08:58 +0000 | [diff] [blame] | 535 | load_ok = silent_load("*"); |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 536 | ASSERT_FALSE(load_ok); |
Vladimir Marko | a8f3918 | 2019-11-21 10:08:58 +0000 | [diff] [blame] | 537 | load_ok = silent_load(scratch_dir + "*"); |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 538 | ASSERT_FALSE(load_ok); |
| 539 | |
| 540 | // Load the primary and first extension with full path. |
| 541 | load_ok = load(base_location + ':' + mid_location); |
| 542 | ASSERT_TRUE(load_ok) << error_msg; |
| 543 | ASSERT_EQ(mid_bcp.size(), boot_image_spaces.size()); |
| 544 | |
| 545 | // Load the primary with full path and fail to load first extension without full path. |
| 546 | load_ok = load(base_location + ':' + mid_name); |
| 547 | ASSERT_TRUE(load_ok) << error_msg; // Primary image loaded successfully. |
| 548 | ASSERT_EQ(head_dex_files.size(), boot_image_spaces.size()); // But only the primary image. |
| 549 | |
| 550 | // Load all the libcore images with full paths. |
| 551 | load_ok = load(base_location + ':' + mid_location + ':' + tail_location); |
| 552 | ASSERT_TRUE(load_ok) << error_msg; |
| 553 | ASSERT_EQ(full_bcp.size(), boot_image_spaces.size()); |
| 554 | |
| 555 | // Load the primary and first extension with full paths, fail to load second extension by name. |
| 556 | load_ok = load(base_location + ':' + mid_location + ':' + tail_name); |
| 557 | ASSERT_TRUE(load_ok) << error_msg; |
| 558 | ASSERT_EQ(mid_bcp.size(), boot_image_spaces.size()); |
| 559 | |
| 560 | // Load the primary with full path and fail to load first extension without full path, |
| 561 | // fail to load second extension because it depends on the first. |
| 562 | load_ok = load(base_location + ':' + mid_name + ':' + tail_location); |
| 563 | ASSERT_TRUE(load_ok) << error_msg; // Primary image loaded successfully. |
| 564 | ASSERT_EQ(head_dex_files.size(), boot_image_spaces.size()); // But only the primary image. |
| 565 | |
| 566 | // Load the primary with full path and extensions with a specified search path. |
| 567 | load_ok = load(base_location + ':' + scratch_dir + '*'); |
| 568 | ASSERT_TRUE(load_ok) << error_msg; |
| 569 | ASSERT_EQ(full_bcp.size(), boot_image_spaces.size()); |
| 570 | |
| 571 | // Load the primary with full path and fail to find extensions in BCP path. |
| 572 | load_ok = load(base_location + ":*"); |
| 573 | ASSERT_TRUE(load_ok) << error_msg; |
| 574 | ASSERT_EQ(head_dex_files.size(), boot_image_spaces.size()); |
| 575 | } |
| 576 | |
| 577 | // Now copy the libcore dex files to the `scratch_dir` and retry loading the boot image |
| 578 | // with BCP in the scratch_dir so that the images can be found based on BCP paths. |
Vladimir Marko | a8f3918 | 2019-11-21 10:08:58 +0000 | [diff] [blame] | 579 | CopyDexFiles(scratch_dir, &boot_class_path); |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 580 | |
Vladimir Marko | a8f3918 | 2019-11-21 10:08:58 +0000 | [diff] [blame] | 581 | for (bool r : { false, true }) { |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 582 | relocate = r; |
| 583 | |
| 584 | // Loading the primary image with just the name now succeeds. |
| 585 | bool load_ok = load(base_name); |
| 586 | ASSERT_TRUE(load_ok) << error_msg; |
Vladimir Marko | c0e0e5e | 2020-01-23 17:43:05 +0000 | [diff] [blame] | 587 | ASSERT_EQ(head_dex_files.size(), boot_image_spaces.size()); |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 588 | |
| 589 | // Loading the primary image with a search path still fails. |
Vladimir Marko | a8f3918 | 2019-11-21 10:08:58 +0000 | [diff] [blame] | 590 | load_ok = silent_load("*"); |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 591 | ASSERT_FALSE(load_ok); |
Vladimir Marko | a8f3918 | 2019-11-21 10:08:58 +0000 | [diff] [blame] | 592 | load_ok = silent_load(scratch_dir + "*"); |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 593 | ASSERT_FALSE(load_ok); |
| 594 | |
| 595 | // Load the primary and first extension without paths. |
| 596 | load_ok = load(base_name + ':' + mid_name); |
| 597 | ASSERT_TRUE(load_ok) << error_msg; |
| 598 | ASSERT_EQ(mid_bcp.size(), boot_image_spaces.size()); |
| 599 | |
Vladimir Marko | c0e0e5e | 2020-01-23 17:43:05 +0000 | [diff] [blame] | 600 | // Load the primary without path and first extension with path. |
| 601 | load_ok = load(base_name + ':' + mid_location); |
| 602 | ASSERT_TRUE(load_ok) << error_msg; |
| 603 | ASSERT_EQ(mid_bcp.size(), boot_image_spaces.size()); |
| 604 | |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 605 | // Load the primary with full path and the first extension without full path. |
| 606 | load_ok = load(base_location + ':' + mid_name); |
| 607 | ASSERT_TRUE(load_ok) << error_msg; // Loaded successfully. |
| 608 | ASSERT_EQ(mid_bcp.size(), boot_image_spaces.size()); // Including the extension. |
| 609 | |
| 610 | // Load all the libcore images without paths. |
| 611 | load_ok = load(base_name + ':' + mid_name + ':' + tail_name); |
| 612 | ASSERT_TRUE(load_ok) << error_msg; |
| 613 | ASSERT_EQ(full_bcp.size(), boot_image_spaces.size()); |
| 614 | |
| 615 | // Load the primary and first extension with full paths and second extension by name. |
| 616 | load_ok = load(base_location + ':' + mid_location + ':' + tail_name); |
| 617 | ASSERT_TRUE(load_ok) << error_msg; |
| 618 | ASSERT_EQ(full_bcp.size(), boot_image_spaces.size()); |
| 619 | |
| 620 | // Load the primary with full path, first extension without path, |
| 621 | // and second extension with full path. |
| 622 | load_ok = load(base_location + ':' + mid_name + ':' + tail_location); |
| 623 | ASSERT_TRUE(load_ok) << error_msg; // Loaded successfully. |
| 624 | ASSERT_EQ(full_bcp.size(), boot_image_spaces.size()); // Including both extensions. |
| 625 | |
| 626 | // Load the primary with full path and find both extensions in BCP path. |
| 627 | load_ok = load(base_location + ":*"); |
| 628 | ASSERT_TRUE(load_ok) << error_msg; |
| 629 | ASSERT_EQ(full_bcp.size(), boot_image_spaces.size()); |
| 630 | |
| 631 | // Fail to load any images with invalid image locations (named component after search paths). |
Vladimir Marko | a8f3918 | 2019-11-21 10:08:58 +0000 | [diff] [blame] | 632 | load_ok = silent_load(base_location + ":*:" + tail_location); |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 633 | ASSERT_FALSE(load_ok); |
Vladimir Marko | a8f3918 | 2019-11-21 10:08:58 +0000 | [diff] [blame] | 634 | load_ok = silent_load(base_location + ':' + scratch_dir + "*:" + tail_location); |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 635 | ASSERT_FALSE(load_ok); |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 636 | |
Vladimir Marko | 3ea6627 | 2019-12-12 14:28:50 +0000 | [diff] [blame] | 637 | // Load the primary and single-image extension with full path. |
| 638 | load_ok = load(base_location + ':' + single_location); |
Vladimir Marko | d0036ac | 2019-11-21 11:47:12 +0000 | [diff] [blame] | 639 | ASSERT_TRUE(load_ok) << error_msg; |
| 640 | ASSERT_EQ(head_dex_files.size() + 1u, boot_image_spaces.size()); |
| 641 | |
Vladimir Marko | 3ea6627 | 2019-12-12 14:28:50 +0000 | [diff] [blame] | 642 | // Load the primary with full path and single-image extension with a specified search path. |
| 643 | load_ok = load(base_location + ':' + single_dir + '*'); |
Vladimir Marko | d0036ac | 2019-11-21 11:47:12 +0000 | [diff] [blame] | 644 | ASSERT_TRUE(load_ok) << error_msg; |
| 645 | ASSERT_EQ(head_dex_files.size() + 1u, boot_image_spaces.size()); |
| 646 | } |
| 647 | |
Vladimir Marko | 3ea6627 | 2019-12-12 14:28:50 +0000 | [diff] [blame] | 648 | // Recompile the single-image extension using file descriptors and compare contents. |
| 649 | std::vector<std::string> expanded_single_filename_prefix = |
| 650 | gc::space::ImageSpace::ExpandMultiImageLocations( |
| 651 | single_dex_files.SubArray(/*pos=*/ 0u, /*length=*/ 1u), |
| 652 | single_filename_prefix, |
| 653 | /*boot_image_extension=*/ true); |
| 654 | CHECK_EQ(1u, expanded_single_filename_prefix.size()); |
| 655 | std::string single_ext_prefix = expanded_single_filename_prefix[0]; |
| 656 | std::string single_ext_prefix2 = single_ext_prefix + "2"; |
Vladimir Marko | d0036ac | 2019-11-21 11:47:12 +0000 | [diff] [blame] | 657 | error_msg.clear(); |
Vladimir Marko | 3ea6627 | 2019-12-12 14:28:50 +0000 | [diff] [blame] | 658 | single_ok = CompileBootImage(extra_args, |
| 659 | single_filename_prefix, |
| 660 | single_dex_files, |
| 661 | &error_msg, |
| 662 | /*use_fd_prefix=*/ single_ext_prefix2); |
| 663 | ASSERT_TRUE(single_ok) << error_msg; |
| 664 | EXPECT_TRUE(CompareFiles(single_ext_prefix + ".art", single_ext_prefix2 + ".art")); |
| 665 | EXPECT_TRUE(CompareFiles(single_ext_prefix + ".vdex", single_ext_prefix2 + ".vdex")); |
| 666 | EXPECT_TRUE(CompareFiles(single_ext_prefix + ".oat", single_ext_prefix2 + ".oat")); |
Vladimir Marko | d0036ac | 2019-11-21 11:47:12 +0000 | [diff] [blame] | 667 | |
Vladimir Marko | c0e0e5e | 2020-01-23 17:43:05 +0000 | [diff] [blame] | 668 | // Test parsing profile specification and creating the boot image extension on-the-fly. |
| 669 | // We must set --android-root in the image compiler options. |
| 670 | AddAndroidRootToImageCompilerOptions(); |
| 671 | for (bool r : { false, true }) { |
| 672 | relocate = r; |
| 673 | |
| 674 | // Try and fail to load everything as compiled extension. |
| 675 | bool load_ok = silent_load(base_location + "!" + single_profile_filename); |
| 676 | ASSERT_FALSE(load_ok); |
| 677 | |
| 678 | // Try and fail to load with invalid spec, two profile name separators. |
| 679 | load_ok = silent_load(base_location + ":" + single_location + "!!arbitrary-profile-name"); |
| 680 | ASSERT_FALSE(load_ok); |
| 681 | |
| 682 | // Try and fail to load with invalid spec, missing profile name. |
| 683 | load_ok = silent_load(base_location + ":" + single_location + "!"); |
| 684 | ASSERT_FALSE(load_ok); |
| 685 | |
| 686 | // Try and fail to load with invalid spec, missing component name. |
| 687 | load_ok = silent_load(base_location + ":!" + single_profile_filename); |
| 688 | ASSERT_FALSE(load_ok); |
| 689 | |
| 690 | // Load primary boot image, specifying invalid extension component and profile name. |
| 691 | load_ok = load(base_location + ":/non-existent/" + single_name + "!non-existent-profile-name"); |
| 692 | ASSERT_TRUE(load_ok) << error_msg; |
| 693 | ASSERT_EQ(head_dex_files.size(), boot_image_spaces.size()); |
| 694 | |
| 695 | // Load primary boot image and the single extension, specifying invalid profile name. |
| 696 | // (Load extension from file.) |
| 697 | load_ok = load(base_location + ":" + single_location + "!non-existent-profile-name"); |
| 698 | ASSERT_TRUE(load_ok) << error_msg; |
| 699 | ASSERT_EQ(head_dex_files.size() + 1u, boot_image_spaces.size()); |
| 700 | ASSERT_EQ(single_dex_files.size(), |
| 701 | boot_image_spaces.back()->GetImageHeader().GetComponentCount()); |
| 702 | |
| 703 | // Load primary boot image and fail to load the single extension, specifying |
| 704 | // invalid extension component name but a valid profile file. |
| 705 | // (Running dex2oat to compile extension is disabled.) |
| 706 | ASSERT_FALSE(Runtime::Current()->IsImageDex2OatEnabled()); |
| 707 | load_ok = load(base_location + ":/non-existent/" + single_name + "!" + single_profile_filename); |
| 708 | ASSERT_TRUE(load_ok) << error_msg; |
| 709 | ASSERT_EQ(head_dex_files.size(), boot_image_spaces.size()); |
| 710 | |
| 711 | EnableImageDex2Oat(); |
| 712 | |
| 713 | // Load primary boot image and the single extension, specifying invalid extension |
| 714 | // component name but a valid profile file. (Compile extension by running dex2oat.) |
| 715 | load_ok = load(base_location + ":/non-existent/" + single_name + "!" + single_profile_filename); |
| 716 | ASSERT_TRUE(load_ok) << error_msg; |
| 717 | ASSERT_EQ(head_dex_files.size() + 1u, boot_image_spaces.size()); |
| 718 | ASSERT_EQ(single_dex_files.size(), |
| 719 | boot_image_spaces.back()->GetImageHeader().GetComponentCount()); |
| 720 | |
| 721 | // Load primary boot image and two extensions, specifying invalid extension component |
| 722 | // names but valid profile files. (Compile extensions by running dex2oat.) |
| 723 | load_ok = load(base_location + ":/non-existent/" + mid_name + "!" + mid_profile_filename |
| 724 | + ":/non-existent/" + tail_name + "!" + tail_profile_filename); |
| 725 | ASSERT_TRUE(load_ok) << error_msg; |
| 726 | ASSERT_EQ(head_dex_files.size() + 2u, boot_image_spaces.size()); |
| 727 | ASSERT_EQ(mid_dex_files.size(), |
| 728 | boot_image_spaces[head_dex_files.size()]->GetImageHeader().GetComponentCount()); |
| 729 | ASSERT_EQ(tail_dex_files.size(), |
| 730 | boot_image_spaces[head_dex_files.size() + 1u]->GetImageHeader().GetComponentCount()); |
| 731 | |
| 732 | // Load primary boot image and fail to load extensions, specifying invalid component |
| 733 | // names but valid profile file only for the second one. As we fail to load the first |
| 734 | // extension, the second extension has a missing dependency and cannot be compiled. |
| 735 | load_ok = load(base_location + ":/non-existent/" + mid_name |
| 736 | + ":/non-existent/" + tail_name + "!" + tail_profile_filename); |
| 737 | ASSERT_TRUE(load_ok) << error_msg; |
| 738 | ASSERT_EQ(head_dex_files.size(), boot_image_spaces.size()); |
| 739 | |
| 740 | DisableImageDex2Oat(); |
| 741 | } |
Vladimir Marko | d0036ac | 2019-11-21 11:47:12 +0000 | [diff] [blame] | 742 | } |
| 743 | |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 744 | } // namespace art |