Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 "oat_file_assistant.h" |
| 18 | |
| 19 | #include <algorithm> |
| 20 | #include <fstream> |
| 21 | #include <string> |
| 22 | #include <vector> |
| 23 | #include <sys/param.h> |
| 24 | |
| 25 | #include <backtrace/BacktraceMap.h> |
| 26 | #include <gtest/gtest.h> |
| 27 | |
| 28 | #include "class_linker.h" |
| 29 | #include "common_runtime_test.h" |
| 30 | #include "mem_map.h" |
| 31 | #include "os.h" |
| 32 | #include "thread-inl.h" |
| 33 | #include "utils.h" |
| 34 | |
| 35 | namespace art { |
| 36 | |
| 37 | class OatFileAssistantTest : public CommonRuntimeTest { |
| 38 | public: |
| 39 | virtual void SetUp() { |
| 40 | ReserveImageSpace(); |
| 41 | CommonRuntimeTest::SetUp(); |
| 42 | |
| 43 | // Create a scratch directory to work from. |
| 44 | scratch_dir_ = android_data_ + "/OatFileAssistantTest"; |
| 45 | ASSERT_EQ(0, mkdir(scratch_dir_.c_str(), 0700)); |
| 46 | |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame^] | 47 | // Create a subdirectory in scratch for odex files. |
| 48 | odex_oat_dir_ = scratch_dir_ + "/oat"; |
| 49 | ASSERT_EQ(0, mkdir(odex_oat_dir_.c_str(), 0700)); |
| 50 | |
| 51 | odex_dir_ = odex_oat_dir_ + "/" + std::string(GetInstructionSetString(kRuntimeISA)); |
| 52 | ASSERT_EQ(0, mkdir(odex_dir_.c_str(), 0700)); |
| 53 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 54 | |
| 55 | // Verify the environment is as we expect |
| 56 | uint32_t checksum; |
| 57 | std::string error_msg; |
| 58 | ASSERT_TRUE(OS::FileExists(GetImageFile().c_str())) |
| 59 | << "Expected pre-compiled boot image to be at: " << GetImageFile(); |
| 60 | ASSERT_TRUE(OS::FileExists(GetDexSrc1().c_str())) |
| 61 | << "Expected dex file to be at: " << GetDexSrc1(); |
| 62 | ASSERT_TRUE(OS::FileExists(GetStrippedDexSrc1().c_str())) |
| 63 | << "Expected stripped dex file to be at: " << GetStrippedDexSrc1(); |
| 64 | ASSERT_FALSE(DexFile::GetChecksum(GetStrippedDexSrc1().c_str(), &checksum, &error_msg)) |
| 65 | << "Expected stripped dex file to be stripped: " << GetStrippedDexSrc1(); |
| 66 | ASSERT_TRUE(OS::FileExists(GetMultiDexSrc1().c_str())) |
| 67 | << "Expected multidex file to be at: " << GetMultiDexSrc1(); |
| 68 | ASSERT_TRUE(OS::FileExists(GetDexSrc2().c_str())) |
| 69 | << "Expected dex file to be at: " << GetDexSrc2(); |
| 70 | } |
| 71 | |
| 72 | virtual void SetUpRuntimeOptions(RuntimeOptions* options) { |
Richard Uhler | 892fc96 | 2015-03-10 16:57:05 +0000 | [diff] [blame] | 73 | // options->push_back(std::make_pair("-verbose:oat", nullptr)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 74 | |
| 75 | // Set up the image location. |
| 76 | options->push_back(std::make_pair("-Ximage:" + GetImageLocation(), |
| 77 | nullptr)); |
| 78 | // Make sure compilercallbacks are not set so that relocation will be |
| 79 | // enabled. |
| 80 | for (std::pair<std::string, const void*>& pair : *options) { |
| 81 | if (pair.first == "compilercallbacks") { |
| 82 | pair.second = nullptr; |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | virtual void PreRuntimeCreate() { |
| 88 | UnreserveImageSpace(); |
| 89 | } |
| 90 | |
| 91 | virtual void PostRuntimeCreate() { |
| 92 | ReserveImageSpace(); |
| 93 | } |
| 94 | |
| 95 | virtual void TearDown() { |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame^] | 96 | ClearDirectory(odex_dir_.c_str()); |
| 97 | ASSERT_EQ(0, rmdir(odex_dir_.c_str())); |
| 98 | |
| 99 | ClearDirectory(odex_oat_dir_.c_str()); |
| 100 | ASSERT_EQ(0, rmdir(odex_oat_dir_.c_str())); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 101 | |
| 102 | ClearDirectory(scratch_dir_.c_str()); |
| 103 | ASSERT_EQ(0, rmdir(scratch_dir_.c_str())); |
| 104 | |
| 105 | CommonRuntimeTest::TearDown(); |
| 106 | } |
| 107 | |
| 108 | void Copy(std::string src, std::string dst) { |
| 109 | std::ifstream src_stream(src, std::ios::binary); |
| 110 | std::ofstream dst_stream(dst, std::ios::binary); |
| 111 | |
| 112 | dst_stream << src_stream.rdbuf(); |
| 113 | } |
| 114 | |
| 115 | // Returns the directory where the pre-compiled core.art can be found. |
| 116 | // TODO: We should factor out this into common tests somewhere rather than |
| 117 | // re-hardcoding it here (This was copied originally from the elf writer |
| 118 | // test). |
| 119 | std::string GetImageDirectory() { |
| 120 | if (IsHost()) { |
| 121 | const char* host_dir = getenv("ANDROID_HOST_OUT"); |
| 122 | CHECK(host_dir != NULL); |
| 123 | return std::string(host_dir) + "/framework"; |
| 124 | } else { |
| 125 | return std::string("/data/art-test"); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | std::string GetImageLocation() { |
| 130 | return GetImageDirectory() + "/core.art"; |
| 131 | } |
| 132 | |
| 133 | std::string GetImageFile() { |
| 134 | return GetImageDirectory() + "/" + GetInstructionSetString(kRuntimeISA) |
| 135 | + "/core.art"; |
| 136 | } |
| 137 | |
| 138 | std::string GetDexSrc1() { |
| 139 | return GetTestDexFileName("Main"); |
| 140 | } |
| 141 | |
| 142 | // Returns the path to a dex file equivalent to GetDexSrc1, but with the dex |
| 143 | // file stripped. |
| 144 | std::string GetStrippedDexSrc1() { |
| 145 | return GetTestDexFileName("MainStripped"); |
| 146 | } |
| 147 | |
| 148 | std::string GetMultiDexSrc1() { |
| 149 | return GetTestDexFileName("MultiDex"); |
| 150 | } |
| 151 | |
| 152 | std::string GetDexSrc2() { |
| 153 | return GetTestDexFileName("Nested"); |
| 154 | } |
| 155 | |
| 156 | // Scratch directory, for dex and odex files (oat files will go in the |
| 157 | // dalvik cache). |
| 158 | std::string GetScratchDir() { |
| 159 | return scratch_dir_; |
| 160 | } |
| 161 | |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame^] | 162 | // Odex directory is the subdirectory in the scratch directory where odex |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 163 | // files should be located. |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame^] | 164 | std::string GetOdexDir() { |
| 165 | return odex_dir_; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | // Generate an odex file for the purposes of test. |
| 169 | // If pic is true, generates a PIC odex. |
| 170 | void GenerateOdexForTest(const std::string& dex_location, |
| 171 | const std::string& odex_location, |
| 172 | bool pic = false) { |
| 173 | // For this operation, we temporarily redirect the dalvik cache so dex2oat |
| 174 | // doesn't find the relocated image file. |
| 175 | std::string android_data_tmp = GetScratchDir() + "AndroidDataTmp"; |
| 176 | setenv("ANDROID_DATA", android_data_tmp.c_str(), 1); |
| 177 | std::vector<std::string> args; |
| 178 | args.push_back("--dex-file=" + dex_location); |
| 179 | args.push_back("--oat-file=" + odex_location); |
| 180 | if (pic) { |
| 181 | args.push_back("--compile-pic"); |
| 182 | } else { |
| 183 | args.push_back("--include-patch-information"); |
Richard Uhler | 05dd8a6 | 2015-03-10 10:02:23 -0700 | [diff] [blame] | 184 | |
| 185 | // We need to use the quick compiler to generate non-PIC code, because |
| 186 | // the optimizing compiler always generates PIC. |
| 187 | args.push_back("--compiler-backend=Quick"); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 188 | } |
| 189 | args.push_back("--runtime-arg"); |
| 190 | args.push_back("-Xnorelocate"); |
| 191 | std::string error_msg; |
| 192 | ASSERT_TRUE(OatFileAssistant::Dex2Oat(args, &error_msg)) << error_msg; |
| 193 | setenv("ANDROID_DATA", android_data_.c_str(), 1); |
| 194 | } |
| 195 | |
| 196 | void GeneratePicOdexForTest(const std::string& dex_location, |
| 197 | const std::string& odex_location) { |
| 198 | GenerateOdexForTest(dex_location, odex_location, true); |
| 199 | } |
| 200 | |
| 201 | private: |
| 202 | // Reserve memory around where the image will be loaded so other memory |
| 203 | // won't conflict when it comes time to load the image. |
| 204 | // This can be called with an already loaded image to reserve the space |
| 205 | // around it. |
| 206 | void ReserveImageSpace() { |
| 207 | MemMap::Init(); |
| 208 | |
| 209 | // Ensure a chunk of memory is reserved for the image space. |
| 210 | uintptr_t reservation_start = ART_BASE_ADDRESS + ART_BASE_ADDRESS_MIN_DELTA; |
| 211 | uintptr_t reservation_end = ART_BASE_ADDRESS + ART_BASE_ADDRESS_MAX_DELTA |
Hiroshi Yamauchi | 3dbf234 | 2015-03-17 16:01:11 -0700 | [diff] [blame] | 212 | // Include the main space that has to come right after the |
| 213 | // image in case of the GSS collector. |
| 214 | + 384 * MB; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 215 | |
| 216 | std::string error_msg; |
| 217 | std::unique_ptr<BacktraceMap> map(BacktraceMap::Create(getpid(), true)); |
| 218 | ASSERT_TRUE(map.get() != nullptr) << "Failed to build process map"; |
| 219 | for (BacktraceMap::const_iterator it = map->begin(); |
| 220 | reservation_start < reservation_end && it != map->end(); ++it) { |
| 221 | if (it->end <= reservation_start) { |
| 222 | continue; |
| 223 | } |
| 224 | |
| 225 | if (it->start < reservation_start) { |
| 226 | reservation_start = std::min(reservation_end, it->end); |
| 227 | } |
| 228 | |
| 229 | image_reservation_.push_back(std::unique_ptr<MemMap>( |
| 230 | MemMap::MapAnonymous("image reservation", |
| 231 | reinterpret_cast<uint8_t*>(reservation_start), |
| 232 | std::min(it->start, reservation_end) - reservation_start, |
| 233 | PROT_NONE, false, false, &error_msg))); |
| 234 | ASSERT_TRUE(image_reservation_.back().get() != nullptr) << error_msg; |
| 235 | LOG(INFO) << "Reserved space for image " << |
| 236 | reinterpret_cast<void*>(image_reservation_.back()->Begin()) << "-" << |
| 237 | reinterpret_cast<void*>(image_reservation_.back()->End()); |
| 238 | reservation_start = it->end; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | |
| 243 | // Unreserve any memory reserved by ReserveImageSpace. This should be called |
| 244 | // before the image is loaded. |
| 245 | void UnreserveImageSpace() { |
| 246 | image_reservation_.clear(); |
| 247 | } |
| 248 | |
| 249 | std::string scratch_dir_; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame^] | 250 | std::string odex_oat_dir_; |
| 251 | std::string odex_dir_; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 252 | std::vector<std::unique_ptr<MemMap>> image_reservation_; |
| 253 | }; |
| 254 | |
| 255 | class OatFileAssistantNoDex2OatTest : public OatFileAssistantTest { |
| 256 | public: |
| 257 | virtual void SetUpRuntimeOptions(RuntimeOptions* options) { |
| 258 | OatFileAssistantTest::SetUpRuntimeOptions(options); |
| 259 | options->push_back(std::make_pair("-Xnodex2oat", nullptr)); |
| 260 | } |
| 261 | }; |
| 262 | |
| 263 | // Generate an oat file for the purposes of test, as opposed to testing |
| 264 | // generation of oat files. |
| 265 | static void GenerateOatForTest(const char* dex_location) { |
| 266 | OatFileAssistant oat_file_assistant(dex_location, kRuntimeISA, false); |
| 267 | |
| 268 | std::string error_msg; |
| 269 | ASSERT_TRUE(oat_file_assistant.GenerateOatFile(&error_msg)) << error_msg; |
| 270 | } |
| 271 | |
| 272 | // Case: We have a DEX file, but no OAT file for it. |
| 273 | // Expect: The oat file status is kOutOfDate. |
| 274 | TEST_F(OatFileAssistantTest, DexNoOat) { |
| 275 | std::string dex_location = GetScratchDir() + "/DexNoOat.jar"; |
| 276 | Copy(GetDexSrc1(), dex_location); |
| 277 | |
| 278 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false); |
| 279 | |
| 280 | EXPECT_EQ(OatFileAssistant::kOutOfDate, oat_file_assistant.GetStatus()); |
| 281 | |
| 282 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 283 | EXPECT_FALSE(oat_file_assistant.OdexFileExists()); |
| 284 | EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 285 | EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation()); |
| 286 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 287 | EXPECT_EQ(OatFileAssistant::kOutOfDate, oat_file_assistant.OdexFileStatus()); |
| 288 | EXPECT_FALSE(oat_file_assistant.OatFileExists()); |
| 289 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 290 | EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation()); |
| 291 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
| 292 | EXPECT_EQ(OatFileAssistant::kOutOfDate, oat_file_assistant.OatFileStatus()); |
| 293 | } |
| 294 | |
| 295 | // Case: We have no DEX file and no OAT file. |
| 296 | // Expect: Status is out of date. Loading should fail, but not crash. |
| 297 | TEST_F(OatFileAssistantTest, NoDexNoOat) { |
| 298 | std::string dex_location = GetScratchDir() + "/NoDexNoOat.jar"; |
| 299 | |
| 300 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true); |
| 301 | |
| 302 | EXPECT_EQ(OatFileAssistant::kOutOfDate, oat_file_assistant.GetStatus()); |
| 303 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 304 | EXPECT_EQ(nullptr, oat_file.get()); |
| 305 | } |
| 306 | |
| 307 | // Case: We have a DEX file and up-to-date OAT file for it. |
| 308 | // Expect: The oat file status is kUpToDate. |
| 309 | TEST_F(OatFileAssistantTest, OatUpToDate) { |
| 310 | std::string dex_location = GetScratchDir() + "/OatUpToDate.jar"; |
| 311 | Copy(GetDexSrc1(), dex_location); |
| 312 | GenerateOatForTest(dex_location.c_str()); |
| 313 | |
| 314 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false); |
| 315 | |
| 316 | EXPECT_EQ(OatFileAssistant::kUpToDate, oat_file_assistant.GetStatus()); |
| 317 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 318 | EXPECT_FALSE(oat_file_assistant.OdexFileExists()); |
| 319 | EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 320 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 321 | EXPECT_TRUE(oat_file_assistant.OatFileExists()); |
| 322 | EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate()); |
| 323 | EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation()); |
| 324 | EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate()); |
| 325 | EXPECT_EQ(OatFileAssistant::kUpToDate, oat_file_assistant.OatFileStatus()); |
| 326 | } |
| 327 | |
| 328 | // Case: We have a MultiDEX file and up-to-date OAT file for it. |
| 329 | // Expect: The oat file status is kUpToDate. |
| 330 | TEST_F(OatFileAssistantTest, MultiDexOatUpToDate) { |
| 331 | std::string dex_location = GetScratchDir() + "/MultiDexOatUpToDate.jar"; |
| 332 | Copy(GetMultiDexSrc1(), dex_location); |
| 333 | GenerateOatForTest(dex_location.c_str()); |
| 334 | |
| 335 | // Verify we can load both dex files. |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 336 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true); |
| 337 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 338 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 339 | EXPECT_TRUE(oat_file->IsExecutable()); |
| 340 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 341 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 342 | EXPECT_EQ(2u, dex_files.size()); |
| 343 | } |
| 344 | |
| 345 | // Case: We have a MultiDEX file and up-to-date OAT file for it with relative |
| 346 | // encoded dex locations. |
| 347 | // Expect: The oat file status is kUpToDate. |
| 348 | TEST_F(OatFileAssistantTest, RelativeEncodedDexLocation) { |
| 349 | std::string dex_location = GetScratchDir() + "/RelativeEncodedDexLocation.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame^] | 350 | std::string oat_location = GetOdexDir() + "/RelativeEncodedDexLocation.oat"; |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 351 | |
| 352 | // Create the dex file |
| 353 | Copy(GetMultiDexSrc1(), dex_location); |
| 354 | |
| 355 | // Create the oat file with relative encoded dex location. |
| 356 | std::vector<std::string> args; |
| 357 | args.push_back("--dex-file=" + dex_location); |
| 358 | args.push_back("--dex-location=" + std::string("RelativeEncodedDexLocation.jar")); |
| 359 | args.push_back("--oat-file=" + oat_location); |
| 360 | |
| 361 | std::string error_msg; |
| 362 | ASSERT_TRUE(OatFileAssistant::Dex2Oat(args, &error_msg)) << error_msg; |
| 363 | |
| 364 | // Verify we can load both dex files. |
| 365 | OatFileAssistant oat_file_assistant(dex_location.c_str(), |
| 366 | oat_location.c_str(), |
| 367 | kRuntimeISA, true); |
| 368 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 369 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 370 | EXPECT_TRUE(oat_file->IsExecutable()); |
| 371 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 372 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 373 | EXPECT_EQ(2u, dex_files.size()); |
| 374 | } |
| 375 | |
| 376 | // Case: We have a DEX file and out of date OAT file. |
| 377 | // Expect: The oat file status is kOutOfDate. |
| 378 | TEST_F(OatFileAssistantTest, OatOutOfDate) { |
| 379 | std::string dex_location = GetScratchDir() + "/OatOutOfDate.jar"; |
| 380 | |
| 381 | // We create a dex, generate an oat for it, then overwrite the dex with a |
| 382 | // different dex to make the oat out of date. |
| 383 | Copy(GetDexSrc1(), dex_location); |
| 384 | GenerateOatForTest(dex_location.c_str()); |
| 385 | Copy(GetDexSrc2(), dex_location); |
| 386 | |
| 387 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false); |
| 388 | EXPECT_EQ(OatFileAssistant::kOutOfDate, oat_file_assistant.GetStatus()); |
| 389 | |
| 390 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 391 | EXPECT_FALSE(oat_file_assistant.OdexFileExists()); |
| 392 | EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 393 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 394 | EXPECT_TRUE(oat_file_assistant.OatFileExists()); |
| 395 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 396 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
| 397 | } |
| 398 | |
| 399 | // Case: We have a DEX file and an ODEX file, but no OAT file. |
| 400 | // Expect: The oat file status is kNeedsRelocation. |
| 401 | TEST_F(OatFileAssistantTest, DexOdexNoOat) { |
| 402 | std::string dex_location = GetScratchDir() + "/DexOdexNoOat.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame^] | 403 | std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 404 | |
| 405 | // Create the dex and odex files |
| 406 | Copy(GetDexSrc1(), dex_location); |
| 407 | GenerateOdexForTest(dex_location, odex_location); |
| 408 | |
| 409 | // Verify the status. |
| 410 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false); |
| 411 | |
| 412 | EXPECT_EQ(OatFileAssistant::kNeedsRelocation, oat_file_assistant.GetStatus()); |
| 413 | |
| 414 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 415 | EXPECT_TRUE(oat_file_assistant.OdexFileExists()); |
| 416 | EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 417 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 418 | EXPECT_TRUE(oat_file_assistant.OdexFileNeedsRelocation()); |
| 419 | EXPECT_EQ(OatFileAssistant::kNeedsRelocation, oat_file_assistant.OdexFileNeedsRelocation()); |
| 420 | EXPECT_FALSE(oat_file_assistant.OatFileExists()); |
| 421 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 422 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
| 423 | } |
| 424 | |
| 425 | // Case: We have a stripped DEX file and an ODEX file, but no OAT file. |
| 426 | // Expect: The oat file status is kNeedsRelocation. |
| 427 | TEST_F(OatFileAssistantTest, StrippedDexOdexNoOat) { |
| 428 | std::string dex_location = GetScratchDir() + "/StrippedDexOdexNoOat.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame^] | 429 | std::string odex_location = GetOdexDir() + "/StrippedDexOdexNoOat.odex"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 430 | |
| 431 | // Create the dex and odex files |
| 432 | Copy(GetDexSrc1(), dex_location); |
| 433 | GenerateOdexForTest(dex_location, odex_location); |
| 434 | |
| 435 | // Strip the dex file |
| 436 | Copy(GetStrippedDexSrc1(), dex_location); |
| 437 | |
| 438 | // Verify the status. |
| 439 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true); |
| 440 | |
| 441 | EXPECT_EQ(OatFileAssistant::kNeedsRelocation, oat_file_assistant.GetStatus()); |
| 442 | |
| 443 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 444 | EXPECT_TRUE(oat_file_assistant.OdexFileExists()); |
| 445 | EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 446 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 447 | EXPECT_FALSE(oat_file_assistant.OatFileExists()); |
| 448 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 449 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
| 450 | |
| 451 | // Make the oat file up to date. |
| 452 | std::string error_msg; |
| 453 | ASSERT_TRUE(oat_file_assistant.MakeUpToDate(&error_msg)) << error_msg; |
| 454 | |
| 455 | EXPECT_EQ(OatFileAssistant::kUpToDate, oat_file_assistant.GetStatus()); |
| 456 | |
| 457 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 458 | EXPECT_TRUE(oat_file_assistant.OdexFileExists()); |
| 459 | EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 460 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 461 | EXPECT_TRUE(oat_file_assistant.OatFileExists()); |
| 462 | EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate()); |
| 463 | EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate()); |
| 464 | |
| 465 | // Verify we can load the dex files from it. |
| 466 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 467 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 468 | EXPECT_TRUE(oat_file->IsExecutable()); |
| 469 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 470 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 471 | EXPECT_EQ(1u, dex_files.size()); |
| 472 | } |
| 473 | |
| 474 | // Case: We have a stripped DEX file, an ODEX file, and an out of date OAT file. |
| 475 | // Expect: The oat file status is kNeedsRelocation. |
| 476 | TEST_F(OatFileAssistantTest, StrippedDexOdexOat) { |
| 477 | std::string dex_location = GetScratchDir() + "/StrippedDexOdexOat.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame^] | 478 | std::string odex_location = GetOdexDir() + "/StrippedDexOdexOat.odex"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 479 | |
| 480 | // Create the oat file from a different dex file so it looks out of date. |
| 481 | Copy(GetDexSrc2(), dex_location); |
| 482 | GenerateOatForTest(dex_location.c_str()); |
| 483 | |
| 484 | // Create the odex file |
| 485 | Copy(GetDexSrc1(), dex_location); |
| 486 | GenerateOdexForTest(dex_location, odex_location); |
| 487 | |
| 488 | // Strip the dex file. |
| 489 | Copy(GetStrippedDexSrc1(), dex_location); |
| 490 | |
| 491 | // Verify the status. |
| 492 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true); |
| 493 | |
| 494 | EXPECT_EQ(OatFileAssistant::kNeedsRelocation, oat_file_assistant.GetStatus()); |
| 495 | |
| 496 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 497 | EXPECT_TRUE(oat_file_assistant.OdexFileExists()); |
| 498 | EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 499 | EXPECT_TRUE(oat_file_assistant.OdexFileNeedsRelocation()); |
| 500 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 501 | EXPECT_TRUE(oat_file_assistant.OatFileExists()); |
| 502 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 503 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
| 504 | |
| 505 | // Make the oat file up to date. |
| 506 | std::string error_msg; |
| 507 | ASSERT_TRUE(oat_file_assistant.MakeUpToDate(&error_msg)) << error_msg; |
| 508 | |
| 509 | EXPECT_EQ(OatFileAssistant::kUpToDate, oat_file_assistant.GetStatus()); |
| 510 | |
| 511 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 512 | EXPECT_TRUE(oat_file_assistant.OdexFileExists()); |
| 513 | EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 514 | EXPECT_TRUE(oat_file_assistant.OdexFileNeedsRelocation()); |
| 515 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 516 | EXPECT_TRUE(oat_file_assistant.OatFileExists()); |
| 517 | EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate()); |
| 518 | EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation()); |
| 519 | EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate()); |
| 520 | |
| 521 | // Verify we can load the dex files from it. |
| 522 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 523 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 524 | EXPECT_TRUE(oat_file->IsExecutable()); |
| 525 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 526 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 527 | EXPECT_EQ(1u, dex_files.size()); |
| 528 | } |
| 529 | |
| 530 | // Case: We have a DEX file, an ODEX file and an OAT file, where the ODEX and |
| 531 | // OAT files both have patch delta of 0. |
| 532 | // Expect: It shouldn't crash. |
| 533 | TEST_F(OatFileAssistantTest, OdexOatOverlap) { |
| 534 | std::string dex_location = GetScratchDir() + "/OdexOatOverlap.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame^] | 535 | std::string odex_location = GetOdexDir() + "/OdexOatOverlap.odex"; |
| 536 | std::string oat_location = GetOdexDir() + "/OdexOatOverlap.oat"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 537 | |
| 538 | // Create the dex and odex files |
| 539 | Copy(GetDexSrc1(), dex_location); |
| 540 | GenerateOdexForTest(dex_location, odex_location); |
| 541 | |
| 542 | // Create the oat file by copying the odex so they are located in the same |
| 543 | // place in memory. |
| 544 | Copy(odex_location, oat_location); |
| 545 | |
| 546 | // Verify things don't go bad. |
| 547 | OatFileAssistant oat_file_assistant(dex_location.c_str(), |
| 548 | oat_location.c_str(), kRuntimeISA, true); |
| 549 | |
| 550 | EXPECT_EQ(OatFileAssistant::kNeedsRelocation, oat_file_assistant.GetStatus()); |
| 551 | |
| 552 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 553 | EXPECT_TRUE(oat_file_assistant.OdexFileExists()); |
| 554 | EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 555 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 556 | EXPECT_TRUE(oat_file_assistant.OatFileExists()); |
| 557 | EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate()); |
| 558 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
| 559 | |
| 560 | // Things aren't relocated, so it should fall back to interpreted. |
| 561 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 562 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 563 | EXPECT_FALSE(oat_file->IsExecutable()); |
| 564 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 565 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 566 | EXPECT_EQ(1u, dex_files.size()); |
| 567 | } |
| 568 | |
| 569 | // Case: We have a DEX file and a PIC ODEX file, but no OAT file. |
| 570 | // Expect: The oat file status is kUpToDate, because PIC needs no relocation. |
| 571 | TEST_F(OatFileAssistantTest, DexPicOdexNoOat) { |
| 572 | std::string dex_location = GetScratchDir() + "/DexPicOdexNoOat.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame^] | 573 | std::string odex_location = GetOdexDir() + "/DexPicOdexNoOat.odex"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 574 | |
| 575 | // Create the dex and odex files |
| 576 | Copy(GetDexSrc1(), dex_location); |
| 577 | GeneratePicOdexForTest(dex_location, odex_location); |
| 578 | |
| 579 | // Verify the status. |
| 580 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false); |
| 581 | |
| 582 | EXPECT_EQ(OatFileAssistant::kUpToDate, oat_file_assistant.GetStatus()); |
| 583 | |
| 584 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 585 | EXPECT_TRUE(oat_file_assistant.OdexFileExists()); |
| 586 | EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 587 | EXPECT_TRUE(oat_file_assistant.OdexFileIsUpToDate()); |
| 588 | EXPECT_FALSE(oat_file_assistant.OatFileExists()); |
| 589 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 590 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
| 591 | } |
| 592 | |
| 593 | // Case: We have a DEX file and up-to-date OAT file for it. |
| 594 | // Expect: We should load an executable dex file. |
| 595 | TEST_F(OatFileAssistantTest, LoadOatUpToDate) { |
| 596 | std::string dex_location = GetScratchDir() + "/LoadOatUpToDate.jar"; |
| 597 | |
| 598 | Copy(GetDexSrc1(), dex_location); |
| 599 | GenerateOatForTest(dex_location.c_str()); |
| 600 | |
| 601 | // Load the oat using an oat file assistant. |
| 602 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true); |
| 603 | |
| 604 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 605 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 606 | EXPECT_TRUE(oat_file->IsExecutable()); |
| 607 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 608 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 609 | EXPECT_EQ(1u, dex_files.size()); |
| 610 | } |
| 611 | |
| 612 | // Case: We have a DEX file and up-to-date OAT file for it. |
| 613 | // Expect: Loading non-executable should load the oat non-executable. |
| 614 | TEST_F(OatFileAssistantTest, LoadNoExecOatUpToDate) { |
| 615 | std::string dex_location = GetScratchDir() + "/LoadNoExecOatUpToDate.jar"; |
| 616 | |
| 617 | Copy(GetDexSrc1(), dex_location); |
| 618 | GenerateOatForTest(dex_location.c_str()); |
| 619 | |
| 620 | // Load the oat using an oat file assistant. |
| 621 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false); |
| 622 | |
| 623 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 624 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 625 | EXPECT_FALSE(oat_file->IsExecutable()); |
| 626 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 627 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 628 | EXPECT_EQ(1u, dex_files.size()); |
| 629 | } |
| 630 | |
| 631 | // Case: We have a DEX file. |
| 632 | // Expect: We should load an executable dex file from an alternative oat |
| 633 | // location. |
| 634 | TEST_F(OatFileAssistantTest, LoadDexNoAlternateOat) { |
| 635 | std::string dex_location = GetScratchDir() + "/LoadDexNoAlternateOat.jar"; |
| 636 | std::string oat_location = GetScratchDir() + "/LoadDexNoAlternateOat.oat"; |
| 637 | |
| 638 | Copy(GetDexSrc1(), dex_location); |
| 639 | |
| 640 | OatFileAssistant oat_file_assistant( |
| 641 | dex_location.c_str(), oat_location.c_str(), kRuntimeISA, true); |
| 642 | std::string error_msg; |
| 643 | ASSERT_TRUE(oat_file_assistant.MakeUpToDate(&error_msg)) << error_msg; |
| 644 | |
| 645 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 646 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 647 | EXPECT_TRUE(oat_file->IsExecutable()); |
| 648 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 649 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 650 | EXPECT_EQ(1u, dex_files.size()); |
| 651 | |
| 652 | EXPECT_TRUE(OS::FileExists(oat_location.c_str())); |
| 653 | |
| 654 | // Verify it didn't create an oat in the default location. |
| 655 | OatFileAssistant ofm(dex_location.c_str(), kRuntimeISA, false); |
| 656 | EXPECT_FALSE(ofm.OatFileExists()); |
| 657 | } |
| 658 | |
| 659 | // Case: Non-existent Dex location. |
| 660 | // Expect: The dex code is out of date, and trying to update it fails. |
| 661 | TEST_F(OatFileAssistantTest, NonExsistentDexLocation) { |
| 662 | std::string dex_location = GetScratchDir() + "/BadDexLocation.jar"; |
| 663 | |
| 664 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true); |
| 665 | |
| 666 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 667 | EXPECT_EQ(OatFileAssistant::kOutOfDate, oat_file_assistant.GetStatus()); |
| 668 | EXPECT_FALSE(oat_file_assistant.OdexFileExists()); |
| 669 | EXPECT_FALSE(oat_file_assistant.OatFileExists()); |
| 670 | EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 671 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 672 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 673 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
| 674 | |
| 675 | std::string error_msg; |
| 676 | EXPECT_FALSE(oat_file_assistant.MakeUpToDate(&error_msg)); |
| 677 | EXPECT_FALSE(error_msg.empty()); |
| 678 | } |
| 679 | |
| 680 | // Turn an absolute path into a path relative to the current working |
| 681 | // directory. |
| 682 | static std::string MakePathRelative(std::string target) { |
| 683 | char buf[MAXPATHLEN]; |
| 684 | std::string cwd = getcwd(buf, MAXPATHLEN); |
| 685 | |
| 686 | // Split the target and cwd paths into components. |
| 687 | std::vector<std::string> target_path; |
| 688 | std::vector<std::string> cwd_path; |
| 689 | Split(target, '/', &target_path); |
| 690 | Split(cwd, '/', &cwd_path); |
| 691 | |
| 692 | // Reverse the path components, so we can use pop_back(). |
| 693 | std::reverse(target_path.begin(), target_path.end()); |
| 694 | std::reverse(cwd_path.begin(), cwd_path.end()); |
| 695 | |
| 696 | // Drop the common prefix of the paths. Because we reversed the path |
| 697 | // components, this becomes the common suffix of target_path and cwd_path. |
| 698 | while (!target_path.empty() && !cwd_path.empty() |
| 699 | && target_path.back() == cwd_path.back()) { |
| 700 | target_path.pop_back(); |
| 701 | cwd_path.pop_back(); |
| 702 | } |
| 703 | |
| 704 | // For each element of the remaining cwd_path, add '..' to the beginning |
| 705 | // of the target path. Because we reversed the path components, we add to |
| 706 | // the end of target_path. |
| 707 | for (unsigned int i = 0; i < cwd_path.size(); i++) { |
| 708 | target_path.push_back(".."); |
| 709 | } |
| 710 | |
| 711 | // Reverse again to get the right path order, and join to get the result. |
| 712 | std::reverse(target_path.begin(), target_path.end()); |
| 713 | return Join(target_path, '/'); |
| 714 | } |
| 715 | |
| 716 | // Case: Non-absolute path to Dex location. |
| 717 | // Expect: Not sure, but it shouldn't crash. |
| 718 | TEST_F(OatFileAssistantTest, NonAbsoluteDexLocation) { |
| 719 | std::string abs_dex_location = GetScratchDir() + "/NonAbsoluteDexLocation.jar"; |
| 720 | Copy(GetDexSrc1(), abs_dex_location); |
| 721 | |
| 722 | std::string dex_location = MakePathRelative(abs_dex_location); |
| 723 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true); |
| 724 | |
| 725 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 726 | EXPECT_EQ(OatFileAssistant::kOutOfDate, oat_file_assistant.GetStatus()); |
| 727 | EXPECT_FALSE(oat_file_assistant.OdexFileExists()); |
| 728 | EXPECT_FALSE(oat_file_assistant.OatFileExists()); |
| 729 | EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 730 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 731 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 732 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
| 733 | } |
| 734 | |
| 735 | // Case: Very short, non-existent Dex location. |
| 736 | // Expect: Dex code is out of date, and trying to update it fails. |
| 737 | TEST_F(OatFileAssistantTest, ShortDexLocation) { |
| 738 | std::string dex_location = "/xx"; |
| 739 | |
| 740 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true); |
| 741 | |
| 742 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 743 | EXPECT_EQ(OatFileAssistant::kOutOfDate, oat_file_assistant.GetStatus()); |
| 744 | EXPECT_FALSE(oat_file_assistant.OdexFileExists()); |
| 745 | EXPECT_FALSE(oat_file_assistant.OatFileExists()); |
| 746 | EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 747 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 748 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 749 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
| 750 | |
| 751 | std::string error_msg; |
| 752 | EXPECT_FALSE(oat_file_assistant.MakeUpToDate(&error_msg)); |
| 753 | EXPECT_FALSE(error_msg.empty()); |
| 754 | } |
| 755 | |
| 756 | // Case: Non-standard extension for dex file. |
| 757 | // Expect: The oat file status is kOutOfDate. |
| 758 | TEST_F(OatFileAssistantTest, LongDexExtension) { |
| 759 | std::string dex_location = GetScratchDir() + "/LongDexExtension.jarx"; |
| 760 | Copy(GetDexSrc1(), dex_location); |
| 761 | |
| 762 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false); |
| 763 | |
| 764 | EXPECT_EQ(OatFileAssistant::kOutOfDate, oat_file_assistant.GetStatus()); |
| 765 | |
| 766 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 767 | EXPECT_FALSE(oat_file_assistant.OdexFileExists()); |
| 768 | EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 769 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 770 | EXPECT_FALSE(oat_file_assistant.OatFileExists()); |
| 771 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 772 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
| 773 | } |
| 774 | |
| 775 | // A task to generate a dex location. Used by the RaceToGenerate test. |
| 776 | class RaceGenerateTask : public Task { |
| 777 | public: |
| 778 | explicit RaceGenerateTask(const std::string& dex_location, const std::string& oat_location) |
| 779 | : dex_location_(dex_location), oat_location_(oat_location), |
| 780 | loaded_oat_file_(nullptr) |
| 781 | {} |
| 782 | |
| 783 | void Run(Thread* self) { |
| 784 | UNUSED(self); |
| 785 | |
| 786 | // Load the dex files, and save a pointer to the loaded oat file, so that |
| 787 | // we can verify only one oat file was loaded for the dex location. |
| 788 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
| 789 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 790 | std::vector<std::string> error_msgs; |
| 791 | dex_files = linker->OpenDexFilesFromOat(dex_location_.c_str(), oat_location_.c_str(), &error_msgs); |
| 792 | CHECK(!dex_files.empty()) << Join(error_msgs, '\n'); |
| 793 | loaded_oat_file_ = dex_files[0]->GetOatFile(); |
| 794 | } |
| 795 | |
| 796 | const OatFile* GetLoadedOatFile() const { |
| 797 | return loaded_oat_file_; |
| 798 | } |
| 799 | |
| 800 | private: |
| 801 | std::string dex_location_; |
| 802 | std::string oat_location_; |
| 803 | const OatFile* loaded_oat_file_; |
| 804 | }; |
| 805 | |
| 806 | // Test the case where multiple processes race to generate an oat file. |
| 807 | // This simulates multiple processes using multiple threads. |
| 808 | // |
| 809 | // We want only one Oat file to be loaded when there is a race to load, to |
| 810 | // avoid using up the virtual memory address space. |
| 811 | TEST_F(OatFileAssistantTest, RaceToGenerate) { |
| 812 | std::string dex_location = GetScratchDir() + "/RaceToGenerate.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame^] | 813 | std::string oat_location = GetOdexDir() + "/RaceToGenerate.oat"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 814 | |
| 815 | // We use the lib core dex file, because it's large, and hopefully should |
| 816 | // take a while to generate. |
| 817 | Copy(GetLibCoreDexFileName(), dex_location); |
| 818 | |
| 819 | const int kNumThreads = 32; |
| 820 | Thread* self = Thread::Current(); |
| 821 | ThreadPool thread_pool("Oat file assistant test thread pool", kNumThreads); |
| 822 | std::vector<std::unique_ptr<RaceGenerateTask>> tasks; |
| 823 | for (int i = 0; i < kNumThreads; i++) { |
| 824 | std::unique_ptr<RaceGenerateTask> task(new RaceGenerateTask(dex_location, oat_location)); |
| 825 | thread_pool.AddTask(self, task.get()); |
| 826 | tasks.push_back(std::move(task)); |
| 827 | } |
| 828 | thread_pool.StartWorkers(self); |
| 829 | thread_pool.Wait(self, true, false); |
| 830 | |
| 831 | // Verify every task got the same pointer. |
| 832 | const OatFile* expected = tasks[0]->GetLoadedOatFile(); |
| 833 | for (auto& task : tasks) { |
| 834 | EXPECT_EQ(expected, task->GetLoadedOatFile()); |
| 835 | } |
| 836 | } |
| 837 | |
| 838 | // Case: We have a DEX file and an ODEX file, no OAT file, and dex2oat is |
| 839 | // disabled. |
| 840 | // Expect: We should load the odex file non-executable. |
| 841 | TEST_F(OatFileAssistantNoDex2OatTest, LoadDexOdexNoOat) { |
| 842 | std::string dex_location = GetScratchDir() + "/LoadDexOdexNoOat.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame^] | 843 | std::string odex_location = GetOdexDir() + "/LoadDexOdexNoOat.odex"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 844 | |
| 845 | // Create the dex and odex files |
| 846 | Copy(GetDexSrc1(), dex_location); |
| 847 | GenerateOdexForTest(dex_location, odex_location); |
| 848 | |
| 849 | // Load the oat using an executable oat file assistant. |
| 850 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true); |
| 851 | |
| 852 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 853 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 854 | EXPECT_FALSE(oat_file->IsExecutable()); |
| 855 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 856 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 857 | EXPECT_EQ(1u, dex_files.size()); |
| 858 | } |
| 859 | |
| 860 | // Case: We have a MultiDEX file and an ODEX file, no OAT file, and dex2oat is |
| 861 | // disabled. |
| 862 | // Expect: We should load the odex file non-executable. |
| 863 | TEST_F(OatFileAssistantNoDex2OatTest, LoadMultiDexOdexNoOat) { |
| 864 | std::string dex_location = GetScratchDir() + "/LoadMultiDexOdexNoOat.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame^] | 865 | std::string odex_location = GetOdexDir() + "/LoadMultiDexOdexNoOat.odex"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 866 | |
| 867 | // Create the dex and odex files |
| 868 | Copy(GetMultiDexSrc1(), dex_location); |
| 869 | GenerateOdexForTest(dex_location, odex_location); |
| 870 | |
| 871 | // Load the oat using an executable oat file assistant. |
| 872 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true); |
| 873 | |
| 874 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 875 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 876 | EXPECT_FALSE(oat_file->IsExecutable()); |
| 877 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 878 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 879 | EXPECT_EQ(2u, dex_files.size()); |
| 880 | } |
| 881 | |
| 882 | TEST(OatFileAssistantUtilsTest, DexFilenameToOdexFilename) { |
| 883 | std::string error_msg; |
| 884 | std::string odex_file; |
| 885 | |
| 886 | EXPECT_TRUE(OatFileAssistant::DexFilenameToOdexFilename( |
| 887 | "/foo/bar/baz.jar", kArm, &odex_file, &error_msg)) << error_msg; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame^] | 888 | EXPECT_EQ("/foo/bar/oat/arm/baz.odex", odex_file); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 889 | |
| 890 | EXPECT_TRUE(OatFileAssistant::DexFilenameToOdexFilename( |
| 891 | "/foo/bar/baz.funnyext", kArm, &odex_file, &error_msg)) << error_msg; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame^] | 892 | EXPECT_EQ("/foo/bar/oat/arm/baz.odex", odex_file); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 893 | |
| 894 | EXPECT_FALSE(OatFileAssistant::DexFilenameToOdexFilename( |
| 895 | "nopath.jar", kArm, &odex_file, &error_msg)); |
| 896 | EXPECT_FALSE(OatFileAssistant::DexFilenameToOdexFilename( |
| 897 | "/foo/bar/baz_noext", kArm, &odex_file, &error_msg)); |
| 898 | } |
| 899 | |
| 900 | |
| 901 | // TODO: More Tests: |
| 902 | // * Test class linker falls back to unquickened dex for DexNoOat |
| 903 | // * Test class linker falls back to unquickened dex for MultiDexNoOat |
| 904 | // * Test multidex files: |
| 905 | // - Multidex with only classes2.dex out of date should have status |
| 906 | // kOutOfDate |
| 907 | // * Test using secondary isa |
| 908 | // * Test with profiling info? |
| 909 | // * Test for status of oat while oat is being generated (how?) |
| 910 | // * Test case where 32 and 64 bit boot class paths differ, |
| 911 | // and we ask IsInBootClassPath for a class in exactly one of the 32 or |
| 912 | // 64 bit boot class paths. |
| 913 | // * Test unexpected scenarios (?): |
| 914 | // - Dex is stripped, don't have odex. |
| 915 | // - Oat file corrupted after status check, before reload unexecutable |
| 916 | // because it's unrelocated and no dex2oat |
| 917 | |
| 918 | } // namespace art |