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