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