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