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