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