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