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 | |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 17 | #include "oat_file_assistant.h" |
| 18 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 19 | #include <sys/param.h> |
| 20 | |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 21 | #include <string> |
| 22 | #include <vector> |
Shubham Ajmera | b22dea0 | 2017-10-04 18:36:41 -0700 | [diff] [blame] | 23 | #include <fcntl.h> |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 24 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 25 | #include <gtest/gtest.h> |
| 26 | |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 27 | #include "android-base/strings.h" |
| 28 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 29 | #include "art_field-inl.h" |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 30 | #include "base/os.h" |
| 31 | #include "base/utils.h" |
Vladimir Marko | 3481ba2 | 2015-04-13 12:22:36 +0100 | [diff] [blame] | 32 | #include "class_linker-inl.h" |
Calin Juravle | 27e0d1f | 2017-07-26 00:16:07 -0700 | [diff] [blame] | 33 | #include "class_loader_context.h" |
Jeff Hao | 0cb1728 | 2017-07-12 14:51:49 -0700 | [diff] [blame] | 34 | #include "common_runtime_test.h" |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 35 | #include "dexopt_test.h" |
David Brazdil | 32bde99 | 2018-05-14 15:24:34 +0100 | [diff] [blame] | 36 | #include "hidden_api.h" |
Calin Juravle | 27e0d1f | 2017-07-26 00:16:07 -0700 | [diff] [blame] | 37 | #include "oat_file.h" |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 38 | #include "oat_file_manager.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 39 | #include "scoped_thread_state_change-inl.h" |
Andreas Gampe | b486a98 | 2017-06-01 13:45:54 -0700 | [diff] [blame] | 40 | #include "thread-current-inl.h" |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 41 | |
| 42 | namespace art { |
| 43 | |
Calin Juravle | 5f9a801 | 2018-02-12 20:27:46 -0800 | [diff] [blame] | 44 | class OatFileAssistantTest : public DexoptTest { |
| 45 | public: |
| 46 | void VerifyOptimizationStatus(const std::string& file, |
| 47 | const std::string& expected_filter, |
| 48 | const std::string& expected_reason) { |
| 49 | std::string compilation_filter; |
| 50 | std::string compilation_reason; |
| 51 | OatFileAssistant::GetOptimizationStatus( |
| 52 | file, kRuntimeISA, &compilation_filter, &compilation_reason); |
| 53 | |
| 54 | ASSERT_EQ(expected_filter, compilation_filter); |
| 55 | ASSERT_EQ(expected_reason, compilation_reason); |
| 56 | } |
| 57 | |
| 58 | void VerifyOptimizationStatus(const std::string& file, |
| 59 | CompilerFilter::Filter expected_filter, |
| 60 | const std::string& expected_reason) { |
| 61 | VerifyOptimizationStatus( |
| 62 | file, CompilerFilter::NameOfFilter(expected_filter), expected_reason); |
| 63 | } |
| 64 | }; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 65 | |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 66 | class ScopedNonWritable { |
| 67 | public: |
| 68 | explicit ScopedNonWritable(const std::string& dex_location) { |
| 69 | is_valid_ = false; |
| 70 | size_t pos = dex_location.rfind('/'); |
| 71 | if (pos != std::string::npos) { |
| 72 | is_valid_ = true; |
| 73 | dex_parent_ = dex_location.substr(0, pos); |
| 74 | if (chmod(dex_parent_.c_str(), 0555) != 0) { |
| 75 | PLOG(ERROR) << "Could not change permissions on " << dex_parent_; |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | bool IsSuccessful() { return is_valid_ && (access(dex_parent_.c_str(), W_OK) != 0); } |
| 81 | |
| 82 | ~ScopedNonWritable() { |
| 83 | if (is_valid_) { |
| 84 | if (chmod(dex_parent_.c_str(), 0777) != 0) { |
| 85 | PLOG(ERROR) << "Could not restore permissions on " << dex_parent_; |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | private: |
| 91 | std::string dex_parent_; |
| 92 | bool is_valid_; |
| 93 | }; |
| 94 | |
| 95 | static bool IsExecutedAsRoot() { |
| 96 | return geteuid() == 0; |
| 97 | } |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 98 | |
Nicolas Geoffray | 3d8a78a | 2018-08-29 21:10:16 +0000 | [diff] [blame] | 99 | // Case: We have a MultiDEX file and up-to-date ODEX file for it with relative |
| 100 | // encoded dex locations. |
| 101 | // Expect: The oat file status is kNoDexOptNeeded. |
| 102 | TEST_F(OatFileAssistantTest, RelativeEncodedDexLocation) { |
| 103 | std::string dex_location = GetScratchDir() + "/RelativeEncodedDexLocation.jar"; |
| 104 | std::string odex_location = GetOdexDir() + "/RelativeEncodedDexLocation.odex"; |
| 105 | |
| 106 | // Create the dex file |
| 107 | Copy(GetMultiDexSrc1(), dex_location); |
| 108 | |
| 109 | // Create the oat file with relative encoded dex location. |
| 110 | std::vector<std::string> args = { |
| 111 | "--dex-file=" + dex_location, |
| 112 | "--dex-location=" + std::string("RelativeEncodedDexLocation.jar"), |
| 113 | "--oat-file=" + odex_location, |
| 114 | "--compiler-filter=speed" |
| 115 | }; |
| 116 | |
| 117 | std::string error_msg; |
| 118 | ASSERT_TRUE(Dex2Oat(args, &error_msg)) << error_msg; |
| 119 | |
| 120 | // Verify we can load both dex files. |
| 121 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true); |
| 122 | |
| 123 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 124 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 125 | EXPECT_TRUE(oat_file->IsExecutable()); |
| 126 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 127 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 128 | EXPECT_EQ(2u, dex_files.size()); |
| 129 | } |
| 130 | |
| 131 | TEST_F(OatFileAssistantTest, MakeUpToDateWithContext) { |
| 132 | std::string dex_location = GetScratchDir() + "/TestDex.jar"; |
| 133 | std::string odex_location = GetOdexDir() + "/TestDex.odex"; |
| 134 | std::string context_location = GetScratchDir() + "/ContextDex.jar"; |
| 135 | Copy(GetDexSrc1(), dex_location); |
| 136 | Copy(GetDexSrc2(), context_location); |
| 137 | |
| 138 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false); |
| 139 | |
| 140 | std::string context_str = "PCL[" + context_location + "]"; |
| 141 | std::unique_ptr<ClassLoaderContext> context = ClassLoaderContext::Create(context_str); |
| 142 | ASSERT_TRUE(context != nullptr); |
| 143 | ASSERT_TRUE(context->OpenDexFiles(kRuntimeISA, "")); |
| 144 | |
| 145 | std::string error_msg; |
| 146 | std::vector<std::string> args; |
| 147 | args.push_back("--dex-file=" + dex_location); |
| 148 | args.push_back("--oat-file=" + odex_location); |
| 149 | args.push_back("--class-loader-context=" + context_str); |
| 150 | ASSERT_TRUE(Dex2Oat(args, &error_msg)) << error_msg; |
| 151 | |
| 152 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 153 | EXPECT_NE(nullptr, oat_file.get()); |
| 154 | EXPECT_EQ(context->EncodeContextForOatFile(""), |
| 155 | oat_file->GetOatHeader().GetStoreValueByKey(OatHeader::kClassPathKey)); |
| 156 | } |
| 157 | |
| 158 | TEST_F(OatFileAssistantTest, GetDexOptNeededWithUpToDateContextRelative) { |
| 159 | std::string dex_location = GetScratchDir() + "/TestDex.jar"; |
| 160 | std::string odex_location = GetOdexDir() + "/TestDex.odex"; |
| 161 | std::string context_location = GetScratchDir() + "/ContextDex.jar"; |
| 162 | Copy(GetDexSrc1(), dex_location); |
| 163 | Copy(GetDexSrc2(), context_location); |
| 164 | |
| 165 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false); |
| 166 | |
| 167 | std::string context_str = "PCL[" + context_location + "]"; |
| 168 | std::unique_ptr<ClassLoaderContext> context = ClassLoaderContext::Create(context_str); |
| 169 | ASSERT_TRUE(context != nullptr); |
| 170 | ASSERT_TRUE(context->OpenDexFiles(kRuntimeISA, "")); |
| 171 | |
| 172 | std::string error_msg; |
| 173 | std::vector<std::string> args; |
| 174 | args.push_back("--dex-file=" + dex_location); |
| 175 | args.push_back("--oat-file=" + odex_location); |
| 176 | args.push_back("--class-loader-context=" + context_str); |
| 177 | ASSERT_TRUE(Dex2Oat(args, &error_msg)) << error_msg; |
| 178 | |
| 179 | // A relative context simulates a dependent split context. |
| 180 | std::unique_ptr<ClassLoaderContext> relative_context = |
| 181 | ClassLoaderContext::Create("PCL[ContextDex.jar]"); |
| 182 | EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded, |
| 183 | oat_file_assistant.GetDexOptNeeded( |
| 184 | CompilerFilter::kDefaultCompilerFilter, |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 185 | /* profile_changed= */ false, |
| 186 | /* downgrade= */ false, |
Nicolas Geoffray | 3d8a78a | 2018-08-29 21:10:16 +0000 | [diff] [blame] | 187 | relative_context.get())); |
| 188 | } |
| 189 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 190 | // Case: We have a DEX file, but no OAT file for it. |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 191 | // Expect: The status is kDex2OatNeeded. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 192 | TEST_F(OatFileAssistantTest, DexNoOat) { |
| 193 | std::string dex_location = GetScratchDir() + "/DexNoOat.jar"; |
| 194 | Copy(GetDexSrc1(), dex_location); |
| 195 | |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 196 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 197 | |
Richard Uhler | 7225a8d | 2016-11-22 10:12:03 +0000 | [diff] [blame] | 198 | EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch, |
Nicolas Geoffray | 49cda06 | 2017-04-21 13:08:25 +0100 | [diff] [blame] | 199 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract)); |
Richard Uhler | 7225a8d | 2016-11-22 10:12:03 +0000 | [diff] [blame] | 200 | EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch, |
Nicolas Geoffray | 49cda06 | 2017-04-21 13:08:25 +0100 | [diff] [blame] | 201 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken)); |
Richard Uhler | 7225a8d | 2016-11-22 10:12:03 +0000 | [diff] [blame] | 202 | EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch, |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 203 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeedProfile)); |
Richard Uhler | 7225a8d | 2016-11-22 10:12:03 +0000 | [diff] [blame] | 204 | EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch, |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 205 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 206 | |
| 207 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 208 | EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus()); |
| 209 | EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 210 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
Calin Juravle | 5f9a801 | 2018-02-12 20:27:46 -0800 | [diff] [blame] | 211 | |
| 212 | VerifyOptimizationStatus(dex_location, "run-from-apk", "unknown"); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | // Case: We have no DEX file and no OAT file. |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 216 | // Expect: Status is kNoDexOptNeeded. Loading should fail, but not crash. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 217 | TEST_F(OatFileAssistantTest, NoDexNoOat) { |
| 218 | std::string dex_location = GetScratchDir() + "/NoDexNoOat.jar"; |
| 219 | |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 220 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 221 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 222 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 223 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 224 | EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles()); |
| 225 | |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 226 | // Trying to get the best oat file should fail, but not crash. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 227 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 228 | EXPECT_EQ(nullptr, oat_file.get()); |
| 229 | } |
| 230 | |
Vladimir Marko | e066932 | 2018-09-03 15:44:54 +0100 | [diff] [blame] | 231 | // Case: We have a DEX file and an ODEX file, but no OAT file. |
| 232 | // Expect: The status is kNoDexOptNeeded. |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 233 | TEST_F(OatFileAssistantTest, OdexUpToDate) { |
| 234 | std::string dex_location = GetScratchDir() + "/OdexUpToDate.jar"; |
| 235 | std::string odex_location = GetOdexDir() + "/OdexUpToDate.odex"; |
| 236 | Copy(GetDexSrc1(), dex_location); |
Vladimir Marko | e066932 | 2018-09-03 15:44:54 +0100 | [diff] [blame] | 237 | GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed, "install"); |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 238 | |
| 239 | // For the use of oat location by making the dex parent not writable. |
| 240 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false); |
| 241 | |
| 242 | EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded, |
| 243 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
| 244 | EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded, |
| 245 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken)); |
| 246 | EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded, |
| 247 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract)); |
| 248 | EXPECT_EQ(-OatFileAssistant::kDex2OatForFilter, |
| 249 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything)); |
| 250 | |
| 251 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 252 | EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus()); |
| 253 | EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus()); |
| 254 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
Calin Juravle | 5f9a801 | 2018-02-12 20:27:46 -0800 | [diff] [blame] | 255 | |
| 256 | VerifyOptimizationStatus(dex_location, CompilerFilter::kSpeed, "install"); |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | // Case: We have a DEX file and a PIC ODEX file, but no OAT file. We load the dex |
| 260 | // file via a symlink. |
Vladimir Marko | e066932 | 2018-09-03 15:44:54 +0100 | [diff] [blame] | 261 | // Expect: The status is kNoDexOptNeeded. |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 262 | TEST_F(OatFileAssistantTest, OdexUpToDateSymLink) { |
| 263 | std::string scratch_dir = GetScratchDir(); |
| 264 | std::string dex_location = GetScratchDir() + "/OdexUpToDate.jar"; |
| 265 | std::string odex_location = GetOdexDir() + "/OdexUpToDate.odex"; |
| 266 | |
| 267 | Copy(GetDexSrc1(), dex_location); |
Vladimir Marko | e066932 | 2018-09-03 15:44:54 +0100 | [diff] [blame] | 268 | GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed); |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 269 | |
| 270 | // Now replace the dex location with a symlink. |
| 271 | std::string link = scratch_dir + "/link"; |
| 272 | ASSERT_EQ(0, symlink(scratch_dir.c_str(), link.c_str())); |
| 273 | dex_location = link + "/OdexUpToDate.jar"; |
| 274 | |
| 275 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false); |
| 276 | |
| 277 | EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded, |
| 278 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
| 279 | EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded, |
| 280 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken)); |
| 281 | EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded, |
| 282 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract)); |
| 283 | EXPECT_EQ(-OatFileAssistant::kDex2OatForFilter, |
| 284 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything)); |
| 285 | |
| 286 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 287 | EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus()); |
| 288 | EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus()); |
| 289 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
| 290 | } |
| 291 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 292 | // 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] | 293 | // Expect: The status is kNoDexOptNeeded. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 294 | TEST_F(OatFileAssistantTest, OatUpToDate) { |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 295 | if (IsExecutedAsRoot()) { |
| 296 | // We cannot simulate non writable locations when executed as root: b/38000545. |
| 297 | LOG(ERROR) << "Test skipped because it's running as root"; |
| 298 | return; |
| 299 | } |
| 300 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 301 | std::string dex_location = GetScratchDir() + "/OatUpToDate.jar"; |
| 302 | Copy(GetDexSrc1(), dex_location); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 303 | GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 304 | |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 305 | // For the use of oat location by making the dex parent not writable. |
| 306 | ScopedNonWritable scoped_non_writable(dex_location); |
| 307 | ASSERT_TRUE(scoped_non_writable.IsSuccessful()); |
| 308 | |
| 309 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false); |
| 310 | |
| 311 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 312 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
| 313 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 314 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken)); |
| 315 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 316 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract)); |
| 317 | EXPECT_EQ(OatFileAssistant::kDex2OatForFilter, |
| 318 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything)); |
| 319 | |
| 320 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 321 | EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus()); |
| 322 | EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus()); |
| 323 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
Calin Juravle | 5f9a801 | 2018-02-12 20:27:46 -0800 | [diff] [blame] | 324 | |
| 325 | VerifyOptimizationStatus(dex_location, CompilerFilter::kSpeed, "unknown"); |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 326 | } |
| 327 | |
Vladimir Marko | e066932 | 2018-09-03 15:44:54 +0100 | [diff] [blame] | 328 | // Case: Passing valid file descriptors of updated odex/vdex files along with the dex file. |
Shubham Ajmera | b22dea0 | 2017-10-04 18:36:41 -0700 | [diff] [blame] | 329 | // Expect: The status is kNoDexOptNeeded. |
| 330 | TEST_F(OatFileAssistantTest, GetDexOptNeededWithFd) { |
| 331 | std::string dex_location = GetScratchDir() + "/OatUpToDate.jar"; |
| 332 | std::string odex_location = GetScratchDir() + "/OatUpToDate.odex"; |
| 333 | std::string vdex_location = GetScratchDir() + "/OatUpToDate.vdex"; |
| 334 | |
| 335 | Copy(GetDexSrc1(), dex_location); |
| 336 | GenerateOatForTest(dex_location.c_str(), |
| 337 | odex_location.c_str(), |
| 338 | CompilerFilter::kSpeed, |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 339 | /* with_alternate_image= */ false); |
Shubham Ajmera | b22dea0 | 2017-10-04 18:36:41 -0700 | [diff] [blame] | 340 | |
Andreas Gampe | dfcd82c | 2018-10-16 20:22:37 -0700 | [diff] [blame] | 341 | android::base::unique_fd odex_fd(open(odex_location.c_str(), O_RDONLY | O_CLOEXEC)); |
| 342 | android::base::unique_fd vdex_fd(open(vdex_location.c_str(), O_RDONLY | O_CLOEXEC)); |
| 343 | android::base::unique_fd zip_fd(open(dex_location.c_str(), O_RDONLY | O_CLOEXEC)); |
Shubham Ajmera | b22dea0 | 2017-10-04 18:36:41 -0700 | [diff] [blame] | 344 | |
| 345 | OatFileAssistant oat_file_assistant(dex_location.c_str(), |
| 346 | kRuntimeISA, |
| 347 | false, |
Nicolas Geoffray | 2974260 | 2017-12-14 10:09:03 +0000 | [diff] [blame] | 348 | false, |
Shubham Ajmera | b22dea0 | 2017-10-04 18:36:41 -0700 | [diff] [blame] | 349 | vdex_fd.get(), |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 350 | odex_fd.get(), |
| 351 | zip_fd.get()); |
Shubham Ajmera | b22dea0 | 2017-10-04 18:36:41 -0700 | [diff] [blame] | 352 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 353 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
| 354 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 355 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken)); |
| 356 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 357 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract)); |
| 358 | EXPECT_EQ(-OatFileAssistant::kDex2OatForFilter, |
| 359 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything)); |
| 360 | |
| 361 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 362 | EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus()); |
| 363 | EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus()); |
| 364 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
| 365 | } |
| 366 | |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 367 | // Case: Passing invalid odex fd and valid vdex and zip fds. |
| 368 | // Expect: The status should be kDex2OatForBootImage. |
Shubham Ajmera | b22dea0 | 2017-10-04 18:36:41 -0700 | [diff] [blame] | 369 | TEST_F(OatFileAssistantTest, GetDexOptNeededWithInvalidOdexFd) { |
| 370 | std::string dex_location = GetScratchDir() + "/OatUpToDate.jar"; |
| 371 | std::string odex_location = GetScratchDir() + "/OatUpToDate.odex"; |
| 372 | std::string vdex_location = GetScratchDir() + "/OatUpToDate.vdex"; |
| 373 | |
| 374 | Copy(GetDexSrc1(), dex_location); |
| 375 | GenerateOatForTest(dex_location.c_str(), |
| 376 | odex_location.c_str(), |
| 377 | CompilerFilter::kSpeed, |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 378 | /* with_alternate_image= */ false); |
Shubham Ajmera | b22dea0 | 2017-10-04 18:36:41 -0700 | [diff] [blame] | 379 | |
Andreas Gampe | dfcd82c | 2018-10-16 20:22:37 -0700 | [diff] [blame] | 380 | android::base::unique_fd vdex_fd(open(vdex_location.c_str(), O_RDONLY | O_CLOEXEC)); |
| 381 | android::base::unique_fd zip_fd(open(dex_location.c_str(), O_RDONLY | O_CLOEXEC)); |
Shubham Ajmera | b22dea0 | 2017-10-04 18:36:41 -0700 | [diff] [blame] | 382 | |
| 383 | OatFileAssistant oat_file_assistant(dex_location.c_str(), |
| 384 | kRuntimeISA, |
| 385 | false, |
Nicolas Geoffray | 2974260 | 2017-12-14 10:09:03 +0000 | [diff] [blame] | 386 | false, |
Shubham Ajmera | b22dea0 | 2017-10-04 18:36:41 -0700 | [diff] [blame] | 387 | vdex_fd.get(), |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 388 | /* oat_fd= */ -1, |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 389 | zip_fd.get()); |
Shubham Ajmera | b22dea0 | 2017-10-04 18:36:41 -0700 | [diff] [blame] | 390 | EXPECT_EQ(-OatFileAssistant::kDex2OatForBootImage, |
| 391 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 392 | EXPECT_EQ(-OatFileAssistant::kDex2OatForBootImage, |
| 393 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything)); |
| 394 | |
| 395 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
Shubham Ajmera | b22dea0 | 2017-10-04 18:36:41 -0700 | [diff] [blame] | 396 | EXPECT_EQ(OatFileAssistant::kOatBootImageOutOfDate, oat_file_assistant.OdexFileStatus()); |
| 397 | EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus()); |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 398 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
Shubham Ajmera | b22dea0 | 2017-10-04 18:36:41 -0700 | [diff] [blame] | 399 | } |
| 400 | |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 401 | // Case: Passing invalid vdex fd and valid odex and zip fds. |
| 402 | // Expect: The status should be kDex2OatFromScratch. |
| 403 | TEST_F(OatFileAssistantTest, GetDexOptNeededWithInvalidVdexFd) { |
| 404 | std::string dex_location = GetScratchDir() + "/OatUpToDate.jar"; |
| 405 | std::string odex_location = GetScratchDir() + "/OatUpToDate.odex"; |
| 406 | |
| 407 | Copy(GetDexSrc1(), dex_location); |
| 408 | GenerateOatForTest(dex_location.c_str(), |
| 409 | odex_location.c_str(), |
| 410 | CompilerFilter::kSpeed, |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 411 | /* with_alternate_image= */ false); |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 412 | |
Andreas Gampe | dfcd82c | 2018-10-16 20:22:37 -0700 | [diff] [blame] | 413 | android::base::unique_fd odex_fd(open(odex_location.c_str(), O_RDONLY | O_CLOEXEC)); |
| 414 | android::base::unique_fd zip_fd(open(dex_location.c_str(), O_RDONLY | O_CLOEXEC)); |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 415 | |
| 416 | OatFileAssistant oat_file_assistant(dex_location.c_str(), |
| 417 | kRuntimeISA, |
| 418 | false, |
Nicolas Geoffray | 2974260 | 2017-12-14 10:09:03 +0000 | [diff] [blame] | 419 | false, |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 420 | /* vdex_fd= */ -1, |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 421 | odex_fd.get(), |
| 422 | zip_fd.get()); |
| 423 | |
| 424 | EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch, |
| 425 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
| 426 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 427 | EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus()); |
| 428 | EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus()); |
| 429 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
| 430 | } |
| 431 | |
| 432 | // Case: Passing invalid vdex and odex fd with valid zip fd. |
Shubham Ajmera | b22dea0 | 2017-10-04 18:36:41 -0700 | [diff] [blame] | 433 | // Expect: The status is kDex2oatFromScratch. |
| 434 | TEST_F(OatFileAssistantTest, GetDexOptNeededWithInvalidOdexVdexFd) { |
| 435 | std::string dex_location = GetScratchDir() + "/OatUpToDate.jar"; |
| 436 | |
| 437 | Copy(GetDexSrc1(), dex_location); |
| 438 | |
Andreas Gampe | dfcd82c | 2018-10-16 20:22:37 -0700 | [diff] [blame] | 439 | android::base::unique_fd zip_fd(open(dex_location.c_str(), O_RDONLY | O_CLOEXEC)); |
Shubham Ajmera | b22dea0 | 2017-10-04 18:36:41 -0700 | [diff] [blame] | 440 | OatFileAssistant oat_file_assistant(dex_location.c_str(), |
| 441 | kRuntimeISA, |
| 442 | false, |
Nicolas Geoffray | 2974260 | 2017-12-14 10:09:03 +0000 | [diff] [blame] | 443 | false, |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 444 | /* vdex_fd= */ -1, |
| 445 | /* oat_fd= */ -1, |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 446 | zip_fd); |
Shubham Ajmera | b22dea0 | 2017-10-04 18:36:41 -0700 | [diff] [blame] | 447 | EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch, |
| 448 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
| 449 | EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus()); |
| 450 | EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus()); |
| 451 | } |
| 452 | |
Richard Uhler | 2f27abd | 2017-01-31 14:02:34 +0000 | [diff] [blame] | 453 | // Case: We have a DEX file and up-to-date (ODEX) VDEX file for it, but no |
| 454 | // ODEX file. |
| 455 | TEST_F(OatFileAssistantTest, VdexUpToDateNoOdex) { |
Richard Uhler | 2f27abd | 2017-01-31 14:02:34 +0000 | [diff] [blame] | 456 | std::string dex_location = GetScratchDir() + "/VdexUpToDateNoOdex.jar"; |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 457 | std::string odex_location = GetOdexDir() + "/VdexUpToDateNoOdex.oat"; |
Richard Uhler | 2f27abd | 2017-01-31 14:02:34 +0000 | [diff] [blame] | 458 | |
Richard Uhler | 9a37efc | 2016-08-05 16:32:55 -0700 | [diff] [blame] | 459 | Copy(GetDexSrc1(), dex_location); |
| 460 | |
Richard Uhler | 2f27abd | 2017-01-31 14:02:34 +0000 | [diff] [blame] | 461 | // Generating and deleting the oat file should have the side effect of |
| 462 | // creating an up-to-date vdex file. |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 463 | GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed); |
| 464 | ASSERT_EQ(0, unlink(odex_location.c_str())); |
Richard Uhler | 2f27abd | 2017-01-31 14:02:34 +0000 | [diff] [blame] | 465 | |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 466 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false); |
Richard Uhler | 2f27abd | 2017-01-31 14:02:34 +0000 | [diff] [blame] | 467 | |
| 468 | // Even though the vdex file is up to date, because we don't have the oat |
| 469 | // file, we can't know that the vdex depends on the boot image and is up to |
| 470 | // date with respect to the boot image. Instead we must assume the vdex file |
| 471 | // depends on the boot image and is out of date with respect to the boot |
| 472 | // image. |
| 473 | EXPECT_EQ(-OatFileAssistant::kDex2OatForBootImage, |
| 474 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
| 475 | |
| 476 | // Make sure we don't crash in this case when we dump the status. We don't |
| 477 | // care what the actual dumped value is. |
| 478 | oat_file_assistant.GetStatusDump(); |
Calin Juravle | 5f9a801 | 2018-02-12 20:27:46 -0800 | [diff] [blame] | 479 | |
| 480 | VerifyOptimizationStatus(dex_location, "run-from-apk", "unknown"); |
Richard Uhler | 2f27abd | 2017-01-31 14:02:34 +0000 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | // Case: We have a DEX file and empty VDEX and ODEX files. |
| 484 | TEST_F(OatFileAssistantTest, EmptyVdexOdex) { |
| 485 | std::string dex_location = GetScratchDir() + "/EmptyVdexOdex.jar"; |
| 486 | std::string odex_location = GetOdexDir() + "/EmptyVdexOdex.oat"; |
| 487 | std::string vdex_location = GetOdexDir() + "/EmptyVdexOdex.vdex"; |
| 488 | |
| 489 | Copy(GetDexSrc1(), dex_location); |
Richard Uhler | 5cd5929 | 2017-02-01 12:54:23 +0000 | [diff] [blame] | 490 | ScratchFile vdex_file(vdex_location.c_str()); |
| 491 | ScratchFile odex_file(odex_location.c_str()); |
Richard Uhler | 2f27abd | 2017-01-31 14:02:34 +0000 | [diff] [blame] | 492 | |
| 493 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false); |
| 494 | EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch, |
| 495 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
| 496 | } |
| 497 | |
| 498 | // Case: We have a DEX file and up-to-date (OAT) VDEX file for it, but no OAT |
| 499 | // file. |
| 500 | TEST_F(OatFileAssistantTest, VdexUpToDateNoOat) { |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 501 | if (IsExecutedAsRoot()) { |
| 502 | // We cannot simulate non writable locations when executed as root: b/38000545. |
| 503 | LOG(ERROR) << "Test skipped because it's running as root"; |
| 504 | return; |
| 505 | } |
Richard Uhler | 2f27abd | 2017-01-31 14:02:34 +0000 | [diff] [blame] | 506 | |
| 507 | std::string dex_location = GetScratchDir() + "/VdexUpToDateNoOat.jar"; |
| 508 | std::string oat_location; |
| 509 | std::string error_msg; |
| 510 | ASSERT_TRUE(OatFileAssistant::DexLocationToOatFilename( |
| 511 | dex_location, kRuntimeISA, &oat_location, &error_msg)) << error_msg; |
| 512 | |
| 513 | Copy(GetDexSrc1(), dex_location); |
| 514 | GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed); |
| 515 | ASSERT_EQ(0, unlink(oat_location.c_str())); |
| 516 | |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 517 | ScopedNonWritable scoped_non_writable(dex_location); |
| 518 | ASSERT_TRUE(scoped_non_writable.IsSuccessful()); |
Richard Uhler | 9a37efc | 2016-08-05 16:32:55 -0700 | [diff] [blame] | 519 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false); |
| 520 | |
Richard Uhler | 2f27abd | 2017-01-31 14:02:34 +0000 | [diff] [blame] | 521 | // Even though the vdex file is up to date, because we don't have the oat |
| 522 | // file, we can't know that the vdex depends on the boot image and is up to |
| 523 | // date with respect to the boot image. Instead we must assume the vdex file |
| 524 | // depends on the boot image and is out of date with respect to the boot |
| 525 | // image. |
| 526 | EXPECT_EQ(OatFileAssistant::kDex2OatForBootImage, |
Richard Uhler | 9a37efc | 2016-08-05 16:32:55 -0700 | [diff] [blame] | 527 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
Richard Uhler | 9a37efc | 2016-08-05 16:32:55 -0700 | [diff] [blame] | 528 | } |
| 529 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 530 | // 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] | 531 | // Expect: The status is kNoDexOptNeeded if the profile hasn't changed, but |
| 532 | // kDex2Oat if the profile has changed. |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 533 | TEST_F(OatFileAssistantTest, ProfileOatUpToDate) { |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 534 | if (IsExecutedAsRoot()) { |
| 535 | // We cannot simulate non writable locations when executed as root: b/38000545. |
| 536 | LOG(ERROR) << "Test skipped because it's running as root"; |
| 537 | return; |
| 538 | } |
| 539 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 540 | std::string dex_location = GetScratchDir() + "/ProfileOatUpToDate.jar"; |
| 541 | Copy(GetDexSrc1(), dex_location); |
| 542 | GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeedProfile); |
| 543 | |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 544 | ScopedNonWritable scoped_non_writable(dex_location); |
| 545 | ASSERT_TRUE(scoped_non_writable.IsSuccessful()); |
| 546 | |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 547 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 548 | |
| 549 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 550 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeedProfile, false)); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 551 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
Nicolas Geoffray | 49cda06 | 2017-04-21 13:08:25 +0100 | [diff] [blame] | 552 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken, false)); |
Richard Uhler | 7225a8d | 2016-11-22 10:12:03 +0000 | [diff] [blame] | 553 | EXPECT_EQ(OatFileAssistant::kDex2OatForFilter, |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 554 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeedProfile, true)); |
Richard Uhler | 7225a8d | 2016-11-22 10:12:03 +0000 | [diff] [blame] | 555 | EXPECT_EQ(OatFileAssistant::kDex2OatForFilter, |
Nicolas Geoffray | 49cda06 | 2017-04-21 13:08:25 +0100 | [diff] [blame] | 556 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken, true)); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 557 | |
| 558 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 559 | EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus()); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 560 | EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus()); |
| 561 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
| 562 | } |
| 563 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 564 | // 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] | 565 | // Expect: The status is kNoDexOptNeeded and we load all dex files. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 566 | TEST_F(OatFileAssistantTest, MultiDexOatUpToDate) { |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 567 | if (IsExecutedAsRoot()) { |
| 568 | // We cannot simulate non writable locations when executed as root: b/38000545. |
| 569 | LOG(ERROR) << "Test skipped because it's running as root"; |
| 570 | return; |
| 571 | } |
| 572 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 573 | std::string dex_location = GetScratchDir() + "/MultiDexOatUpToDate.jar"; |
| 574 | Copy(GetMultiDexSrc1(), dex_location); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 575 | GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 576 | |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 577 | ScopedNonWritable scoped_non_writable(dex_location); |
| 578 | ASSERT_TRUE(scoped_non_writable.IsSuccessful()); |
| 579 | |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 580 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 581 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 582 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed, false)); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 583 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 584 | |
| 585 | // Verify we can load both dex files. |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 586 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 587 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 588 | EXPECT_TRUE(oat_file->IsExecutable()); |
| 589 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 590 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 591 | EXPECT_EQ(2u, dex_files.size()); |
| 592 | } |
| 593 | |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 594 | // Case: We have a MultiDEX file where the non-main multdex entry is out of date. |
Richard Uhler | 67ff7d1 | 2015-05-14 13:21:13 -0700 | [diff] [blame] | 595 | // Expect: The status is kDex2OatNeeded. |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 596 | TEST_F(OatFileAssistantTest, MultiDexNonMainOutOfDate) { |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 597 | if (IsExecutedAsRoot()) { |
| 598 | // We cannot simulate non writable locations when executed as root: b/38000545. |
| 599 | LOG(ERROR) << "Test skipped because it's running as root"; |
| 600 | return; |
| 601 | } |
| 602 | |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 603 | std::string dex_location = GetScratchDir() + "/MultiDexNonMainOutOfDate.jar"; |
Richard Uhler | 67ff7d1 | 2015-05-14 13:21:13 -0700 | [diff] [blame] | 604 | |
| 605 | // Compile code for GetMultiDexSrc1. |
| 606 | Copy(GetMultiDexSrc1(), dex_location); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 607 | GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed); |
Richard Uhler | 67ff7d1 | 2015-05-14 13:21:13 -0700 | [diff] [blame] | 608 | |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 609 | // Now overwrite the dex file with GetMultiDexSrc2 so the non-main checksum |
Richard Uhler | 67ff7d1 | 2015-05-14 13:21:13 -0700 | [diff] [blame] | 610 | // is out of date. |
| 611 | Copy(GetMultiDexSrc2(), dex_location); |
| 612 | |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 613 | ScopedNonWritable scoped_non_writable(dex_location); |
| 614 | ASSERT_TRUE(scoped_non_writable.IsSuccessful()); |
| 615 | |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 616 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true); |
Richard Uhler | 7225a8d | 2016-11-22 10:12:03 +0000 | [diff] [blame] | 617 | EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch, |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 618 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed, false)); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 619 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 67ff7d1 | 2015-05-14 13:21:13 -0700 | [diff] [blame] | 620 | } |
| 621 | |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 622 | // Case: We have a stripped MultiDEX file where the non-main multidex entry is |
| 623 | // out of date with respect to the odex file. |
| 624 | TEST_F(OatFileAssistantTest, StrippedMultiDexNonMainOutOfDate) { |
| 625 | std::string dex_location = GetScratchDir() + "/StrippedMultiDexNonMainOutOfDate.jar"; |
| 626 | std::string odex_location = GetOdexDir() + "/StrippedMultiDexNonMainOutOfDate.odex"; |
| 627 | |
| 628 | // Compile the oat from GetMultiDexSrc1. |
| 629 | Copy(GetMultiDexSrc1(), dex_location); |
| 630 | GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed); |
| 631 | |
| 632 | // Compile the odex from GetMultiDexSrc2, which has a different non-main |
| 633 | // dex checksum. |
| 634 | Copy(GetMultiDexSrc2(), dex_location); |
Nicolas Geoffray | 49cda06 | 2017-04-21 13:08:25 +0100 | [diff] [blame] | 635 | GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kQuicken); |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 636 | |
| 637 | // Strip the dex file. |
| 638 | Copy(GetStrippedDexSrc1(), dex_location); |
| 639 | |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 640 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, /*load_executable=*/false); |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 641 | |
| 642 | // Because the dex file is stripped, the odex file is considered the source |
| 643 | // of truth for the dex checksums. The oat file should be considered |
| 644 | // unusable. |
| 645 | std::unique_ptr<OatFile> best_file = oat_file_assistant.GetBestOatFile(); |
| 646 | ASSERT_TRUE(best_file.get() != nullptr); |
| 647 | EXPECT_EQ(best_file->GetLocation(), odex_location); |
| 648 | EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles()); |
| 649 | EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus()); |
| 650 | EXPECT_EQ(OatFileAssistant::kOatDexOutOfDate, oat_file_assistant.OatFileStatus()); |
| 651 | } |
| 652 | |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 653 | // Case: We have a DEX file and an OAT file out of date with respect to the |
| 654 | // dex checksum. |
| 655 | TEST_F(OatFileAssistantTest, OatDexOutOfDate) { |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 656 | if (IsExecutedAsRoot()) { |
| 657 | // We cannot simulate non writable locations when executed as root: b/38000545. |
| 658 | LOG(ERROR) << "Test skipped because it's running as root"; |
| 659 | return; |
| 660 | } |
| 661 | |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 662 | std::string dex_location = GetScratchDir() + "/OatDexOutOfDate.jar"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 663 | |
| 664 | // We create a dex, generate an oat for it, then overwrite the dex with a |
| 665 | // different dex to make the oat out of date. |
| 666 | Copy(GetDexSrc1(), dex_location); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 667 | GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 668 | Copy(GetDexSrc2(), dex_location); |
| 669 | |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 670 | ScopedNonWritable scoped_non_writable(dex_location); |
| 671 | ASSERT_TRUE(scoped_non_writable.IsSuccessful()); |
| 672 | |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 673 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false); |
Richard Uhler | 7225a8d | 2016-11-22 10:12:03 +0000 | [diff] [blame] | 674 | EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch, |
Nicolas Geoffray | 49cda06 | 2017-04-21 13:08:25 +0100 | [diff] [blame] | 675 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract)); |
Richard Uhler | 7225a8d | 2016-11-22 10:12:03 +0000 | [diff] [blame] | 676 | EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch, |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 677 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 678 | |
| 679 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 680 | EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus()); |
| 681 | EXPECT_EQ(OatFileAssistant::kOatDexOutOfDate, oat_file_assistant.OatFileStatus()); |
| 682 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
| 683 | } |
| 684 | |
Richard Uhler | 2f27abd | 2017-01-31 14:02:34 +0000 | [diff] [blame] | 685 | // Case: We have a DEX file and an (ODEX) VDEX file out of date with respect |
| 686 | // to the dex checksum, but no ODEX file. |
| 687 | TEST_F(OatFileAssistantTest, VdexDexOutOfDate) { |
Richard Uhler | 2f27abd | 2017-01-31 14:02:34 +0000 | [diff] [blame] | 688 | std::string dex_location = GetScratchDir() + "/VdexDexOutOfDate.jar"; |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 689 | std::string odex_location = GetOdexDir() + "/VdexDexOutOfDate.oat"; |
Richard Uhler | 2f27abd | 2017-01-31 14:02:34 +0000 | [diff] [blame] | 690 | |
| 691 | Copy(GetDexSrc1(), dex_location); |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 692 | GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed); |
| 693 | ASSERT_EQ(0, unlink(odex_location.c_str())); |
Richard Uhler | 2f27abd | 2017-01-31 14:02:34 +0000 | [diff] [blame] | 694 | Copy(GetDexSrc2(), dex_location); |
| 695 | |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 696 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false); |
Richard Uhler | 2f27abd | 2017-01-31 14:02:34 +0000 | [diff] [blame] | 697 | |
| 698 | EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch, |
| 699 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
| 700 | } |
| 701 | |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 702 | // Case: We have a MultiDEX (ODEX) VDEX file where the non-main multidex entry |
| 703 | // is out of date and there is no corresponding ODEX file. |
| 704 | TEST_F(OatFileAssistantTest, VdexMultiDexNonMainOutOfDate) { |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 705 | std::string dex_location = GetScratchDir() + "/VdexMultiDexNonMainOutOfDate.jar"; |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 706 | std::string odex_location = GetOdexDir() + "/VdexMultiDexNonMainOutOfDate.odex"; |
Richard Uhler | 2f27abd | 2017-01-31 14:02:34 +0000 | [diff] [blame] | 707 | |
| 708 | Copy(GetMultiDexSrc1(), dex_location); |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 709 | GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed); |
| 710 | ASSERT_EQ(0, unlink(odex_location.c_str())); |
Richard Uhler | 2f27abd | 2017-01-31 14:02:34 +0000 | [diff] [blame] | 711 | Copy(GetMultiDexSrc2(), dex_location); |
| 712 | |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 713 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false); |
Richard Uhler | 2f27abd | 2017-01-31 14:02:34 +0000 | [diff] [blame] | 714 | |
| 715 | EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch, |
| 716 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
| 717 | } |
| 718 | |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 719 | // Case: We have a DEX file and an OAT file out of date with respect to the |
| 720 | // boot image. |
| 721 | TEST_F(OatFileAssistantTest, OatImageOutOfDate) { |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 722 | if (IsExecutedAsRoot()) { |
| 723 | // We cannot simulate non writable locations when executed as root: b/38000545. |
| 724 | LOG(ERROR) << "Test skipped because it's running as root"; |
| 725 | return; |
| 726 | } |
| 727 | |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 728 | std::string dex_location = GetScratchDir() + "/OatImageOutOfDate.jar"; |
| 729 | |
| 730 | Copy(GetDexSrc1(), dex_location); |
| 731 | GenerateOatForTest(dex_location.c_str(), |
| 732 | CompilerFilter::kSpeed, |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 733 | /* with_alternate_image= */ true); |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 734 | |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 735 | ScopedNonWritable scoped_non_writable(dex_location); |
| 736 | ASSERT_TRUE(scoped_non_writable.IsSuccessful()); |
| 737 | |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 738 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false); |
Richard Uhler | 7225a8d | 2016-11-22 10:12:03 +0000 | [diff] [blame] | 739 | EXPECT_EQ(OatFileAssistant::kDex2OatForBootImage, |
Nicolas Geoffray | 49cda06 | 2017-04-21 13:08:25 +0100 | [diff] [blame] | 740 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract)); |
Richard Uhler | 7225a8d | 2016-11-22 10:12:03 +0000 | [diff] [blame] | 741 | EXPECT_EQ(OatFileAssistant::kDex2OatForBootImage, |
Nicolas Geoffray | 49cda06 | 2017-04-21 13:08:25 +0100 | [diff] [blame] | 742 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken)); |
Richard Uhler | 7225a8d | 2016-11-22 10:12:03 +0000 | [diff] [blame] | 743 | EXPECT_EQ(OatFileAssistant::kDex2OatForBootImage, |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 744 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
| 745 | |
| 746 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 747 | EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus()); |
| 748 | EXPECT_EQ(OatFileAssistant::kOatBootImageOutOfDate, oat_file_assistant.OatFileStatus()); |
| 749 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
| 750 | } |
| 751 | |
| 752 | // Case: We have a DEX file and a verify-at-runtime OAT file out of date with |
| 753 | // respect to the boot image. |
| 754 | // It shouldn't matter that the OAT file is out of date, because it is |
| 755 | // verify-at-runtime. |
| 756 | TEST_F(OatFileAssistantTest, OatVerifyAtRuntimeImageOutOfDate) { |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 757 | if (IsExecutedAsRoot()) { |
| 758 | // We cannot simulate non writable locations when executed as root: b/38000545. |
| 759 | LOG(ERROR) << "Test skipped because it's running as root"; |
| 760 | return; |
| 761 | } |
| 762 | |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 763 | std::string dex_location = GetScratchDir() + "/OatVerifyAtRuntimeImageOutOfDate.jar"; |
| 764 | |
| 765 | Copy(GetDexSrc1(), dex_location); |
| 766 | GenerateOatForTest(dex_location.c_str(), |
Nicolas Geoffray | 49cda06 | 2017-04-21 13:08:25 +0100 | [diff] [blame] | 767 | CompilerFilter::kExtract, |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 768 | /* with_alternate_image= */ true); |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 769 | |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 770 | ScopedNonWritable scoped_non_writable(dex_location); |
| 771 | ASSERT_TRUE(scoped_non_writable.IsSuccessful()); |
| 772 | |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 773 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false); |
| 774 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
Nicolas Geoffray | 49cda06 | 2017-04-21 13:08:25 +0100 | [diff] [blame] | 775 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract)); |
Richard Uhler | 7225a8d | 2016-11-22 10:12:03 +0000 | [diff] [blame] | 776 | EXPECT_EQ(OatFileAssistant::kDex2OatForFilter, |
Nicolas Geoffray | 49cda06 | 2017-04-21 13:08:25 +0100 | [diff] [blame] | 777 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken)); |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 778 | |
| 779 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 780 | EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus()); |
| 781 | EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 782 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 783 | } |
| 784 | |
| 785 | // Case: We have a DEX file and an ODEX file, but no OAT file. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 786 | TEST_F(OatFileAssistantTest, DexOdexNoOat) { |
| 787 | std::string dex_location = GetScratchDir() + "/DexOdexNoOat.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 788 | std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 789 | |
| 790 | // Create the dex and odex files |
| 791 | Copy(GetDexSrc1(), dex_location); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 792 | GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 793 | |
| 794 | // Verify the status. |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 795 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 796 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 797 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
Nicolas Geoffray | 49cda06 | 2017-04-21 13:08:25 +0100 | [diff] [blame] | 798 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract)); |
Vladimir Marko | e066932 | 2018-09-03 15:44:54 +0100 | [diff] [blame] | 799 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 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()); |
Vladimir Marko | e066932 | 2018-09-03 15:44:54 +0100 | [diff] [blame] | 803 | EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus()); |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 804 | EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 805 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 5f946da | 2015-07-17 12:28:32 -0700 | [diff] [blame] | 806 | |
| 807 | // We should still be able to get the non-executable odex file to run from. |
| 808 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 809 | ASSERT_TRUE(oat_file.get() != nullptr); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 810 | } |
| 811 | |
Richard Uhler | 5923b52 | 2016-12-08 09:48:01 +0000 | [diff] [blame] | 812 | // Case: We have a stripped DEX file and a PIC ODEX file, but no OAT file. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 813 | TEST_F(OatFileAssistantTest, StrippedDexOdexNoOat) { |
| 814 | std::string dex_location = GetScratchDir() + "/StrippedDexOdexNoOat.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 815 | std::string odex_location = GetOdexDir() + "/StrippedDexOdexNoOat.odex"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 816 | |
| 817 | // Create the dex and odex files |
| 818 | Copy(GetDexSrc1(), dex_location); |
Vladimir Marko | e066932 | 2018-09-03 15:44:54 +0100 | [diff] [blame] | 819 | GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 820 | |
| 821 | // Strip the dex file |
| 822 | Copy(GetStrippedDexSrc1(), dex_location); |
| 823 | |
| 824 | // Verify the status. |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 825 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 826 | |
Richard Uhler | 5923b52 | 2016-12-08 09:48:01 +0000 | [diff] [blame] | 827 | EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded, |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 828 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 829 | |
| 830 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
Richard Uhler | 5923b52 | 2016-12-08 09:48:01 +0000 | [diff] [blame] | 831 | EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus()); |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 832 | EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 833 | EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 834 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 835 | // Verify we can load the dex files from it. |
| 836 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 837 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 838 | EXPECT_TRUE(oat_file->IsExecutable()); |
| 839 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 840 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 841 | EXPECT_EQ(1u, dex_files.size()); |
| 842 | } |
| 843 | |
Richard Uhler | 5923b52 | 2016-12-08 09:48:01 +0000 | [diff] [blame] | 844 | // Case: We have a stripped DEX file, a PIC ODEX file, and an out-of-date OAT file. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 845 | TEST_F(OatFileAssistantTest, StrippedDexOdexOat) { |
| 846 | std::string dex_location = GetScratchDir() + "/StrippedDexOdexOat.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 847 | std::string odex_location = GetOdexDir() + "/StrippedDexOdexOat.odex"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 848 | |
| 849 | // Create the oat file from a different dex file so it looks out of date. |
| 850 | Copy(GetDexSrc2(), dex_location); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 851 | GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 852 | |
| 853 | // Create the odex file |
| 854 | Copy(GetDexSrc1(), dex_location); |
Vladimir Marko | e066932 | 2018-09-03 15:44:54 +0100 | [diff] [blame] | 855 | GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 856 | |
| 857 | // Strip the dex file. |
| 858 | Copy(GetStrippedDexSrc1(), dex_location); |
| 859 | |
| 860 | // Verify the status. |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 861 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 862 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 863 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
Nicolas Geoffray | 49cda06 | 2017-04-21 13:08:25 +0100 | [diff] [blame] | 864 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract)); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 865 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 866 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
Nicolas Geoffray | 08e9eed | 2017-04-25 17:36:51 +0100 | [diff] [blame] | 867 | EXPECT_EQ(-OatFileAssistant::kDex2OatForFilter, // Compiling from the .vdex file |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 868 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 869 | |
| 870 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
Richard Uhler | 5923b52 | 2016-12-08 09:48:01 +0000 | [diff] [blame] | 871 | EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus()); |
| 872 | EXPECT_EQ(OatFileAssistant::kOatDexOutOfDate, oat_file_assistant.OatFileStatus()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 873 | EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 874 | |
| 875 | // Verify we can load the dex files from it. |
| 876 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 877 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 878 | EXPECT_TRUE(oat_file->IsExecutable()); |
| 879 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 880 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 881 | EXPECT_EQ(1u, dex_files.size()); |
| 882 | } |
| 883 | |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 884 | // Case: We have a stripped (or resource-only) DEX file, no ODEX file and no |
| 885 | // OAT file. Expect: The status is kNoDexOptNeeded. |
| 886 | TEST_F(OatFileAssistantTest, ResourceOnlyDex) { |
| 887 | std::string dex_location = GetScratchDir() + "/ResourceOnlyDex.jar"; |
| 888 | |
| 889 | Copy(GetStrippedDexSrc1(), dex_location); |
| 890 | |
| 891 | // Verify the status. |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 892 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 893 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 894 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 895 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
| 896 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
Nicolas Geoffray | 49cda06 | 2017-04-21 13:08:25 +0100 | [diff] [blame] | 897 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract)); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 898 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
Nicolas Geoffray | 49cda06 | 2017-04-21 13:08:25 +0100 | [diff] [blame] | 899 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken)); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 900 | |
| 901 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 902 | EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus()); |
| 903 | EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 904 | EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles()); |
| 905 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 906 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 907 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 908 | |
| 909 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 910 | EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus()); |
| 911 | EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 912 | EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles()); |
| 913 | } |
| 914 | |
Vladimir Marko | e066932 | 2018-09-03 15:44:54 +0100 | [diff] [blame] | 915 | // Case: We have a DEX file, an ODEX file and an OAT file. |
| 916 | // Expect: It shouldn't crash. We should load the odex file executable. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 917 | TEST_F(OatFileAssistantTest, OdexOatOverlap) { |
| 918 | std::string dex_location = GetScratchDir() + "/OdexOatOverlap.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 919 | std::string odex_location = GetOdexDir() + "/OdexOatOverlap.odex"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 920 | |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 921 | // Create the dex, the odex and the oat files. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 922 | Copy(GetDexSrc1(), dex_location); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 923 | GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed); |
Vladimir Marko | e066932 | 2018-09-03 15:44:54 +0100 | [diff] [blame] | 924 | GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 925 | |
| 926 | // Verify things don't go bad. |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 927 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 928 | |
Vladimir Marko | e066932 | 2018-09-03 15:44:54 +0100 | [diff] [blame] | 929 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 930 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 931 | |
| 932 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
Vladimir Marko | e066932 | 2018-09-03 15:44:54 +0100 | [diff] [blame] | 933 | EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus()); |
| 934 | EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 935 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 936 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 937 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 938 | ASSERT_TRUE(oat_file.get() != nullptr); |
Richard Uhler | f16d572 | 2015-05-11 09:32:47 -0700 | [diff] [blame] | 939 | |
Vladimir Marko | e066932 | 2018-09-03 15:44:54 +0100 | [diff] [blame] | 940 | EXPECT_TRUE(oat_file->IsExecutable()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 941 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 942 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 943 | EXPECT_EQ(1u, dex_files.size()); |
| 944 | } |
| 945 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 946 | // Case: We have a DEX file and a VerifyAtRuntime ODEX file, but no OAT file. |
| 947 | // Expect: The status is kNoDexOptNeeded, because VerifyAtRuntime contains no code. |
| 948 | TEST_F(OatFileAssistantTest, DexVerifyAtRuntimeOdexNoOat) { |
| 949 | std::string dex_location = GetScratchDir() + "/DexVerifyAtRuntimeOdexNoOat.jar"; |
| 950 | std::string odex_location = GetOdexDir() + "/DexVerifyAtRuntimeOdexNoOat.odex"; |
David Brazdil | ce4b0ba | 2016-01-28 15:05:49 +0000 | [diff] [blame] | 951 | |
| 952 | // Create the dex and odex files |
| 953 | Copy(GetDexSrc1(), dex_location); |
Nicolas Geoffray | 49cda06 | 2017-04-21 13:08:25 +0100 | [diff] [blame] | 954 | GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kExtract); |
David Brazdil | ce4b0ba | 2016-01-28 15:05:49 +0000 | [diff] [blame] | 955 | |
| 956 | // Verify the status. |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 957 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false); |
David Brazdil | ce4b0ba | 2016-01-28 15:05:49 +0000 | [diff] [blame] | 958 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 959 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
Nicolas Geoffray | 49cda06 | 2017-04-21 13:08:25 +0100 | [diff] [blame] | 960 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract)); |
Richard Uhler | 7225a8d | 2016-11-22 10:12:03 +0000 | [diff] [blame] | 961 | EXPECT_EQ(-OatFileAssistant::kDex2OatForFilter, |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 962 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
David Brazdil | ce4b0ba | 2016-01-28 15:05:49 +0000 | [diff] [blame] | 963 | |
| 964 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
Richard Uhler | 3e580bc | 2016-11-08 16:23:07 +0000 | [diff] [blame] | 965 | EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus()); |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 966 | EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus()); |
David Brazdil | ce4b0ba | 2016-01-28 15:05:49 +0000 | [diff] [blame] | 967 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
| 968 | } |
| 969 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 970 | // Case: We have a DEX file and up-to-date OAT file for it. |
| 971 | // Expect: We should load an executable dex file. |
| 972 | TEST_F(OatFileAssistantTest, LoadOatUpToDate) { |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 973 | if (IsExecutedAsRoot()) { |
| 974 | // We cannot simulate non writable locations when executed as root: b/38000545. |
| 975 | LOG(ERROR) << "Test skipped because it's running as root"; |
| 976 | return; |
| 977 | } |
| 978 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 979 | std::string dex_location = GetScratchDir() + "/LoadOatUpToDate.jar"; |
| 980 | |
| 981 | Copy(GetDexSrc1(), dex_location); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 982 | GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 983 | |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 984 | ScopedNonWritable scoped_non_writable(dex_location); |
| 985 | ASSERT_TRUE(scoped_non_writable.IsSuccessful()); |
| 986 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 987 | // Load the oat using an oat file assistant. |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 988 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 989 | |
| 990 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 991 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 992 | EXPECT_TRUE(oat_file->IsExecutable()); |
| 993 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 994 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 995 | EXPECT_EQ(1u, dex_files.size()); |
| 996 | } |
| 997 | |
Nicolas Geoffray | 49cda06 | 2017-04-21 13:08:25 +0100 | [diff] [blame] | 998 | // Case: We have a DEX file and up-to-date quicken OAT file for it. |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 999 | // Expect: We should still load the oat file as executable. |
| 1000 | TEST_F(OatFileAssistantTest, LoadExecInterpretOnlyOatUpToDate) { |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 1001 | if (IsExecutedAsRoot()) { |
| 1002 | // We cannot simulate non writable locations when executed as root: b/38000545. |
| 1003 | LOG(ERROR) << "Test skipped because it's running as root"; |
| 1004 | return; |
| 1005 | } |
| 1006 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 1007 | std::string dex_location = GetScratchDir() + "/LoadExecInterpretOnlyOatUpToDate.jar"; |
| 1008 | |
| 1009 | Copy(GetDexSrc1(), dex_location); |
Nicolas Geoffray | 49cda06 | 2017-04-21 13:08:25 +0100 | [diff] [blame] | 1010 | GenerateOatForTest(dex_location.c_str(), CompilerFilter::kQuicken); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 1011 | |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 1012 | ScopedNonWritable scoped_non_writable(dex_location); |
| 1013 | ASSERT_TRUE(scoped_non_writable.IsSuccessful()); |
| 1014 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 1015 | // Load the oat using an oat file assistant. |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 1016 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1017 | |
| 1018 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 1019 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 1020 | EXPECT_TRUE(oat_file->IsExecutable()); |
| 1021 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 1022 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 1023 | EXPECT_EQ(1u, dex_files.size()); |
| 1024 | } |
| 1025 | |
| 1026 | // Case: We have a DEX file and up-to-date OAT file for it. |
| 1027 | // Expect: Loading non-executable should load the oat non-executable. |
| 1028 | TEST_F(OatFileAssistantTest, LoadNoExecOatUpToDate) { |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 1029 | if (IsExecutedAsRoot()) { |
| 1030 | // We cannot simulate non writable locations when executed as root: b/38000545. |
| 1031 | LOG(ERROR) << "Test skipped because it's running as root"; |
| 1032 | return; |
| 1033 | } |
| 1034 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1035 | std::string dex_location = GetScratchDir() + "/LoadNoExecOatUpToDate.jar"; |
| 1036 | |
| 1037 | Copy(GetDexSrc1(), dex_location); |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 1038 | |
| 1039 | ScopedNonWritable scoped_non_writable(dex_location); |
| 1040 | ASSERT_TRUE(scoped_non_writable.IsSuccessful()); |
| 1041 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 1042 | GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1043 | |
| 1044 | // Load the oat using an oat file assistant. |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 1045 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1046 | |
| 1047 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 1048 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 1049 | EXPECT_FALSE(oat_file->IsExecutable()); |
| 1050 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 1051 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 1052 | EXPECT_EQ(1u, dex_files.size()); |
| 1053 | } |
| 1054 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1055 | // Turn an absolute path into a path relative to the current working |
| 1056 | // directory. |
Andreas Gampe | ca620d7 | 2016-11-08 08:09:33 -0800 | [diff] [blame] | 1057 | static std::string MakePathRelative(const std::string& target) { |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1058 | char buf[MAXPATHLEN]; |
| 1059 | std::string cwd = getcwd(buf, MAXPATHLEN); |
| 1060 | |
| 1061 | // Split the target and cwd paths into components. |
| 1062 | std::vector<std::string> target_path; |
| 1063 | std::vector<std::string> cwd_path; |
| 1064 | Split(target, '/', &target_path); |
| 1065 | Split(cwd, '/', &cwd_path); |
| 1066 | |
| 1067 | // Reverse the path components, so we can use pop_back(). |
| 1068 | std::reverse(target_path.begin(), target_path.end()); |
| 1069 | std::reverse(cwd_path.begin(), cwd_path.end()); |
| 1070 | |
| 1071 | // Drop the common prefix of the paths. Because we reversed the path |
| 1072 | // components, this becomes the common suffix of target_path and cwd_path. |
| 1073 | while (!target_path.empty() && !cwd_path.empty() |
| 1074 | && target_path.back() == cwd_path.back()) { |
| 1075 | target_path.pop_back(); |
| 1076 | cwd_path.pop_back(); |
| 1077 | } |
| 1078 | |
| 1079 | // For each element of the remaining cwd_path, add '..' to the beginning |
| 1080 | // of the target path. Because we reversed the path components, we add to |
| 1081 | // the end of target_path. |
| 1082 | for (unsigned int i = 0; i < cwd_path.size(); i++) { |
| 1083 | target_path.push_back(".."); |
| 1084 | } |
| 1085 | |
| 1086 | // Reverse again to get the right path order, and join to get the result. |
| 1087 | std::reverse(target_path.begin(), target_path.end()); |
Andreas Gampe | 9186ced | 2016-12-12 14:28:21 -0800 | [diff] [blame] | 1088 | return android::base::Join(target_path, '/'); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1089 | } |
| 1090 | |
| 1091 | // Case: Non-absolute path to Dex location. |
| 1092 | // Expect: Not sure, but it shouldn't crash. |
| 1093 | TEST_F(OatFileAssistantTest, NonAbsoluteDexLocation) { |
| 1094 | std::string abs_dex_location = GetScratchDir() + "/NonAbsoluteDexLocation.jar"; |
| 1095 | Copy(GetDexSrc1(), abs_dex_location); |
| 1096 | |
| 1097 | std::string dex_location = MakePathRelative(abs_dex_location); |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 1098 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1099 | |
| 1100 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
Richard Uhler | 7225a8d | 2016-11-22 10:12:03 +0000 | [diff] [blame] | 1101 | EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch, |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 1102 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 1103 | EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus()); |
| 1104 | EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1105 | } |
| 1106 | |
| 1107 | // Case: Very short, non-existent Dex location. |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 1108 | // Expect: kNoDexOptNeeded. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1109 | TEST_F(OatFileAssistantTest, ShortDexLocation) { |
| 1110 | std::string dex_location = "/xx"; |
| 1111 | |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 1112 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1113 | |
| 1114 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 1115 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 1116 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 1117 | EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus()); |
| 1118 | EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 1119 | EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1120 | } |
| 1121 | |
| 1122 | // Case: Non-standard extension for dex file. |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 1123 | // Expect: The status is kDex2OatNeeded. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1124 | TEST_F(OatFileAssistantTest, LongDexExtension) { |
| 1125 | std::string dex_location = GetScratchDir() + "/LongDexExtension.jarx"; |
| 1126 | Copy(GetDexSrc1(), dex_location); |
| 1127 | |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 1128 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1129 | |
Richard Uhler | 7225a8d | 2016-11-22 10:12:03 +0000 | [diff] [blame] | 1130 | EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch, |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 1131 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1132 | |
| 1133 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 1134 | EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus()); |
| 1135 | EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1136 | } |
| 1137 | |
Nicolas Geoffray | 3d8a78a | 2018-08-29 21:10:16 +0000 | [diff] [blame] | 1138 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1139 | // A task to generate a dex location. Used by the RaceToGenerate test. |
| 1140 | class RaceGenerateTask : public Task { |
| 1141 | public: |
Nicolas Geoffray | 1e76d7a | 2018-09-03 13:23:34 +0100 | [diff] [blame] | 1142 | RaceGenerateTask(const std::string& dex_location, |
| 1143 | const std::string& oat_location, |
| 1144 | Mutex* lock) |
| 1145 | : dex_location_(dex_location), |
| 1146 | oat_location_(oat_location), |
| 1147 | lock_(lock), |
| 1148 | loaded_oat_file_(nullptr) |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1149 | {} |
| 1150 | |
Andreas Gampe | fa6a1b0 | 2018-09-07 08:11:55 -0700 | [diff] [blame] | 1151 | void Run(Thread* self ATTRIBUTE_UNUSED) override { |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1152 | // Load the dex files, and save a pointer to the loaded oat file, so that |
| 1153 | // 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] | 1154 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 1155 | std::vector<std::string> error_msgs; |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 1156 | const OatFile* oat_file = nullptr; |
Nicolas Geoffray | 3d8a78a | 2018-08-29 21:10:16 +0000 | [diff] [blame] | 1157 | { |
Nicolas Geoffray | 1e76d7a | 2018-09-03 13:23:34 +0100 | [diff] [blame] | 1158 | MutexLock mu(Thread::Current(), *lock_); |
Nicolas Geoffray | 3d8a78a | 2018-08-29 21:10:16 +0000 | [diff] [blame] | 1159 | // Create the oat file. |
| 1160 | std::vector<std::string> args; |
| 1161 | args.push_back("--dex-file=" + dex_location_); |
| 1162 | args.push_back("--oat-file=" + oat_location_); |
| 1163 | std::string error_msg; |
Nicolas Geoffray | 1e76d7a | 2018-09-03 13:23:34 +0100 | [diff] [blame] | 1164 | ASSERT_TRUE(DexoptTest::Dex2Oat(args, &error_msg)) << error_msg; |
Nicolas Geoffray | 3d8a78a | 2018-08-29 21:10:16 +0000 | [diff] [blame] | 1165 | } |
| 1166 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 1167 | dex_files = Runtime::Current()->GetOatFileManager().OpenDexFilesFromOat( |
| 1168 | dex_location_.c_str(), |
Jeff Hao | 0cb1728 | 2017-07-12 14:51:49 -0700 | [diff] [blame] | 1169 | Runtime::Current()->GetSystemClassLoader(), |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 1170 | /*dex_elements=*/nullptr, |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 1171 | &oat_file, |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 1172 | &error_msgs); |
Andreas Gampe | 9186ced | 2016-12-12 14:28:21 -0800 | [diff] [blame] | 1173 | CHECK(!dex_files.empty()) << android::base::Join(error_msgs, '\n'); |
Nicolas Geoffray | 3d8a78a | 2018-08-29 21:10:16 +0000 | [diff] [blame] | 1174 | if (dex_files[0]->GetOatDexFile() != nullptr) { |
| 1175 | loaded_oat_file_ = dex_files[0]->GetOatDexFile()->GetOatFile(); |
| 1176 | } |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 1177 | CHECK_EQ(loaded_oat_file_, oat_file); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1178 | } |
| 1179 | |
| 1180 | const OatFile* GetLoadedOatFile() const { |
| 1181 | return loaded_oat_file_; |
| 1182 | } |
| 1183 | |
| 1184 | private: |
| 1185 | std::string dex_location_; |
| 1186 | std::string oat_location_; |
Nicolas Geoffray | 1e76d7a | 2018-09-03 13:23:34 +0100 | [diff] [blame] | 1187 | Mutex* lock_; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1188 | const OatFile* loaded_oat_file_; |
| 1189 | }; |
| 1190 | |
Nicolas Geoffray | 3d8a78a | 2018-08-29 21:10:16 +0000 | [diff] [blame] | 1191 | // Test the case where dex2oat invocations race with multiple processes trying to |
| 1192 | // load the oat file. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1193 | TEST_F(OatFileAssistantTest, RaceToGenerate) { |
| 1194 | std::string dex_location = GetScratchDir() + "/RaceToGenerate.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 1195 | std::string oat_location = GetOdexDir() + "/RaceToGenerate.oat"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1196 | |
Jeff Hao | 0cb1728 | 2017-07-12 14:51:49 -0700 | [diff] [blame] | 1197 | // Start the runtime to initialize the system's class loader. |
| 1198 | Thread::Current()->TransitionFromSuspendedToRunnable(); |
| 1199 | runtime_->Start(); |
| 1200 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1201 | // We use the lib core dex file, because it's large, and hopefully should |
| 1202 | // take a while to generate. |
Narayan Kamath | d1ef436 | 2015-11-12 11:49:06 +0000 | [diff] [blame] | 1203 | Copy(GetLibCoreDexFileNames()[0], dex_location); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1204 | |
Nicolas Geoffray | 3d8a78a | 2018-08-29 21:10:16 +0000 | [diff] [blame] | 1205 | const size_t kNumThreads = 32; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1206 | Thread* self = Thread::Current(); |
| 1207 | ThreadPool thread_pool("Oat file assistant test thread pool", kNumThreads); |
| 1208 | std::vector<std::unique_ptr<RaceGenerateTask>> tasks; |
Nicolas Geoffray | 1e76d7a | 2018-09-03 13:23:34 +0100 | [diff] [blame] | 1209 | Mutex lock("RaceToGenerate"); |
Nicolas Geoffray | 3d8a78a | 2018-08-29 21:10:16 +0000 | [diff] [blame] | 1210 | for (size_t i = 0; i < kNumThreads; i++) { |
Nicolas Geoffray | 1e76d7a | 2018-09-03 13:23:34 +0100 | [diff] [blame] | 1211 | std::unique_ptr<RaceGenerateTask> task(new RaceGenerateTask(dex_location, oat_location, &lock)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1212 | thread_pool.AddTask(self, task.get()); |
| 1213 | tasks.push_back(std::move(task)); |
| 1214 | } |
| 1215 | thread_pool.StartWorkers(self); |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 1216 | thread_pool.Wait(self, /* do_work= */ true, /* may_hold_locks= */ false); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1217 | |
Nicolas Geoffray | 3d8a78a | 2018-08-29 21:10:16 +0000 | [diff] [blame] | 1218 | // Verify that tasks which got an oat file got a unique one. |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 1219 | std::set<const OatFile*> oat_files; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1220 | for (auto& task : tasks) { |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 1221 | const OatFile* oat_file = task->GetLoadedOatFile(); |
Nicolas Geoffray | 3d8a78a | 2018-08-29 21:10:16 +0000 | [diff] [blame] | 1222 | if (oat_file != nullptr) { |
| 1223 | EXPECT_TRUE(oat_files.find(oat_file) == oat_files.end()); |
| 1224 | oat_files.insert(oat_file); |
| 1225 | } |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1226 | } |
| 1227 | } |
| 1228 | |
Nicolas Geoffray | 3d8a78a | 2018-08-29 21:10:16 +0000 | [diff] [blame] | 1229 | // Case: We have a DEX file and an ODEX file, and no OAT file, |
Vladimir Marko | e066932 | 2018-09-03 15:44:54 +0100 | [diff] [blame] | 1230 | // Expect: We should load the odex file executable. |
Nicolas Geoffray | 3d8a78a | 2018-08-29 21:10:16 +0000 | [diff] [blame] | 1231 | TEST_F(DexoptTest, LoadDexOdexNoOat) { |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1232 | std::string dex_location = GetScratchDir() + "/LoadDexOdexNoOat.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 1233 | std::string odex_location = GetOdexDir() + "/LoadDexOdexNoOat.odex"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1234 | |
| 1235 | // Create the dex and odex files |
| 1236 | Copy(GetDexSrc1(), dex_location); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 1237 | GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1238 | |
| 1239 | // Load the oat using an executable oat file assistant. |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 1240 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1241 | |
| 1242 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 1243 | ASSERT_TRUE(oat_file.get() != nullptr); |
Vladimir Marko | e066932 | 2018-09-03 15:44:54 +0100 | [diff] [blame] | 1244 | EXPECT_TRUE(oat_file->IsExecutable()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1245 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 1246 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 1247 | EXPECT_EQ(1u, dex_files.size()); |
| 1248 | } |
| 1249 | |
Nicolas Geoffray | 3d8a78a | 2018-08-29 21:10:16 +0000 | [diff] [blame] | 1250 | // Case: We have a MultiDEX file and an ODEX file, and no OAT file. |
Vladimir Marko | e066932 | 2018-09-03 15:44:54 +0100 | [diff] [blame] | 1251 | // Expect: We should load the odex file executable. |
Nicolas Geoffray | 3d8a78a | 2018-08-29 21:10:16 +0000 | [diff] [blame] | 1252 | TEST_F(DexoptTest, LoadMultiDexOdexNoOat) { |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1253 | std::string dex_location = GetScratchDir() + "/LoadMultiDexOdexNoOat.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 1254 | std::string odex_location = GetOdexDir() + "/LoadMultiDexOdexNoOat.odex"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1255 | |
| 1256 | // Create the dex and odex files |
| 1257 | Copy(GetMultiDexSrc1(), dex_location); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 1258 | GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1259 | |
| 1260 | // Load the oat using an executable oat file assistant. |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 1261 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1262 | |
| 1263 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 1264 | ASSERT_TRUE(oat_file.get() != nullptr); |
Vladimir Marko | e066932 | 2018-09-03 15:44:54 +0100 | [diff] [blame] | 1265 | EXPECT_TRUE(oat_file->IsExecutable()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1266 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 1267 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 1268 | EXPECT_EQ(2u, dex_files.size()); |
| 1269 | } |
| 1270 | |
Richard Uhler | b81881d | 2016-04-19 13:08:04 -0700 | [diff] [blame] | 1271 | TEST(OatFileAssistantUtilsTest, DexLocationToOdexFilename) { |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1272 | std::string error_msg; |
| 1273 | std::string odex_file; |
| 1274 | |
Richard Uhler | b81881d | 2016-04-19 13:08:04 -0700 | [diff] [blame] | 1275 | EXPECT_TRUE(OatFileAssistant::DexLocationToOdexFilename( |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 1276 | "/foo/bar/baz.jar", InstructionSet::kArm, &odex_file, &error_msg)) << error_msg; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 1277 | EXPECT_EQ("/foo/bar/oat/arm/baz.odex", odex_file); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1278 | |
Richard Uhler | b81881d | 2016-04-19 13:08:04 -0700 | [diff] [blame] | 1279 | EXPECT_TRUE(OatFileAssistant::DexLocationToOdexFilename( |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 1280 | "/foo/bar/baz.funnyext", InstructionSet::kArm, &odex_file, &error_msg)) << error_msg; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 1281 | EXPECT_EQ("/foo/bar/oat/arm/baz.odex", odex_file); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1282 | |
Richard Uhler | b81881d | 2016-04-19 13:08:04 -0700 | [diff] [blame] | 1283 | EXPECT_FALSE(OatFileAssistant::DexLocationToOdexFilename( |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 1284 | "nopath.jar", InstructionSet::kArm, &odex_file, &error_msg)); |
Richard Uhler | b81881d | 2016-04-19 13:08:04 -0700 | [diff] [blame] | 1285 | EXPECT_FALSE(OatFileAssistant::DexLocationToOdexFilename( |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 1286 | "/foo/bar/baz_noext", InstructionSet::kArm, &odex_file, &error_msg)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1287 | } |
| 1288 | |
Richard Uhler | 23cedd2 | 2015-04-08 13:17:29 -0700 | [diff] [blame] | 1289 | // Verify the dexopt status values from dalvik.system.DexFile |
| 1290 | // match the OatFileAssistant::DexOptStatus values. |
| 1291 | TEST_F(OatFileAssistantTest, DexOptStatusValues) { |
Richard Uhler | 7225a8d | 2016-11-22 10:12:03 +0000 | [diff] [blame] | 1292 | std::pair<OatFileAssistant::DexOptNeeded, const char*> mapping[] = { |
| 1293 | {OatFileAssistant::kNoDexOptNeeded, "NO_DEXOPT_NEEDED"}, |
| 1294 | {OatFileAssistant::kDex2OatFromScratch, "DEX2OAT_FROM_SCRATCH"}, |
| 1295 | {OatFileAssistant::kDex2OatForBootImage, "DEX2OAT_FOR_BOOT_IMAGE"}, |
| 1296 | {OatFileAssistant::kDex2OatForFilter, "DEX2OAT_FOR_FILTER"}, |
Richard Uhler | 7225a8d | 2016-11-22 10:12:03 +0000 | [diff] [blame] | 1297 | }; |
| 1298 | |
Richard Uhler | 23cedd2 | 2015-04-08 13:17:29 -0700 | [diff] [blame] | 1299 | ScopedObjectAccess soa(Thread::Current()); |
| 1300 | StackHandleScope<1> hs(soa.Self()); |
| 1301 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
| 1302 | Handle<mirror::Class> dexfile( |
| 1303 | hs.NewHandle(linker->FindSystemClass(soa.Self(), "Ldalvik/system/DexFile;"))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 1304 | ASSERT_FALSE(dexfile == nullptr); |
Richard Uhler | 23cedd2 | 2015-04-08 13:17:29 -0700 | [diff] [blame] | 1305 | linker->EnsureInitialized(soa.Self(), dexfile, true, true); |
| 1306 | |
Richard Uhler | 7225a8d | 2016-11-22 10:12:03 +0000 | [diff] [blame] | 1307 | for (std::pair<OatFileAssistant::DexOptNeeded, const char*> field : mapping) { |
| 1308 | ArtField* art_field = mirror::Class::FindStaticField( |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 1309 | soa.Self(), dexfile.Get(), field.second, "I"); |
Richard Uhler | 7225a8d | 2016-11-22 10:12:03 +0000 | [diff] [blame] | 1310 | ASSERT_FALSE(art_field == nullptr); |
| 1311 | EXPECT_EQ(art_field->GetTypeAsPrimitiveType(), Primitive::kPrimInt); |
| 1312 | EXPECT_EQ(field.first, art_field->GetInt(dexfile.Get())); |
| 1313 | } |
Richard Uhler | 23cedd2 | 2015-04-08 13:17:29 -0700 | [diff] [blame] | 1314 | } |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1315 | |
Calin Juravle | 44e5efa | 2017-09-12 00:54:26 -0700 | [diff] [blame] | 1316 | TEST_F(OatFileAssistantTest, GetDexOptNeededWithOutOfDateContext) { |
| 1317 | std::string dex_location = GetScratchDir() + "/TestDex.jar"; |
| 1318 | std::string context_location = GetScratchDir() + "/ContextDex.jar"; |
| 1319 | Copy(GetDexSrc1(), dex_location); |
| 1320 | Copy(GetDexSrc2(), context_location); |
| 1321 | |
| 1322 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false); |
| 1323 | |
Calin Juravle | 44e5efa | 2017-09-12 00:54:26 -0700 | [diff] [blame] | 1324 | std::string error_msg; |
| 1325 | std::string context_str = "PCL[" + context_location + "]"; |
| 1326 | std::unique_ptr<ClassLoaderContext> context = ClassLoaderContext::Create(context_str); |
| 1327 | ASSERT_TRUE(context != nullptr); |
| 1328 | ASSERT_TRUE(context->OpenDexFiles(kRuntimeISA, "")); |
| 1329 | |
Calin Juravle | 44e5efa | 2017-09-12 00:54:26 -0700 | [diff] [blame] | 1330 | // Update the context by overriding the jar file. |
| 1331 | Copy(GetMultiDexSrc2(), context_location); |
| 1332 | std::unique_ptr<ClassLoaderContext> updated_context = ClassLoaderContext::Create(context_str); |
| 1333 | ASSERT_TRUE(updated_context != nullptr); |
| 1334 | // DexOptNeeded should advise compilation from scratch. |
| 1335 | EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch, |
| 1336 | oat_file_assistant.GetDexOptNeeded( |
Nicolas Geoffray | 3d8a78a | 2018-08-29 21:10:16 +0000 | [diff] [blame] | 1337 | CompilerFilter::kDefaultCompilerFilter, |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 1338 | /* profile_changed= */ false, |
| 1339 | /* downgrade= */ false, |
Nicolas Geoffray | 3d8a78a | 2018-08-29 21:10:16 +0000 | [diff] [blame] | 1340 | updated_context.get())); |
Nicolas Geoffray | 2974260 | 2017-12-14 10:09:03 +0000 | [diff] [blame] | 1341 | } |
| 1342 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1343 | // TODO: More Tests: |
| 1344 | // * Test class linker falls back to unquickened dex for DexNoOat |
| 1345 | // * Test class linker falls back to unquickened dex for MultiDexNoOat |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1346 | // * Test using secondary isa |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1347 | // * Test for status of oat while oat is being generated (how?) |
| 1348 | // * Test case where 32 and 64 bit boot class paths differ, |
| 1349 | // and we ask IsInBootClassPath for a class in exactly one of the 32 or |
| 1350 | // 64 bit boot class paths. |
| 1351 | // * Test unexpected scenarios (?): |
| 1352 | // - Dex is stripped, don't have odex. |
| 1353 | // - Oat file corrupted after status check, before reload unexecutable |
| 1354 | // because it's unrelocated and no dex2oat |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1355 | } // namespace art |