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