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