Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [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 | |
| 17 | #include <string> |
| 18 | #include <vector> |
| 19 | |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 20 | #include <gtest/gtest.h> |
Christopher Ferris | 7188478 | 2022-03-26 15:37:43 -0700 | [diff] [blame] | 21 | #include <procinfo/process_map.h> |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 22 | |
Nicolas Geoffray | 3d8a78a | 2018-08-29 21:10:16 +0000 | [diff] [blame] | 23 | #include "android-base/stringprintf.h" |
| 24 | #include "android-base/strings.h" |
Chris Morin | 88c6d26 | 2018-02-13 15:26:21 -0800 | [diff] [blame] | 25 | #include "base/file_utils.h" |
David Sehr | 79e2607 | 2018-04-06 17:58:50 -0700 | [diff] [blame] | 26 | #include "base/mem_map.h" |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 27 | #include "common_runtime_test.h" |
| 28 | #include "compiler_callbacks.h" |
Calin Juravle | 028c7ef | 2021-05-26 15:37:00 -0700 | [diff] [blame] | 29 | #include "dex/art_dex_file_loader.h" |
| 30 | #include "dex/dex_file_loader.h" |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 31 | #include "dex2oat_environment_test.h" |
| 32 | #include "dexopt_test.h" |
| 33 | #include "gc/space/image_space.h" |
Nicolas Geoffray | 3d8a78a | 2018-08-29 21:10:16 +0000 | [diff] [blame] | 34 | #include "hidden_api.h" |
Vladimir Marko | d3d00c0 | 2019-11-07 15:09:07 +0000 | [diff] [blame] | 35 | #include "oat.h" |
Calin Juravle | 028c7ef | 2021-05-26 15:37:00 -0700 | [diff] [blame] | 36 | #include "profile/profile_compilation_info.h" |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 37 | |
| 38 | namespace art { |
| 39 | void DexoptTest::SetUp() { |
| 40 | ReserveImageSpace(); |
| 41 | Dex2oatEnvironmentTest::SetUp(); |
| 42 | } |
| 43 | |
| 44 | void DexoptTest::PreRuntimeCreate() { |
| 45 | std::string error_msg; |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 46 | UnreserveImageSpace(); |
| 47 | } |
| 48 | |
| 49 | void DexoptTest::PostRuntimeCreate() { |
| 50 | ReserveImageSpace(); |
| 51 | } |
| 52 | |
Vladimir Marko | 00fe35e | 2018-12-03 18:43:54 +0000 | [diff] [blame] | 53 | bool DexoptTest::Dex2Oat(const std::vector<std::string>& args, std::string* error_msg) { |
Vladimir Marko | 00fe35e | 2018-12-03 18:43:54 +0000 | [diff] [blame] | 54 | std::vector<std::string> argv; |
Vladimir Marko | 7a85e70 | 2018-12-03 18:47:23 +0000 | [diff] [blame] | 55 | if (!CommonRuntimeTest::StartDex2OatCommandLine(&argv, error_msg)) { |
| 56 | return false; |
Vladimir Marko | 00fe35e | 2018-12-03 18:43:54 +0000 | [diff] [blame] | 57 | } |
Vladimir Marko | 00fe35e | 2018-12-03 18:43:54 +0000 | [diff] [blame] | 58 | |
Vladimir Marko | 7a85e70 | 2018-12-03 18:47:23 +0000 | [diff] [blame] | 59 | Runtime* runtime = Runtime::Current(); |
David Brazdil | 6dfdfef | 2019-04-11 17:39:11 +0100 | [diff] [blame] | 60 | if (runtime->GetHiddenApiEnforcementPolicy() == hiddenapi::EnforcementPolicy::kEnabled) { |
Nicolas Geoffray | 3d8a78a | 2018-08-29 21:10:16 +0000 | [diff] [blame] | 61 | argv.push_back("--runtime-arg"); |
David Brazdil | 6dfdfef | 2019-04-11 17:39:11 +0100 | [diff] [blame] | 62 | argv.push_back("-Xhidden-api-policy:enabled"); |
Nicolas Geoffray | 3d8a78a | 2018-08-29 21:10:16 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | if (!kIsTargetBuild) { |
| 66 | argv.push_back("--host"); |
| 67 | } |
| 68 | |
Nicolas Geoffray | 3d8a78a | 2018-08-29 21:10:16 +0000 | [diff] [blame] | 69 | argv.insert(argv.end(), args.begin(), args.end()); |
| 70 | |
| 71 | std::string command_line(android::base::Join(argv, ' ')); |
| 72 | return Exec(argv, error_msg); |
| 73 | } |
| 74 | |
David Srbecky | cf0c6ef | 2020-02-05 16:25:36 +0000 | [diff] [blame] | 75 | std::string DexoptTest::GenerateAlternateImage(const std::string& scratch_dir) { |
| 76 | std::vector<std::string> libcore_dex_files = GetLibCoreDexFileNames(); |
| 77 | std::vector<std::string> libcore_dex_locations = GetLibCoreDexLocations(); |
| 78 | |
| 79 | std::string image_dir = scratch_dir + GetInstructionSetString(kRuntimeISA); |
| 80 | int mkdir_result = mkdir(image_dir.c_str(), 0700); |
| 81 | CHECK_EQ(0, mkdir_result) << image_dir.c_str(); |
| 82 | |
| 83 | std::vector<std::string> extra_args { |
| 84 | "--compiler-filter=verify", |
| 85 | android::base::StringPrintf("--base=0x%08x", ART_BASE_ADDRESS), |
| 86 | }; |
| 87 | std::string filename_prefix = image_dir + "/boot-interpreter"; |
| 88 | ArrayRef<const std::string> dex_files(libcore_dex_files); |
| 89 | ArrayRef<const std::string> dex_locations(libcore_dex_locations); |
| 90 | std::string error_msg; |
| 91 | bool ok = CompileBootImage(extra_args, filename_prefix, dex_files, dex_locations, &error_msg); |
| 92 | EXPECT_TRUE(ok) << error_msg; |
| 93 | |
| 94 | return scratch_dir + "boot-interpreter.art"; |
| 95 | } |
| 96 | |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 97 | void DexoptTest::GenerateOatForTest(const std::string& dex_location, |
Vladimir Marko | e066932 | 2018-09-03 15:44:54 +0100 | [diff] [blame] | 98 | const std::string& oat_location, |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 99 | CompilerFilter::Filter filter, |
Calin Juravle | 5f9a801 | 2018-02-12 20:27:46 -0800 | [diff] [blame] | 100 | bool with_alternate_image, |
Nicolas Geoffray | 35de14b | 2019-01-10 13:10:36 +0000 | [diff] [blame] | 101 | const char* compilation_reason, |
| 102 | const std::vector<std::string>& extra_args) { |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 103 | std::vector<std::string> args; |
| 104 | args.push_back("--dex-file=" + dex_location); |
| 105 | args.push_back("--oat-file=" + oat_location); |
| 106 | args.push_back("--compiler-filter=" + CompilerFilter::NameOfFilter(filter)); |
| 107 | args.push_back("--runtime-arg"); |
| 108 | |
| 109 | // Use -Xnorelocate regardless of the relocate argument. |
| 110 | // We control relocation by redirecting the dalvik cache when needed |
| 111 | // rather than use this flag. |
| 112 | args.push_back("-Xnorelocate"); |
| 113 | |
Mathieu Chartier | d0af56c | 2017-02-17 12:56:25 -0800 | [diff] [blame] | 114 | ScratchFile profile_file; |
| 115 | if (CompilerFilter::DependsOnProfile(filter)) { |
Calin Juravle | 028c7ef | 2021-05-26 15:37:00 -0700 | [diff] [blame] | 116 | // Create a profile with some basic content so that dex2oat |
| 117 | // doesn't get an empty profile and changes the filter to verify. |
| 118 | std::string error_msg; |
| 119 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 120 | const ArtDexFileLoader dex_file_loader; |
| 121 | ASSERT_TRUE(dex_file_loader.Open( |
| 122 | dex_location.c_str(), dex_location.c_str(), /*verify=*/ false, /*verify_checksum=*/ false, |
| 123 | &error_msg, &dex_files)); |
| 124 | EXPECT_GE(dex_files.size(), 1U); |
| 125 | std::unique_ptr<const DexFile>& dex_file = dex_files[0]; |
| 126 | ProfileCompilationInfo info; |
| 127 | |
| 128 | info.AddClass(*dex_file, dex::TypeIndex(0)); |
| 129 | |
| 130 | ASSERT_TRUE(info.Save(profile_file.GetFd())); |
| 131 | ASSERT_EQ(0, profile_file.GetFile()->Flush()); |
| 132 | |
| 133 | // Set the argument |
Mathieu Chartier | d0af56c | 2017-02-17 12:56:25 -0800 | [diff] [blame] | 134 | args.push_back("--profile-file=" + profile_file.GetFilename()); |
| 135 | } |
| 136 | |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 137 | std::string image_location = GetImageLocation(); |
David Srbecky | cf0c6ef | 2020-02-05 16:25:36 +0000 | [diff] [blame] | 138 | std::optional<ScratchDir> scratch; |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 139 | if (with_alternate_image) { |
David Srbecky | cf0c6ef | 2020-02-05 16:25:36 +0000 | [diff] [blame] | 140 | scratch.emplace(); // Create the scratch directory for the generated boot image. |
| 141 | std::string alternate_image_location = GenerateAlternateImage(scratch->GetPath()); |
| 142 | args.push_back("--boot-image=" + alternate_image_location); |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 143 | } |
| 144 | |
Calin Juravle | 5f9a801 | 2018-02-12 20:27:46 -0800 | [diff] [blame] | 145 | if (compilation_reason != nullptr) { |
| 146 | args.push_back("--compilation-reason=" + std::string(compilation_reason)); |
| 147 | } |
| 148 | |
Nicolas Geoffray | 35de14b | 2019-01-10 13:10:36 +0000 | [diff] [blame] | 149 | args.insert(args.end(), extra_args.begin(), extra_args.end()); |
| 150 | |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 151 | std::string error_msg; |
Nicolas Geoffray | 3d8a78a | 2018-08-29 21:10:16 +0000 | [diff] [blame] | 152 | ASSERT_TRUE(Dex2Oat(args, &error_msg)) << error_msg; |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 153 | |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 154 | // Verify the odex file was generated as expected. |
Vladimir Marko | f4efa9e | 2018-10-17 14:12:45 +0100 | [diff] [blame] | 155 | std::unique_ptr<OatFile> odex_file(OatFile::Open(/*zip_fd=*/ -1, |
Nicolas Geoffray | 3002509 | 2018-04-19 14:43:29 +0100 | [diff] [blame] | 156 | oat_location.c_str(), |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 157 | oat_location.c_str(), |
Vladimir Marko | f4efa9e | 2018-10-17 14:12:45 +0100 | [diff] [blame] | 158 | /*executable=*/ false, |
| 159 | /*low_4gb=*/ false, |
Vladimir Marko | b7bf843 | 2019-12-03 13:18:50 +0000 | [diff] [blame] | 160 | dex_location, |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 161 | &error_msg)); |
| 162 | ASSERT_TRUE(odex_file.get() != nullptr) << error_msg; |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 163 | EXPECT_EQ(filter, odex_file->GetCompilerFilter()); |
| 164 | |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 165 | if (CompilerFilter::DependsOnImageChecksum(filter)) { |
Vladimir Marko | 436c6f5 | 2019-07-25 14:50:14 +0100 | [diff] [blame] | 166 | const OatHeader& oat_header = odex_file->GetOatHeader(); |
| 167 | const char* oat_bcp = oat_header.GetStoreValueByKey(OatHeader::kBootClassPathKey); |
| 168 | ASSERT_TRUE(oat_bcp != nullptr); |
| 169 | ASSERT_EQ(oat_bcp, android::base::Join(Runtime::Current()->GetBootClassPathLocations(), ':')); |
Vladimir Marko | 0ace563 | 2018-12-14 11:11:47 +0000 | [diff] [blame] | 170 | const char* checksums = oat_header.GetStoreValueByKey(OatHeader::kBootClassPathChecksumsKey); |
| 171 | ASSERT_TRUE(checksums != nullptr); |
Vladimir Marko | 436c6f5 | 2019-07-25 14:50:14 +0100 | [diff] [blame] | 172 | |
| 173 | bool match = gc::space::ImageSpace::VerifyBootClassPathChecksums( |
| 174 | checksums, |
| 175 | oat_bcp, |
Victor Hsieh | 61ffd04 | 2021-05-20 15:14:25 -0700 | [diff] [blame] | 176 | ArrayRef<const std::string>(&image_location, 1), |
Vladimir Marko | 436c6f5 | 2019-07-25 14:50:14 +0100 | [diff] [blame] | 177 | ArrayRef<const std::string>(Runtime::Current()->GetBootClassPathLocations()), |
| 178 | ArrayRef<const std::string>(Runtime::Current()->GetBootClassPath()), |
Victor Hsieh | a09d8b7 | 2021-05-24 14:21:55 -0700 | [diff] [blame] | 179 | ArrayRef<const int>(Runtime::Current()->GetBootClassPathFds()), |
Vladimir Marko | 436c6f5 | 2019-07-25 14:50:14 +0100 | [diff] [blame] | 180 | kRuntimeISA, |
Vladimir Marko | 436c6f5 | 2019-07-25 14:50:14 +0100 | [diff] [blame] | 181 | &error_msg); |
| 182 | ASSERT_EQ(!with_alternate_image, match) << error_msg; |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 183 | } |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | void DexoptTest::GenerateOdexForTest(const std::string& dex_location, |
Vladimir Marko | e066932 | 2018-09-03 15:44:54 +0100 | [diff] [blame] | 187 | const std::string& odex_location, |
| 188 | CompilerFilter::Filter filter, |
Nicolas Geoffray | 35de14b | 2019-01-10 13:10:36 +0000 | [diff] [blame] | 189 | const char* compilation_reason, |
| 190 | const std::vector<std::string>& extra_args) { |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 191 | GenerateOatForTest(dex_location, |
| 192 | odex_location, |
| 193 | filter, |
Vladimir Marko | f4efa9e | 2018-10-17 14:12:45 +0100 | [diff] [blame] | 194 | /*with_alternate_image=*/ false, |
Nicolas Geoffray | 35de14b | 2019-01-10 13:10:36 +0000 | [diff] [blame] | 195 | compilation_reason, |
| 196 | extra_args); |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | void DexoptTest::GenerateOatForTest(const char* dex_location, |
Vladimir Marko | e066932 | 2018-09-03 15:44:54 +0100 | [diff] [blame] | 200 | CompilerFilter::Filter filter, |
| 201 | bool with_alternate_image) { |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 202 | std::string oat_location; |
| 203 | std::string error_msg; |
| 204 | ASSERT_TRUE(OatFileAssistant::DexLocationToOatFilename( |
| 205 | dex_location, kRuntimeISA, &oat_location, &error_msg)) << error_msg; |
| 206 | GenerateOatForTest(dex_location, |
| 207 | oat_location, |
| 208 | filter, |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 209 | with_alternate_image); |
| 210 | } |
| 211 | |
| 212 | void DexoptTest::GenerateOatForTest(const char* dex_location, CompilerFilter::Filter filter) { |
Vladimir Marko | f4efa9e | 2018-10-17 14:12:45 +0100 | [diff] [blame] | 213 | GenerateOatForTest(dex_location, filter, /*with_alternate_image=*/ false); |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 214 | } |
| 215 | |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 216 | void DexoptTest::ReserveImageSpace() { |
| 217 | MemMap::Init(); |
| 218 | |
| 219 | // Ensure a chunk of memory is reserved for the image space. |
Christopher Ferris | 77b38df | 2018-01-18 16:16:49 -0800 | [diff] [blame] | 220 | uint64_t reservation_start = ART_BASE_ADDRESS; |
| 221 | uint64_t reservation_end = ART_BASE_ADDRESS + 384 * MB; |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 222 | |
Christopher Ferris | 7188478 | 2022-03-26 15:37:43 -0700 | [diff] [blame] | 223 | std::vector<android::procinfo::MapInfo> maps; |
| 224 | ASSERT_TRUE(android::procinfo::ReadProcessMaps(getpid(), &maps)); |
| 225 | for (const android::procinfo::MapInfo& map_info : maps) { |
| 226 | ReserveImageSpaceChunk(reservation_start, std::min(map_info.start, reservation_end)); |
| 227 | reservation_start = std::max(reservation_start, map_info.end); |
| 228 | if (reservation_start >= reservation_end) { |
| 229 | break; |
| 230 | } |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 231 | } |
| 232 | ReserveImageSpaceChunk(reservation_start, reservation_end); |
| 233 | } |
| 234 | |
| 235 | void DexoptTest::ReserveImageSpaceChunk(uintptr_t start, uintptr_t end) { |
| 236 | if (start < end) { |
| 237 | std::string error_msg; |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 238 | image_reservation_.push_back(MemMap::MapAnonymous("image reservation", |
| 239 | reinterpret_cast<uint8_t*>(start), |
| 240 | end - start, |
| 241 | PROT_NONE, |
Vladimir Marko | 1130659 | 2018-10-26 14:22:59 +0100 | [diff] [blame] | 242 | /*low_4gb=*/ false, |
| 243 | /*reuse=*/ false, |
| 244 | /*reservation=*/ nullptr, |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 245 | &error_msg)); |
| 246 | ASSERT_TRUE(image_reservation_.back().IsValid()) << error_msg; |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 247 | LOG(INFO) << "Reserved space for image " << |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 248 | reinterpret_cast<void*>(image_reservation_.back().Begin()) << "-" << |
| 249 | reinterpret_cast<void*>(image_reservation_.back().End()); |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | |
| 253 | void DexoptTest::UnreserveImageSpace() { |
| 254 | image_reservation_.clear(); |
| 255 | } |
| 256 | |
| 257 | } // namespace art |