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 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 17 | #include <algorithm> |
| 18 | #include <fstream> |
| 19 | #include <string> |
| 20 | #include <vector> |
| 21 | #include <sys/param.h> |
| 22 | |
| 23 | #include <backtrace/BacktraceMap.h> |
| 24 | #include <gtest/gtest.h> |
| 25 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 26 | #include "art_field-inl.h" |
Vladimir Marko | 3481ba2 | 2015-04-13 12:22:36 +0100 | [diff] [blame] | 27 | #include "class_linker-inl.h" |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 28 | #include "common_runtime_test.h" |
Andreas Gampe | bb9c6b1 | 2015-03-29 13:56:36 -0700 | [diff] [blame] | 29 | #include "compiler_callbacks.h" |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 30 | #include "dex2oat_environment_test.h" |
Richard Uhler | f16d572 | 2015-05-11 09:32:47 -0700 | [diff] [blame] | 31 | #include "gc/space/image_space.h" |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 32 | #include "mem_map.h" |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 33 | #include "oat_file_assistant.h" |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 34 | #include "oat_file_manager.h" |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 35 | #include "os.h" |
Richard Uhler | 23cedd2 | 2015-04-08 13:17:29 -0700 | [diff] [blame] | 36 | #include "scoped_thread_state_change.h" |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 37 | #include "thread-inl.h" |
| 38 | #include "utils.h" |
| 39 | |
| 40 | namespace art { |
| 41 | |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 42 | class OatFileAssistantTest : public Dex2oatEnvironmentTest { |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 43 | public: |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 44 | virtual void SetUp() OVERRIDE { |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 45 | ReserveImageSpace(); |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 46 | Dex2oatEnvironmentTest::SetUp(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 47 | } |
| 48 | |
Richard Uhler | a48403e | 2016-04-26 10:24:38 -0700 | [diff] [blame] | 49 | // Pre-Relocate the image to a known non-zero offset so we don't have to |
| 50 | // deal with the runtime randomly relocating the image by 0 and messing up |
| 51 | // the expected results of the tests. |
| 52 | bool PreRelocateImage(std::string* error_msg) { |
| 53 | std::string image; |
| 54 | if (!GetCachedImageFile(&image, error_msg)) { |
| 55 | return false; |
| 56 | } |
| 57 | |
| 58 | std::string patchoat = GetAndroidRoot(); |
| 59 | patchoat += kIsDebugBuild ? "/bin/patchoatd" : "/bin/patchoat"; |
| 60 | |
| 61 | std::vector<std::string> argv; |
| 62 | argv.push_back(patchoat); |
| 63 | argv.push_back("--input-image-location=" + GetImageLocation()); |
| 64 | argv.push_back("--output-image-file=" + image); |
| 65 | argv.push_back("--instruction-set=" + std::string(GetInstructionSetString(kRuntimeISA))); |
| 66 | argv.push_back("--base-offset-delta=0x00008000"); |
| 67 | return Exec(argv, error_msg); |
| 68 | } |
| 69 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 70 | virtual void PreRuntimeCreate() { |
Richard Uhler | a48403e | 2016-04-26 10:24:38 -0700 | [diff] [blame] | 71 | std::string error_msg; |
| 72 | ASSERT_TRUE(PreRelocateImage(&error_msg)) << error_msg; |
| 73 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 74 | UnreserveImageSpace(); |
| 75 | } |
| 76 | |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 77 | virtual void PostRuntimeCreate() OVERRIDE { |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 78 | ReserveImageSpace(); |
| 79 | } |
| 80 | |
Richard Uhler | 93aa210 | 2015-08-10 14:47:41 -0700 | [diff] [blame] | 81 | // Generate a non-PIC odex file for the purposes of test. |
Richard Uhler | 94f5bda | 2015-07-22 08:25:11 -0700 | [diff] [blame] | 82 | // The generated odex file will be un-relocated. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 83 | void GenerateOdexForTest(const std::string& dex_location, |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 84 | const std::string& odex_location, |
Richard Uhler | a48403e | 2016-04-26 10:24:38 -0700 | [diff] [blame] | 85 | CompilerFilter::Filter filter, |
| 86 | bool pic = false, |
| 87 | bool with_patch_info = true) { |
| 88 | // Temporarily redirect the dalvik cache so dex2oat doesn't find the |
| 89 | // relocated image file. |
| 90 | std::string android_data_tmp = GetScratchDir() + "AndroidDataTmp"; |
| 91 | setenv("ANDROID_DATA", android_data_tmp.c_str(), 1); |
Richard Uhler | 93aa210 | 2015-08-10 14:47:41 -0700 | [diff] [blame] | 92 | std::vector<std::string> args; |
| 93 | args.push_back("--dex-file=" + dex_location); |
Richard Uhler | a48403e | 2016-04-26 10:24:38 -0700 | [diff] [blame] | 94 | args.push_back("--oat-file=" + odex_location); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 95 | args.push_back("--compiler-filter=" + CompilerFilter::NameOfFilter(filter)); |
Richard Uhler | a48403e | 2016-04-26 10:24:38 -0700 | [diff] [blame] | 96 | args.push_back("--runtime-arg"); |
| 97 | args.push_back("-Xnorelocate"); |
Richard Uhler | 93aa210 | 2015-08-10 14:47:41 -0700 | [diff] [blame] | 98 | |
Richard Uhler | a48403e | 2016-04-26 10:24:38 -0700 | [diff] [blame] | 99 | if (pic) { |
| 100 | args.push_back("--compile-pic"); |
| 101 | } |
| 102 | |
| 103 | if (with_patch_info) { |
| 104 | args.push_back("--include-patch-information"); |
| 105 | } |
Richard Uhler | 93aa210 | 2015-08-10 14:47:41 -0700 | [diff] [blame] | 106 | |
| 107 | std::string error_msg; |
| 108 | ASSERT_TRUE(OatFileAssistant::Dex2Oat(args, &error_msg)) << error_msg; |
Richard Uhler | a48403e | 2016-04-26 10:24:38 -0700 | [diff] [blame] | 109 | setenv("ANDROID_DATA", android_data_.c_str(), 1); |
Richard Uhler | 93aa210 | 2015-08-10 14:47:41 -0700 | [diff] [blame] | 110 | |
| 111 | // Verify the odex file was generated as expected and really is |
| 112 | // unrelocated. |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 113 | std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(), |
| 114 | odex_location.c_str(), |
| 115 | nullptr, |
| 116 | nullptr, |
| 117 | false, |
| 118 | /*low_4gb*/false, |
| 119 | dex_location.c_str(), |
| 120 | &error_msg)); |
Richard Uhler | 93aa210 | 2015-08-10 14:47:41 -0700 | [diff] [blame] | 121 | ASSERT_TRUE(odex_file.get() != nullptr) << error_msg; |
Richard Uhler | a48403e | 2016-04-26 10:24:38 -0700 | [diff] [blame] | 122 | EXPECT_EQ(pic, odex_file->IsPic()); |
| 123 | EXPECT_EQ(with_patch_info, odex_file->HasPatchInfo()); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 124 | EXPECT_EQ(filter, odex_file->GetCompilerFilter()); |
| 125 | |
Vladimir Marko | f6d1e0f | 2016-05-23 15:32:42 +0100 | [diff] [blame] | 126 | if (CompilerFilter::IsBytecodeCompilationEnabled(filter)) { |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 127 | const std::vector<gc::space::ImageSpace*> image_spaces = |
Richard Uhler | a48403e | 2016-04-26 10:24:38 -0700 | [diff] [blame] | 128 | Runtime::Current()->GetHeap()->GetBootImageSpaces(); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 129 | ASSERT_TRUE(!image_spaces.empty() && image_spaces[0] != nullptr); |
| 130 | const ImageHeader& image_header = image_spaces[0]->GetImageHeader(); |
| 131 | const OatHeader& oat_header = odex_file->GetOatHeader(); |
Jeff Hao | b11ffb7 | 2016-04-07 15:40:54 -0700 | [diff] [blame] | 132 | uint32_t combined_checksum = OatFileAssistant::CalculateCombinedImageChecksum(); |
| 133 | EXPECT_EQ(combined_checksum, oat_header.GetImageFileLocationOatChecksum()); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 134 | EXPECT_NE(reinterpret_cast<uintptr_t>(image_header.GetOatDataBegin()), |
| 135 | oat_header.GetImageFileLocationOatDataBegin()); |
| 136 | EXPECT_NE(image_header.GetPatchDelta(), oat_header.GetImagePatchDelta()); |
| 137 | } |
Richard Uhler | 93aa210 | 2015-08-10 14:47:41 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | void GeneratePicOdexForTest(const std::string& dex_location, |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 141 | const std::string& odex_location, |
| 142 | CompilerFilter::Filter filter) { |
Richard Uhler | a48403e | 2016-04-26 10:24:38 -0700 | [diff] [blame] | 143 | GenerateOdexForTest(dex_location, odex_location, filter, true, false); |
Calin Juravle | b077e15 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 144 | } |
David Brazdil | ce4b0ba | 2016-01-28 15:05:49 +0000 | [diff] [blame] | 145 | |
Richard Uhler | d1537b5 | 2016-03-29 13:27:41 -0700 | [diff] [blame] | 146 | // Generate a non-PIC odex file without patch information for the purposes |
| 147 | // of test. The generated odex file will be un-relocated. |
Richard Uhler | d1537b5 | 2016-03-29 13:27:41 -0700 | [diff] [blame] | 148 | void GenerateNoPatchOdexForTest(const std::string& dex_location, |
| 149 | const std::string& odex_location, |
| 150 | CompilerFilter::Filter filter) { |
Richard Uhler | a48403e | 2016-04-26 10:24:38 -0700 | [diff] [blame] | 151 | GenerateOdexForTest(dex_location, odex_location, filter, false, false); |
Richard Uhler | d1537b5 | 2016-03-29 13:27:41 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 154 | private: |
| 155 | // Reserve memory around where the image will be loaded so other memory |
| 156 | // won't conflict when it comes time to load the image. |
| 157 | // This can be called with an already loaded image to reserve the space |
| 158 | // around it. |
| 159 | void ReserveImageSpace() { |
| 160 | MemMap::Init(); |
| 161 | |
| 162 | // Ensure a chunk of memory is reserved for the image space. |
Richard Uhler | a48403e | 2016-04-26 10:24:38 -0700 | [diff] [blame] | 163 | // The reservation_end includes room for the main space that has to come |
| 164 | // right after the image in case of the GSS collector. |
| 165 | uintptr_t reservation_start = ART_BASE_ADDRESS; |
| 166 | uintptr_t reservation_end = ART_BASE_ADDRESS + 384 * MB; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 167 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 168 | std::unique_ptr<BacktraceMap> map(BacktraceMap::Create(getpid(), true)); |
| 169 | ASSERT_TRUE(map.get() != nullptr) << "Failed to build process map"; |
| 170 | for (BacktraceMap::const_iterator it = map->begin(); |
| 171 | reservation_start < reservation_end && it != map->end(); ++it) { |
Richard Uhler | 3efe979 | 2015-03-30 16:18:03 -0700 | [diff] [blame] | 172 | ReserveImageSpaceChunk(reservation_start, std::min(it->start, reservation_end)); |
| 173 | reservation_start = std::max(reservation_start, it->end); |
| 174 | } |
| 175 | ReserveImageSpaceChunk(reservation_start, reservation_end); |
| 176 | } |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 177 | |
Richard Uhler | 3efe979 | 2015-03-30 16:18:03 -0700 | [diff] [blame] | 178 | // Reserve a chunk of memory for the image space in the given range. |
| 179 | // Only has effect for chunks with a positive number of bytes. |
| 180 | void ReserveImageSpaceChunk(uintptr_t start, uintptr_t end) { |
| 181 | if (start < end) { |
| 182 | std::string error_msg; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 183 | image_reservation_.push_back(std::unique_ptr<MemMap>( |
| 184 | MemMap::MapAnonymous("image reservation", |
Richard Uhler | 3efe979 | 2015-03-30 16:18:03 -0700 | [diff] [blame] | 185 | reinterpret_cast<uint8_t*>(start), end - start, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 186 | PROT_NONE, false, false, &error_msg))); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 187 | ASSERT_TRUE(image_reservation_.back().get() != nullptr) << error_msg; |
| 188 | LOG(INFO) << "Reserved space for image " << |
| 189 | reinterpret_cast<void*>(image_reservation_.back()->Begin()) << "-" << |
| 190 | reinterpret_cast<void*>(image_reservation_.back()->End()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | |
| 194 | |
| 195 | // Unreserve any memory reserved by ReserveImageSpace. This should be called |
| 196 | // before the image is loaded. |
| 197 | void UnreserveImageSpace() { |
| 198 | image_reservation_.clear(); |
| 199 | } |
| 200 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 201 | std::vector<std::unique_ptr<MemMap>> image_reservation_; |
| 202 | }; |
| 203 | |
| 204 | class OatFileAssistantNoDex2OatTest : public OatFileAssistantTest { |
| 205 | public: |
| 206 | virtual void SetUpRuntimeOptions(RuntimeOptions* options) { |
| 207 | OatFileAssistantTest::SetUpRuntimeOptions(options); |
| 208 | options->push_back(std::make_pair("-Xnodex2oat", nullptr)); |
| 209 | } |
| 210 | }; |
| 211 | |
| 212 | // Generate an oat file for the purposes of test, as opposed to testing |
| 213 | // generation of oat files. |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 214 | static void GenerateOatForTest(const char* dex_location, CompilerFilter::Filter filter) { |
| 215 | // Use an oat file assistant to find the proper oat location. |
Richard Uhler | b81881d | 2016-04-19 13:08:04 -0700 | [diff] [blame] | 216 | std::string oat_location; |
| 217 | std::string error_msg; |
| 218 | ASSERT_TRUE(OatFileAssistant::DexLocationToOatFilename( |
| 219 | dex_location, kRuntimeISA, &oat_location, &error_msg)) << error_msg; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 220 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 221 | std::vector<std::string> args; |
| 222 | args.push_back("--dex-file=" + std::string(dex_location)); |
Richard Uhler | b81881d | 2016-04-19 13:08:04 -0700 | [diff] [blame] | 223 | args.push_back("--oat-file=" + oat_location); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 224 | args.push_back("--compiler-filter=" + CompilerFilter::NameOfFilter(filter)); |
| 225 | args.push_back("--runtime-arg"); |
| 226 | args.push_back("-Xnorelocate"); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 227 | ASSERT_TRUE(OatFileAssistant::Dex2Oat(args, &error_msg)) << error_msg; |
| 228 | |
| 229 | // Verify the oat file was generated as expected. |
Richard Uhler | b81881d | 2016-04-19 13:08:04 -0700 | [diff] [blame] | 230 | std::unique_ptr<OatFile> oat_file(OatFile::Open(oat_location.c_str(), |
| 231 | oat_location.c_str(), |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 232 | nullptr, |
| 233 | nullptr, |
| 234 | false, |
| 235 | /*low_4gb*/false, |
| 236 | dex_location, |
| 237 | &error_msg)); |
| 238 | ASSERT_TRUE(oat_file.get() != nullptr) << error_msg; |
| 239 | EXPECT_EQ(filter, oat_file->GetCompilerFilter()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | // Case: We have a DEX file, but no OAT file for it. |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 243 | // Expect: The status is kDex2OatNeeded. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 244 | TEST_F(OatFileAssistantTest, DexNoOat) { |
| 245 | std::string dex_location = GetScratchDir() + "/DexNoOat.jar"; |
| 246 | Copy(GetDexSrc1(), dex_location); |
| 247 | |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 248 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 249 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 250 | EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, |
| 251 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime)); |
| 252 | EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, |
| 253 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly)); |
| 254 | EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, |
| 255 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeedProfile)); |
| 256 | EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, |
| 257 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 258 | |
| 259 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 260 | EXPECT_FALSE(oat_file_assistant.OdexFileExists()); |
| 261 | EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 262 | EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation()); |
| 263 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 264 | EXPECT_EQ(OatFileAssistant::kOatOutOfDate, oat_file_assistant.OdexFileStatus()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 265 | EXPECT_FALSE(oat_file_assistant.OatFileExists()); |
| 266 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 267 | EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation()); |
| 268 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 269 | EXPECT_EQ(OatFileAssistant::kOatOutOfDate, oat_file_assistant.OatFileStatus()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 270 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | // Case: We have no DEX file and no OAT file. |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 274 | // Expect: Status is kNoDexOptNeeded. Loading should fail, but not crash. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 275 | TEST_F(OatFileAssistantTest, NoDexNoOat) { |
| 276 | std::string dex_location = GetScratchDir() + "/NoDexNoOat.jar"; |
| 277 | |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 278 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 279 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 280 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 281 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 282 | EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles()); |
| 283 | |
| 284 | // Trying to make the oat file up to date should not fail or crash. |
| 285 | std::string error_msg; |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 286 | EXPECT_EQ(OatFileAssistant::kUpdateSucceeded, oat_file_assistant.MakeUpToDate(false, &error_msg)); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 287 | |
| 288 | // Trying to get the best oat file should fail, but not crash. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 289 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 290 | EXPECT_EQ(nullptr, oat_file.get()); |
| 291 | } |
| 292 | |
| 293 | // 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] | 294 | // Expect: The status is kNoDexOptNeeded. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 295 | TEST_F(OatFileAssistantTest, OatUpToDate) { |
| 296 | std::string dex_location = GetScratchDir() + "/OatUpToDate.jar"; |
| 297 | Copy(GetDexSrc1(), dex_location); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 298 | GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 299 | |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 300 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 301 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 302 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 303 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
| 304 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 305 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly)); |
| 306 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 307 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime)); |
| 308 | EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, |
| 309 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything)); |
| 310 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 311 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 312 | EXPECT_FALSE(oat_file_assistant.OdexFileExists()); |
| 313 | EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 314 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 315 | EXPECT_TRUE(oat_file_assistant.OatFileExists()); |
| 316 | EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate()); |
| 317 | EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation()); |
| 318 | EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate()); |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 319 | EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 320 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 321 | } |
| 322 | |
Richard Uhler | 9a37efc | 2016-08-05 16:32:55 -0700 | [diff] [blame^] | 323 | // Case: We have a DEX file and ODEX file for a different dex location. |
| 324 | // Expect: The status is kDex2OatNeeded. |
| 325 | TEST_F(OatFileAssistantTest, OatForDifferentDex) { |
| 326 | // Generate an odex file for OatForDifferentDex_A.jar |
| 327 | std::string dex_location_a = GetScratchDir() + "/OatForDifferentDex_A.jar"; |
| 328 | std::string odex_location = GetOdexDir() + "/OatForDifferentDex.odex"; |
| 329 | Copy(GetDexSrc1(), dex_location_a); |
| 330 | GenerateOdexForTest(dex_location_a, odex_location, CompilerFilter::kSpeed); |
| 331 | |
| 332 | // Try to use that odex file for OatForDifferentDex.jar |
| 333 | std::string dex_location = GetScratchDir() + "/OatForDifferentDex.jar"; |
| 334 | Copy(GetDexSrc1(), dex_location); |
| 335 | |
| 336 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false); |
| 337 | |
| 338 | EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, |
| 339 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
| 340 | |
| 341 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 342 | EXPECT_TRUE(oat_file_assistant.OdexFileExists()); |
| 343 | EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 344 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 345 | EXPECT_FALSE(oat_file_assistant.OatFileExists()); |
| 346 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 347 | EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation()); |
| 348 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
| 349 | } |
| 350 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 351 | // Case: We have a DEX file and speed-profile OAT file for it. |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 352 | // Expect: The status is kNoDexOptNeeded if the profile hasn't changed, but |
| 353 | // kDex2Oat if the profile has changed. |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 354 | TEST_F(OatFileAssistantTest, ProfileOatUpToDate) { |
| 355 | std::string dex_location = GetScratchDir() + "/ProfileOatUpToDate.jar"; |
| 356 | Copy(GetDexSrc1(), dex_location); |
| 357 | GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeedProfile); |
| 358 | |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 359 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 360 | |
| 361 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 362 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeedProfile, false)); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 363 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 364 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly, false)); |
| 365 | EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, |
| 366 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeedProfile, true)); |
| 367 | EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, |
| 368 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly, true)); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 369 | |
| 370 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 371 | EXPECT_FALSE(oat_file_assistant.OdexFileExists()); |
| 372 | EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 373 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 374 | EXPECT_TRUE(oat_file_assistant.OatFileExists()); |
| 375 | EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate()); |
| 376 | EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation()); |
| 377 | EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate()); |
| 378 | EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus()); |
| 379 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
| 380 | } |
| 381 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 382 | // 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] | 383 | // Expect: The status is kNoDexOptNeeded and we load all dex files. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 384 | TEST_F(OatFileAssistantTest, MultiDexOatUpToDate) { |
| 385 | std::string dex_location = GetScratchDir() + "/MultiDexOatUpToDate.jar"; |
| 386 | Copy(GetMultiDexSrc1(), dex_location); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 387 | GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 388 | |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 389 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 390 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 391 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed, false)); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 392 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 393 | |
| 394 | // Verify we can load both dex files. |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 395 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 396 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 397 | EXPECT_TRUE(oat_file->IsExecutable()); |
| 398 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 399 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 400 | EXPECT_EQ(2u, dex_files.size()); |
| 401 | } |
| 402 | |
Richard Uhler | 67ff7d1 | 2015-05-14 13:21:13 -0700 | [diff] [blame] | 403 | // Case: We have a MultiDEX file where the secondary dex file is out of date. |
| 404 | // Expect: The status is kDex2OatNeeded. |
| 405 | TEST_F(OatFileAssistantTest, MultiDexSecondaryOutOfDate) { |
| 406 | std::string dex_location = GetScratchDir() + "/MultiDexSecondaryOutOfDate.jar"; |
| 407 | |
| 408 | // Compile code for GetMultiDexSrc1. |
| 409 | Copy(GetMultiDexSrc1(), dex_location); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 410 | GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed); |
Richard Uhler | 67ff7d1 | 2015-05-14 13:21:13 -0700 | [diff] [blame] | 411 | |
| 412 | // Now overwrite the dex file with GetMultiDexSrc2 so the secondary checksum |
| 413 | // is out of date. |
| 414 | Copy(GetMultiDexSrc2(), dex_location); |
| 415 | |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 416 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 417 | EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 418 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed, false)); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 419 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 67ff7d1 | 2015-05-14 13:21:13 -0700 | [diff] [blame] | 420 | } |
| 421 | |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 422 | // Case: We have a MultiDEX file and up-to-date OAT file for it with relative |
| 423 | // encoded dex locations. |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 424 | // Expect: The oat file status is kNoDexOptNeeded. |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 425 | TEST_F(OatFileAssistantTest, RelativeEncodedDexLocation) { |
| 426 | std::string dex_location = GetScratchDir() + "/RelativeEncodedDexLocation.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 427 | std::string oat_location = GetOdexDir() + "/RelativeEncodedDexLocation.oat"; |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 428 | |
| 429 | // Create the dex file |
| 430 | Copy(GetMultiDexSrc1(), dex_location); |
| 431 | |
| 432 | // Create the oat file with relative encoded dex location. |
| 433 | std::vector<std::string> args; |
| 434 | args.push_back("--dex-file=" + dex_location); |
| 435 | args.push_back("--dex-location=" + std::string("RelativeEncodedDexLocation.jar")); |
| 436 | args.push_back("--oat-file=" + oat_location); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 437 | args.push_back("--compiler-filter=speed"); |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 438 | |
| 439 | std::string error_msg; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 440 | ASSERT_TRUE(OatFileAssistant::Dex2Oat(args, &error_msg)) << error_msg; |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 441 | |
| 442 | // Verify we can load both dex files. |
| 443 | OatFileAssistant oat_file_assistant(dex_location.c_str(), |
| 444 | oat_location.c_str(), |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 445 | kRuntimeISA, true); |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 446 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 447 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 448 | EXPECT_TRUE(oat_file->IsExecutable()); |
| 449 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 450 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 451 | EXPECT_EQ(2u, dex_files.size()); |
| 452 | } |
| 453 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 454 | // Case: We have a DEX file and out-of-date OAT file. |
| 455 | // Expect: The status is kDex2OatNeeded. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 456 | TEST_F(OatFileAssistantTest, OatOutOfDate) { |
| 457 | std::string dex_location = GetScratchDir() + "/OatOutOfDate.jar"; |
| 458 | |
| 459 | // We create a dex, generate an oat for it, then overwrite the dex with a |
| 460 | // different dex to make the oat out of date. |
| 461 | Copy(GetDexSrc1(), dex_location); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 462 | GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 463 | Copy(GetDexSrc2(), dex_location); |
| 464 | |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 465 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 466 | EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, |
| 467 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime)); |
| 468 | EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, |
| 469 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 470 | |
| 471 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 472 | EXPECT_FALSE(oat_file_assistant.OdexFileExists()); |
| 473 | EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 474 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 475 | EXPECT_TRUE(oat_file_assistant.OatFileExists()); |
| 476 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 477 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 478 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | // 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] | 482 | // Expect: The status is kPatchOatNeeded. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 483 | TEST_F(OatFileAssistantTest, DexOdexNoOat) { |
| 484 | std::string dex_location = GetScratchDir() + "/DexOdexNoOat.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 485 | std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 486 | |
| 487 | // Create the dex and odex files |
| 488 | Copy(GetDexSrc1(), dex_location); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 489 | GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 490 | |
| 491 | // Verify the status. |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 492 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 493 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 494 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 495 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime)); |
| 496 | EXPECT_EQ(OatFileAssistant::kPatchOatNeeded, |
| 497 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 498 | |
| 499 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 500 | EXPECT_TRUE(oat_file_assistant.OdexFileExists()); |
| 501 | EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 502 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 503 | EXPECT_TRUE(oat_file_assistant.OdexFileNeedsRelocation()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 504 | EXPECT_FALSE(oat_file_assistant.OatFileExists()); |
| 505 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 506 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 507 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 5f946da | 2015-07-17 12:28:32 -0700 | [diff] [blame] | 508 | |
| 509 | // We should still be able to get the non-executable odex file to run from. |
| 510 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 511 | ASSERT_TRUE(oat_file.get() != nullptr); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | // 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] | 515 | // Expect: The status is kPatchOatNeeded |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 516 | TEST_F(OatFileAssistantTest, StrippedDexOdexNoOat) { |
| 517 | std::string dex_location = GetScratchDir() + "/StrippedDexOdexNoOat.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 518 | std::string odex_location = GetOdexDir() + "/StrippedDexOdexNoOat.odex"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 519 | |
| 520 | // Create the dex and odex files |
| 521 | Copy(GetDexSrc1(), dex_location); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 522 | GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 523 | |
| 524 | // Strip the dex file |
| 525 | Copy(GetStrippedDexSrc1(), dex_location); |
| 526 | |
| 527 | // Verify the status. |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 528 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 529 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 530 | EXPECT_EQ(OatFileAssistant::kPatchOatNeeded, |
| 531 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 532 | |
| 533 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 534 | EXPECT_TRUE(oat_file_assistant.OdexFileExists()); |
| 535 | EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 536 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 537 | EXPECT_FALSE(oat_file_assistant.OatFileExists()); |
| 538 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 539 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 540 | EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 541 | |
| 542 | // Make the oat file up to date. |
| 543 | std::string error_msg; |
Richard Uhler | f4b3487 | 2016-04-13 11:03:46 -0700 | [diff] [blame] | 544 | Runtime::Current()->AddCompilerOption("--compiler-filter=speed"); |
Richard Uhler | 1e86061 | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 545 | ASSERT_EQ(OatFileAssistant::kUpdateSucceeded, |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 546 | oat_file_assistant.MakeUpToDate(false, &error_msg)) << error_msg; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 547 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 548 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 549 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 550 | |
| 551 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 552 | EXPECT_TRUE(oat_file_assistant.OdexFileExists()); |
| 553 | EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 554 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 555 | EXPECT_TRUE(oat_file_assistant.OatFileExists()); |
| 556 | EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate()); |
| 557 | EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 558 | EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 559 | |
| 560 | // Verify we can load the dex files from it. |
| 561 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 562 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 563 | EXPECT_TRUE(oat_file->IsExecutable()); |
| 564 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 565 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 566 | EXPECT_EQ(1u, dex_files.size()); |
| 567 | } |
| 568 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 569 | // Case: We have a stripped DEX file, an ODEX file, and an out-of-date OAT file. |
| 570 | // Expect: The status is kPatchOatNeeded. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 571 | TEST_F(OatFileAssistantTest, StrippedDexOdexOat) { |
| 572 | std::string dex_location = GetScratchDir() + "/StrippedDexOdexOat.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 573 | std::string odex_location = GetOdexDir() + "/StrippedDexOdexOat.odex"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 574 | |
| 575 | // Create the oat file from a different dex file so it looks out of date. |
| 576 | Copy(GetDexSrc2(), dex_location); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 577 | GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 578 | |
| 579 | // Create the odex file |
| 580 | Copy(GetDexSrc1(), dex_location); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 581 | GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 582 | |
| 583 | // Strip the dex file. |
| 584 | Copy(GetStrippedDexSrc1(), dex_location); |
| 585 | |
| 586 | // Verify the status. |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 587 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 588 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 589 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 590 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime)); |
| 591 | EXPECT_EQ(OatFileAssistant::kPatchOatNeeded, |
| 592 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
| 593 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, // Can't run dex2oat because dex file is stripped. |
| 594 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 595 | |
| 596 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 597 | EXPECT_TRUE(oat_file_assistant.OdexFileExists()); |
| 598 | EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 599 | EXPECT_TRUE(oat_file_assistant.OdexFileNeedsRelocation()); |
| 600 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 601 | EXPECT_TRUE(oat_file_assistant.OatFileExists()); |
| 602 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 603 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 604 | EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 605 | |
| 606 | // Make the oat file up to date. |
| 607 | std::string error_msg; |
Richard Uhler | f4b3487 | 2016-04-13 11:03:46 -0700 | [diff] [blame] | 608 | Runtime::Current()->AddCompilerOption("--compiler-filter=speed"); |
Richard Uhler | 1e86061 | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 609 | ASSERT_EQ(OatFileAssistant::kUpdateSucceeded, |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 610 | oat_file_assistant.MakeUpToDate(false, &error_msg)) << error_msg; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 611 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 612 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 613 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
| 614 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, // Can't run dex2oat because dex file is stripped. |
| 615 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 616 | |
| 617 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 618 | EXPECT_TRUE(oat_file_assistant.OdexFileExists()); |
| 619 | EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 620 | EXPECT_TRUE(oat_file_assistant.OdexFileNeedsRelocation()); |
| 621 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 622 | EXPECT_TRUE(oat_file_assistant.OatFileExists()); |
| 623 | EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate()); |
| 624 | EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation()); |
| 625 | EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 626 | EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 627 | |
| 628 | // Verify we can load the dex files from it. |
| 629 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 630 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 631 | EXPECT_TRUE(oat_file->IsExecutable()); |
| 632 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 633 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 634 | EXPECT_EQ(1u, dex_files.size()); |
| 635 | } |
| 636 | |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 637 | // Case: We have a stripped (or resource-only) DEX file, no ODEX file and no |
| 638 | // OAT file. Expect: The status is kNoDexOptNeeded. |
| 639 | TEST_F(OatFileAssistantTest, ResourceOnlyDex) { |
| 640 | std::string dex_location = GetScratchDir() + "/ResourceOnlyDex.jar"; |
| 641 | |
| 642 | Copy(GetStrippedDexSrc1(), dex_location); |
| 643 | |
| 644 | // Verify the status. |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 645 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 646 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 647 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 648 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
| 649 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 650 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime)); |
| 651 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 652 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly)); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 653 | |
| 654 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 655 | EXPECT_FALSE(oat_file_assistant.OdexFileExists()); |
| 656 | EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 657 | EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation()); |
| 658 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 659 | EXPECT_FALSE(oat_file_assistant.OatFileExists()); |
| 660 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 661 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
| 662 | EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles()); |
| 663 | |
| 664 | // Make the oat file up to date. This should have no effect. |
| 665 | std::string error_msg; |
Richard Uhler | f4b3487 | 2016-04-13 11:03:46 -0700 | [diff] [blame] | 666 | Runtime::Current()->AddCompilerOption("--compiler-filter=speed"); |
Richard Uhler | 1e86061 | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 667 | EXPECT_EQ(OatFileAssistant::kUpdateSucceeded, |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 668 | oat_file_assistant.MakeUpToDate(false, &error_msg)) << error_msg; |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 669 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 670 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 671 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 672 | |
| 673 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 674 | EXPECT_FALSE(oat_file_assistant.OdexFileExists()); |
| 675 | EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 676 | EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation()); |
| 677 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 678 | EXPECT_FALSE(oat_file_assistant.OatFileExists()); |
| 679 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 680 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
| 681 | EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles()); |
| 682 | } |
| 683 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 684 | // Case: We have a DEX file, no ODEX file and an OAT file that needs |
| 685 | // relocation. |
| 686 | // Expect: The status is kSelfPatchOatNeeded. |
| 687 | TEST_F(OatFileAssistantTest, SelfRelocation) { |
| 688 | std::string dex_location = GetScratchDir() + "/SelfRelocation.jar"; |
| 689 | std::string oat_location = GetOdexDir() + "/SelfRelocation.oat"; |
| 690 | |
| 691 | // Create the dex and odex files |
| 692 | Copy(GetDexSrc1(), dex_location); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 693 | GenerateOdexForTest(dex_location, oat_location, CompilerFilter::kSpeed); |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 694 | |
| 695 | OatFileAssistant oat_file_assistant(dex_location.c_str(), |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 696 | oat_location.c_str(), kRuntimeISA, true); |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 697 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 698 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 699 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly)); |
| 700 | EXPECT_EQ(OatFileAssistant::kSelfPatchOatNeeded, |
| 701 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
| 702 | EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, |
| 703 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything)); |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 704 | |
| 705 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 706 | EXPECT_FALSE(oat_file_assistant.OdexFileExists()); |
| 707 | EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 708 | EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation()); |
| 709 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 710 | EXPECT_TRUE(oat_file_assistant.OatFileExists()); |
| 711 | EXPECT_TRUE(oat_file_assistant.OatFileNeedsRelocation()); |
| 712 | EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate()); |
| 713 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 714 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 715 | |
| 716 | // Make the oat file up to date. |
| 717 | std::string error_msg; |
Richard Uhler | f4b3487 | 2016-04-13 11:03:46 -0700 | [diff] [blame] | 718 | Runtime::Current()->AddCompilerOption("--compiler-filter=speed"); |
Richard Uhler | 1e86061 | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 719 | ASSERT_EQ(OatFileAssistant::kUpdateSucceeded, |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 720 | oat_file_assistant.MakeUpToDate(false, &error_msg)) << error_msg; |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 721 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 722 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 723 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 724 | |
| 725 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 726 | EXPECT_FALSE(oat_file_assistant.OdexFileExists()); |
| 727 | EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 728 | EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation()); |
| 729 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 730 | EXPECT_TRUE(oat_file_assistant.OatFileExists()); |
| 731 | EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate()); |
| 732 | EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation()); |
| 733 | EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 734 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 735 | |
| 736 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 737 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 738 | EXPECT_TRUE(oat_file->IsExecutable()); |
| 739 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 740 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 741 | EXPECT_EQ(1u, dex_files.size()); |
| 742 | } |
| 743 | |
Richard Uhler | d1537b5 | 2016-03-29 13:27:41 -0700 | [diff] [blame] | 744 | // Case: We have a DEX file, no ODEX file and an OAT file that needs |
| 745 | // relocation but doesn't have patch info. |
| 746 | // Expect: The status is kDex2OatNeeded, because we can't run patchoat. |
| 747 | TEST_F(OatFileAssistantTest, NoSelfRelocation) { |
| 748 | std::string dex_location = GetScratchDir() + "/NoSelfRelocation.jar"; |
| 749 | std::string oat_location = GetOdexDir() + "/NoSelfRelocation.oat"; |
| 750 | |
| 751 | // Create the dex and odex files |
| 752 | Copy(GetDexSrc1(), dex_location); |
| 753 | GenerateNoPatchOdexForTest(dex_location, oat_location, CompilerFilter::kSpeed); |
| 754 | |
| 755 | OatFileAssistant oat_file_assistant(dex_location.c_str(), |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 756 | oat_location.c_str(), kRuntimeISA, true); |
Richard Uhler | d1537b5 | 2016-03-29 13:27:41 -0700 | [diff] [blame] | 757 | |
| 758 | EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, |
| 759 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
| 760 | |
| 761 | // Make the oat file up to date. |
| 762 | std::string error_msg; |
Richard Uhler | f4b3487 | 2016-04-13 11:03:46 -0700 | [diff] [blame] | 763 | Runtime::Current()->AddCompilerOption("--compiler-filter=speed"); |
Richard Uhler | 1e86061 | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 764 | ASSERT_EQ(OatFileAssistant::kUpdateSucceeded, |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 765 | oat_file_assistant.MakeUpToDate(false, &error_msg)) << error_msg; |
Richard Uhler | d1537b5 | 2016-03-29 13:27:41 -0700 | [diff] [blame] | 766 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 767 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
| 768 | |
| 769 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 770 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 771 | EXPECT_TRUE(oat_file->IsExecutable()); |
| 772 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 773 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 774 | EXPECT_EQ(1u, dex_files.size()); |
| 775 | } |
| 776 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 777 | // Case: We have a DEX file, an ODEX file and an OAT file, where the ODEX and |
| 778 | // OAT files both have patch delta of 0. |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 779 | // Expect: It shouldn't crash, and status is kPatchOatNeeded. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 780 | TEST_F(OatFileAssistantTest, OdexOatOverlap) { |
| 781 | std::string dex_location = GetScratchDir() + "/OdexOatOverlap.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 782 | std::string odex_location = GetOdexDir() + "/OdexOatOverlap.odex"; |
| 783 | std::string oat_location = GetOdexDir() + "/OdexOatOverlap.oat"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 784 | |
| 785 | // Create the dex and odex files |
| 786 | Copy(GetDexSrc1(), dex_location); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 787 | GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 788 | |
| 789 | // Create the oat file by copying the odex so they are located in the same |
| 790 | // place in memory. |
| 791 | Copy(odex_location, oat_location); |
| 792 | |
| 793 | // Verify things don't go bad. |
| 794 | OatFileAssistant oat_file_assistant(dex_location.c_str(), |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 795 | oat_location.c_str(), kRuntimeISA, true); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 796 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 797 | EXPECT_EQ(OatFileAssistant::kPatchOatNeeded, |
| 798 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 799 | |
| 800 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 801 | EXPECT_TRUE(oat_file_assistant.OdexFileExists()); |
| 802 | EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 803 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 804 | EXPECT_TRUE(oat_file_assistant.OatFileExists()); |
| 805 | EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate()); |
| 806 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 807 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 808 | |
| 809 | // Things aren't relocated, so it should fall back to interpreted. |
| 810 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 811 | ASSERT_TRUE(oat_file.get() != nullptr); |
Richard Uhler | f16d572 | 2015-05-11 09:32:47 -0700 | [diff] [blame] | 812 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 813 | EXPECT_FALSE(oat_file->IsExecutable()); |
| 814 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 815 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 816 | EXPECT_EQ(1u, dex_files.size()); |
| 817 | } |
| 818 | |
| 819 | // 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] | 820 | // Expect: The status is kNoDexOptNeeded, because PIC needs no relocation. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 821 | TEST_F(OatFileAssistantTest, DexPicOdexNoOat) { |
| 822 | std::string dex_location = GetScratchDir() + "/DexPicOdexNoOat.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 823 | std::string odex_location = GetOdexDir() + "/DexPicOdexNoOat.odex"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 824 | |
| 825 | // Create the dex and odex files |
| 826 | Copy(GetDexSrc1(), dex_location); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 827 | GeneratePicOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 828 | |
| 829 | // Verify the status. |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 830 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 831 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 832 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 833 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
| 834 | EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, |
| 835 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 836 | |
| 837 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 838 | EXPECT_TRUE(oat_file_assistant.OdexFileExists()); |
| 839 | EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 840 | EXPECT_TRUE(oat_file_assistant.OdexFileIsUpToDate()); |
| 841 | EXPECT_FALSE(oat_file_assistant.OatFileExists()); |
| 842 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 843 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 844 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 845 | } |
| 846 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 847 | // Case: We have a DEX file and a VerifyAtRuntime ODEX file, but no OAT file. |
| 848 | // Expect: The status is kNoDexOptNeeded, because VerifyAtRuntime contains no code. |
| 849 | TEST_F(OatFileAssistantTest, DexVerifyAtRuntimeOdexNoOat) { |
| 850 | std::string dex_location = GetScratchDir() + "/DexVerifyAtRuntimeOdexNoOat.jar"; |
| 851 | std::string odex_location = GetOdexDir() + "/DexVerifyAtRuntimeOdexNoOat.odex"; |
David Brazdil | ce4b0ba | 2016-01-28 15:05:49 +0000 | [diff] [blame] | 852 | |
| 853 | // Create the dex and odex files |
| 854 | Copy(GetDexSrc1(), dex_location); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 855 | GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kVerifyAtRuntime); |
David Brazdil | ce4b0ba | 2016-01-28 15:05:49 +0000 | [diff] [blame] | 856 | |
| 857 | // Verify the status. |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 858 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false); |
David Brazdil | ce4b0ba | 2016-01-28 15:05:49 +0000 | [diff] [blame] | 859 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 860 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 861 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime)); |
| 862 | EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, |
| 863 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
David Brazdil | ce4b0ba | 2016-01-28 15:05:49 +0000 | [diff] [blame] | 864 | |
| 865 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 866 | EXPECT_TRUE(oat_file_assistant.OdexFileExists()); |
| 867 | EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate()); |
Nicolas Geoffray | 845e506 | 2016-03-23 06:42:05 +0000 | [diff] [blame] | 868 | EXPECT_TRUE(oat_file_assistant.OdexFileIsUpToDate()); |
David Brazdil | ce4b0ba | 2016-01-28 15:05:49 +0000 | [diff] [blame] | 869 | EXPECT_FALSE(oat_file_assistant.OatFileExists()); |
| 870 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 871 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
| 872 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
| 873 | } |
| 874 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 875 | // Case: We have a DEX file and up-to-date OAT file for it. |
| 876 | // Expect: We should load an executable dex file. |
| 877 | TEST_F(OatFileAssistantTest, LoadOatUpToDate) { |
| 878 | std::string dex_location = GetScratchDir() + "/LoadOatUpToDate.jar"; |
| 879 | |
| 880 | Copy(GetDexSrc1(), dex_location); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 881 | GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 882 | |
| 883 | // Load the oat using an oat file assistant. |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 884 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 885 | |
| 886 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 887 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 888 | EXPECT_TRUE(oat_file->IsExecutable()); |
| 889 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 890 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 891 | EXPECT_EQ(1u, dex_files.size()); |
| 892 | } |
| 893 | |
| 894 | // Case: We have a DEX file and up-to-date interpret-only OAT file for it. |
| 895 | // Expect: We should still load the oat file as executable. |
| 896 | TEST_F(OatFileAssistantTest, LoadExecInterpretOnlyOatUpToDate) { |
| 897 | std::string dex_location = GetScratchDir() + "/LoadExecInterpretOnlyOatUpToDate.jar"; |
| 898 | |
| 899 | Copy(GetDexSrc1(), dex_location); |
| 900 | GenerateOatForTest(dex_location.c_str(), CompilerFilter::kInterpretOnly); |
| 901 | |
| 902 | // Load the oat using an oat file assistant. |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 903 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 904 | |
| 905 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 906 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 907 | EXPECT_TRUE(oat_file->IsExecutable()); |
| 908 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 909 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 910 | EXPECT_EQ(1u, dex_files.size()); |
| 911 | } |
| 912 | |
| 913 | // Case: We have a DEX file and up-to-date OAT file for it. |
| 914 | // Expect: Loading non-executable should load the oat non-executable. |
| 915 | TEST_F(OatFileAssistantTest, LoadNoExecOatUpToDate) { |
| 916 | std::string dex_location = GetScratchDir() + "/LoadNoExecOatUpToDate.jar"; |
| 917 | |
| 918 | Copy(GetDexSrc1(), dex_location); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 919 | GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 920 | |
| 921 | // Load the oat using an oat file assistant. |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 922 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 923 | |
| 924 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 925 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 926 | EXPECT_FALSE(oat_file->IsExecutable()); |
| 927 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 928 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 929 | EXPECT_EQ(1u, dex_files.size()); |
| 930 | } |
| 931 | |
| 932 | // Case: We have a DEX file. |
| 933 | // Expect: We should load an executable dex file from an alternative oat |
| 934 | // location. |
| 935 | TEST_F(OatFileAssistantTest, LoadDexNoAlternateOat) { |
| 936 | std::string dex_location = GetScratchDir() + "/LoadDexNoAlternateOat.jar"; |
| 937 | std::string oat_location = GetScratchDir() + "/LoadDexNoAlternateOat.oat"; |
| 938 | |
| 939 | Copy(GetDexSrc1(), dex_location); |
| 940 | |
| 941 | OatFileAssistant oat_file_assistant( |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 942 | dex_location.c_str(), oat_location.c_str(), kRuntimeISA, true); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 943 | std::string error_msg; |
Richard Uhler | f4b3487 | 2016-04-13 11:03:46 -0700 | [diff] [blame] | 944 | Runtime::Current()->AddCompilerOption("--compiler-filter=speed"); |
Richard Uhler | 1e86061 | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 945 | ASSERT_EQ(OatFileAssistant::kUpdateSucceeded, |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 946 | oat_file_assistant.MakeUpToDate(false, &error_msg)) << error_msg; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 947 | |
| 948 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 949 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 950 | EXPECT_TRUE(oat_file->IsExecutable()); |
| 951 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 952 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 953 | EXPECT_EQ(1u, dex_files.size()); |
| 954 | |
| 955 | EXPECT_TRUE(OS::FileExists(oat_location.c_str())); |
| 956 | |
| 957 | // Verify it didn't create an oat in the default location. |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 958 | OatFileAssistant ofm(dex_location.c_str(), kRuntimeISA, false); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 959 | EXPECT_FALSE(ofm.OatFileExists()); |
| 960 | } |
| 961 | |
Richard Uhler | 8327cf7 | 2015-10-13 16:34:59 -0700 | [diff] [blame] | 962 | // Case: We have a DEX file but can't write the oat file. |
| 963 | // Expect: We should fail to make the oat file up to date. |
| 964 | TEST_F(OatFileAssistantTest, LoadDexUnwriteableAlternateOat) { |
| 965 | std::string dex_location = GetScratchDir() + "/LoadDexUnwriteableAlternateOat.jar"; |
| 966 | |
| 967 | // Make the oat location unwritable by inserting some non-existent |
| 968 | // intermediate directories. |
| 969 | std::string oat_location = GetScratchDir() + "/foo/bar/LoadDexUnwriteableAlternateOat.oat"; |
| 970 | |
| 971 | Copy(GetDexSrc1(), dex_location); |
| 972 | |
| 973 | OatFileAssistant oat_file_assistant( |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 974 | dex_location.c_str(), oat_location.c_str(), kRuntimeISA, true); |
Richard Uhler | 8327cf7 | 2015-10-13 16:34:59 -0700 | [diff] [blame] | 975 | std::string error_msg; |
Richard Uhler | f4b3487 | 2016-04-13 11:03:46 -0700 | [diff] [blame] | 976 | Runtime::Current()->AddCompilerOption("--compiler-filter=speed"); |
Richard Uhler | 1e86061 | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 977 | ASSERT_EQ(OatFileAssistant::kUpdateNotAttempted, |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 978 | oat_file_assistant.MakeUpToDate(false, &error_msg)); |
Richard Uhler | 8327cf7 | 2015-10-13 16:34:59 -0700 | [diff] [blame] | 979 | |
| 980 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 981 | ASSERT_TRUE(oat_file.get() == nullptr); |
| 982 | } |
| 983 | |
| 984 | // Case: We don't have a DEX file and can't write the oat file. |
| 985 | // Expect: We should fail to generate the oat file without crashing. |
| 986 | TEST_F(OatFileAssistantTest, GenNoDex) { |
| 987 | std::string dex_location = GetScratchDir() + "/GenNoDex.jar"; |
| 988 | std::string oat_location = GetScratchDir() + "/GenNoDex.oat"; |
| 989 | |
| 990 | OatFileAssistant oat_file_assistant( |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 991 | dex_location.c_str(), oat_location.c_str(), kRuntimeISA, true); |
Richard Uhler | 8327cf7 | 2015-10-13 16:34:59 -0700 | [diff] [blame] | 992 | std::string error_msg; |
Richard Uhler | f4b3487 | 2016-04-13 11:03:46 -0700 | [diff] [blame] | 993 | Runtime::Current()->AddCompilerOption("--compiler-filter=speed"); |
Richard Uhler | 1e86061 | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 994 | EXPECT_EQ(OatFileAssistant::kUpdateNotAttempted, |
Richard Uhler | f4b3487 | 2016-04-13 11:03:46 -0700 | [diff] [blame] | 995 | oat_file_assistant.GenerateOatFile(&error_msg)); |
Richard Uhler | 8327cf7 | 2015-10-13 16:34:59 -0700 | [diff] [blame] | 996 | } |
| 997 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 998 | // Turn an absolute path into a path relative to the current working |
| 999 | // directory. |
| 1000 | static std::string MakePathRelative(std::string target) { |
| 1001 | char buf[MAXPATHLEN]; |
| 1002 | std::string cwd = getcwd(buf, MAXPATHLEN); |
| 1003 | |
| 1004 | // Split the target and cwd paths into components. |
| 1005 | std::vector<std::string> target_path; |
| 1006 | std::vector<std::string> cwd_path; |
| 1007 | Split(target, '/', &target_path); |
| 1008 | Split(cwd, '/', &cwd_path); |
| 1009 | |
| 1010 | // Reverse the path components, so we can use pop_back(). |
| 1011 | std::reverse(target_path.begin(), target_path.end()); |
| 1012 | std::reverse(cwd_path.begin(), cwd_path.end()); |
| 1013 | |
| 1014 | // Drop the common prefix of the paths. Because we reversed the path |
| 1015 | // components, this becomes the common suffix of target_path and cwd_path. |
| 1016 | while (!target_path.empty() && !cwd_path.empty() |
| 1017 | && target_path.back() == cwd_path.back()) { |
| 1018 | target_path.pop_back(); |
| 1019 | cwd_path.pop_back(); |
| 1020 | } |
| 1021 | |
| 1022 | // For each element of the remaining cwd_path, add '..' to the beginning |
| 1023 | // of the target path. Because we reversed the path components, we add to |
| 1024 | // the end of target_path. |
| 1025 | for (unsigned int i = 0; i < cwd_path.size(); i++) { |
| 1026 | target_path.push_back(".."); |
| 1027 | } |
| 1028 | |
| 1029 | // Reverse again to get the right path order, and join to get the result. |
| 1030 | std::reverse(target_path.begin(), target_path.end()); |
| 1031 | return Join(target_path, '/'); |
| 1032 | } |
| 1033 | |
| 1034 | // Case: Non-absolute path to Dex location. |
| 1035 | // Expect: Not sure, but it shouldn't crash. |
| 1036 | TEST_F(OatFileAssistantTest, NonAbsoluteDexLocation) { |
| 1037 | std::string abs_dex_location = GetScratchDir() + "/NonAbsoluteDexLocation.jar"; |
| 1038 | Copy(GetDexSrc1(), abs_dex_location); |
| 1039 | |
| 1040 | std::string dex_location = MakePathRelative(abs_dex_location); |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 1041 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1042 | |
| 1043 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 1044 | EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, |
| 1045 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1046 | EXPECT_FALSE(oat_file_assistant.OdexFileExists()); |
| 1047 | EXPECT_FALSE(oat_file_assistant.OatFileExists()); |
| 1048 | EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 1049 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 1050 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 1051 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
| 1052 | } |
| 1053 | |
| 1054 | // Case: Very short, non-existent Dex location. |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 1055 | // Expect: kNoDexOptNeeded. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1056 | TEST_F(OatFileAssistantTest, ShortDexLocation) { |
| 1057 | std::string dex_location = "/xx"; |
| 1058 | |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 1059 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1060 | |
| 1061 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 1062 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 1063 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1064 | EXPECT_FALSE(oat_file_assistant.OdexFileExists()); |
| 1065 | EXPECT_FALSE(oat_file_assistant.OatFileExists()); |
| 1066 | EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 1067 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 1068 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 1069 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 1070 | EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1071 | |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 1072 | // Trying to make it up to date should have no effect. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1073 | std::string error_msg; |
Richard Uhler | f4b3487 | 2016-04-13 11:03:46 -0700 | [diff] [blame] | 1074 | Runtime::Current()->AddCompilerOption("--compiler-filter=speed"); |
Richard Uhler | 1e86061 | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 1075 | EXPECT_EQ(OatFileAssistant::kUpdateSucceeded, |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 1076 | oat_file_assistant.MakeUpToDate(false, &error_msg)); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 1077 | EXPECT_TRUE(error_msg.empty()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1078 | } |
| 1079 | |
| 1080 | // Case: Non-standard extension for dex file. |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 1081 | // Expect: The status is kDex2OatNeeded. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1082 | TEST_F(OatFileAssistantTest, LongDexExtension) { |
| 1083 | std::string dex_location = GetScratchDir() + "/LongDexExtension.jarx"; |
| 1084 | Copy(GetDexSrc1(), dex_location); |
| 1085 | |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 1086 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1087 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 1088 | EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, |
| 1089 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1090 | |
| 1091 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 1092 | EXPECT_FALSE(oat_file_assistant.OdexFileExists()); |
| 1093 | EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 1094 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 1095 | EXPECT_FALSE(oat_file_assistant.OatFileExists()); |
| 1096 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 1097 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
| 1098 | } |
| 1099 | |
| 1100 | // A task to generate a dex location. Used by the RaceToGenerate test. |
| 1101 | class RaceGenerateTask : public Task { |
| 1102 | public: |
| 1103 | explicit RaceGenerateTask(const std::string& dex_location, const std::string& oat_location) |
Jeff Hao | f0192c8 | 2016-03-28 20:39:50 -0700 | [diff] [blame] | 1104 | : dex_location_(dex_location), oat_location_(oat_location), loaded_oat_file_(nullptr) |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1105 | {} |
| 1106 | |
Roland Levillain | 4b8f1ec | 2015-08-26 18:34:03 +0100 | [diff] [blame] | 1107 | void Run(Thread* self ATTRIBUTE_UNUSED) { |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1108 | // Load the dex files, and save a pointer to the loaded oat file, so that |
| 1109 | // we can verify only one oat file was loaded for the dex location. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1110 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 1111 | std::vector<std::string> error_msgs; |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 1112 | const OatFile* oat_file = nullptr; |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 1113 | dex_files = Runtime::Current()->GetOatFileManager().OpenDexFilesFromOat( |
| 1114 | dex_location_.c_str(), |
| 1115 | oat_location_.c_str(), |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1116 | /*class_loader*/nullptr, |
| 1117 | /*dex_elements*/nullptr, |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 1118 | &oat_file, |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 1119 | &error_msgs); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1120 | CHECK(!dex_files.empty()) << Join(error_msgs, '\n'); |
Richard Uhler | 07b3c23 | 2015-03-31 15:57:54 -0700 | [diff] [blame] | 1121 | CHECK(dex_files[0]->GetOatDexFile() != nullptr) << dex_files[0]->GetLocation(); |
| 1122 | loaded_oat_file_ = dex_files[0]->GetOatDexFile()->GetOatFile(); |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 1123 | CHECK_EQ(loaded_oat_file_, oat_file); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1124 | } |
| 1125 | |
| 1126 | const OatFile* GetLoadedOatFile() const { |
| 1127 | return loaded_oat_file_; |
| 1128 | } |
| 1129 | |
| 1130 | private: |
| 1131 | std::string dex_location_; |
| 1132 | std::string oat_location_; |
| 1133 | const OatFile* loaded_oat_file_; |
| 1134 | }; |
| 1135 | |
| 1136 | // Test the case where multiple processes race to generate an oat file. |
| 1137 | // This simulates multiple processes using multiple threads. |
| 1138 | // |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 1139 | // We want unique Oat files to be loaded even when there is a race to load. |
| 1140 | // TODO: The test case no longer tests locking the way it was intended since we now get multiple |
| 1141 | // copies of the same Oat files mapped at different locations. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1142 | TEST_F(OatFileAssistantTest, RaceToGenerate) { |
| 1143 | std::string dex_location = GetScratchDir() + "/RaceToGenerate.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 1144 | std::string oat_location = GetOdexDir() + "/RaceToGenerate.oat"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1145 | |
| 1146 | // We use the lib core dex file, because it's large, and hopefully should |
| 1147 | // take a while to generate. |
Narayan Kamath | d1ef436 | 2015-11-12 11:49:06 +0000 | [diff] [blame] | 1148 | Copy(GetLibCoreDexFileNames()[0], dex_location); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1149 | |
| 1150 | const int kNumThreads = 32; |
| 1151 | Thread* self = Thread::Current(); |
| 1152 | ThreadPool thread_pool("Oat file assistant test thread pool", kNumThreads); |
| 1153 | std::vector<std::unique_ptr<RaceGenerateTask>> tasks; |
| 1154 | for (int i = 0; i < kNumThreads; i++) { |
| 1155 | std::unique_ptr<RaceGenerateTask> task(new RaceGenerateTask(dex_location, oat_location)); |
| 1156 | thread_pool.AddTask(self, task.get()); |
| 1157 | tasks.push_back(std::move(task)); |
| 1158 | } |
| 1159 | thread_pool.StartWorkers(self); |
| 1160 | thread_pool.Wait(self, true, false); |
| 1161 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 1162 | // Verify every task got a unique oat file. |
| 1163 | std::set<const OatFile*> oat_files; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1164 | for (auto& task : tasks) { |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 1165 | const OatFile* oat_file = task->GetLoadedOatFile(); |
| 1166 | EXPECT_TRUE(oat_files.find(oat_file) == oat_files.end()); |
| 1167 | oat_files.insert(oat_file); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1168 | } |
| 1169 | } |
| 1170 | |
| 1171 | // Case: We have a DEX file and an ODEX file, no OAT file, and dex2oat is |
| 1172 | // disabled. |
| 1173 | // Expect: We should load the odex file non-executable. |
| 1174 | TEST_F(OatFileAssistantNoDex2OatTest, LoadDexOdexNoOat) { |
| 1175 | std::string dex_location = GetScratchDir() + "/LoadDexOdexNoOat.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 1176 | std::string odex_location = GetOdexDir() + "/LoadDexOdexNoOat.odex"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1177 | |
| 1178 | // Create the dex and odex files |
| 1179 | Copy(GetDexSrc1(), dex_location); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 1180 | GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1181 | |
| 1182 | // Load the oat using an executable oat file assistant. |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 1183 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1184 | |
| 1185 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 1186 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 1187 | EXPECT_FALSE(oat_file->IsExecutable()); |
| 1188 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 1189 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 1190 | EXPECT_EQ(1u, dex_files.size()); |
| 1191 | } |
| 1192 | |
| 1193 | // Case: We have a MultiDEX file and an ODEX file, no OAT file, and dex2oat is |
| 1194 | // disabled. |
| 1195 | // Expect: We should load the odex file non-executable. |
| 1196 | TEST_F(OatFileAssistantNoDex2OatTest, LoadMultiDexOdexNoOat) { |
| 1197 | std::string dex_location = GetScratchDir() + "/LoadMultiDexOdexNoOat.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 1198 | std::string odex_location = GetOdexDir() + "/LoadMultiDexOdexNoOat.odex"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1199 | |
| 1200 | // Create the dex and odex files |
| 1201 | Copy(GetMultiDexSrc1(), dex_location); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 1202 | GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1203 | |
| 1204 | // Load the oat using an executable oat file assistant. |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 1205 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1206 | |
| 1207 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 1208 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 1209 | EXPECT_FALSE(oat_file->IsExecutable()); |
| 1210 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 1211 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 1212 | EXPECT_EQ(2u, dex_files.size()); |
| 1213 | } |
| 1214 | |
Richard Uhler | f4b3487 | 2016-04-13 11:03:46 -0700 | [diff] [blame] | 1215 | TEST_F(OatFileAssistantTest, RuntimeCompilerFilterOptionUsed) { |
| 1216 | std::string dex_location = GetScratchDir() + "/RuntimeCompilerFilterOptionUsed.jar"; |
| 1217 | Copy(GetDexSrc1(), dex_location); |
| 1218 | |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 1219 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false); |
Richard Uhler | f4b3487 | 2016-04-13 11:03:46 -0700 | [diff] [blame] | 1220 | |
| 1221 | std::string error_msg; |
| 1222 | Runtime::Current()->AddCompilerOption("--compiler-filter=interpret-only"); |
| 1223 | EXPECT_EQ(OatFileAssistant::kUpdateSucceeded, |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 1224 | oat_file_assistant.MakeUpToDate(false, &error_msg)) << error_msg; |
Richard Uhler | f4b3487 | 2016-04-13 11:03:46 -0700 | [diff] [blame] | 1225 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 1226 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly)); |
| 1227 | EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, |
| 1228 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
| 1229 | |
| 1230 | Runtime::Current()->AddCompilerOption("--compiler-filter=speed"); |
| 1231 | EXPECT_EQ(OatFileAssistant::kUpdateSucceeded, |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 1232 | oat_file_assistant.MakeUpToDate(false, &error_msg)) << error_msg; |
Richard Uhler | f4b3487 | 2016-04-13 11:03:46 -0700 | [diff] [blame] | 1233 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 1234 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly)); |
| 1235 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 1236 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
| 1237 | |
| 1238 | Runtime::Current()->AddCompilerOption("--compiler-filter=bogus"); |
| 1239 | EXPECT_EQ(OatFileAssistant::kUpdateNotAttempted, |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 1240 | oat_file_assistant.MakeUpToDate(false, &error_msg)); |
Richard Uhler | f4b3487 | 2016-04-13 11:03:46 -0700 | [diff] [blame] | 1241 | } |
| 1242 | |
Richard Uhler | b81881d | 2016-04-19 13:08:04 -0700 | [diff] [blame] | 1243 | TEST(OatFileAssistantUtilsTest, DexLocationToOdexFilename) { |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1244 | std::string error_msg; |
| 1245 | std::string odex_file; |
| 1246 | |
Richard Uhler | b81881d | 2016-04-19 13:08:04 -0700 | [diff] [blame] | 1247 | EXPECT_TRUE(OatFileAssistant::DexLocationToOdexFilename( |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 1248 | "/foo/bar/baz.jar", kArm, &odex_file, &error_msg)) << error_msg; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 1249 | EXPECT_EQ("/foo/bar/oat/arm/baz.odex", odex_file); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1250 | |
Richard Uhler | b81881d | 2016-04-19 13:08:04 -0700 | [diff] [blame] | 1251 | EXPECT_TRUE(OatFileAssistant::DexLocationToOdexFilename( |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 1252 | "/foo/bar/baz.funnyext", kArm, &odex_file, &error_msg)) << error_msg; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 1253 | EXPECT_EQ("/foo/bar/oat/arm/baz.odex", odex_file); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1254 | |
Richard Uhler | b81881d | 2016-04-19 13:08:04 -0700 | [diff] [blame] | 1255 | EXPECT_FALSE(OatFileAssistant::DexLocationToOdexFilename( |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 1256 | "nopath.jar", kArm, &odex_file, &error_msg)); |
Richard Uhler | b81881d | 2016-04-19 13:08:04 -0700 | [diff] [blame] | 1257 | EXPECT_FALSE(OatFileAssistant::DexLocationToOdexFilename( |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 1258 | "/foo/bar/baz_noext", kArm, &odex_file, &error_msg)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1259 | } |
| 1260 | |
Richard Uhler | 23cedd2 | 2015-04-08 13:17:29 -0700 | [diff] [blame] | 1261 | // Verify the dexopt status values from dalvik.system.DexFile |
| 1262 | // match the OatFileAssistant::DexOptStatus values. |
| 1263 | TEST_F(OatFileAssistantTest, DexOptStatusValues) { |
| 1264 | ScopedObjectAccess soa(Thread::Current()); |
| 1265 | StackHandleScope<1> hs(soa.Self()); |
| 1266 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
| 1267 | Handle<mirror::Class> dexfile( |
| 1268 | hs.NewHandle(linker->FindSystemClass(soa.Self(), "Ldalvik/system/DexFile;"))); |
| 1269 | ASSERT_FALSE(dexfile.Get() == nullptr); |
| 1270 | linker->EnsureInitialized(soa.Self(), dexfile, true, true); |
| 1271 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1272 | ArtField* no_dexopt_needed = mirror::Class::FindStaticField( |
Richard Uhler | 23cedd2 | 2015-04-08 13:17:29 -0700 | [diff] [blame] | 1273 | soa.Self(), dexfile, "NO_DEXOPT_NEEDED", "I"); |
| 1274 | ASSERT_FALSE(no_dexopt_needed == nullptr); |
| 1275 | EXPECT_EQ(no_dexopt_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt); |
| 1276 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, no_dexopt_needed->GetInt(dexfile.Get())); |
| 1277 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1278 | ArtField* dex2oat_needed = mirror::Class::FindStaticField( |
Richard Uhler | 23cedd2 | 2015-04-08 13:17:29 -0700 | [diff] [blame] | 1279 | soa.Self(), dexfile, "DEX2OAT_NEEDED", "I"); |
| 1280 | ASSERT_FALSE(dex2oat_needed == nullptr); |
| 1281 | EXPECT_EQ(dex2oat_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt); |
| 1282 | EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, dex2oat_needed->GetInt(dexfile.Get())); |
| 1283 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1284 | ArtField* patchoat_needed = mirror::Class::FindStaticField( |
Richard Uhler | 23cedd2 | 2015-04-08 13:17:29 -0700 | [diff] [blame] | 1285 | soa.Self(), dexfile, "PATCHOAT_NEEDED", "I"); |
| 1286 | ASSERT_FALSE(patchoat_needed == nullptr); |
| 1287 | EXPECT_EQ(patchoat_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt); |
| 1288 | EXPECT_EQ(OatFileAssistant::kPatchOatNeeded, patchoat_needed->GetInt(dexfile.Get())); |
| 1289 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1290 | ArtField* self_patchoat_needed = mirror::Class::FindStaticField( |
Richard Uhler | 23cedd2 | 2015-04-08 13:17:29 -0700 | [diff] [blame] | 1291 | soa.Self(), dexfile, "SELF_PATCHOAT_NEEDED", "I"); |
| 1292 | ASSERT_FALSE(self_patchoat_needed == nullptr); |
| 1293 | EXPECT_EQ(self_patchoat_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt); |
| 1294 | EXPECT_EQ(OatFileAssistant::kSelfPatchOatNeeded, self_patchoat_needed->GetInt(dexfile.Get())); |
| 1295 | } |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1296 | |
| 1297 | // TODO: More Tests: |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 1298 | // * Image checksum change is out of date for kIntepretOnly, but not |
| 1299 | // kVerifyAtRuntime. But target of kVerifyAtRuntime still says current |
| 1300 | // kInterpretOnly is out of date. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1301 | // * Test class linker falls back to unquickened dex for DexNoOat |
| 1302 | // * Test class linker falls back to unquickened dex for MultiDexNoOat |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1303 | // * Test using secondary isa |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1304 | // * Test for status of oat while oat is being generated (how?) |
| 1305 | // * Test case where 32 and 64 bit boot class paths differ, |
| 1306 | // and we ask IsInBootClassPath for a class in exactly one of the 32 or |
| 1307 | // 64 bit boot class paths. |
| 1308 | // * Test unexpected scenarios (?): |
| 1309 | // - Dex is stripped, don't have odex. |
| 1310 | // - Oat file corrupted after status check, before reload unexecutable |
| 1311 | // because it's unrelocated and no dex2oat |
Calin Juravle | b077e15 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 1312 | // * Test unrelocated specific target compilation type can be relocated to |
| 1313 | // make it up to date. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1314 | |
| 1315 | } // namespace art |