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 | |
| 20 | #include <backtrace/BacktraceMap.h> |
| 21 | #include <gtest/gtest.h> |
| 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" |
| 29 | #include "dex2oat_environment_test.h" |
| 30 | #include "dexopt_test.h" |
| 31 | #include "gc/space/image_space.h" |
Nicolas Geoffray | 3d8a78a | 2018-08-29 21:10:16 +0000 | [diff] [blame] | 32 | #include "hidden_api.h" |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 33 | |
| 34 | namespace art { |
| 35 | void DexoptTest::SetUp() { |
| 36 | ReserveImageSpace(); |
| 37 | Dex2oatEnvironmentTest::SetUp(); |
| 38 | } |
| 39 | |
| 40 | void DexoptTest::PreRuntimeCreate() { |
| 41 | std::string error_msg; |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 42 | UnreserveImageSpace(); |
| 43 | } |
| 44 | |
| 45 | void DexoptTest::PostRuntimeCreate() { |
| 46 | ReserveImageSpace(); |
| 47 | } |
| 48 | |
Vladimir Marko | 00fe35e | 2018-12-03 18:43:54 +0000 | [diff] [blame] | 49 | 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] | 50 | std::vector<std::string> argv; |
Vladimir Marko | 7a85e70 | 2018-12-03 18:47:23 +0000 | [diff] [blame] | 51 | if (!CommonRuntimeTest::StartDex2OatCommandLine(&argv, error_msg)) { |
| 52 | return false; |
Vladimir Marko | 00fe35e | 2018-12-03 18:43:54 +0000 | [diff] [blame] | 53 | } |
Vladimir Marko | 00fe35e | 2018-12-03 18:43:54 +0000 | [diff] [blame] | 54 | |
Vladimir Marko | 7a85e70 | 2018-12-03 18:47:23 +0000 | [diff] [blame] | 55 | Runtime* runtime = Runtime::Current(); |
David Brazdil | 6dfdfef | 2019-04-11 17:39:11 +0100 | [diff] [blame] | 56 | if (runtime->GetHiddenApiEnforcementPolicy() == hiddenapi::EnforcementPolicy::kEnabled) { |
Nicolas Geoffray | 3d8a78a | 2018-08-29 21:10:16 +0000 | [diff] [blame] | 57 | argv.push_back("--runtime-arg"); |
David Brazdil | 6dfdfef | 2019-04-11 17:39:11 +0100 | [diff] [blame] | 58 | argv.push_back("-Xhidden-api-policy:enabled"); |
Nicolas Geoffray | 3d8a78a | 2018-08-29 21:10:16 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | if (!kIsTargetBuild) { |
| 62 | argv.push_back("--host"); |
| 63 | } |
| 64 | |
Nicolas Geoffray | 3d8a78a | 2018-08-29 21:10:16 +0000 | [diff] [blame] | 65 | argv.insert(argv.end(), args.begin(), args.end()); |
| 66 | |
| 67 | std::string command_line(android::base::Join(argv, ' ')); |
| 68 | return Exec(argv, error_msg); |
| 69 | } |
| 70 | |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 71 | void DexoptTest::GenerateOatForTest(const std::string& dex_location, |
Vladimir Marko | e066932 | 2018-09-03 15:44:54 +0100 | [diff] [blame] | 72 | const std::string& oat_location, |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 73 | CompilerFilter::Filter filter, |
Calin Juravle | 5f9a801 | 2018-02-12 20:27:46 -0800 | [diff] [blame] | 74 | bool with_alternate_image, |
Nicolas Geoffray | 35de14b | 2019-01-10 13:10:36 +0000 | [diff] [blame] | 75 | const char* compilation_reason, |
| 76 | const std::vector<std::string>& extra_args) { |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 77 | std::string dalvik_cache = GetDalvikCache(GetInstructionSetString(kRuntimeISA)); |
| 78 | std::string dalvik_cache_tmp = dalvik_cache + ".redirected"; |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 79 | |
| 80 | std::vector<std::string> args; |
| 81 | args.push_back("--dex-file=" + dex_location); |
| 82 | args.push_back("--oat-file=" + oat_location); |
| 83 | args.push_back("--compiler-filter=" + CompilerFilter::NameOfFilter(filter)); |
| 84 | args.push_back("--runtime-arg"); |
| 85 | |
| 86 | // Use -Xnorelocate regardless of the relocate argument. |
| 87 | // We control relocation by redirecting the dalvik cache when needed |
| 88 | // rather than use this flag. |
| 89 | args.push_back("-Xnorelocate"); |
| 90 | |
Mathieu Chartier | d0af56c | 2017-02-17 12:56:25 -0800 | [diff] [blame] | 91 | ScratchFile profile_file; |
| 92 | if (CompilerFilter::DependsOnProfile(filter)) { |
| 93 | args.push_back("--profile-file=" + profile_file.GetFilename()); |
| 94 | } |
| 95 | |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 96 | std::string image_location = GetImageLocation(); |
| 97 | if (with_alternate_image) { |
| 98 | args.push_back("--boot-image=" + GetImageLocation2()); |
| 99 | } |
| 100 | |
Calin Juravle | 5f9a801 | 2018-02-12 20:27:46 -0800 | [diff] [blame] | 101 | if (compilation_reason != nullptr) { |
| 102 | args.push_back("--compilation-reason=" + std::string(compilation_reason)); |
| 103 | } |
| 104 | |
Nicolas Geoffray | 35de14b | 2019-01-10 13:10:36 +0000 | [diff] [blame] | 105 | args.insert(args.end(), extra_args.begin(), extra_args.end()); |
| 106 | |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 107 | std::string error_msg; |
Nicolas Geoffray | 3d8a78a | 2018-08-29 21:10:16 +0000 | [diff] [blame] | 108 | ASSERT_TRUE(Dex2Oat(args, &error_msg)) << error_msg; |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 109 | |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 110 | // Verify the odex file was generated as expected. |
Vladimir Marko | f4efa9e | 2018-10-17 14:12:45 +0100 | [diff] [blame] | 111 | std::unique_ptr<OatFile> odex_file(OatFile::Open(/*zip_fd=*/ -1, |
Nicolas Geoffray | 3002509 | 2018-04-19 14:43:29 +0100 | [diff] [blame] | 112 | oat_location.c_str(), |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 113 | oat_location.c_str(), |
Vladimir Marko | f4efa9e | 2018-10-17 14:12:45 +0100 | [diff] [blame] | 114 | /*executable=*/ false, |
| 115 | /*low_4gb=*/ false, |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 116 | dex_location.c_str(), |
Vladimir Marko | f4efa9e | 2018-10-17 14:12:45 +0100 | [diff] [blame] | 117 | /*reservation=*/ nullptr, |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 118 | &error_msg)); |
| 119 | ASSERT_TRUE(odex_file.get() != nullptr) << error_msg; |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 120 | EXPECT_EQ(filter, odex_file->GetCompilerFilter()); |
| 121 | |
Vladimir Marko | 0ace563 | 2018-12-14 11:11:47 +0000 | [diff] [blame] | 122 | std::string boot_image_checksums = gc::space::ImageSpace::GetBootClassPathChecksums( |
Vladimir Marko | bcd99be | 2019-03-22 16:21:31 +0000 | [diff] [blame] | 123 | ArrayRef<const std::string>(Runtime::Current()->GetBootClassPath()), |
Andreas Gampe | 8682354 | 2019-02-25 09:38:49 -0800 | [diff] [blame] | 124 | image_location, |
| 125 | kRuntimeISA, |
| 126 | gc::space::ImageSpaceLoadingOrder::kSystemFirst, |
| 127 | &error_msg); |
Vladimir Marko | 0ace563 | 2018-12-14 11:11:47 +0000 | [diff] [blame] | 128 | ASSERT_FALSE(boot_image_checksums.empty()) << error_msg; |
| 129 | |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 130 | const OatHeader& oat_header = odex_file->GetOatHeader(); |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 131 | |
| 132 | if (CompilerFilter::DependsOnImageChecksum(filter)) { |
Vladimir Marko | 0ace563 | 2018-12-14 11:11:47 +0000 | [diff] [blame] | 133 | const char* checksums = oat_header.GetStoreValueByKey(OatHeader::kBootClassPathChecksumsKey); |
| 134 | ASSERT_TRUE(checksums != nullptr); |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 135 | if (with_alternate_image) { |
Vladimir Marko | 0ace563 | 2018-12-14 11:11:47 +0000 | [diff] [blame] | 136 | EXPECT_NE(boot_image_checksums, checksums); |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 137 | } else { |
Vladimir Marko | 0ace563 | 2018-12-14 11:11:47 +0000 | [diff] [blame] | 138 | EXPECT_EQ(boot_image_checksums, checksums); |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 139 | } |
| 140 | } |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | void DexoptTest::GenerateOdexForTest(const std::string& dex_location, |
Vladimir Marko | e066932 | 2018-09-03 15:44:54 +0100 | [diff] [blame] | 144 | const std::string& odex_location, |
| 145 | CompilerFilter::Filter filter, |
Nicolas Geoffray | 35de14b | 2019-01-10 13:10:36 +0000 | [diff] [blame] | 146 | const char* compilation_reason, |
| 147 | const std::vector<std::string>& extra_args) { |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 148 | GenerateOatForTest(dex_location, |
| 149 | odex_location, |
| 150 | filter, |
Vladimir Marko | f4efa9e | 2018-10-17 14:12:45 +0100 | [diff] [blame] | 151 | /*with_alternate_image=*/ false, |
Nicolas Geoffray | 35de14b | 2019-01-10 13:10:36 +0000 | [diff] [blame] | 152 | compilation_reason, |
| 153 | extra_args); |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | void DexoptTest::GenerateOatForTest(const char* dex_location, |
Vladimir Marko | e066932 | 2018-09-03 15:44:54 +0100 | [diff] [blame] | 157 | CompilerFilter::Filter filter, |
| 158 | bool with_alternate_image) { |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 159 | std::string oat_location; |
| 160 | std::string error_msg; |
| 161 | ASSERT_TRUE(OatFileAssistant::DexLocationToOatFilename( |
| 162 | dex_location, kRuntimeISA, &oat_location, &error_msg)) << error_msg; |
| 163 | GenerateOatForTest(dex_location, |
| 164 | oat_location, |
| 165 | filter, |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 166 | with_alternate_image); |
| 167 | } |
| 168 | |
| 169 | void DexoptTest::GenerateOatForTest(const char* dex_location, CompilerFilter::Filter filter) { |
Vladimir Marko | f4efa9e | 2018-10-17 14:12:45 +0100 | [diff] [blame] | 170 | GenerateOatForTest(dex_location, filter, /*with_alternate_image=*/ false); |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 171 | } |
| 172 | |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 173 | void DexoptTest::ReserveImageSpace() { |
| 174 | MemMap::Init(); |
| 175 | |
| 176 | // Ensure a chunk of memory is reserved for the image space. |
Christopher Ferris | 77b38df | 2018-01-18 16:16:49 -0800 | [diff] [blame] | 177 | uint64_t reservation_start = ART_BASE_ADDRESS; |
| 178 | uint64_t reservation_end = ART_BASE_ADDRESS + 384 * MB; |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 179 | |
| 180 | std::unique_ptr<BacktraceMap> map(BacktraceMap::Create(getpid(), true)); |
| 181 | ASSERT_TRUE(map.get() != nullptr) << "Failed to build process map"; |
Christopher Ferris | 5cf8b53 | 2017-12-03 12:46:17 -0800 | [diff] [blame] | 182 | for (BacktraceMap::iterator it = map->begin(); |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 183 | reservation_start < reservation_end && it != map->end(); ++it) { |
Christopher Ferris | 5cf8b53 | 2017-12-03 12:46:17 -0800 | [diff] [blame] | 184 | const backtrace_map_t* entry = *it; |
| 185 | ReserveImageSpaceChunk(reservation_start, std::min(entry->start, reservation_end)); |
| 186 | reservation_start = std::max(reservation_start, entry->end); |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 187 | } |
| 188 | ReserveImageSpaceChunk(reservation_start, reservation_end); |
| 189 | } |
| 190 | |
| 191 | void DexoptTest::ReserveImageSpaceChunk(uintptr_t start, uintptr_t end) { |
| 192 | if (start < end) { |
| 193 | std::string error_msg; |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 194 | image_reservation_.push_back(MemMap::MapAnonymous("image reservation", |
| 195 | reinterpret_cast<uint8_t*>(start), |
| 196 | end - start, |
| 197 | PROT_NONE, |
Vladimir Marko | 1130659 | 2018-10-26 14:22:59 +0100 | [diff] [blame] | 198 | /*low_4gb=*/ false, |
| 199 | /*reuse=*/ false, |
| 200 | /*reservation=*/ nullptr, |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 201 | &error_msg)); |
| 202 | ASSERT_TRUE(image_reservation_.back().IsValid()) << error_msg; |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 203 | LOG(INFO) << "Reserved space for image " << |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 204 | reinterpret_cast<void*>(image_reservation_.back().Begin()) << "-" << |
| 205 | reinterpret_cast<void*>(image_reservation_.back().End()); |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | |
| 209 | void DexoptTest::UnreserveImageSpace() { |
| 210 | image_reservation_.clear(); |
| 211 | } |
| 212 | |
| 213 | } // namespace art |