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