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