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 | |
| 23 | #include "common_runtime_test.h" |
| 24 | #include "compiler_callbacks.h" |
| 25 | #include "dex2oat_environment_test.h" |
| 26 | #include "dexopt_test.h" |
| 27 | #include "gc/space/image_space.h" |
| 28 | #include "mem_map.h" |
| 29 | |
| 30 | namespace art { |
| 31 | void DexoptTest::SetUp() { |
| 32 | ReserveImageSpace(); |
| 33 | Dex2oatEnvironmentTest::SetUp(); |
| 34 | } |
| 35 | |
| 36 | void DexoptTest::PreRuntimeCreate() { |
| 37 | std::string error_msg; |
| 38 | ASSERT_TRUE(PreRelocateImage(GetImageLocation(), &error_msg)) << error_msg; |
| 39 | ASSERT_TRUE(PreRelocateImage(GetImageLocation2(), &error_msg)) << error_msg; |
| 40 | UnreserveImageSpace(); |
| 41 | } |
| 42 | |
| 43 | void DexoptTest::PostRuntimeCreate() { |
| 44 | ReserveImageSpace(); |
| 45 | } |
| 46 | |
| 47 | void DexoptTest::GenerateOatForTest(const std::string& dex_location, |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 48 | const std::string& oat_location_in, |
| 49 | CompilerFilter::Filter filter, |
| 50 | bool relocate, |
| 51 | bool pic, |
Calin Juravle | 5f9a801 | 2018-02-12 20:27:46 -0800 | [diff] [blame^] | 52 | bool with_alternate_image, |
| 53 | const char* compilation_reason) { |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 54 | std::string dalvik_cache = GetDalvikCache(GetInstructionSetString(kRuntimeISA)); |
| 55 | std::string dalvik_cache_tmp = dalvik_cache + ".redirected"; |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 56 | std::string oat_location = oat_location_in; |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 57 | if (!relocate) { |
| 58 | // Temporarily redirect the dalvik cache so dex2oat doesn't find the |
| 59 | // relocated image file. |
| 60 | 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] | 61 | // If the oat location is in dalvik cache, replace the cache path with the temporary one. |
| 62 | size_t pos = oat_location.find(dalvik_cache); |
| 63 | if (pos != std::string::npos) { |
| 64 | oat_location = oat_location.replace(pos, dalvik_cache.length(), dalvik_cache_tmp); |
| 65 | } |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | std::vector<std::string> args; |
| 69 | args.push_back("--dex-file=" + dex_location); |
| 70 | args.push_back("--oat-file=" + oat_location); |
| 71 | args.push_back("--compiler-filter=" + CompilerFilter::NameOfFilter(filter)); |
| 72 | args.push_back("--runtime-arg"); |
| 73 | |
| 74 | // Use -Xnorelocate regardless of the relocate argument. |
| 75 | // We control relocation by redirecting the dalvik cache when needed |
| 76 | // rather than use this flag. |
| 77 | args.push_back("-Xnorelocate"); |
| 78 | |
Mathieu Chartier | d0af56c | 2017-02-17 12:56:25 -0800 | [diff] [blame] | 79 | ScratchFile profile_file; |
| 80 | if (CompilerFilter::DependsOnProfile(filter)) { |
| 81 | args.push_back("--profile-file=" + profile_file.GetFilename()); |
| 82 | } |
| 83 | |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 84 | if (pic) { |
| 85 | args.push_back("--compile-pic"); |
| 86 | } |
| 87 | |
| 88 | std::string image_location = GetImageLocation(); |
| 89 | if (with_alternate_image) { |
| 90 | args.push_back("--boot-image=" + GetImageLocation2()); |
| 91 | } |
| 92 | |
Calin Juravle | 5f9a801 | 2018-02-12 20:27:46 -0800 | [diff] [blame^] | 93 | if (compilation_reason != nullptr) { |
| 94 | args.push_back("--compilation-reason=" + std::string(compilation_reason)); |
| 95 | } |
| 96 | |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 97 | std::string error_msg; |
| 98 | ASSERT_TRUE(OatFileAssistant::Dex2Oat(args, &error_msg)) << error_msg; |
| 99 | |
| 100 | if (!relocate) { |
| 101 | // Restore the dalvik cache if needed. |
| 102 | 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] | 103 | oat_location = oat_location_in; |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | // Verify the odex file was generated as expected. |
| 107 | std::unique_ptr<OatFile> odex_file(OatFile::Open(oat_location.c_str(), |
| 108 | oat_location.c_str(), |
| 109 | nullptr, |
| 110 | nullptr, |
| 111 | false, |
| 112 | /*low_4gb*/false, |
| 113 | dex_location.c_str(), |
| 114 | &error_msg)); |
| 115 | ASSERT_TRUE(odex_file.get() != nullptr) << error_msg; |
| 116 | EXPECT_EQ(pic, odex_file->IsPic()); |
| 117 | EXPECT_EQ(filter, odex_file->GetCompilerFilter()); |
| 118 | |
| 119 | std::unique_ptr<ImageHeader> image_header( |
| 120 | gc::space::ImageSpace::ReadImageHeader(image_location.c_str(), |
| 121 | kRuntimeISA, |
| 122 | &error_msg)); |
| 123 | ASSERT_TRUE(image_header != nullptr) << error_msg; |
| 124 | const OatHeader& oat_header = odex_file->GetOatHeader(); |
Richard Uhler | bc26b72 | 2017-03-10 14:27:10 +0000 | [diff] [blame] | 125 | uint32_t combined_checksum = image_header->GetOatChecksum(); |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 126 | |
| 127 | if (CompilerFilter::DependsOnImageChecksum(filter)) { |
| 128 | if (with_alternate_image) { |
| 129 | EXPECT_NE(combined_checksum, oat_header.GetImageFileLocationOatChecksum()); |
| 130 | } else { |
| 131 | EXPECT_EQ(combined_checksum, oat_header.GetImageFileLocationOatChecksum()); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | if (!with_alternate_image) { |
Nicolas Geoffray | 49cda06 | 2017-04-21 13:08:25 +0100 | [diff] [blame] | 136 | if (CompilerFilter::IsAotCompilationEnabled(filter)) { |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 137 | if (relocate) { |
| 138 | EXPECT_EQ(reinterpret_cast<uintptr_t>(image_header->GetOatDataBegin()), |
| 139 | oat_header.GetImageFileLocationOatDataBegin()); |
| 140 | EXPECT_EQ(image_header->GetPatchDelta(), oat_header.GetImagePatchDelta()); |
| 141 | } else { |
| 142 | EXPECT_NE(reinterpret_cast<uintptr_t>(image_header->GetOatDataBegin()), |
| 143 | oat_header.GetImageFileLocationOatDataBegin()); |
| 144 | EXPECT_NE(image_header->GetPatchDelta(), oat_header.GetImagePatchDelta()); |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | void DexoptTest::GenerateOdexForTest(const std::string& dex_location, |
| 151 | const std::string& odex_location, |
| 152 | CompilerFilter::Filter filter) { |
| 153 | GenerateOatForTest(dex_location, |
| 154 | odex_location, |
| 155 | filter, |
| 156 | /*relocate*/false, |
| 157 | /*pic*/false, |
| 158 | /*with_alternate_image*/false); |
| 159 | } |
| 160 | |
| 161 | void DexoptTest::GeneratePicOdexForTest(const std::string& dex_location, |
| 162 | const std::string& odex_location, |
Calin Juravle | 5f9a801 | 2018-02-12 20:27:46 -0800 | [diff] [blame^] | 163 | CompilerFilter::Filter filter, |
| 164 | const char* compilation_reason) { |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 165 | GenerateOatForTest(dex_location, |
| 166 | odex_location, |
| 167 | filter, |
| 168 | /*relocate*/false, |
| 169 | /*pic*/true, |
Calin Juravle | 5f9a801 | 2018-02-12 20:27:46 -0800 | [diff] [blame^] | 170 | /*with_alternate_image*/false, |
| 171 | compilation_reason); |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | void DexoptTest::GenerateOatForTest(const char* dex_location, |
| 175 | CompilerFilter::Filter filter, |
| 176 | bool relocate, |
| 177 | bool pic, |
| 178 | bool with_alternate_image) { |
| 179 | std::string oat_location; |
| 180 | std::string error_msg; |
| 181 | ASSERT_TRUE(OatFileAssistant::DexLocationToOatFilename( |
| 182 | dex_location, kRuntimeISA, &oat_location, &error_msg)) << error_msg; |
| 183 | GenerateOatForTest(dex_location, |
| 184 | oat_location, |
| 185 | filter, |
| 186 | relocate, |
| 187 | pic, |
| 188 | with_alternate_image); |
| 189 | } |
| 190 | |
| 191 | void DexoptTest::GenerateOatForTest(const char* dex_location, CompilerFilter::Filter filter) { |
| 192 | GenerateOatForTest(dex_location, |
| 193 | filter, |
| 194 | /*relocate*/true, |
| 195 | /*pic*/false, |
| 196 | /*with_alternate_image*/false); |
| 197 | } |
| 198 | |
| 199 | bool DexoptTest::PreRelocateImage(const std::string& image_location, std::string* error_msg) { |
| 200 | std::string image; |
| 201 | if (!GetCachedImageFile(image_location, &image, error_msg)) { |
| 202 | return false; |
| 203 | } |
| 204 | |
| 205 | std::string patchoat = GetAndroidRoot(); |
| 206 | patchoat += kIsDebugBuild ? "/bin/patchoatd" : "/bin/patchoat"; |
| 207 | |
| 208 | std::vector<std::string> argv; |
| 209 | argv.push_back(patchoat); |
| 210 | argv.push_back("--input-image-location=" + image_location); |
| 211 | argv.push_back("--output-image-file=" + image); |
| 212 | argv.push_back("--instruction-set=" + std::string(GetInstructionSetString(kRuntimeISA))); |
| 213 | argv.push_back("--base-offset-delta=0x00008000"); |
| 214 | return Exec(argv, error_msg); |
| 215 | } |
| 216 | |
| 217 | void DexoptTest::ReserveImageSpace() { |
| 218 | MemMap::Init(); |
| 219 | |
| 220 | // Ensure a chunk of memory is reserved for the image space. |
| 221 | // The reservation_end includes room for the main space that has to come |
| 222 | // right after the image in case of the GSS collector. |
Christopher Ferris | 77b38df | 2018-01-18 16:16:49 -0800 | [diff] [blame] | 223 | uint64_t reservation_start = ART_BASE_ADDRESS; |
| 224 | uint64_t reservation_end = ART_BASE_ADDRESS + 384 * MB; |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 225 | |
| 226 | std::unique_ptr<BacktraceMap> map(BacktraceMap::Create(getpid(), true)); |
| 227 | ASSERT_TRUE(map.get() != nullptr) << "Failed to build process map"; |
Christopher Ferris | 5cf8b53 | 2017-12-03 12:46:17 -0800 | [diff] [blame] | 228 | for (BacktraceMap::iterator it = map->begin(); |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 229 | reservation_start < reservation_end && it != map->end(); ++it) { |
Christopher Ferris | 5cf8b53 | 2017-12-03 12:46:17 -0800 | [diff] [blame] | 230 | const backtrace_map_t* entry = *it; |
| 231 | ReserveImageSpaceChunk(reservation_start, std::min(entry->start, reservation_end)); |
| 232 | reservation_start = std::max(reservation_start, entry->end); |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 233 | } |
| 234 | ReserveImageSpaceChunk(reservation_start, reservation_end); |
| 235 | } |
| 236 | |
| 237 | void DexoptTest::ReserveImageSpaceChunk(uintptr_t start, uintptr_t end) { |
| 238 | if (start < end) { |
| 239 | std::string error_msg; |
| 240 | image_reservation_.push_back(std::unique_ptr<MemMap>( |
| 241 | MemMap::MapAnonymous("image reservation", |
| 242 | reinterpret_cast<uint8_t*>(start), end - start, |
| 243 | PROT_NONE, false, false, &error_msg))); |
| 244 | ASSERT_TRUE(image_reservation_.back().get() != nullptr) << error_msg; |
| 245 | LOG(INFO) << "Reserved space for image " << |
| 246 | reinterpret_cast<void*>(image_reservation_.back()->Begin()) << "-" << |
| 247 | reinterpret_cast<void*>(image_reservation_.back()->End()); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | void DexoptTest::UnreserveImageSpace() { |
| 252 | image_reservation_.clear(); |
| 253 | } |
| 254 | |
| 255 | } // namespace art |