1 /*
2  * Copyright (C) 2018 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 "oatdump_test.h"
18 
19 namespace art {
20 
21 // Oat file compiled with a boot image. oatdump invoked with a boot image.
TEST_P(OatDumpTest,TestDumpOatWithRuntimeWithBootImage)22 TEST_P(OatDumpTest, TestDumpOatWithRuntimeWithBootImage) {
23   ASSERT_TRUE(GenerateAppOdexFile(GetParam()));
24   ASSERT_TRUE(Exec(GetParam(),
25                    kArgOatApp | kArgBootImage | kArgBcp | kArgIsa,
26                    {},
27                    kExpectOat | kExpectCode | kExpectBssMappingsForBcp));
28 }
29 
30 // Oat file compiled without a boot image. oatdump invoked without a boot image.
TEST_P(OatDumpTest,TestDumpOatWithRuntimeWithNoBootImage)31 TEST_P(OatDumpTest, TestDumpOatWithRuntimeWithNoBootImage) {
32   TEST_DISABLED_FOR_DEBUG_BUILD();  // DCHECK failed.
33   ASSERT_TRUE(GenerateAppOdexFile(GetParam(), {"--boot-image=/nonx/boot.art"}));
34   ASSERT_TRUE(Exec(GetParam(),
35                    kArgOatApp | kArgBcp | kArgIsa,
36                    {"--boot-image=/nonx/boot.art"},
37                    kExpectOat | kExpectCode | kExpectBssMappingsForBcp));
38 }
39 
40 // Dex code cannot be found in the vdex file, and no --dex-file is specified. Dump header only.
TEST_P(OatDumpTest,TestDumpOatTryWithRuntimeDexNotFound)41 TEST_P(OatDumpTest, TestDumpOatTryWithRuntimeDexNotFound) {
42   ASSERT_TRUE(
43       GenerateAppOdexFile(GetParam(), {"--dex-location=/nonx/app.jar", "--copy-dex-files=false"}));
44   ASSERT_TRUE(Exec(GetParam(), kArgOatApp | kArgBootImage | kArgBcp | kArgIsa, {}, kExpectOat));
45 }
46 
47 // Dex code cannot be found in the vdex file, but can be found in the specified dex file.
TEST_P(OatDumpTest,TestDumpOatWithRuntimeDexSpecified)48 TEST_P(OatDumpTest, TestDumpOatWithRuntimeDexSpecified) {
49   ASSERT_TRUE(
50       GenerateAppOdexFile(GetParam(), {"--dex-location=/nonx/app.jar", "--copy-dex-files=false"}));
51   ASSERT_TRUE(Exec(GetParam(),
52                    kArgOatApp | kArgDexApp | kArgBootImage | kArgBcp | kArgIsa,
53                    {},
54                    kExpectOat | kExpectCode | kExpectBssMappingsForBcp));
55 }
56 
57 // Oat file compiled with a boot image. oatdump invoked without a boot image.
TEST_P(OatDumpTest,TestDumpOatWithoutRuntimeBcpMismatch)58 TEST_P(OatDumpTest, TestDumpOatWithoutRuntimeBcpMismatch) {
59   ASSERT_TRUE(GenerateAppOdexFile(GetParam()));
60   ASSERT_TRUE(Exec(GetParam(),
61                    kArgOatApp | kArgBcp | kArgIsa,
62                    {"--boot-image=/nonx/boot.art"},
63                    kExpectOat | kExpectCode | kExpectBssOffsetsForBcp));
64 }
65 
66 // Bootclasspath not specified.
TEST_P(OatDumpTest,TestDumpOatWithoutRuntimeNoBcp)67 TEST_P(OatDumpTest, TestDumpOatWithoutRuntimeNoBcp) {
68   ASSERT_TRUE(GenerateAppOdexFile(GetParam()));
69   ASSERT_TRUE(Exec(GetParam(), kArgOatApp, {}, kExpectOat | kExpectCode | kExpectBssOffsetsForBcp));
70 }
71 
72 // Dex code cannot be found in the vdex file, and no --dex-file is specified. Dump header only.
TEST_P(OatDumpTest,TestDumpOatWithoutRuntimeDexNotFound)73 TEST_P(OatDumpTest, TestDumpOatWithoutRuntimeDexNotFound) {
74   ASSERT_TRUE(
75       GenerateAppOdexFile(GetParam(), {"--dex-location=/nonx/app.jar", "--copy-dex-files=false"}));
76   ASSERT_TRUE(Exec(GetParam(), kArgOatApp, {}, kExpectOat));
77 }
78 
79 // Dex code cannot be found in the vdex file, but can be found in the specified dex file.
TEST_P(OatDumpTest,TestDumpOatWithoutRuntimeDexSpecified)80 TEST_P(OatDumpTest, TestDumpOatWithoutRuntimeDexSpecified) {
81   ASSERT_TRUE(
82       GenerateAppOdexFile(GetParam(), {"--dex-location=/nonx/app.jar", "--copy-dex-files=false"}));
83   ASSERT_TRUE(Exec(
84       GetParam(), kArgOatApp | kArgDexApp, {}, kExpectOat | kExpectCode | kExpectBssOffsetsForBcp));
85 }
86 
TEST_P(OatDumpTest,TestDumpAppImageWithBootImage)87 TEST_P(OatDumpTest, TestDumpAppImageWithBootImage) {
88   TEST_DISABLED_WITHOUT_BAKER_READ_BARRIERS();  // GC bug, b/126305867
89   const std::string app_image_arg = "--app-image-file=" + GetAppImageName();
90   ASSERT_TRUE(GenerateAppOdexFile(GetParam(), {app_image_arg}));
91   ASSERT_TRUE(Exec(GetParam(),
92                    kArgAppImage | kArgOatApp | kArgBootImage | kArgBcp | kArgIsa,
93                    {},
94                    kExpectImage | kExpectOat | kExpectCode | kExpectBssMappingsForBcp));
95 }
96 
97 // Deprecated usage, but checked for compatibility.
TEST_P(OatDumpTest,TestDumpAppImageWithBootImageLegacy)98 TEST_P(OatDumpTest, TestDumpAppImageWithBootImageLegacy) {
99   TEST_DISABLED_WITHOUT_BAKER_READ_BARRIERS();  // GC bug, b/126305867
100   const std::string app_image_arg = "--app-image-file=" + GetAppImageName();
101   ASSERT_TRUE(GenerateAppOdexFile(GetParam(), {app_image_arg}));
102   ASSERT_TRUE(Exec(GetParam(),
103                    kArgAppImage | kArgImage | kArgBcp | kArgIsa,
104                    {"--app-oat=" + GetAppOdexName()},
105                    kExpectImage | kExpectOat | kExpectCode | kExpectBssMappingsForBcp));
106 }
107 
TEST_P(OatDumpTest,TestDumpAppImageInvalidPath)108 TEST_P(OatDumpTest, TestDumpAppImageInvalidPath) {
109   TEST_DISABLED_WITHOUT_BAKER_READ_BARRIERS();  // GC bug, b/126305867
110   const std::string app_image_arg = "--app-image-file=" + GetAppImageName();
111   ASSERT_TRUE(GenerateAppOdexFile(GetParam(), {app_image_arg}));
112   ASSERT_TRUE(Exec(GetParam(),
113                    kArgOatApp | kArgBootImage | kArgBcp | kArgIsa,
114                    {"--app-image=missing_app_image.art"},
115                    /*expects=*/0,
116                    /*expect_failure=*/true));
117 }
118 
119 // The runtime can start, but the boot image check should fail.
TEST_P(OatDumpTest,TestDumpAppImageWithWrongBootImage)120 TEST_P(OatDumpTest, TestDumpAppImageWithWrongBootImage) {
121   TEST_DISABLED_WITHOUT_BAKER_READ_BARRIERS();  // GC bug, b/126305867
122   const std::string app_image_arg = "--app-image-file=" + GetAppImageName();
123   ASSERT_TRUE(GenerateAppOdexFile(GetParam(), {app_image_arg}));
124   ASSERT_TRUE(Exec(GetParam(),
125                    kArgAppImage | kArgOatApp | kArgBcp | kArgIsa,
126                    {"--boot-image=/nonx/boot.art"},
127                    /*expects=*/0,
128                    /*expect_failure=*/true));
129 }
130 
131 // Not possible.
TEST_P(OatDumpTest,TestDumpAppImageWithoutRuntime)132 TEST_P(OatDumpTest, TestDumpAppImageWithoutRuntime) {
133   TEST_DISABLED_WITHOUT_BAKER_READ_BARRIERS();  // GC bug, b/126305867
134   const std::string app_image_arg = "--app-image-file=" + GetAppImageName();
135   ASSERT_TRUE(GenerateAppOdexFile(GetParam(), {app_image_arg}));
136   ASSERT_TRUE(Exec(GetParam(),
137                    kArgAppImage | kArgOatApp,
138                    {},
139                    /*expects=*/0,
140                    /*expect_failure=*/true));
141 }
142 
143 // Dex code cannot be found in the vdex file, and no --dex-file is specified. Cannot dump app image.
TEST_P(OatDumpTest,TestDumpAppImageDexNotFound)144 TEST_P(OatDumpTest, TestDumpAppImageDexNotFound) {
145   TEST_DISABLED_WITHOUT_BAKER_READ_BARRIERS();  // GC bug, b/126305867
146   const std::string app_image_arg = "--app-image-file=" + GetAppImageName();
147   ASSERT_TRUE(GenerateAppOdexFile(
148       GetParam(), {app_image_arg, "--dex-location=/nonx/app.jar", "--copy-dex-files=false"}));
149   ASSERT_TRUE(Exec(GetParam(),
150                    kArgAppImage | kArgOatApp | kArgBootImage | kArgBcp | kArgIsa,
151                    {},
152                    /*expects=*/0,
153                    /*expect_failure=*/true));
154 }
155 
156 // Dex code cannot be found in the vdex file, but can be found in the specified dex file.
TEST_P(OatDumpTest,TestDumpAppImageDexSpecified)157 TEST_P(OatDumpTest, TestDumpAppImageDexSpecified) {
158   TEST_DISABLED_WITHOUT_BAKER_READ_BARRIERS();  // GC bug, b/126305867
159   const std::string app_image_arg = "--app-image-file=" + GetAppImageName();
160   ASSERT_TRUE(GenerateAppOdexFile(
161       GetParam(), {app_image_arg, "--dex-location=/nonx/app.jar", "--copy-dex-files=false"}));
162   ASSERT_TRUE(Exec(GetParam(),
163                    kArgAppImage | kArgOatApp | kArgDexApp | kArgBootImage | kArgBcp | kArgIsa,
164                    {},
165                    kExpectImage | kExpectOat | kExpectCode | kExpectBssMappingsForBcp));
166 }
167 
168 }  // namespace art
169