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