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