blob: 901bbc4be20a6aff6fb9bf9ae742026deb0fb3b6 [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 }
64};
Richard Uhler66d874d2015-01-15 09:37:19 -080065
Calin Juravle357c66d2017-05-04 01:57:17 +000066class ScopedNonWritable {
67 public:
68 explicit ScopedNonWritable(const std::string& dex_location) {
69 is_valid_ = false;
70 size_t pos = dex_location.rfind('/');
71 if (pos != std::string::npos) {
72 is_valid_ = true;
73 dex_parent_ = dex_location.substr(0, pos);
74 if (chmod(dex_parent_.c_str(), 0555) != 0) {
75 PLOG(ERROR) << "Could not change permissions on " << dex_parent_;
76 }
77 }
78 }
79
80 bool IsSuccessful() { return is_valid_ && (access(dex_parent_.c_str(), W_OK) != 0); }
81
82 ~ScopedNonWritable() {
83 if (is_valid_) {
84 if (chmod(dex_parent_.c_str(), 0777) != 0) {
85 PLOG(ERROR) << "Could not restore permissions on " << dex_parent_;
86 }
87 }
88 }
89
90 private:
91 std::string dex_parent_;
92 bool is_valid_;
93};
94
95static bool IsExecutedAsRoot() {
96 return geteuid() == 0;
97}
Calin Juravle36eb3132017-01-13 16:32:38 -080098
Nicolas Geoffray3d8a78a2018-08-29 21:10:16 +000099// Case: We have a MultiDEX file and up-to-date ODEX file for it with relative
100// encoded dex locations.
101// Expect: The oat file status is kNoDexOptNeeded.
102TEST_F(OatFileAssistantTest, RelativeEncodedDexLocation) {
103 std::string dex_location = GetScratchDir() + "/RelativeEncodedDexLocation.jar";
104 std::string odex_location = GetOdexDir() + "/RelativeEncodedDexLocation.odex";
105
106 // Create the dex file
107 Copy(GetMultiDexSrc1(), dex_location);
108
109 // Create the oat file with relative encoded dex location.
110 std::vector<std::string> args = {
111 "--dex-file=" + dex_location,
112 "--dex-location=" + std::string("RelativeEncodedDexLocation.jar"),
113 "--oat-file=" + odex_location,
114 "--compiler-filter=speed"
115 };
116
117 std::string error_msg;
118 ASSERT_TRUE(Dex2Oat(args, &error_msg)) << error_msg;
119
120 // Verify we can load both dex files.
121 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
122
123 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
124 ASSERT_TRUE(oat_file.get() != nullptr);
125 EXPECT_TRUE(oat_file->IsExecutable());
126 std::vector<std::unique_ptr<const DexFile>> dex_files;
127 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
128 EXPECT_EQ(2u, dex_files.size());
129}
130
131TEST_F(OatFileAssistantTest, MakeUpToDateWithContext) {
132 std::string dex_location = GetScratchDir() + "/TestDex.jar";
133 std::string odex_location = GetOdexDir() + "/TestDex.odex";
134 std::string context_location = GetScratchDir() + "/ContextDex.jar";
135 Copy(GetDexSrc1(), dex_location);
136 Copy(GetDexSrc2(), context_location);
137
138 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
139
140 std::string context_str = "PCL[" + context_location + "]";
141 std::unique_ptr<ClassLoaderContext> context = ClassLoaderContext::Create(context_str);
142 ASSERT_TRUE(context != nullptr);
143 ASSERT_TRUE(context->OpenDexFiles(kRuntimeISA, ""));
144
145 std::string error_msg;
146 std::vector<std::string> args;
147 args.push_back("--dex-file=" + dex_location);
148 args.push_back("--oat-file=" + odex_location);
149 args.push_back("--class-loader-context=" + context_str);
150 ASSERT_TRUE(Dex2Oat(args, &error_msg)) << error_msg;
151
152 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
153 EXPECT_NE(nullptr, oat_file.get());
154 EXPECT_EQ(context->EncodeContextForOatFile(""),
155 oat_file->GetOatHeader().GetStoreValueByKey(OatHeader::kClassPathKey));
156}
157
158TEST_F(OatFileAssistantTest, GetDexOptNeededWithUpToDateContextRelative) {
159 std::string dex_location = GetScratchDir() + "/TestDex.jar";
160 std::string odex_location = GetOdexDir() + "/TestDex.odex";
161 std::string context_location = GetScratchDir() + "/ContextDex.jar";
162 Copy(GetDexSrc1(), dex_location);
163 Copy(GetDexSrc2(), context_location);
164
165 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
166
167 std::string context_str = "PCL[" + context_location + "]";
168 std::unique_ptr<ClassLoaderContext> context = ClassLoaderContext::Create(context_str);
169 ASSERT_TRUE(context != nullptr);
170 ASSERT_TRUE(context->OpenDexFiles(kRuntimeISA, ""));
171
172 std::string error_msg;
173 std::vector<std::string> args;
174 args.push_back("--dex-file=" + dex_location);
175 args.push_back("--oat-file=" + odex_location);
176 args.push_back("--class-loader-context=" + context_str);
177 ASSERT_TRUE(Dex2Oat(args, &error_msg)) << error_msg;
178
179 // A relative context simulates a dependent split context.
180 std::unique_ptr<ClassLoaderContext> relative_context =
181 ClassLoaderContext::Create("PCL[ContextDex.jar]");
182 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
183 oat_file_assistant.GetDexOptNeeded(
184 CompilerFilter::kDefaultCompilerFilter,
185 /* downgrade */ false,
186 /* profile_changed */ false,
187 relative_context.get()));
188}
189
Richard Uhler66d874d2015-01-15 09:37:19 -0800190// Case: We have a DEX file, but no OAT file for it.
Richard Uhler95abd042015-03-24 09:51:28 -0700191// Expect: The status is kDex2OatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800192TEST_F(OatFileAssistantTest, DexNoOat) {
193 std::string dex_location = GetScratchDir() + "/DexNoOat.jar";
194 Copy(GetDexSrc1(), dex_location);
195
Richard Uhlerd1472a22016-04-15 15:18:56 -0700196 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800197
Richard Uhler7225a8d2016-11-22 10:12:03 +0000198 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100199 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000200 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100201 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000202 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000203 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeedProfile));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000204 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000205 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800206
207 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000208 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
209 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700210 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Calin Juravle5f9a8012018-02-12 20:27:46 -0800211
212 VerifyOptimizationStatus(dex_location, "run-from-apk", "unknown");
Richard Uhler66d874d2015-01-15 09:37:19 -0800213}
214
215// Case: We have no DEX file and no OAT file.
Richard Uhler9b994ea2015-06-24 08:44:19 -0700216// Expect: Status is kNoDexOptNeeded. Loading should fail, but not crash.
Richard Uhler66d874d2015-01-15 09:37:19 -0800217TEST_F(OatFileAssistantTest, NoDexNoOat) {
218 std::string dex_location = GetScratchDir() + "/NoDexNoOat.jar";
219
Richard Uhlerd1472a22016-04-15 15:18:56 -0700220 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800221
Andreas Gampe29d38e72016-03-23 15:31:51 +0000222 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
223 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700224 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
225
Richard Uhler9b994ea2015-06-24 08:44:19 -0700226 // Trying to get the best oat file should fail, but not crash.
Richard Uhler66d874d2015-01-15 09:37:19 -0800227 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
228 EXPECT_EQ(nullptr, oat_file.get());
229}
230
Calin Juravle357c66d2017-05-04 01:57:17 +0000231// Case: We have a DEX file and a PIC ODEX file, but no OAT file.
232// Expect: The status is kNoDexOptNeeded, because PIC needs no relocation.
233TEST_F(OatFileAssistantTest, OdexUpToDate) {
234 std::string dex_location = GetScratchDir() + "/OdexUpToDate.jar";
235 std::string odex_location = GetOdexDir() + "/OdexUpToDate.odex";
236 Copy(GetDexSrc1(), dex_location);
Calin Juravle5f9a8012018-02-12 20:27:46 -0800237 GeneratePicOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed, "install");
Calin Juravle357c66d2017-05-04 01:57:17 +0000238
239 // For the use of oat location by making the dex parent not writable.
240 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
241
242 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
243 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
244 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
245 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken));
246 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
247 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract));
248 EXPECT_EQ(-OatFileAssistant::kDex2OatForFilter,
249 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
250
251 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
252 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus());
253 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
254 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Calin Juravle5f9a8012018-02-12 20:27:46 -0800255
256 VerifyOptimizationStatus(dex_location, CompilerFilter::kSpeed, "install");
Calin Juravle357c66d2017-05-04 01:57:17 +0000257}
258
259// Case: We have a DEX file and a PIC ODEX file, but no OAT file. We load the dex
260// file via a symlink.
261// Expect: The status is kNoDexOptNeeded, because PIC needs no relocation.
262TEST_F(OatFileAssistantTest, OdexUpToDateSymLink) {
263 std::string scratch_dir = GetScratchDir();
264 std::string dex_location = GetScratchDir() + "/OdexUpToDate.jar";
265 std::string odex_location = GetOdexDir() + "/OdexUpToDate.odex";
266
267 Copy(GetDexSrc1(), dex_location);
268 GeneratePicOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
269
270 // Now replace the dex location with a symlink.
271 std::string link = scratch_dir + "/link";
272 ASSERT_EQ(0, symlink(scratch_dir.c_str(), link.c_str()));
273 dex_location = link + "/OdexUpToDate.jar";
274
275 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
276
277 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
278 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
279 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
280 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken));
281 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
282 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract));
283 EXPECT_EQ(-OatFileAssistant::kDex2OatForFilter,
284 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
285
286 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
287 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus());
288 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
289 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
290}
291
Richard Uhler66d874d2015-01-15 09:37:19 -0800292// Case: We have a DEX file and up-to-date OAT file for it.
Richard Uhler95abd042015-03-24 09:51:28 -0700293// Expect: The status is kNoDexOptNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800294TEST_F(OatFileAssistantTest, OatUpToDate) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000295 if (IsExecutedAsRoot()) {
296 // We cannot simulate non writable locations when executed as root: b/38000545.
297 LOG(ERROR) << "Test skipped because it's running as root";
298 return;
299 }
300
Richard Uhler66d874d2015-01-15 09:37:19 -0800301 std::string dex_location = GetScratchDir() + "/OatUpToDate.jar";
302 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000303 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800304
Calin Juravle357c66d2017-05-04 01:57:17 +0000305 // For the use of oat location by making the dex parent not writable.
306 ScopedNonWritable scoped_non_writable(dex_location);
307 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
308
309 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
310
311 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
312 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
313 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
314 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken));
315 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
316 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract));
317 EXPECT_EQ(OatFileAssistant::kDex2OatForFilter,
318 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
319
320 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
321 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
322 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus());
323 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Calin Juravle5f9a8012018-02-12 20:27:46 -0800324
325 VerifyOptimizationStatus(dex_location, CompilerFilter::kSpeed, "unknown");
Calin Juravle357c66d2017-05-04 01:57:17 +0000326}
327
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700328// Case: Passing valid file descriptors of updated odex/vdex filesalong with
329// the dex file.
330// Expect: The status is kNoDexOptNeeded.
331TEST_F(OatFileAssistantTest, GetDexOptNeededWithFd) {
332 std::string dex_location = GetScratchDir() + "/OatUpToDate.jar";
333 std::string odex_location = GetScratchDir() + "/OatUpToDate.odex";
334 std::string vdex_location = GetScratchDir() + "/OatUpToDate.vdex";
335
336 Copy(GetDexSrc1(), dex_location);
337 GenerateOatForTest(dex_location.c_str(),
338 odex_location.c_str(),
339 CompilerFilter::kSpeed,
340 true,
341 false,
342 false);
343
344 android::base::unique_fd odex_fd(open(odex_location.c_str(), O_RDONLY));
345 android::base::unique_fd vdex_fd(open(vdex_location.c_str(), O_RDONLY));
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700346 android::base::unique_fd zip_fd(open(dex_location.c_str(), O_RDONLY));
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700347
348 OatFileAssistant oat_file_assistant(dex_location.c_str(),
349 kRuntimeISA,
350 false,
Nicolas Geoffray29742602017-12-14 10:09:03 +0000351 false,
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700352 vdex_fd.get(),
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700353 odex_fd.get(),
354 zip_fd.get());
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700355 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
356 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
357 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
358 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken));
359 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
360 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract));
361 EXPECT_EQ(-OatFileAssistant::kDex2OatForFilter,
362 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
363
364 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
365 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus());
366 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
367 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
368}
369
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700370// Case: Passing invalid odex fd and valid vdex and zip fds.
371// Expect: The status should be kDex2OatForBootImage.
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700372TEST_F(OatFileAssistantTest, GetDexOptNeededWithInvalidOdexFd) {
373 std::string dex_location = GetScratchDir() + "/OatUpToDate.jar";
374 std::string odex_location = GetScratchDir() + "/OatUpToDate.odex";
375 std::string vdex_location = GetScratchDir() + "/OatUpToDate.vdex";
376
377 Copy(GetDexSrc1(), dex_location);
378 GenerateOatForTest(dex_location.c_str(),
379 odex_location.c_str(),
380 CompilerFilter::kSpeed,
381 true,
382 false,
383 false);
384
385 android::base::unique_fd vdex_fd(open(vdex_location.c_str(), O_RDONLY));
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700386 android::base::unique_fd zip_fd(open(dex_location.c_str(), O_RDONLY));
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700387
388 OatFileAssistant oat_file_assistant(dex_location.c_str(),
389 kRuntimeISA,
390 false,
Nicolas Geoffray29742602017-12-14 10:09:03 +0000391 false,
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700392 vdex_fd.get(),
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700393 -1 /* oat_fd */,
394 zip_fd.get());
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700395 EXPECT_EQ(-OatFileAssistant::kDex2OatForBootImage,
396 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700397 EXPECT_EQ(-OatFileAssistant::kDex2OatForBootImage,
398 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
399
400 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700401 EXPECT_EQ(OatFileAssistant::kOatBootImageOutOfDate, oat_file_assistant.OdexFileStatus());
402 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700403 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700404}
405
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700406// Case: Passing invalid vdex fd and valid odex and zip fds.
407// Expect: The status should be kDex2OatFromScratch.
408TEST_F(OatFileAssistantTest, GetDexOptNeededWithInvalidVdexFd) {
409 std::string dex_location = GetScratchDir() + "/OatUpToDate.jar";
410 std::string odex_location = GetScratchDir() + "/OatUpToDate.odex";
411
412 Copy(GetDexSrc1(), dex_location);
413 GenerateOatForTest(dex_location.c_str(),
414 odex_location.c_str(),
415 CompilerFilter::kSpeed,
416 true,
417 false,
418 false);
419
420 android::base::unique_fd odex_fd(open(odex_location.c_str(), O_RDONLY));
421 android::base::unique_fd zip_fd(open(dex_location.c_str(), O_RDONLY));
422
423 OatFileAssistant oat_file_assistant(dex_location.c_str(),
424 kRuntimeISA,
425 false,
Nicolas Geoffray29742602017-12-14 10:09:03 +0000426 false,
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700427 -1 /* vdex_fd */,
428 odex_fd.get(),
429 zip_fd.get());
430
431 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
432 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
433 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
434 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
435 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
436 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
437}
438
439// Case: Passing invalid vdex and odex fd with valid zip fd.
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700440// Expect: The status is kDex2oatFromScratch.
441TEST_F(OatFileAssistantTest, GetDexOptNeededWithInvalidOdexVdexFd) {
442 std::string dex_location = GetScratchDir() + "/OatUpToDate.jar";
443
444 Copy(GetDexSrc1(), dex_location);
445
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700446 android::base::unique_fd zip_fd(open(dex_location.c_str(), O_RDONLY));
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700447 OatFileAssistant oat_file_assistant(dex_location.c_str(),
448 kRuntimeISA,
449 false,
Nicolas Geoffray29742602017-12-14 10:09:03 +0000450 false,
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700451 -1 /* vdex_fd */,
452 -1 /* oat_fd */,
453 zip_fd);
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700454 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
455 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
456 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
457 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
458}
459
Richard Uhler2f27abd2017-01-31 14:02:34 +0000460// Case: We have a DEX file and up-to-date (ODEX) VDEX file for it, but no
461// ODEX file.
462TEST_F(OatFileAssistantTest, VdexUpToDateNoOdex) {
Richard Uhler2f27abd2017-01-31 14:02:34 +0000463 std::string dex_location = GetScratchDir() + "/VdexUpToDateNoOdex.jar";
Calin Juravle357c66d2017-05-04 01:57:17 +0000464 std::string odex_location = GetOdexDir() + "/VdexUpToDateNoOdex.oat";
Richard Uhler2f27abd2017-01-31 14:02:34 +0000465
Richard Uhler9a37efc2016-08-05 16:32:55 -0700466 Copy(GetDexSrc1(), dex_location);
467
Richard Uhler2f27abd2017-01-31 14:02:34 +0000468 // Generating and deleting the oat file should have the side effect of
469 // creating an up-to-date vdex file.
Calin Juravle357c66d2017-05-04 01:57:17 +0000470 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
471 ASSERT_EQ(0, unlink(odex_location.c_str()));
Richard Uhler2f27abd2017-01-31 14:02:34 +0000472
Calin Juravle357c66d2017-05-04 01:57:17 +0000473 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000474
475 // Even though the vdex file is up to date, because we don't have the oat
476 // file, we can't know that the vdex depends on the boot image and is up to
477 // date with respect to the boot image. Instead we must assume the vdex file
478 // depends on the boot image and is out of date with respect to the boot
479 // image.
480 EXPECT_EQ(-OatFileAssistant::kDex2OatForBootImage,
481 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
482
483 // Make sure we don't crash in this case when we dump the status. We don't
484 // care what the actual dumped value is.
485 oat_file_assistant.GetStatusDump();
Calin Juravle5f9a8012018-02-12 20:27:46 -0800486
487 VerifyOptimizationStatus(dex_location, "run-from-apk", "unknown");
Richard Uhler2f27abd2017-01-31 14:02:34 +0000488}
489
490// Case: We have a DEX file and empty VDEX and ODEX files.
491TEST_F(OatFileAssistantTest, EmptyVdexOdex) {
492 std::string dex_location = GetScratchDir() + "/EmptyVdexOdex.jar";
493 std::string odex_location = GetOdexDir() + "/EmptyVdexOdex.oat";
494 std::string vdex_location = GetOdexDir() + "/EmptyVdexOdex.vdex";
495
496 Copy(GetDexSrc1(), dex_location);
Richard Uhler5cd59292017-02-01 12:54:23 +0000497 ScratchFile vdex_file(vdex_location.c_str());
498 ScratchFile odex_file(odex_location.c_str());
Richard Uhler2f27abd2017-01-31 14:02:34 +0000499
500 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
501 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
502 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
503}
504
505// Case: We have a DEX file and up-to-date (OAT) VDEX file for it, but no OAT
506// file.
507TEST_F(OatFileAssistantTest, VdexUpToDateNoOat) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000508 if (IsExecutedAsRoot()) {
509 // We cannot simulate non writable locations when executed as root: b/38000545.
510 LOG(ERROR) << "Test skipped because it's running as root";
511 return;
512 }
Richard Uhler2f27abd2017-01-31 14:02:34 +0000513
514 std::string dex_location = GetScratchDir() + "/VdexUpToDateNoOat.jar";
515 std::string oat_location;
516 std::string error_msg;
517 ASSERT_TRUE(OatFileAssistant::DexLocationToOatFilename(
518 dex_location, kRuntimeISA, &oat_location, &error_msg)) << error_msg;
519
520 Copy(GetDexSrc1(), dex_location);
521 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
522 ASSERT_EQ(0, unlink(oat_location.c_str()));
523
Calin Juravle357c66d2017-05-04 01:57:17 +0000524 ScopedNonWritable scoped_non_writable(dex_location);
525 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
Richard Uhler9a37efc2016-08-05 16:32:55 -0700526 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
527
Richard Uhler2f27abd2017-01-31 14:02:34 +0000528 // Even though the vdex file is up to date, because we don't have the oat
529 // file, we can't know that the vdex depends on the boot image and is up to
530 // date with respect to the boot image. Instead we must assume the vdex file
531 // depends on the boot image and is out of date with respect to the boot
532 // image.
533 EXPECT_EQ(OatFileAssistant::kDex2OatForBootImage,
Richard Uhler9a37efc2016-08-05 16:32:55 -0700534 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler9a37efc2016-08-05 16:32:55 -0700535}
536
Andreas Gampe29d38e72016-03-23 15:31:51 +0000537// Case: We have a DEX file and speed-profile OAT file for it.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700538// Expect: The status is kNoDexOptNeeded if the profile hasn't changed, but
539// kDex2Oat if the profile has changed.
Andreas Gampe29d38e72016-03-23 15:31:51 +0000540TEST_F(OatFileAssistantTest, ProfileOatUpToDate) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000541 if (IsExecutedAsRoot()) {
542 // We cannot simulate non writable locations when executed as root: b/38000545.
543 LOG(ERROR) << "Test skipped because it's running as root";
544 return;
545 }
546
Andreas Gampe29d38e72016-03-23 15:31:51 +0000547 std::string dex_location = GetScratchDir() + "/ProfileOatUpToDate.jar";
548 Copy(GetDexSrc1(), dex_location);
549 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeedProfile);
550
Calin Juravle357c66d2017-05-04 01:57:17 +0000551 ScopedNonWritable scoped_non_writable(dex_location);
552 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
553
Richard Uhlerd1472a22016-04-15 15:18:56 -0700554 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000555
556 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700557 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeedProfile, false));
Andreas Gampe29d38e72016-03-23 15:31:51 +0000558 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100559 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken, false));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000560 EXPECT_EQ(OatFileAssistant::kDex2OatForFilter,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700561 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeedProfile, true));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000562 EXPECT_EQ(OatFileAssistant::kDex2OatForFilter,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100563 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken, true));
Andreas Gampe29d38e72016-03-23 15:31:51 +0000564
565 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000566 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
Andreas Gampe29d38e72016-03-23 15:31:51 +0000567 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus());
568 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
569}
570
Richard Uhler66d874d2015-01-15 09:37:19 -0800571// Case: We have a MultiDEX file and up-to-date OAT file for it.
Richard Uhler95abd042015-03-24 09:51:28 -0700572// Expect: The status is kNoDexOptNeeded and we load all dex files.
Richard Uhler66d874d2015-01-15 09:37:19 -0800573TEST_F(OatFileAssistantTest, MultiDexOatUpToDate) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000574 if (IsExecutedAsRoot()) {
575 // We cannot simulate non writable locations when executed as root: b/38000545.
576 LOG(ERROR) << "Test skipped because it's running as root";
577 return;
578 }
579
Richard Uhler66d874d2015-01-15 09:37:19 -0800580 std::string dex_location = GetScratchDir() + "/MultiDexOatUpToDate.jar";
581 Copy(GetMultiDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000582 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800583
Calin Juravle357c66d2017-05-04 01:57:17 +0000584 ScopedNonWritable scoped_non_writable(dex_location);
585 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
586
Richard Uhlerd1472a22016-04-15 15:18:56 -0700587 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000588 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700589 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed, false));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700590 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler95abd042015-03-24 09:51:28 -0700591
592 // Verify we can load both dex files.
Richard Uhlere5fed032015-03-18 08:21:11 -0700593 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
Richard Uhler66d874d2015-01-15 09:37:19 -0800594 ASSERT_TRUE(oat_file.get() != nullptr);
595 EXPECT_TRUE(oat_file->IsExecutable());
596 std::vector<std::unique_ptr<const DexFile>> dex_files;
Richard Uhlere5fed032015-03-18 08:21:11 -0700597 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
598 EXPECT_EQ(2u, dex_files.size());
599}
600
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000601// Case: We have a MultiDEX file where the non-main multdex entry is out of date.
Richard Uhler67ff7d12015-05-14 13:21:13 -0700602// Expect: The status is kDex2OatNeeded.
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000603TEST_F(OatFileAssistantTest, MultiDexNonMainOutOfDate) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000604 if (IsExecutedAsRoot()) {
605 // We cannot simulate non writable locations when executed as root: b/38000545.
606 LOG(ERROR) << "Test skipped because it's running as root";
607 return;
608 }
609
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000610 std::string dex_location = GetScratchDir() + "/MultiDexNonMainOutOfDate.jar";
Richard Uhler67ff7d12015-05-14 13:21:13 -0700611
612 // Compile code for GetMultiDexSrc1.
613 Copy(GetMultiDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000614 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler67ff7d12015-05-14 13:21:13 -0700615
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000616 // Now overwrite the dex file with GetMultiDexSrc2 so the non-main checksum
Richard Uhler67ff7d12015-05-14 13:21:13 -0700617 // is out of date.
618 Copy(GetMultiDexSrc2(), dex_location);
619
Calin Juravle357c66d2017-05-04 01:57:17 +0000620 ScopedNonWritable scoped_non_writable(dex_location);
621 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
622
Richard Uhlerd1472a22016-04-15 15:18:56 -0700623 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler7225a8d2016-11-22 10:12:03 +0000624 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700625 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed, false));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700626 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler67ff7d12015-05-14 13:21:13 -0700627}
628
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000629// Case: We have a stripped MultiDEX file where the non-main multidex entry is
630// out of date with respect to the odex file.
631TEST_F(OatFileAssistantTest, StrippedMultiDexNonMainOutOfDate) {
632 std::string dex_location = GetScratchDir() + "/StrippedMultiDexNonMainOutOfDate.jar";
633 std::string odex_location = GetOdexDir() + "/StrippedMultiDexNonMainOutOfDate.odex";
634
635 // Compile the oat from GetMultiDexSrc1.
636 Copy(GetMultiDexSrc1(), dex_location);
637 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
638
639 // Compile the odex from GetMultiDexSrc2, which has a different non-main
640 // dex checksum.
641 Copy(GetMultiDexSrc2(), dex_location);
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100642 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kQuicken);
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000643
644 // Strip the dex file.
645 Copy(GetStrippedDexSrc1(), dex_location);
646
647 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, /*load_executable*/false);
648
649 // Because the dex file is stripped, the odex file is considered the source
650 // of truth for the dex checksums. The oat file should be considered
651 // unusable.
652 std::unique_ptr<OatFile> best_file = oat_file_assistant.GetBestOatFile();
653 ASSERT_TRUE(best_file.get() != nullptr);
654 EXPECT_EQ(best_file->GetLocation(), odex_location);
655 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
656 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus());
657 EXPECT_EQ(OatFileAssistant::kOatDexOutOfDate, oat_file_assistant.OatFileStatus());
658}
659
Richard Uhler03bc6592016-11-22 09:42:04 +0000660// Case: We have a DEX file and an OAT file out of date with respect to the
661// dex checksum.
662TEST_F(OatFileAssistantTest, OatDexOutOfDate) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000663 if (IsExecutedAsRoot()) {
664 // We cannot simulate non writable locations when executed as root: b/38000545.
665 LOG(ERROR) << "Test skipped because it's running as root";
666 return;
667 }
668
Richard Uhler03bc6592016-11-22 09:42:04 +0000669 std::string dex_location = GetScratchDir() + "/OatDexOutOfDate.jar";
Richard Uhler66d874d2015-01-15 09:37:19 -0800670
671 // We create a dex, generate an oat for it, then overwrite the dex with a
672 // different dex to make the oat out of date.
673 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000674 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800675 Copy(GetDexSrc2(), dex_location);
676
Calin Juravle357c66d2017-05-04 01:57:17 +0000677 ScopedNonWritable scoped_non_writable(dex_location);
678 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
679
Richard Uhlerd1472a22016-04-15 15:18:56 -0700680 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler7225a8d2016-11-22 10:12:03 +0000681 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100682 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000683 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000684 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800685
686 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000687 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
688 EXPECT_EQ(OatFileAssistant::kOatDexOutOfDate, oat_file_assistant.OatFileStatus());
689 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
690}
691
Richard Uhler2f27abd2017-01-31 14:02:34 +0000692// Case: We have a DEX file and an (ODEX) VDEX file out of date with respect
693// to the dex checksum, but no ODEX file.
694TEST_F(OatFileAssistantTest, VdexDexOutOfDate) {
Richard Uhler2f27abd2017-01-31 14:02:34 +0000695 std::string dex_location = GetScratchDir() + "/VdexDexOutOfDate.jar";
Calin Juravle357c66d2017-05-04 01:57:17 +0000696 std::string odex_location = GetOdexDir() + "/VdexDexOutOfDate.oat";
Richard Uhler2f27abd2017-01-31 14:02:34 +0000697
698 Copy(GetDexSrc1(), dex_location);
Calin Juravle357c66d2017-05-04 01:57:17 +0000699 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
700 ASSERT_EQ(0, unlink(odex_location.c_str()));
Richard Uhler2f27abd2017-01-31 14:02:34 +0000701 Copy(GetDexSrc2(), dex_location);
702
Calin Juravle357c66d2017-05-04 01:57:17 +0000703 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000704
705 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
706 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
707}
708
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000709// Case: We have a MultiDEX (ODEX) VDEX file where the non-main multidex entry
710// is out of date and there is no corresponding ODEX file.
711TEST_F(OatFileAssistantTest, VdexMultiDexNonMainOutOfDate) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000712 std::string dex_location = GetScratchDir() + "/VdexMultiDexNonMainOutOfDate.jar";
Calin Juravle357c66d2017-05-04 01:57:17 +0000713 std::string odex_location = GetOdexDir() + "/VdexMultiDexNonMainOutOfDate.odex";
Richard Uhler2f27abd2017-01-31 14:02:34 +0000714
715 Copy(GetMultiDexSrc1(), dex_location);
Calin Juravle357c66d2017-05-04 01:57:17 +0000716 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
717 ASSERT_EQ(0, unlink(odex_location.c_str()));
Richard Uhler2f27abd2017-01-31 14:02:34 +0000718 Copy(GetMultiDexSrc2(), dex_location);
719
Calin Juravle357c66d2017-05-04 01:57:17 +0000720 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000721
722 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
723 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
724}
725
Richard Uhler03bc6592016-11-22 09:42:04 +0000726// Case: We have a DEX file and an OAT file out of date with respect to the
727// boot image.
728TEST_F(OatFileAssistantTest, OatImageOutOfDate) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000729 if (IsExecutedAsRoot()) {
730 // We cannot simulate non writable locations when executed as root: b/38000545.
731 LOG(ERROR) << "Test skipped because it's running as root";
732 return;
733 }
734
Richard Uhler03bc6592016-11-22 09:42:04 +0000735 std::string dex_location = GetScratchDir() + "/OatImageOutOfDate.jar";
736
737 Copy(GetDexSrc1(), dex_location);
738 GenerateOatForTest(dex_location.c_str(),
739 CompilerFilter::kSpeed,
740 /*relocate*/true,
741 /*pic*/false,
Richard Uhler03bc6592016-11-22 09:42:04 +0000742 /*with_alternate_image*/true);
743
Calin Juravle357c66d2017-05-04 01:57:17 +0000744 ScopedNonWritable scoped_non_writable(dex_location);
745 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
746
Richard Uhler03bc6592016-11-22 09:42:04 +0000747 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler7225a8d2016-11-22 10:12:03 +0000748 EXPECT_EQ(OatFileAssistant::kDex2OatForBootImage,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100749 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000750 EXPECT_EQ(OatFileAssistant::kDex2OatForBootImage,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100751 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000752 EXPECT_EQ(OatFileAssistant::kDex2OatForBootImage,
Richard Uhler03bc6592016-11-22 09:42:04 +0000753 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
754
755 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
756 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
757 EXPECT_EQ(OatFileAssistant::kOatBootImageOutOfDate, oat_file_assistant.OatFileStatus());
758 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
759}
760
761// Case: We have a DEX file and a verify-at-runtime OAT file out of date with
762// respect to the boot image.
763// It shouldn't matter that the OAT file is out of date, because it is
764// verify-at-runtime.
765TEST_F(OatFileAssistantTest, OatVerifyAtRuntimeImageOutOfDate) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000766 if (IsExecutedAsRoot()) {
767 // We cannot simulate non writable locations when executed as root: b/38000545.
768 LOG(ERROR) << "Test skipped because it's running as root";
769 return;
770 }
771
Richard Uhler03bc6592016-11-22 09:42:04 +0000772 std::string dex_location = GetScratchDir() + "/OatVerifyAtRuntimeImageOutOfDate.jar";
773
774 Copy(GetDexSrc1(), dex_location);
775 GenerateOatForTest(dex_location.c_str(),
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100776 CompilerFilter::kExtract,
Richard Uhler03bc6592016-11-22 09:42:04 +0000777 /*relocate*/true,
778 /*pic*/false,
Richard Uhler03bc6592016-11-22 09:42:04 +0000779 /*with_alternate_image*/true);
780
Calin Juravle357c66d2017-05-04 01:57:17 +0000781 ScopedNonWritable scoped_non_writable(dex_location);
782 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
783
Richard Uhler03bc6592016-11-22 09:42:04 +0000784 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
785 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100786 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000787 EXPECT_EQ(OatFileAssistant::kDex2OatForFilter,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100788 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken));
Richard Uhler03bc6592016-11-22 09:42:04 +0000789
790 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
791 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
792 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700793 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800794}
795
796// Case: We have a DEX file and an ODEX file, but no OAT file.
Richard Uhler66d874d2015-01-15 09:37:19 -0800797TEST_F(OatFileAssistantTest, DexOdexNoOat) {
798 std::string dex_location = GetScratchDir() + "/DexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700799 std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800800
801 // Create the dex and odex files
802 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000803 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800804
805 // Verify the status.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700806 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800807
Andreas Gampe29d38e72016-03-23 15:31:51 +0000808 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100809 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract));
Richard Uhler5923b522016-12-08 09:48:01 +0000810 EXPECT_EQ(-OatFileAssistant::kDex2OatForRelocation,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000811 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800812
813 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000814 EXPECT_EQ(OatFileAssistant::kOatRelocationOutOfDate, oat_file_assistant.OdexFileStatus());
815 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700816 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler5f946da2015-07-17 12:28:32 -0700817
818 // We should still be able to get the non-executable odex file to run from.
819 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
820 ASSERT_TRUE(oat_file.get() != nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800821}
822
Richard Uhler5923b522016-12-08 09:48:01 +0000823// Case: We have a stripped DEX file and a PIC ODEX file, but no OAT file.
Richard Uhler66d874d2015-01-15 09:37:19 -0800824TEST_F(OatFileAssistantTest, StrippedDexOdexNoOat) {
825 std::string dex_location = GetScratchDir() + "/StrippedDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700826 std::string odex_location = GetOdexDir() + "/StrippedDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800827
828 // Create the dex and odex files
829 Copy(GetDexSrc1(), dex_location);
Richard Uhler5923b522016-12-08 09:48:01 +0000830 GeneratePicOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800831
832 // Strip the dex file
833 Copy(GetStrippedDexSrc1(), dex_location);
834
835 // Verify the status.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700836 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800837
Richard Uhler5923b522016-12-08 09:48:01 +0000838 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000839 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800840
841 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler5923b522016-12-08 09:48:01 +0000842 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus());
Richard Uhler03bc6592016-11-22 09:42:04 +0000843 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700844 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800845
Richard Uhler66d874d2015-01-15 09:37:19 -0800846 // Verify we can load the dex files from it.
847 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
848 ASSERT_TRUE(oat_file.get() != nullptr);
849 EXPECT_TRUE(oat_file->IsExecutable());
850 std::vector<std::unique_ptr<const DexFile>> dex_files;
851 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
852 EXPECT_EQ(1u, dex_files.size());
853}
854
Richard Uhler5923b522016-12-08 09:48:01 +0000855// 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 -0800856TEST_F(OatFileAssistantTest, StrippedDexOdexOat) {
857 std::string dex_location = GetScratchDir() + "/StrippedDexOdexOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700858 std::string odex_location = GetOdexDir() + "/StrippedDexOdexOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800859
860 // Create the oat file from a different dex file so it looks out of date.
861 Copy(GetDexSrc2(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000862 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800863
864 // Create the odex file
865 Copy(GetDexSrc1(), dex_location);
Richard Uhler5923b522016-12-08 09:48:01 +0000866 GeneratePicOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800867
868 // Strip the dex file.
869 Copy(GetStrippedDexSrc1(), dex_location);
870
871 // Verify the status.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700872 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800873
Andreas Gampe29d38e72016-03-23 15:31:51 +0000874 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100875 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract));
Andreas Gampe29d38e72016-03-23 15:31:51 +0000876 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
877 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Nicolas Geoffray08e9eed2017-04-25 17:36:51 +0100878 EXPECT_EQ(-OatFileAssistant::kDex2OatForFilter, // Compiling from the .vdex file
Andreas Gampe29d38e72016-03-23 15:31:51 +0000879 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
Richard Uhler66d874d2015-01-15 09:37:19 -0800880
881 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler5923b522016-12-08 09:48:01 +0000882 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus());
883 EXPECT_EQ(OatFileAssistant::kOatDexOutOfDate, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700884 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800885
886 // Verify we can load the dex files from it.
887 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
888 ASSERT_TRUE(oat_file.get() != nullptr);
889 EXPECT_TRUE(oat_file->IsExecutable());
890 std::vector<std::unique_ptr<const DexFile>> dex_files;
891 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
892 EXPECT_EQ(1u, dex_files.size());
893}
894
Richard Uhler9b994ea2015-06-24 08:44:19 -0700895// Case: We have a stripped (or resource-only) DEX file, no ODEX file and no
896// OAT file. Expect: The status is kNoDexOptNeeded.
897TEST_F(OatFileAssistantTest, ResourceOnlyDex) {
898 std::string dex_location = GetScratchDir() + "/ResourceOnlyDex.jar";
899
900 Copy(GetStrippedDexSrc1(), dex_location);
901
902 // Verify the status.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700903 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler9b994ea2015-06-24 08:44:19 -0700904
Andreas Gampe29d38e72016-03-23 15:31:51 +0000905 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
906 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
907 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100908 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract));
Andreas Gampe29d38e72016-03-23 15:31:51 +0000909 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100910 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700911
912 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000913 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
914 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700915 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
916
Andreas Gampe29d38e72016-03-23 15:31:51 +0000917 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
918 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700919
920 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000921 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
922 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700923 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
924}
925
Richard Uhler66d874d2015-01-15 09:37:19 -0800926// Case: We have a DEX file, an ODEX file and an OAT file, where the ODEX and
927// OAT files both have patch delta of 0.
Richard Uhler5923b522016-12-08 09:48:01 +0000928// Expect: It shouldn't crash.
Richard Uhler66d874d2015-01-15 09:37:19 -0800929TEST_F(OatFileAssistantTest, OdexOatOverlap) {
930 std::string dex_location = GetScratchDir() + "/OdexOatOverlap.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700931 std::string odex_location = GetOdexDir() + "/OdexOatOverlap.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800932
Calin Juravle357c66d2017-05-04 01:57:17 +0000933 // Create the dex, the odex and the oat files.
Richard Uhler66d874d2015-01-15 09:37:19 -0800934 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000935 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Calin Juravle357c66d2017-05-04 01:57:17 +0000936 GenerateOatForTest(dex_location.c_str(),
937 CompilerFilter::kSpeed,
938 /*relocate*/false,
939 /*pic*/false,
940 /*with_alternate_image*/false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800941
942 // Verify things don't go bad.
Calin Juravle357c66d2017-05-04 01:57:17 +0000943 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800944
Calin Juravle357c66d2017-05-04 01:57:17 +0000945 // -kDex2OatForRelocation is expected rather than kDex2OatForRelocation
946 // based on the assumption that the odex location is more up-to-date than the oat
Richard Uhler70a84262016-11-08 16:51:51 +0000947 // location, even if they both need relocation.
Calin Juravle357c66d2017-05-04 01:57:17 +0000948 EXPECT_EQ(-OatFileAssistant::kDex2OatForRelocation,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000949 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800950
951 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000952 EXPECT_EQ(OatFileAssistant::kOatRelocationOutOfDate, oat_file_assistant.OdexFileStatus());
953 EXPECT_EQ(OatFileAssistant::kOatRelocationOutOfDate, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700954 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800955
956 // Things aren't relocated, so it should fall back to interpreted.
957 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
958 ASSERT_TRUE(oat_file.get() != nullptr);
Richard Uhlerf16d5722015-05-11 09:32:47 -0700959
Richard Uhler66d874d2015-01-15 09:37:19 -0800960 EXPECT_FALSE(oat_file->IsExecutable());
961 std::vector<std::unique_ptr<const DexFile>> dex_files;
962 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
963 EXPECT_EQ(1u, dex_files.size());
964}
965
Andreas Gampe29d38e72016-03-23 15:31:51 +0000966// Case: We have a DEX file and a VerifyAtRuntime ODEX file, but no OAT file.
967// Expect: The status is kNoDexOptNeeded, because VerifyAtRuntime contains no code.
968TEST_F(OatFileAssistantTest, DexVerifyAtRuntimeOdexNoOat) {
969 std::string dex_location = GetScratchDir() + "/DexVerifyAtRuntimeOdexNoOat.jar";
970 std::string odex_location = GetOdexDir() + "/DexVerifyAtRuntimeOdexNoOat.odex";
David Brazdilce4b0ba2016-01-28 15:05:49 +0000971
972 // Create the dex and odex files
973 Copy(GetDexSrc1(), dex_location);
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100974 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kExtract);
David Brazdilce4b0ba2016-01-28 15:05:49 +0000975
976 // Verify the status.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700977 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
David Brazdilce4b0ba2016-01-28 15:05:49 +0000978
Andreas Gampe29d38e72016-03-23 15:31:51 +0000979 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100980 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000981 EXPECT_EQ(-OatFileAssistant::kDex2OatForFilter,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000982 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
David Brazdilce4b0ba2016-01-28 15:05:49 +0000983
984 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler3e580bc2016-11-08 16:23:07 +0000985 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus());
Richard Uhler03bc6592016-11-22 09:42:04 +0000986 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
David Brazdilce4b0ba2016-01-28 15:05:49 +0000987 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
988}
989
Richard Uhler66d874d2015-01-15 09:37:19 -0800990// Case: We have a DEX file and up-to-date OAT file for it.
991// Expect: We should load an executable dex file.
992TEST_F(OatFileAssistantTest, LoadOatUpToDate) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000993 if (IsExecutedAsRoot()) {
994 // We cannot simulate non writable locations when executed as root: b/38000545.
995 LOG(ERROR) << "Test skipped because it's running as root";
996 return;
997 }
998
Richard Uhler66d874d2015-01-15 09:37:19 -0800999 std::string dex_location = GetScratchDir() + "/LoadOatUpToDate.jar";
1000
1001 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +00001002 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -08001003
Calin Juravle357c66d2017-05-04 01:57:17 +00001004 ScopedNonWritable scoped_non_writable(dex_location);
1005 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
1006
Richard Uhler66d874d2015-01-15 09:37:19 -08001007 // Load the oat using an oat file assistant.
Richard Uhlerd1472a22016-04-15 15:18:56 -07001008 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Andreas Gampe29d38e72016-03-23 15:31:51 +00001009
1010 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1011 ASSERT_TRUE(oat_file.get() != nullptr);
1012 EXPECT_TRUE(oat_file->IsExecutable());
1013 std::vector<std::unique_ptr<const DexFile>> dex_files;
1014 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
1015 EXPECT_EQ(1u, dex_files.size());
1016}
1017
Nicolas Geoffray49cda062017-04-21 13:08:25 +01001018// Case: We have a DEX file and up-to-date quicken OAT file for it.
Andreas Gampe29d38e72016-03-23 15:31:51 +00001019// Expect: We should still load the oat file as executable.
1020TEST_F(OatFileAssistantTest, LoadExecInterpretOnlyOatUpToDate) {
Calin Juravle357c66d2017-05-04 01:57:17 +00001021 if (IsExecutedAsRoot()) {
1022 // We cannot simulate non writable locations when executed as root: b/38000545.
1023 LOG(ERROR) << "Test skipped because it's running as root";
1024 return;
1025 }
1026
Andreas Gampe29d38e72016-03-23 15:31:51 +00001027 std::string dex_location = GetScratchDir() + "/LoadExecInterpretOnlyOatUpToDate.jar";
1028
1029 Copy(GetDexSrc1(), dex_location);
Nicolas Geoffray49cda062017-04-21 13:08:25 +01001030 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kQuicken);
Andreas Gampe29d38e72016-03-23 15:31:51 +00001031
Calin Juravle357c66d2017-05-04 01:57:17 +00001032 ScopedNonWritable scoped_non_writable(dex_location);
1033 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
1034
Andreas Gampe29d38e72016-03-23 15:31:51 +00001035 // Load the oat using an oat file assistant.
Richard Uhlerd1472a22016-04-15 15:18:56 -07001036 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001037
1038 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1039 ASSERT_TRUE(oat_file.get() != nullptr);
1040 EXPECT_TRUE(oat_file->IsExecutable());
1041 std::vector<std::unique_ptr<const DexFile>> dex_files;
1042 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
1043 EXPECT_EQ(1u, dex_files.size());
1044}
1045
1046// Case: We have a DEX file and up-to-date OAT file for it.
1047// Expect: Loading non-executable should load the oat non-executable.
1048TEST_F(OatFileAssistantTest, LoadNoExecOatUpToDate) {
Calin Juravle357c66d2017-05-04 01:57:17 +00001049 if (IsExecutedAsRoot()) {
1050 // We cannot simulate non writable locations when executed as root: b/38000545.
1051 LOG(ERROR) << "Test skipped because it's running as root";
1052 return;
1053 }
1054
Richard Uhler66d874d2015-01-15 09:37:19 -08001055 std::string dex_location = GetScratchDir() + "/LoadNoExecOatUpToDate.jar";
1056
1057 Copy(GetDexSrc1(), dex_location);
Calin Juravle357c66d2017-05-04 01:57:17 +00001058
1059 ScopedNonWritable scoped_non_writable(dex_location);
1060 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
1061
Andreas Gampe29d38e72016-03-23 15:31:51 +00001062 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -08001063
1064 // Load the oat using an oat file assistant.
Richard Uhlerd1472a22016-04-15 15:18:56 -07001065 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -08001066
1067 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1068 ASSERT_TRUE(oat_file.get() != nullptr);
1069 EXPECT_FALSE(oat_file->IsExecutable());
1070 std::vector<std::unique_ptr<const DexFile>> dex_files;
1071 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
1072 EXPECT_EQ(1u, dex_files.size());
1073}
1074
Richard Uhler66d874d2015-01-15 09:37:19 -08001075// Turn an absolute path into a path relative to the current working
1076// directory.
Andreas Gampeca620d72016-11-08 08:09:33 -08001077static std::string MakePathRelative(const std::string& target) {
Richard Uhler66d874d2015-01-15 09:37:19 -08001078 char buf[MAXPATHLEN];
1079 std::string cwd = getcwd(buf, MAXPATHLEN);
1080
1081 // Split the target and cwd paths into components.
1082 std::vector<std::string> target_path;
1083 std::vector<std::string> cwd_path;
1084 Split(target, '/', &target_path);
1085 Split(cwd, '/', &cwd_path);
1086
1087 // Reverse the path components, so we can use pop_back().
1088 std::reverse(target_path.begin(), target_path.end());
1089 std::reverse(cwd_path.begin(), cwd_path.end());
1090
1091 // Drop the common prefix of the paths. Because we reversed the path
1092 // components, this becomes the common suffix of target_path and cwd_path.
1093 while (!target_path.empty() && !cwd_path.empty()
1094 && target_path.back() == cwd_path.back()) {
1095 target_path.pop_back();
1096 cwd_path.pop_back();
1097 }
1098
1099 // For each element of the remaining cwd_path, add '..' to the beginning
1100 // of the target path. Because we reversed the path components, we add to
1101 // the end of target_path.
1102 for (unsigned int i = 0; i < cwd_path.size(); i++) {
1103 target_path.push_back("..");
1104 }
1105
1106 // Reverse again to get the right path order, and join to get the result.
1107 std::reverse(target_path.begin(), target_path.end());
Andreas Gampe9186ced2016-12-12 14:28:21 -08001108 return android::base::Join(target_path, '/');
Richard Uhler66d874d2015-01-15 09:37:19 -08001109}
1110
1111// Case: Non-absolute path to Dex location.
1112// Expect: Not sure, but it shouldn't crash.
1113TEST_F(OatFileAssistantTest, NonAbsoluteDexLocation) {
1114 std::string abs_dex_location = GetScratchDir() + "/NonAbsoluteDexLocation.jar";
1115 Copy(GetDexSrc1(), abs_dex_location);
1116
1117 std::string dex_location = MakePathRelative(abs_dex_location);
Richard Uhlerd1472a22016-04-15 15:18:56 -07001118 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001119
1120 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler7225a8d2016-11-22 10:12:03 +00001121 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Andreas Gampe29d38e72016-03-23 15:31:51 +00001122 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler03bc6592016-11-22 09:42:04 +00001123 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
1124 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler66d874d2015-01-15 09:37:19 -08001125}
1126
1127// Case: Very short, non-existent Dex location.
Richard Uhler9b994ea2015-06-24 08:44:19 -07001128// Expect: kNoDexOptNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -08001129TEST_F(OatFileAssistantTest, ShortDexLocation) {
1130 std::string dex_location = "/xx";
1131
Richard Uhlerd1472a22016-04-15 15:18:56 -07001132 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001133
1134 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Andreas Gampe29d38e72016-03-23 15:31:51 +00001135 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
1136 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler03bc6592016-11-22 09:42:04 +00001137 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
1138 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -07001139 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -08001140}
1141
1142// Case: Non-standard extension for dex file.
Richard Uhler95abd042015-03-24 09:51:28 -07001143// Expect: The status is kDex2OatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -08001144TEST_F(OatFileAssistantTest, LongDexExtension) {
1145 std::string dex_location = GetScratchDir() + "/LongDexExtension.jarx";
1146 Copy(GetDexSrc1(), dex_location);
1147
Richard Uhlerd1472a22016-04-15 15:18:56 -07001148 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -08001149
Richard Uhler7225a8d2016-11-22 10:12:03 +00001150 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Andreas Gampe29d38e72016-03-23 15:31:51 +00001151 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -08001152
1153 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +00001154 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
1155 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler66d874d2015-01-15 09:37:19 -08001156}
1157
Nicolas Geoffray3d8a78a2018-08-29 21:10:16 +00001158
Richard Uhler66d874d2015-01-15 09:37:19 -08001159// A task to generate a dex location. Used by the RaceToGenerate test.
1160class RaceGenerateTask : public Task {
1161 public:
Nicolas Geoffray3d8a78a2018-08-29 21:10:16 +00001162 RaceGenerateTask(const std::string& dex_location, const std::string& oat_location)
1163 : dex_location_(dex_location), oat_location_(oat_location), loaded_oat_file_(nullptr)
Richard Uhler66d874d2015-01-15 09:37:19 -08001164 {}
1165
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001166 void Run(Thread* self ATTRIBUTE_UNUSED) {
Richard Uhler66d874d2015-01-15 09:37:19 -08001167 // Load the dex files, and save a pointer to the loaded oat file, so that
1168 // we can verify only one oat file was loaded for the dex location.
Richard Uhler66d874d2015-01-15 09:37:19 -08001169 std::vector<std::unique_ptr<const DexFile>> dex_files;
1170 std::vector<std::string> error_msgs;
Mathieu Chartiere58991b2015-10-13 07:59:34 -07001171 const OatFile* oat_file = nullptr;
Nicolas Geoffray3d8a78a2018-08-29 21:10:16 +00001172 {
1173 // Create the oat file.
1174 std::vector<std::string> args;
1175 args.push_back("--dex-file=" + dex_location_);
1176 args.push_back("--oat-file=" + oat_location_);
1177 std::string error_msg;
1178 ASSERT_TRUE(DexoptTest::Dex2Oat(args, &error_msg)) << error_msg;
1179 }
1180
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001181 dex_files = Runtime::Current()->GetOatFileManager().OpenDexFilesFromOat(
1182 dex_location_.c_str(),
Jeff Hao0cb17282017-07-12 14:51:49 -07001183 Runtime::Current()->GetSystemClassLoader(),
Mathieu Chartierfbc31082016-01-24 11:59:56 -08001184 /*dex_elements*/nullptr,
Mathieu Chartiere58991b2015-10-13 07:59:34 -07001185 &oat_file,
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001186 &error_msgs);
Andreas Gampe9186ced2016-12-12 14:28:21 -08001187 CHECK(!dex_files.empty()) << android::base::Join(error_msgs, '\n');
Nicolas Geoffray3d8a78a2018-08-29 21:10:16 +00001188 if (dex_files[0]->GetOatDexFile() != nullptr) {
1189 loaded_oat_file_ = dex_files[0]->GetOatDexFile()->GetOatFile();
1190 }
Mathieu Chartiere58991b2015-10-13 07:59:34 -07001191 CHECK_EQ(loaded_oat_file_, oat_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001192 }
1193
1194 const OatFile* GetLoadedOatFile() const {
1195 return loaded_oat_file_;
1196 }
1197
1198 private:
1199 std::string dex_location_;
1200 std::string oat_location_;
1201 const OatFile* loaded_oat_file_;
1202};
1203
Nicolas Geoffray3d8a78a2018-08-29 21:10:16 +00001204// Test the case where dex2oat invocations race with multiple processes trying to
1205// load the oat file.
Richard Uhler66d874d2015-01-15 09:37:19 -08001206TEST_F(OatFileAssistantTest, RaceToGenerate) {
1207 std::string dex_location = GetScratchDir() + "/RaceToGenerate.jar";
Richard Uhler63434112015-03-16 14:32:16 -07001208 std::string oat_location = GetOdexDir() + "/RaceToGenerate.oat";
Richard Uhler66d874d2015-01-15 09:37:19 -08001209
Jeff Hao0cb17282017-07-12 14:51:49 -07001210 // Start the runtime to initialize the system's class loader.
1211 Thread::Current()->TransitionFromSuspendedToRunnable();
1212 runtime_->Start();
1213
Richard Uhler66d874d2015-01-15 09:37:19 -08001214 // We use the lib core dex file, because it's large, and hopefully should
1215 // take a while to generate.
Narayan Kamathd1ef4362015-11-12 11:49:06 +00001216 Copy(GetLibCoreDexFileNames()[0], dex_location);
Richard Uhler66d874d2015-01-15 09:37:19 -08001217
Nicolas Geoffray3d8a78a2018-08-29 21:10:16 +00001218 const size_t kNumThreads = 32;
Richard Uhler66d874d2015-01-15 09:37:19 -08001219 Thread* self = Thread::Current();
1220 ThreadPool thread_pool("Oat file assistant test thread pool", kNumThreads);
1221 std::vector<std::unique_ptr<RaceGenerateTask>> tasks;
Nicolas Geoffray3d8a78a2018-08-29 21:10:16 +00001222 for (size_t i = 0; i < kNumThreads; i++) {
Richard Uhler66d874d2015-01-15 09:37:19 -08001223 std::unique_ptr<RaceGenerateTask> task(new RaceGenerateTask(dex_location, oat_location));
1224 thread_pool.AddTask(self, task.get());
1225 tasks.push_back(std::move(task));
1226 }
1227 thread_pool.StartWorkers(self);
Nicolas Geoffray3d8a78a2018-08-29 21:10:16 +00001228 thread_pool.Wait(self, /* do_work */ true, /* may_hold_locks */ false);
Richard Uhler66d874d2015-01-15 09:37:19 -08001229
Nicolas Geoffray3d8a78a2018-08-29 21:10:16 +00001230 // Verify that tasks which got an oat file got a unique one.
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001231 std::set<const OatFile*> oat_files;
Richard Uhler66d874d2015-01-15 09:37:19 -08001232 for (auto& task : tasks) {
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001233 const OatFile* oat_file = task->GetLoadedOatFile();
Nicolas Geoffray3d8a78a2018-08-29 21:10:16 +00001234 if (oat_file != nullptr) {
1235 EXPECT_TRUE(oat_files.find(oat_file) == oat_files.end());
1236 oat_files.insert(oat_file);
1237 }
Richard Uhler66d874d2015-01-15 09:37:19 -08001238 }
1239}
1240
Nicolas Geoffray3d8a78a2018-08-29 21:10:16 +00001241// Case: We have a DEX file and an ODEX file, and no OAT file,
Richard Uhler66d874d2015-01-15 09:37:19 -08001242// Expect: We should load the odex file non-executable.
Nicolas Geoffray3d8a78a2018-08-29 21:10:16 +00001243TEST_F(DexoptTest, LoadDexOdexNoOat) {
Richard Uhler66d874d2015-01-15 09:37:19 -08001244 std::string dex_location = GetScratchDir() + "/LoadDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -07001245 std::string odex_location = GetOdexDir() + "/LoadDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -08001246
1247 // Create the dex and odex files
1248 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +00001249 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -08001250
1251 // Load the oat using an executable oat file assistant.
Richard Uhlerd1472a22016-04-15 15:18:56 -07001252 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001253
1254 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1255 ASSERT_TRUE(oat_file.get() != nullptr);
1256 EXPECT_FALSE(oat_file->IsExecutable());
1257 std::vector<std::unique_ptr<const DexFile>> dex_files;
1258 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
1259 EXPECT_EQ(1u, dex_files.size());
1260}
1261
Nicolas Geoffray3d8a78a2018-08-29 21:10:16 +00001262// Case: We have a MultiDEX file and an ODEX file, and no OAT file.
Richard Uhler66d874d2015-01-15 09:37:19 -08001263// Expect: We should load the odex file non-executable.
Nicolas Geoffray3d8a78a2018-08-29 21:10:16 +00001264TEST_F(DexoptTest, LoadMultiDexOdexNoOat) {
Richard Uhler66d874d2015-01-15 09:37:19 -08001265 std::string dex_location = GetScratchDir() + "/LoadMultiDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -07001266 std::string odex_location = GetOdexDir() + "/LoadMultiDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -08001267
1268 // Create the dex and odex files
1269 Copy(GetMultiDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +00001270 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -08001271
1272 // Load the oat using an executable oat file assistant.
Richard Uhlerd1472a22016-04-15 15:18:56 -07001273 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001274
1275 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1276 ASSERT_TRUE(oat_file.get() != nullptr);
1277 EXPECT_FALSE(oat_file->IsExecutable());
1278 std::vector<std::unique_ptr<const DexFile>> dex_files;
1279 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
1280 EXPECT_EQ(2u, dex_files.size());
1281}
1282
Richard Uhlerb81881d2016-04-19 13:08:04 -07001283TEST(OatFileAssistantUtilsTest, DexLocationToOdexFilename) {
Richard Uhler66d874d2015-01-15 09:37:19 -08001284 std::string error_msg;
1285 std::string odex_file;
1286
Richard Uhlerb81881d2016-04-19 13:08:04 -07001287 EXPECT_TRUE(OatFileAssistant::DexLocationToOdexFilename(
Vladimir Marko33bff252017-11-01 14:35:42 +00001288 "/foo/bar/baz.jar", InstructionSet::kArm, &odex_file, &error_msg)) << error_msg;
Richard Uhler63434112015-03-16 14:32:16 -07001289 EXPECT_EQ("/foo/bar/oat/arm/baz.odex", odex_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001290
Richard Uhlerb81881d2016-04-19 13:08:04 -07001291 EXPECT_TRUE(OatFileAssistant::DexLocationToOdexFilename(
Vladimir Marko33bff252017-11-01 14:35:42 +00001292 "/foo/bar/baz.funnyext", InstructionSet::kArm, &odex_file, &error_msg)) << error_msg;
Richard Uhler63434112015-03-16 14:32:16 -07001293 EXPECT_EQ("/foo/bar/oat/arm/baz.odex", odex_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001294
Richard Uhlerb81881d2016-04-19 13:08:04 -07001295 EXPECT_FALSE(OatFileAssistant::DexLocationToOdexFilename(
Vladimir Marko33bff252017-11-01 14:35:42 +00001296 "nopath.jar", InstructionSet::kArm, &odex_file, &error_msg));
Richard Uhlerb81881d2016-04-19 13:08:04 -07001297 EXPECT_FALSE(OatFileAssistant::DexLocationToOdexFilename(
Vladimir Marko33bff252017-11-01 14:35:42 +00001298 "/foo/bar/baz_noext", InstructionSet::kArm, &odex_file, &error_msg));
Richard Uhler66d874d2015-01-15 09:37:19 -08001299}
1300
Richard Uhler23cedd22015-04-08 13:17:29 -07001301// Verify the dexopt status values from dalvik.system.DexFile
1302// match the OatFileAssistant::DexOptStatus values.
1303TEST_F(OatFileAssistantTest, DexOptStatusValues) {
Richard Uhler7225a8d2016-11-22 10:12:03 +00001304 std::pair<OatFileAssistant::DexOptNeeded, const char*> mapping[] = {
1305 {OatFileAssistant::kNoDexOptNeeded, "NO_DEXOPT_NEEDED"},
1306 {OatFileAssistant::kDex2OatFromScratch, "DEX2OAT_FROM_SCRATCH"},
1307 {OatFileAssistant::kDex2OatForBootImage, "DEX2OAT_FOR_BOOT_IMAGE"},
1308 {OatFileAssistant::kDex2OatForFilter, "DEX2OAT_FOR_FILTER"},
1309 {OatFileAssistant::kDex2OatForRelocation, "DEX2OAT_FOR_RELOCATION"},
Richard Uhler7225a8d2016-11-22 10:12:03 +00001310 };
1311
Richard Uhler23cedd22015-04-08 13:17:29 -07001312 ScopedObjectAccess soa(Thread::Current());
1313 StackHandleScope<1> hs(soa.Self());
1314 ClassLinker* linker = Runtime::Current()->GetClassLinker();
1315 Handle<mirror::Class> dexfile(
1316 hs.NewHandle(linker->FindSystemClass(soa.Self(), "Ldalvik/system/DexFile;")));
Andreas Gampefa4333d2017-02-14 11:10:34 -08001317 ASSERT_FALSE(dexfile == nullptr);
Richard Uhler23cedd22015-04-08 13:17:29 -07001318 linker->EnsureInitialized(soa.Self(), dexfile, true, true);
1319
Richard Uhler7225a8d2016-11-22 10:12:03 +00001320 for (std::pair<OatFileAssistant::DexOptNeeded, const char*> field : mapping) {
1321 ArtField* art_field = mirror::Class::FindStaticField(
Vladimir Marko19a4d372016-12-08 14:41:46 +00001322 soa.Self(), dexfile.Get(), field.second, "I");
Richard Uhler7225a8d2016-11-22 10:12:03 +00001323 ASSERT_FALSE(art_field == nullptr);
1324 EXPECT_EQ(art_field->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
1325 EXPECT_EQ(field.first, art_field->GetInt(dexfile.Get()));
1326 }
Richard Uhler23cedd22015-04-08 13:17:29 -07001327}
Richard Uhler66d874d2015-01-15 09:37:19 -08001328
Calin Juravle44e5efa2017-09-12 00:54:26 -07001329TEST_F(OatFileAssistantTest, GetDexOptNeededWithOutOfDateContext) {
1330 std::string dex_location = GetScratchDir() + "/TestDex.jar";
1331 std::string context_location = GetScratchDir() + "/ContextDex.jar";
1332 Copy(GetDexSrc1(), dex_location);
1333 Copy(GetDexSrc2(), context_location);
1334
1335 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
1336
Calin Juravle44e5efa2017-09-12 00:54:26 -07001337 std::string error_msg;
1338 std::string context_str = "PCL[" + context_location + "]";
1339 std::unique_ptr<ClassLoaderContext> context = ClassLoaderContext::Create(context_str);
1340 ASSERT_TRUE(context != nullptr);
1341 ASSERT_TRUE(context->OpenDexFiles(kRuntimeISA, ""));
1342
Calin Juravle44e5efa2017-09-12 00:54:26 -07001343 // Update the context by overriding the jar file.
1344 Copy(GetMultiDexSrc2(), context_location);
1345 std::unique_ptr<ClassLoaderContext> updated_context = ClassLoaderContext::Create(context_str);
1346 ASSERT_TRUE(updated_context != nullptr);
1347 // DexOptNeeded should advise compilation from scratch.
1348 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
1349 oat_file_assistant.GetDexOptNeeded(
Nicolas Geoffray3d8a78a2018-08-29 21:10:16 +00001350 CompilerFilter::kDefaultCompilerFilter,
1351 /* downgrade */ false,
1352 /* profile_changed */ false,
1353 updated_context.get()));
Nicolas Geoffray29742602017-12-14 10:09:03 +00001354}
1355
Richard Uhler66d874d2015-01-15 09:37:19 -08001356// TODO: More Tests:
1357// * Test class linker falls back to unquickened dex for DexNoOat
1358// * Test class linker falls back to unquickened dex for MultiDexNoOat
Richard Uhler66d874d2015-01-15 09:37:19 -08001359// * Test using secondary isa
Richard Uhler66d874d2015-01-15 09:37:19 -08001360// * Test for status of oat while oat is being generated (how?)
1361// * Test case where 32 and 64 bit boot class paths differ,
1362// and we ask IsInBootClassPath for a class in exactly one of the 32 or
1363// 64 bit boot class paths.
1364// * Test unexpected scenarios (?):
1365// - Dex is stripped, don't have odex.
1366// - Oat file corrupted after status check, before reload unexecutable
1367// because it's unrelocated and no dex2oat
Richard Uhler66d874d2015-01-15 09:37:19 -08001368} // namespace art