• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef ART_RUNTIME_DEX2OAT_ENVIRONMENT_TEST_H_
18 #define ART_RUNTIME_DEX2OAT_ENVIRONMENT_TEST_H_
19 
20 #include <fstream>
21 #include <string>
22 #include <vector>
23 
24 #include "base/file_utils.h"
25 #include "base/os.h"
26 #include "base/stl_util.h"
27 #include "base/utils.h"
28 #include "common_runtime_test.h"
29 #include "compiler_callbacks.h"
30 #include "dex/art_dex_file_loader.h"
31 #include "dex/dex_file_loader.h"
32 #include "exec_utils.h"
33 #include "gc/heap.h"
34 #include "gc/space/image_space.h"
35 #include "gtest/gtest.h"
36 #include "oat_file_assistant.h"
37 #include "runtime.h"
38 #include "ziparchive/zip_writer.h"
39 
40 namespace art {
41 
42 static constexpr bool kDebugArgs = false;
43 
44 class Dex2oatScratchDirs {
45  public:
SetUp(const std::string & android_data)46   void SetUp(const std::string& android_data) {
47     // Create a scratch directory to work from.
48 
49     // Get the realpath of the android data. The oat dir should always point to real location
50     // when generating oat files in dalvik-cache. This avoids complicating the unit tests
51     // when matching the expected paths.
52     UniqueCPtr<const char[]> android_data_real(realpath(android_data.c_str(), nullptr));
53     ASSERT_TRUE(android_data_real != nullptr)
54         << "Could not get the realpath of the android data" << android_data << strerror(errno);
55 
56     scratch_dir_.assign(android_data_real.get());
57     scratch_dir_ += "/Dex2oatEnvironmentTest";
58     ASSERT_EQ(0, mkdir(scratch_dir_.c_str(), 0700));
59 
60     // Create a subdirectory in scratch for odex files.
61     odex_oat_dir_ = scratch_dir_ + "/oat";
62     ASSERT_EQ(0, mkdir(odex_oat_dir_.c_str(), 0700));
63 
64     odex_dir_ = odex_oat_dir_ + "/" + std::string(GetInstructionSetString(kRuntimeISA));
65     ASSERT_EQ(0, mkdir(odex_dir_.c_str(), 0700));
66   }
67 
TearDown()68   void TearDown() {
69     CommonArtTest::ClearDirectory(odex_dir_.c_str());
70     ASSERT_EQ(0, rmdir(odex_dir_.c_str()));
71 
72     CommonArtTest::ClearDirectory(odex_oat_dir_.c_str());
73     ASSERT_EQ(0, rmdir(odex_oat_dir_.c_str()));
74 
75     CommonArtTest::ClearDirectory(scratch_dir_.c_str());
76     ASSERT_EQ(0, rmdir(scratch_dir_.c_str()));
77   }
78 
79   // Scratch directory, for dex and odex files (oat files will go in the
80   // dalvik cache).
GetScratchDir()81   const std::string& GetScratchDir() const { return scratch_dir_; }
82 
83   // Odex directory is the subdirectory in the scratch directory where odex
84   // files should be located.
GetOdexDir()85   const std::string& GetOdexDir() const { return odex_dir_; }
86 
87  private:
88   std::string scratch_dir_;
89   std::string odex_oat_dir_;
90   std::string odex_dir_;
91 };
92 
93 // Test class that provides some helpers to set a test up for compilation using dex2oat.
94 class Dex2oatEnvironmentTest : public Dex2oatScratchDirs, public CommonRuntimeTest {
95  public:
SetUp()96   void SetUp() override {
97     CommonRuntimeTest::SetUp();
98     Dex2oatScratchDirs::SetUp(android_data_);
99 
100     // Verify the environment is as we expect
101     std::vector<uint32_t> checksums;
102     std::vector<std::string> dex_locations;
103     std::string error_msg;
104     ASSERT_TRUE(OS::FileExists(GetSystemImageFile().c_str()))
105       << "Expected pre-compiled boot image to be at: " << GetSystemImageFile();
106     ASSERT_TRUE(OS::FileExists(GetDexSrc1().c_str()))
107       << "Expected dex file to be at: " << GetDexSrc1();
108     ASSERT_TRUE(OS::FileExists(GetResourceOnlySrc1().c_str()))
109       << "Expected stripped dex file to be at: " << GetResourceOnlySrc1();
110     ASSERT_TRUE(ArtDexFileLoader::GetMultiDexChecksums(
111         GetResourceOnlySrc1().c_str(), &checksums, &dex_locations, &error_msg))
112         << "Expected stripped dex file to be stripped: " << GetResourceOnlySrc1();
113     ASSERT_TRUE(OS::FileExists(GetDexSrc2().c_str()))
114       << "Expected dex file to be at: " << GetDexSrc2();
115 
116     // GetMultiDexSrc2 should have the same primary dex checksum as
117     // GetMultiDexSrc1, but a different secondary dex checksum.
118     static constexpr bool kVerifyChecksum = true;
119     std::vector<std::unique_ptr<const DexFile>> multi1;
120     ArtDexFileLoader dex_file_loader1(GetMultiDexSrc1());
121     ASSERT_TRUE(dex_file_loader1.Open(/* verify= */ true, kVerifyChecksum, &error_msg, &multi1))
122         << error_msg;
123     ASSERT_GT(multi1.size(), 1u);
124 
125     std::vector<std::unique_ptr<const DexFile>> multi2;
126     ArtDexFileLoader dex_file_loader2(GetMultiDexSrc2());
127     ASSERT_TRUE(dex_file_loader2.Open(/* verify= */ true, kVerifyChecksum, &error_msg, &multi2))
128         << error_msg;
129     ASSERT_GT(multi2.size(), 1u);
130 
131     ASSERT_EQ(multi1[0]->GetLocationChecksum(), multi2[0]->GetLocationChecksum());
132     ASSERT_NE(multi1[1]->GetLocationChecksum(), multi2[1]->GetLocationChecksum());
133   }
134 
SetUpRuntimeOptions(RuntimeOptions * options)135   void SetUpRuntimeOptions(RuntimeOptions* options) override {
136     // options->push_back(std::make_pair("-verbose:oat", nullptr));
137 
138     // Set up the image location.
139     options->push_back(std::make_pair("-Ximage:" + GetImageLocation(),
140           nullptr));
141     // Make sure compilercallbacks are not set so that relocation will be
142     // enabled.
143     callbacks_.reset();
144   }
145 
TearDown()146   void TearDown() override {
147     Dex2oatScratchDirs::TearDown();
148     CommonRuntimeTest::TearDown();
149   }
150 
Copy(const std::string & src,const std::string & dst)151   static void Copy(const std::string& src, const std::string& dst) {
152     std::ifstream  src_stream(src, std::ios::binary);
153     std::ofstream  dst_stream(dst, std::ios::binary);
154 
155     dst_stream << src_stream.rdbuf();
156   }
157 
GetDexSrc1()158   std::string GetDexSrc1() const {
159     return GetTestDexFileName("Main");
160   }
161 
162   // Returns the path to a dex file equivalent to GetDexSrc1, but with the dex
163   // file stripped.
GetResourceOnlySrc1()164   std::string GetResourceOnlySrc1() const {
165     return GetTestDexFileName("MainStripped");
166   }
167 
GetMultiDexSrc1()168   std::string GetMultiDexSrc1() const {
169     return GetTestDexFileName("MultiDex");
170   }
171 
GetMultiDexUncompressedAlignedSrc1()172   std::string GetMultiDexUncompressedAlignedSrc1() const {
173     return GetTestDexFileName("MultiDexUncompressedAligned");
174   }
175 
176   // Returns the path to a multidex file equivalent to GetMultiDexSrc2, but
177   // with the contents of the secondary dex file changed.
GetMultiDexSrc2()178   std::string GetMultiDexSrc2() const {
179     return GetTestDexFileName("MultiDexModifiedSecondary");
180   }
181 
GetDexSrc2()182   std::string GetDexSrc2() const {
183     return GetTestDexFileName("Nested");
184   }
185 
Dex2Oat(const std::vector<std::string> & dex2oat_args,std::string * output,std::string * error_msg)186   int Dex2Oat(const std::vector<std::string>& dex2oat_args,
187               std::string* output,
188               std::string* error_msg) {
189     std::vector<std::string> argv;
190     if (!CommonRuntimeTest::StartDex2OatCommandLine(&argv, error_msg)) {
191       ::testing::AssertionFailure() << "Could not start dex2oat cmd line " << *error_msg;
192     }
193 
194     Runtime* runtime = Runtime::Current();
195     if (!runtime->IsVerificationEnabled()) {
196       argv.push_back("--compiler-filter=assume-verified");
197     }
198 
199     if (runtime->MustRelocateIfPossible()) {
200       argv.push_back("--runtime-arg");
201       argv.push_back("-Xrelocate");
202     } else {
203       argv.push_back("--runtime-arg");
204       argv.push_back("-Xnorelocate");
205     }
206 
207     if (!kIsTargetBuild) {
208       argv.push_back("--host");
209     }
210 
211     argv.insert(argv.end(), dex2oat_args.begin(), dex2oat_args.end());
212 
213     // We must set --android-root.
214     const char* android_root = getenv("ANDROID_ROOT");
215     CHECK(android_root != nullptr);
216     argv.push_back("--android-root=" + std::string(android_root));
217 
218     if (kDebugArgs) {
219       std::string all_args;
220       for (const std::string& arg : argv) {
221         all_args += arg + " ";
222       }
223       LOG(ERROR) << all_args;
224     }
225 
226     // We need dex2oat to actually log things.
227     auto post_fork_fn = []() { return setenv("ANDROID_LOG_TAGS", "*:d", 1) == 0; };
228     ForkAndExecResult res = ForkAndExec(argv, post_fork_fn, output);
229     if (res.stage != ForkAndExecResult::kFinished) {
230       *error_msg = strerror(errno);
231       ::testing::AssertionFailure() << "Failed to finish dex2oat invocation: " << *error_msg;
232     }
233 
234     if (!res.StandardSuccess()) {
235       // We cannot use ASSERT_TRUE since the method returns an int and not void.
236       ::testing::AssertionFailure() << "dex2oat fork/exec failed: " << *error_msg;
237     }
238 
239     return res.status_code;
240   }
241 
CreateDexMetadata(const std::string & vdex,const std::string & out_dm)242   void CreateDexMetadata(const std::string& vdex, const std::string& out_dm) {
243     // Read the vdex bytes.
244     std::unique_ptr<File> vdex_file(OS::OpenFileForReading(vdex.c_str()));
245     std::vector<uint8_t> data(vdex_file->GetLength());
246     ASSERT_TRUE(vdex_file->ReadFully(data.data(), data.size()));
247 
248     // Zip the content.
249     FILE* file = fopen(out_dm.c_str(), "wbe");
250     ZipWriter writer(file);
251     writer.StartEntry("primary.vdex", ZipWriter::kAlign32);
252     writer.WriteBytes(data.data(), data.size());
253     writer.FinishEntry();
254     writer.Finish();
255     fflush(file);
256     fclose(file);
257   }
258 };
259 
260 }  // namespace art
261 
262 #endif  // ART_RUNTIME_DEX2OAT_ENVIRONMENT_TEST_H_
263