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