blob: a99bd514572b44203f41f145c865a61de1a96459 [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
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070017#include "oat_file_assistant.h"
18
Richard Uhler66d874d2015-01-15 09:37:19 -080019#include <sys/param.h>
20
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070021#include <string>
22#include <vector>
Shubham Ajmerab22dea02017-10-04 18:36:41 -070023#include <fcntl.h>
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070024
Richard Uhler66d874d2015-01-15 09:37:19 -080025#include <gtest/gtest.h>
26
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070027#include "android-base/strings.h"
28
Mathieu Chartierc7853442015-03-27 14:35:38 -070029#include "art_field-inl.h"
David Sehrc431b9d2018-03-02 12:01:51 -080030#include "base/os.h"
31#include "base/utils.h"
Vladimir Marko3481ba22015-04-13 12:22:36 +010032#include "class_linker-inl.h"
Calin Juravle27e0d1f2017-07-26 00:16:07 -070033#include "class_loader_context.h"
Jeff Hao0cb17282017-07-12 14:51:49 -070034#include "common_runtime_test.h"
Calin Juravle36eb3132017-01-13 16:32:38 -080035#include "dexopt_test.h"
David Brazdil32bde992018-05-14 15:24:34 +010036#include "hidden_api.h"
Calin Juravle27e0d1f2017-07-26 00:16:07 -070037#include "oat_file.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070038#include "oat_file_manager.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070039#include "scoped_thread_state_change-inl.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070040#include "thread-current-inl.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080041
42namespace art {
43
Calin Juravle5f9a8012018-02-12 20:27:46 -080044class OatFileAssistantTest : public DexoptTest {
45 public:
46 void VerifyOptimizationStatus(const std::string& file,
47 const std::string& expected_filter,
48 const std::string& expected_reason) {
49 std::string compilation_filter;
50 std::string compilation_reason;
51 OatFileAssistant::GetOptimizationStatus(
52 file, kRuntimeISA, &compilation_filter, &compilation_reason);
53
54 ASSERT_EQ(expected_filter, compilation_filter);
55 ASSERT_EQ(expected_reason, compilation_reason);
56 }
57
58 void VerifyOptimizationStatus(const std::string& file,
59 CompilerFilter::Filter expected_filter,
60 const std::string& expected_reason) {
61 VerifyOptimizationStatus(
62 file, CompilerFilter::NameOfFilter(expected_filter), expected_reason);
63 }
Vladimir Markof3d88a82018-12-21 16:38:47 +000064 void InsertNewBootClasspathEntry() {
65 std::string extra_dex_filename = GetMultiDexSrc1();
66 Runtime* runtime = Runtime::Current();
67 runtime->boot_class_path_.push_back(extra_dex_filename);
68 if (!runtime->boot_class_path_locations_.empty()) {
69 runtime->boot_class_path_locations_.push_back(extra_dex_filename);
70 }
71 }
Calin Juravle5f9a8012018-02-12 20:27:46 -080072};
Richard Uhler66d874d2015-01-15 09:37:19 -080073
Calin Juravle357c66d2017-05-04 01:57:17 +000074class ScopedNonWritable {
75 public:
76 explicit ScopedNonWritable(const std::string& dex_location) {
77 is_valid_ = false;
78 size_t pos = dex_location.rfind('/');
79 if (pos != std::string::npos) {
80 is_valid_ = true;
81 dex_parent_ = dex_location.substr(0, pos);
82 if (chmod(dex_parent_.c_str(), 0555) != 0) {
83 PLOG(ERROR) << "Could not change permissions on " << dex_parent_;
84 }
85 }
86 }
87
88 bool IsSuccessful() { return is_valid_ && (access(dex_parent_.c_str(), W_OK) != 0); }
89
90 ~ScopedNonWritable() {
91 if (is_valid_) {
92 if (chmod(dex_parent_.c_str(), 0777) != 0) {
93 PLOG(ERROR) << "Could not restore permissions on " << dex_parent_;
94 }
95 }
96 }
97
98 private:
99 std::string dex_parent_;
100 bool is_valid_;
101};
102
103static bool IsExecutedAsRoot() {
104 return geteuid() == 0;
105}
Calin Juravle36eb3132017-01-13 16:32:38 -0800106
Nicolas Geoffray3d8a78a2018-08-29 21:10:16 +0000107// Case: We have a MultiDEX file and up-to-date ODEX file for it with relative
108// encoded dex locations.
109// Expect: The oat file status is kNoDexOptNeeded.
110TEST_F(OatFileAssistantTest, RelativeEncodedDexLocation) {
111 std::string dex_location = GetScratchDir() + "/RelativeEncodedDexLocation.jar";
112 std::string odex_location = GetOdexDir() + "/RelativeEncodedDexLocation.odex";
113
114 // Create the dex file
115 Copy(GetMultiDexSrc1(), dex_location);
116
117 // Create the oat file with relative encoded dex location.
118 std::vector<std::string> args = {
119 "--dex-file=" + dex_location,
120 "--dex-location=" + std::string("RelativeEncodedDexLocation.jar"),
121 "--oat-file=" + odex_location,
122 "--compiler-filter=speed"
123 };
124
125 std::string error_msg;
126 ASSERT_TRUE(Dex2Oat(args, &error_msg)) << error_msg;
127
128 // Verify we can load both dex files.
129 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
130
131 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
132 ASSERT_TRUE(oat_file.get() != nullptr);
133 EXPECT_TRUE(oat_file->IsExecutable());
134 std::vector<std::unique_ptr<const DexFile>> dex_files;
135 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
136 EXPECT_EQ(2u, dex_files.size());
137}
138
139TEST_F(OatFileAssistantTest, MakeUpToDateWithContext) {
140 std::string dex_location = GetScratchDir() + "/TestDex.jar";
141 std::string odex_location = GetOdexDir() + "/TestDex.odex";
142 std::string context_location = GetScratchDir() + "/ContextDex.jar";
143 Copy(GetDexSrc1(), dex_location);
144 Copy(GetDexSrc2(), context_location);
145
146 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
147
148 std::string context_str = "PCL[" + context_location + "]";
149 std::unique_ptr<ClassLoaderContext> context = ClassLoaderContext::Create(context_str);
150 ASSERT_TRUE(context != nullptr);
151 ASSERT_TRUE(context->OpenDexFiles(kRuntimeISA, ""));
152
153 std::string error_msg;
154 std::vector<std::string> args;
155 args.push_back("--dex-file=" + dex_location);
156 args.push_back("--oat-file=" + odex_location);
157 args.push_back("--class-loader-context=" + context_str);
158 ASSERT_TRUE(Dex2Oat(args, &error_msg)) << error_msg;
159
160 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
161 EXPECT_NE(nullptr, oat_file.get());
162 EXPECT_EQ(context->EncodeContextForOatFile(""),
163 oat_file->GetOatHeader().GetStoreValueByKey(OatHeader::kClassPathKey));
164}
165
166TEST_F(OatFileAssistantTest, GetDexOptNeededWithUpToDateContextRelative) {
167 std::string dex_location = GetScratchDir() + "/TestDex.jar";
168 std::string odex_location = GetOdexDir() + "/TestDex.odex";
169 std::string context_location = GetScratchDir() + "/ContextDex.jar";
170 Copy(GetDexSrc1(), dex_location);
171 Copy(GetDexSrc2(), context_location);
172
173 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
174
175 std::string context_str = "PCL[" + context_location + "]";
176 std::unique_ptr<ClassLoaderContext> context = ClassLoaderContext::Create(context_str);
177 ASSERT_TRUE(context != nullptr);
178 ASSERT_TRUE(context->OpenDexFiles(kRuntimeISA, ""));
179
180 std::string error_msg;
181 std::vector<std::string> args;
182 args.push_back("--dex-file=" + dex_location);
183 args.push_back("--oat-file=" + odex_location);
184 args.push_back("--class-loader-context=" + context_str);
185 ASSERT_TRUE(Dex2Oat(args, &error_msg)) << error_msg;
186
187 // A relative context simulates a dependent split context.
188 std::unique_ptr<ClassLoaderContext> relative_context =
189 ClassLoaderContext::Create("PCL[ContextDex.jar]");
190 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
191 oat_file_assistant.GetDexOptNeeded(
192 CompilerFilter::kDefaultCompilerFilter,
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700193 /* profile_changed= */ false,
194 /* downgrade= */ false,
Nicolas Geoffray3d8a78a2018-08-29 21:10:16 +0000195 relative_context.get()));
196}
197
Richard Uhler66d874d2015-01-15 09:37:19 -0800198// Case: We have a DEX file, but no OAT file for it.
Richard Uhler95abd042015-03-24 09:51:28 -0700199// Expect: The status is kDex2OatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800200TEST_F(OatFileAssistantTest, DexNoOat) {
201 std::string dex_location = GetScratchDir() + "/DexNoOat.jar";
202 Copy(GetDexSrc1(), dex_location);
203
Richard Uhlerd1472a22016-04-15 15:18:56 -0700204 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800205
Richard Uhler7225a8d2016-11-22 10:12:03 +0000206 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100207 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000208 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100209 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000210 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000211 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeedProfile));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000212 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000213 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800214
215 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000216 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
217 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700218 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Calin Juravle5f9a8012018-02-12 20:27:46 -0800219
220 VerifyOptimizationStatus(dex_location, "run-from-apk", "unknown");
Richard Uhler66d874d2015-01-15 09:37:19 -0800221}
222
223// Case: We have no DEX file and no OAT file.
Richard Uhler9b994ea2015-06-24 08:44:19 -0700224// Expect: Status is kNoDexOptNeeded. Loading should fail, but not crash.
Richard Uhler66d874d2015-01-15 09:37:19 -0800225TEST_F(OatFileAssistantTest, NoDexNoOat) {
226 std::string dex_location = GetScratchDir() + "/NoDexNoOat.jar";
227
Richard Uhlerd1472a22016-04-15 15:18:56 -0700228 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800229
Andreas Gampe29d38e72016-03-23 15:31:51 +0000230 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
231 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700232 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
233
Richard Uhler9b994ea2015-06-24 08:44:19 -0700234 // Trying to get the best oat file should fail, but not crash.
Richard Uhler66d874d2015-01-15 09:37:19 -0800235 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
236 EXPECT_EQ(nullptr, oat_file.get());
237}
238
Vladimir Markoe0669322018-09-03 15:44:54 +0100239// Case: We have a DEX file and an ODEX file, but no OAT file.
240// Expect: The status is kNoDexOptNeeded.
Calin Juravle357c66d2017-05-04 01:57:17 +0000241TEST_F(OatFileAssistantTest, OdexUpToDate) {
242 std::string dex_location = GetScratchDir() + "/OdexUpToDate.jar";
243 std::string odex_location = GetOdexDir() + "/OdexUpToDate.odex";
244 Copy(GetDexSrc1(), dex_location);
Vladimir Markoe0669322018-09-03 15:44:54 +0100245 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed, "install");
Calin Juravle357c66d2017-05-04 01:57:17 +0000246
Vladimir Markof3d88a82018-12-21 16:38:47 +0000247 // Force the use of oat location by making the dex parent not writable.
248 OatFileAssistant oat_file_assistant(
249 dex_location.c_str(), kRuntimeISA, /*load_executable=*/ false);
Calin Juravle357c66d2017-05-04 01:57:17 +0000250
251 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
Vladimir Markof3d88a82018-12-21 16:38:47 +0000252 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Calin Juravle357c66d2017-05-04 01:57:17 +0000253 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
Vladimir Markof3d88a82018-12-21 16:38:47 +0000254 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken));
Calin Juravle357c66d2017-05-04 01:57:17 +0000255 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
Vladimir Markof3d88a82018-12-21 16:38:47 +0000256 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract));
Calin Juravle357c66d2017-05-04 01:57:17 +0000257 EXPECT_EQ(-OatFileAssistant::kDex2OatForFilter,
Vladimir Markof3d88a82018-12-21 16:38:47 +0000258 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
259
260 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
261 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus());
262 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
263 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
264
265 VerifyOptimizationStatus(dex_location, CompilerFilter::kSpeed, "install");
266}
267
268// Case: We have an ODEX file compiled against partial boot image.
269// Expect: The status is kNoDexOptNeeded.
270TEST_F(OatFileAssistantTest, OdexUpToDatePartialBootImage) {
271 std::string dex_location = GetScratchDir() + "/OdexUpToDate.jar";
272 std::string odex_location = GetOdexDir() + "/OdexUpToDate.odex";
273 Copy(GetDexSrc1(), dex_location);
274 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed, "install");
275
276 // Insert an extra dex file to the boot class path.
277 InsertNewBootClasspathEntry();
278
279 // Force the use of oat location by making the dex parent not writable.
280 OatFileAssistant oat_file_assistant(
281 dex_location.c_str(), kRuntimeISA, /*load_executable=*/ false);
282
283 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
284 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
285 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
286 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken));
287 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
288 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract));
289 EXPECT_EQ(-OatFileAssistant::kDex2OatForFilter,
290 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
Calin Juravle357c66d2017-05-04 01:57:17 +0000291
292 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
293 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus());
294 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
295 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Calin Juravle5f9a8012018-02-12 20:27:46 -0800296
297 VerifyOptimizationStatus(dex_location, CompilerFilter::kSpeed, "install");
Calin Juravle357c66d2017-05-04 01:57:17 +0000298}
299
300// Case: We have a DEX file and a PIC ODEX file, but no OAT file. We load the dex
301// file via a symlink.
Vladimir Markoe0669322018-09-03 15:44:54 +0100302// Expect: The status is kNoDexOptNeeded.
Calin Juravle357c66d2017-05-04 01:57:17 +0000303TEST_F(OatFileAssistantTest, OdexUpToDateSymLink) {
304 std::string scratch_dir = GetScratchDir();
305 std::string dex_location = GetScratchDir() + "/OdexUpToDate.jar";
306 std::string odex_location = GetOdexDir() + "/OdexUpToDate.odex";
307
308 Copy(GetDexSrc1(), dex_location);
Vladimir Markoe0669322018-09-03 15:44:54 +0100309 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Calin Juravle357c66d2017-05-04 01:57:17 +0000310
311 // Now replace the dex location with a symlink.
312 std::string link = scratch_dir + "/link";
313 ASSERT_EQ(0, symlink(scratch_dir.c_str(), link.c_str()));
314 dex_location = link + "/OdexUpToDate.jar";
315
316 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
317
318 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
319 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
320 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
321 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken));
322 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
323 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract));
324 EXPECT_EQ(-OatFileAssistant::kDex2OatForFilter,
325 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
326
327 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
328 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus());
329 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
330 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
331}
332
Richard Uhler66d874d2015-01-15 09:37:19 -0800333// Case: We have a DEX file and up-to-date OAT file for it.
Richard Uhler95abd042015-03-24 09:51:28 -0700334// Expect: The status is kNoDexOptNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800335TEST_F(OatFileAssistantTest, OatUpToDate) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000336 if (IsExecutedAsRoot()) {
337 // We cannot simulate non writable locations when executed as root: b/38000545.
338 LOG(ERROR) << "Test skipped because it's running as root";
339 return;
340 }
341
Richard Uhler66d874d2015-01-15 09:37:19 -0800342 std::string dex_location = GetScratchDir() + "/OatUpToDate.jar";
343 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000344 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800345
Vladimir Markof3d88a82018-12-21 16:38:47 +0000346 // Force the use of oat location by making the dex parent not writable.
Calin Juravle357c66d2017-05-04 01:57:17 +0000347 ScopedNonWritable scoped_non_writable(dex_location);
348 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
349
350 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
351
352 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
353 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
354 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
355 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken));
356 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
357 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract));
358 EXPECT_EQ(OatFileAssistant::kDex2OatForFilter,
359 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
360
361 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
362 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
363 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus());
364 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Calin Juravle5f9a8012018-02-12 20:27:46 -0800365
366 VerifyOptimizationStatus(dex_location, CompilerFilter::kSpeed, "unknown");
Calin Juravle357c66d2017-05-04 01:57:17 +0000367}
368
Vladimir Markoe0669322018-09-03 15:44:54 +0100369// Case: Passing valid file descriptors of updated odex/vdex files along with the dex file.
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700370// Expect: The status is kNoDexOptNeeded.
371TEST_F(OatFileAssistantTest, GetDexOptNeededWithFd) {
372 std::string dex_location = GetScratchDir() + "/OatUpToDate.jar";
373 std::string odex_location = GetScratchDir() + "/OatUpToDate.odex";
374 std::string vdex_location = GetScratchDir() + "/OatUpToDate.vdex";
375
376 Copy(GetDexSrc1(), dex_location);
377 GenerateOatForTest(dex_location.c_str(),
378 odex_location.c_str(),
379 CompilerFilter::kSpeed,
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700380 /* with_alternate_image= */ false);
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700381
Andreas Gampedfcd82c2018-10-16 20:22:37 -0700382 android::base::unique_fd odex_fd(open(odex_location.c_str(), O_RDONLY | O_CLOEXEC));
383 android::base::unique_fd vdex_fd(open(vdex_location.c_str(), O_RDONLY | O_CLOEXEC));
384 android::base::unique_fd zip_fd(open(dex_location.c_str(), O_RDONLY | O_CLOEXEC));
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700385
386 OatFileAssistant oat_file_assistant(dex_location.c_str(),
387 kRuntimeISA,
388 false,
Nicolas Geoffray29742602017-12-14 10:09:03 +0000389 false,
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700390 vdex_fd.get(),
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700391 odex_fd.get(),
392 zip_fd.get());
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700393 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
394 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
395 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
396 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken));
397 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
398 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract));
399 EXPECT_EQ(-OatFileAssistant::kDex2OatForFilter,
400 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
401
402 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
403 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus());
404 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
405 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
406}
407
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700408// Case: Passing invalid odex fd and valid vdex and zip fds.
409// Expect: The status should be kDex2OatForBootImage.
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700410TEST_F(OatFileAssistantTest, GetDexOptNeededWithInvalidOdexFd) {
411 std::string dex_location = GetScratchDir() + "/OatUpToDate.jar";
412 std::string odex_location = GetScratchDir() + "/OatUpToDate.odex";
413 std::string vdex_location = GetScratchDir() + "/OatUpToDate.vdex";
414
415 Copy(GetDexSrc1(), dex_location);
416 GenerateOatForTest(dex_location.c_str(),
417 odex_location.c_str(),
418 CompilerFilter::kSpeed,
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700419 /* with_alternate_image= */ false);
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700420
Andreas Gampedfcd82c2018-10-16 20:22:37 -0700421 android::base::unique_fd vdex_fd(open(vdex_location.c_str(), O_RDONLY | O_CLOEXEC));
422 android::base::unique_fd zip_fd(open(dex_location.c_str(), O_RDONLY | O_CLOEXEC));
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700423
424 OatFileAssistant oat_file_assistant(dex_location.c_str(),
425 kRuntimeISA,
426 false,
Nicolas Geoffray29742602017-12-14 10:09:03 +0000427 false,
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700428 vdex_fd.get(),
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700429 /* oat_fd= */ -1,
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700430 zip_fd.get());
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700431 EXPECT_EQ(-OatFileAssistant::kDex2OatForBootImage,
432 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700433 EXPECT_EQ(-OatFileAssistant::kDex2OatForBootImage,
434 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
435
436 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700437 EXPECT_EQ(OatFileAssistant::kOatBootImageOutOfDate, oat_file_assistant.OdexFileStatus());
438 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700439 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700440}
441
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700442// Case: Passing invalid vdex fd and valid odex and zip fds.
443// Expect: The status should be kDex2OatFromScratch.
444TEST_F(OatFileAssistantTest, GetDexOptNeededWithInvalidVdexFd) {
445 std::string dex_location = GetScratchDir() + "/OatUpToDate.jar";
446 std::string odex_location = GetScratchDir() + "/OatUpToDate.odex";
447
448 Copy(GetDexSrc1(), dex_location);
449 GenerateOatForTest(dex_location.c_str(),
450 odex_location.c_str(),
451 CompilerFilter::kSpeed,
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700452 /* with_alternate_image= */ false);
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700453
Andreas Gampedfcd82c2018-10-16 20:22:37 -0700454 android::base::unique_fd odex_fd(open(odex_location.c_str(), O_RDONLY | O_CLOEXEC));
455 android::base::unique_fd zip_fd(open(dex_location.c_str(), O_RDONLY | O_CLOEXEC));
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700456
457 OatFileAssistant oat_file_assistant(dex_location.c_str(),
458 kRuntimeISA,
459 false,
Nicolas Geoffray29742602017-12-14 10:09:03 +0000460 false,
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700461 /* vdex_fd= */ -1,
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700462 odex_fd.get(),
463 zip_fd.get());
464
465 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
466 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
467 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
468 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
469 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
470 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
471}
472
473// Case: Passing invalid vdex and odex fd with valid zip fd.
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700474// Expect: The status is kDex2oatFromScratch.
475TEST_F(OatFileAssistantTest, GetDexOptNeededWithInvalidOdexVdexFd) {
476 std::string dex_location = GetScratchDir() + "/OatUpToDate.jar";
477
478 Copy(GetDexSrc1(), dex_location);
479
Andreas Gampedfcd82c2018-10-16 20:22:37 -0700480 android::base::unique_fd zip_fd(open(dex_location.c_str(), O_RDONLY | O_CLOEXEC));
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700481 OatFileAssistant oat_file_assistant(dex_location.c_str(),
482 kRuntimeISA,
483 false,
Nicolas Geoffray29742602017-12-14 10:09:03 +0000484 false,
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700485 /* vdex_fd= */ -1,
486 /* oat_fd= */ -1,
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700487 zip_fd);
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700488 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
489 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
490 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
491 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
492}
493
Richard Uhler2f27abd2017-01-31 14:02:34 +0000494// Case: We have a DEX file and up-to-date (ODEX) VDEX file for it, but no
495// ODEX file.
496TEST_F(OatFileAssistantTest, VdexUpToDateNoOdex) {
Richard Uhler2f27abd2017-01-31 14:02:34 +0000497 std::string dex_location = GetScratchDir() + "/VdexUpToDateNoOdex.jar";
Calin Juravle357c66d2017-05-04 01:57:17 +0000498 std::string odex_location = GetOdexDir() + "/VdexUpToDateNoOdex.oat";
Richard Uhler2f27abd2017-01-31 14:02:34 +0000499
Richard Uhler9a37efc2016-08-05 16:32:55 -0700500 Copy(GetDexSrc1(), dex_location);
501
Richard Uhler2f27abd2017-01-31 14:02:34 +0000502 // Generating and deleting the oat file should have the side effect of
503 // creating an up-to-date vdex file.
Calin Juravle357c66d2017-05-04 01:57:17 +0000504 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
505 ASSERT_EQ(0, unlink(odex_location.c_str()));
Richard Uhler2f27abd2017-01-31 14:02:34 +0000506
Calin Juravle357c66d2017-05-04 01:57:17 +0000507 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000508
509 // Even though the vdex file is up to date, because we don't have the oat
510 // file, we can't know that the vdex depends on the boot image and is up to
511 // date with respect to the boot image. Instead we must assume the vdex file
512 // depends on the boot image and is out of date with respect to the boot
513 // image.
514 EXPECT_EQ(-OatFileAssistant::kDex2OatForBootImage,
515 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
516
517 // Make sure we don't crash in this case when we dump the status. We don't
518 // care what the actual dumped value is.
519 oat_file_assistant.GetStatusDump();
Calin Juravle5f9a8012018-02-12 20:27:46 -0800520
521 VerifyOptimizationStatus(dex_location, "run-from-apk", "unknown");
Richard Uhler2f27abd2017-01-31 14:02:34 +0000522}
523
524// Case: We have a DEX file and empty VDEX and ODEX files.
525TEST_F(OatFileAssistantTest, EmptyVdexOdex) {
526 std::string dex_location = GetScratchDir() + "/EmptyVdexOdex.jar";
527 std::string odex_location = GetOdexDir() + "/EmptyVdexOdex.oat";
528 std::string vdex_location = GetOdexDir() + "/EmptyVdexOdex.vdex";
529
530 Copy(GetDexSrc1(), dex_location);
Richard Uhler5cd59292017-02-01 12:54:23 +0000531 ScratchFile vdex_file(vdex_location.c_str());
532 ScratchFile odex_file(odex_location.c_str());
Richard Uhler2f27abd2017-01-31 14:02:34 +0000533
534 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
535 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
536 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
537}
538
539// Case: We have a DEX file and up-to-date (OAT) VDEX file for it, but no OAT
540// file.
541TEST_F(OatFileAssistantTest, VdexUpToDateNoOat) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000542 if (IsExecutedAsRoot()) {
543 // We cannot simulate non writable locations when executed as root: b/38000545.
544 LOG(ERROR) << "Test skipped because it's running as root";
545 return;
546 }
Richard Uhler2f27abd2017-01-31 14:02:34 +0000547
548 std::string dex_location = GetScratchDir() + "/VdexUpToDateNoOat.jar";
549 std::string oat_location;
550 std::string error_msg;
551 ASSERT_TRUE(OatFileAssistant::DexLocationToOatFilename(
552 dex_location, kRuntimeISA, &oat_location, &error_msg)) << error_msg;
553
554 Copy(GetDexSrc1(), dex_location);
555 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
556 ASSERT_EQ(0, unlink(oat_location.c_str()));
557
Calin Juravle357c66d2017-05-04 01:57:17 +0000558 ScopedNonWritable scoped_non_writable(dex_location);
559 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
Richard Uhler9a37efc2016-08-05 16:32:55 -0700560 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
561
Richard Uhler2f27abd2017-01-31 14:02:34 +0000562 // Even though the vdex file is up to date, because we don't have the oat
563 // file, we can't know that the vdex depends on the boot image and is up to
564 // date with respect to the boot image. Instead we must assume the vdex file
565 // depends on the boot image and is out of date with respect to the boot
566 // image.
567 EXPECT_EQ(OatFileAssistant::kDex2OatForBootImage,
Richard Uhler9a37efc2016-08-05 16:32:55 -0700568 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler9a37efc2016-08-05 16:32:55 -0700569}
570
Andreas Gampe29d38e72016-03-23 15:31:51 +0000571// Case: We have a DEX file and speed-profile OAT file for it.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700572// Expect: The status is kNoDexOptNeeded if the profile hasn't changed, but
573// kDex2Oat if the profile has changed.
Andreas Gampe29d38e72016-03-23 15:31:51 +0000574TEST_F(OatFileAssistantTest, ProfileOatUpToDate) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000575 if (IsExecutedAsRoot()) {
576 // We cannot simulate non writable locations when executed as root: b/38000545.
577 LOG(ERROR) << "Test skipped because it's running as root";
578 return;
579 }
580
Andreas Gampe29d38e72016-03-23 15:31:51 +0000581 std::string dex_location = GetScratchDir() + "/ProfileOatUpToDate.jar";
582 Copy(GetDexSrc1(), dex_location);
583 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeedProfile);
584
Calin Juravle357c66d2017-05-04 01:57:17 +0000585 ScopedNonWritable scoped_non_writable(dex_location);
586 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
587
Richard Uhlerd1472a22016-04-15 15:18:56 -0700588 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000589
590 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700591 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeedProfile, false));
Andreas Gampe29d38e72016-03-23 15:31:51 +0000592 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100593 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken, false));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000594 EXPECT_EQ(OatFileAssistant::kDex2OatForFilter,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700595 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeedProfile, true));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000596 EXPECT_EQ(OatFileAssistant::kDex2OatForFilter,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100597 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken, true));
Andreas Gampe29d38e72016-03-23 15:31:51 +0000598
599 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000600 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
Andreas Gampe29d38e72016-03-23 15:31:51 +0000601 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus());
602 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
603}
604
Richard Uhler66d874d2015-01-15 09:37:19 -0800605// Case: We have a MultiDEX file and up-to-date OAT file for it.
Richard Uhler95abd042015-03-24 09:51:28 -0700606// Expect: The status is kNoDexOptNeeded and we load all dex files.
Richard Uhler66d874d2015-01-15 09:37:19 -0800607TEST_F(OatFileAssistantTest, MultiDexOatUpToDate) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000608 if (IsExecutedAsRoot()) {
609 // We cannot simulate non writable locations when executed as root: b/38000545.
610 LOG(ERROR) << "Test skipped because it's running as root";
611 return;
612 }
613
Richard Uhler66d874d2015-01-15 09:37:19 -0800614 std::string dex_location = GetScratchDir() + "/MultiDexOatUpToDate.jar";
615 Copy(GetMultiDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000616 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800617
Calin Juravle357c66d2017-05-04 01:57:17 +0000618 ScopedNonWritable scoped_non_writable(dex_location);
619 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
620
Richard Uhlerd1472a22016-04-15 15:18:56 -0700621 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000622 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700623 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed, false));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700624 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler95abd042015-03-24 09:51:28 -0700625
626 // Verify we can load both dex files.
Richard Uhlere5fed032015-03-18 08:21:11 -0700627 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
Richard Uhler66d874d2015-01-15 09:37:19 -0800628 ASSERT_TRUE(oat_file.get() != nullptr);
629 EXPECT_TRUE(oat_file->IsExecutable());
630 std::vector<std::unique_ptr<const DexFile>> dex_files;
Richard Uhlere5fed032015-03-18 08:21:11 -0700631 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
632 EXPECT_EQ(2u, dex_files.size());
633}
634
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000635// Case: We have a MultiDEX file where the non-main multdex entry is out of date.
Richard Uhler67ff7d12015-05-14 13:21:13 -0700636// Expect: The status is kDex2OatNeeded.
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000637TEST_F(OatFileAssistantTest, MultiDexNonMainOutOfDate) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000638 if (IsExecutedAsRoot()) {
639 // We cannot simulate non writable locations when executed as root: b/38000545.
640 LOG(ERROR) << "Test skipped because it's running as root";
641 return;
642 }
643
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000644 std::string dex_location = GetScratchDir() + "/MultiDexNonMainOutOfDate.jar";
Richard Uhler67ff7d12015-05-14 13:21:13 -0700645
646 // Compile code for GetMultiDexSrc1.
647 Copy(GetMultiDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000648 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler67ff7d12015-05-14 13:21:13 -0700649
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000650 // Now overwrite the dex file with GetMultiDexSrc2 so the non-main checksum
Richard Uhler67ff7d12015-05-14 13:21:13 -0700651 // is out of date.
652 Copy(GetMultiDexSrc2(), dex_location);
653
Calin Juravle357c66d2017-05-04 01:57:17 +0000654 ScopedNonWritable scoped_non_writable(dex_location);
655 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
656
Richard Uhlerd1472a22016-04-15 15:18:56 -0700657 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler7225a8d2016-11-22 10:12:03 +0000658 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700659 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed, false));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700660 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler67ff7d12015-05-14 13:21:13 -0700661}
662
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000663// Case: We have a stripped MultiDEX file where the non-main multidex entry is
664// out of date with respect to the odex file.
665TEST_F(OatFileAssistantTest, StrippedMultiDexNonMainOutOfDate) {
666 std::string dex_location = GetScratchDir() + "/StrippedMultiDexNonMainOutOfDate.jar";
667 std::string odex_location = GetOdexDir() + "/StrippedMultiDexNonMainOutOfDate.odex";
668
669 // Compile the oat from GetMultiDexSrc1.
670 Copy(GetMultiDexSrc1(), dex_location);
671 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
672
673 // Compile the odex from GetMultiDexSrc2, which has a different non-main
674 // dex checksum.
675 Copy(GetMultiDexSrc2(), dex_location);
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100676 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kQuicken);
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000677
678 // Strip the dex file.
679 Copy(GetStrippedDexSrc1(), dex_location);
680
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700681 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, /*load_executable=*/false);
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000682
683 // Because the dex file is stripped, the odex file is considered the source
684 // of truth for the dex checksums. The oat file should be considered
685 // unusable.
686 std::unique_ptr<OatFile> best_file = oat_file_assistant.GetBestOatFile();
687 ASSERT_TRUE(best_file.get() != nullptr);
688 EXPECT_EQ(best_file->GetLocation(), odex_location);
689 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
690 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus());
691 EXPECT_EQ(OatFileAssistant::kOatDexOutOfDate, oat_file_assistant.OatFileStatus());
692}
693
Richard Uhler03bc6592016-11-22 09:42:04 +0000694// Case: We have a DEX file and an OAT file out of date with respect to the
695// dex checksum.
696TEST_F(OatFileAssistantTest, OatDexOutOfDate) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000697 if (IsExecutedAsRoot()) {
698 // We cannot simulate non writable locations when executed as root: b/38000545.
699 LOG(ERROR) << "Test skipped because it's running as root";
700 return;
701 }
702
Richard Uhler03bc6592016-11-22 09:42:04 +0000703 std::string dex_location = GetScratchDir() + "/OatDexOutOfDate.jar";
Richard Uhler66d874d2015-01-15 09:37:19 -0800704
705 // We create a dex, generate an oat for it, then overwrite the dex with a
706 // different dex to make the oat out of date.
707 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000708 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800709 Copy(GetDexSrc2(), dex_location);
710
Calin Juravle357c66d2017-05-04 01:57:17 +0000711 ScopedNonWritable scoped_non_writable(dex_location);
712 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
713
Richard Uhlerd1472a22016-04-15 15:18:56 -0700714 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler7225a8d2016-11-22 10:12:03 +0000715 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100716 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000717 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000718 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800719
720 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000721 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
722 EXPECT_EQ(OatFileAssistant::kOatDexOutOfDate, oat_file_assistant.OatFileStatus());
723 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
724}
725
Richard Uhler2f27abd2017-01-31 14:02:34 +0000726// Case: We have a DEX file and an (ODEX) VDEX file out of date with respect
727// to the dex checksum, but no ODEX file.
728TEST_F(OatFileAssistantTest, VdexDexOutOfDate) {
Richard Uhler2f27abd2017-01-31 14:02:34 +0000729 std::string dex_location = GetScratchDir() + "/VdexDexOutOfDate.jar";
Calin Juravle357c66d2017-05-04 01:57:17 +0000730 std::string odex_location = GetOdexDir() + "/VdexDexOutOfDate.oat";
Richard Uhler2f27abd2017-01-31 14:02:34 +0000731
732 Copy(GetDexSrc1(), dex_location);
Calin Juravle357c66d2017-05-04 01:57:17 +0000733 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
734 ASSERT_EQ(0, unlink(odex_location.c_str()));
Richard Uhler2f27abd2017-01-31 14:02:34 +0000735 Copy(GetDexSrc2(), dex_location);
736
Calin Juravle357c66d2017-05-04 01:57:17 +0000737 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000738
739 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
740 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
741}
742
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000743// Case: We have a MultiDEX (ODEX) VDEX file where the non-main multidex entry
744// is out of date and there is no corresponding ODEX file.
745TEST_F(OatFileAssistantTest, VdexMultiDexNonMainOutOfDate) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000746 std::string dex_location = GetScratchDir() + "/VdexMultiDexNonMainOutOfDate.jar";
Calin Juravle357c66d2017-05-04 01:57:17 +0000747 std::string odex_location = GetOdexDir() + "/VdexMultiDexNonMainOutOfDate.odex";
Richard Uhler2f27abd2017-01-31 14:02:34 +0000748
749 Copy(GetMultiDexSrc1(), dex_location);
Calin Juravle357c66d2017-05-04 01:57:17 +0000750 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
751 ASSERT_EQ(0, unlink(odex_location.c_str()));
Richard Uhler2f27abd2017-01-31 14:02:34 +0000752 Copy(GetMultiDexSrc2(), dex_location);
753
Calin Juravle357c66d2017-05-04 01:57:17 +0000754 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000755
756 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
757 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
758}
759
Richard Uhler03bc6592016-11-22 09:42:04 +0000760// Case: We have a DEX file and an OAT file out of date with respect to the
761// boot image.
762TEST_F(OatFileAssistantTest, OatImageOutOfDate) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000763 if (IsExecutedAsRoot()) {
764 // We cannot simulate non writable locations when executed as root: b/38000545.
765 LOG(ERROR) << "Test skipped because it's running as root";
766 return;
767 }
768
Richard Uhler03bc6592016-11-22 09:42:04 +0000769 std::string dex_location = GetScratchDir() + "/OatImageOutOfDate.jar";
770
771 Copy(GetDexSrc1(), dex_location);
772 GenerateOatForTest(dex_location.c_str(),
773 CompilerFilter::kSpeed,
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700774 /* with_alternate_image= */ true);
Richard Uhler03bc6592016-11-22 09:42:04 +0000775
Calin Juravle357c66d2017-05-04 01:57:17 +0000776 ScopedNonWritable scoped_non_writable(dex_location);
777 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
778
Richard Uhler03bc6592016-11-22 09:42:04 +0000779 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler7225a8d2016-11-22 10:12:03 +0000780 EXPECT_EQ(OatFileAssistant::kDex2OatForBootImage,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100781 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000782 EXPECT_EQ(OatFileAssistant::kDex2OatForBootImage,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100783 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000784 EXPECT_EQ(OatFileAssistant::kDex2OatForBootImage,
Richard Uhler03bc6592016-11-22 09:42:04 +0000785 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
786
787 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
788 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
789 EXPECT_EQ(OatFileAssistant::kOatBootImageOutOfDate, oat_file_assistant.OatFileStatus());
790 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
791}
792
793// Case: We have a DEX file and a verify-at-runtime OAT file out of date with
794// respect to the boot image.
795// It shouldn't matter that the OAT file is out of date, because it is
796// verify-at-runtime.
797TEST_F(OatFileAssistantTest, OatVerifyAtRuntimeImageOutOfDate) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000798 if (IsExecutedAsRoot()) {
799 // We cannot simulate non writable locations when executed as root: b/38000545.
800 LOG(ERROR) << "Test skipped because it's running as root";
801 return;
802 }
803
Richard Uhler03bc6592016-11-22 09:42:04 +0000804 std::string dex_location = GetScratchDir() + "/OatVerifyAtRuntimeImageOutOfDate.jar";
805
806 Copy(GetDexSrc1(), dex_location);
807 GenerateOatForTest(dex_location.c_str(),
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100808 CompilerFilter::kExtract,
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700809 /* with_alternate_image= */ true);
Richard Uhler03bc6592016-11-22 09:42:04 +0000810
Calin Juravle357c66d2017-05-04 01:57:17 +0000811 ScopedNonWritable scoped_non_writable(dex_location);
812 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
813
Richard Uhler03bc6592016-11-22 09:42:04 +0000814 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
815 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100816 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000817 EXPECT_EQ(OatFileAssistant::kDex2OatForFilter,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100818 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken));
Richard Uhler03bc6592016-11-22 09:42:04 +0000819
820 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
821 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
822 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700823 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800824}
825
826// Case: We have a DEX file and an ODEX file, but no OAT file.
Richard Uhler66d874d2015-01-15 09:37:19 -0800827TEST_F(OatFileAssistantTest, DexOdexNoOat) {
828 std::string dex_location = GetScratchDir() + "/DexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700829 std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800830
831 // Create the dex and odex files
832 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000833 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800834
835 // Verify the status.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700836 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800837
Andreas Gampe29d38e72016-03-23 15:31:51 +0000838 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100839 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract));
Vladimir Markoe0669322018-09-03 15:44:54 +0100840 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000841 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800842
843 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Vladimir Markoe0669322018-09-03 15:44:54 +0100844 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus());
Richard Uhler03bc6592016-11-22 09:42:04 +0000845 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700846 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler5f946da2015-07-17 12:28:32 -0700847
848 // We should still be able to get the non-executable odex file to run from.
849 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
850 ASSERT_TRUE(oat_file.get() != nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800851}
852
Richard Uhler5923b522016-12-08 09:48:01 +0000853// Case: We have a stripped DEX file and a PIC ODEX file, but no OAT file.
Richard Uhler66d874d2015-01-15 09:37:19 -0800854TEST_F(OatFileAssistantTest, StrippedDexOdexNoOat) {
855 std::string dex_location = GetScratchDir() + "/StrippedDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700856 std::string odex_location = GetOdexDir() + "/StrippedDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800857
858 // Create the dex and odex files
859 Copy(GetDexSrc1(), dex_location);
Vladimir Markoe0669322018-09-03 15:44:54 +0100860 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800861
862 // Strip the dex file
863 Copy(GetStrippedDexSrc1(), dex_location);
864
865 // Verify the status.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700866 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800867
Richard Uhler5923b522016-12-08 09:48:01 +0000868 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000869 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800870
871 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler5923b522016-12-08 09:48:01 +0000872 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus());
Richard Uhler03bc6592016-11-22 09:42:04 +0000873 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700874 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800875
Richard Uhler66d874d2015-01-15 09:37:19 -0800876 // Verify we can load the dex files from it.
877 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
878 ASSERT_TRUE(oat_file.get() != nullptr);
879 EXPECT_TRUE(oat_file->IsExecutable());
880 std::vector<std::unique_ptr<const DexFile>> dex_files;
881 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
882 EXPECT_EQ(1u, dex_files.size());
883}
884
Richard Uhler5923b522016-12-08 09:48:01 +0000885// 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 -0800886TEST_F(OatFileAssistantTest, StrippedDexOdexOat) {
887 std::string dex_location = GetScratchDir() + "/StrippedDexOdexOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700888 std::string odex_location = GetOdexDir() + "/StrippedDexOdexOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800889
890 // Create the oat file from a different dex file so it looks out of date.
891 Copy(GetDexSrc2(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000892 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800893
894 // Create the odex file
895 Copy(GetDexSrc1(), dex_location);
Vladimir Markoe0669322018-09-03 15:44:54 +0100896 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800897
898 // Strip the dex file.
899 Copy(GetStrippedDexSrc1(), dex_location);
900
901 // Verify the status.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700902 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800903
Andreas Gampe29d38e72016-03-23 15:31:51 +0000904 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100905 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract));
Andreas Gampe29d38e72016-03-23 15:31:51 +0000906 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
907 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Nicolas Geoffray08e9eed2017-04-25 17:36:51 +0100908 EXPECT_EQ(-OatFileAssistant::kDex2OatForFilter, // Compiling from the .vdex file
Andreas Gampe29d38e72016-03-23 15:31:51 +0000909 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
Richard Uhler66d874d2015-01-15 09:37:19 -0800910
911 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler5923b522016-12-08 09:48:01 +0000912 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus());
913 EXPECT_EQ(OatFileAssistant::kOatDexOutOfDate, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700914 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800915
916 // Verify we can load the dex files from it.
917 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
918 ASSERT_TRUE(oat_file.get() != nullptr);
919 EXPECT_TRUE(oat_file->IsExecutable());
920 std::vector<std::unique_ptr<const DexFile>> dex_files;
921 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
922 EXPECT_EQ(1u, dex_files.size());
923}
924
Richard Uhler9b994ea2015-06-24 08:44:19 -0700925// Case: We have a stripped (or resource-only) DEX file, no ODEX file and no
926// OAT file. Expect: The status is kNoDexOptNeeded.
927TEST_F(OatFileAssistantTest, ResourceOnlyDex) {
928 std::string dex_location = GetScratchDir() + "/ResourceOnlyDex.jar";
929
930 Copy(GetStrippedDexSrc1(), dex_location);
931
932 // Verify the status.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700933 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler9b994ea2015-06-24 08:44:19 -0700934
Andreas Gampe29d38e72016-03-23 15:31:51 +0000935 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
936 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
937 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100938 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract));
Andreas Gampe29d38e72016-03-23 15:31:51 +0000939 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100940 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700941
942 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000943 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
944 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700945 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
946
Andreas Gampe29d38e72016-03-23 15:31:51 +0000947 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
948 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700949
950 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000951 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
952 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700953 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
954}
955
Vladimir Markoe0669322018-09-03 15:44:54 +0100956// Case: We have a DEX file, an ODEX file and an OAT file.
957// Expect: It shouldn't crash. We should load the odex file executable.
Richard Uhler66d874d2015-01-15 09:37:19 -0800958TEST_F(OatFileAssistantTest, OdexOatOverlap) {
959 std::string dex_location = GetScratchDir() + "/OdexOatOverlap.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700960 std::string odex_location = GetOdexDir() + "/OdexOatOverlap.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800961
Calin Juravle357c66d2017-05-04 01:57:17 +0000962 // Create the dex, the odex and the oat files.
Richard Uhler66d874d2015-01-15 09:37:19 -0800963 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000964 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Vladimir Markoe0669322018-09-03 15:44:54 +0100965 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800966
967 // Verify things don't go bad.
Calin Juravle357c66d2017-05-04 01:57:17 +0000968 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800969
Vladimir Markoe0669322018-09-03 15:44:54 +0100970 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
971 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800972
973 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Vladimir Markoe0669322018-09-03 15:44:54 +0100974 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus());
975 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700976 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800977
Richard Uhler66d874d2015-01-15 09:37:19 -0800978 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
979 ASSERT_TRUE(oat_file.get() != nullptr);
Richard Uhlerf16d5722015-05-11 09:32:47 -0700980
Vladimir Markoe0669322018-09-03 15:44:54 +0100981 EXPECT_TRUE(oat_file->IsExecutable());
Richard Uhler66d874d2015-01-15 09:37:19 -0800982 std::vector<std::unique_ptr<const DexFile>> dex_files;
983 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
984 EXPECT_EQ(1u, dex_files.size());
985}
986
Andreas Gampe29d38e72016-03-23 15:31:51 +0000987// Case: We have a DEX file and a VerifyAtRuntime ODEX file, but no OAT file.
988// Expect: The status is kNoDexOptNeeded, because VerifyAtRuntime contains no code.
989TEST_F(OatFileAssistantTest, DexVerifyAtRuntimeOdexNoOat) {
990 std::string dex_location = GetScratchDir() + "/DexVerifyAtRuntimeOdexNoOat.jar";
991 std::string odex_location = GetOdexDir() + "/DexVerifyAtRuntimeOdexNoOat.odex";
David Brazdilce4b0ba2016-01-28 15:05:49 +0000992
993 // Create the dex and odex files
994 Copy(GetDexSrc1(), dex_location);
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100995 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kExtract);
David Brazdilce4b0ba2016-01-28 15:05:49 +0000996
997 // Verify the status.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700998 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
David Brazdilce4b0ba2016-01-28 15:05:49 +0000999
Andreas Gampe29d38e72016-03-23 15:31:51 +00001000 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Nicolas Geoffray49cda062017-04-21 13:08:25 +01001001 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract));
Richard Uhler7225a8d2016-11-22 10:12:03 +00001002 EXPECT_EQ(-OatFileAssistant::kDex2OatForFilter,
Andreas Gampe29d38e72016-03-23 15:31:51 +00001003 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
David Brazdilce4b0ba2016-01-28 15:05:49 +00001004
1005 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler3e580bc2016-11-08 16:23:07 +00001006 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus());
Richard Uhler03bc6592016-11-22 09:42:04 +00001007 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
David Brazdilce4b0ba2016-01-28 15:05:49 +00001008 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
1009}
1010
Richard Uhler66d874d2015-01-15 09:37:19 -08001011// Case: We have a DEX file and up-to-date OAT file for it.
1012// Expect: We should load an executable dex file.
1013TEST_F(OatFileAssistantTest, LoadOatUpToDate) {
Calin Juravle357c66d2017-05-04 01:57:17 +00001014 if (IsExecutedAsRoot()) {
1015 // We cannot simulate non writable locations when executed as root: b/38000545.
1016 LOG(ERROR) << "Test skipped because it's running as root";
1017 return;
1018 }
1019
Richard Uhler66d874d2015-01-15 09:37:19 -08001020 std::string dex_location = GetScratchDir() + "/LoadOatUpToDate.jar";
1021
1022 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +00001023 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -08001024
Calin Juravle357c66d2017-05-04 01:57:17 +00001025 ScopedNonWritable scoped_non_writable(dex_location);
1026 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
1027
Richard Uhler66d874d2015-01-15 09:37:19 -08001028 // Load the oat using an oat file assistant.
Richard Uhlerd1472a22016-04-15 15:18:56 -07001029 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Andreas Gampe29d38e72016-03-23 15:31:51 +00001030
1031 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1032 ASSERT_TRUE(oat_file.get() != nullptr);
1033 EXPECT_TRUE(oat_file->IsExecutable());
1034 std::vector<std::unique_ptr<const DexFile>> dex_files;
1035 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
1036 EXPECT_EQ(1u, dex_files.size());
1037}
1038
Nicolas Geoffray49cda062017-04-21 13:08:25 +01001039// Case: We have a DEX file and up-to-date quicken OAT file for it.
Andreas Gampe29d38e72016-03-23 15:31:51 +00001040// Expect: We should still load the oat file as executable.
1041TEST_F(OatFileAssistantTest, LoadExecInterpretOnlyOatUpToDate) {
Calin Juravle357c66d2017-05-04 01:57:17 +00001042 if (IsExecutedAsRoot()) {
1043 // We cannot simulate non writable locations when executed as root: b/38000545.
1044 LOG(ERROR) << "Test skipped because it's running as root";
1045 return;
1046 }
1047
Andreas Gampe29d38e72016-03-23 15:31:51 +00001048 std::string dex_location = GetScratchDir() + "/LoadExecInterpretOnlyOatUpToDate.jar";
1049
1050 Copy(GetDexSrc1(), dex_location);
Nicolas Geoffray49cda062017-04-21 13:08:25 +01001051 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kQuicken);
Andreas Gampe29d38e72016-03-23 15:31:51 +00001052
Calin Juravle357c66d2017-05-04 01:57:17 +00001053 ScopedNonWritable scoped_non_writable(dex_location);
1054 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
1055
Andreas Gampe29d38e72016-03-23 15:31:51 +00001056 // Load the oat using an oat file assistant.
Richard Uhlerd1472a22016-04-15 15:18:56 -07001057 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001058
1059 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1060 ASSERT_TRUE(oat_file.get() != nullptr);
1061 EXPECT_TRUE(oat_file->IsExecutable());
1062 std::vector<std::unique_ptr<const DexFile>> dex_files;
1063 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
1064 EXPECT_EQ(1u, dex_files.size());
1065}
1066
1067// Case: We have a DEX file and up-to-date OAT file for it.
1068// Expect: Loading non-executable should load the oat non-executable.
1069TEST_F(OatFileAssistantTest, LoadNoExecOatUpToDate) {
Calin Juravle357c66d2017-05-04 01:57:17 +00001070 if (IsExecutedAsRoot()) {
1071 // We cannot simulate non writable locations when executed as root: b/38000545.
1072 LOG(ERROR) << "Test skipped because it's running as root";
1073 return;
1074 }
1075
Richard Uhler66d874d2015-01-15 09:37:19 -08001076 std::string dex_location = GetScratchDir() + "/LoadNoExecOatUpToDate.jar";
1077
1078 Copy(GetDexSrc1(), dex_location);
Calin Juravle357c66d2017-05-04 01:57:17 +00001079
1080 ScopedNonWritable scoped_non_writable(dex_location);
1081 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
1082
Andreas Gampe29d38e72016-03-23 15:31:51 +00001083 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -08001084
1085 // Load the oat using an oat file assistant.
Richard Uhlerd1472a22016-04-15 15:18:56 -07001086 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -08001087
1088 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1089 ASSERT_TRUE(oat_file.get() != nullptr);
1090 EXPECT_FALSE(oat_file->IsExecutable());
1091 std::vector<std::unique_ptr<const DexFile>> dex_files;
1092 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
1093 EXPECT_EQ(1u, dex_files.size());
1094}
1095
Richard Uhler66d874d2015-01-15 09:37:19 -08001096// Turn an absolute path into a path relative to the current working
1097// directory.
Andreas Gampeca620d72016-11-08 08:09:33 -08001098static std::string MakePathRelative(const std::string& target) {
Richard Uhler66d874d2015-01-15 09:37:19 -08001099 char buf[MAXPATHLEN];
1100 std::string cwd = getcwd(buf, MAXPATHLEN);
1101
1102 // Split the target and cwd paths into components.
1103 std::vector<std::string> target_path;
1104 std::vector<std::string> cwd_path;
1105 Split(target, '/', &target_path);
1106 Split(cwd, '/', &cwd_path);
1107
1108 // Reverse the path components, so we can use pop_back().
1109 std::reverse(target_path.begin(), target_path.end());
1110 std::reverse(cwd_path.begin(), cwd_path.end());
1111
1112 // Drop the common prefix of the paths. Because we reversed the path
1113 // components, this becomes the common suffix of target_path and cwd_path.
1114 while (!target_path.empty() && !cwd_path.empty()
1115 && target_path.back() == cwd_path.back()) {
1116 target_path.pop_back();
1117 cwd_path.pop_back();
1118 }
1119
1120 // For each element of the remaining cwd_path, add '..' to the beginning
1121 // of the target path. Because we reversed the path components, we add to
1122 // the end of target_path.
1123 for (unsigned int i = 0; i < cwd_path.size(); i++) {
1124 target_path.push_back("..");
1125 }
1126
1127 // Reverse again to get the right path order, and join to get the result.
1128 std::reverse(target_path.begin(), target_path.end());
Andreas Gampe9186ced2016-12-12 14:28:21 -08001129 return android::base::Join(target_path, '/');
Richard Uhler66d874d2015-01-15 09:37:19 -08001130}
1131
1132// Case: Non-absolute path to Dex location.
1133// Expect: Not sure, but it shouldn't crash.
1134TEST_F(OatFileAssistantTest, NonAbsoluteDexLocation) {
1135 std::string abs_dex_location = GetScratchDir() + "/NonAbsoluteDexLocation.jar";
1136 Copy(GetDexSrc1(), abs_dex_location);
1137
1138 std::string dex_location = MakePathRelative(abs_dex_location);
Richard Uhlerd1472a22016-04-15 15:18:56 -07001139 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001140
1141 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler7225a8d2016-11-22 10:12:03 +00001142 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Andreas Gampe29d38e72016-03-23 15:31:51 +00001143 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler03bc6592016-11-22 09:42:04 +00001144 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
1145 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler66d874d2015-01-15 09:37:19 -08001146}
1147
1148// Case: Very short, non-existent Dex location.
Richard Uhler9b994ea2015-06-24 08:44:19 -07001149// Expect: kNoDexOptNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -08001150TEST_F(OatFileAssistantTest, ShortDexLocation) {
1151 std::string dex_location = "/xx";
1152
Richard Uhlerd1472a22016-04-15 15:18:56 -07001153 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001154
1155 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Andreas Gampe29d38e72016-03-23 15:31:51 +00001156 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
1157 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler03bc6592016-11-22 09:42:04 +00001158 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
1159 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -07001160 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -08001161}
1162
1163// Case: Non-standard extension for dex file.
Richard Uhler95abd042015-03-24 09:51:28 -07001164// Expect: The status is kDex2OatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -08001165TEST_F(OatFileAssistantTest, LongDexExtension) {
1166 std::string dex_location = GetScratchDir() + "/LongDexExtension.jarx";
1167 Copy(GetDexSrc1(), dex_location);
1168
Richard Uhlerd1472a22016-04-15 15:18:56 -07001169 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -08001170
Richard Uhler7225a8d2016-11-22 10:12:03 +00001171 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Andreas Gampe29d38e72016-03-23 15:31:51 +00001172 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -08001173
1174 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +00001175 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
1176 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler66d874d2015-01-15 09:37:19 -08001177}
1178
Nicolas Geoffray3d8a78a2018-08-29 21:10:16 +00001179
Richard Uhler66d874d2015-01-15 09:37:19 -08001180// A task to generate a dex location. Used by the RaceToGenerate test.
1181class RaceGenerateTask : public Task {
1182 public:
Nicolas Geoffray1e76d7a2018-09-03 13:23:34 +01001183 RaceGenerateTask(const std::string& dex_location,
1184 const std::string& oat_location,
1185 Mutex* lock)
1186 : dex_location_(dex_location),
1187 oat_location_(oat_location),
1188 lock_(lock),
1189 loaded_oat_file_(nullptr)
Richard Uhler66d874d2015-01-15 09:37:19 -08001190 {}
1191
Andreas Gampefa6a1b02018-09-07 08:11:55 -07001192 void Run(Thread* self ATTRIBUTE_UNUSED) override {
Richard Uhler66d874d2015-01-15 09:37:19 -08001193 // Load the dex files, and save a pointer to the loaded oat file, so that
1194 // we can verify only one oat file was loaded for the dex location.
Richard Uhler66d874d2015-01-15 09:37:19 -08001195 std::vector<std::unique_ptr<const DexFile>> dex_files;
1196 std::vector<std::string> error_msgs;
Mathieu Chartiere58991b2015-10-13 07:59:34 -07001197 const OatFile* oat_file = nullptr;
Nicolas Geoffray3d8a78a2018-08-29 21:10:16 +00001198 {
Nicolas Geoffray1e76d7a2018-09-03 13:23:34 +01001199 MutexLock mu(Thread::Current(), *lock_);
Nicolas Geoffray3d8a78a2018-08-29 21:10:16 +00001200 // Create the oat file.
1201 std::vector<std::string> args;
1202 args.push_back("--dex-file=" + dex_location_);
1203 args.push_back("--oat-file=" + oat_location_);
1204 std::string error_msg;
Nicolas Geoffray1e76d7a2018-09-03 13:23:34 +01001205 ASSERT_TRUE(DexoptTest::Dex2Oat(args, &error_msg)) << error_msg;
Nicolas Geoffray3d8a78a2018-08-29 21:10:16 +00001206 }
1207
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001208 dex_files = Runtime::Current()->GetOatFileManager().OpenDexFilesFromOat(
1209 dex_location_.c_str(),
Jeff Hao0cb17282017-07-12 14:51:49 -07001210 Runtime::Current()->GetSystemClassLoader(),
Andreas Gampe98ea9d92018-10-19 14:06:15 -07001211 /*dex_elements=*/nullptr,
Mathieu Chartiere58991b2015-10-13 07:59:34 -07001212 &oat_file,
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001213 &error_msgs);
Andreas Gampe9186ced2016-12-12 14:28:21 -08001214 CHECK(!dex_files.empty()) << android::base::Join(error_msgs, '\n');
Nicolas Geoffray3d8a78a2018-08-29 21:10:16 +00001215 if (dex_files[0]->GetOatDexFile() != nullptr) {
1216 loaded_oat_file_ = dex_files[0]->GetOatDexFile()->GetOatFile();
1217 }
Mathieu Chartiere58991b2015-10-13 07:59:34 -07001218 CHECK_EQ(loaded_oat_file_, oat_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001219 }
1220
1221 const OatFile* GetLoadedOatFile() const {
1222 return loaded_oat_file_;
1223 }
1224
1225 private:
1226 std::string dex_location_;
1227 std::string oat_location_;
Nicolas Geoffray1e76d7a2018-09-03 13:23:34 +01001228 Mutex* lock_;
Richard Uhler66d874d2015-01-15 09:37:19 -08001229 const OatFile* loaded_oat_file_;
1230};
1231
Nicolas Geoffray3d8a78a2018-08-29 21:10:16 +00001232// Test the case where dex2oat invocations race with multiple processes trying to
1233// load the oat file.
Richard Uhler66d874d2015-01-15 09:37:19 -08001234TEST_F(OatFileAssistantTest, RaceToGenerate) {
1235 std::string dex_location = GetScratchDir() + "/RaceToGenerate.jar";
Richard Uhler63434112015-03-16 14:32:16 -07001236 std::string oat_location = GetOdexDir() + "/RaceToGenerate.oat";
Richard Uhler66d874d2015-01-15 09:37:19 -08001237
Jeff Hao0cb17282017-07-12 14:51:49 -07001238 // Start the runtime to initialize the system's class loader.
1239 Thread::Current()->TransitionFromSuspendedToRunnable();
1240 runtime_->Start();
1241
Richard Uhler66d874d2015-01-15 09:37:19 -08001242 // We use the lib core dex file, because it's large, and hopefully should
1243 // take a while to generate.
Narayan Kamathd1ef4362015-11-12 11:49:06 +00001244 Copy(GetLibCoreDexFileNames()[0], dex_location);
Richard Uhler66d874d2015-01-15 09:37:19 -08001245
Nicolas Geoffray3d8a78a2018-08-29 21:10:16 +00001246 const size_t kNumThreads = 32;
Richard Uhler66d874d2015-01-15 09:37:19 -08001247 Thread* self = Thread::Current();
1248 ThreadPool thread_pool("Oat file assistant test thread pool", kNumThreads);
1249 std::vector<std::unique_ptr<RaceGenerateTask>> tasks;
Nicolas Geoffray1e76d7a2018-09-03 13:23:34 +01001250 Mutex lock("RaceToGenerate");
Nicolas Geoffray3d8a78a2018-08-29 21:10:16 +00001251 for (size_t i = 0; i < kNumThreads; i++) {
Nicolas Geoffray1e76d7a2018-09-03 13:23:34 +01001252 std::unique_ptr<RaceGenerateTask> task(new RaceGenerateTask(dex_location, oat_location, &lock));
Richard Uhler66d874d2015-01-15 09:37:19 -08001253 thread_pool.AddTask(self, task.get());
1254 tasks.push_back(std::move(task));
1255 }
1256 thread_pool.StartWorkers(self);
Andreas Gampe98ea9d92018-10-19 14:06:15 -07001257 thread_pool.Wait(self, /* do_work= */ true, /* may_hold_locks= */ false);
Richard Uhler66d874d2015-01-15 09:37:19 -08001258
Nicolas Geoffray3d8a78a2018-08-29 21:10:16 +00001259 // Verify that tasks which got an oat file got a unique one.
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001260 std::set<const OatFile*> oat_files;
Richard Uhler66d874d2015-01-15 09:37:19 -08001261 for (auto& task : tasks) {
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001262 const OatFile* oat_file = task->GetLoadedOatFile();
Nicolas Geoffray3d8a78a2018-08-29 21:10:16 +00001263 if (oat_file != nullptr) {
1264 EXPECT_TRUE(oat_files.find(oat_file) == oat_files.end());
1265 oat_files.insert(oat_file);
1266 }
Richard Uhler66d874d2015-01-15 09:37:19 -08001267 }
1268}
1269
Nicolas Geoffray3d8a78a2018-08-29 21:10:16 +00001270// Case: We have a DEX file and an ODEX file, and no OAT file,
Vladimir Markoe0669322018-09-03 15:44:54 +01001271// Expect: We should load the odex file executable.
Nicolas Geoffray3d8a78a2018-08-29 21:10:16 +00001272TEST_F(DexoptTest, LoadDexOdexNoOat) {
Richard Uhler66d874d2015-01-15 09:37:19 -08001273 std::string dex_location = GetScratchDir() + "/LoadDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -07001274 std::string odex_location = GetOdexDir() + "/LoadDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -08001275
1276 // Create the dex and odex files
1277 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +00001278 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -08001279
1280 // Load the oat using an executable oat file assistant.
Richard Uhlerd1472a22016-04-15 15:18:56 -07001281 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001282
1283 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1284 ASSERT_TRUE(oat_file.get() != nullptr);
Vladimir Markoe0669322018-09-03 15:44:54 +01001285 EXPECT_TRUE(oat_file->IsExecutable());
Richard Uhler66d874d2015-01-15 09:37:19 -08001286 std::vector<std::unique_ptr<const DexFile>> dex_files;
1287 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
1288 EXPECT_EQ(1u, dex_files.size());
1289}
1290
Nicolas Geoffray3d8a78a2018-08-29 21:10:16 +00001291// Case: We have a MultiDEX file and an ODEX file, and no OAT file.
Vladimir Markoe0669322018-09-03 15:44:54 +01001292// Expect: We should load the odex file executable.
Nicolas Geoffray3d8a78a2018-08-29 21:10:16 +00001293TEST_F(DexoptTest, LoadMultiDexOdexNoOat) {
Richard Uhler66d874d2015-01-15 09:37:19 -08001294 std::string dex_location = GetScratchDir() + "/LoadMultiDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -07001295 std::string odex_location = GetOdexDir() + "/LoadMultiDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -08001296
1297 // Create the dex and odex files
1298 Copy(GetMultiDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +00001299 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -08001300
1301 // Load the oat using an executable oat file assistant.
Richard Uhlerd1472a22016-04-15 15:18:56 -07001302 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001303
1304 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1305 ASSERT_TRUE(oat_file.get() != nullptr);
Vladimir Markoe0669322018-09-03 15:44:54 +01001306 EXPECT_TRUE(oat_file->IsExecutable());
Richard Uhler66d874d2015-01-15 09:37:19 -08001307 std::vector<std::unique_ptr<const DexFile>> dex_files;
1308 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
1309 EXPECT_EQ(2u, dex_files.size());
1310}
1311
Richard Uhlerb81881d2016-04-19 13:08:04 -07001312TEST(OatFileAssistantUtilsTest, DexLocationToOdexFilename) {
Richard Uhler66d874d2015-01-15 09:37:19 -08001313 std::string error_msg;
1314 std::string odex_file;
1315
Richard Uhlerb81881d2016-04-19 13:08:04 -07001316 EXPECT_TRUE(OatFileAssistant::DexLocationToOdexFilename(
Vladimir Marko33bff252017-11-01 14:35:42 +00001317 "/foo/bar/baz.jar", InstructionSet::kArm, &odex_file, &error_msg)) << error_msg;
Richard Uhler63434112015-03-16 14:32:16 -07001318 EXPECT_EQ("/foo/bar/oat/arm/baz.odex", odex_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001319
Richard Uhlerb81881d2016-04-19 13:08:04 -07001320 EXPECT_TRUE(OatFileAssistant::DexLocationToOdexFilename(
Vladimir Marko33bff252017-11-01 14:35:42 +00001321 "/foo/bar/baz.funnyext", InstructionSet::kArm, &odex_file, &error_msg)) << error_msg;
Richard Uhler63434112015-03-16 14:32:16 -07001322 EXPECT_EQ("/foo/bar/oat/arm/baz.odex", odex_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001323
Richard Uhlerb81881d2016-04-19 13:08:04 -07001324 EXPECT_FALSE(OatFileAssistant::DexLocationToOdexFilename(
Vladimir Marko33bff252017-11-01 14:35:42 +00001325 "nopath.jar", InstructionSet::kArm, &odex_file, &error_msg));
Richard Uhlerb81881d2016-04-19 13:08:04 -07001326 EXPECT_FALSE(OatFileAssistant::DexLocationToOdexFilename(
Vladimir Marko33bff252017-11-01 14:35:42 +00001327 "/foo/bar/baz_noext", InstructionSet::kArm, &odex_file, &error_msg));
Richard Uhler66d874d2015-01-15 09:37:19 -08001328}
1329
Richard Uhler23cedd22015-04-08 13:17:29 -07001330// Verify the dexopt status values from dalvik.system.DexFile
1331// match the OatFileAssistant::DexOptStatus values.
1332TEST_F(OatFileAssistantTest, DexOptStatusValues) {
Richard Uhler7225a8d2016-11-22 10:12:03 +00001333 std::pair<OatFileAssistant::DexOptNeeded, const char*> mapping[] = {
1334 {OatFileAssistant::kNoDexOptNeeded, "NO_DEXOPT_NEEDED"},
1335 {OatFileAssistant::kDex2OatFromScratch, "DEX2OAT_FROM_SCRATCH"},
1336 {OatFileAssistant::kDex2OatForBootImage, "DEX2OAT_FOR_BOOT_IMAGE"},
1337 {OatFileAssistant::kDex2OatForFilter, "DEX2OAT_FOR_FILTER"},
Richard Uhler7225a8d2016-11-22 10:12:03 +00001338 };
1339
Richard Uhler23cedd22015-04-08 13:17:29 -07001340 ScopedObjectAccess soa(Thread::Current());
1341 StackHandleScope<1> hs(soa.Self());
1342 ClassLinker* linker = Runtime::Current()->GetClassLinker();
1343 Handle<mirror::Class> dexfile(
1344 hs.NewHandle(linker->FindSystemClass(soa.Self(), "Ldalvik/system/DexFile;")));
Andreas Gampefa4333d2017-02-14 11:10:34 -08001345 ASSERT_FALSE(dexfile == nullptr);
Richard Uhler23cedd22015-04-08 13:17:29 -07001346 linker->EnsureInitialized(soa.Self(), dexfile, true, true);
1347
Richard Uhler7225a8d2016-11-22 10:12:03 +00001348 for (std::pair<OatFileAssistant::DexOptNeeded, const char*> field : mapping) {
1349 ArtField* art_field = mirror::Class::FindStaticField(
Vladimir Marko19a4d372016-12-08 14:41:46 +00001350 soa.Self(), dexfile.Get(), field.second, "I");
Richard Uhler7225a8d2016-11-22 10:12:03 +00001351 ASSERT_FALSE(art_field == nullptr);
1352 EXPECT_EQ(art_field->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
1353 EXPECT_EQ(field.first, art_field->GetInt(dexfile.Get()));
1354 }
Richard Uhler23cedd22015-04-08 13:17:29 -07001355}
Richard Uhler66d874d2015-01-15 09:37:19 -08001356
Calin Juravle44e5efa2017-09-12 00:54:26 -07001357TEST_F(OatFileAssistantTest, GetDexOptNeededWithOutOfDateContext) {
1358 std::string dex_location = GetScratchDir() + "/TestDex.jar";
1359 std::string context_location = GetScratchDir() + "/ContextDex.jar";
1360 Copy(GetDexSrc1(), dex_location);
1361 Copy(GetDexSrc2(), context_location);
1362
1363 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
1364
Calin Juravle44e5efa2017-09-12 00:54:26 -07001365 std::string error_msg;
1366 std::string context_str = "PCL[" + context_location + "]";
1367 std::unique_ptr<ClassLoaderContext> context = ClassLoaderContext::Create(context_str);
1368 ASSERT_TRUE(context != nullptr);
1369 ASSERT_TRUE(context->OpenDexFiles(kRuntimeISA, ""));
1370
Calin Juravle44e5efa2017-09-12 00:54:26 -07001371 // Update the context by overriding the jar file.
1372 Copy(GetMultiDexSrc2(), context_location);
1373 std::unique_ptr<ClassLoaderContext> updated_context = ClassLoaderContext::Create(context_str);
1374 ASSERT_TRUE(updated_context != nullptr);
1375 // DexOptNeeded should advise compilation from scratch.
1376 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
1377 oat_file_assistant.GetDexOptNeeded(
Nicolas Geoffray3d8a78a2018-08-29 21:10:16 +00001378 CompilerFilter::kDefaultCompilerFilter,
Andreas Gampe98ea9d92018-10-19 14:06:15 -07001379 /* profile_changed= */ false,
1380 /* downgrade= */ false,
Nicolas Geoffray3d8a78a2018-08-29 21:10:16 +00001381 updated_context.get()));
Nicolas Geoffray29742602017-12-14 10:09:03 +00001382}
1383
Richard Uhler66d874d2015-01-15 09:37:19 -08001384// TODO: More Tests:
1385// * Test class linker falls back to unquickened dex for DexNoOat
1386// * Test class linker falls back to unquickened dex for MultiDexNoOat
Richard Uhler66d874d2015-01-15 09:37:19 -08001387// * Test using secondary isa
Richard Uhler66d874d2015-01-15 09:37:19 -08001388// * Test for status of oat while oat is being generated (how?)
1389// * Test case where 32 and 64 bit boot class paths differ,
1390// and we ask IsInBootClassPath for a class in exactly one of the 32 or
1391// 64 bit boot class paths.
1392// * Test unexpected scenarios (?):
1393// - Dex is stripped, don't have odex.
1394// - Oat file corrupted after status check, before reload unexecutable
1395// because it's unrelocated and no dex2oat
Richard Uhler66d874d2015-01-15 09:37:19 -08001396} // namespace art