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