1 /*
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
17 #include "oat_file_assistant.h"
18
19 #include <sys/param.h>
20
21 #include <string>
22 #include <vector>
23 #include <fcntl.h>
24
25 #include <gtest/gtest.h>
26
27 #include "android-base/strings.h"
28
29 #include "art_field-inl.h"
30 #include "base/os.h"
31 #include "base/utils.h"
32 #include "class_linker-inl.h"
33 #include "class_loader_context.h"
34 #include "common_runtime_test.h"
35 #include "dexopt_test.h"
36 #include "hidden_api.h"
37 #include "oat.h"
38 #include "oat_file.h"
39 #include "oat_file_manager.h"
40 #include "scoped_thread_state_change-inl.h"
41 #include "thread-current-inl.h"
42
43 namespace art {
44
45 class OatFileAssistantTest : public DexoptTest {
46 public:
VerifyOptimizationStatus(OatFileAssistant * assistant,const std::string & file,const std::string & expected_filter,const std::string & expected_reason,const std::string & expected_odex_status)47 void VerifyOptimizationStatus(OatFileAssistant* assistant,
48 const std::string& file,
49 const std::string& expected_filter,
50 const std::string& expected_reason,
51 const std::string& expected_odex_status) {
52 // Verify the static methods (called from PM for dexOptNeeded).
53 std::string compilation_filter1;
54 std::string compilation_reason1;
55
56 OatFileAssistant::GetOptimizationStatus(
57 file, kRuntimeISA, &compilation_filter1, &compilation_reason1);
58
59 ASSERT_EQ(expected_filter, compilation_filter1);
60 ASSERT_EQ(expected_reason, compilation_reason1);
61
62 // Verify the instance methods (called at runtime for systrace).
63 std::string odex_location2; // ignored
64 std::string compilation_filter2;
65 std::string compilation_reason2;
66 std::string odex_status2;
67
68 assistant->GetOptimizationStatus(
69 &odex_location2,
70 &compilation_filter2,
71 &compilation_reason2,
72 &odex_status2);
73
74 ASSERT_EQ(expected_filter, compilation_filter2);
75 ASSERT_EQ(expected_reason, compilation_reason2);
76 ASSERT_EQ(expected_odex_status, odex_status2);
77 }
78
VerifyOptimizationStatus(OatFileAssistant * assistant,const std::string & file,CompilerFilter::Filter expected_filter,const std::string & expected_reason,const std::string & expected_odex_status)79 void VerifyOptimizationStatus(OatFileAssistant* assistant,
80 const std::string& file,
81 CompilerFilter::Filter expected_filter,
82 const std::string& expected_reason,
83 const std::string& expected_odex_status) {
84 VerifyOptimizationStatus(
85 assistant,
86 file,
87 CompilerFilter::NameOfFilter(expected_filter),
88 expected_reason,
89 expected_odex_status);
90 }
91
InsertNewBootClasspathEntry()92 void InsertNewBootClasspathEntry() {
93 std::string extra_dex_filename = GetMultiDexSrc1();
94 Runtime* runtime = Runtime::Current();
95 runtime->boot_class_path_.push_back(extra_dex_filename);
96 if (!runtime->boot_class_path_locations_.empty()) {
97 runtime->boot_class_path_locations_.push_back(extra_dex_filename);
98 }
99 }
100
GetDexOptNeeded(OatFileAssistant * assistant,CompilerFilter::Filter compiler_filter,bool profile_changed=false,bool downgrade=false)101 int GetDexOptNeeded(
102 OatFileAssistant* assistant,
103 CompilerFilter::Filter compiler_filter,
104 bool profile_changed = false,
105 bool downgrade = false) {
106 return assistant->GetDexOptNeeded(
107 compiler_filter,
108 profile_changed,
109 downgrade);
110 }
111
InitializeDefaultContext()112 static std::unique_ptr<ClassLoaderContext> InitializeDefaultContext() {
113 auto context = ClassLoaderContext::Default();
114 context->OpenDexFiles();
115 return context;
116 }
117
118 std::unique_ptr<ClassLoaderContext> default_context_ = InitializeDefaultContext();
119 };
120
121 class ScopedNonWritable {
122 public:
ScopedNonWritable(const std::string & dex_location)123 explicit ScopedNonWritable(const std::string& dex_location) {
124 is_valid_ = false;
125 size_t pos = dex_location.rfind('/');
126 if (pos != std::string::npos) {
127 is_valid_ = true;
128 dex_parent_ = dex_location.substr(0, pos);
129 if (chmod(dex_parent_.c_str(), 0555) != 0) {
130 PLOG(ERROR) << "Could not change permissions on " << dex_parent_;
131 }
132 }
133 }
134
IsSuccessful()135 bool IsSuccessful() { return is_valid_ && (access(dex_parent_.c_str(), W_OK) != 0); }
136
~ScopedNonWritable()137 ~ScopedNonWritable() {
138 if (is_valid_) {
139 if (chmod(dex_parent_.c_str(), 0777) != 0) {
140 PLOG(ERROR) << "Could not restore permissions on " << dex_parent_;
141 }
142 }
143 }
144
145 private:
146 std::string dex_parent_;
147 bool is_valid_;
148 };
149
IsExecutedAsRoot()150 static bool IsExecutedAsRoot() {
151 return geteuid() == 0;
152 }
153
154 // Case: We have a MultiDEX file and up-to-date ODEX file for it with relative
155 // encoded dex locations.
156 // Expect: The oat file status is kNoDexOptNeeded.
TEST_F(OatFileAssistantTest,RelativeEncodedDexLocation)157 TEST_F(OatFileAssistantTest, RelativeEncodedDexLocation) {
158 std::string dex_location = GetScratchDir() + "/RelativeEncodedDexLocation.jar";
159 std::string odex_location = GetOdexDir() + "/RelativeEncodedDexLocation.odex";
160
161 // Create the dex file
162 Copy(GetMultiDexSrc1(), dex_location);
163
164 // Create the oat file with relative encoded dex location.
165 std::vector<std::string> args = {
166 "--dex-file=" + dex_location,
167 "--dex-location=" + std::string("RelativeEncodedDexLocation.jar"),
168 "--oat-file=" + odex_location,
169 "--compiler-filter=speed"
170 };
171
172 std::string error_msg;
173 ASSERT_TRUE(Dex2Oat(args, &error_msg)) << error_msg;
174
175 // Verify we can load both dex files.
176 OatFileAssistant oat_file_assistant(dex_location.c_str(),
177 kRuntimeISA,
178 default_context_.get(),
179 true);
180
181 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
182 ASSERT_TRUE(oat_file.get() != nullptr);
183 EXPECT_TRUE(oat_file->IsExecutable());
184 std::vector<std::unique_ptr<const DexFile>> dex_files;
185 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
186 EXPECT_EQ(2u, dex_files.size());
187 }
188
TEST_F(OatFileAssistantTest,MakeUpToDateWithContext)189 TEST_F(OatFileAssistantTest, MakeUpToDateWithContext) {
190 std::string dex_location = GetScratchDir() + "/TestDex.jar";
191 std::string odex_location = GetOdexDir() + "/TestDex.odex";
192 std::string context_location = GetScratchDir() + "/ContextDex.jar";
193 Copy(GetDexSrc1(), dex_location);
194 Copy(GetDexSrc2(), context_location);
195
196 std::string context_str = "PCL[" + context_location + "]";
197 std::unique_ptr<ClassLoaderContext> context = ClassLoaderContext::Create(context_str);
198 ASSERT_TRUE(context != nullptr);
199 ASSERT_TRUE(context->OpenDexFiles());
200
201 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, context.get(), false);
202
203 std::string error_msg;
204 std::vector<std::string> args;
205 args.push_back("--dex-file=" + dex_location);
206 args.push_back("--oat-file=" + odex_location);
207 args.push_back("--class-loader-context=" + context_str);
208 ASSERT_TRUE(Dex2Oat(args, &error_msg)) << error_msg;
209
210 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
211 ASSERT_NE(nullptr, oat_file.get());
212 ASSERT_NE(nullptr, oat_file->GetOatHeader().GetStoreValueByKey(OatHeader::kClassPathKey));
213 EXPECT_EQ(context->EncodeContextForOatFile(""),
214 oat_file->GetOatHeader().GetStoreValueByKey(OatHeader::kClassPathKey));
215 }
216
TEST_F(OatFileAssistantTest,GetDexOptNeededWithUpToDateContextRelative)217 TEST_F(OatFileAssistantTest, GetDexOptNeededWithUpToDateContextRelative) {
218 std::string dex_location = GetScratchDir() + "/TestDex.jar";
219 std::string odex_location = GetOdexDir() + "/TestDex.odex";
220 std::string context_location = GetScratchDir() + "/ContextDex.jar";
221 Copy(GetDexSrc1(), dex_location);
222 Copy(GetDexSrc2(), context_location);
223
224 // A relative context simulates a dependent split context.
225 std::unique_ptr<ClassLoaderContext> relative_context =
226 ClassLoaderContext::Create("PCL[ContextDex.jar]");
227 ASSERT_TRUE(relative_context != nullptr);
228 std::vector<int> context_fds;
229 ASSERT_TRUE(relative_context->OpenDexFiles(GetScratchDir(), context_fds));
230
231 OatFileAssistant oat_file_assistant(dex_location.c_str(),
232 kRuntimeISA,
233 relative_context.get(),
234 false);
235
236 std::string error_msg;
237 std::vector<std::string> args;
238 args.push_back("--dex-file=" + dex_location);
239 args.push_back("--oat-file=" + odex_location);
240 args.push_back("--class-loader-context=PCL[" + context_location + "]");
241 ASSERT_TRUE(Dex2Oat(args, &error_msg)) << error_msg;
242
243 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
244 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kDefaultCompilerFilter));
245 }
246
247 // Case: We have a DEX file, but no OAT file for it.
248 // Expect: The status is kDex2OatNeeded.
TEST_F(OatFileAssistantTest,DexNoOat)249 TEST_F(OatFileAssistantTest, DexNoOat) {
250 std::string dex_location = GetScratchDir() + "/DexNoOat.jar";
251 Copy(GetDexSrc1(), dex_location);
252
253 OatFileAssistant oat_file_assistant(dex_location.c_str(),
254 kRuntimeISA,
255 default_context_.get(),
256 false);
257
258 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
259 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kExtract));
260 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
261 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kVerify));
262 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
263 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kSpeedProfile));
264 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
265 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kSpeed));
266
267 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
268 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
269 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
270 EXPECT_TRUE(oat_file_assistant.HasDexFiles());
271
272 VerifyOptimizationStatus(
273 &oat_file_assistant,
274 dex_location,
275 "run-from-apk",
276 "unknown",
277 "io-error-no-oat");
278 }
279
280 // Case: We have no DEX file and no OAT file.
281 // Expect: Status is kNoDexOptNeeded. Loading should fail, but not crash.
TEST_F(OatFileAssistantTest,NoDexNoOat)282 TEST_F(OatFileAssistantTest, NoDexNoOat) {
283 std::string dex_location = GetScratchDir() + "/NoDexNoOat.jar";
284
285 OatFileAssistant oat_file_assistant(dex_location.c_str(),
286 kRuntimeISA,
287 default_context_.get(),
288 true);
289
290 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
291 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kSpeed));
292 EXPECT_FALSE(oat_file_assistant.HasDexFiles());
293
294 // Trying to get the best oat file should fail, but not crash.
295 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
296 EXPECT_EQ(nullptr, oat_file.get());
297 }
298
299 // Case: We have a DEX file and an ODEX file, but no OAT file.
300 // Expect: The status is kNoDexOptNeeded.
TEST_F(OatFileAssistantTest,OdexUpToDate)301 TEST_F(OatFileAssistantTest, OdexUpToDate) {
302 std::string dex_location = GetScratchDir() + "/OdexUpToDate.jar";
303 std::string odex_location = GetOdexDir() + "/OdexUpToDate.odex";
304 Copy(GetDexSrc1(), dex_location);
305 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed, "install");
306
307 // Force the use of oat location by making the dex parent not writable.
308 OatFileAssistant oat_file_assistant(
309 dex_location.c_str(),
310 kRuntimeISA,
311 default_context_.get(),
312 /*load_executable=*/ false);
313
314 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
315 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kSpeed));
316 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
317 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kVerify));
318 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
319 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kExtract));
320 EXPECT_EQ(-OatFileAssistant::kDex2OatForFilter,
321 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kEverything));
322
323 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
324 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus());
325 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
326 EXPECT_TRUE(oat_file_assistant.HasDexFiles());
327
328 VerifyOptimizationStatus(
329 &oat_file_assistant,
330 dex_location,
331 CompilerFilter::kSpeed,
332 "install",
333 "up-to-date");
334 }
335
336 // Case: We have an ODEX file compiled against partial boot image.
337 // Expect: The status is kNoDexOptNeeded.
TEST_F(OatFileAssistantTest,OdexUpToDatePartialBootImage)338 TEST_F(OatFileAssistantTest, OdexUpToDatePartialBootImage) {
339 std::string dex_location = GetScratchDir() + "/OdexUpToDate.jar";
340 std::string odex_location = GetOdexDir() + "/OdexUpToDate.odex";
341 Copy(GetDexSrc1(), dex_location);
342 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed, "install");
343
344 // Insert an extra dex file to the boot class path.
345 InsertNewBootClasspathEntry();
346
347 // Force the use of oat location by making the dex parent not writable.
348 OatFileAssistant oat_file_assistant(
349 dex_location.c_str(),
350 kRuntimeISA,
351 default_context_.get(),
352 /*load_executable=*/ false);
353
354 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
355 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kSpeed));
356 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
357 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kVerify));
358 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
359 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kExtract));
360 EXPECT_EQ(-OatFileAssistant::kDex2OatForFilter,
361 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kEverything));
362
363 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
364 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus());
365 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
366 EXPECT_TRUE(oat_file_assistant.HasDexFiles());
367
368 VerifyOptimizationStatus(
369 &oat_file_assistant,
370 dex_location,
371 CompilerFilter::kSpeed,
372 "install",
373 "up-to-date");
374 }
375
376 // Case: We have a DEX file and a PIC ODEX file, but no OAT file. We load the dex
377 // file via a symlink.
378 // Expect: The status is kNoDexOptNeeded.
TEST_F(OatFileAssistantTest,OdexUpToDateSymLink)379 TEST_F(OatFileAssistantTest, OdexUpToDateSymLink) {
380 std::string scratch_dir = GetScratchDir();
381 std::string dex_location = GetScratchDir() + "/OdexUpToDate.jar";
382 std::string odex_location = GetOdexDir() + "/OdexUpToDate.odex";
383
384 Copy(GetDexSrc1(), dex_location);
385 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
386
387 // Now replace the dex location with a symlink.
388 std::string link = scratch_dir + "/link";
389 ASSERT_EQ(0, symlink(scratch_dir.c_str(), link.c_str()));
390 dex_location = link + "/OdexUpToDate.jar";
391
392 OatFileAssistant oat_file_assistant(dex_location.c_str(),
393 kRuntimeISA,
394 default_context_.get(),
395 false);
396
397 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
398 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kSpeed));
399 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
400 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kVerify));
401 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
402 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kExtract));
403 EXPECT_EQ(-OatFileAssistant::kDex2OatForFilter,
404 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kEverything));
405
406 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
407 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus());
408 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
409 EXPECT_TRUE(oat_file_assistant.HasDexFiles());
410 }
411
412 // Case: We have a DEX file and up-to-date OAT file for it.
413 // Expect: The status is kNoDexOptNeeded.
TEST_F(OatFileAssistantTest,OatUpToDate)414 TEST_F(OatFileAssistantTest, OatUpToDate) {
415 if (IsExecutedAsRoot()) {
416 // We cannot simulate non writable locations when executed as root: b/38000545.
417 LOG(ERROR) << "Test skipped because it's running as root";
418 return;
419 }
420
421 std::string dex_location = GetScratchDir() + "/OatUpToDate.jar";
422 Copy(GetDexSrc1(), dex_location);
423 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
424
425 // Force the use of oat location by making the dex parent not writable.
426 ScopedNonWritable scoped_non_writable(dex_location);
427 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
428
429 OatFileAssistant oat_file_assistant(dex_location.c_str(),
430 kRuntimeISA,
431 default_context_.get(),
432 false);
433
434 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
435 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kSpeed));
436 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
437 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kVerify));
438 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
439 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kExtract));
440 EXPECT_EQ(OatFileAssistant::kDex2OatForFilter,
441 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kEverything));
442
443 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
444 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
445 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus());
446 EXPECT_TRUE(oat_file_assistant.HasDexFiles());
447
448 VerifyOptimizationStatus(
449 &oat_file_assistant,
450 dex_location,
451 CompilerFilter::kSpeed,
452 "unknown",
453 "up-to-date");
454 }
455
456 // Case: Passing valid file descriptors of updated odex/vdex files along with the dex file.
457 // Expect: The status is kNoDexOptNeeded.
TEST_F(OatFileAssistantTest,GetDexOptNeededWithFd)458 TEST_F(OatFileAssistantTest, GetDexOptNeededWithFd) {
459 std::string dex_location = GetScratchDir() + "/OatUpToDate.jar";
460 std::string odex_location = GetScratchDir() + "/OatUpToDate.odex";
461 std::string vdex_location = GetScratchDir() + "/OatUpToDate.vdex";
462
463 Copy(GetDexSrc1(), dex_location);
464 GenerateOatForTest(dex_location.c_str(),
465 odex_location.c_str(),
466 CompilerFilter::kSpeed,
467 /* with_alternate_image= */ false);
468
469 android::base::unique_fd odex_fd(open(odex_location.c_str(), O_RDONLY | O_CLOEXEC));
470 android::base::unique_fd vdex_fd(open(vdex_location.c_str(), O_RDONLY | O_CLOEXEC));
471 android::base::unique_fd zip_fd(open(dex_location.c_str(), O_RDONLY | O_CLOEXEC));
472
473 OatFileAssistant oat_file_assistant(dex_location.c_str(),
474 kRuntimeISA,
475 default_context_.get(),
476 false,
477 false,
478 vdex_fd.get(),
479 odex_fd.get(),
480 zip_fd.get());
481 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
482 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kSpeed));
483 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
484 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kVerify));
485 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
486 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kExtract));
487 EXPECT_EQ(-OatFileAssistant::kDex2OatForFilter,
488 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kEverything));
489
490 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
491 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus());
492 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
493 EXPECT_TRUE(oat_file_assistant.HasDexFiles());
494 }
495
496 // Case: Passing invalid odex fd and valid vdex and zip fds.
497 // Expect: The status should be kDex2OatForBootImage.
TEST_F(OatFileAssistantTest,GetDexOptNeededWithInvalidOdexFd)498 TEST_F(OatFileAssistantTest, GetDexOptNeededWithInvalidOdexFd) {
499 std::string dex_location = GetScratchDir() + "/OatUpToDate.jar";
500 std::string odex_location = GetScratchDir() + "/OatUpToDate.odex";
501 std::string vdex_location = GetScratchDir() + "/OatUpToDate.vdex";
502
503 Copy(GetDexSrc1(), dex_location);
504 GenerateOatForTest(dex_location.c_str(),
505 odex_location.c_str(),
506 CompilerFilter::kSpeed,
507 /* with_alternate_image= */ false);
508
509 android::base::unique_fd vdex_fd(open(vdex_location.c_str(), O_RDONLY | O_CLOEXEC));
510 android::base::unique_fd zip_fd(open(dex_location.c_str(), O_RDONLY | O_CLOEXEC));
511
512 OatFileAssistant oat_file_assistant(dex_location.c_str(),
513 kRuntimeISA,
514 default_context_.get(),
515 false,
516 false,
517 vdex_fd.get(),
518 /* oat_fd= */ -1,
519 zip_fd.get());
520 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
521 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kVerify));
522 EXPECT_EQ(-OatFileAssistant::kDex2OatForFilter,
523 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kSpeed));
524 EXPECT_EQ(-OatFileAssistant::kDex2OatForFilter,
525 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kEverything));
526
527 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
528 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
529 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
530 EXPECT_TRUE(oat_file_assistant.HasDexFiles());
531 }
532
533 // Case: Passing invalid vdex fd and valid odex and zip fds.
534 // Expect: The status should be kDex2OatFromScratch.
TEST_F(OatFileAssistantTest,GetDexOptNeededWithInvalidVdexFd)535 TEST_F(OatFileAssistantTest, GetDexOptNeededWithInvalidVdexFd) {
536 std::string dex_location = GetScratchDir() + "/OatUpToDate.jar";
537 std::string odex_location = GetScratchDir() + "/OatUpToDate.odex";
538
539 Copy(GetDexSrc1(), dex_location);
540 GenerateOatForTest(dex_location.c_str(),
541 odex_location.c_str(),
542 CompilerFilter::kSpeed,
543 /* with_alternate_image= */ false);
544
545 android::base::unique_fd odex_fd(open(odex_location.c_str(), O_RDONLY | O_CLOEXEC));
546 android::base::unique_fd zip_fd(open(dex_location.c_str(), O_RDONLY | O_CLOEXEC));
547
548 OatFileAssistant oat_file_assistant(dex_location.c_str(),
549 kRuntimeISA,
550 default_context_.get(),
551 false,
552 false,
553 /* vdex_fd= */ -1,
554 odex_fd.get(),
555 zip_fd.get());
556
557 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
558 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kSpeed));
559 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
560 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
561 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
562 EXPECT_TRUE(oat_file_assistant.HasDexFiles());
563 }
564
565 // Case: Passing invalid vdex and odex fd with valid zip fd.
566 // Expect: The status is kDex2oatFromScratch.
TEST_F(OatFileAssistantTest,GetDexOptNeededWithInvalidOdexVdexFd)567 TEST_F(OatFileAssistantTest, GetDexOptNeededWithInvalidOdexVdexFd) {
568 std::string dex_location = GetScratchDir() + "/OatUpToDate.jar";
569
570 Copy(GetDexSrc1(), dex_location);
571
572 android::base::unique_fd zip_fd(open(dex_location.c_str(), O_RDONLY | O_CLOEXEC));
573 OatFileAssistant oat_file_assistant(dex_location.c_str(),
574 kRuntimeISA,
575 default_context_.get(),
576 false,
577 false,
578 /* vdex_fd= */ -1,
579 /* oat_fd= */ -1,
580 zip_fd);
581 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
582 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kSpeed));
583 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
584 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
585 }
586
587 // Case: We have a DEX file and up-to-date VDEX file for it, but no
588 // ODEX file.
TEST_F(OatFileAssistantTest,VdexUpToDateNoOdex)589 TEST_F(OatFileAssistantTest, VdexUpToDateNoOdex) {
590 std::string dex_location = GetScratchDir() + "/VdexUpToDateNoOdex.jar";
591 std::string odex_location = GetOdexDir() + "/VdexUpToDateNoOdex.oat";
592
593 Copy(GetDexSrc1(), dex_location);
594
595 // Generating and deleting the oat file should have the side effect of
596 // creating an up-to-date vdex file.
597 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
598 ASSERT_EQ(0, unlink(odex_location.c_str()));
599
600 OatFileAssistant oat_file_assistant(dex_location.c_str(),
601 kRuntimeISA,
602 default_context_.get(),
603 false);
604
605 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
606 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kVerify));
607 EXPECT_EQ(-OatFileAssistant::kDex2OatForFilter,
608 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kSpeed));
609
610 // Make sure we don't crash in this case when we dump the status. We don't
611 // care what the actual dumped value is.
612 oat_file_assistant.GetStatusDump();
613
614 VerifyOptimizationStatus(
615 &oat_file_assistant,
616 dex_location,
617 "verify",
618 "vdex",
619 "up-to-date");
620 }
621
622 // Case: We have a DEX file and empty VDEX and ODEX files.
TEST_F(OatFileAssistantTest,EmptyVdexOdex)623 TEST_F(OatFileAssistantTest, EmptyVdexOdex) {
624 std::string dex_location = GetScratchDir() + "/EmptyVdexOdex.jar";
625 std::string odex_location = GetOdexDir() + "/EmptyVdexOdex.oat";
626 std::string vdex_location = GetOdexDir() + "/EmptyVdexOdex.vdex";
627
628 Copy(GetDexSrc1(), dex_location);
629 ScratchFile vdex_file(vdex_location.c_str());
630 ScratchFile odex_file(odex_location.c_str());
631
632 OatFileAssistant oat_file_assistant(dex_location.c_str(),
633 kRuntimeISA,
634 default_context_.get(),
635 false);
636 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
637 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kSpeed));
638 }
639
640 // Case: We have a DEX file and up-to-date (OAT) VDEX file for it, but no OAT
641 // file.
TEST_F(OatFileAssistantTest,VdexUpToDateNoOat)642 TEST_F(OatFileAssistantTest, VdexUpToDateNoOat) {
643 if (IsExecutedAsRoot()) {
644 // We cannot simulate non writable locations when executed as root: b/38000545.
645 LOG(ERROR) << "Test skipped because it's running as root";
646 return;
647 }
648
649 std::string dex_location = GetScratchDir() + "/VdexUpToDateNoOat.jar";
650 std::string oat_location;
651 std::string error_msg;
652 ASSERT_TRUE(OatFileAssistant::DexLocationToOatFilename(
653 dex_location, kRuntimeISA, &oat_location, &error_msg)) << error_msg;
654
655 Copy(GetDexSrc1(), dex_location);
656 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
657 ASSERT_EQ(0, unlink(oat_location.c_str()));
658
659 ScopedNonWritable scoped_non_writable(dex_location);
660 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
661 OatFileAssistant oat_file_assistant(dex_location.c_str(),
662 kRuntimeISA,
663 default_context_.get(),
664 false);
665
666 EXPECT_EQ(OatFileAssistant::kDex2OatForFilter,
667 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kSpeed));
668 }
669
670 // Case: We have a DEX file and speed-profile OAT file for it.
671 // Expect: The status is kNoDexOptNeeded if the profile hasn't changed, but
672 // kDex2Oat if the profile has changed.
TEST_F(OatFileAssistantTest,ProfileOatUpToDate)673 TEST_F(OatFileAssistantTest, ProfileOatUpToDate) {
674 if (IsExecutedAsRoot()) {
675 // We cannot simulate non writable locations when executed as root: b/38000545.
676 LOG(ERROR) << "Test skipped because it's running as root";
677 return;
678 }
679
680 std::string dex_location = GetScratchDir() + "/ProfileOatUpToDate.jar";
681 Copy(GetDexSrc1(), dex_location);
682 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeedProfile);
683
684 ScopedNonWritable scoped_non_writable(dex_location);
685 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
686
687 OatFileAssistant oat_file_assistant(dex_location.c_str(),
688 kRuntimeISA,
689 default_context_.get(),
690 false);
691
692 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
693 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kSpeedProfile, false));
694 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
695 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kVerify, false));
696 EXPECT_EQ(OatFileAssistant::kDex2OatForFilter,
697 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kSpeedProfile, true));
698 EXPECT_EQ(OatFileAssistant::kDex2OatForFilter,
699 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kVerify, true));
700
701 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
702 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
703 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus());
704 EXPECT_TRUE(oat_file_assistant.HasDexFiles());
705 }
706
707 // Case: We have a MultiDEX file and up-to-date OAT file for it.
708 // Expect: The status is kNoDexOptNeeded and we load all dex files.
TEST_F(OatFileAssistantTest,MultiDexOatUpToDate)709 TEST_F(OatFileAssistantTest, MultiDexOatUpToDate) {
710 if (IsExecutedAsRoot()) {
711 // We cannot simulate non writable locations when executed as root: b/38000545.
712 LOG(ERROR) << "Test skipped because it's running as root";
713 return;
714 }
715
716 std::string dex_location = GetScratchDir() + "/MultiDexOatUpToDate.jar";
717 Copy(GetMultiDexSrc1(), dex_location);
718 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
719
720 ScopedNonWritable scoped_non_writable(dex_location);
721 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
722
723 OatFileAssistant oat_file_assistant(dex_location.c_str(),
724 kRuntimeISA,
725 default_context_.get(),
726 true);
727 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
728 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kSpeed));
729 EXPECT_TRUE(oat_file_assistant.HasDexFiles());
730
731 // Verify we can load both dex files.
732 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
733 ASSERT_TRUE(oat_file.get() != nullptr);
734 EXPECT_TRUE(oat_file->IsExecutable());
735 std::vector<std::unique_ptr<const DexFile>> dex_files;
736 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
737 EXPECT_EQ(2u, dex_files.size());
738 }
739
740 // Case: We have a MultiDEX file where the non-main multdex entry is out of date.
741 // Expect: The status is kDex2OatNeeded.
TEST_F(OatFileAssistantTest,MultiDexNonMainOutOfDate)742 TEST_F(OatFileAssistantTest, MultiDexNonMainOutOfDate) {
743 if (IsExecutedAsRoot()) {
744 // We cannot simulate non writable locations when executed as root: b/38000545.
745 LOG(ERROR) << "Test skipped because it's running as root";
746 return;
747 }
748
749 std::string dex_location = GetScratchDir() + "/MultiDexNonMainOutOfDate.jar";
750
751 // Compile code for GetMultiDexSrc1.
752 Copy(GetMultiDexSrc1(), dex_location);
753 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
754
755 // Now overwrite the dex file with GetMultiDexSrc2 so the non-main checksum
756 // is out of date.
757 Copy(GetMultiDexSrc2(), dex_location);
758
759 ScopedNonWritable scoped_non_writable(dex_location);
760 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
761
762 OatFileAssistant oat_file_assistant(dex_location.c_str(),
763 kRuntimeISA,
764 default_context_.get(),
765 true);
766 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
767 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kSpeed));
768 EXPECT_TRUE(oat_file_assistant.HasDexFiles());
769 }
770
771 // Case: We have a DEX file and an OAT file out of date with respect to the
772 // dex checksum.
TEST_F(OatFileAssistantTest,OatDexOutOfDate)773 TEST_F(OatFileAssistantTest, OatDexOutOfDate) {
774 if (IsExecutedAsRoot()) {
775 // We cannot simulate non writable locations when executed as root: b/38000545.
776 LOG(ERROR) << "Test skipped because it's running as root";
777 return;
778 }
779
780 std::string dex_location = GetScratchDir() + "/OatDexOutOfDate.jar";
781
782 // We create a dex, generate an oat for it, then overwrite the dex with a
783 // different dex to make the oat out of date.
784 Copy(GetDexSrc1(), dex_location);
785 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
786 Copy(GetDexSrc2(), dex_location);
787
788 ScopedNonWritable scoped_non_writable(dex_location);
789 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
790
791 OatFileAssistant oat_file_assistant(dex_location.c_str(),
792 kRuntimeISA,
793 default_context_.get(),
794 false);
795 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
796 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kExtract));
797 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
798 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kSpeed));
799
800 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
801 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
802 EXPECT_EQ(OatFileAssistant::kOatDexOutOfDate, oat_file_assistant.OatFileStatus());
803 EXPECT_TRUE(oat_file_assistant.HasDexFiles());
804
805 VerifyOptimizationStatus(
806 &oat_file_assistant,
807 dex_location,
808 "run-from-apk-fallback",
809 "unknown",
810 "apk-more-recent");
811 }
812
813 // Case: We have a DEX file and an (ODEX) VDEX file out of date with respect
814 // to the dex checksum, but no ODEX file.
TEST_F(OatFileAssistantTest,VdexDexOutOfDate)815 TEST_F(OatFileAssistantTest, VdexDexOutOfDate) {
816 std::string dex_location = GetScratchDir() + "/VdexDexOutOfDate.jar";
817 std::string odex_location = GetOdexDir() + "/VdexDexOutOfDate.oat";
818
819 Copy(GetDexSrc1(), dex_location);
820 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
821 ASSERT_EQ(0, unlink(odex_location.c_str()));
822 Copy(GetDexSrc2(), dex_location);
823
824 OatFileAssistant oat_file_assistant(dex_location.c_str(),
825 kRuntimeISA,
826 default_context_.get(),
827 false);
828
829 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
830 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kSpeed));
831 }
832
833 // Case: We have a MultiDEX (ODEX) VDEX file where the non-main multidex entry
834 // is out of date and there is no corresponding ODEX file.
TEST_F(OatFileAssistantTest,VdexMultiDexNonMainOutOfDate)835 TEST_F(OatFileAssistantTest, VdexMultiDexNonMainOutOfDate) {
836 std::string dex_location = GetScratchDir() + "/VdexMultiDexNonMainOutOfDate.jar";
837 std::string odex_location = GetOdexDir() + "/VdexMultiDexNonMainOutOfDate.odex";
838
839 Copy(GetMultiDexSrc1(), dex_location);
840 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
841 ASSERT_EQ(0, unlink(odex_location.c_str()));
842 Copy(GetMultiDexSrc2(), dex_location);
843
844 OatFileAssistant oat_file_assistant(dex_location.c_str(),
845 kRuntimeISA,
846 default_context_.get(),
847 false);
848
849 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
850 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kSpeed));
851 }
852
853 // Case: We have a DEX file and an OAT file out of date with respect to the
854 // boot image.
TEST_F(OatFileAssistantTest,OatImageOutOfDate)855 TEST_F(OatFileAssistantTest, OatImageOutOfDate) {
856 if (IsExecutedAsRoot()) {
857 // We cannot simulate non writable locations when executed as root: b/38000545.
858 LOG(ERROR) << "Test skipped because it's running as root";
859 return;
860 }
861
862 std::string dex_location = GetScratchDir() + "/OatImageOutOfDate.jar";
863
864 Copy(GetDexSrc1(), dex_location);
865 GenerateOatForTest(dex_location.c_str(),
866 CompilerFilter::kSpeed,
867 /* with_alternate_image= */ true);
868
869 ScopedNonWritable scoped_non_writable(dex_location);
870 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
871
872 OatFileAssistant oat_file_assistant(dex_location.c_str(),
873 kRuntimeISA,
874 default_context_.get(),
875 false);
876 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
877 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kExtract));
878 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
879 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kVerify));
880 EXPECT_EQ(OatFileAssistant::kDex2OatForFilter,
881 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kSpeed));
882
883 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
884 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
885 EXPECT_EQ(OatFileAssistant::kOatBootImageOutOfDate, oat_file_assistant.OatFileStatus());
886 EXPECT_TRUE(oat_file_assistant.HasDexFiles());
887
888 VerifyOptimizationStatus(
889 &oat_file_assistant,
890 dex_location,
891 "verify",
892 "vdex",
893 "up-to-date");
894 }
895
896 // Case: We have a DEX file and a verify-at-runtime OAT file out of date with
897 // respect to the boot image.
898 // It shouldn't matter that the OAT file is out of date, because it is
899 // verify-at-runtime.
TEST_F(OatFileAssistantTest,OatVerifyAtRuntimeImageOutOfDate)900 TEST_F(OatFileAssistantTest, OatVerifyAtRuntimeImageOutOfDate) {
901 if (IsExecutedAsRoot()) {
902 // We cannot simulate non writable locations when executed as root: b/38000545.
903 LOG(ERROR) << "Test skipped because it's running as root";
904 return;
905 }
906
907 std::string dex_location = GetScratchDir() + "/OatVerifyAtRuntimeImageOutOfDate.jar";
908
909 Copy(GetDexSrc1(), dex_location);
910 GenerateOatForTest(dex_location.c_str(),
911 CompilerFilter::kExtract,
912 /* with_alternate_image= */ true);
913
914 ScopedNonWritable scoped_non_writable(dex_location);
915 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
916
917 OatFileAssistant oat_file_assistant(dex_location.c_str(),
918 kRuntimeISA,
919 default_context_.get(),
920 false);
921 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
922 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kExtract));
923 EXPECT_EQ(OatFileAssistant::kDex2OatForFilter,
924 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kVerify));
925
926 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
927 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
928 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus());
929 EXPECT_TRUE(oat_file_assistant.HasDexFiles());
930 }
931
932 // Case: We have a DEX file and an ODEX file, but no OAT file.
TEST_F(OatFileAssistantTest,DexOdexNoOat)933 TEST_F(OatFileAssistantTest, DexOdexNoOat) {
934 std::string dex_location = GetScratchDir() + "/DexOdexNoOat.jar";
935 std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex";
936
937 // Create the dex and odex files
938 Copy(GetDexSrc1(), dex_location);
939 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
940
941 // Verify the status.
942 OatFileAssistant oat_file_assistant(dex_location.c_str(),
943 kRuntimeISA,
944 default_context_.get(),
945 false);
946
947 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
948 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kExtract));
949 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
950 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kSpeed));
951
952 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
953 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus());
954 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
955 EXPECT_TRUE(oat_file_assistant.HasDexFiles());
956
957 // We should still be able to get the non-executable odex file to run from.
958 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
959 ASSERT_TRUE(oat_file.get() != nullptr);
960 }
961
962 // Case: We have a resource-only DEX file, no ODEX file and no
963 // OAT file. Expect: The status is kNoDexOptNeeded.
TEST_F(OatFileAssistantTest,ResourceOnlyDex)964 TEST_F(OatFileAssistantTest, ResourceOnlyDex) {
965 std::string dex_location = GetScratchDir() + "/ResourceOnlyDex.jar";
966
967 Copy(GetResourceOnlySrc1(), dex_location);
968
969 // Verify the status.
970 OatFileAssistant oat_file_assistant(dex_location.c_str(),
971 kRuntimeISA,
972 default_context_.get(),
973 true);
974
975 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
976 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kSpeed));
977 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
978 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kExtract));
979 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
980 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kVerify));
981
982 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
983 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
984 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
985 EXPECT_FALSE(oat_file_assistant.HasDexFiles());
986
987 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
988 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kSpeed));
989
990 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
991 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
992 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
993 EXPECT_FALSE(oat_file_assistant.HasDexFiles());
994 }
995
996 // Case: We have a DEX file, an ODEX file and an OAT file.
997 // Expect: It shouldn't crash. We should load the odex file executable.
TEST_F(OatFileAssistantTest,OdexOatOverlap)998 TEST_F(OatFileAssistantTest, OdexOatOverlap) {
999 std::string dex_location = GetScratchDir() + "/OdexOatOverlap.jar";
1000 std::string odex_location = GetOdexDir() + "/OdexOatOverlap.odex";
1001
1002 // Create the dex, the odex and the oat files.
1003 Copy(GetDexSrc1(), dex_location);
1004 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
1005 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
1006
1007 // Verify things don't go bad.
1008 OatFileAssistant oat_file_assistant(dex_location.c_str(),
1009 kRuntimeISA,
1010 default_context_.get(),
1011 true);
1012
1013 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
1014 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kSpeed));
1015
1016 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
1017 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus());
1018 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus());
1019 EXPECT_TRUE(oat_file_assistant.HasDexFiles());
1020
1021 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1022 ASSERT_TRUE(oat_file.get() != nullptr);
1023
1024 EXPECT_TRUE(oat_file->IsExecutable());
1025 std::vector<std::unique_ptr<const DexFile>> dex_files;
1026 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
1027 EXPECT_EQ(1u, dex_files.size());
1028 }
1029
1030 // Case: We have a DEX file and a VerifyAtRuntime ODEX file, but no OAT file.
1031 // Expect: The status is kNoDexOptNeeded, because VerifyAtRuntime contains no code.
TEST_F(OatFileAssistantTest,DexVerifyAtRuntimeOdexNoOat)1032 TEST_F(OatFileAssistantTest, DexVerifyAtRuntimeOdexNoOat) {
1033 std::string dex_location = GetScratchDir() + "/DexVerifyAtRuntimeOdexNoOat.jar";
1034 std::string odex_location = GetOdexDir() + "/DexVerifyAtRuntimeOdexNoOat.odex";
1035
1036 // Create the dex and odex files
1037 Copy(GetDexSrc1(), dex_location);
1038 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kExtract);
1039
1040 // Verify the status.
1041 OatFileAssistant oat_file_assistant(dex_location.c_str(),
1042 kRuntimeISA,
1043 default_context_.get(),
1044 false);
1045
1046 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
1047 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kExtract));
1048 EXPECT_EQ(-OatFileAssistant::kDex2OatForFilter,
1049 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kSpeed));
1050
1051 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
1052 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus());
1053 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
1054 EXPECT_TRUE(oat_file_assistant.HasDexFiles());
1055 }
1056
1057 // Case: We have a DEX file and up-to-date OAT file for it.
1058 // Expect: We should load an executable dex file.
TEST_F(OatFileAssistantTest,LoadOatUpToDate)1059 TEST_F(OatFileAssistantTest, LoadOatUpToDate) {
1060 if (IsExecutedAsRoot()) {
1061 // We cannot simulate non writable locations when executed as root: b/38000545.
1062 LOG(ERROR) << "Test skipped because it's running as root";
1063 return;
1064 }
1065
1066 std::string dex_location = GetScratchDir() + "/LoadOatUpToDate.jar";
1067
1068 Copy(GetDexSrc1(), dex_location);
1069 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
1070
1071 ScopedNonWritable scoped_non_writable(dex_location);
1072 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
1073
1074 // Load the oat using an oat file assistant.
1075 OatFileAssistant oat_file_assistant(dex_location.c_str(),
1076 kRuntimeISA,
1077 default_context_.get(),
1078 true);
1079
1080 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1081 ASSERT_TRUE(oat_file.get() != nullptr);
1082 EXPECT_TRUE(oat_file->IsExecutable());
1083 std::vector<std::unique_ptr<const DexFile>> dex_files;
1084 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
1085 EXPECT_EQ(1u, dex_files.size());
1086 }
1087
1088 // Case: We have a DEX file and up-to-date quicken OAT file for it.
1089 // Expect: We should still load the oat file as executable.
TEST_F(OatFileAssistantTest,LoadExecInterpretOnlyOatUpToDate)1090 TEST_F(OatFileAssistantTest, LoadExecInterpretOnlyOatUpToDate) {
1091 if (IsExecutedAsRoot()) {
1092 // We cannot simulate non writable locations when executed as root: b/38000545.
1093 LOG(ERROR) << "Test skipped because it's running as root";
1094 return;
1095 }
1096
1097 std::string dex_location = GetScratchDir() + "/LoadExecInterpretOnlyOatUpToDate.jar";
1098
1099 Copy(GetDexSrc1(), dex_location);
1100 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kVerify);
1101
1102 ScopedNonWritable scoped_non_writable(dex_location);
1103 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
1104
1105 // Load the oat using an oat file assistant.
1106 OatFileAssistant oat_file_assistant(dex_location.c_str(),
1107 kRuntimeISA,
1108 default_context_.get(),
1109 true);
1110
1111 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1112 ASSERT_TRUE(oat_file.get() != nullptr);
1113 EXPECT_TRUE(oat_file->IsExecutable());
1114 std::vector<std::unique_ptr<const DexFile>> dex_files;
1115 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
1116 EXPECT_EQ(1u, dex_files.size());
1117 }
1118
1119 // Case: We have a DEX file and up-to-date OAT file for it.
1120 // Expect: Loading non-executable should load the oat non-executable.
TEST_F(OatFileAssistantTest,LoadNoExecOatUpToDate)1121 TEST_F(OatFileAssistantTest, LoadNoExecOatUpToDate) {
1122 if (IsExecutedAsRoot()) {
1123 // We cannot simulate non writable locations when executed as root: b/38000545.
1124 LOG(ERROR) << "Test skipped because it's running as root";
1125 return;
1126 }
1127
1128 std::string dex_location = GetScratchDir() + "/LoadNoExecOatUpToDate.jar";
1129
1130 Copy(GetDexSrc1(), dex_location);
1131
1132 ScopedNonWritable scoped_non_writable(dex_location);
1133 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
1134
1135 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
1136
1137 // Load the oat using an oat file assistant.
1138 OatFileAssistant oat_file_assistant(dex_location.c_str(),
1139 kRuntimeISA,
1140 default_context_.get(),
1141 false);
1142
1143 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1144 ASSERT_TRUE(oat_file.get() != nullptr);
1145 EXPECT_FALSE(oat_file->IsExecutable());
1146 std::vector<std::unique_ptr<const DexFile>> dex_files;
1147 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
1148 EXPECT_EQ(1u, dex_files.size());
1149 }
1150
1151 // Turn an absolute path into a path relative to the current working
1152 // directory.
MakePathRelative(const std::string & target)1153 static std::string MakePathRelative(const std::string& target) {
1154 char buf[MAXPATHLEN];
1155 std::string cwd = getcwd(buf, MAXPATHLEN);
1156
1157 // Split the target and cwd paths into components.
1158 std::vector<std::string> target_path;
1159 std::vector<std::string> cwd_path;
1160 Split(target, '/', &target_path);
1161 Split(cwd, '/', &cwd_path);
1162
1163 // Reverse the path components, so we can use pop_back().
1164 std::reverse(target_path.begin(), target_path.end());
1165 std::reverse(cwd_path.begin(), cwd_path.end());
1166
1167 // Drop the common prefix of the paths. Because we reversed the path
1168 // components, this becomes the common suffix of target_path and cwd_path.
1169 while (!target_path.empty() && !cwd_path.empty()
1170 && target_path.back() == cwd_path.back()) {
1171 target_path.pop_back();
1172 cwd_path.pop_back();
1173 }
1174
1175 // For each element of the remaining cwd_path, add '..' to the beginning
1176 // of the target path. Because we reversed the path components, we add to
1177 // the end of target_path.
1178 for (unsigned int i = 0; i < cwd_path.size(); i++) {
1179 target_path.push_back("..");
1180 }
1181
1182 // Reverse again to get the right path order, and join to get the result.
1183 std::reverse(target_path.begin(), target_path.end());
1184 return android::base::Join(target_path, '/');
1185 }
1186
1187 // Case: Non-absolute path to Dex location.
1188 // Expect: Not sure, but it shouldn't crash.
TEST_F(OatFileAssistantTest,NonAbsoluteDexLocation)1189 TEST_F(OatFileAssistantTest, NonAbsoluteDexLocation) {
1190 std::string abs_dex_location = GetScratchDir() + "/NonAbsoluteDexLocation.jar";
1191 Copy(GetDexSrc1(), abs_dex_location);
1192
1193 std::string dex_location = MakePathRelative(abs_dex_location);
1194 OatFileAssistant oat_file_assistant(dex_location.c_str(),
1195 kRuntimeISA,
1196 default_context_.get(),
1197 true);
1198
1199 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
1200 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
1201 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kSpeed));
1202 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
1203 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
1204 }
1205
1206 // Case: Very short, non-existent Dex location.
1207 // Expect: kNoDexOptNeeded.
TEST_F(OatFileAssistantTest,ShortDexLocation)1208 TEST_F(OatFileAssistantTest, ShortDexLocation) {
1209 std::string dex_location = "/xx";
1210
1211 OatFileAssistant oat_file_assistant(dex_location.c_str(),
1212 kRuntimeISA,
1213 default_context_.get(),
1214 true);
1215
1216 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
1217 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
1218 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kSpeed));
1219 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
1220 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
1221 EXPECT_FALSE(oat_file_assistant.HasDexFiles());
1222 }
1223
1224 // Case: Non-standard extension for dex file.
1225 // Expect: The status is kDex2OatNeeded.
TEST_F(OatFileAssistantTest,LongDexExtension)1226 TEST_F(OatFileAssistantTest, LongDexExtension) {
1227 std::string dex_location = GetScratchDir() + "/LongDexExtension.jarx";
1228 Copy(GetDexSrc1(), dex_location);
1229
1230 OatFileAssistant oat_file_assistant(dex_location.c_str(),
1231 kRuntimeISA,
1232 default_context_.get(),
1233 false);
1234
1235 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
1236 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kSpeed));
1237
1238 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
1239 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
1240 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
1241 }
1242
1243 // A task to generate a dex location. Used by the RaceToGenerate test.
1244 class RaceGenerateTask : public Task {
1245 public:
RaceGenerateTask(OatFileAssistantTest & test,const std::string & dex_location,const std::string & oat_location,Mutex * lock)1246 RaceGenerateTask(OatFileAssistantTest& test,
1247 const std::string& dex_location,
1248 const std::string& oat_location,
1249 Mutex* lock)
1250 : test_(test),
1251 dex_location_(dex_location),
1252 oat_location_(oat_location),
1253 lock_(lock),
1254 loaded_oat_file_(nullptr)
1255 {}
1256
Run(Thread * self ATTRIBUTE_UNUSED)1257 void Run(Thread* self ATTRIBUTE_UNUSED) override {
1258 // Load the dex files, and save a pointer to the loaded oat file, so that
1259 // we can verify only one oat file was loaded for the dex location.
1260 std::vector<std::unique_ptr<const DexFile>> dex_files;
1261 std::vector<std::string> error_msgs;
1262 const OatFile* oat_file = nullptr;
1263 {
1264 MutexLock mu(Thread::Current(), *lock_);
1265 // Create the oat file.
1266 std::vector<std::string> args;
1267 args.push_back("--dex-file=" + dex_location_);
1268 args.push_back("--oat-file=" + oat_location_);
1269 std::string error_msg;
1270 ASSERT_TRUE(test_.Dex2Oat(args, &error_msg)) << error_msg;
1271 }
1272
1273 dex_files = Runtime::Current()->GetOatFileManager().OpenDexFilesFromOat(
1274 dex_location_.c_str(),
1275 Runtime::Current()->GetSystemClassLoader(),
1276 /*dex_elements=*/nullptr,
1277 &oat_file,
1278 &error_msgs);
1279 CHECK(!dex_files.empty()) << android::base::Join(error_msgs, '\n');
1280 if (dex_files[0]->GetOatDexFile() != nullptr) {
1281 loaded_oat_file_ = dex_files[0]->GetOatDexFile()->GetOatFile();
1282 }
1283 CHECK_EQ(loaded_oat_file_, oat_file);
1284 }
1285
GetLoadedOatFile() const1286 const OatFile* GetLoadedOatFile() const {
1287 return loaded_oat_file_;
1288 }
1289
1290 private:
1291 OatFileAssistantTest& test_;
1292 std::string dex_location_;
1293 std::string oat_location_;
1294 Mutex* lock_;
1295 const OatFile* loaded_oat_file_;
1296 };
1297
1298 // Test the case where dex2oat invocations race with multiple processes trying to
1299 // load the oat file.
TEST_F(OatFileAssistantTest,RaceToGenerate)1300 TEST_F(OatFileAssistantTest, RaceToGenerate) {
1301 std::string dex_location = GetScratchDir() + "/RaceToGenerate.jar";
1302 std::string oat_location = GetOdexDir() + "/RaceToGenerate.oat";
1303
1304 // Start the runtime to initialize the system's class loader.
1305 Thread::Current()->TransitionFromSuspendedToRunnable();
1306 runtime_->Start();
1307
1308 // We use the lib core dex file, because it's large, and hopefully should
1309 // take a while to generate.
1310 Copy(GetLibCoreDexFileNames()[0], dex_location);
1311
1312 const size_t kNumThreads = 32;
1313 Thread* self = Thread::Current();
1314 ThreadPool thread_pool("Oat file assistant test thread pool", kNumThreads);
1315 std::vector<std::unique_ptr<RaceGenerateTask>> tasks;
1316 Mutex lock("RaceToGenerate");
1317 for (size_t i = 0; i < kNumThreads; i++) {
1318 std::unique_ptr<RaceGenerateTask> task(
1319 new RaceGenerateTask(*this, dex_location, oat_location, &lock));
1320 thread_pool.AddTask(self, task.get());
1321 tasks.push_back(std::move(task));
1322 }
1323 thread_pool.StartWorkers(self);
1324 thread_pool.Wait(self, /* do_work= */ true, /* may_hold_locks= */ false);
1325
1326 // Verify that tasks which got an oat file got a unique one.
1327 std::set<const OatFile*> oat_files;
1328 for (auto& task : tasks) {
1329 const OatFile* oat_file = task->GetLoadedOatFile();
1330 if (oat_file != nullptr) {
1331 EXPECT_TRUE(oat_files.find(oat_file) == oat_files.end());
1332 oat_files.insert(oat_file);
1333 }
1334 }
1335 }
1336
1337 // Case: We have a DEX file and an ODEX file, and no OAT file,
1338 // Expect: We should load the odex file executable.
TEST_F(OatFileAssistantTest,LoadDexOdexNoOat)1339 TEST_F(OatFileAssistantTest, LoadDexOdexNoOat) {
1340 std::string dex_location = GetScratchDir() + "/LoadDexOdexNoOat.jar";
1341 std::string odex_location = GetOdexDir() + "/LoadDexOdexNoOat.odex";
1342
1343 // Create the dex and odex files
1344 Copy(GetDexSrc1(), dex_location);
1345 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
1346
1347 // Load the oat using an executable oat file assistant.
1348 OatFileAssistant oat_file_assistant(dex_location.c_str(),
1349 kRuntimeISA,
1350 default_context_.get(),
1351 true);
1352
1353 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1354 ASSERT_TRUE(oat_file.get() != nullptr);
1355 EXPECT_TRUE(oat_file->IsExecutable());
1356 std::vector<std::unique_ptr<const DexFile>> dex_files;
1357 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
1358 EXPECT_EQ(1u, dex_files.size());
1359 }
1360
1361 // Case: We have a MultiDEX file and an ODEX file, and no OAT file.
1362 // Expect: We should load the odex file executable.
TEST_F(OatFileAssistantTest,LoadMultiDexOdexNoOat)1363 TEST_F(OatFileAssistantTest, LoadMultiDexOdexNoOat) {
1364 std::string dex_location = GetScratchDir() + "/LoadMultiDexOdexNoOat.jar";
1365 std::string odex_location = GetOdexDir() + "/LoadMultiDexOdexNoOat.odex";
1366
1367 // Create the dex and odex files
1368 Copy(GetMultiDexSrc1(), dex_location);
1369 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
1370
1371 // Load the oat using an executable oat file assistant.
1372 OatFileAssistant oat_file_assistant(dex_location.c_str(),
1373 kRuntimeISA,
1374 default_context_.get(),
1375 true);
1376
1377 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1378 ASSERT_TRUE(oat_file.get() != nullptr);
1379 EXPECT_TRUE(oat_file->IsExecutable());
1380 std::vector<std::unique_ptr<const DexFile>> dex_files;
1381 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
1382 EXPECT_EQ(2u, dex_files.size());
1383 }
1384
TEST(OatFileAssistantUtilsTest,DexLocationToOdexFilename)1385 TEST(OatFileAssistantUtilsTest, DexLocationToOdexFilename) {
1386 std::string error_msg;
1387 std::string odex_file;
1388
1389 EXPECT_TRUE(OatFileAssistant::DexLocationToOdexFilename(
1390 "/foo/bar/baz.jar", InstructionSet::kArm, &odex_file, &error_msg)) << error_msg;
1391 EXPECT_EQ("/foo/bar/oat/arm/baz.odex", odex_file);
1392
1393 EXPECT_TRUE(OatFileAssistant::DexLocationToOdexFilename(
1394 "/foo/bar/baz.funnyext", InstructionSet::kArm, &odex_file, &error_msg)) << error_msg;
1395 EXPECT_EQ("/foo/bar/oat/arm/baz.odex", odex_file);
1396
1397 EXPECT_FALSE(OatFileAssistant::DexLocationToOdexFilename(
1398 "nopath.jar", InstructionSet::kArm, &odex_file, &error_msg));
1399 EXPECT_FALSE(OatFileAssistant::DexLocationToOdexFilename(
1400 "/foo/bar/baz_noext", InstructionSet::kArm, &odex_file, &error_msg));
1401 }
1402
1403 // Verify the dexopt status values from dalvik.system.DexFile
1404 // match the OatFileAssistant::DexOptStatus values.
TEST_F(OatFileAssistantTest,DexOptStatusValues)1405 TEST_F(OatFileAssistantTest, DexOptStatusValues) {
1406 std::pair<OatFileAssistant::DexOptNeeded, const char*> mapping[] = {
1407 {OatFileAssistant::kNoDexOptNeeded, "NO_DEXOPT_NEEDED"},
1408 {OatFileAssistant::kDex2OatFromScratch, "DEX2OAT_FROM_SCRATCH"},
1409 {OatFileAssistant::kDex2OatForBootImage, "DEX2OAT_FOR_BOOT_IMAGE"},
1410 {OatFileAssistant::kDex2OatForFilter, "DEX2OAT_FOR_FILTER"},
1411 };
1412
1413 ScopedObjectAccess soa(Thread::Current());
1414 StackHandleScope<1> hs(soa.Self());
1415 ClassLinker* linker = Runtime::Current()->GetClassLinker();
1416 Handle<mirror::Class> dexfile(
1417 hs.NewHandle(linker->FindSystemClass(soa.Self(), "Ldalvik/system/DexFile;")));
1418 ASSERT_FALSE(dexfile == nullptr);
1419 linker->EnsureInitialized(soa.Self(), dexfile, true, true);
1420
1421 for (std::pair<OatFileAssistant::DexOptNeeded, const char*> field : mapping) {
1422 ArtField* art_field = mirror::Class::FindStaticField(
1423 soa.Self(), dexfile.Get(), field.second, "I");
1424 ASSERT_FALSE(art_field == nullptr);
1425 EXPECT_EQ(art_field->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
1426 EXPECT_EQ(field.first, art_field->GetInt(dexfile.Get()));
1427 }
1428 }
1429
TEST_F(OatFileAssistantTest,GetDexOptNeededWithOutOfDateContext)1430 TEST_F(OatFileAssistantTest, GetDexOptNeededWithOutOfDateContext) {
1431 std::string dex_location = GetScratchDir() + "/TestDex.jar";
1432 std::string odex_location = GetOdexDir() + "/TestDex.odex";
1433
1434 std::string context_location = GetScratchDir() + "/ContextDex.jar";
1435 Copy(GetDexSrc1(), dex_location);
1436 Copy(GetDexSrc2(), context_location);
1437
1438 std::string context_str = "PCL[" + context_location + "]";
1439
1440 std::unique_ptr<ClassLoaderContext> context = ClassLoaderContext::Create(context_str);
1441 ASSERT_TRUE(context != nullptr);
1442 ASSERT_TRUE(context->OpenDexFiles());
1443
1444 std::string error_msg;
1445 std::vector<std::string> args;
1446 args.push_back("--dex-file=" + dex_location);
1447 args.push_back("--oat-file=" + odex_location);
1448 args.push_back("--class-loader-context=" + context_str);
1449 ASSERT_TRUE(Dex2Oat(args, &error_msg)) << error_msg;
1450
1451 // Update the context by overriding the jar file.
1452 Copy(GetMultiDexSrc2(), context_location);
1453
1454 {
1455 std::unique_ptr<ClassLoaderContext> updated_context = ClassLoaderContext::Create(context_str);
1456 ASSERT_TRUE(updated_context != nullptr);
1457 std::vector<int> context_fds;
1458 ASSERT_TRUE(updated_context->OpenDexFiles("", context_fds, /*only_read_checksums*/ true));
1459 OatFileAssistant oat_file_assistant(
1460 dex_location.c_str(), kRuntimeISA, updated_context.get(), false);
1461 // DexOptNeeded should advise compilation for filter when the context changes.
1462 EXPECT_EQ(-OatFileAssistant::kDex2OatForFilter,
1463 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kDefaultCompilerFilter));
1464 }
1465 {
1466 std::unique_ptr<ClassLoaderContext> updated_context = ClassLoaderContext::Create(context_str);
1467 ASSERT_TRUE(updated_context != nullptr);
1468 std::vector<int> context_fds;
1469 ASSERT_TRUE(updated_context->OpenDexFiles("", context_fds, /*only_read_checksums*/ true));
1470 OatFileAssistant oat_file_assistant(
1471 dex_location.c_str(), kRuntimeISA, updated_context.get(), false);
1472 // Now check that DexOptNeeded does not advise compilation if we only extracted the file.
1473 args.push_back("--compiler-filter=extract");
1474 ASSERT_TRUE(Dex2Oat(args, &error_msg)) << error_msg;
1475 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
1476 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kExtract));
1477 }
1478 {
1479 std::unique_ptr<ClassLoaderContext> updated_context = ClassLoaderContext::Create(context_str);
1480 ASSERT_TRUE(updated_context != nullptr);
1481 std::vector<int> context_fds;
1482 ASSERT_TRUE(updated_context->OpenDexFiles("", context_fds, /*only_read_checksums*/ true));
1483 OatFileAssistant oat_file_assistant(
1484 dex_location.c_str(), kRuntimeISA, updated_context.get(), false);
1485 // Now check that DexOptNeeded does not advise compilation if we only verify the file.
1486 args.push_back("--compiler-filter=verify");
1487 ASSERT_TRUE(Dex2Oat(args, &error_msg)) << error_msg;
1488 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
1489 GetDexOptNeeded(&oat_file_assistant, CompilerFilter::kExtract));
1490 }
1491 }
1492
1493 // Test that GetLocation of a dex file is the same whether the dex
1494 // filed is backed by an oat file or not.
TEST_F(OatFileAssistantTest,GetDexLocation)1495 TEST_F(OatFileAssistantTest, GetDexLocation) {
1496 std::string dex_location = GetScratchDir() + "/TestDex.jar";
1497 std::string oat_location = GetOdexDir() + "/TestDex.odex";
1498 std::string art_location = GetOdexDir() + "/TestDex.art";
1499
1500 // Start the runtime to initialize the system's class loader.
1501 Thread::Current()->TransitionFromSuspendedToRunnable();
1502 runtime_->Start();
1503
1504 Copy(GetDexSrc1(), dex_location);
1505
1506 std::vector<std::unique_ptr<const DexFile>> dex_files;
1507 std::vector<std::string> error_msgs;
1508 const OatFile* oat_file = nullptr;
1509
1510 dex_files = Runtime::Current()->GetOatFileManager().OpenDexFilesFromOat(
1511 dex_location.c_str(),
1512 Runtime::Current()->GetSystemClassLoader(),
1513 /*dex_elements=*/nullptr,
1514 &oat_file,
1515 &error_msgs);
1516 ASSERT_EQ(dex_files.size(), 1u) << android::base::Join(error_msgs, "\n");
1517 EXPECT_EQ(oat_file, nullptr);
1518 std::string stored_dex_location = dex_files[0]->GetLocation();
1519 {
1520 // Create the oat file.
1521 std::vector<std::string> args;
1522 args.push_back("--dex-file=" + dex_location);
1523 args.push_back("--dex-location=TestDex.jar");
1524 args.push_back("--oat-file=" + oat_location);
1525 args.push_back("--app-image-file=" + art_location);
1526 std::string error_msg;
1527 ASSERT_TRUE(DexoptTest::Dex2Oat(args, &error_msg)) << error_msg;
1528 }
1529 dex_files = Runtime::Current()->GetOatFileManager().OpenDexFilesFromOat(
1530 dex_location.c_str(),
1531 Runtime::Current()->GetSystemClassLoader(),
1532 /*dex_elements=*/nullptr,
1533 &oat_file,
1534 &error_msgs);
1535 ASSERT_EQ(dex_files.size(), 1u) << android::base::Join(error_msgs, "\n");
1536 ASSERT_NE(oat_file, nullptr);
1537 std::string oat_stored_dex_location = dex_files[0]->GetLocation();
1538 EXPECT_EQ(oat_stored_dex_location, stored_dex_location);
1539 }
1540
1541 // Test that a dex file on the platform location gets the right hiddenapi domain,
1542 // regardless of whether it has a backing oat file.
TEST_F(OatFileAssistantTest,SystemFrameworkDir)1543 TEST_F(OatFileAssistantTest, SystemFrameworkDir) {
1544 std::string filebase = "OatFileAssistantTestSystemFrameworkDir";
1545 std::string dex_location = GetAndroidRoot() + "/framework/" + filebase + ".jar";
1546 Copy(GetDexSrc1(), dex_location);
1547
1548 std::string odex_dir = GetAndroidRoot() + "/framework/oat/";
1549 mkdir(odex_dir.c_str(), 0700);
1550 odex_dir = odex_dir + std::string(GetInstructionSetString(kRuntimeISA));
1551 mkdir(odex_dir.c_str(), 0700);
1552 std::string oat_location = odex_dir + "/" + filebase + ".odex";
1553 std::string vdex_location = odex_dir + "/" + filebase + ".vdex";
1554 std::string art_location = odex_dir + "/" + filebase + ".art";
1555 // Clean up in case previous run crashed.
1556 remove(oat_location.c_str());
1557 remove(vdex_location.c_str());
1558 remove(art_location.c_str());
1559
1560 // Start the runtime to initialize the system's class loader.
1561 Thread::Current()->TransitionFromSuspendedToRunnable();
1562 runtime_->Start();
1563
1564 std::vector<std::unique_ptr<const DexFile>> dex_files_first;
1565 std::vector<std::unique_ptr<const DexFile>> dex_files_second;
1566 std::vector<std::string> error_msgs;
1567 const OatFile* oat_file = nullptr;
1568
1569 dex_files_first = Runtime::Current()->GetOatFileManager().OpenDexFilesFromOat(
1570 dex_location.c_str(),
1571 Runtime::Current()->GetSystemClassLoader(),
1572 /*dex_elements=*/nullptr,
1573 &oat_file,
1574 &error_msgs);
1575 ASSERT_EQ(dex_files_first.size(), 1u) << android::base::Join(error_msgs, "\n");
1576 EXPECT_EQ(oat_file, nullptr) << dex_location;
1577 EXPECT_EQ(dex_files_first[0]->GetOatDexFile(), nullptr);
1578
1579 // Register the dex file to get a domain.
1580 {
1581 ScopedObjectAccess soa(Thread::Current());
1582 Runtime::Current()->GetClassLinker()->RegisterDexFile(
1583 *dex_files_first[0],
1584 soa.Decode<mirror::ClassLoader>(Runtime::Current()->GetSystemClassLoader()));
1585 }
1586 std::string stored_dex_location = dex_files_first[0]->GetLocation();
1587 EXPECT_EQ(dex_files_first[0]->GetHiddenapiDomain(), hiddenapi::Domain::kPlatform);
1588 {
1589 // Create the oat file.
1590 std::vector<std::string> args;
1591 args.push_back("--dex-file=" + dex_location);
1592 args.push_back("--dex-location=" + filebase + ".jar");
1593 args.push_back("--oat-file=" + oat_location);
1594 args.push_back("--app-image-file=" + art_location);
1595 std::string error_msg;
1596 ASSERT_TRUE(DexoptTest::Dex2Oat(args, &error_msg)) << error_msg;
1597 }
1598 dex_files_second = Runtime::Current()->GetOatFileManager().OpenDexFilesFromOat(
1599 dex_location.c_str(),
1600 Runtime::Current()->GetSystemClassLoader(),
1601 /*dex_elements=*/nullptr,
1602 &oat_file,
1603 &error_msgs);
1604 ASSERT_EQ(dex_files_second.size(), 1u) << android::base::Join(error_msgs, "\n");
1605 ASSERT_NE(oat_file, nullptr);
1606 EXPECT_NE(dex_files_second[0]->GetOatDexFile(), nullptr);
1607 EXPECT_NE(dex_files_second[0]->GetOatDexFile()->GetOatFile(), nullptr);
1608
1609 // Register the dex file to get a domain.
1610 {
1611 ScopedObjectAccess soa(Thread::Current());
1612 Runtime::Current()->GetClassLinker()->RegisterDexFile(
1613 *dex_files_second[0],
1614 soa.Decode<mirror::ClassLoader>(Runtime::Current()->GetSystemClassLoader()));
1615 }
1616 std::string oat_stored_dex_location = dex_files_second[0]->GetLocation();
1617 EXPECT_EQ(oat_stored_dex_location, stored_dex_location);
1618 EXPECT_EQ(dex_files_second[0]->GetHiddenapiDomain(), hiddenapi::Domain::kPlatform);
1619 EXPECT_EQ(0, remove(oat_location.c_str()));
1620 }
1621
1622 // Make sure OAT files that require app images are not loaded as executable.
TEST_F(OatFileAssistantTest,LoadOatNoArt)1623 TEST_F(OatFileAssistantTest, LoadOatNoArt) {
1624 std::string dex_location = GetScratchDir() + "/TestDex.jar";
1625 std::string odex_location = GetOdexDir() + "/TestDex.odex";
1626 std::string art_location = GetOdexDir() + "/TestDex.art";
1627 Copy(GetDexSrc1(), dex_location);
1628 GenerateOdexForTest(dex_location,
1629 odex_location,
1630 CompilerFilter::kSpeed,
1631 "install",
1632 {
1633 "--app-image-file=" + art_location,
1634 });
1635
1636 unlink(art_location.c_str());
1637
1638 std::vector<std::string> error_msgs;
1639 const OatFile* oat_file = nullptr;
1640
1641 // Start the runtime to initialize the system's class loader.
1642 Thread::Current()->TransitionFromSuspendedToRunnable();
1643 runtime_->Start();
1644
1645 const auto dex_files = Runtime::Current()->GetOatFileManager().OpenDexFilesFromOat(
1646 dex_location.c_str(),
1647 Runtime::Current()->GetSystemClassLoader(),
1648 /*dex_elements=*/nullptr,
1649 &oat_file,
1650 &error_msgs);
1651
1652 EXPECT_FALSE(dex_files.empty());
1653 ASSERT_NE(oat_file, nullptr);
1654 EXPECT_FALSE(oat_file->IsExecutable());
1655 }
1656
TEST_F(OatFileAssistantTest,GetDexOptNeededWithApexVersions)1657 TEST_F(OatFileAssistantTest, GetDexOptNeededWithApexVersions) {
1658 std::string dex_location = GetScratchDir() + "/TestDex.jar";
1659 std::string odex_location = GetOdexDir() + "/TestDex.odex";
1660 Copy(GetDexSrc1(), dex_location);
1661
1662 // Test that using the current's runtime apex versions works.
1663 {
1664 std::string error_msg;
1665 std::vector<std::string> args;
1666 args.push_back("--dex-file=" + dex_location);
1667 args.push_back("--oat-file=" + odex_location);
1668 args.push_back("--apex-versions=" + Runtime::Current()->GetApexVersions());
1669 ASSERT_TRUE(Dex2Oat(args, &error_msg)) << error_msg;
1670
1671 OatFileAssistant oat_file_assistant(
1672 dex_location.c_str(), kRuntimeISA, default_context_.get(), false);
1673 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus());
1674 }
1675
1676 // Test that a subset of apex versions works.
1677 {
1678 std::string error_msg;
1679 std::vector<std::string> args;
1680 args.push_back("--dex-file=" + dex_location);
1681 args.push_back("--oat-file=" + odex_location);
1682 args.push_back("--apex-versions=" + Runtime::Current()->GetApexVersions().substr(0, 1));
1683 ASSERT_TRUE(Dex2Oat(args, &error_msg)) << error_msg;
1684
1685 OatFileAssistant oat_file_assistant(
1686 dex_location.c_str(), kRuntimeISA, default_context_.get(), false);
1687 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus());
1688 }
1689
1690 // Test that different apex versions require to recompile.
1691 {
1692 std::string error_msg;
1693 std::vector<std::string> args;
1694 args.push_back("--dex-file=" + dex_location);
1695 args.push_back("--oat-file=" + odex_location);
1696 args.push_back("--apex-versions=/1/2/3/4");
1697 ASSERT_TRUE(Dex2Oat(args, &error_msg)) << error_msg;
1698
1699 OatFileAssistant oat_file_assistant(
1700 dex_location.c_str(), kRuntimeISA, default_context_.get(), false);
1701 EXPECT_EQ(OatFileAssistant::kDex2OatForBootImage, oat_file_assistant.OdexFileStatus());
1702 }
1703 }
1704
1705 // TODO: More Tests:
1706 // * Test class linker falls back to unquickened dex for DexNoOat
1707 // * Test class linker falls back to unquickened dex for MultiDexNoOat
1708 // * Test using secondary isa
1709 // * Test for status of oat while oat is being generated (how?)
1710 // * Test case where 32 and 64 bit boot class paths differ,
1711 // and we ask IsInBootClassPath for a class in exactly one of the 32 or
1712 // 64 bit boot class paths.
1713 // * Test unexpected scenarios (?):
1714 // - Dex is stripped, don't have odex.
1715 // - Oat file corrupted after status check, before reload unexecutable
1716 // because it's unrelocated and no dex2oat
1717 } // namespace art
1718