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, |
| 48 | const std::string& oat_location, |
| 49 | CompilerFilter::Filter filter, |
| 50 | bool relocate, |
| 51 | bool pic, |
| 52 | bool with_alternate_image) { |
| 53 | std::string dalvik_cache = GetDalvikCache(GetInstructionSetString(kRuntimeISA)); |
| 54 | std::string dalvik_cache_tmp = dalvik_cache + ".redirected"; |
| 55 | |
| 56 | if (!relocate) { |
| 57 | // Temporarily redirect the dalvik cache so dex2oat doesn't find the |
| 58 | // relocated image file. |
| 59 | ASSERT_EQ(0, rename(dalvik_cache.c_str(), dalvik_cache_tmp.c_str())) << strerror(errno); |
| 60 | } |
| 61 | |
| 62 | std::vector<std::string> args; |
| 63 | args.push_back("--dex-file=" + dex_location); |
| 64 | args.push_back("--oat-file=" + oat_location); |
| 65 | args.push_back("--compiler-filter=" + CompilerFilter::NameOfFilter(filter)); |
| 66 | args.push_back("--runtime-arg"); |
| 67 | |
| 68 | // Use -Xnorelocate regardless of the relocate argument. |
| 69 | // We control relocation by redirecting the dalvik cache when needed |
| 70 | // rather than use this flag. |
| 71 | args.push_back("-Xnorelocate"); |
| 72 | |
| 73 | if (pic) { |
| 74 | args.push_back("--compile-pic"); |
| 75 | } |
| 76 | |
| 77 | std::string image_location = GetImageLocation(); |
| 78 | if (with_alternate_image) { |
| 79 | args.push_back("--boot-image=" + GetImageLocation2()); |
| 80 | } |
| 81 | |
| 82 | std::string error_msg; |
| 83 | ASSERT_TRUE(OatFileAssistant::Dex2Oat(args, &error_msg)) << error_msg; |
| 84 | |
| 85 | if (!relocate) { |
| 86 | // Restore the dalvik cache if needed. |
| 87 | ASSERT_EQ(0, rename(dalvik_cache_tmp.c_str(), dalvik_cache.c_str())) << strerror(errno); |
| 88 | } |
| 89 | |
| 90 | // Verify the odex file was generated as expected. |
| 91 | std::unique_ptr<OatFile> odex_file(OatFile::Open(oat_location.c_str(), |
| 92 | oat_location.c_str(), |
| 93 | nullptr, |
| 94 | nullptr, |
| 95 | false, |
| 96 | /*low_4gb*/false, |
| 97 | dex_location.c_str(), |
| 98 | &error_msg)); |
| 99 | ASSERT_TRUE(odex_file.get() != nullptr) << error_msg; |
| 100 | EXPECT_EQ(pic, odex_file->IsPic()); |
| 101 | EXPECT_EQ(filter, odex_file->GetCompilerFilter()); |
| 102 | |
| 103 | std::unique_ptr<ImageHeader> image_header( |
| 104 | gc::space::ImageSpace::ReadImageHeader(image_location.c_str(), |
| 105 | kRuntimeISA, |
| 106 | &error_msg)); |
| 107 | ASSERT_TRUE(image_header != nullptr) << error_msg; |
| 108 | const OatHeader& oat_header = odex_file->GetOatHeader(); |
| 109 | uint32_t combined_checksum = OatFileAssistant::CalculateCombinedImageChecksum(); |
| 110 | |
| 111 | if (CompilerFilter::DependsOnImageChecksum(filter)) { |
| 112 | if (with_alternate_image) { |
| 113 | EXPECT_NE(combined_checksum, oat_header.GetImageFileLocationOatChecksum()); |
| 114 | } else { |
| 115 | EXPECT_EQ(combined_checksum, oat_header.GetImageFileLocationOatChecksum()); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | if (!with_alternate_image) { |
| 120 | if (CompilerFilter::IsBytecodeCompilationEnabled(filter)) { |
| 121 | if (relocate) { |
| 122 | EXPECT_EQ(reinterpret_cast<uintptr_t>(image_header->GetOatDataBegin()), |
| 123 | oat_header.GetImageFileLocationOatDataBegin()); |
| 124 | EXPECT_EQ(image_header->GetPatchDelta(), oat_header.GetImagePatchDelta()); |
| 125 | } else { |
| 126 | EXPECT_NE(reinterpret_cast<uintptr_t>(image_header->GetOatDataBegin()), |
| 127 | oat_header.GetImageFileLocationOatDataBegin()); |
| 128 | EXPECT_NE(image_header->GetPatchDelta(), oat_header.GetImagePatchDelta()); |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | void DexoptTest::GenerateOdexForTest(const std::string& dex_location, |
| 135 | const std::string& odex_location, |
| 136 | CompilerFilter::Filter filter) { |
| 137 | GenerateOatForTest(dex_location, |
| 138 | odex_location, |
| 139 | filter, |
| 140 | /*relocate*/false, |
| 141 | /*pic*/false, |
| 142 | /*with_alternate_image*/false); |
| 143 | } |
| 144 | |
| 145 | void DexoptTest::GeneratePicOdexForTest(const std::string& dex_location, |
| 146 | const std::string& odex_location, |
| 147 | CompilerFilter::Filter filter) { |
| 148 | GenerateOatForTest(dex_location, |
| 149 | odex_location, |
| 150 | filter, |
| 151 | /*relocate*/false, |
| 152 | /*pic*/true, |
| 153 | /*with_alternate_image*/false); |
| 154 | } |
| 155 | |
| 156 | void DexoptTest::GenerateOatForTest(const char* dex_location, |
| 157 | CompilerFilter::Filter filter, |
| 158 | bool relocate, |
| 159 | bool pic, |
| 160 | bool with_alternate_image) { |
| 161 | std::string oat_location; |
| 162 | std::string error_msg; |
| 163 | ASSERT_TRUE(OatFileAssistant::DexLocationToOatFilename( |
| 164 | dex_location, kRuntimeISA, &oat_location, &error_msg)) << error_msg; |
| 165 | GenerateOatForTest(dex_location, |
| 166 | oat_location, |
| 167 | filter, |
| 168 | relocate, |
| 169 | pic, |
| 170 | with_alternate_image); |
| 171 | } |
| 172 | |
| 173 | void DexoptTest::GenerateOatForTest(const char* dex_location, CompilerFilter::Filter filter) { |
| 174 | GenerateOatForTest(dex_location, |
| 175 | filter, |
| 176 | /*relocate*/true, |
| 177 | /*pic*/false, |
| 178 | /*with_alternate_image*/false); |
| 179 | } |
| 180 | |
| 181 | bool DexoptTest::PreRelocateImage(const std::string& image_location, std::string* error_msg) { |
| 182 | std::string image; |
| 183 | if (!GetCachedImageFile(image_location, &image, error_msg)) { |
| 184 | return false; |
| 185 | } |
| 186 | |
| 187 | std::string patchoat = GetAndroidRoot(); |
| 188 | patchoat += kIsDebugBuild ? "/bin/patchoatd" : "/bin/patchoat"; |
| 189 | |
| 190 | std::vector<std::string> argv; |
| 191 | argv.push_back(patchoat); |
| 192 | argv.push_back("--input-image-location=" + image_location); |
| 193 | argv.push_back("--output-image-file=" + image); |
| 194 | argv.push_back("--instruction-set=" + std::string(GetInstructionSetString(kRuntimeISA))); |
| 195 | argv.push_back("--base-offset-delta=0x00008000"); |
| 196 | return Exec(argv, error_msg); |
| 197 | } |
| 198 | |
| 199 | void DexoptTest::ReserveImageSpace() { |
| 200 | MemMap::Init(); |
| 201 | |
| 202 | // Ensure a chunk of memory is reserved for the image space. |
| 203 | // The reservation_end includes room for the main space that has to come |
| 204 | // right after the image in case of the GSS collector. |
| 205 | uintptr_t reservation_start = ART_BASE_ADDRESS; |
| 206 | uintptr_t reservation_end = ART_BASE_ADDRESS + 384 * MB; |
| 207 | |
| 208 | std::unique_ptr<BacktraceMap> map(BacktraceMap::Create(getpid(), true)); |
| 209 | ASSERT_TRUE(map.get() != nullptr) << "Failed to build process map"; |
| 210 | for (BacktraceMap::const_iterator it = map->begin(); |
| 211 | reservation_start < reservation_end && it != map->end(); ++it) { |
| 212 | ReserveImageSpaceChunk(reservation_start, std::min(it->start, reservation_end)); |
| 213 | reservation_start = std::max(reservation_start, it->end); |
| 214 | } |
| 215 | ReserveImageSpaceChunk(reservation_start, reservation_end); |
| 216 | } |
| 217 | |
| 218 | void DexoptTest::ReserveImageSpaceChunk(uintptr_t start, uintptr_t end) { |
| 219 | if (start < end) { |
| 220 | std::string error_msg; |
| 221 | image_reservation_.push_back(std::unique_ptr<MemMap>( |
| 222 | MemMap::MapAnonymous("image reservation", |
| 223 | reinterpret_cast<uint8_t*>(start), end - start, |
| 224 | PROT_NONE, false, false, &error_msg))); |
| 225 | ASSERT_TRUE(image_reservation_.back().get() != nullptr) << error_msg; |
| 226 | LOG(INFO) << "Reserved space for image " << |
| 227 | reinterpret_cast<void*>(image_reservation_.back()->Begin()) << "-" << |
| 228 | reinterpret_cast<void*>(image_reservation_.back()->End()); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | void DexoptTest::UnreserveImageSpace() { |
| 233 | image_reservation_.clear(); |
| 234 | } |
| 235 | |
| 236 | } // namespace art |