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