1 /*
2  * Copyright (C) 2017 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 <gtest/gtest.h>
18 
19 #include "android-base/stringprintf.h"
20 
21 #include "dexopt_test.h"
22 #include "noop_compiler_callbacks.h"
23 
24 namespace art {
25 namespace gc {
26 namespace space {
27 
TEST_F(DexoptTest,ValidateOatFile)28 TEST_F(DexoptTest, ValidateOatFile) {
29   std::string dex1 = GetScratchDir() + "/Dex1.jar";
30   std::string multidex1 = GetScratchDir() + "/MultiDex1.jar";
31   std::string dex2 = GetScratchDir() + "/Dex2.jar";
32   std::string oat_location = GetScratchDir() + "/Oat.oat";
33 
34   Copy(GetDexSrc1(), dex1);
35   Copy(GetMultiDexSrc1(), multidex1);
36   Copy(GetDexSrc2(), dex2);
37 
38   std::string error_msg;
39   std::vector<std::string> args;
40   args.push_back("--dex-file=" + dex1);
41   args.push_back("--dex-file=" + multidex1);
42   args.push_back("--dex-file=" + dex2);
43   args.push_back("--oat-file=" + oat_location);
44   ASSERT_TRUE(OatFileAssistant::Dex2Oat(args, &error_msg)) << error_msg;
45 
46   std::unique_ptr<OatFile> oat(OatFile::Open(/* zip_fd */ -1,
47                                              oat_location.c_str(),
48                                              oat_location.c_str(),
49                                              nullptr,
50                                              nullptr,
51                                              false,
52                                              /*low_4gb*/false,
53                                              nullptr,
54                                              &error_msg));
55   ASSERT_TRUE(oat != nullptr) << error_msg;
56 
57   // Originally all the dex checksums should be up to date.
58   EXPECT_TRUE(ImageSpace::ValidateOatFile(*oat, &error_msg)) << error_msg;
59 
60   // Invalidate the dex1 checksum.
61   Copy(GetDexSrc2(), dex1);
62   EXPECT_FALSE(ImageSpace::ValidateOatFile(*oat, &error_msg));
63 
64   // Restore the dex1 checksum.
65   Copy(GetDexSrc1(), dex1);
66   EXPECT_TRUE(ImageSpace::ValidateOatFile(*oat, &error_msg)) << error_msg;
67 
68   // Invalidate the non-main multidex checksum.
69   Copy(GetMultiDexSrc2(), multidex1);
70   EXPECT_FALSE(ImageSpace::ValidateOatFile(*oat, &error_msg));
71 
72   // Restore the multidex checksum.
73   Copy(GetMultiDexSrc1(), multidex1);
74   EXPECT_TRUE(ImageSpace::ValidateOatFile(*oat, &error_msg)) << error_msg;
75 
76   // Invalidate the dex2 checksum.
77   Copy(GetDexSrc1(), dex2);
78   EXPECT_FALSE(ImageSpace::ValidateOatFile(*oat, &error_msg));
79 
80   // restore the dex2 checksum.
81   Copy(GetDexSrc2(), dex2);
82   EXPECT_TRUE(ImageSpace::ValidateOatFile(*oat, &error_msg)) << error_msg;
83 
84   // Replace the multidex file with a non-multidex file.
85   Copy(GetDexSrc1(), multidex1);
86   EXPECT_FALSE(ImageSpace::ValidateOatFile(*oat, &error_msg));
87 
88   // Restore the multidex file
89   Copy(GetMultiDexSrc1(), multidex1);
90   EXPECT_TRUE(ImageSpace::ValidateOatFile(*oat, &error_msg)) << error_msg;
91 
92   // Replace dex1 with a multidex file.
93   Copy(GetMultiDexSrc1(), dex1);
94   EXPECT_FALSE(ImageSpace::ValidateOatFile(*oat, &error_msg));
95 
96   // Restore the dex1 file.
97   Copy(GetDexSrc1(), dex1);
98   EXPECT_TRUE(ImageSpace::ValidateOatFile(*oat, &error_msg)) << error_msg;
99 
100   // Remove the dex2 file.
101   EXPECT_EQ(0, unlink(dex2.c_str()));
102   EXPECT_FALSE(ImageSpace::ValidateOatFile(*oat, &error_msg));
103 
104   // Restore the dex2 file.
105   Copy(GetDexSrc2(), dex2);
106   EXPECT_TRUE(ImageSpace::ValidateOatFile(*oat, &error_msg)) << error_msg;
107 
108   // Remove the multidex file.
109   EXPECT_EQ(0, unlink(multidex1.c_str()));
110   EXPECT_FALSE(ImageSpace::ValidateOatFile(*oat, &error_msg));
111 }
112 
113 template <bool kImage, bool kRelocate, bool kPatchoat, bool kImageDex2oat>
114 class ImageSpaceLoadingTest : public CommonRuntimeTest {
115  protected:
SetUpRuntimeOptions(RuntimeOptions * options)116   void SetUpRuntimeOptions(RuntimeOptions* options) OVERRIDE {
117     if (kImage) {
118       options->emplace_back(android::base::StringPrintf("-Ximage:%s", GetCoreArtLocation().c_str()),
119                             nullptr);
120     }
121     options->emplace_back(kRelocate ? "-Xrelocate" : "-Xnorelocate", nullptr);
122     if (!kPatchoat) {
123       options->emplace_back("-Xpatchoat:false", nullptr);
124     }
125     options->emplace_back(kImageDex2oat ? "-Ximage-dex2oat" : "-Xnoimage-dex2oat", nullptr);
126 
127     // We want to test the relocation behavior of ImageSpace. As such, don't pretend we're a
128     // compiler.
129     callbacks_.reset();
130   }
131 };
132 
133 using ImageSpacePatchoatTest = ImageSpaceLoadingTest<true, true, true, true>;
TEST_F(ImageSpacePatchoatTest,Test)134 TEST_F(ImageSpacePatchoatTest, Test) {
135   EXPECT_FALSE(Runtime::Current()->GetHeap()->GetBootImageSpaces().empty());
136 }
137 
138 using ImageSpaceDex2oatTest = ImageSpaceLoadingTest<false, true, false, true>;
TEST_F(ImageSpaceDex2oatTest,Test)139 TEST_F(ImageSpaceDex2oatTest, Test) {
140   EXPECT_FALSE(Runtime::Current()->GetHeap()->GetBootImageSpaces().empty());
141 }
142 
143 using ImageSpaceNoDex2oatNoPatchoatTest = ImageSpaceLoadingTest<true, true, false, false>;
TEST_F(ImageSpaceNoDex2oatNoPatchoatTest,Test)144 TEST_F(ImageSpaceNoDex2oatNoPatchoatTest, Test) {
145   EXPECT_TRUE(Runtime::Current()->GetHeap()->GetBootImageSpaces().empty());
146 }
147 
148 using ImageSpaceNoRelocateNoDex2oatNoPatchoatTest = ImageSpaceLoadingTest<true, false, false, false>;
TEST_F(ImageSpaceNoRelocateNoDex2oatNoPatchoatTest,Test)149 TEST_F(ImageSpaceNoRelocateNoDex2oatNoPatchoatTest, Test) {
150   EXPECT_FALSE(Runtime::Current()->GetHeap()->GetBootImageSpaces().empty());
151 }
152 
153 }  // namespace space
154 }  // namespace gc
155 }  // namespace art
156