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