blob: f777340cfdeedd5411171dccebb86959652cc1a0 [file] [log] [blame]
Richard Uhler66d874d2015-01-15 09:37:19 -08001/*
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 Uhler66d874d2015-01-15 09:37:19 -080017#include <string>
18#include <vector>
19#include <sys/param.h>
20
Andreas Gampe9186ced2016-12-12 14:28:21 -080021#include "android-base/strings.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080022#include <gtest/gtest.h>
23
Mathieu Chartierc7853442015-03-27 14:35:38 -070024#include "art_field-inl.h"
Vladimir Marko3481ba22015-04-13 12:22:36 +010025#include "class_linker-inl.h"
Calin Juravle36eb3132017-01-13 16:32:38 -080026#include "dexopt_test.h"
Andreas Gampee1459ae2016-06-29 09:36:30 -070027#include "oat_file_assistant.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070028#include "oat_file_manager.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080029#include "os.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070030#include "scoped_thread_state_change-inl.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080031#include "thread-inl.h"
32#include "utils.h"
33
34namespace art {
35
Calin Juravle36eb3132017-01-13 16:32:38 -080036class OatFileAssistantTest : public DexoptTest {};
Richard Uhler66d874d2015-01-15 09:37:19 -080037
Calin Juravle36eb3132017-01-13 16:32:38 -080038class OatFileAssistantNoDex2OatTest : public DexoptTest {
Richard Uhler66d874d2015-01-15 09:37:19 -080039 public:
40 virtual void SetUpRuntimeOptions(RuntimeOptions* options) {
Calin Juravle36eb3132017-01-13 16:32:38 -080041 DexoptTest::SetUpRuntimeOptions(options);
Richard Uhler66d874d2015-01-15 09:37:19 -080042 options->push_back(std::make_pair("-Xnodex2oat", nullptr));
43 }
44};
45
Calin Juravle36eb3132017-01-13 16:32:38 -080046
Richard Uhler66d874d2015-01-15 09:37:19 -080047// Case: We have a DEX file, but no OAT file for it.
Richard Uhler95abd042015-03-24 09:51:28 -070048// Expect: The status is kDex2OatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -080049TEST_F(OatFileAssistantTest, DexNoOat) {
50 std::string dex_location = GetScratchDir() + "/DexNoOat.jar";
51 Copy(GetDexSrc1(), dex_location);
52
Richard Uhlerd1472a22016-04-15 15:18:56 -070053 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -080054
Richard Uhler7225a8d2016-11-22 10:12:03 +000055 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Andreas Gampe29d38e72016-03-23 15:31:51 +000056 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
Richard Uhler7225a8d2016-11-22 10:12:03 +000057 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Andreas Gampe29d38e72016-03-23 15:31:51 +000058 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly));
Richard Uhler7225a8d2016-11-22 10:12:03 +000059 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Andreas Gampe29d38e72016-03-23 15:31:51 +000060 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeedProfile));
Richard Uhler7225a8d2016-11-22 10:12:03 +000061 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Andreas Gampe29d38e72016-03-23 15:31:51 +000062 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -080063
64 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +000065 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
66 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -070067 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -080068}
69
70// Case: We have no DEX file and no OAT file.
Richard Uhler9b994ea2015-06-24 08:44:19 -070071// Expect: Status is kNoDexOptNeeded. Loading should fail, but not crash.
Richard Uhler66d874d2015-01-15 09:37:19 -080072TEST_F(OatFileAssistantTest, NoDexNoOat) {
73 std::string dex_location = GetScratchDir() + "/NoDexNoOat.jar";
74
Richard Uhlerd1472a22016-04-15 15:18:56 -070075 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -080076
Andreas Gampe29d38e72016-03-23 15:31:51 +000077 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
78 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler9b994ea2015-06-24 08:44:19 -070079 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 Uhlerd1472a22016-04-15 15:18:56 -070083 EXPECT_EQ(OatFileAssistant::kUpdateSucceeded, oat_file_assistant.MakeUpToDate(false, &error_msg));
Richard Uhler9b994ea2015-06-24 08:44:19 -070084
85 // Trying to get the best oat file should fail, but not crash.
Richard Uhler66d874d2015-01-15 09:37:19 -080086 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 Uhler95abd042015-03-24 09:51:28 -070091// Expect: The status is kNoDexOptNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -080092TEST_F(OatFileAssistantTest, OatUpToDate) {
93 std::string dex_location = GetScratchDir() + "/OatUpToDate.jar";
94 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +000095 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -080096
Richard Uhlerd1472a22016-04-15 15:18:56 -070097 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -080098
Andreas Gampe29d38e72016-03-23 15:31:51 +000099 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 Uhler7225a8d2016-11-22 10:12:03 +0000105 EXPECT_EQ(OatFileAssistant::kDex2OatForFilter,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000106 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
107
Richard Uhler66d874d2015-01-15 09:37:19 -0800108 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000109 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
Richard Uhler95abd042015-03-24 09:51:28 -0700110 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700111 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800112}
113
Richard Uhler2f27abd2017-01-31 14:02:34 +0000114// Case: We have a DEX file and up-to-date (ODEX) VDEX file for it, but no
115// ODEX file.
116TEST_F(OatFileAssistantTest, VdexUpToDateNoOdex) {
117 // This test case is only meaningful if vdex is enabled.
118 if (!kIsVdexEnabled) {
119 return;
120 }
Richard Uhler9a37efc2016-08-05 16:32:55 -0700121
Richard Uhler2f27abd2017-01-31 14:02:34 +0000122 std::string dex_location = GetScratchDir() + "/VdexUpToDateNoOdex.jar";
123 std::string oat_location = GetOdexDir() + "/VdexUpToDateNoOdex.oat";
124
Richard Uhler9a37efc2016-08-05 16:32:55 -0700125 Copy(GetDexSrc1(), dex_location);
126
Richard Uhler2f27abd2017-01-31 14:02:34 +0000127 // 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.
151TEST_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 Uhler5cd59292017-02-01 12:54:23 +0000157 ScratchFile vdex_file(vdex_location.c_str());
158 ScratchFile odex_file(odex_location.c_str());
Richard Uhler2f27abd2017-01-31 14:02:34 +0000159
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.
167TEST_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 Uhler9a37efc2016-08-05 16:32:55 -0700183 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
184
Richard Uhler2f27abd2017-01-31 14:02:34 +0000185 // 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 Uhler9a37efc2016-08-05 16:32:55 -0700191 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler9a37efc2016-08-05 16:32:55 -0700192}
193
Andreas Gampe29d38e72016-03-23 15:31:51 +0000194// Case: We have a DEX file and speed-profile OAT file for it.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700195// Expect: The status is kNoDexOptNeeded if the profile hasn't changed, but
196// kDex2Oat if the profile has changed.
Andreas Gampe29d38e72016-03-23 15:31:51 +0000197TEST_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 Uhlerd1472a22016-04-15 15:18:56 -0700202 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000203
204 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700205 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeedProfile, false));
Andreas Gampe29d38e72016-03-23 15:31:51 +0000206 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700207 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly, false));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000208 EXPECT_EQ(OatFileAssistant::kDex2OatForFilter,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700209 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeedProfile, true));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000210 EXPECT_EQ(OatFileAssistant::kDex2OatForFilter,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700211 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly, true));
Andreas Gampe29d38e72016-03-23 15:31:51 +0000212
213 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000214 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
Andreas Gampe29d38e72016-03-23 15:31:51 +0000215 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus());
216 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
217}
218
Richard Uhler66d874d2015-01-15 09:37:19 -0800219// Case: We have a MultiDEX file and up-to-date OAT file for it.
Richard Uhler95abd042015-03-24 09:51:28 -0700220// Expect: The status is kNoDexOptNeeded and we load all dex files.
Richard Uhler66d874d2015-01-15 09:37:19 -0800221TEST_F(OatFileAssistantTest, MultiDexOatUpToDate) {
222 std::string dex_location = GetScratchDir() + "/MultiDexOatUpToDate.jar";
223 Copy(GetMultiDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000224 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800225
Richard Uhlerd1472a22016-04-15 15:18:56 -0700226 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000227 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700228 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed, false));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700229 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler95abd042015-03-24 09:51:28 -0700230
231 // Verify we can load both dex files.
Richard Uhlere5fed032015-03-18 08:21:11 -0700232 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
Richard Uhler66d874d2015-01-15 09:37:19 -0800233 ASSERT_TRUE(oat_file.get() != nullptr);
234 EXPECT_TRUE(oat_file->IsExecutable());
235 std::vector<std::unique_ptr<const DexFile>> dex_files;
Richard Uhlere5fed032015-03-18 08:21:11 -0700236 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
237 EXPECT_EQ(2u, dex_files.size());
238}
239
Richard Uhler67ff7d12015-05-14 13:21:13 -0700240// Case: We have a MultiDEX file where the secondary dex file is out of date.
241// Expect: The status is kDex2OatNeeded.
242TEST_F(OatFileAssistantTest, MultiDexSecondaryOutOfDate) {
243 std::string dex_location = GetScratchDir() + "/MultiDexSecondaryOutOfDate.jar";
244
245 // Compile code for GetMultiDexSrc1.
246 Copy(GetMultiDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000247 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler67ff7d12015-05-14 13:21:13 -0700248
249 // Now overwrite the dex file with GetMultiDexSrc2 so the secondary checksum
250 // is out of date.
251 Copy(GetMultiDexSrc2(), dex_location);
252
Richard Uhlerd1472a22016-04-15 15:18:56 -0700253 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler7225a8d2016-11-22 10:12:03 +0000254 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700255 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed, false));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700256 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler67ff7d12015-05-14 13:21:13 -0700257}
258
Richard Uhlere5fed032015-03-18 08:21:11 -0700259// Case: We have a MultiDEX file and up-to-date OAT file for it with relative
260// encoded dex locations.
Richard Uhler95abd042015-03-24 09:51:28 -0700261// Expect: The oat file status is kNoDexOptNeeded.
Richard Uhlere5fed032015-03-18 08:21:11 -0700262TEST_F(OatFileAssistantTest, RelativeEncodedDexLocation) {
263 std::string dex_location = GetScratchDir() + "/RelativeEncodedDexLocation.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700264 std::string oat_location = GetOdexDir() + "/RelativeEncodedDexLocation.oat";
Richard Uhlere5fed032015-03-18 08:21:11 -0700265
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 Gampe29d38e72016-03-23 15:31:51 +0000274 args.push_back("--compiler-filter=speed");
Richard Uhlere5fed032015-03-18 08:21:11 -0700275
276 std::string error_msg;
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700277 ASSERT_TRUE(OatFileAssistant::Dex2Oat(args, &error_msg)) << error_msg;
Richard Uhlere5fed032015-03-18 08:21:11 -0700278
279 // Verify we can load both dex files.
280 OatFileAssistant oat_file_assistant(dex_location.c_str(),
281 oat_location.c_str(),
Richard Uhlerd1472a22016-04-15 15:18:56 -0700282 kRuntimeISA, true);
Richard Uhlere5fed032015-03-18 08:21:11 -0700283 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 Uhler66d874d2015-01-15 09:37:19 -0800288 EXPECT_EQ(2u, dex_files.size());
289}
290
Richard Uhler03bc6592016-11-22 09:42:04 +0000291// Case: We have a DEX file and an OAT file out of date with respect to the
292// dex checksum.
293TEST_F(OatFileAssistantTest, OatDexOutOfDate) {
294 std::string dex_location = GetScratchDir() + "/OatDexOutOfDate.jar";
Richard Uhler66d874d2015-01-15 09:37:19 -0800295
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 Gampe29d38e72016-03-23 15:31:51 +0000299 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800300 Copy(GetDexSrc2(), dex_location);
301
Richard Uhlerd1472a22016-04-15 15:18:56 -0700302 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler7225a8d2016-11-22 10:12:03 +0000303 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000304 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000305 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000306 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800307
308 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000309 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 Uhler2f27abd2017-01-31 14:02:34 +0000314// 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.
316TEST_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.
341TEST_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 Uhler03bc6592016-11-22 09:42:04 +0000364// Case: We have a DEX file and an OAT file out of date with respect to the
365// boot image.
366TEST_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 Uhler03bc6592016-11-22 09:42:04 +0000374 /*with_alternate_image*/true);
375
376 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler7225a8d2016-11-22 10:12:03 +0000377 EXPECT_EQ(OatFileAssistant::kDex2OatForBootImage,
Richard Uhler03bc6592016-11-22 09:42:04 +0000378 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000379 EXPECT_EQ(OatFileAssistant::kDex2OatForBootImage,
Richard Uhler03bc6592016-11-22 09:42:04 +0000380 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000381 EXPECT_EQ(OatFileAssistant::kDex2OatForBootImage,
Richard Uhler03bc6592016-11-22 09:42:04 +0000382 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.
394TEST_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 Uhler03bc6592016-11-22 09:42:04 +0000402 /*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 Uhler7225a8d2016-11-22 10:12:03 +0000407 EXPECT_EQ(OatFileAssistant::kDex2OatForFilter,
Richard Uhler03bc6592016-11-22 09:42:04 +0000408 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 Uhler9b994ea2015-06-24 08:44:19 -0700413 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800414}
415
416// Case: We have a DEX file and an ODEX file, but no OAT file.
Richard Uhler66d874d2015-01-15 09:37:19 -0800417TEST_F(OatFileAssistantTest, DexOdexNoOat) {
418 std::string dex_location = GetScratchDir() + "/DexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700419 std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800420
421 // Create the dex and odex files
422 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000423 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800424
425 // Verify the status.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700426 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800427
Andreas Gampe29d38e72016-03-23 15:31:51 +0000428 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
429 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
Richard Uhler5923b522016-12-08 09:48:01 +0000430 EXPECT_EQ(-OatFileAssistant::kDex2OatForRelocation,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000431 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800432
433 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000434 EXPECT_EQ(OatFileAssistant::kOatRelocationOutOfDate, oat_file_assistant.OdexFileStatus());
435 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700436 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler5f946da2015-07-17 12:28:32 -0700437
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 Uhler66d874d2015-01-15 09:37:19 -0800441}
442
Richard Uhler5923b522016-12-08 09:48:01 +0000443// Case: We have a stripped DEX file and a PIC ODEX file, but no OAT file.
Richard Uhler66d874d2015-01-15 09:37:19 -0800444TEST_F(OatFileAssistantTest, StrippedDexOdexNoOat) {
445 std::string dex_location = GetScratchDir() + "/StrippedDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700446 std::string odex_location = GetOdexDir() + "/StrippedDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800447
448 // Create the dex and odex files
449 Copy(GetDexSrc1(), dex_location);
Richard Uhler5923b522016-12-08 09:48:01 +0000450 GeneratePicOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800451
452 // Strip the dex file
453 Copy(GetStrippedDexSrc1(), dex_location);
454
455 // Verify the status.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700456 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800457
Richard Uhler5923b522016-12-08 09:48:01 +0000458 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000459 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800460
461 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler5923b522016-12-08 09:48:01 +0000462 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus());
Richard Uhler03bc6592016-11-22 09:42:04 +0000463 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700464 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800465
Richard Uhler66d874d2015-01-15 09:37:19 -0800466 // 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 Uhler5923b522016-12-08 09:48:01 +0000475// Case: We have a stripped DEX file, a PIC ODEX file, and an out-of-date OAT file.
Richard Uhler66d874d2015-01-15 09:37:19 -0800476TEST_F(OatFileAssistantTest, StrippedDexOdexOat) {
477 std::string dex_location = GetScratchDir() + "/StrippedDexOdexOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700478 std::string odex_location = GetOdexDir() + "/StrippedDexOdexOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800479
480 // Create the oat file from a different dex file so it looks out of date.
481 Copy(GetDexSrc2(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000482 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800483
484 // Create the odex file
485 Copy(GetDexSrc1(), dex_location);
Richard Uhler5923b522016-12-08 09:48:01 +0000486 GeneratePicOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800487
488 // Strip the dex file.
489 Copy(GetStrippedDexSrc1(), dex_location);
490
491 // Verify the status.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700492 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800493
Andreas Gampe29d38e72016-03-23 15:31:51 +0000494 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
495 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
Andreas Gampe29d38e72016-03-23 15:31:51 +0000496 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 Uhler66d874d2015-01-15 09:37:19 -0800500
501 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler5923b522016-12-08 09:48:01 +0000502 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus());
503 EXPECT_EQ(OatFileAssistant::kOatDexOutOfDate, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700504 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800505
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 Uhler9b994ea2015-06-24 08:44:19 -0700515// Case: We have a stripped (or resource-only) DEX file, no ODEX file and no
516// OAT file. Expect: The status is kNoDexOptNeeded.
517TEST_F(OatFileAssistantTest, ResourceOnlyDex) {
518 std::string dex_location = GetScratchDir() + "/ResourceOnlyDex.jar";
519
520 Copy(GetStrippedDexSrc1(), dex_location);
521
522 // Verify the status.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700523 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler9b994ea2015-06-24 08:44:19 -0700524
Andreas Gampe29d38e72016-03-23 15:31:51 +0000525 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 Uhler9b994ea2015-06-24 08:44:19 -0700531
532 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000533 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
534 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700535 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 Uhlerf4b34872016-04-13 11:03:46 -0700539 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
Richard Uhler1e860612016-03-30 12:17:55 -0700540 EXPECT_EQ(OatFileAssistant::kUpdateSucceeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700541 oat_file_assistant.MakeUpToDate(false, &error_msg)) << error_msg;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700542
Andreas Gampe29d38e72016-03-23 15:31:51 +0000543 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
544 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700545
546 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000547 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
548 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700549 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
550}
551
Richard Uhler66d874d2015-01-15 09:37:19 -0800552// 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 Uhler5923b522016-12-08 09:48:01 +0000554// Expect: It shouldn't crash.
Richard Uhler66d874d2015-01-15 09:37:19 -0800555TEST_F(OatFileAssistantTest, OdexOatOverlap) {
556 std::string dex_location = GetScratchDir() + "/OdexOatOverlap.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700557 std::string odex_location = GetOdexDir() + "/OdexOatOverlap.odex";
558 std::string oat_location = GetOdexDir() + "/OdexOatOverlap.oat";
Richard Uhler66d874d2015-01-15 09:37:19 -0800559
560 // Create the dex and odex files
561 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000562 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800563
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 Uhlerd1472a22016-04-15 15:18:56 -0700570 oat_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800571
Richard Uhler5923b522016-12-08 09:48:01 +0000572 // kDex2OatForRelocation is expected rather than -kDex2OatForRelocation
Richard Uhler7225a8d2016-11-22 10:12:03 +0000573 // based on the assumption that the oat location is more up-to-date than the odex
Richard Uhler70a84262016-11-08 16:51:51 +0000574 // location, even if they both need relocation.
Richard Uhler5923b522016-12-08 09:48:01 +0000575 EXPECT_EQ(OatFileAssistant::kDex2OatForRelocation,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000576 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800577
578 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000579 EXPECT_EQ(OatFileAssistant::kOatRelocationOutOfDate, oat_file_assistant.OdexFileStatus());
580 EXPECT_EQ(OatFileAssistant::kOatRelocationOutOfDate, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700581 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800582
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 Uhlerf16d5722015-05-11 09:32:47 -0700586
Richard Uhler66d874d2015-01-15 09:37:19 -0800587 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 Uhler95abd042015-03-24 09:51:28 -0700594// Expect: The status is kNoDexOptNeeded, because PIC needs no relocation.
Richard Uhler66d874d2015-01-15 09:37:19 -0800595TEST_F(OatFileAssistantTest, DexPicOdexNoOat) {
596 std::string dex_location = GetScratchDir() + "/DexPicOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700597 std::string odex_location = GetOdexDir() + "/DexPicOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800598
599 // Create the dex and odex files
600 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000601 GeneratePicOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800602
603 // Verify the status.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700604 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800605
Andreas Gampe29d38e72016-03-23 15:31:51 +0000606 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
607 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000608 EXPECT_EQ(-OatFileAssistant::kDex2OatForFilter,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000609 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
Richard Uhler66d874d2015-01-15 09:37:19 -0800610
611 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler3e580bc2016-11-08 16:23:07 +0000612 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus());
Richard Uhler03bc6592016-11-22 09:42:04 +0000613 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700614 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800615}
616
Andreas Gampe29d38e72016-03-23 15:31:51 +0000617// 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.
619TEST_F(OatFileAssistantTest, DexVerifyAtRuntimeOdexNoOat) {
620 std::string dex_location = GetScratchDir() + "/DexVerifyAtRuntimeOdexNoOat.jar";
621 std::string odex_location = GetOdexDir() + "/DexVerifyAtRuntimeOdexNoOat.odex";
David Brazdilce4b0ba2016-01-28 15:05:49 +0000622
623 // Create the dex and odex files
624 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000625 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kVerifyAtRuntime);
David Brazdilce4b0ba2016-01-28 15:05:49 +0000626
627 // Verify the status.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700628 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
David Brazdilce4b0ba2016-01-28 15:05:49 +0000629
Andreas Gampe29d38e72016-03-23 15:31:51 +0000630 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
631 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000632 EXPECT_EQ(-OatFileAssistant::kDex2OatForFilter,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000633 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
David Brazdilce4b0ba2016-01-28 15:05:49 +0000634
635 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler3e580bc2016-11-08 16:23:07 +0000636 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus());
Richard Uhler03bc6592016-11-22 09:42:04 +0000637 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
David Brazdilce4b0ba2016-01-28 15:05:49 +0000638 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
639}
640
Richard Uhler66d874d2015-01-15 09:37:19 -0800641// Case: We have a DEX file and up-to-date OAT file for it.
642// Expect: We should load an executable dex file.
643TEST_F(OatFileAssistantTest, LoadOatUpToDate) {
644 std::string dex_location = GetScratchDir() + "/LoadOatUpToDate.jar";
645
646 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000647 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800648
649 // Load the oat using an oat file assistant.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700650 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000651
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.
662TEST_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 Uhlerd1472a22016-04-15 15:18:56 -0700669 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800670
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.
681TEST_F(OatFileAssistantTest, LoadNoExecOatUpToDate) {
682 std::string dex_location = GetScratchDir() + "/LoadNoExecOatUpToDate.jar";
683
684 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000685 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800686
687 // Load the oat using an oat file assistant.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700688 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800689
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.
701TEST_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 Uhlerd1472a22016-04-15 15:18:56 -0700708 dex_location.c_str(), oat_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800709 std::string error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -0700710 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
Richard Uhler1e860612016-03-30 12:17:55 -0700711 ASSERT_EQ(OatFileAssistant::kUpdateSucceeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700712 oat_file_assistant.MakeUpToDate(false, &error_msg)) << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800713
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 Uhlerd1472a22016-04-15 15:18:56 -0700724 OatFileAssistant ofm(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler03bc6592016-11-22 09:42:04 +0000725 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, ofm.OatFileStatus());
Richard Uhler66d874d2015-01-15 09:37:19 -0800726}
727
Richard Uhler8327cf72015-10-13 16:34:59 -0700728// 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.
730TEST_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 Uhlerd1472a22016-04-15 15:18:56 -0700740 dex_location.c_str(), oat_location.c_str(), kRuntimeISA, true);
Richard Uhler8327cf72015-10-13 16:34:59 -0700741 std::string error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -0700742 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
Richard Uhler1e860612016-03-30 12:17:55 -0700743 ASSERT_EQ(OatFileAssistant::kUpdateNotAttempted,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700744 oat_file_assistant.MakeUpToDate(false, &error_msg));
Richard Uhler8327cf72015-10-13 16:34:59 -0700745
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.
752TEST_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 Uhlerd1472a22016-04-15 15:18:56 -0700757 dex_location.c_str(), oat_location.c_str(), kRuntimeISA, true);
Richard Uhler8327cf72015-10-13 16:34:59 -0700758 std::string error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -0700759 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
Richard Uhler1e860612016-03-30 12:17:55 -0700760 EXPECT_EQ(OatFileAssistant::kUpdateNotAttempted,
Richard Uhlerf4b34872016-04-13 11:03:46 -0700761 oat_file_assistant.GenerateOatFile(&error_msg));
Richard Uhler8327cf72015-10-13 16:34:59 -0700762}
763
Richard Uhler66d874d2015-01-15 09:37:19 -0800764// Turn an absolute path into a path relative to the current working
765// directory.
Andreas Gampeca620d72016-11-08 08:09:33 -0800766static std::string MakePathRelative(const std::string& target) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800767 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 Gampe9186ced2016-12-12 14:28:21 -0800797 return android::base::Join(target_path, '/');
Richard Uhler66d874d2015-01-15 09:37:19 -0800798}
799
800// Case: Non-absolute path to Dex location.
801// Expect: Not sure, but it shouldn't crash.
802TEST_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 Uhlerd1472a22016-04-15 15:18:56 -0700807 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800808
809 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler7225a8d2016-11-22 10:12:03 +0000810 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000811 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler03bc6592016-11-22 09:42:04 +0000812 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
813 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler66d874d2015-01-15 09:37:19 -0800814}
815
816// Case: Very short, non-existent Dex location.
Richard Uhler9b994ea2015-06-24 08:44:19 -0700817// Expect: kNoDexOptNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800818TEST_F(OatFileAssistantTest, ShortDexLocation) {
819 std::string dex_location = "/xx";
820
Richard Uhlerd1472a22016-04-15 15:18:56 -0700821 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800822
823 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Andreas Gampe29d38e72016-03-23 15:31:51 +0000824 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
825 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler03bc6592016-11-22 09:42:04 +0000826 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
827 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700828 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800829
Richard Uhler9b994ea2015-06-24 08:44:19 -0700830 // Trying to make it up to date should have no effect.
Richard Uhler66d874d2015-01-15 09:37:19 -0800831 std::string error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -0700832 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
Richard Uhler1e860612016-03-30 12:17:55 -0700833 EXPECT_EQ(OatFileAssistant::kUpdateSucceeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700834 oat_file_assistant.MakeUpToDate(false, &error_msg));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700835 EXPECT_TRUE(error_msg.empty());
Richard Uhler66d874d2015-01-15 09:37:19 -0800836}
837
838// Case: Non-standard extension for dex file.
Richard Uhler95abd042015-03-24 09:51:28 -0700839// Expect: The status is kDex2OatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800840TEST_F(OatFileAssistantTest, LongDexExtension) {
841 std::string dex_location = GetScratchDir() + "/LongDexExtension.jarx";
842 Copy(GetDexSrc1(), dex_location);
843
Richard Uhlerd1472a22016-04-15 15:18:56 -0700844 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800845
Richard Uhler7225a8d2016-11-22 10:12:03 +0000846 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000847 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800848
849 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000850 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
851 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler66d874d2015-01-15 09:37:19 -0800852}
853
854// A task to generate a dex location. Used by the RaceToGenerate test.
855class RaceGenerateTask : public Task {
856 public:
857 explicit RaceGenerateTask(const std::string& dex_location, const std::string& oat_location)
Jeff Haof0192c82016-03-28 20:39:50 -0700858 : dex_location_(dex_location), oat_location_(oat_location), loaded_oat_file_(nullptr)
Richard Uhler66d874d2015-01-15 09:37:19 -0800859 {}
860
Roland Levillain4b8f1ec2015-08-26 18:34:03 +0100861 void Run(Thread* self ATTRIBUTE_UNUSED) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800862 // 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 Uhler66d874d2015-01-15 09:37:19 -0800864 std::vector<std::unique_ptr<const DexFile>> dex_files;
865 std::vector<std::string> error_msgs;
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700866 const OatFile* oat_file = nullptr;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700867 dex_files = Runtime::Current()->GetOatFileManager().OpenDexFilesFromOat(
868 dex_location_.c_str(),
869 oat_location_.c_str(),
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800870 /*class_loader*/nullptr,
871 /*dex_elements*/nullptr,
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700872 &oat_file,
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700873 &error_msgs);
Andreas Gampe9186ced2016-12-12 14:28:21 -0800874 CHECK(!dex_files.empty()) << android::base::Join(error_msgs, '\n');
Richard Uhler07b3c232015-03-31 15:57:54 -0700875 CHECK(dex_files[0]->GetOatDexFile() != nullptr) << dex_files[0]->GetLocation();
876 loaded_oat_file_ = dex_files[0]->GetOatDexFile()->GetOatFile();
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700877 CHECK_EQ(loaded_oat_file_, oat_file);
Richard Uhler66d874d2015-01-15 09:37:19 -0800878 }
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 Chartierf9c6fc62015-10-07 11:44:05 -0700893// 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 Uhler66d874d2015-01-15 09:37:19 -0800896TEST_F(OatFileAssistantTest, RaceToGenerate) {
897 std::string dex_location = GetScratchDir() + "/RaceToGenerate.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700898 std::string oat_location = GetOdexDir() + "/RaceToGenerate.oat";
Richard Uhler66d874d2015-01-15 09:37:19 -0800899
900 // We use the lib core dex file, because it's large, and hopefully should
901 // take a while to generate.
Narayan Kamathd1ef4362015-11-12 11:49:06 +0000902 Copy(GetLibCoreDexFileNames()[0], dex_location);
Richard Uhler66d874d2015-01-15 09:37:19 -0800903
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 Chartierf9c6fc62015-10-07 11:44:05 -0700916 // Verify every task got a unique oat file.
917 std::set<const OatFile*> oat_files;
Richard Uhler66d874d2015-01-15 09:37:19 -0800918 for (auto& task : tasks) {
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700919 const OatFile* oat_file = task->GetLoadedOatFile();
920 EXPECT_TRUE(oat_files.find(oat_file) == oat_files.end());
921 oat_files.insert(oat_file);
Richard Uhler66d874d2015-01-15 09:37:19 -0800922 }
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.
928TEST_F(OatFileAssistantNoDex2OatTest, LoadDexOdexNoOat) {
929 std::string dex_location = GetScratchDir() + "/LoadDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700930 std::string odex_location = GetOdexDir() + "/LoadDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800931
932 // Create the dex and odex files
933 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000934 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800935
936 // Load the oat using an executable oat file assistant.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700937 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800938
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.
950TEST_F(OatFileAssistantNoDex2OatTest, LoadMultiDexOdexNoOat) {
951 std::string dex_location = GetScratchDir() + "/LoadMultiDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700952 std::string odex_location = GetOdexDir() + "/LoadMultiDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800953
954 // Create the dex and odex files
955 Copy(GetMultiDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000956 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800957
958 // Load the oat using an executable oat file assistant.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700959 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800960
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 Uhlerf4b34872016-04-13 11:03:46 -0700969TEST_F(OatFileAssistantTest, RuntimeCompilerFilterOptionUsed) {
970 std::string dex_location = GetScratchDir() + "/RuntimeCompilerFilterOptionUsed.jar";
971 Copy(GetDexSrc1(), dex_location);
972
Richard Uhlerd1472a22016-04-15 15:18:56 -0700973 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhlerf4b34872016-04-13 11:03:46 -0700974
975 std::string error_msg;
976 Runtime::Current()->AddCompilerOption("--compiler-filter=interpret-only");
977 EXPECT_EQ(OatFileAssistant::kUpdateSucceeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700978 oat_file_assistant.MakeUpToDate(false, &error_msg)) << error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -0700979 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
980 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000981 EXPECT_EQ(OatFileAssistant::kDex2OatForFilter,
Richard Uhlerf4b34872016-04-13 11:03:46 -0700982 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
983
984 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
985 EXPECT_EQ(OatFileAssistant::kUpdateSucceeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700986 oat_file_assistant.MakeUpToDate(false, &error_msg)) << error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -0700987 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 Uhlerd1472a22016-04-15 15:18:56 -0700994 oat_file_assistant.MakeUpToDate(false, &error_msg));
Richard Uhlerf4b34872016-04-13 11:03:46 -0700995}
996
Richard Uhlerb81881d2016-04-19 13:08:04 -0700997TEST(OatFileAssistantUtilsTest, DexLocationToOdexFilename) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800998 std::string error_msg;
999 std::string odex_file;
1000
Richard Uhlerb81881d2016-04-19 13:08:04 -07001001 EXPECT_TRUE(OatFileAssistant::DexLocationToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001002 "/foo/bar/baz.jar", kArm, &odex_file, &error_msg)) << error_msg;
Richard Uhler63434112015-03-16 14:32:16 -07001003 EXPECT_EQ("/foo/bar/oat/arm/baz.odex", odex_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001004
Richard Uhlerb81881d2016-04-19 13:08:04 -07001005 EXPECT_TRUE(OatFileAssistant::DexLocationToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001006 "/foo/bar/baz.funnyext", kArm, &odex_file, &error_msg)) << error_msg;
Richard Uhler63434112015-03-16 14:32:16 -07001007 EXPECT_EQ("/foo/bar/oat/arm/baz.odex", odex_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001008
Richard Uhlerb81881d2016-04-19 13:08:04 -07001009 EXPECT_FALSE(OatFileAssistant::DexLocationToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001010 "nopath.jar", kArm, &odex_file, &error_msg));
Richard Uhlerb81881d2016-04-19 13:08:04 -07001011 EXPECT_FALSE(OatFileAssistant::DexLocationToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001012 "/foo/bar/baz_noext", kArm, &odex_file, &error_msg));
Richard Uhler66d874d2015-01-15 09:37:19 -08001013}
1014
Richard Uhler23cedd22015-04-08 13:17:29 -07001015// Verify the dexopt status values from dalvik.system.DexFile
1016// match the OatFileAssistant::DexOptStatus values.
1017TEST_F(OatFileAssistantTest, DexOptStatusValues) {
Richard Uhler7225a8d2016-11-22 10:12:03 +00001018 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 Uhler7225a8d2016-11-22 10:12:03 +00001024 };
1025
Richard Uhler23cedd22015-04-08 13:17:29 -07001026 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 Uhler7225a8d2016-11-22 10:12:03 +00001034 for (std::pair<OatFileAssistant::DexOptNeeded, const char*> field : mapping) {
1035 ArtField* art_field = mirror::Class::FindStaticField(
Vladimir Marko19a4d372016-12-08 14:41:46 +00001036 soa.Self(), dexfile.Get(), field.second, "I");
Richard Uhler7225a8d2016-11-22 10:12:03 +00001037 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 Uhler23cedd22015-04-08 13:17:29 -07001041}
Richard Uhler66d874d2015-01-15 09:37:19 -08001042
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 Uhler66d874d2015-01-15 09:37:19 -08001046// * Test using secondary isa
Richard Uhler66d874d2015-01-15 09:37:19 -08001047// * 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 Uhler66d874d2015-01-15 09:37:19 -08001055} // namespace art