-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathdex2oat_test.cc
2422 lines (2184 loc) · 107 KB
/
dex2oat_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <sys/wait.h>
#include <unistd.h>
#include <algorithm>
#include <iterator>
#include <optional>
#include <regex>
#include <sstream>
#include <string>
#include <vector>
#include "android-base/logging.h"
#include "android-base/macros.h"
#include "android-base/stringprintf.h"
#include "arch/instruction_set_features.h"
#include "base/macros.h"
#include "base/mutex-inl.h"
#include "base/string_view_cpp20.h"
#include "base/utils.h"
#include "base/zip_archive.h"
#include "common_runtime_test.h"
#include "dex/art_dex_file_loader.h"
#include "dex/base64_test_util.h"
#include "dex/bytecode_utils.h"
#include "dex/class_accessor-inl.h"
#include "dex/code_item_accessors-inl.h"
#include "dex/dex_file-inl.h"
#include "dex/dex_file_loader.h"
#include "dex2oat_environment_test.h"
#include "gc_root-inl.h"
#include "intern_table-inl.h"
#include "oat/elf_file.h"
#include "oat/elf_file_impl.h"
#include "oat/oat.h"
#include "oat/oat_file.h"
#include "profile/profile_compilation_info.h"
#include "vdex_file.h"
#include "ziparchive/zip_writer.h"
namespace art {
using android::base::StringPrintf;
class Dex2oatTest : public Dex2oatEnvironmentTest {
public:
void TearDown() override {
Dex2oatEnvironmentTest::TearDown();
output_ = "";
error_msg_ = "";
}
protected:
int GenerateOdexForTestWithStatus(const std::vector<std::string>& dex_locations,
const std::string& odex_location,
CompilerFilter::Filter filter,
std::string* error_msg,
const std::vector<std::string>& extra_args = {},
bool use_fd = false) {
std::unique_ptr<File> oat_file;
std::vector<std::string> args;
args.reserve(dex_locations.size() + extra_args.size() + 6);
// Add dex file args.
for (const std::string& dex_location : dex_locations) {
args.push_back("--dex-file=" + dex_location);
}
if (use_fd) {
oat_file.reset(OS::CreateEmptyFile(odex_location.c_str()));
CHECK(oat_file != nullptr) << odex_location;
args.push_back("--oat-fd=" + std::to_string(oat_file->Fd()));
args.push_back("--oat-location=" + odex_location);
} else {
args.push_back("--oat-file=" + odex_location);
}
args.push_back("--compiler-filter=" + CompilerFilter::NameOfFilter(filter));
args.push_back("--runtime-arg");
args.push_back("-Xnorelocate");
// Unless otherwise stated, use a small amount of threads, so that potential aborts are
// shorter. This can be overridden with extra_args.
args.push_back("-j4");
args.insert(args.end(), extra_args.begin(), extra_args.end());
int status = Dex2Oat(args, &output_, error_msg);
if (oat_file != nullptr) {
CHECK_EQ(oat_file->FlushClose(), 0) << "Could not flush and close oat file";
}
return status;
}
::testing::AssertionResult GenerateOdexForTest(const std::string& dex_location,
const std::string& odex_location,
CompilerFilter::Filter filter,
const std::vector<std::string>& extra_args = {},
bool expect_success = true,
bool use_fd = false,
bool use_zip_fd = false) WARN_UNUSED {
return GenerateOdexForTest(dex_location,
odex_location,
filter,
extra_args,
expect_success,
use_fd,
use_zip_fd,
[](const OatFile&) {});
}
bool test_accepts_odex_file_on_failure = false;
template <typename T>
::testing::AssertionResult GenerateOdexForTest(const std::string& dex_location,
const std::string& odex_location,
CompilerFilter::Filter filter,
const std::vector<std::string>& extra_args,
bool expect_success,
bool use_fd,
bool use_zip_fd,
T check_oat) WARN_UNUSED {
std::vector<std::string> dex_locations;
if (use_zip_fd) {
std::string loc_arg = "--zip-location=" + dex_location;
CHECK(std::any_of(extra_args.begin(), extra_args.end(), [&](const std::string& s) {
return s == loc_arg;
}));
CHECK(std::any_of(extra_args.begin(), extra_args.end(), [](const std::string& s) {
return StartsWith(s, "--zip-fd=");
}));
} else {
dex_locations.push_back(dex_location);
}
std::string error_msg;
int status = GenerateOdexForTestWithStatus(
dex_locations, odex_location, filter, &error_msg, extra_args, use_fd);
bool success = (WIFEXITED(status) && WEXITSTATUS(status) == 0);
if (expect_success) {
if (!success) {
return ::testing::AssertionFailure() << "Failed to compile odex: " << error_msg << std::endl
<< output_;
}
// Verify the odex file was generated as expected.
std::unique_ptr<OatFile> odex_file(OatFile::Open(/*zip_fd=*/-1,
odex_location,
odex_location,
/*executable=*/false,
/*low_4gb=*/false,
dex_location,
&error_msg));
if (odex_file == nullptr) {
return ::testing::AssertionFailure() << "Could not open odex file: " << error_msg;
}
CheckFilter(filter, odex_file->GetCompilerFilter());
check_oat(*(odex_file.get()));
} else {
if (success) {
return ::testing::AssertionFailure() << "Succeeded to compile odex: " << output_;
}
error_msg_ = error_msg;
if (!test_accepts_odex_file_on_failure) {
// Verify there's no loadable odex file.
std::unique_ptr<OatFile> odex_file(OatFile::Open(/*zip_fd=*/-1,
odex_location,
odex_location,
/*executable=*/false,
/*low_4gb=*/false,
dex_location,
&error_msg));
if (odex_file != nullptr) {
return ::testing::AssertionFailure() << "Could open odex file: " << error_msg;
}
}
}
return ::testing::AssertionSuccess();
}
// Check the input compiler filter against the generated oat file's filter. May be overridden
// in subclasses when equality is not expected.
virtual void CheckFilter(CompilerFilter::Filter expected, CompilerFilter::Filter actual) {
EXPECT_EQ(expected, actual);
}
std::string output_ = "";
std::string error_msg_ = "";
};
// This test class provides an easy way to validate an expected filter which is different
// then the one pass to generate the odex file (compared to adding yet another argument
// to what's already huge test methods).
class Dex2oatWithExpectedFilterTest : public Dex2oatTest {
protected:
void CheckFilter([[maybe_unused]] CompilerFilter::Filter expected,
CompilerFilter::Filter actual) override {
EXPECT_EQ(expected_filter_, actual);
}
CompilerFilter::Filter expected_filter_;
};
class Dex2oatSwapTest : public Dex2oatTest {
protected:
void RunTest(bool use_fd, bool expect_use, const std::vector<std::string>& extra_args = {}) {
std::string dex_location = GetScratchDir() + "/Dex2OatSwapTest.jar";
std::string odex_location = GetOdexDir() + "/Dex2OatSwapTest.odex";
Copy(GetTestDexFileName(), dex_location);
std::vector<std::string> copy(extra_args);
std::unique_ptr<ScratchFile> sf;
if (use_fd) {
sf.reset(new ScratchFile());
copy.push_back(android::base::StringPrintf("--swap-fd=%d", sf->GetFd()));
} else {
std::string swap_location = GetOdexDir() + "/Dex2OatSwapTest.odex.swap";
copy.push_back("--swap-file=" + swap_location);
}
ASSERT_TRUE(GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed, copy));
CheckValidity();
CheckResult(expect_use);
}
virtual std::string GetTestDexFileName() {
return Dex2oatEnvironmentTest::GetTestDexFileName("VerifierDeps");
}
virtual void CheckResult(bool expect_use) {
if (kIsTargetBuild) {
CheckTargetResult(expect_use);
} else {
CheckHostResult(expect_use);
}
}
virtual void CheckTargetResult([[maybe_unused]] bool expect_use) {
// TODO: Ignore for now, as we won't capture any output (it goes to the logcat). We may do
// something for variants with file descriptor where we can control the lifetime of
// the swap file and thus take a look at it.
}
virtual void CheckHostResult(bool expect_use) {
if (!kIsTargetBuild) {
if (expect_use) {
EXPECT_NE(output_.find("Large app, accepted running with swap."), std::string::npos)
<< output_;
} else {
EXPECT_EQ(output_.find("Large app, accepted running with swap."), std::string::npos)
<< output_;
}
}
}
// Check whether the dex2oat run was really successful.
virtual void CheckValidity() {
if (kIsTargetBuild) {
CheckTargetValidity();
} else {
CheckHostValidity();
}
}
virtual void CheckTargetValidity() {
// TODO: Ignore for now, as we won't capture any output (it goes to the logcat). We may do
// something for variants with file descriptor where we can control the lifetime of
// the swap file and thus take a look at it.
}
// On the host, we can get the dex2oat output. Here, look for "dex2oat took."
virtual void CheckHostValidity() {
EXPECT_NE(output_.find("dex2oat took"), std::string::npos) << output_;
}
};
TEST_F(Dex2oatSwapTest, DoNotUseSwapDefaultSingleSmall) {
RunTest(/*use_fd=*/false, /*expect_use=*/false);
RunTest(/*use_fd=*/true, /*expect_use=*/false);
}
TEST_F(Dex2oatSwapTest, DoNotUseSwapSingle) {
RunTest(/*use_fd=*/false, /*expect_use=*/false, {"--swap-dex-size-threshold=0"});
RunTest(/*use_fd=*/true, /*expect_use=*/false, {"--swap-dex-size-threshold=0"});
}
TEST_F(Dex2oatSwapTest, DoNotUseSwapSmall) {
RunTest(/*use_fd=*/false, /*expect_use=*/false, {"--swap-dex-count-threshold=0"});
RunTest(/*use_fd=*/true, /*expect_use=*/false, {"--swap-dex-count-threshold=0"});
}
TEST_F(Dex2oatSwapTest, DoUseSwapSingleSmall) {
RunTest(/*use_fd=*/false,
/*expect_use=*/true,
{"--swap-dex-size-threshold=0", "--swap-dex-count-threshold=0"});
RunTest(/*use_fd=*/true,
/*expect_use=*/true,
{"--swap-dex-size-threshold=0", "--swap-dex-count-threshold=0"});
}
class Dex2oatSwapUseTest : public Dex2oatSwapTest {
protected:
void CheckHostResult(bool expect_use) override {
if (!kIsTargetBuild) {
if (expect_use) {
EXPECT_NE(output_.find("Large app, accepted running with swap."), std::string::npos)
<< output_;
} else {
EXPECT_EQ(output_.find("Large app, accepted running with swap."), std::string::npos)
<< output_;
}
}
}
std::string GetTestDexFileName() override {
// Use Statics as it has a handful of functions.
return CommonRuntimeTest::GetTestDexFileName("Statics");
}
void GrabResult1() {
if (!kIsTargetBuild) {
native_alloc_1_ = ParseNativeAlloc();
swap_1_ = ParseSwap(/*expected=*/false);
} else {
native_alloc_1_ = std::numeric_limits<size_t>::max();
swap_1_ = 0;
}
}
void GrabResult2() {
if (!kIsTargetBuild) {
native_alloc_2_ = ParseNativeAlloc();
swap_2_ = ParseSwap(/*expected=*/true);
} else {
native_alloc_2_ = 0;
swap_2_ = std::numeric_limits<size_t>::max();
}
}
private:
size_t ParseNativeAlloc() {
std::regex native_alloc_regex("dex2oat took.*native alloc=[^ ]+ \\(([0-9]+)B\\)");
std::smatch native_alloc_match;
bool found = std::regex_search(output_, native_alloc_match, native_alloc_regex);
if (!found) {
EXPECT_TRUE(found);
return 0;
}
if (native_alloc_match.size() != 2U) {
EXPECT_EQ(native_alloc_match.size(), 2U);
return 0;
}
std::istringstream stream(native_alloc_match[1].str());
size_t value;
stream >> value;
return value;
}
size_t ParseSwap(bool expected) {
std::regex swap_regex("dex2oat took[^\\n]+swap=[^ ]+ \\(([0-9]+)B\\)");
std::smatch swap_match;
bool found = std::regex_search(output_, swap_match, swap_regex);
if (found != expected) {
EXPECT_EQ(expected, found);
return 0;
}
if (!found) {
return 0;
}
if (swap_match.size() != 2U) {
EXPECT_EQ(swap_match.size(), 2U);
return 0;
}
std::istringstream stream(swap_match[1].str());
size_t value;
stream >> value;
return value;
}
protected:
size_t native_alloc_1_;
size_t native_alloc_2_;
size_t swap_1_;
size_t swap_2_;
};
TEST_F(Dex2oatSwapUseTest, CheckSwapUsage) {
// Native memory usage isn't correctly tracked when running under ASan.
TEST_DISABLED_FOR_MEMORY_TOOL();
// The `native_alloc_2_ >= native_alloc_1_` assertion below may not
// hold true on some x86 or x86_64 systems; disable this test while we
// investigate (b/29259363).
TEST_DISABLED_FOR_X86();
TEST_DISABLED_FOR_X86_64();
RunTest(/*use_fd=*/false,
/*expect_use=*/false);
GrabResult1();
std::string output_1 = output_;
output_ = "";
RunTest(/*use_fd=*/false,
/*expect_use=*/true,
{"--swap-dex-size-threshold=0", "--swap-dex-count-threshold=0"});
GrabResult2();
std::string output_2 = output_;
if (native_alloc_2_ >= native_alloc_1_ || swap_1_ >= swap_2_) {
EXPECT_LT(native_alloc_2_, native_alloc_1_);
EXPECT_LT(swap_1_, swap_2_);
LOG(ERROR) << output_1;
LOG(ERROR) << output_2;
}
}
class Dex2oatVeryLargeTest : public Dex2oatTest {
protected:
void CheckFilter([[maybe_unused]] CompilerFilter::Filter input,
[[maybe_unused]] CompilerFilter::Filter result) override {
// Ignore, we'll do our own checks.
}
void RunTest(CompilerFilter::Filter filter,
bool expect_large,
bool expect_downgrade,
const std::vector<std::string>& extra_args = {}) {
RunTest(filter, filter, expect_large, expect_downgrade, extra_args);
}
void RunTest(CompilerFilter::Filter filter,
CompilerFilter::Filter expected_filter,
bool expect_large,
bool expect_downgrade,
const std::vector<std::string>& extra_args = {}) {
std::string dex_location = GetScratchDir() + "/DexNoOat.jar";
std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex";
std::string app_image_file = GetScratchDir() + "/Test.art";
Copy(GetDexSrc1(), dex_location);
std::vector<std::string> new_args(extra_args);
new_args.push_back("--app-image-file=" + app_image_file);
ASSERT_TRUE(GenerateOdexForTest(dex_location, odex_location, filter, new_args));
CheckValidity();
CheckResult(dex_location,
odex_location,
app_image_file,
expected_filter,
expect_large,
expect_downgrade);
}
void CheckResult(const std::string& dex_location,
const std::string& odex_location,
const std::string& app_image_file,
CompilerFilter::Filter expected_filter,
bool expect_large,
bool expect_downgrade) {
if (expect_downgrade) {
EXPECT_TRUE(expect_large);
}
// Host/target independent checks.
std::string error_msg;
std::unique_ptr<OatFile> odex_file(OatFile::Open(/*zip_fd=*/-1,
odex_location,
odex_location,
/*executable=*/false,
/*low_4gb=*/false,
dex_location,
&error_msg));
ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
EXPECT_GT(app_image_file.length(), 0u);
std::unique_ptr<File> file(OS::OpenFileForReading(app_image_file.c_str()));
if (expect_large) {
// Note: we cannot check the following
// EXPECT_FALSE(CompilerFilter::IsAotCompilationEnabled(odex_file->GetCompilerFilter()));
// The reason is that the filter override currently happens when the dex files are
// loaded in dex2oat, which is after the oat file has been started. Thus, the header
// store cannot be changed, and the original filter is set in stone.
for (const OatDexFile* oat_dex_file : odex_file->GetOatDexFiles()) {
std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg);
ASSERT_TRUE(dex_file != nullptr);
uint32_t class_def_count = dex_file->NumClassDefs();
ASSERT_LT(class_def_count, std::numeric_limits<uint16_t>::max());
for (uint16_t class_def_index = 0; class_def_index < class_def_count; ++class_def_index) {
OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
EXPECT_EQ(oat_class.GetType(), OatClassType::kNoneCompiled);
}
}
// If the input filter was "below," it should have been used.
EXPECT_EQ(odex_file->GetCompilerFilter(), expected_filter);
// If expect large, make sure the app image isn't generated or is empty.
if (file != nullptr) {
EXPECT_EQ(file->GetLength(), 0u);
}
} else {
EXPECT_EQ(odex_file->GetCompilerFilter(), expected_filter);
ASSERT_TRUE(file != nullptr) << app_image_file;
EXPECT_GT(file->GetLength(), 0u);
}
// Host/target dependent checks.
if (kIsTargetBuild) {
CheckTargetResult(expect_downgrade);
} else {
CheckHostResult(expect_downgrade);
}
}
void CheckTargetResult([[maybe_unused]] bool expect_downgrade) {
// TODO: Ignore for now. May do something for fd things.
}
void CheckHostResult(bool expect_downgrade) {
if (!kIsTargetBuild) {
if (expect_downgrade) {
EXPECT_NE(output_.find("Very large app, downgrading to"), std::string::npos) << output_;
} else {
EXPECT_EQ(output_.find("Very large app, downgrading to"), std::string::npos) << output_;
}
}
}
// Check whether the dex2oat run was really successful.
void CheckValidity() {
if (kIsTargetBuild) {
CheckTargetValidity();
} else {
CheckHostValidity();
}
}
void CheckTargetValidity() {
// TODO: Ignore for now.
}
// On the host, we can get the dex2oat output. Here, look for "dex2oat took."
void CheckHostValidity() {
EXPECT_NE(output_.find("dex2oat took"), std::string::npos) << output_;
}
};
TEST_F(Dex2oatVeryLargeTest, DontUseVeryLarge) {
RunTest(CompilerFilter::kAssumeVerified, false, false);
RunTest(CompilerFilter::kSpeed, false, false);
RunTest(CompilerFilter::kAssumeVerified, false, false, {"--very-large-app-threshold=10000000"});
RunTest(CompilerFilter::kSpeed, false, false, {"--very-large-app-threshold=10000000"});
}
TEST_F(Dex2oatVeryLargeTest, UseVeryLarge) {
RunTest(CompilerFilter::kAssumeVerified, true, false, {"--very-large-app-threshold=100"});
RunTest(CompilerFilter::kSpeed, true, true, {"--very-large-app-threshold=100"});
}
// Regressin test for b/35665292.
TEST_F(Dex2oatVeryLargeTest, SpeedProfileNoProfile) {
// Test that dex2oat doesn't crash with speed-profile but no input profile.
RunTest(CompilerFilter::kSpeedProfile, CompilerFilter::kVerify, false, false);
}
class Dex2oatLayoutTest : public Dex2oatTest {
protected:
void CheckFilter([[maybe_unused]] CompilerFilter::Filter input,
[[maybe_unused]] CompilerFilter::Filter result) override {
// Ignore, we'll do our own checks.
}
// Emits a profile with a single dex file with the given location and classes ranging
// from `class_offset` to `class_offset + num_classes`.
void GenerateProfile(const std::string& test_profile,
const std::string& dex_location,
size_t num_classes,
size_t class_offset = 0) {
const char* location = dex_location.c_str();
std::string error_msg;
std::vector<std::unique_ptr<const DexFile>> dex_files;
ArtDexFileLoader dex_file_loader(location);
ASSERT_TRUE(dex_file_loader.Open(
/*verify=*/true, /*verify_checksum=*/true, &error_msg, &dex_files));
EXPECT_EQ(dex_files.size(), 1U);
std::unique_ptr<const DexFile>& dex_file = dex_files[0];
int profile_test_fd =
open(test_profile.c_str(), O_CREAT | O_TRUNC | O_WRONLY | O_CLOEXEC, 0644);
CHECK_GE(profile_test_fd, 0);
ProfileCompilationInfo info;
std::vector<dex::TypeIndex> classes;
for (size_t i = 0; i < num_classes; ++i) {
classes.push_back(dex::TypeIndex(class_offset + 1 + i));
}
info.AddClassesForDex(dex_file.get(), classes.begin(), classes.end());
bool result = info.Save(profile_test_fd);
close(profile_test_fd);
ASSERT_TRUE(result);
}
// Compiles a dex file with profiles.
void CompileProfileOdex(const std::string& dex_location,
const std::string& odex_location,
const std::string& app_image_file_name,
bool use_fd,
const std::vector<std::string>& profile_locations,
const std::vector<std::string>& extra_args = {},
bool expect_success = true) {
std::vector<std::string> copy(extra_args);
for (const std::string& profile_location : profile_locations) {
copy.push_back("--profile-file=" + profile_location);
}
std::unique_ptr<File> app_image_file;
if (!app_image_file_name.empty()) {
if (use_fd) {
app_image_file.reset(OS::CreateEmptyFile(app_image_file_name.c_str()));
copy.push_back("--app-image-fd=" + std::to_string(app_image_file->Fd()));
} else {
copy.push_back("--app-image-file=" + app_image_file_name);
}
}
ASSERT_TRUE(GenerateOdexForTest(
dex_location, odex_location, CompilerFilter::kSpeedProfile, copy, expect_success, use_fd));
if (app_image_file != nullptr) {
ASSERT_EQ(app_image_file->FlushCloseOrErase(), 0) << "Could not flush and close art file";
}
}
// Same as above, but generates the profile internally with classes ranging from 0 to
// `num_profile_classes`.
void CompileProfileOdex(const std::string& dex_location,
const std::string& odex_location,
const std::string& app_image_file_name,
bool use_fd,
size_t num_profile_classes,
const std::vector<std::string>& extra_args = {},
bool expect_success = true) {
const std::string profile_location = GetScratchDir() + "/primary.prof";
GenerateProfile(profile_location, dex_location, num_profile_classes);
CompileProfileOdex(dex_location,
odex_location,
app_image_file_name,
use_fd,
{profile_location},
extra_args,
expect_success);
}
uint32_t GetImageObjectSectionSize(const std::string& image_file_name) {
EXPECT_FALSE(image_file_name.empty());
std::unique_ptr<File> file(OS::OpenFileForReading(image_file_name.c_str()));
CHECK(file != nullptr);
ImageHeader image_header;
const bool success = file->ReadFully(&image_header, sizeof(image_header));
CHECK(success);
CHECK(image_header.IsValid());
ReaderMutexLock mu(Thread::Current(), *Locks::mutator_lock_);
return image_header.GetObjectsSection().Size();
}
void RunTest(bool app_image) {
std::string dex_location = GetScratchDir() + "/DexNoOat.jar";
std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex";
std::string app_image_file = app_image ? (GetOdexDir() + "/DexOdexNoOat.art") : "";
Copy(GetDexSrc2(), dex_location);
uint32_t image_file_empty_profile = 0;
if (app_image) {
CompileProfileOdex(dex_location,
odex_location,
app_image_file,
/*use_fd=*/false,
/*num_profile_classes=*/0);
CheckValidity();
// Don't check the result since CheckResult relies on the class being in the profile.
image_file_empty_profile = GetImageObjectSectionSize(app_image_file);
EXPECT_GT(image_file_empty_profile, 0u);
CheckCompilerFilter(dex_location, odex_location, CompilerFilter::Filter::kVerify);
}
// Small profile.
CompileProfileOdex(dex_location,
odex_location,
app_image_file,
/*use_fd=*/false,
/*num_profile_classes=*/1);
CheckValidity();
CheckResult(dex_location, odex_location, app_image_file);
CheckCompilerFilter(dex_location, odex_location, CompilerFilter::Filter::kSpeedProfile);
if (app_image) {
// Test that the profile made a difference by adding more classes.
const uint32_t image_file_small_profile = GetImageObjectSectionSize(app_image_file);
ASSERT_LT(image_file_empty_profile, image_file_small_profile);
}
}
void CheckCompilerFilter(const std::string& dex_location,
const std::string& odex_location,
CompilerFilter::Filter expected_filter) {
std::string error_msg;
std::unique_ptr<OatFile> odex_file(OatFile::Open(/*zip_fd=*/-1,
odex_location,
odex_location,
/*executable=*/false,
/*low_4gb=*/false,
dex_location,
&error_msg));
EXPECT_EQ(odex_file->GetCompilerFilter(), expected_filter);
}
void RunTestVDex() {
std::string dex_location = GetScratchDir() + "/DexNoOat.jar";
std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex";
std::string vdex_location = GetOdexDir() + "/DexOdexNoOat.vdex";
std::string app_image_file_name = GetOdexDir() + "/DexOdexNoOat.art";
Copy(GetDexSrc2(), dex_location);
std::unique_ptr<File> vdex_file1(OS::CreateEmptyFile(vdex_location.c_str()));
CHECK(vdex_file1 != nullptr) << vdex_location;
ScratchFile vdex_file2;
{
std::string input_vdex = "--input-vdex-fd=-1";
std::string output_vdex = StringPrintf("--output-vdex-fd=%d", vdex_file1->Fd());
CompileProfileOdex(dex_location,
odex_location,
app_image_file_name,
/*use_fd=*/true,
/*num_profile_classes=*/1,
{input_vdex, output_vdex});
EXPECT_GT(vdex_file1->GetLength(), 0u);
}
{
// Test that vdex and dexlayout fail gracefully.
std::string input_vdex = StringPrintf("--input-vdex-fd=%d", vdex_file1->Fd());
std::string output_vdex = StringPrintf("--output-vdex-fd=%d", vdex_file2.GetFd());
CompileProfileOdex(dex_location,
odex_location,
app_image_file_name,
/*use_fd=*/true,
/*num_profile_classes=*/1,
{input_vdex, output_vdex},
/*expect_success=*/true);
EXPECT_GT(vdex_file2.GetFile()->GetLength(), 0u);
}
ASSERT_EQ(vdex_file1->FlushCloseOrErase(), 0) << "Could not flush and close vdex file";
CheckValidity();
}
void CheckResult(const std::string& dex_location,
const std::string& odex_location,
const std::string& app_image_file_name) {
// Host/target independent checks.
std::string error_msg;
std::unique_ptr<OatFile> odex_file(OatFile::Open(/*zip_fd=*/-1,
odex_location,
odex_location,
/*executable=*/false,
/*low_4gb=*/false,
dex_location,
&error_msg));
ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
const char* location = dex_location.c_str();
std::vector<std::unique_ptr<const DexFile>> dex_files;
ArtDexFileLoader dex_file_loader(location);
ASSERT_TRUE(dex_file_loader.Open(
/*verify=*/true, /*verify_checksum=*/true, &error_msg, &dex_files));
EXPECT_EQ(dex_files.size(), 1U);
std::unique_ptr<const DexFile>& old_dex_file = dex_files[0];
for (const OatDexFile* oat_dex_file : odex_file->GetOatDexFiles()) {
std::unique_ptr<const DexFile> new_dex_file = oat_dex_file->OpenDexFile(&error_msg);
ASSERT_TRUE(new_dex_file != nullptr);
uint32_t class_def_count = new_dex_file->NumClassDefs();
ASSERT_LT(class_def_count, std::numeric_limits<uint16_t>::max());
ASSERT_GE(class_def_count, 2U);
// Make sure the indexes stay the same.
std::string old_class0 = old_dex_file->PrettyType(old_dex_file->GetClassDef(0).class_idx_);
std::string old_class1 = old_dex_file->PrettyType(old_dex_file->GetClassDef(1).class_idx_);
std::string new_class0 = new_dex_file->PrettyType(new_dex_file->GetClassDef(0).class_idx_);
std::string new_class1 = new_dex_file->PrettyType(new_dex_file->GetClassDef(1).class_idx_);
EXPECT_EQ(old_class0, new_class0);
EXPECT_EQ(old_class1, new_class1);
}
EXPECT_EQ(odex_file->GetCompilerFilter(), CompilerFilter::kSpeedProfile);
if (!app_image_file_name.empty()) {
// Go peek at the image header to make sure it was large enough to contain the class.
std::unique_ptr<File> file(OS::OpenFileForReading(app_image_file_name.c_str()));
ImageHeader image_header;
bool success = file->ReadFully(&image_header, sizeof(image_header));
ASSERT_TRUE(success);
ASSERT_TRUE(image_header.IsValid());
EXPECT_GT(image_header.GetObjectsSection().Size(), 0u);
}
}
// Check whether the dex2oat run was really successful.
void CheckValidity() {
if (kIsTargetBuild) {
CheckTargetValidity();
} else {
CheckHostValidity();
}
}
void CheckTargetValidity() {
// TODO: Ignore for now.
}
// On the host, we can get the dex2oat output. Here, look for "dex2oat took."
void CheckHostValidity() {
EXPECT_NE(output_.find("dex2oat took"), std::string::npos) << output_;
}
};
TEST_F(Dex2oatLayoutTest, TestLayout) { RunTest(/*app_image=*/false); }
TEST_F(Dex2oatLayoutTest, TestLayoutAppImage) { RunTest(/*app_image=*/true); }
TEST_F(Dex2oatLayoutTest, TestLayoutAppImageMissingBootImage) {
std::string dex_location = GetScratchDir() + "/DexNoOat.jar";
std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex";
std::string app_image_file = GetOdexDir() + "/DexOdexNoOat.art";
Copy(GetDexSrc2(), dex_location);
CompileProfileOdex(dex_location,
odex_location,
app_image_file,
/*use_fd=*/false,
/*num_profile_classes=*/1,
/*extra_args=*/{"--boot-image=/nonx/boot.art"},
/*expect_success=*/true);
// Verify the odex file does not require an image.
std::string error_msg;
std::unique_ptr<OatFile> odex_file(OatFile::Open(/*zip_fd=*/-1,
odex_location,
odex_location,
/*executable=*/false,
/*low_4gb=*/false,
dex_location,
&error_msg));
ASSERT_TRUE(odex_file != nullptr) << "Could not open odex file: " << error_msg;
CheckFilter(CompilerFilter::kSpeedProfile, odex_file->GetCompilerFilter());
ASSERT_FALSE(odex_file->GetOatHeader().RequiresImage());
}
TEST_F(Dex2oatLayoutTest, TestLayoutMultipleProfiles) {
std::string dex_location = GetScratchDir() + "/Dex.jar";
std::string odex_location = GetOdexDir() + "/Dex.odex";
std::string app_image_file = GetOdexDir() + "/Dex.art";
Copy(GetDexSrc2(), dex_location);
const std::string profile1_location = GetScratchDir() + "/primary.prof";
GenerateProfile(profile1_location, dex_location, /*num_classes=*/1, /*class_offset=*/0);
CompileProfileOdex(dex_location,
odex_location,
app_image_file,
/*use_fd=*/false,
{profile1_location});
uint32_t image_file_size_profile1 = GetImageObjectSectionSize(app_image_file);
const std::string profile2_location = GetScratchDir() + "/secondary.prof";
GenerateProfile(profile2_location, dex_location, /*num_classes=*/1, /*class_offset=*/1);
CompileProfileOdex(dex_location,
odex_location,
app_image_file,
/*use_fd=*/false,
{profile2_location});
uint32_t image_file_size_profile2 = GetImageObjectSectionSize(app_image_file);
CompileProfileOdex(dex_location,
odex_location,
app_image_file,
/*use_fd=*/false,
{profile1_location, profile2_location});
uint32_t image_file_size_multiple_profiles = GetImageObjectSectionSize(app_image_file);
CheckCompilerFilter(dex_location, odex_location, CompilerFilter::Filter::kSpeedProfile);
// The image file generated with multiple profiles should be larger than any image file generated
// with each profile.
ASSERT_GT(image_file_size_multiple_profiles, image_file_size_profile1);
ASSERT_GT(image_file_size_multiple_profiles, image_file_size_profile2);
}
TEST_F(Dex2oatLayoutTest, TestLayoutMultipleProfilesChecksumMismatch) {
std::string dex_location = GetScratchDir() + "/Dex.jar";
// Create two profiles whose dex locations are the same but checksums are different.
Copy(GetDexSrc1(), dex_location);
const std::string profile_old = GetScratchDir() + "/profile_old.prof";
GenerateProfile(profile_old, dex_location, /*num_classes=*/1, /*class_offset=*/0);
Copy(GetDexSrc2(), dex_location);
const std::string profile_new = GetScratchDir() + "/profile_new.prof";
GenerateProfile(profile_new, dex_location, /*num_classes=*/1, /*class_offset=*/0);
// Create an empty profile for reference.
const std::string profile_empty = GetScratchDir() + "/profile_empty.prof";
GenerateProfile(profile_empty, dex_location, /*num_classes=*/0, /*class_offset=*/0);
std::string odex_location = GetOdexDir() + "/Dex.odex";
std::string app_image_file = GetOdexDir() + "/Dex.art";
// This should produce a normal image because only `profile_new` is used and it has the right
// checksum.
CompileProfileOdex(dex_location,
odex_location,
app_image_file,
/*use_fd=*/false,
{profile_new, profile_old});
uint32_t image_size_right_checksum = GetImageObjectSectionSize(app_image_file);
// This should produce an empty image because only `profile_old` is used and it has the wrong
// checksum. Note that dex2oat does not abort compilation when the profile verification fails
// (b/62602192, b/65260586).
CompileProfileOdex(dex_location,
odex_location,
app_image_file,
/*use_fd=*/false,
{profile_old, profile_new});
uint32_t image_size_wrong_checksum = GetImageObjectSectionSize(app_image_file);
// Create an empty image using an empty profile for reference.
CompileProfileOdex(dex_location,
odex_location,
app_image_file,
/*use_fd=*/false,
{profile_empty});
uint32_t image_size_empty = GetImageObjectSectionSize(app_image_file);
EXPECT_GT(image_size_right_checksum, image_size_empty);
EXPECT_EQ(image_size_wrong_checksum, image_size_empty);
}
TEST_F(Dex2oatLayoutTest, TestVdexLayout) { RunTestVDex(); }
class Dex2oatWatchdogTest : public Dex2oatTest {
protected:
void RunTest(bool expect_success, const std::vector<std::string>& extra_args = {}) {
std::string dex_location = GetScratchDir() + "/Dex2OatSwapTest.jar";
std::string odex_location = GetOdexDir() + "/Dex2OatSwapTest.odex";
Copy(GetTestDexFileName(), dex_location);
std::vector<std::string> copy(extra_args);
std::string swap_location = GetOdexDir() + "/Dex2OatSwapTest.odex.swap";
copy.push_back("--swap-file=" + swap_location);
copy.push_back("-j512"); // Excessive idle threads just slow down dex2oat.
ASSERT_TRUE(GenerateOdexForTest(
dex_location, odex_location, CompilerFilter::kSpeed, copy, expect_success));
}
std::string GetTestDexFileName() { return GetDexSrc1(); }
};
TEST_F(Dex2oatWatchdogTest, TestWatchdogOK) {
// Check with default.
RunTest(true);
// Check with ten minutes.
RunTest(true, {"--watchdog-timeout=600000"});
}