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 | fdd4684 | 2020-03-25 14:57:17 +0000 | [diff] [blame] | 44 | #include "dex/type_reference.h" |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 45 | #include "gc/space/image_space.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 | static void WriteLine(File* file, std::string line) { |
| 77 | line += '\n'; |
| 78 | EXPECT_TRUE(file->WriteFully(&line[0], line.length())); |
| 79 | } |
| 80 | |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 81 | void AddRuntimeArg(std::vector<std::string>& args, const std::string& arg) { |
| 82 | args.push_back("--runtime-arg"); |
| 83 | args.push_back(arg); |
| 84 | } |
| 85 | |
Vladimir Marko | a8f3918 | 2019-11-21 10:08:58 +0000 | [diff] [blame] | 86 | ImageSizes CompileImageAndGetSizes(ArrayRef<const std::string> dex_files, |
| 87 | const std::vector<std::string>& extra_args) { |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 88 | ImageSizes ret; |
David Srbecky | cf0c6ef | 2020-02-05 16:25:36 +0000 | [diff] [blame] | 89 | ScratchDir scratch; |
| 90 | std::string filename_prefix = scratch.GetPath() + "boot"; |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 91 | std::vector<std::string> local_extra_args = extra_args; |
| 92 | local_extra_args.push_back(android::base::StringPrintf("--base=0x%08x", kBaseAddress)); |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 93 | std::string error_msg; |
David Srbecky | cf0c6ef | 2020-02-05 16:25:36 +0000 | [diff] [blame] | 94 | if (!CompileBootImage(local_extra_args, filename_prefix, dex_files, &error_msg)) { |
| 95 | LOG(ERROR) << "Failed to compile image " << filename_prefix << error_msg; |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 96 | } |
David Srbecky | cf0c6ef | 2020-02-05 16:25:36 +0000 | [diff] [blame] | 97 | std::string art_file = filename_prefix + ".art"; |
| 98 | std::string oat_file = filename_prefix + ".oat"; |
| 99 | std::string vdex_file = filename_prefix + ".vdex"; |
Vladimir Marko | a5785a2 | 2018-03-08 14:25:47 +0000 | [diff] [blame] | 100 | int64_t art_size = OS::GetFileSizeBytes(art_file.c_str()); |
| 101 | int64_t oat_size = OS::GetFileSizeBytes(oat_file.c_str()); |
| 102 | int64_t vdex_size = OS::GetFileSizeBytes(vdex_file.c_str()); |
| 103 | CHECK_GT(art_size, 0u) << art_file; |
| 104 | CHECK_GT(oat_size, 0u) << oat_file; |
| 105 | CHECK_GT(vdex_size, 0u) << vdex_file; |
| 106 | ret.art_size = art_size; |
| 107 | ret.oat_size = oat_size; |
| 108 | ret.vdex_size = vdex_size; |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 109 | return ret; |
| 110 | } |
| 111 | |
Vladimir Marko | a8f3918 | 2019-11-21 10:08:58 +0000 | [diff] [blame] | 112 | MemMap ReserveCoreImageAddressSpace(/*out*/std::string* error_msg) { |
| 113 | constexpr size_t kReservationSize = 256 * MB; // This should be enough for the compiled images. |
| 114 | // Extend to both directions for maximum relocation difference. |
| 115 | static_assert(ART_BASE_ADDRESS_MIN_DELTA < 0); |
| 116 | static_assert(ART_BASE_ADDRESS_MAX_DELTA > 0); |
| 117 | static_assert(IsAligned<kPageSize>(ART_BASE_ADDRESS_MIN_DELTA)); |
| 118 | static_assert(IsAligned<kPageSize>(ART_BASE_ADDRESS_MAX_DELTA)); |
| 119 | constexpr size_t kExtra = ART_BASE_ADDRESS_MAX_DELTA - ART_BASE_ADDRESS_MIN_DELTA; |
| 120 | uint32_t min_relocated_address = kBaseAddress + ART_BASE_ADDRESS_MIN_DELTA; |
| 121 | return MemMap::MapAnonymous("Reservation", |
| 122 | reinterpret_cast<uint8_t*>(min_relocated_address), |
| 123 | kReservationSize + kExtra, |
| 124 | PROT_NONE, |
| 125 | /*low_4gb=*/ true, |
| 126 | /*reuse=*/ false, |
| 127 | /*reservation=*/ nullptr, |
| 128 | error_msg); |
| 129 | } |
| 130 | |
| 131 | void CopyDexFiles(const std::string& dir, /*inout*/std::vector<std::string>* dex_files) { |
| 132 | CHECK(EndsWith(dir, "/")); |
| 133 | for (std::string& dex_file : *dex_files) { |
| 134 | size_t slash_pos = dex_file.rfind('/'); |
David Srbecky | cf0c6ef | 2020-02-05 16:25:36 +0000 | [diff] [blame] | 135 | CHECK(OS::FileExists(dex_file.c_str())) << dex_file; |
Vladimir Marko | a8f3918 | 2019-11-21 10:08:58 +0000 | [diff] [blame] | 136 | CHECK_NE(std::string::npos, slash_pos); |
| 137 | std::string new_location = dir + dex_file.substr(slash_pos + 1u); |
| 138 | std::ifstream src_stream(dex_file, std::ios::binary); |
| 139 | std::ofstream dst_stream(new_location, std::ios::binary); |
| 140 | dst_stream << src_stream.rdbuf(); |
| 141 | dex_file = new_location; |
| 142 | } |
| 143 | } |
Vladimir Marko | d0036ac | 2019-11-21 11:47:12 +0000 | [diff] [blame] | 144 | |
| 145 | bool CompareFiles(const std::string& filename1, const std::string& filename2) { |
| 146 | std::unique_ptr<File> file1(OS::OpenFileForReading(filename1.c_str())); |
| 147 | std::unique_ptr<File> file2(OS::OpenFileForReading(filename2.c_str())); |
| 148 | // Did we open the files? |
| 149 | if (file1 == nullptr || file2 == nullptr) { |
| 150 | return false; |
| 151 | } |
| 152 | // Are they non-empty and the same length? |
| 153 | if (file1->GetLength() <= 0 || file2->GetLength() != file1->GetLength()) { |
| 154 | return false; |
| 155 | } |
| 156 | return file1->Compare(file2.get()) == 0; |
| 157 | } |
Vladimir Marko | c0e0e5e | 2020-01-23 17:43:05 +0000 | [diff] [blame] | 158 | |
| 159 | void AddAndroidRootToImageCompilerOptions() { |
| 160 | const char* android_root = getenv("ANDROID_ROOT"); |
| 161 | CHECK(android_root != nullptr); |
| 162 | Runtime::Current()->image_compiler_options_.push_back( |
| 163 | "--android-root=" + std::string(android_root)); |
| 164 | } |
| 165 | |
| 166 | void EnableImageDex2Oat() { |
| 167 | Runtime::Current()->image_dex2oat_enabled_ = true; |
| 168 | } |
| 169 | |
| 170 | void DisableImageDex2Oat() { |
| 171 | Runtime::Current()->image_dex2oat_enabled_ = false; |
| 172 | } |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 173 | }; |
| 174 | |
Vladimir Marko | a5785a2 | 2018-03-08 14:25:47 +0000 | [diff] [blame] | 175 | TEST_F(Dex2oatImageTest, TestModesAndFilters) { |
Roland Levillain | f5dd114 | 2018-07-03 13:29:18 +0100 | [diff] [blame] | 176 | // This test crashes on the gtest-heap-poisoning configuration |
| 177 | // (AddressSanitizer + CMS/RosAlloc + heap-poisoning); see b/111061592. |
| 178 | // Temporarily disable this test on this configuration to keep |
| 179 | // our automated build/testing green while we work on a fix. |
| 180 | TEST_DISABLED_FOR_MEMORY_TOOL_WITH_HEAP_POISONING_WITHOUT_READ_BARRIERS(); |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 181 | if (kIsTargetBuild) { |
| 182 | // This test is too slow for target builds. |
| 183 | return; |
| 184 | } |
Vladimir Marko | a8f3918 | 2019-11-21 10:08:58 +0000 | [diff] [blame] | 185 | // Compile only a subset of the libcore dex files to make this test shorter. |
| 186 | std::vector<std::string> libcore_dex_files = GetLibCoreDexFileNames(); |
| 187 | // The primary image must contain at least core-oj and core-libart to initialize the runtime |
| 188 | // and we also need the core-icu4j if we want to compile these with full profile. |
| 189 | ASSERT_NE(std::string::npos, libcore_dex_files[0].find("core-oj")); |
| 190 | ASSERT_NE(std::string::npos, libcore_dex_files[1].find("core-libart")); |
| 191 | ASSERT_NE(std::string::npos, libcore_dex_files[2].find("core-icu4j")); |
| 192 | ArrayRef<const std::string> dex_files = |
| 193 | ArrayRef<const std::string>(libcore_dex_files).SubArray(/*pos=*/ 0u, /*length=*/ 3u); |
| 194 | |
| 195 | ImageSizes base_sizes = CompileImageAndGetSizes(dex_files, {}); |
Mathieu Chartier | f68b698 | 2019-06-27 09:40:20 -0700 | [diff] [blame] | 196 | ImageSizes everything_sizes; |
| 197 | ImageSizes filter_sizes; |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 198 | std::cout << "Base compile sizes " << base_sizes << std::endl; |
Mathieu Chartier | f68b698 | 2019-06-27 09:40:20 -0700 | [diff] [blame] | 199 | // Compile all methods and classes |
Vladimir Marko | c0e0e5e | 2020-01-23 17:43:05 +0000 | [diff] [blame] | 200 | std::vector<std::string> libcore_dexes = GetLibCoreDexFileNames(); |
| 201 | ArrayRef<const std::string> libcore_dexes_array(libcore_dexes); |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 202 | { |
Mathieu Chartier | f68b698 | 2019-06-27 09:40:20 -0700 | [diff] [blame] | 203 | ScratchFile profile_file; |
Vladimir Marko | c0e0e5e | 2020-01-23 17:43:05 +0000 | [diff] [blame] | 204 | GenerateProfile(libcore_dexes_array, |
Vladimir Marko | c526e42 | 2019-11-20 10:27:14 +0000 | [diff] [blame] | 205 | profile_file.GetFile(), |
| 206 | /*method_frequency=*/ 1u, |
| 207 | /*type_frequency=*/ 1u); |
Mathieu Chartier | f68b698 | 2019-06-27 09:40:20 -0700 | [diff] [blame] | 208 | everything_sizes = CompileImageAndGetSizes( |
Vladimir Marko | a8f3918 | 2019-11-21 10:08:58 +0000 | [diff] [blame] | 209 | dex_files, |
Mathieu Chartier | f68b698 | 2019-06-27 09:40:20 -0700 | [diff] [blame] | 210 | {"--profile-file=" + profile_file.GetFilename(), |
| 211 | "--compiler-filter=speed-profile"}); |
| 212 | profile_file.Close(); |
| 213 | std::cout << "All methods and classes sizes " << everything_sizes << std::endl; |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 214 | // Putting all classes as image classes should increase art size |
Mathieu Chartier | f68b698 | 2019-06-27 09:40:20 -0700 | [diff] [blame] | 215 | EXPECT_GE(everything_sizes.art_size, base_sizes.art_size); |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 216 | // Sanity check that dex is the same size. |
Mathieu Chartier | f68b698 | 2019-06-27 09:40:20 -0700 | [diff] [blame] | 217 | EXPECT_EQ(everything_sizes.vdex_size, base_sizes.vdex_size); |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 218 | } |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 219 | static size_t kMethodFrequency = 3; |
| 220 | static size_t kTypeFrequency = 4; |
| 221 | // Test compiling fewer methods and classes. |
| 222 | { |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 223 | ScratchFile profile_file; |
Vladimir Marko | c0e0e5e | 2020-01-23 17:43:05 +0000 | [diff] [blame] | 224 | GenerateProfile(libcore_dexes_array, |
Vladimir Marko | c526e42 | 2019-11-20 10:27:14 +0000 | [diff] [blame] | 225 | profile_file.GetFile(), |
| 226 | kMethodFrequency, |
| 227 | kTypeFrequency); |
Mathieu Chartier | f68b698 | 2019-06-27 09:40:20 -0700 | [diff] [blame] | 228 | filter_sizes = CompileImageAndGetSizes( |
Vladimir Marko | a8f3918 | 2019-11-21 10:08:58 +0000 | [diff] [blame] | 229 | dex_files, |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 230 | {"--profile-file=" + profile_file.GetFilename(), |
| 231 | "--compiler-filter=speed-profile"}); |
| 232 | profile_file.Close(); |
Mathieu Chartier | f68b698 | 2019-06-27 09:40:20 -0700 | [diff] [blame] | 233 | std::cout << "Fewer methods and classes sizes " << filter_sizes << std::endl; |
| 234 | EXPECT_LE(filter_sizes.art_size, everything_sizes.art_size); |
| 235 | EXPECT_LE(filter_sizes.oat_size, everything_sizes.oat_size); |
| 236 | EXPECT_LE(filter_sizes.vdex_size, everything_sizes.vdex_size); |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 237 | } |
Jeff Hao | c23b0c0 | 2017-07-27 18:19:38 -0700 | [diff] [blame] | 238 | // Test dirty image objects. |
| 239 | { |
| 240 | ScratchFile classes; |
Vladimir Marko | fdd4684 | 2020-03-25 14:57:17 +0000 | [diff] [blame] | 241 | VisitDexes(libcore_dexes_array, |
| 242 | VoidFunctor(), |
| 243 | [&](TypeReference ref) { |
Mathieu Chartier | f68b698 | 2019-06-27 09:40:20 -0700 | [diff] [blame] | 244 | WriteLine(classes.GetFile(), ref.dex_file->PrettyType(ref.TypeIndex())); |
| 245 | }, /*method_frequency=*/ 1u, /*class_frequency=*/ 1u); |
| 246 | ImageSizes image_classes_sizes = CompileImageAndGetSizes( |
Vladimir Marko | a8f3918 | 2019-11-21 10:08:58 +0000 | [diff] [blame] | 247 | dex_files, |
Jeff Hao | c23b0c0 | 2017-07-27 18:19:38 -0700 | [diff] [blame] | 248 | {"--dirty-image-objects=" + classes.GetFilename()}); |
| 249 | classes.Close(); |
| 250 | std::cout << "Dirty image object sizes " << image_classes_sizes << std::endl; |
| 251 | } |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 254 | TEST_F(Dex2oatImageTest, TestExtension) { |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 255 | std::string error_msg; |
Vladimir Marko | a8f3918 | 2019-11-21 10:08:58 +0000 | [diff] [blame] | 256 | MemMap reservation = ReserveCoreImageAddressSpace(&error_msg); |
| 257 | ASSERT_TRUE(reservation.IsValid()) << error_msg; |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 258 | |
David Srbecky | cf0c6ef | 2020-02-05 16:25:36 +0000 | [diff] [blame] | 259 | ScratchDir scratch; |
| 260 | const std::string& scratch_dir = scratch.GetPath(); |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 261 | std::string image_dir = scratch_dir + GetInstructionSetString(kRuntimeISA); |
David Srbecky | cf0c6ef | 2020-02-05 16:25:36 +0000 | [diff] [blame] | 262 | int mkdir_result = mkdir(image_dir.c_str(), 0700); |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 263 | ASSERT_EQ(0, mkdir_result); |
| 264 | std::string filename_prefix = image_dir + "/core"; |
| 265 | |
| 266 | // Copy the libcore dex files to a custom dir inside `scratch_dir` so that we do not |
| 267 | // accidentally load pre-compiled core images from their original directory based on BCP paths. |
| 268 | std::string jar_dir = scratch_dir + "jars"; |
| 269 | mkdir_result = mkdir(jar_dir.c_str(), 0700); |
| 270 | ASSERT_EQ(0, mkdir_result); |
| 271 | jar_dir += '/'; |
| 272 | std::vector<std::string> libcore_dex_files = GetLibCoreDexFileNames(); |
Vladimir Marko | a8f3918 | 2019-11-21 10:08:58 +0000 | [diff] [blame] | 273 | CopyDexFiles(jar_dir, &libcore_dex_files); |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 274 | |
| 275 | ArrayRef<const std::string> full_bcp(libcore_dex_files); |
| 276 | size_t total_dex_files = full_bcp.size(); |
Vladimir Marko | c526e42 | 2019-11-20 10:27:14 +0000 | [diff] [blame] | 277 | 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] | 278 | |
Vladimir Marko | c526e42 | 2019-11-20 10:27:14 +0000 | [diff] [blame] | 279 | // The primary image must contain at least core-oj and core-libart to initialize the runtime |
| 280 | // 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] | 281 | ASSERT_NE(std::string::npos, full_bcp[0].find("core-oj")); |
| 282 | ASSERT_NE(std::string::npos, full_bcp[1].find("core-libart")); |
Vladimir Marko | c526e42 | 2019-11-20 10:27:14 +0000 | [diff] [blame] | 283 | ASSERT_NE(std::string::npos, full_bcp[2].find("core-icu4j")); |
| 284 | 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] | 285 | // Middle part is everything else except for conscrypt. |
| 286 | ASSERT_NE(std::string::npos, full_bcp[full_bcp.size() - 1u].find("conscrypt")); |
| 287 | ArrayRef<const std::string> mid_bcp = |
| 288 | full_bcp.SubArray(/*pos=*/ 0u, /*length=*/ total_dex_files - 1u); |
Vladimir Marko | c526e42 | 2019-11-20 10:27:14 +0000 | [diff] [blame] | 289 | ArrayRef<const std::string> mid_dex_files = mid_bcp.SubArray(/*pos=*/ 3u); |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 290 | // Tail is just the conscrypt. |
| 291 | ArrayRef<const std::string> tail_dex_files = |
| 292 | full_bcp.SubArray(/*pos=*/ total_dex_files - 1u, /*length=*/ 1u); |
| 293 | |
| 294 | // Prepare the "head", "mid" and "tail" names and locations. |
| 295 | std::string base_name = "core.art"; |
| 296 | std::string base_location = scratch_dir + base_name; |
| 297 | std::vector<std::string> expanded_mid = gc::space::ImageSpace::ExpandMultiImageLocations( |
| 298 | mid_dex_files.SubArray(/*pos=*/ 0u, /*length=*/ 1u), |
| 299 | base_location, |
| 300 | /*boot_image_extension=*/ true); |
| 301 | CHECK_EQ(1u, expanded_mid.size()); |
| 302 | std::string mid_location = expanded_mid[0]; |
| 303 | size_t mid_slash_pos = mid_location.rfind('/'); |
| 304 | ASSERT_NE(std::string::npos, mid_slash_pos); |
| 305 | std::string mid_name = mid_location.substr(mid_slash_pos + 1u); |
| 306 | CHECK_EQ(1u, tail_dex_files.size()); |
| 307 | std::vector<std::string> expanded_tail = gc::space::ImageSpace::ExpandMultiImageLocations( |
| 308 | tail_dex_files, base_location, /*boot_image_extension=*/ true); |
| 309 | CHECK_EQ(1u, expanded_tail.size()); |
| 310 | std::string tail_location = expanded_tail[0]; |
| 311 | size_t tail_slash_pos = tail_location.rfind('/'); |
| 312 | ASSERT_NE(std::string::npos, tail_slash_pos); |
| 313 | std::string tail_name = tail_location.substr(tail_slash_pos + 1u); |
| 314 | |
Vladimir Marko | c0e0e5e | 2020-01-23 17:43:05 +0000 | [diff] [blame] | 315 | // Create profiles. |
| 316 | ScratchFile head_profile_file; |
| 317 | GenerateProfile(head_dex_files, |
| 318 | head_profile_file.GetFile(), |
| 319 | /*method_frequency=*/ 1u, |
| 320 | /*type_frequency=*/ 1u); |
| 321 | const std::string& head_profile_filename = head_profile_file.GetFilename(); |
| 322 | ScratchFile mid_profile_file; |
| 323 | GenerateProfile(mid_dex_files, |
| 324 | mid_profile_file.GetFile(), |
| 325 | /*method_frequency=*/ 5u, |
| 326 | /*type_frequency=*/ 4u); |
| 327 | const std::string& mid_profile_filename = mid_profile_file.GetFilename(); |
| 328 | ScratchFile tail_profile_file; |
| 329 | GenerateProfile(tail_dex_files, |
| 330 | tail_profile_file.GetFile(), |
| 331 | /*method_frequency=*/ 5u, |
| 332 | /*type_frequency=*/ 4u); |
| 333 | const std::string& tail_profile_filename = tail_profile_file.GetFilename(); |
| 334 | |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 335 | // Compile the "head", i.e. the primary boot image. |
Vladimir Marko | c0e0e5e | 2020-01-23 17:43:05 +0000 | [diff] [blame] | 336 | std::vector<std::string> extra_args; |
| 337 | extra_args.push_back("--profile-file=" + head_profile_filename); |
Vladimir Marko | c526e42 | 2019-11-20 10:27:14 +0000 | [diff] [blame] | 338 | extra_args.push_back(android::base::StringPrintf("--base=0x%08x", kBaseAddress)); |
| 339 | 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] | 340 | ASSERT_TRUE(head_ok) << error_msg; |
| 341 | |
| 342 | // Compile the "mid", i.e. the first extension. |
| 343 | std::string mid_bcp_string = android::base::Join(mid_bcp, ':'); |
Vladimir Marko | c0e0e5e | 2020-01-23 17:43:05 +0000 | [diff] [blame] | 344 | extra_args.clear(); |
| 345 | extra_args.push_back("--profile-file=" + mid_profile_filename); |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 346 | AddRuntimeArg(extra_args, "-Xbootclasspath:" + mid_bcp_string); |
| 347 | AddRuntimeArg(extra_args, "-Xbootclasspath-locations:" + mid_bcp_string); |
| 348 | extra_args.push_back("--boot-image=" + base_location); |
| 349 | bool mid_ok = CompileBootImage(extra_args, filename_prefix, mid_dex_files, &error_msg); |
| 350 | ASSERT_TRUE(mid_ok) << error_msg; |
| 351 | |
| 352 | // 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] | 353 | extra_args.clear(); |
| 354 | extra_args.push_back("--profile-file=" + tail_profile_filename); |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 355 | std::string full_bcp_string = android::base::Join(full_bcp, ':'); |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 356 | AddRuntimeArg(extra_args, "-Xbootclasspath:" + full_bcp_string); |
| 357 | AddRuntimeArg(extra_args, "-Xbootclasspath-locations:" + full_bcp_string); |
| 358 | extra_args.push_back("--boot-image=" + base_location); |
| 359 | bool tail_ok = CompileBootImage(extra_args, filename_prefix, tail_dex_files, &error_msg); |
| 360 | ASSERT_FALSE(tail_ok) << error_msg; |
| 361 | |
| 362 | // Now compile the tail against both "head" and "mid". |
| 363 | CHECK(StartsWith(extra_args.back(), "--boot-image=")); |
| 364 | extra_args.back() = "--boot-image=" + base_location + ':' + mid_location; |
| 365 | tail_ok = CompileBootImage(extra_args, filename_prefix, tail_dex_files, &error_msg); |
| 366 | ASSERT_TRUE(tail_ok) << error_msg; |
| 367 | |
Vladimir Marko | 3ea6627 | 2019-12-12 14:28:50 +0000 | [diff] [blame] | 368 | // Prepare directory for the single-image test that squashes the "mid" and "tail". |
| 369 | std::string single_dir = scratch_dir + "single"; |
| 370 | mkdir_result = mkdir(single_dir.c_str(), 0700); |
| 371 | ASSERT_EQ(0, mkdir_result); |
| 372 | single_dir += '/'; |
| 373 | std::string single_image_dir = single_dir + GetInstructionSetString(kRuntimeISA); |
| 374 | mkdir_result = mkdir(single_image_dir.c_str(), 0700); |
| 375 | ASSERT_EQ(0, mkdir_result); |
| 376 | std::string single_filename_prefix = single_image_dir + "/core"; |
| 377 | |
Vladimir Marko | c0e0e5e | 2020-01-23 17:43:05 +0000 | [diff] [blame] | 378 | // The dex files for the single-image are everything not in the "head". |
| 379 | ArrayRef<const std::string> single_dex_files = full_bcp.SubArray(/*pos=*/ head_dex_files.size()); |
| 380 | |
Vladimir Marko | 3ea6627 | 2019-12-12 14:28:50 +0000 | [diff] [blame] | 381 | // Create a smaller profile for the single-image test that squashes the "mid" and "tail". |
| 382 | ScratchFile single_profile_file; |
Vladimir Marko | c0e0e5e | 2020-01-23 17:43:05 +0000 | [diff] [blame] | 383 | GenerateProfile(single_dex_files, |
Vladimir Marko | 3ea6627 | 2019-12-12 14:28:50 +0000 | [diff] [blame] | 384 | single_profile_file.GetFile(), |
| 385 | /*method_frequency=*/ 5u, |
| 386 | /*type_frequency=*/ 4u); |
Vladimir Marko | c0e0e5e | 2020-01-23 17:43:05 +0000 | [diff] [blame] | 387 | const std::string& single_profile_filename = single_profile_file.GetFilename(); |
Vladimir Marko | 3ea6627 | 2019-12-12 14:28:50 +0000 | [diff] [blame] | 388 | |
| 389 | // Prepare the single image name and location. |
| 390 | CHECK_GE(single_dex_files.size(), 2u); |
| 391 | std::string single_base_location = single_dir + base_name; |
| 392 | std::vector<std::string> expanded_single = gc::space::ImageSpace::ExpandMultiImageLocations( |
| 393 | single_dex_files.SubArray(/*pos=*/ 0u, /*length=*/ 1u), |
| 394 | single_base_location, |
| 395 | /*boot_image_extension=*/ true); |
| 396 | CHECK_EQ(1u, expanded_single.size()); |
| 397 | std::string single_location = expanded_single[0]; |
| 398 | size_t single_slash_pos = single_location.rfind('/'); |
| 399 | ASSERT_NE(std::string::npos, single_slash_pos); |
| 400 | std::string single_name = single_location.substr(single_slash_pos + 1u); |
| 401 | CHECK_EQ(single_name, mid_name); |
| 402 | |
| 403 | // Compile the single-image against the primary boot image. |
Vladimir Marko | c0e0e5e | 2020-01-23 17:43:05 +0000 | [diff] [blame] | 404 | extra_args.clear(); |
| 405 | extra_args.push_back("--profile-file=" + single_profile_filename); |
Vladimir Marko | 3ea6627 | 2019-12-12 14:28:50 +0000 | [diff] [blame] | 406 | AddRuntimeArg(extra_args, "-Xbootclasspath:" + full_bcp_string); |
| 407 | AddRuntimeArg(extra_args, "-Xbootclasspath-locations:" + full_bcp_string); |
| 408 | extra_args.push_back("--boot-image=" + base_location); |
| 409 | extra_args.push_back("--single-image"); |
| 410 | extra_args.push_back("--avoid-storing-invocation"); // For comparison below. |
| 411 | error_msg.clear(); |
| 412 | bool single_ok = |
| 413 | CompileBootImage(extra_args, single_filename_prefix, single_dex_files, &error_msg); |
| 414 | ASSERT_TRUE(single_ok) << error_msg; |
| 415 | |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 416 | reservation = MemMap::Invalid(); // Free the reserved memory for loading images. |
| 417 | |
| 418 | // Try to load the boot image with different image locations. |
| 419 | std::vector<std::string> boot_class_path = libcore_dex_files; |
| 420 | std::vector<std::unique_ptr<gc::space::ImageSpace>> boot_image_spaces; |
| 421 | bool relocate = false; |
| 422 | MemMap extra_reservation; |
| 423 | auto load = [&](const std::string& image_location) { |
| 424 | boot_image_spaces.clear(); |
| 425 | extra_reservation = MemMap::Invalid(); |
| 426 | ScopedObjectAccess soa(Thread::Current()); |
| 427 | return gc::space::ImageSpace::LoadBootImage(/*boot_class_path=*/ boot_class_path, |
| 428 | /*boot_class_path_locations=*/ libcore_dex_files, |
| 429 | image_location, |
| 430 | kRuntimeISA, |
| 431 | gc::space::ImageSpaceLoadingOrder::kSystemFirst, |
| 432 | relocate, |
| 433 | /*executable=*/ true, |
| 434 | /*is_zygote=*/ false, |
| 435 | /*extra_reservation_size=*/ 0u, |
| 436 | &boot_image_spaces, |
| 437 | &extra_reservation); |
| 438 | }; |
Vladimir Marko | a8f3918 | 2019-11-21 10:08:58 +0000 | [diff] [blame] | 439 | auto silent_load = [&](const std::string& image_location) { |
| 440 | ScopedLogSeverity quiet(LogSeverity::FATAL); |
| 441 | return load(image_location); |
| 442 | }; |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 443 | |
Vladimir Marko | a8f3918 | 2019-11-21 10:08:58 +0000 | [diff] [blame] | 444 | for (bool r : { false, true }) { |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 445 | relocate = r; |
| 446 | |
| 447 | // Load primary image with full path. |
| 448 | bool load_ok = load(base_location); |
| 449 | ASSERT_TRUE(load_ok) << error_msg; |
| 450 | ASSERT_FALSE(extra_reservation.IsValid()); |
| 451 | ASSERT_EQ(head_dex_files.size(), boot_image_spaces.size()); |
| 452 | |
| 453 | // Fail to load primary image with just the name. |
Vladimir Marko | a8f3918 | 2019-11-21 10:08:58 +0000 | [diff] [blame] | 454 | load_ok = silent_load(base_name); |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 455 | ASSERT_FALSE(load_ok); |
| 456 | |
| 457 | // Fail to load primary image with a search path. |
Vladimir Marko | a8f3918 | 2019-11-21 10:08:58 +0000 | [diff] [blame] | 458 | load_ok = silent_load("*"); |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 459 | ASSERT_FALSE(load_ok); |
Vladimir Marko | a8f3918 | 2019-11-21 10:08:58 +0000 | [diff] [blame] | 460 | load_ok = silent_load(scratch_dir + "*"); |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 461 | ASSERT_FALSE(load_ok); |
| 462 | |
| 463 | // Load the primary and first extension with full path. |
| 464 | load_ok = load(base_location + ':' + mid_location); |
| 465 | ASSERT_TRUE(load_ok) << error_msg; |
| 466 | ASSERT_EQ(mid_bcp.size(), boot_image_spaces.size()); |
| 467 | |
| 468 | // Load the primary with full path and fail to load first extension without full path. |
| 469 | load_ok = load(base_location + ':' + mid_name); |
| 470 | ASSERT_TRUE(load_ok) << error_msg; // Primary image loaded successfully. |
| 471 | ASSERT_EQ(head_dex_files.size(), boot_image_spaces.size()); // But only the primary image. |
| 472 | |
| 473 | // Load all the libcore images with full paths. |
| 474 | load_ok = load(base_location + ':' + mid_location + ':' + tail_location); |
| 475 | ASSERT_TRUE(load_ok) << error_msg; |
| 476 | ASSERT_EQ(full_bcp.size(), boot_image_spaces.size()); |
| 477 | |
| 478 | // Load the primary and first extension with full paths, fail to load second extension by name. |
| 479 | load_ok = load(base_location + ':' + mid_location + ':' + tail_name); |
| 480 | ASSERT_TRUE(load_ok) << error_msg; |
| 481 | ASSERT_EQ(mid_bcp.size(), boot_image_spaces.size()); |
| 482 | |
| 483 | // Load the primary with full path and fail to load first extension without full path, |
| 484 | // fail to load second extension because it depends on the first. |
| 485 | load_ok = load(base_location + ':' + mid_name + ':' + tail_location); |
| 486 | ASSERT_TRUE(load_ok) << error_msg; // Primary image loaded successfully. |
| 487 | ASSERT_EQ(head_dex_files.size(), boot_image_spaces.size()); // But only the primary image. |
| 488 | |
| 489 | // Load the primary with full path and extensions with a specified search path. |
| 490 | load_ok = load(base_location + ':' + scratch_dir + '*'); |
| 491 | ASSERT_TRUE(load_ok) << error_msg; |
| 492 | ASSERT_EQ(full_bcp.size(), boot_image_spaces.size()); |
| 493 | |
| 494 | // Load the primary with full path and fail to find extensions in BCP path. |
| 495 | load_ok = load(base_location + ":*"); |
| 496 | ASSERT_TRUE(load_ok) << error_msg; |
| 497 | ASSERT_EQ(head_dex_files.size(), boot_image_spaces.size()); |
| 498 | } |
| 499 | |
| 500 | // Now copy the libcore dex files to the `scratch_dir` and retry loading the boot image |
| 501 | // 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] | 502 | CopyDexFiles(scratch_dir, &boot_class_path); |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 503 | |
Vladimir Marko | a8f3918 | 2019-11-21 10:08:58 +0000 | [diff] [blame] | 504 | for (bool r : { false, true }) { |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 505 | relocate = r; |
| 506 | |
| 507 | // Loading the primary image with just the name now succeeds. |
| 508 | bool load_ok = load(base_name); |
| 509 | ASSERT_TRUE(load_ok) << error_msg; |
Vladimir Marko | c0e0e5e | 2020-01-23 17:43:05 +0000 | [diff] [blame] | 510 | ASSERT_EQ(head_dex_files.size(), boot_image_spaces.size()); |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 511 | |
| 512 | // Loading the primary image with a search path still fails. |
Vladimir Marko | a8f3918 | 2019-11-21 10:08:58 +0000 | [diff] [blame] | 513 | load_ok = silent_load("*"); |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 514 | ASSERT_FALSE(load_ok); |
Vladimir Marko | a8f3918 | 2019-11-21 10:08:58 +0000 | [diff] [blame] | 515 | load_ok = silent_load(scratch_dir + "*"); |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 516 | ASSERT_FALSE(load_ok); |
| 517 | |
| 518 | // Load the primary and first extension without paths. |
| 519 | load_ok = load(base_name + ':' + mid_name); |
| 520 | ASSERT_TRUE(load_ok) << error_msg; |
| 521 | ASSERT_EQ(mid_bcp.size(), boot_image_spaces.size()); |
| 522 | |
Vladimir Marko | c0e0e5e | 2020-01-23 17:43:05 +0000 | [diff] [blame] | 523 | // Load the primary without path and first extension with path. |
| 524 | load_ok = load(base_name + ':' + mid_location); |
| 525 | ASSERT_TRUE(load_ok) << error_msg; |
| 526 | ASSERT_EQ(mid_bcp.size(), boot_image_spaces.size()); |
| 527 | |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 528 | // Load the primary with full path and the first extension without full path. |
| 529 | load_ok = load(base_location + ':' + mid_name); |
| 530 | ASSERT_TRUE(load_ok) << error_msg; // Loaded successfully. |
| 531 | ASSERT_EQ(mid_bcp.size(), boot_image_spaces.size()); // Including the extension. |
| 532 | |
| 533 | // Load all the libcore images without paths. |
| 534 | load_ok = load(base_name + ':' + mid_name + ':' + tail_name); |
| 535 | ASSERT_TRUE(load_ok) << error_msg; |
| 536 | ASSERT_EQ(full_bcp.size(), boot_image_spaces.size()); |
| 537 | |
| 538 | // Load the primary and first extension with full paths and second extension by name. |
| 539 | load_ok = load(base_location + ':' + mid_location + ':' + tail_name); |
| 540 | ASSERT_TRUE(load_ok) << error_msg; |
| 541 | ASSERT_EQ(full_bcp.size(), boot_image_spaces.size()); |
| 542 | |
| 543 | // Load the primary with full path, first extension without path, |
| 544 | // and second extension with full path. |
| 545 | load_ok = load(base_location + ':' + mid_name + ':' + tail_location); |
| 546 | ASSERT_TRUE(load_ok) << error_msg; // Loaded successfully. |
| 547 | ASSERT_EQ(full_bcp.size(), boot_image_spaces.size()); // Including both extensions. |
| 548 | |
| 549 | // Load the primary with full path and find both extensions in BCP path. |
| 550 | load_ok = load(base_location + ":*"); |
| 551 | ASSERT_TRUE(load_ok) << error_msg; |
| 552 | ASSERT_EQ(full_bcp.size(), boot_image_spaces.size()); |
| 553 | |
| 554 | // 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] | 555 | load_ok = silent_load(base_location + ":*:" + tail_location); |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 556 | ASSERT_FALSE(load_ok); |
Vladimir Marko | a8f3918 | 2019-11-21 10:08:58 +0000 | [diff] [blame] | 557 | load_ok = silent_load(base_location + ':' + scratch_dir + "*:" + tail_location); |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 558 | ASSERT_FALSE(load_ok); |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 559 | |
Vladimir Marko | 3ea6627 | 2019-12-12 14:28:50 +0000 | [diff] [blame] | 560 | // Load the primary and single-image extension with full path. |
| 561 | load_ok = load(base_location + ':' + single_location); |
Vladimir Marko | d0036ac | 2019-11-21 11:47:12 +0000 | [diff] [blame] | 562 | ASSERT_TRUE(load_ok) << error_msg; |
| 563 | ASSERT_EQ(head_dex_files.size() + 1u, boot_image_spaces.size()); |
| 564 | |
Vladimir Marko | 3ea6627 | 2019-12-12 14:28:50 +0000 | [diff] [blame] | 565 | // Load the primary with full path and single-image extension with a specified search path. |
| 566 | load_ok = load(base_location + ':' + single_dir + '*'); |
Vladimir Marko | d0036ac | 2019-11-21 11:47:12 +0000 | [diff] [blame] | 567 | ASSERT_TRUE(load_ok) << error_msg; |
| 568 | ASSERT_EQ(head_dex_files.size() + 1u, boot_image_spaces.size()); |
| 569 | } |
| 570 | |
Vladimir Marko | 3ea6627 | 2019-12-12 14:28:50 +0000 | [diff] [blame] | 571 | // Recompile the single-image extension using file descriptors and compare contents. |
| 572 | std::vector<std::string> expanded_single_filename_prefix = |
| 573 | gc::space::ImageSpace::ExpandMultiImageLocations( |
| 574 | single_dex_files.SubArray(/*pos=*/ 0u, /*length=*/ 1u), |
| 575 | single_filename_prefix, |
| 576 | /*boot_image_extension=*/ true); |
| 577 | CHECK_EQ(1u, expanded_single_filename_prefix.size()); |
| 578 | std::string single_ext_prefix = expanded_single_filename_prefix[0]; |
| 579 | std::string single_ext_prefix2 = single_ext_prefix + "2"; |
Vladimir Marko | d0036ac | 2019-11-21 11:47:12 +0000 | [diff] [blame] | 580 | error_msg.clear(); |
Vladimir Marko | 3ea6627 | 2019-12-12 14:28:50 +0000 | [diff] [blame] | 581 | single_ok = CompileBootImage(extra_args, |
| 582 | single_filename_prefix, |
| 583 | single_dex_files, |
| 584 | &error_msg, |
| 585 | /*use_fd_prefix=*/ single_ext_prefix2); |
| 586 | ASSERT_TRUE(single_ok) << error_msg; |
| 587 | EXPECT_TRUE(CompareFiles(single_ext_prefix + ".art", single_ext_prefix2 + ".art")); |
| 588 | EXPECT_TRUE(CompareFiles(single_ext_prefix + ".vdex", single_ext_prefix2 + ".vdex")); |
| 589 | EXPECT_TRUE(CompareFiles(single_ext_prefix + ".oat", single_ext_prefix2 + ".oat")); |
Vladimir Marko | d0036ac | 2019-11-21 11:47:12 +0000 | [diff] [blame] | 590 | |
Vladimir Marko | c0e0e5e | 2020-01-23 17:43:05 +0000 | [diff] [blame] | 591 | // Test parsing profile specification and creating the boot image extension on-the-fly. |
| 592 | // We must set --android-root in the image compiler options. |
| 593 | AddAndroidRootToImageCompilerOptions(); |
| 594 | for (bool r : { false, true }) { |
| 595 | relocate = r; |
| 596 | |
| 597 | // Try and fail to load everything as compiled extension. |
| 598 | bool load_ok = silent_load(base_location + "!" + single_profile_filename); |
| 599 | ASSERT_FALSE(load_ok); |
| 600 | |
| 601 | // Try and fail to load with invalid spec, two profile name separators. |
| 602 | load_ok = silent_load(base_location + ":" + single_location + "!!arbitrary-profile-name"); |
| 603 | ASSERT_FALSE(load_ok); |
| 604 | |
| 605 | // Try and fail to load with invalid spec, missing profile name. |
| 606 | load_ok = silent_load(base_location + ":" + single_location + "!"); |
| 607 | ASSERT_FALSE(load_ok); |
| 608 | |
| 609 | // Try and fail to load with invalid spec, missing component name. |
| 610 | load_ok = silent_load(base_location + ":!" + single_profile_filename); |
| 611 | ASSERT_FALSE(load_ok); |
| 612 | |
| 613 | // Load primary boot image, specifying invalid extension component and profile name. |
| 614 | load_ok = load(base_location + ":/non-existent/" + single_name + "!non-existent-profile-name"); |
| 615 | ASSERT_TRUE(load_ok) << error_msg; |
| 616 | ASSERT_EQ(head_dex_files.size(), boot_image_spaces.size()); |
| 617 | |
| 618 | // Load primary boot image and the single extension, specifying invalid profile name. |
| 619 | // (Load extension from file.) |
| 620 | load_ok = load(base_location + ":" + single_location + "!non-existent-profile-name"); |
| 621 | ASSERT_TRUE(load_ok) << error_msg; |
| 622 | ASSERT_EQ(head_dex_files.size() + 1u, boot_image_spaces.size()); |
| 623 | ASSERT_EQ(single_dex_files.size(), |
| 624 | boot_image_spaces.back()->GetImageHeader().GetComponentCount()); |
| 625 | |
| 626 | // Load primary boot image and fail to load the single extension, specifying |
| 627 | // invalid extension component name but a valid profile file. |
| 628 | // (Running dex2oat to compile extension is disabled.) |
| 629 | ASSERT_FALSE(Runtime::Current()->IsImageDex2OatEnabled()); |
| 630 | load_ok = load(base_location + ":/non-existent/" + single_name + "!" + single_profile_filename); |
| 631 | ASSERT_TRUE(load_ok) << error_msg; |
| 632 | ASSERT_EQ(head_dex_files.size(), boot_image_spaces.size()); |
| 633 | |
| 634 | EnableImageDex2Oat(); |
| 635 | |
| 636 | // Load primary boot image and the single extension, specifying invalid extension |
| 637 | // component name but a valid profile file. (Compile extension by running dex2oat.) |
| 638 | load_ok = load(base_location + ":/non-existent/" + single_name + "!" + single_profile_filename); |
| 639 | ASSERT_TRUE(load_ok) << error_msg; |
| 640 | ASSERT_EQ(head_dex_files.size() + 1u, boot_image_spaces.size()); |
| 641 | ASSERT_EQ(single_dex_files.size(), |
| 642 | boot_image_spaces.back()->GetImageHeader().GetComponentCount()); |
| 643 | |
| 644 | // Load primary boot image and two extensions, specifying invalid extension component |
| 645 | // names but valid profile files. (Compile extensions by running dex2oat.) |
| 646 | load_ok = load(base_location + ":/non-existent/" + mid_name + "!" + mid_profile_filename |
| 647 | + ":/non-existent/" + tail_name + "!" + tail_profile_filename); |
| 648 | ASSERT_TRUE(load_ok) << error_msg; |
| 649 | ASSERT_EQ(head_dex_files.size() + 2u, boot_image_spaces.size()); |
| 650 | ASSERT_EQ(mid_dex_files.size(), |
| 651 | boot_image_spaces[head_dex_files.size()]->GetImageHeader().GetComponentCount()); |
| 652 | ASSERT_EQ(tail_dex_files.size(), |
| 653 | boot_image_spaces[head_dex_files.size() + 1u]->GetImageHeader().GetComponentCount()); |
| 654 | |
| 655 | // Load primary boot image and fail to load extensions, specifying invalid component |
| 656 | // names but valid profile file only for the second one. As we fail to load the first |
| 657 | // extension, the second extension has a missing dependency and cannot be compiled. |
| 658 | load_ok = load(base_location + ":/non-existent/" + mid_name |
| 659 | + ":/non-existent/" + tail_name + "!" + tail_profile_filename); |
| 660 | ASSERT_TRUE(load_ok) << error_msg; |
| 661 | ASSERT_EQ(head_dex_files.size(), boot_image_spaces.size()); |
| 662 | |
| 663 | DisableImageDex2Oat(); |
| 664 | } |
Vladimir Marko | d0036ac | 2019-11-21 11:47:12 +0000 | [diff] [blame] | 665 | } |
| 666 | |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 667 | } // namespace art |