blob: 9b354893303d7518002edf07bc89b33c7012f0b1 [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 Uhler69bcf2c2017-01-24 10:25:21 +0000240// Case: We have a MultiDEX file where the non-main multdex entry is out of date.
Richard Uhler67ff7d12015-05-14 13:21:13 -0700241// Expect: The status is kDex2OatNeeded.
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000242TEST_F(OatFileAssistantTest, MultiDexNonMainOutOfDate) {
243 std::string dex_location = GetScratchDir() + "/MultiDexNonMainOutOfDate.jar";
Richard Uhler67ff7d12015-05-14 13:21:13 -0700244
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
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000249 // Now overwrite the dex file with GetMultiDexSrc2 so the non-main checksum
Richard Uhler67ff7d12015-05-14 13:21:13 -0700250 // 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 Uhler69bcf2c2017-01-24 10:25:21 +0000259// Case: We have a stripped MultiDEX file where the non-main multidex entry is
260// out of date with respect to the odex file.
261TEST_F(OatFileAssistantTest, StrippedMultiDexNonMainOutOfDate) {
262 std::string dex_location = GetScratchDir() + "/StrippedMultiDexNonMainOutOfDate.jar";
263 std::string odex_location = GetOdexDir() + "/StrippedMultiDexNonMainOutOfDate.odex";
264
265 // Compile the oat from GetMultiDexSrc1.
266 Copy(GetMultiDexSrc1(), dex_location);
267 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
268
269 // Compile the odex from GetMultiDexSrc2, which has a different non-main
270 // dex checksum.
271 Copy(GetMultiDexSrc2(), dex_location);
272 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kInterpretOnly);
273
274 // Strip the dex file.
275 Copy(GetStrippedDexSrc1(), dex_location);
276
277 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, /*load_executable*/false);
278
279 // Because the dex file is stripped, the odex file is considered the source
280 // of truth for the dex checksums. The oat file should be considered
281 // unusable.
282 std::unique_ptr<OatFile> best_file = oat_file_assistant.GetBestOatFile();
283 ASSERT_TRUE(best_file.get() != nullptr);
284 EXPECT_EQ(best_file->GetLocation(), odex_location);
285 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
286 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus());
287 EXPECT_EQ(OatFileAssistant::kOatDexOutOfDate, oat_file_assistant.OatFileStatus());
288}
289
Richard Uhlere5fed032015-03-18 08:21:11 -0700290// Case: We have a MultiDEX file and up-to-date OAT file for it with relative
291// encoded dex locations.
Richard Uhler95abd042015-03-24 09:51:28 -0700292// Expect: The oat file status is kNoDexOptNeeded.
Richard Uhlere5fed032015-03-18 08:21:11 -0700293TEST_F(OatFileAssistantTest, RelativeEncodedDexLocation) {
294 std::string dex_location = GetScratchDir() + "/RelativeEncodedDexLocation.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700295 std::string oat_location = GetOdexDir() + "/RelativeEncodedDexLocation.oat";
Richard Uhlere5fed032015-03-18 08:21:11 -0700296
297 // Create the dex file
298 Copy(GetMultiDexSrc1(), dex_location);
299
300 // Create the oat file with relative encoded dex location.
301 std::vector<std::string> args;
302 args.push_back("--dex-file=" + dex_location);
303 args.push_back("--dex-location=" + std::string("RelativeEncodedDexLocation.jar"));
304 args.push_back("--oat-file=" + oat_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000305 args.push_back("--compiler-filter=speed");
Richard Uhlere5fed032015-03-18 08:21:11 -0700306
307 std::string error_msg;
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700308 ASSERT_TRUE(OatFileAssistant::Dex2Oat(args, &error_msg)) << error_msg;
Richard Uhlere5fed032015-03-18 08:21:11 -0700309
310 // Verify we can load both dex files.
311 OatFileAssistant oat_file_assistant(dex_location.c_str(),
312 oat_location.c_str(),
Richard Uhlerd1472a22016-04-15 15:18:56 -0700313 kRuntimeISA, true);
Richard Uhlere5fed032015-03-18 08:21:11 -0700314 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
315 ASSERT_TRUE(oat_file.get() != nullptr);
316 EXPECT_TRUE(oat_file->IsExecutable());
317 std::vector<std::unique_ptr<const DexFile>> dex_files;
318 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800319 EXPECT_EQ(2u, dex_files.size());
320}
321
Richard Uhler03bc6592016-11-22 09:42:04 +0000322// Case: We have a DEX file and an OAT file out of date with respect to the
323// dex checksum.
324TEST_F(OatFileAssistantTest, OatDexOutOfDate) {
325 std::string dex_location = GetScratchDir() + "/OatDexOutOfDate.jar";
Richard Uhler66d874d2015-01-15 09:37:19 -0800326
327 // We create a dex, generate an oat for it, then overwrite the dex with a
328 // different dex to make the oat out of date.
329 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000330 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800331 Copy(GetDexSrc2(), dex_location);
332
Richard Uhlerd1472a22016-04-15 15:18:56 -0700333 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler7225a8d2016-11-22 10:12:03 +0000334 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000335 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000336 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000337 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800338
339 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000340 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
341 EXPECT_EQ(OatFileAssistant::kOatDexOutOfDate, oat_file_assistant.OatFileStatus());
342 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
343}
344
Richard Uhler2f27abd2017-01-31 14:02:34 +0000345// Case: We have a DEX file and an (ODEX) VDEX file out of date with respect
346// to the dex checksum, but no ODEX file.
347TEST_F(OatFileAssistantTest, VdexDexOutOfDate) {
348 // This test case is only meaningful if vdex is enabled.
349 if (!kIsVdexEnabled) {
350 return;
351 }
352
353 std::string dex_location = GetScratchDir() + "/VdexDexOutOfDate.jar";
354 std::string oat_location = GetOdexDir() + "/VdexDexOutOfDate.oat";
355
356 Copy(GetDexSrc1(), dex_location);
357 GenerateOdexForTest(dex_location, oat_location, CompilerFilter::kSpeed);
358 ASSERT_EQ(0, unlink(oat_location.c_str()));
359 Copy(GetDexSrc2(), dex_location);
360
361 OatFileAssistant oat_file_assistant(dex_location.c_str(),
362 oat_location.c_str(),
363 kRuntimeISA,
364 false);
365
366 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
367 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
368}
369
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000370// Case: We have a MultiDEX (ODEX) VDEX file where the non-main multidex entry
371// is out of date and there is no corresponding ODEX file.
372TEST_F(OatFileAssistantTest, VdexMultiDexNonMainOutOfDate) {
Richard Uhler2f27abd2017-01-31 14:02:34 +0000373 // This test case is only meaningful if vdex is enabled.
374 if (!kIsVdexEnabled) {
375 return;
376 }
377
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000378 std::string dex_location = GetScratchDir() + "/VdexMultiDexNonMainOutOfDate.jar";
379 std::string oat_location = GetOdexDir() + "/VdexMultiDexNonMainOutOfDate.oat";
Richard Uhler2f27abd2017-01-31 14:02:34 +0000380
381 Copy(GetMultiDexSrc1(), dex_location);
382 GenerateOdexForTest(dex_location, oat_location, CompilerFilter::kSpeed);
383 ASSERT_EQ(0, unlink(oat_location.c_str()));
384 Copy(GetMultiDexSrc2(), dex_location);
385
386 OatFileAssistant oat_file_assistant(dex_location.c_str(),
387 oat_location.c_str(),
388 kRuntimeISA,
389 false);
390
391 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
392 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
393}
394
Richard Uhler03bc6592016-11-22 09:42:04 +0000395// Case: We have a DEX file and an OAT file out of date with respect to the
396// boot image.
397TEST_F(OatFileAssistantTest, OatImageOutOfDate) {
398 std::string dex_location = GetScratchDir() + "/OatImageOutOfDate.jar";
399
400 Copy(GetDexSrc1(), dex_location);
401 GenerateOatForTest(dex_location.c_str(),
402 CompilerFilter::kSpeed,
403 /*relocate*/true,
404 /*pic*/false,
Richard Uhler03bc6592016-11-22 09:42:04 +0000405 /*with_alternate_image*/true);
406
407 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler7225a8d2016-11-22 10:12:03 +0000408 EXPECT_EQ(OatFileAssistant::kDex2OatForBootImage,
Richard Uhler03bc6592016-11-22 09:42:04 +0000409 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000410 EXPECT_EQ(OatFileAssistant::kDex2OatForBootImage,
Richard Uhler03bc6592016-11-22 09:42:04 +0000411 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000412 EXPECT_EQ(OatFileAssistant::kDex2OatForBootImage,
Richard Uhler03bc6592016-11-22 09:42:04 +0000413 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
414
415 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
416 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
417 EXPECT_EQ(OatFileAssistant::kOatBootImageOutOfDate, oat_file_assistant.OatFileStatus());
418 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
419}
420
421// Case: We have a DEX file and a verify-at-runtime OAT file out of date with
422// respect to the boot image.
423// It shouldn't matter that the OAT file is out of date, because it is
424// verify-at-runtime.
425TEST_F(OatFileAssistantTest, OatVerifyAtRuntimeImageOutOfDate) {
426 std::string dex_location = GetScratchDir() + "/OatVerifyAtRuntimeImageOutOfDate.jar";
427
428 Copy(GetDexSrc1(), dex_location);
429 GenerateOatForTest(dex_location.c_str(),
430 CompilerFilter::kVerifyAtRuntime,
431 /*relocate*/true,
432 /*pic*/false,
Richard Uhler03bc6592016-11-22 09:42:04 +0000433 /*with_alternate_image*/true);
434
435 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
436 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
437 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000438 EXPECT_EQ(OatFileAssistant::kDex2OatForFilter,
Richard Uhler03bc6592016-11-22 09:42:04 +0000439 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly));
440
441 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
442 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
443 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700444 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800445}
446
447// Case: We have a DEX file and an ODEX file, but no OAT file.
Richard Uhler66d874d2015-01-15 09:37:19 -0800448TEST_F(OatFileAssistantTest, DexOdexNoOat) {
449 std::string dex_location = GetScratchDir() + "/DexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700450 std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800451
452 // Create the dex and odex files
453 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000454 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800455
456 // Verify the status.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700457 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800458
Andreas Gampe29d38e72016-03-23 15:31:51 +0000459 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
460 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
Richard Uhler5923b522016-12-08 09:48:01 +0000461 EXPECT_EQ(-OatFileAssistant::kDex2OatForRelocation,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000462 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800463
464 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000465 EXPECT_EQ(OatFileAssistant::kOatRelocationOutOfDate, oat_file_assistant.OdexFileStatus());
466 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700467 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler5f946da2015-07-17 12:28:32 -0700468
469 // We should still be able to get the non-executable odex file to run from.
470 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
471 ASSERT_TRUE(oat_file.get() != nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800472}
473
Richard Uhler5923b522016-12-08 09:48:01 +0000474// Case: We have a stripped DEX file and a PIC ODEX file, but no OAT file.
Richard Uhler66d874d2015-01-15 09:37:19 -0800475TEST_F(OatFileAssistantTest, StrippedDexOdexNoOat) {
476 std::string dex_location = GetScratchDir() + "/StrippedDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700477 std::string odex_location = GetOdexDir() + "/StrippedDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800478
479 // Create the dex and odex files
480 Copy(GetDexSrc1(), dex_location);
Richard Uhler5923b522016-12-08 09:48:01 +0000481 GeneratePicOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800482
483 // Strip the dex file
484 Copy(GetStrippedDexSrc1(), dex_location);
485
486 // Verify the status.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700487 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800488
Richard Uhler5923b522016-12-08 09:48:01 +0000489 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000490 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800491
492 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler5923b522016-12-08 09:48:01 +0000493 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus());
Richard Uhler03bc6592016-11-22 09:42:04 +0000494 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700495 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800496
Richard Uhler66d874d2015-01-15 09:37:19 -0800497 // Verify we can load the dex files from it.
498 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
499 ASSERT_TRUE(oat_file.get() != nullptr);
500 EXPECT_TRUE(oat_file->IsExecutable());
501 std::vector<std::unique_ptr<const DexFile>> dex_files;
502 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
503 EXPECT_EQ(1u, dex_files.size());
504}
505
Richard Uhler5923b522016-12-08 09:48:01 +0000506// 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 -0800507TEST_F(OatFileAssistantTest, StrippedDexOdexOat) {
508 std::string dex_location = GetScratchDir() + "/StrippedDexOdexOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700509 std::string odex_location = GetOdexDir() + "/StrippedDexOdexOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800510
511 // Create the oat file from a different dex file so it looks out of date.
512 Copy(GetDexSrc2(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000513 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800514
515 // Create the odex file
516 Copy(GetDexSrc1(), dex_location);
Richard Uhler5923b522016-12-08 09:48:01 +0000517 GeneratePicOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800518
519 // Strip the dex file.
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 Uhler66d874d2015-01-15 09:37:19 -0800524
Andreas Gampe29d38e72016-03-23 15:31:51 +0000525 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
526 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
Andreas Gampe29d38e72016-03-23 15:31:51 +0000527 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
528 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
529 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, // Can't run dex2oat because dex file is stripped.
530 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
Richard Uhler66d874d2015-01-15 09:37:19 -0800531
532 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler5923b522016-12-08 09:48:01 +0000533 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus());
534 EXPECT_EQ(OatFileAssistant::kOatDexOutOfDate, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700535 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800536
537 // Verify we can load the dex files from it.
538 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
539 ASSERT_TRUE(oat_file.get() != nullptr);
540 EXPECT_TRUE(oat_file->IsExecutable());
541 std::vector<std::unique_ptr<const DexFile>> dex_files;
542 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
543 EXPECT_EQ(1u, dex_files.size());
544}
545
Richard Uhler9b994ea2015-06-24 08:44:19 -0700546// Case: We have a stripped (or resource-only) DEX file, no ODEX file and no
547// OAT file. Expect: The status is kNoDexOptNeeded.
548TEST_F(OatFileAssistantTest, ResourceOnlyDex) {
549 std::string dex_location = GetScratchDir() + "/ResourceOnlyDex.jar";
550
551 Copy(GetStrippedDexSrc1(), dex_location);
552
553 // Verify the status.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700554 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler9b994ea2015-06-24 08:44:19 -0700555
Andreas Gampe29d38e72016-03-23 15:31:51 +0000556 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
557 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
558 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
559 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
560 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
561 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700562
563 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000564 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
565 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700566 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
567
568 // Make the oat file up to date. This should have no effect.
569 std::string error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -0700570 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
Richard Uhler1e860612016-03-30 12:17:55 -0700571 EXPECT_EQ(OatFileAssistant::kUpdateSucceeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700572 oat_file_assistant.MakeUpToDate(false, &error_msg)) << error_msg;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700573
Andreas Gampe29d38e72016-03-23 15:31:51 +0000574 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
575 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700576
577 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000578 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
579 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700580 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
581}
582
Richard Uhler66d874d2015-01-15 09:37:19 -0800583// Case: We have a DEX file, an ODEX file and an OAT file, where the ODEX and
584// OAT files both have patch delta of 0.
Richard Uhler5923b522016-12-08 09:48:01 +0000585// Expect: It shouldn't crash.
Richard Uhler66d874d2015-01-15 09:37:19 -0800586TEST_F(OatFileAssistantTest, OdexOatOverlap) {
587 std::string dex_location = GetScratchDir() + "/OdexOatOverlap.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700588 std::string odex_location = GetOdexDir() + "/OdexOatOverlap.odex";
589 std::string oat_location = GetOdexDir() + "/OdexOatOverlap.oat";
Richard Uhler66d874d2015-01-15 09:37:19 -0800590
591 // Create the dex and odex files
592 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000593 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800594
595 // Create the oat file by copying the odex so they are located in the same
596 // place in memory.
597 Copy(odex_location, oat_location);
598
599 // Verify things don't go bad.
600 OatFileAssistant oat_file_assistant(dex_location.c_str(),
Richard Uhlerd1472a22016-04-15 15:18:56 -0700601 oat_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800602
Richard Uhler5923b522016-12-08 09:48:01 +0000603 // kDex2OatForRelocation is expected rather than -kDex2OatForRelocation
Richard Uhler7225a8d2016-11-22 10:12:03 +0000604 // based on the assumption that the oat location is more up-to-date than the odex
Richard Uhler70a84262016-11-08 16:51:51 +0000605 // location, even if they both need relocation.
Richard Uhler5923b522016-12-08 09:48:01 +0000606 EXPECT_EQ(OatFileAssistant::kDex2OatForRelocation,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000607 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800608
609 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000610 EXPECT_EQ(OatFileAssistant::kOatRelocationOutOfDate, oat_file_assistant.OdexFileStatus());
611 EXPECT_EQ(OatFileAssistant::kOatRelocationOutOfDate, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700612 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800613
614 // Things aren't relocated, so it should fall back to interpreted.
615 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
616 ASSERT_TRUE(oat_file.get() != nullptr);
Richard Uhlerf16d5722015-05-11 09:32:47 -0700617
Richard Uhler66d874d2015-01-15 09:37:19 -0800618 EXPECT_FALSE(oat_file->IsExecutable());
619 std::vector<std::unique_ptr<const DexFile>> dex_files;
620 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
621 EXPECT_EQ(1u, dex_files.size());
622}
623
624// Case: We have a DEX file and a PIC ODEX file, but no OAT file.
Richard Uhler95abd042015-03-24 09:51:28 -0700625// Expect: The status is kNoDexOptNeeded, because PIC needs no relocation.
Richard Uhler66d874d2015-01-15 09:37:19 -0800626TEST_F(OatFileAssistantTest, DexPicOdexNoOat) {
627 std::string dex_location = GetScratchDir() + "/DexPicOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700628 std::string odex_location = GetOdexDir() + "/DexPicOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800629
630 // Create the dex and odex files
631 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000632 GeneratePicOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800633
634 // Verify the status.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700635 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800636
Andreas Gampe29d38e72016-03-23 15:31:51 +0000637 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
638 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000639 EXPECT_EQ(-OatFileAssistant::kDex2OatForFilter,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000640 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
Richard Uhler66d874d2015-01-15 09:37:19 -0800641
642 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler3e580bc2016-11-08 16:23:07 +0000643 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus());
Richard Uhler03bc6592016-11-22 09:42:04 +0000644 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700645 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800646}
647
Andreas Gampe29d38e72016-03-23 15:31:51 +0000648// Case: We have a DEX file and a VerifyAtRuntime ODEX file, but no OAT file.
649// Expect: The status is kNoDexOptNeeded, because VerifyAtRuntime contains no code.
650TEST_F(OatFileAssistantTest, DexVerifyAtRuntimeOdexNoOat) {
651 std::string dex_location = GetScratchDir() + "/DexVerifyAtRuntimeOdexNoOat.jar";
652 std::string odex_location = GetOdexDir() + "/DexVerifyAtRuntimeOdexNoOat.odex";
David Brazdilce4b0ba2016-01-28 15:05:49 +0000653
654 // Create the dex and odex files
655 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000656 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kVerifyAtRuntime);
David Brazdilce4b0ba2016-01-28 15:05:49 +0000657
658 // Verify the status.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700659 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
David Brazdilce4b0ba2016-01-28 15:05:49 +0000660
Andreas Gampe29d38e72016-03-23 15:31:51 +0000661 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
662 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000663 EXPECT_EQ(-OatFileAssistant::kDex2OatForFilter,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000664 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
David Brazdilce4b0ba2016-01-28 15:05:49 +0000665
666 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler3e580bc2016-11-08 16:23:07 +0000667 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus());
Richard Uhler03bc6592016-11-22 09:42:04 +0000668 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
David Brazdilce4b0ba2016-01-28 15:05:49 +0000669 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
670}
671
Richard Uhler66d874d2015-01-15 09:37:19 -0800672// Case: We have a DEX file and up-to-date OAT file for it.
673// Expect: We should load an executable dex file.
674TEST_F(OatFileAssistantTest, LoadOatUpToDate) {
675 std::string dex_location = GetScratchDir() + "/LoadOatUpToDate.jar";
676
677 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000678 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800679
680 // Load the oat using an oat file assistant.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700681 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000682
683 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
684 ASSERT_TRUE(oat_file.get() != nullptr);
685 EXPECT_TRUE(oat_file->IsExecutable());
686 std::vector<std::unique_ptr<const DexFile>> dex_files;
687 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
688 EXPECT_EQ(1u, dex_files.size());
689}
690
691// Case: We have a DEX file and up-to-date interpret-only OAT file for it.
692// Expect: We should still load the oat file as executable.
693TEST_F(OatFileAssistantTest, LoadExecInterpretOnlyOatUpToDate) {
694 std::string dex_location = GetScratchDir() + "/LoadExecInterpretOnlyOatUpToDate.jar";
695
696 Copy(GetDexSrc1(), dex_location);
697 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kInterpretOnly);
698
699 // Load the oat using an oat file assistant.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700700 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800701
702 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
703 ASSERT_TRUE(oat_file.get() != nullptr);
704 EXPECT_TRUE(oat_file->IsExecutable());
705 std::vector<std::unique_ptr<const DexFile>> dex_files;
706 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
707 EXPECT_EQ(1u, dex_files.size());
708}
709
710// Case: We have a DEX file and up-to-date OAT file for it.
711// Expect: Loading non-executable should load the oat non-executable.
712TEST_F(OatFileAssistantTest, LoadNoExecOatUpToDate) {
713 std::string dex_location = GetScratchDir() + "/LoadNoExecOatUpToDate.jar";
714
715 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000716 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800717
718 // Load the oat using an oat file assistant.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700719 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800720
721 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
722 ASSERT_TRUE(oat_file.get() != nullptr);
723 EXPECT_FALSE(oat_file->IsExecutable());
724 std::vector<std::unique_ptr<const DexFile>> dex_files;
725 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
726 EXPECT_EQ(1u, dex_files.size());
727}
728
729// Case: We have a DEX file.
730// Expect: We should load an executable dex file from an alternative oat
731// location.
732TEST_F(OatFileAssistantTest, LoadDexNoAlternateOat) {
733 std::string dex_location = GetScratchDir() + "/LoadDexNoAlternateOat.jar";
734 std::string oat_location = GetScratchDir() + "/LoadDexNoAlternateOat.oat";
735
736 Copy(GetDexSrc1(), dex_location);
737
738 OatFileAssistant oat_file_assistant(
Richard Uhlerd1472a22016-04-15 15:18:56 -0700739 dex_location.c_str(), oat_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800740 std::string error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -0700741 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
Richard Uhler1e860612016-03-30 12:17:55 -0700742 ASSERT_EQ(OatFileAssistant::kUpdateSucceeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700743 oat_file_assistant.MakeUpToDate(false, &error_msg)) << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800744
745 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
746 ASSERT_TRUE(oat_file.get() != nullptr);
747 EXPECT_TRUE(oat_file->IsExecutable());
748 std::vector<std::unique_ptr<const DexFile>> dex_files;
749 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
750 EXPECT_EQ(1u, dex_files.size());
751
752 EXPECT_TRUE(OS::FileExists(oat_location.c_str()));
753
754 // Verify it didn't create an oat in the default location.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700755 OatFileAssistant ofm(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler03bc6592016-11-22 09:42:04 +0000756 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, ofm.OatFileStatus());
Richard Uhler66d874d2015-01-15 09:37:19 -0800757}
758
Richard Uhler8327cf72015-10-13 16:34:59 -0700759// Case: We have a DEX file but can't write the oat file.
760// Expect: We should fail to make the oat file up to date.
761TEST_F(OatFileAssistantTest, LoadDexUnwriteableAlternateOat) {
762 std::string dex_location = GetScratchDir() + "/LoadDexUnwriteableAlternateOat.jar";
763
764 // Make the oat location unwritable by inserting some non-existent
765 // intermediate directories.
766 std::string oat_location = GetScratchDir() + "/foo/bar/LoadDexUnwriteableAlternateOat.oat";
767
768 Copy(GetDexSrc1(), dex_location);
769
770 OatFileAssistant oat_file_assistant(
Richard Uhlerd1472a22016-04-15 15:18:56 -0700771 dex_location.c_str(), oat_location.c_str(), kRuntimeISA, true);
Richard Uhler8327cf72015-10-13 16:34:59 -0700772 std::string error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -0700773 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
Richard Uhler1e860612016-03-30 12:17:55 -0700774 ASSERT_EQ(OatFileAssistant::kUpdateNotAttempted,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700775 oat_file_assistant.MakeUpToDate(false, &error_msg));
Richard Uhler8327cf72015-10-13 16:34:59 -0700776
777 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
778 ASSERT_TRUE(oat_file.get() == nullptr);
779}
780
781// Case: We don't have a DEX file and can't write the oat file.
782// Expect: We should fail to generate the oat file without crashing.
783TEST_F(OatFileAssistantTest, GenNoDex) {
784 std::string dex_location = GetScratchDir() + "/GenNoDex.jar";
785 std::string oat_location = GetScratchDir() + "/GenNoDex.oat";
786
787 OatFileAssistant oat_file_assistant(
Richard Uhlerd1472a22016-04-15 15:18:56 -0700788 dex_location.c_str(), oat_location.c_str(), kRuntimeISA, true);
Richard Uhler8327cf72015-10-13 16:34:59 -0700789 std::string error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -0700790 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
Richard Uhler1e860612016-03-30 12:17:55 -0700791 EXPECT_EQ(OatFileAssistant::kUpdateNotAttempted,
Richard Uhlerf4b34872016-04-13 11:03:46 -0700792 oat_file_assistant.GenerateOatFile(&error_msg));
Richard Uhler8327cf72015-10-13 16:34:59 -0700793}
794
Richard Uhler66d874d2015-01-15 09:37:19 -0800795// Turn an absolute path into a path relative to the current working
796// directory.
Andreas Gampeca620d72016-11-08 08:09:33 -0800797static std::string MakePathRelative(const std::string& target) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800798 char buf[MAXPATHLEN];
799 std::string cwd = getcwd(buf, MAXPATHLEN);
800
801 // Split the target and cwd paths into components.
802 std::vector<std::string> target_path;
803 std::vector<std::string> cwd_path;
804 Split(target, '/', &target_path);
805 Split(cwd, '/', &cwd_path);
806
807 // Reverse the path components, so we can use pop_back().
808 std::reverse(target_path.begin(), target_path.end());
809 std::reverse(cwd_path.begin(), cwd_path.end());
810
811 // Drop the common prefix of the paths. Because we reversed the path
812 // components, this becomes the common suffix of target_path and cwd_path.
813 while (!target_path.empty() && !cwd_path.empty()
814 && target_path.back() == cwd_path.back()) {
815 target_path.pop_back();
816 cwd_path.pop_back();
817 }
818
819 // For each element of the remaining cwd_path, add '..' to the beginning
820 // of the target path. Because we reversed the path components, we add to
821 // the end of target_path.
822 for (unsigned int i = 0; i < cwd_path.size(); i++) {
823 target_path.push_back("..");
824 }
825
826 // Reverse again to get the right path order, and join to get the result.
827 std::reverse(target_path.begin(), target_path.end());
Andreas Gampe9186ced2016-12-12 14:28:21 -0800828 return android::base::Join(target_path, '/');
Richard Uhler66d874d2015-01-15 09:37:19 -0800829}
830
831// Case: Non-absolute path to Dex location.
832// Expect: Not sure, but it shouldn't crash.
833TEST_F(OatFileAssistantTest, NonAbsoluteDexLocation) {
834 std::string abs_dex_location = GetScratchDir() + "/NonAbsoluteDexLocation.jar";
835 Copy(GetDexSrc1(), abs_dex_location);
836
837 std::string dex_location = MakePathRelative(abs_dex_location);
Richard Uhlerd1472a22016-04-15 15:18:56 -0700838 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800839
840 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler7225a8d2016-11-22 10:12:03 +0000841 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000842 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler03bc6592016-11-22 09:42:04 +0000843 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
844 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler66d874d2015-01-15 09:37:19 -0800845}
846
847// Case: Very short, non-existent Dex location.
Richard Uhler9b994ea2015-06-24 08:44:19 -0700848// Expect: kNoDexOptNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800849TEST_F(OatFileAssistantTest, ShortDexLocation) {
850 std::string dex_location = "/xx";
851
Richard Uhlerd1472a22016-04-15 15:18:56 -0700852 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800853
854 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Andreas Gampe29d38e72016-03-23 15:31:51 +0000855 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
856 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler03bc6592016-11-22 09:42:04 +0000857 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
858 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700859 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800860
Richard Uhler9b994ea2015-06-24 08:44:19 -0700861 // Trying to make it up to date should have no effect.
Richard Uhler66d874d2015-01-15 09:37:19 -0800862 std::string error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -0700863 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
Richard Uhler1e860612016-03-30 12:17:55 -0700864 EXPECT_EQ(OatFileAssistant::kUpdateSucceeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700865 oat_file_assistant.MakeUpToDate(false, &error_msg));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700866 EXPECT_TRUE(error_msg.empty());
Richard Uhler66d874d2015-01-15 09:37:19 -0800867}
868
869// Case: Non-standard extension for dex file.
Richard Uhler95abd042015-03-24 09:51:28 -0700870// Expect: The status is kDex2OatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800871TEST_F(OatFileAssistantTest, LongDexExtension) {
872 std::string dex_location = GetScratchDir() + "/LongDexExtension.jarx";
873 Copy(GetDexSrc1(), dex_location);
874
Richard Uhlerd1472a22016-04-15 15:18:56 -0700875 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800876
Richard Uhler7225a8d2016-11-22 10:12:03 +0000877 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000878 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800879
880 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000881 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
882 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler66d874d2015-01-15 09:37:19 -0800883}
884
885// A task to generate a dex location. Used by the RaceToGenerate test.
886class RaceGenerateTask : public Task {
887 public:
888 explicit RaceGenerateTask(const std::string& dex_location, const std::string& oat_location)
Jeff Haof0192c82016-03-28 20:39:50 -0700889 : dex_location_(dex_location), oat_location_(oat_location), loaded_oat_file_(nullptr)
Richard Uhler66d874d2015-01-15 09:37:19 -0800890 {}
891
Roland Levillain4b8f1ec2015-08-26 18:34:03 +0100892 void Run(Thread* self ATTRIBUTE_UNUSED) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800893 // Load the dex files, and save a pointer to the loaded oat file, so that
894 // we can verify only one oat file was loaded for the dex location.
Richard Uhler66d874d2015-01-15 09:37:19 -0800895 std::vector<std::unique_ptr<const DexFile>> dex_files;
896 std::vector<std::string> error_msgs;
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700897 const OatFile* oat_file = nullptr;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700898 dex_files = Runtime::Current()->GetOatFileManager().OpenDexFilesFromOat(
899 dex_location_.c_str(),
900 oat_location_.c_str(),
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800901 /*class_loader*/nullptr,
902 /*dex_elements*/nullptr,
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700903 &oat_file,
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700904 &error_msgs);
Andreas Gampe9186ced2016-12-12 14:28:21 -0800905 CHECK(!dex_files.empty()) << android::base::Join(error_msgs, '\n');
Richard Uhler07b3c232015-03-31 15:57:54 -0700906 CHECK(dex_files[0]->GetOatDexFile() != nullptr) << dex_files[0]->GetLocation();
907 loaded_oat_file_ = dex_files[0]->GetOatDexFile()->GetOatFile();
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700908 CHECK_EQ(loaded_oat_file_, oat_file);
Richard Uhler66d874d2015-01-15 09:37:19 -0800909 }
910
911 const OatFile* GetLoadedOatFile() const {
912 return loaded_oat_file_;
913 }
914
915 private:
916 std::string dex_location_;
917 std::string oat_location_;
918 const OatFile* loaded_oat_file_;
919};
920
921// Test the case where multiple processes race to generate an oat file.
922// This simulates multiple processes using multiple threads.
923//
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700924// We want unique Oat files to be loaded even when there is a race to load.
925// TODO: The test case no longer tests locking the way it was intended since we now get multiple
926// copies of the same Oat files mapped at different locations.
Richard Uhler66d874d2015-01-15 09:37:19 -0800927TEST_F(OatFileAssistantTest, RaceToGenerate) {
928 std::string dex_location = GetScratchDir() + "/RaceToGenerate.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700929 std::string oat_location = GetOdexDir() + "/RaceToGenerate.oat";
Richard Uhler66d874d2015-01-15 09:37:19 -0800930
931 // We use the lib core dex file, because it's large, and hopefully should
932 // take a while to generate.
Narayan Kamathd1ef4362015-11-12 11:49:06 +0000933 Copy(GetLibCoreDexFileNames()[0], dex_location);
Richard Uhler66d874d2015-01-15 09:37:19 -0800934
935 const int kNumThreads = 32;
936 Thread* self = Thread::Current();
937 ThreadPool thread_pool("Oat file assistant test thread pool", kNumThreads);
938 std::vector<std::unique_ptr<RaceGenerateTask>> tasks;
939 for (int i = 0; i < kNumThreads; i++) {
940 std::unique_ptr<RaceGenerateTask> task(new RaceGenerateTask(dex_location, oat_location));
941 thread_pool.AddTask(self, task.get());
942 tasks.push_back(std::move(task));
943 }
944 thread_pool.StartWorkers(self);
945 thread_pool.Wait(self, true, false);
946
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700947 // Verify every task got a unique oat file.
948 std::set<const OatFile*> oat_files;
Richard Uhler66d874d2015-01-15 09:37:19 -0800949 for (auto& task : tasks) {
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700950 const OatFile* oat_file = task->GetLoadedOatFile();
951 EXPECT_TRUE(oat_files.find(oat_file) == oat_files.end());
952 oat_files.insert(oat_file);
Richard Uhler66d874d2015-01-15 09:37:19 -0800953 }
954}
955
956// Case: We have a DEX file and an ODEX file, no OAT file, and dex2oat is
957// disabled.
958// Expect: We should load the odex file non-executable.
959TEST_F(OatFileAssistantNoDex2OatTest, LoadDexOdexNoOat) {
960 std::string dex_location = GetScratchDir() + "/LoadDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700961 std::string odex_location = GetOdexDir() + "/LoadDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800962
963 // Create the dex and odex files
964 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000965 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800966
967 // Load the oat using an executable oat file assistant.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700968 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800969
970 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
971 ASSERT_TRUE(oat_file.get() != nullptr);
972 EXPECT_FALSE(oat_file->IsExecutable());
973 std::vector<std::unique_ptr<const DexFile>> dex_files;
974 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
975 EXPECT_EQ(1u, dex_files.size());
976}
977
978// Case: We have a MultiDEX file and an ODEX file, no OAT file, and dex2oat is
979// disabled.
980// Expect: We should load the odex file non-executable.
981TEST_F(OatFileAssistantNoDex2OatTest, LoadMultiDexOdexNoOat) {
982 std::string dex_location = GetScratchDir() + "/LoadMultiDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700983 std::string odex_location = GetOdexDir() + "/LoadMultiDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800984
985 // Create the dex and odex files
986 Copy(GetMultiDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000987 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800988
989 // Load the oat using an executable oat file assistant.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700990 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800991
992 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
993 ASSERT_TRUE(oat_file.get() != nullptr);
994 EXPECT_FALSE(oat_file->IsExecutable());
995 std::vector<std::unique_ptr<const DexFile>> dex_files;
996 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
997 EXPECT_EQ(2u, dex_files.size());
998}
999
Richard Uhlerf4b34872016-04-13 11:03:46 -07001000TEST_F(OatFileAssistantTest, RuntimeCompilerFilterOptionUsed) {
1001 std::string dex_location = GetScratchDir() + "/RuntimeCompilerFilterOptionUsed.jar";
1002 Copy(GetDexSrc1(), dex_location);
1003
Richard Uhlerd1472a22016-04-15 15:18:56 -07001004 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhlerf4b34872016-04-13 11:03:46 -07001005
1006 std::string error_msg;
1007 Runtime::Current()->AddCompilerOption("--compiler-filter=interpret-only");
1008 EXPECT_EQ(OatFileAssistant::kUpdateSucceeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -07001009 oat_file_assistant.MakeUpToDate(false, &error_msg)) << error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -07001010 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
1011 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly));
Richard Uhler7225a8d2016-11-22 10:12:03 +00001012 EXPECT_EQ(OatFileAssistant::kDex2OatForFilter,
Richard Uhlerf4b34872016-04-13 11:03:46 -07001013 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
1014
1015 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
1016 EXPECT_EQ(OatFileAssistant::kUpdateSucceeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -07001017 oat_file_assistant.MakeUpToDate(false, &error_msg)) << error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -07001018 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
1019 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly));
1020 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
1021 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
1022
1023 Runtime::Current()->AddCompilerOption("--compiler-filter=bogus");
1024 EXPECT_EQ(OatFileAssistant::kUpdateNotAttempted,
Richard Uhlerd1472a22016-04-15 15:18:56 -07001025 oat_file_assistant.MakeUpToDate(false, &error_msg));
Richard Uhlerf4b34872016-04-13 11:03:46 -07001026}
1027
Richard Uhlerb81881d2016-04-19 13:08:04 -07001028TEST(OatFileAssistantUtilsTest, DexLocationToOdexFilename) {
Richard Uhler66d874d2015-01-15 09:37:19 -08001029 std::string error_msg;
1030 std::string odex_file;
1031
Richard Uhlerb81881d2016-04-19 13:08:04 -07001032 EXPECT_TRUE(OatFileAssistant::DexLocationToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001033 "/foo/bar/baz.jar", kArm, &odex_file, &error_msg)) << error_msg;
Richard Uhler63434112015-03-16 14:32:16 -07001034 EXPECT_EQ("/foo/bar/oat/arm/baz.odex", odex_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001035
Richard Uhlerb81881d2016-04-19 13:08:04 -07001036 EXPECT_TRUE(OatFileAssistant::DexLocationToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001037 "/foo/bar/baz.funnyext", kArm, &odex_file, &error_msg)) << error_msg;
Richard Uhler63434112015-03-16 14:32:16 -07001038 EXPECT_EQ("/foo/bar/oat/arm/baz.odex", odex_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001039
Richard Uhlerb81881d2016-04-19 13:08:04 -07001040 EXPECT_FALSE(OatFileAssistant::DexLocationToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001041 "nopath.jar", kArm, &odex_file, &error_msg));
Richard Uhlerb81881d2016-04-19 13:08:04 -07001042 EXPECT_FALSE(OatFileAssistant::DexLocationToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001043 "/foo/bar/baz_noext", kArm, &odex_file, &error_msg));
Richard Uhler66d874d2015-01-15 09:37:19 -08001044}
1045
Richard Uhler23cedd22015-04-08 13:17:29 -07001046// Verify the dexopt status values from dalvik.system.DexFile
1047// match the OatFileAssistant::DexOptStatus values.
1048TEST_F(OatFileAssistantTest, DexOptStatusValues) {
Richard Uhler7225a8d2016-11-22 10:12:03 +00001049 std::pair<OatFileAssistant::DexOptNeeded, const char*> mapping[] = {
1050 {OatFileAssistant::kNoDexOptNeeded, "NO_DEXOPT_NEEDED"},
1051 {OatFileAssistant::kDex2OatFromScratch, "DEX2OAT_FROM_SCRATCH"},
1052 {OatFileAssistant::kDex2OatForBootImage, "DEX2OAT_FOR_BOOT_IMAGE"},
1053 {OatFileAssistant::kDex2OatForFilter, "DEX2OAT_FOR_FILTER"},
1054 {OatFileAssistant::kDex2OatForRelocation, "DEX2OAT_FOR_RELOCATION"},
Richard Uhler7225a8d2016-11-22 10:12:03 +00001055 };
1056
Richard Uhler23cedd22015-04-08 13:17:29 -07001057 ScopedObjectAccess soa(Thread::Current());
1058 StackHandleScope<1> hs(soa.Self());
1059 ClassLinker* linker = Runtime::Current()->GetClassLinker();
1060 Handle<mirror::Class> dexfile(
1061 hs.NewHandle(linker->FindSystemClass(soa.Self(), "Ldalvik/system/DexFile;")));
Andreas Gampefa4333d2017-02-14 11:10:34 -08001062 ASSERT_FALSE(dexfile == nullptr);
Richard Uhler23cedd22015-04-08 13:17:29 -07001063 linker->EnsureInitialized(soa.Self(), dexfile, true, true);
1064
Richard Uhler7225a8d2016-11-22 10:12:03 +00001065 for (std::pair<OatFileAssistant::DexOptNeeded, const char*> field : mapping) {
1066 ArtField* art_field = mirror::Class::FindStaticField(
Vladimir Marko19a4d372016-12-08 14:41:46 +00001067 soa.Self(), dexfile.Get(), field.second, "I");
Richard Uhler7225a8d2016-11-22 10:12:03 +00001068 ASSERT_FALSE(art_field == nullptr);
1069 EXPECT_EQ(art_field->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
1070 EXPECT_EQ(field.first, art_field->GetInt(dexfile.Get()));
1071 }
Richard Uhler23cedd22015-04-08 13:17:29 -07001072}
Richard Uhler66d874d2015-01-15 09:37:19 -08001073
1074// TODO: More Tests:
1075// * Test class linker falls back to unquickened dex for DexNoOat
1076// * Test class linker falls back to unquickened dex for MultiDexNoOat
Richard Uhler66d874d2015-01-15 09:37:19 -08001077// * Test using secondary isa
Richard Uhler66d874d2015-01-15 09:37:19 -08001078// * Test for status of oat while oat is being generated (how?)
1079// * Test case where 32 and 64 bit boot class paths differ,
1080// and we ask IsInBootClassPath for a class in exactly one of the 32 or
1081// 64 bit boot class paths.
1082// * Test unexpected scenarios (?):
1083// - Dex is stripped, don't have odex.
1084// - Oat file corrupted after status check, before reload unexecutable
1085// because it's unrelocated and no dex2oat
Richard Uhler66d874d2015-01-15 09:37:19 -08001086} // namespace art