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