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; |
| 42 | ASSERT_TRUE(PreRelocateImage(GetImageLocation(), &error_msg)) << error_msg; |
| 43 | ASSERT_TRUE(PreRelocateImage(GetImageLocation2(), &error_msg)) << error_msg; |
| 44 | UnreserveImageSpace(); |
| 45 | } |
| 46 | |
| 47 | void DexoptTest::PostRuntimeCreate() { |
| 48 | ReserveImageSpace(); |
| 49 | } |
| 50 | |
Nicolas Geoffray | 3d8a78a | 2018-08-29 21:10:16 +0000 | [diff] [blame^] | 51 | static std::string ImageLocation() { |
| 52 | Runtime* runtime = Runtime::Current(); |
| 53 | const std::vector<gc::space::ImageSpace*>& image_spaces = |
| 54 | runtime->GetHeap()->GetBootImageSpaces(); |
| 55 | if (image_spaces.empty()) { |
| 56 | return ""; |
| 57 | } |
| 58 | return image_spaces[0]->GetImageLocation(); |
| 59 | } |
| 60 | |
| 61 | bool DexoptTest::Dex2Oat(const std::vector<std::string>& args, std::string* error_msg) { |
| 62 | Runtime* runtime = Runtime::Current(); |
| 63 | |
| 64 | std::vector<std::string> argv; |
| 65 | argv.push_back(runtime->GetCompilerExecutable()); |
| 66 | if (runtime->IsJavaDebuggable()) { |
| 67 | argv.push_back("--debuggable"); |
| 68 | } |
| 69 | runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv); |
| 70 | |
| 71 | if (runtime->GetHiddenApiEnforcementPolicy() != hiddenapi::EnforcementPolicy::kNoChecks) { |
| 72 | argv.push_back("--runtime-arg"); |
| 73 | argv.push_back("-Xhidden-api-checks"); |
| 74 | } |
| 75 | |
| 76 | if (!kIsTargetBuild) { |
| 77 | argv.push_back("--host"); |
| 78 | } |
| 79 | |
| 80 | argv.push_back("--boot-image=" + ImageLocation()); |
| 81 | |
| 82 | std::vector<std::string> compiler_options = runtime->GetCompilerOptions(); |
| 83 | argv.insert(argv.end(), compiler_options.begin(), compiler_options.end()); |
| 84 | |
| 85 | argv.insert(argv.end(), args.begin(), args.end()); |
| 86 | |
| 87 | std::string command_line(android::base::Join(argv, ' ')); |
| 88 | return Exec(argv, error_msg); |
| 89 | } |
| 90 | |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 91 | void DexoptTest::GenerateOatForTest(const std::string& dex_location, |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 92 | const std::string& oat_location_in, |
| 93 | CompilerFilter::Filter filter, |
| 94 | bool relocate, |
| 95 | bool pic, |
Calin Juravle | 5f9a801 | 2018-02-12 20:27:46 -0800 | [diff] [blame] | 96 | bool with_alternate_image, |
| 97 | const char* compilation_reason) { |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 98 | std::string dalvik_cache = GetDalvikCache(GetInstructionSetString(kRuntimeISA)); |
| 99 | std::string dalvik_cache_tmp = dalvik_cache + ".redirected"; |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 100 | std::string oat_location = oat_location_in; |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 101 | if (!relocate) { |
| 102 | // Temporarily redirect the dalvik cache so dex2oat doesn't find the |
| 103 | // relocated image file. |
| 104 | ASSERT_EQ(0, rename(dalvik_cache.c_str(), dalvik_cache_tmp.c_str())) << strerror(errno); |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 105 | // If the oat location is in dalvik cache, replace the cache path with the temporary one. |
| 106 | size_t pos = oat_location.find(dalvik_cache); |
| 107 | if (pos != std::string::npos) { |
| 108 | oat_location = oat_location.replace(pos, dalvik_cache.length(), dalvik_cache_tmp); |
| 109 | } |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | std::vector<std::string> args; |
| 113 | args.push_back("--dex-file=" + dex_location); |
| 114 | args.push_back("--oat-file=" + oat_location); |
| 115 | args.push_back("--compiler-filter=" + CompilerFilter::NameOfFilter(filter)); |
| 116 | args.push_back("--runtime-arg"); |
| 117 | |
| 118 | // Use -Xnorelocate regardless of the relocate argument. |
| 119 | // We control relocation by redirecting the dalvik cache when needed |
| 120 | // rather than use this flag. |
| 121 | args.push_back("-Xnorelocate"); |
| 122 | |
Mathieu Chartier | d0af56c | 2017-02-17 12:56:25 -0800 | [diff] [blame] | 123 | ScratchFile profile_file; |
| 124 | if (CompilerFilter::DependsOnProfile(filter)) { |
| 125 | args.push_back("--profile-file=" + profile_file.GetFilename()); |
| 126 | } |
| 127 | |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 128 | if (pic) { |
| 129 | args.push_back("--compile-pic"); |
| 130 | } |
| 131 | |
| 132 | std::string image_location = GetImageLocation(); |
| 133 | if (with_alternate_image) { |
| 134 | args.push_back("--boot-image=" + GetImageLocation2()); |
| 135 | } |
| 136 | |
Calin Juravle | 5f9a801 | 2018-02-12 20:27:46 -0800 | [diff] [blame] | 137 | if (compilation_reason != nullptr) { |
| 138 | args.push_back("--compilation-reason=" + std::string(compilation_reason)); |
| 139 | } |
| 140 | |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 141 | std::string error_msg; |
Nicolas Geoffray | 3d8a78a | 2018-08-29 21:10:16 +0000 | [diff] [blame^] | 142 | ASSERT_TRUE(Dex2Oat(args, &error_msg)) << error_msg; |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 143 | |
| 144 | if (!relocate) { |
| 145 | // Restore the dalvik cache if needed. |
| 146 | ASSERT_EQ(0, rename(dalvik_cache_tmp.c_str(), dalvik_cache.c_str())) << strerror(errno); |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 147 | oat_location = oat_location_in; |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | // Verify the odex file was generated as expected. |
Nicolas Geoffray | 3002509 | 2018-04-19 14:43:29 +0100 | [diff] [blame] | 151 | std::unique_ptr<OatFile> odex_file(OatFile::Open(/* zip_fd */ -1, |
| 152 | oat_location.c_str(), |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 153 | oat_location.c_str(), |
| 154 | nullptr, |
| 155 | nullptr, |
| 156 | false, |
| 157 | /*low_4gb*/false, |
| 158 | dex_location.c_str(), |
| 159 | &error_msg)); |
| 160 | ASSERT_TRUE(odex_file.get() != nullptr) << error_msg; |
| 161 | EXPECT_EQ(pic, odex_file->IsPic()); |
| 162 | EXPECT_EQ(filter, odex_file->GetCompilerFilter()); |
| 163 | |
| 164 | std::unique_ptr<ImageHeader> image_header( |
| 165 | gc::space::ImageSpace::ReadImageHeader(image_location.c_str(), |
| 166 | kRuntimeISA, |
| 167 | &error_msg)); |
| 168 | ASSERT_TRUE(image_header != nullptr) << error_msg; |
| 169 | const OatHeader& oat_header = odex_file->GetOatHeader(); |
Richard Uhler | bc26b72 | 2017-03-10 14:27:10 +0000 | [diff] [blame] | 170 | uint32_t combined_checksum = image_header->GetOatChecksum(); |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 171 | |
| 172 | if (CompilerFilter::DependsOnImageChecksum(filter)) { |
| 173 | if (with_alternate_image) { |
| 174 | EXPECT_NE(combined_checksum, oat_header.GetImageFileLocationOatChecksum()); |
| 175 | } else { |
| 176 | EXPECT_EQ(combined_checksum, oat_header.GetImageFileLocationOatChecksum()); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | if (!with_alternate_image) { |
Nicolas Geoffray | 49cda06 | 2017-04-21 13:08:25 +0100 | [diff] [blame] | 181 | if (CompilerFilter::IsAotCompilationEnabled(filter)) { |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 182 | if (relocate) { |
| 183 | EXPECT_EQ(reinterpret_cast<uintptr_t>(image_header->GetOatDataBegin()), |
| 184 | oat_header.GetImageFileLocationOatDataBegin()); |
| 185 | EXPECT_EQ(image_header->GetPatchDelta(), oat_header.GetImagePatchDelta()); |
| 186 | } else { |
| 187 | EXPECT_NE(reinterpret_cast<uintptr_t>(image_header->GetOatDataBegin()), |
| 188 | oat_header.GetImageFileLocationOatDataBegin()); |
| 189 | EXPECT_NE(image_header->GetPatchDelta(), oat_header.GetImagePatchDelta()); |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | void DexoptTest::GenerateOdexForTest(const std::string& dex_location, |
| 196 | const std::string& odex_location, |
| 197 | CompilerFilter::Filter filter) { |
| 198 | GenerateOatForTest(dex_location, |
| 199 | odex_location, |
| 200 | filter, |
| 201 | /*relocate*/false, |
| 202 | /*pic*/false, |
| 203 | /*with_alternate_image*/false); |
| 204 | } |
| 205 | |
| 206 | void DexoptTest::GeneratePicOdexForTest(const std::string& dex_location, |
| 207 | const std::string& odex_location, |
Calin Juravle | 5f9a801 | 2018-02-12 20:27:46 -0800 | [diff] [blame] | 208 | CompilerFilter::Filter filter, |
| 209 | const char* compilation_reason) { |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 210 | GenerateOatForTest(dex_location, |
| 211 | odex_location, |
| 212 | filter, |
| 213 | /*relocate*/false, |
| 214 | /*pic*/true, |
Calin Juravle | 5f9a801 | 2018-02-12 20:27:46 -0800 | [diff] [blame] | 215 | /*with_alternate_image*/false, |
| 216 | compilation_reason); |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | void DexoptTest::GenerateOatForTest(const char* dex_location, |
| 220 | CompilerFilter::Filter filter, |
| 221 | bool relocate, |
| 222 | bool pic, |
| 223 | bool with_alternate_image) { |
| 224 | std::string oat_location; |
| 225 | std::string error_msg; |
| 226 | ASSERT_TRUE(OatFileAssistant::DexLocationToOatFilename( |
| 227 | dex_location, kRuntimeISA, &oat_location, &error_msg)) << error_msg; |
| 228 | GenerateOatForTest(dex_location, |
| 229 | oat_location, |
| 230 | filter, |
| 231 | relocate, |
| 232 | pic, |
| 233 | with_alternate_image); |
| 234 | } |
| 235 | |
| 236 | void DexoptTest::GenerateOatForTest(const char* dex_location, CompilerFilter::Filter filter) { |
| 237 | GenerateOatForTest(dex_location, |
| 238 | filter, |
| 239 | /*relocate*/true, |
| 240 | /*pic*/false, |
| 241 | /*with_alternate_image*/false); |
| 242 | } |
| 243 | |
| 244 | bool DexoptTest::PreRelocateImage(const std::string& image_location, std::string* error_msg) { |
Chris Morin | 88c6d26 | 2018-02-13 15:26:21 -0800 | [diff] [blame] | 245 | std::string dalvik_cache; |
| 246 | bool have_android_data; |
| 247 | bool dalvik_cache_exists; |
| 248 | bool is_global_cache; |
| 249 | GetDalvikCache(GetInstructionSetString(kRuntimeISA), |
| 250 | true, |
| 251 | &dalvik_cache, |
| 252 | &have_android_data, |
| 253 | &dalvik_cache_exists, |
| 254 | &is_global_cache); |
| 255 | if (!dalvik_cache_exists) { |
| 256 | *error_msg = "Failed to create dalvik cache"; |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 257 | return false; |
| 258 | } |
| 259 | |
| 260 | std::string patchoat = GetAndroidRoot(); |
| 261 | patchoat += kIsDebugBuild ? "/bin/patchoatd" : "/bin/patchoat"; |
| 262 | |
| 263 | std::vector<std::string> argv; |
| 264 | argv.push_back(patchoat); |
| 265 | argv.push_back("--input-image-location=" + image_location); |
Chris Morin | 88c6d26 | 2018-02-13 15:26:21 -0800 | [diff] [blame] | 266 | argv.push_back("--output-image-directory=" + dalvik_cache); |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 267 | argv.push_back("--instruction-set=" + std::string(GetInstructionSetString(kRuntimeISA))); |
| 268 | argv.push_back("--base-offset-delta=0x00008000"); |
| 269 | return Exec(argv, error_msg); |
| 270 | } |
| 271 | |
| 272 | void DexoptTest::ReserveImageSpace() { |
| 273 | MemMap::Init(); |
| 274 | |
| 275 | // Ensure a chunk of memory is reserved for the image space. |
| 276 | // The reservation_end includes room for the main space that has to come |
| 277 | // right after the image in case of the GSS collector. |
Christopher Ferris | 77b38df | 2018-01-18 16:16:49 -0800 | [diff] [blame] | 278 | uint64_t reservation_start = ART_BASE_ADDRESS; |
| 279 | uint64_t reservation_end = ART_BASE_ADDRESS + 384 * MB; |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 280 | |
| 281 | std::unique_ptr<BacktraceMap> map(BacktraceMap::Create(getpid(), true)); |
| 282 | ASSERT_TRUE(map.get() != nullptr) << "Failed to build process map"; |
Christopher Ferris | 5cf8b53 | 2017-12-03 12:46:17 -0800 | [diff] [blame] | 283 | for (BacktraceMap::iterator it = map->begin(); |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 284 | reservation_start < reservation_end && it != map->end(); ++it) { |
Christopher Ferris | 5cf8b53 | 2017-12-03 12:46:17 -0800 | [diff] [blame] | 285 | const backtrace_map_t* entry = *it; |
| 286 | ReserveImageSpaceChunk(reservation_start, std::min(entry->start, reservation_end)); |
| 287 | reservation_start = std::max(reservation_start, entry->end); |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 288 | } |
| 289 | ReserveImageSpaceChunk(reservation_start, reservation_end); |
| 290 | } |
| 291 | |
| 292 | void DexoptTest::ReserveImageSpaceChunk(uintptr_t start, uintptr_t end) { |
| 293 | if (start < end) { |
| 294 | std::string error_msg; |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 295 | image_reservation_.push_back(MemMap::MapAnonymous("image reservation", |
| 296 | reinterpret_cast<uint8_t*>(start), |
| 297 | end - start, |
| 298 | PROT_NONE, |
| 299 | /* low_4gb*/ false, |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 300 | &error_msg)); |
| 301 | ASSERT_TRUE(image_reservation_.back().IsValid()) << error_msg; |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 302 | LOG(INFO) << "Reserved space for image " << |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 303 | reinterpret_cast<void*>(image_reservation_.back().Begin()) << "-" << |
| 304 | reinterpret_cast<void*>(image_reservation_.back().End()); |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 305 | } |
| 306 | } |
| 307 | |
| 308 | void DexoptTest::UnreserveImageSpace() { |
| 309 | image_reservation_.clear(); |
| 310 | } |
| 311 | |
| 312 | } // namespace art |