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 | 67ff7d1 | 2015-05-14 13:21:13 -0700 | [diff] [blame] | 240 | // Case: We have a MultiDEX file where the secondary dex file is out of date. |
| 241 | // Expect: The status is kDex2OatNeeded. |
| 242 | TEST_F(OatFileAssistantTest, MultiDexSecondaryOutOfDate) { |
| 243 | std::string dex_location = GetScratchDir() + "/MultiDexSecondaryOutOfDate.jar"; |
| 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 | |
| 249 | // Now overwrite the dex file with GetMultiDexSrc2 so the secondary checksum |
| 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 | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 259 | // Case: We have a MultiDEX file and up-to-date OAT file for it with relative |
| 260 | // encoded dex locations. |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 261 | // Expect: The oat file status is kNoDexOptNeeded. |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 262 | TEST_F(OatFileAssistantTest, RelativeEncodedDexLocation) { |
| 263 | std::string dex_location = GetScratchDir() + "/RelativeEncodedDexLocation.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 264 | std::string oat_location = GetOdexDir() + "/RelativeEncodedDexLocation.oat"; |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 265 | |
| 266 | // Create the dex file |
| 267 | Copy(GetMultiDexSrc1(), dex_location); |
| 268 | |
| 269 | // Create the oat file with relative encoded dex location. |
| 270 | std::vector<std::string> args; |
| 271 | args.push_back("--dex-file=" + dex_location); |
| 272 | args.push_back("--dex-location=" + std::string("RelativeEncodedDexLocation.jar")); |
| 273 | args.push_back("--oat-file=" + oat_location); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 274 | args.push_back("--compiler-filter=speed"); |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 275 | |
| 276 | std::string error_msg; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 277 | ASSERT_TRUE(OatFileAssistant::Dex2Oat(args, &error_msg)) << error_msg; |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 278 | |
| 279 | // Verify we can load both dex files. |
| 280 | OatFileAssistant oat_file_assistant(dex_location.c_str(), |
| 281 | oat_location.c_str(), |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 282 | kRuntimeISA, true); |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 283 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 284 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 285 | EXPECT_TRUE(oat_file->IsExecutable()); |
| 286 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 287 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 288 | EXPECT_EQ(2u, dex_files.size()); |
| 289 | } |
| 290 | |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 291 | // Case: We have a DEX file and an OAT file out of date with respect to the |
| 292 | // dex checksum. |
| 293 | TEST_F(OatFileAssistantTest, OatDexOutOfDate) { |
| 294 | std::string dex_location = GetScratchDir() + "/OatDexOutOfDate.jar"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 295 | |
| 296 | // We create a dex, generate an oat for it, then overwrite the dex with a |
| 297 | // different dex to make the oat out of date. |
| 298 | Copy(GetDexSrc1(), dex_location); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 299 | GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 300 | Copy(GetDexSrc2(), dex_location); |
| 301 | |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 302 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false); |
Richard Uhler | 7225a8d | 2016-11-22 10:12:03 +0000 | [diff] [blame] | 303 | EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch, |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 304 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime)); |
Richard Uhler | 7225a8d | 2016-11-22 10:12:03 +0000 | [diff] [blame] | 305 | EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch, |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 306 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 307 | |
| 308 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 309 | EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus()); |
| 310 | EXPECT_EQ(OatFileAssistant::kOatDexOutOfDate, oat_file_assistant.OatFileStatus()); |
| 311 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
| 312 | } |
| 313 | |
Richard Uhler | 2f27abd | 2017-01-31 14:02:34 +0000 | [diff] [blame] | 314 | // Case: We have a DEX file and an (ODEX) VDEX file out of date with respect |
| 315 | // to the dex checksum, but no ODEX file. |
| 316 | TEST_F(OatFileAssistantTest, VdexDexOutOfDate) { |
| 317 | // This test case is only meaningful if vdex is enabled. |
| 318 | if (!kIsVdexEnabled) { |
| 319 | return; |
| 320 | } |
| 321 | |
| 322 | std::string dex_location = GetScratchDir() + "/VdexDexOutOfDate.jar"; |
| 323 | std::string oat_location = GetOdexDir() + "/VdexDexOutOfDate.oat"; |
| 324 | |
| 325 | Copy(GetDexSrc1(), dex_location); |
| 326 | GenerateOdexForTest(dex_location, oat_location, CompilerFilter::kSpeed); |
| 327 | ASSERT_EQ(0, unlink(oat_location.c_str())); |
| 328 | Copy(GetDexSrc2(), dex_location); |
| 329 | |
| 330 | OatFileAssistant oat_file_assistant(dex_location.c_str(), |
| 331 | oat_location.c_str(), |
| 332 | kRuntimeISA, |
| 333 | false); |
| 334 | |
| 335 | EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch, |
| 336 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
| 337 | } |
| 338 | |
| 339 | // Case: We have a MultiDEX (ODEX) VDEX file where the secondary dex file is |
| 340 | // out of date and there is no corresponding ODEX file. |
| 341 | TEST_F(OatFileAssistantTest, VdexMultiDexSecondaryOutOfDate) { |
| 342 | // This test case is only meaningful if vdex is enabled. |
| 343 | if (!kIsVdexEnabled) { |
| 344 | return; |
| 345 | } |
| 346 | |
| 347 | std::string dex_location = GetScratchDir() + "/VdexMultiDexSecondaryOutOfDate.jar"; |
| 348 | std::string oat_location = GetOdexDir() + "/VdexMultiDexSecondaryOutOfDate.oat"; |
| 349 | |
| 350 | Copy(GetMultiDexSrc1(), dex_location); |
| 351 | GenerateOdexForTest(dex_location, oat_location, CompilerFilter::kSpeed); |
| 352 | ASSERT_EQ(0, unlink(oat_location.c_str())); |
| 353 | Copy(GetMultiDexSrc2(), dex_location); |
| 354 | |
| 355 | OatFileAssistant oat_file_assistant(dex_location.c_str(), |
| 356 | oat_location.c_str(), |
| 357 | kRuntimeISA, |
| 358 | false); |
| 359 | |
| 360 | EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch, |
| 361 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
| 362 | } |
| 363 | |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 364 | // Case: We have a DEX file and an OAT file out of date with respect to the |
| 365 | // boot image. |
| 366 | TEST_F(OatFileAssistantTest, OatImageOutOfDate) { |
| 367 | std::string dex_location = GetScratchDir() + "/OatImageOutOfDate.jar"; |
| 368 | |
| 369 | Copy(GetDexSrc1(), dex_location); |
| 370 | GenerateOatForTest(dex_location.c_str(), |
| 371 | CompilerFilter::kSpeed, |
| 372 | /*relocate*/true, |
| 373 | /*pic*/false, |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 374 | /*with_alternate_image*/true); |
| 375 | |
| 376 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false); |
Richard Uhler | 7225a8d | 2016-11-22 10:12:03 +0000 | [diff] [blame] | 377 | EXPECT_EQ(OatFileAssistant::kDex2OatForBootImage, |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 378 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime)); |
Richard Uhler | 7225a8d | 2016-11-22 10:12:03 +0000 | [diff] [blame] | 379 | EXPECT_EQ(OatFileAssistant::kDex2OatForBootImage, |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 380 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly)); |
Richard Uhler | 7225a8d | 2016-11-22 10:12:03 +0000 | [diff] [blame] | 381 | EXPECT_EQ(OatFileAssistant::kDex2OatForBootImage, |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 382 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
| 383 | |
| 384 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 385 | EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus()); |
| 386 | EXPECT_EQ(OatFileAssistant::kOatBootImageOutOfDate, oat_file_assistant.OatFileStatus()); |
| 387 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
| 388 | } |
| 389 | |
| 390 | // Case: We have a DEX file and a verify-at-runtime OAT file out of date with |
| 391 | // respect to the boot image. |
| 392 | // It shouldn't matter that the OAT file is out of date, because it is |
| 393 | // verify-at-runtime. |
| 394 | TEST_F(OatFileAssistantTest, OatVerifyAtRuntimeImageOutOfDate) { |
| 395 | std::string dex_location = GetScratchDir() + "/OatVerifyAtRuntimeImageOutOfDate.jar"; |
| 396 | |
| 397 | Copy(GetDexSrc1(), dex_location); |
| 398 | GenerateOatForTest(dex_location.c_str(), |
| 399 | CompilerFilter::kVerifyAtRuntime, |
| 400 | /*relocate*/true, |
| 401 | /*pic*/false, |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 402 | /*with_alternate_image*/true); |
| 403 | |
| 404 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false); |
| 405 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 406 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime)); |
Richard Uhler | 7225a8d | 2016-11-22 10:12:03 +0000 | [diff] [blame] | 407 | EXPECT_EQ(OatFileAssistant::kDex2OatForFilter, |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 408 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly)); |
| 409 | |
| 410 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 411 | EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus()); |
| 412 | EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 413 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | // 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] | 417 | TEST_F(OatFileAssistantTest, DexOdexNoOat) { |
| 418 | std::string dex_location = GetScratchDir() + "/DexOdexNoOat.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 419 | std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 420 | |
| 421 | // Create the dex and odex files |
| 422 | Copy(GetDexSrc1(), dex_location); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 423 | GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 424 | |
| 425 | // Verify the status. |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 426 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 427 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 428 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 429 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime)); |
Richard Uhler | 5923b52 | 2016-12-08 09:48:01 +0000 | [diff] [blame] | 430 | EXPECT_EQ(-OatFileAssistant::kDex2OatForRelocation, |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 431 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 432 | |
| 433 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 434 | EXPECT_EQ(OatFileAssistant::kOatRelocationOutOfDate, oat_file_assistant.OdexFileStatus()); |
| 435 | EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 436 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 5f946da | 2015-07-17 12:28:32 -0700 | [diff] [blame] | 437 | |
| 438 | // We should still be able to get the non-executable odex file to run from. |
| 439 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 440 | ASSERT_TRUE(oat_file.get() != nullptr); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 441 | } |
| 442 | |
Richard Uhler | 5923b52 | 2016-12-08 09:48:01 +0000 | [diff] [blame] | 443 | // 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] | 444 | TEST_F(OatFileAssistantTest, StrippedDexOdexNoOat) { |
| 445 | std::string dex_location = GetScratchDir() + "/StrippedDexOdexNoOat.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 446 | std::string odex_location = GetOdexDir() + "/StrippedDexOdexNoOat.odex"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 447 | |
| 448 | // Create the dex and odex files |
| 449 | Copy(GetDexSrc1(), dex_location); |
Richard Uhler | 5923b52 | 2016-12-08 09:48:01 +0000 | [diff] [blame] | 450 | GeneratePicOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 451 | |
| 452 | // Strip the dex file |
| 453 | Copy(GetStrippedDexSrc1(), dex_location); |
| 454 | |
| 455 | // Verify the status. |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 456 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 457 | |
Richard Uhler | 5923b52 | 2016-12-08 09:48:01 +0000 | [diff] [blame] | 458 | EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded, |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 459 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 460 | |
| 461 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
Richard Uhler | 5923b52 | 2016-12-08 09:48:01 +0000 | [diff] [blame] | 462 | EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus()); |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 463 | EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 464 | EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 465 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 466 | // Verify we can load the dex files from it. |
| 467 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 468 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 469 | EXPECT_TRUE(oat_file->IsExecutable()); |
| 470 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 471 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 472 | EXPECT_EQ(1u, dex_files.size()); |
| 473 | } |
| 474 | |
Richard Uhler | 5923b52 | 2016-12-08 09:48:01 +0000 | [diff] [blame] | 475 | // 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] | 476 | TEST_F(OatFileAssistantTest, StrippedDexOdexOat) { |
| 477 | std::string dex_location = GetScratchDir() + "/StrippedDexOdexOat.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 478 | std::string odex_location = GetOdexDir() + "/StrippedDexOdexOat.odex"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 479 | |
| 480 | // Create the oat file from a different dex file so it looks out of date. |
| 481 | Copy(GetDexSrc2(), dex_location); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 482 | GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 483 | |
| 484 | // Create the odex file |
| 485 | Copy(GetDexSrc1(), dex_location); |
Richard Uhler | 5923b52 | 2016-12-08 09:48:01 +0000 | [diff] [blame] | 486 | GeneratePicOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 487 | |
| 488 | // Strip the dex file. |
| 489 | Copy(GetStrippedDexSrc1(), dex_location); |
| 490 | |
| 491 | // Verify the status. |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 492 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 493 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 494 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 495 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime)); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 496 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 497 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
| 498 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, // Can't run dex2oat because dex file is stripped. |
| 499 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 500 | |
| 501 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
Richard Uhler | 5923b52 | 2016-12-08 09:48:01 +0000 | [diff] [blame] | 502 | EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus()); |
| 503 | EXPECT_EQ(OatFileAssistant::kOatDexOutOfDate, oat_file_assistant.OatFileStatus()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 504 | EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 505 | |
| 506 | // Verify we can load the dex files from it. |
| 507 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 508 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 509 | EXPECT_TRUE(oat_file->IsExecutable()); |
| 510 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 511 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 512 | EXPECT_EQ(1u, dex_files.size()); |
| 513 | } |
| 514 | |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 515 | // Case: We have a stripped (or resource-only) DEX file, no ODEX file and no |
| 516 | // OAT file. Expect: The status is kNoDexOptNeeded. |
| 517 | TEST_F(OatFileAssistantTest, ResourceOnlyDex) { |
| 518 | std::string dex_location = GetScratchDir() + "/ResourceOnlyDex.jar"; |
| 519 | |
| 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 | 9b994ea | 2015-06-24 08:44:19 -0700 | [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::kSpeed)); |
| 527 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 528 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime)); |
| 529 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 530 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly)); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 531 | |
| 532 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 533 | EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus()); |
| 534 | EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 535 | EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles()); |
| 536 | |
| 537 | // Make the oat file up to date. This should have no effect. |
| 538 | std::string error_msg; |
Richard Uhler | f4b3487 | 2016-04-13 11:03:46 -0700 | [diff] [blame] | 539 | Runtime::Current()->AddCompilerOption("--compiler-filter=speed"); |
Richard Uhler | 1e86061 | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 540 | EXPECT_EQ(OatFileAssistant::kUpdateSucceeded, |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 541 | oat_file_assistant.MakeUpToDate(false, &error_msg)) << error_msg; |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 542 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 543 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 544 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 545 | |
| 546 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 547 | EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus()); |
| 548 | EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 549 | EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles()); |
| 550 | } |
| 551 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 552 | // Case: We have a DEX file, an ODEX file and an OAT file, where the ODEX and |
| 553 | // OAT files both have patch delta of 0. |
Richard Uhler | 5923b52 | 2016-12-08 09:48:01 +0000 | [diff] [blame] | 554 | // Expect: It shouldn't crash. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 555 | TEST_F(OatFileAssistantTest, OdexOatOverlap) { |
| 556 | std::string dex_location = GetScratchDir() + "/OdexOatOverlap.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 557 | std::string odex_location = GetOdexDir() + "/OdexOatOverlap.odex"; |
| 558 | std::string oat_location = GetOdexDir() + "/OdexOatOverlap.oat"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 559 | |
| 560 | // Create the dex and odex files |
| 561 | Copy(GetDexSrc1(), dex_location); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 562 | GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 563 | |
| 564 | // Create the oat file by copying the odex so they are located in the same |
| 565 | // place in memory. |
| 566 | Copy(odex_location, oat_location); |
| 567 | |
| 568 | // Verify things don't go bad. |
| 569 | OatFileAssistant oat_file_assistant(dex_location.c_str(), |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 570 | oat_location.c_str(), kRuntimeISA, true); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 571 | |
Richard Uhler | 5923b52 | 2016-12-08 09:48:01 +0000 | [diff] [blame] | 572 | // kDex2OatForRelocation is expected rather than -kDex2OatForRelocation |
Richard Uhler | 7225a8d | 2016-11-22 10:12:03 +0000 | [diff] [blame] | 573 | // 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] | 574 | // location, even if they both need relocation. |
Richard Uhler | 5923b52 | 2016-12-08 09:48:01 +0000 | [diff] [blame] | 575 | EXPECT_EQ(OatFileAssistant::kDex2OatForRelocation, |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 576 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 577 | |
| 578 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 579 | EXPECT_EQ(OatFileAssistant::kOatRelocationOutOfDate, oat_file_assistant.OdexFileStatus()); |
| 580 | EXPECT_EQ(OatFileAssistant::kOatRelocationOutOfDate, oat_file_assistant.OatFileStatus()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 581 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 582 | |
| 583 | // Things aren't relocated, so it should fall back to interpreted. |
| 584 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 585 | ASSERT_TRUE(oat_file.get() != nullptr); |
Richard Uhler | f16d572 | 2015-05-11 09:32:47 -0700 | [diff] [blame] | 586 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 587 | EXPECT_FALSE(oat_file->IsExecutable()); |
| 588 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 589 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 590 | EXPECT_EQ(1u, dex_files.size()); |
| 591 | } |
| 592 | |
| 593 | // 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] | 594 | // Expect: The status is kNoDexOptNeeded, because PIC needs no relocation. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 595 | TEST_F(OatFileAssistantTest, DexPicOdexNoOat) { |
| 596 | std::string dex_location = GetScratchDir() + "/DexPicOdexNoOat.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 597 | std::string odex_location = GetOdexDir() + "/DexPicOdexNoOat.odex"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 598 | |
| 599 | // Create the dex and odex files |
| 600 | Copy(GetDexSrc1(), dex_location); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 601 | GeneratePicOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 602 | |
| 603 | // Verify the status. |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 604 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 605 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 606 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 607 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
Richard Uhler | 7225a8d | 2016-11-22 10:12:03 +0000 | [diff] [blame] | 608 | EXPECT_EQ(-OatFileAssistant::kDex2OatForFilter, |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 609 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 610 | |
| 611 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
Richard Uhler | 3e580bc | 2016-11-08 16:23:07 +0000 | [diff] [blame] | 612 | EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus()); |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 613 | EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 614 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 615 | } |
| 616 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 617 | // Case: We have a DEX file and a VerifyAtRuntime ODEX file, but no OAT file. |
| 618 | // Expect: The status is kNoDexOptNeeded, because VerifyAtRuntime contains no code. |
| 619 | TEST_F(OatFileAssistantTest, DexVerifyAtRuntimeOdexNoOat) { |
| 620 | std::string dex_location = GetScratchDir() + "/DexVerifyAtRuntimeOdexNoOat.jar"; |
| 621 | std::string odex_location = GetOdexDir() + "/DexVerifyAtRuntimeOdexNoOat.odex"; |
David Brazdil | ce4b0ba | 2016-01-28 15:05:49 +0000 | [diff] [blame] | 622 | |
| 623 | // Create the dex and odex files |
| 624 | Copy(GetDexSrc1(), dex_location); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 625 | GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kVerifyAtRuntime); |
David Brazdil | ce4b0ba | 2016-01-28 15:05:49 +0000 | [diff] [blame] | 626 | |
| 627 | // Verify the status. |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 628 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false); |
David Brazdil | ce4b0ba | 2016-01-28 15:05:49 +0000 | [diff] [blame] | 629 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 630 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 631 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime)); |
Richard Uhler | 7225a8d | 2016-11-22 10:12:03 +0000 | [diff] [blame] | 632 | EXPECT_EQ(-OatFileAssistant::kDex2OatForFilter, |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 633 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
David Brazdil | ce4b0ba | 2016-01-28 15:05:49 +0000 | [diff] [blame] | 634 | |
| 635 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
Richard Uhler | 3e580bc | 2016-11-08 16:23:07 +0000 | [diff] [blame] | 636 | EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus()); |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 637 | EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus()); |
David Brazdil | ce4b0ba | 2016-01-28 15:05:49 +0000 | [diff] [blame] | 638 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
| 639 | } |
| 640 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 641 | // Case: We have a DEX file and up-to-date OAT file for it. |
| 642 | // Expect: We should load an executable dex file. |
| 643 | TEST_F(OatFileAssistantTest, LoadOatUpToDate) { |
| 644 | std::string dex_location = GetScratchDir() + "/LoadOatUpToDate.jar"; |
| 645 | |
| 646 | Copy(GetDexSrc1(), dex_location); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 647 | GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 648 | |
| 649 | // Load the oat using an oat file assistant. |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 650 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 651 | |
| 652 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 653 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 654 | EXPECT_TRUE(oat_file->IsExecutable()); |
| 655 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 656 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 657 | EXPECT_EQ(1u, dex_files.size()); |
| 658 | } |
| 659 | |
| 660 | // Case: We have a DEX file and up-to-date interpret-only OAT file for it. |
| 661 | // Expect: We should still load the oat file as executable. |
| 662 | TEST_F(OatFileAssistantTest, LoadExecInterpretOnlyOatUpToDate) { |
| 663 | std::string dex_location = GetScratchDir() + "/LoadExecInterpretOnlyOatUpToDate.jar"; |
| 664 | |
| 665 | Copy(GetDexSrc1(), dex_location); |
| 666 | GenerateOatForTest(dex_location.c_str(), CompilerFilter::kInterpretOnly); |
| 667 | |
| 668 | // Load the oat using an oat file assistant. |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 669 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 670 | |
| 671 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 672 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 673 | EXPECT_TRUE(oat_file->IsExecutable()); |
| 674 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 675 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 676 | EXPECT_EQ(1u, dex_files.size()); |
| 677 | } |
| 678 | |
| 679 | // Case: We have a DEX file and up-to-date OAT file for it. |
| 680 | // Expect: Loading non-executable should load the oat non-executable. |
| 681 | TEST_F(OatFileAssistantTest, LoadNoExecOatUpToDate) { |
| 682 | std::string dex_location = GetScratchDir() + "/LoadNoExecOatUpToDate.jar"; |
| 683 | |
| 684 | Copy(GetDexSrc1(), dex_location); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 685 | GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 686 | |
| 687 | // Load the oat using an oat file assistant. |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 688 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 689 | |
| 690 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 691 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 692 | EXPECT_FALSE(oat_file->IsExecutable()); |
| 693 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 694 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 695 | EXPECT_EQ(1u, dex_files.size()); |
| 696 | } |
| 697 | |
| 698 | // Case: We have a DEX file. |
| 699 | // Expect: We should load an executable dex file from an alternative oat |
| 700 | // location. |
| 701 | TEST_F(OatFileAssistantTest, LoadDexNoAlternateOat) { |
| 702 | std::string dex_location = GetScratchDir() + "/LoadDexNoAlternateOat.jar"; |
| 703 | std::string oat_location = GetScratchDir() + "/LoadDexNoAlternateOat.oat"; |
| 704 | |
| 705 | Copy(GetDexSrc1(), dex_location); |
| 706 | |
| 707 | OatFileAssistant oat_file_assistant( |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 708 | dex_location.c_str(), oat_location.c_str(), kRuntimeISA, true); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 709 | std::string error_msg; |
Richard Uhler | f4b3487 | 2016-04-13 11:03:46 -0700 | [diff] [blame] | 710 | Runtime::Current()->AddCompilerOption("--compiler-filter=speed"); |
Richard Uhler | 1e86061 | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 711 | ASSERT_EQ(OatFileAssistant::kUpdateSucceeded, |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 712 | oat_file_assistant.MakeUpToDate(false, &error_msg)) << error_msg; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 713 | |
| 714 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 715 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 716 | EXPECT_TRUE(oat_file->IsExecutable()); |
| 717 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 718 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 719 | EXPECT_EQ(1u, dex_files.size()); |
| 720 | |
| 721 | EXPECT_TRUE(OS::FileExists(oat_location.c_str())); |
| 722 | |
| 723 | // Verify it didn't create an oat in the default location. |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 724 | OatFileAssistant ofm(dex_location.c_str(), kRuntimeISA, false); |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 725 | EXPECT_EQ(OatFileAssistant::kOatCannotOpen, ofm.OatFileStatus()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 726 | } |
| 727 | |
Richard Uhler | 8327cf7 | 2015-10-13 16:34:59 -0700 | [diff] [blame] | 728 | // Case: We have a DEX file but can't write the oat file. |
| 729 | // Expect: We should fail to make the oat file up to date. |
| 730 | TEST_F(OatFileAssistantTest, LoadDexUnwriteableAlternateOat) { |
| 731 | std::string dex_location = GetScratchDir() + "/LoadDexUnwriteableAlternateOat.jar"; |
| 732 | |
| 733 | // Make the oat location unwritable by inserting some non-existent |
| 734 | // intermediate directories. |
| 735 | std::string oat_location = GetScratchDir() + "/foo/bar/LoadDexUnwriteableAlternateOat.oat"; |
| 736 | |
| 737 | Copy(GetDexSrc1(), dex_location); |
| 738 | |
| 739 | OatFileAssistant oat_file_assistant( |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 740 | dex_location.c_str(), oat_location.c_str(), kRuntimeISA, true); |
Richard Uhler | 8327cf7 | 2015-10-13 16:34:59 -0700 | [diff] [blame] | 741 | std::string error_msg; |
Richard Uhler | f4b3487 | 2016-04-13 11:03:46 -0700 | [diff] [blame] | 742 | Runtime::Current()->AddCompilerOption("--compiler-filter=speed"); |
Richard Uhler | 1e86061 | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 743 | ASSERT_EQ(OatFileAssistant::kUpdateNotAttempted, |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 744 | oat_file_assistant.MakeUpToDate(false, &error_msg)); |
Richard Uhler | 8327cf7 | 2015-10-13 16:34:59 -0700 | [diff] [blame] | 745 | |
| 746 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 747 | ASSERT_TRUE(oat_file.get() == nullptr); |
| 748 | } |
| 749 | |
| 750 | // Case: We don't have a DEX file and can't write the oat file. |
| 751 | // Expect: We should fail to generate the oat file without crashing. |
| 752 | TEST_F(OatFileAssistantTest, GenNoDex) { |
| 753 | std::string dex_location = GetScratchDir() + "/GenNoDex.jar"; |
| 754 | std::string oat_location = GetScratchDir() + "/GenNoDex.oat"; |
| 755 | |
| 756 | OatFileAssistant oat_file_assistant( |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 757 | dex_location.c_str(), oat_location.c_str(), kRuntimeISA, true); |
Richard Uhler | 8327cf7 | 2015-10-13 16:34:59 -0700 | [diff] [blame] | 758 | std::string error_msg; |
Richard Uhler | f4b3487 | 2016-04-13 11:03:46 -0700 | [diff] [blame] | 759 | Runtime::Current()->AddCompilerOption("--compiler-filter=speed"); |
Richard Uhler | 1e86061 | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 760 | EXPECT_EQ(OatFileAssistant::kUpdateNotAttempted, |
Richard Uhler | f4b3487 | 2016-04-13 11:03:46 -0700 | [diff] [blame] | 761 | oat_file_assistant.GenerateOatFile(&error_msg)); |
Richard Uhler | 8327cf7 | 2015-10-13 16:34:59 -0700 | [diff] [blame] | 762 | } |
| 763 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 764 | // Turn an absolute path into a path relative to the current working |
| 765 | // directory. |
Andreas Gampe | ca620d7 | 2016-11-08 08:09:33 -0800 | [diff] [blame] | 766 | static std::string MakePathRelative(const std::string& target) { |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 767 | char buf[MAXPATHLEN]; |
| 768 | std::string cwd = getcwd(buf, MAXPATHLEN); |
| 769 | |
| 770 | // Split the target and cwd paths into components. |
| 771 | std::vector<std::string> target_path; |
| 772 | std::vector<std::string> cwd_path; |
| 773 | Split(target, '/', &target_path); |
| 774 | Split(cwd, '/', &cwd_path); |
| 775 | |
| 776 | // Reverse the path components, so we can use pop_back(). |
| 777 | std::reverse(target_path.begin(), target_path.end()); |
| 778 | std::reverse(cwd_path.begin(), cwd_path.end()); |
| 779 | |
| 780 | // Drop the common prefix of the paths. Because we reversed the path |
| 781 | // components, this becomes the common suffix of target_path and cwd_path. |
| 782 | while (!target_path.empty() && !cwd_path.empty() |
| 783 | && target_path.back() == cwd_path.back()) { |
| 784 | target_path.pop_back(); |
| 785 | cwd_path.pop_back(); |
| 786 | } |
| 787 | |
| 788 | // For each element of the remaining cwd_path, add '..' to the beginning |
| 789 | // of the target path. Because we reversed the path components, we add to |
| 790 | // the end of target_path. |
| 791 | for (unsigned int i = 0; i < cwd_path.size(); i++) { |
| 792 | target_path.push_back(".."); |
| 793 | } |
| 794 | |
| 795 | // Reverse again to get the right path order, and join to get the result. |
| 796 | std::reverse(target_path.begin(), target_path.end()); |
Andreas Gampe | 9186ced | 2016-12-12 14:28:21 -0800 | [diff] [blame] | 797 | return android::base::Join(target_path, '/'); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 798 | } |
| 799 | |
| 800 | // Case: Non-absolute path to Dex location. |
| 801 | // Expect: Not sure, but it shouldn't crash. |
| 802 | TEST_F(OatFileAssistantTest, NonAbsoluteDexLocation) { |
| 803 | std::string abs_dex_location = GetScratchDir() + "/NonAbsoluteDexLocation.jar"; |
| 804 | Copy(GetDexSrc1(), abs_dex_location); |
| 805 | |
| 806 | std::string dex_location = MakePathRelative(abs_dex_location); |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 807 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 808 | |
| 809 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
Richard Uhler | 7225a8d | 2016-11-22 10:12:03 +0000 | [diff] [blame] | 810 | EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch, |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 811 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 812 | EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus()); |
| 813 | EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | // Case: Very short, non-existent Dex location. |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 817 | // Expect: kNoDexOptNeeded. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 818 | TEST_F(OatFileAssistantTest, ShortDexLocation) { |
| 819 | std::string dex_location = "/xx"; |
| 820 | |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 821 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 822 | |
| 823 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 824 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 825 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 826 | EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus()); |
| 827 | EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 828 | EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 829 | |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 830 | // Trying to make it up to date should have no effect. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 831 | std::string error_msg; |
Richard Uhler | f4b3487 | 2016-04-13 11:03:46 -0700 | [diff] [blame] | 832 | Runtime::Current()->AddCompilerOption("--compiler-filter=speed"); |
Richard Uhler | 1e86061 | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 833 | EXPECT_EQ(OatFileAssistant::kUpdateSucceeded, |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 834 | oat_file_assistant.MakeUpToDate(false, &error_msg)); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 835 | EXPECT_TRUE(error_msg.empty()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | // Case: Non-standard extension for dex file. |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 839 | // Expect: The status is kDex2OatNeeded. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 840 | TEST_F(OatFileAssistantTest, LongDexExtension) { |
| 841 | std::string dex_location = GetScratchDir() + "/LongDexExtension.jarx"; |
| 842 | Copy(GetDexSrc1(), dex_location); |
| 843 | |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 844 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 845 | |
Richard Uhler | 7225a8d | 2016-11-22 10:12:03 +0000 | [diff] [blame] | 846 | EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch, |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 847 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 848 | |
| 849 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 850 | EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus()); |
| 851 | EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | // A task to generate a dex location. Used by the RaceToGenerate test. |
| 855 | class RaceGenerateTask : public Task { |
| 856 | public: |
| 857 | explicit RaceGenerateTask(const std::string& dex_location, const std::string& oat_location) |
Jeff Hao | f0192c8 | 2016-03-28 20:39:50 -0700 | [diff] [blame] | 858 | : dex_location_(dex_location), oat_location_(oat_location), loaded_oat_file_(nullptr) |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 859 | {} |
| 860 | |
Roland Levillain | 4b8f1ec | 2015-08-26 18:34:03 +0100 | [diff] [blame] | 861 | void Run(Thread* self ATTRIBUTE_UNUSED) { |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 862 | // Load the dex files, and save a pointer to the loaded oat file, so that |
| 863 | // 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] | 864 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 865 | std::vector<std::string> error_msgs; |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 866 | const OatFile* oat_file = nullptr; |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 867 | dex_files = Runtime::Current()->GetOatFileManager().OpenDexFilesFromOat( |
| 868 | dex_location_.c_str(), |
| 869 | oat_location_.c_str(), |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 870 | /*class_loader*/nullptr, |
| 871 | /*dex_elements*/nullptr, |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 872 | &oat_file, |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 873 | &error_msgs); |
Andreas Gampe | 9186ced | 2016-12-12 14:28:21 -0800 | [diff] [blame] | 874 | CHECK(!dex_files.empty()) << android::base::Join(error_msgs, '\n'); |
Richard Uhler | 07b3c23 | 2015-03-31 15:57:54 -0700 | [diff] [blame] | 875 | CHECK(dex_files[0]->GetOatDexFile() != nullptr) << dex_files[0]->GetLocation(); |
| 876 | loaded_oat_file_ = dex_files[0]->GetOatDexFile()->GetOatFile(); |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 877 | CHECK_EQ(loaded_oat_file_, oat_file); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 878 | } |
| 879 | |
| 880 | const OatFile* GetLoadedOatFile() const { |
| 881 | return loaded_oat_file_; |
| 882 | } |
| 883 | |
| 884 | private: |
| 885 | std::string dex_location_; |
| 886 | std::string oat_location_; |
| 887 | const OatFile* loaded_oat_file_; |
| 888 | }; |
| 889 | |
| 890 | // Test the case where multiple processes race to generate an oat file. |
| 891 | // This simulates multiple processes using multiple threads. |
| 892 | // |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 893 | // We want unique Oat files to be loaded even when there is a race to load. |
| 894 | // TODO: The test case no longer tests locking the way it was intended since we now get multiple |
| 895 | // copies of the same Oat files mapped at different locations. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 896 | TEST_F(OatFileAssistantTest, RaceToGenerate) { |
| 897 | std::string dex_location = GetScratchDir() + "/RaceToGenerate.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 898 | std::string oat_location = GetOdexDir() + "/RaceToGenerate.oat"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 899 | |
| 900 | // We use the lib core dex file, because it's large, and hopefully should |
| 901 | // take a while to generate. |
Narayan Kamath | d1ef436 | 2015-11-12 11:49:06 +0000 | [diff] [blame] | 902 | Copy(GetLibCoreDexFileNames()[0], dex_location); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 903 | |
| 904 | const int kNumThreads = 32; |
| 905 | Thread* self = Thread::Current(); |
| 906 | ThreadPool thread_pool("Oat file assistant test thread pool", kNumThreads); |
| 907 | std::vector<std::unique_ptr<RaceGenerateTask>> tasks; |
| 908 | for (int i = 0; i < kNumThreads; i++) { |
| 909 | std::unique_ptr<RaceGenerateTask> task(new RaceGenerateTask(dex_location, oat_location)); |
| 910 | thread_pool.AddTask(self, task.get()); |
| 911 | tasks.push_back(std::move(task)); |
| 912 | } |
| 913 | thread_pool.StartWorkers(self); |
| 914 | thread_pool.Wait(self, true, false); |
| 915 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 916 | // Verify every task got a unique oat file. |
| 917 | std::set<const OatFile*> oat_files; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 918 | for (auto& task : tasks) { |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 919 | const OatFile* oat_file = task->GetLoadedOatFile(); |
| 920 | EXPECT_TRUE(oat_files.find(oat_file) == oat_files.end()); |
| 921 | oat_files.insert(oat_file); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 922 | } |
| 923 | } |
| 924 | |
| 925 | // Case: We have a DEX file and an ODEX file, no OAT file, and dex2oat is |
| 926 | // disabled. |
| 927 | // Expect: We should load the odex file non-executable. |
| 928 | TEST_F(OatFileAssistantNoDex2OatTest, LoadDexOdexNoOat) { |
| 929 | std::string dex_location = GetScratchDir() + "/LoadDexOdexNoOat.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 930 | std::string odex_location = GetOdexDir() + "/LoadDexOdexNoOat.odex"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 931 | |
| 932 | // Create the dex and odex files |
| 933 | Copy(GetDexSrc1(), dex_location); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 934 | GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 935 | |
| 936 | // Load the oat using an executable oat file assistant. |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 937 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 938 | |
| 939 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 940 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 941 | EXPECT_FALSE(oat_file->IsExecutable()); |
| 942 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 943 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 944 | EXPECT_EQ(1u, dex_files.size()); |
| 945 | } |
| 946 | |
| 947 | // Case: We have a MultiDEX file and an ODEX file, no OAT file, and dex2oat is |
| 948 | // disabled. |
| 949 | // Expect: We should load the odex file non-executable. |
| 950 | TEST_F(OatFileAssistantNoDex2OatTest, LoadMultiDexOdexNoOat) { |
| 951 | std::string dex_location = GetScratchDir() + "/LoadMultiDexOdexNoOat.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 952 | std::string odex_location = GetOdexDir() + "/LoadMultiDexOdexNoOat.odex"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 953 | |
| 954 | // Create the dex and odex files |
| 955 | Copy(GetMultiDexSrc1(), dex_location); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 956 | GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 957 | |
| 958 | // Load the oat using an executable oat file assistant. |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 959 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 960 | |
| 961 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 962 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 963 | EXPECT_FALSE(oat_file->IsExecutable()); |
| 964 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 965 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 966 | EXPECT_EQ(2u, dex_files.size()); |
| 967 | } |
| 968 | |
Richard Uhler | f4b3487 | 2016-04-13 11:03:46 -0700 | [diff] [blame] | 969 | TEST_F(OatFileAssistantTest, RuntimeCompilerFilterOptionUsed) { |
| 970 | std::string dex_location = GetScratchDir() + "/RuntimeCompilerFilterOptionUsed.jar"; |
| 971 | Copy(GetDexSrc1(), dex_location); |
| 972 | |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 973 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false); |
Richard Uhler | f4b3487 | 2016-04-13 11:03:46 -0700 | [diff] [blame] | 974 | |
| 975 | std::string error_msg; |
| 976 | Runtime::Current()->AddCompilerOption("--compiler-filter=interpret-only"); |
| 977 | EXPECT_EQ(OatFileAssistant::kUpdateSucceeded, |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 978 | oat_file_assistant.MakeUpToDate(false, &error_msg)) << error_msg; |
Richard Uhler | f4b3487 | 2016-04-13 11:03:46 -0700 | [diff] [blame] | 979 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 980 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly)); |
Richard Uhler | 7225a8d | 2016-11-22 10:12:03 +0000 | [diff] [blame] | 981 | EXPECT_EQ(OatFileAssistant::kDex2OatForFilter, |
Richard Uhler | f4b3487 | 2016-04-13 11:03:46 -0700 | [diff] [blame] | 982 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
| 983 | |
| 984 | Runtime::Current()->AddCompilerOption("--compiler-filter=speed"); |
| 985 | EXPECT_EQ(OatFileAssistant::kUpdateSucceeded, |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 986 | oat_file_assistant.MakeUpToDate(false, &error_msg)) << error_msg; |
Richard Uhler | f4b3487 | 2016-04-13 11:03:46 -0700 | [diff] [blame] | 987 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 988 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly)); |
| 989 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 990 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
| 991 | |
| 992 | Runtime::Current()->AddCompilerOption("--compiler-filter=bogus"); |
| 993 | EXPECT_EQ(OatFileAssistant::kUpdateNotAttempted, |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 994 | oat_file_assistant.MakeUpToDate(false, &error_msg)); |
Richard Uhler | f4b3487 | 2016-04-13 11:03:46 -0700 | [diff] [blame] | 995 | } |
| 996 | |
Richard Uhler | b81881d | 2016-04-19 13:08:04 -0700 | [diff] [blame] | 997 | TEST(OatFileAssistantUtilsTest, DexLocationToOdexFilename) { |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 998 | std::string error_msg; |
| 999 | std::string odex_file; |
| 1000 | |
Richard Uhler | b81881d | 2016-04-19 13:08:04 -0700 | [diff] [blame] | 1001 | EXPECT_TRUE(OatFileAssistant::DexLocationToOdexFilename( |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 1002 | "/foo/bar/baz.jar", kArm, &odex_file, &error_msg)) << error_msg; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 1003 | EXPECT_EQ("/foo/bar/oat/arm/baz.odex", odex_file); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1004 | |
Richard Uhler | b81881d | 2016-04-19 13:08:04 -0700 | [diff] [blame] | 1005 | EXPECT_TRUE(OatFileAssistant::DexLocationToOdexFilename( |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 1006 | "/foo/bar/baz.funnyext", kArm, &odex_file, &error_msg)) << error_msg; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 1007 | EXPECT_EQ("/foo/bar/oat/arm/baz.odex", odex_file); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1008 | |
Richard Uhler | b81881d | 2016-04-19 13:08:04 -0700 | [diff] [blame] | 1009 | EXPECT_FALSE(OatFileAssistant::DexLocationToOdexFilename( |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 1010 | "nopath.jar", kArm, &odex_file, &error_msg)); |
Richard Uhler | b81881d | 2016-04-19 13:08:04 -0700 | [diff] [blame] | 1011 | EXPECT_FALSE(OatFileAssistant::DexLocationToOdexFilename( |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 1012 | "/foo/bar/baz_noext", kArm, &odex_file, &error_msg)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1013 | } |
| 1014 | |
Richard Uhler | 23cedd2 | 2015-04-08 13:17:29 -0700 | [diff] [blame] | 1015 | // Verify the dexopt status values from dalvik.system.DexFile |
| 1016 | // match the OatFileAssistant::DexOptStatus values. |
| 1017 | TEST_F(OatFileAssistantTest, DexOptStatusValues) { |
Richard Uhler | 7225a8d | 2016-11-22 10:12:03 +0000 | [diff] [blame] | 1018 | std::pair<OatFileAssistant::DexOptNeeded, const char*> mapping[] = { |
| 1019 | {OatFileAssistant::kNoDexOptNeeded, "NO_DEXOPT_NEEDED"}, |
| 1020 | {OatFileAssistant::kDex2OatFromScratch, "DEX2OAT_FROM_SCRATCH"}, |
| 1021 | {OatFileAssistant::kDex2OatForBootImage, "DEX2OAT_FOR_BOOT_IMAGE"}, |
| 1022 | {OatFileAssistant::kDex2OatForFilter, "DEX2OAT_FOR_FILTER"}, |
| 1023 | {OatFileAssistant::kDex2OatForRelocation, "DEX2OAT_FOR_RELOCATION"}, |
Richard Uhler | 7225a8d | 2016-11-22 10:12:03 +0000 | [diff] [blame] | 1024 | }; |
| 1025 | |
Richard Uhler | 23cedd2 | 2015-04-08 13:17:29 -0700 | [diff] [blame] | 1026 | ScopedObjectAccess soa(Thread::Current()); |
| 1027 | StackHandleScope<1> hs(soa.Self()); |
| 1028 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
| 1029 | Handle<mirror::Class> dexfile( |
| 1030 | hs.NewHandle(linker->FindSystemClass(soa.Self(), "Ldalvik/system/DexFile;"))); |
| 1031 | ASSERT_FALSE(dexfile.Get() == nullptr); |
| 1032 | linker->EnsureInitialized(soa.Self(), dexfile, true, true); |
| 1033 | |
Richard Uhler | 7225a8d | 2016-11-22 10:12:03 +0000 | [diff] [blame] | 1034 | for (std::pair<OatFileAssistant::DexOptNeeded, const char*> field : mapping) { |
| 1035 | ArtField* art_field = mirror::Class::FindStaticField( |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 1036 | soa.Self(), dexfile.Get(), field.second, "I"); |
Richard Uhler | 7225a8d | 2016-11-22 10:12:03 +0000 | [diff] [blame] | 1037 | ASSERT_FALSE(art_field == nullptr); |
| 1038 | EXPECT_EQ(art_field->GetTypeAsPrimitiveType(), Primitive::kPrimInt); |
| 1039 | EXPECT_EQ(field.first, art_field->GetInt(dexfile.Get())); |
| 1040 | } |
Richard Uhler | 23cedd2 | 2015-04-08 13:17:29 -0700 | [diff] [blame] | 1041 | } |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1042 | |
| 1043 | // TODO: More Tests: |
| 1044 | // * Test class linker falls back to unquickened dex for DexNoOat |
| 1045 | // * Test class linker falls back to unquickened dex for MultiDexNoOat |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1046 | // * Test using secondary isa |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1047 | // * Test for status of oat while oat is being generated (how?) |
| 1048 | // * Test case where 32 and 64 bit boot class paths differ, |
| 1049 | // and we ask IsInBootClassPath for a class in exactly one of the 32 or |
| 1050 | // 64 bit boot class paths. |
| 1051 | // * Test unexpected scenarios (?): |
| 1052 | // - Dex is stripped, don't have odex. |
| 1053 | // - Oat file corrupted after status check, before reload unexecutable |
| 1054 | // because it's unrelocated and no dex2oat |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1055 | } // namespace art |