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