blob: 92347b10436908d6919faa232ed1e6aa8d852b8d [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"
Vladimir Markod3d00c02019-11-07 15:09:07 +000037#include "oat.h"
Calin Juravle27e0d1f2017-07-26 00:16:07 -070038#include "oat_file.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070039#include "oat_file_manager.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070040#include "scoped_thread_state_change-inl.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070041#include "thread-current-inl.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080042
43namespace art {
44
Calin Juravle5f9a8012018-02-12 20:27:46 -080045class OatFileAssistantTest : public DexoptTest {
46 public:
47 void VerifyOptimizationStatus(const std::string& file,
48 const std::string& expected_filter,
49 const std::string& expected_reason) {
50 std::string compilation_filter;
51 std::string compilation_reason;
52 OatFileAssistant::GetOptimizationStatus(
53 file, kRuntimeISA, &compilation_filter, &compilation_reason);
54
55 ASSERT_EQ(expected_filter, compilation_filter);
56 ASSERT_EQ(expected_reason, compilation_reason);
57 }
58
59 void VerifyOptimizationStatus(const std::string& file,
60 CompilerFilter::Filter expected_filter,
61 const std::string& expected_reason) {
62 VerifyOptimizationStatus(
63 file, CompilerFilter::NameOfFilter(expected_filter), expected_reason);
64 }
Calin Juravle0a5cad32020-02-14 20:29:26 +000065
Vladimir Markof3d88a82018-12-21 16:38:47 +000066 void InsertNewBootClasspathEntry() {
67 std::string extra_dex_filename = GetMultiDexSrc1();
68 Runtime* runtime = Runtime::Current();
69 runtime->boot_class_path_.push_back(extra_dex_filename);
70 if (!runtime->boot_class_path_locations_.empty()) {
71 runtime->boot_class_path_locations_.push_back(extra_dex_filename);
72 }
73 }
Calin Juravle0a5cad32020-02-14 20:29:26 +000074
75 int GetDexOptNeeded(OatFileAssistant* assistant,
76 CompilerFilter::Filter compiler_filter,
77 bool profile_changed) {
78 std::vector<int> context_fds;
79 return GetDexOptNeeded(assistant,
80 compiler_filter,
81 ClassLoaderContext::Default(),
82 context_fds,
83 profile_changed,
84 /*downgrade=*/ false);
85 }
86
87 int GetDexOptNeeded(
88 OatFileAssistant* assistant,
89 CompilerFilter::Filter compiler_filter,
90 const std::unique_ptr<ClassLoaderContext>& context = ClassLoaderContext::Default(),
91 const std::vector<int>& context_fds = std::vector<int>(),
92 bool profile_changed = false,
93 bool downgrade = false) {
94 return assistant->GetDexOptNeeded(
95 compiler_filter,
96 context.get(),
97 context_fds,
98 profile_changed,
99 downgrade);
100 }
Calin Juravle5f9a8012018-02-12 20:27:46 -0800101};
Richard Uhler66d874d2015-01-15 09:37:19 -0800102
Calin Juravle357c66d2017-05-04 01:57:17 +0000103class ScopedNonWritable {
104 public:
105 explicit ScopedNonWritable(const std::string& dex_location) {
106 is_valid_ = false;
107 size_t pos = dex_location.rfind('/');
108 if (pos != std::string::npos) {
109 is_valid_ = true;
110 dex_parent_ = dex_location.substr(0, pos);
111 if (chmod(dex_parent_.c_str(), 0555) != 0) {
112 PLOG(ERROR) << "Could not change permissions on " << dex_parent_;
113 }
114 }
115 }
116
117 bool IsSuccessful() { return is_valid_ && (access(dex_parent_.c_str(), W_OK) != 0); }
118
119 ~ScopedNonWritable() {
120 if (is_valid_) {
121 if (chmod(dex_parent_.c_str(), 0777) != 0) {
122 PLOG(ERROR) << "Could not restore permissions on " << dex_parent_;
123 }
124 }
125 }
126
127 private:
128 std::string dex_parent_;
129 bool is_valid_;
130};
131
132static bool IsExecutedAsRoot() {
133 return geteuid() == 0;
134}
Calin Juravle36eb3132017-01-13 16:32:38 -0800135
Nicolas Geoffray3d8a78a2018-08-29 21:10:16 +0000136// Case: We have a MultiDEX file and up-to-date ODEX file for it with relative
137// encoded dex locations.
138// Expect: The oat file status is kNoDexOptNeeded.
139TEST_F(OatFileAssistantTest, RelativeEncodedDexLocation) {
140 std::string dex_location = GetScratchDir() + "/RelativeEncodedDexLocation.jar";
141 std::string odex_location = GetOdexDir() + "/RelativeEncodedDexLocation.odex";
142
143 // Create the dex file
144 Copy(GetMultiDexSrc1(), dex_location);
145
146 // Create the oat file with relative encoded dex location.
147 std::vector<std::string> args = {
148 "--dex-file=" + dex_location,
149 "--dex-location=" + std::string("RelativeEncodedDexLocation.jar"),
150 "--oat-file=" + odex_location,
151 "--compiler-filter=speed"
152 };
153
154 std::string error_msg;
155 ASSERT_TRUE(Dex2Oat(args, &error_msg)) << error_msg;
156
157 // Verify we can load both dex files.
158 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
159
160 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
161 ASSERT_TRUE(oat_file.get() != nullptr);
162 EXPECT_TRUE(oat_file->IsExecutable());
163 std::vector<std::unique_ptr<const DexFile>> dex_files;
164 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
165 EXPECT_EQ(2u, dex_files.size());
166}
167
168TEST_F(OatFileAssistantTest, MakeUpToDateWithContext) {
169 std::string dex_location = GetScratchDir() + "/TestDex.jar";
170 std::string odex_location = GetOdexDir() + "/TestDex.odex";
171 std::string context_location = GetScratchDir() + "/ContextDex.jar";
172 Copy(GetDexSrc1(), dex_location);
173 Copy(GetDexSrc2(), context_location);
174
175 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
176
177 std::string context_str = "PCL[" + context_location + "]";
178 std::unique_ptr<ClassLoaderContext> context = ClassLoaderContext::Create(context_str);
179 ASSERT_TRUE(context != nullptr);
180 ASSERT_TRUE(context->OpenDexFiles(kRuntimeISA, ""));
181
182 std::string error_msg;
183 std::vector<std::string> args;
184 args.push_back("--dex-file=" + dex_location);
185 args.push_back("--oat-file=" + odex_location);
186 args.push_back("--class-loader-context=" + context_str);
187 ASSERT_TRUE(Dex2Oat(args, &error_msg)) << error_msg;
188
189 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
190 EXPECT_NE(nullptr, oat_file.get());
191 EXPECT_EQ(context->EncodeContextForOatFile(""),
192 oat_file->GetOatHeader().GetStoreValueByKey(OatHeader::kClassPathKey));
193}
194
195TEST_F(OatFileAssistantTest, GetDexOptNeededWithUpToDateContextRelative) {
196 std::string dex_location = GetScratchDir() + "/TestDex.jar";
197 std::string odex_location = GetOdexDir() + "/TestDex.odex";
198 std::string context_location = GetScratchDir() + "/ContextDex.jar";
199 Copy(GetDexSrc1(), dex_location);
200 Copy(GetDexSrc2(), context_location);
201
202 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
203
204 std::string context_str = "PCL[" + context_location + "]";
205 std::unique_ptr<ClassLoaderContext> context = ClassLoaderContext::Create(context_str);
206 ASSERT_TRUE(context != nullptr);
207 ASSERT_TRUE(context->OpenDexFiles(kRuntimeISA, ""));
208
209 std::string error_msg;
210 std::vector<std::string> args;
211 args.push_back("--dex-file=" + dex_location);
212 args.push_back("--oat-file=" + odex_location);
213 args.push_back("--class-loader-context=" + context_str);
214 ASSERT_TRUE(Dex2Oat(args, &error_msg)) << error_msg;
215
216 // A relative context simulates a dependent split context.
217 std::unique_ptr<ClassLoaderContext> relative_context =
218 ClassLoaderContext::Create("PCL[ContextDex.jar]");
219 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
Calin Juravle0a5cad32020-02-14 20:29:26 +0000220 GetDexOptNeeded(&oat_file_assistant,
Nicolas Geoffray3d8a78a2018-08-29 21:10:16 +0000221 CompilerFilter::kDefaultCompilerFilter,
Calin Juravle0a5cad32020-02-14 20:29:26 +0000222 relative_context));
Nicolas Geoffray3d8a78a2018-08-29 21:10:16 +0000223}
224
Richard Uhler66d874d2015-01-15 09:37:19 -0800225// Case: We have a DEX file, but no OAT file for it.
Richard Uhler95abd042015-03-24 09:51:28 -0700226// Expect: The status is kDex2OatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800227TEST_F(OatFileAssistantTest, DexNoOat) {
228 std::string dex_location = GetScratchDir() + "/DexNoOat.jar";
229 Copy(GetDexSrc1(), dex_location);
230
Richard Uhlerd1472a22016-04-15 15:18:56 -0700231 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800232
Richard Uhler7225a8d2016-11-22 10:12:03 +0000233 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Calin Juravle0a5cad32020-02-14 20:29:26 +0000234 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kExtract));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000235 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Calin Juravle0a5cad32020-02-14 20:29:26 +0000236 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kQuicken));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000237 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Calin Juravle0a5cad32020-02-14 20:29:26 +0000238 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kSpeedProfile));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000239 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Calin Juravle0a5cad32020-02-14 20:29:26 +0000240 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800241
242 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000243 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
244 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Nicolas Geoffray90a18cf2020-06-25 15:12:59 +0100245 EXPECT_TRUE(oat_file_assistant.HasDexFiles());
Calin Juravle5f9a8012018-02-12 20:27:46 -0800246
247 VerifyOptimizationStatus(dex_location, "run-from-apk", "unknown");
Richard Uhler66d874d2015-01-15 09:37:19 -0800248}
249
250// Case: We have no DEX file and no OAT file.
Richard Uhler9b994ea2015-06-24 08:44:19 -0700251// Expect: Status is kNoDexOptNeeded. Loading should fail, but not crash.
Richard Uhler66d874d2015-01-15 09:37:19 -0800252TEST_F(OatFileAssistantTest, NoDexNoOat) {
253 std::string dex_location = GetScratchDir() + "/NoDexNoOat.jar";
254
Richard Uhlerd1472a22016-04-15 15:18:56 -0700255 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800256
Andreas Gampe29d38e72016-03-23 15:31:51 +0000257 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Calin Juravle0a5cad32020-02-14 20:29:26 +0000258 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kSpeed));
Nicolas Geoffray90a18cf2020-06-25 15:12:59 +0100259 EXPECT_FALSE(oat_file_assistant.HasDexFiles());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700260
Richard Uhler9b994ea2015-06-24 08:44:19 -0700261 // Trying to get the best oat file should fail, but not crash.
Richard Uhler66d874d2015-01-15 09:37:19 -0800262 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
263 EXPECT_EQ(nullptr, oat_file.get());
264}
265
Vladimir Markoe0669322018-09-03 15:44:54 +0100266// Case: We have a DEX file and an ODEX file, but no OAT file.
267// Expect: The status is kNoDexOptNeeded.
Calin Juravle357c66d2017-05-04 01:57:17 +0000268TEST_F(OatFileAssistantTest, OdexUpToDate) {
269 std::string dex_location = GetScratchDir() + "/OdexUpToDate.jar";
270 std::string odex_location = GetOdexDir() + "/OdexUpToDate.odex";
271 Copy(GetDexSrc1(), dex_location);
Vladimir Markoe0669322018-09-03 15:44:54 +0100272 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed, "install");
Calin Juravle357c66d2017-05-04 01:57:17 +0000273
Vladimir Markof3d88a82018-12-21 16:38:47 +0000274 // Force the use of oat location by making the dex parent not writable.
275 OatFileAssistant oat_file_assistant(
276 dex_location.c_str(), kRuntimeISA, /*load_executable=*/ false);
Calin Juravle357c66d2017-05-04 01:57:17 +0000277
278 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
Calin Juravle0a5cad32020-02-14 20:29:26 +0000279 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kSpeed));
Calin Juravle357c66d2017-05-04 01:57:17 +0000280 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
Calin Juravle0a5cad32020-02-14 20:29:26 +0000281 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kQuicken));
Calin Juravle357c66d2017-05-04 01:57:17 +0000282 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
Calin Juravle0a5cad32020-02-14 20:29:26 +0000283 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kExtract));
Calin Juravle357c66d2017-05-04 01:57:17 +0000284 EXPECT_EQ(-OatFileAssistant::kDex2OatForFilter,
Calin Juravle0a5cad32020-02-14 20:29:26 +0000285 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kEverything));
Vladimir Markof3d88a82018-12-21 16:38:47 +0000286
287 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
288 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus());
289 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Nicolas Geoffray90a18cf2020-06-25 15:12:59 +0100290 EXPECT_TRUE(oat_file_assistant.HasDexFiles());
Vladimir Markof3d88a82018-12-21 16:38:47 +0000291
292 VerifyOptimizationStatus(dex_location, CompilerFilter::kSpeed, "install");
293}
294
295// Case: We have an ODEX file compiled against partial boot image.
296// Expect: The status is kNoDexOptNeeded.
297TEST_F(OatFileAssistantTest, OdexUpToDatePartialBootImage) {
298 std::string dex_location = GetScratchDir() + "/OdexUpToDate.jar";
299 std::string odex_location = GetOdexDir() + "/OdexUpToDate.odex";
300 Copy(GetDexSrc1(), dex_location);
301 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed, "install");
302
303 // Insert an extra dex file to the boot class path.
304 InsertNewBootClasspathEntry();
305
306 // Force the use of oat location by making the dex parent not writable.
307 OatFileAssistant oat_file_assistant(
308 dex_location.c_str(), kRuntimeISA, /*load_executable=*/ false);
309
310 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
Calin Juravle0a5cad32020-02-14 20:29:26 +0000311 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kSpeed));
Vladimir Markof3d88a82018-12-21 16:38:47 +0000312 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
Calin Juravle0a5cad32020-02-14 20:29:26 +0000313 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kQuicken));
Vladimir Markof3d88a82018-12-21 16:38:47 +0000314 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
Calin Juravle0a5cad32020-02-14 20:29:26 +0000315 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kExtract));
Vladimir Markof3d88a82018-12-21 16:38:47 +0000316 EXPECT_EQ(-OatFileAssistant::kDex2OatForFilter,
Calin Juravle0a5cad32020-02-14 20:29:26 +0000317 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kEverything));
Calin Juravle357c66d2017-05-04 01:57:17 +0000318
319 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
320 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus());
321 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Nicolas Geoffray90a18cf2020-06-25 15:12:59 +0100322 EXPECT_TRUE(oat_file_assistant.HasDexFiles());
Calin Juravle5f9a8012018-02-12 20:27:46 -0800323
324 VerifyOptimizationStatus(dex_location, CompilerFilter::kSpeed, "install");
Calin Juravle357c66d2017-05-04 01:57:17 +0000325}
326
327// Case: We have a DEX file and a PIC ODEX file, but no OAT file. We load the dex
328// file via a symlink.
Vladimir Markoe0669322018-09-03 15:44:54 +0100329// Expect: The status is kNoDexOptNeeded.
Calin Juravle357c66d2017-05-04 01:57:17 +0000330TEST_F(OatFileAssistantTest, OdexUpToDateSymLink) {
331 std::string scratch_dir = GetScratchDir();
332 std::string dex_location = GetScratchDir() + "/OdexUpToDate.jar";
333 std::string odex_location = GetOdexDir() + "/OdexUpToDate.odex";
334
335 Copy(GetDexSrc1(), dex_location);
Vladimir Markoe0669322018-09-03 15:44:54 +0100336 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Calin Juravle357c66d2017-05-04 01:57:17 +0000337
338 // Now replace the dex location with a symlink.
339 std::string link = scratch_dir + "/link";
340 ASSERT_EQ(0, symlink(scratch_dir.c_str(), link.c_str()));
341 dex_location = link + "/OdexUpToDate.jar";
342
343 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
344
345 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
Calin Juravle0a5cad32020-02-14 20:29:26 +0000346 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kSpeed));
Calin Juravle357c66d2017-05-04 01:57:17 +0000347 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
Calin Juravle0a5cad32020-02-14 20:29:26 +0000348 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kQuicken));
Calin Juravle357c66d2017-05-04 01:57:17 +0000349 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
Calin Juravle0a5cad32020-02-14 20:29:26 +0000350 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kExtract));
Calin Juravle357c66d2017-05-04 01:57:17 +0000351 EXPECT_EQ(-OatFileAssistant::kDex2OatForFilter,
Calin Juravle0a5cad32020-02-14 20:29:26 +0000352 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kEverything));
Calin Juravle357c66d2017-05-04 01:57:17 +0000353
354 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
355 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus());
356 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Nicolas Geoffray90a18cf2020-06-25 15:12:59 +0100357 EXPECT_TRUE(oat_file_assistant.HasDexFiles());
Calin Juravle357c66d2017-05-04 01:57:17 +0000358}
359
Richard Uhler66d874d2015-01-15 09:37:19 -0800360// Case: We have a DEX file and up-to-date OAT file for it.
Richard Uhler95abd042015-03-24 09:51:28 -0700361// Expect: The status is kNoDexOptNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800362TEST_F(OatFileAssistantTest, OatUpToDate) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000363 if (IsExecutedAsRoot()) {
364 // We cannot simulate non writable locations when executed as root: b/38000545.
365 LOG(ERROR) << "Test skipped because it's running as root";
366 return;
367 }
368
Richard Uhler66d874d2015-01-15 09:37:19 -0800369 std::string dex_location = GetScratchDir() + "/OatUpToDate.jar";
370 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000371 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800372
Vladimir Markof3d88a82018-12-21 16:38:47 +0000373 // Force the use of oat location by making the dex parent not writable.
Calin Juravle357c66d2017-05-04 01:57:17 +0000374 ScopedNonWritable scoped_non_writable(dex_location);
375 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
376
377 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
378
379 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Calin Juravle0a5cad32020-02-14 20:29:26 +0000380 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kSpeed));
Calin Juravle357c66d2017-05-04 01:57:17 +0000381 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Calin Juravle0a5cad32020-02-14 20:29:26 +0000382 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kQuicken));
Calin Juravle357c66d2017-05-04 01:57:17 +0000383 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Calin Juravle0a5cad32020-02-14 20:29:26 +0000384 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kExtract));
Calin Juravle357c66d2017-05-04 01:57:17 +0000385 EXPECT_EQ(OatFileAssistant::kDex2OatForFilter,
Calin Juravle0a5cad32020-02-14 20:29:26 +0000386 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kEverything));
Calin Juravle357c66d2017-05-04 01:57:17 +0000387
388 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
389 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
390 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus());
Nicolas Geoffray90a18cf2020-06-25 15:12:59 +0100391 EXPECT_TRUE(oat_file_assistant.HasDexFiles());
Calin Juravle5f9a8012018-02-12 20:27:46 -0800392
393 VerifyOptimizationStatus(dex_location, CompilerFilter::kSpeed, "unknown");
Calin Juravle357c66d2017-05-04 01:57:17 +0000394}
395
Vladimir Markoe0669322018-09-03 15:44:54 +0100396// Case: Passing valid file descriptors of updated odex/vdex files along with the dex file.
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700397// Expect: The status is kNoDexOptNeeded.
398TEST_F(OatFileAssistantTest, GetDexOptNeededWithFd) {
399 std::string dex_location = GetScratchDir() + "/OatUpToDate.jar";
400 std::string odex_location = GetScratchDir() + "/OatUpToDate.odex";
401 std::string vdex_location = GetScratchDir() + "/OatUpToDate.vdex";
402
403 Copy(GetDexSrc1(), dex_location);
404 GenerateOatForTest(dex_location.c_str(),
405 odex_location.c_str(),
406 CompilerFilter::kSpeed,
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700407 /* with_alternate_image= */ false);
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700408
Andreas Gampedfcd82c2018-10-16 20:22:37 -0700409 android::base::unique_fd odex_fd(open(odex_location.c_str(), O_RDONLY | O_CLOEXEC));
410 android::base::unique_fd vdex_fd(open(vdex_location.c_str(), O_RDONLY | O_CLOEXEC));
411 android::base::unique_fd zip_fd(open(dex_location.c_str(), O_RDONLY | O_CLOEXEC));
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700412
413 OatFileAssistant oat_file_assistant(dex_location.c_str(),
414 kRuntimeISA,
415 false,
Nicolas Geoffray29742602017-12-14 10:09:03 +0000416 false,
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700417 vdex_fd.get(),
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700418 odex_fd.get(),
419 zip_fd.get());
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700420 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Calin Juravle0a5cad32020-02-14 20:29:26 +0000421 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kSpeed));
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700422 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Calin Juravle0a5cad32020-02-14 20:29:26 +0000423 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kQuicken));
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700424 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Calin Juravle0a5cad32020-02-14 20:29:26 +0000425 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kExtract));
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700426 EXPECT_EQ(-OatFileAssistant::kDex2OatForFilter,
Calin Juravle0a5cad32020-02-14 20:29:26 +0000427 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kEverything));
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700428
429 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
430 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus());
431 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Nicolas Geoffray90a18cf2020-06-25 15:12:59 +0100432 EXPECT_TRUE(oat_file_assistant.HasDexFiles());
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700433}
434
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700435// Case: Passing invalid odex fd and valid vdex and zip fds.
436// Expect: The status should be kDex2OatForBootImage.
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700437TEST_F(OatFileAssistantTest, GetDexOptNeededWithInvalidOdexFd) {
438 std::string dex_location = GetScratchDir() + "/OatUpToDate.jar";
439 std::string odex_location = GetScratchDir() + "/OatUpToDate.odex";
440 std::string vdex_location = GetScratchDir() + "/OatUpToDate.vdex";
441
442 Copy(GetDexSrc1(), dex_location);
443 GenerateOatForTest(dex_location.c_str(),
444 odex_location.c_str(),
445 CompilerFilter::kSpeed,
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700446 /* with_alternate_image= */ false);
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700447
Andreas Gampedfcd82c2018-10-16 20:22:37 -0700448 android::base::unique_fd vdex_fd(open(vdex_location.c_str(), O_RDONLY | O_CLOEXEC));
449 android::base::unique_fd zip_fd(open(dex_location.c_str(), O_RDONLY | O_CLOEXEC));
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700450
451 OatFileAssistant oat_file_assistant(dex_location.c_str(),
452 kRuntimeISA,
453 false,
Nicolas Geoffray29742602017-12-14 10:09:03 +0000454 false,
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700455 vdex_fd.get(),
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700456 /* oat_fd= */ -1,
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700457 zip_fd.get());
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700458 EXPECT_EQ(-OatFileAssistant::kDex2OatForBootImage,
Calin Juravle0a5cad32020-02-14 20:29:26 +0000459 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kSpeed));
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700460 EXPECT_EQ(-OatFileAssistant::kDex2OatForBootImage,
Calin Juravle0a5cad32020-02-14 20:29:26 +0000461 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kEverything));
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700462
463 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700464 EXPECT_EQ(OatFileAssistant::kOatBootImageOutOfDate, oat_file_assistant.OdexFileStatus());
465 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Nicolas Geoffray90a18cf2020-06-25 15:12:59 +0100466 EXPECT_TRUE(oat_file_assistant.HasDexFiles());
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700467}
468
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700469// Case: Passing invalid vdex fd and valid odex and zip fds.
470// Expect: The status should be kDex2OatFromScratch.
471TEST_F(OatFileAssistantTest, GetDexOptNeededWithInvalidVdexFd) {
472 std::string dex_location = GetScratchDir() + "/OatUpToDate.jar";
473 std::string odex_location = GetScratchDir() + "/OatUpToDate.odex";
474
475 Copy(GetDexSrc1(), dex_location);
476 GenerateOatForTest(dex_location.c_str(),
477 odex_location.c_str(),
478 CompilerFilter::kSpeed,
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700479 /* with_alternate_image= */ false);
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700480
Andreas Gampedfcd82c2018-10-16 20:22:37 -0700481 android::base::unique_fd odex_fd(open(odex_location.c_str(), O_RDONLY | O_CLOEXEC));
482 android::base::unique_fd zip_fd(open(dex_location.c_str(), O_RDONLY | O_CLOEXEC));
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700483
484 OatFileAssistant oat_file_assistant(dex_location.c_str(),
485 kRuntimeISA,
486 false,
Nicolas Geoffray29742602017-12-14 10:09:03 +0000487 false,
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700488 /* vdex_fd= */ -1,
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700489 odex_fd.get(),
490 zip_fd.get());
491
492 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Calin Juravle0a5cad32020-02-14 20:29:26 +0000493 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kSpeed));
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700494 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
495 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
496 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Nicolas Geoffray90a18cf2020-06-25 15:12:59 +0100497 EXPECT_TRUE(oat_file_assistant.HasDexFiles());
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700498}
499
500// Case: Passing invalid vdex and odex fd with valid zip fd.
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700501// Expect: The status is kDex2oatFromScratch.
502TEST_F(OatFileAssistantTest, GetDexOptNeededWithInvalidOdexVdexFd) {
503 std::string dex_location = GetScratchDir() + "/OatUpToDate.jar";
504
505 Copy(GetDexSrc1(), dex_location);
506
Andreas Gampedfcd82c2018-10-16 20:22:37 -0700507 android::base::unique_fd zip_fd(open(dex_location.c_str(), O_RDONLY | O_CLOEXEC));
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700508 OatFileAssistant oat_file_assistant(dex_location.c_str(),
509 kRuntimeISA,
510 false,
Nicolas Geoffray29742602017-12-14 10:09:03 +0000511 false,
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700512 /* vdex_fd= */ -1,
513 /* oat_fd= */ -1,
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700514 zip_fd);
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700515 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Calin Juravle0a5cad32020-02-14 20:29:26 +0000516 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kSpeed));
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700517 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
518 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
519}
520
Richard Uhler2f27abd2017-01-31 14:02:34 +0000521// Case: We have a DEX file and up-to-date (ODEX) VDEX file for it, but no
522// ODEX file.
523TEST_F(OatFileAssistantTest, VdexUpToDateNoOdex) {
Richard Uhler2f27abd2017-01-31 14:02:34 +0000524 std::string dex_location = GetScratchDir() + "/VdexUpToDateNoOdex.jar";
Calin Juravle357c66d2017-05-04 01:57:17 +0000525 std::string odex_location = GetOdexDir() + "/VdexUpToDateNoOdex.oat";
Richard Uhler2f27abd2017-01-31 14:02:34 +0000526
Richard Uhler9a37efc2016-08-05 16:32:55 -0700527 Copy(GetDexSrc1(), dex_location);
528
Richard Uhler2f27abd2017-01-31 14:02:34 +0000529 // Generating and deleting the oat file should have the side effect of
530 // creating an up-to-date vdex file.
Calin Juravle357c66d2017-05-04 01:57:17 +0000531 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
532 ASSERT_EQ(0, unlink(odex_location.c_str()));
Richard Uhler2f27abd2017-01-31 14:02:34 +0000533
Calin Juravle357c66d2017-05-04 01:57:17 +0000534 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000535
536 // Even though the vdex file is up to date, because we don't have the oat
537 // file, we can't know that the vdex depends on the boot image and is up to
538 // date with respect to the boot image. Instead we must assume the vdex file
539 // depends on the boot image and is out of date with respect to the boot
540 // image.
541 EXPECT_EQ(-OatFileAssistant::kDex2OatForBootImage,
Calin Juravle0a5cad32020-02-14 20:29:26 +0000542 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kSpeed));
Richard Uhler2f27abd2017-01-31 14:02:34 +0000543
544 // Make sure we don't crash in this case when we dump the status. We don't
545 // care what the actual dumped value is.
546 oat_file_assistant.GetStatusDump();
Calin Juravle5f9a8012018-02-12 20:27:46 -0800547
548 VerifyOptimizationStatus(dex_location, "run-from-apk", "unknown");
Richard Uhler2f27abd2017-01-31 14:02:34 +0000549}
550
551// Case: We have a DEX file and empty VDEX and ODEX files.
552TEST_F(OatFileAssistantTest, EmptyVdexOdex) {
553 std::string dex_location = GetScratchDir() + "/EmptyVdexOdex.jar";
554 std::string odex_location = GetOdexDir() + "/EmptyVdexOdex.oat";
555 std::string vdex_location = GetOdexDir() + "/EmptyVdexOdex.vdex";
556
557 Copy(GetDexSrc1(), dex_location);
Richard Uhler5cd59292017-02-01 12:54:23 +0000558 ScratchFile vdex_file(vdex_location.c_str());
559 ScratchFile odex_file(odex_location.c_str());
Richard Uhler2f27abd2017-01-31 14:02:34 +0000560
561 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
562 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Calin Juravle0a5cad32020-02-14 20:29:26 +0000563 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kSpeed));
Richard Uhler2f27abd2017-01-31 14:02:34 +0000564}
565
566// Case: We have a DEX file and up-to-date (OAT) VDEX file for it, but no OAT
567// file.
568TEST_F(OatFileAssistantTest, VdexUpToDateNoOat) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000569 if (IsExecutedAsRoot()) {
570 // We cannot simulate non writable locations when executed as root: b/38000545.
571 LOG(ERROR) << "Test skipped because it's running as root";
572 return;
573 }
Richard Uhler2f27abd2017-01-31 14:02:34 +0000574
575 std::string dex_location = GetScratchDir() + "/VdexUpToDateNoOat.jar";
576 std::string oat_location;
577 std::string error_msg;
578 ASSERT_TRUE(OatFileAssistant::DexLocationToOatFilename(
579 dex_location, kRuntimeISA, &oat_location, &error_msg)) << error_msg;
580
581 Copy(GetDexSrc1(), dex_location);
582 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
583 ASSERT_EQ(0, unlink(oat_location.c_str()));
584
Calin Juravle357c66d2017-05-04 01:57:17 +0000585 ScopedNonWritable scoped_non_writable(dex_location);
586 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
Richard Uhler9a37efc2016-08-05 16:32:55 -0700587 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
588
Richard Uhler2f27abd2017-01-31 14:02:34 +0000589 // Even though the vdex file is up to date, because we don't have the oat
590 // file, we can't know that the vdex depends on the boot image and is up to
591 // date with respect to the boot image. Instead we must assume the vdex file
592 // depends on the boot image and is out of date with respect to the boot
593 // image.
594 EXPECT_EQ(OatFileAssistant::kDex2OatForBootImage,
Calin Juravle0a5cad32020-02-14 20:29:26 +0000595 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kSpeed));
Richard Uhler9a37efc2016-08-05 16:32:55 -0700596}
597
Andreas Gampe29d38e72016-03-23 15:31:51 +0000598// Case: We have a DEX file and speed-profile OAT file for it.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700599// Expect: The status is kNoDexOptNeeded if the profile hasn't changed, but
600// kDex2Oat if the profile has changed.
Andreas Gampe29d38e72016-03-23 15:31:51 +0000601TEST_F(OatFileAssistantTest, ProfileOatUpToDate) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000602 if (IsExecutedAsRoot()) {
603 // We cannot simulate non writable locations when executed as root: b/38000545.
604 LOG(ERROR) << "Test skipped because it's running as root";
605 return;
606 }
607
Andreas Gampe29d38e72016-03-23 15:31:51 +0000608 std::string dex_location = GetScratchDir() + "/ProfileOatUpToDate.jar";
609 Copy(GetDexSrc1(), dex_location);
610 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeedProfile);
611
Calin Juravle357c66d2017-05-04 01:57:17 +0000612 ScopedNonWritable scoped_non_writable(dex_location);
613 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
614
Richard Uhlerd1472a22016-04-15 15:18:56 -0700615 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000616
617 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Calin Juravle0a5cad32020-02-14 20:29:26 +0000618 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kSpeedProfile, false));
Andreas Gampe29d38e72016-03-23 15:31:51 +0000619 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Calin Juravle0a5cad32020-02-14 20:29:26 +0000620 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kQuicken, false));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000621 EXPECT_EQ(OatFileAssistant::kDex2OatForFilter,
Calin Juravle0a5cad32020-02-14 20:29:26 +0000622 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kSpeedProfile, true));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000623 EXPECT_EQ(OatFileAssistant::kDex2OatForFilter,
Calin Juravle0a5cad32020-02-14 20:29:26 +0000624 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kQuicken, true));
Andreas Gampe29d38e72016-03-23 15:31:51 +0000625
626 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000627 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
Andreas Gampe29d38e72016-03-23 15:31:51 +0000628 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus());
Nicolas Geoffray90a18cf2020-06-25 15:12:59 +0100629 EXPECT_TRUE(oat_file_assistant.HasDexFiles());
Andreas Gampe29d38e72016-03-23 15:31:51 +0000630}
631
Richard Uhler66d874d2015-01-15 09:37:19 -0800632// Case: We have a MultiDEX file and up-to-date OAT file for it.
Richard Uhler95abd042015-03-24 09:51:28 -0700633// Expect: The status is kNoDexOptNeeded and we load all dex files.
Richard Uhler66d874d2015-01-15 09:37:19 -0800634TEST_F(OatFileAssistantTest, MultiDexOatUpToDate) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000635 if (IsExecutedAsRoot()) {
636 // We cannot simulate non writable locations when executed as root: b/38000545.
637 LOG(ERROR) << "Test skipped because it's running as root";
638 return;
639 }
640
Richard Uhler66d874d2015-01-15 09:37:19 -0800641 std::string dex_location = GetScratchDir() + "/MultiDexOatUpToDate.jar";
642 Copy(GetMultiDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000643 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800644
Calin Juravle357c66d2017-05-04 01:57:17 +0000645 ScopedNonWritable scoped_non_writable(dex_location);
646 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
647
Richard Uhlerd1472a22016-04-15 15:18:56 -0700648 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000649 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Calin Juravle0a5cad32020-02-14 20:29:26 +0000650 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kSpeed));
Nicolas Geoffray90a18cf2020-06-25 15:12:59 +0100651 EXPECT_TRUE(oat_file_assistant.HasDexFiles());
Richard Uhler95abd042015-03-24 09:51:28 -0700652
653 // Verify we can load both dex files.
Richard Uhlere5fed032015-03-18 08:21:11 -0700654 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
Richard Uhler66d874d2015-01-15 09:37:19 -0800655 ASSERT_TRUE(oat_file.get() != nullptr);
656 EXPECT_TRUE(oat_file->IsExecutable());
657 std::vector<std::unique_ptr<const DexFile>> dex_files;
Richard Uhlere5fed032015-03-18 08:21:11 -0700658 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
659 EXPECT_EQ(2u, dex_files.size());
660}
661
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000662// Case: We have a MultiDEX file where the non-main multdex entry is out of date.
Richard Uhler67ff7d12015-05-14 13:21:13 -0700663// Expect: The status is kDex2OatNeeded.
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000664TEST_F(OatFileAssistantTest, MultiDexNonMainOutOfDate) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000665 if (IsExecutedAsRoot()) {
666 // We cannot simulate non writable locations when executed as root: b/38000545.
667 LOG(ERROR) << "Test skipped because it's running as root";
668 return;
669 }
670
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000671 std::string dex_location = GetScratchDir() + "/MultiDexNonMainOutOfDate.jar";
Richard Uhler67ff7d12015-05-14 13:21:13 -0700672
673 // Compile code for GetMultiDexSrc1.
674 Copy(GetMultiDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000675 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler67ff7d12015-05-14 13:21:13 -0700676
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000677 // Now overwrite the dex file with GetMultiDexSrc2 so the non-main checksum
Richard Uhler67ff7d12015-05-14 13:21:13 -0700678 // is out of date.
679 Copy(GetMultiDexSrc2(), dex_location);
680
Calin Juravle357c66d2017-05-04 01:57:17 +0000681 ScopedNonWritable scoped_non_writable(dex_location);
682 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
683
Richard Uhlerd1472a22016-04-15 15:18:56 -0700684 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler7225a8d2016-11-22 10:12:03 +0000685 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Calin Juravle0a5cad32020-02-14 20:29:26 +0000686 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kSpeed));
Nicolas Geoffray90a18cf2020-06-25 15:12:59 +0100687 EXPECT_TRUE(oat_file_assistant.HasDexFiles());
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000688}
689
Richard Uhler03bc6592016-11-22 09:42:04 +0000690// Case: We have a DEX file and an OAT file out of date with respect to the
691// dex checksum.
692TEST_F(OatFileAssistantTest, OatDexOutOfDate) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000693 if (IsExecutedAsRoot()) {
694 // We cannot simulate non writable locations when executed as root: b/38000545.
695 LOG(ERROR) << "Test skipped because it's running as root";
696 return;
697 }
698
Richard Uhler03bc6592016-11-22 09:42:04 +0000699 std::string dex_location = GetScratchDir() + "/OatDexOutOfDate.jar";
Richard Uhler66d874d2015-01-15 09:37:19 -0800700
701 // We create a dex, generate an oat for it, then overwrite the dex with a
702 // different dex to make the oat out of date.
703 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000704 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800705 Copy(GetDexSrc2(), dex_location);
706
Calin Juravle357c66d2017-05-04 01:57:17 +0000707 ScopedNonWritable scoped_non_writable(dex_location);
708 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
709
Richard Uhlerd1472a22016-04-15 15:18:56 -0700710 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler7225a8d2016-11-22 10:12:03 +0000711 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Calin Juravle0a5cad32020-02-14 20:29:26 +0000712 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kExtract));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000713 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Calin Juravle0a5cad32020-02-14 20:29:26 +0000714 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800715
716 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000717 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
718 EXPECT_EQ(OatFileAssistant::kOatDexOutOfDate, oat_file_assistant.OatFileStatus());
Nicolas Geoffray90a18cf2020-06-25 15:12:59 +0100719 EXPECT_TRUE(oat_file_assistant.HasDexFiles());
Richard Uhler03bc6592016-11-22 09:42:04 +0000720}
721
Richard Uhler2f27abd2017-01-31 14:02:34 +0000722// Case: We have a DEX file and an (ODEX) VDEX file out of date with respect
723// to the dex checksum, but no ODEX file.
724TEST_F(OatFileAssistantTest, VdexDexOutOfDate) {
Richard Uhler2f27abd2017-01-31 14:02:34 +0000725 std::string dex_location = GetScratchDir() + "/VdexDexOutOfDate.jar";
Calin Juravle357c66d2017-05-04 01:57:17 +0000726 std::string odex_location = GetOdexDir() + "/VdexDexOutOfDate.oat";
Richard Uhler2f27abd2017-01-31 14:02:34 +0000727
728 Copy(GetDexSrc1(), dex_location);
Calin Juravle357c66d2017-05-04 01:57:17 +0000729 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
730 ASSERT_EQ(0, unlink(odex_location.c_str()));
Richard Uhler2f27abd2017-01-31 14:02:34 +0000731 Copy(GetDexSrc2(), dex_location);
732
Calin Juravle357c66d2017-05-04 01:57:17 +0000733 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000734
735 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Calin Juravle0a5cad32020-02-14 20:29:26 +0000736 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kSpeed));
Richard Uhler2f27abd2017-01-31 14:02:34 +0000737}
738
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000739// Case: We have a MultiDEX (ODEX) VDEX file where the non-main multidex entry
740// is out of date and there is no corresponding ODEX file.
741TEST_F(OatFileAssistantTest, VdexMultiDexNonMainOutOfDate) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000742 std::string dex_location = GetScratchDir() + "/VdexMultiDexNonMainOutOfDate.jar";
Calin Juravle357c66d2017-05-04 01:57:17 +0000743 std::string odex_location = GetOdexDir() + "/VdexMultiDexNonMainOutOfDate.odex";
Richard Uhler2f27abd2017-01-31 14:02:34 +0000744
745 Copy(GetMultiDexSrc1(), dex_location);
Calin Juravle357c66d2017-05-04 01:57:17 +0000746 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
747 ASSERT_EQ(0, unlink(odex_location.c_str()));
Richard Uhler2f27abd2017-01-31 14:02:34 +0000748 Copy(GetMultiDexSrc2(), dex_location);
749
Calin Juravle357c66d2017-05-04 01:57:17 +0000750 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000751
752 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Calin Juravle0a5cad32020-02-14 20:29:26 +0000753 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kSpeed));
Richard Uhler2f27abd2017-01-31 14:02:34 +0000754}
755
Richard Uhler03bc6592016-11-22 09:42:04 +0000756// Case: We have a DEX file and an OAT file out of date with respect to the
757// boot image.
758TEST_F(OatFileAssistantTest, OatImageOutOfDate) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000759 if (IsExecutedAsRoot()) {
760 // We cannot simulate non writable locations when executed as root: b/38000545.
761 LOG(ERROR) << "Test skipped because it's running as root";
762 return;
763 }
764
Richard Uhler03bc6592016-11-22 09:42:04 +0000765 std::string dex_location = GetScratchDir() + "/OatImageOutOfDate.jar";
766
767 Copy(GetDexSrc1(), dex_location);
768 GenerateOatForTest(dex_location.c_str(),
769 CompilerFilter::kSpeed,
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700770 /* with_alternate_image= */ true);
Richard Uhler03bc6592016-11-22 09:42:04 +0000771
Calin Juravle357c66d2017-05-04 01:57:17 +0000772 ScopedNonWritable scoped_non_writable(dex_location);
773 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
774
Richard Uhler03bc6592016-11-22 09:42:04 +0000775 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler7225a8d2016-11-22 10:12:03 +0000776 EXPECT_EQ(OatFileAssistant::kDex2OatForBootImage,
Calin Juravle0a5cad32020-02-14 20:29:26 +0000777 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kExtract));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000778 EXPECT_EQ(OatFileAssistant::kDex2OatForBootImage,
Calin Juravle0a5cad32020-02-14 20:29:26 +0000779 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kQuicken));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000780 EXPECT_EQ(OatFileAssistant::kDex2OatForBootImage,
Calin Juravle0a5cad32020-02-14 20:29:26 +0000781 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kSpeed));
Richard Uhler03bc6592016-11-22 09:42:04 +0000782
783 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
784 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
785 EXPECT_EQ(OatFileAssistant::kOatBootImageOutOfDate, oat_file_assistant.OatFileStatus());
Nicolas Geoffray90a18cf2020-06-25 15:12:59 +0100786 EXPECT_TRUE(oat_file_assistant.HasDexFiles());
Richard Uhler03bc6592016-11-22 09:42:04 +0000787}
788
789// Case: We have a DEX file and a verify-at-runtime OAT file out of date with
790// respect to the boot image.
791// It shouldn't matter that the OAT file is out of date, because it is
792// verify-at-runtime.
793TEST_F(OatFileAssistantTest, OatVerifyAtRuntimeImageOutOfDate) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000794 if (IsExecutedAsRoot()) {
795 // We cannot simulate non writable locations when executed as root: b/38000545.
796 LOG(ERROR) << "Test skipped because it's running as root";
797 return;
798 }
799
Richard Uhler03bc6592016-11-22 09:42:04 +0000800 std::string dex_location = GetScratchDir() + "/OatVerifyAtRuntimeImageOutOfDate.jar";
801
802 Copy(GetDexSrc1(), dex_location);
803 GenerateOatForTest(dex_location.c_str(),
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100804 CompilerFilter::kExtract,
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700805 /* with_alternate_image= */ true);
Richard Uhler03bc6592016-11-22 09:42:04 +0000806
Calin Juravle357c66d2017-05-04 01:57:17 +0000807 ScopedNonWritable scoped_non_writable(dex_location);
808 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
809
Richard Uhler03bc6592016-11-22 09:42:04 +0000810 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
811 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Calin Juravle0a5cad32020-02-14 20:29:26 +0000812 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kExtract));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000813 EXPECT_EQ(OatFileAssistant::kDex2OatForFilter,
Calin Juravle0a5cad32020-02-14 20:29:26 +0000814 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kQuicken));
Richard Uhler03bc6592016-11-22 09:42:04 +0000815
816 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
817 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
818 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus());
Nicolas Geoffray90a18cf2020-06-25 15:12:59 +0100819 EXPECT_TRUE(oat_file_assistant.HasDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800820}
821
822// Case: We have a DEX file and an ODEX file, but no OAT file.
Richard Uhler66d874d2015-01-15 09:37:19 -0800823TEST_F(OatFileAssistantTest, DexOdexNoOat) {
824 std::string dex_location = GetScratchDir() + "/DexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700825 std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800826
827 // Create the dex and odex files
828 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000829 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800830
831 // Verify the status.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700832 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800833
Andreas Gampe29d38e72016-03-23 15:31:51 +0000834 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Calin Juravle0a5cad32020-02-14 20:29:26 +0000835 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kExtract));
Vladimir Markoe0669322018-09-03 15:44:54 +0100836 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Calin Juravle0a5cad32020-02-14 20:29:26 +0000837 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800838
839 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Vladimir Markoe0669322018-09-03 15:44:54 +0100840 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus());
Richard Uhler03bc6592016-11-22 09:42:04 +0000841 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Nicolas Geoffray90a18cf2020-06-25 15:12:59 +0100842 EXPECT_TRUE(oat_file_assistant.HasDexFiles());
Richard Uhler5f946da2015-07-17 12:28:32 -0700843
844 // We should still be able to get the non-executable odex file to run from.
845 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
846 ASSERT_TRUE(oat_file.get() != nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800847}
848
Nicolas Geoffray90a18cf2020-06-25 15:12:59 +0100849// Case: We have a resource-only DEX file, no ODEX file and no
Richard Uhler9b994ea2015-06-24 08:44:19 -0700850// OAT file. Expect: The status is kNoDexOptNeeded.
851TEST_F(OatFileAssistantTest, ResourceOnlyDex) {
852 std::string dex_location = GetScratchDir() + "/ResourceOnlyDex.jar";
853
854 Copy(GetStrippedDexSrc1(), dex_location);
855
856 // Verify the status.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700857 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler9b994ea2015-06-24 08:44:19 -0700858
Andreas Gampe29d38e72016-03-23 15:31:51 +0000859 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Calin Juravle0a5cad32020-02-14 20:29:26 +0000860 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kSpeed));
Andreas Gampe29d38e72016-03-23 15:31:51 +0000861 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Calin Juravle0a5cad32020-02-14 20:29:26 +0000862 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kExtract));
Andreas Gampe29d38e72016-03-23 15:31:51 +0000863 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Calin Juravle0a5cad32020-02-14 20:29:26 +0000864 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kQuicken));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700865
866 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000867 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
868 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Nicolas Geoffray90a18cf2020-06-25 15:12:59 +0100869 EXPECT_FALSE(oat_file_assistant.HasDexFiles());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700870
Andreas Gampe29d38e72016-03-23 15:31:51 +0000871 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Calin Juravle0a5cad32020-02-14 20:29:26 +0000872 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kSpeed));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700873
874 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000875 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
876 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Nicolas Geoffray90a18cf2020-06-25 15:12:59 +0100877 EXPECT_FALSE(oat_file_assistant.HasDexFiles());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700878}
879
Vladimir Markoe0669322018-09-03 15:44:54 +0100880// Case: We have a DEX file, an ODEX file and an OAT file.
881// Expect: It shouldn't crash. We should load the odex file executable.
Richard Uhler66d874d2015-01-15 09:37:19 -0800882TEST_F(OatFileAssistantTest, OdexOatOverlap) {
883 std::string dex_location = GetScratchDir() + "/OdexOatOverlap.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700884 std::string odex_location = GetOdexDir() + "/OdexOatOverlap.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800885
Calin Juravle357c66d2017-05-04 01:57:17 +0000886 // Create the dex, the odex and the oat files.
Richard Uhler66d874d2015-01-15 09:37:19 -0800887 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000888 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Vladimir Markoe0669322018-09-03 15:44:54 +0100889 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800890
891 // Verify things don't go bad.
Calin Juravle357c66d2017-05-04 01:57:17 +0000892 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800893
Vladimir Markoe0669322018-09-03 15:44:54 +0100894 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Calin Juravle0a5cad32020-02-14 20:29:26 +0000895 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800896
897 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Vladimir Markoe0669322018-09-03 15:44:54 +0100898 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus());
899 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus());
Nicolas Geoffray90a18cf2020-06-25 15:12:59 +0100900 EXPECT_TRUE(oat_file_assistant.HasDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800901
Richard Uhler66d874d2015-01-15 09:37:19 -0800902 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
903 ASSERT_TRUE(oat_file.get() != nullptr);
Richard Uhlerf16d5722015-05-11 09:32:47 -0700904
Vladimir Markoe0669322018-09-03 15:44:54 +0100905 EXPECT_TRUE(oat_file->IsExecutable());
Richard Uhler66d874d2015-01-15 09:37:19 -0800906 std::vector<std::unique_ptr<const DexFile>> dex_files;
907 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
908 EXPECT_EQ(1u, dex_files.size());
909}
910
Andreas Gampe29d38e72016-03-23 15:31:51 +0000911// Case: We have a DEX file and a VerifyAtRuntime ODEX file, but no OAT file.
912// Expect: The status is kNoDexOptNeeded, because VerifyAtRuntime contains no code.
913TEST_F(OatFileAssistantTest, DexVerifyAtRuntimeOdexNoOat) {
914 std::string dex_location = GetScratchDir() + "/DexVerifyAtRuntimeOdexNoOat.jar";
915 std::string odex_location = GetOdexDir() + "/DexVerifyAtRuntimeOdexNoOat.odex";
David Brazdilce4b0ba2016-01-28 15:05:49 +0000916
917 // Create the dex and odex files
918 Copy(GetDexSrc1(), dex_location);
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100919 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kExtract);
David Brazdilce4b0ba2016-01-28 15:05:49 +0000920
921 // Verify the status.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700922 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
David Brazdilce4b0ba2016-01-28 15:05:49 +0000923
Andreas Gampe29d38e72016-03-23 15:31:51 +0000924 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Calin Juravle0a5cad32020-02-14 20:29:26 +0000925 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kExtract));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000926 EXPECT_EQ(-OatFileAssistant::kDex2OatForFilter,
Calin Juravle0a5cad32020-02-14 20:29:26 +0000927 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kSpeed));
David Brazdilce4b0ba2016-01-28 15:05:49 +0000928
929 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler3e580bc2016-11-08 16:23:07 +0000930 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus());
Richard Uhler03bc6592016-11-22 09:42:04 +0000931 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Nicolas Geoffray90a18cf2020-06-25 15:12:59 +0100932 EXPECT_TRUE(oat_file_assistant.HasDexFiles());
David Brazdilce4b0ba2016-01-28 15:05:49 +0000933}
934
Richard Uhler66d874d2015-01-15 09:37:19 -0800935// Case: We have a DEX file and up-to-date OAT file for it.
936// Expect: We should load an executable dex file.
937TEST_F(OatFileAssistantTest, LoadOatUpToDate) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000938 if (IsExecutedAsRoot()) {
939 // We cannot simulate non writable locations when executed as root: b/38000545.
940 LOG(ERROR) << "Test skipped because it's running as root";
941 return;
942 }
943
Richard Uhler66d874d2015-01-15 09:37:19 -0800944 std::string dex_location = GetScratchDir() + "/LoadOatUpToDate.jar";
945
946 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000947 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800948
Calin Juravle357c66d2017-05-04 01:57:17 +0000949 ScopedNonWritable scoped_non_writable(dex_location);
950 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
951
Richard Uhler66d874d2015-01-15 09:37:19 -0800952 // Load the oat using an oat file assistant.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700953 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000954
955 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
956 ASSERT_TRUE(oat_file.get() != nullptr);
957 EXPECT_TRUE(oat_file->IsExecutable());
958 std::vector<std::unique_ptr<const DexFile>> dex_files;
959 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
960 EXPECT_EQ(1u, dex_files.size());
961}
962
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100963// Case: We have a DEX file and up-to-date quicken OAT file for it.
Andreas Gampe29d38e72016-03-23 15:31:51 +0000964// Expect: We should still load the oat file as executable.
965TEST_F(OatFileAssistantTest, LoadExecInterpretOnlyOatUpToDate) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000966 if (IsExecutedAsRoot()) {
967 // We cannot simulate non writable locations when executed as root: b/38000545.
968 LOG(ERROR) << "Test skipped because it's running as root";
969 return;
970 }
971
Andreas Gampe29d38e72016-03-23 15:31:51 +0000972 std::string dex_location = GetScratchDir() + "/LoadExecInterpretOnlyOatUpToDate.jar";
973
974 Copy(GetDexSrc1(), dex_location);
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100975 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kQuicken);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000976
Calin Juravle357c66d2017-05-04 01:57:17 +0000977 ScopedNonWritable scoped_non_writable(dex_location);
978 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
979
Andreas Gampe29d38e72016-03-23 15:31:51 +0000980 // Load the oat using an oat file assistant.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700981 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800982
983 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
984 ASSERT_TRUE(oat_file.get() != nullptr);
985 EXPECT_TRUE(oat_file->IsExecutable());
986 std::vector<std::unique_ptr<const DexFile>> dex_files;
987 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
988 EXPECT_EQ(1u, dex_files.size());
989}
990
991// Case: We have a DEX file and up-to-date OAT file for it.
992// Expect: Loading non-executable should load the oat non-executable.
993TEST_F(OatFileAssistantTest, LoadNoExecOatUpToDate) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000994 if (IsExecutedAsRoot()) {
995 // We cannot simulate non writable locations when executed as root: b/38000545.
996 LOG(ERROR) << "Test skipped because it's running as root";
997 return;
998 }
999
Richard Uhler66d874d2015-01-15 09:37:19 -08001000 std::string dex_location = GetScratchDir() + "/LoadNoExecOatUpToDate.jar";
1001
1002 Copy(GetDexSrc1(), dex_location);
Calin Juravle357c66d2017-05-04 01:57:17 +00001003
1004 ScopedNonWritable scoped_non_writable(dex_location);
1005 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
1006
Andreas Gampe29d38e72016-03-23 15:31:51 +00001007 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -08001008
1009 // Load the oat using an oat file assistant.
Richard Uhlerd1472a22016-04-15 15:18:56 -07001010 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -08001011
1012 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1013 ASSERT_TRUE(oat_file.get() != nullptr);
1014 EXPECT_FALSE(oat_file->IsExecutable());
1015 std::vector<std::unique_ptr<const DexFile>> dex_files;
1016 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
1017 EXPECT_EQ(1u, dex_files.size());
1018}
1019
Richard Uhler66d874d2015-01-15 09:37:19 -08001020// Turn an absolute path into a path relative to the current working
1021// directory.
Andreas Gampeca620d72016-11-08 08:09:33 -08001022static std::string MakePathRelative(const std::string& target) {
Richard Uhler66d874d2015-01-15 09:37:19 -08001023 char buf[MAXPATHLEN];
1024 std::string cwd = getcwd(buf, MAXPATHLEN);
1025
1026 // Split the target and cwd paths into components.
1027 std::vector<std::string> target_path;
1028 std::vector<std::string> cwd_path;
1029 Split(target, '/', &target_path);
1030 Split(cwd, '/', &cwd_path);
1031
1032 // Reverse the path components, so we can use pop_back().
1033 std::reverse(target_path.begin(), target_path.end());
1034 std::reverse(cwd_path.begin(), cwd_path.end());
1035
1036 // Drop the common prefix of the paths. Because we reversed the path
1037 // components, this becomes the common suffix of target_path and cwd_path.
1038 while (!target_path.empty() && !cwd_path.empty()
1039 && target_path.back() == cwd_path.back()) {
1040 target_path.pop_back();
1041 cwd_path.pop_back();
1042 }
1043
1044 // For each element of the remaining cwd_path, add '..' to the beginning
1045 // of the target path. Because we reversed the path components, we add to
1046 // the end of target_path.
1047 for (unsigned int i = 0; i < cwd_path.size(); i++) {
1048 target_path.push_back("..");
1049 }
1050
1051 // Reverse again to get the right path order, and join to get the result.
1052 std::reverse(target_path.begin(), target_path.end());
Andreas Gampe9186ced2016-12-12 14:28:21 -08001053 return android::base::Join(target_path, '/');
Richard Uhler66d874d2015-01-15 09:37:19 -08001054}
1055
1056// Case: Non-absolute path to Dex location.
1057// Expect: Not sure, but it shouldn't crash.
1058TEST_F(OatFileAssistantTest, NonAbsoluteDexLocation) {
1059 std::string abs_dex_location = GetScratchDir() + "/NonAbsoluteDexLocation.jar";
1060 Copy(GetDexSrc1(), abs_dex_location);
1061
1062 std::string dex_location = MakePathRelative(abs_dex_location);
Richard Uhlerd1472a22016-04-15 15:18:56 -07001063 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001064
1065 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler7225a8d2016-11-22 10:12:03 +00001066 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Calin Juravle0a5cad32020-02-14 20:29:26 +00001067 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kSpeed));
Richard Uhler03bc6592016-11-22 09:42:04 +00001068 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
1069 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler66d874d2015-01-15 09:37:19 -08001070}
1071
1072// Case: Very short, non-existent Dex location.
Richard Uhler9b994ea2015-06-24 08:44:19 -07001073// Expect: kNoDexOptNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -08001074TEST_F(OatFileAssistantTest, ShortDexLocation) {
1075 std::string dex_location = "/xx";
1076
Richard Uhlerd1472a22016-04-15 15:18:56 -07001077 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001078
1079 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Andreas Gampe29d38e72016-03-23 15:31:51 +00001080 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Calin Juravle0a5cad32020-02-14 20:29:26 +00001081 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kSpeed));
Richard Uhler03bc6592016-11-22 09:42:04 +00001082 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
1083 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Nicolas Geoffray90a18cf2020-06-25 15:12:59 +01001084 EXPECT_FALSE(oat_file_assistant.HasDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -08001085}
1086
1087// Case: Non-standard extension for dex file.
Richard Uhler95abd042015-03-24 09:51:28 -07001088// Expect: The status is kDex2OatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -08001089TEST_F(OatFileAssistantTest, LongDexExtension) {
1090 std::string dex_location = GetScratchDir() + "/LongDexExtension.jarx";
1091 Copy(GetDexSrc1(), dex_location);
1092
Richard Uhlerd1472a22016-04-15 15:18:56 -07001093 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -08001094
Richard Uhler7225a8d2016-11-22 10:12:03 +00001095 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Calin Juravle0a5cad32020-02-14 20:29:26 +00001096 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -08001097
1098 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +00001099 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
1100 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler66d874d2015-01-15 09:37:19 -08001101}
1102
1103// A task to generate a dex location. Used by the RaceToGenerate test.
1104class RaceGenerateTask : public Task {
1105 public:
Vladimir Markob9c29f62019-03-20 14:22:51 +00001106 RaceGenerateTask(OatFileAssistantTest& test,
1107 const std::string& dex_location,
Nicolas Geoffray1e76d7a2018-09-03 13:23:34 +01001108 const std::string& oat_location,
1109 Mutex* lock)
Vladimir Markob9c29f62019-03-20 14:22:51 +00001110 : test_(test),
1111 dex_location_(dex_location),
Nicolas Geoffray1e76d7a2018-09-03 13:23:34 +01001112 oat_location_(oat_location),
1113 lock_(lock),
1114 loaded_oat_file_(nullptr)
Richard Uhler66d874d2015-01-15 09:37:19 -08001115 {}
1116
Andreas Gampefa6a1b02018-09-07 08:11:55 -07001117 void Run(Thread* self ATTRIBUTE_UNUSED) override {
Richard Uhler66d874d2015-01-15 09:37:19 -08001118 // Load the dex files, and save a pointer to the loaded oat file, so that
1119 // we can verify only one oat file was loaded for the dex location.
Richard Uhler66d874d2015-01-15 09:37:19 -08001120 std::vector<std::unique_ptr<const DexFile>> dex_files;
1121 std::vector<std::string> error_msgs;
Mathieu Chartiere58991b2015-10-13 07:59:34 -07001122 const OatFile* oat_file = nullptr;
Nicolas Geoffray3d8a78a2018-08-29 21:10:16 +00001123 {
Nicolas Geoffray1e76d7a2018-09-03 13:23:34 +01001124 MutexLock mu(Thread::Current(), *lock_);
Nicolas Geoffray3d8a78a2018-08-29 21:10:16 +00001125 // Create the oat file.
1126 std::vector<std::string> args;
1127 args.push_back("--dex-file=" + dex_location_);
1128 args.push_back("--oat-file=" + oat_location_);
1129 std::string error_msg;
Vladimir Markob9c29f62019-03-20 14:22:51 +00001130 ASSERT_TRUE(test_.Dex2Oat(args, &error_msg)) << error_msg;
Nicolas Geoffray3d8a78a2018-08-29 21:10:16 +00001131 }
1132
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001133 dex_files = Runtime::Current()->GetOatFileManager().OpenDexFilesFromOat(
1134 dex_location_.c_str(),
Jeff Hao0cb17282017-07-12 14:51:49 -07001135 Runtime::Current()->GetSystemClassLoader(),
Andreas Gampe98ea9d92018-10-19 14:06:15 -07001136 /*dex_elements=*/nullptr,
Mathieu Chartiere58991b2015-10-13 07:59:34 -07001137 &oat_file,
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001138 &error_msgs);
Andreas Gampe9186ced2016-12-12 14:28:21 -08001139 CHECK(!dex_files.empty()) << android::base::Join(error_msgs, '\n');
Nicolas Geoffray3d8a78a2018-08-29 21:10:16 +00001140 if (dex_files[0]->GetOatDexFile() != nullptr) {
1141 loaded_oat_file_ = dex_files[0]->GetOatDexFile()->GetOatFile();
1142 }
Mathieu Chartiere58991b2015-10-13 07:59:34 -07001143 CHECK_EQ(loaded_oat_file_, oat_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001144 }
1145
1146 const OatFile* GetLoadedOatFile() const {
1147 return loaded_oat_file_;
1148 }
1149
1150 private:
Vladimir Markob9c29f62019-03-20 14:22:51 +00001151 OatFileAssistantTest& test_;
Richard Uhler66d874d2015-01-15 09:37:19 -08001152 std::string dex_location_;
1153 std::string oat_location_;
Nicolas Geoffray1e76d7a2018-09-03 13:23:34 +01001154 Mutex* lock_;
Richard Uhler66d874d2015-01-15 09:37:19 -08001155 const OatFile* loaded_oat_file_;
1156};
1157
Nicolas Geoffray3d8a78a2018-08-29 21:10:16 +00001158// Test the case where dex2oat invocations race with multiple processes trying to
1159// load the oat file.
Richard Uhler66d874d2015-01-15 09:37:19 -08001160TEST_F(OatFileAssistantTest, RaceToGenerate) {
1161 std::string dex_location = GetScratchDir() + "/RaceToGenerate.jar";
Richard Uhler63434112015-03-16 14:32:16 -07001162 std::string oat_location = GetOdexDir() + "/RaceToGenerate.oat";
Richard Uhler66d874d2015-01-15 09:37:19 -08001163
Jeff Hao0cb17282017-07-12 14:51:49 -07001164 // Start the runtime to initialize the system's class loader.
1165 Thread::Current()->TransitionFromSuspendedToRunnable();
1166 runtime_->Start();
1167
Richard Uhler66d874d2015-01-15 09:37:19 -08001168 // We use the lib core dex file, because it's large, and hopefully should
1169 // take a while to generate.
Narayan Kamathd1ef4362015-11-12 11:49:06 +00001170 Copy(GetLibCoreDexFileNames()[0], dex_location);
Richard Uhler66d874d2015-01-15 09:37:19 -08001171
Nicolas Geoffray3d8a78a2018-08-29 21:10:16 +00001172 const size_t kNumThreads = 32;
Richard Uhler66d874d2015-01-15 09:37:19 -08001173 Thread* self = Thread::Current();
1174 ThreadPool thread_pool("Oat file assistant test thread pool", kNumThreads);
1175 std::vector<std::unique_ptr<RaceGenerateTask>> tasks;
Nicolas Geoffray1e76d7a2018-09-03 13:23:34 +01001176 Mutex lock("RaceToGenerate");
Nicolas Geoffray3d8a78a2018-08-29 21:10:16 +00001177 for (size_t i = 0; i < kNumThreads; i++) {
Vladimir Markob9c29f62019-03-20 14:22:51 +00001178 std::unique_ptr<RaceGenerateTask> task(
1179 new RaceGenerateTask(*this, dex_location, oat_location, &lock));
Richard Uhler66d874d2015-01-15 09:37:19 -08001180 thread_pool.AddTask(self, task.get());
1181 tasks.push_back(std::move(task));
1182 }
1183 thread_pool.StartWorkers(self);
Andreas Gampe98ea9d92018-10-19 14:06:15 -07001184 thread_pool.Wait(self, /* do_work= */ true, /* may_hold_locks= */ false);
Richard Uhler66d874d2015-01-15 09:37:19 -08001185
Nicolas Geoffray3d8a78a2018-08-29 21:10:16 +00001186 // Verify that tasks which got an oat file got a unique one.
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001187 std::set<const OatFile*> oat_files;
Richard Uhler66d874d2015-01-15 09:37:19 -08001188 for (auto& task : tasks) {
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001189 const OatFile* oat_file = task->GetLoadedOatFile();
Nicolas Geoffray3d8a78a2018-08-29 21:10:16 +00001190 if (oat_file != nullptr) {
1191 EXPECT_TRUE(oat_files.find(oat_file) == oat_files.end());
1192 oat_files.insert(oat_file);
1193 }
Richard Uhler66d874d2015-01-15 09:37:19 -08001194 }
1195}
1196
Nicolas Geoffray3d8a78a2018-08-29 21:10:16 +00001197// Case: We have a DEX file and an ODEX file, and no OAT file,
Vladimir Markoe0669322018-09-03 15:44:54 +01001198// Expect: We should load the odex file executable.
Nicolas Geoffray3d8a78a2018-08-29 21:10:16 +00001199TEST_F(DexoptTest, LoadDexOdexNoOat) {
Richard Uhler66d874d2015-01-15 09:37:19 -08001200 std::string dex_location = GetScratchDir() + "/LoadDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -07001201 std::string odex_location = GetOdexDir() + "/LoadDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -08001202
1203 // Create the dex and odex files
1204 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +00001205 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -08001206
1207 // Load the oat using an executable oat file assistant.
Richard Uhlerd1472a22016-04-15 15:18:56 -07001208 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001209
1210 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1211 ASSERT_TRUE(oat_file.get() != nullptr);
Vladimir Markoe0669322018-09-03 15:44:54 +01001212 EXPECT_TRUE(oat_file->IsExecutable());
Richard Uhler66d874d2015-01-15 09:37:19 -08001213 std::vector<std::unique_ptr<const DexFile>> dex_files;
1214 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
1215 EXPECT_EQ(1u, dex_files.size());
1216}
1217
Nicolas Geoffray3d8a78a2018-08-29 21:10:16 +00001218// Case: We have a MultiDEX file and an ODEX file, and no OAT file.
Vladimir Markoe0669322018-09-03 15:44:54 +01001219// Expect: We should load the odex file executable.
Nicolas Geoffray3d8a78a2018-08-29 21:10:16 +00001220TEST_F(DexoptTest, LoadMultiDexOdexNoOat) {
Richard Uhler66d874d2015-01-15 09:37:19 -08001221 std::string dex_location = GetScratchDir() + "/LoadMultiDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -07001222 std::string odex_location = GetOdexDir() + "/LoadMultiDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -08001223
1224 // Create the dex and odex files
1225 Copy(GetMultiDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +00001226 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -08001227
1228 // Load the oat using an executable oat file assistant.
Richard Uhlerd1472a22016-04-15 15:18:56 -07001229 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001230
1231 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1232 ASSERT_TRUE(oat_file.get() != nullptr);
Vladimir Markoe0669322018-09-03 15:44:54 +01001233 EXPECT_TRUE(oat_file->IsExecutable());
Richard Uhler66d874d2015-01-15 09:37:19 -08001234 std::vector<std::unique_ptr<const DexFile>> dex_files;
1235 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
1236 EXPECT_EQ(2u, dex_files.size());
1237}
1238
Richard Uhlerb81881d2016-04-19 13:08:04 -07001239TEST(OatFileAssistantUtilsTest, DexLocationToOdexFilename) {
Richard Uhler66d874d2015-01-15 09:37:19 -08001240 std::string error_msg;
1241 std::string odex_file;
1242
Richard Uhlerb81881d2016-04-19 13:08:04 -07001243 EXPECT_TRUE(OatFileAssistant::DexLocationToOdexFilename(
Vladimir Marko33bff252017-11-01 14:35:42 +00001244 "/foo/bar/baz.jar", InstructionSet::kArm, &odex_file, &error_msg)) << error_msg;
Richard Uhler63434112015-03-16 14:32:16 -07001245 EXPECT_EQ("/foo/bar/oat/arm/baz.odex", odex_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001246
Richard Uhlerb81881d2016-04-19 13:08:04 -07001247 EXPECT_TRUE(OatFileAssistant::DexLocationToOdexFilename(
Vladimir Marko33bff252017-11-01 14:35:42 +00001248 "/foo/bar/baz.funnyext", InstructionSet::kArm, &odex_file, &error_msg)) << error_msg;
Richard Uhler63434112015-03-16 14:32:16 -07001249 EXPECT_EQ("/foo/bar/oat/arm/baz.odex", odex_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001250
Richard Uhlerb81881d2016-04-19 13:08:04 -07001251 EXPECT_FALSE(OatFileAssistant::DexLocationToOdexFilename(
Vladimir Marko33bff252017-11-01 14:35:42 +00001252 "nopath.jar", InstructionSet::kArm, &odex_file, &error_msg));
Richard Uhlerb81881d2016-04-19 13:08:04 -07001253 EXPECT_FALSE(OatFileAssistant::DexLocationToOdexFilename(
Vladimir Marko33bff252017-11-01 14:35:42 +00001254 "/foo/bar/baz_noext", InstructionSet::kArm, &odex_file, &error_msg));
Richard Uhler66d874d2015-01-15 09:37:19 -08001255}
1256
Richard Uhler23cedd22015-04-08 13:17:29 -07001257// Verify the dexopt status values from dalvik.system.DexFile
1258// match the OatFileAssistant::DexOptStatus values.
1259TEST_F(OatFileAssistantTest, DexOptStatusValues) {
Richard Uhler7225a8d2016-11-22 10:12:03 +00001260 std::pair<OatFileAssistant::DexOptNeeded, const char*> mapping[] = {
1261 {OatFileAssistant::kNoDexOptNeeded, "NO_DEXOPT_NEEDED"},
1262 {OatFileAssistant::kDex2OatFromScratch, "DEX2OAT_FROM_SCRATCH"},
1263 {OatFileAssistant::kDex2OatForBootImage, "DEX2OAT_FOR_BOOT_IMAGE"},
1264 {OatFileAssistant::kDex2OatForFilter, "DEX2OAT_FOR_FILTER"},
Richard Uhler7225a8d2016-11-22 10:12:03 +00001265 };
1266
Richard Uhler23cedd22015-04-08 13:17:29 -07001267 ScopedObjectAccess soa(Thread::Current());
1268 StackHandleScope<1> hs(soa.Self());
1269 ClassLinker* linker = Runtime::Current()->GetClassLinker();
1270 Handle<mirror::Class> dexfile(
1271 hs.NewHandle(linker->FindSystemClass(soa.Self(), "Ldalvik/system/DexFile;")));
Andreas Gampefa4333d2017-02-14 11:10:34 -08001272 ASSERT_FALSE(dexfile == nullptr);
Richard Uhler23cedd22015-04-08 13:17:29 -07001273 linker->EnsureInitialized(soa.Self(), dexfile, true, true);
1274
Richard Uhler7225a8d2016-11-22 10:12:03 +00001275 for (std::pair<OatFileAssistant::DexOptNeeded, const char*> field : mapping) {
1276 ArtField* art_field = mirror::Class::FindStaticField(
Vladimir Marko19a4d372016-12-08 14:41:46 +00001277 soa.Self(), dexfile.Get(), field.second, "I");
Richard Uhler7225a8d2016-11-22 10:12:03 +00001278 ASSERT_FALSE(art_field == nullptr);
1279 EXPECT_EQ(art_field->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
1280 EXPECT_EQ(field.first, art_field->GetInt(dexfile.Get()));
1281 }
Richard Uhler23cedd22015-04-08 13:17:29 -07001282}
Richard Uhler66d874d2015-01-15 09:37:19 -08001283
Calin Juravle44e5efa2017-09-12 00:54:26 -07001284TEST_F(OatFileAssistantTest, GetDexOptNeededWithOutOfDateContext) {
1285 std::string dex_location = GetScratchDir() + "/TestDex.jar";
Calin Juravleaf322422020-02-11 13:45:53 -08001286 std::string odex_location = GetOdexDir() + "/TestDex.odex";
1287
Calin Juravle44e5efa2017-09-12 00:54:26 -07001288 std::string context_location = GetScratchDir() + "/ContextDex.jar";
1289 Copy(GetDexSrc1(), dex_location);
1290 Copy(GetDexSrc2(), context_location);
1291
Calin Juravle44e5efa2017-09-12 00:54:26 -07001292 std::string context_str = "PCL[" + context_location + "]";
Calin Juravleaf322422020-02-11 13:45:53 -08001293
Calin Juravle44e5efa2017-09-12 00:54:26 -07001294 std::unique_ptr<ClassLoaderContext> context = ClassLoaderContext::Create(context_str);
1295 ASSERT_TRUE(context != nullptr);
1296 ASSERT_TRUE(context->OpenDexFiles(kRuntimeISA, ""));
1297
Calin Juravleaf322422020-02-11 13:45:53 -08001298 std::string error_msg;
1299 std::vector<std::string> args;
1300 args.push_back("--dex-file=" + dex_location);
1301 args.push_back("--oat-file=" + odex_location);
1302 args.push_back("--class-loader-context=" + context_str);
1303 ASSERT_TRUE(Dex2Oat(args, &error_msg)) << error_msg;
1304
Calin Juravle44e5efa2017-09-12 00:54:26 -07001305 // Update the context by overriding the jar file.
1306 Copy(GetMultiDexSrc2(), context_location);
Calin Juravleaf322422020-02-11 13:45:53 -08001307
1308 {
Calin Juravle0a5cad32020-02-14 20:29:26 +00001309 std::unique_ptr<ClassLoaderContext> updated_context = ClassLoaderContext::Create(context_str);
1310 ASSERT_TRUE(updated_context != nullptr);
Calin Juravleaf322422020-02-11 13:45:53 -08001311 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
1312 // DexOptNeeded should advise compilation from scratch when the context changes.
1313 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Calin Juravle0a5cad32020-02-14 20:29:26 +00001314 GetDexOptNeeded(&oat_file_assistant,
Calin Juravleaf322422020-02-11 13:45:53 -08001315 CompilerFilter::kDefaultCompilerFilter,
Calin Juravle0a5cad32020-02-14 20:29:26 +00001316 updated_context));
Calin Juravleaf322422020-02-11 13:45:53 -08001317 }
1318 {
Calin Juravle0a5cad32020-02-14 20:29:26 +00001319 std::unique_ptr<ClassLoaderContext> updated_context = ClassLoaderContext::Create(context_str);
1320 ASSERT_TRUE(updated_context != nullptr);
Calin Juravleaf322422020-02-11 13:45:53 -08001321 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
1322 // Now check that DexOptNeeded does not advise compilation if we only extracted the file.
1323 args.push_back("--compiler-filter=extract");
1324 ASSERT_TRUE(Dex2Oat(args, &error_msg)) << error_msg;
1325 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Calin Juravle0a5cad32020-02-14 20:29:26 +00001326 GetDexOptNeeded(&oat_file_assistant,
Calin Juravleaf322422020-02-11 13:45:53 -08001327 CompilerFilter::kExtract,
Calin Juravle0a5cad32020-02-14 20:29:26 +00001328 updated_context));
Calin Juravleaf322422020-02-11 13:45:53 -08001329 }
Nicolas Geoffray29742602017-12-14 10:09:03 +00001330}
1331
Nicolas Geoffraye3e0f702019-03-12 07:02:02 +00001332// Test that GetLocation of a dex file is the same whether the dex
1333// filed is backed by an oat file or not.
1334TEST_F(OatFileAssistantTest, GetDexLocation) {
1335 std::string dex_location = GetScratchDir() + "/TestDex.jar";
1336 std::string oat_location = GetOdexDir() + "/TestDex.odex";
Eric Holkbc89ed42020-04-29 19:59:24 +00001337 std::string art_location = GetOdexDir() + "/TestDex.art";
Nicolas Geoffraye3e0f702019-03-12 07:02:02 +00001338
1339 // Start the runtime to initialize the system's class loader.
1340 Thread::Current()->TransitionFromSuspendedToRunnable();
1341 runtime_->Start();
1342
1343 Copy(GetDexSrc1(), dex_location);
1344
1345 std::vector<std::unique_ptr<const DexFile>> dex_files;
1346 std::vector<std::string> error_msgs;
1347 const OatFile* oat_file = nullptr;
1348
1349 dex_files = Runtime::Current()->GetOatFileManager().OpenDexFilesFromOat(
1350 dex_location.c_str(),
1351 Runtime::Current()->GetSystemClassLoader(),
1352 /*dex_elements=*/nullptr,
1353 &oat_file,
1354 &error_msgs);
Vladimir Markob7bf8432019-12-03 13:18:50 +00001355 ASSERT_EQ(dex_files.size(), 1u) << android::base::Join(error_msgs, "\n");
Nicolas Geoffraye3e0f702019-03-12 07:02:02 +00001356 EXPECT_EQ(oat_file, nullptr);
1357 std::string stored_dex_location = dex_files[0]->GetLocation();
1358 {
1359 // Create the oat file.
1360 std::vector<std::string> args;
1361 args.push_back("--dex-file=" + dex_location);
1362 args.push_back("--dex-location=TestDex.jar");
1363 args.push_back("--oat-file=" + oat_location);
Eric Holkbc89ed42020-04-29 19:59:24 +00001364 args.push_back("--app-image-file=" + art_location);
Nicolas Geoffraye3e0f702019-03-12 07:02:02 +00001365 std::string error_msg;
1366 ASSERT_TRUE(DexoptTest::Dex2Oat(args, &error_msg)) << error_msg;
1367 }
1368 dex_files = Runtime::Current()->GetOatFileManager().OpenDexFilesFromOat(
1369 dex_location.c_str(),
1370 Runtime::Current()->GetSystemClassLoader(),
1371 /*dex_elements=*/nullptr,
1372 &oat_file,
1373 &error_msgs);
Vladimir Markob7bf8432019-12-03 13:18:50 +00001374 ASSERT_EQ(dex_files.size(), 1u) << android::base::Join(error_msgs, "\n");
1375 ASSERT_NE(oat_file, nullptr);
Nicolas Geoffraye3e0f702019-03-12 07:02:02 +00001376 std::string oat_stored_dex_location = dex_files[0]->GetLocation();
1377 EXPECT_EQ(oat_stored_dex_location, stored_dex_location);
1378}
1379
1380// Test that a dex file on the platform location gets the right hiddenapi domain,
1381// regardless of whether it has a backing oat file.
1382TEST_F(OatFileAssistantTest, SystemFrameworkDir) {
1383 std::string filebase = "OatFileAssistantTestSystemFrameworkDir";
1384 std::string dex_location = GetAndroidRoot() + "/framework/" + filebase + ".jar";
1385 Copy(GetDexSrc1(), dex_location);
1386
1387 std::string odex_dir = GetAndroidRoot() + "/framework/oat/";
1388 mkdir(odex_dir.c_str(), 0700);
1389 odex_dir = odex_dir + std::string(GetInstructionSetString(kRuntimeISA));
1390 mkdir(odex_dir.c_str(), 0700);
1391 std::string oat_location = odex_dir + "/" + filebase + ".odex";
Eric Holkbc89ed42020-04-29 19:59:24 +00001392 std::string art_location = odex_dir + "/" + filebase + ".art";
Nicolas Geoffraye3e0f702019-03-12 07:02:02 +00001393 // Clean up in case previous run crashed.
1394 remove(oat_location.c_str());
1395
1396 // Start the runtime to initialize the system's class loader.
1397 Thread::Current()->TransitionFromSuspendedToRunnable();
1398 runtime_->Start();
1399
1400 std::vector<std::unique_ptr<const DexFile>> dex_files_first;
1401 std::vector<std::unique_ptr<const DexFile>> dex_files_second;
1402 std::vector<std::string> error_msgs;
1403 const OatFile* oat_file = nullptr;
1404
1405 dex_files_first = Runtime::Current()->GetOatFileManager().OpenDexFilesFromOat(
1406 dex_location.c_str(),
1407 Runtime::Current()->GetSystemClassLoader(),
1408 /*dex_elements=*/nullptr,
1409 &oat_file,
1410 &error_msgs);
Vladimir Markob7bf8432019-12-03 13:18:50 +00001411 ASSERT_EQ(dex_files_first.size(), 1u) << android::base::Join(error_msgs, "\n");
Nicolas Geoffraye3e0f702019-03-12 07:02:02 +00001412 EXPECT_EQ(oat_file, nullptr) << dex_location;
1413 EXPECT_EQ(dex_files_first[0]->GetOatDexFile(), nullptr);
1414
1415 // Register the dex file to get a domain.
1416 {
1417 ScopedObjectAccess soa(Thread::Current());
1418 Runtime::Current()->GetClassLinker()->RegisterDexFile(
1419 *dex_files_first[0],
1420 soa.Decode<mirror::ClassLoader>(Runtime::Current()->GetSystemClassLoader()));
1421 }
1422 std::string stored_dex_location = dex_files_first[0]->GetLocation();
1423 EXPECT_EQ(dex_files_first[0]->GetHiddenapiDomain(), hiddenapi::Domain::kPlatform);
1424 {
1425 // Create the oat file.
1426 std::vector<std::string> args;
1427 args.push_back("--dex-file=" + dex_location);
1428 args.push_back("--dex-location=" + filebase + ".jar");
1429 args.push_back("--oat-file=" + oat_location);
Eric Holkbc89ed42020-04-29 19:59:24 +00001430 args.push_back("--app-image-file=" + art_location);
Nicolas Geoffraye3e0f702019-03-12 07:02:02 +00001431 std::string error_msg;
1432 ASSERT_TRUE(DexoptTest::Dex2Oat(args, &error_msg)) << error_msg;
1433 }
1434 dex_files_second = Runtime::Current()->GetOatFileManager().OpenDexFilesFromOat(
1435 dex_location.c_str(),
1436 Runtime::Current()->GetSystemClassLoader(),
1437 /*dex_elements=*/nullptr,
1438 &oat_file,
1439 &error_msgs);
Vladimir Markob7bf8432019-12-03 13:18:50 +00001440 ASSERT_EQ(dex_files_second.size(), 1u) << android::base::Join(error_msgs, "\n");
1441 ASSERT_NE(oat_file, nullptr);
Nicolas Geoffraye3e0f702019-03-12 07:02:02 +00001442 EXPECT_NE(dex_files_second[0]->GetOatDexFile(), nullptr);
1443 EXPECT_NE(dex_files_second[0]->GetOatDexFile()->GetOatFile(), nullptr);
1444
1445 // Register the dex file to get a domain.
1446 {
1447 ScopedObjectAccess soa(Thread::Current());
1448 Runtime::Current()->GetClassLinker()->RegisterDexFile(
1449 *dex_files_second[0],
1450 soa.Decode<mirror::ClassLoader>(Runtime::Current()->GetSystemClassLoader()));
1451 }
1452 std::string oat_stored_dex_location = dex_files_second[0]->GetLocation();
1453 EXPECT_EQ(oat_stored_dex_location, stored_dex_location);
1454 EXPECT_EQ(dex_files_second[0]->GetHiddenapiDomain(), hiddenapi::Domain::kPlatform);
1455 EXPECT_EQ(0, remove(oat_location.c_str()));
1456}
1457
Eric Holkbc89ed42020-04-29 19:59:24 +00001458// Make sure OAT files that require app images are not loaded as executable.
1459TEST_F(OatFileAssistantTest, LoadOatNoArt) {
1460 std::string dex_location = GetScratchDir() + "/TestDex.jar";
1461 std::string odex_location = GetOdexDir() + "/TestDex.odex";
1462 std::string art_location = GetOdexDir() + "/TestDex.art";
1463 Copy(GetDexSrc1(), dex_location);
1464 GenerateOdexForTest(dex_location,
1465 odex_location,
1466 CompilerFilter::kSpeed,
1467 "install",
1468 {
1469 "--app-image-file=" + art_location,
1470 });
1471
1472 unlink(art_location.c_str());
1473
1474 std::vector<std::string> error_msgs;
1475 const OatFile* oat_file = nullptr;
1476
1477 // Start the runtime to initialize the system's class loader.
1478 Thread::Current()->TransitionFromSuspendedToRunnable();
1479 runtime_->Start();
1480
1481 const auto dex_files = Runtime::Current()->GetOatFileManager().OpenDexFilesFromOat(
1482 dex_location.c_str(),
1483 Runtime::Current()->GetSystemClassLoader(),
1484 /*dex_elements=*/nullptr,
1485 &oat_file,
1486 &error_msgs);
1487
1488 EXPECT_FALSE(dex_files.empty());
Nicolas Geoffray90a18cf2020-06-25 15:12:59 +01001489 ASSERT_NE(oat_file, nullptr);
Eric Holkbc89ed42020-04-29 19:59:24 +00001490 EXPECT_FALSE(oat_file->IsExecutable());
1491}
1492
Richard Uhler66d874d2015-01-15 09:37:19 -08001493// TODO: More Tests:
1494// * Test class linker falls back to unquickened dex for DexNoOat
1495// * Test class linker falls back to unquickened dex for MultiDexNoOat
Richard Uhler66d874d2015-01-15 09:37:19 -08001496// * Test using secondary isa
Richard Uhler66d874d2015-01-15 09:37:19 -08001497// * Test for status of oat while oat is being generated (how?)
1498// * Test case where 32 and 64 bit boot class paths differ,
1499// and we ask IsInBootClassPath for a class in exactly one of the 32 or
1500// 64 bit boot class paths.
1501// * Test unexpected scenarios (?):
1502// - Dex is stripped, don't have odex.
1503// - Oat file corrupted after status check, before reload unexecutable
1504// because it's unrelocated and no dex2oat
Richard Uhler66d874d2015-01-15 09:37:19 -08001505} // namespace art